Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   1  <?php
   2  // This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
  16  
  17  namespace qbank_usage\tables;
  18  
  19  global $CFG;
  20  require_once($CFG->libdir.'/tablelib.php');
  21  
  22  use context_course;
  23  use html_writer;
  24  use moodle_url;
  25  use qbank_usage\helper;
  26  use table_sql;
  27  
  28  /**
  29   * Class question_usage_table.
  30   * An extension of regular Moodle table.
  31   *
  32   * @package    qbank_usage
  33   * @copyright  2021 Catalyst IT Australia Pty Ltd
  34   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class question_usage_table extends table_sql {
  38  
  39      /**
  40       * Search string.
  41       *
  42       * @var string $search
  43       */
  44      public $search = '';
  45  
  46      /**
  47       * Question id.
  48       *
  49       * @var \question_definition $question
  50       */
  51      public $question;
  52  
  53      /**
  54       * @var bool $specificversion Are we displaying the usage for a specific version, rather than all versions of the question?
  55       */
  56      protected $specificversion;
  57  
  58      /**
  59       * constructor.
  60       * Sets the SQL for the table and the pagination.
  61       *
  62       * @param string $uniqueid
  63       * @param \question_definition $question
  64       */
  65      public function __construct(string $uniqueid, \question_definition $question, bool $specificversion = false) {
  66          global $PAGE;
  67          parent::__construct($uniqueid);
  68          $this->question = $question;
  69          $columns = ['modulename', 'coursename', 'attempts'];
  70          $headers = [
  71              get_string('modulename', 'qbank_usage'),
  72              get_string('coursename', 'qbank_usage'),
  73              get_string('attempts', 'qbank_usage')
  74          ];
  75          $this->is_collapsible = false;
  76          $this->no_sorting('modulename');
  77          $this->no_sorting('coursename');
  78          $this->no_sorting('attempts');
  79          $this->define_columns($columns);
  80          $this->define_headers($headers);
  81          $this->define_baseurl($PAGE->url);
  82          $this->specificversion = $specificversion;
  83          $this->set_attribute('id', 'question_usage_table');
  84      }
  85  
  86      public function query_db($pagesize, $useinitialsbar = true) {
  87          global $DB;
  88          if (!$this->is_downloading()) {
  89              $total = helper::get_question_entry_usage_count($this->question, $this->specificversion);
  90              $this->pagesize($pagesize, $total);
  91          }
  92  
  93          $sql = helper::question_usage_sql($this->specificversion);
  94          $params = [$this->question->id, $this->question->questionbankentryid, 'mod_quiz', 'slot'];
  95          if ($this->specificversion) {
  96              $params[] = $this->question->id;
  97          }
  98  
  99          if (!$this->is_downloading()) {
 100              $this->rawdata = $DB->get_records_sql($sql, $params, $this->get_page_start(), $this->get_page_size());
 101          } else {
 102              $this->rawdata = $DB->get_records_sql($sql, $params);
 103          }
 104      }
 105  
 106      public function col_modulename(\stdClass $values): string {
 107          $cm = get_fast_modinfo($values->courseid)->instances['quiz'][$values->quizid];
 108  
 109          return html_writer::link(new moodle_url('/mod/quiz/view.php', ['q' => $values->quizid]), $cm->get_formatted_name());
 110      }
 111  
 112      public function col_coursename(\stdClass $values): string {
 113          $course = get_course($values->courseid);
 114          $context = context_course::instance($course->id);
 115  
 116          return html_writer::link(course_get_url($course), format_string($course->fullname, true, [
 117              'context' => $context,
 118          ]));
 119      }
 120  
 121      public function col_attempts(\stdClass $values): string {
 122          return helper::get_question_attempts_count_in_quiz($this->question->id, $values->quizid);
 123      }
 124  
 125      /**
 126       * Export this data so it can be used as the context for a mustache template/fragment.
 127       *
 128       * @return string
 129       */
 130      public function export_for_fragment(): string {
 131          ob_start();
 132          $this->out(10, true);
 133          return ob_get_clean();
 134      }
 135  
 136  }