Skip to content

Commit

Permalink
MDL-82124 mod_assign: recalculate penalty
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Nguyen committed Aug 30, 2024
1 parent ca4f081 commit 1ea7ad5
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lang/en/grades.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@
$string['modgradeerrorbadpoint'] = 'Invalid grade value. This must be an integer between 1 and {$a}';
$string['modgradeerrorbadscale'] = 'Invalid scale selected. Please make sure you select a scale from the selections below.';
$string['modgrademaxgrade'] = 'Maximum grade';
$string['modgraderecalculatepenalty'] = 'Recalculate penalty';
$string['modgraderecalculatepenalty_help'] = 'The penalty will be recalculated for all users.';
$string['modgraderescalegrades'] = 'Rescale existing grades';
$string['modgraderescalegrades_help'] = 'When changing the maximum grades on a gradebook item you need to specify whether or not this will cause existing percentage grades to change as well.
Expand Down
1 change: 1 addition & 0 deletions mod/assign/lang/en/assign.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@
$string['page-mod-assign-view'] = 'Assignment module main and submission page';
$string['paramtimeremaining'] = '{$a} remaining';
$string['participant'] = 'Participant';
$string['penaltyduedatechangemessage'] = 'Some grades have already been awarded. In order to change the due date, you must first choose whether or not to recalculate existing grades.';
$string['pluginadministration'] = 'Assignment administration';
$string['pluginname'] = 'Assignment';
$string['preventsubmissionnotingroup'] = 'Require group to make submission';
Expand Down
7 changes: 7 additions & 0 deletions mod/assign/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,13 @@ public function update_instance($formdata) {
$update->nosubmissions = (!$this->is_any_submission_plugin_enabled()) ? 1: 0;
$DB->update_record('assign', $update);

// Check if we need to recalculate penalty for existing grades.
if (!empty($formdata->recalculatepenalty) && $formdata->recalculatepenalty === 'yes') {
$assign = clone $this->get_instance();
$assign->cmidnumber = $this->get_course_module()->idnumber;
assign_update_grades($assign);
}

return $result;
}

Expand Down
37 changes: 36 additions & 1 deletion mod/assign/mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class mod_assign_mod_form extends moodleform_mod {
* @return void
*/
public function definition() {
global $CFG, $COURSE, $DB;
global $CFG, $COURSE, $DB, $OUTPUT;;
$mform = $this->_form;

$mform->addElement('header', 'general', get_string('general', 'form'));
Expand Down Expand Up @@ -95,9 +95,23 @@ public function definition() {
$mform->addElement('date_time_selector', 'allowsubmissionsfromdate', $name, $options);
$mform->addHelpButton('allowsubmissionsfromdate', 'allowsubmissionsfromdate', 'assign');

// Add the option to recalculate the penalty if there is existing grade.
if (\mod_assign\penalty\helper::is_penalty_enabled($assignment->get_instance()->id) && $assignment->count_grades() > 0) {
// Create notification.
$notice = $OUTPUT->notification(get_string('penaltyduedatechangemessage', 'assign'), 'warning', false);
$mform->addElement('html', $notice);
$mform->addElement('select', 'recalculatepenalty', get_string('modgraderecalculatepenalty', 'grades'), [
'' => get_string('choose'),
'no' => get_string('no'),
'yes' => get_string('yes'),
]);
$mform->addHelpButton('recalculatepenalty', 'modgraderecalculatepenalty', 'grades');
}

$name = get_string('duedate', 'assign');
$mform->addElement('date_time_selector', 'duedate', $name, array('optional'=>true));
$mform->addHelpButton('duedate', 'duedate', 'assign');
$mform->disabledIf('duedate', 'recalculatepenalty', 'eq', '');

$name = get_string('cutoffdate', 'assign');
$mform->addElement('date_time_selector', 'cutoffdate', $name, array('optional'=>true));
Expand Down Expand Up @@ -255,6 +269,9 @@ public function definition() {

// Hide if the grade type is not set to point.
$mform->hideIf('gradepenalty', 'grade[modgrade_type]', 'neq', 'point');

// Disable if the recalculate penalty is not set.
$mform->disabledIf('gradepenalty', 'recalculatepenalty', 'eq', '');
}

$this->standard_coursemodule_elements();
Expand All @@ -263,6 +280,24 @@ public function definition() {
$this->add_action_buttons();
}

/**
* Override definition after data has been set.
*
* The value of date time selector will be lost in a POST request, if the selector is disabled.
* So, we need to set the value again.
*
* return void
*/
function definition_after_data() {
$mform = $this->_form;

// The value of date time selector will be lost in a POST request.
$recalculatepenalty = optional_param('recalculatepenalty', null, PARAM_TEXT);
if ($recalculatepenalty === '') {
$mform->setConstant('duedate', $mform->_defaultValues['duedate']);
}
}

/**
* Perform minimal validation on the settings form
* @param array $data
Expand Down
32 changes: 32 additions & 0 deletions mod/assign/override_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,22 @@ protected function definition() {
get_string('allowsubmissionsfromdate', 'assign'), array('optional' => true));
$mform->setDefault('allowsubmissionsfromdate', $assigninstance->allowsubmissionsfromdate);

// Add the option to recalculate the penalty if there is existing grade.
if ($this->assign->count_grades() > 0) {
// Create notification.
$notice = $OUTPUT->notification(get_string('penaltyduedatechangemessage', 'assign'), 'warning', false);
$mform->addElement('html', $notice);
$mform->addElement('select', 'recalculatepenalty', get_string('modgraderecalculatepenalty', 'grades'), [
'' => get_string('choose'),
'no' => get_string('no'),
'yes' => get_string('yes'),
]);
$mform->addHelpButton('recalculatepenalty', 'modgraderecalculatepenalty', 'grades');
}

$mform->addElement('date_time_selector', 'duedate', get_string('duedate', 'assign'), array('optional' => true));
$mform->setDefault('duedate', $assigninstance->duedate);
$mform->disabledIf('duedate', 'recalculatepenalty', 'eq', '');

$mform->addElement('date_time_selector', 'cutoffdate', get_string('cutoffdate', 'assign'), array('optional' => true));
$mform->setDefault('cutoffdate', $assigninstance->cutoffdate);
Expand Down Expand Up @@ -286,6 +300,24 @@ protected function definition() {

}

/**
* Override definition after data has been set.
*
* The value of date time selector will be lost in a POST request, if the selector is disabled.
* So, we need to set the value again.
*
* return void
*/
function definition_after_data() {
$mform = $this->_form;

// The value of date time selector will be lost in a POST request.
$recalculatepenalty = optional_param('recalculatepenalty', null, PARAM_TEXT);
if ($recalculatepenalty === '') {
$mform->setConstant('duedate', $mform->_defaultValues['duedate']);
}
}

/**
* Validate the submitted form data.
*
Expand Down
16 changes: 16 additions & 0 deletions mod/assign/overrideedit.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,22 @@
$event->trigger();
}

// Check if we need to recalculate penalty for existing grades.
if (!empty($fromform->recalculatepenalty) && $fromform->recalculatepenalty === 'yes') {
$assignintance = clone $assign->get_instance();
$assignintance->cmidnumber = $assign->get_course_module()->idnumber;
// If it is user mode.
if (!$groupmode) {
assign_update_grades($assignintance, $fromform->userid);
} else {
// If it is group mode.
$groupmembers = groups_get_members($fromform->groupid);
foreach ($groupmembers as $groupmember) {
assign_update_grades($assignintance, $groupmember->id);
}
}
}

assign_update_events($assign, $fromform);

if (!empty($fromform->submitbutton)) {
Expand Down

0 comments on commit 1ea7ad5

Please sign in to comment.