-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.php
More file actions
executable file
·227 lines (192 loc) · 7.26 KB
/
Copy pathlib.php
File metadata and controls
executable file
·227 lines (192 loc) · 7.26 KB
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
<?php
// Voting session types
define('BLOCK_MISSIONMAP_VOTINGTYPE_ALL', 1);
define('BLOCK_MISSIONMAP_VOTINGTYPE_GROUPS', 2);
// Voting algorithms
define('BLOCK_MISSIONMAP_VOTINGAL_MAJORITY', 1);
define('BLOCK_MISSIONMAP_VOTINGAL_TIMEBOUND', 2);
define('BLOCK_MISSIONMAP_VOTINGAL_THRESHOLD', 3);
define('BLOCK_MISSIONMAP_VOTINGAL_RANDOM', 4);
// Voting tiebreaker types
define('BLOCK_MISSIONMAP_TIEBREAKER_RANDOM', 1);
define('BLOCK_MISSIONMAP_TIEBREAKER_SECONDROUND', 2);
define('BLOCK_MISSIONMAP_TIEBREAKER_MINERVA', 3);
// Voting thresholds
define('BLOCK_MISSIONMAP_THRESHOLD_25', 25);
define('BLOCK_MISSIONMAP_THRESHOLD_50', 50);
define('BLOCK_MISSIONMAP_THRESHOLD_75', 75);
define('BLOCK_MISSIONMAP_THRESHOLD_90', 90);
// Voting result options
define('BLOCK_MISSIONMAP_OPTION_URL', 1);
define('BLOCK_MISSIONMAP_OPTION_SECTION', 2);
define('BLOCK_MISSIONMAP_OPTION_SUBLEVEL', 3);
function block_mission_map_output_fragment_chapter_form($args)
{
$args = (object) $args;
$o = '';
$formdata = [];
if (!empty($args->jsonformdata)) {
$serialiseddata = json_decode($args->jsonformdata);
parse_str($serialiseddata, $formdata);
}
$mform = new \block_mission_map\local\forms\chapter_form(
$formdata,
[
'id' => $serialiseddata->id,
'name' => $serialiseddata->name,
'map_image' => $serialiseddata->map_image,
'has_lock' => $serialiseddata->has_lock,
'unlocking_date' => $serialiseddata->unlocking_date,
'blockid' => $serialiseddata->blockid,
'courseid' => $serialiseddata->courseid
]
);
if (!empty($args->jsonformdata)) {
$mform->is_validated();
}
ob_start();
$mform->display();
$o .= ob_get_contents();
ob_end_clean();
return $o;
}
function block_mission_map_output_fragment_level_form($args)
{
$args = (object) $args;
$o = '';
$formdata = [];
if (!empty($args->jsonformdata)) {
$serialiseddata = json_decode($args->jsonformdata);
parse_str($serialiseddata, $formdata);
}
$mform = new \block_mission_map\local\forms\level_form(
$formdata,
[
'id' => $serialiseddata->id,
'chapterid' => $serialiseddata->chapterid,
'name' => $serialiseddata->name,
'description' => $serialiseddata->description,
'sections' => $serialiseddata->sections,
'type' => $serialiseddata->url,
'courseid' => $serialiseddata->courseid,
'sectionid' => $serialiseddata->sectionid,
'url' => $serialiseddata->url
]
);
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 block_mission_map_output_fragment_sublevel_form($args)
{
$args = (object) $args;
$o = '';
$formdata = [];
if (!empty($args->jsonformdata)) {
$serialiseddata = json_decode($args->jsonformdata);
parse_str($serialiseddata, $formdata);
}
$mform = new \block_mission_map\local\forms\sublevel_form(
$formdata,
[
'id' => $serialiseddata->id,
'chapterid' => $serialiseddata->chapterid,
'parentlevelid' => $serialiseddata->parentlevelid,
'name' => $serialiseddata->name,
'url' => $serialiseddata->url,
'type' => $serialiseddata->url,
'courseid' => $serialiseddata->courseid,
'sectionid' => $serialiseddata->sectionid
]
);
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;
}
/**
* Form for editing Mission Map block instances.
*/
function block_mission_map_pluginfile($course, $birecord_or_cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
global $DB, $CFG, $USER;
if ($context->contextlevel != CONTEXT_BLOCK) {
send_file_not_found();
}
// If block is in course context, then check if user has capability to access course.
if ($context->get_course_context(false)) {
require_course_login($course);
} else if ($CFG->forcelogin) {
require_login();
} else {
// Get parent context and see if user have proper permission.
$parentcontext = $context->get_parent_context();
if ($parentcontext->contextlevel === CONTEXT_COURSECAT) {
// Check if category is visible and user can view this category.
if (!core_course_category::get($parentcontext->instanceid, IGNORE_MISSING)) {
send_file_not_found();
}
} else if ($parentcontext->contextlevel === CONTEXT_USER && $parentcontext->instanceid != $USER->id) {
// The block is in the context of a user, it is only visible to the user who it belongs to.
send_file_not_found();
}
// At this point there is no way to check SYSTEM context, so ignoring it.
}
$allowedfileareas = ['content', 'map_bg_1', 'map_bg_2', 'map_bg_3', 'map_bg_4'];
if (!in_array($filearea, $allowedfileareas, true)) {
send_file_not_found();
}
$fs = get_file_storage();
$filename = array_pop($args);
$pathinfo = block_mission_map_get_path_from_pluginfile($filearea, $args);
$filepath = $pathinfo['filepath'];
$itemid = $pathinfo['itemid'];
if (!$file = $fs->get_file($context->id, 'block_mission_map', $filearea, $itemid, $filepath, $filename) or $file->is_directory()) {
send_file_not_found();
}
if ($parentcontext = context::instance_by_id($birecord_or_cm->parentcontextid, IGNORE_MISSING)) {
if ($parentcontext->contextlevel == CONTEXT_USER) {
// force download on all personal pages including /my/
//because we do not have reliable way to find out from where this is used
$forcedownload = true;
}
} else {
// weird, there should be parent context, better force dowload then
$forcedownload = true;
}
// NOTE: it woudl be nice to have file revisions here, for now rely on standard file lifetime,
// do not lower it because the files are dispalyed very often.
\core\session\manager::write_close();
send_stored_file($file, null, 0, $forcedownload, $options);
}
/**
* Given an array with a file path, it returns the itemid and the filepath for the defined filearea.
*
* @param string $filearea The filearea.
* @param array $args The path (the part after the filearea and before the filename).
* @return array The itemid and the filepath inside the $args path, for the defined filearea.
*/
function block_mission_map_get_path_from_pluginfile(string $filearea, array $args) : array {
// First argument is itemid.
$itemid = (int)array_shift($args);
// Get the filepath.
if (empty($args)) {
$filepath = '/';
} else {
$filepath = '/' . implode('/', $args) . '/';
}
return [
'itemid' => $itemid,
'filepath' => $filepath,
];
}