From a32f163dfd26aee73d2a5f11c62cf469623e0e15 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Wed, 27 Mar 2013 16:07:29 +1100 Subject: [PATCH 1/3] MDL-38147 do not use DB->sql_length because it does not work on MSSQL --- lib/coursecatlib.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/coursecatlib.php b/lib/coursecatlib.php index 30752cea582..c7f0fa5eee3 100644 --- a/lib/coursecatlib.php +++ b/lib/coursecatlib.php @@ -737,7 +737,7 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate { $fields[] = 'c.summary'; $fields[] = 'c.summaryformat'; } else { - $fields[] = $DB->sql_length('c.summary'). ' hassummary'; + $fields[] = $DB->sql_substr('c.summary', 1, 1). ' hassummary'; } $sql = "SELECT ". join(',', $fields). ", $ctxselect FROM {course} c @@ -749,6 +749,9 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate { if ($checkvisibility) { // Loop through all records and make sure we only return the courses accessible by user. foreach ($list as $course) { + if (isset($list[$course->id]->hassummary)) { + $list[$course->id]->hassummary = strlen($list[$course->id]->hassummary) > 0; + } if (empty($course->visible)) { // load context only if we need to check capability context_helper::preload_from_record($course); From 5e71c37ea924c72cb416a27d5d0b4abdbde2633d Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Wed, 27 Mar 2013 17:05:51 +1100 Subject: [PATCH 2/3] MDL-38147 added comments for SQL-based course search that might not work on some DB --- lib/tests/coursecatlib_test.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/tests/coursecatlib_test.php b/lib/tests/coursecatlib_test.php index c071edcc9ba..baa5ecde928 100644 --- a/lib/tests/coursecatlib_test.php +++ b/lib/tests/coursecatlib_test.php @@ -405,17 +405,25 @@ class coursecatlib_testcase extends advanced_testcase { // search courses // search by text + $res = coursecat::search_courses(array('search' => 'Test')); + $this->assertEquals(array($c4->id, $c3->id, $c1->id, $c8->id, $c5->id), array_keys($res)); + $this->assertEquals(5, coursecat::search_courses_count(array('search' => 'Test'))); + + // search by text with specified offset and limit + $options = array('sort' => array('fullname' => 1), 'offset' => 1, 'limit' => 2); + $res = coursecat::search_courses(array('search' => 'Test'), $options); + $this->assertEquals(array($c4->id, $c5->id), array_keys($res)); + $this->assertEquals(5, coursecat::search_courses_count(array('search' => 'Test'), $options)); + + // IMPORTANT: the tests below may fail on some databases + // case-insensitive search $res = coursecat::search_courses(array('search' => 'test')); $this->assertEquals(array($c4->id, $c3->id, $c1->id, $c8->id, $c5->id), array_keys($res)); $this->assertEquals(5, coursecat::search_courses_count(array('search' => 'test'))); + // non-latin language search $res = coursecat::search_courses(array('search' => 'Математика')); $this->assertEquals(array($c3->id, $c6->id), array_keys($res)); $this->assertEquals(2, coursecat::search_courses_count(array('search' => 'Математика'), array())); - - $options = array('sort' => array('fullname' => 1), 'offset' => 1, 'limit' => 2); - $res = coursecat::search_courses(array('search' => 'test'), $options); - $this->assertEquals(array($c4->id, $c5->id), array_keys($res)); - $this->assertEquals(5, coursecat::search_courses_count(array('search' => 'test'), $options)); } } \ No newline at end of file From 0a02b80cdaa2c3412a071024d33cab364b4164ae Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Wed, 27 Mar 2013 17:06:27 +1100 Subject: [PATCH 3/3] MDL-38147 fixed bug with windows PHP different function behaviour --- lib/coursecatlib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/coursecatlib.php b/lib/coursecatlib.php index c7f0fa5eee3..0faa0fbc1a0 100644 --- a/lib/coursecatlib.php +++ b/lib/coursecatlib.php @@ -123,7 +123,7 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate { if (array_key_exists($name, self::$coursecatfields)) { if ($this->$name === false) { // property was not retrieved from DB, retrieve all not retrieved fields - $notretrievedfields = array_diff(self::$coursecatfields, array_filter(self::$coursecatfields)); + $notretrievedfields = array_diff_key(self::$coursecatfields, array_filter(self::$coursecatfields)); $record = $DB->get_record('course_categories', array('id' => $this->id), join(',', array_keys($notretrievedfields)), MUST_EXIST); foreach ($record as $key => $value) {