Merge branch 'MDL-54801-master' of git://github.com/jleyva/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2016-07-26 19:54:53 +02:00
commit 7647f3887f
2 changed files with 40 additions and 5 deletions

View File

@ -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;
}
}
}

View File

@ -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);