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
 
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
1441 ariadna 85
     * @deprecated since Moodle 5.0.
86
     * @todo MDL-82413 Final deprecation in Moodle 6.0.
1 efrain 87
     */
1441 ariadna 88
    #[\core\attribute\deprecated(null, since: '5.0', reason: 'This method should not be used', mdl: 'MDL-71378')]
1 efrain 89
    public function find_system_areas(): ?\moodle_recordset {
1441 ariadna 90
        \core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
1 efrain 91
        global $DB;
92
        $params = [
93
            'syscontext' => CONTEXT_SYSTEM,
94
            'coursecat' => CONTEXT_COURSECAT,
95
            'coursecat2' => CONTEXT_COURSECAT,
96
        ];
97
 
98
        $sql = "SELECT {$this->get_type()} AS type,
99
                       qc.contextid AS contextid,
100
                       {$this->get_standard_area_fields_sql()}
101
                       a.id AS itemid,
102
                       {$this->get_reftable_field_sql()}
103
                       q.id AS refid,
104
                       " . SITEID . "  as courseid,
105
                       cc.id as categoryid,
106
                       a.{$this->get_fieldname()} AS content
107
                  FROM {question} q
108
            INNER JOIN {question_answers} a
109
                    ON a.question = q.id
110
            INNER JOIN {question_versions} qv
111
                    ON qv.questionid = q.id
112
            INNER JOIN {question_bank_entries} qbe
113
                    ON qbe.id = qv.questionbankentryid
114
            INNER JOIN {question_categories} qc
115
                    ON qc.id = qbe.questioncategoryid
116
            INNER JOIN {context} ctx
117
                    ON ctx.id = qc.contextid
118
             LEFT JOIN {course_categories} cc
119
                    ON cc.id = ctx.instanceid
120
                   AND ctx.contextlevel = :coursecat
121
                 WHERE (ctx.contextlevel = :syscontext)
122
                    OR (ctx.contextlevel = :coursecat2)
123
              ORDER BY a.id";
124
 
125
        return $DB->get_recordset_sql($sql, $params);
126
    }
127
 
128
    /**
129
     * Find recordset of the course areas.
130
     *
131
     * @param int $courseid
132
     * @return \moodle_recordset
133
     */
134
    public function find_course_areas(int $courseid): ?\moodle_recordset {
135
        global $DB;
136
 
137
        $coursecontext = \context_course::instance($courseid);
138
        $param = [
139
            'module' => CONTEXT_MODULE,
140
            'coursecontextpath' => $DB->sql_like_escape($coursecontext->path) . '/%',
141
        ];
142
 
143
        $sql = "SELECT {$this->get_type()} AS type,
144
                       ctx.id AS contextid,
145
                       {$this->get_standard_area_fields_sql()}
146
                       a.id AS itemid,
147
                       {$this->get_reftable_field_sql()}
148
                       q.id AS refid,
149
                       {$courseid} AS courseid,
150
                       a.{$this->get_fieldname()} AS content
151
                  FROM {question} q
152
            INNER JOIN {question_answers} a
153
                    ON a.question = q.id
154
            INNER JOIN {question_versions} qv
155
                    ON qv.questionid = q.id
156
            INNER JOIN {question_bank_entries} qbe
157
                    ON qbe.id = qv.questionbankentryid
158
            INNER JOIN {question_categories} qc
159
                    ON qc.id = qbe.questioncategoryid
160
            INNER JOIN {context} ctx
161
                    ON ctx.id = qc.contextid
1441 ariadna 162
                 WHERE ctx.contextlevel = :module
163
                   AND {$DB->sql_like('ctx.path', ':coursecontextpath')}
1 efrain 164
              ORDER BY a.id ASC";
165
 
166
        return $DB->get_recordset_sql($sql, $param);
167
    }
168
}