Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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   * Defines the renderer for the quiz module.
  19   *
  20   * @package   mod_quiz
  21   * @copyright 2011 The Open University
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  
  29  /**
  30   * The renderer for the quiz module.
  31   *
  32   * @copyright  2011 The Open University
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class mod_quiz_renderer extends plugin_renderer_base {
  36      /**
  37       * Builds the review page
  38       *
  39       * @param quiz_attempt $attemptobj an instance of quiz_attempt.
  40       * @param array $slots an array of intgers relating to questions.
  41       * @param int $page the current page number
  42       * @param bool $showall whether to show entire attempt on one page.
  43       * @param bool $lastpage if true the current page is the last page.
  44       * @param mod_quiz_display_options $displayoptions instance of mod_quiz_display_options.
  45       * @param array $summarydata contains all table data
  46       * @return $output containing html data.
  47       */
  48      public function review_page(quiz_attempt $attemptobj, $slots, $page, $showall,
  49                                  $lastpage, mod_quiz_display_options $displayoptions,
  50                                  $summarydata) {
  51  
  52          $output = '';
  53          $output .= $this->header();
  54          $output .= $this->review_summary_table($summarydata, $page);
  55          $output .= $this->review_form($page, $showall, $displayoptions,
  56                  $this->questions($attemptobj, true, $slots, $page, $showall, $displayoptions),
  57                  $attemptobj);
  58  
  59          $output .= $this->review_next_navigation($attemptobj, $page, $lastpage, $showall);
  60          $output .= $this->footer();
  61          return $output;
  62      }
  63  
  64      /**
  65       * Renders the review question pop-up.
  66       *
  67       * @param quiz_attempt $attemptobj an instance of quiz_attempt.
  68       * @param int $slot which question to display.
  69       * @param int $seq which step of the question attempt to show. null = latest.
  70       * @param mod_quiz_display_options $displayoptions instance of mod_quiz_display_options.
  71       * @param array $summarydata contains all table data
  72       * @return $output containing html data.
  73       */
  74      public function review_question_page(quiz_attempt $attemptobj, $slot, $seq,
  75              mod_quiz_display_options $displayoptions, $summarydata) {
  76  
  77          $output = '';
  78          $output .= $this->header();
  79          $output .= $this->review_summary_table($summarydata, 0);
  80  
  81          if (!is_null($seq)) {
  82              $output .= $attemptobj->render_question_at_step($slot, $seq, true, $this);
  83          } else {
  84              $output .= $attemptobj->render_question($slot, true, $this);
  85          }
  86  
  87          $output .= $this->close_window_button();
  88          $output .= $this->footer();
  89          return $output;
  90      }
  91  
  92      /**
  93       * Renders the review question pop-up.
  94       *
  95       * @param quiz_attempt $attemptobj an instance of quiz_attempt.
  96       * @param string $message Why the review is not allowed.
  97       * @return string html to output.
  98       */
  99      public function review_question_not_allowed(quiz_attempt $attemptobj, $message) {
 100          $output = '';
 101          $output .= $this->header();
 102          $output .= $this->heading(format_string($attemptobj->get_quiz_name(), true,
 103                                    array("context" => $attemptobj->get_quizobj()->get_context())));
 104          $output .= $this->notification($message);
 105          $output .= $this->close_window_button();
 106          $output .= $this->footer();
 107          return $output;
 108      }
 109  
 110      /**
 111       * Filters the summarydata array.
 112       *
 113       * @param array $summarydata contains row data for table
 114       * @param int $page the current page number
 115       * @return $summarydata containing filtered row data
 116       */
 117      protected function filter_review_summary_table($summarydata, $page) {
 118          if ($page == 0) {
 119              return $summarydata;
 120          }
 121  
 122          // Only show some of summary table on subsequent pages.
 123          foreach ($summarydata as $key => $rowdata) {
 124              if (!in_array($key, array('user', 'attemptlist'))) {
 125                  unset($summarydata[$key]);
 126              }
 127          }
 128  
 129          return $summarydata;
 130      }
 131  
 132      /**
 133       * Outputs the table containing data from summary data array
 134       *
 135       * @param array $summarydata contains row data for table
 136       * @param int $page contains the current page number
 137       */
 138      public function review_summary_table($summarydata, $page) {
 139          $summarydata = $this->filter_review_summary_table($summarydata, $page);
 140          if (empty($summarydata)) {
 141              return '';
 142          }
 143  
 144          $output = '';
 145          $output .= html_writer::start_tag('table', array(
 146                  'class' => 'generaltable generalbox quizreviewsummary'));
 147          $output .= html_writer::start_tag('tbody');
 148          foreach ($summarydata as $rowdata) {
 149              if ($rowdata['title'] instanceof renderable) {
 150                  $title = $this->render($rowdata['title']);
 151              } else {
 152                  $title = $rowdata['title'];
 153              }
 154  
 155              if ($rowdata['content'] instanceof renderable) {
 156                  $content = $this->render($rowdata['content']);
 157              } else {
 158                  $content = $rowdata['content'];
 159              }
 160  
 161              $output .= html_writer::tag('tr',
 162                  html_writer::tag('th', $title, array('class' => 'cell', 'scope' => 'row')) .
 163                          html_writer::tag('td', $content, array('class' => 'cell'))
 164              );
 165          }
 166  
 167          $output .= html_writer::end_tag('tbody');
 168          $output .= html_writer::end_tag('table');
 169          return $output;
 170      }
 171  
 172      /**
 173       * Renders each question
 174       *
 175       * @param quiz_attempt $attemptobj instance of quiz_attempt
 176       * @param bool $reviewing
 177       * @param array $slots array of intgers relating to questions
 178       * @param int $page current page number
 179       * @param bool $showall if true shows attempt on single page
 180       * @param mod_quiz_display_options $displayoptions instance of mod_quiz_display_options
 181       */
 182      public function questions(quiz_attempt $attemptobj, $reviewing, $slots, $page, $showall,
 183                                mod_quiz_display_options $displayoptions) {
 184          $output = '';
 185          foreach ($slots as $slot) {
 186              $output .= $attemptobj->render_question($slot, $reviewing, $this,
 187                      $attemptobj->review_url($slot, $page, $showall));
 188          }
 189          return $output;
 190      }
 191  
 192      /**
 193       * Renders the main bit of the review page.
 194       *
 195       * @param array $summarydata contain row data for table
 196       * @param int $page current page number
 197       * @param mod_quiz_display_options $displayoptions instance of mod_quiz_display_options
 198       * @param $content contains each question
 199       * @param quiz_attempt $attemptobj instance of quiz_attempt
 200       * @param bool $showall if true display attempt on one page
 201       */
 202      public function review_form($page, $showall, $displayoptions, $content, $attemptobj) {
 203          if ($displayoptions->flags != question_display_options::EDITABLE) {
 204              return $content;
 205          }
 206  
 207          $this->page->requires->js_init_call('M.mod_quiz.init_review_form', null, false,
 208                  quiz_get_js_module());
 209  
 210          $output = '';
 211          $output .= html_writer::start_tag('form', array('action' => $attemptobj->review_url(null,
 212                  $page, $showall), 'method' => 'post', 'class' => 'questionflagsaveform'));
 213          $output .= html_writer::start_tag('div');
 214          $output .= $content;
 215          $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey',
 216                  'value' => sesskey()));
 217          $output .= html_writer::start_tag('div', array('class' => 'submitbtns'));
 218          $output .= html_writer::empty_tag('input', array('type' => 'submit',
 219                  'class' => 'questionflagsavebutton btn btn-secondary', 'name' => 'savingflags',
 220                  'value' => get_string('saveflags', 'question')));
 221          $output .= html_writer::end_tag('div');
 222          $output .= html_writer::end_tag('div');
 223          $output .= html_writer::end_tag('form');
 224  
 225          return $output;
 226      }
 227  
 228      /**
 229       * Returns either a liink or button
 230       *
 231       * @param quiz_attempt $attemptobj instance of quiz_attempt
 232       */
 233      public function finish_review_link(quiz_attempt $attemptobj) {
 234          $url = $attemptobj->view_url();
 235  
 236          if ($attemptobj->get_access_manager(time())->attempt_must_be_in_popup()) {
 237              $this->page->requires->js_init_call('M.mod_quiz.secure_window.init_close_button',
 238                      array($url), false, quiz_get_js_module());
 239              return html_writer::empty_tag('input', array('type' => 'button',
 240                      'value' => get_string('finishreview', 'quiz'),
 241                      'id' => 'secureclosebutton',
 242                      'class' => 'mod_quiz-next-nav btn btn-primary'));
 243  
 244          } else {
 245              return html_writer::link($url, get_string('finishreview', 'quiz'),
 246                      array('class' => 'mod_quiz-next-nav'));
 247          }
 248      }
 249  
 250      /**
 251       * Creates the navigation links/buttons at the bottom of the reivew attempt page.
 252       *
 253       * Note, the name of this function is no longer accurate, but when the design
 254       * changed, it was decided to keep the old name for backwards compatibility.
 255       *
 256       * @param quiz_attempt $attemptobj instance of quiz_attempt
 257       * @param int $page the current page
 258       * @param bool $lastpage if true current page is the last page
 259       * @param bool|null $showall if true, the URL will be to review the entire attempt on one page,
 260       *      and $page will be ignored. If null, a sensible default will be chosen.
 261       *
 262       * @return string HTML fragment.
 263       */
 264      public function review_next_navigation(quiz_attempt $attemptobj, $page, $lastpage, $showall = null) {
 265          $nav = '';
 266          if ($page > 0) {
 267              $nav .= link_arrow_left(get_string('navigateprevious', 'quiz'),
 268                      $attemptobj->review_url(null, $page - 1, $showall), false, 'mod_quiz-prev-nav');
 269          }
 270          if ($lastpage) {
 271              $nav .= $this->finish_review_link($attemptobj);
 272          } else {
 273              $nav .= link_arrow_right(get_string('navigatenext', 'quiz'),
 274                      $attemptobj->review_url(null, $page + 1, $showall), false, 'mod_quiz-next-nav');
 275          }
 276          return html_writer::tag('div', $nav, array('class' => 'submitbtns'));
 277      }
 278  
 279      /**
 280       * Return the HTML of the quiz timer.
 281       * @return string HTML content.
 282       */
 283      public function countdown_timer(quiz_attempt $attemptobj, $timenow) {
 284  
 285          $timeleft = $attemptobj->get_time_left_display($timenow);
 286          if ($timeleft !== false) {
 287              $ispreview = $attemptobj->is_preview();
 288              $timerstartvalue = $timeleft;
 289              if (!$ispreview) {
 290                  // Make sure the timer starts just above zero. If $timeleft was <= 0, then
 291                  // this will just have the effect of causing the quiz to be submitted immediately.
 292                  $timerstartvalue = max($timerstartvalue, 1);
 293              }
 294              $this->initialise_timer($timerstartvalue, $ispreview);
 295          }
 296  
 297  
 298          return $this->output->render_from_template('mod_quiz/timer', (object)[]);
 299      }
 300  
 301      /**
 302       * Create a preview link
 303       *
 304       * @param moodle_url $url contains a url to the given page
 305       */
 306      public function restart_preview_button($url) {
 307          return $this->single_button($url, get_string('startnewpreview', 'quiz'));
 308      }
 309  
 310      /**
 311       * Outputs the navigation block panel
 312       *
 313       * @param quiz_nav_panel_base $panel instance of quiz_nav_panel_base
 314       */
 315      public function navigation_panel(quiz_nav_panel_base $panel) {
 316  
 317          $output = '';
 318          $userpicture = $panel->user_picture();
 319          if ($userpicture) {
 320              $fullname = fullname($userpicture->user);
 321              if ($userpicture->size === true) {
 322                  $fullname = html_writer::div($fullname);
 323              }
 324              $output .= html_writer::tag('div', $this->render($userpicture) . $fullname,
 325                      array('id' => 'user-picture', 'class' => 'clearfix'));
 326          }
 327          $output .= $panel->render_before_button_bits($this);
 328  
 329          $bcc = $panel->get_button_container_class();
 330          $output .= html_writer::start_tag('div', array('class' => "qn_buttons clearfix $bcc"));
 331          foreach ($panel->get_question_buttons() as $button) {
 332              $output .= $this->render($button);
 333          }
 334          $output .= html_writer::end_tag('div');
 335  
 336          $output .= html_writer::tag('div', $panel->render_end_bits($this),
 337                  array('class' => 'othernav'));
 338  
 339          $this->page->requires->js_init_call('M.mod_quiz.nav.init', null, false,
 340                  quiz_get_js_module());
 341  
 342          return $output;
 343      }
 344  
 345      /**
 346       * Display a quiz navigation button.
 347       *
 348       * @param quiz_nav_question_button $button
 349       * @return string HTML fragment.
 350       */
 351      protected function render_quiz_nav_question_button(quiz_nav_question_button $button) {
 352          $classes = array('qnbutton', $button->stateclass, $button->navmethod, 'btn');
 353          $extrainfo = array();
 354  
 355          if ($button->currentpage) {
 356              $classes[] = 'thispage';
 357              $extrainfo[] = get_string('onthispage', 'quiz');
 358          }
 359  
 360          // Flagged?
 361          if ($button->flagged) {
 362              $classes[] = 'flagged';
 363              $flaglabel = get_string('flagged', 'question');
 364          } else {
 365              $flaglabel = '';
 366          }
 367          $extrainfo[] = html_writer::tag('span', $flaglabel, array('class' => 'flagstate'));
 368  
 369          if (is_numeric($button->number)) {
 370              $qnostring = 'questionnonav';
 371          } else {
 372              $qnostring = 'questionnonavinfo';
 373          }
 374  
 375          $a = new stdClass();
 376          $a->number = $button->number;
 377          $a->attributes = implode(' ', $extrainfo);
 378          $tagcontents = html_writer::tag('span', '', array('class' => 'thispageholder')) .
 379                          html_writer::tag('span', '', array('class' => 'trafficlight')) .
 380                          get_string($qnostring, 'quiz', $a);
 381          $tagattributes = array('class' => implode(' ', $classes), 'id' => $button->id,
 382                                    'title' => $button->statestring, 'data-quiz-page' => $button->page);
 383  
 384          if ($button->url) {
 385              return html_writer::link($button->url, $tagcontents, $tagattributes);
 386          } else {
 387              return html_writer::tag('span', $tagcontents, $tagattributes);
 388          }
 389      }
 390  
 391      /**
 392       * Display a quiz navigation heading.
 393       *
 394       * @param quiz_nav_section_heading $heading the heading.
 395       * @return string HTML fragment.
 396       */
 397      protected function render_quiz_nav_section_heading(quiz_nav_section_heading $heading) {
 398          if (empty($heading->heading)) {
 399              $headingtext = get_string('sectionnoname', 'quiz');
 400              $class = ' dimmed_text';
 401          } else {
 402              $headingtext = $heading->heading;
 403              $class = '';
 404          }
 405          return $this->heading($headingtext, 3, 'mod_quiz-section-heading' . $class);
 406      }
 407  
 408      /**
 409       * outputs the link the other attempts.
 410       *
 411       * @param mod_quiz_links_to_other_attempts $links
 412       */
 413      protected function render_mod_quiz_links_to_other_attempts(
 414              mod_quiz_links_to_other_attempts $links) {
 415          $attemptlinks = array();
 416          foreach ($links->links as $attempt => $url) {
 417              if (!$url) {
 418                  $attemptlinks[] = html_writer::tag('strong', $attempt);
 419              } else if ($url instanceof renderable) {
 420                  $attemptlinks[] = $this->render($url);
 421              } else {
 422                  $attemptlinks[] = html_writer::link($url, $attempt);
 423              }
 424          }
 425          return implode(', ', $attemptlinks);
 426      }
 427  
 428      public function start_attempt_page(quiz $quizobj, mod_quiz_preflight_check_form $mform) {
 429          $output = '';
 430          $output .= $this->header();
 431          $output .= $this->heading(format_string($quizobj->get_quiz_name(), true,
 432                                    array("context" => $quizobj->get_context())));
 433          $output .= $this->quiz_intro($quizobj->get_quiz(), $quizobj->get_cm());
 434          $output .= $mform->render();
 435          $output .= $this->footer();
 436          return $output;
 437      }
 438  
 439      /**
 440       * Attempt Page
 441       *
 442       * @param quiz_attempt $attemptobj Instance of quiz_attempt
 443       * @param int $page Current page number
 444       * @param quiz_access_manager $accessmanager Instance of quiz_access_manager
 445       * @param array $messages An array of messages
 446       * @param array $slots Contains an array of integers that relate to questions
 447       * @param int $id The ID of an attempt
 448       * @param int $nextpage The number of the next page
 449       */
 450      public function attempt_page($attemptobj, $page, $accessmanager, $messages, $slots, $id,
 451              $nextpage) {
 452          $output = '';
 453          $output .= $this->header();
 454          $output .= $this->quiz_notices($messages);
 455          $output .= $this->countdown_timer($attemptobj, time());
 456          $output .= $this->attempt_form($attemptobj, $page, $slots, $id, $nextpage);
 457          $output .= $this->footer();
 458          return $output;
 459      }
 460  
 461      /**
 462       * Returns any notices.
 463       *
 464       * @param array $messages
 465       */
 466      public function quiz_notices($messages) {
 467          if (!$messages) {
 468              return '';
 469          }
 470          return $this->box($this->heading(get_string('accessnoticesheader', 'quiz'), 3) .
 471                  $this->access_messages($messages), 'quizaccessnotices');
 472      }
 473  
 474      /**
 475       * Ouputs the form for making an attempt
 476       *
 477       * @param quiz_attempt $attemptobj
 478       * @param int $page Current page number
 479       * @param array $slots Array of integers relating to questions
 480       * @param int $id ID of the attempt
 481       * @param int $nextpage Next page number
 482       */
 483      public function attempt_form($attemptobj, $page, $slots, $id, $nextpage) {
 484          $output = '';
 485  
 486          // Start the form.
 487          $output .= html_writer::start_tag('form',
 488                  array('action' => new moodle_url($attemptobj->processattempt_url(),
 489                  array('cmid' => $attemptobj->get_cmid())), 'method' => 'post',
 490                  'enctype' => 'multipart/form-data', 'accept-charset' => 'utf-8',
 491                  'id' => 'responseform'));
 492          $output .= html_writer::start_tag('div');
 493  
 494          // Print all the questions.
 495          foreach ($slots as $slot) {
 496              $output .= $attemptobj->render_question($slot, false, $this,
 497                      $attemptobj->attempt_url($slot, $page), $this);
 498          }
 499  
 500          $navmethod = $attemptobj->get_quiz()->navmethod;
 501          $output .= $this->attempt_navigation_buttons($page, $attemptobj->is_last_page($page), $navmethod);
 502  
 503          // Some hidden fields to trach what is going on.
 504          $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'attempt',
 505                  'value' => $attemptobj->get_attemptid()));
 506          $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'thispage',
 507                  'value' => $page, 'id' => 'followingpage'));
 508          $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'nextpage',
 509                  'value' => $nextpage));
 510          $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'timeup',
 511                  'value' => '0', 'id' => 'timeup'));
 512          $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey',
 513                  'value' => sesskey()));
 514          $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'scrollpos',
 515                  'value' => '', 'id' => 'scrollpos'));
 516  
 517          // Add a hidden field with questionids. Do this at the end of the form, so
 518          // if you navigate before the form has finished loading, it does not wipe all
 519          // the student's answers.
 520          $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'slots',
 521                  'value' => implode(',', $attemptobj->get_active_slots($page))));
 522  
 523          // Finish the form.
 524          $output .= html_writer::end_tag('div');
 525          $output .= html_writer::end_tag('form');
 526  
 527          $output .= $this->connection_warning();
 528  
 529          return $output;
 530      }
 531  
 532      /**
 533       * Display the prev/next buttons that go at the bottom of each page of the attempt.
 534       *
 535       * @param int $page the page number. Starts at 0 for the first page.
 536       * @param bool $lastpage is this the last page in the quiz?
 537       * @param string $navmethod Optional quiz attribute, 'free' (default) or 'sequential'
 538       * @return string HTML fragment.
 539       */
 540      protected function attempt_navigation_buttons($page, $lastpage, $navmethod = 'free') {
 541          $output = '';
 542  
 543          $output .= html_writer::start_tag('div', array('class' => 'submitbtns'));
 544          if ($page > 0 && $navmethod == 'free') {
 545              $output .= html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'previous',
 546                      'value' => get_string('navigateprevious', 'quiz'), 'class' => 'mod_quiz-prev-nav btn btn-secondary',
 547                      'id' => 'mod_quiz-prev-nav'));
 548              $this->page->requires->js_call_amd('core_form/submit', 'init', ['mod_quiz-prev-nav']);
 549          }
 550          if ($lastpage) {
 551              $nextlabel = get_string('endtest', 'quiz');
 552          } else {
 553              $nextlabel = get_string('navigatenext', 'quiz');
 554          }
 555          $output .= html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'next',
 556                  'value' => $nextlabel, 'class' => 'mod_quiz-next-nav btn btn-primary', 'id' => 'mod_quiz-next-nav'));
 557          $output .= html_writer::end_tag('div');
 558          $this->page->requires->js_call_amd('core_form/submit', 'init', ['mod_quiz-next-nav']);
 559  
 560          return $output;
 561      }
 562  
 563      /**
 564       * Render a button which allows students to redo a question in the attempt.
 565       *
 566       * @param int $slot the number of the slot to generate the button for.
 567       * @param bool $disabled if true, output the button disabled.
 568       * @return string HTML fragment.
 569       */
 570      public function redo_question_button($slot, $disabled) {
 571          $attributes = array('type' => 'submit',  'name' => 'redoslot' . $slot,
 572              'value' => get_string('redoquestion', 'quiz'),
 573              'class' => 'mod_quiz-redo_question_button btn btn-secondary');
 574          if ($disabled) {
 575              $attributes['disabled'] = 'disabled';
 576          }
 577          return html_writer::div(html_writer::empty_tag('input', $attributes));
 578      }
 579  
 580      /**
 581       * Output the JavaScript required to initialise the countdown timer.
 582       * @param int $timerstartvalue time remaining, in seconds.
 583       */
 584      public function initialise_timer($timerstartvalue, $ispreview) {
 585          $options = array($timerstartvalue, (bool)$ispreview);
 586          $this->page->requires->js_init_call('M.mod_quiz.timer.init', $options, false, quiz_get_js_module());
 587      }
 588  
 589      /**
 590       * Output a page with an optional message, and JavaScript code to close the
 591       * current window and redirect the parent window to a new URL.
 592       * @param moodle_url $url the URL to redirect the parent window to.
 593       * @param string $message message to display before closing the window. (optional)
 594       * @return string HTML to output.
 595       */
 596      public function close_attempt_popup($url, $message = '') {
 597          $output = '';
 598          $output .= $this->header();
 599          $output .= $this->box_start();
 600  
 601          if ($message) {
 602              $output .= html_writer::tag('p', $message);
 603              $output .= html_writer::tag('p', get_string('windowclosing', 'quiz'));
 604              $delay = 5;
 605          } else {
 606              $output .= html_writer::tag('p', get_string('pleaseclose', 'quiz'));
 607              $delay = 0;
 608          }
 609          $this->page->requires->js_init_call('M.mod_quiz.secure_window.close',
 610                  array($url, $delay), false, quiz_get_js_module());
 611  
 612          $output .= $this->box_end();
 613          $output .= $this->footer();
 614          return $output;
 615      }
 616  
 617      /**
 618       * Print each message in an array, surrounded by &lt;p>, &lt;/p> tags.
 619       *
 620       * @param array $messages the array of message strings.
 621       * @param bool $return if true, return a string, instead of outputting.
 622       *
 623       * @return string HTML to output.
 624       */
 625      public function access_messages($messages) {
 626          $output = '';
 627          foreach ($messages as $message) {
 628              $output .= html_writer::tag('p', $message) . "\n";
 629          }
 630          return $output;
 631      }
 632  
 633      /*
 634       * Summary Page
 635       */
 636      /**
 637       * Create the summary page
 638       *
 639       * @param quiz_attempt $attemptobj
 640       * @param mod_quiz_display_options $displayoptions
 641       */
 642      public function summary_page($attemptobj, $displayoptions) {
 643          $output = '';
 644          $output .= $this->header();
 645          $output .= $this->heading(format_string($attemptobj->get_quiz_name()));
 646          $output .= $this->heading(get_string('summaryofattempt', 'quiz'), 3);
 647          $output .= $this->summary_table($attemptobj, $displayoptions);
 648          $output .= $this->summary_page_controls($attemptobj);
 649          $output .= $this->footer();
 650          return $output;
 651      }
 652  
 653      /**
 654       * Generates the table of summarydata
 655       *
 656       * @param quiz_attempt $attemptobj
 657       * @param mod_quiz_display_options $displayoptions
 658       */
 659      public function summary_table($attemptobj, $displayoptions) {
 660          // Prepare the summary table header.
 661          $table = new html_table();
 662          $table->attributes['class'] = 'generaltable quizsummaryofattempt boxaligncenter';
 663          $table->head = array(get_string('question', 'quiz'), get_string('status', 'quiz'));
 664          $table->align = array('left', 'left');
 665          $table->size = array('', '');
 666          $markscolumn = $displayoptions->marks >= question_display_options::MARK_AND_MAX;
 667          if ($markscolumn) {
 668              $table->head[] = get_string('marks', 'quiz');
 669              $table->align[] = 'left';
 670              $table->size[] = '';
 671          }
 672          $tablewidth = count($table->align);
 673          $table->data = array();
 674  
 675          // Get the summary info for each question.
 676          $slots = $attemptobj->get_slots();
 677          foreach ($slots as $slot) {
 678              // Add a section headings if we need one here.
 679              $heading = $attemptobj->get_heading_before_slot($slot);
 680              if ($heading) {
 681                  $heading = format_string($heading);
 682              }
 683              $sections = $attemptobj->get_quizobj()->get_sections();
 684              if (!is_null($heading) && empty($heading) && count($sections) > 1) {
 685                  $heading = get_string('sectionnoname', 'quiz');
 686                  $heading = \html_writer::span($heading, 'dimmed_text');
 687              }
 688  
 689              if ($heading) {
 690                  $cell = new html_table_cell($heading);
 691                  $cell->header = true;
 692                  $cell->colspan = $tablewidth;
 693                  $table->data[] = array($cell);
 694                  $table->rowclasses[] = 'quizsummaryheading';
 695              }
 696  
 697              // Don't display information items.
 698              if (!$attemptobj->is_real_question($slot)) {
 699                  continue;
 700              }
 701  
 702              // Real question, show it.
 703              $flag = '';
 704              if ($attemptobj->is_question_flagged($slot)) {
 705                  // Quiz has custom JS manipulating these image tags - so we can't use the pix_icon method here.
 706                  $flag = html_writer::empty_tag('img', array('src' => $this->image_url('i/flagged'),
 707                          'alt' => get_string('flagged', 'question'), 'class' => 'questionflag icon-post'));
 708              }
 709              if ($attemptobj->can_navigate_to($slot)) {
 710                  $row = array(html_writer::link($attemptobj->attempt_url($slot),
 711                          $attemptobj->get_question_number($slot) . $flag),
 712                          $attemptobj->get_question_status($slot, $displayoptions->correctness));
 713              } else {
 714                  $row = array($attemptobj->get_question_number($slot) . $flag,
 715                                  $attemptobj->get_question_status($slot, $displayoptions->correctness));
 716              }
 717              if ($markscolumn) {
 718                  $row[] = $attemptobj->get_question_mark($slot);
 719              }
 720              $table->data[] = $row;
 721              $table->rowclasses[] = 'quizsummary' . $slot . ' ' . $attemptobj->get_question_state_class(
 722                      $slot, $displayoptions->correctness);
 723          }
 724  
 725          // Print the summary table.
 726          $output = html_writer::table($table);
 727  
 728          return $output;
 729      }
 730  
 731      /**
 732       * Creates any controls a the page should have.
 733       *
 734       * @param quiz_attempt $attemptobj
 735       */
 736      public function summary_page_controls($attemptobj) {
 737          $output = '';
 738  
 739          // Return to place button.
 740          if ($attemptobj->get_state() == quiz_attempt::IN_PROGRESS) {
 741              $button = new single_button(
 742                      new moodle_url($attemptobj->attempt_url(null, $attemptobj->get_currentpage())),
 743                      get_string('returnattempt', 'quiz'));
 744              $output .= $this->container($this->container($this->render($button),
 745                      'controls'), 'submitbtns mdl-align');
 746          }
 747  
 748          // Finish attempt button.
 749          $options = array(
 750              'attempt' => $attemptobj->get_attemptid(),
 751              'finishattempt' => 1,
 752              'timeup' => 0,
 753              'slots' => '',
 754              'cmid' => $attemptobj->get_cmid(),
 755              'sesskey' => sesskey(),
 756          );
 757  
 758          $button = new single_button(
 759                  new moodle_url($attemptobj->processattempt_url(), $options),
 760                  get_string('submitallandfinish', 'quiz'));
 761          $button->id = 'responseform';
 762          if ($attemptobj->get_state() == quiz_attempt::IN_PROGRESS) {
 763              $button->add_action(new confirm_action(get_string('confirmclose', 'quiz'), null,
 764                      get_string('submitallandfinish', 'quiz')));
 765          }
 766  
 767          $duedate = $attemptobj->get_due_date();
 768          $message = '';
 769          if ($attemptobj->get_state() == quiz_attempt::OVERDUE) {
 770              $message = get_string('overduemustbesubmittedby', 'quiz', userdate($duedate));
 771  
 772          } else if ($duedate) {
 773              $message = get_string('mustbesubmittedby', 'quiz', userdate($duedate));
 774          }
 775  
 776          $output .= $this->countdown_timer($attemptobj, time());
 777          $output .= $this->container($message . $this->container(
 778                  $this->render($button), 'controls'), 'submitbtns mdl-align');
 779  
 780          return $output;
 781      }
 782  
 783      /*
 784       * View Page
 785       */
 786      /**
 787       * Generates the view page
 788       *
 789       * @param int $course The id of the course
 790       * @param array $quiz Array conting quiz data
 791       * @param int $cm Course Module ID
 792       * @param int $context The page context ID
 793       * @param array $infomessages information about this quiz
 794       * @param mod_quiz_view_object $viewobj
 795       * @param string $buttontext text for the start/continue attempt button, if
 796       *      it should be shown.
 797       * @param array $infomessages further information about why the student cannot
 798       *      attempt this quiz now, if appicable this quiz
 799       */
 800      public function view_page($course, $quiz, $cm, $context, $viewobj) {
 801          $output = '';
 802          $output .= $this->view_information($quiz, $cm, $context, $viewobj->infomessages);
 803          $output .= $this->view_table($quiz, $context, $viewobj);
 804          $output .= $this->view_result_info($quiz, $context, $cm, $viewobj);
 805          $output .= $this->box($this->view_page_buttons($viewobj), 'quizattempt');
 806          return $output;
 807      }
 808  
 809      /**
 810       * Work out, and render, whatever buttons, and surrounding info, should appear
 811       * at the end of the review page.
 812       * @param mod_quiz_view_object $viewobj the information required to display
 813       * the view page.
 814       * @return string HTML to output.
 815       */
 816      public function view_page_buttons(mod_quiz_view_object $viewobj) {
 817          global $CFG;
 818          $output = '';
 819  
 820          if (!$viewobj->quizhasquestions) {
 821              $output .= $this->no_questions_message($viewobj->canedit, $viewobj->editurl);
 822          }
 823  
 824          $output .= $this->access_messages($viewobj->preventmessages);
 825  
 826          if ($viewobj->buttontext) {
 827              $output .= $this->start_attempt_button($viewobj->buttontext,
 828                      $viewobj->startattempturl, $viewobj->preflightcheckform,
 829                      $viewobj->popuprequired, $viewobj->popupoptions);
 830          }
 831  
 832          if ($viewobj->showbacktocourse) {
 833              $output .= $this->single_button($viewobj->backtocourseurl,
 834                      get_string('backtocourse', 'quiz'), 'get',
 835                      array('class' => 'continuebutton'));
 836          }
 837  
 838          return $output;
 839      }
 840  
 841      /**
 842       * Generates the view attempt button
 843       *
 844       * @param string $buttontext the label to display on the button.
 845       * @param moodle_url $url The URL to POST to in order to start the attempt.
 846       * @param mod_quiz_preflight_check_form $preflightcheckform deprecated.
 847       * @param bool $popuprequired whether the attempt needs to be opened in a pop-up.
 848       * @param array $popupoptions the options to use if we are opening a popup.
 849       * @return string HTML fragment.
 850       */
 851      public function start_attempt_button($buttontext, moodle_url $url,
 852              mod_quiz_preflight_check_form $preflightcheckform = null,
 853              $popuprequired = false, $popupoptions = null) {
 854  
 855          if (is_string($preflightcheckform)) {
 856              // Calling code was not updated since the API change.
 857              debugging('The third argument to start_attempt_button should now be the ' .
 858                      'mod_quiz_preflight_check_form from ' .
 859                      'quiz_access_manager::get_preflight_check_form, not a warning message string.');
 860          }
 861  
 862          $button = new single_button($url, $buttontext, 'post', true);
 863          $button->class .= ' quizstartbuttondiv';
 864          if ($popuprequired) {
 865              $button->class .= ' quizsecuremoderequired';
 866          }
 867  
 868          $popupjsoptions = null;
 869          if ($popuprequired && $popupoptions) {
 870              $action = new popup_action('click', $url, 'popup', $popupoptions);
 871              $popupjsoptions = $action->get_js_options();
 872          }
 873  
 874          if ($preflightcheckform) {
 875              $checkform = $preflightcheckform->render();
 876          } else {
 877              $checkform = null;
 878          }
 879  
 880          $this->page->requires->js_call_amd('mod_quiz/preflightcheck', 'init',
 881                  array('.quizstartbuttondiv [type=submit]', get_string('startattempt', 'quiz'),
 882                         '#mod_quiz_preflight_form', $popupjsoptions));
 883  
 884          return $this->render($button) . $checkform;
 885      }
 886  
 887      /**
 888       * Generate a message saying that this quiz has no questions, with a button to
 889       * go to the edit page, if the user has the right capability.
 890       * @param object $quiz the quiz settings.
 891       * @param object $cm the course_module object.
 892       * @param object $context the quiz context.
 893       * @return string HTML to output.
 894       */
 895      public function no_questions_message($canedit, $editurl) {
 896          $output = html_writer::start_tag('div', array('class' => 'card text-center mb-3'));
 897          $output .= html_writer::start_tag('div', array('class' => 'card-body'));
 898  
 899          $output .= $this->notification(get_string('noquestions', 'quiz'), 'warning', false);
 900          if ($canedit) {
 901              $output .= $this->single_button($editurl, get_string('editquiz', 'quiz'), 'get');
 902          }
 903          $output .= html_writer::end_tag('div');
 904          $output .= html_writer::end_tag('div');
 905  
 906          return $output;
 907      }
 908  
 909      /**
 910       * Outputs an error message for any guests accessing the quiz
 911       *
 912       * @param int $course The course ID
 913       * @param array $quiz Array contingin quiz data
 914       * @param int $cm Course Module ID
 915       * @param int $context The page contect ID
 916       * @param array $messages Array containing any messages
 917       */
 918      public function view_page_guest($course, $quiz, $cm, $context, $messages) {
 919          $output = '';
 920          $output .= $this->view_information($quiz, $cm, $context, $messages);
 921          $guestno = html_writer::tag('p', get_string('guestsno', 'quiz'));
 922          $liketologin = html_writer::tag('p', get_string('liketologin'));
 923          $referer = get_local_referer(false);
 924          $output .= $this->confirm($guestno."\n\n".$liketologin."\n", get_login_url(), $referer);
 925          return $output;
 926      }
 927  
 928      /**
 929       * Outputs and error message for anyone who is not enrolle don the course
 930       *
 931       * @param int $course The course ID
 932       * @param array $quiz Array contingin quiz data
 933       * @param int $cm Course Module ID
 934       * @param int $context The page contect ID
 935       * @param array $messages Array containing any messages
 936       */
 937      public function view_page_notenrolled($course, $quiz, $cm, $context, $messages) {
 938          global $CFG;
 939          $output = '';
 940          $output .= $this->view_information($quiz, $cm, $context, $messages);
 941          $youneedtoenrol = html_writer::tag('p', get_string('youneedtoenrol', 'quiz'));
 942          $button = html_writer::tag('p',
 943                  $this->continue_button($CFG->wwwroot . '/course/view.php?id=' . $course->id));
 944          $output .= $this->box($youneedtoenrol."\n\n".$button."\n", 'generalbox', 'notice');
 945          return $output;
 946      }
 947  
 948      /**
 949       * Output the page information
 950       *
 951       * @param object $quiz the quiz settings.
 952       * @param object $cm the course_module object.
 953       * @param context $context the quiz context.
 954       * @param array $messages any access messages that should be described.
 955       * @return string HTML to output.
 956       */
 957      public function view_information($quiz, $cm, $context, $messages) {
 958          global $USER;
 959  
 960          $output = '';
 961  
 962          // Print quiz name.
 963          $output .= $this->heading(format_string($quiz->name));
 964  
 965          // Print any activity information (eg completion requirements / dates).
 966          $cminfo = cm_info::create($cm);
 967          $completiondetails = \core_completion\cm_completion_details::get_instance($cminfo, $USER->id);
 968          $activitydates = \core\activity_dates::get_dates_for_module($cminfo, $USER->id);
 969          $output .= $this->output->activity_information($cminfo, $completiondetails, $activitydates);
 970  
 971          // Print quiz description.
 972          $output .= $this->quiz_intro($quiz, $cm);
 973  
 974          // Output any access messages.
 975          if ($messages) {
 976              $output .= $this->box($this->access_messages($messages), 'quizinfo');
 977          }
 978  
 979          // Show number of attempts summary to those who can view reports.
 980          if (has_capability('mod/quiz:viewreports', $context)) {
 981              if ($strattemptnum = $this->quiz_attempt_summary_link_to_reports($quiz, $cm,
 982                      $context)) {
 983                  $output .= html_writer::tag('div', $strattemptnum,
 984                          array('class' => 'quizattemptcounts'));
 985              }
 986          }
 987  
 988          if (has_any_capability(['mod/quiz:manageoverrides', 'mod/quiz:viewoverrides'], $context)) {
 989              if ($overrideinfo = $this->quiz_override_summary_links($quiz, $cm)) {
 990                  $output .= html_writer::tag('div', $overrideinfo, ['class' => 'quizattemptcounts']);
 991              }
 992          }
 993  
 994          return $output;
 995      }
 996  
 997      /**
 998       * Output the quiz intro.
 999       * @param object $quiz the quiz settings.
1000       * @param object $cm the course_module object.
1001       * @return string HTML to output.
1002       */
1003      public function quiz_intro($quiz, $cm) {
1004          if (html_is_blank($quiz->intro)) {
1005              return '';
1006          }
1007  
1008          return $this->box(format_module_intro('quiz', $quiz, $cm->id), 'generalbox', 'intro');
1009      }
1010  
1011      /**
1012       * Generates the table heading.
1013       */
1014      public function view_table_heading() {
1015          return $this->heading(get_string('summaryofattempts', 'quiz'), 3);
1016      }
1017  
1018      /**
1019       * Generates the table of data
1020       *
1021       * @param array $quiz Array contining quiz data
1022       * @param int $context The page context ID
1023       * @param mod_quiz_view_object $viewobj
1024       */
1025      public function view_table($quiz, $context, $viewobj) {
1026          if (!$viewobj->attempts) {
1027              return '';
1028          }
1029  
1030          // Prepare table header.
1031          $table = new html_table();
1032          $table->attributes['class'] = 'generaltable quizattemptsummary';
1033          $table->head = array();
1034          $table->align = array();
1035          $table->size = array();
1036          if ($viewobj->attemptcolumn) {
1037              $table->head[] = get_string('attemptnumber', 'quiz');
1038              $table->align[] = 'center';
1039              $table->size[] = '';
1040          }
1041          $table->head[] = get_string('attemptstate', 'quiz');
1042          $table->align[] = 'left';
1043          $table->size[] = '';
1044          if ($viewobj->markcolumn) {
1045              $table->head[] = get_string('marks', 'quiz') . ' / ' .
1046                      quiz_format_grade($quiz, $quiz->sumgrades);
1047              $table->align[] = 'center';
1048              $table->size[] = '';
1049          }
1050          if ($viewobj->gradecolumn) {
1051              $table->head[] = get_string('gradenoun') . ' / ' .
1052                      quiz_format_grade($quiz, $quiz->grade);
1053              $table->align[] = 'center';
1054              $table->size[] = '';
1055          }
1056          if ($viewobj->canreviewmine) {
1057              $table->head[] = get_string('review', 'quiz');
1058              $table->align[] = 'center';
1059              $table->size[] = '';
1060          }
1061          if ($viewobj->feedbackcolumn) {
1062              $table->head[] = get_string('feedback', 'quiz');
1063              $table->align[] = 'left';
1064              $table->size[] = '';
1065          }
1066  
1067          // One row for each attempt.
1068          foreach ($viewobj->attemptobjs as $attemptobj) {
1069              $attemptoptions = $attemptobj->get_display_options(true);
1070              $row = array();
1071  
1072              // Add the attempt number.
1073              if ($viewobj->attemptcolumn) {
1074                  if ($attemptobj->is_preview()) {
1075                      $row[] = get_string('preview', 'quiz');
1076                  } else {
1077                      $row[] = $attemptobj->get_attempt_number();
1078                  }
1079              }
1080  
1081              $row[] = $this->attempt_state($attemptobj);
1082  
1083              if ($viewobj->markcolumn) {
1084                  if ($attemptoptions->marks >= question_display_options::MARK_AND_MAX &&
1085                          $attemptobj->is_finished()) {
1086                      $row[] = quiz_format_grade($quiz, $attemptobj->get_sum_marks());
1087                  } else {
1088                      $row[] = '';
1089                  }
1090              }
1091  
1092              // Ouside the if because we may be showing feedback but not grades.
1093              $attemptgrade = quiz_rescale_grade($attemptobj->get_sum_marks(), $quiz, false);
1094  
1095              if ($viewobj->gradecolumn) {
1096                  if ($attemptoptions->marks >= question_display_options::MARK_AND_MAX &&
1097                          $attemptobj->is_finished()) {
1098  
1099                      // Highlight the highest grade if appropriate.
1100                      if ($viewobj->overallstats && !$attemptobj->is_preview()
1101                              && $viewobj->numattempts > 1 && !is_null($viewobj->mygrade)
1102                              && $attemptobj->get_state() == quiz_attempt::FINISHED
1103                              && $attemptgrade == $viewobj->mygrade
1104                              && $quiz->grademethod == QUIZ_GRADEHIGHEST) {
1105                          $table->rowclasses[$attemptobj->get_attempt_number()] = 'bestrow';
1106                      }
1107  
1108                      $row[] = quiz_format_grade($quiz, $attemptgrade);
1109                  } else {
1110                      $row[] = '';
1111                  }
1112              }
1113  
1114              if ($viewobj->canreviewmine) {
1115                  $row[] = $viewobj->accessmanager->make_review_link($attemptobj->get_attempt(),
1116                          $attemptoptions, $this);
1117              }
1118  
1119              if ($viewobj->feedbackcolumn && $attemptobj->is_finished()) {
1120                  if ($attemptoptions->overallfeedback) {
1121                      $row[] = quiz_feedback_for_grade($attemptgrade, $quiz, $context);
1122                  } else {
1123                      $row[] = '';
1124                  }
1125              }
1126  
1127              if ($attemptobj->is_preview()) {
1128                  $table->data['preview'] = $row;
1129              } else {
1130                  $table->data[$attemptobj->get_attempt_number()] = $row;
1131              }
1132          } // End of loop over attempts.
1133  
1134          $output = '';
1135          $output .= $this->view_table_heading();
1136          $output .= html_writer::table($table);
1137          return $output;
1138      }
1139  
1140      /**
1141       * Generate a brief textual desciption of the current state of an attempt.
1142       * @param quiz_attempt $attemptobj the attempt
1143       * @param int $timenow the time to use as 'now'.
1144       * @return string the appropriate lang string to describe the state.
1145       */
1146      public function attempt_state($attemptobj) {
1147          switch ($attemptobj->get_state()) {
1148              case quiz_attempt::IN_PROGRESS:
1149                  return get_string('stateinprogress', 'quiz');
1150  
1151              case quiz_attempt::OVERDUE:
1152                  return get_string('stateoverdue', 'quiz') . html_writer::tag('span',
1153                          get_string('stateoverduedetails', 'quiz',
1154                                  userdate($attemptobj->get_due_date())),
1155                          array('class' => 'statedetails'));
1156  
1157              case quiz_attempt::FINISHED:
1158                  return get_string('statefinished', 'quiz') . html_writer::tag('span',
1159                          get_string('statefinisheddetails', 'quiz',
1160                                  userdate($attemptobj->get_submitted_date())),
1161                          array('class' => 'statedetails'));
1162  
1163              case quiz_attempt::ABANDONED:
1164                  return get_string('stateabandoned', 'quiz');
1165          }
1166      }
1167  
1168      /**
1169       * Generates data pertaining to quiz results
1170       *
1171       * @param array $quiz Array containing quiz data
1172       * @param int $context The page context ID
1173       * @param int $cm The Course Module Id
1174       * @param mod_quiz_view_object $viewobj
1175       */
1176      public function view_result_info($quiz, $context, $cm, $viewobj) {
1177          $output = '';
1178          if (!$viewobj->numattempts && !$viewobj->gradecolumn && is_null($viewobj->mygrade)) {
1179              return $output;
1180          }
1181          $resultinfo = '';
1182  
1183          if ($viewobj->overallstats) {
1184              if ($viewobj->moreattempts) {
1185                  $a = new stdClass();
1186                  $a->method = quiz_get_grading_option_name($quiz->grademethod);
1187                  $a->mygrade = quiz_format_grade($quiz, $viewobj->mygrade);
1188                  $a->quizgrade = quiz_format_grade($quiz, $quiz->grade);
1189                  $resultinfo .= $this->heading(get_string('gradesofar', 'quiz', $a), 3);
1190              } else {
1191                  $a = new stdClass();
1192                  $a->grade = quiz_format_grade($quiz, $viewobj->mygrade);
1193                  $a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
1194                  $a = get_string('outofshort', 'quiz', $a);
1195                  $resultinfo .= $this->heading(get_string('yourfinalgradeis', 'quiz', $a), 3);
1196              }
1197          }
1198  
1199          if ($viewobj->mygradeoverridden) {
1200  
1201              $resultinfo .= html_writer::tag('p', get_string('overriddennotice', 'grades'),
1202                      array('class' => 'overriddennotice'))."\n";
1203          }
1204          if ($viewobj->gradebookfeedback) {
1205              $resultinfo .= $this->heading(get_string('comment', 'quiz'), 3);
1206              $resultinfo .= html_writer::div($viewobj->gradebookfeedback, 'quizteacherfeedback') . "\n";
1207          }
1208          if ($viewobj->feedbackcolumn) {
1209              $resultinfo .= $this->heading(get_string('overallfeedback', 'quiz'), 3);
1210              $resultinfo .= html_writer::div(
1211                      quiz_feedback_for_grade($viewobj->mygrade, $quiz, $context),
1212                      'quizgradefeedback') . "\n";
1213          }
1214  
1215          if ($resultinfo) {
1216              $output .= $this->box($resultinfo, 'generalbox', 'feedback');
1217          }
1218          return $output;
1219      }
1220  
1221      /**
1222       * Output either a link to the review page for an attempt, or a button to
1223       * open the review in a popup window.
1224       *
1225       * @param moodle_url $url of the target page.
1226       * @param bool $reviewinpopup whether a pop-up is required.
1227       * @param array $popupoptions options to pass to the popup_action constructor.
1228       * @return string HTML to output.
1229       */
1230      public function review_link($url, $reviewinpopup, $popupoptions) {
1231          if ($reviewinpopup) {
1232              $button = new single_button($url, get_string('review', 'quiz'));
1233              $button->add_action(new popup_action('click', $url, 'quizpopup', $popupoptions));
1234              return $this->render($button);
1235  
1236          } else {
1237              return html_writer::link($url, get_string('review', 'quiz'),
1238                      array('title' => get_string('reviewthisattempt', 'quiz')));
1239          }
1240      }
1241  
1242      /**
1243       * Displayed where there might normally be a review link, to explain why the
1244       * review is not available at this time.
1245       * @param string $message optional message explaining why the review is not possible.
1246       * @return string HTML to output.
1247       */
1248      public function no_review_message($message) {
1249          return html_writer::nonempty_tag('span', $message,
1250                  array('class' => 'noreviewmessage'));
1251      }
1252  
1253      /**
1254       * Returns the same as {@link quiz_num_attempt_summary()} but wrapped in a link
1255       * to the quiz reports.
1256       *
1257       * @param stdClass $quiz the quiz object. Only $quiz->id is used at the moment.
1258       * @param stdClass $cm the cm object. Only $cm->course, $cm->groupmode and $cm->groupingid
1259       * fields are used at the moment.
1260       * @param context $context the quiz context.
1261       * @param bool $returnzero if false (default), when no attempts have been made '' is returned
1262       *      instead of 'Attempts: 0'.
1263       * @param int $currentgroup if there is a concept of current group where this method is being
1264       *      called (e.g. a report) pass it in here. Default 0 which means no current group.
1265       * @return string HTML fragment for the link.
1266       */
1267      public function quiz_attempt_summary_link_to_reports($quiz, $cm, $context,
1268                                                            $returnzero = false, $currentgroup = 0) {
1269          global $CFG;
1270          $summary = quiz_num_attempt_summary($quiz, $cm, $returnzero, $currentgroup);
1271          if (!$summary) {
1272              return '';
1273          }
1274  
1275          require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
1276          $url = new moodle_url('/mod/quiz/report.php', array(
1277                  'id' => $cm->id, 'mode' => quiz_report_default_report($context)));
1278          return html_writer::link($url, $summary);
1279      }
1280  
1281      /**
1282       * Render a summary of the number of group and user overrides, with corresponding links.
1283       *
1284       * @param stdClass $quiz the quiz settings.
1285       * @param stdClass|cm_info $cm the cm object.
1286       * @param int $currentgroup currently selected group, if there is one.
1287       * @return string HTML fragment for the link.
1288       */
1289      public function quiz_override_summary_links(stdClass $quiz, stdClass $cm, $currentgroup = 0): string {
1290  
1291          $baseurl = new moodle_url('/mod/quiz/overrides.php', ['cmid' => $cm->id]);
1292          $counts = quiz_override_summary($quiz, $cm, $currentgroup);
1293  
1294          $links = [];
1295          if ($counts['group']) {
1296              $links[] = html_writer::link(new moodle_url($baseurl, ['mode' => 'group']),
1297                      get_string('overridessummarygroup', 'quiz', $counts['group']));
1298          }
1299          if ($counts['user']) {
1300              $links[] = html_writer::link(new moodle_url($baseurl, ['mode' => 'user']),
1301                      get_string('overridessummaryuser', 'quiz', $counts['user']));
1302          }
1303  
1304          if (!$links) {
1305              return '';
1306          }
1307  
1308          $links = implode(', ', $links);
1309          switch ($counts['mode']) {
1310              case 'onegroup':
1311                  return get_string('overridessummarythisgroup', 'quiz', $links);
1312  
1313              case 'somegroups':
1314                  return get_string('overridessummaryyourgroups', 'quiz', $links);
1315  
1316              case 'allgroups':
1317                  return get_string('overridessummary', 'quiz', $links);
1318  
1319              default:
1320                  throw new coding_exception('Unexpected mode ' . $counts['mode']);
1321          }
1322      }
1323  
1324      /**
1325       * Outputs a chart.
1326       *
1327       * @param \core\chart_base $chart The chart.
1328       * @param string $title The title to display above the graph.
1329       * @param array $attrs extra container html attributes.
1330       * @return string HTML fragment for the graph.
1331       */
1332      public function chart(\core\chart_base $chart, $title, $attrs = []) {
1333          return $this->heading($title, 3) . html_writer::tag('div',
1334              $this->render($chart), array_merge(['class' => 'graph'], $attrs));
1335      }
1336  
1337      /**
1338       * Output a graph, or a message saying that GD is required.
1339       * @param moodle_url $url the URL of the graph.
1340       * @param string $title the title to display above the graph.
1341       * @return string HTML fragment for the graph.
1342       */
1343      public function graph(moodle_url $url, $title) {
1344          global $CFG;
1345  
1346          $graph = html_writer::empty_tag('img', array('src' => $url, 'alt' => $title));
1347  
1348          return $this->heading($title, 3) . html_writer::tag('div', $graph, array('class' => 'graph'));
1349      }
1350  
1351      /**
1352       * Output the connection warning messages, which are initially hidden, and
1353       * only revealed by JavaScript if necessary.
1354       */
1355      public function connection_warning() {
1356          $options = array('filter' => false, 'newlines' => false);
1357          $warning = format_text(get_string('connectionerror', 'quiz'), FORMAT_MARKDOWN, $options);
1358          $ok = format_text(get_string('connectionok', 'quiz'), FORMAT_MARKDOWN, $options);
1359          return html_writer::tag('div', $warning,
1360                      array('id' => 'connection-error', 'style' => 'display: none;', 'role' => 'alert')) .
1361                      html_writer::tag('div', $ok, array('id' => 'connection-ok', 'style' => 'display: none;', 'role' => 'alert'));
1362      }
1363  }
1364  
1365  
1366  class mod_quiz_links_to_other_attempts implements renderable {
1367      /**
1368       * @var array string attempt number => url, or null for the current attempt.
1369       * url may be either a moodle_url, or a renderable.
1370       */
1371      public $links = array();
1372  }
1373  
1374  
1375  class mod_quiz_view_object {
1376      /** @var array $infomessages of messages with information to display about the quiz. */
1377      public $infomessages;
1378      /** @var array $attempts contains all the user's attempts at this quiz. */
1379      public $attempts;
1380      /** @var array $attemptobjs quiz_attempt objects corresponding to $attempts. */
1381      public $attemptobjs;
1382      /** @var quiz_access_manager $accessmanager contains various access rules. */
1383      public $accessmanager;
1384      /** @var bool $canreviewmine whether the current user has the capability to
1385       *       review their own attempts. */
1386      public $canreviewmine;
1387      /** @var bool $canedit whether the current user has the capability to edit the quiz. */
1388      public $canedit;
1389      /** @var moodle_url $editurl the URL for editing this quiz. */
1390      public $editurl;
1391      /** @var int $attemptcolumn contains the number of attempts done. */
1392      public $attemptcolumn;
1393      /** @var int $gradecolumn contains the grades of any attempts. */
1394      public $gradecolumn;
1395      /** @var int $markcolumn contains the marks of any attempt. */
1396      public $markcolumn;
1397      /** @var int $overallstats contains all marks for any attempt. */
1398      public $overallstats;
1399      /** @var string $feedbackcolumn contains any feedback for and attempt. */
1400      public $feedbackcolumn;
1401      /** @var string $timenow contains a timestamp in string format. */
1402      public $timenow;
1403      /** @var int $numattempts contains the total number of attempts. */
1404      public $numattempts;
1405      /** @var float $mygrade contains the user's final grade for a quiz. */
1406      public $mygrade;
1407      /** @var bool $moreattempts whether this user is allowed more attempts. */
1408      public $moreattempts;
1409      /** @var int $mygradeoverridden contains an overriden grade. */
1410      public $mygradeoverridden;
1411      /** @var string $gradebookfeedback contains any feedback for a gradebook. */
1412      public $gradebookfeedback;
1413      /** @var bool $unfinished contains 1 if an attempt is unfinished. */
1414      public $unfinished;
1415      /** @var object $lastfinishedattempt the last attempt from the attempts array. */
1416      public $lastfinishedattempt;
1417      /** @var array $preventmessages of messages telling the user why they can't
1418       *       attempt the quiz now. */
1419      public $preventmessages;
1420      /** @var string $buttontext caption for the start attempt button. If this is null, show no
1421       *      button, or if it is '' show a back to the course button. */
1422      public $buttontext;
1423      /** @var moodle_url $startattempturl URL to start an attempt. */
1424      public $startattempturl;
1425      /** @var moodleform|null $preflightcheckform confirmation form that must be
1426       *       submitted before an attempt is started, if required. */
1427      public $preflightcheckform;
1428      /** @var moodle_url $startattempturl URL for any Back to the course button. */
1429      public $backtocourseurl;
1430      /** @var bool $showbacktocourse should we show a back to the course button? */
1431      public $showbacktocourse;
1432      /** @var bool whether the attempt must take place in a popup window. */
1433      public $popuprequired;
1434      /** @var array options to use for the popup window, if required. */
1435      public $popupoptions;
1436      /** @var bool $quizhasquestions whether the quiz has any questions. */
1437      public $quizhasquestions;
1438  
1439      public function __get($field) {
1440          switch ($field) {
1441              case 'startattemptwarning':
1442                  debugging('startattemptwarning has been deprecated. It is now always blank.');
1443                  return '';
1444  
1445              default:
1446                  debugging('Unknown property ' . $field);
1447                  return null;
1448          }
1449      }
1450  }