Skip to content

Commit

Permalink
MDL-67116 form: Require grade in multi-grade-item activities
Browse files Browse the repository at this point in the history
AMOS BEGIN
  CPY [rating,rating],[grade_rating_name,rating]
AMOS END
  • Loading branch information
andrewnicols committed Nov 13, 2019
1 parent a492f69 commit fe795b5
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 29 deletions.
3 changes: 2 additions & 1 deletion course/modlib.php
Expand Up @@ -411,7 +411,7 @@ function set_moduleinfo_defaults($moduleinfo) {
// Convert the 'use grade' checkbox into a grade-item number: 0 if checked, null if not.
if (isset($moduleinfo->completionusegrade) && $moduleinfo->completionusegrade) {
$moduleinfo->completiongradeitemnumber = 0;
} else {
} else if (!isset($moduleinfo->completiongradeitemnumber)) {
$moduleinfo->completiongradeitemnumber = null;
}

Expand Down Expand Up @@ -684,6 +684,7 @@ function get_moduleinfo_data($cm, $course) {
$data->completionview = $cm->completionview;
$data->completionexpected = $cm->completionexpected;
$data->completionusegrade = is_null($cm->completiongradeitemnumber) ? 0 : 1;
$data->completiongradeitemnumber = $cm->completiongradeitemnumber;
$data->showdescription = $cm->showdescription;
$data->tags = core_tag_tag::get_item_tags_array('core', 'course_modules', $cm->id);
if (!empty($CFG->enableavailability)) {
Expand Down
89 changes: 69 additions & 20 deletions course/moodleform_mod.php
Expand Up @@ -370,6 +370,9 @@ function definition_after_data() {
if ($mform->elementExists('completionusegrade')) {
$mform->freeze('completionusegrade');
}
if ($mform->elementExists('completiongradeitemnumber')) {
$mform->freeze('completiongradeitemnumber');
}
$mform->freeze($this->_customcompletionelements);
}
}
Expand Down Expand Up @@ -409,7 +412,6 @@ function validation($data, $files) {
}
}

$hasgradedefined = false;
$component = "mod_{$this->_modname}";
$itemnames = component_gradeitems::get_itemname_mapping_for_component($component);
foreach ($itemnames as $itemnumber => $itemname) {
Expand Down Expand Up @@ -451,23 +453,45 @@ function validation($data, $files) {
}
}

// We have a grade if we've already found one, or the 'assessed' field is set to a non falsey value (this is for
// ratings), or the grade field name is set to a non falsey value (this is all other grading items).
$hasgradedefined = $hasgradedefined || !empty($data[$assessedfieldname]) || !empty($data[$gradefieldname]);
}

if (!empty($data['completionusegrade']) && !$hasgradedefined) {
$errors['completionusegrade'] = get_string('badcompletionusegrade', 'completion');
// We have a grade if there is a non-falsey value for:
// - the assessedfieldname for Ratings there; or
// - the gradefieldname for Ratings there.
if (empty($data[$assessedfieldname]) && empty($data[$gradefieldname])) {
// There are no grades set therefore completion is not allowed.
if (isset($data['completiongradeitemnumber']) && $data['completiongradeitemnumber'] == (string) $itemnumber) {
$errors['completiongradeitemnumber'] = get_string(
'badcompletiongradeitemnumber',
'completion',
get_string("grade_{$itemname}_name", $component)
);
}
}
}

// Completion: Don't let them choose automatic completion without turning
// on some conditions. Ignore this check when completion settings are
// locked, as the options are then disabled.
if (array_key_exists('completion', $data) &&
$data['completion'] == COMPLETION_TRACKING_AUTOMATIC &&
!empty($data['completionunlocked'])) {
if (empty($data['completionview']) && empty($data['completionusegrade']) &&
!$this->completion_rule_enabled($data)) {
$automaticcompletion = array_key_exists('completion', $data);
$automaticcompletion = $automaticcompletion && $data['completion'] == COMPLETION_TRACKING_AUTOMATIC;
$automaticcompletion = $automaticcompletion && !empty($data['completionunlocked']);

if ($automaticcompletion) {
// View to complete.
$rulesenabled = !empty($data['completionview']);

// Use grade to complete (only one grade item).
$rulesenabled = $rulesenabled || !empty($data['completionusegrade']);

// Use grade to complete (specific grade item).
if (!$rulesenabled && isset($data['completiongradeitemnumber'])) {
$rulesenabled = $data['completiongradeitemnumber'] != '';
}

// Module-specific completion rules.
$rulesenabled = $rulesenabled || $this->completion_rule_enabled($data);

if (!$rulesenabled) {
// No rules are enabled. Can't set automatically completed without rules.
$errors['completion'] = get_string('badautocompletion', 'completion');
}
}
Expand Down Expand Up @@ -666,28 +690,53 @@ protected function standard_coursemodule_elements() {
$gotcompletionoptions = true;
}

// Automatic completion once it's graded
if (plugin_supports('mod', $this->_modname, FEATURE_GRADE_HAS_GRADE, false)) {
$mform->addElement('checkbox', 'completionusegrade', get_string('completionusegrade', 'completion'),
get_string('completionusegrade_desc', 'completion'));
$mform->hideIf('completionusegrade', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
$mform->addHelpButton('completionusegrade', 'completionusegrade', 'completion');
// This activity supports grading.
$gotcompletionoptions = true;

$component = "mod_{$this->_modname}";
$itemnames = component_gradeitems::get_itemname_mapping_for_component($component);

if (count($itemnames) === 1) {
// Only add disable if logic if we've got exactly one grade item since we can't do
// make the disable conditional on multiple elements.
// Only one gradeitem in this activity.
// We use the completionusegrade field here.
$mform->addElement(
'checkbox',
'completionusegrade',
get_string('completionusegrade', 'completion'),
get_string('completionusegrade_desc', 'completion')
);
$mform->hideIf('completionusegrade', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
$mform->addHelpButton('completionusegrade', 'completionusegrade', 'completion');

// The disabledIf logic differs between ratings and other grade items due to different field types.
if ($this->_features->rating) {
// If using the rating system, there is no grade unless ratings are enabled.
$mform->disabledIf('completionusegrade', 'assessed', 'eq', 0);
} else {
// All other field types use the '$gradefieldname' field's modgrade_type.
$itemnumbers = array_keys($itemnames);
$itemnumber = array_shift($itemnumbers);
$gradefieldname = component_gradeitems::get_field_name_for_itemnumber($component, $itemnumber, 'grade');
$mform->disabledIf('completionusegrade', "{$gradefieldname}[modgrade_type]", 'eq', 'none');
}
} else if (count($itemnames) > 1) {
// There are multiple grade items in this activity.
// Show them all.
$options = [
'' => get_string('activitygradenotrequired', 'completion'),
];
foreach ($itemnames as $itemnumber => $itemname) {
$options[$itemnumber] = get_string("grade_{$itemname}_name", $component);
}

$mform->addElement(
'select',
'completiongradeitemnumber',
get_string('completionusegrade', 'completion'),
$options
);
$mform->hideIf('completiongradeitemnumber', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
}
}

Expand Down
3 changes: 2 additions & 1 deletion lang/en/completion.php
Expand Up @@ -34,14 +34,15 @@
$string['activitiescompletednote'] = 'Note: Activity completion must be set for an activity to appear in the above list.';
$string['activitycompletion'] = 'Activity completion';
$string['activitycompletionupdated'] = 'Changes saved';
$string['activitygradenotrequired'] = 'Grade not required';
$string['affectedactivities'] = 'The changes will affect the following <b>{$a}</b> activities or resources:';
$string['aggregationmethod'] = 'Aggregation method';
$string['all'] = 'All';
$string['any'] = 'Any';
$string['approval'] = 'Approval';
$string['areyousureoverridecompletion'] = 'Are you sure you want to override the current completion state of this activity for this user and mark it "{$a}"?';
$string['badautocompletion'] = 'When you select automatic completion, you must also enable at least one requirement (below).';
$string['badcompletionusegrade'] = 'When you select require grade, you must also enable at least one grade item.';
$string['badcompletiongradeitemnumber'] = 'Require grade can\'t be enabled for <b>{$a}</b> because grading by {$a} is not enabled.';
$string['bulkactivitycompletion'] = 'Bulk edit activity completion';
$string['bulkactivitydetail'] = 'Select the activities you wish to bulk edit.';
$string['bulkcompletiontracking'] = 'Completion tracking';
Expand Down
15 changes: 8 additions & 7 deletions mod/forum/lang/en/forum.php
Expand Up @@ -738,26 +738,27 @@
$string['yourreply'] = 'Your reply';
$string['forumsubjectdeleted'] = 'This forum post has been removed';
$string['forumbodydeleted'] = 'The content of this forum post has been removed and can no longer be accessed.';
$string['gradeusers'] = 'Grade users';
$string['forumgrader'] = 'Forum grader';
$string['grading'] = 'Grading';
$string['viewconversation'] = 'View discussion';

$string['grade_forum_header'] = 'Whole forum grading';
$string['grade_forum_name'] = 'Whole forum';
$string['grade_forum_title'] = 'Grade';
$string['gradingstatus'] = 'Grade status:';
$string['grade_rating_name'] = 'Rating';
$string['gradeusers'] = 'Grade users';
$string['graded'] = 'Graded';
$string['notgraded'] = 'Not graded';
$string['gradeforrating'] = 'Grade for rating: {$a->str_long_grade}';
$string['gradeforratinghidden'] = 'Grade for rating hidden';
$string['gradeforwholeforum'] = 'Grade for forum: {$a->str_long_grade}';
$string['grading'] = 'Grading';
$string['gradingstatus'] = 'Grade status:';
$string['gradeforwholeforumhidden'] = 'Grade for forum hidden';
$string['gradeitemnameforwholeforum'] = 'Whole forum grade for {$a->name}';
$string['gradeitemnameforrating'] = 'Rating grade for {$a->name}';
$string['grades:gradesavedfor'] = 'Grade saved for {$a->fullname}';
$string['grades:gradesavefailed'] = 'Unable to save grade for {$a->fullname}: {$a->error}';
$string['showmoreusers'] = 'Show more users';
$string['notgraded'] = 'Not graded';
$string['nousersmatch'] = 'No user(s) found for given criteria';
$string['showmoreusers'] = 'Show more users';
$string['viewconversation'] = 'View conversation';
$string['viewgrades'] = 'View grades';

// Deprecated since Moodle 3.8.
Expand Down

0 comments on commit fe795b5

Please sign in to comment.