MDL-48634 grades: Add an option to rescale when changing the maxgrade

This commit is contained in:
Damyon Wiese 2015-04-15 07:57:28 -04:00 committed by Mark Nelson
parent 9d5d9c64ff
commit d629c601c5
12 changed files with 422 additions and 11 deletions

View file

@ -453,6 +453,11 @@ function can_update_moduleinfo($cm) {
function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) {
global $DB, $CFG;
$data = new stdClass();
if ($mform) {
$data = $mform->get_data();
}
// Attempt to include module library before we make any changes to DB.
include_modulelib($moduleinfo->modulename);
@ -523,9 +528,44 @@ function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) {
$moduleinfo->introformat = $moduleinfo->introeditor['format'];
unset($moduleinfo->introeditor);
}
// Get the a copy of the grade_item before it is modified incase we need to scale the grades.
$oldgradeitem = null;
$newgradeitem = null;
if (!empty($data->grade_rescalegrades)) {
// Fetch the grade item before it is updated.
$oldgradeitem = grade_item::fetch(array('itemtype' => 'mod',
'itemmodule' => $moduleinfo->modulename,
'iteminstance' => $moduleinfo->instance,
'itemnumber' => 0,
'courseid' => $moduleinfo->course));
}
$updateinstancefunction = $moduleinfo->modulename."_update_instance";
if (!$updateinstancefunction($moduleinfo, $mform)) {
print_error('cannotupdatemod', '', course_get_url($course, $cw->section), $moduleinfo->modulename);
print_error('cannotupdatemod', '', course_get_url($course, $cm->section), $moduleinfo->modulename);
}
// This needs to happen AFTER the grademin/grademax have already been updated.
if (!empty($data->grade_rescalegrades)) {
// Get the grade_item after the update call the activity to scale the grades.
$newgradeitem = grade_item::fetch(array('itemtype' => 'mod',
'itemmodule' => $moduleinfo->modulename,
'iteminstance' => $moduleinfo->instance,
'itemnumber' => 0,
'courseid' => $moduleinfo->course));
if ($newgradeitem && $oldgradeitem->gradetype == GRADE_TYPE_VALUE && $newgradeitem->gradetype == GRADE_TYPE_VALUE) {
$params = array(
$course,
$cm,
$oldgradeitem->grademin,
$oldgradeitem->grademax,
$newgradeitem->grademin,
$newgradeitem->grademax
);
if (!component_callback('mod_' . $moduleinfo->modulename, 'rescale_activity_grades', $params)) {
print_error('cannotreprocessgrades', '', course_get_url($course, $cm->section), $moduleinfo->modulename);
}
}
}
// Make sure visibility is set correctly (in particular in calendar).

View file

@ -109,9 +109,9 @@ abstract class moodleform_mod extends moodleform {
$this->_features->defaultcompletion = plugin_supports('mod', $this->_modname, FEATURE_MODEDIT_DEFAULT_COMPLETION, true);
$this->_features->rating = plugin_supports('mod', $this->_modname, FEATURE_RATE, false);
$this->_features->showdescription = plugin_supports('mod', $this->_modname, FEATURE_SHOW_DESCRIPTION, false);
$this->_features->gradecat = ($this->_features->outcomes or $this->_features->hasgrades);
$this->_features->advancedgrading = plugin_supports('mod', $this->_modname, FEATURE_ADVANCED_GRADING, false);
$this->_features->canrescale = (component_callback_exists('mod_' . $this->_modname, 'rescale_activity_grades') !== false);
}
/**
@ -641,6 +641,11 @@ abstract class moodleform_mod extends moodleform {
public function standard_grading_coursemodule_elements() {
global $COURSE, $CFG;
$mform =& $this->_form;
$isupdate = !empty($this->_cm);
$gradeoptions = array('isupdate' => $isupdate,
'currentgrade' => false,
'hasgrades' => false,
'canrescale' => $this->_features->canrescale);
if ($this->_features->hasgrades) {
@ -650,7 +655,18 @@ abstract class moodleform_mod extends moodleform {
//if supports grades and grades arent being handled via ratings
if (!$this->_features->rating) {
$mform->addElement('modgrade', 'grade', get_string('grade'));
if ($isupdate) {
$gradeitem = grade_item::fetch(array('itemtype' => 'mod',
'itemmodule' => $this->_cm->modname,
'iteminstance' => $this->_cm->instance,
'itemnumber' => 0,
'courseid' => $COURSE->id));
$gradeoptions['currentgrade'] = $gradeitem->grademax;
$gradeoptions['hasgrades'] = $gradeitem->has_grades();
}
$mform->addElement('modgrade', 'grade', get_string('grade'), $gradeoptions);
$mform->addHelpButton('grade', 'modgrade', 'grades');
$mform->setDefault('grade', $CFG->gradepointdefault);
}