mirrored from git://git.moodle.org/moodle.git
/
custom_completion.php
176 lines (155 loc) · 6.07 KB
/
custom_completion.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?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 context_module;
use core_completion\activity_custom_completion;
use grade_grade;
use grade_item;
use quiz;
use quiz_access_manager;
/**
* 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 {
/**
* Check passing grade (or no attempts left) requirement for completion.
*
* @return bool True if the passing grade (or no attempts left) requirement is disabled or met.
*/
protected function check_passing_grade_or_all_attempts(): bool {
global $CFG;
require_once($CFG->libdir . '/gradelib.php');
$completionpassorattempts = $this->cm->customdata['customcompletionrules']['completionpassorattemptsexhausted'];
if (empty($completionpassorattempts['completionpass'])) {
return true;
}
// Check for passing grade.
$item = grade_item::fetch([
'courseid' => $this->cm->get_course()->id,
'itemtype' => 'mod',
'itemmodule' => 'quiz',
'iteminstance' => $this->cm->instance,
'outcomeid' => null
]);
if ($item) {
$grades = grade_grade::fetch_users_grades($item, [$this->userid], false);
if (!empty($grades[$this->userid]) && $grades[$this->userid]->is_passed($item)) {
return true;
}
}
// If a passing grade is required and exhausting all available attempts is not accepted for completion,
// then this quiz is not complete.
if (empty($completionpassorattempts['completionattemptsexhausted'])) {
return false;
}
// Check if all attempts are used up.
$attempts = quiz_get_user_attempts($this->cm->instance, $this->userid, 'finished', true);
if (!$attempts) {
return false;
}
$lastfinishedattempt = end($attempts);
$context = context_module::instance($this->cm->id);
$quizobj = quiz::create($this->cm->instance, $this->userid);
$accessmanager = new quiz_access_manager(
$quizobj,
time(),
has_capability('mod/quiz:ignoretimelimits', $context, $this->userid, false)
);
return $accessmanager->is_finished(count($attempts), $lastfinishedattempt);
}
/**
* Check minimum attempts requirement for completion.
*
* @return bool True if minimum attempts requirement is disabled or met.
*/
protected function check_min_attempts() {
$minattempts = $this->cm->customdata['customcompletionrules']['completionminattempts'];
if (!$minattempts) {
return true;
}
// Check if the user has done enough attempts.
$attempts = quiz_get_user_attempts($this->cm->instance, $this->userid, 'finished', true);
return $minattempts <= count($attempts);
}
/**
* 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 {
$this->validate_rule($rule);
switch ($rule) {
case 'completionpassorattemptsexhausted':
$status = static::check_passing_grade_or_all_attempts();
break;
case 'completionminattempts':
$status = static::check_min_attempts();
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),
];
}
/**
* Returns an array of all completion rules, in the order they should be displayed to users.
*
* @return array
*/
public function get_sort_order(): array {
return [
'completionview',
'completionminattempts',
'completionusegrade',
'completionpassorattemptsexhausted',
];
}
}