Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 311 and 400]

   1  <?php
   2  // This file is part of the Query submission plugin
   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 tool_brickfield\local\areas\core_question;
  18  
  19  use core\event\question_created;
  20  use core\event\question_updated;
  21  use tool_brickfield\area_base;
  22  
  23  /**
  24   * Base class for various question-related areas.
  25   *
  26   * This is an abstract class so it will be skipped by manager when it finds all areas
  27   *
  28   * @package    tool_brickfield
  29   * @copyright  2020 onward: Brickfield Education Labs, www.brickfield.ie
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  abstract class base extends area_base {
  33  
  34      /**
  35       * Find recordset of the relevant areas.
  36       *
  37       * @param \core\event\base $event
  38       * @return \moodle_recordset|null
  39       */
  40      public function find_relevant_areas(\core\event\base $event): ?\moodle_recordset {
  41          global $DB;
  42          if (($event instanceof question_created) || ($event instanceof question_updated)) {
  43              $sql = "SELECT {$this->get_type()} AS type,
  44                         ctx.id AS contextid,
  45                         {$this->get_standard_area_fields_sql()}
  46                         q.id AS itemid,
  47                         {$this->get_course_and_cat_sql($event)}
  48                         q.{$this->get_fieldname()} AS content
  49                    FROM {question} q
  50              INNER JOIN {question_versions} qv
  51                      ON qv.questionid = q.id
  52              INNER JOIN {question_bank_entries} qbe
  53                      ON qbe.id = qv.questionbankentryid
  54              INNER JOIN {question_categories} qc
  55                      ON qc.id = qbe.questioncategoryid
  56              INNER JOIN {context} ctx
  57                      ON ctx.id = qc.contextid
  58                   WHERE (q.id = :refid)
  59                ORDER BY q.id";
  60  
  61              $rs = $DB->get_recordset_sql($sql, ['refid' => $event->objectid]);
  62              return $rs;
  63          }
  64          return null;
  65      }
  66  
  67      /**
  68       * Find recordset of the course areas.
  69       *
  70       * @param int $courseid
  71       * @return \moodle_recordset
  72       */
  73      public function find_course_areas(int $courseid): ?\moodle_recordset {
  74          global $DB;
  75          $coursecontext = \context_course::instance($courseid);
  76          $param = [
  77              'ctxcourse' => CONTEXT_COURSE,
  78              'courseid' => $courseid,
  79              'module' => CONTEXT_MODULE,
  80              'coursecontextpath' => $DB->sql_like_escape($coursecontext->path) . '/%',
  81          ];
  82  
  83          $sql = "SELECT {$this->get_type()} AS type,
  84                         ctx.id AS contextid,
  85                         {$this->get_standard_area_fields_sql()}
  86                         q.id AS itemid,
  87                         {$courseid} AS courseid,
  88                         null AS categoryid,
  89                         q.{$this->get_fieldname()} AS content
  90                    FROM {question} q
  91              INNER JOIN {question_versions} qv
  92                      ON qv.questionid = q.id
  93              INNER JOIN {question_bank_entries} qbe
  94                      ON qbe.id = qv.questionbankentryid
  95              INNER JOIN {question_categories} qc
  96                      ON qc.id = qbe.questioncategoryid
  97              INNER JOIN {context} ctx
  98                      ON ctx.id = qc.contextid
  99                   WHERE (ctx.contextlevel = :ctxcourse
 100                     AND ctx.id = qc.contextid
 101                     AND ctx.instanceid = :courseid)
 102                      OR (ctx.contextlevel = :module
 103                     AND {$DB->sql_like('ctx.path', ':coursecontextpath')})
 104                ORDER BY q.id ASC";
 105  
 106          return $DB->get_recordset_sql($sql, $param);
 107      }
 108  
 109      /**
 110       * Return an array of area objects that contain content at the site and system levels only. This would be question content from
 111       * question categories at the system context only.
 112       *
 113       * @return \moodle_recordset
 114       */
 115      public function find_system_areas(): ?\moodle_recordset {
 116          global $DB;
 117          $params = [
 118              'syscontext' => CONTEXT_SYSTEM,
 119              'coursecat' => CONTEXT_COURSECAT,
 120              'coursecat2' => CONTEXT_COURSECAT,
 121          ];
 122  
 123          $sql = "SELECT {$this->get_type()} AS type,
 124                         qc.contextid AS contextid,
 125                         {$this->get_standard_area_fields_sql()}
 126                         q.id AS itemid,
 127                         " . SITEID . "  as courseid,
 128                         cc.id as categoryid,
 129                         q.{$this->get_fieldname()} AS content
 130                    FROM {question} q
 131              INNER JOIN {question_versions} qv
 132                      ON qv.questionid = q.id
 133              INNER JOIN {question_bank_entries} qbe
 134                      ON qbe.id = qv.questionbankentryid
 135              INNER JOIN {question_categories} qc
 136                      ON qc.id = qbe.questioncategoryid
 137              INNER JOIN {context} ctx
 138                      ON ctx.id = qc.contextid
 139               LEFT JOIN {course_categories} cc
 140                      ON cc.id = ctx.instanceid
 141                     AND ctx.contextlevel = :coursecat
 142                   WHERE (ctx.contextlevel = :syscontext)
 143                      OR (ctx.contextlevel = :coursecat2)
 144                ORDER BY q.id";
 145  
 146          return $DB->get_recordset_sql($sql, $params);
 147      }
 148  
 149      /**
 150       * Returns the moodle_url of the page to edit the error.
 151       *
 152       * @param \stdClass $componentinfo
 153       * @return \moodle_url
 154       */
 155      public static function get_edit_url(\stdClass $componentinfo): \moodle_url {
 156          $questionid = $componentinfo->itemid;
 157          // Question answers are editable on main question page.
 158          // Hence, use refid for these links.
 159          if ($componentinfo->tablename === 'question_answers') {
 160              $questionid = $componentinfo->refid;
 161          }
 162          // Default to SITEID if courseid is null, i.e. system or category level questions.
 163          $thiscourseid = ($componentinfo->courseid !== null) ? $componentinfo->courseid : SITEID;
 164          return new \moodle_url('/question/bank/editquestion/question.php', ['courseid' => $thiscourseid, 'id' => $questionid]);
 165      }
 166  
 167      /**
 168       * Determine the course and category id SQL depending on the specific context associated with question data.
 169       *
 170       * @param \core\event\base $event
 171       * @return string
 172       */
 173      protected function get_course_and_cat_sql(\core\event\base $event): string {
 174          $courseid = 'null';
 175          $catid = 'null';
 176  
 177          if ($record = self::get_course_and_category(CONTEXT_MODULE, $event->objectid)) {
 178              if ($record->contextlevel == CONTEXT_MODULE) {
 179                  $courseid = $record->courseid;
 180              } else if ($record->contextlevel == CONTEXT_COURSE) {
 181                  $courseid = $record->instanceid;
 182              } else if ($record->contextlevel == CONTEXT_COURSECAT) {
 183                  $catid = $record->instanceid;
 184              } else if ($record->contextlevel == CONTEXT_SYSTEM) {
 185                  $courseid = 1;
 186              }
 187          }
 188  
 189          return "
 190              {$courseid} AS courseid,
 191              {$catid} AS categoryid,
 192          ";
 193      }
 194  
 195      /**
 196       * Get the course and category data for the question.
 197       *
 198       * @param int $coursemodule
 199       * @param int $refid
 200       * @return \stdClass|false
 201       */
 202      public static function get_course_and_category($coursemodule, $refid) {
 203          global $DB;
 204  
 205          $sql = 'SELECT ctx.instanceid,
 206                         cm.course as courseid,
 207                         ctx.contextlevel
 208                    FROM {question} q
 209              INNER JOIN {question_versions} qv
 210                      ON qv.questionid = q.id
 211              INNER JOIN {question_bank_entries} qbe
 212                      ON qbe.id = qv.questionbankentryid
 213              INNER JOIN {question_categories} qc
 214                      ON qc.id = qbe.questioncategoryid
 215              INNER JOIN {context} ctx
 216                      ON ctx.id = qc.contextid
 217               LEFT JOIN {course_modules} cm
 218                      ON cm.id = ctx.instanceid
 219                     AND ctx.contextlevel = :coursemodule
 220                   WHERE q.id = :refid';
 221          $params = [
 222                  'coursemodule' => $coursemodule,
 223                  'refid' => $refid
 224          ];
 225          return $DB->get_record_sql($sql, $params);
 226      }
 227  }