Skip to content

Commit

Permalink
MDL-70935 mod_quiz: Custom completion implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaies authored and mickhawkins committed Apr 7, 2021
1 parent e5ac4fd commit 26856f4
Show file tree
Hide file tree
Showing 3 changed files with 424 additions and 0 deletions.
97 changes: 97 additions & 0 deletions mod/quiz/classes/completion/custom_completion.php
@@ -0,0 +1,97 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

declare(strict_types=1);

namespace mod_quiz\completion;

use core_completion\activity_custom_completion;

/**
* Activity custom completion subclass for the quiz activity.
*
* Class for defining mod_quiz's custom completion rules and fetching the completion statuses
* of the custom completion rules for a given quiz instance and a user.
*
* @package mod_quiz
* @copyright 2021 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class custom_completion extends activity_custom_completion {

/**
* Fetches the completion state for a given completion rule.
*
* @param string $rule The completion rule.
* @return int The completion state.
*/
public function get_state(string $rule): int {
global $DB;

$this->validate_rule($rule);

$quiz = $DB->get_record('quiz', ['id' => $this->cm->instance], '*', MUST_EXIST);

switch ($rule) {
case 'completionpassorattemptsexhausted':
$status = quiz_completion_check_passing_grade_or_all_attempts(
$this->cm->get_course(),
$this->cm,
$this->userid,
$quiz
);
break;
case 'completionminattempts':
$status = quiz_completion_check_min_attempts($this->userid, $quiz);
break;
}

return empty($status) ? COMPLETION_INCOMPLETE : COMPLETION_COMPLETE;
}

/**
* Fetch the list of custom completion rules that this module defines.
*
* @return array
*/
public static function get_defined_custom_rules(): array {
return [
'completionpassorattemptsexhausted',
'completionminattempts',
];
}

/**
* Returns an associative array of the descriptions of custom completion rules.
*
* @return array
*/
public function get_custom_rule_descriptions(): array {
$minattempts = $this->cm->customdata['customcompletionrules']['completionminattempts'];

$completionpassorattempts = $this->cm->customdata['customcompletionrules']['completionpassorattemptsexhausted'];
if (!empty($completionpassorattempts['completionattemptsexhausted'])) {
$passorallattemptslabel = get_string('completiondetail:passorexhaust', 'mod_quiz');
} else {
$passorallattemptslabel = get_string('completiondetail:passgrade', 'mod_quiz');
}

return [
'completionpassorattemptsexhausted' => $passorallattemptslabel,
'completionminattempts' => get_string('completiondetail:minattempts', 'mod_quiz', $minattempts),
];
}
}
3 changes: 3 additions & 0 deletions mod/quiz/lang/en/quiz.php
Expand Up @@ -179,6 +179,9 @@
$string['commentorgrade'] = 'Make comment or override grade';
$string['comments'] = 'Comments';
$string['completedon'] = 'Completed on';
$string['completiondetail:minattempts'] = 'Make attempts: {$a}';
$string['completiondetail:passgrade'] = 'Receive a pass grade';
$string['completiondetail:passorexhaust'] = 'Receive a pass grade or complete all available attempts';
$string['completionminattempts'] = 'Minimum number of attempts:';
$string['completionminattemptsdesc'] = 'Minimum number of attempts required: {$a}';
$string['completionminattemptsgroup'] = 'Require attempts';
Expand Down

0 comments on commit 26856f4

Please sign in to comment.