Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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