Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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