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 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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  use mod_quiz\local\reports\attempts_report_table;
  18  use mod_quiz\quiz_attempt;
  19  
  20  /**
  21   * This is a table subclass for displaying the quiz responses report.
  22   *
  23   * @copyright 2008 Jean-Michel Vedrine
  24   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  class quiz_last_responses_table extends attempts_report_table {
  27  
  28      /**
  29       * Constructor
  30       * @param stdClass $quiz
  31       * @param context $context
  32       * @param string $qmsubselect
  33       * @param quiz_responses_options $options
  34       * @param \core\dml\sql_join $groupstudentsjoins
  35       * @param \core\dml\sql_join $studentsjoins
  36       * @param array $questions
  37       * @param moodle_url $reporturl
  38       */
  39      public function __construct($quiz, $context, $qmsubselect, quiz_responses_options $options,
  40              \core\dml\sql_join $groupstudentsjoins, \core\dml\sql_join $studentsjoins, $questions, $reporturl) {
  41          parent::__construct('mod-quiz-report-responses-report', $quiz, $context,
  42                  $qmsubselect, $options, $groupstudentsjoins, $studentsjoins, $questions, $reporturl);
  43      }
  44  
  45      public function build_table() {
  46          if (!$this->rawdata) {
  47              return;
  48          }
  49  
  50          $this->strtimeformat = str_replace(',', ' ', get_string('strftimedatetime'));
  51          parent::build_table();
  52      }
  53  
  54      public function col_sumgrades($attempt) {
  55          if ($attempt->state != quiz_attempt::FINISHED) {
  56              return '-';
  57          }
  58  
  59          $grade = quiz_rescale_grade($attempt->sumgrades, $this->quiz);
  60          if ($this->is_downloading()) {
  61              return $grade;
  62          }
  63  
  64          $gradehtml = '<a href="review.php?q=' . $this->quiz->id . '&amp;attempt=' .
  65                  $attempt->attempt . '">' . $grade . '</a>';
  66          return $gradehtml;
  67      }
  68  
  69      public function data_col($slot, $field, $attempt) {
  70          if ($attempt->usageid == 0) {
  71              return '-';
  72          }
  73  
  74          $value = $this->field_from_extra_data($attempt, $slot, $field);
  75  
  76          if (is_null($value)) {
  77              $summary = '-';
  78          } else {
  79              $summary = trim($value);
  80          }
  81  
  82          if ($this->is_downloading() && $this->is_downloading() != 'html') {
  83              return $summary;
  84          }
  85          $summary = s($summary);
  86  
  87          if ($this->is_downloading() || $field != 'responsesummary') {
  88              return $summary;
  89          }
  90  
  91          return $this->make_review_link($summary, $attempt, $slot);
  92      }
  93  
  94      /**
  95       * Column text from the extra data loaded in load_extra_data(), before html formatting etc.
  96       *
  97       * @param stdClass $attempt
  98       * @param int $slot
  99       * @param string $field
 100       * @return string
 101       */
 102      protected function field_from_extra_data($attempt, $slot, $field) {
 103          if (!isset($this->lateststeps[$attempt->usageid][$slot])) {
 104              return '-';
 105          }
 106          return $this->lateststeps[$attempt->usageid][$slot]->$field;
 107      }
 108  
 109      public function other_cols($colname, $attempt) {
 110          if (preg_match('/^question(\d+)$/', $colname, $matches)) {
 111              return $this->data_col($matches[1], 'questionsummary', $attempt);
 112  
 113          } else if (preg_match('/^response(\d+)$/', $colname, $matches)) {
 114              return $this->data_col($matches[1], 'responsesummary', $attempt);
 115  
 116          } else if (preg_match('/^right(\d+)$/', $colname, $matches)) {
 117              return $this->data_col($matches[1], 'rightanswer', $attempt);
 118  
 119          } else {
 120              return parent::other_cols($colname, $attempt);
 121          }
 122      }
 123  
 124      protected function requires_extra_data() {
 125          return true;
 126      }
 127  
 128      protected function is_latest_step_column($column) {
 129          if (preg_match('/^(?:question|response|right)([0-9]+)/', $column, $matches)) {
 130              return $matches[1];
 131          }
 132          return false;
 133      }
 134  
 135      /**
 136       * Get any fields that might be needed when sorting on date for a particular slot.
 137       * @param int    $slot  the slot for the column we want.
 138       * @param string $alias the table alias for latest state information relating to that slot.
 139       * @return string sql fragment to alias fields.
 140       */
 141      protected function get_required_latest_state_fields($slot, $alias) {
 142          global $DB;
 143          return $DB->sql_order_by_text("{$alias}.questionsummary") . " AS question{$slot},
 144                  " . $DB->sql_order_by_text("{$alias}.rightanswer") . " AS right{$slot},
 145                  " . $DB->sql_order_by_text("{$alias}.responsesummary") . " AS response{$slot}";
 146      }
 147  }