MDL-78547 quiz: Remove unnecessary call to clear_regrade_date_cache()
[moodle.git] / mod / quiz / report / overview / report.php
blob81b2dc2b1cf886008b352699cb0c265530c48a9d
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.
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/>.
17 /**
18 * This file defines the quiz overview report class.
20 * @package quiz_overview
21 * @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 use mod_quiz\local\reports\attempts_report;
26 use mod_quiz\question\bank\qbank_helper;
27 use mod_quiz\quiz_attempt;
28 use mod_quiz\quiz_settings;
30 defined('MOODLE_INTERNAL') || die();
32 require_once($CFG->dirroot . '/mod/quiz/report/overview/overview_options.php');
33 require_once($CFG->dirroot . '/mod/quiz/report/overview/overview_form.php');
34 require_once($CFG->dirroot . '/mod/quiz/report/overview/overview_table.php');
37 /**
38 * Quiz report subclass for the overview (grades) report.
40 * @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class quiz_overview_report extends attempts_report {
45 public function display($quiz, $cm, $course) {
46 global $DB, $PAGE;
48 list($currentgroup, $studentsjoins, $groupstudentsjoins, $allowedjoins) = $this->init(
49 'overview', 'quiz_overview_settings_form', $quiz, $cm, $course);
51 $options = new quiz_overview_options('overview', $quiz, $cm, $course);
53 if ($fromform = $this->form->get_data()) {
54 $options->process_settings_from_form($fromform);
56 } else {
57 $options->process_settings_from_params();
60 $this->form->set_data($options->get_initial_form_data());
62 // Load the required questions.
63 $questions = quiz_report_get_significant_questions($quiz);
64 // Prepare for downloading, if applicable.
65 $courseshortname = format_string($course->shortname, true,
66 ['context' => context_course::instance($course->id)]);
67 $table = new quiz_overview_table($quiz, $this->context, $this->qmsubselect,
68 $options, $groupstudentsjoins, $studentsjoins, $questions, $options->get_url());
69 $filename = quiz_report_download_filename(get_string('overviewfilename', 'quiz_overview'),
70 $courseshortname, $quiz->name);
71 $table->is_downloading($options->download, $filename,
72 $courseshortname . ' ' . format_string($quiz->name, true));
73 if ($table->is_downloading()) {
74 raise_memory_limit(MEMORY_EXTRA);
77 $this->hasgroupstudents = false;
78 if (!empty($groupstudentsjoins->joins)) {
79 $sql = "SELECT DISTINCT u.id
80 FROM {user} u
81 $groupstudentsjoins->joins
82 WHERE $groupstudentsjoins->wheres";
83 $this->hasgroupstudents = $DB->record_exists_sql($sql, $groupstudentsjoins->params);
85 $hasstudents = false;
86 if (!empty($studentsjoins->joins)) {
87 $sql = "SELECT DISTINCT u.id
88 FROM {user} u
89 $studentsjoins->joins
90 WHERE $studentsjoins->wheres";
91 $hasstudents = $DB->record_exists_sql($sql, $studentsjoins->params);
93 if ($options->attempts == self::ALL_WITH) {
94 // This option is only available to users who can access all groups in
95 // groups mode, so setting allowed to empty (which means all quiz attempts
96 // are accessible, is not a security porblem.
97 $allowedjoins = new \core\dml\sql_join();
100 $this->process_actions($quiz, $cm, $currentgroup, $groupstudentsjoins, $allowedjoins, $options->get_url());
102 $hasquestions = quiz_has_questions($quiz->id);
104 // Start output.
105 if (!$table->is_downloading()) {
106 // Only print headers if not asked to download data.
107 $this->print_standard_header_and_messages($cm, $course, $quiz,
108 $options, $currentgroup, $hasquestions, $hasstudents);
110 // Print the display options.
111 $this->form->display();
114 $hasstudents = $hasstudents && (!$currentgroup || $this->hasgroupstudents);
115 if ($hasquestions && ($hasstudents || $options->attempts == self::ALL_WITH)) {
116 // Construct the SQL.
117 $table->setup_sql_queries($allowedjoins);
119 if (!$table->is_downloading()) {
120 // Output the regrade buttons.
121 if (has_capability('mod/quiz:regrade', $this->context)) {
122 $regradesneeded = $this->count_question_attempts_needing_regrade(
123 $quiz, $groupstudentsjoins);
124 if ($currentgroup) {
125 $a= new stdClass();
126 $a->groupname = format_string(groups_get_group_name($currentgroup), true, [
127 'context' => $this->context,
129 $a->coursestudents = get_string('participants');
130 $a->countregradeneeded = $regradesneeded;
131 $regradealldrydolabel =
132 get_string('regradealldrydogroup', 'quiz_overview', $a);
133 $regradealldrylabel =
134 get_string('regradealldrygroup', 'quiz_overview', $a);
135 $regradealllabel =
136 get_string('regradeallgroup', 'quiz_overview', $a);
137 } else {
138 $regradealldrydolabel =
139 get_string('regradealldrydo', 'quiz_overview', $regradesneeded);
140 $regradealldrylabel =
141 get_string('regradealldry', 'quiz_overview');
142 $regradealllabel =
143 get_string('regradeall', 'quiz_overview');
145 $displayurl = new moodle_url($options->get_url(), ['sesskey' => sesskey()]);
146 echo '<div class="regradebuttons">';
147 echo '<form action="'.$displayurl->out_omit_querystring().'">';
148 echo '<div>';
149 echo html_writer::input_hidden_params($displayurl);
150 echo '<input type="submit" class="btn btn-secondary" name="regradeall" value="'.$regradealllabel.'"/>';
151 echo '<input type="submit" class="btn btn-secondary ml-1" name="regradealldry" value="' .
152 $regradealldrylabel . '"/>';
153 if ($regradesneeded) {
154 echo '<input type="submit" class="btn btn-secondary ml-1" name="regradealldrydo" value="' .
155 $regradealldrydolabel . '"/>';
157 echo '</div>';
158 echo '</form>';
159 echo '</div>';
161 // Print information on the grading method.
162 if ($strattempthighlight = quiz_report_highlighting_grading_method(
163 $quiz, $this->qmsubselect, $options->onlygraded)) {
164 echo '<div class="quizattemptcounts mt-3">' . $strattempthighlight . '</div>';
168 // Define table columns.
169 $columns = [];
170 $headers = [];
172 if (!$table->is_downloading() && $options->checkboxcolumn) {
173 $columnname = 'checkbox';
174 $columns[] = $columnname;
175 $headers[] = $table->checkbox_col_header($columnname);
178 $this->add_user_columns($table, $columns, $headers);
179 $this->add_state_column($columns, $headers);
180 $this->add_time_columns($columns, $headers);
182 $this->add_grade_columns($quiz, $options->usercanseegrades, $columns, $headers, false);
184 if (!$table->is_downloading() && has_capability('mod/quiz:regrade', $this->context) &&
185 $this->has_regraded_questions($table->sql->from, $table->sql->where, $table->sql->params)) {
186 $columns[] = 'regraded';
187 $headers[] = get_string('regrade', 'quiz_overview');
190 if ($options->slotmarks) {
191 foreach ($questions as $slot => $question) {
192 $columns[] = 'qsgrade' . $slot;
193 $header = get_string('qbrief', 'quiz', $question->number);
194 if (!$table->is_downloading()) {
195 $header .= '<br />';
196 } else {
197 $header .= ' ';
199 $header .= '/' . quiz_rescale_grade($question->maxmark, $quiz, 'question');
200 $headers[] = $header;
204 $this->set_up_table_columns($table, $columns, $headers, $this->get_base_url(), $options, false);
205 $table->set_attribute('class', 'generaltable generalbox grades');
207 $table->out($options->pagesize, true);
210 if (!$table->is_downloading() && $options->usercanseegrades) {
211 $output = $PAGE->get_renderer('mod_quiz');
212 list($bands, $bandwidth) = self::get_bands_count_and_width($quiz);
213 $labels = self::get_bands_labels($bands, $bandwidth, $quiz);
215 if ($currentgroup && $this->hasgroupstudents) {
216 $sql = "SELECT qg.id
217 FROM {quiz_grades} qg
218 JOIN {user} u on u.id = qg.userid
219 {$groupstudentsjoins->joins}
220 WHERE qg.quiz = $quiz->id AND {$groupstudentsjoins->wheres}";
221 if ($DB->record_exists_sql($sql, $groupstudentsjoins->params)) {
222 $data = quiz_report_grade_bands($bandwidth, $bands, $quiz->id, $groupstudentsjoins);
223 $chart = self::get_chart($labels, $data);
224 $groupname = format_string(groups_get_group_name($currentgroup), true, [
225 'context' => $this->context,
227 $graphname = get_string('overviewreportgraphgroup', 'quiz_overview', $groupname);
228 // Numerical range data should display in LTR even for RTL languages.
229 echo $output->chart($chart, $graphname, ['dir' => 'ltr']);
233 if ($DB->record_exists('quiz_grades', ['quiz' => $quiz->id])) {
234 $data = quiz_report_grade_bands($bandwidth, $bands, $quiz->id, new \core\dml\sql_join());
235 $chart = self::get_chart($labels, $data);
236 $graphname = get_string('overviewreportgraph', 'quiz_overview');
237 // Numerical range data should display in LTR even for RTL languages.
238 echo $output->chart($chart, $graphname, ['dir' => 'ltr']);
241 return true;
245 * Extends parent function processing any submitted actions.
247 * @param stdClass $quiz
248 * @param stdClass $cm
249 * @param int $currentgroup
250 * @param \core\dml\sql_join $groupstudentsjoins (joins, wheres, params)
251 * @param \core\dml\sql_join $allowedjoins (joins, wheres, params)
252 * @param moodle_url $redirecturl
254 protected function process_actions($quiz, $cm, $currentgroup, \core\dml\sql_join $groupstudentsjoins,
255 \core\dml\sql_join $allowedjoins, $redirecturl) {
256 parent::process_actions($quiz, $cm, $currentgroup, $groupstudentsjoins, $allowedjoins, $redirecturl);
258 if (empty($currentgroup) || $this->hasgroupstudents) {
259 if (optional_param('regrade', 0, PARAM_BOOL) && confirm_sesskey()) {
260 if ($attemptids = optional_param_array('attemptid', [], PARAM_INT)) {
261 $this->start_regrade($quiz, $cm);
262 $this->regrade_attempts($quiz, false, $groupstudentsjoins, $attemptids);
263 $this->finish_regrade($redirecturl);
268 if (optional_param('regradeall', 0, PARAM_BOOL) && confirm_sesskey()) {
269 $this->start_regrade($quiz, $cm);
270 $this->regrade_attempts($quiz, false, $groupstudentsjoins);
271 $this->finish_regrade($redirecturl);
273 } else if (optional_param('regradealldry', 0, PARAM_BOOL) && confirm_sesskey()) {
274 $this->start_regrade($quiz, $cm);
275 $this->regrade_attempts($quiz, true, $groupstudentsjoins);
276 $this->finish_regrade($redirecturl);
278 } else if (optional_param('regradealldrydo', 0, PARAM_BOOL) && confirm_sesskey()) {
279 $this->start_regrade($quiz, $cm);
280 $this->regrade_attempts_needing_it($quiz, $groupstudentsjoins);
281 $this->finish_regrade($redirecturl);
286 * Check necessary capabilities, and start the display of the regrade progress page.
287 * @param stdClass $quiz the quiz settings.
288 * @param stdClass $cm the cm object for the quiz.
290 protected function start_regrade($quiz, $cm) {
291 require_capability('mod/quiz:regrade', $this->context);
292 $this->print_header_and_tabs(
293 $cm,
294 get_course($cm->course),
295 $quiz,
296 $this->mode
301 * Finish displaying the regrade progress page.
302 * @param moodle_url $nexturl where to send the user after the regrade.
303 * @uses exit. This method never returns.
305 protected function finish_regrade($nexturl) {
306 global $OUTPUT;
307 \core\notification::success(get_string('regradecomplete', 'quiz_overview'));
308 echo $OUTPUT->continue_button($nexturl);
309 echo $OUTPUT->footer();
310 die();
314 * Unlock the session and allow the regrading process to run in the background.
316 protected function unlock_session() {
317 \core\session\manager::write_close();
318 ignore_user_abort(true);
322 * Regrade a particular quiz attempt. Either for real ($dryrun = false), or
323 * as a pretend regrade to see which fractions would change. The outcome is
324 * stored in the quiz_overview_regrades table.
326 * Note, $attempt is not upgraded in the database. The caller needs to do that.
327 * However, $attempt->sumgrades is updated, if this is not a dry run.
329 * @param stdClass $attempt the quiz attempt to regrade.
330 * @param bool $dryrun if true, do a pretend regrade, otherwise do it for real.
331 * @param array $slots if null, regrade all questions, otherwise, just regrade
332 * the questions with those slots.
333 * @return array messages array with keys slot number, and values reasons why that slot cannot be regraded.
335 public function regrade_attempt($attempt, $dryrun = false, $slots = null): array {
336 global $DB;
337 // Need more time for a quiz with many questions.
338 core_php_time_limit::raise(300);
340 $transaction = $DB->start_delegated_transaction();
342 $quba = question_engine::load_questions_usage_by_activity($attempt->uniqueid);
343 $versioninformation = qbank_helper::get_version_information_for_questions_in_attempt(
344 $attempt, $this->context);
346 if (is_null($slots)) {
347 $slots = $quba->get_slots();
350 $messages = [];
351 $finished = $attempt->state == quiz_attempt::FINISHED;
352 foreach ($slots as $slot) {
353 $qqr = new stdClass();
354 $qqr->oldfraction = $quba->get_question_fraction($slot);
355 $otherquestionversion = question_bank::load_question($versioninformation[$slot]->newquestionid);
357 $message = $quba->validate_can_regrade_with_other_version($slot, $otherquestionversion);
358 if ($message) {
359 $messages[$slot] = $message;
360 continue;
363 $quba->regrade_question($slot, $finished, null, $otherquestionversion);
365 $qqr->newfraction = $quba->get_question_fraction($slot);
367 if (abs($qqr->oldfraction - $qqr->newfraction) > 1e-7) {
368 $qqr->questionusageid = $quba->get_id();
369 $qqr->slot = $slot;
370 $qqr->regraded = empty($dryrun);
371 $qqr->timemodified = time();
372 $DB->insert_record('quiz_overview_regrades', $qqr, false);
376 if (!$dryrun) {
377 question_engine::save_questions_usage_by_activity($quba);
379 $params = [
380 'objectid' => $attempt->id,
381 'relateduserid' => $attempt->userid,
382 'context' => $this->context,
383 'other' => [
384 'quizid' => $attempt->quiz
387 $event = \mod_quiz\event\attempt_regraded::create($params);
388 $event->trigger();
391 $transaction->allow_commit();
393 // Really, PHP should not need this hint, but without this, we just run out of memory.
394 $quba = null;
395 $transaction = null;
396 gc_collect_cycles();
397 return $messages;
401 * Regrade attempts for this quiz, exactly which attempts are regraded is
402 * controlled by the parameters.
404 * @param stdClass $quiz the quiz settings.
405 * @param bool $dryrun if true, do a pretend regrade, otherwise do it for real.
406 * @param \core\dml\sql_join|null $groupstudentsjoins empty for all attempts, otherwise regrade attempts
407 * for these users.
408 * @param array $attemptids blank for all attempts, otherwise only regrade
409 * attempts whose id is in this list.
411 protected function regrade_attempts($quiz, $dryrun = false,
412 core\dml\sql_join $groupstudentsjoins = null, $attemptids = []) {
413 global $DB;
414 $this->unlock_session();
416 $userfieldsapi = \core_user\fields::for_name();
417 $sql = "SELECT quiza.*, " . $userfieldsapi->get_sql('u', false, '', '', false)->selects . "
418 FROM {quiz_attempts} quiza
419 JOIN {user} u ON u.id = quiza.userid";
420 $where = "quiz = :qid AND preview = 0";
421 $params = ['qid' => $quiz->id];
423 if ($this->hasgroupstudents && !empty($groupstudentsjoins->joins)) {
424 $sql .= "\n{$groupstudentsjoins->joins}";
425 $where .= " AND {$groupstudentsjoins->wheres}";
426 $params += $groupstudentsjoins->params;
429 if ($attemptids) {
430 list($attemptidcondition, $attemptidparams) = $DB->get_in_or_equal($attemptids, SQL_PARAMS_NAMED);
431 $where .= " AND quiza.id $attemptidcondition";
432 $params += $attemptidparams;
435 $sql .= "\nWHERE {$where}";
436 $attempts = $DB->get_records_sql($sql, $params);
437 if (!$attempts) {
438 return;
441 $this->regrade_batch_of_attempts($quiz, $attempts, $dryrun, $groupstudentsjoins);
445 * Regrade those questions in those attempts that are marked as needing regrading
446 * in the quiz_overview_regrades table.
447 * @param stdClass $quiz the quiz settings.
448 * @param \core\dml\sql_join $groupstudentsjoins empty for all attempts, otherwise regrade attempts
449 * for these users.
451 protected function regrade_attempts_needing_it($quiz, \core\dml\sql_join $groupstudentsjoins) {
452 global $DB;
453 $this->unlock_session();
455 $join = '{quiz_overview_regrades} qqr ON qqr.questionusageid = quiza.uniqueid';
456 $where = "quiza.quiz = :qid AND quiza.preview = 0 AND qqr.regraded = 0";
457 $params = ['qid' => $quiz->id];
459 // Fetch all attempts that need regrading.
460 if ($this->hasgroupstudents && !empty($groupstudentsjoins->joins)) {
461 $join .= "\nJOIN {user} u ON u.id = quiza.userid
462 {$groupstudentsjoins->joins}";
463 $where .= " AND {$groupstudentsjoins->wheres}";
464 $params += $groupstudentsjoins->params;
467 $toregrade = $DB->get_recordset_sql("
468 SELECT quiza.uniqueid, qqr.slot
469 FROM {quiz_attempts} quiza
470 JOIN $join
471 WHERE $where", $params);
473 $attemptquestions = [];
474 foreach ($toregrade as $row) {
475 $attemptquestions[$row->uniqueid][] = $row->slot;
477 $toregrade->close();
479 if (!$attemptquestions) {
480 return;
483 list($uniqueidcondition, $params) = $DB->get_in_or_equal(array_keys($attemptquestions));
484 $userfieldsapi = \core_user\fields::for_name();
485 $attempts = $DB->get_records_sql("
486 SELECT quiza.*, " . $userfieldsapi->get_sql('u', false, '', '', false)->selects . "
487 FROM {quiz_attempts} quiza
488 JOIN {user} u ON u.id = quiza.userid
489 WHERE quiza.uniqueid $uniqueidcondition
490 ", $params);
492 foreach ($attempts as $attempt) {
493 $attempt->regradeonlyslots = $attemptquestions[$attempt->uniqueid];
496 $this->regrade_batch_of_attempts($quiz, $attempts, false, $groupstudentsjoins);
500 * This is a helper used by {@link regrade_attempts()} and
501 * {@link regrade_attempts_needing_it()}.
503 * Given an array of attempts, it regrades them all, or does a dry run.
504 * Each object in the attempts array must be a row from the quiz_attempts
505 * table, with the \core_user\fields::for_name() fields from the user table joined in.
506 * In addition, if $attempt->regradeonlyslots is set, then only those slots
507 * are regraded, otherwise all slots are regraded.
509 * @param stdClass $quiz the quiz settings.
510 * @param array $attempts of data from the quiz_attempts table, with extra data as above.
511 * @param bool $dryrun if true, do a pretend regrade, otherwise do it for real.
512 * @param \core\dml\sql_join $groupstudentsjoins empty for all attempts, otherwise regrade attempts
514 protected function regrade_batch_of_attempts($quiz, array $attempts,
515 bool $dryrun, \core\dml\sql_join $groupstudentsjoins) {
516 global $OUTPUT;
517 $this->clear_regrade_table($quiz, $groupstudentsjoins);
519 $progressbar = new progress_bar('quiz_overview_regrade', 500, true);
520 $a = [
521 'count' => count($attempts),
522 'done' => 0,
524 foreach ($attempts as $attempt) {
525 $a['done']++;
526 $a['attemptnum'] = $attempt->attempt;
527 $a['name'] = fullname($attempt);
528 $a['attemptid'] = $attempt->id;
529 if (!isset($attempt->regradeonlyslots)) {
530 $attempt->regradeonlyslots = null;
532 $progressbar->update($a['done'], $a['count'],
533 get_string('regradingattemptxofywithdetails', 'quiz_overview', $a));
534 $messages = $this->regrade_attempt($attempt, $dryrun, $attempt->regradeonlyslots);
535 if ($messages) {
536 $items = [];
537 foreach ($messages as $slot => $message) {
538 $items[] = get_string('regradingattemptissue', 'quiz_overview',
539 ['slot' => $slot, 'reason' => $message]);
541 echo $OUTPUT->notification(
542 html_writer::tag('p', get_string('regradingattemptxofyproblem', 'quiz_overview', $a)) .
543 html_writer::alist($items), \core\output\notification::NOTIFY_WARNING);
546 $progressbar->update($a['done'], $a['count'],
547 get_string('regradedsuccessfullyxofy', 'quiz_overview', $a));
549 if (!$dryrun) {
550 $this->update_overall_grades($quiz);
555 * Count the number of attempts in need of a regrade.
557 * @param stdClass $quiz the quiz settings.
558 * @param \core\dml\sql_join $groupstudentsjoins (joins, wheres, params) If this is given, only data relating
559 * to these users is cleared.
560 * @return int the number of attempts.
562 protected function count_question_attempts_needing_regrade($quiz, \core\dml\sql_join $groupstudentsjoins) {
563 global $DB;
565 $userjoin = '';
566 $usertest = '';
567 $params = [];
568 if ($this->hasgroupstudents) {
569 $userjoin = "JOIN {user} u ON u.id = quiza.userid
570 {$groupstudentsjoins->joins}";
571 $usertest = "{$groupstudentsjoins->wheres} AND u.id = quiza.userid AND ";
572 $params = $groupstudentsjoins->params;
575 $params['cquiz'] = $quiz->id;
576 $sql = "SELECT COUNT(DISTINCT quiza.id)
577 FROM {quiz_attempts} quiza
578 JOIN {quiz_overview_regrades} qqr ON quiza.uniqueid = qqr.questionusageid
579 $userjoin
580 WHERE
581 $usertest
582 quiza.quiz = :cquiz AND
583 quiza.preview = 0 AND
584 qqr.regraded = 0";
585 return $DB->count_records_sql($sql, $params);
589 * Are there any pending regrades in the table we are going to show?
590 * @param string $from tables used by the main query.
591 * @param string $where where clause used by the main query.
592 * @param array $params required by the SQL.
593 * @return bool whether there are pending regrades.
595 protected function has_regraded_questions($from, $where, $params) {
596 global $DB;
597 return $DB->record_exists_sql("
598 SELECT 1
599 FROM {$from}
600 JOIN {quiz_overview_regrades} qor ON qor.questionusageid = quiza.uniqueid
601 WHERE {$where}", $params);
605 * Remove all information about pending/complete regrades from the database.
606 * @param stdClass $quiz the quiz settings.
607 * @param \core\dml\sql_join $groupstudentsjoins (joins, wheres, params). If this is given, only data relating
608 * to these users is cleared.
610 protected function clear_regrade_table($quiz, \core\dml\sql_join $groupstudentsjoins) {
611 global $DB;
613 // Fetch all attempts that need regrading.
614 $select = "questionusageid IN (
615 SELECT uniqueid
616 FROM {quiz_attempts} quiza";
617 $where = "WHERE quiza.quiz = :qid";
618 $params = ['qid' => $quiz->id];
619 if ($this->hasgroupstudents && !empty($groupstudentsjoins->joins)) {
620 $select .= "\nJOIN {user} u ON u.id = quiza.userid
621 {$groupstudentsjoins->joins}";
622 $where .= " AND {$groupstudentsjoins->wheres}";
623 $params += $groupstudentsjoins->params;
625 $select .= "\n$where)";
627 $DB->delete_records_select('quiz_overview_regrades', $select, $params);
631 * Update the final grades for all attempts. This method is used following a regrade.
633 * @param stdClass $quiz the quiz settings.
635 protected function update_overall_grades($quiz) {
636 $gradecalculator = $this->quizobj->get_grade_calculator();
637 $gradecalculator->recompute_all_attempt_sumgrades();
638 $gradecalculator->recompute_all_final_grades();
639 quiz_update_grades($quiz);
643 * Get the bands configuration for the quiz.
645 * This returns the configuration for having between 11 and 20 bars in
646 * a chart based on the maximum grade to be given on a quiz. The width of
647 * a band is the number of grade points it encapsulates.
649 * @param stdClass $quiz The quiz object.
650 * @return array Contains the number of bands, and their width.
652 public static function get_bands_count_and_width($quiz) {
653 $bands = $quiz->grade;
654 while ($bands > 20 || $bands <= 10) {
655 if ($bands > 50) {
656 $bands /= 5;
657 } else if ($bands > 20) {
658 $bands /= 2;
660 if ($bands < 4) {
661 $bands *= 5;
662 } else if ($bands <= 10) {
663 $bands *= 2;
666 // See MDL-34589. Using doubles as array keys causes problems in PHP 5.4, hence the explicit cast to int.
667 $bands = (int) ceil($bands);
668 return [$bands, $quiz->grade / $bands];
672 * Get the bands labels.
674 * @param int $bands The number of bands.
675 * @param int $bandwidth The band width.
676 * @param stdClass $quiz The quiz object.
677 * @return string[] The labels.
679 public static function get_bands_labels($bands, $bandwidth, $quiz) {
680 $bandlabels = [];
681 for ($i = 1; $i <= $bands; $i++) {
682 $bandlabels[] = quiz_format_grade($quiz, ($i - 1) * $bandwidth) . ' - ' . quiz_format_grade($quiz, $i * $bandwidth);
684 return $bandlabels;
688 * Get a chart.
690 * @param string[] $labels Chart labels.
691 * @param int[] $data The data.
692 * @return \core\chart_base
694 protected static function get_chart($labels, $data) {
695 $chart = new \core\chart_bar();
696 $chart->set_labels($labels);
697 $chart->get_xaxis(0, true)->set_label(get_string('gradenoun'));
699 $yaxis = $chart->get_yaxis(0, true);
700 $yaxis->set_label(get_string('participants'));
701 $yaxis->set_stepsize(max(1, round(max($data) / 10)));
703 $series = new \core\chart_series(get_string('participants'), $data);
704 $chart->add_series($series);
705 return $chart;