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 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   * Base class for the table used by a {@link quiz_attempts_report}.
  19   *
  20   * @package   mod_quiz
  21   * @copyright 2010 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  require_once($CFG->libdir.'/tablelib.php');
  29  
  30  
  31  /**
  32   * Base class for the table used by a {@link quiz_attempts_report}.
  33   *
  34   * @copyright 2010 The Open University
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  abstract class quiz_attempts_report_table extends table_sql {
  38      public $useridfield = 'userid';
  39  
  40      /** @var moodle_url the URL of this report. */
  41      protected $reporturl;
  42  
  43      /** @var array the display options. */
  44      protected $displayoptions;
  45  
  46      /**
  47       * @var array information about the latest step of each question.
  48       * Loaded by {@link load_question_latest_steps()}, if applicable.
  49       */
  50      protected $lateststeps = null;
  51  
  52      /** @var object the quiz settings for the quiz we are reporting on. */
  53      protected $quiz;
  54  
  55      /** @var context the quiz context. */
  56      protected $context;
  57  
  58      /** @var string HTML fragment to select the first/best/last attempt, if appropriate. */
  59      protected $qmsubselect;
  60  
  61      /** @var object mod_quiz_attempts_report_options the options affecting this report. */
  62      protected $options;
  63  
  64      /** @var \core\dml\sql_join Contains joins, wheres, params to find students
  65       * in the currently selected group, if applicable.
  66       */
  67      protected $groupstudentsjoins;
  68  
  69      /** @var \core\dml\sql_join Contains joins, wheres, params to find the students in the course. */
  70      protected $studentsjoins;
  71  
  72      /** @var object the questions that comprise this quiz.. */
  73      protected $questions;
  74  
  75      /** @var bool whether to include the column with checkboxes to select each attempt. */
  76      protected $includecheckboxes;
  77  
  78      /** @var string The toggle group name for the checkboxes in the checkbox column. */
  79      protected $togglegroup = 'quiz-attempts';
  80  
  81      /**
  82       * Constructor
  83       * @param string $uniqueid
  84       * @param object $quiz
  85       * @param context $context
  86       * @param string $qmsubselect
  87       * @param mod_quiz_attempts_report_options $options
  88       * @param \core\dml\sql_join $groupstudentsjoins Contains joins, wheres, params
  89       * @param \core\dml\sql_join $studentsjoins Contains joins, wheres, params
  90       * @param array $questions
  91       * @param moodle_url $reporturl
  92       */
  93      public function __construct($uniqueid, $quiz, $context, $qmsubselect,
  94              mod_quiz_attempts_report_options $options, \core\dml\sql_join $groupstudentsjoins, \core\dml\sql_join $studentsjoins,
  95              $questions, $reporturl) {
  96          parent::__construct($uniqueid);
  97          $this->quiz = $quiz;
  98          $this->context = $context;
  99          $this->qmsubselect = $qmsubselect;
 100          $this->groupstudentsjoins = $groupstudentsjoins;
 101          $this->studentsjoins = $studentsjoins;
 102          $this->questions = $questions;
 103          $this->includecheckboxes = $options->checkboxcolumn;
 104          $this->reporturl = $reporturl;
 105          $this->options = $options;
 106      }
 107  
 108      /**
 109       * Generate the display of the checkbox column.
 110       * @param object $attempt the table row being output.
 111       * @return string HTML content to go inside the td.
 112       */
 113      public function col_checkbox($attempt) {
 114          global $OUTPUT;
 115  
 116          if ($attempt->attempt) {
 117              $checkbox = new \core\output\checkbox_toggleall($this->togglegroup, false, [
 118                  'id' => "attemptid_{$attempt->attempt}",
 119                  'name' => 'attemptid[]',
 120                  'value' => $attempt->attempt,
 121                  'label' => get_string('selectattempt', 'quiz'),
 122                  'labelclasses' => 'accesshide',
 123              ]);
 124              return $OUTPUT->render($checkbox);
 125          } else {
 126              return '';
 127          }
 128      }
 129  
 130      /**
 131       * Generate the display of the user's picture column.
 132       * @param object $attempt the table row being output.
 133       * @return string HTML content to go inside the td.
 134       */
 135      public function col_picture($attempt) {
 136          global $OUTPUT;
 137          $user = new stdClass();
 138          $additionalfields = explode(',', implode(',', \core_user\fields::get_picture_fields()));
 139          $user = username_load_fields_from_object($user, $attempt, null, $additionalfields);
 140          $user->id = $attempt->userid;
 141          return $OUTPUT->user_picture($user);
 142      }
 143  
 144      /**
 145       * Generate the display of the user's full name column.
 146       * @param object $attempt the table row being output.
 147       * @return string HTML content to go inside the td.
 148       */
 149      public function col_fullname($attempt) {
 150          $html = parent::col_fullname($attempt);
 151          if ($this->is_downloading() || empty($attempt->attempt)) {
 152              return $html;
 153          }
 154  
 155          return $html . html_writer::empty_tag('br') . html_writer::link(
 156                  new moodle_url('/mod/quiz/review.php', array('attempt' => $attempt->attempt)),
 157                  get_string('reviewattempt', 'quiz'), array('class' => 'reviewlink'));
 158      }
 159  
 160      /**
 161       * Generate the display of the attempt state column.
 162       * @param object $attempt the table row being output.
 163       * @return string HTML content to go inside the td.
 164       */
 165      public function col_state($attempt) {
 166          if (!is_null($attempt->attempt)) {
 167              return quiz_attempt::state_name($attempt->state);
 168          } else {
 169              return  '-';
 170          }
 171      }
 172  
 173      /**
 174       * Generate the display of the start time column.
 175       * @param object $attempt the table row being output.
 176       * @return string HTML content to go inside the td.
 177       */
 178      public function col_timestart($attempt) {
 179          if ($attempt->attempt) {
 180              return userdate($attempt->timestart, $this->strtimeformat);
 181          } else {
 182              return  '-';
 183          }
 184      }
 185  
 186      /**
 187       * Generate the display of the finish time column.
 188       * @param object $attempt the table row being output.
 189       * @return string HTML content to go inside the td.
 190       */
 191      public function col_timefinish($attempt) {
 192          if ($attempt->attempt && $attempt->timefinish) {
 193              return userdate($attempt->timefinish, $this->strtimeformat);
 194          } else {
 195              return  '-';
 196          }
 197      }
 198  
 199      /**
 200       * Generate the display of the time taken column.
 201       * @param object $attempt the table row being output.
 202       * @return string HTML content to go inside the td.
 203       */
 204      public function col_duration($attempt) {
 205          if ($attempt->timefinish) {
 206              return format_time($attempt->timefinish - $attempt->timestart);
 207          } else {
 208              return '-';
 209          }
 210      }
 211  
 212      /**
 213       * Generate the display of the feedback column.
 214       * @param object $attempt the table row being output.
 215       * @return string HTML content to go inside the td.
 216       */
 217      public function col_feedbacktext($attempt) {
 218          if ($attempt->state != quiz_attempt::FINISHED) {
 219              return '-';
 220          }
 221  
 222          $feedback = quiz_report_feedback_for_grade(
 223                  quiz_rescale_grade($attempt->sumgrades, $this->quiz, false),
 224                  $this->quiz->id, $this->context);
 225  
 226          if ($this->is_downloading()) {
 227              $feedback = strip_tags($feedback);
 228          }
 229  
 230          return $feedback;
 231      }
 232  
 233      public function get_row_class($attempt) {
 234          if ($this->qmsubselect && $attempt->gradedattempt) {
 235              return 'gradedattempt';
 236          } else {
 237              return '';
 238          }
 239      }
 240  
 241      /**
 242       * Make a link to review an individual question in a popup window.
 243       *
 244       * @param string $data HTML fragment. The text to make into the link.
 245       * @param object $attempt data for the row of the table being output.
 246       * @param int $slot the number used to identify this question within this usage.
 247       */
 248      public function make_review_link($data, $attempt, $slot) {
 249          global $OUTPUT, $CFG;
 250  
 251          $flag = '';
 252          if ($this->is_flagged($attempt->usageid, $slot)) {
 253              $flag = $OUTPUT->pix_icon('i/flagged', get_string('flagged', 'question'),
 254                      'moodle', array('class' => 'questionflag'));
 255          }
 256  
 257          $feedbackimg = '';
 258          $state = $this->slot_state($attempt, $slot);
 259          if ($state && $state->is_finished() && $state != question_state::$needsgrading) {
 260              $feedbackimg = $this->icon_for_fraction($this->slot_fraction($attempt, $slot));
 261          }
 262  
 263          $output = html_writer::tag('span', $feedbackimg . html_writer::tag('span',
 264                  $data, array('class' => $state->get_state_class(true))) . $flag, array('class' => 'que'));
 265  
 266          $reviewparams = array('attempt' => $attempt->attempt, 'slot' => $slot);
 267          if (isset($attempt->try)) {
 268              $reviewparams['step'] = $this->step_no_for_try($attempt->usageid, $slot, $attempt->try);
 269          }
 270          $url = new moodle_url('/mod/quiz/reviewquestion.php', $reviewparams);
 271          $output = $OUTPUT->action_link($url, $output,
 272                  new popup_action('click', $url, 'reviewquestion',
 273                          array('height' => 450, 'width' => 650)),
 274                  array('title' => get_string('reviewresponse', 'quiz')));
 275  
 276          if (!empty($CFG->enableplagiarism)) {
 277              require_once($CFG->libdir . '/plagiarismlib.php');
 278              $output .= plagiarism_get_links([
 279                  'context' => $this->context->id,
 280                  'component' => 'qtype_'.$this->questions[$slot]->qtype,
 281                  'cmid' => $this->context->instanceid,
 282                  'area' => $attempt->usageid,
 283                  'itemid' => $slot,
 284                  'userid' => $attempt->userid]);
 285          }
 286          return $output;
 287      }
 288  
 289      /**
 290       * @param object $attempt the row data
 291       * @param int $slot
 292       * @return question_state
 293       */
 294      protected function slot_state($attempt, $slot) {
 295          $stepdata = $this->lateststeps[$attempt->usageid][$slot];
 296          return question_state::get($stepdata->state);
 297      }
 298  
 299      /**
 300       * @param int $questionusageid
 301       * @param int $slot
 302       * @return bool
 303       */
 304      protected function is_flagged($questionusageid, $slot) {
 305          $stepdata = $this->lateststeps[$questionusageid][$slot];
 306          return $stepdata->flagged;
 307      }
 308  
 309  
 310      /**
 311       * @param object $attempt the row data
 312       * @param int $slot
 313       * @return float
 314       */
 315      protected function slot_fraction($attempt, $slot) {
 316          $stepdata = $this->lateststeps[$attempt->usageid][$slot];
 317          return $stepdata->fraction;
 318      }
 319  
 320      /**
 321       * Return an appropriate icon (green tick, red cross, etc.) for a grade.
 322       * @param float $fraction grade on a scale 0..1.
 323       * @return string html fragment.
 324       */
 325      protected function icon_for_fraction($fraction) {
 326          global $OUTPUT;
 327  
 328          $feedbackclass = question_state::graded_state_for_fraction($fraction)->get_feedback_class();
 329          return $OUTPUT->pix_icon('i/grade_' . $feedbackclass, get_string($feedbackclass, 'question'),
 330                  'moodle', array('class' => 'icon'));
 331      }
 332  
 333      /**
 334       * Load any extra data after main query. At this point you can call {@link get_qubaids_condition} to get the condition that
 335       * limits the query to just the question usages shown in this report page or alternatively for all attempts if downloading a
 336       * full report.
 337       */
 338      protected function load_extra_data() {
 339          $this->lateststeps = $this->load_question_latest_steps();
 340      }
 341  
 342      /**
 343       * Load information about the latest state of selected questions in selected attempts.
 344       *
 345       * The results are returned as an two dimensional array $qubaid => $slot => $dataobject
 346       *
 347       * @param qubaid_condition|null $qubaids used to restrict which usages are included
 348       * in the query. See {@link qubaid_condition}.
 349       * @return array of records. See the SQL in this function to see the fields available.
 350       */
 351      protected function load_question_latest_steps(qubaid_condition $qubaids = null) {
 352          if ($qubaids === null) {
 353              $qubaids = $this->get_qubaids_condition();
 354          }
 355          $dm = new question_engine_data_mapper();
 356          $latesstepdata = $dm->load_questions_usages_latest_steps(
 357                  $qubaids, array_keys($this->questions));
 358  
 359          $lateststeps = array();
 360          foreach ($latesstepdata as $step) {
 361              $lateststeps[$step->questionusageid][$step->slot] = $step;
 362          }
 363  
 364          return $lateststeps;
 365      }
 366  
 367      /**
 368       * Does this report require loading any more data after the main query. After the main query then
 369       * you can use $this->get
 370       *
 371       * @return bool should {@link query_db()} call {@link load_extra_data}?
 372       */
 373      protected function requires_extra_data() {
 374          return $this->requires_latest_steps_loaded();
 375      }
 376  
 377      /**
 378       * Does this report require the detailed information for each question from the
 379       * question_attempts_steps table?
 380       * @return bool should {@link load_extra_data} call {@link load_question_latest_steps}?
 381       */
 382      protected function requires_latest_steps_loaded() {
 383          return false;
 384      }
 385  
 386      /**
 387       * Is this a column that depends on joining to the latest state information?
 388       * If so, return the corresponding slot. If not, return false.
 389       * @param string $column a column name
 390       * @return int false if no, else a slot.
 391       */
 392      protected function is_latest_step_column($column) {
 393          return false;
 394      }
 395  
 396      /**
 397       * Get any fields that might be needed when sorting on date for a particular slot.
 398       *
 399       * Note: these values are only used for sorting. The values displayed are taken
 400       * from $this->lateststeps loaded in load_extra_data().
 401       *
 402       * @param int $slot the slot for the column we want.
 403       * @param string $alias the table alias for latest state information relating to that slot.
 404       * @return string definitions of extra fields to add to the SELECT list of the query.
 405       */
 406      protected function get_required_latest_state_fields($slot, $alias) {
 407          return '';
 408      }
 409  
 410      /**
 411       * Contruct all the parts of the main database query.
 412       * @param \core\dml\sql_join $allowedstudentsjoins (joins, wheres, params) defines allowed users for the report.
 413       * @return array with 4 elements ($fields, $from, $where, $params) that can be used to
 414       *     build the actual database query.
 415       */
 416      public function base_sql(\core\dml\sql_join $allowedstudentsjoins) {
 417          global $DB;
 418  
 419          // Please note this uniqueid column is not the same as quiza.uniqueid.
 420          $fields = 'DISTINCT ' . $DB->sql_concat('u.id', "'#'", 'COALESCE(quiza.attempt, 0)') . ' AS uniqueid,';
 421  
 422          if ($this->qmsubselect) {
 423              $fields .= "\n(CASE WHEN $this->qmsubselect THEN 1 ELSE 0 END) AS gradedattempt,";
 424          }
 425  
 426          $userfieldsapi = \core_user\fields::for_identity($this->context)->with_name()
 427                  ->excluding('id', 'idnumber', 'picture', 'imagealt', 'institution', 'department', 'email');
 428          $userfields = $userfieldsapi->get_sql('u', true, '', '', false);
 429  
 430          $fields .= '
 431                  quiza.uniqueid AS usageid,
 432                  quiza.id AS attempt,
 433                  u.id AS userid,
 434                  u.idnumber,
 435                  u.picture,
 436                  u.imagealt,
 437                  u.institution,
 438                  u.department,
 439                  u.email,' . $userfields->selects . ',
 440                  quiza.state,
 441                  quiza.sumgrades,
 442                  quiza.timefinish,
 443                  quiza.timestart,
 444                  CASE WHEN quiza.timefinish = 0 THEN null
 445                       WHEN quiza.timefinish > quiza.timestart THEN quiza.timefinish - quiza.timestart
 446                       ELSE 0 END AS duration';
 447              // To explain that last bit, timefinish can be non-zero and less
 448              // than timestart when you have two load-balanced servers with very
 449              // badly synchronised clocks, and a student does a really quick attempt.
 450  
 451          // This part is the same for all cases. Join the users and quiz_attempts tables.
 452          $from = " {user} u";
 453          $from .= "\n{$userfields->joins}";
 454          $from .= "\nLEFT JOIN {quiz_attempts} quiza ON
 455                                      quiza.userid = u.id AND quiza.quiz = :quizid";
 456          $params = array_merge($userfields->params, ['quizid' => $this->quiz->id]);
 457  
 458          if ($this->qmsubselect && $this->options->onlygraded) {
 459              $from .= " AND (quiza.state <> :finishedstate OR $this->qmsubselect)";
 460              $params['finishedstate'] = quiz_attempt::FINISHED;
 461          }
 462  
 463          switch ($this->options->attempts) {
 464              case quiz_attempts_report::ALL_WITH:
 465                  // Show all attempts, including students who are no longer in the course.
 466                  $where = 'quiza.id IS NOT NULL AND quiza.preview = 0';
 467                  break;
 468              case quiz_attempts_report::ENROLLED_WITH:
 469                  // Show only students with attempts.
 470                  $from .= "\n" . $allowedstudentsjoins->joins;
 471                  $where = "quiza.preview = 0 AND quiza.id IS NOT NULL AND " . $allowedstudentsjoins->wheres;
 472                  $params = array_merge($params, $allowedstudentsjoins->params);
 473                  break;
 474              case quiz_attempts_report::ENROLLED_WITHOUT:
 475                  // Show only students without attempts.
 476                  $from .= "\n" . $allowedstudentsjoins->joins;
 477                  $where = "quiza.id IS NULL AND " . $allowedstudentsjoins->wheres;
 478                  $params = array_merge($params, $allowedstudentsjoins->params);
 479                  break;
 480              case quiz_attempts_report::ENROLLED_ALL:
 481                  // Show all students with or without attempts.
 482                  $from .= "\n" . $allowedstudentsjoins->joins;
 483                  $where = "(quiza.preview = 0 OR quiza.preview IS NULL) AND " . $allowedstudentsjoins->wheres;
 484                  $params = array_merge($params, $allowedstudentsjoins->params);
 485                  break;
 486          }
 487  
 488          if ($this->options->states) {
 489              list($statesql, $stateparams) = $DB->get_in_or_equal($this->options->states,
 490                      SQL_PARAMS_NAMED, 'state');
 491              $params += $stateparams;
 492              $where .= " AND (quiza.state $statesql OR quiza.state IS NULL)";
 493          }
 494  
 495          return array($fields, $from, $where, $params);
 496      }
 497  
 498      /**
 499       * A chance for subclasses to modify the SQL after the count query has been generated,
 500       * and before the full query is constructed.
 501       * @param string $fields SELECT list.
 502       * @param string $from JOINs part of the SQL.
 503       * @param string $where WHERE clauses.
 504       * @param array $params Query params.
 505       * @return array with 4 elements ($fields, $from, $where, $params) as from base_sql.
 506       */
 507      protected function update_sql_after_count($fields, $from, $where, $params) {
 508          return [$fields, $from, $where, $params];
 509      }
 510  
 511      /**
 512       * Set up the SQL queries (count rows, and get data).
 513       *
 514       * @param \core\dml\sql_join $allowedjoins (joins, wheres, params) defines allowed users for the report.
 515       */
 516      public function setup_sql_queries($allowedjoins) {
 517          list($fields, $from, $where, $params) = $this->base_sql($allowedjoins);
 518  
 519          // The WHERE clause is vital here, because some parts of tablelib.php will expect to
 520          // add bits like ' AND x = 1' on the end, and that needs to leave to valid SQL.
 521          $this->set_count_sql("SELECT COUNT(1) FROM (SELECT $fields FROM $from WHERE $where) temp WHERE 1 = 1", $params);
 522  
 523          list($fields, $from, $where, $params) = $this->update_sql_after_count($fields, $from, $where, $params);
 524          $this->set_sql($fields, $from, $where, $params);
 525      }
 526  
 527      /**
 528       * Add the information about the latest state of the question with slot
 529       * $slot to the query.
 530       *
 531       * The extra information is added as a join to a
 532       * 'table' with alias qa$slot, with columns that are a union of
 533       * the columns of the question_attempts and question_attempts_states tables.
 534       *
 535       * @param int $slot the question to add information for.
 536       */
 537      protected function add_latest_state_join($slot) {
 538          $alias = 'qa' . $slot;
 539  
 540          $fields = $this->get_required_latest_state_fields($slot, $alias);
 541          if (!$fields) {
 542              return;
 543          }
 544  
 545          // This condition roughly filters the list of attempts to be considered.
 546          // It is only used in a subselect to help crappy databases (see MDL-30122)
 547          // therefore, it is better to use a very simple join, which may include
 548          // too many records, than to do a super-accurate join.
 549          $qubaids = new qubaid_join("{quiz_attempts} {$alias}quiza", "{$alias}quiza.uniqueid",
 550                  "{$alias}quiza.quiz = :{$alias}quizid", array("{$alias}quizid" => $this->sql->params['quizid']));
 551  
 552          $dm = new question_engine_data_mapper();
 553          list($inlineview, $viewparams) = $dm->question_attempt_latest_state_view($alias, $qubaids);
 554  
 555          $this->sql->fields .= ",\n$fields";
 556          $this->sql->from .= "\nLEFT JOIN $inlineview ON " .
 557                  "$alias.questionusageid = quiza.uniqueid AND $alias.slot = :{$alias}slot";
 558          $this->sql->params[$alias . 'slot'] = $slot;
 559          $this->sql->params = array_merge($this->sql->params, $viewparams);
 560      }
 561  
 562      /**
 563       * Get an appropriate qubaid_condition for loading more data about the
 564       * attempts we are displaying.
 565       * @return qubaid_condition
 566       */
 567      protected function get_qubaids_condition() {
 568          if (is_null($this->rawdata)) {
 569              throw new coding_exception(
 570                      'Cannot call get_qubaids_condition until the main data has been loaded.');
 571          }
 572  
 573          if ($this->is_downloading()) {
 574              // We want usages for all attempts.
 575              return new qubaid_join("(
 576                  SELECT DISTINCT quiza.uniqueid
 577                    FROM " . $this->sql->from . "
 578                   WHERE " . $this->sql->where . "
 579                      ) quizasubquery", 'quizasubquery.uniqueid',
 580                      "1 = 1", $this->sql->params);
 581          }
 582  
 583          $qubaids = array();
 584          foreach ($this->rawdata as $attempt) {
 585              if ($attempt->usageid > 0) {
 586                  $qubaids[] = $attempt->usageid;
 587              }
 588          }
 589  
 590          return new qubaid_list($qubaids);
 591      }
 592  
 593      public function query_db($pagesize, $useinitialsbar = true) {
 594          $doneslots = array();
 595          foreach ($this->get_sort_columns() as $column => $notused) {
 596              $slot = $this->is_latest_step_column($column);
 597              if ($slot && !in_array($slot, $doneslots)) {
 598                  $this->add_latest_state_join($slot);
 599                  $doneslots[] = $slot;
 600              }
 601          }
 602  
 603          parent::query_db($pagesize, $useinitialsbar);
 604  
 605          if ($this->requires_extra_data()) {
 606              $this->load_extra_data();
 607          }
 608      }
 609  
 610      public function get_sort_columns() {
 611          // Add attemptid as a final tie-break to the sort. This ensures that
 612          // Attempts by the same student appear in order when just sorting by name.
 613          $sortcolumns = parent::get_sort_columns();
 614          $sortcolumns['quiza.id'] = SORT_ASC;
 615          return $sortcolumns;
 616      }
 617  
 618      public function wrap_html_start() {
 619          if ($this->is_downloading() || !$this->includecheckboxes) {
 620              return;
 621          }
 622  
 623          $url = $this->options->get_url();
 624          $url->param('sesskey', sesskey());
 625  
 626          echo '<div id="tablecontainer">';
 627          echo '<form id="attemptsform" method="post" action="' . $url->out_omit_querystring() . '">';
 628  
 629          echo html_writer::input_hidden_params($url);
 630          echo '<div>';
 631      }
 632  
 633      public function wrap_html_finish() {
 634          global $PAGE;
 635          if ($this->is_downloading() || !$this->includecheckboxes) {
 636              return;
 637          }
 638  
 639          echo '<div id="commands">';
 640          $this->submit_buttons();
 641          echo '</div>';
 642  
 643          // Close the form.
 644          echo '</div>';
 645          echo '</form></div>';
 646      }
 647  
 648      /**
 649       * Output any submit buttons required by the $this->includecheckboxes form.
 650       */
 651      protected function submit_buttons() {
 652          global $PAGE;
 653          if (has_capability('mod/quiz:deleteattempts', $this->context)) {
 654              $deletebuttonparams = [
 655                  'type'  => 'submit',
 656                  'class' => 'btn btn-secondary mr-1',
 657                  'id'    => 'deleteattemptsbutton',
 658                  'name'  => 'delete',
 659                  'value' => get_string('deleteselected', 'quiz_overview'),
 660                  'data-action' => 'toggle',
 661                  'data-togglegroup' => $this->togglegroup,
 662                  'data-toggle' => 'action',
 663                  'disabled' => true
 664              ];
 665              echo html_writer::empty_tag('input', $deletebuttonparams);
 666              $PAGE->requires->event_handler('#deleteattemptsbutton', 'click', 'M.util.show_confirm_dialog',
 667                      array('message' => get_string('deleteattemptcheck', 'quiz')));
 668          }
 669      }
 670  
 671      /**
 672       * Generates the contents for the checkbox column header.
 673       *
 674       * It returns the HTML for a master \core\output\checkbox_toggleall component that selects/deselects all quiz attempts.
 675       *
 676       * @param string $columnname The name of the checkbox column.
 677       * @return string
 678       */
 679      public function checkbox_col_header(string $columnname) {
 680          global $OUTPUT;
 681  
 682          // Make sure to disable sorting on this column.
 683          $this->no_sorting($columnname);
 684  
 685          // Build the select/deselect all control.
 686          $selectallid = $this->uniqueid . '-selectall-attempts';
 687          $selectalltext = get_string('selectall', 'quiz');
 688          $deselectalltext = get_string('selectnone', 'quiz');
 689          $mastercheckbox = new \core\output\checkbox_toggleall($this->togglegroup, true, [
 690              'id' => $selectallid,
 691              'name' => $selectallid,
 692              'value' => 1,
 693              'label' => $selectalltext,
 694              'labelclasses' => 'accesshide',
 695              'selectall' => $selectalltext,
 696              'deselectall' => $deselectalltext,
 697          ]);
 698  
 699          return $OUTPUT->render($mastercheckbox);
 700      }
 701  }