Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 400 and 401] [Versions 401 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       * constructor.
  55       * Sets the SQL for the table and the pagination.
  56       *
  57       * @param string $uniqueid
  58       * @param \question_definition $question
  59       */
  60      public function __construct(string $uniqueid, \question_definition $question) {
  61          global $PAGE;
  62          parent::__construct($uniqueid);
  63          $this->question = $question;
  64          $columns = ['modulename', 'coursename', 'attempts'];
  65          $headers = [
  66              get_string('modulename', 'qbank_usage'),
  67              get_string('coursename', 'qbank_usage'),
  68              get_string('attempts', 'qbank_usage')
  69          ];
  70          $this->is_collapsible = false;
  71          $this->no_sorting('modulename');
  72          $this->no_sorting('coursename');
  73          $this->no_sorting('attempts');
  74          $this->define_columns($columns);
  75          $this->define_headers($headers);
  76          $this->define_baseurl($PAGE->url);
  77      }
  78  
  79      public function query_db($pagesize, $useinitialsbar = true) {
  80          global $DB;
  81          if (!$this->is_downloading()) {
  82              $total = helper::get_question_entry_usage_count($this->question);
  83              $this->pagesize($pagesize, $total);
  84          }
  85  
  86          $sql = helper::question_usage_sql();
  87          $params = [$this->question->id, $this->question->questionbankentryid, 'mod_quiz', 'slot'];
  88  
  89          if (!$this->is_downloading()) {
  90              $this->rawdata = $DB->get_records_sql($sql, $params, $this->get_page_start(), $this->get_page_size());
  91          } else {
  92              $this->rawdata = $DB->get_records_sql($sql, $params);
  93          }
  94      }
  95  
  96      public function col_modulename(\stdClass $values): string {
  97          $cm = get_fast_modinfo($values->courseid)->instances['quiz'][$values->quizid];
  98  
  99          return html_writer::link(new moodle_url('/mod/quiz/view.php', ['q' => $values->quizid]), $cm->get_formatted_name());
 100      }
 101  
 102      public function col_coursename(\stdClass $values): string {
 103          $course = get_course($values->courseid);
 104          $context = context_course::instance($course->id);
 105  
 106          return html_writer::link(course_get_url($course), format_string($course->fullname, true, [
 107              'context' => $context,
 108          ]));
 109      }
 110  
 111      public function col_attempts(\stdClass $values): string {
 112          return helper::get_question_attempts_count_in_quiz($this->question->id, $values->quizid);
 113      }
 114  
 115      /**
 116       * Export this data so it can be used as the context for a mustache template/fragment.
 117       *
 118       * @return string
 119       */
 120      public function export_for_fragment(): string {
 121          ob_start();
 122          $this->out(10, true);
 123          return ob_get_clean();
 124      }
 125  
 126  }