2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Quiz statistics report class.
20 * @package quiz_statistics
21 * @copyright 2014 Open University
22 * @author James Pratt <me@jamiep.org>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->dirroot . '/mod/quiz/report/statistics/statistics_form.php');
29 require_once($CFG->dirroot . '/mod/quiz/report/statistics/statistics_table.php');
30 require_once($CFG->dirroot . '/mod/quiz/report/statistics/statistics_question_table.php');
31 require_once($CFG->dirroot . '/mod/quiz/report/statistics/statisticslib.php');
33 * The quiz statistics report provides summary information about each question in
34 * a quiz, compared to the whole quiz. It also provides a drill-down to more
35 * detailed information about each question.
37 * @copyright 2008 Jamie Pratt
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class quiz_statistics_report extends quiz_default_report {
42 /** @var context_module context of this quiz.*/
45 /** @var quiz_statistics_table instance of table class used for main questions stats table. */
48 /** @var \core\progress\base|null $progress Handles progress reporting or not. */
49 protected $progress = null;
54 public function display($quiz, $cm, $course) {
57 raise_memory_limit(MEMORY_HUGE);
59 $this->context = context_module::instance($cm->id);
61 if (!quiz_has_questions($quiz->id)) {
62 $this->print_header_and_tabs($cm, $course, $quiz, 'statistics');
63 echo quiz_no_questions_message($quiz, $cm, $this->context);
67 // Work out the display options.
68 $download = optional_param('download', '', PARAM_ALPHA);
69 $everything = optional_param('everything', 0, PARAM_BOOL);
70 $recalculate = optional_param('recalculate', 0, PARAM_BOOL);
71 // A qid paramter indicates we should display the detailed analysis of a sub question.
72 $qid = optional_param('qid', 0, PARAM_INT);
73 $slot = optional_param('slot', 0, PARAM_INT);
74 $variantno = optional_param('variant', null, PARAM_INT);
75 $whichattempts = optional_param('whichattempts', $quiz->grademethod, PARAM_INT);
76 $whichtries = optional_param('whichtries', question_attempt::LAST_TRY, PARAM_ALPHA);
78 $pageoptions = array();
79 $pageoptions['id'] = $cm->id;
80 $pageoptions['mode'] = 'statistics';
82 $reporturl = new moodle_url('/mod/quiz/report.php', $pageoptions);
84 $mform = new quiz_statistics_settings_form($reporturl, compact('quiz'));
86 $mform->set_data(array('whichattempts' => $whichattempts, 'whichtries' => $whichtries));
88 if ($whichattempts != $quiz->grademethod) {
89 $reporturl->param('whichattempts', $whichattempts);
92 if ($whichtries != question_attempt::LAST_TRY) {
93 $reporturl->param('whichtries', $whichtries);
96 // Find out current groups mode.
97 $currentgroup = $this->get_current_group($cm, $course, $this->context);
98 $nostudentsingroup = false; // True if a group is selected and there is no one in it.
99 if (empty($currentgroup)) {
101 $groupstudents = array();
103 } else if ($currentgroup == self::NO_GROUPS_ALLOWED) {
104 $groupstudents = array();
105 $nostudentsingroup = true;
108 // All users who can attempt quizzes and who are in the currently selected group.
109 $groupstudents = get_users_by_capability($this->context,
110 array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'),
111 '', '', '', '', $currentgroup, '', false);
112 if (!$groupstudents) {
113 $nostudentsingroup = true;
117 $qubaids = quiz_statistics_qubaids_condition($quiz->id, $groupstudents, $whichattempts);
119 // If recalculate was requested, handle that.
120 if ($recalculate && confirm_sesskey()) {
121 $this->clear_cached_data($qubaids);
122 redirect($reporturl);
125 // Set up the main table.
126 $this->table = new quiz_statistics_table();
128 $report = get_string('completestatsfilename', 'quiz_statistics');
130 $report = get_string('questionstatsfilename', 'quiz_statistics');
132 $courseshortname = format_string($course->shortname, true,
133 array('context' => context_course::instance($course->id)));
134 $filename = quiz_report_download_filename($report, $courseshortname, $quiz->name);
135 $this->table->is_downloading($download, $filename,
136 get_string('quizstructureanalysis', 'quiz_statistics'));
137 $questions = $this->load_and_initialise_questions_for_calculations($quiz);
139 // Print the page header stuff (if not downloading.
140 if (!$this->table->is_downloading()) {
141 $this->print_header_and_tabs($cm, $course, $quiz, 'statistics');
144 if (!$nostudentsingroup) {
145 // Get the data to be displayed.
146 $progress = $this->get_progress_trace_instance();
147 list($quizstats, $questionstats) =
148 $this->get_all_stats_and_analysis($quiz, $whichattempts, $whichtries, $groupstudents, $questions, $progress);
150 // Or create empty stats containers.
151 $quizstats = new \quiz_statistics\calculated($whichattempts);
152 $questionstats = new \core_question\statistics\questions\all_calculated_for_qubaid_condition();
155 // Set up the table, if there is data.
156 if ($quizstats->s()) {
157 $this->table->statistics_setup($quiz, $cm->id, $reporturl, $quizstats->s());
160 // Print the rest of the page header stuff (if not downloading.
161 if (!$this->table->is_downloading()) {
163 if (groups_get_activity_groupmode($cm)) {
164 groups_print_activity_menu($cm, $reporturl->out());
165 if ($currentgroup && !$groupstudents) {
166 $OUTPUT->notification(get_string('nostudentsingroup', 'quiz_statistics'));
170 if (!$this->table->is_downloading() && $quizstats->s() == 0) {
171 echo $OUTPUT->notification(get_string('noattempts', 'quiz'));
174 foreach ($questionstats->any_error_messages() as $errormessage) {
175 echo $OUTPUT->notification($errormessage);
178 // Print display options form.
182 if ($everything) { // Implies is downloading.
183 // Overall report, then the analysis of each question.
184 $quizinfo = $quizstats->get_formatted_quiz_info_data($course, $cm, $quiz);
185 $this->download_quiz_info_table($quizinfo);
187 if ($quizstats->s()) {
188 $this->output_quiz_structure_analysis_table($questionstats);
190 if ($this->table->is_downloading() == 'xhtml' && $quizstats->s() != 0) {
191 $this->output_statistics_graph($quiz->id, $currentgroup, $whichattempts);
194 $this->output_all_question_response_analysis($qubaids, $questions, $questionstats, $reporturl, $whichtries);
197 $this->table->export_class_instance()->finish_document();
200 // Report on an individual sub-question indexed questionid.
201 if (is_null($questionstats->for_subq($qid, $variantno))) {
202 print_error('questiondoesnotexist', 'question');
205 $this->output_individual_question_data($quiz, $questionstats->for_subq($qid, $variantno));
206 $this->output_individual_question_response_analysis($questionstats->for_subq($qid, $variantno)->question,
208 $questionstats->for_subq($qid, $variantno)->s,
212 // Back to overview link.
213 echo $OUTPUT->box('<a href="' . $reporturl->out() . '">' .
214 get_string('backtoquizreport', 'quiz_statistics') . '</a>',
215 'boxaligncenter generalbox boxwidthnormal mdl-align');
217 // Report on an individual question indexed by position.
218 if (!isset($questions[$slot])) {
219 print_error('questiondoesnotexist', 'question');
222 if ($variantno === null &&
223 ($questionstats->for_slot($slot)->get_sub_question_ids()
224 || $questionstats->for_slot($slot)->get_variants())) {
225 if (!$this->table->is_downloading()) {
226 $number = $questionstats->for_slot($slot)->question->number;
227 echo $OUTPUT->heading(get_string('slotstructureanalysis', 'quiz_statistics', $number), 3);
229 $this->table->define_baseurl(new moodle_url($reporturl, array('slot' => $slot)));
230 $this->table->format_and_add_array_of_rows($questionstats->structure_analysis_for_one_slot($slot));
232 $this->output_individual_question_data($quiz, $questionstats->for_slot($slot, $variantno));
233 $this->output_individual_question_response_analysis($questions[$slot],
235 $questionstats->for_slot($slot, $variantno)->s,
240 if (!$this->table->is_downloading()) {
241 // Back to overview link.
242 echo $OUTPUT->box('<a href="' . $reporturl->out() . '">' .
243 get_string('backtoquizreport', 'quiz_statistics') . '</a>',
244 'backtomainstats boxaligncenter generalbox boxwidthnormal mdl-align');
246 $this->table->finish_output();
249 } else if ($this->table->is_downloading()) {
250 // Downloading overview report.
251 $quizinfo = $quizstats->get_formatted_quiz_info_data($course, $cm, $quiz);
252 $this->download_quiz_info_table($quizinfo);
253 if ($quizstats->s()) {
254 $this->output_quiz_structure_analysis_table($questionstats);
256 $this->table->finish_output();
259 // On-screen display of overview report.
260 echo $OUTPUT->heading(get_string('quizinformation', 'quiz_statistics'), 3);
261 echo $this->output_caching_info($quizstats->timemodified, $quiz->id, $groupstudents, $whichattempts, $reporturl);
262 echo $this->everything_download_options();
263 $quizinfo = $quizstats->get_formatted_quiz_info_data($course, $cm, $quiz);
264 echo $this->output_quiz_info_table($quizinfo);
265 if ($quizstats->s()) {
266 echo $OUTPUT->heading(get_string('quizstructureanalysis', 'quiz_statistics'), 3);
267 $this->output_quiz_structure_analysis_table($questionstats);
268 $this->output_statistics_graph($quiz->id, $currentgroup, $whichattempts);
276 * Display the statistical and introductory information about a question.
277 * Only called when not downloading.
279 * @param object $quiz the quiz settings.
280 * @param \core_question\statistics\questions\calculated $questionstat the question to report on.
282 protected function output_individual_question_data($quiz, $questionstat) {
285 // On-screen display. Show a summary of the question's place in the quiz,
286 // and the question statistics.
287 $datumfromtable = $this->table->format_row($questionstat);
289 // Set up the question info table.
290 $questioninfotable = new html_table();
291 $questioninfotable->align = array('center', 'center');
292 $questioninfotable->width = '60%';
293 $questioninfotable->attributes['class'] = 'generaltable titlesleft';
295 $questioninfotable->data = array();
296 $questioninfotable->data[] = array(get_string('modulename', 'quiz'), $quiz->name);
297 $questioninfotable->data[] = array(get_string('questionname', 'quiz_statistics'),
298 $questionstat->question->name.' '.$datumfromtable['actions']);
300 if ($questionstat->variant !== null) {
301 $questioninfotable->data[] = array(get_string('variant', 'quiz_statistics'), $questionstat->variant);
304 $questioninfotable->data[] = array(get_string('questiontype', 'quiz_statistics'),
305 $datumfromtable['icon'] . ' ' .
306 question_bank::get_qtype($questionstat->question->qtype, false)->menu_name() . ' ' .
307 $datumfromtable['icon']);
308 $questioninfotable->data[] = array(get_string('positions', 'quiz_statistics'),
309 $questionstat->positions);
311 // Set up the question statistics table.
312 $questionstatstable = new html_table();
313 $questionstatstable->align = array('center', 'center');
314 $questionstatstable->width = '60%';
315 $questionstatstable->attributes['class'] = 'generaltable titlesleft';
317 unset($datumfromtable['number']);
318 unset($datumfromtable['icon']);
319 $actions = $datumfromtable['actions'];
320 unset($datumfromtable['actions']);
321 unset($datumfromtable['name']);
323 's' => get_string('attempts', 'quiz_statistics'),
324 'facility' => get_string('facility', 'quiz_statistics'),
325 'sd' => get_string('standarddeviationq', 'quiz_statistics'),
326 'random_guess_score' => get_string('random_guess_score', 'quiz_statistics'),
327 'intended_weight' => get_string('intended_weight', 'quiz_statistics'),
328 'effective_weight' => get_string('effective_weight', 'quiz_statistics'),
329 'discrimination_index' => get_string('discrimination_index', 'quiz_statistics'),
330 'discriminative_efficiency' =>
331 get_string('discriminative_efficiency', 'quiz_statistics')
333 foreach ($datumfromtable as $item => $value) {
334 $questionstatstable->data[] = array($labels[$item], $value);
337 // Display the various bits.
338 echo $OUTPUT->heading(get_string('questioninformation', 'quiz_statistics'), 3);
339 echo html_writer::table($questioninfotable);
340 echo $this->render_question_text($questionstat->question);
341 echo $OUTPUT->heading(get_string('questionstatistics', 'quiz_statistics'), 3);
342 echo html_writer::table($questionstatstable);
346 * Output question text in a box with urls appropriate for a preview of the question.
348 * @param object $question question data.
349 * @return string HTML of question text, ready for display.
351 protected function render_question_text($question) {
354 $text = question_rewrite_question_preview_urls($question->questiontext, $question->id,
355 $question->contextid, 'question', 'questiontext', $question->id,
356 $this->context->id, 'quiz_statistics');
358 return $OUTPUT->box(format_text($text, $question->questiontextformat,
359 array('noclean' => true, 'para' => false, 'overflowdiv' => true)),
360 'questiontext boxaligncenter generalbox boxwidthnormal mdl-align');
364 * Display the response analysis for a question.
366 * @param object $question the question to report on.
367 * @param int|null $variantno the variant
369 * @param moodle_url $reporturl the URL to redisplay this report.
370 * @param qubaid_condition $qubaids
371 * @param string $whichtries
373 protected function output_individual_question_response_analysis($question, $variantno, $s, $reporturl, $qubaids,
374 $whichtries = question_attempt::LAST_TRY) {
377 if (!question_bank::get_qtype($question->qtype, false)->can_analyse_responses()) {
381 $qtable = new quiz_statistics_question_table($question->id);
382 $exportclass = $this->table->export_class_instance();
383 $qtable->export_class_instance($exportclass);
384 if (!$this->table->is_downloading()) {
385 // Output an appropriate title.
386 echo $OUTPUT->heading(get_string('analysisofresponses', 'quiz_statistics'), 3);
389 // Work out an appropriate title.
390 $questiontabletitle = '"' . $question->name . '"';
391 if (!empty($question->number)) {
392 $questiontabletitle = '(' . $question->number . ') ' . $questiontabletitle;
394 if (!is_null($variantno)) {
395 $questiontabletitle .= ' '.get_string('variantno', 'quiz_statistics', $variantno);
397 if ($this->table->is_downloading() == 'xhtml') {
398 $questiontabletitle = get_string('analysisofresponsesfor', 'quiz_statistics', $questiontabletitle);
402 $exportclass->start_table($questiontabletitle);
404 if ($this->table->is_downloading() == 'xhtml') {
405 echo $this->render_question_text($question);
409 $responesanalyser = new \core_question\statistics\responses\analyser($question, $whichtries);
410 $responseanalysis = $responesanalyser->load_cached($qubaids, $whichtries);
412 $qtable->question_setup($reporturl, $question, $s, $responseanalysis);
413 if ($this->table->is_downloading()) {
414 $exportclass->output_headers($qtable->headers);
417 // Where no variant no is specified the variant no is actually one.
418 if ($variantno === null) {
421 foreach ($responseanalysis->get_subpart_ids($variantno) as $partid) {
422 $subpart = $responseanalysis->get_analysis_for_subpart($variantno, $partid);
423 foreach ($subpart->get_response_class_ids() as $responseclassid) {
424 $responseclass = $subpart->get_response_class($responseclassid);
425 $tabledata = $responseclass->data_for_question_response_table($subpart->has_multiple_response_classes(), $partid);
426 foreach ($tabledata as $row) {
427 $qtable->add_data_keyed($qtable->format_row($row));
432 $qtable->finish_output(!$this->table->is_downloading());
436 * Output the table that lists all the questions in the quiz with their statistics.
438 * @param \core_question\statistics\questions\all_calculated_for_qubaid_condition $questionstats the stats for all questions in
439 * the quiz including subqs and
442 protected function output_quiz_structure_analysis_table($questionstats) {
444 $limitvariants = !$this->table->is_downloading();
445 foreach ($questionstats->get_all_slots() as $slot) {
446 // Output the data for these question statistics.
447 $tooutput = array_merge($tooutput, $questionstats->structure_analysis_for_one_slot($slot, $limitvariants));
449 $this->table->format_and_add_array_of_rows($tooutput);
453 * Return HTML for table of overall quiz statistics.
455 * @param array $quizinfo as returned by {@link get_formatted_quiz_info_data()}.
456 * @return string the HTML.
458 protected function output_quiz_info_table($quizinfo) {
460 $quizinfotable = new html_table();
461 $quizinfotable->align = array('center', 'center');
462 $quizinfotable->width = '60%';
463 $quizinfotable->attributes['class'] = 'generaltable titlesleft';
464 $quizinfotable->data = array();
466 foreach ($quizinfo as $heading => $value) {
467 $quizinfotable->data[] = array($heading, $value);
470 return html_writer::table($quizinfotable);
474 * Download the table of overall quiz statistics.
476 * @param array $quizinfo as returned by {@link get_formatted_quiz_info_data()}.
478 protected function download_quiz_info_table($quizinfo) {
481 // XHTML download is a special case.
482 if ($this->table->is_downloading() == 'xhtml') {
483 echo $OUTPUT->heading(get_string('quizinformation', 'quiz_statistics'), 3);
484 echo $this->output_quiz_info_table($quizinfo);
488 // Reformat the data ready for output.
491 foreach ($quizinfo as $heading => $value) {
492 $headers[] = $heading;
497 $exportclass = $this->table->export_class_instance();
498 $exportclass->start_table(get_string('quizinformation', 'quiz_statistics'));
499 $exportclass->output_headers($headers);
500 $exportclass->add_data($row);
501 $exportclass->finish_table();
505 * Output the HTML needed to show the statistics graph.
508 * @param $currentgroup
509 * @param $whichattempts
511 protected function output_statistics_graph($quizid, $currentgroup, $whichattempts) {
514 $output = $PAGE->get_renderer('mod_quiz');
515 $imageurl = new moodle_url('/mod/quiz/report/statistics/statistics_graph.php',
516 compact('quizid', 'currentgroup', 'whichattempts'));
517 $graphname = get_string('statisticsreportgraph', 'quiz_statistics');
518 echo $output->graph($imageurl, $graphname);
522 * Get the quiz and question statistics, either by loading the cached results,
523 * or by recomputing them.
525 * @param object $quiz the quiz settings.
526 * @param string $whichattempts which attempts to use, represented internally as one of the constants as used in
527 * $quiz->grademethod ie.
528 * QUIZ_GRADEAVERAGE, QUIZ_GRADEHIGHEST, QUIZ_ATTEMPTLAST or QUIZ_ATTEMPTFIRST
529 * we calculate stats based on which attempts would affect the grade for each student.
530 * @param string $whichtries which tries to analyse for response analysis. Will be one of
531 * question_attempt::FIRST_TRY, LAST_TRY or ALL_TRIES.
532 * @param array $groupstudents students in this group.
533 * @param array $questions full question data.
534 * @param \core\progress\base|null $progress
535 * @return array with 2 elements: - $quizstats The statistics for overall attempt scores.
536 * - $questionstats \core_question\statistics\questions\all_calculated_for_qubaid_condition
538 public function get_all_stats_and_analysis($quiz, $whichattempts, $whichtries, $groupstudents, $questions, $progress = null) {
540 if ($progress === null) {
541 $progress = new \core\progress\null();
544 $qubaids = quiz_statistics_qubaids_condition($quiz->id, $groupstudents, $whichattempts);
546 $qcalc = new \core_question\statistics\questions\calculator($questions, $progress);
548 $quizcalc = new \quiz_statistics\calculator($progress);
550 $progress->start_progress('', 3);
551 if ($quizcalc->get_last_calculated_time($qubaids) === false) {
554 $questionstats = $qcalc->calculate($qubaids);
555 $progress->progress(1);
557 $quizstats = $quizcalc->calculate($quiz->id, $whichattempts, $groupstudents, count($questions),
558 $qcalc->get_sum_of_mark_variance());
559 $progress->progress(2);
561 $quizstats = $quizcalc->get_cached($qubaids);
562 $progress->progress(1);
563 $questionstats = $qcalc->get_cached($qubaids);
564 $progress->progress(2);
567 if ($quizstats->s()) {
568 $subquestions = $questionstats->get_sub_questions();
569 $this->analyse_responses_for_all_questions_and_subquestions($questions,
575 $progress->progress(3);
576 $progress->end_progress();
578 return array($quizstats, $questionstats);
582 * Appropriate instance depending if we want html output for the user or not.
584 * @return \core\progress\base child of \core\progress\base to handle the display (or not) of task progress.
586 protected function get_progress_trace_instance() {
587 if ($this->progress === null) {
588 if (!$this->table->is_downloading()) {
589 $this->progress = new \core\progress\display_if_slow(get_string('calculatingallstats', 'quiz_statistics'));
590 $this->progress->set_display_names();
592 $this->progress = new \core\progress\null();
595 return $this->progress;
599 * Analyse responses for all questions and sub questions in this quiz.
601 * @param object[] $questions as returned by self::load_and_initialise_questions_for_calculations
602 * @param object[] $subquestions full question objects.
603 * @param qubaid_condition $qubaids the question usages whose responses to analyse.
604 * @param string $whichtries which tries to analyse \question_attempt::FIRST_TRY, LAST_TRY or ALL_TRIES.
605 * @param null|\core\progress\base $progress Used to indicate progress of task.
607 protected function analyse_responses_for_all_questions_and_subquestions($questions, $subquestions, $qubaids,
608 $whichtries, $progress = null) {
609 if ($progress === null) {
610 $progress = new \core\progress\null();
613 // Starting response analysis tasks.
614 $progress->start_progress('', count($questions) + count($subquestions));
616 $done = $this->analyse_responses_for_questions($questions, $qubaids, $whichtries, $progress);
618 $this->analyse_responses_for_questions($subquestions, $qubaids, $whichtries, $progress, $done);
620 // Finished all response analysis tasks.
621 $progress->end_progress();
625 * Analyse responses for an array of questions or sub questions.
627 * @param object[] $questions as returned by self::load_and_initialise_questions_for_calculations.
628 * @param qubaid_condition $qubaids the question usages whose responses to analyse.
629 * @param string $whichtries which tries to analyse \question_attempt::FIRST_TRY, LAST_TRY or ALL_TRIES.
630 * @param null|\core\progress\base $progress Used to indicate progress of task.
631 * @param int[] $done array keys are ids of questions that have been analysed before calling method.
632 * @return array array keys are ids of questions that were analysed after this method call.
634 protected function analyse_responses_for_questions($questions, $qubaids, $whichtries, $progress = null, $done = array()) {
635 $countquestions = count($questions);
636 if (!$countquestions) {
639 if ($progress === null) {
640 $progress = new \core\progress\null();
642 $progress->start_progress('', $countquestions, $countquestions);
643 foreach ($questions as $question) {
644 $progress->increment_progress();
645 if (question_bank::get_qtype($question->qtype, false)->can_analyse_responses() && !isset($done[$question->id])) {
646 $responesstats = new \core_question\statistics\responses\analyser($question, $whichtries);
647 if ($responesstats->get_last_analysed_time($qubaids, $whichtries) === false) {
648 $responesstats->calculate($qubaids, $whichtries);
651 $done[$question->id] = 1;
653 $progress->end_progress();
658 * Return a little form for the user to request to download the full report, including quiz stats and response analysis for
659 * all questions and sub-questions.
661 * @return string HTML.
663 protected function everything_download_options() {
664 $downloadoptions = $this->table->get_download_menu();
666 $downloadelements = new stdClass();
667 $downloadelements->formatsmenu = html_writer::select($downloadoptions, 'download',
668 $this->table->defaultdownloadformat, false);
669 $downloadelements->downloadbutton = '<input type="submit" value="' .
670 get_string('download') . '"/>';
672 $output = '<form action="'. $this->table->baseurl .'" method="post">';
673 $output .= '<div class="mdl-align">';
674 $output .= '<input type="hidden" name="everything" value="1"/>';
675 $output .= html_writer::tag('label', get_string('downloadeverything', 'quiz_statistics', $downloadelements));
676 $output .= '</div></form>';
682 * Return HTML for a message that says when the stats were last calculated and a 'recalculate now' button.
684 * @param int $lastcachetime the time the stats were last cached.
685 * @param int $quizid the quiz id.
686 * @param array $groupstudents ids of students in the group or empty array if groups not used.
687 * @param string $whichattempts which attempts to use, represented internally as one of the constants as used in
688 * $quiz->grademethod ie.
689 * QUIZ_GRADEAVERAGE, QUIZ_GRADEHIGHEST, QUIZ_ATTEMPTLAST or QUIZ_ATTEMPTFIRST
690 * we calculate stats based on which attempts would affect the grade for each student.
691 * @param moodle_url $reporturl url for this report
692 * @return string HTML.
694 protected function output_caching_info($lastcachetime, $quizid, $groupstudents, $whichattempts, $reporturl) {
697 if (empty($lastcachetime)) {
701 // Find the number of attempts since the cached statistics were computed.
702 list($fromqa, $whereqa, $qaparams) = quiz_statistics_attempts_sql($quizid, $groupstudents, $whichattempts, true);
703 $count = $DB->count_records_sql("
707 AND quiza.timefinish > {$lastcachetime}", $qaparams);
713 // Generate the output.
715 $a->lastcalculated = format_time(time() - $lastcachetime);
718 $recalcualteurl = new moodle_url($reporturl,
719 array('recalculate' => 1, 'sesskey' => sesskey()));
721 $output .= $OUTPUT->box_start(
722 'boxaligncenter generalbox boxwidthnormal mdl-align', 'cachingnotice');
723 $output .= get_string('lastcalculated', 'quiz_statistics', $a);
724 $output .= $OUTPUT->single_button($recalcualteurl,
725 get_string('recalculatenow', 'quiz_statistics'));
726 $output .= $OUTPUT->box_end(true);
732 * Clear the cached data for a particular report configuration. This will trigger a re-computation the next time the report
735 * @param $qubaids qubaid_condition
737 protected function clear_cached_data($qubaids) {
739 $DB->delete_records('quiz_statistics', array('hashcode' => $qubaids->get_hash_code()));
740 $DB->delete_records('question_statistics', array('hashcode' => $qubaids->get_hash_code()));
741 $DB->delete_records('question_response_analysis', array('hashcode' => $qubaids->get_hash_code()));
745 * Load the questions in this quiz and add some properties to the objects needed in the reports.
747 * @param object $quiz the quiz.
748 * @return array of questions for this quiz.
750 public function load_and_initialise_questions_for_calculations($quiz) {
751 // Load the questions.
752 $questions = quiz_report_get_significant_questions($quiz);
753 $questionids = array();
754 foreach ($questions as $question) {
755 $questionids[] = $question->id;
757 $fullquestions = question_load_questions($questionids);
758 foreach ($questions as $qno => $question) {
759 $q = $fullquestions[$question->id];
760 $q->maxmark = $question->maxmark;
762 $q->number = $question->number;
763 $questions[$qno] = $q;
769 * Output all response analysis for all questions, sub-questions and variants. For download in a number of formats.
773 * @param $questionstats
775 * @param $whichtries string
777 protected function output_all_question_response_analysis($qubaids,
781 $whichtries = question_attempt::LAST_TRY) {
782 foreach ($questions as $slot => $question) {
783 if (question_bank::get_qtype(
784 $question->qtype, false)->can_analyse_responses()
786 if ($questionstats->for_slot($slot)->get_variants()) {
787 foreach ($questionstats->for_slot($slot)->get_variants() as $variantno) {
788 $this->output_individual_question_response_analysis($question,
790 $questionstats->for_slot($slot, $variantno)->s,
796 $this->output_individual_question_response_analysis($question,
798 $questionstats->for_slot($slot)->s,
803 } else if ($subqids = $questionstats->for_slot($slot)->get_sub_question_ids()) {
804 foreach ($subqids as $subqid) {
805 if ($variants = $questionstats->for_subq($subqid)->get_variants()) {
806 foreach ($variants as $variantno) {
807 $this->output_individual_question_response_analysis(
808 $questionstats->for_subq($subqid, $variantno)->question,
810 $questionstats->for_subq($subqid, $variantno)->s,
816 $this->output_individual_question_response_analysis(
817 $questionstats->for_subq($subqid)->question,
819 $questionstats->for_subq($subqid)->s,