From 2c4dc16e7112055b82e7bf809880ba434f2b04c9 Mon Sep 17 00:00:00 2001
From: Tobias Baden <tobias.baden@stud.hs-hannover.de>
Date: Thu, 30 Aug 2018 14:21:19 +0200
Subject: [PATCH] Updated the search - Enabled Shortname and fullname search -
 Updated Version

---
 block_hshcourselist.php | 16 ++++++++++------
 version.php             |  2 +-
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/block_hshcourselist.php b/block_hshcourselist.php
index 0773f22..023ecd3 100644
--- a/block_hshcourselist.php
+++ b/block_hshcourselist.php
@@ -179,8 +179,12 @@ class block_hshcourselist extends block_base {
         global $DB;
         $params = array(SITEID);
         $keywordArray = explode(" ", $search);
-        foreach($keywordArray as $key => $value) {
-            $keywordArray[$key] = "%".$keywordArray[$key]."%";
+        $paramscount = count($keywordArray);
+        $duplicatedKeywordArray = array();
+        for ($x = 0; $x <= $paramscount-1; $x++) {
+            $actualLength = count($duplicatedKeywordArray);
+            $duplicatedKeywordArray[$actualLength] = "%".$keywordArray[$x]."%";
+            $duplicatedKeywordArray[$actualLength+1] = "%".$keywordArray[$x]."%";
         }
         $where = 'id != ? AND (';
         if ($splitterms) {
@@ -200,10 +204,10 @@ class block_hshcourselist extends block_base {
                 $params = array_merge($params, array($search));
                 $where .= 'id = ?';
             } else {
-                $params = array_merge($params, $keywordArray);
-                $where .= 'fullname ILIKE ?';
-                for ($i = 0; $i <= count($keywordArray)-2; $i++) {
-                    $where .= ' AND fullname ILIKE ? OR shortname ILIKE ?';
+                $params = array_merge($params, $duplicatedKeywordArray);
+                $where .= '(fullname ILIKE ? OR shortname ILIKE ?)';
+                for ($i = 1; $i <= $paramscount-1; $i++) {
+                    $where .= ' AND (fullname ILIKE ? OR shortname ILIKE ?)';
                 }
             }
         }
diff --git a/version.php b/version.php
index c9c93f8..46d4497 100644
--- a/version.php
+++ b/version.php
@@ -1,4 +1,4 @@
 <?php
 $plugin->component = 'block_hshcourselist';
-$plugin->version = 2018083002;  // YYYYMMDDHH
+$plugin->version = 2018083003;  // YYYYMMDDHH
 $plugin->requires = 2016112900; // YYYYMMDDHH
-- 
GitLab