-
Notifications
You must be signed in to change notification settings - Fork 3
/
content.php
371 lines (321 loc) · 15.2 KB
/
content.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
// This file is part of Moodle - http://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/>.
/**
* Prints a particular instance of openstudio
*
* You can have a rather longer description of the file as well,
* if you like, and it can span multiple lines.
*
* @package mod_openstudio
* @copyright 2017 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_openstudio\local\api\content;
use mod_openstudio\local\api\lock;
use mod_openstudio\local\util;
use mod_openstudio\local\api\contentversion;
use mod_openstudio\local\api\flags;
use mod_openstudio\local\api\comments;
use mod_openstudio\local\api\user;
use mod_openstudio\local\api\tracking;
use mod_openstudio\local\forms\comment_form;
use mod_openstudio\local\renderer_utils;
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once(dirname(__FILE__).'/lib.php');
// Course_module ID.
$id = optional_param('id', 0, PARAM_INT);
// Folder id.
$folderid = optional_param('folderid', 0, PARAM_INT);
// Content id.
$contentid = optional_param('sid', 0, PARAM_INT);
// User id.
$iscontentversion = optional_param('contentversion', 0, PARAM_INT); // View content version.
$restoreversion = optional_param('restoreversion', 0, PARAM_INT); // Restore content version.
$archiveversion = optional_param('archiveversion', 0, PARAM_INT); // Archive content version.
$userid = optional_param('vuid', $USER->id, PARAM_INT);
$coursedata = util::render_page_init($id, array('mod/openstudio:view'));
$cm = $coursedata->cm;
$cmid = $cm->id;
$cminstance = $coursedata->cminstance;
$course = $coursedata->course;
$mcontext = $coursedata->mcontext;
$permissions = $coursedata->permissions;
$theme = $coursedata->theme;
require_login($course, true, $cm);
require_capability('mod/openstudio:view', $mcontext);
// Terms and conditions check.
util::honesty_check($id);
$returnurliferror = new moodle_url('/mod/openstudio/view.php', array('id' => $cm->id));
// Restore content version.
if ($restoreversion) {
$contentversiondata = contentversion::get($contentid, $USER->id);
if ($contentversiondata != false) {
$contentrestoredata = content::get($contentversiondata->contentid);
}
if ($contentrestoredata) {
$actionallowed = ($contentrestoredata->userid == $USER->id) && $permissions->addcontent && $permissions->versioningon;
$actionallowed = $actionallowed || $permissions->managecontent;
if ($actionallowed) {
$restoredata = content::restore_version($userid, $contentid, $cm);
// If restore success will return restore content id.
// The process for view content detail will affect with content restore.
// Else process for view content version detail.
if ($restoredata) {
$contentid = $restoredata->id;
$redirectorurl = new moodle_url('/mod/openstudio/content.php',
array('id' => $cm->id, 'sid' => $contentid, 'vuid' => $restoredata->userid, 'folderid' => $folderid));
return redirect($redirectorurl);
}
}
}
}
// Get content and content version data.
$showdeletedcontentversions = false;
// Handle content and content version.
if ($iscontentversion) {
$contentdata = contentversion::get($contentid, $USER->id, $showdeletedcontentversions);
} else {
$contentandversions = contentversion::get_content_and_versions($contentid, $USER->id, $showdeletedcontentversions);
$contentdata = lock::determine_lock_status($contentandversions->contentdata);
}
if ($contentdata === false) {
return redirect($returnurliferror->out(false));
}
// Check the viewing user has permission to view content.
if (!util::can_read_content($cminstance, $permissions, $contentdata, $folderid)) {
throw new \moodle_exception('errornopermissiontoviewcontent', 'openstudio', $returnurliferror->out(false));
}
// Archive content version.
if ($archiveversion) {
$actionallowed = ($contentdata->userid == $userid) && $permissions->addcontent;
$actionallowed = $actionallowed || $permissions->managecontent;
if ($actionallowed) {
if ($permissions->versioningon) {
content::delete($userid, $contentid, $cminstance->versioning, $cm);
} else {
content::empty_content($userid, $contentid, true, $cminstance->versioning, $cm);
if (($contentdata->levelid == 0) && ($contentdata->levelcontainer == 0)) {
$redirectorurl = new moodle_url('/mod/openstudio/view.php',
array('id' => $cm->id, 'vid' => content::VISIBILITY_PRIVATE_PINBOARD, 'fblock' => -1));
return redirect($redirectorurl);
}
}
$redirectorurl = new moodle_url('/mod/openstudio/content.php',
array('id' => $cm->id, 'sid' => $contentdata->id, 'vuid' => $contentdata->userid, 'folderid' => $folderid));
return redirect($redirectorurl);
}
}
$contentdata->iscontentversion = false;
if ($iscontentversion) {
$contentdata->iscontentversion = true;
$contentcurrenteversionurl = new moodle_url('/mod/openstudio/content.php',
array('id' => $cm->id, 'sid' => $contentdata->contentid, 'vuid' => $contentdata->userid, 'folderid' => $folderid));
$contentdata->contentcurrenteversionurl = $contentcurrenteversionurl->out(false);
$contentdata->contentpreviousversionlink = '';
$contentdata->contentnextversionlink = '';
$contentdata->contentallversionlink = '';
if ($contentdata->numberofversions > 1) {
if ($contentdata->versionnumber > 1) {
$contentpreviousversionlink = new moodle_url('/mod/openstudio/content.php', array(
'id' => $cmid,
'sid' => $contentdata->previousversionid,
'vuid' => $contentdata->userid,
'folderid' => $folderid,
'contentversion' => 1
));
$contentdata->contentpreviousversionlink = $contentpreviousversionlink->out(false);
}
if ($contentdata->versionnumber < $contentdata->numberofversions) {
$contentnextversionlink = new moodle_url('/mod/openstudio/content.php', array(
'id' => $cmid,
'sid' => $contentdata->nextversionid,
'vuid' => $contentdata->userid,
'folderid' => $folderid,
'contentversion' => 1
));
$contentdata->contentnextversionlink = $contentnextversionlink->out(false);
}
$contentallversionlink = new moodle_url('/mod/openstudio/view.php', array(
'id' => $cmid,
'sid' => $contentdata->contentid,
'vid' => content::VISIBILITY_PRIVATE_PINBOARD,
'contentversion' => 1
));
$contentdata->contentallversionlink = $contentallversionlink->out(false);
}
$contentdataname = get_string('contentversiontitle', 'openstudio',
array('versionnumber' => $contentdata->versionnumber, 'numberofversions' => $contentdata->numberofversions));
} else {
// Returns all the values from the array and indexes the array numerically.
// We need this because mustache requires it.
$contentdata->contentversions = array_values($contentandversions->contentversions);
$contentdata->iscontentversion = false;
$contentdataname = $contentdata->name;
if ($contentdata->l3name) {
$contentdataname = $contentdata->l3name;
}
}
$contentdata->contentdataname = $contentdataname;
// Get page url.
$pageurl = util::get_current_url();
// Render page header and crumb trail.
$pagetitle = $pageheading = get_string('pageheader', 'openstudio',
array('cname' => $course->shortname, 'cmname' => $cm->name, 'title' => $contentdataname));
util::page_setup($PAGE, $pagetitle, $pageheading, $pageurl, $course, $cm);
// Is the content mine?
$permissions->contentismine = $contentdata->isownedbyviewer;
$contentisinpinboard = false;
if ($contentdata->levelid == 0) {
$contentisinpinboard = true;
}
$contentdatavisibilitycontext = $contentdata->visibilitycontext;
$crumbarray = array();
switch ($contentdatavisibilitycontext) {
case content::VISIBILITY_MODULE:
$vid = content::VISIBILITY_MODULE;
$areaurl = new moodle_url('/mod/openstudio/view.php',
array('id' => $cm->id, 'vid' => content::VISIBILITY_MODULE));
$areaurlname = get_string('navmymodule', 'openstudio');
break;
case content::VISIBILITY_GROUP:
$vid = content::VISIBILITY_GROUP;
$areaurl = new moodle_url('/mod/openstudio/view.php',
array('id' => $cm->id, 'vid' => content::VISIBILITY_GROUP));
$areaurlname = get_string('navmygroup', 'openstudio');
break;
case content::VISIBILITY_PRIVATE:
default:
$vid = content::VISIBILITY_PRIVATE;
$areaurl = new moodle_url('/mod/openstudio/view.php',
array('id' => $cm->id, 'vid' => content::VISIBILITY_PRIVATE));
$areaurlname = get_string('navactivities', 'openstudio');
break;
}
// Only show the pinboard link if the slot is mine.
if ($permissions->contentismine && $contentisinpinboard) {
$vid = content::VISIBILITY_PRIVATE_PINBOARD;
$areaurl = new moodle_url('/mod/openstudio/view.php',
array('id' => $cm->id, 'vid' => content::VISIBILITY_PRIVATE_PINBOARD, 'fblock' => -1));
$areaurlname = get_string('navmypinboard', 'openstudio');
$crumbarray[$areaurlname] = $areaurl->out(false);
} else {
$pageview = 'activities';
$crumbarray[$areaurlname] = $areaurl->out(false);
if (!$permissions->contentismine) {
$crumbkey = get_string('profileswork', 'openstudio', array('name' => $contentdata->firstname));
$crumbarray[$crumbkey] = new moodle_url(
'/mod/openstudio/view.php',
array('id' => $cm->id,
'vid' => content::VISIBILITY_WORKSPACE,
'vuid' => $contentdata->userid));
}
}
// Generate stream html.
$renderer = $PAGE->get_renderer('mod_openstudio');
$PAGE->set_button($renderer->searchform($theme, $vid, $id));
if (!$permissions->contentismine) {
$contentdata->reportabuselink = util::render_report_abuse_link(
'openstudio', $permissions->activecmcontextid, 'content', $contentdata->id,
$pageurl, $pageurl, $permissions->activeuserid);
}
$contentdata->vid = $vid;
$contentdata->placeholdertext = $areaurlname;
$contentdata->folderid = $folderid;
$contentdata->folderid = $folderid;
$contentdata->isfoldercontent = false;
if ($folderid) {
$folderdata = content::get($folderid);
$folderdata = lock::determine_lock_status($folderdata);
$contentdetails = renderer_utils::content_details($cmid, $permissions, $contentdata, $iscontentversion);
if ($contentdetails->contentdataname) {
$contentdataname = $contentdetails->contentdataname;
}
if ($folderdata) {
$contentdata->name = $folderdata->name;
$folderedit = new moodle_url('/mod/openstudio/contentedit.php',
array('id' => $cm->id, 'lid' => 0, 'sid' => $folderdata->id, 'type' => content::TYPE_FOLDER));
$contentdata->folderedit = $folderedit->out(false);
$contentdata->isfoldercontent = true;
$contentdata->folderid = $folderdata->id;
$contentdata->containingfolderlocktype = ($folderdata->locktype);
$folderoverview = new moodle_url('/mod/openstudio/folder.php',
array('id' => $id, 'sid' => $folderid, 'lid' => $contentdata->levelid,
'vuid' => $contentdata->userid));
$crumbarray[$contentdata->name] = $folderoverview;
$contentdata->foldereditenable = ($permissions->addcontent && $USER->id == $folderdata->userid)
|| $permissions->managecontent;
$contentdata->folderlinkoverview = $folderoverview;
}
}
$crumbarray[$contentdataname] = $pageurl;
util::add_breadcrumb($PAGE, $cm->id, navigation_node::TYPE_ACTIVITY, $crumbarray);
$config = [
'paths' => [
'ansi_up' => (new moodle_url('/mod/openstudio/js/ansi_up.min'))->out(false),
'marked' => (new moodle_url('/mod/openstudio/js/marked.min'))->out(false),
'es5-shim' => (new moodle_url('/mod/openstudio/js/es5-shim.min'))->out(false),
'notebook' => (new moodle_url('/mod/openstudio/js/notebook.min'))->out(false),
'prism' => (new moodle_url('/mod/openstudio/js/prism.min'))->out(false),
'leaflet' => (new moodle_url('/mod/openstudio/js/leaflet.min'))->out(false),
],
'shim' => [
'ansi_up' => ['exports' => 'ansi_up'],
'marked' => ['exports' => 'marked'],
'es5-shim' => ['exports' => 'es5-shim'],
'notebook' => [
'deps' => ['marked'], 'exports' => 'notebook',
],
'prism' => ['exports' => 'Prism'],
'leaflet' => ['exports' => 'leaflet'],
],
];
$requirejs = 'require.config(' . json_encode($config) . ')';
$PAGE->requires->js_amd_inline($requirejs);
$PAGE->requires->strings_for_js(
array('contentactionarchivepost', 'modulejsdialogcancel', 'archivedialogheader',
'modulejsdialogcontentarchiveconfirm', 'deletearchiveversionheader', 'deletearchiveversionheaderconfirm'),
'mod_openstudio');
$PAGE->requires->js_call_amd('mod_openstudio/previewipynb', 'init');
if (isset($contentdata->showextradata) && $contentdata->showextradata & content::INFO_GPSDATA) {
$PAGE->requires->css('/mod/openstudio/css/leaflet.css');
}
// Update flag and tracking.
$tracking = tracking::READ_CONTENT;
$logaction = 'content_viewed';
if ($iscontentversion) {
$tracking = tracking::READ_CONTENT_VERSION;
$logaction = 'contentversion_viewed';
}
flags::toggle($contentdata->id, flags::READ_CONTENT, 'on', $USER->id, $contentdata->id);
tracking::log_action($contentdata->id, $tracking, $USER->id);
// Note: This header statement is needed because the slot form data contains
// object and script code and browsers like webkit thinks this is a cross-site
// scripting issue and refuses to loaded embbedded video players.
// Given we clean out all the user generated content, there should be no security
// risk of disabling the security check for this page.
header('X-XSS-Protection: 0');
// Update flag and tracking.
flags::toggle(
$contentdata->id, flags::READ_CONTENT, 'on', $USER->id, $contentdata->id);
tracking::log_action($contentdata->id, flags::READ_CONTENT, $USER->id);
$html = $renderer->siteheader(
$coursedata, $permissions, $theme, $cm->name, '', '');
echo $OUTPUT->header(); // Header.
echo $html;
echo $renderer->content_page($cm, $permissions, $contentdata, $cminstance); // Content detail.
$PAGE->requires->js_call_amd('mod_openstudio/contentpage', 'init');
// Finish the page.
echo $OUTPUT->footer();