From c1da311ad6a32e7d028b58fd844c3f1afe2aad28 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Tue, 12 Jul 2016 15:41:32 +0100 Subject: [PATCH] MDL-54801 webservice: Support 'ids' search in get_categories --- course/externallib.php | 26 +++++++++++++++++++++----- course/tests/externallib_test.php | 19 +++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/course/externallib.php b/course/externallib.php index 34587232045..c6af31b7ee4 100644 --- a/course/externallib.php +++ b/course/externallib.php @@ -1449,6 +1449,7 @@ class core_course_external extends external_api { 'key' => new external_value(PARAM_ALPHA, 'The category column to search, expected keys (value format) are:'. '"id" (int) the category id,'. + '"ids" (string) category ids separated by commas,'. '"name" (string) the category name,'. '"parent" (int) the parent category id,'. '"idnumber" (string) category idnumber'. @@ -1501,11 +1502,23 @@ class core_course_external extends external_api { switch ($key) { case 'id': $value = clean_param($crit['value'], PARAM_INT); + $conditions[$key] = $value; + $wheres[] = $key . " = :" . $key; + break; + + case 'ids': + $value = clean_param($crit['value'], PARAM_SEQUENCE); + $ids = explode(',', $value); + list($sqlids, $paramids) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED); + $conditions = array_merge($conditions, $paramids); + $wheres[] = 'id ' . $sqlids; break; case 'idnumber': if (has_capability('moodle/category:manage', $context)) { $value = clean_param($crit['value'], PARAM_RAW); + $conditions[$key] = $value; + $wheres[] = $key . " = :" . $key; } else { // We must throw an exception. // Otherwise the dev client would think no idnumber exists. @@ -1517,10 +1530,14 @@ class core_course_external extends external_api { case 'name': $value = clean_param($crit['value'], PARAM_TEXT); + $conditions[$key] = $value; + $wheres[] = $key . " = :" . $key; break; case 'parent': $value = clean_param($crit['value'], PARAM_INT); + $conditions[$key] = $value; + $wheres[] = $key . " = :" . $key; break; case 'visible': @@ -1528,6 +1545,8 @@ class core_course_external extends external_api { or has_capability('moodle/category:viewhiddencategories', context_system::instance())) { $value = clean_param($crit['value'], PARAM_INT); + $conditions[$key] = $value; + $wheres[] = $key . " = :" . $key; } else { throw new moodle_exception('criteriaerror', 'webservice', '', null, @@ -1538,6 +1557,8 @@ class core_course_external extends external_api { case 'theme': if (has_capability('moodle/category:manage', $context)) { $value = clean_param($crit['value'], PARAM_THEME); + $conditions[$key] = $value; + $wheres[] = $key . " = :" . $key; } else { throw new moodle_exception('criteriaerror', 'webservice', '', null, @@ -1550,11 +1571,6 @@ class core_course_external extends external_api { 'webservice', '', null, 'You can not search on this criteria: ' . $key); } - - if (isset($value)) { - $conditions[$key] = $value; - $wheres[] = $key . " = :" . $key; - } } } diff --git a/course/tests/externallib_test.php b/course/tests/externallib_test.php index 71372e7a4a3..db334e8505b 100644 --- a/course/tests/externallib_test.php +++ b/course/tests/externallib_test.php @@ -223,9 +223,28 @@ class core_course_externallib_testcase extends externallib_advanced_testcase { $this->assertEquals($category['descriptionformat'], FORMAT_HTML); } + // Check categories by ids. + $ids = implode(',', array_keys($generatedcats)); + $categories = core_course_external::get_categories(array( + array('key' => 'ids', 'value' => $ids)), 0); + + // We need to execute the return values cleaning process to simulate the web service server. + $categories = external_api::clean_returnvalue(core_course_external::get_categories_returns(), $categories); + + // Check we retrieve the good total number of categories. + $this->assertEquals(6, count($categories)); + // Check ids. + $returnedids = []; + foreach ($categories as $category) { + $returnedids[] = $category['id']; + } + // Sort the arrays upon comparision. + $this->assertEquals(array_keys($generatedcats), $returnedids, '', 0.0, 10, true); + // Check different params. $categories = core_course_external::get_categories(array( array('key' => 'id', 'value' => $category1->id), + array('key' => 'ids', 'value' => $category1->id), array('key' => 'idnumber', 'value' => $category1->idnumber), array('key' => 'visible', 'value' => 1)), 0);