mirror of
https://github.com/moodle/moodle.git
synced 2025-04-21 00:12:56 +02:00
MDL-32662 Added update_groupings method
This commit is contained in:
parent
882bac854f
commit
87e959f33b
@ -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.'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user