Proyectos de Subversion Moodle

Rev

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