Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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   * Renderer outputting the quiz editing UI.
  19   *
  20   * @package mod_quiz
  21   * @copyright 2013 The Open University.
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_quiz\output;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use core_question\local\bank\question_version_status;
  29  use mod_quiz\question\bank\qbank_helper;
  30  use \mod_quiz\structure;
  31  use \html_writer;
  32  use qbank_previewquestion\question_preview_options;
  33  use renderable;
  34  
  35  /**
  36   * Renderer outputting the quiz editing UI.
  37   *
  38   * @copyright 2013 The Open University.
  39   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   * @since Moodle 2.7
  41   */
  42  class edit_renderer extends \plugin_renderer_base {
  43  
  44      /** @var string The toggle group name of the checkboxes for the toggle-all functionality. */
  45      protected $togglegroup = 'quiz-questions';
  46  
  47      /**
  48       * Render the edit page
  49       *
  50       * @param \quiz $quizobj object containing all the quiz settings information.
  51       * @param structure $structure object containing the structure of the quiz.
  52       * @param \core_question\local\bank\question_edit_contexts $contexts the relevant question bank contexts.
  53       * @param \moodle_url $pageurl the canonical URL of this page.
  54       * @param array $pagevars the variables from {@link question_edit_setup()}.
  55       * @return string HTML to output.
  56       */
  57      public function edit_page(\quiz $quizobj, structure $structure,
  58          \core_question\local\bank\question_edit_contexts $contexts, \moodle_url $pageurl, array $pagevars) {
  59          $output = '';
  60  
  61          // Page title.
  62          $output .= $this->heading(get_string('questions', 'quiz'));
  63  
  64          // Information at the top.
  65          $output .= $this->quiz_state_warnings($structure);
  66  
  67          $output .= html_writer::start_div('mod_quiz-edit-top-controls');
  68  
  69          $output .= html_writer::start_div('d-flex justify-content-between flex-wrap mb-1');
  70          $output .= html_writer::start_div('d-flex flex-column justify-content-around');
  71          $output .= $this->quiz_information($structure);
  72          $output .= html_writer::end_tag('div');
  73          $output .= $this->maximum_grade_input($structure, $pageurl);
  74          $output .= html_writer::end_tag('div');
  75  
  76          $output .= html_writer::start_div('d-flex justify-content-between flex-wrap mb-1');
  77          $output .= html_writer::start_div('mod_quiz-edit-action-buttons btn-group edit-toolbar', ['role' => 'group']);
  78          $output .= $this->repaginate_button($structure, $pageurl);
  79          $output .= $this->selectmultiple_button($structure);
  80          $output .= html_writer::end_tag('div');
  81  
  82          $output .= html_writer::start_div('d-flex flex-column justify-content-around');
  83          $output .= $this->total_marks($quizobj->get_quiz());
  84          $output .= html_writer::end_tag('div');
  85          $output .= html_writer::end_tag('div');
  86  
  87          $output .= $this->selectmultiple_controls($structure);
  88          $output .= html_writer::end_tag('div');
  89  
  90          // Show the questions organised into sections and pages.
  91          $output .= $this->start_section_list($structure);
  92  
  93          foreach ($structure->get_sections() as $section) {
  94              $output .= $this->start_section($structure, $section);
  95              $output .= $this->questions_in_section($structure, $section, $contexts, $pagevars, $pageurl);
  96  
  97              if ($structure->is_last_section($section)) {
  98                  $output .= \html_writer::start_div('last-add-menu');
  99                  $output .= html_writer::tag('span', $this->add_menu_actions($structure, 0,
 100                          $pageurl, $contexts, $pagevars), array('class' => 'add-menu-outer'));
 101                  $output .= \html_writer::end_div();
 102              }
 103  
 104              $output .= $this->end_section();
 105          }
 106  
 107          $output .= $this->end_section_list();
 108  
 109          // Initialise the JavaScript.
 110          $this->initialise_editing_javascript($structure, $contexts, $pagevars, $pageurl);
 111  
 112          // Include the contents of any other popups required.
 113          if ($structure->can_be_edited()) {
 114              $thiscontext = $contexts->lowest();
 115              $this->page->requires->js_call_amd('mod_quiz/quizquestionbank', 'init', [
 116                  $thiscontext->id
 117              ]);
 118  
 119              $this->page->requires->js_call_amd('mod_quiz/add_random_question', 'init', [
 120                  $thiscontext->id,
 121                  $pagevars['cat'],
 122                  $pageurl->out_as_local_url(true),
 123                  $pageurl->param('cmid'),
 124                  \core\plugininfo\qbank::is_plugin_enabled(\qbank_managecategories\helper::PLUGINNAME),
 125              ]);
 126  
 127              // Include the question chooser.
 128              $output .= $this->question_chooser();
 129          }
 130  
 131          return $output;
 132      }
 133  
 134      /**
 135       * Render any warnings that might be required about the state of the quiz,
 136       * e.g. if it has been attempted, or if the shuffle questions option is
 137       * turned on.
 138       *
 139       * @param structure $structure the quiz structure.
 140       * @return string HTML to output.
 141       */
 142      public function quiz_state_warnings(structure $structure) {
 143          $warnings = $structure->get_edit_page_warnings();
 144  
 145          if (empty($warnings)) {
 146              return '';
 147          }
 148  
 149          $output = array();
 150          foreach ($warnings as $warning) {
 151              $output[] = \html_writer::tag('p', $warning);
 152          }
 153          return $this->box(implode("\n", $output), 'statusdisplay');
 154      }
 155  
 156      /**
 157       * Render the status bar.
 158       *
 159       * @param structure $structure the quiz structure.
 160       * @return string HTML to output.
 161       */
 162      public function quiz_information(structure $structure) {
 163          list($currentstatus, $explanation) = $structure->get_dates_summary();
 164  
 165          $output = html_writer::span(
 166                      get_string('numquestionsx', 'quiz', $structure->get_question_count()),
 167                      'numberofquestions') . ' | ' .
 168                  html_writer::span($currentstatus, 'quizopeningstatus',
 169                      array('title' => $explanation));
 170  
 171          return html_writer::div($output, 'statusbar');
 172      }
 173  
 174      /**
 175       * Render the form for setting a quiz' overall grade
 176       *
 177       * @param structure $structure the quiz structure.
 178       * @param \moodle_url $pageurl the canonical URL of this page.
 179       * @return string HTML to output.
 180       */
 181      public function maximum_grade_input($structure, \moodle_url $pageurl) {
 182          $output = '';
 183          $output .= html_writer::start_div('maxgrade');
 184          $output .= html_writer::start_tag('form', array('method' => 'post', 'action' => 'edit.php',
 185                  'class' => 'quizsavegradesform form-inline'));
 186          $output .= html_writer::start_tag('fieldset', array('class' => 'invisiblefieldset'));
 187          $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
 188          $output .= html_writer::input_hidden_params($pageurl);
 189          $output .= html_writer::tag('label', get_string('maximumgrade') . ' ',
 190                  array('for' => 'inputmaxgrade'));
 191          $output .= html_writer::empty_tag('input', array('type' => 'text', 'id' => 'inputmaxgrade',
 192                  'name' => 'maxgrade', 'size' => ($structure->get_decimal_places_for_grades() + 2),
 193                  'value' => $structure->formatted_quiz_grade(),
 194                  'class' => 'form-control'));
 195          $output .= html_writer::empty_tag('input', array('type' => 'submit', 'class' => 'btn btn-secondary ml-1',
 196                  'name' => 'savechanges', 'value' => get_string('save', 'quiz')));
 197          $output .= html_writer::end_tag('fieldset');
 198          $output .= html_writer::end_tag('form');
 199          $output .= html_writer::end_tag('div');
 200          return $output;
 201      }
 202  
 203      /**
 204       * Return the repaginate button
 205       * @param structure $structure the structure of the quiz being edited.
 206       * @param \moodle_url $pageurl the canonical URL of this page.
 207       * @return string HTML to output.
 208       */
 209      protected function repaginate_button(structure $structure, \moodle_url $pageurl) {
 210          $header = html_writer::tag('span', get_string('repaginatecommand', 'quiz'), array('class' => 'repaginatecommand'));
 211          $form = $this->repaginate_form($structure, $pageurl);
 212  
 213          $buttonoptions = array(
 214              'type'  => 'submit',
 215              'name'  => 'repaginate',
 216              'id'    => 'repaginatecommand',
 217              'value' => get_string('repaginatecommand', 'quiz'),
 218              'class' => 'btn btn-secondary mr-1',
 219              'data-header' => $header,
 220              'data-form'   => $form,
 221          );
 222          if (!$structure->can_be_repaginated()) {
 223              $buttonoptions['disabled'] = 'disabled';
 224          } else {
 225              $this->page->requires->js_call_amd('mod_quiz/repaginate', 'init');
 226          }
 227  
 228          return html_writer::empty_tag('input', $buttonoptions);
 229      }
 230  
 231      /**
 232       * Generate the bulk action button.
 233       *
 234       * @param structure $structure the structure of the quiz being edited.
 235       * @return string HTML to output.
 236       */
 237      protected function selectmultiple_button(structure $structure) {
 238          $buttonoptions = array(
 239              'type'  => 'button',
 240              'name'  => 'selectmultiple',
 241              'id'    => 'selectmultiplecommand',
 242              'value' => get_string('selectmultipleitems', 'quiz'),
 243              'class' => 'btn btn-secondary'
 244          );
 245          if (!$structure->can_be_edited()) {
 246              $buttonoptions['disabled'] = 'disabled';
 247          }
 248  
 249          return html_writer::tag('button', get_string('selectmultipleitems', 'quiz'), $buttonoptions);
 250      }
 251  
 252      /**
 253       * Generate the controls that appear when the bulk action button is pressed.
 254       *
 255       * @param structure $structure the structure of the quiz being edited.
 256       * @return string HTML to output.
 257       */
 258      protected function selectmultiple_controls(structure $structure) {
 259          $output = '';
 260  
 261          // Bulk action button delete and bulk action button cancel.
 262          $buttondeleteoptions = array(
 263              'type' => 'button',
 264              'id' => 'selectmultipledeletecommand',
 265              'value' => get_string('deleteselected', 'mod_quiz'),
 266              'class' => 'btn btn-secondary',
 267              'data-action' => 'toggle',
 268              'data-togglegroup' => $this->togglegroup,
 269              'data-toggle' => 'action',
 270              'disabled' => true
 271          );
 272          $buttoncanceloptions = array(
 273              'type' => 'button',
 274              'id' => 'selectmultiplecancelcommand',
 275              'value' => get_string('cancel', 'moodle'),
 276              'class' => 'btn btn-secondary'
 277          );
 278  
 279          $groupoptions = array(
 280              'class' => 'btn-group selectmultiplecommand actions m-1',
 281              'role' => 'group'
 282          );
 283  
 284          $output .= html_writer::tag('div',
 285                          html_writer::tag('button', get_string('deleteselected', 'mod_quiz'), $buttondeleteoptions) .
 286                          " " .
 287                          html_writer::tag('button', get_string('cancel', 'moodle'),
 288                  $buttoncanceloptions), $groupoptions);
 289  
 290          $toolbaroptions = array(
 291              'class' => 'btn-toolbar m-1',
 292              'role' => 'toolbar',
 293              'aria-label' => get_string('selectmultipletoolbar', 'quiz'),
 294          );
 295  
 296          // Select all/deselect all questions.
 297          $selectallid = 'questionselectall';
 298          $selectalltext = get_string('selectall', 'moodle');
 299          $deselectalltext = get_string('deselectall', 'moodle');
 300          $mastercheckbox = new \core\output\checkbox_toggleall($this->togglegroup, true, [
 301              'id' => $selectallid,
 302              'name' => $selectallid,
 303              'value' => 1,
 304              'label' => $selectalltext,
 305              'selectall' => $selectalltext,
 306              'deselectall' => $deselectalltext,
 307          ], true);
 308  
 309          $selectdeselect = html_writer::div($this->render($mastercheckbox), 'selectmultiplecommandbuttons');
 310          $output .= html_writer::tag('div', $selectdeselect, $toolbaroptions);
 311          return $output;
 312      }
 313  
 314      /**
 315       * Return the repaginate form
 316       * @param structure $structure the structure of the quiz being edited.
 317       * @param \moodle_url $pageurl the canonical URL of this page.
 318       * @return string HTML to output.
 319       */
 320      protected function repaginate_form(structure $structure, \moodle_url $pageurl) {
 321          $perpage = array();
 322          $perpage[0] = get_string('allinone', 'quiz');
 323          for ($i = 1; $i <= 50; ++$i) {
 324              $perpage[$i] = $i;
 325          }
 326  
 327          $hiddenurl = clone($pageurl);
 328          $hiddenurl->param('sesskey', sesskey());
 329  
 330          $select = html_writer::select($perpage, 'questionsperpage',
 331                  $structure->get_questions_per_page(), false, array('class' => 'custom-select'));
 332  
 333          $buttonattributes = array(
 334              'type' => 'submit',
 335              'name' => 'repaginate',
 336              'value' => get_string('go'),
 337              'class' => 'btn btn-secondary ml-1'
 338          );
 339  
 340          $formcontent = html_writer::tag('form', html_writer::div(
 341                      html_writer::input_hidden_params($hiddenurl) .
 342                      get_string('repaginate', 'quiz', $select) .
 343                      html_writer::empty_tag('input', $buttonattributes)
 344                  ), array('action' => 'edit.php', 'method' => 'post'));
 345  
 346          return html_writer::div($formcontent, '', array('id' => 'repaginatedialog'));
 347      }
 348  
 349      /**
 350       * Render the total marks available for the quiz.
 351       *
 352       * @param \stdClass $quiz the quiz settings from the database.
 353       * @return string HTML to output.
 354       */
 355      public function total_marks($quiz) {
 356          $totalmark = html_writer::span(quiz_format_grade($quiz, $quiz->sumgrades), 'mod_quiz_summarks');
 357          return html_writer::tag('span',
 358                  get_string('totalmarksx', 'quiz', $totalmark),
 359                  array('class' => 'totalpoints'));
 360      }
 361  
 362      /**
 363       * Generate the starting container html for the start of a list of sections
 364       * @param structure $structure the structure of the quiz being edited.
 365       * @return string HTML to output.
 366       */
 367      protected function start_section_list(structure $structure) {
 368          $class = 'slots';
 369          if ($structure->get_section_count() == 1) {
 370              $class .= ' only-one-section';
 371          }
 372          return html_writer::start_tag('ul', array('class' => $class, 'role' => 'presentation'));
 373      }
 374  
 375      /**
 376       * Generate the closing container html for the end of a list of sections
 377       * @return string HTML to output.
 378       */
 379      protected function end_section_list() {
 380          return html_writer::end_tag('ul');
 381      }
 382  
 383      /**
 384       * Display the start of a section, before the questions.
 385       *
 386       * @param structure $structure the structure of the quiz being edited.
 387       * @param \stdClass $section The quiz_section entry from DB
 388       * @return string HTML to output.
 389       */
 390      protected function start_section($structure, $section) {
 391  
 392          $output = '';
 393  
 394          $sectionstyle = '';
 395          if ($structure->is_only_one_slot_in_section($section)) {
 396              $sectionstyle = ' only-has-one-slot';
 397          }
 398  
 399          if ($section->heading) {
 400              $sectionheadingtext = format_string($section->heading);
 401              $sectionheading = html_writer::span($sectionheadingtext, 'instancesection');
 402          } else {
 403              // Use a sr-only default section heading, so we don't end up with an empty section heading.
 404              $sectionheadingtext = get_string('sectionnoname', 'quiz');
 405              $sectionheading = html_writer::span($sectionheadingtext, 'instancesection sr-only');
 406          }
 407  
 408          $output .= html_writer::start_tag('li', array('id' => 'section-'.$section->id,
 409              'class' => 'section main clearfix'.$sectionstyle, 'role' => 'presentation',
 410              'data-sectionname' => $sectionheadingtext));
 411  
 412          $output .= html_writer::start_div('content');
 413  
 414          $output .= html_writer::start_div('section-heading');
 415  
 416          $headingtext = $this->heading(html_writer::span($sectionheading, 'sectioninstance'), 3);
 417  
 418          if (!$structure->can_be_edited()) {
 419              $editsectionheadingicon = '';
 420          } else {
 421              $editsectionheadingicon = html_writer::link(new \moodle_url('#'),
 422                  $this->pix_icon('t/editstring', get_string('sectionheadingedit', 'quiz', $sectionheadingtext),
 423                          'moodle', array('class' => 'editicon visibleifjs')),
 424                          array('class' => 'editing_section', 'data-action' => 'edit_section_title', 'role' => 'button'));
 425          }
 426          $output .= html_writer::div($headingtext . $editsectionheadingicon, 'instancesectioncontainer');
 427  
 428          if (!$structure->is_first_section($section) && $structure->can_be_edited()) {
 429              $output .= $this->section_remove_icon($section);
 430          }
 431          $output .= $this->section_shuffle_questions($structure, $section);
 432  
 433          $output .= html_writer::end_div($output, 'section-heading');
 434  
 435          return $output;
 436      }
 437  
 438      /**
 439       * Display a checkbox for shuffling question within a section.
 440       *
 441       * @param structure $structure object containing the structure of the quiz.
 442       * @param \stdClass $section data from the quiz_section table.
 443       * @return string HTML to output.
 444       */
 445      public function section_shuffle_questions(structure $structure, $section) {
 446          $checkboxattributes = array(
 447              'type' => 'checkbox',
 448              'id' => 'shuffle-' . $section->id,
 449              'value' => 1,
 450              'data-action' => 'shuffle_questions',
 451              'class' => 'cm-edit-action',
 452          );
 453  
 454          if (!$structure->can_be_edited()) {
 455              $checkboxattributes['disabled'] = 'disabled';
 456          }
 457          if ($section->shufflequestions) {
 458              $checkboxattributes['checked'] = 'checked';
 459          }
 460  
 461          if ($structure->is_first_section($section)) {
 462              $help = $this->help_icon('shufflequestions', 'quiz');
 463          } else {
 464              $help = '';
 465          }
 466  
 467          $helpspan = html_writer::span($help, 'shuffle-help-tip');
 468          $progressspan = html_writer::span('', 'shuffle-progress');
 469          $checkbox = html_writer::empty_tag('input', $checkboxattributes);
 470          $label = html_writer::label(get_string('shufflequestions', 'quiz'),
 471                  $checkboxattributes['id'], false);
 472          return html_writer::span($progressspan . $checkbox . $label. ' ' . $helpspan,
 473                  'instanceshufflequestions', array('data-action' => 'shuffle_questions'));
 474      }
 475  
 476      /**
 477       * Display the end of a section, after the questions.
 478       *
 479       * @return string HTML to output.
 480       */
 481      protected function end_section() {
 482          $output = html_writer::end_tag('div');
 483          $output .= html_writer::end_tag('li');
 484  
 485          return $output;
 486      }
 487  
 488      /**
 489       * Render an icon to remove a section from the quiz.
 490       *
 491       * @param object $section the section to be removed.
 492       * @return string HTML to output.
 493       */
 494      public function section_remove_icon($section) {
 495          $title = get_string('sectionheadingremove', 'quiz', format_string($section->heading));
 496          $url = new \moodle_url('/mod/quiz/edit.php',
 497                  array('sesskey' => sesskey(), 'removesection' => '1', 'sectionid' => $section->id));
 498          $image = $this->pix_icon('t/delete', $title);
 499          return $this->action_link($url, $image, null, array(
 500                  'class' => 'cm-edit-action editing_delete', 'data-action' => 'deletesection'));
 501      }
 502  
 503      /**
 504       * Renders HTML to display the questions in a section of the quiz.
 505       *
 506       * This function calls {@link core_course_renderer::quiz_section_question()}
 507       *
 508       * @param structure $structure object containing the structure of the quiz.
 509       * @param \stdClass $section information about the section.
 510       * @param \core_question\local\bank\question_edit_contexts $contexts the relevant question bank contexts.
 511       * @param array $pagevars the variables from {@link \question_edit_setup()}.
 512       * @param \moodle_url $pageurl the canonical URL of this page.
 513       * @return string HTML to output.
 514       */
 515      public function questions_in_section(structure $structure, $section,
 516              $contexts, $pagevars, $pageurl) {
 517  
 518          $output = '';
 519          foreach ($structure->get_slots_in_section($section->id) as $slot) {
 520              $output .= $this->question_row($structure, $slot, $contexts, $pagevars, $pageurl);
 521          }
 522          return html_writer::tag('ul', $output, array('class' => 'section img-text'));
 523      }
 524  
 525      /**
 526       * Displays one question with the surrounding controls.
 527       *
 528       * @param structure $structure object containing the structure of the quiz.
 529       * @param int $slot which slot we are outputting.
 530       * @param \core_question\local\bank\question_edit_contexts $contexts the relevant question bank contexts.
 531       * @param array $pagevars the variables from {@link \question_edit_setup()}.
 532       * @param \moodle_url $pageurl the canonical URL of this page.
 533       * @return string HTML to output.
 534       */
 535      public function question_row(structure $structure, $slot, $contexts, $pagevars, $pageurl) {
 536          $output = '';
 537  
 538          $output .= $this->page_row($structure, $slot, $contexts, $pagevars, $pageurl);
 539  
 540          // Page split/join icon.
 541          $joinhtml = '';
 542          if ($structure->can_be_edited() && !$structure->is_last_slot_in_quiz($slot) &&
 543                                              !$structure->is_last_slot_in_section($slot)) {
 544              $joinhtml = $this->page_split_join_button($structure, $slot);
 545          }
 546          // Question HTML.
 547          $questionhtml = $this->question($structure, $slot, $pageurl);
 548          $qtype = $structure->get_question_type_for_slot($slot);
 549          $questionclasses = 'activity ' . $qtype . ' qtype_' . $qtype . ' slot';
 550  
 551          $output .= html_writer::tag('li', $questionhtml . $joinhtml,
 552                  array('class' => $questionclasses, 'id' => 'slot-' . $structure->get_slot_id_for_slot($slot),
 553                          'data-canfinish' => $structure->can_finish_during_the_attempt($slot)));
 554  
 555          return $output;
 556      }
 557  
 558      /**
 559       * Displays one question with the surrounding controls.
 560       *
 561       * @param structure $structure object containing the structure of the quiz.
 562       * @param int $slot the first slot on the page we are outputting.
 563       * @param \core_question\local\bank\question_edit_contexts $contexts the relevant question bank contexts.
 564       * @param array $pagevars the variables from {@link \question_edit_setup()}.
 565       * @param \moodle_url $pageurl the canonical URL of this page.
 566       * @return string HTML to output.
 567       */
 568      public function page_row(structure $structure, $slot, $contexts, $pagevars, $pageurl) {
 569          $output = '';
 570  
 571          $pagenumber = $structure->get_page_number_for_slot($slot);
 572  
 573          // Put page in a heading for accessibility and styling.
 574          $page = $this->heading(get_string('page') . ' ' . $pagenumber, 4);
 575  
 576          if ($structure->is_first_slot_on_page($slot)) {
 577              // Add the add-menu at the page level.
 578              $addmenu = html_writer::tag('span', $this->add_menu_actions($structure,
 579                      $pagenumber, $pageurl, $contexts, $pagevars),
 580                      array('class' => 'add-menu-outer'));
 581  
 582              $addquestionform = $this->add_question_form($structure,
 583                      $pagenumber, $pageurl, $pagevars);
 584  
 585              $output .= html_writer::tag('li', $page . $addmenu . $addquestionform,
 586                      array('class' => 'pagenumber activity yui3-dd-drop page', 'id' => 'page-' . $pagenumber));
 587          }
 588  
 589          return $output;
 590      }
 591  
 592      /**
 593       * Returns the add menu that is output once per page.
 594       * @param structure $structure object containing the structure of the quiz.
 595       * @param int $page the page number that this menu will add to.
 596       * @param \moodle_url $pageurl the canonical URL of this page.
 597       * @param \core_question\local\bank\question_edit_contexts $contexts the relevant question bank contexts.
 598       * @param array $pagevars the variables from {@link \question_edit_setup()}.
 599       * @return string HTML to output.
 600       */
 601      public function add_menu_actions(structure $structure, $page, \moodle_url $pageurl,
 602              \core_question\local\bank\question_edit_contexts $contexts, array $pagevars) {
 603  
 604          $actions = $this->edit_menu_actions($structure, $page, $pageurl, $pagevars);
 605          if (empty($actions)) {
 606              return '';
 607          }
 608          $menu = new \action_menu();
 609          $menu->set_constraint('.mod-quiz-edit-content');
 610          $trigger = html_writer::tag('span', get_string('add', 'quiz'), array('class' => 'add-menu'));
 611          $menu->set_menu_trigger($trigger);
 612          // The menu appears within an absolutely positioned element causing width problems.
 613          // Make sure no-wrap is set so that we don't get a squashed menu.
 614          $menu->set_nowrap_on_items(true);
 615  
 616          // Disable the link if quiz has attempts.
 617          if (!$structure->can_be_edited()) {
 618              return $this->render($menu);
 619          }
 620  
 621          foreach ($actions as $action) {
 622              if ($action instanceof \action_menu_link) {
 623                  $action->add_class('add-menu');
 624              }
 625              $menu->add($action);
 626          }
 627          $menu->attributes['class'] .= ' page-add-actions commands';
 628  
 629          // Prioritise the menu ahead of all other actions.
 630          $menu->prioritise = true;
 631  
 632          return $this->render($menu);
 633      }
 634  
 635      /**
 636       * Returns the list of actions to go in the add menu.
 637       * @param structure $structure object containing the structure of the quiz.
 638       * @param int $page the page number that this menu will add to.
 639       * @param \moodle_url $pageurl the canonical URL of this page.
 640       * @param array $pagevars the variables from {@link \question_edit_setup()}.
 641       * @return array the actions.
 642       */
 643      public function edit_menu_actions(structure $structure, $page,
 644              \moodle_url $pageurl, array $pagevars) {
 645          $questioncategoryid = question_get_category_id_from_pagevars($pagevars);
 646          static $str;
 647          if (!isset($str)) {
 648              $str = get_strings(array('addasection', 'addaquestion', 'addarandomquestion',
 649                      'addarandomselectedquestion', 'questionbank'), 'quiz');
 650          }
 651  
 652          // Get section, page, slotnumber and maxmark.
 653          $actions = array();
 654  
 655          // Add a new question to the quiz.
 656          $returnurl = new \moodle_url($pageurl, array('addonpage' => $page));
 657          $params = array('returnurl' => $returnurl->out_as_local_url(false),
 658                  'cmid' => $structure->get_cmid(), 'category' => $questioncategoryid,
 659                  'addonpage' => $page, 'appendqnumstring' => 'addquestion');
 660  
 661          $actions['addaquestion'] = new \action_menu_link_secondary(
 662              new \moodle_url('/question/bank/editquestion/addquestion.php', $params),
 663              new \pix_icon('t/add', $str->addaquestion, 'moodle', array('class' => 'iconsmall', 'title' => '')),
 664              $str->addaquestion, array('class' => 'cm-edit-action addquestion', 'data-action' => 'addquestion')
 665          );
 666  
 667          // Call question bank.
 668          $icon = new \pix_icon('t/add', $str->questionbank, 'moodle', array('class' => 'iconsmall', 'title' => ''));
 669          if ($page) {
 670              $title = get_string('addquestionfrombanktopage', 'quiz', $page);
 671          } else {
 672              $title = get_string('addquestionfrombankatend', 'quiz');
 673          }
 674          $attributes = array('class' => 'cm-edit-action questionbank',
 675                  'data-header' => $title, 'data-action' => 'questionbank', 'data-addonpage' => $page);
 676          $actions['questionbank'] = new \action_menu_link_secondary($pageurl, $icon, $str->questionbank, $attributes);
 677  
 678          // Add a random question.
 679          if ($structure->can_add_random_questions()) {
 680              $returnurl = new \moodle_url('/mod/quiz/edit.php', array('cmid' => $structure->get_cmid(), 'data-addonpage' => $page));
 681              $params = ['returnurl' => $returnurl, 'cmid' => $structure->get_cmid(), 'appendqnumstring' => 'addarandomquestion'];
 682              $url = new \moodle_url('/mod/quiz/addrandom.php', $params);
 683              $icon = new \pix_icon('t/add', $str->addarandomquestion, 'moodle', array('class' => 'iconsmall', 'title' => ''));
 684              $attributes = array('class' => 'cm-edit-action addarandomquestion', 'data-action' => 'addarandomquestion');
 685              if ($page) {
 686                  $title = get_string('addrandomquestiontopage', 'quiz', $page);
 687              } else {
 688                  $title = get_string('addrandomquestionatend', 'quiz');
 689              }
 690              $attributes = array_merge(array('data-header' => $title, 'data-addonpage' => $page), $attributes);
 691              $actions['addarandomquestion'] = new \action_menu_link_secondary($url, $icon, $str->addarandomquestion, $attributes);
 692          }
 693  
 694          // Add a new section to the add_menu if possible. This is always added to the HTML
 695          // then hidden with CSS when no needed, so that as things are re-ordered, etc. with
 696          // Ajax it can be relevaled again when necessary.
 697          $params = array('cmid' => $structure->get_cmid(), 'addsectionatpage' => $page);
 698  
 699          $actions['addasection'] = new \action_menu_link_secondary(
 700              new \moodle_url($pageurl, $params),
 701              new \pix_icon('t/add', $str->addasection, 'moodle', array('class' => 'iconsmall', 'title' => '')),
 702              $str->addasection, array('class' => 'cm-edit-action addasection', 'data-action' => 'addasection')
 703          );
 704  
 705          return $actions;
 706      }
 707  
 708      /**
 709       * Render the form that contains the data for adding a new question to the quiz.
 710       *
 711       * @param structure $structure object containing the structure of the quiz.
 712       * @param int $page the page number that this menu will add to.
 713       * @param \moodle_url $pageurl the canonical URL of this page.
 714       * @param array $pagevars the variables from {@link \question_edit_setup()}.
 715       * @return string HTML to output.
 716       */
 717      protected function add_question_form(structure $structure, $page, \moodle_url $pageurl, array $pagevars) {
 718  
 719          $questioncategoryid = question_get_category_id_from_pagevars($pagevars);
 720  
 721          $output = html_writer::tag('input', null,
 722                  array('type' => 'hidden', 'name' => 'returnurl',
 723                          'value' => $pageurl->out_as_local_url(false, array('addonpage' => $page))));
 724          $output .= html_writer::tag('input', null,
 725                  array('type' => 'hidden', 'name' => 'cmid', 'value' => $structure->get_cmid()));
 726          $output .= html_writer::tag('input', null,
 727                  array('type' => 'hidden', 'name' => 'appendqnumstring', 'value' => 'addquestion'));
 728          $output .= html_writer::tag('input', null,
 729                  array('type' => 'hidden', 'name' => 'category', 'value' => $questioncategoryid));
 730  
 731          return html_writer::tag('form', html_writer::div($output),
 732                  array('class' => 'addnewquestion', 'method' => 'post',
 733                          'action' => new \moodle_url('/question/bank/editquestion/addquestion.php')));
 734      }
 735  
 736      /**
 737       * Display a question.
 738       *
 739       * @param structure $structure object containing the structure of the quiz.
 740       * @param int $slot the first slot on the page we are outputting.
 741       * @param \moodle_url $pageurl the canonical URL of this page.
 742       * @return string HTML to output.
 743       */
 744      public function question(structure $structure, int $slot, \moodle_url $pageurl) {
 745          // Get the data required by the question_slot template.
 746          $slotid = $structure->get_slot_id_for_slot($slot);
 747  
 748          $output = '';
 749          $output .= html_writer::start_tag('div');
 750  
 751          if ($structure->can_be_edited()) {
 752              $output .= $this->question_move_icon($structure, $slot);
 753          }
 754  
 755          $data = [
 756              'slotid' => $slotid,
 757              'canbeedited' => $structure->can_be_edited(),
 758              'checkbox' => $this->get_checkbox_render($structure, $slot),
 759              'questionnumber' => $this->question_number($structure->get_displayed_number_for_slot($slot)),
 760              'questionname' => $this->get_question_name_for_slot($structure, $slot, $pageurl),
 761              'questionicons' => $this->get_action_icon($structure, $slot, $pageurl),
 762              'questiondependencyicon' => ($structure->can_be_edited() ? $this->question_dependency_icon($structure, $slot) : ''),
 763              'versionselection' => false,
 764              'draftversion' => $structure->get_question_in_slot($slot)->status == question_version_status::QUESTION_STATUS_DRAFT,
 765          ];
 766  
 767          $data['versionoptions'] = [];
 768          if ($structure->get_slot_by_number($slot)->qtype !== 'random') {
 769              $data['versionselection'] = true;
 770              $data['versionoption'] = $structure->get_version_choices_for_slot($slot);
 771              $this->page->requires->js_call_amd('mod_quiz/question_slot', 'init', [$slotid]);
 772          }
 773  
 774          // Render the question slot template.
 775          $output .= $this->render_from_template('mod_quiz/question_slot', $data);
 776  
 777          $output .= html_writer::end_tag('div');
 778  
 779          return $output;
 780      }
 781  
 782      /**
 783       * Get the checkbox render.
 784       *
 785       * @param structure $structure object containing the structure of the quiz.
 786       * @param int $slot the slot on the page we are outputting.
 787       * @return string HTML to output.
 788       */
 789      public function get_checkbox_render(structure $structure, int $slot) : string {
 790          $questionslot = $structure->get_displayed_number_for_slot($slot);
 791          $checkbox = new \core\output\checkbox_toggleall($this->togglegroup, false,
 792              [
 793                  'id' => 'selectquestion-' . $questionslot,
 794                  'name' => 'selectquestion[]',
 795                  'value' => $questionslot,
 796                  'classes' => 'select-multiple-checkbox',
 797                  'label' => get_string('selectquestionslot', 'quiz', $questionslot),
 798                  'labelclasses' => 'sr-only',
 799              ]);
 800  
 801          return $this->render($checkbox);
 802      }
 803  
 804      /**
 805       * Get the question name for the slot.
 806       *
 807       * @param structure $structure object containing the structure of the quiz.
 808       * @param int $slot the slot on the page we are outputting.
 809       * @param \moodle_url $pageurl the canonical URL of this page.
 810       * @return string HTML to output.
 811       */
 812      public function get_question_name_for_slot(structure $structure, int $slot, \moodle_url $pageurl) : string {
 813          // Display the link to the question (or do nothing if question has no url).
 814          if ($structure->get_question_type_for_slot($slot) === 'random') {
 815              $questionname = $this->random_question($structure, $slot, $pageurl);
 816          } else {
 817              $questionname = $this->question_name($structure, $slot, $pageurl);
 818          }
 819  
 820          return $questionname;
 821      }
 822  
 823      /**
 824       * Get the action icons render.
 825       *
 826       * @param structure $structure object containing the structure of the quiz.
 827       * @param int $slot the slot on the page we are outputting.
 828       * @param \moodle_url $pageurl the canonical URL of this page.
 829       * @return string HTML to output.
 830       */
 831      public function get_action_icon(structure $structure, int $slot, \moodle_url $pageurl) : string {
 832          // Action icons.
 833          $qtype = $structure->get_question_type_for_slot($slot);
 834          $slotinfo = $structure->get_slot_by_number($slot);
 835          $questionicons = '';
 836          if ($qtype !== 'random') {
 837              $questionicons .= $this->question_preview_icon($structure->get_quiz(),
 838                      $structure->get_question_in_slot($slot),
 839                      null, null, $slotinfo->requestedversion ?: question_preview_options::ALWAYS_LATEST);
 840          }
 841          if ($structure->can_be_edited() && $structure->has_use_capability($slot)) {
 842              $questionicons .= $this->question_remove_icon($structure, $slot, $pageurl);
 843          }
 844          $questionicons .= $this->marked_out_of_field($structure, $slot);
 845  
 846          return $questionicons;
 847      }
 848  
 849      /**
 850       * Render the move icon.
 851       *
 852       * @param structure $structure object containing the structure of the quiz.
 853       * @param int $slot the first slot on the page we are outputting.
 854       * @return string The markup for the move action.
 855       */
 856      public function question_move_icon(structure $structure, $slot) {
 857          return html_writer::link(new \moodle_url('#'),
 858              $this->pix_icon('i/dragdrop', get_string('move'), 'moodle', array('class' => 'iconsmall', 'title' => '')),
 859              array('class' => 'editing_move', 'data-action' => 'move')
 860          );
 861      }
 862  
 863      /**
 864       * Output the question number.
 865       * @param string $number The number, or 'i'.
 866       * @return string HTML to output.
 867       */
 868      public function question_number($number) {
 869          if (is_numeric($number)) {
 870              $number = html_writer::span(get_string('question'), 'accesshide') . ' ' . $number;
 871          }
 872          return html_writer::tag('span', $number, array('class' => 'slotnumber'));
 873      }
 874  
 875      /**
 876       * Render the preview icon.
 877       *
 878       * @param \stdClass $quiz the quiz settings from the database.
 879       * @param \stdClass $questiondata which question to preview.
 880       *      If ->questionid is set, that is used instead of ->id.
 881       * @param bool $label if true, show the preview question label after the icon
 882       * @param int $variant which question variant to preview (optional).
 883       * @param int $restartversion version to use when restarting the preview
 884       * @return string HTML to output.
 885       */
 886      public function question_preview_icon($quiz, $questiondata, $label = null, $variant = null, $restartversion = null) {
 887          $question = clone($questiondata);
 888          if (isset($question->questionid)) {
 889  
 890              $question->id = $question->questionid;
 891          }
 892  
 893          $url = quiz_question_preview_url($quiz, $question, $variant, $restartversion);
 894  
 895          // Do we want a label?
 896          $strpreviewlabel = '';
 897          if ($label) {
 898              $strpreviewlabel = ' ' . get_string('preview', 'quiz');
 899          }
 900  
 901          // Build the icon.
 902          $strpreviewquestion = get_string('previewquestion', 'quiz');
 903          $image = $this->pix_icon('t/preview', $strpreviewquestion);
 904  
 905          $action = new \popup_action('click', $url, 'questionpreview',
 906                  \qbank_previewquestion\helper::question_preview_popup_params());
 907  
 908          return $this->action_link($url, $image . $strpreviewlabel, $action,
 909                  array('title' => $strpreviewquestion, 'class' => 'preview'));
 910      }
 911  
 912      /**
 913       * Render an icon to remove a question from the quiz.
 914       *
 915       * @param structure $structure object containing the structure of the quiz.
 916       * @param int $slot the first slot on the page we are outputting.
 917       * @param \moodle_url $pageurl the canonical URL of the edit page.
 918       * @return string HTML to output.
 919       */
 920      public function question_remove_icon(structure $structure, $slot, $pageurl) {
 921          $url = new \moodle_url($pageurl, array('sesskey' => sesskey(), 'remove' => $slot));
 922          $strdelete = get_string('delete');
 923  
 924          $image = $this->pix_icon('t/delete', $strdelete);
 925  
 926          return $this->action_link($url, $image, null, array('title' => $strdelete,
 927                      'class' => 'cm-edit-action editing_delete', 'data-action' => 'delete'));
 928      }
 929  
 930      /**
 931       * Display an icon to split or join two pages of the quiz.
 932       *
 933       * @param structure $structure object containing the structure of the quiz.
 934       * @param int $slot the first slot on the page we are outputting.
 935       * @return string HTML to output.
 936       */
 937      public function page_split_join_button($structure, $slot) {
 938          $insertpagebreak = !$structure->is_last_slot_on_page($slot);
 939          $url = new \moodle_url('repaginate.php', array('quizid' => $structure->get_quizid(),
 940                  'slot' => $slot, 'repag' => $insertpagebreak ? 2 : 1, 'sesskey' => sesskey()));
 941  
 942          if ($insertpagebreak) {
 943              $title = get_string('addpagebreak', 'quiz');
 944              $image = $this->image_icon('e/insert_page_break', $title);
 945              $action = 'addpagebreak';
 946          } else {
 947              $title = get_string('removepagebreak', 'quiz');
 948              $image = $this->image_icon('e/remove_page_break', $title);
 949              $action = 'removepagebreak';
 950          }
 951  
 952          // Disable the link if quiz has attempts.
 953          $disabled = null;
 954          if (!$structure->can_be_edited()) {
 955              $disabled = 'disabled';
 956          }
 957          return html_writer::span($this->action_link($url, $image, null, array('title' => $title,
 958                      'class' => 'page_split_join cm-edit-action', 'disabled' => $disabled, 'data-action' => $action)),
 959                  'page_split_join_wrapper');
 960      }
 961  
 962      /**
 963       * Display the icon for whether this question can only be seen if the previous
 964       * one has been answered.
 965       *
 966       * @param structure $structure object containing the structure of the quiz.
 967       * @param int $slot the first slot on the page we are outputting.
 968       * @return string HTML to output.
 969       */
 970      public function question_dependency_icon($structure, $slot) {
 971          $a = array(
 972              'thisq' => $structure->get_displayed_number_for_slot($slot),
 973              'previousq' => $structure->get_displayed_number_for_slot(max($slot - 1, 1)),
 974          );
 975          if ($structure->is_question_dependent_on_previous_slot($slot)) {
 976              $title = get_string('questiondependencyremove', 'quiz', $a);
 977              $image = $this->pix_icon('t/locked', get_string('questiondependsonprevious', 'quiz'),
 978                      'moodle', array('title' => ''));
 979              $action = 'removedependency';
 980          } else {
 981              $title = get_string('questiondependencyadd', 'quiz', $a);
 982              $image = $this->pix_icon('t/unlocked', get_string('questiondependencyfree', 'quiz'),
 983                      'moodle', array('title' => ''));
 984              $action = 'adddependency';
 985          }
 986  
 987          // Disable the link if quiz has attempts.
 988          $disabled = null;
 989          if (!$structure->can_be_edited()) {
 990              $disabled = 'disabled';
 991          }
 992          $extraclass = '';
 993          if (!$structure->can_question_depend_on_previous_slot($slot)) {
 994              $extraclass = ' question_dependency_cannot_depend';
 995          }
 996          return html_writer::span($this->action_link('#', $image, null, array('title' => $title,
 997                  'class' => 'cm-edit-action', 'disabled' => $disabled, 'data-action' => $action)),
 998                  'question_dependency_wrapper' . $extraclass);
 999      }
1000  
1001      /**
1002       * Renders html to display a name with the link to the question on a quiz edit page
1003       *
1004       * If the user does not have permission to edi the question, it is rendered
1005       * without a link
1006       *
1007       * @param structure $structure object containing the structure of the quiz.
1008       * @param int $slot which slot we are outputting.
1009       * @param \moodle_url $pageurl the canonical URL of this page.
1010       * @return string HTML to output.
1011       */
1012      public function question_name(structure $structure, $slot, $pageurl) {
1013          $output = '';
1014  
1015          $question = $structure->get_question_in_slot($slot);
1016          $editurl = new \moodle_url('/question/bank/editquestion/question.php', array(
1017                  'returnurl' => $pageurl->out_as_local_url(),
1018                  'cmid' => $structure->get_cmid(), 'id' => $question->questionid));
1019  
1020          $instancename = quiz_question_tostring($question);
1021  
1022          $qtype = \question_bank::get_qtype($question->qtype, false);
1023          $namestr = $qtype->local_name();
1024  
1025          $icon = $this->pix_icon('icon', $namestr, $qtype->plugin_name(), array('title' => $namestr,
1026                  'class' => 'activityicon', 'alt' => ' ', 'role' => 'presentation'));
1027  
1028          $editicon = $this->pix_icon('t/edit', '', 'moodle', array('title' => ''));
1029  
1030          // Need plain question name without html tags for link title.
1031          $title = shorten_text(format_string($question->name), 100);
1032  
1033          // Display the link itself.
1034          $activitylink = $icon . html_writer::tag('span', $editicon . $instancename, array('class' => 'instancename'));
1035          $output .= html_writer::link($editurl, $activitylink,
1036                  array('title' => get_string('editquestion', 'quiz').' '.$title));
1037  
1038          return $output;
1039      }
1040  
1041      /**
1042       * Renders html to display a random question the link to edit the configuration
1043       * and also to see that category in the question bank.
1044       *
1045       * @param structure $structure object containing the structure of the quiz.
1046       * @param int $slotnumber which slot we are outputting.
1047       * @param \moodle_url $pageurl the canonical URL of this page.
1048       * @return string HTML to output.
1049       */
1050      public function random_question(structure $structure, $slotnumber, $pageurl) {
1051          $question = $structure->get_question_in_slot($slotnumber);
1052          $slot = $structure->get_slot_by_number($slotnumber);
1053          $editurl = new \moodle_url('/mod/quiz/editrandom.php',
1054                  array('returnurl' => $pageurl->out_as_local_url(), 'slotid' => $slot->id));
1055  
1056          $temp = clone($question);
1057          $temp->questiontext = '';
1058          $temp->name = qbank_helper::describe_random_question($slot);
1059          $instancename = quiz_question_tostring($temp);
1060  
1061          $configuretitle = get_string('configurerandomquestion', 'quiz');
1062          $qtype = \question_bank::get_qtype($question->qtype, false);
1063          $namestr = $qtype->local_name();
1064          $icon = $this->pix_icon('icon', $namestr, $qtype->plugin_name(), array('title' => $namestr,
1065                  'class' => 'icon activityicon', 'alt' => ' ', 'role' => 'presentation'));
1066  
1067          $editicon = $this->pix_icon('t/edit', $configuretitle, 'moodle', array('title' => ''));
1068          $qbankurlparams = [
1069              'cmid' => $structure->get_cmid(),
1070              'cat' => $slot->category . ',' . $slot->contextid,
1071              'recurse' => $slot->randomrecurse,
1072          ];
1073  
1074          $slottags = [];
1075          if (isset($slot->randomtags)) {
1076              $slottags = $slot->randomtags;
1077          }
1078          foreach ($slottags as $index => $slottag) {
1079              $slottag = explode(',', $slottag);
1080              $qbankurlparams["qtagids[{$index}]"] = $slottag[0];
1081          }
1082  
1083          // If this is a random question, display a link to show the questions
1084          // selected from in the question bank.
1085          $qbankurl = new \moodle_url('/question/edit.php', $qbankurlparams);
1086          $qbanklink = ' ' . \html_writer::link($qbankurl,
1087                          get_string('seequestions', 'quiz'), array('class' => 'mod_quiz_random_qbank_link'));
1088  
1089          return html_writer::link($editurl, $icon . $editicon, array('title' => $configuretitle)) .
1090                  ' ' . $instancename . ' ' . $qbanklink;
1091      }
1092  
1093      /**
1094       * Display the 'marked out of' information for a question.
1095       * Along with the regrade action.
1096       * @param structure $structure object containing the structure of the quiz.
1097       * @param int $slot which slot we are outputting.
1098       * @return string HTML to output.
1099       */
1100      public function marked_out_of_field(structure $structure, $slot) {
1101          if (!$structure->is_real_question($slot)) {
1102              $output = html_writer::span('',
1103                      'instancemaxmark decimalplaces_' . $structure->get_decimal_places_for_question_marks());
1104  
1105              $output .= html_writer::span(
1106                      $this->pix_icon('spacer', '', 'moodle', array('class' => 'editicon visibleifjs', 'title' => '')),
1107                      'editing_maxmark');
1108              return html_writer::span($output, 'instancemaxmarkcontainer infoitem');
1109          }
1110  
1111          $output = html_writer::span($structure->formatted_question_grade($slot),
1112                  'instancemaxmark decimalplaces_' . $structure->get_decimal_places_for_question_marks(),
1113                  array('title' => get_string('maxmark', 'quiz')));
1114  
1115          $output .= html_writer::span(
1116              html_writer::link(
1117                  new \moodle_url('#'),
1118                  $this->pix_icon('t/editstring', '', 'moodle', array('class' => 'editicon visibleifjs', 'title' => '')),
1119                  array(
1120                      'class' => 'editing_maxmark',
1121                      'data-action' => 'editmaxmark',
1122                      'title' => get_string('editmaxmark', 'quiz'),
1123                  )
1124              )
1125          );
1126          return html_writer::span($output, 'instancemaxmarkcontainer');
1127      }
1128  
1129      /**
1130       * Renders the question chooser.
1131       *
1132       * @param renderable
1133       * @return string
1134       */
1135      public function render_question_chooser(renderable $chooser) {
1136          return $this->render_from_template('mod_quiz/question_chooser', $chooser->export_for_template($this));
1137      }
1138  
1139      /**
1140       * Render the question type chooser dialogue.
1141       * @return string HTML to output.
1142       */
1143      public function question_chooser() {
1144          $chooser = \mod_quiz\output\question_chooser::get($this->page->course, [], null);
1145          $container = html_writer::div($this->render($chooser), '', array('id' => 'qtypechoicecontainer'));
1146          return html_writer::div($container, 'createnewquestion');
1147      }
1148  
1149      /**
1150       * Render the contents of the question bank pop-up in its initial state,
1151       * when it just contains a loading progress indicator.
1152       * @return string HTML to output.
1153       */
1154      public function question_bank_loading() {
1155          return html_writer::div($this->pix_icon('i/loading', get_string('loading')), 'questionbankloading');
1156      }
1157  
1158      /**
1159       * Initialise the JavaScript for the general editing. (JavaScript for popups
1160       * is handled with the specific code for those.)
1161       *
1162       * @param structure $structure object containing the structure of the quiz.
1163       * @param \core_question\local\bank\question_edit_contexts $contexts the relevant question bank contexts.
1164       * @param array $pagevars the variables from {@link \question_edit_setup()}.
1165       * @param \moodle_url $pageurl the canonical URL of this page.
1166       * @return bool Always returns true
1167       */
1168      protected function initialise_editing_javascript(structure $structure,
1169              \core_question\local\bank\question_edit_contexts $contexts, array $pagevars, \moodle_url $pageurl) {
1170  
1171          $config = new \stdClass();
1172          $config->resourceurl = '/mod/quiz/edit_rest.php';
1173          $config->sectionurl = '/mod/quiz/edit_rest.php';
1174          $config->pageparams = array();
1175          $config->questiondecimalpoints = $structure->get_decimal_places_for_question_marks();
1176          $config->pagehtml = $this->new_page_template($structure, $contexts, $pagevars, $pageurl);
1177          $config->addpageiconhtml = $this->add_page_icon_template($structure);
1178  
1179          $this->page->requires->yui_module('moodle-mod_quiz-toolboxes',
1180                  'M.mod_quiz.init_resource_toolbox',
1181                  array(array(
1182                          'courseid' => $structure->get_courseid(),
1183                          'quizid' => $structure->get_quizid(),
1184                          'ajaxurl' => $config->resourceurl,
1185                          'config' => $config,
1186                  ))
1187          );
1188          unset($config->pagehtml);
1189          unset($config->addpageiconhtml);
1190  
1191          $this->page->requires->strings_for_js(array('areyousureremoveselected'), 'quiz');
1192          $this->page->requires->yui_module('moodle-mod_quiz-toolboxes',
1193                  'M.mod_quiz.init_section_toolbox',
1194                  array(array(
1195                          'courseid' => $structure,
1196                          'quizid' => $structure->get_quizid(),
1197                          'ajaxurl' => $config->sectionurl,
1198                          'config' => $config,
1199                  ))
1200          );
1201  
1202          $this->page->requires->yui_module('moodle-mod_quiz-dragdrop', 'M.mod_quiz.init_section_dragdrop',
1203                  array(array(
1204                          'courseid' => $structure,
1205                          'quizid' => $structure->get_quizid(),
1206                          'ajaxurl' => $config->sectionurl,
1207                          'config' => $config,
1208                  )), null, true);
1209  
1210          $this->page->requires->yui_module('moodle-mod_quiz-dragdrop', 'M.mod_quiz.init_resource_dragdrop',
1211                  array(array(
1212                          'courseid' => $structure,
1213                          'quizid' => $structure->get_quizid(),
1214                          'ajaxurl' => $config->resourceurl,
1215                          'config' => $config,
1216                  )), null, true);
1217  
1218          // Require various strings for the command toolbox.
1219          $this->page->requires->strings_for_js(array(
1220                  'clicktohideshow',
1221                  'deletechecktype',
1222                  'deletechecktypename',
1223                  'edittitle',
1224                  'edittitleinstructions',
1225                  'emptydragdropregion',
1226                  'hide',
1227                  'markedthistopic',
1228                  'markthistopic',
1229                  'move',
1230                  'movecontent',
1231                  'moveleft',
1232                  'movesection',
1233                  'page',
1234                  'question',
1235                  'selectall',
1236                  'show',
1237                  'tocontent',
1238          ), 'moodle');
1239  
1240          $this->page->requires->strings_for_js(array(
1241                  'addpagebreak',
1242                  'cannotremoveallsectionslots',
1243                  'cannotremoveslots',
1244                  'confirmremovesectionheading',
1245                  'confirmremovequestion',
1246                  'dragtoafter',
1247                  'dragtostart',
1248                  'numquestionsx',
1249                  'sectionheadingedit',
1250                  'sectionheadingremove',
1251                  'sectionnoname',
1252                  'removepagebreak',
1253                  'questiondependencyadd',
1254                  'questiondependencyfree',
1255                  'questiondependencyremove',
1256                  'questiondependsonprevious',
1257          ), 'quiz');
1258  
1259          foreach (\question_bank::get_all_qtypes() as $qtype => $notused) {
1260              $this->page->requires->string_for_js('pluginname', 'qtype_' . $qtype);
1261          }
1262  
1263          return true;
1264      }
1265  
1266      /**
1267       * HTML for a page, with ids stripped, so it can be used as a javascript template.
1268       *
1269       * @param structure $structure object containing the structure of the quiz.
1270       * @param \core_question\local\bank\question_edit_contexts $contexts the relevant question bank contexts.
1271       * @param array $pagevars the variables from {@link \question_edit_setup()}.
1272       * @param \moodle_url $pageurl the canonical URL of this page.
1273       * @return string HTML for a new page.
1274       */
1275      protected function new_page_template(structure $structure,
1276              \core_question\local\bank\question_edit_contexts $contexts, array $pagevars, \moodle_url $pageurl) {
1277          if (!$structure->has_questions()) {
1278              return '';
1279          }
1280  
1281          $pagehtml = $this->page_row($structure, 1, $contexts, $pagevars, $pageurl);
1282  
1283          // Normalise the page number.
1284          $pagenumber = $structure->get_page_number_for_slot(1);
1285          $strcontexts = array();
1286          $strcontexts[] = 'page-';
1287          $strcontexts[] = get_string('page') . ' ';
1288          $strcontexts[] = 'addonpage%3D';
1289          $strcontexts[] = 'addonpage=';
1290          $strcontexts[] = 'addonpage="';
1291          $strcontexts[] = get_string('addquestionfrombanktopage', 'quiz', '');
1292          $strcontexts[] = 'data-addonpage%3D';
1293          $strcontexts[] = 'action-menu-';
1294  
1295          foreach ($strcontexts as $strcontext) {
1296              $pagehtml = str_replace($strcontext . $pagenumber, $strcontext . '%%PAGENUMBER%%', $pagehtml);
1297          }
1298  
1299          return $pagehtml;
1300      }
1301  
1302      /**
1303       * HTML for a page, with ids stripped, so it can be used as a javascript template.
1304       *
1305       * @param structure $structure object containing the structure of the quiz.
1306       * @return string HTML for a new icon
1307       */
1308      protected function add_page_icon_template(structure $structure) {
1309  
1310          if (!$structure->has_questions()) {
1311              return '';
1312          }
1313  
1314          $html = $this->page_split_join_button($structure, 1);
1315          return str_replace('&amp;slot=1&amp;', '&amp;slot=%%SLOT%%&amp;', $html);
1316      }
1317  
1318      /**
1319       * Return the contents of the question bank, to be displayed in the question-bank pop-up.
1320       *
1321       * @param \mod_quiz\question\bank\custom_view $questionbank the question bank view object.
1322       * @param array $pagevars the variables from {@link \question_edit_setup()}.
1323       * @return string HTML to output / send back in response to an AJAX request.
1324       */
1325      public function question_bank_contents(\mod_quiz\question\bank\custom_view $questionbank, array $pagevars) {
1326  
1327          $qbank = $questionbank->render($pagevars, 'editq');
1328          return html_writer::div(html_writer::div($qbank, 'bd'), 'questionbankformforpopup');
1329      }
1330  }