-
Notifications
You must be signed in to change notification settings - Fork 2
/
view.php
529 lines (486 loc) · 23.3 KB
/
view.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
<?php
// This file is part of Mindmap module for 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/>.
/**
* advmindmap view page
*
* @package mod
* @subpackage mindmap
* @author ekpenso.com
* @copyright 2012 Andy Chan <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once("../../config.php");
require_once("lib.php");
$id = optional_param('id', 0, PARAM_INT); // Course Module ID, or
$a = optional_param('a', 0, PARAM_INT); // newmodule ID
$viewuser = optional_param('viewuser', 0, PARAM_INT); // view other
$viewgroup = optional_param('viewgroup', 0, PARAM_INT); // view other
$viewdummy = optional_param('viewdummy', 0, PARAM_INT); // view dummy group
$group = optional_param('group', 0, PARAM_INT); // group id
if ($id) {
if (! $cm = $DB->get_record("course_modules", array("id"=>$id))) {
print_error('invalidcoursemodule');
}
if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
print_error('coursemisconf', 'advmindmap');
}
if (! $advmindmap = $DB->get_record("advmindmap", array("id"=>$cm->instance))) {
print_error('invalidid', 'advmindmap');
}
} else {
if (! $advmindmap = $DB->get_record("advmindmap", array("id"=>$a))) {
print_error('invalidid', 'advmindmap');
}
if (! $course = $DB->get_record("course", array("id"=>$advmindmap->course))) {
print_error('coursemisconf', 'advmindmap');
}
if (! $cm = get_coursemodule_from_instance("advmindmap", $advmindmap->id, $course->id)) {
print_error('invalidcoursemodule');
}
}
require_course_login($course, true, $cm);
$context = context_module::instance($cm->id);
//add_to_log($course->id, "advmindmap", "view", "view.php?id=".$cm->id, $advmindmap->id, $cm->id);
$eventdata = array();
$eventdata['objectid'] = $advmindmap->id;
$eventdata['context'] = $context;
$eventdata['courseid'] = $course->id;
$eventdata['other'] = array("content" => "advmindmapactivityview", "group"=>$group, "viewuser"=>$viewuser, "viewgroup"=>$viewgroup, "viewdummy"=>$viewdummy);
$event = \mod_advmindmap\event\course_module_viewed::create($eventdata);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('advmindmap', $advmindmap);
$event->add_record_snapshot('course_modules', $cm);
$event->trigger();
$url = new moodle_url('/mod/advmindmap/view.php');
$url->param('id', $id);
$url->param('a', $a);
$url->param('viewuser', $viewuser);
$url->param('viewgroup', $viewgroup);
$url->param('group', $group);
$PAGE->set_url($url);
$PAGE->set_pagelayout('incourse');
$PAGE->set_context($context);
// check course module group mode and get groups
$groupmode = groups_get_activity_groupmode($cm, $course);
if ($groupmode) {
// get all group mind maps
$params = array($advmindmap->id, 0);
$allgroupmindmaps = $DB->get_records_select("advmindmap_instances", "advno = ? AND groupid <> ?", $params);
if ($allowgroups = groups_get_activity_allowed_groups($cm)) {
// Gets a list of groups that the user is allowed to access
$allowgids = array_keys($allowgroups);
$usergroups = array();
// Check if user is member of any group
foreach ($allowgids as $gid) {
if (groups_is_member($gid, $USER->id)) {
$usergroups[] = $gid;
}
}
// Group mind maps that this user is a member
$caneditmindmaps = array();
foreach ($allgroupmindmaps as $agm) {
if (in_array($agm->groupid, $usergroups)) {
$caneditmindmaps[$agm->id] = $agm;
}
}
// if user is not in any group and cannot view other people mind map = they cannot view anything
if (empty($usergroups) && !has_capability('mod/advmindmap:viewother', $context)) {
print_error('errornotingroup', 'advmindmap');
}
} else {
// no group this user can view
print_error('errornotingroup', 'advmindmap');
}
}
// Initially user cannot see anything before permission checking
$canedit = false;
// Preparation: check if any mindmaps for current user, if not, add one for them
if (!has_capability('mod/advmindmap:givecomment', $context)) { // not teacher
if ($groupmode) { // group mind map
// A user may in multiple groups
$params = array();
$query_params = array('advno'=>$advmindmap->id);
if (!empty($usergroups)) {
list($in_sql, $in_params) = $DB->get_in_or_equal($usergroups, SQL_PARAMS_NAMED);
} else {
print_error('errornotingroup', 'advmindmap');
}
$params = array_merge($in_params, $query_params);
$advmindmap_instances = $DB->get_records_select("advmindmap_instances", "advno = :advno AND groupid $in_sql", $params);
if (!$advmindmap_instances) {
// create an mind map instance for each group the user is in
foreach ($usergroups as $ug) {
$advmindmap_instance = advmindmap_set_new_instance($advmindmap);
$advmindmap_instance->groupid = $ug;
$advmindmap_instance->id = $DB->insert_record("advmindmap_instances", $advmindmap_instance);
}
// mindmap created, let's reload
header('Location: view.php?id='.$id);
}
// if user want to see other group, check the permission
if ($viewgroup) {
if (!has_capability('mod/advmindmap:viewother', $context)) {
foreach ($advmindmap_instances as $ami) {
if ($ami->id == $viewgroup) {
$matchgroup = true;
break;
}
}
if (!$matchgroup || $groupmode == 1) print_error('errorcannotviewgroupmindmap', 'advmindmap');
}
}
} else if (!$advmindmap->numdummygroups) { // individual mind map, view own mindmap
$advmindmap_instance = $DB->get_record("advmindmap_instances", array("advno"=>$cm->instance, "userid"=>$USER->id));
if (!$advmindmap_instance) {
$advmindmap_instance = advmindmap_set_new_instance($advmindmap);
$advmindmap_instance->userid = $USER->id;
$advmindmap_instance->id = $DB->insert_record("advmindmap_instances", $advmindmap_instance);
// mindmap created, let's reload
header('Location: view.php?id='.$id);
}
if($viewuser) {
if (!has_capability('mod/advmindmap:viewother', $context)) {
print_error('errorcannotviewusermindmap', 'advmindmap');
}
$advmindmap_instance = $DB->get_record("advmindmap_instances", array("advno"=>$cm->instance, "id"=>$viewuser));
}
}
} else { // teacher or overrided permission
if ($groupmode && $viewgroup) { // group mind map, view other mind map
if (!$advmindmap_instance = $DB->get_record("advmindmap_instances", array("id"=>$viewgroup))) {
print_error("The selected group have no mindmaps");
}
} else if (!$groupmode && !$advmindmap->numdummygroups && $viewuser) {// individual mind map, view other mindmap
if (!$advmindmap_instance = $DB->get_record("advmindmap_instances", array("id"=>$viewuser))) {
print_error("The selected user have no mindmaps");
}
} else if (!$advmindmap->numdummygroups) { // individual mind map, view own mindmap
if (!$advmindmap_instance = $DB->get_record("advmindmap_instances", array("advno"=>$cm->instance, "userid"=>$USER->id))) {
$advmindmap_instance = advmindmap_set_new_instance($advmindmap);
$advmindmap_instance->userid = $USER->id;
$advmindmap_instance->id = $DB->insert_record("advmindmap_instances", $advmindmap_instance);
// mindmap created, let's reload
header('Location: view.php?id='.$id);
}
}
}
if ($advmindmap->numdummygroups > 0 && $viewdummy) {
$advmindmap_instance = $DB->get_record("advmindmap_instances", array("id"=>$viewdummy));
}
// end adding map
/// Print the page header
$stradvmindmaps = get_string("modulenameplural", "advmindmap");
$stradvmindmap = get_string("modulename", "advmindmap");
$courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
$title = $courseshortname . ': ' . format_string($advmindmap->name);
$PAGE->set_title($title);
$PAGE->set_heading($course->fullname);
echo $OUTPUT->header();
echo $OUTPUT->box_start();
if ($advmindmap->editable == '0') {
echo html_writer::tag('p', get_string('uneditable', 'advmindmap'), array('style', 'color:red;font-weight:bold;'));
}
// Check if user have permission to edit mindmap, display a message if viewing others' mindmap
if ($groupmode) {
if (!has_capability('mod/advmindmap:givecomment', $context)) {
if (!$viewgroup) {
$advmindmap_instance = current($caneditmindmaps);
} else {
$advmindmap_instance = $allgroupmindmaps[$viewgroup];
}
echo "<p>".get_string('viewingauthor', 'advmindmap', '<strong>'.groups_get_group_name($advmindmap_instance->groupid).'</strong>');
if (in_array($advmindmap_instance->groupid, $usergroups)) {
echo " ".get_string('ismember', 'advmindmap').'</p>';
$canedit = true;
}
} else {
if ($viewgroup) {
$advmindmap_instance = $allgroupmindmaps[$viewgroup];
echo "<p>".get_string('viewingauthor', 'advmindmap', '<strong>'.groups_get_group_name($advmindmap_instance->groupid).'</strong>');
echo " (<a href='view.php?id=$id'>".get_string('viewown', 'advmindmap')."</a>)";
} else {
$canedit = true;
}
}
} else {
if ($advmindmap->numdummygroups > 0) {
if ($viewdummy) {
echo "<p>".get_string('viewingauthor', 'advmindmap', '<strong>'.$advmindmap_instance->name.'</strong>')."</p>";
$canedit = true;
}
} else {
if ($advmindmap_instance->userid != $USER->id) {
// viewing other user mindmap
if ($author = $DB->get_record('user', array('id'=>$advmindmap_instance->userid))) {
echo "<p>".get_string('viewingauthor', 'advmindmap', '<strong>'.fullname($author).'</strong>');
echo " (<a href='view.php?id=$id'>".get_string('viewown', 'advmindmap')."</a>)";
echo "</p>";
} else {
print_error('errorinvalidauthor', 'advmindmap');
}
} else {
$canedit = true;
}
}
}
if (!empty($advmindmap->intro)) {
// Issue #7: suggestion by rabser
echo $OUTPUT->box(format_module_intro('advmindmap', $advmindmap, $cm->id), 'generalbox', 'intro');
}
if (isset($advmindmap_instance)) {
echo "<p>".get_string('lastupdated', 'advmindmap').date("Y-n-j H:i", $advmindmap_instance->timemodified?$advmindmap_instance->timemodified:$advmindmap_instance->timecreated)."</p>";
}
// Locking: user can still occupy the mindmap forever if they reload the page
$islocked = false;
if ($groupmode && $canedit && !has_capability('mod/advmindmap:givecomment', $context)) {
$locktime = 3600; // 1 hour
$now = time();
$waitingtime = 3600- ($now - $advmindmap_instance->timeaccessed);
$wmin = floor($waitingtime/60);
$wsec = $waitingtime%60;
if ($advmindmap_instance->useraccessed && ($advmindmap_instance->useraccessed != $USER->id)) {
if ($now - $advmindmap_instance->timeaccessed < 3600) {
$islocked = true;
$canedit = false;
$editinguser = $DB->get_record('user', array('id'=>$advmindmap_instance->useraccessed));
echo "<p style='color:red;'><img src='". $PAGE->theme->name ."/pix/i/lock.gif'/> ".get_string('editingbyuser', 'advmindmap', fullname($editinguser))."</p>";
echo "<p style='color:red;'>".get_string('unlocktime', 'advmindmap')."<span id='countdown'></span></p>";
}
} else {
advmindmap_add_access_record($advmindmap_instance);
echo "<p><img src='".$PAGE->theme->name."/pix/i/lock.gif'/> ".get_string('lockedbyyou', 'advmindmap');
echo "<span style='font-weight:bold; font-size:2.0em;'>→<span> <input type='button' value='".get_string('unlockbutton', 'advmindmap')."' onclick='unlockmindmap()' /></p>";
}
}
echo $OUTPUT->box_end();
// Construct a unique link for copying
$uniquelink = $CFG->wwwroot."/mod/advmindmap/view.php?id=$id";
if ($viewuser) $uniquelink .= "&viewuser=$viewuser";
else if ($viewgroup) $uniquelink .= "&viewgroup=$viewgroup";
else if ($viewdummy) $uniquelink .= "&viewdummy=$viewdummy";
else if (isset($advmindmap_instance)) {
if ($groupmode) $uniquelink .= "&viewgroup=".$advmindmap_instance->id;
else $uniquelink .= "&viewuser=".$advmindmap_instance->id;
}
?>
<div id="flashcontent"></div>
<?php if (isset($advmindmap_instance)) { // Display link for copying and help icon ?>
<div style="width:85%; margin:5px auto; font-size:0.8em;">
<?php echo get_string('uniquelink', 'advmindmap'); ?>: <input type="text" id="uniquelink" onclick="selectAll('uniquelink');" style="width:500px;" value="<?php echo $uniquelink; ?>" />
<?php echo get_string('copylink', 'advmindmap'); ?>
<div style="float:right;">
<?php
echo '<span class="helplink">';
echo $OUTPUT->help_icon('detail', 'advmindmap');
echo '</span>';
?>
</div>
</div>
<?php } ?>
<script type="text/javascript" src="./swfobject.js"></script>
<script type="text/javascript">
// <![CDATA[
function selectAll(id) {
document.getElementById(id).focus();
document.getElementById(id).select();
}
function mm_save(str) {
alert(decodeURI(str));
}
var so = new SWFObject("./viewer.swf", "viewer", 800, 600, "9", "#FFFFFF");
<?php if(! $viewuser) { ?>
so.addVariable("load_url", "./xml.php?id=<?php echo $advmindmap_instance->id;?>");
<?php } else { ?>
so.addVariable("load_url", "./xml.php?id=<?php echo $viewuser;?>");
<?php } ?>
<?php if ($canedit && $advmindmap->editable == '1') {?> // now we check the global setting by teacher instead of the instance
so.addVariable('save_url', "./save.php?id=<?php echo $advmindmap_instance->id;?>");
so.addVariable('editable', "true");
<?php } else { ?>
so.addVariable('editable', "false");
<?php } ?>
so.addVariable("lang", "en");
<?php if(isset($advmindmap_instance)){ ?>
so.write("flashcontent");
<?php } ?>
<?php if ($islocked) { ?>
var interval;
var minutes = <?php echo $wmin; ?>;
var seconds = <?php echo $wsec; ?>;
window.onload = function() {
countdown('countdown');
}
<?php } ?>
function countdown(element) {
interval = setInterval(function() {
var el = document.getElementById(element);
if(seconds == 0) {
if(minutes == 0) {
el.innerHTML = "00m 00s";
clearInterval(interval);
location.reload(true);
return;
} else {
minutes--;
seconds = 60;
}
}
if(minutes > 0) {
var minute_text = minutes + (minutes > 1 ? ' m' : ' m');
} else {
var minute_text = '';
}
var second_text = seconds > 1 ? 's' : 's';
el.innerHTML = minute_text + ' ' + seconds + ' ' + second_text;
seconds--;
}, 1000);
}
function unlockmindmap() {
if (confirm('<?php echo get_string('unlockconfirm', 'advmindmap') ?>')) {
location.href = 'unlock.php?id=<?php echo $id ?>&instanceid=<?php echo $advmindmap_instance->id ?>';
}
}
// ]]>
</script>
<?php
// Only show a table listing other mindmaps if user have the permission
if (has_capability('mod/advmindmap:viewother', $context)) {
$table = new html_table();
if ($groupmode == 1 || $groupmode == 2) { // group mind map
if (!$advmindmaps = $DB->get_records_sql("SELECT groupid, id, timecreated, timemodified FROM {advmindmap_instances} WHERE advno = ?", array($advmindmap_instance->advno))) {
notice("No advmindmap was created yet.", "../../course/view.php?id=$course->id");
echo $OUTPUT->footer();
die;
}
$strname = get_string("groupname", "advmindmap");
$strmembers = get_string("groupmembers", "advmindmap");
$strdate = get_string("lastupdated", "advmindmap");
$table->head = array($strname, $strmembers, $strdate);
$table->align = array("left", "left", "left");
foreach ($allowgroups as $ag) {
// Only display link and last updated date if a mindmap was init,
// else only display a name without link
// group member list
$members = groups_get_members($ag->id);
if (empty($members)) {
$memberlist = get_string('notavailable', 'advmindmap');
} else {
$firstmember = array_shift($members);
$memberlist = fullname($firstmember);
if (count($members) > 0) {
foreach ($members as $m) {
$memberlist .= ", ".fullname($m);
}
}
}
if (in_array($ag->id, array_keys($advmindmaps))) {
if ($advmindmap_instance->id == $advmindmaps[$ag->id]->id) {
$link = $ag->name." ".get_string('viewing', 'advmindmap');
} else {
$link = "<a href=\"view.php?id=$cm->id&viewgroup=".$advmindmaps[$ag->id]->id."\">".$ag->name."</a>";
}
$lastupdated = userdate($advmindmaps[$ag->id]->timemodified?$advmindmaps[$ag->id]->timemodified:$advmindmaps[$ag->id]->timecreated);
$table->data[] = array($link, $memberlist, $lastupdated);
} else {
$table->data[] = array($ag->name, $memberlist, get_string('notavailable', 'advmindmap'));
}
}
echo "<br />";
echo html_writer::table($table);
} else if ($groupmode == 0) { // individual mind map or dummy group mind map
if ($advmindmap->numdummygroups > 0) {
$params = array($advmindmap->id, 0, 0);
$dummymindmaps = $DB->get_records_select('advmindmap_instances', 'advno = ? AND userid = ? AND groupid = ?', $params, 'id ASC');
if ($dummymindmaps) {
$strgroup = get_string("group");
$strdate = get_string("lastupdated", "advmindmap");
$table->head = array($strgroup, $strdate);
$table->align = array("left", "left");
foreach ($dummymindmaps as $dm) {
$link = html_writer::link("view.php?id=".$cm->id."&viewdummy=".$dm->id, $dm->name);
$lastupdated = userdate($dm->timemodified?$dm->timemodified:$dm->timecreated);
$table->data[] = array($link, $lastupdated);
}
echo "<br />";
echo html_writer::table($table);
}
} else {
if (!$advmindmaps = $DB->get_records_sql("SELECT userid, groupid, id, timecreated, timemodified FROM {advmindmap_instances} WHERE advno = ?", array($advmindmap_instance->advno))) {
notice("No advmindmap was created yet.", "../../course/view.php?id=$course->id");
echo $OUTPUT->footer();
die;
}
// Issue #6: suggestion by rabser
// Get all students in the course able to view mindmaps
$context = context_course::instance($course->id);
$students = get_enrolled_users($context);
if (count($students) > 0) {
$students = array_keys($students);
} else {
notice(get_string('errornostudentincourse', 'advmindmap'));
echo $OUTPUT->footer();
die;
}
// DIRTY HACK: create a temp course object just to generate this menu
$hack_course = $course;
$hack_course->groupmode = VISIBLEGROUPS;
if ($visible_groups = groups_print_course_menu($hack_course, "view.php?id=$id&viewuser=$viewuser")) {
echo $OUTPUT->box_start();
groups_print_course_menu($hack_course, "view.php?id=$id&viewuser=$viewuser");
echo $OUTPUT->box_end();
}
$strname = get_string("name");
$strclass = get_string("class", "advmindmap");
$strclassno = get_string("classno", "advmindmap");
$strdate = get_string("lastupdated", "advmindmap");
$table->head = array ($strname, $strclass, $strclassno, $strdate);
$table->align = array ("left", "left", "left", "left");
if ($group) {
if ($group_members = groups_get_members($group, 'u.id')) {
$group_members = array_keys($group_members);
$students = array_intersect($students, $group_members);
} else {
print_error('errornouseringroup', "advmindmap");
echo $OUTPUT->footer();
exit;
}
}
foreach ($students as $studentid) {
$user = $DB->get_record("user", array("id"=>$studentid));
$params = array($studentid, 'class');
if (!$class = $DB->get_field_sql("SELECT data FROM {user_info_data} WHERE userid = ? AND fieldid = (SELECT id FROM {user_info_field} WHERE shortname = ?)", $params)) {
$class = '---';
}
$params = array($studentid, 'classno');
if (!$classno = $DB->get_field_sql("SELECT data FROM {user_info_data} WHERE userid = ? AND fieldid = (SELECT id FROM {user_info_field} WHERE shortname = ?)", $params)) {
$classno = '---';
}
// Only display link and last updated date if a mindmap was init,
// else only display a name without link
if (in_array($studentid, array_keys($advmindmaps))) {
$link = "<a href=\"view.php?id=$cm->id&group=$group&viewuser=".$advmindmaps[$studentid]->id."\">".fullname($user)."</a>";
$lastupdated = userdate($advmindmaps[$studentid]->timemodified?$advmindmaps[$studentid]->timemodified:$advmindmaps[$studentid]->timecreated);
$table->data[] = array($link, $class, $classno, $lastupdated);
} else {
$table->data[] = array(fullname($user), $class, $classno, get_string('notavailable', 'advmindmap'));
}
}
echo "<br />";
echo html_writer::table($table);
}
}
}
echo $OUTPUT->footer($course);