Skip to content
Browse files

MDL-32662 Added update_groupings method

  • Loading branch information...
1 parent 882bac8 commit 87e959f33b16f995eb4c8676d2262ec3f15f4915 @jleyva jleyva committed Apr 30, 2012
Showing with 89 additions and 0 deletions.
  1. +89 −0 group/externallib.php
View
89 group/externallib.php
@@ -637,6 +637,95 @@ public static function create_groupings_returns() {
);
}
+ /**
+ * 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.'
+ );
+ }
+
}
/**

0 comments on commit 87e959f

Please sign in to comment.
Something went wrong with that request. Please try again.