Skip to content

Commit

Permalink
MDL-32941 use + operator instead array merge to avoid duplicate. Sort…
Browse files Browse the repository at this point in the history
… resulting array for better display to the client and for entring in the excluding algo. Exception reformatting. Minor formatting fixes.
  • Loading branch information
mouneyrac committed May 17, 2012
1 parent 20854c7 commit 6c6ec1d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 17 deletions.
90 changes: 74 additions & 16 deletions course/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,18 +255,22 @@ public static function get_course_contents_returns() {
* @since Moodle 2.2
*/
public static function get_categories_parameters() {
return new external_function_parameters(
return new external_function_parameters(
array(
'criteria' => new external_multiple_structure(
new external_single_structure(
array(
'key' => new external_value(PARAM_ALPHA, 'The category column to search, expected keys (value format) are:
"id" (int) the category id,
"name" (string) the category name,
"parent" (int) the parent category id,
"idnumber" (string) category idnumber,
"visible" (int) whether the category is visible or not,
"theme" (string) category theme'),
'key' => new external_value(PARAM_ALPHA,
'The category column to search, expected keys (value format) are:'.
'"id" (int) the category id,'.
'"name" (string) the category name,'.
'"parent" (int) the parent category id,'.
'"idnumber" (string) category idnumber'.
' - user must have \'moodle/category:manage\' to search on idnumber,'.
'"visible" (int) whether the category is visible or not'.
' - user must have \'moodle/category:manage\' or \'moodle/category:viewhiddencategories\' to search on visible,'.
'"theme" (string) category theme'.
' - user must have \'moodle/category:manage\' to search on theme'),
'value' => new external_value(PARAM_RAW, 'the value to match')
)
), VALUE_DEFAULT, array()
Expand Down Expand Up @@ -307,7 +311,8 @@ public static function get_categories($criteria = array(), $addsubcategories = t

$context = context_system::instance();
$value = null;

// $criteriaerrormsg is a code error, do not fix typo, do not edit it.
$criteriaerrormsg = 'Missing permissions to search on a criteria.';
switch ($key) {
case 'id':
$value = clean_param($crit['value'], PARAM_INT);
Expand All @@ -316,6 +321,12 @@ public static function get_categories($criteria = array(), $addsubcategories = t
case 'idnumber':
if (has_capability('moodle/category:manage', $context)) {
$value = clean_param($crit['value'], PARAM_RAW);
} else {
// We must throw an exception.
// Otherwise the dev client would think no idnumber exists.
throw new moodle_exception($criteriaerrormsg,
'', '', null,
'You don\'t have the permissions to search on the "idnumber" field.');
}
break;

Expand All @@ -328,17 +339,31 @@ public static function get_categories($criteria = array(), $addsubcategories = t
break;

case 'visible':
$value = clean_param($crit['value'], PARAM_INT);
if (has_capability('moodle/category:manage', $context)
or has_capability('moodle/category:viewhiddencategories',
context_system::instance())) {
$value = clean_param($crit['value'], PARAM_INT);
} else {
throw new moodle_exception($criteriaerrormsg,
'', '', null,
'You don\'t have the permissions to search on the "visible" field.');
}
break;

case 'theme':
if (has_capability('moodle/category:manage', $context)) {
$value = clean_param($crit['value'], PARAM_THEME);
} else {
throw new moodle_exception($criteriaerrormsg,
'', '', null,
'You don\'t have the permissions to search on the "theme" field.');
}
break;

default:
throw new moodle_exception('invalidextparam', 'webservice', '', $key);
throw new moodle_exception($criteriaerrormsg,
'', '', null,
'You can not search on this criteria: ' . $key);
}

if (isset($value)) {
Expand All @@ -351,7 +376,7 @@ public static function get_categories($criteria = array(), $addsubcategories = t
if (!empty($wheres)) {
$wheres = implode(" AND ", $wheres);

$categories = $DB->get_records_select('course_categories', $wheres, $conditions 'path');
$categories = $DB->get_records_select('course_categories', $wheres, $conditions);

// Retrieve its sub subcategories (all levels).
if ($categories and !empty($params['addsubcategories'])) {
Expand All @@ -360,16 +385,16 @@ public static function get_categories($criteria = array(), $addsubcategories = t
foreach ($categories as $category) {
$sqllike = $DB->sql_like('path', ':path');
$sqlparams = array('path' => $category->path.'/%'); // It will NOT include the specified category.
$subcategories = $DB->get_records_select('course_categories', $sqllike, $sqlparams, 'path');
$newcategories = array_merge($newcategories, $subcategories); // Both arrays have integer as keys.
$subcategories = $DB->get_records_select('course_categories', $sqllike, $sqlparams);
$newcategories = $newcategories + $subcategories; // Both arrays have integer as keys.
}
$categories = array_merge($categories, $newcategories);
$categories = $categories + $newcategories;
}
}

} else {
// Retrieve all categories in the database.
$categories = $DB->get_records('course_categories', array(), 'path');
$categories = $DB->get_records('course_categories');
}

// The not returned categories. key => category id, value => reason of exclusion.
Expand All @@ -378,6 +403,10 @@ public static function get_categories($criteria = array(), $addsubcategories = t
// The returned categories.
$categoriesinfo = array();

// We need to sort the categories by path.
// The parent cats need to be checked by the algo first.
usort($categories, "core_course_external::compare_categories_by_path");

foreach ($categories as $category) {

// Check if the category is a child of an excluded category, if yes exclude it too (excluded => do not return).
Expand Down Expand Up @@ -453,9 +482,38 @@ public static function get_categories($criteria = array(), $addsubcategories = t
}
}

// Sorting the resulting array so it looks a bit better for the client developer.
usort($categoriesinfo, "core_course_external::compare_categories_by_sortorder");

return $categoriesinfo;
}

/**
* Sort categories array by path
* private function: only used by get_categories
*
* @param array $category1
* @param array $category2
* @return int result of strcmp
* @since Moodle 2.3
*/
public static function compare_categories_by_path($category1, $category2) {
return strcmp($category1->path, $category2->path);
}

/**
* Sort categories array by sortorder
* private function: only used by get_categories
*
* @param array $category1
* @param array $category2
* @return int result of strcmp
* @since Moodle 2.3
*/
public static function compare_categories_by_sortorder($category1, $category2) {
return strcmp($category1['sortorder'], $category2['sortorder']);
}

/**
* Returns description of method result value
*
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
defined('MOODLE_INTERNAL') || die();


$version = 2012051100.00; // YYYYMMDD = weekly release date of this DEV branch
$version = 2012051100.01; // YYYYMMDD = weekly release date of this DEV branch
// RR = release increments - 00 in DEV branches
// .XX = incremental changes

Expand Down

0 comments on commit 6c6ec1d

Please sign in to comment.