-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocallib.php
230 lines (208 loc) · 8.12 KB
/
locallib.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle 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.
//
// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') || die();
function adastra_page_require($page) {
// Custom CSS.
$page->requires->css(new moodle_url('/mod/' . mod_adastra\local\data\exercise_round::TABLE . '/assets/css/main.css'));
// Include highlight.js for source code syntax highlighting.
$page->requires->css(new moodle_url('https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.2.0/styles/default.min.css'));
// JS code is included as an AMD module.
}
/**
* Filter/format exercise page content so that Moodle filters are activated,
* e.g. the Moodle MathJax loader renders Latex math formulas.
* This function may be used to filter exercise descriptions and submission
* feedbacks that originate from an exercise service.
*
* @param string $content (HTML) content to filter.
* @param contex|int $ctx Moodle context object or context ID of the exercise (round).
* @return string
*/
function adastra_filter_exercise_content($content, $ctx) {
return format_text($content, FORMAT_HTML, array(
'trusted' => true, // Parameter $content is trusted and its dangerous elements are not removed, like <input>.
'noclean' => true,
'filter' => true, // Actiave Moodle filters.
'para' => false, // No extra <div> wrapping.
'context' => $ctx,
'allowid' => true, // Retain HTML element IDs.
));
}
/**
* Convert a number to a roman numeral. Number should be between 0 and 1999.
* Derived from A+ (a-plus/lib/helpers.py).
*
* @param int $number
* @return string
*/
function adastra_roman_numeral($number) {
$numbers = array(1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1);
$letters = array('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I');
$roman = '';
$lennumbers = count($numbers);
for ($i = 0; $i < $lennumbers; ++$i) {
while ($number >= $numbers[$i]) {
$roman .= $letters[$i];
$number -= $numbers[$i];
}
}
return $roman;
}
/**
* Add a learning object with its parent objects to the page navbar after the exercise round node.
*
* @param moodle_page $page
* @param int $cmid Moodle course module ID of the exercise round.
* @param mod_adastra\local\data\learning_object $exercise
* @return navigation_node The navigation node of the given exercise.
*/
function adastra_navbar_add_exercise(moodle_page $page, $cmid, mod_adastra\local\data\learning_object $exercise) {
$roundnav = $page->navigation->find($cmid, navigation_node::TYPE_ACTIVITY);
$parents = array($exercise);
$ex = $exercise;
while ($ex = $ex->get_parent_object()) {
$parents[] = $ex;
}
$previousnode = $roundnav;
// Leaf child comes last in the navbar.
for ($i = count($parents) - 1; $i >= 0; --$i) {
$previousnode = adastra_navbar_add_one_exercise($previousnode, $parents[$i]);
}
return $previousnode;
}
/**
* Add a single learning object navbar node after the given node.
*
* @param navigation_node $previousnode
* @param mod_adastra\local\data\learning_object $learningobject
* @return navigation_node
*/
function adastra_navbar_add_one_exercise(navigation_node $previousnode, mod_adastra\local\data\learning_object $learningobject) {
return $previousnode->add(
$learningobject->get_name(),
mod_adastra\local\urls\urls::exercise($learningobject, true, false),
navigation_node::TYPE_CUSTOM,
null,
'ex' . $learningobject->get_id()
);
}
/**
* Add a submission node to the navbar.
*
* @param navigation_node $prevnode
* @param mod_adastra\local\data\submission $submission
* @return navigation_node
*/
function adastra_navbar_add_submission(navigation_node $prevnode, mod_adastra\local\data\submission $submission) {
$submissionnav = $prevnode->add(
get_string('submissionnumber', mod_adastra\local\data\exercise_round::MODNAME, $submission->get_counter()),
mod_adastra\local\urls\urls::submission($submission, true),
navigation_node::TYPE_CUSTOM,
null,
'sub' . $submission->get_id()
);
return $submissionnav;
}
/**
* Return true if the current HTTP request was AJAX.
* Depends on the HTTP request header X-Requested-With.
*
* @return bool
*/
function adastra_is_ajax() {
return !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
/**
* Picks the selected language's value from |lang:value|lang:value| format text.
* Adapted from A+ (a-plus/lib/localization_syntax.py)
*
* @param string $text
* @param null|string $preferredlang The language to use. If null, the current language is used.
* @param boolean $includeall If true, all values in $text are returned in an array indexed by the lang codes.
* If $text is a string without any multilang values, it is returned directly.
* @return string|array
*/
function adastra_parse_localization(string $text, string $preferredlang = null, bool $includeall = false) {
if (strpos($text, '|') !== false) {
$currentlang = isset($preferredlang) ? $preferredlang : current_language();
$variants = explode('|', $text);
$exercisenumber = $variants[0];
$langs = array();
foreach ($variants as $variant) {
$parts = explode(':', $variant);
if (count($parts) !== 2) {
continue;
}
list($lang, $val) = $parts;
$langs[$lang] = $val;
if (!$includeall && $lang === $currentlang) {
return $exercisenumber . $val;
}
}
if ($includeall) {
return $langs;
} else if (isset($langs['en'])) {
return $exercisenumber . $langs['en'];
} else if (!empty($langs)) {
return $exercisenumber . reset($langs);
}
return $exercisenumber;
}
return $text;
}
/**
* Pick the value for the current language from the given text that
* may contain HTML span elements in the format of the Moodle multilang filter.
* (<span lang="en" class="multilang">English value</span>)
*
* @param string $text
* @param null|string $preferredlang The language to use. If null, the current language is used.
* @return string The parsed text.
*/
function adastra_parse_multilang_filter_localization(string $text, string $preferredlang = null) : string {
$offset = 0;
$pos = stripos($text, '<span', $offset);
if ($pos === false) {
// No multilang values.
return $text;
}
$start = substr($text, 0, $pos); // Substring preceding any multilang spans.
$currentlang = isset($preferredlang) ? $preferredlang : current_language();
$pattern = '/<span(?:\s+lang="(?P<lang>[a-zA-Z0-9_-]+)"|\s+class="multilang"){2}\s*>(?P<value>[^<]*)<\/span>/i';
$langs = array();
while ($pos !== false) {
$offset = $pos;
$matches = array();
if (preg_match($pattern, $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
$lang = $matches['lang'][0];
$value = $matches['value'][0];
if ($lang === $currentlang) {
return $start . $value;
}
$langs[$lang] = $value;
// Move offset over the span.
$offset = $matches[0][1] + strlen($matches[0][0]);
}
// Find the next span.
$pos = stripos($text, '<span', $offset);
}
if (isset($langs['en'])) {
return $start . $langs['en'];
} else if (!empty($langs)) {
return $start . reset($langs);
}
return $text;
}