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 311 and 401]

   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  
  22  /**
  23   * Base class for various question-related areas.
  24   *
  25   * This is an abstract class so it will be skipped by manager when it finds all areas.
  26   *
  27   * @package    tool_brickfield
  28   * @copyright  2020 onward: Brickfield Education Labs, www.brickfield.ie
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  abstract class answerbase extends base {
  32  
  33      /**
  34       * Get table name reference.
  35       *
  36       * @return string
  37       */
  38      public function get_ref_tablename(): string {
  39          return 'question';
  40      }
  41  
  42      /**
  43       * Find recordset of the relevant areas.
  44       *
  45       * @param \core\event\base $event
  46       * @return \moodle_recordset|null
  47       */
  48      public function find_relevant_areas(\core\event\base $event): ?\moodle_recordset {
  49          global $DB;
  50          if (($event instanceof question_created) || ($event instanceof question_updated)) {
  51              $sql = "SELECT {$this->get_type()} AS type,
  52                             ctx.id AS contextid,
  53                             {$this->get_standard_area_fields_sql()}
  54                             a.id AS itemid,
  55                             {$this->get_reftable_field_sql()}
  56                             q.id AS refid,
  57                             {$this->get_course_and_cat_sql($event)}
  58                             a.{$this->get_fieldname()} AS content
  59                        FROM {question} q
  60                  INNER JOIN {question_answers} a
  61                          ON a.question = q.id
  62                  INNER JOIN {question_versions} qv
  63                          ON qv.questionid = q.id
  64                  INNER JOIN {question_bank_entries} qbe
  65                          ON qbe.id = qv.questionbankentryid
  66                  INNER JOIN {question_categories} qc
  67                          ON qc.id = qbe.questioncategoryid
  68                  INNER JOIN {context} ctx
  69                          ON ctx.id = qc.contextid
  70                       WHERE (q.id = :refid)
  71                    ORDER BY a.id";
  72  
  73              $rs = $DB->get_recordset_sql($sql, ['refid' => $event->objectid]);
  74              return $rs;
  75  
  76          }
  77          return null;
  78      }
  79  
  80      /**
  81       * Return an array of area objects that contain content at the site and system levels only. This would be question content from
  82       * question categories at the system context, or course category context.
  83       *
  84       * @return mixed
  85       */
  86      public function find_system_areas(): ?\moodle_recordset {
  87          global $DB;
  88          $params = [
  89              'syscontext' => CONTEXT_SYSTEM,
  90              'coursecat' => CONTEXT_COURSECAT,
  91              'coursecat2' => CONTEXT_COURSECAT,
  92          ];
  93  
  94          $sql = "SELECT {$this->get_type()} AS type,
  95                         qc.contextid AS contextid,
  96                         {$this->get_standard_area_fields_sql()}
  97                         a.id AS itemid,
  98                         {$this->get_reftable_field_sql()}
  99                         q.id AS refid,
 100                         " . SITEID . "  as courseid,
 101                         cc.id as categoryid,
 102                         a.{$this->get_fieldname()} AS content
 103                    FROM {question} q
 104              INNER JOIN {question_answers} a
 105                      ON a.question = q.id
 106              INNER JOIN {question_versions} qv
 107                      ON qv.questionid = q.id
 108              INNER JOIN {question_bank_entries} qbe
 109                      ON qbe.id = qv.questionbankentryid
 110              INNER JOIN {question_categories} qc
 111                      ON qc.id = qbe.questioncategoryid
 112              INNER JOIN {context} ctx
 113                      ON ctx.id = qc.contextid
 114               LEFT JOIN {course_categories} cc
 115                      ON cc.id = ctx.instanceid
 116                     AND ctx.contextlevel = :coursecat
 117                   WHERE (ctx.contextlevel = :syscontext)
 118                      OR (ctx.contextlevel = :coursecat2)
 119                ORDER BY a.id";
 120  
 121          return $DB->get_recordset_sql($sql, $params);
 122      }
 123  
 124      /**
 125       * Find recordset of the course areas.
 126       *
 127       * @param int $courseid
 128       * @return \moodle_recordset
 129       */
 130      public function find_course_areas(int $courseid): ?\moodle_recordset {
 131          global $DB;
 132  
 133          $coursecontext = \context_course::instance($courseid);
 134          $param = [
 135              'ctxcourse' => CONTEXT_COURSE,
 136              'courseid' => $courseid,
 137              'module' => CONTEXT_MODULE,
 138              'coursecontextpath' => $DB->sql_like_escape($coursecontext->path) . '/%',
 139          ];
 140  
 141          $sql = "SELECT {$this->get_type()} AS type,
 142                         ctx.id AS contextid,
 143                         {$this->get_standard_area_fields_sql()}
 144                         a.id AS itemid,
 145                         {$this->get_reftable_field_sql()}
 146                         q.id AS refid,
 147                         {$courseid} AS courseid,
 148                         a.{$this->get_fieldname()} AS content
 149                    FROM {question} q
 150              INNER JOIN {question_answers} a
 151                      ON a.question = q.id
 152              INNER JOIN {question_versions} qv
 153                      ON qv.questionid = q.id
 154              INNER JOIN {question_bank_entries} qbe
 155                      ON qbe.id = qv.questionbankentryid
 156              INNER JOIN {question_categories} qc
 157                      ON qc.id = qbe.questioncategoryid
 158              INNER JOIN {context} ctx
 159                      ON ctx.id = qc.contextid
 160                   WHERE (ctx.contextlevel = :ctxcourse
 161                     AND ctx.id = qc.contextid
 162                     AND ctx.instanceid = :courseid)
 163                      OR (ctx.contextlevel = :module
 164                     AND {$DB->sql_like('ctx.path', ':coursecontextpath')})
 165                ORDER BY a.id ASC";
 166  
 167          return $DB->get_recordset_sql($sql, $param);
 168      }
 169  }