From 938335e6c54558794c8cf9ab857806b9d86a2f44 Mon Sep 17 00:00:00 2001
From: Tobias Baden <tobias.baden@stud.hs-hannover.de>
Date: Wed, 29 Aug 2018 19:27:35 +0200
Subject: [PATCH] Fixed the search function - Search is now an "OR"-Search --
 Finding Courses containing Keyword1 OR Keyword2 - Updated Version

---
 amd/build/jmodule.min.js |  2 +-
 amd/src/jmodule.js       | 94 +++++++++++++++++++++-------------------
 block_hshcourselist.php  |  2 +-
 javascript/listselect.js |  3 +-
 version.php              |  2 +-
 5 files changed, 54 insertions(+), 49 deletions(-)

diff --git a/amd/build/jmodule.min.js b/amd/build/jmodule.min.js
index aa1cf37..73c4854 100644
--- a/amd/build/jmodule.min.js
+++ b/amd/build/jmodule.min.js
@@ -1,2 +1,2 @@
 /*! hshcourselist 2018-08-29 */
-define(["jquery"],function(a){function b(b,c,d,e,f,g,h,i,j){c&&(a("#hshcourseprogress").show(),a.ajax({type:"GET",url:j+"/blocks/hshcourselist/hshcourselist.php",data:"course="+b+"&instanceid="+e+"&sesskey="+f+"&contextid="+g+"&idSearching="+h+"&orderbysemester="+i,dataType:"json",success:function(b){var c=JSON.stringify(b),e=JSON.parse(c),f=a("<ul class='list-group' id='hshcourselistul'></ul>");e.length>0&&a.each(e,function(b,c){switch(d){case"0":displaystr=c.shortname;break;case"1":displaystr=c.fullname;break;case"2":displaystr=c.shortname+": "+c.fullname;break;default:displaystr=c.fullname}a('<li class="list-group-item hshcoursesearchlistitem " value="'+c.id+'"><a href="'+j+"/course/view.php?id="+c.id+'">'+displaystr+"</a> </li>").appendTo(f)}),a("#hshcourselistul").replaceWith(f),a("#hshcourseprogress").hide()},error:function(){console.error("error"),"abort"!==o.statusText&&(a("#hshcourseprogress").hide(),void 0!==o.statusText&&console.error("ABORT + STATUS : "+o.statusText))}}))}return{init:function(c,d){var e,c=c,f=c.instanceid,g=c.sesskey,h=c.displaymode,i=c.contextid,j=!1,k=!1,l=!1,m=c.orderbysemester;a("#hshcourseprogress").hide(),a(".idSarchingLabel").hide(),a("#orderbysemestercheckbox_id").on("change",function(){m=document.getElementById("orderbysemestercheckbox_id").checked,j=!0,e=document.getElementById("hshcourselistsearch").value,"#"===e.slice(0,1)?(a(".idSarchingLabel").show(),k=!0,e=e.slice(1)):"%"===e.slice(0,1)?l=!0:(a(".idSarchingLabel").hide(),k=!1),(1==l||1==k||e.length>=3)&&b(e,j,h,f,g,i,k,m,d)}),a("#hshcourselistsearch").on("keyup",function(c){var n=c.keyCode;m=document.getElementById("orderbysemestercheckbox_id").checked,40!==n&&38!==n&&13!==n&&(j=!0,e=document.getElementById("hshcourselistsearch").value,"#"===e.slice(0,1)?(a(".idSarchingLabel").show(),k=!0,e=e.slice(1)):"%"===e.slice(0,1)?l=!0:(a(".idSarchingLabel").hide(),k=!1),(1==l||1==k||e.length>=3)&&b(e,j,h,f,g,i,k,m,d))}),a("#hshcourseform").on("submit",function(a){a.preventDefault,j=!0,k=!1,l=!1,m=document.getElementById("orderbysemestercheckbox_id").checked,e=document.getElementById("hshcourselistsearch").value,"#"===e.slice(0,1)?(k=!0,e=e.slice(1)):"%"===e.slice(0,1)?l=!0:k=!1,(1==l||1==k||e.length>=3)&&b(e,j,h,f,g,i,k,m,d)})}}});
\ No newline at end of file
+define(["jquery"],function(a){function b(b,c,d,e,f,g,h,i,j){if(c){var k=b.split(/[ ,]+/),l=["-1"],m=a("<ul class='list-group' id='hshcourselistul'></ul>");a("#hshcourseprogress").show(),k.forEach(function(b){a.ajax({type:"GET",url:j+"/blocks/hshcourselist/hshcourselist.php",data:"course="+b+"&instanceid="+e+"&sesskey="+f+"&contextid="+g+"&idSearching="+h+"&orderbysemester="+i,dataType:"json",success:function(b){var c=JSON.stringify(b),e=JSON.parse(c);e.length>0&&a.each(e,function(b,c){switch(d){case"0":displaystr=c.shortname;break;case"1":displaystr=c.fullname;break;case"2":displaystr=c.shortname+": "+c.fullname;break;default:displaystr=c.fullname}l.includes(c.id)||(l.push(c.id),a('<li class="list-group-item hshcoursesearchlistitem " value="'+c.id+'"><a href="'+j+"/course/view.php?id="+c.id+'">'+displaystr+"</a> </li>").appendTo(m))})},error:function(a){console.error("error - courselist")}})}),void 0!==m&&(a("#hshcourselistul").replaceWith(m),a("#hshcourseprogress").hide())}}return{init:function(c,d){var e,c=c,f=c.instanceid,g=c.sesskey,h=c.displaymode,i=c.contextid,j=!1,k=!1,l=!1,m=c.orderbysemester;a("#hshcourseprogress").hide(),a(".idSarchingLabel").hide(),a("#orderbysemestercheckbox_id").on("change",function(){m=document.getElementById("orderbysemestercheckbox_id").checked,j=!0,e=document.getElementById("hshcourselistsearch").value,"#"===e.slice(0,1)?(a(".idSarchingLabel").show(),k=!0,e=e.slice(1)):"%"===e.slice(0,1)?l=!0:(a(".idSarchingLabel").hide(),k=!1),(1==l||1==k||e.length>=3)&&b(e,j,h,f,g,i,k,m,d)}),a("#hshcourselistsearch").on("keyup",function(c){var n=c.keyCode;m=document.getElementById("orderbysemestercheckbox_id").checked,40!==n&&38!==n&&13!==n&&(j=!0,e=document.getElementById("hshcourselistsearch").value,"#"===e.slice(0,1)?(a(".idSarchingLabel").show(),k=!0,e=e.slice(1)):"%"===e.slice(0,1)?l=!0:(a(".idSarchingLabel").hide(),k=!1),(1==l||1==k||e.length>=3)&&b(e,j,h,f,g,i,k,m,d))}),a("#hshcourseform").on("submit",function(a){a.preventDefault,j=!0,k=!1,l=!1,m=document.getElementById("orderbysemestercheckbox_id").checked,e=document.getElementById("hshcourselistsearch").value,"#"===e.slice(0,1)?(k=!0,e=e.slice(1)):"%"===e.slice(0,1)?l=!0:k=!1,(1==l||1==k||e.length>=3)&&b(e,j,h,f,g,i,k,m,d)})}}});
\ No newline at end of file
diff --git a/amd/src/jmodule.js b/amd/src/jmodule.js
index b1c7c87..972ab1b 100644
--- a/amd/src/jmodule.js
+++ b/amd/src/jmodule.js
@@ -1,55 +1,59 @@
 define(['jquery'], function ($) {
     function search(searchstring, reloadBool, displaymode, instanceid, sesskey, contextid, idSearching, orderbysemester, wwwroot) {
         if (reloadBool) {
+            var splittedSearchstring = searchstring.split(/[ ,]+/);
+            var courseInList = ["-1"];
+            var list = $("<ul class='list-group' id='hshcourselistul'></ul>");;
             $("#hshcourseprogress").show();
-            $.ajax({
-                type: 'GET',
-                url: wwwroot + '/blocks/hshcourselist/hshcourselist.php',
-                data: 'course=' + searchstring + '&instanceid=' + instanceid
-                    + '&sesskey=' + sesskey + '&contextid=' + contextid
-                    + '&idSearching=' + idSearching + '&orderbysemester=' + orderbysemester,
-                dataType: "json",
-                success: function (response) {
-                    var plainJSON = JSON.stringify(response);
-                    var courses = JSON.parse(plainJSON);
-                    var list = $("<ul class='list-group' id='hshcourselistul'></ul>");
-                    if (courses.length > 0) {
-                        $.each(courses, function (index, course) {
-                            switch (displaymode) {
-                                case '0':
-                                    displaystr = course.shortname;
-                                    break;
-
-                                case '1':
-                                    displaystr = course.fullname;
-                                    break;
-
-                                case '2':
-                                    displaystr = course.shortname + ': ' + course.fullname;
-                                    break;
-
-                                default:
-                                    displaystr = course.fullname;
-                                    break;
-                            }
-                            $('<li class="list-group-item hshcoursesearchlistitem " value="' + course.id + '">'
-                                + '<a href="' + wwwroot + '/course/view.php?id=' + course.id + '">' + displaystr + '</a> </li>')
-                                .appendTo(list);
-                        });
-                    }
-                    $("#hshcourselistul").replaceWith(list);
-                    $("#hshcourseprogress").hide();
-                },
-                error: function () {
-                    console.error("error");
-                    if (o.statusText !== 'abort') {
-                        $("#hshcourseprogress").hide();
-                        if (o.statusText !== undefined) {
-                            console.error("ABORT + STATUS : " + o.statusText);
+            splittedSearchstring.forEach(function (searchquery) {
+                $.ajax({
+                    type: 'GET',
+                    url: wwwroot + '/blocks/hshcourselist/hshcourselist.php',
+                    data: 'course=' + searchquery + '&instanceid=' + instanceid
+                        + '&sesskey=' + sesskey + '&contextid=' + contextid
+                        + '&idSearching=' + idSearching + '&orderbysemester=' + orderbysemester,
+                    dataType: "json",
+                    success: function (response) {
+                        var plainJSON = JSON.stringify(response);
+                        var courses = JSON.parse(plainJSON);
+                        if (courses.length > 0) {
+                            $.each(courses, function (index, course) {
+                                switch (displaymode) {
+                                    case '0':
+                                        displaystr = course.shortname;
+                                        break;
+                                    case '1':
+                                        displaystr = course.fullname;
+                                        break;
+                                    case '2':
+                                        displaystr = course.shortname + ': ' + course.fullname;
+                                        break;
+                                    default:
+                                        displaystr = course.fullname;
+                                        break;
+                                }
+                                if (!courseInList.includes(course.id)) {
+                                    courseInList.push(course.id);
+                                    $('<li class="list-group-item hshcoursesearchlistitem " value="' + course.id + '">'
+                                        + '<a href="' + wwwroot + '/course/view.php?id=' + course.id + '">' + displaystr + '</a> </li>')
+                                        .appendTo(list);
+                                } else {
+                                    // Kurs schon in der Liste
+                                }
+                            });
                         }
+                    },
+                    error: function (o) {
+                        console.error("error - courselist");
                     }
-                }
+                });
             });
+            if (list !== undefined) {
+                $("#hshcourselistul").replaceWith(list);
+                $("#hshcourseprogress").hide();
+            } else {
+
+            }
         } else {
         }
     }
diff --git a/block_hshcourselist.php b/block_hshcourselist.php
index 2921f7c..bca3336 100644
--- a/block_hshcourselist.php
+++ b/block_hshcourselist.php
@@ -3,7 +3,6 @@
 defined('MOODLE_INTERNAL') || die();
 class block_hshcourselist extends block_base {
     private $globalconf;
-
     public function init() {
         $this->globalconf = get_config('block_hshcourselist');
         $this->title = get_string('default_blocktitle', 'block_hshcourselist');
@@ -37,6 +36,7 @@ class block_hshcourselist extends block_base {
         $this->content = new stdClass();
         $context_block = context_block::instance($this->instance->id);
         $search = optional_param('hshcoursesearch', '', PARAM_TEXT);
+        
         $hshcoursesubmit = optional_param('hshcoursesubmit', false, PARAM_TEXT);
         // set placeholder
         if($this->config->placeholder) {
diff --git a/javascript/listselect.js b/javascript/listselect.js
index ad368ba..cb0b737 100644
--- a/javascript/listselect.js
+++ b/javascript/listselect.js
@@ -1,4 +1,4 @@
-(function($) {
+/*(function($) {
     function changeActionString(current) {
         var actionString = getRootWebSitePath() + '/course/view.php?id=' + current.val();
         $('#hshcourseform').attr('action', actionString);
@@ -76,3 +76,4 @@
         scrollIntoViewIfNeeded($current[0]);
     });
 }(jQuery));
+*/
\ No newline at end of file
diff --git a/version.php b/version.php
index 31744bb..82ca49a 100644
--- a/version.php
+++ b/version.php
@@ -1,4 +1,4 @@
 <?php
 $plugin->component = 'block_hshcourselist';
-$plugin->version = 2018082900;  // YYYYMMDDHH
+$plugin->version = 2018082901;  // YYYYMMDDHH
 $plugin->requires = 2016112900; // YYYYMMDDHH
-- 
GitLab