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   * The file defines a base class that can be used to build a report like the
  19   * overview or responses report, that has one row per attempt.
  20   *
  21   * @package   mod_quiz
  22   * @copyright 2010 The Open University
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->libdir.'/tablelib.php');
  30  
  31  
  32  /**
  33   * Base class for quiz reports that are basically a table with one row for each attempt.
  34   *
  35   * @copyright 2010 The Open University
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  abstract class quiz_attempts_report extends quiz_default_report {
  39      /** @var int default page size for reports. */
  40      const DEFAULT_PAGE_SIZE = 30;
  41  
  42      /** @var string constant used for the options, means all users with attempts. */
  43      const ALL_WITH = 'all_with';
  44      /** @var string constant used for the options, means only enrolled users with attempts. */
  45      const ENROLLED_WITH = 'enrolled_with';
  46      /** @var string constant used for the options, means only enrolled users without attempts. */
  47      const ENROLLED_WITHOUT = 'enrolled_without';
  48      /** @var string constant used for the options, means all enrolled users. */
  49      const ENROLLED_ALL = 'enrolled_any';
  50  
  51      /** @var string the mode this report is. */
  52      protected $mode;
  53  
  54      /** @var object the quiz context. */
  55      protected $context;
  56  
  57      /** @var mod_quiz_attempts_report_form The settings form to use. */
  58      protected $form;
  59  
  60      /** @var string SQL fragment for selecting the attempt that gave the final grade,
  61       * if applicable. */
  62      protected $qmsubselect;
  63  
  64      /** @var boolean caches the results of {@link should_show_grades()}. */
  65      protected $showgrades = null;
  66  
  67      /**
  68       *  Initialise various aspects of this report.
  69       *
  70       * @param string $mode
  71       * @param string $formclass
  72       * @param object $quiz
  73       * @param object $cm
  74       * @param object $course
  75       * @return array with four elements:
  76       *      0 => integer the current group id (0 for none).
  77       *      1 => \core\dml\sql_join Contains joins, wheres, params for all the students in this course.
  78       *      2 => \core\dml\sql_join Contains joins, wheres, params for all the students in the current group.
  79       *      3 => \core\dml\sql_join Contains joins, wheres, params for all the students to show in the report.
  80       *              Will be the same as either element 1 or 2.
  81       */
  82      protected function init($mode, $formclass, $quiz, $cm, $course) {
  83          $this->mode = $mode;
  84  
  85          $this->context = context_module::instance($cm->id);
  86  
  87          list($currentgroup, $studentsjoins, $groupstudentsjoins, $allowedjoins) = $this->get_students_joins(
  88                  $cm, $course);
  89  
  90          $this->qmsubselect = quiz_report_qm_filter_select($quiz);
  91  
  92          $this->form = new $formclass($this->get_base_url(),
  93                  array('quiz' => $quiz, 'currentgroup' => $currentgroup, 'context' => $this->context));
  94  
  95          return array($currentgroup, $studentsjoins, $groupstudentsjoins, $allowedjoins);
  96      }
  97  
  98      /**
  99       * Get the base URL for this report.
 100       * @return moodle_url the URL.
 101       */
 102      protected function get_base_url() {
 103          return new moodle_url('/mod/quiz/report.php',
 104                  array('id' => $this->context->instanceid, 'mode' => $this->mode));
 105      }
 106  
 107      /**
 108       * Get sql fragments (joins) which can be used to build queries that
 109       * will select an appropriate set of students to show in the reports.
 110       *
 111       * @param object $cm the course module.
 112       * @param object $course the course settings.
 113       * @return array with four elements:
 114       *      0 => integer the current group id (0 for none).
 115       *      1 => \core\dml\sql_join Contains joins, wheres, params for all the students in this course.
 116       *      2 => \core\dml\sql_join Contains joins, wheres, params for all the students in the current group.
 117       *      3 => \core\dml\sql_join Contains joins, wheres, params for all the students to show in the report.
 118       *              Will be the same as either element 1 or 2.
 119       */
 120      protected function get_students_joins($cm, $course = null) {
 121          $currentgroup = $this->get_current_group($cm, $course, $this->context);
 122  
 123          $empty = new \core\dml\sql_join();
 124          if ($currentgroup == self::NO_GROUPS_ALLOWED) {
 125              return array($currentgroup, $empty, $empty, $empty);
 126          }
 127  
 128          $studentsjoins = get_enrolled_with_capabilities_join($this->context, '',
 129                  array('mod/quiz:attempt', 'mod/quiz:reviewmyattempts'));
 130  
 131          if (empty($currentgroup)) {
 132              return array($currentgroup, $studentsjoins, $empty, $studentsjoins);
 133          }
 134  
 135          // We have a currently selected group.
 136          $groupstudentsjoins = get_enrolled_with_capabilities_join($this->context, '',
 137                  array('mod/quiz:attempt', 'mod/quiz:reviewmyattempts'), $currentgroup);
 138  
 139          return array($currentgroup, $studentsjoins, $groupstudentsjoins, $groupstudentsjoins);
 140      }
 141  
 142      /**
 143       * Outputs the things you commonly want at the top of a quiz report.
 144       *
 145       * Calls through to {@link print_header_and_tabs()} and then
 146       * outputs the standard group selector, number of attempts summary,
 147       * and messages to cover common cases when the report can't be shown.
 148       *
 149       * @param stdClass $cm the course_module information.
 150       * @param stdClass $course the course settings.
 151       * @param stdClass $quiz the quiz settings.
 152       * @param mod_quiz_attempts_report_options $options the current report settings.
 153       * @param int $currentgroup the current group.
 154       * @param bool $hasquestions whether there are any questions in the quiz.
 155       * @param bool $hasstudents whether there are any relevant students.
 156       */
 157      protected function print_standard_header_and_messages($cm, $course, $quiz,
 158              $options, $currentgroup, $hasquestions, $hasstudents) {
 159          global $OUTPUT;
 160  
 161          $this->print_header_and_tabs($cm, $course, $quiz, $this->mode);
 162  
 163          if (groups_get_activity_groupmode($cm)) {
 164              // Groups are being used, so output the group selector if we are not downloading.
 165              groups_print_activity_menu($cm, $options->get_url());
 166          }
 167  
 168          // Print information on the number of existing attempts.
 169          if ($strattemptnum = quiz_num_attempt_summary($quiz, $cm, true, $currentgroup)) {
 170              echo '<div class="quizattemptcounts">' . $strattemptnum . '</div>';
 171          }
 172  
 173          if (!$hasquestions) {
 174              echo quiz_no_questions_message($quiz, $cm, $this->context);
 175          } else if ($currentgroup == self::NO_GROUPS_ALLOWED) {
 176              echo $OUTPUT->notification(get_string('notingroup'));
 177          } else if (!$hasstudents) {
 178              echo $OUTPUT->notification(get_string('nostudentsyet'));
 179          } else if ($currentgroup && !$this->hasgroupstudents) {
 180              echo $OUTPUT->notification(get_string('nostudentsingroup'));
 181          }
 182      }
 183  
 184      /**
 185       * Add all the user-related columns to the $columns and $headers arrays.
 186       * @param table_sql $table the table being constructed.
 187       * @param array $columns the list of columns. Added to.
 188       * @param array $headers the columns headings. Added to.
 189       */
 190      protected function add_user_columns($table, &$columns, &$headers) {
 191          global $CFG;
 192          if (!$table->is_downloading() && $CFG->grade_report_showuserimage) {
 193              $columns[] = 'picture';
 194              $headers[] = '';
 195          }
 196          if (!$table->is_downloading()) {
 197              $columns[] = 'fullname';
 198              $headers[] = get_string('name');
 199          } else {
 200              $columns[] = 'lastname';
 201              $headers[] = get_string('lastname');
 202              $columns[] = 'firstname';
 203              $headers[] = get_string('firstname');
 204          }
 205  
 206          // TODO Does not support custom user profile fields (MDL-70456).
 207          $extrafields = \core_user\fields::get_identity_fields($this->context, false);
 208          foreach ($extrafields as $field) {
 209              $columns[] = $field;
 210              $headers[] = \core_user\fields::get_display_name($field);
 211          }
 212      }
 213  
 214      /**
 215       * Set the display options for the user-related columns in the table.
 216       * @param table_sql $table the table being constructed.
 217       */
 218      protected function configure_user_columns($table) {
 219          $table->column_suppress('picture');
 220          $table->column_suppress('fullname');
 221          // TODO Does not support custom user profile fields (MDL-70456).
 222          $extrafields = \core_user\fields::get_identity_fields($this->context, false);
 223          foreach ($extrafields as $field) {
 224              $table->column_suppress($field);
 225          }
 226  
 227          $table->column_class('picture', 'picture');
 228          $table->column_class('lastname', 'bold');
 229          $table->column_class('firstname', 'bold');
 230          $table->column_class('fullname', 'bold');
 231      }
 232  
 233      /**
 234       * Add the state column to the $columns and $headers arrays.
 235       * @param array $columns the list of columns. Added to.
 236       * @param array $headers the columns headings. Added to.
 237       */
 238      protected function add_state_column(&$columns, &$headers) {
 239          $columns[] = 'state';
 240          $headers[] = get_string('attemptstate', 'quiz');
 241      }
 242  
 243      /**
 244       * Add all the time-related columns to the $columns and $headers arrays.
 245       * @param array $columns the list of columns. Added to.
 246       * @param array $headers the columns headings. Added to.
 247       */
 248      protected function add_time_columns(&$columns, &$headers) {
 249          $columns[] = 'timestart';
 250          $headers[] = get_string('startedon', 'quiz');
 251  
 252          $columns[] = 'timefinish';
 253          $headers[] = get_string('timecompleted', 'quiz');
 254  
 255          $columns[] = 'duration';
 256          $headers[] = get_string('attemptduration', 'quiz');
 257      }
 258  
 259      /**
 260       * Add all the grade and feedback columns, if applicable, to the $columns
 261       * and $headers arrays.
 262       * @param object $quiz the quiz settings.
 263       * @param bool $usercanseegrades whether the user is allowed to see grades for this quiz.
 264       * @param array $columns the list of columns. Added to.
 265       * @param array $headers the columns headings. Added to.
 266       * @param bool $includefeedback whether to include the feedbacktext columns
 267       */
 268      protected function add_grade_columns($quiz, $usercanseegrades, &$columns, &$headers, $includefeedback = true) {
 269          if ($usercanseegrades) {
 270              $columns[] = 'sumgrades';
 271              $headers[] = get_string('grade', 'quiz') . '/' .
 272                      quiz_format_grade($quiz, $quiz->grade);
 273          }
 274  
 275          if ($includefeedback && quiz_has_feedback($quiz)) {
 276              $columns[] = 'feedbacktext';
 277              $headers[] = get_string('feedback', 'quiz');
 278          }
 279      }
 280  
 281      /**
 282       * Set up the table.
 283       * @param table_sql $table the table being constructed.
 284       * @param array $columns the list of columns.
 285       * @param array $headers the columns headings.
 286       * @param moodle_url $reporturl the URL of this report.
 287       * @param mod_quiz_attempts_report_options $options the display options.
 288       * @param bool $collapsible whether to allow columns in the report to be collapsed.
 289       */
 290      protected function set_up_table_columns($table, $columns, $headers, $reporturl,
 291              mod_quiz_attempts_report_options $options, $collapsible) {
 292          $table->define_columns($columns);
 293          $table->define_headers($headers);
 294          $table->sortable(true, 'uniqueid');
 295  
 296          $table->define_baseurl($options->get_url());
 297  
 298          $this->configure_user_columns($table);
 299  
 300          $table->no_sorting('feedbacktext');
 301          $table->column_class('sumgrades', 'bold');
 302  
 303          $table->set_attribute('id', 'attempts');
 304  
 305          $table->collapsible($collapsible);
 306      }
 307  
 308      /**
 309       * Process any submitted actions.
 310       * @param object $quiz the quiz settings.
 311       * @param object $cm the cm object for the quiz.
 312       * @param int $currentgroup the currently selected group.
 313       * @param \core\dml\sql_join $groupstudentsjoins (joins, wheres, params) the students in the current group.
 314       * @param \core\dml\sql_join $allowedjoins (joins, wheres, params) the users whose attempt this user is allowed to modify.
 315       * @param moodle_url $redirecturl where to redircet to after a successful action.
 316       */
 317      protected function process_actions($quiz, $cm, $currentgroup, \core\dml\sql_join $groupstudentsjoins,
 318              \core\dml\sql_join $allowedjoins, $redirecturl) {
 319          if (empty($currentgroup) || $this->hasgroupstudents) {
 320              if (optional_param('delete', 0, PARAM_BOOL) && confirm_sesskey()) {
 321                  if ($attemptids = optional_param_array('attemptid', array(), PARAM_INT)) {
 322                      require_capability('mod/quiz:deleteattempts', $this->context);
 323                      $this->delete_selected_attempts($quiz, $cm, $attemptids, $allowedjoins);
 324                      redirect($redirecturl);
 325                  }
 326              }
 327          }
 328      }
 329  
 330      /**
 331       * Delete the quiz attempts
 332       * @param object $quiz the quiz settings. Attempts that don't belong to
 333       * this quiz are not deleted.
 334       * @param object $cm the course_module object.
 335       * @param array $attemptids the list of attempt ids to delete.
 336       * @param \core\dml\sql_join $allowedjoins (joins, wheres, params) This list of userids that are visible in the report.
 337       *      Users can only delete attempts that they are allowed to see in the report.
 338       *      Empty means all users.
 339       */
 340      protected function delete_selected_attempts($quiz, $cm, $attemptids, \core\dml\sql_join $allowedjoins) {
 341          global $DB;
 342  
 343          foreach ($attemptids as $attemptid) {
 344              if (empty($allowedjoins->joins)) {
 345                  $sql = "SELECT quiza.*
 346                            FROM {quiz_attempts} quiza
 347                            JOIN {user} u ON u.id = quiza.userid
 348                           WHERE quiza.id = :attemptid";
 349              } else {
 350                  $sql = "SELECT quiza.*
 351                            FROM {quiz_attempts} quiza
 352                            JOIN {user} u ON u.id = quiza.userid
 353                          {$allowedjoins->joins}
 354                           WHERE {$allowedjoins->wheres} AND quiza.id = :attemptid";
 355              }
 356              $params = $allowedjoins->params + array('attemptid' => $attemptid);
 357              $attempt = $DB->get_record_sql($sql, $params, IGNORE_MULTIPLE);
 358              if (!$attempt || $attempt->quiz != $quiz->id || $attempt->preview != 0) {
 359                  // Ensure the attempt exists, belongs to this quiz and belongs to
 360                  // a student included in the report. If not skip.
 361                  continue;
 362              }
 363  
 364              // Set the course module id before calling quiz_delete_attempt().
 365              $quiz->cmid = $cm->id;
 366              quiz_delete_attempt($attempt, $quiz);
 367          }
 368      }
 369  
 370      /**
 371       * Get information about which students to show in the report.
 372       * @param object $cm the coures module.
 373       * @param object $course the course settings.
 374       * @return array with four elements:
 375       *      0 => integer the current group id (0 for none).
 376       *      1 => array ids of all the students in this course.
 377       *      2 => array ids of all the students in the current group.
 378       *      3 => array ids of all the students to show in the report. Will be the
 379       *              same as either element 1 or 2.
 380       * @deprecated since Moodle 3.2 Please use get_students_joins() instead.
 381       */
 382      protected function load_relevant_students($cm, $course = null) {
 383          $msg = 'The function load_relevant_students() is deprecated. Please use get_students_joins() instead.';
 384          throw new coding_exception($msg);
 385      }
 386  }