forked from EvokeNet/moodle-local_evokegame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
257 lines (200 loc) · 7.11 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/**
* Plugin lib.
*
* @package local_evokegame
* @copyright 2021 World Bank Group <https://worldbank.org>
* @author Willian Mano <[email protected]>
*/
use local_evokegame\util\game;
defined('MOODLE_INTERNAL') || die();
/**
* Inject the custom fields elements into all moodle module settings forms.
*
* @param moodleform $formwrapper The moodle quickforms wrapper object.
* @param MoodleQuickForm $mform The actual form object (required to modify the form).
*/
function local_evokegame_coursemodule_standard_elements($formwrapper, $mform) {
$course = $formwrapper->get_course();
if (!game::is_enabled_in_course($course->id)) {
return;
}
// Add custom fields to the form.
$handler = local_evokegame\customfield\mod_handler::create();
$handler->set_parent_context($formwrapper->get_context()); // For course handler only.
$cm = $formwrapper->get_coursemodule();
if (empty($cm)) {
$cmid = 0;
} else {
$cmid = $cm->id;
}
$handler->instance_form_definition($mform, $cmid);
// Prepare custom fields data.
$data = $formwrapper->get_current();
$oldid = $data->id;
$data->id = $cmid;
$handler->instance_form_before_set_data($data);
$data->id = $oldid;
}
/**
* Validates the custom fields elements of all moodle module settings forms.
*
* @param moodleform $formwrapper The moodle quickforms wrapper object.
* @param \stdClass $data The form data.
*/
function local_evokegame_coursemodule_validation($formwrapper, $data) {
// Add the custom fields validation.
$handler = local_evokegame\customfield\mod_handler::create();
return $handler->instance_form_validation($data, []);
}
/**
* Saves the data of custom fields elements of all moodle module settings forms.
*
* @param object $moduleinfo the module info
* @param object $course the course of the module
*/
function local_evokegame_coursemodule_edit_post_actions($moduleinfo, $course) {
// Save custom fields if there are any of them in the form.
$handler = local_evokegame\customfield\mod_handler::create();
// Make sure to set the handler's parent context first.
$context = context_module::instance($moduleinfo->coursemodule);
$handler->set_parent_context($context);
// Save the custom field data.
$moduleinfo->id = $moduleinfo->coursemodule;
$handler->instance_form_save($moduleinfo, true);
return $moduleinfo;
}
function local_evokegame_output_fragment_chooseavatar_form($args) {
GLOBAL $CFG;
$args = (object) $args;
$o = html_writer::start_div('chooseavatar-form');
for ($i = 1; $i < 33; $i++) {
$url = $CFG->wwwroot . '/local/evokegame/pix/a' . $i . '.svg';
$o .= html_writer::img($url, 'avatar', ['class' => 'avatar', 'data-id' => $i]);
}
$o .= html_writer::end_div();
return $o;
}
function local_evokegame_extend_navigation_course($navigation, $course, $context) {
if (has_capability('moodle/course:update', $context)) {
$url = new moodle_url('/local/evokegame/badge.php', array('id' => $course->id));
$navigation->add(
get_string('badgessettings', 'local_evokegame'),
$url,
navigation_node::TYPE_CUSTOM,
null,
'badgessettings',
new pix_icon('t/award', '')
);
$url = new moodle_url('/local/evokegame/coursesettings.php', array('id' => $course->id));
$navigation->add(
get_string('coursesettings', 'local_evokegame'),
$url,
navigation_node::TYPE_CUSTOM,
null,
'evokegamecoursesettings',
new pix_icon('i/course', '')
);
}
}
function local_evokegame_output_fragment_badge_form($args) {
$args = (object) $args;
$o = '';
$formdata = [];
if (!empty($args->jsonformdata)) {
$serialiseddata = json_decode($args->jsonformdata);
$formdata = (array) $serialiseddata;
}
$mform = new \local_evokegame\forms\badge($formdata, [
'id' => $serialiseddata->id,
'name' => $serialiseddata->name,
'description' => $serialiseddata->description,
'type' => $serialiseddata->type,
'highlight' => $serialiseddata->highlight,
'courseid' => $serialiseddata->courseid,
'badgeid' => $serialiseddata->badgeid,
]);
if (!empty($args->jsonformdata)) {
// If we were passed non-empty form data we want the mform to call validation functions and show errors.
$mform->is_validated();
}
ob_start();
$mform->display();
$o .= ob_get_contents();
ob_end_clean();
return $o;
}
function local_evokegame_output_fragment_badgecriteria_form($args) {
$args = (object) $args;
$o = '';
$formdata = [];
if (!empty($args->jsonformdata)) {
$serialiseddata = json_decode($args->jsonformdata);
$formdata = (array)$serialiseddata;
}
$mform = new \local_evokegame\forms\badgecriteria($formdata, [
'courseid' => $serialiseddata->courseid,
'badgeid' => $serialiseddata->badgeid,
]);
if (!empty($args->jsonformdata)) {
// If we were passed non-empty form data we want the mform to call validation functions and show errors.
$mform->is_validated();
}
ob_start();
$mform->display();
$o .= ob_get_contents();
ob_end_clean();
return $o;
}
/**
* Add callback to invoke conversion of bootstrap alert to Toastr notifications
*
* @return void
*/
function local_evokegame_before_footer() {
global $PAGE;
$PAGE->requires->js_call_amd('local_evokegame/alerttotoastr', 'init');
}
/**
* Serves the files from the local_evokegame file areas.
*
* @package local_evokegame
* @category files
*
* @param stdClass $course The course object.
* @param stdClass $cm The course module object.
* @param stdClass $context The local_evokegame's context.
* @param string $filearea The name of the file area.
* @param array $args Extra arguments (itemid, path).
* @param bool $forcedownload Whether or not force download.
* @param array $options Additional options affecting the file serving.
*/
function local_evokegame_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, $options = array()) {
if ($context->contextlevel != CONTEXT_COURSE) {
send_file_not_found();
}
require_login($course, false, $cm);
$itemid = (int)array_shift($args);
if ($itemid == 0) {
return false;
}
$relativepath = implode('/', $args);
$fullpath = "/{$context->id}/local_evokegame/$filearea/$itemid/$relativepath";
$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
send_stored_file($file, 0, 0, $forcedownload, $options);
}
function local_evokegame_moove_additional_header() {
global $PAGE;
if (isguestuser() || !isloggedin()) {
return false;
}
$context = \context_course::instance($PAGE->course->id);
if (!is_enrolled($context)) {
return false;
}
$evokegame = new \local_evokegame\output\evokegame();
return $evokegame->get_dashboardnavbar($PAGE->course, $context);
}