From 882bac854f95a26fbd40d03e6ddd3cf1f1ee88ac Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Mon, 30 Apr 2012 16:50:50 +0200 Subject: [PATCH 01/10] MDL-32662 Added method create_groupings --- group/externallib.php | 87 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/group/externallib.php b/group/externallib.php index 128ad9e4ba5..cfba689eb1a 100644 --- a/group/externallib.php +++ b/group/externallib.php @@ -550,6 +550,93 @@ class core_group_external extends external_api { return null; } + /** + * Returns description of method parameters + * @return external_function_parameters + */ + public static function create_groupings_parameters() { + return new external_function_parameters( + array( + 'groupings' => new external_multiple_structure( + new external_single_structure( + array( + 'courseid' => new external_value(PARAM_INT, 'id of course'), + 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'), + 'description' => new external_value(PARAM_RAW, 'grouping description text') + ) + ), 'List of grouping object. A grouping has a courseid, a name and a description.' + ) + ) + ); + } + + /** + * Create groupings + * @param array $groupings array of grouping description arrays (with keys groupname and courseid) + * @return array of newly created groupings + */ + public static function create_groupings($groupings) { + global $CFG, $DB; + require_once("$CFG->dirroot/group/lib.php"); + + $params = self::validate_parameters(self::create_groupings_parameters(), array('groupings'=>$groupings)); + + $transaction = $DB->start_delegated_transaction(); + + $groupings = array(); + + foreach ($params['groupings'] as $grouping) { + $grouping = (object)$grouping; + + if (trim($grouping->name) == '') { + throw new invalid_parameter_exception('Invalid grouping name'); + } + if ($DB->get_record('groupings', array('courseid'=>$grouping->courseid, 'name'=>$grouping->name))) { + throw new invalid_parameter_exception('Grouping with the same name already exists in the course'); + } + + // Now security checks . + $context = context_course::instance($grouping->courseid); + try { + self::validate_context($context); + } catch (Exception $e) { + $exceptionparam = new stdClass(); + $exceptionparam->message = $e->getMessage(); + $exceptionparam->courseid = $grouping->courseid; + throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); + } + require_capability('moodle/course:managegroups', $context); + + // We must force allways FORMAT_HTML. + $grouping->descriptionformat = FORMAT_HTML; + + // Finally create the grouping. + $grouping->id = groups_create_grouping($grouping); + $groupings[] = (array)$grouping; + } + + $transaction->allow_commit(); + + return $groupings; + } + + /** + * Returns description of method result value + * @return external_description + */ + public static function create_groupings_returns() { + return new external_multiple_structure( + new external_single_structure( + array( + 'id' => new external_value(PARAM_INT, 'grouping record id'), + 'courseid' => new external_value(PARAM_INT, 'id of course'), + 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'), + 'description' => new external_value(PARAM_CLEANHTML, 'grouping description text') + ) + ), 'List of grouping object. A grouping has an id, a courseid, a name and a description.' + ); + } + } /** From 87e959f33b16f995eb4c8676d2262ec3f15f4915 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Mon, 30 Apr 2012 16:52:30 +0200 Subject: [PATCH 02/10] MDL-32662 Added update_groupings method --- group/externallib.php | 89 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/group/externallib.php b/group/externallib.php index cfba689eb1a..2ac1446d9ee 100644 --- a/group/externallib.php +++ b/group/externallib.php @@ -637,6 +637,95 @@ class core_group_external extends external_api { ); } + /** + * Returns description of method parameters + * @return external_function_parameters + */ + public static function update_groupings_parameters() { + return new external_function_parameters( + array( + 'groupings' => new external_multiple_structure( + new external_single_structure( + array( + 'id' => new external_value(PARAM_INT, 'id of grouping'), + 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'), + 'description' => new external_value(PARAM_RAW, 'grouping description text') + ) + ), 'List of grouping object. A grouping has a courseid, a name and a description.' + ) + ) + ); + } + + /** + * Update groupings + * @param array $groupings array of grouping description arrays (with keys groupname and courseid) + * @return array of newly updated groupings + */ + public static function update_groupings($groupings) { + global $CFG, $DB; + require_once("$CFG->dirroot/group/lib.php"); + + $params = self::validate_parameters(self::update_groupings_parameters(), array('groupings'=>$groupings)); + + $transaction = $DB->start_delegated_transaction(); + + $groupings = array(); + + foreach ($params['groupings'] as $grouping) { + $grouping = (object)$grouping; + + if (trim($grouping->name) == '') { + throw new invalid_parameter_exception('Invalid grouping name'); + } + + if (! $currentgrouping = $DB->get_record('groupings', array('id'=>$grouping->id))) { + throw new invalid_parameter_exception("Grouping $grouping->id does not exist in the course"); + } + $grouping->courseid = $currentgrouping->courseid; + + // Now security checks. + $context = context_course::instance($grouping->courseid); + try { + self::validate_context($context); + } catch (Exception $e) { + $exceptionparam = new stdClass(); + $exceptionparam->message = $e->getMessage(); + $exceptionparam->courseid = $grouping->courseid; + throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); + } + require_capability('moodle/course:managegroups', $context); + + // We must force allways FORMAT_HTML. + $grouping->descriptionformat = FORMAT_HTML; + + // Finally update the grouping. + groups_update_grouping($grouping); + $groupings[] = (array)$grouping; + } + + $transaction->allow_commit(); + + return $groupings; + } + + /** + * Returns description of method result value + * @return external_description + */ + public static function update_groupings_returns() { + return new external_multiple_structure( + new external_single_structure( + array( + 'id' => new external_value(PARAM_INT, 'grouping record id'), + 'courseid' => new external_value(PARAM_INT, 'id of course'), + 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'), + 'description' => new external_value(PARAM_CLEANHTML, 'grouping description text') + ) + ), 'List of grouping object. A grouping has an id, a courseid, a name and a description.' + ); + } + } /** From ed849fbaceb1840368452fbe8f41da86d368ec60 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Mon, 30 Apr 2012 16:53:04 +0200 Subject: [PATCH 03/10] MDL-32662 Added get_groupings method --- group/externallib.php | 71 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/group/externallib.php b/group/externallib.php index 2ac1446d9ee..cfe5cb09dbe 100644 --- a/group/externallib.php +++ b/group/externallib.php @@ -726,6 +726,77 @@ class core_group_external extends external_api { ); } + /** + * Returns description of method parameters + * @return external_function_parameters + */ + public static function get_groupings_parameters() { + return new external_function_parameters( + array( + 'groupingids' => new external_multiple_structure(new external_value(PARAM_INT, 'grouping ID') + ,'List of grouping id. A grouping id is an integer.'), + ) + ); + } + + /** + * Get groupings definition specified by ids + * @param array $groupingids arrays of grouping ids + * @return array of grouping objects (id, courseid, name) + */ + public static function get_groupings($groupingids) { + global $CFG; + require_once("$CFG->dirroot/group/lib.php"); + + $params = self::validate_parameters(self::get_groupings_parameters(), array('groupingids'=>$groupingids)); + + $groupings = array(); + foreach ($params['groupingids'] as $groupingid) { + // Validate params. + $grouping = groups_get_grouping($groupingid, '*', MUST_EXIST); + + // Now security checks. + $context = context_course::instance($grouping->courseid); + try { + self::validate_context($context); + } catch (Exception $e) { + $exceptionparam = new stdClass(); + $exceptionparam->message = $e->getMessage(); + $exceptionparam->courseid = $grouping->courseid; + throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); + } + require_capability('moodle/course:managegroups', $context); + + $grouping->description = file_rewrite_pluginfile_urls($grouping->description, 'webservice/pluginfile.php', $context->id, 'grouping', 'description', $grouping->id); + + $options = new stdClass; + $options->noclean = true; + $options->para = false; + $grouping->description = format_text($grouping->description, FORMAT_HTML, $options); + + $groupings[] = (array)$grouping; + } + + return $groupings; + } + + /** + * Returns description of method result value + * @return external_description + */ + public static function get_groupings_returns() { + return new external_multiple_structure( + new external_single_structure( + array( + 'id' => new external_value(PARAM_INT, 'grouping record id'), + 'courseid' => new external_value(PARAM_INT, 'id of course'), + 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'), + 'description' => new external_value(PARAM_CLEANHTML, 'grouping description text') + ) + ) + ); + } + } /** From 85e42d221879a768b858be696e4fe790d9902d77 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Mon, 30 Apr 2012 16:53:53 +0200 Subject: [PATCH 04/10] MDL-32662 Added get_course_groupings method --- group/externallib.php | 70 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/group/externallib.php b/group/externallib.php index cfe5cb09dbe..be8be1da295 100644 --- a/group/externallib.php +++ b/group/externallib.php @@ -797,6 +797,76 @@ class core_group_external extends external_api { ); } + /** + * Returns description of method parameters + * @return external_function_parameters + */ + public static function get_course_groupings_parameters() { + return new external_function_parameters( + array( + 'courseid' => new external_value(PARAM_INT, 'id of course'), + ) + ); + } + + /** + * Get all groupings in the specified course + * @param int $courseid id of course + * @return array of grouping objects (id, courseid, name, enrolmentkey) + */ + public static function get_course_groupings($courseid) { + global $CFG; + require_once("$CFG->dirroot/group/lib.php"); + + $params = self::validate_parameters(self::get_course_groupings_parameters(), array('courseid'=>$courseid)); + + // Now security checks. + $context = context_course::instance($params['courseid']); + + try { + self::validate_context($context); + } catch (Exception $e) { + $exceptionparam = new stdClass(); + $exceptionparam->message = $e->getMessage(); + $exceptionparam->courseid = $params['courseid']; + throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); + } + require_capability('moodle/course:managegroups', $context); + + $gs = groups_get_all_groupings($params['courseid']); + + $groupings = array(); + foreach ($gs as $grouping) { + $grouping->description = file_rewrite_pluginfile_urls($grouping->description, 'webservice/pluginfile.php', $context->id, 'grouping', 'description', $grouping->id); + + $options = new stdClass; + $options->noclean = true; + $options->para = false; + $grouping->description = format_text($grouping->description, FORMAT_HTML, $options); + + $groupings[] = (array)$grouping; + } + + return $groupings; + } + + /** + * Returns description of method result value + * @return external_description + */ + public static function get_course_groupings_returns() { + return new external_multiple_structure( + new external_single_structure( + array( + 'id' => new external_value(PARAM_INT, 'grouping record id'), + 'courseid' => new external_value(PARAM_INT, 'id of course'), + 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'), + 'description' => new external_value(PARAM_CLEANHTML, 'grouping description text') + ) + ) + ); + } + } /** From a531136e9822ec44318198f91914ad62eb81cae3 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Mon, 30 Apr 2012 16:54:52 +0200 Subject: [PATCH 05/10] MDL-32662 Added delete_groupings method --- group/externallib.php | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/group/externallib.php b/group/externallib.php index be8be1da295..5ca5935401f 100644 --- a/group/externallib.php +++ b/group/externallib.php @@ -867,6 +867,65 @@ class core_group_external extends external_api { ); } + /** + * Returns description of method parameters + * @return external_function_parameters + */ + public static function delete_groupings_parameters() { + return new external_function_parameters( + array( + 'groupingids' => new external_multiple_structure(new external_value(PARAM_INT, 'grouping ID')), + ) + ); + } + + /** + * Delete groupings + * @param array $groupingids array of grouping ids + * @return void + */ + public static function delete_groupings($groupingids) { + global $CFG, $DB; + require_once("$CFG->dirroot/group/lib.php"); + + $params = self::validate_parameters(self::delete_groupings_parameters(), array('groupingids'=>$groupingids)); + + $transaction = $DB->start_delegated_transaction(); + + foreach ($params['groupingids'] as $groupingid) { + // Validate params. + $groupingid = validate_param($groupingid, PARAM_INTEGER); + if (!$grouping = groups_get_grouping($groupingid, 'id, courseid', IGNORE_MISSING)) { + // Silently ignore attempts to delete nonexisting groupings. + continue; + } + + // Now security checks. + $context = context_course::instance($grouping->courseid); + try { + self::validate_context($context); + } catch (Exception $e) { + $exceptionparam = new stdClass(); + $exceptionparam->message = $e->getMessage(); + $exceptionparam->courseid = $grouping->courseid; + throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); + } + require_capability('moodle/course:managegroups', $context); + + groups_delete_grouping($grouping); + } + + $transaction->allow_commit(); + } + + /** + * Returns description of method result value + * @return external_description + */ + public static function delete_groupings_returns() { + return null; + } + } /** From 20053b8c3d2f2b826cce4b86de12e7c814a0bf2a Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Mon, 30 Apr 2012 16:55:28 +0200 Subject: [PATCH 06/10] MDL-32662 Added assign_grouping method --- group/externallib.php | 70 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/group/externallib.php b/group/externallib.php index 5ca5935401f..ba346720548 100644 --- a/group/externallib.php +++ b/group/externallib.php @@ -926,6 +926,76 @@ class core_group_external extends external_api { return null; } + /** + * Returns description of method parameters + * @return external_function_parameters + */ + public static function assign_grouping_parameters() { + return new external_function_parameters( + array( + 'assignments'=> new external_multiple_structure( + new external_single_structure( + array( + 'groupingid' => new external_value(PARAM_INT, 'grouping record id'), + 'groupid' => new external_value(PARAM_INT, 'group record id'), + ) + ) + ) + ) + ); + } + + /** + * Assign a group to a grouping + * @param array $assignments of arrays with keys groupid, groupingid + * @return void + */ + public static function assign_grouping($assignments) { + global $CFG, $DB; + require_once("$CFG->dirroot/group/lib.php"); + + $params = self::validate_parameters(self::assign_grouping_parameters(), array('assignments'=>$assignments)); + + $transaction = $DB->start_delegated_transaction(); + foreach ($params['assignments'] as $assignment) { + // Validate params. + $groupingid = $assignment['groupingid']; + $groupid = $assignment['groupid']; + + $grouping = groups_get_grouping($groupingid, 'id, courseid', MUST_EXIST); + $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST); + + if ($DB->record_exists('groupings_groups', array('groupingid'=>$groupingid, 'groupid'=>$groupid))) { + // Continue silently if the group is yet assigned to the grouping. + continue; + } + + // now security checks + $context = context_course::instance($grouping->courseid); + try { + self::validate_context($context); + } catch (Exception $e) { + $exceptionparam = new stdClass(); + $exceptionparam->message = $e->getMessage(); + $exceptionparam->courseid = $group->courseid; + throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); + } + require_capability('moodle/course:managegroups', $context); + + groups_assign_grouping($groupingid, $groupid); + } + + $transaction->allow_commit(); + } + + /** + * Returns description of method result value + * @return null + */ + public static function assign_grouping_returns() { + return null; + } + } /** From fb3f5d3173d330ddc4d9d0aebac5a0b1b27b85db Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Mon, 30 Apr 2012 16:56:22 +0200 Subject: [PATCH 07/10] MDL-32662 Added unassign_grouping method --- group/externallib.php | 70 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/group/externallib.php b/group/externallib.php index ba346720548..eb3efdb5b17 100644 --- a/group/externallib.php +++ b/group/externallib.php @@ -996,6 +996,76 @@ class core_group_external extends external_api { return null; } + /** + * Returns description of method parameters + * @return external_function_parameters + */ + public static function unassign_grouping_parameters() { + return new external_function_parameters( + array( + 'unassignments'=> new external_multiple_structure( + new external_single_structure( + array( + 'groupingid' => new external_value(PARAM_INT, 'grouping record id'), + 'groupid' => new external_value(PARAM_INT, 'group record id'), + ) + ) + ) + ) + ); + } + + /** + * Unassign a group from a grouping + * @param array $unassignments of arrays with keys groupid, groupingid + * @return void + */ + public static function unassign_grouping($unassignments) { + global $CFG, $DB; + require_once("$CFG->dirroot/group/lib.php"); + + $params = self::validate_parameters(self::unassign_grouping_parameters(), array('unassignments'=>$unassignments)); + + $transaction = $DB->start_delegated_transaction(); + foreach ($params['unassignments'] as $unassignment) { + // Validate params. + $groupingid = $unassignment['groupingid']; + $groupid = $unassignment['groupid']; + + $grouping = groups_get_grouping($groupingid, 'id, courseid', MUST_EXIST); + $group = groups_get_group($groupid, 'id, courseid', MUST_EXIST); + + if (!$DB->record_exists('groupings_groups', array('groupingid'=>$groupingid, 'groupid'=>$groupid))) { + // Continue silently if the group is not assigned to the grouping. + continue; + } + + // now security checks + $context = context_course::instance($grouping->courseid); + try { + self::validate_context($context); + } catch (Exception $e) { + $exceptionparam = new stdClass(); + $exceptionparam->message = $e->getMessage(); + $exceptionparam->courseid = $group->courseid; + throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam); + } + require_capability('moodle/course:managegroups', $context); + + groups_unassign_grouping($groupingid, $groupid); + } + + $transaction->allow_commit(); + } + + /** + * Returns description of method result value + * @return null + */ + public static function unassign_grouping_returns() { + return null; + } + } /** From 7ce235909a442e60983a9a9059e89aa791d6615f Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Mon, 30 Apr 2012 17:00:03 +0200 Subject: [PATCH 08/10] MDL-32662 Added new groupings methods to services file --- lib/db/services.php | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/lib/db/services.php b/lib/db/services.php index 476b4bb1b63..e943e1d7284 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -163,6 +163,62 @@ $functions = array( 'capabilities'=> 'moodle/course:managegroups', ), + 'core_group_create_groupings' => array( + 'classname' => 'core_group_external', + 'methodname' => 'create_groupings', + 'classpath' => 'group/externallib.php', + 'description' => 'Creates new groupings', + 'type' => 'write', + ), + + 'core_group_update_groupings' => array( + 'classname' => 'core_group_external', + 'methodname' => 'update_groupings', + 'classpath' => 'group/externallib.php', + 'description' => 'Updates existing groupings', + 'type' => 'write', + ), + + 'core_group_get_groupings' => array( + 'classname' => 'core_group_external', + 'methodname' => 'get_groupings', + 'classpath' => 'group/externallib.php', + 'description' => 'Returns groupings details.', + 'type' => 'read', + ), + + 'core_group_get_course_groupings' => array( + 'classname' => 'core_group_external', + 'methodname' => 'get_course_groupings', + 'classpath' => 'group/externallib.php', + 'description' => 'Returns all groupings in specified course.', + 'type' => 'read', + ), + + 'core_group_delete_groupings' => array( + 'classname' => 'core_group_external', + 'methodname' => 'delete_groupings', + 'classpath' => 'group/externallib.php', + 'description' => 'Deletes all specified groupings.', + 'type' => 'write', + ), + + 'core_group_assign_grouping' => array( + 'classname' => 'core_group_external', + 'methodname' => 'assign_grouping', + 'classpath' => 'group/externallib.php', + 'description' => 'Assing groups from groupings', + 'type' => 'write', + ), + + 'core_group_unassign_grouping' => array( + 'classname' => 'core_group_external', + 'methodname' => 'unassign_grouping', + 'classpath' => 'group/externallib.php', + 'description' => 'Unassing groups from groupings', + 'type' => 'write', + ), + // === file related functions === 'moodle_file_get_files' => array( From 67aa60f9b71bc9725a7e469b4e59087d6f8e907f Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Thu, 10 May 2012 11:41:46 +0200 Subject: [PATCH 09/10] MDL-32662 Fixed review problems --- group/externallib.php | 83 +++++++++++++++++++++++++++++-------------- lib/db/services.php | 40 ++++++++++----------- 2 files changed, 77 insertions(+), 46 deletions(-) diff --git a/group/externallib.php b/group/externallib.php index eb3efdb5b17..0ff3b5157dc 100644 --- a/group/externallib.php +++ b/group/externallib.php @@ -552,7 +552,9 @@ class core_group_external extends external_api { /** * Returns description of method parameters + * * @return external_function_parameters + * @since Moodle 2.3 */ public static function create_groupings_parameters() { return new external_function_parameters( @@ -572,8 +574,10 @@ class core_group_external extends external_api { /** * Create groupings + * * @param array $groupings array of grouping description arrays (with keys groupname and courseid) * @return array of newly created groupings + * @since Moodle 2.3 */ public static function create_groupings($groupings) { global $CFG, $DB; @@ -591,7 +595,7 @@ class core_group_external extends external_api { if (trim($grouping->name) == '') { throw new invalid_parameter_exception('Invalid grouping name'); } - if ($DB->get_record('groupings', array('courseid'=>$grouping->courseid, 'name'=>$grouping->name))) { + if ($DB->count_records('groupings', array('courseid'=>$grouping->courseid, 'name'=>$grouping->name))) { throw new invalid_parameter_exception('Grouping with the same name already exists in the course'); } @@ -622,7 +626,9 @@ class core_group_external extends external_api { /** * Returns description of method result value + * * @return external_description + * @since Moodle 2.3 */ public static function create_groupings_returns() { return new external_multiple_structure( @@ -639,7 +645,9 @@ class core_group_external extends external_api { /** * Returns description of method parameters + * * @return external_function_parameters + * @since Moodle 2.3 */ public static function update_groupings_parameters() { return new external_function_parameters( @@ -659,8 +667,10 @@ class core_group_external extends external_api { /** * Update groupings + * * @param array $groupings array of grouping description arrays (with keys groupname and courseid) * @return array of newly updated groupings + * @since Moodle 2.3 */ public static function update_groupings($groupings) { global $CFG, $DB; @@ -670,8 +680,6 @@ class core_group_external extends external_api { $transaction = $DB->start_delegated_transaction(); - $groupings = array(); - foreach ($params['groupings'] as $grouping) { $grouping = (object)$grouping; @@ -701,52 +709,49 @@ class core_group_external extends external_api { // Finally update the grouping. groups_update_grouping($grouping); - $groupings[] = (array)$grouping; } $transaction->allow_commit(); - return $groupings; + return null; } - /** + /** * Returns description of method result value + * * @return external_description + * @since Moodle 2.3 */ public static function update_groupings_returns() { - return new external_multiple_structure( - new external_single_structure( - array( - 'id' => new external_value(PARAM_INT, 'grouping record id'), - 'courseid' => new external_value(PARAM_INT, 'id of course'), - 'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'), - 'description' => new external_value(PARAM_CLEANHTML, 'grouping description text') - ) - ), 'List of grouping object. A grouping has an id, a courseid, a name and a description.' - ); + return null; } /** * Returns description of method parameters + * * @return external_function_parameters + * @since Moodle 2.3 */ public static function get_groupings_parameters() { return new external_function_parameters( array( 'groupingids' => new external_multiple_structure(new external_value(PARAM_INT, 'grouping ID') - ,'List of grouping id. A grouping id is an integer.'), + , 'List of grouping id. A grouping id is an integer.'), ) ); } /** * Get groupings definition specified by ids + * * @param array $groupingids arrays of grouping ids * @return array of grouping objects (id, courseid, name) + * @since Moodle 2.3 */ public static function get_groupings($groupingids) { global $CFG; require_once("$CFG->dirroot/group/lib.php"); + require_once("$CFG->libdir/filelib.php"); $params = self::validate_parameters(self::get_groupings_parameters(), array('groupingids'=>$groupingids)); @@ -780,9 +785,11 @@ class core_group_external extends external_api { return $groupings; } - /** + /** * Returns description of method result value + * * @return external_description + * @since Moodle 2.3 */ public static function get_groupings_returns() { return new external_multiple_structure( @@ -799,7 +806,9 @@ class core_group_external extends external_api { /** * Returns description of method parameters + * * @return external_function_parameters + * @since Moodle 2.3 */ public static function get_course_groupings_parameters() { return new external_function_parameters( @@ -811,12 +820,15 @@ class core_group_external extends external_api { /** * Get all groupings in the specified course + * * @param int $courseid id of course * @return array of grouping objects (id, courseid, name, enrolmentkey) + * @since Moodle 2.3 */ public static function get_course_groupings($courseid) { global $CFG; require_once("$CFG->dirroot/group/lib.php"); + require_once("$CFG->libdir/filelib.php"); $params = self::validate_parameters(self::get_course_groupings_parameters(), array('courseid'=>$courseid)); @@ -850,9 +862,11 @@ class core_group_external extends external_api { return $groupings; } - /** + /** * Returns description of method result value + * * @return external_description + * @since Moodle 2.3 */ public static function get_course_groupings_returns() { return new external_multiple_structure( @@ -869,7 +883,9 @@ class core_group_external extends external_api { /** * Returns description of method parameters + * * @return external_function_parameters + * @since Moodle 2.3 */ public static function delete_groupings_parameters() { return new external_function_parameters( @@ -881,8 +897,10 @@ class core_group_external extends external_api { /** * Delete groupings + * * @param array $groupingids array of grouping ids * @return void + * @since Moodle 2.3 */ public static function delete_groupings($groupingids) { global $CFG, $DB; @@ -893,8 +911,7 @@ class core_group_external extends external_api { $transaction = $DB->start_delegated_transaction(); foreach ($params['groupingids'] as $groupingid) { - // Validate params. - $groupingid = validate_param($groupingid, PARAM_INTEGER); + if (!$grouping = groups_get_grouping($groupingid, 'id, courseid', IGNORE_MISSING)) { // Silently ignore attempts to delete nonexisting groupings. continue; @@ -918,9 +935,11 @@ class core_group_external extends external_api { $transaction->allow_commit(); } - /** + /** * Returns description of method result value + * * @return external_description + * @since Moodle 2.3 */ public static function delete_groupings_returns() { return null; @@ -928,7 +947,9 @@ class core_group_external extends external_api { /** * Returns description of method parameters + * * @return external_function_parameters + * @since Moodle 2.3 */ public static function assign_grouping_parameters() { return new external_function_parameters( @@ -947,8 +968,10 @@ class core_group_external extends external_api { /** * Assign a group to a grouping + * * @param array $assignments of arrays with keys groupid, groupingid * @return void + * @since Moodle 2.3 */ public static function assign_grouping($assignments) { global $CFG, $DB; @@ -970,7 +993,7 @@ class core_group_external extends external_api { continue; } - // now security checks + // Now security checks. $context = context_course::instance($grouping->courseid); try { self::validate_context($context); @@ -988,9 +1011,11 @@ class core_group_external extends external_api { $transaction->allow_commit(); } - /** + /** * Returns description of method result value + * * @return null + * @since Moodle 2.3 */ public static function assign_grouping_returns() { return null; @@ -998,7 +1023,9 @@ class core_group_external extends external_api { /** * Returns description of method parameters + * * @return external_function_parameters + * @since Moodle 2.3 */ public static function unassign_grouping_parameters() { return new external_function_parameters( @@ -1017,8 +1044,10 @@ class core_group_external extends external_api { /** * Unassign a group from a grouping + * * @param array $unassignments of arrays with keys groupid, groupingid * @return void + * @since Moodle 2.3 */ public static function unassign_grouping($unassignments) { global $CFG, $DB; @@ -1040,7 +1069,7 @@ class core_group_external extends external_api { continue; } - // now security checks + // Now security checks. $context = context_course::instance($grouping->courseid); try { self::validate_context($context); @@ -1058,9 +1087,11 @@ class core_group_external extends external_api { $transaction->allow_commit(); } - /** + /** * Returns description of method result value + * * @return null + * @since Moodle 2.3 */ public static function unassign_grouping_returns() { return null; diff --git a/lib/db/services.php b/lib/db/services.php index e943e1d7284..4a4559f7b74 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -188,35 +188,35 @@ $functions = array( ), 'core_group_get_course_groupings' => array( - 'classname' => 'core_group_external', - 'methodname' => 'get_course_groupings', - 'classpath' => 'group/externallib.php', - 'description' => 'Returns all groupings in specified course.', - 'type' => 'read', + 'classname' => 'core_group_external', + 'methodname' => 'get_course_groupings', + 'classpath' => 'group/externallib.php', + 'description' => 'Returns all groupings in specified course.', + 'type' => 'read', ), 'core_group_delete_groupings' => array( - 'classname' => 'core_group_external', - 'methodname' => 'delete_groupings', - 'classpath' => 'group/externallib.php', - 'description' => 'Deletes all specified groupings.', - 'type' => 'write', + 'classname' => 'core_group_external', + 'methodname' => 'delete_groupings', + 'classpath' => 'group/externallib.php', + 'description' => 'Deletes all specified groupings.', + 'type' => 'write', ), 'core_group_assign_grouping' => array( - 'classname' => 'core_group_external', - 'methodname' => 'assign_grouping', - 'classpath' => 'group/externallib.php', - 'description' => 'Assing groups from groupings', - 'type' => 'write', + 'classname' => 'core_group_external', + 'methodname' => 'assign_grouping', + 'classpath' => 'group/externallib.php', + 'description' => 'Assing groups from groupings', + 'type' => 'write', ), 'core_group_unassign_grouping' => array( - 'classname' => 'core_group_external', - 'methodname' => 'unassign_grouping', - 'classpath' => 'group/externallib.php', - 'description' => 'Unassing groups from groupings', - 'type' => 'write', + 'classname' => 'core_group_external', + 'methodname' => 'unassign_grouping', + 'classpath' => 'group/externallib.php', + 'description' => 'Unassing groups from groupings', + 'type' => 'write', ), // === file related functions === From 2c8ad38effa7389655c4a99d28cfe466d09b51be Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Wed, 16 May 2012 10:45:40 +0200 Subject: [PATCH 10/10] MDL-32662 core_group_xxx_groupings: Fixed integrator review potential problem detected in update_groupings. More info: http://tracker.moodle.org/browse/MDL-32662?focusedCommentId=157706&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-157706 --- group/externallib.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/group/externallib.php b/group/externallib.php index 0ff3b5157dc..b0e7ce9077a 100644 --- a/group/externallib.php +++ b/group/externallib.php @@ -574,7 +574,7 @@ class core_group_external extends external_api { /** * Create groupings - * + * * @param array $groupings array of grouping description arrays (with keys groupname and courseid) * @return array of newly created groupings * @since Moodle 2.3 @@ -690,6 +690,13 @@ class core_group_external extends external_api { if (! $currentgrouping = $DB->get_record('groupings', array('id'=>$grouping->id))) { throw new invalid_parameter_exception("Grouping $grouping->id does not exist in the course"); } + + // Check if the new modified grouping name already exists in the course. + if ($grouping->name != $currentgrouping->name and + $DB->count_records('groupings', array('courseid'=>$currentgrouping->courseid, 'name'=>$grouping->name))) { + throw new invalid_parameter_exception('A different grouping with the same name already exists in the course'); + } + $grouping->courseid = $currentgrouping->courseid; // Now security checks.