forked from arborrow/moodle-courseformat_grid
-
Notifications
You must be signed in to change notification settings - Fork 10
/
lib.php
executable file
·585 lines (494 loc) · 21.4 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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
<?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
*/
defined('MOODLE_INTERNAL') || die();
/**
* Indicates this format uses sections.
*
* @return bool Returns true
*/
function callback_grid_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_grid_load_content(&$navigation, $course, $coursenode) {
return $navigation->load_generic_course_sections(
$course, $coursenode, 'grid');
}
/**
* The string that is used to describe a section of the course
* e.g. Topic, Week...
*
* @return string
*/
function callback_grid_definition() {
return get_string('topic', 'format_grid');
}
/**
* The GET argument variable that is used to identify the section being
* viewed by the user (if there is one)
*
* @return string
*/
function callback_grid_request_key() {
return 'topic';
}
function callback_grid_get_section_name($course, $section) {
// We can't add a node without any text
if (!empty($section->name)) {
return format_string($section->name, true,
array('context' => get_context_instance(CONTEXT_COURSE, $course->id)));
} if ($section->section == 0) {
return get_string('topic0', 'format_grid');
} else {
return get_string('topic', 'format_grid').' '.$section->section;
}
}
/**
* Declares support for course AJAX features
*
* @see course_format_ajax_support()
* @return stdClass
*/
function callback_grid_ajax_support() {
$ajaxsupport = new stdClass();
$ajaxsupport->capable = true;
$ajaxsupport->testedbrowsers = array(
'MSIE' => 6.0, 'Gecko' => 20061111, 'Safari' => 531, 'Chrome' => 6.0);
return $ajaxsupport;
}
function _grid_moodle_url($url, array $params = null) {
return new moodle_url('/course/format/grid/'.$url, $params);
}
function _is_empty_text($text) {
return empty($text) ||
preg_match('/^(?:\s| )*$/si',
htmlentities($text, 0 /*ENT_HTML401*/, 'UTF-8', true));
}
function _grid_get_icon($course, $sectionid, $sectionnumber = 0) {
global $CFG, $DB;
if (!$sectionid)
return false;
if (!$sectionicon = $DB->get_record('format_grid_icon',
array('sectionid' => $sectionid))) {
$newicon = new stdClass();
$newicon->sectionid = $sectionid;
if (!$newicon->id = $DB->insert_record('format_grid_icon', $newicon, true)) {
throw new moodle_exception('invalidrecordid', 'format_grid', '',
'Could not create icon. Grid format database is not ready. An admin must visit the notification section.');
}
$sectionicon = false;
}
return $sectionicon;
}
//get section icon, if it doesnt exist create it.
function _get_summary_visibility($course) {
global $CFG, $DB;
if (!$summary_status = $DB->get_record('format_grid_summary', array('course_id' => $course))) {
$new_status = new stdClass();
$new_status->course_id = $course;
$new_status->show_summary = 1;
if (!$new_status->id = $DB->insert_record('format_grid_summary', $new_status)) {
throw new moodle_exception('invalidrecordid', 'format_grid', '',
'Could not set summary status. Grid format database is not ready. An admin must visit the notification section.');
}
$summary_status = $new_status;
}
return $summary_status;
}
//Checks whether there has been new activity in section $section
function _new_activity($section, $course) {
global $CFG, $USER, $DB;
if (isset($USER->lastcourseaccess[$course->id])) {
$course->lastaccess = $USER->lastcourseaccess[$course->id];
} else {
$course->lastaccess = 0;
}
$sql = "SELECT id, url FROM {$CFG->prefix}log ".
'WHERE course = :courseid AND time > :lastaccess AND action = :edit';
$params = array(
'courseid' => $course->id,
'lastaccess'=> $course->lastaccess,
'edit'=>'editsection');
$activity = $DB->get_records_sql($sql, $params);
foreach($activity as $url_obj) {
$list = explode('=', $url_obj->url);
if($section->id == $list[1])
return true;
}
return false;
}
//Attempts to return a 40 character title for the section icon.
//If section names are set, they are used. Otherwise it scans
//the summary for what looks like the first line.
function _get_title($section) {
$title = is_object($section) && isset($section->name) &&
is_string($section->name)?trim($section->name):'';
if (!empty($title)) {
// Apply filters and clean tags
$title = trim(format_string($section->name, true));
}
if (empty($title)) {
$title = trim(format_text($section->summary));
// Finds first header content. If it doesn't found,
// trying to find first paragraph.
foreach(array('h[1-6]', 'p') as $tag) {
if (preg_match('#<('.$tag.')\b[^>]*>(?P<text>.*?)</\1>#si', $title, $m)) {
if (!_is_empty_text($m['text'])) {
$title = $m['text'];
break;
}
}
}
$title = trim(clean_param($title, PARAM_NOTAGS));
}
if (strlen($title) > 40) {
$title = _text_limit($title, 40);
}
return $title;
}
// Cutes long texts up to certain length without breaking words
function _text_limit($text, $length, $replacer = '...') {
if(strlen($text) > $length) {
$text = wordwrap($text, $length, "\n", true);
$pos = strpos($text, "\n");
if ($pos === false)
$pos = $length;
$text = trim(substr($text, 0, $pos)) . $replacer;
}
return $text;
}
function _make_block_topic0($section, $top) {
global $OUTPUT, $context,
$sections, $course,
$editing, $has_cap_update,
$mods, $modnames, $modnamesused,
$url_pic_edit, $str_edit_summary;
if (!is_numeric($section) || !array_key_exists($section, $sections))
return false;
$thissection = $sections[$section];
if (!is_object($thissection))
return false;
$summaryformatoptions = new stdClass();
$summaryformatoptions->noclean = true;
$summaryformatoptions->overflowdiv = true;
if ($top) {
echo html_writer::start_tag('ul', array('class'=>'topicscss'));
}
echo html_writer::start_tag('li', array(
'id' =>'section-0',
'class'=>'section main' . ($top ? '' :' grid_section')));
echo html_writer::tag('div', ' ', array('class'=>'left side'));
echo html_writer::tag('div', ' ', array('class'=>'right side'));
echo html_writer::start_tag('div', array('class'=>'content'));
echo html_writer::start_tag('div', array('class'=>'summary'));
$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
if ($top) {
$summarytext = file_rewrite_pluginfile_urls($thissection->summary, 'pluginfile.php', $coursecontext->id, 'course', 'section', $thissection->id);
echo format_text($summarytext, FORMAT_HTML, $summaryformatoptions);
} else {
$summarytext = file_rewrite_pluginfile_urls($thissection->summary, 'pluginfile.php', $coursecontext->id, 'course', 'section', $thissection->id);
$summarytext = file_rewrite_pluginfile_urls($thissection->summary, 'pluginfile.php', $coursecontext->id, 'course', 'section', $thissection->id);
echo format_text($summarytext, FORMAT_HTML, $summaryformatoptions);
}
if ($editing && $has_cap_update) {
$link = html_writer::link(
new moodle_url('editsection.php', array('id' => $thissection->id)),
html_writer::empty_tag('img', array(
'src' => $url_pic_edit,
'alt' => $str_edit_summary,
'class' => 'icon edit')),
array('title' => $str_edit_summary));
echo $top ? html_writer::tag('p', $link) : $link;
}
echo html_writer::end_tag('div');
print_section($course, $thissection, $mods, $modnamesused);
if ($editing) {
print_section_add_menus($course, $section, $modnames);
if ($top) {
$str_hide_summary = get_string('hide_summary', 'format_grid');
$str_hide_summary_alt = get_string('hide_summary_alt', 'format_grid');
echo html_writer::link(
_grid_moodle_url('mod_summary.php', array(
'sesskey' => sesskey(),
'course' => $course->id,
'showsummary' => 0)),
html_writer::empty_tag('img', array(
'src' => $OUTPUT->pix_url('into_grid', 'format_grid'),
'alt' => $str_hide_summary_alt)) . ' ' . $str_hide_summary,
array('title' => $str_hide_summary_alt));
}
}
echo html_writer::end_tag('div');
echo html_writer::end_tag('li');
if ($top) {
echo html_writer::end_tag('ul');
}
return true;
}
function _make_block_icon_topics($without_topic0) {
global $OUTPUT, $USER, $DB, $context,
$sections, $course, $editing,
$has_cap_update, $has_cap_vishidsect, $url_pic_edit;
$str_topic = get_string('topic', 'format_grid');
$url_pic_new_activity = $OUTPUT->pix_url('new_activity', 'format_grid');
if ($editing) {
$str_edit_image = get_string('editimage', 'format_grid');
$str_edit_image_alt = get_string('editimage_alt', 'format_grid');
}
//start at 1 to skip the summary block
//or include the summary block if it's in the grid display
for($section = $without_topic0 ? 1 : 0;
$section <= $course->numsections; $section++) {
if (!empty($sections[$section])) {
$thissection = $sections[$section];
} else {
// Create a new section structure
$thissection = new stdClass();
$thissection->course = $course->id;
$thissection->section = $section;
$thissection->summary = "$str_topic $section";
$thissection->visible = 1;
if (!$thissection->id = $DB->insert_record('course_sections', $thissection)) {
throw new moodle_exception('invalidrecordid', 'format_grid', '',
'Error inserting new topic.');
}
$sections[$section] = $thissection;
}
//check if course is visible to user, if so show course
if ($has_cap_vishidsect || $thissection->visible || !$course->hiddensections) {
$str_title = _get_title($thissection);
if($section == 0 && _is_empty_text($str_title)) {
$str_title = get_string('general_information', 'format_grid');
}
//Get the module icon
if ($editing && $has_cap_update) {
$onclickevent = "select_topic_edit(event, {$thissection->section})";
} else {
$onclickevent = "select_topic(event, {$thissection->section})";
}
echo html_writer::start_tag('li');
echo html_writer::start_tag('a', array(
'href' => '#section-' . $thissection->section,
'class' => 'icon_link',
'onclick' => $onclickevent));
echo html_writer::tag('p', $str_title, array('class' => 'icon_content'));
if(_new_activity($thissection, $course)) {
echo html_writer::empty_tag('img', array(
'class' => 'new_activity',
'src' => $url_pic_new_activity,
'alt' => ''));
}
echo html_writer::start_tag('div', array('class'=>'image_holder'));
$sectionicon = _grid_get_icon(
$course, $thissection->id, $section);
if(is_object($sectionicon) && !empty($sectionicon->imagepath)) {
echo html_writer::empty_tag('img', array(
'src' => moodle_url::make_pluginfile_url(
$context->id, 'course', 'section', $thissection->id,
'/', $sectionicon->imagepath), 'alt' => ''));
} else if($section == 0) {
echo html_writer::empty_tag('img', array(
'src' => $OUTPUT->pix_url('info', 'format_grid'),
'alt' => ''));
}
echo html_writer::end_tag('div');
echo html_writer::end_tag('a');
if ($editing && $has_cap_update) {
echo html_writer::link(
_grid_moodle_url('editimage.php', array(
'sectionid' => $thissection->id,
'contextid' => $context->id,
'userid' => $USER->id)),
html_writer::empty_tag('img', array(
'src' => $url_pic_edit,
'alt' => $str_edit_image_alt)) . ' ' . $str_edit_image,
array('title' => $str_edit_image_alt));
if($section == 0) {
$str_display_summary = get_string('display_summary', 'format_grid');
$str_display_summary_alt = get_string('display_summary_alt', 'format_grid');
echo html_writer::empty_tag('br') . html_writer::link(
_grid_moodle_url('mod_summary.php', array(
'sesskey' => sesskey(),
'course' => $course->id,
'showsummary' => 1)),
html_writer::empty_tag('img', array(
'src' => $OUTPUT->pix_url('out_of_grid', 'format_grid'),
'alt' => $str_display_summary_alt)) . ' ' . $str_display_summary,
array('title' => $str_display_summary_alt));
}
}
echo html_writer::end_tag('li');
}
}
}
/// If currently moving a file then show the current clipboard
function _make_block_show_clipboard_if_file_moving() {
global $USER, $course;
if (is_object($course) && ismoving($course->id)) {
$str_cancel= get_string('cancel');
$str_activity_clipboard = clean_param(format_string(
get_string('activityclipboard', '', $USER->activitycopyname)),
PARAM_NOTAGS);
$stractivityclipboard .= ' ('
.html_writer::link(new moodle_url('/mod.php', array(
'cancelcopy' => 'true',
'sesskey' => sesskey())), $str_cancel);
echo html_writer::tag('li', $stractivityclipboard,
array('class' => 'clipboard'));
}
}
function _make_block_topics() {
global $OUTPUT,
$course, $sections, $editing,
$has_cap_update, $has_cap_vishidsect,
$mods, $modnames, $modnamesused,
$str_edit_summary, $url_pic_edit;
$summaryformatoptions = new stdClass();
$summaryformatoptions->noclean = true;
$summaryformatoptions->overflowdiv = true;
$str_hidden_topic = get_string('hidden_topic', 'format_grid');
if ($editing && $has_cap_update) {
$str_move_up = get_string('moveup');
$str_move_down = get_string('movedown');
$str_topic_hide = get_string('hidefromothers', 'format_grid');
$str_topic_show = get_string('showfromothers', 'format_grid');
$url_pic_move_up = $OUTPUT->pix_url('t/up');
$url_pic_move_down = $OUTPUT->pix_url('t/down');
$url_pic_topic_hide = $OUTPUT->pix_url('i/hide');
$url_pic_topic_show = $OUTPUT->pix_url('i/show');
}
for($section = 1; $section <= $course->numsections; $section++) {
if (empty($sections[$section])) {
//Section should have been created in the icons section above. If it's empty then its an error.
throw new coding_exception('Error, section ' . $section . ' not found!');
}
$thissection = $sections[$section];
if (!$has_cap_vishidsect && !$thissection->visible && $course->hiddensections) {
continue;
}
$sectionstyle = 'section main';
if (!$thissection->visible) {
$sectionstyle .= ' hidden';
}
$sectionstyle .= ' grid_section';
echo html_writer::start_tag('li', array(
'id' => 'section-' . $section,
'class' => $sectionstyle));
// Note, 'right side' is BEFORE content.
echo html_writer::tag('div', ' ', array('class'=>'left side'));
echo html_writer::start_tag('div', array('class' => 'right side'));
if ($editing && $has_cap_update) {
// Show the hide/show eye
if ($thissection->visible) {
$op_topic_visible = 'hide';
$str_topic_visible_text = $str_topic_hide;
$url_pic_topic_visible = $url_pic_topic_hide;
} else {
$op_topic_visible = 'show';
$str_topic_visible_text = $str_topic_show;
$url_pic_topic_visible = $url_pic_topic_show;
}
echo html_writer::link(
new moodle_url('/course/view.php#section-' . $section, array(
'id' => $course->id,
$op_topic_visible => $section,
'sesskey' => sesskey())),
html_writer::empty_tag('img', array(
'src' => $url_pic_topic_visible,
'class' => 'icon ' . $op_topic_visible,
'alt' => $str_topic_visible_text)),
array('title' => $str_topic_visible_text, 'class' => 'editing_showhide'));
// Add a arrow to move section up
if ($section > 1) {
echo _move_section_arrow($section, $course, -1, 'up',
$str_move_up, $url_pic_move_up, 'moveup');
}
// Add a arrow to move section down
if ($section < $course->numsections) {
echo _move_section_arrow($section, $course, -1, 'down',
$str_move_down, $url_pic_move_down, 'movedown');
}
}
echo html_writer::end_tag('div');
echo html_writer::start_tag('div', array('class' => 'content'));
if ($has_cap_vishidsect || $thissection->visible) {
//if visible
if (!empty($thissection->name)) {
echo format_text($OUTPUT->heading(
$thissection->name, 3, 'sectionname'), FORMAT_HTML);
}
echo html_writer::start_tag('div', array('class' => 'summary'));
$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
$summarytext = file_rewrite_pluginfile_urls($thissection->summary, 'pluginfile.php', $coursecontext->id, 'course', 'section', $thissection->id);
echo format_text($summarytext, FORMAT_HTML, $summaryformatoptions);
if ($editing && $has_cap_update) {
echo html_writer::link(
new moodle_url('editsection.php', array('id' => $thissection->id)),
html_writer::empty_tag('img', array(
'src' => $url_pic_edit,
'alt' => $str_edit_summary,
'class' => 'icon edit')),
array('title' => $str_edit_summary));
}
echo html_writer::end_tag('div');
print_section($course, $thissection, $mods, $modnamesused);
if ($editing) {
print_section_add_menus($course, $section, $modnames);
}
} else {
$str_title = _get_title($thissection->summary);
echo html_writer::tag('h2', $str_title);
echo html_writer::tag('p', $str_hidden_topic);
}
echo html_writer::end_tag('div');
echo html_writer::end_tag('li');
}
}
function _move_section_arrow($section, $course,
$op_move_delta, $op_move_style,
$str_move_text, $url_pic_move, $anchorclass = '') {
$url = new moodle_url('/course/view.php#section-'.($section + $op_move_delta), array(
'id' => $course->id,
'random' => rand(1, 10000),
'section' => $section,
'move' => $op_move_delta,
'sesskey' => sesskey()));
$img = html_writer::empty_tag('img', array(
'src' => $url_pic_move,
'alt' => $str_move_text,
'class' => 'icon ' . $op_move_style));
return html_writer::link($url, $img, array(
'title' => $str_move_text,
'class' => $anchorclass
));
}