This repository has been archived by the owner on Apr 4, 2022. It is now read-only.
/
lib.php
566 lines (505 loc) · 23.3 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
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
<?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/>.
/**
* This file contains general functions for the course format Topic
*
* @since 2.0
* @package moodlecore
* @copyright 2009 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->libdir.'/filelib.php');
//require_once($CFG->libdir.'/modinfolib.php');
/**
* Indicates this format uses sections.
*
* @return bool Returns true
*/
function callback_slides_uses_sections() {
return true;
}
/**
* Used to display the course structure for a course where format=topic
*
* This is called automatically by {@link load_course()} if the current course
* format = weeks.
*
* @param array $path An array of keys to the course node in the navigation
* @param stdClass $modinfo The mod info object for the current course
* @return bool Returns true
*/
function callback_slides_load_content(&$navigation, $course, $coursenode) {
return $navigation->load_generic_course_sections($course, $coursenode, 'slides');
}
/**
* The string that is used to describe a section of the course
* e.g. Topic, Week...
*
* @return string
*/
function callback_slides_definition() {
return get_string('topic');
}
/**
* The GET argument variable that is used to identify the section being
* viewed by the user (if there is one)
*
* @return string
*/
function callback_slides_request_key() {
return 'topic';
}
function callback_slides_get_section_name($course, $section) {
// We can't add a node without any text
if (!empty($section->name)) {
return $section->name;
} else if ($section->section == 0) {
return get_string('section0name', 'format_slides');
} else {
return get_string('topic').' '.$section->section;
}
}
/**
* Declares support for course AJAX features
*
* @see course_format_ajax_support()
* @return stdClass
*/
function callback_slides_ajax_support() {
$ajaxsupport = new stdClass();
$ajaxsupport->capable = false;
$ajaxsupport->testedbrowsers = array('MSIE' => 6.0, 'Gecko' => 20061111, 'Safari' => 531, 'Chrome' => 6.0);
return $ajaxsupport;
}
function slides_initialise_topicsnavigator(moodle_page $page, $args) {
// $modulepath = '/course/format/slides/';
$module = array(
'name' => 'topics_navigator',
'fullpath' => '/course/format/slides/module.js',
'requires' => array("transition")
);
$page->requires->js_module($module);
$page->requires->js_init_call('M.format_slides.init', $args, true);
}
/**
* Prints a section full of activity modules
* @Overrides course/lib.php=>print_section
*/
function ss_print_section($course, $section, $mods, $modnamesused, $absolute=false, $width="100%", $hidecompletion=false) {
#print_r($mods);
global $CFG, $USER, $DB, $PAGE, $OUTPUT;
static $initialised;
static $groupbuttons;
static $groupbuttonslink;
static $isediting;
static $ismoving;
static $strmovehere;
static $strmovefull;
static $strunreadpostsone;
static $usetracking;
static $groupings;
if (!isset($initialised)) {
$groupbuttons = ($course->groupmode or (!$course->groupmodeforce));
$groupbuttonslink = (!$course->groupmodeforce);
$isediting = $PAGE->user_is_editing();
$ismoving = $isediting && ismoving($course->id);
if ($ismoving) {
$strmovehere = get_string("movehere");
$strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'"));
}
include_once($CFG->dirroot.'/mod/forum/lib.php');
if ($usetracking = forum_tp_can_track_forums()) {
$strunreadpostsone = get_string('unreadpostsone', 'forum');
}
$initialised = true;
}
$labelformatoptions = new stdClass();
$labelformatoptions->noclean = true;
$labelformatoptions->overflowdiv = true;
/// Casting $course->modinfo to string prevents one notice when the field is null
$modinfo = get_fast_modinfo($course);
$completioninfo = new completion_info($course);
//Accessibility: replace table with list <ul>, but don't output empty list.
if (!empty($section->sequence)) {
// Fix bug #5027, don't want style=\"width:$width\".
echo "<ul class=\"section img-text\">\n";
$sectionmods = explode(",", $section->sequence);
foreach ($sectionmods as $modnumber) {
if (empty($mods[$modnumber])) {
continue;
}
$mod = $mods[$modnumber];
if ($ismoving and $mod->id == $USER->activitycopy) {
// do not display moving mod
continue;
}
if (isset($modinfo->cms[$modnumber])) {
// We can continue (because it will not be displayed at all)
// if:
// 1) The activity is not visible to users
// and
// 2a) The 'showavailability' option is not set (if that is set,
// we need to display the activity so we can show
// availability info)
// or
// 2b) The 'availableinfo' is empty, i.e. the activity was
// hidden in a way that leaves no info, such as using the
// eye icon.
if (!$modinfo->cms[$modnumber]->uservisible &&
(empty($modinfo->cms[$modnumber]->showavailability) ||
empty($modinfo->cms[$modnumber]->availableinfo))) {
// visibility shortcut
continue;
}
} else {
if (!file_exists("$CFG->dirroot/mod/$mod->modname/lib.php")) {
// module not installed
continue;
}
if (!coursemodule_visible_for_user($mod) &&
empty($mod->showavailability)) {
// full visibility check
continue;
}
}
// In some cases the activity is visible to user, but it is
// dimmed. This is done if viewhiddenactivities is true and if:
// 1. the activity is not visible, or
// 2. the activity has dates set which do not include current, or
// 3. the activity has any other conditions set (regardless of whether
// current user meets them)
$canviewhidden = has_capability(
'moodle/course:viewhiddenactivities',
get_context_instance(CONTEXT_MODULE, $mod->id));
$accessiblebutdim = false;
if ($canviewhidden) {
$accessiblebutdim = !$mod->visible;
if (!empty($CFG->enableavailability)) {
$accessiblebutdim = $accessiblebutdim ||
$mod->availablefrom > time() ||
($mod->availableuntil && $mod->availableuntil < time()) ||
count($mod->conditionsgrade) > 0 ||
count($mod->conditionscompletion) > 0;
}
}
$liclasses = array();
$liclasses[] = 'activity';
$liclasses[] = $mod->modname;
$liclasses[] = 'modtype_'.$mod->modname;
echo html_writer::start_tag('li', array('class'=>join(' ', $liclasses), 'id'=>'module-'.$modnumber));
if ($ismoving) {
echo '<a title="'.$strmovefull.'"'.
' href="'.$CFG->wwwroot.'/course/mod.php?moveto='.$mod->id.'&sesskey='.sesskey().'">'.
'<img class="movetarget" src="'.$OUTPUT->pix_url('movehere') . '" '.
' alt="'.$strmovehere.'" /></a><br />
';
}
$classes = array('mod-indent');
if (!empty($mod->indent)) {
$classes[] = 'mod-indent-'.$mod->indent;
if ($mod->indent > 15) {
$classes[] = 'mod-indent-huge';
}
}
echo html_writer::start_tag('div', array('class'=>join(' ', $classes)));
$extra = '';
if (!empty($modinfo->cms[$modnumber]->extra)) {
$extra = $modinfo->cms[$modnumber]->extra;
}
if ($mod->modname == "label") {
if ($accessiblebutdim || !$mod->uservisible) {
echo '<div class="dimmed_text"><span class="accesshide">'.
get_string('hiddenfromstudents').'</span>';
} else {
echo '<div>';
}
echo format_text($extra, FORMAT_HTML, $labelformatoptions);
echo "</div>";
if (!empty($mod->groupingid) && has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) {
if (!isset($groupings)) {
$groupings = groups_get_all_groupings($course->id);
}
echo " <span class=\"groupinglabel\">(".format_string($groupings[$mod->groupingid]->name).')</span>';
}
} else { // Normal activity
$instancename = format_string($modinfo->cms[$modnumber]->name, true, $course->id);
# echo format_string($modinfo->cms[$modnumber]->intro, true, $course->id);
# echo format_string($modinfo->cms[$modnumber]->name, true, $course->id);
# print_r($modinfo->cms[$modnumber]);
$customicon = $modinfo->cms[$modnumber]->icon;
if (!empty($customicon)) {
if (substr($customicon, 0, 4) === 'mod/') {
list($modname, $iconname) = explode('/', substr($customicon, 4), 2);
$icon = $OUTPUT->pix_url($iconname, $modname);
} else {
$icon = $OUTPUT->pix_url($customicon);
}
} else {
$icon = $OUTPUT->pix_url('icon', $mod->modname);
}
//Accessibility: for files get description via icon, this is very ugly hack!
$altname = '';
$altname = $mod->modfullname;
if (!empty($customicon)) {
$archetype = plugin_supports('mod', $mod->modname, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER);
if ($archetype == MOD_ARCHETYPE_RESOURCE) {
$mimetype = mimeinfo_from_icon('type', $customicon);
$altname = get_mimetype_description($mimetype);
}
}
// Avoid unnecessary duplication.
if (false !== stripos($instancename, $altname)) {
$altname = '';
}
// File type after name, for alphabetic lists (screen reader).
if ($altname) {
$altname = get_accesshide(' '.$altname);
}
// We may be displaying this just in order to show information
// about visibility, without the actual link
if ($mod->uservisible) {
// Display normal module link
if (!$accessiblebutdim) {
$linkcss = '';
$accesstext ='';
} else {
$linkcss = ' class="dimmed" ';
$accesstext = '<span class="accesshide">'.
get_string('hiddenfromstudents').': </span>';
}
echo '<a '.$linkcss.' '.$extra.
' href="'.$CFG->wwwroot.'/mod/'.$mod->modname.'/view.php?id='.$mod->id.'">'.
'<img src="'.$icon.'" class="activityicon" alt="'.get_string('modulename',$mod->modname).'" /> '.
$accesstext.'<span class="instancename">'.$instancename.$altname.'</span></a>';
if (!empty($mod->groupingid) && has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) {
if (!isset($groupings)) {
$groupings = groups_get_all_groupings($course->id);
}
echo " <span class=\"groupinglabel\">(".format_string($groupings[$mod->groupingid]->name).')</span>';
}
} else {
// Display greyed-out text of link
echo '<span class="dimmed_text" '.$extra.' ><span class="accesshide">'.
get_string('notavailableyet','condition').': </span>'.
'<img src="'.$icon.'" class="activityicon" alt="'.get_string('modulename', $mod->modname).'" /> <span>'.
$instancename.$altname.'</span></span>';
}
}
if ($usetracking && $mod->modname == 'forum') {
if ($unread = forum_tp_count_forum_unread_posts($mod, $course)) {
echo '<span class="unread"> <a href="'.$CFG->wwwroot.'/mod/forum/view.php?id='.$mod->id.'">';
if ($unread == 1) {
echo $strunreadpostsone;
} else {
print_string('unreadpostsnumber', 'forum', $unread);
}
echo '</a></span>';
}
}
if ($isediting) {
if ($groupbuttons and plugin_supports('mod', $mod->modname, FEATURE_GROUPS, 0)) {
if (! $mod->groupmodelink = $groupbuttonslink) {
$mod->groupmode = $course->groupmode;
}
} else {
$mod->groupmode = false;
}
echo ' ';
echo make_editing_buttons($mod, $absolute, true, $mod->indent, $section->section);
hook_make_edit_icon_button($mod, $OUTPUT);
}
// Completion
$completion = $hidecompletion
? COMPLETION_TRACKING_NONE
: $completioninfo->is_enabled($mod);
if ($completion!=COMPLETION_TRACKING_NONE && isloggedin() &&
!isguestuser() && $mod->uservisible) {
$completiondata = $completioninfo->get_data($mod,true);
$completionicon = '';
if ($isediting) {
switch ($completion) {
case COMPLETION_TRACKING_MANUAL :
$completionicon = 'manual-enabled'; break;
case COMPLETION_TRACKING_AUTOMATIC :
$completionicon = 'auto-enabled'; break;
default: // wtf
}
} else if ($completion==COMPLETION_TRACKING_MANUAL) {
switch($completiondata->completionstate) {
case COMPLETION_INCOMPLETE:
$completionicon = 'manual-n'; break;
case COMPLETION_COMPLETE:
$completionicon = 'manual-y'; break;
}
} else { // Automatic
switch($completiondata->completionstate) {
case COMPLETION_INCOMPLETE:
$completionicon = 'auto-n'; break;
case COMPLETION_COMPLETE:
$completionicon = 'auto-y'; break;
case COMPLETION_COMPLETE_PASS:
$completionicon = 'auto-pass'; break;
case COMPLETION_COMPLETE_FAIL:
$completionicon = 'auto-fail'; break;
}
}
if ($completionicon) {
$imgsrc = $OUTPUT->pix_url('i/completion-'.$completionicon);
$imgalt = s(get_string('completion-alt-'.$completionicon, 'completion'));
if ($completion == COMPLETION_TRACKING_MANUAL && !$isediting) {
$imgtitle = s(get_string('completion-title-'.$completionicon, 'completion'));
$newstate =
$completiondata->completionstate==COMPLETION_COMPLETE
? COMPLETION_INCOMPLETE
: COMPLETION_COMPLETE;
// In manual mode the icon is a toggle form...
// If this completion state is used by the
// conditional activities system, we need to turn
// off the JS.
if (!empty($CFG->enableavailability) &&
condition_info::completion_value_used_as_condition($course, $mod)) {
$extraclass = ' preventjs';
} else {
$extraclass = '';
}
echo "
<form class='togglecompletion$extraclass' method='post' action='togglecompletion.php'><div>
<input type='hidden' name='id' value='{$mod->id}' />
<input type='hidden' name='sesskey' value='".sesskey()."' />
<input type='hidden' name='completionstate' value='$newstate' />
<input type='image' src='$imgsrc' alt='$imgalt' title='$imgtitle' />
</div></form>";
} else {
// In auto mode, or when editing, the icon is just an image
echo "<span class='autocompletion'>";
echo "<img src='$imgsrc' alt='$imgalt' title='$imgalt' /></span>";
}
}
}
// Show availability information (for someone who isn't allowed to
// see the activity itself, or for staff)
if (!$mod->uservisible) {
echo '<div class="availabilityinfo">'.$mod->availableinfo.'</div>';
} else if ($canviewhidden && !empty($CFG->enableavailability)) {
$ci = new condition_info($mod);
$fullinfo = $ci->get_full_information();
if($fullinfo) {
echo '<div class="availabilityinfo">'.get_string($mod->showavailability
? 'userrestriction_visible'
: 'userrestriction_hidden','condition',
$fullinfo).'</div>';
}
}
echo hook_show_activity_intro($mod);
echo html_writer::end_tag('div');
echo html_writer::end_tag('li')."\n";
}
} elseif ($ismoving) {
echo "<ul class=\"section\">\n";
}
if ($ismoving) {
echo '<li><a title="'.$strmovefull.'"'.
' href="'.$CFG->wwwroot.'/course/mod.php?movetosection='.$section->id.'&sesskey='.sesskey().'">'.
'<img class="movetarget" src="'.$OUTPUT->pix_url('movehere') . '" '.
' alt="'.$strmovehere.'" /></a></li>
';
}
if (!empty($section->sequence) || $ismoving) {
echo "</ul><!--class='section'-->\n\n";
}
}
function slides_make_outline($course,$topics_info,$sections,$editing){
// TOPICS OUTLINE list
echo '<li id="section--1" class="section outline clearfix" >';
echo "<ul id='section-outline'>\n";
// check activity completion and add to array where section id is key
$completionbysection = array();
$completioninfo = new completion_info($course);
$activitieswithcompletion = $completioninfo->get_activities();
// unknown > 0 = incomplete < complete
foreach($activitieswithcompletion as $activity){
$completionbysection[$activity->section] = "complete";
$activity_status_data = $completioninfo->get_data($activity->id, $USER->id);
if($activity_status_data->completionstate < 0) {
$completionbysection[$activity->section] = "unknown";
} else if ($activity_status_data->completionstate === 0 && $completionbysection[$activity->section] != "unknown") {
$completionbysection[$activity->section] = "incomplete";
}
}
//sort by id
for($i=0; $i<=$course->numsections; $i++) {
$section = $sections[$i];
$section_complete = isset($completionbysection[$section->id]) ? $completionbysection[$section->id] : "complete";
$sectionname = !empty($section->name) ? $section->name : "Topic " . $section->section;
$style = "left:" . $topics_info[$section->id]->x_offset . "; top:" .$topics_info[$section->id]->y_offset;
$css = $editing ? "$section_complete editing" : "$section_complete";
$css .= $section->visible ? "" : " hidden";
$css .= $course->marker == $i ? " highlight" : "";
echo '<li class="'. $css . '" id="topiclink' . $section->id . '" style="' .$style . ';">';
echo '<a href="view.php?id='.$course->id.'&topic='.$section->section.'" title="'.$sectionname.'" class="outline-link">' .$sectionname;
// echo "</a> $complete</li>\n";
// TODO: Fix complete
echo "</a></li>\n";
}
echo "</ul>\n";
echo "</li>\n";
}
function hook_show_activity_intro($mod){
global $DB;
try {
$intro = $DB->get_field($mod->modname, 'intro', array('id'=>$mod->instance));
} catch(Exception $err) {
$intro = "";
}
echo "<div class='mod-intro'>$intro</div>";
}
function hook_make_edit_icon_button($mod){
global $OUTPUT;
$iconimage = $OUTPUT->pix_url('i/icon');
echo '<span class="commands">' .
'<a href="format/slides/choose_modicon.php?module='.$mod->id.'&name='.$mod->name.'">' .
'<img src="format/slides/pix/i/icon.png" class="iconsmall" alt="" />' .
'</a></span>';
}
function format_slides_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload){
$filename = array_pop($args);
// TODO: check filepath
$filepath = $args ? '/'.implode('/', $args).'/' : '/';
$fs = get_file_storage();
//echo $context->id . 'format_slides ' . $filearea . " " . $args[0] . " " . $filepath . " " . $filename . "<br/>";
if (!$file = $fs->get_file($context->id, 'format_slides', $filearea, $args[0], "/", $filename)) {
send_file_not_found();
}
session_get_instance()->write_close();
send_stored_file($file, 60*60, 0, $forcedownload);
exit;
}
function format_slides_delete_course($courseid) {
global $DB;
// 1 remove icon files
$fs = get_file_storage();
$custom_icons = $DB->get_records("format_slides_modicons", array('course_id'=>$courseid));
foreach($custom_icons as $icon){
$ctx = get_context_instance(CONTEXT_MODULE, $icon->activity_id);
$file1 = $fs->get_file($ctx->id, "format_slides", "activity_icon", 0, "/", $icon->icon_up);
$file2 = $fs->get_file($ctx->id, "format_slides", "activity_icon", 0, "/", $icon->icon_over);
if($file1) $file1->delete();
if($file2) $file2->delete();
}
// 2 clear db
$DB->delete_records("format_slides_modicons", array("course_id"=>$courseid));
$DB->delete_records("format_slides", array("course_id"=>$courseid));
}