Skip to content

Commit

Permalink
MDL-32662 Added update_groupings method
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed May 10, 2012
1 parent 882bac8 commit 87e959f
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions group/externallib.php
Expand Up @@ -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.'
);
}

}

/**
Expand Down

0 comments on commit 87e959f

Please sign in to comment.