MDL-67306 webservices: Added webservice for Grade category creation

This commit is contained in:
Peter Burnett
2020-09-03 00:20:44 +02:00
committed by Eloy Lafuente (stronk7)
parent 1250e912c7
commit 309f54bf81
6 changed files with 392 additions and 133 deletions
+164
View File
@@ -27,6 +27,7 @@ defined('MOODLE_INTERNAL') || die;
require_once("$CFG->libdir/externallib.php");
require_once("$CFG->libdir/gradelib.php");
require_once("$CFG->dirroot/grade/edit/tree/lib.php");
require_once("$CFG->dirroot/grade/querylib.php");
/**
@@ -570,4 +571,167 @@ class core_grades_external extends external_api {
as defined in lib/grade/constants.php'
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.10
*/
public static function create_gradecategory_parameters() {
return new external_function_parameters(
[
'courseid' => new external_value(PARAM_INT, 'id of course', VALUE_REQUIRED),
'fullname' => new external_value(PARAM_TEXT, 'fullname of category', VALUE_REQUIRED),
'options' => new external_single_structure([
'aggregation' => new external_value(PARAM_INT, 'aggregation method', VALUE_OPTIONAL),
'aggregateonlygraded' => new external_value(PARAM_BOOL, 'exclude empty grades', VALUE_OPTIONAL),
'aggregateoutcomes' => new external_value(PARAM_BOOL, 'aggregate outcomes', VALUE_OPTIONAL),
'droplow' => new external_value(PARAM_INT, 'drop low grades', VALUE_OPTIONAL),
'itemname' => new external_value(PARAM_TEXT, 'the category total name', VALUE_OPTIONAL),
'iteminfo' => new external_value(PARAM_TEXT, 'the category iteminfo', VALUE_OPTIONAL),
'idnumber' => new external_value(PARAM_TEXT, 'the category idnumber', VALUE_OPTIONAL),
'gradetype' => new external_value(PARAM_INT, 'the grade type', VALUE_OPTIONAL),
'grademax' => new external_value(PARAM_INT, 'the grade max', VALUE_OPTIONAL),
'grademin' => new external_value(PARAM_INT, 'the grade min', VALUE_OPTIONAL),
'gradepass' => new external_value(PARAM_INT, 'the grade to pass', VALUE_OPTIONAL),
'display' => new external_value(PARAM_INT, 'the display type', VALUE_OPTIONAL),
'decimals' => new external_value(PARAM_INT, 'the decimal count', VALUE_OPTIONAL),
'hiddenuntil' => new external_value(PARAM_INT, 'grades hidden until', VALUE_OPTIONAL),
'locktime' => new external_value(PARAM_INT, 'lock grades after', VALUE_OPTIONAL),
'weightoverride' => new external_value(PARAM_BOOL, 'weight adjusted', VALUE_OPTIONAL),
'aggregationcoef2' => new external_value(PARAM_RAW, 'weight coefficient', VALUE_OPTIONAL),
'parentcategoryid' => new external_value(PARAM_INT, 'The parent category id', VALUE_OPTIONAL),
'parentcategoryidnumber' => new external_value(PARAM_TEXT, 'the parent category idnumber', VALUE_OPTIONAL),
], 'optional category data', VALUE_DEFAULT, [])
]
);
}
/**
* Creates a gradecategory inside of the specified course.
*
* @param int $courseid the courseid to create the gradecategory in.
* @param string $fullname the fullname of the grade category to create.
* @param array $options array of options to set.
*
* @return array array of created categoryid and warnings.
*/
public static function create_gradecategory(int $courseid, string $fullname, array $options) {
global $CFG, $DB;
$params = self::validate_parameters(self::create_gradecategory_parameters(),
['courseid' => $courseid, 'fullname' => $fullname, 'options' => $options]);
// Now params are validated, update the references.
$courseid = $params['courseid'];
$fullname = $params['fullname'];
$options = $params['options'];
// Check that the context and permissions are OK.
$context = context_course::instance($courseid);
self::validate_context($context);
require_capability('moodle/grade:manage', $context);
$defaultparentcat = new grade_category(['courseid' => $courseid, 'depth' => 1], true);
// Setup default data so WS call needs to contain only data to set.
// This is not done in the Parameters, so that the array of options can be optional.
$data = [
'fullname' => $fullname,
'aggregation' => grade_get_setting($courseid, 'displaytype', $CFG->grade_displaytype),
'aggregateonlygraded' => 1,
'aggregateoutcomes' => 0,
'droplow' => 0,
'grade_item_itemname' => '',
'grade_item_iteminfo' => '',
'grade_item_idnumber' => '',
'grade_item_gradetype' => GRADE_TYPE_VALUE,
'grade_item_grademax' => 100,
'grade_item_grademin' => 1,
'grade_item_gradepass' => 1,
'grade_item_display' => GRADE_DISPLAY_TYPE_DEFAULT,
// Hack. This must be -2 to use the default setting.
'grade_item_decimals' => -2,
'grade_item_hiddenuntil' => 0,
'grade_item_locktime' => 0,
'grade_item_weightoverride' => 0,
'grade_item_aggregationcoef2' => 0,
'parentcategory' => $defaultparentcat->id
];
// Most of the data items need boilerplate prepended. These are the exceptions.
$ignorekeys = ['aggregation', 'aggregateonlygraded', 'aggregateoutcomes', 'droplow', 'parentcategoryid', 'parentcategoryidnumber'];
foreach ($options as $key => $value) {
if (!in_array($key, $ignorekeys)) {
$fullkey = 'grade_item_' . $key;
$data[$fullkey] = $value;
} else {
$data[$key] = $value;
}
}
// Handle parent category special case.
if (array_key_exists('parentcategoryid', $options) && $parentcat = $DB->get_record('grade_categories',
['id' => $options['parentcategoryid'], 'courseid' => $courseid])) {
$data['parentcategory'] = $parentcat->id;
} else if (array_key_exists('parentcategoryidnumber', $options) && $parentcatgradeitem = $DB->get_record('grade_items',
['itemtype' => 'category', 'idnumber' => $options['parentcategoryidnumber']], '*', IGNORE_MULTIPLE)) {
if ($parentcat = $DB->get_record('grade_categories', ['courseid' => $courseid, 'id' => $parentcatgradeitem->iteminstance])) {
$data['parentcategory'] = $parentcat->id;
}
}
// Create new gradecategory item.
$gradecategory = new grade_category(['courseid' => $courseid], false);
$gradecategory->apply_default_settings();
$gradecategory->apply_forced_settings();
// Data Validation.
if (array_key_exists('grade_item_gradetype', $data) and $data['grade_item_gradetype'] == GRADE_TYPE_SCALE) {
if (empty($data['grade_item_scaleid'])) {
$warnings[] = ['item' => 'scaleid', 'warningcode' => 'invalidscale',
'message' => get_string('missingscale', 'grades')];
}
}
if (array_key_exists('grade_item_grademin', $data) and array_key_exists('grade_item_grademax', $data)) {
if (($data['grade_item_grademax'] != 0 OR $data['grade_item_grademin'] != 0) AND
($data['grade_item_grademax'] == $data['grade_item_grademin'] OR
$data['grade_item_grademax'] < $data['grade_item_grademin'])) {
$warnings[] = ['item' => 'grademax', 'warningcode' => 'invalidgrade',
'message' => get_string('incorrectminmax', 'grades')];
}
}
if (!empty($warnings)) {
return ['categoryid' => null, 'warnings' => $warnings];
}
// Now call the update function with data. Transactioned so the gradebook isn't broken on bad data.
try {
$transaction = $DB->start_delegated_transaction();
grade_edit_tree::update_gradecategory($gradecategory, (object) $data);
$transaction->allow_commit();
} catch (Exception $e) {
// If the submitted data was broken for any reason.
$warnings['database'] = $e->getMessage();
$transaction->rollback();
return ['warnings' => $warnings];
}
return['categoryid' => $gradecategory->id, 'warnings' => []];
}
/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 3.10
*/
public static function create_gradecategory_returns() {
return new external_single_structure([
'categoryid' => new external_value(PARAM_INT, 'The ID of the created category', VALUE_OPTIONAL),
'warnings' => new external_warnings(),
]);
}
}