Search moodle.org's
Developer Documentation

See Release Notes

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

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

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * 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->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          // TODO Does not support custom user profile fields (MDL-70456).
 427          $userfields = \core_user\fields::for_identity($this->context, false)->with_name()
 428                  ->excluding('id', 'idnumber', 'picture', 'imagealt', 'institution', 'department', 'email');
 429          $extrafields = $userfields->get_sql('u')->selects;
 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' . $extrafields . ',
 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 .= "\nLEFT JOIN {quiz_attempts} quiza ON
 454                                      quiza.userid = u.id AND quiza.quiz = :quizid";
 455          $params = array('quizid' => $this->quiz->id);
 456  
 457          if ($this->qmsubselect && $this->options->onlygraded) {
 458              $from .= " AND (quiza.state <> :finishedstate OR $this->qmsubselect)";
 459              $params['finishedstate'] = quiz_attempt::FINISHED;
 460          }
 461  
 462          switch ($this->options->attempts) {
 463              case quiz_attempts_report::ALL_WITH:
 464                  // Show all attempts, including students who are no longer in the course.
 465                  $where = 'quiza.id IS NOT NULL AND quiza.preview = 0';
 466                  break;
 467              case quiz_attempts_report::ENROLLED_WITH:
 468                  // Show only students with attempts.
 469                  $from .= "\n" . $allowedstudentsjoins->joins;
 470                  $where = "quiza.preview = 0 AND quiza.id IS NOT NULL AND " . $allowedstudentsjoins->wheres;
 471                  $params = array_merge($params, $allowedstudentsjoins->params);
 472                  break;
 473              case quiz_attempts_report::ENROLLED_WITHOUT:
 474                  // Show only students without attempts.
 475                  $from .= "\n" . $allowedstudentsjoins->joins;
 476                  $where = "quiza.id IS NULL AND " . $allowedstudentsjoins->wheres;
 477                  $params = array_merge($params, $allowedstudentsjoins->params);
 478                  break;
 479              case quiz_attempts_report::ENROLLED_ALL:
 480                  // Show all students with or without attempts.
 481                  $from .= "\n" . $allowedstudentsjoins->joins;
 482                  $where = "(quiza.preview = 0 OR quiza.preview IS NULL) AND " . $allowedstudentsjoins->wheres;
 483                  $params = array_merge($params, $allowedstudentsjoins->params);
 484                  break;
 485          }
 486  
 487          if ($this->options->states) {
 488              list($statesql, $stateparams) = $DB->get_in_or_equal($this->options->states,
 489                      SQL_PARAMS_NAMED, 'state');
 490              $params += $stateparams;
 491              $where .= " AND (quiza.state $statesql OR quiza.state IS NULL)";
 492          }
 493  
 494          return array($fields, $from, $where, $params);
 495      }
 496  
 497      /**
 498       * A chance for subclasses to modify the SQL after the count query has been generated,
 499       * and before the full query is constructed.
 500       * @param string $fields SELECT list.
 501       * @param string $from JOINs part of the SQL.
 502       * @param string $where WHERE clauses.
 503       * @param array $params Query params.
 504       * @return array with 4 elements ($fields, $from, $where, $params) as from base_sql.
 505       */
 506      protected function update_sql_after_count($fields, $from, $where, $params) {
 507          return [$fields, $from, $where, $params];
 508      }
 509  
 510      /**
 511       * Set up the SQL queries (count rows, and get data).
 512       *
 513       * @param \core\dml\sql_join $allowedjoins (joins, wheres, params) defines allowed users for the report.
 514       */
 515      public function setup_sql_queries($allowedjoins) {
 516          list($fields, $from, $where, $params) = $this->base_sql($allowedjoins);
 517  
 518          // The WHERE clause is vital here, because some parts of tablelib.php will expect to
 519          // add bits like ' AND x = 1' on the end, and that needs to leave to valid SQL.
 520          $this->set_count_sql("SELECT COUNT(1) FROM (SELECT $fields FROM $from WHERE $where) temp WHERE 1 = 1", $params);
 521  
 522          list($fields, $from, $where, $params) = $this->update_sql_after_count($fields, $from, $where, $params);
 523          $this->set_sql($fields, $from, $where, $params);
 524      }
 525  
 526      /**
 527       * Add the information about the latest state of the question with slot
 528       * $slot to the query.
 529       *
 530       * The extra information is added as a join to a
 531       * 'table' with alias qa$slot, with columns that are a union of
 532       * the columns of the question_attempts and question_attempts_states tables.
 533       *
 534       * @param int $slot the question to add information for.
 535       */
 536      protected function add_latest_state_join($slot) {
 537          $alias = 'qa' . $slot;
 538  
 539          $fields = $this->get_required_latest_state_fields($slot, $alias);
 540          if (!$fields) {
 541              return;
 542          }
 543  
 544          // This condition roughly filters the list of attempts to be considered.
 545          // It is only used in a subselect to help crappy databases (see MDL-30122)
 546          // therefore, it is better to use a very simple join, which may include
 547          // too many records, than to do a super-accurate join.
 548          $qubaids = new qubaid_join("{quiz_attempts} {$alias}quiza", "{$alias}quiza.uniqueid",
 549                  "{$alias}quiza.quiz = :{$alias}quizid", array("{$alias}quizid" => $this->sql->params['quizid']));
 550  
 551          $dm = new question_engine_data_mapper();
 552          list($inlineview, $viewparams) = $dm->question_attempt_latest_state_view($alias, $qubaids);
 553  
 554          $this->sql->fields .= ",\n$fields";
 555          $this->sql->from .= "\nLEFT JOIN $inlineview ON " .
 556                  "$alias.questionusageid = quiza.uniqueid AND $alias.slot = :{$alias}slot";
 557          $this->sql->params[$alias . 'slot'] = $slot;
 558          $this->sql->params = array_merge($this->sql->params, $viewparams);
 559      }
 560  
 561      /**
 562       * Get an appropriate qubaid_condition for loading more data about the
 563       * attempts we are displaying.
 564       * @return qubaid_condition
 565       */
 566      protected function get_qubaids_condition() {
 567          if (is_null($this->rawdata)) {
 568              throw new coding_exception(
 569                      'Cannot call get_qubaids_condition until the main data has been loaded.');
 570          }
 571  
 572          if ($this->is_downloading()) {
 573              // We want usages for all attempts.
 574              return new qubaid_join("(
 575                  SELECT DISTINCT quiza.uniqueid
 576                    FROM " . $this->sql->from . "
 577                   WHERE " . $this->sql->where . "
 578                      ) quizasubquery", 'quizasubquery.uniqueid',
 579                      "1 = 1", $this->sql->params);
 580          }
 581  
 582          $qubaids = array();
 583          foreach ($this->rawdata as $attempt) {
 584              if ($attempt->usageid > 0) {
 585                  $qubaids[] = $attempt->usageid;
 586              }
 587          }
 588  
 589          return new qubaid_list($qubaids);
 590      }
 591  
 592      public function query_db($pagesize, $useinitialsbar = true) {
 593          $doneslots = array();
 594          foreach ($this->get_sort_columns() as $column => $notused) {
 595              $slot = $this->is_latest_step_column($column);
 596              if ($slot && !in_array($slot, $doneslots)) {
 597                  $this->add_latest_state_join($slot);
 598                  $doneslots[] = $slot;
 599              }
 600          }
 601  
 602          parent::query_db($pagesize, $useinitialsbar);
 603  
 604          if ($this->requires_extra_data()) {
 605              $this->load_extra_data();
 606          }
 607      }
 608  
 609      public function get_sort_columns() {
 610          // Add attemptid as a final tie-break to the sort. This ensures that
 611          // Attempts by the same student appear in order when just sorting by name.
 612          $sortcolumns = parent::get_sort_columns();
 613          $sortcolumns['quiza.id'] = SORT_ASC;
 614          return $sortcolumns;
 615      }
 616  
 617      public function wrap_html_start() {
 618          if ($this->is_downloading() || !$this->includecheckboxes) {
 619              return;
 620          }
 621  
 622          $url = $this->options->get_url();
 623          $url->param('sesskey', sesskey());
 624  
 625          echo '<div id="tablecontainer">';
 626          echo '<form id="attemptsform" method="post" action="' . $url->out_omit_querystring() . '">';
 627  
 628          echo html_writer::input_hidden_params($url);
 629          echo '<div>';
 630      }
 631  
 632      public function wrap_html_finish() {
 633          global $PAGE;
 634          if ($this->is_downloading() || !$this->includecheckboxes) {
 635              return;
 636          }
 637  
 638          echo '<div id="commands">';
 639          $this->submit_buttons();
 640          echo '</div>';
 641  
 642          // Close the form.
 643          echo '</div>';
 644          echo '</form></div>';
 645      }
 646  
 647      /**
 648       * Output any submit buttons required by the $this->includecheckboxes form.
 649       */
 650      protected function submit_buttons() {
 651          global $PAGE;
 652          if (has_capability('mod/quiz:deleteattempts', $this->context)) {
 653              $deletebuttonparams = [
 654                  'type'  => 'submit',
 655                  'class' => 'btn btn-secondary mr-1',
 656                  'id'    => 'deleteattemptsbutton',
 657                  'name'  => 'delete',
 658                  'value' => get_string('deleteselected', 'quiz_overview'),
 659                  'data-action' => 'toggle',
 660                  'data-togglegroup' => $this->togglegroup,
 661                  'data-toggle' => 'action',
 662                  'disabled' => true
 663              ];
 664              echo html_writer::empty_tag('input', $deletebuttonparams);
 665              $PAGE->requires->event_handler('#deleteattemptsbutton', 'click', 'M.util.show_confirm_dialog',
 666                      array('message' => get_string('deleteattemptcheck', 'quiz')));
 667          }
 668      }
 669  
 670      /**
 671       * Generates the contents for the checkbox column header.
 672       *
 673       * It returns the HTML for a master \core\output\checkbox_toggleall component that selects/deselects all quiz attempts.
 674       *
 675       * @param string $columnname The name of the checkbox column.
 676       * @return string
 677       */
 678      public function checkbox_col_header(string $columnname) {
 679          global $OUTPUT;
 680  
 681          // Make sure to disable sorting on this column.
 682          $this->no_sorting($columnname);
 683  
 684          // Build the select/deselect all control.
 685          $selectallid = $this->uniqueid . '-selectall-attempts';
 686          $selectalltext = get_string('selectall', 'quiz');
 687          $deselectalltext = get_string('selectnone', 'quiz');
 688          $mastercheckbox = new \core\output\checkbox_toggleall($this->togglegroup, true, [
 689              'id' => $selectallid,
 690              'name' => $selectallid,
 691              'value' => 1,
 692              'label' => $selectalltext,
 693              'labelclasses' => 'accesshide',
 694              'selectall' => $selectalltext,
 695              'deselectall' => $deselectalltext,
 696          ]);
 697  
 698          return $OUTPUT->render($mastercheckbox);
 699      }
 700  }