Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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       * @return string
  36       */
  37      public function get_ref_tablename(): string {
  38          return 'question';
  39      }
  40  
  41      /**
  42       * Find recordset of the relevant areas.
  43       * @param \core\event\base $event
  44       * @return \moodle_recordset|null
  45       * @throws \coding_exception
  46       * @throws \dml_exception
  47       */
  48      public function find_relevant_areas(\core\event\base $event): ?\moodle_recordset {
  49          global $DB;
  50  
  51          if (($event instanceof question_created) || ($event instanceof question_updated)) {
  52              $rs = $DB->get_recordset_sql(
  53                  "SELECT {$this->get_type()} AS type,
  54                  ctx.id AS contextid,
  55                  {$this->get_standard_area_fields_sql()}
  56                  a.id AS itemid,
  57                  {$this->get_reftable_field_sql()}
  58                  t.id AS refid,
  59                  {$this->get_course_and_cat_sql($event)}
  60                  a.{$this->get_fieldname()} AS content
  61              FROM {question} t
  62              INNER JOIN {question_answers} a ON a.question = t.id
  63              INNER JOIN {question_categories} qc ON qc.id = t.category
  64              INNER JOIN {context} ctx ON ctx.id = qc.contextid
  65              WHERE (t.id = :refid)
  66              ORDER BY a.id",
  67                  [
  68                      'refid' => $event->objectid,
  69                  ]);
  70              return $rs;
  71          }
  72          return null;
  73      }
  74  
  75      /**
  76       * Return an array of area objects that contain content at the site and system levels only. This would be question content from
  77       * question categories at the system context, or course category context.
  78       * @return mixed
  79       * @throws \dml_exception
  80       */
  81      public function find_system_areas(): ?\moodle_recordset {
  82          global $DB;
  83          $select = 'SELECT ' . $this->get_type() . ' AS type, qc.contextid AS contextid, ' . $this->get_standard_area_fields_sql() .
  84              ' a.id AS itemid, ' . $this->get_reftable_field_sql() . 't.id AS refid, '.
  85              SITEID . ' as courseid, cc.id as categoryid, a.'.$this->get_fieldname().' AS content ';
  86          $from = 'FROM {question} t ' .
  87              'INNER JOIN {question_answers} a ON a.question = t.id ' .
  88              'INNER JOIN {question_categories} qc ON qc.id = t.category ' .
  89              'INNER JOIN {context} ctx ON ctx.id = qc.contextid ' .
  90              'LEFT JOIN {course_categories} cc ON cc.id = ctx.instanceid AND ctx.contextlevel = :coursecat ';
  91          $where = 'WHERE (ctx.contextlevel = :syscontext) OR (ctx.contextlevel = :coursecat2) ';
  92          $order = 'ORDER BY a.id';
  93          $params = [
  94              'syscontext' => CONTEXT_SYSTEM,
  95              'coursecat' => CONTEXT_COURSECAT,
  96              'coursecat2' => CONTEXT_COURSECAT,
  97          ];
  98  
  99          return $DB->get_recordset_sql($select . $from . $where . $order, $params);
 100      }
 101  
 102      /**
 103       * Find recordset of the course areas.
 104       * @param int $courseid
 105       * @return \moodle_recordset
 106       * @throws \coding_exception
 107       * @throws \dml_exception
 108       */
 109      public function find_course_areas(int $courseid): ?\moodle_recordset {
 110          global $DB;
 111  
 112          $coursecontext = \context_course::instance($courseid);
 113          return $DB->get_recordset_sql(
 114              "SELECT {$this->get_type()} AS type,
 115                  ctx.id AS contextid,
 116                  {$this->get_standard_area_fields_sql()}
 117                  a.id AS itemid,
 118                  {$this->get_reftable_field_sql()}
 119                  t.id AS refid,
 120                  {$courseid} AS courseid,
 121                  a.{$this->get_fieldname()} AS content
 122              FROM {question} t
 123              INNER JOIN {question_answers} a ON a.question = t.id
 124              INNER JOIN {question_categories} qc ON qc.id = t.category
 125              INNER JOIN {context} ctx ON ctx.id = qc.contextid
 126              WHERE (ctx.contextlevel = :ctxcourse AND ctx.id = qc.contextid AND ctx.instanceid = :courseid) OR
 127                  (ctx.contextlevel = :module AND {$DB->sql_like('ctx.path', ':coursecontextpath')})
 128              ORDER BY a.id",
 129              ['ctxcourse' => CONTEXT_COURSE,
 130               'courseid' => $courseid,
 131               'module' => CONTEXT_MODULE,
 132               'coursecontextpath' => $DB->sql_like_escape($coursecontext->path) . '/%',
 133              ]);
 134      }
 135  }