Skip to content

Commit

Permalink
MDL-70935 mod_quiz: combine 'pass' and 'attemptsexhausted' rules
Browse files Browse the repository at this point in the history
Custom rules are combined using the AND operator. However, the
'completionpass' and 'completionattemptsexhausted' rules are exceptions
because they are combined together with OR, and form a single new rule
that then will be combined with the rest of the rules using AND.
This commit combines the mentioned rules into a single rule named
'completionpassorattemptsexhausted'.
  • Loading branch information
rezaies authored and mickhawkins committed Apr 7, 2021
1 parent 1ef67ca commit e5ac4fd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
5 changes: 4 additions & 1 deletion mod/quiz/lang/en/quiz.php
Expand Up @@ -186,8 +186,8 @@
$string['completionpass'] = 'Require passing grade';
$string['completionpassdesc'] = 'Student must achieve a passing grade to complete this activity';
$string['completionpass_help'] = 'If enabled, this activity is considered complete when the student receives a pass grade (as specified in the Grade section of the quiz settings) or higher.';
$string['completionpassorattemptsexhausteddesc'] = 'Student must achieve a passing grade, or exhaust all available attempts to complete this activity';
$string['completionattemptsexhausted'] = 'Or all available attempts completed';
$string['completionattemptsexhausteddesc'] = 'Complete if all available attempts are exhausted';
$string['completionattemptsexhausted_help'] = 'Mark quiz complete when the student has exhausted the maximum number of attempts.';
$string['configadaptive'] = 'If you choose Yes for this option then the student will be allowed multiple responses to a question even within the same attempt at the quiz.';
$string['configattemptsallowed'] = 'Restriction on the number of attempts students are allowed at the quiz.';
Expand Down Expand Up @@ -1018,3 +1018,6 @@
$string['reviewofattempt'] = 'Review of attempt {$a}';
$string['reviewofpreview'] = 'Review of preview';
$string['settingsoverrides'] = 'Settings overrides';

// Deprecated since Moodle 3.11.
$string['completionattemptsexhausteddesc'] = 'Complete if all available attempts are exhausted';
55 changes: 31 additions & 24 deletions mod/quiz/lib.php
Expand Up @@ -1949,8 +1949,6 @@ function quiz_completion_check_passing_grade_or_all_attempts($course, $cm, $user
* @throws coding_exception
*/
function quiz_completion_check_min_attempts($userid, $quiz) {
global $DB;

if (empty($quiz->completionminattempts)) {
return true;
}
Expand Down Expand Up @@ -2174,7 +2172,7 @@ function quiz_get_coursemodule_info($coursemodule) {
global $DB;

$dbparams = ['id' => $coursemodule->instance];
$fields = 'id, name, intro, introformat, completionattemptsexhausted, completionpass';
$fields = 'id, name, intro, introformat, completionattemptsexhausted, completionpass, completionminattempts';
if (!$quiz = $DB->get_record('quiz', $dbparams, $fields)) {
return false;
}
Expand All @@ -2189,8 +2187,16 @@ function quiz_get_coursemodule_info($coursemodule) {

// Populate the custom completion rules as key => value pairs, but only if the completion mode is 'automatic'.
if ($coursemodule->completion == COMPLETION_TRACKING_AUTOMATIC) {
$result->customdata['customcompletionrules']['completionattemptsexhausted'] = $quiz->completionattemptsexhausted;
$result->customdata['customcompletionrules']['completionpass'] = $quiz->completionpass;
if ($quiz->completionpass || $quiz->completionattemptsexhausted) {
$result->customdata['customcompletionrules']['completionpassorattemptsexhausted'] = [
'completionpass' => $quiz->completionpass,
'completionattemptsexhausted' => $quiz->completionattemptsexhausted,
];
} else {
$result->customdata['customcompletionrules']['completionpassorattemptsexhausted'] = [];
}

$result->customdata['customcompletionrules']['completionminattempts'] = $quiz->completionminattempts;
}

return $result;
Expand All @@ -2210,27 +2216,28 @@ function mod_quiz_get_completion_active_rule_descriptions($cm) {
}

$descriptions = [];
foreach ($cm->customdata['customcompletionrules'] as $key => $val) {
switch ($key) {
case 'completionattemptsexhausted':
if (!empty($val)) {
$descriptions[] = get_string('completionattemptsexhausteddesc', 'quiz');
}
break;
case 'completionpass':
if (!empty($val)) {
$descriptions[] = get_string('completionpassdesc', 'quiz', format_time($val));
}
break;
case 'completionminattempts':
if (!empty($val)) {
$descriptions[] = get_string('completionminattemptsdesc', 'quiz', $val);
}
break;
default:
break;
$rules = $cm->customdata['customcompletionrules'];

if (!empty($rules['completionpassorattemptsexhausted'])) {
if (!empty($rules['completionpassorattemptsexhausted']['completionattemptsexhausted'])) {
$descriptions[] = get_string('completionpassorattemptsexhausteddesc', 'quiz');
} else if (!empty($rules['completionpassorattemptsexhausted']['completionpass'])) {
$descriptions[] = get_string('completionpassdesc', 'quiz',
format_time($rules['completionpassorattemptsexhausted']['completionpass']));
}
} else {
// Fallback.
if (!empty($rules['completionattemptsexhausted'])) {
$descriptions[] = get_string('completionpassorattemptsexhausteddesc', 'quiz');
} else if (!empty($rules['completionpass'])) {
$descriptions[] = get_string('completionpassdesc', 'quiz', format_time($rules['completionpass']));
}
}

if (!empty($rules['completionminattempts'])) {
$descriptions[] = get_string('completionminattemptsdesc', 'quiz', $rules['completionminattempts']);
}

return $descriptions;
}

Expand Down
3 changes: 1 addition & 2 deletions mod/quiz/tests/lib_test.php
Expand Up @@ -1134,8 +1134,7 @@ public function test_mod_quiz_completion_get_active_rule_descriptions() {
$moddefaults->completion = 2;

$activeruledescriptions = [
get_string('completionattemptsexhausteddesc', 'quiz'),
get_string('completionpassdesc', 'quiz'),
get_string('completionpassorattemptsexhausteddesc', 'quiz'),
];
$this->assertEquals(mod_quiz_get_completion_active_rule_descriptions($cm1), $activeruledescriptions);
$this->assertEquals(mod_quiz_get_completion_active_rule_descriptions($cm2), []);
Expand Down

0 comments on commit e5ac4fd

Please sign in to comment.