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 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 * @param \core\event\base $event 37 * @return \moodle_recordset|null 38 * @throws \coding_exception 39 * @throws \dml_exception 40 */ 41 public function find_relevant_areas(\core\event\base $event): ?\moodle_recordset { 42 global $DB; 43 44 if (($event instanceof question_created) || ($event instanceof question_updated)) { 45 $rs = $DB->get_recordset_sql( 46 "SELECT {$this->get_type()} AS type, 47 ctx.id AS contextid, 48 {$this->get_standard_area_fields_sql()} 49 t.id AS itemid, 50 {$this->get_course_and_cat_sql($event)} 51 t.{$this->get_fieldname()} AS content 52 FROM {question} t 53 INNER JOIN {question_categories} qc ON qc.id = t.category 54 INNER JOIN {context} ctx ON ctx.id = qc.contextid 55 WHERE (t.id = :refid) 56 ORDER BY t.id", 57 [ 58 'refid' => $event->objectid, 59 ]); 60 return $rs; 61 } 62 return null; 63 } 64 65 /** 66 * Find recordset of the course areas. 67 * @param int $courseid 68 * @return \moodle_recordset 69 * @throws \coding_exception 70 * @throws \dml_exception 71 */ 72 public function find_course_areas(int $courseid): ?\moodle_recordset { 73 global $DB; 74 75 $coursecontext = \context_course::instance($courseid); 76 return $DB->get_recordset_sql( 77 "SELECT {$this->get_type()} AS type, 78 ctx.id AS contextid, 79 {$this->get_standard_area_fields_sql()} 80 t.id AS itemid, 81 {$courseid} AS courseid, 82 null AS categoryid, 83 t.{$this->get_fieldname()} AS content 84 FROM {question} t 85 INNER JOIN {question_categories} qc ON qc.id = t.category 86 INNER JOIN {context} ctx ON ctx.id = qc.contextid 87 WHERE (ctx.contextlevel = :ctxcourse AND ctx.id = qc.contextid AND ctx.instanceid = :courseid) OR 88 (ctx.contextlevel = :module AND {$DB->sql_like('ctx.path', ':coursecontextpath')}) 89 ORDER BY t.id ASC", 90 [ 91 'ctxcourse' => CONTEXT_COURSE, 92 'courseid' => $courseid, 93 'module' => CONTEXT_MODULE, 94 'coursecontextpath' => $DB->sql_like_escape($coursecontext->path) . '/%', 95 ]); 96 } 97 98 /** 99 * Return an array of area objects that contain content at the site and system levels only. This would be question content from 100 * question categories at the system context only. 101 * @return \moodle_recordset 102 * @throws \dml_exception 103 */ 104 public function find_system_areas(): ?\moodle_recordset { 105 global $DB; 106 107 $select = 'SELECT ' . $this->get_type() . ' AS type, qc.contextid AS contextid, ' . $this->get_standard_area_fields_sql() . 108 ' t.id AS itemid, ' . SITEID . ' as courseid, cc.id as categoryid,' . 109 ' t.'.$this->get_fieldname().' AS content '; 110 $from = 'FROM {question} t ' . 111 'INNER JOIN {question_categories} qc ON qc.id = t.category ' . 112 'INNER JOIN {context} ctx ON ctx.id = qc.contextid ' . 113 'LEFT JOIN {course_categories} cc ON cc.id = ctx.instanceid AND ctx.contextlevel = :coursecat '; 114 $where = 'WHERE (ctx.contextlevel = :syscontext) OR (ctx.contextlevel = :coursecat2) '; 115 $order = 'ORDER BY t.id'; 116 $params = [ 117 'syscontext' => CONTEXT_SYSTEM, 118 'coursecat' => CONTEXT_COURSECAT, 119 'coursecat2' => CONTEXT_COURSECAT, 120 ]; 121 122 return $DB->get_recordset_sql($select . $from . $where . $order, $params); 123 } 124 125 /** 126 * Returns the moodle_url of the page to edit the error. 127 * @param \stdClass $componentinfo 128 * @return \moodle_url 129 * @throws \moodle_exception 130 */ 131 public static function get_edit_url(\stdClass $componentinfo): \moodle_url { 132 $questionid = $componentinfo->itemid; 133 // Question answers are editable on main question page 134 // Hence, use refid for these links. 135 if ($componentinfo->tablename == 'question_answers') { 136 $questionid = $componentinfo->refid; 137 } 138 // Default to SITEID if courseid is null, i.e. system or category level questions. 139 $thiscourseid = ($componentinfo->courseid !== null) ? $componentinfo->courseid : SITEID; 140 return new \moodle_url('/question/question.php', ['courseid' => $thiscourseid, 'id' => $questionid]); 141 } 142 143 /** 144 * Determine the course and category id SQL depending on the specific context associated with question data. 145 * @param \core\event\base $event 146 * @return string 147 * @throws \dml_exception 148 */ 149 protected function get_course_and_cat_sql(\core\event\base $event): string { 150 global $DB; 151 152 $courseid = 'null'; 153 $catid = 'null'; 154 155 $sql = " 156 SELECT ctx.instanceid, cm.course as courseid, ctx.contextlevel 157 FROM {question} q 158 INNER JOIN {question_categories} qc ON qc.id = q.category 159 INNER JOIN {context} ctx ON ctx.id = qc.contextid 160 LEFT JOIN {course_modules} cm ON cm.id = ctx.instanceid AND ctx.contextlevel = :coursemodule 161 WHERE q.id = :refid 162 "; 163 $params = [ 164 'coursemodule' => CONTEXT_MODULE, 165 'refid' => $event->objectid, 166 ]; 167 168 if ($record = $DB->get_record_sql($sql, $params)) { 169 if ($record->contextlevel == CONTEXT_MODULE) { 170 $courseid = $record->courseid; 171 } else if ($record->contextlevel == CONTEXT_COURSE) { 172 $courseid = $record->instanceid; 173 } else if ($record->contextlevel == CONTEXT_COURSECAT) { 174 $catid = $record->instanceid; 175 } else if ($record->contextlevel == CONTEXT_SYSTEM) { 176 $courseid = 1; 177 } 178 } 179 180 return " 181 {$courseid} AS courseid, 182 {$catid} AS categoryid, 183 "; 184 } 185 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body