/** * Create grade item for given setaskment. * * @param stdClass $setask record with extra cmidnumber * @param array $grades optional array/object of grade(s); 'reset' means reset grades in gradebook * @return int 0 if ok, error code otherwise */ function setask_grade_item_update($setask, $grades = null) { global $CFG; require_once $CFG->libdir . '/gradelib.php'; if (!isset($setask->courseid)) { $setask->courseid = $setask->course; } $params = array('itemname' => $setask->name, 'idnumber' => $setask->cmidnumber); // Check if feedback plugin for gradebook is enabled, if yes then // gradetype = GRADE_TYPE_TEXT else GRADE_TYPE_NONE. $gradefeedbackenabled = false; if (isset($setask->gradefeedbackenabled)) { $gradefeedbackenabled = $setask->gradefeedbackenabled; } else { if ($setask->grade == 0) { // Grade feedback is needed only when grade == 0. require_once $CFG->dirroot . '/mod/setask/locallib.php'; $mod = get_coursemodule_from_instance('setask', $setask->id, $setask->courseid); $cm = context_module::instance($mod->id); $setaskment = new setask($cm, null, null); $gradefeedbackenabled = $setaskment->is_gradebook_feedback_enabled(); } } if ($setask->grade > 0) { $params['gradetype'] = GRADE_TYPE_VALUE; $params['grademax'] = $setask->grade; $params['grademin'] = 0; } else { if ($setask->grade < 0) { $params['gradetype'] = GRADE_TYPE_SCALE; $params['scaleid'] = -$setask->grade; } else { if ($gradefeedbackenabled) { // $setask->grade == 0 and feedback enabled. $params['gradetype'] = GRADE_TYPE_TEXT; } else { // $setask->grade == 0 and no feedback enabled. $params['gradetype'] = GRADE_TYPE_NONE; } } } if ($grades === 'reset') { $params['reset'] = true; $grades = null; } return grade_update('mod/setask', $setask->courseid, 'mod', 'setask', $setask->id, 0, $grades, $params); }