From efc68f119d93c6aa3305bd8300c2feb3aca726a3 Mon Sep 17 00:00:00 2001 From: Kevin Bruton Date: Sun, 10 Jan 2016 00:12:58 +0100 Subject: [PATCH] MDL-52568 core_cohort: added ability to return all cohorts in WS --- cohort/externallib.php | 21 ++++++++++++--------- cohort/upgrade.txt | 4 ++++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/cohort/externallib.php b/cohort/externallib.php index 88defef95e4..233be522532 100644 --- a/cohort/externallib.php +++ b/cohort/externallib.php @@ -208,7 +208,7 @@ class core_cohort_external extends external_api { return new external_function_parameters( array( 'cohortids' => new external_multiple_structure(new external_value(PARAM_INT, 'Cohort ID') - , 'List of cohort id. A cohort id is an integer.'), + , 'List of cohort id. A cohort id is an integer.', VALUE_DEFAULT, array()), ) ); } @@ -220,16 +220,19 @@ class core_cohort_external extends external_api { * @return array of cohort objects (id, courseid, name) * @since Moodle 2.5 */ - public static function get_cohorts($cohortids) { + public static function get_cohorts($cohortids = array()) { global $DB; $params = self::validate_parameters(self::get_cohorts_parameters(), array('cohortids' => $cohortids)); - $cohorts = array(); - foreach ($params['cohortids'] as $cohortid) { - // Validate params. - $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST); + if (empty($cohortids)) { + $cohorts = $DB->get_records('cohort'); + } else { + $cohorts = $DB->get_records_list('cohort', 'id', $params['cohortids']); + } + $cohortsinfo = array(); + foreach ($cohorts as $cohort) { // Now security checks. $context = context::instance_by_id($cohort->contextid, MUST_EXIST); if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) { @@ -244,12 +247,12 @@ class core_cohort_external extends external_api { external_format_text($cohort->description, $cohort->descriptionformat, $context->id, 'cohort', 'description', $cohort->id); - $cohorts[] = (array) $cohort; + $cohortsinfo[] = (array) $cohort; } - - return $cohorts; + return $cohortsinfo; } + /** * Returns description of method result value * diff --git a/cohort/upgrade.txt b/cohort/upgrade.txt index d776e6f5cf4..3e8344cafa3 100644 --- a/cohort/upgrade.txt +++ b/cohort/upgrade.txt @@ -1,6 +1,10 @@ This files describes API changes in /cohort/ information provided here is intended especially for developers. +=== 3.0.3 === +* The Webservice core_cohort_get_cohorts now has the added functionality of getting all cohorts + by not passing any parameters + === 2.6 === * Webservice core_cohort_update_cohorts was incorrectly specifiying float as the parameter type for cohort id. This field is actually int and input is now reported and processed as such. -- 2.11.4.GIT