-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.php
180 lines (174 loc) · 7.62 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
// This file is part of Stack - http://stack.maths.ed.ac.uk/
//
// Stack is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Stack is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Stack. If not, see <http://www.gnu.org/licenses/>.
/**
* Function library for gitsync plugin
*
* @package qbank_gitsync
* @copyright 2023 The University of Edinburgh
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Convert a string into an array of category names.
*
* Each category name is cleaned by a call to clean_param(, PARAM_TEXT),
* which matches the cleaning in question/bank/managecategories/category_form.php.
*
* @param string|null $path
* @return array of category names.
*/
function split_category_path(?string $path): array {
$rawnames = preg_split('~(?<!/)/(?!/)~', $path);
$names = [];
foreach ($rawnames as $rawname) {
$names[] = clean_param(trim(str_replace('//', '/', $rawname)), PARAM_TEXT);
}
return $names;
}
/**
* Return the correct context given a valid selection of identifying information
*
* Required parameters are dependent on the supplied context level.
* System: nothing.
* Course category: category name.
* Course: course name.
* Module: course name and module name.
*
* If the id of the category, course or module is known this can be supplied instead.
*
* @param int $contextlevel
* @param string|null $categoryname
* @param string|null $coursename
* @param string|null $modulename
* @param string|null $instanceid
* @return object
*/
function get_context(int $contextlevel, ?string $categoryname = null,
?string $coursename = null, ?string $modulename = null, ?string $instanceid = null):object {
global $DB;
if ($instanceid === '') {
$instanceid = null;
}
$result = new \stdClass();
$result->categoryname = null;
$result->coursename = null;
$result->modulename = null;
$result->instanceid = null;
switch ($contextlevel) {
case \CONTEXT_SYSTEM:
$result->contextlevel = 'system';
$result->context = context_system::instance();
return $result;
case \CONTEXT_COURSECAT:
if (is_null($instanceid)) {
$instanceid = $DB->get_field('course_categories', 'id', ['name' => $categoryname], $strictness = MUST_EXIST);
$result->categoryname = $categoryname;
} else {
$result->categoryname = $DB->get_field('course_categories', 'name',
['id' => $instanceid], $strictness = MUST_EXIST);
}
$result->contextlevel = 'coursecategory';
$result->context = context_coursecat::instance($instanceid);
$result->instanceid = $instanceid;
return $result;
case \CONTEXT_COURSE:
if (is_null($instanceid)) {
$instanceid = $DB->get_field('course', 'id', ['fullname' => $coursename], $strictness = MUST_EXIST);
$result->coursename = $coursename;
} else {
$result->coursename = $DB->get_field('course', 'fullname', ['id' => $instanceid], $strictness = MUST_EXIST);
}
$result->contextlevel = 'course';
$result->context = context_course::instance($instanceid);
$result->instanceid = $instanceid;
return $result;
case \CONTEXT_MODULE:
if (is_null($instanceid)) {
// Assuming here that the module is a quiz.
$instanceid = $DB->get_field_sql("
SELECT cm.id
FROM {course_modules} cm
JOIN {quiz} q ON q.course = cm.course AND q.id = cm.instance
JOIN {course} c ON c.id = cm.course
JOIN {modules} m ON m.id = cm.module
WHERE c.fullname = :coursename
AND q.name = :quizname
AND m.name = 'quiz'",
['coursename' => $coursename, 'quizname' => $modulename], $strictness = MUST_EXIST);
$result->coursename = $coursename;
$result->modulename = $modulename;
} else {
$instancedata = $DB->get_record_sql("
SELECT c.fullname as coursename, q.name as modulename
FROM {course_modules} cm
JOIN {quiz} q ON q.course = cm.course AND q.id = cm.instance
JOIN {course} c ON c.id = cm.course
JOIN {modules} m ON m.id = cm.module
WHERE cm.id = :instanceid
AND m.name = 'quiz'",
['instanceid' => $instanceid], $strictness = MUST_EXIST);
$result->coursename = $instancedata->coursename;
$result->modulename = $instancedata->modulename;
}
$result->contextlevel = 'module';
$result->context = context_module::instance($instanceid);
$result->instanceid = $instanceid;
return $result;
default:
throw new moodle_exception('contexterror', 'qbank_gitsync', null, $contextlevel);;
}
}
/**
* Return information on the latest version of a question given its questionbankentryid.
*
* @param string $questionbankentryid
* @return stdClass Contains properties of question such as version and context
*/
function get_question_data(string $questionbankentryid):stdClass {
global $DB;
$questiondata = $DB->get_record_sql("
SELECT qc.contextid as contextid, c.contextlevel as contextlevel,
q.id as questionid, c.instanceid as instanceid,
qc.id as categoryid, qv.version as version
FROM {question_categories} qc
JOIN {question_bank_entries} qbe ON qc.id = qbe.questioncategoryid
JOIN {question_versions} qv ON qbe.id = qv.questionbankentryid
JOIN {question} q ON qv.questionid = q.id
JOIN {context} c on qc.contextid = c.id
WHERE qbe.id = :questionbankentryid1
AND qv.version = (SELECT MAX(version) FROM {question_versions} WHERE questionbankentryid = :questionbankentryid2)",
['questionbankentryid1' => $questionbankentryid, 'questionbankentryid2' => $questionbankentryid],
MUST_EXIST);
return $questiondata;
}
/**
* Return information on the latest version of a question given its questionbankentryid.
*
* @param string $questionbankentryid
* @return stdClass Contains properties of question such as version and context
*/
function get_minimal_question_data(string $questionbankentryid):stdClass {
global $DB;
$questiondata = $DB->get_record_sql("
SELECT q.id as questionid, q.name as name, qv.version as version, qv.status as status
FROM {question} q
JOIN {question_versions} qv ON qv.questionid = q.id
JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
WHERE qbe.id = :questionbankentryid1
AND qv.version = (SELECT MAX(version) FROM {question_versions} WHERE questionbankentryid = :questionbankentryid2)",
['questionbankentryid1' => $questionbankentryid, 'questionbankentryid2' => $questionbankentryid],
MUST_EXIST);
return $questiondata;
}