Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
            'module' => CONTEXT_MODULE,
78
            'coursecontextpath' => $DB->sql_like_escape($coursecontext->path) . '/%',
79
        ];
80
 
81
        $sql = "SELECT {$this->get_type()} AS type,
82
                       ctx.id AS contextid,
83
                       {$this->get_standard_area_fields_sql()}
84
                       q.id AS itemid,
85
                       {$courseid} AS courseid,
86
                       null AS categoryid,
87
                       q.{$this->get_fieldname()} AS content
88
                  FROM {question} q
89
            INNER JOIN {question_versions} qv
90
                    ON qv.questionid = q.id
91
            INNER JOIN {question_bank_entries} qbe
92
                    ON qbe.id = qv.questionbankentryid
93
            INNER JOIN {question_categories} qc
94
                    ON qc.id = qbe.questioncategoryid
95
            INNER JOIN {context} ctx
96
                    ON ctx.id = qc.contextid
1441 ariadna 97
                 WHERE ctx.contextlevel = :module
98
                   AND {$DB->sql_like('ctx.path', ':coursecontextpath')}
1 efrain 99
              ORDER BY q.id ASC";
100
 
101
        return $DB->get_recordset_sql($sql, $param);
102
    }
103
 
104
    /**
105
     * Return an array of area objects that contain content at the site and system levels only. This would be question content from
106
     * question categories at the system context only.
107
     *
108
     * @return \moodle_recordset
1441 ariadna 109
     * @deprecated since Moodle 5.0.
110
     * @todo MDL-82413 Final deprecation in Moodle 6.0.
1 efrain 111
     */
1441 ariadna 112
    #[\core\attribute\deprecated(null, since: '5.0', reason: 'This method should not be used', mdl: 'MDL-71378')]
1 efrain 113
    public function find_system_areas(): ?\moodle_recordset {
1441 ariadna 114
        \core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
1 efrain 115
        global $DB;
116
        $params = [
117
            'syscontext' => CONTEXT_SYSTEM,
118
            'coursecat' => CONTEXT_COURSECAT,
119
            'coursecat2' => CONTEXT_COURSECAT,
120
        ];
121
 
122
        $sql = "SELECT {$this->get_type()} AS type,
123
                       qc.contextid AS contextid,
124
                       {$this->get_standard_area_fields_sql()}
125
                       q.id AS itemid,
126
                       " . SITEID . "  as courseid,
127
                       cc.id as categoryid,
128
                       q.{$this->get_fieldname()} AS content
129
                  FROM {question} q
130
            INNER JOIN {question_versions} qv
131
                    ON qv.questionid = q.id
132
            INNER JOIN {question_bank_entries} qbe
133
                    ON qbe.id = qv.questionbankentryid
134
            INNER JOIN {question_categories} qc
135
                    ON qc.id = qbe.questioncategoryid
136
            INNER JOIN {context} ctx
137
                    ON ctx.id = qc.contextid
138
             LEFT JOIN {course_categories} cc
139
                    ON cc.id = ctx.instanceid
140
                   AND ctx.contextlevel = :coursecat
141
                 WHERE (ctx.contextlevel = :syscontext)
142
                    OR (ctx.contextlevel = :coursecat2)
143
              ORDER BY q.id";
144
 
145
        return $DB->get_recordset_sql($sql, $params);
146
    }
147
 
148
    /**
149
     * Returns the moodle_url of the page to edit the error.
150
     *
151
     * @param \stdClass $componentinfo
152
     * @return \moodle_url
153
     */
154
    public static function get_edit_url(\stdClass $componentinfo): \moodle_url {
155
        $questionid = $componentinfo->itemid;
156
        // Question answers are editable on main question page.
157
        // Hence, use refid for these links.
158
        if ($componentinfo->tablename === 'question_answers') {
159
            $questionid = $componentinfo->refid;
160
        }
161
        // Default to SITEID if courseid is null, i.e. system or category level questions.
162
        $thiscourseid = ($componentinfo->courseid !== null) ? $componentinfo->courseid : SITEID;
163
        return new \moodle_url('/question/bank/editquestion/question.php', ['courseid' => $thiscourseid, 'id' => $questionid]);
164
    }
165
 
166
    /**
167
     * Determine the course and category id SQL depending on the specific context associated with question data.
168
     *
169
     * @param \core\event\base $event
170
     * @return string
171
     */
172
    protected function get_course_and_cat_sql(\core\event\base $event): string {
173
        $courseid = 'null';
174
        $catid = 'null';
175
 
176
        if ($record = self::get_course_and_category(CONTEXT_MODULE, $event->objectid)) {
1441 ariadna 177
            $courseid = $record->courseid;
1 efrain 178
        }
179
 
180
        return "
181
            {$courseid} AS courseid,
182
            {$catid} AS categoryid,
183
        ";
184
    }
185
 
186
    /**
187
     * Get the course and category data for the question.
188
     *
189
     * @param int $coursemodule
190
     * @param int $refid
191
     * @return \stdClass|false
192
     */
193
    public static function get_course_and_category($coursemodule, $refid) {
194
        global $DB;
195
 
1441 ariadna 196
        if ($coursemodule !== CONTEXT_MODULE) {
197
            debugging("Invalid contextlevel: ($coursemodule}", DEBUG_DEVELOPER);
198
        }
199
 
1 efrain 200
        $sql = 'SELECT ctx.instanceid,
201
                       cm.course as courseid,
202
                       ctx.contextlevel
203
                  FROM {question} q
204
            INNER JOIN {question_versions} qv
205
                    ON qv.questionid = q.id
206
            INNER JOIN {question_bank_entries} qbe
207
                    ON qbe.id = qv.questionbankentryid
208
            INNER JOIN {question_categories} qc
209
                    ON qc.id = qbe.questioncategoryid
210
            INNER JOIN {context} ctx
211
                    ON ctx.id = qc.contextid
1441 ariadna 212
            INNER JOIN {course_modules} cm
1 efrain 213
                    ON cm.id = ctx.instanceid
214
                   AND ctx.contextlevel = :coursemodule
215
                 WHERE q.id = :refid';
216
        $params = [
217
                'coursemodule' => $coursemodule,
218
                'refid' => $refid
219
        ];
220
        return $DB->get_record_sql($sql, $params);
221
    }
222
}