Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * List of deprecated mod_quiz functions.
19
 *
20
 * @package   mod_quiz
21
 * @copyright 2021 Shamim Rezaie <shamim@moodle.com>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
use mod_quiz\access_manager;
26
use mod_quiz\quiz_settings;
27
use mod_quiz\task\update_overdue_attempts;
28
 
29
/**
30
 * @deprecated since Moodle 3.11
31
 */
32
function quiz_get_completion_state() {
33
    $completionclass = \mod_quiz\completion\custom_completion::class;
34
    throw new coding_exception(__FUNCTION__ . "() has been removed, please use the '{$completionclass}' class instead");
35
}
36
 
37
/**
38
 * @deprecated since Moodle 4.0
39
 */
40
function quiz_retrieve_tags_for_slot_ids() {
41
    throw new coding_exception(__FUNCTION__ . '() has been removed.');
42
}
43
 
44
/**
45
 * Verify that the question exists, and the user has permission to use it.
46
 *
47
 * @deprecated in 4.1 use mod_quiz\structure::has_use_capability(...) instead.
48
 *
49
 * @param stdClass $quiz the quiz settings.
50
 * @param int $slot which question in the quiz to test.
51
 * @return bool whether the user can use this question.
52
 */
53
function quiz_has_question_use($quiz, $slot) {
54
    global $DB;
55
 
56
    debugging('Deprecated. Please use mod_quiz\structure::has_use_capability instead.');
57
 
58
    $sql = 'SELECT q.*
59
              FROM {quiz_slots} slot
60
              JOIN {question_references} qre ON qre.itemid = slot.id
61
              JOIN {question_bank_entries} qbe ON qbe.id = qre.questionbankentryid
62
              JOIN {question_versions} qve ON qve.questionbankentryid = qbe.id
63
              JOIN {question} q ON q.id = qve.questionid
64
             WHERE slot.quizid = ?
65
               AND slot.slot = ?
66
               AND qre.component = ?
67
               AND qre.questionarea = ?';
68
 
69
    $question = $DB->get_record_sql($sql, [$quiz->id, $slot, 'mod_quiz', 'slot']);
70
 
71
    if (!$question) {
72
        return false;
73
    }
74
    return question_has_capability_on($question, 'use');
75
}
76
 
77
/**
78
 * @copyright 2012 the Open University
79
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
80
 * @deprecated since Moodle 4.2. Code moved to mod_quiz\task\update_overdue_attempts.
81
 * @todo MDL-76612 Final deprecation in Moodle 4.6
82
 */
83
class mod_quiz_overdue_attempt_updater {
84
 
85
    /**
86
     * @deprecated since Moodle 4.2. Code moved to mod_quiz\task\update_overdue_attempts. that was.
87
     */
88
    public function update_overdue_attempts($timenow, $processto) {
89
        debugging('mod_quiz_overdue_attempt_updater has been deprecated. The code wsa moved to ' .
90
                'mod_quiz\task\update_overdue_attempts.');
91
        return (new update_overdue_attempts())->update_all_overdue_attempts((int) $timenow, (int) $processto);
92
    }
93
 
94
    /**
95
     * @deprecated since Moodle 4.2. Code moved to mod_quiz\task\update_overdue_attempts.
96
     */
97
    public function get_list_of_overdue_attempts($processto) {
98
        debugging('mod_quiz_overdue_attempt_updater has been deprecated. The code wsa moved to ' .
99
                'mod_quiz\task\update_overdue_attempts.');
100
        return (new update_overdue_attempts())->get_list_of_overdue_attempts((int) $processto);
101
    }
102
}
103
 
104
/**
105
 * Class for quiz exceptions. Just saves a couple of arguments on the
106
 * constructor for a moodle_exception.
107
 *
108
 * @copyright 2008 Tim Hunt
109
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
110
 * @since     Moodle 2.0
111
 * @deprecated since Moodle 4.2. Please just use moodle_exception.
112
 * @todo MDL-76612 Final deprecation in Moodle 4.6
113
 */
114
class moodle_quiz_exception extends moodle_exception {
115
    /**
116
     * Constructor.
117
     *
118
     * @param quiz_settings $quizobj the quiz the error relates to.
119
     * @param string $errorcode The name of the string from error.php to print.
120
     * @param mixed $a Extra words and phrases that might be required in the error string.
121
     * @param string $link The url where the user will be prompted to continue.
122
     *      If no url is provided the user will be directed to the site index page.
123
     * @param string|null $debuginfo optional debugging information.
124
     * @deprecated since Moodle 4.2. Please just use moodle_exception.
125
     */
126
    public function __construct($quizobj, $errorcode, $a = null, $link = '', $debuginfo = null) {
127
        debugging('Class moodle_quiz_exception is deprecated. ' .
128
                'Please use a standard moodle_exception instead.', DEBUG_DEVELOPER);
129
        if (!$link) {
130
            $link = $quizobj->view_url();
131
        }
132
        parent::__construct($errorcode, 'quiz', $link, $a, $debuginfo);
133
    }
134
}
135
 
136
/**
137
 * Update the sumgrades field of the quiz. This needs to be called whenever
138
 * the grading structure of the quiz is changed. For example if a question is
139
 * added or removed, or a question weight is changed.
140
 *
141
 * You should call {@see quiz_delete_previews()} before you call this function.
142
 *
143
 * @param stdClass $quiz a quiz.
144
 * @deprecated since Moodle 4.2. Please use grade_calculator::recompute_quiz_sumgrades.
145
 * @todo MDL-76612 Final deprecation in Moodle 4.6
146
 */
147
function quiz_update_sumgrades($quiz) {
148
    debugging('quiz_update_sumgrades is deprecated. ' .
149
        'Please use a standard grade_calculator::recompute_quiz_sumgrades instead.', DEBUG_DEVELOPER);
150
    quiz_settings::create($quiz->id)->get_grade_calculator()->recompute_quiz_sumgrades();
151
}
152
 
153
/**
154
 * Update the sumgrades field of the attempts at a quiz.
155
 *
156
 * @param stdClass $quiz a quiz.
157
 * @deprecated since Moodle 4.2. Please use grade_calculator::recompute_all_attempt_sumgrades.
158
 * @todo MDL-76612 Final deprecation in Moodle 4.6
159
 */
160
function quiz_update_all_attempt_sumgrades($quiz) {
161
    debugging('quiz_update_all_attempt_sumgrades is deprecated. ' .
162
        'Please use a standard grade_calculator::recompute_all_attempt_sumgrades instead.', DEBUG_DEVELOPER);
163
    quiz_settings::create($quiz->id)->get_grade_calculator()->recompute_all_attempt_sumgrades();
164
}
165
 
166
/**
167
 * Update the final grade at this quiz for all students.
168
 *
169
 * This function is equivalent to calling quiz_save_best_grade for all
170
 * users, but much more efficient.
171
 *
172
 * @param stdClass $quiz the quiz settings.
173
 * @deprecated since Moodle 4.2. Please use grade_calculator::recompute_all_final_grades.
174
 * @todo MDL-76612 Final deprecation in Moodle 4.6
175
 */
176
function quiz_update_all_final_grades($quiz) {
177
    debugging('quiz_update_all_final_grades is deprecated. ' .
178
        'Please use a standard grade_calculator::recompute_all_final_grades instead.', DEBUG_DEVELOPER);
179
    quiz_settings::create($quiz->id)->get_grade_calculator()->recompute_all_final_grades();
180
}
181
 
182
/**
183
 * The quiz grade is the maximum that student's results are marked out of. When it
184
 * changes, the corresponding data in quiz_grades and quiz_feedback needs to be
185
 * rescaled. After calling this function, you probably need to call
186
 * quiz_update_all_attempt_sumgrades, grade_calculator::recompute_all_final_grades();
187
 * quiz_update_grades. (At least, that is what this comment has said for years, but
188
 * it seems to call recompute_all_final_grades itself.)
189
 *
190
 * @param float $newgrade the new maximum grade for the quiz.
191
 * @param stdClass $quiz the quiz we are updating. Passed by reference so its
192
 *      grade field can be updated too.
193
 * @return bool indicating success or failure.
194
 * @deprecated since Moodle 4.2. Please use grade_calculator::update_quiz_maximum_grade.
195
 * @todo MDL-76612 Final deprecation in Moodle 4.6
196
 */
197
function quiz_set_grade($newgrade, $quiz) {
198
    debugging('quiz_set_grade is deprecated. ' .
199
        'Please use a standard grade_calculator::update_quiz_maximum_grade instead.', DEBUG_DEVELOPER);
200
    quiz_settings::create($quiz->id)->get_grade_calculator()->update_quiz_maximum_grade($newgrade);
201
    return true;
202
}
203
 
204
/**
205
 * Save the overall grade for a user at a quiz in the quiz_grades table
206
 *
207
 * @param stdClass $quiz The quiz for which the best grade is to be calculated and then saved.
208
 * @param int $userid The userid to calculate the grade for. Defaults to the current user.
209
 * @param array $attempts The attempts of this user. Useful if you are
210
 * looping through many users. Attempts can be fetched in one master query to
211
 * avoid repeated querying.
212
 * @return bool Indicates success or failure.
213
 * @deprecated since Moodle 4.2. Please use grade_calculator::update_quiz_maximum_grade.
214
 * @todo MDL-76612 Final deprecation in Moodle 4.6
215
 */
216
function quiz_save_best_grade($quiz, $userid = null, $attempts = []) {
217
    debugging('quiz_save_best_grade is deprecated. ' .
218
        'Please use a standard grade_calculator::recompute_final_grade instead.', DEBUG_DEVELOPER);
219
    quiz_settings::create($quiz->id)->get_grade_calculator()->recompute_final_grade($userid, $attempts);
220
    return true;
221
}
222
 
223
/**
224
 * Calculate the overall grade for a quiz given a number of attempts by a particular user.
225
 *
226
 * @param stdClass $quiz    the quiz settings object.
227
 * @param array $attempts an array of all the user's attempts at this quiz in order.
228
 * @return float          the overall grade
229
 * @deprecated since Moodle 4.2. No direct replacement.
230
 * @todo MDL-76612 Final deprecation in Moodle 4.6
231
 */
232
function quiz_calculate_best_grade($quiz, $attempts) {
233
    debugging('quiz_calculate_best_grade is deprecated with no direct replacement. It was only used ' .
234
        'in one place in the quiz code so this logic is now private to grade_calculator.', DEBUG_DEVELOPER);
235
 
236
    switch ($quiz->grademethod) {
237
 
238
        case QUIZ_ATTEMPTFIRST:
239
            $firstattempt = reset($attempts);
240
            return $firstattempt->sumgrades;
241
 
242
        case QUIZ_ATTEMPTLAST:
243
            $lastattempt = end($attempts);
244
            return $lastattempt->sumgrades;
245
 
246
        case QUIZ_GRADEAVERAGE:
247
            $sum = 0;
248
            $count = 0;
249
            foreach ($attempts as $attempt) {
250
                if (!is_null($attempt->sumgrades)) {
251
                    $sum += $attempt->sumgrades;
252
                    $count++;
253
                }
254
            }
255
            if ($count == 0) {
256
                return null;
257
            }
258
            return $sum / $count;
259
 
260
        case QUIZ_GRADEHIGHEST:
261
        default:
262
            $max = null;
263
            foreach ($attempts as $attempt) {
264
                if ($attempt->sumgrades > $max) {
265
                    $max = $attempt->sumgrades;
266
                }
267
            }
268
            return $max;
269
    }
270
}
271
 
272
/**
273
 * Return the attempt with the best grade for a quiz
274
 *
275
 * Which attempt is the best depends on $quiz->grademethod. If the grade
276
 * method is GRADEAVERAGE then this function simply returns the last attempt.
277
 * @return stdClass         The attempt with the best grade
278
 * @param stdClass $quiz    The quiz for which the best grade is to be calculated
279
 * @param array $attempts An array of all the attempts of the user at the quiz
280
 * @deprecated since Moodle 4.2. No direct replacement.
281
 * @todo MDL-76612 Final deprecation in Moodle 4.6
282
 */
283
function quiz_calculate_best_attempt($quiz, $attempts) {
284
    debugging('quiz_calculate_best_attempt is deprecated with no direct replacement. ' .
285
        'It was not used anywhere!', DEBUG_DEVELOPER);
286
 
287
    switch ($quiz->grademethod) {
288
 
289
        case QUIZ_ATTEMPTFIRST:
290
            foreach ($attempts as $attempt) {
291
                return $attempt;
292
            }
293
            break;
294
 
295
        case QUIZ_GRADEAVERAGE: // We need to do something with it.
296
        case QUIZ_ATTEMPTLAST:
297
            foreach ($attempts as $attempt) {
298
                $final = $attempt;
299
            }
300
            return $final;
301
 
302
        default:
303
        case QUIZ_GRADEHIGHEST:
304
            $max = -1;
305
            foreach ($attempts as $attempt) {
306
                if ($attempt->sumgrades > $max) {
307
                    $max = $attempt->sumgrades;
308
                    $maxattempt = $attempt;
309
                }
310
            }
311
            return $maxattempt;
312
    }
313
}
314
 
315
/**
316
 * Deletes a quiz override from the database and clears any corresponding calendar events
317
 *
318
 * @deprecated since Moodle 4.4
319
 * @todo MDL-80944 Final deprecation in Moodle 4.8
320
 * @param stdClass $quiz The quiz object.
321
 * @param int $overrideid The id of the override being deleted
322
 * @param bool $log Whether to trigger logs.
323
 * @return bool true on success
324
 */
325
#[\core\attribute\deprecated('override_manager::delete_override_by_id', since: '4.4')]
326
function quiz_delete_override($quiz, $overrideid, $log = true) {
327
    \core\deprecation::emit_deprecation_if_present(__FUNCTION__);
328
    $quizsettings = quiz_settings::create($quiz->id);
329
    $quizsettings->get_override_manager()->delete_overrides_by_id(
330
        ids: [$overrideid],
331
        shouldlog: $log,
332
    );
333
 
334
    return true;
335
}
336
 
337
/**
338
 * Deletes all quiz overrides from the database and clears any corresponding calendar events
339
 *
340
 * @deprecated since Moodle 4.4
341
 * @todo MDL-80944 Final deprecation in Moodle 4.8
342
 * @param stdClass $quiz The quiz object.
343
 * @param bool $log Whether to trigger logs.
344
 */
345
#[\core\attribute\deprecated('override_manager::delete_all_overrides', since: '4.4')]
346
function quiz_delete_all_overrides($quiz, $log = true) {
347
    \core\deprecation::emit_deprecation_if_present(__FUNCTION__);
348
    $quizsettings = quiz_settings::create($quiz->id);
349
    $quizsettings->get_override_manager()->delete_all_overrides(shouldlog: $log);
350
}