forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
2232 lines (2046 loc) · 99.9 KB
/
renderer.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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* Renderer for use with the course section and all the goodness that falls
* within it.
*
* This renderer should contain methods useful to courses, and categories.
*
* @package moodlecore
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* The core course renderer
*
* Can be retrieved with the following:
* $renderer = $PAGE->get_renderer('core','course');
*/
class core_course_renderer extends plugin_renderer_base {
const COURSECAT_SHOW_COURSES_NONE = 0; /* do not show courses at all */
const COURSECAT_SHOW_COURSES_COUNT = 5; /* do not show courses but show number of courses next to category name */
const COURSECAT_SHOW_COURSES_COLLAPSED = 10;
const COURSECAT_SHOW_COURSES_AUTO = 15; /* will choose between collapsed and expanded automatically */
const COURSECAT_SHOW_COURSES_EXPANDED = 20;
const COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT = 30;
/**
* A cache of strings
* @var stdClass
*/
protected $strings;
/**
* Override the constructor so that we can initialise the string cache
*
* @param moodle_page $page
* @param string $target
*/
public function __construct(moodle_page $page, $target) {
$this->strings = new stdClass;
parent::__construct($page, $target);
$this->add_modchoosertoggle();
}
/**
* Adds the item in course settings navigation to toggle modchooser
*
* Theme can overwrite as an empty function to exclude it (for example if theme does not
* use modchooser at all)
*/
protected function add_modchoosertoggle() {
global $CFG;
static $modchoosertoggleadded = false;
// Add the module chooser toggle to the course page
if ($modchoosertoggleadded || $this->page->state > moodle_page::STATE_PRINTING_HEADER ||
$this->page->course->id == SITEID ||
!$this->page->user_is_editing() ||
!($context = context_course::instance($this->page->course->id)) ||
!has_capability('moodle/course:update', $context) ||
!course_ajax_enabled($this->page->course) ||
!($coursenode = $this->page->settingsnav->find('courseadmin', navigation_node::TYPE_COURSE)) ||
!$coursenode->get('editsettings')) {
// too late or we are on site page or we could not find the course settings node
// or we are not allowed to edit
return;
}
$modchoosertoggleadded = true;
if ($this->page->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
// We are on the course page, retain the current page params e.g. section.
$modchoosertoggleurl = clone($this->page->url);
} else {
// Edit on the main course page.
$modchoosertoggleurl = new moodle_url('/course/view.php', array('id' => $this->page->course->id,
'return' => $this->page->url->out_as_local_url(false)));
}
$modchoosertoggleurl->param('sesskey', sesskey());
if ($usemodchooser = get_user_preferences('usemodchooser', $CFG->modchooserdefault)) {
$modchoosertogglestring = get_string('modchooserdisable', 'moodle');
$modchoosertoggleurl->param('modchooser', 'off');
} else {
$modchoosertogglestring = get_string('modchooserenable', 'moodle');
$modchoosertoggleurl->param('modchooser', 'on');
}
$modchoosertoggle = navigation_node::create($modchoosertogglestring, $modchoosertoggleurl, navigation_node::TYPE_SETTING);
$coursenode->add_node($modchoosertoggle, 'editsettings');
$modchoosertoggle->add_class('modchoosertoggle');
$modchoosertoggle->add_class('visibleifjs');
user_preference_allow_ajax_update('usemodchooser', PARAM_BOOL);
}
/**
* Renders course info box.
*
* @param stdClass|course_in_list $course
* @return string
*/
public function course_info_box(stdClass $course) {
$content = '';
$content .= $this->output->box_start('generalbox info');
$chelper = new coursecat_helper();
$chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED);
$content .= $this->coursecat_coursebox($chelper, $course);
$content .= $this->output->box_end();
return $content;
}
/**
* Renderers a structured array of courses and categories into a nice XHTML tree structure.
*
* @deprecated since 2.5
*
* Please see http://docs.moodle.org/dev/Courses_lists_upgrade_to_2.5
*
* @param array $ignored argument ignored
* @return string
*/
public final function course_category_tree(array $ignored) {
debugging('Function core_course_renderer::course_category_tree() is deprecated, please use frontpage_combo_list()', DEBUG_DEVELOPER);
return $this->frontpage_combo_list();
}
/**
* Renderers a category for use with course_category_tree
*
* @deprecated since 2.5
*
* Please see http://docs.moodle.org/dev/Courses_lists_upgrade_to_2.5
*
* @param array $category
* @param int $depth
* @return string
*/
protected final function course_category_tree_category(stdClass $category, $depth=1) {
debugging('Function core_course_renderer::course_category_tree_category() is deprecated', DEBUG_DEVELOPER);
return '';
}
/**
* Build the HTML for the module chooser javascript popup
*
* @param array $modules A set of modules as returned form @see
* get_module_metadata
* @param object $course The course that will be displayed
* @return string The composed HTML for the module
*/
public function course_modchooser($modules, $course) {
static $isdisplayed = false;
if ($isdisplayed) {
return '';
}
$isdisplayed = true;
// Add the module chooser
$this->page->requires->yui_module('moodle-course-modchooser',
'M.course.init_chooser',
array(array('courseid' => $course->id, 'closeButtonTitle' => get_string('close', 'editor')))
);
$this->page->requires->strings_for_js(array(
'addresourceoractivity',
'modchooserenable',
'modchooserdisable',
), 'moodle');
// Add the header
$header = html_writer::tag('div', get_string('addresourceoractivity', 'moodle'),
array('class' => 'hd choosertitle'));
$formcontent = html_writer::start_tag('form', array('action' => new moodle_url('/course/jumpto.php'),
'id' => 'chooserform', 'method' => 'post'));
$formcontent .= html_writer::start_tag('div', array('id' => 'typeformdiv'));
$formcontent .= html_writer::tag('input', '', array('type' => 'hidden', 'id' => 'course',
'name' => 'course', 'value' => $course->id));
$formcontent .= html_writer::tag('input', '',
array('type' => 'hidden', 'class' => 'jump', 'name' => 'jump', 'value' => ''));
$formcontent .= html_writer::tag('input', '', array('type' => 'hidden', 'name' => 'sesskey',
'value' => sesskey()));
$formcontent .= html_writer::end_tag('div');
// Put everything into one tag 'options'
$formcontent .= html_writer::start_tag('div', array('class' => 'options'));
$formcontent .= html_writer::tag('div', get_string('selectmoduletoviewhelp', 'moodle'),
array('class' => 'instruction'));
// Put all options into one tag 'alloptions' to allow us to handle scrolling
$formcontent .= html_writer::start_tag('div', array('class' => 'alloptions'));
// Activities
$activities = array_filter($modules, create_function('$mod', 'return ($mod->archetype !== MOD_ARCHETYPE_RESOURCE && $mod->archetype !== MOD_ARCHETYPE_SYSTEM);'));
if (count($activities)) {
$formcontent .= $this->course_modchooser_title('activities');
$formcontent .= $this->course_modchooser_module_types($activities);
}
// Resources
$resources = array_filter($modules, create_function('$mod', 'return ($mod->archetype === MOD_ARCHETYPE_RESOURCE);'));
if (count($resources)) {
$formcontent .= $this->course_modchooser_title('resources');
$formcontent .= $this->course_modchooser_module_types($resources);
}
$formcontent .= html_writer::end_tag('div'); // modoptions
$formcontent .= html_writer::end_tag('div'); // types
$formcontent .= html_writer::start_tag('div', array('class' => 'submitbuttons'));
$formcontent .= html_writer::tag('input', '',
array('type' => 'submit', 'name' => 'submitbutton', 'class' => 'submitbutton', 'value' => get_string('add')));
$formcontent .= html_writer::tag('input', '',
array('type' => 'submit', 'name' => 'addcancel', 'class' => 'addcancel', 'value' => get_string('cancel')));
$formcontent .= html_writer::end_tag('div');
$formcontent .= html_writer::end_tag('form');
// Wrap the whole form in a div
$formcontent = html_writer::tag('div', $formcontent, array('id' => 'chooseform'));
// Put all of the content together
$content = $formcontent;
$content = html_writer::tag('div', $content, array('class' => 'choosercontainer'));
return $header . html_writer::tag('div', $content, array('class' => 'chooserdialoguebody'));
}
/**
* Build the HTML for a specified set of modules
*
* @param array $modules A set of modules as used by the
* course_modchooser_module function
* @return string The composed HTML for the module
*/
protected function course_modchooser_module_types($modules) {
$return = '';
foreach ($modules as $module) {
if (!isset($module->types)) {
$return .= $this->course_modchooser_module($module);
} else {
$return .= $this->course_modchooser_module($module, array('nonoption'));
foreach ($module->types as $type) {
$return .= $this->course_modchooser_module($type, array('option', 'subtype'));
}
}
}
return $return;
}
/**
* Return the HTML for the specified module adding any required classes
*
* @param object $module An object containing the title, and link. An
* icon, and help text may optionally be specified. If the module
* contains subtypes in the types option, then these will also be
* displayed.
* @param array $classes Additional classes to add to the encompassing
* div element
* @return string The composed HTML for the module
*/
protected function course_modchooser_module($module, $classes = array('option')) {
$output = '';
$output .= html_writer::start_tag('div', array('class' => implode(' ', $classes)));
$output .= html_writer::start_tag('label', array('for' => 'module_' . $module->name));
if (!isset($module->types)) {
$output .= html_writer::tag('input', '', array('type' => 'radio',
'name' => 'jumplink', 'id' => 'module_' . $module->name, 'value' => $module->link));
}
$output .= html_writer::start_tag('span', array('class' => 'modicon'));
if (isset($module->icon)) {
// Add an icon if we have one
$output .= $module->icon;
}
$output .= html_writer::end_tag('span');
$output .= html_writer::tag('span', $module->title, array('class' => 'typename'));
if (!isset($module->help)) {
// Add help if found
$module->help = get_string('nohelpforactivityorresource', 'moodle');
}
// Format the help text using markdown with the following options
$options = new stdClass();
$options->trusted = false;
$options->noclean = false;
$options->smiley = false;
$options->filter = false;
$options->para = true;
$options->newlines = false;
$options->overflowdiv = false;
$module->help = format_text($module->help, FORMAT_MARKDOWN, $options);
$output .= html_writer::tag('span', $module->help, array('class' => 'typesummary'));
$output .= html_writer::end_tag('label');
$output .= html_writer::end_tag('div');
return $output;
}
protected function course_modchooser_title($title, $identifier = null) {
$module = new stdClass();
$module->name = $title;
$module->types = array();
$module->title = get_string($title, $identifier);
$module->help = '';
return $this->course_modchooser_module($module, array('moduletypetitle'));
}
/**
* Renders HTML for displaying the sequence of course module editing buttons
*
* @see course_get_cm_edit_actions()
*
* @param array $actions array of action_link or pix_icon objects
* @return string
*/
public function course_section_cm_edit_actions($actions) {
$output = html_writer::start_tag('span', array('class' => 'commands'));
foreach ($actions as $action) {
if ($action instanceof renderable) {
$output .= $this->output->render($action);
} else {
$output .= $action;
}
}
$output .= html_writer::end_tag('span');
return $output;
}
/**
* Renders HTML for the menus to add activities and resources to the current course
*
* Note, if theme overwrites this function and it does not use modchooser,
* see also {@link core_course_renderer::add_modchoosertoggle()}
*
* @param stdClass $course
* @param int $section relative section number (field course_sections.section)
* @param int $sectionreturn The section to link back to
* @param array $displayoptions additional display options, for example blocks add
* option 'inblock' => true, suggesting to display controls vertically
* @return string
*/
function course_section_add_cm_control($course, $section, $sectionreturn = null, $displayoptions = array()) {
global $CFG;
$vertical = !empty($displayoptions['inblock']);
// check to see if user can add menus and there are modules to add
if (!has_capability('moodle/course:manageactivities', context_course::instance($course->id))
|| !$this->page->user_is_editing()
|| !($modnames = get_module_types_names()) || empty($modnames)) {
return '';
}
// Retrieve all modules with associated metadata
$modules = get_module_metadata($course, $modnames, $sectionreturn);
$urlparams = array('section' => $section);
// We'll sort resources and activities into two lists
$activities = array(MOD_CLASS_ACTIVITY => array(), MOD_CLASS_RESOURCE => array());
foreach ($modules as $module) {
if (!array_key_exists($module->archetype, $activities)) {
// System modules cannot be added by user, do not add to dropdown
} else if (isset($module->types)) {
// This module has a subtype
// NOTE: this is legacy stuff, module subtypes are very strongly discouraged!!
$subtypes = array();
foreach ($module->types as $subtype) {
$link = $subtype->link->out(true, $urlparams);
$subtypes[$link] = $subtype->title;
}
// Sort module subtypes into the list
if (!empty($module->title)) {
// This grouping has a name
$activities[$module->archetype][] = array($module->title => $subtypes);
} else {
// This grouping does not have a name
$activities[$module->archetype] = array_merge($activities[$module->archetype], $subtypes);
}
} else {
// This module has no subtypes
$link = $module->link->out(true, $urlparams);
$activities[$module->archetype][$link] = $module->title;
}
}
$straddactivity = get_string('addactivity');
$straddresource = get_string('addresource');
$sectionname = get_section_name($course, $section);
$strresourcelabel = get_string('addresourcetosection', null, $sectionname);
$stractivitylabel = get_string('addactivitytosection', null, $sectionname);
$output = html_writer::start_tag('div', array('class' => 'section_add_menus', 'id' => 'add_menus-section-' . $section));
if (!$vertical) {
$output .= html_writer::start_tag('div', array('class' => 'horizontal'));
}
if (!empty($activities[MOD_CLASS_RESOURCE])) {
$select = new url_select($activities[MOD_CLASS_RESOURCE], '', array(''=>$straddresource), "ressection$section");
$select->set_help_icon('resources');
$select->set_label($strresourcelabel, array('class' => 'accesshide'));
$output .= $this->output->render($select);
}
if (!empty($activities[MOD_CLASS_ACTIVITY])) {
$select = new url_select($activities[MOD_CLASS_ACTIVITY], '', array(''=>$straddactivity), "section$section");
$select->set_help_icon('activities');
$select->set_label($stractivitylabel, array('class' => 'accesshide'));
$output .= $this->output->render($select);
}
if (!$vertical) {
$output .= html_writer::end_tag('div');
}
$output .= html_writer::end_tag('div');
if (course_ajax_enabled($course) && $course->id == $this->page->course->id) {
// modchooser can be added only for the current course set on the page!
$straddeither = get_string('addresourceoractivity');
// The module chooser link
$modchooser = html_writer::start_tag('div', array('class' => 'mdl-right'));
$modchooser.= html_writer::start_tag('div', array('class' => 'section-modchooser'));
$icon = $this->output->pix_icon('t/add', '');
$span = html_writer::tag('span', $straddeither, array('class' => 'section-modchooser-text'));
$modchooser .= html_writer::tag('span', $icon . $span, array('class' => 'section-modchooser-link'));
$modchooser.= html_writer::end_tag('div');
$modchooser.= html_writer::end_tag('div');
// Wrap the normal output in a noscript div
$usemodchooser = get_user_preferences('usemodchooser', $CFG->modchooserdefault);
if ($usemodchooser) {
$output = html_writer::tag('div', $output, array('class' => 'hiddenifjs addresourcedropdown'));
$modchooser = html_writer::tag('div', $modchooser, array('class' => 'visibleifjs addresourcemodchooser'));
} else {
// If the module chooser is disabled, we need to ensure that the dropdowns are shown even if javascript is disabled
$output = html_writer::tag('div', $output, array('class' => 'show addresourcedropdown'));
$modchooser = html_writer::tag('div', $modchooser, array('class' => 'hide addresourcemodchooser'));
}
$output = $this->course_modchooser($modules, $course) . $modchooser . $output;
}
return $output;
}
/**
* Renders html to display a course search form
*
* @param string $value default value to populate the search field
* @param string $format display format - 'plain' (default), 'short' or 'navbar'
* @return string
*/
function course_search_form($value = '', $format = 'plain') {
static $count = 0;
$formid = 'coursesearch';
if ((++$count) > 1) {
$formid .= $count;
}
switch ($format) {
case 'navbar' :
$formid = 'coursesearchnavbar';
$inputid = 'navsearchbox';
$inputsize = 20;
break;
case 'short' :
$inputid = 'shortsearchbox';
$inputsize = 12;
break;
default :
$inputid = 'coursesearchbox';
$inputsize = 30;
}
$strsearchcourses= get_string("searchcourses");
$searchurl = new moodle_url('/course/search.php');
$output = html_writer::start_tag('form', array('id' => $formid, 'action' => $searchurl, 'method' => 'get'));
$output .= html_writer::start_tag('fieldset', array('class' => 'coursesearchbox invisiblefieldset'));
$output .= html_writer::tag('label', $strsearchcourses.': ', array('for' => $inputid));
$output .= html_writer::empty_tag('input', array('type' => 'text', 'id' => $inputid,
'size' => $inputsize, 'name' => 'search', 'value' => s($value)));
$output .= html_writer::empty_tag('input', array('type' => 'submit',
'value' => get_string('go')));
$output .= html_writer::end_tag('fieldset');
$output .= html_writer::end_tag('form');
return $output;
}
/**
* Renders html for completion box on course page
*
* If completion is disabled, returns empty string
* If completion is automatic, returns an icon of the current completion state
* If completion is manual, returns a form (with an icon inside) that allows user to
* toggle completion
*
* @param stdClass $course course object
* @param completion_info $completioninfo completion info for the course, it is recommended
* to fetch once for all modules in course/section for performance
* @param cm_info $mod module to show completion for
* @param array $displayoptions display options, not used in core
* @return string
*/
public function course_section_cm_completion($course, &$completioninfo, cm_info $mod, $displayoptions = array()) {
global $CFG;
$output = '';
if (!empty($displayoptions['hidecompletion']) || !isloggedin() || isguestuser() || !$mod->uservisible) {
return $output;
}
if ($completioninfo === null) {
$completioninfo = new completion_info($course);
}
$completion = $completioninfo->is_enabled($mod);
if ($completion == COMPLETION_TRACKING_NONE) {
return $output;
}
$completiondata = $completioninfo->get_data($mod, true);
$completionicon = '';
if ($this->page->user_is_editing()) {
switch ($completion) {
case COMPLETION_TRACKING_MANUAL :
$completionicon = 'manual-enabled'; break;
case COMPLETION_TRACKING_AUTOMATIC :
$completionicon = 'auto-enabled'; break;
}
} 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) {
$formattedname = $mod->get_formatted_name();
$imgalt = get_string('completion-alt-' . $completionicon, 'completion', $formattedname);
if ($completion == COMPLETION_TRACKING_MANUAL && !$this->page->user_is_editing()) {
$imgtitle = get_string('completion-title-' . $completionicon, 'completion', $formattedname);
$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.
$extraclass = '';
if (!empty($CFG->enableavailability) &&
condition_info::completion_value_used_as_condition($course, $mod)) {
$extraclass = ' preventjs';
}
$output .= html_writer::start_tag('form', array('method' => 'post',
'action' => new moodle_url('/course/togglecompletion.php'),
'class' => 'togglecompletion'. $extraclass));
$output .= html_writer::start_tag('div');
$output .= html_writer::empty_tag('input', array(
'type' => 'hidden', 'name' => 'id', 'value' => $mod->id));
$output .= html_writer::empty_tag('input', array(
'type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
$output .= html_writer::empty_tag('input', array(
'type' => 'hidden', 'name' => 'modulename', 'value' => $mod->name));
$output .= html_writer::empty_tag('input', array(
'type' => 'hidden', 'name' => 'completionstate', 'value' => $newstate));
$output .= html_writer::empty_tag('input', array(
'type' => 'image',
'src' => $this->output->pix_url('i/completion-'.$completionicon),
'alt' => $imgalt, 'title' => $imgtitle,
'aria-live' => 'polite'));
$output .= html_writer::end_tag('div');
$output .= html_writer::end_tag('form');
} else {
// In auto mode, or when editing, the icon is just an image
$completionpixicon = new pix_icon('i/completion-'.$completionicon, $imgalt, '',
array('title' => $imgalt));
$output .= html_writer::tag('span', $this->output->render($completionpixicon),
array('class' => 'autocompletion'));
}
}
return $output;
}
/**
* Checks if course module has any conditions that may make it unavailable for
* all or some of the students
*
* This function is internal and is only used to create CSS classes for the module name/text
*
* @param cm_info $mod
* @return bool
*/
protected function is_cm_conditionally_hidden(cm_info $mod) {
global $CFG;
$conditionalhidden = false;
if (!empty($CFG->enableavailability)) {
$conditionalhidden = $mod->availablefrom > time() ||
($mod->availableuntil && $mod->availableuntil < time()) ||
count($mod->conditionsgrade) > 0 ||
count($mod->conditionscompletion) > 0;
}
return $conditionalhidden;
}
/**
* Renders html to display a name with the link to the course module on a course page
*
* If module is unavailable for user but still needs to be displayed
* in the list, just the name is returned without a link
*
* Note, that for course modules that never have separate pages (i.e. labels)
* this function return an empty string
*
* @param cm_info $mod
* @param array $displayoptions
* @return string
*/
public function course_section_cm_name(cm_info $mod, $displayoptions = array()) {
global $CFG;
$output = '';
if (!$mod->uservisible &&
(empty($mod->showavailability) || empty($mod->availableinfo))) {
// nothing to be displayed to the user
return $output;
}
$url = $mod->get_url();
if (!$url) {
return $output;
}
//Accessibility: for files get description via icon, this is very ugly hack!
$instancename = $mod->get_formatted_name();
$altname = '';
$altname = $mod->modfullname;
// Avoid unnecessary duplication: if e.g. a forum name already
// includes the word forum (or Forum, etc) then it is unhelpful
// to include that in the accessible description that is added.
if (false !== strpos(textlib::strtolower($instancename),
textlib::strtolower($altname))) {
$altname = '';
}
// File type after name, for alphabetic lists (screen reader).
if ($altname) {
$altname = get_accesshide(' '.$altname);
}
// For items which are hidden but available to current user
// ($mod->uservisible), we show those as dimmed only if the user has
// viewhiddenactivities, so that teachers see 'items which might not
// be available to some students' dimmed but students do not see 'item
// which is actually available to current student' dimmed.
$conditionalhidden = $this->is_cm_conditionally_hidden($mod);
$accessiblebutdim = (!$mod->visible || $conditionalhidden) &&
(!$mod->uservisible || has_capability('moodle/course:viewhiddenactivities',
context_course::instance($mod->course)));
$linkclasses = '';
$accesstext = '';
$textclasses = '';
if ($accessiblebutdim) {
$linkclasses .= ' dimmed';
$textclasses .= ' dimmed_text';
if ($conditionalhidden) {
$linkclasses .= ' conditionalhidden';
$textclasses .= ' conditionalhidden';
}
if ($mod->uservisible) {
// show accessibility note only if user can access the module himself
$accesstext = get_accesshide(get_string('hiddenfromstudents').': ');
}
}
// Get on-click attribute value if specified and decode the onclick - it
// has already been encoded for display (puke).
$onclick = htmlspecialchars_decode($mod->get_on_click(), ENT_QUOTES);
$groupinglabel = '';
if (!empty($mod->groupingid) && has_capability('moodle/course:managegroups', context_course::instance($mod->course))) {
$groupings = groups_get_all_groupings($mod->course);
$groupinglabel = html_writer::tag('span', '('.format_string($groupings[$mod->groupingid]->name).')',
array('class' => 'groupinglabel '.$textclasses));
}
// Display link itself.
$activitylink = html_writer::empty_tag('img', array('src' => $mod->get_icon_url(),
'class' => 'iconlarge activityicon', 'alt' => $mod->modfullname)) . $accesstext .
html_writer::tag('span', $instancename . $altname, array('class' => 'instancename'));
if ($mod->uservisible) {
$output .= html_writer::link($url, $activitylink, array('class' => $linkclasses, 'onclick' => $onclick)) .
$groupinglabel;
} else {
// We may be displaying this just in order to show information
// about visibility, without the actual link ($mod->uservisible)
$output .= html_writer::tag('div', $activitylink, array('class' => $textclasses)) .
$groupinglabel;
}
return $output;
}
/**
* Renders html to display the module content on the course page (i.e. text of the labels)
*
* @param cm_info $mod
* @param array $displayoptions
* @return string
*/
public function course_section_cm_text(cm_info $mod, $displayoptions = array()) {
$output = '';
if (!$mod->uservisible &&
(empty($mod->showavailability) || empty($mod->availableinfo))) {
// nothing to be displayed to the user
return $output;
}
$content = $mod->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
$conditionalhidden = $this->is_cm_conditionally_hidden($mod);
$accessiblebutdim = !$mod->visible || $conditionalhidden;
$textclasses = '';
$accesstext = '';
if ($accessiblebutdim) {
$textclasses .= ' dimmed_text';
if ($conditionalhidden) {
$textclasses .= ' conditionalhidden';
}
if ($mod->uservisible) {
// show accessibility note only if user can access the module himself
$accesstext = get_accesshide(get_string('hiddenfromstudents').': ');
}
}
if ($mod->get_url()) {
if ($content) {
// If specified, display extra content after link.
$output = html_writer::tag('div', $content, array('class' =>
trim('contentafterlink ' . $textclasses)));
}
} else {
// No link, so display only content.
$output = html_writer::tag('div', $accesstext . $content, array('class' => $textclasses));
}
return $output;
}
/**
* Renders HTML to show course module availability information (for someone who isn't allowed
* to see the activity itself, or for staff)
*
* @param cm_info $mod
* @param array $displayoptions
* @return string
*/
public function course_section_cm_availability(cm_info $mod, $displayoptions = array()) {
global $CFG;
if (!$mod->uservisible) {
// this is a student who is not allowed to see the module but might be allowed
// to see availability info (i.e. "Available from ...")
if (!empty($mod->showavailability) && !empty($mod->availableinfo)) {
$output = html_writer::tag('div', $mod->availableinfo, array('class' => 'availabilityinfo'));
}
return $output;
}
// this is a teacher who is allowed to see module but still should see the
// information that module is not available to all/some students
$modcontext = context_module::instance($mod->id);
$canviewhidden = has_capability('moodle/course:viewhiddenactivities', $modcontext);
if ($canviewhidden && !empty($CFG->enableavailability)) {
// Don't add availability information if user is not editing and activity is hidden.
if ($mod->visible || $this->page->user_is_editing()) {
$hidinfoclass = '';
if (!$mod->visible) {
$hidinfoclass = 'hide';
}
$ci = new condition_info($mod);
$fullinfo = $ci->get_full_information();
if($fullinfo) {
return '<div class="availabilityinfo '.$hidinfoclass.'">'.get_string($mod->showavailability
? 'userrestriction_visible'
: 'userrestriction_hidden','condition',
$fullinfo).'</div>';
}
}
}
return '';
}
/**
* Renders HTML to display one course module in a course section
*
* This includes link, content, availability, completion info and additional information
* that module type wants to display (i.e. number of unread forum posts)
*
* This function calls:
* {@link core_course_renderer::course_section_cm_name()}
* {@link cm_info::get_after_link()}
* {@link core_course_renderer::course_section_cm_text()}
* {@link core_course_renderer::course_section_cm_availability()}
* {@link core_course_renderer::course_section_cm_completion()}
* {@link course_get_cm_edit_actions()}
* {@link core_course_renderer::course_section_cm_edit_actions()}
*
* @param stdClass $course
* @param completion_info $completioninfo
* @param cm_info $mod
* @param int|null $sectionreturn
* @param array $displayoptions
* @return string
*/
public function course_section_cm($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = array()) {
$output = '';
// We return empty string (because course module 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 (!$mod->uservisible &&
(empty($mod->showavailability) || empty($mod->availableinfo))) {
return $output;
}
$indentclasses = 'mod-indent';
if (!empty($mod->indent)) {
$indentclasses .= ' mod-indent-'.$mod->indent;
if ($mod->indent > 15) {
$indentclasses .= ' mod-indent-huge';
}
}
$output .= html_writer::start_tag('div', array('class' => $indentclasses));
// Start the div for the activity title, excluding the edit icons.
$output .= html_writer::start_tag('div', array('class' => 'activityinstance'));
// Display the link to the module (or do nothing if module has no url)
$output .= $this->course_section_cm_name($mod, $displayoptions);
// Module can put text after the link (e.g. forum unread)
$output .= $mod->get_after_link();
// Closing the tag which contains everything but edit icons. Content part of the module should not be part of this.
$output .= html_writer::end_tag('div'); // .activityinstance
// If there is content but NO link (eg label), then display the
// content here (BEFORE any icons). In this case cons must be
// displayed after the content so that it makes more sense visually
// and for accessibility reasons, e.g. if you have a one-line label
// it should work similarly (at least in terms of ordering) to an
// activity.
$contentpart = $this->course_section_cm_text($mod, $displayoptions);
$url = $mod->get_url();
if (empty($url)) {
$output .= $contentpart;
}
if ($this->page->user_is_editing()) {
$editactions = course_get_cm_edit_actions($mod, $mod->indent, $sectionreturn);
$output .= ' '. $this->course_section_cm_edit_actions($editactions);
$output .= $mod->get_after_edit_icons();
}
$output .= $this->course_section_cm_completion($course, $completioninfo, $mod, $displayoptions);
// If there is content AND a link, then display the content here
// (AFTER any icons). Otherwise it was displayed before
if (!empty($url)) {
$output .= $contentpart;
}
// show availability info (if module is not available)
$output .= $this->course_section_cm_availability($mod, $displayoptions);
$output .= html_writer::end_tag('div'); // $indentclasses
return $output;
}
/**
* Renders HTML to display a list of course modules in a course section
* Also displays "move here" controls in Javascript-disabled mode
*
* This function calls {@link core_course_renderer::course_section_cm()}
*
* @param stdClass $course course object
* @param int|stdClass|section_info $section relative section number or section object
* @param int $sectionreturn section number to return to
* @param int $displayoptions
* @return void
*/
public function course_section_cm_list($course, $section, $sectionreturn = null, $displayoptions = array()) {
global $USER;
$output = '';
$modinfo = get_fast_modinfo($course);
if (is_object($section)) {
$section = $modinfo->get_section_info($section->section);
} else {
$section = $modinfo->get_section_info($section);
}
$completioninfo = new completion_info($course);
// check if we are currently in the process of moving a module with JavaScript disabled
$ismoving = $this->page->user_is_editing() && ismoving($course->id);
if ($ismoving) {
$movingpix = new pix_icon('movehere', get_string('movehere'), 'moodle', array('class' => 'movetarget'));
$strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'"));
}
// Get the list of modules visible to user (excluding the module being moved if there is one)
$moduleshtml = array();
if (!empty($modinfo->sections[$section->section])) {
foreach ($modinfo->sections[$section->section] as $modnumber) {
$mod = $modinfo->cms[$modnumber];
if ($ismoving and $mod->id == $USER->activitycopy) {
// do not display moving mod
continue;
}
if ($modulehtml = $this->course_section_cm($course,
$completioninfo, $mod, $sectionreturn, $displayoptions)) {
$moduleshtml[$modnumber] = $modulehtml;
}
}
}
if (!empty($moduleshtml) || $ismoving) {
$output .= html_writer::start_tag('ul', array('class' => 'section img-text'));
foreach ($moduleshtml as $modnumber => $modulehtml) {
if ($ismoving) {
$movingurl = new moodle_url('/course/mod.php', array('moveto' => $modnumber, 'sesskey' => sesskey()));
$output .= html_writer::tag('li', html_writer::link($movingurl, $this->output->render($movingpix)),
array('class' => 'movehere', 'title' => $strmovefull));
}
$mod = $modinfo->cms[$modnumber];
$modclasses = 'activity '. $mod->modname. ' modtype_'.$mod->modname. ' '. $mod->get_extra_classes();
$output .= html_writer::start_tag('li', array('class' => $modclasses, 'id' => 'module-'. $mod->id));
$output .= $modulehtml;
$output .= html_writer::end_tag('li');
}
if ($ismoving) {
$movingurl = new moodle_url('/course/mod.php', array('movetosection' => $section->id, 'sesskey' => sesskey()));
$output .= html_writer::tag('li', html_writer::link($movingurl, $this->output->render($movingpix)),
array('class' => 'movehere', 'title' => $strmovefull));
}
$output .= html_writer::end_tag('ul'); // .section
}
return $output;
}
/**
* Displays a custom list of courses with paging bar if necessary
*
* If $paginationurl is specified but $totalcount is not, the link 'View more'
* appears under the list.
*
* If both $paginationurl and $totalcount are specified, and $totalcount is
* bigger than count($courses), a paging bar is displayed above and under the
* courses list.
*
* @param array $courses array of course records (or instances of course_in_list) to show on this page
* @param bool $showcategoryname whether to add category name to the course description
* @param string $additionalclasses additional CSS classes to add to the div.courses
* @param moodle_url $paginationurl url to view more or url to form links to the other pages in paging bar
* @param int $totalcount total number of courses on all pages, if omitted $paginationurl will be displayed as 'View more' link
* @param int $page current page number (defaults to 0 referring to the first page)
* @param int $perpage number of records per page (defaults to $CFG->coursesperpage)
* @return string