forked from PukunuiAustralia/moodle-courseformat_grid
/
lib.php
executable file
·1818 lines (1636 loc) · 82.6 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
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/>.
/**
* Grid Format - A topics based format that uses a grid of user selectable images to popup a light box of the section.
*
* @package course/format
* @subpackage grid
* @copyright © 2012+ G J Barnard in respect to modifications of standard topics format.
* @author G J Barnard - gjbarnard at gmail dot com, about.me/gjbarnard and {@link http://moodle.org/user/profile.php?id=442195}
* @author Based on code originally written by Paul Krix and Julian Ridden.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/course/format/lib.php'); // For format_base.
class format_grid extends format_base {
// CONTRIB-4099:....
// Width constants - 128, 192, 210, 256, 320, 384, 448, 512, 576, 640, 704 and 768:...
private static $imagecontainerwidths = array(128 => '128', 192 => '192', 210 => '210', 256 => '256', 320 => '320', 384 => '384',
448 => '448', 512 => '512', 576 => '576', 640 => '640', 704 => '704', 768 => '768');
// Ratio constants - 3-2, 3-1, 3-3, 2-3, 1-3, 4-3 and 3-4:...
private static $imagecontainerratios = array(1 => '3-2', 2 => '3-1', 3 => '3-3', 4 => '2-3', 5 => '1-3', 6 => '4-3', 7 => '3-4');
// Border width constants - 1 to 10:....
private static $borderwidths = array(1 => '1', 2 => '2', 3 => '3', 4 => '4', 5 => '5', 6 => '6', 7 => '7', 8 => '8', 9 => '9', 10 => '10');
// Image holder height and new activity position for all on the basis that once calculated the majority of courses will be the same.
private static $currentwidth = 210;
private static $currentratio = 1; // 3-2.
private static $currentborderwidth = 3;
private static $currentheight = 140;
private static $activitymargintop = 101;
private static $activitymarginleft = 1118;
private $settings;
/**
* Creates a new instance of class
*
* Please use {@link course_get_format($courseorid)} to get an instance of the format class
*
* @param string $format
* @param int $courseid
* @return format_grid
*/
protected function __construct($format, $courseid) {
if ($courseid === 0) {
global $COURSE;
$courseid = $COURSE->id; // Save lots of global $COURSE as we will never be the site course.
}
parent::__construct($format, $courseid);
}
/**
* Prevents ability to change a static variable outside of the class.
* @return array Array of imagecontainer widths.
*/
public static function get_image_container_widths() {
return self::$imagecontainerwidths;
}
/**
* Gets the default image container width.
* @return int Default image container width.
*/
public static function get_default_image_container_width() {
return 210;
}
/**
* Prevents ability to change a static variable outside of the class.
* @return array Array of image container ratios.
*/
public static function get_image_container_ratios() {
return self::$imagecontainerratios;
}
/**
* Gets the default image container ratio.
* @return int Default image container ratio.
*/
public static function get_default_image_container_ratio() {
return 1; // '3-2'.
}
/**
* Gets the default image resize method.
* @return int Default image resize method.
*/
public static function get_default_image_resize_method() {
return 1; // 'Scale'.
}
/**
* Gets the default border colour.
* @return string Default border colour.
*/
public static function get_default_border_colour() {
return '#dddddd';
}
/**
* Prevents ability to change a static variable outside of the class.
* @return array Array of border widths.
*/
public static function get_border_widths() {
return self::$borderwidths;
}
/**
* Gets the default border width.
* @return int Default border width.
*/
public static function get_default_border_width() {
return 3; // '3'.
}
/**
* Gets the default border width.
* @return int Default border width.
*/
public static function get_default_border_radius() {
return 2; // On.
}
/**
* Gets the default image container background colour.
* @return string Default image container background colour.
*/
public static function get_default_image_container_background_colour() {
return '#f1f2f2';
}
/**
* Gets the default current selected section colour.
* @return string Default current selected section colour.
*/
public static function get_default_current_selected_section_colour() {
return '#8E66FF';
}
/**
* Gets the default current selected image container colour.
* @return string Default current selected image container colour.
*/
public static function get_default_current_selected_image_container_colour() {
return '#ffc540';
}
/**
* Gets the displayed image path for storage of the displayed image.
* @return string The path.
*/
public static function get_image_path() {
return '/gridimage/';
}
public static function get_maximum_image_width() {
return 768;
}
/**
* Returns the format's settings and gets them if they do not exist.
* @return array The settings as an array.
*/
public function get_settings() {
if (empty($this->settings) == true) {
$this->settings = $this->get_format_options();
}
return $this->settings;
}
/**
* Gets the name for the provided section.
*
* @param stdClass $section The section.
* @return string The section name.
*/
public function get_section_name($section) {
$section = $this->get_section($section);
if (!empty($section->name)) {
return format_string($section->name, true, array('context' => context_course::instance($this->courseid)));
} if ($section->section == 0) {
return get_string('topic0', 'format_grid');
} else {
return get_string('topic', 'format_grid') . ' ' . $section->section;
}
}
/**
* Indicates this format uses sections.
*
* @return bool Returns true
*/
public function uses_sections() {
return true;
}
/**
* The URL to use for the specified course (with section)
*
* @param int|stdClass $section Section object from database or just field course_sections.section
* if omitted the course view page is returned
* @param array $options options for view URL. At the moment core uses:
* 'navigation' (bool) if true and section has no separate page, the function returns null
* 'sr' (int) used by multipage formats to specify to which section to return
* @return null|moodle_url
*/
public function get_view_url($section, $options = array()) {
$course = $this->get_course();
$url = new moodle_url('/course/view.php', array('id' => $course->id));
$sr = null;
if (array_key_exists('sr', $options)) {
$sr = $options['sr'];
}
if (is_object($section)) {
$sectionno = $section->section;
} else {
$sectionno = $section;
}
if ($sectionno !== null) {
if ($sr !== null) {
if ($sr) {
$usercoursedisplay = COURSE_DISPLAY_MULTIPAGE;
$sectionno = $sr;
} else {
$usercoursedisplay = COURSE_DISPLAY_SINGLEPAGE;
}
} else {
$usercoursedisplay = $course->coursedisplay;
}
if ($sectionno != 0 && $usercoursedisplay == COURSE_DISPLAY_MULTIPAGE) {
$url->param('section', $sectionno);
} else {
if (!empty($options['navigation'])) {
return null;
}
$url->set_anchor('section-' . $sectionno);
}
}
return $url;
}
/**
* Returns the information about the ajax support in the given source format
*
* The returned object's property (boolean)capable indicates that
* the course format supports Moodle course ajax features.
* The property (array)testedbrowsers can be used as a parameter for {@link ajaxenabled()}.
*
* @return stdClass
*/
public function supports_ajax() {
$ajaxsupport = new stdClass();
$ajaxsupport->capable = true;
return $ajaxsupport;
}
/**
* Custom action after section has been moved in AJAX mode
*
* Used in course/rest.php
*
* @return array This will be passed in ajax respose
*/
public function ajax_section_move() {
global $PAGE;
$titles = array();
$course = $this->get_course();
$modinfo = get_fast_modinfo($course);
$renderer = $this->get_renderer($PAGE);
if ($renderer && ($sections = $modinfo->get_section_info_all())) {
foreach ($sections as $number => $section) {
$titles[$number] = $renderer->section_title($section, $course);
}
}
return array('sectiontitles' => $titles, 'action' => 'move');
}
/**
* Returns the list of blocks to be automatically added for the newly created course
*
* @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
* each of values is an array of block names (for left and right side columns)
*/
public function get_default_blocks() {
return array(
BLOCK_POS_LEFT => array(),
BLOCK_POS_RIGHT => array('search_forums', 'news_items', 'calendar_upcoming', 'recent_activity')
);
}
/**
* Definitions of the additional options that this course format uses for the course.
*
* @param bool $foreditform
* @return array of options
*/
public function course_format_options($foreditform = false) {
static $courseformatoptions = false;
if ($courseformatoptions === false) {
/* Note: Because 'admin_setting_configcolourpicker' in 'settings.php' needs to use a prefixing '#'
this needs to be stripped off here if it's there for the format's specific colour picker. */
$defaultbordercolour = get_config('format_grid', 'defaultbordercolour');
if ($defaultbordercolour[0] == '#') {
$defaultbordercolour = substr($defaultbordercolour, 1);
}
$defaultimagecontainerbackgroundcolour = get_config('format_grid', 'defaultimagecontainerbackgroundcolour');
if ($defaultimagecontainerbackgroundcolour[0] == '#') {
$defaultimagecontainerbackgroundcolour = substr($defaultimagecontainerbackgroundcolour, 1);
}
$defaultcurrentselectedsectioncolour = get_config('format_grid', 'defaultcurrentselectedsectioncolour');
if ($defaultcurrentselectedsectioncolour[0] == '#') {
$defaultcurrentselectedsectioncolour = substr($defaultcurrentselectedsectioncolour, 1);
}
$defaultcurrentselectedimagecontainercolour = get_config('format_grid', 'defaultcurrentselectedimagecontainercolour');
if ($defaultcurrentselectedimagecontainercolour[0] == '#') {
$defaultcurrentselectedimagecontainercolour = substr($defaultcurrentselectedimagecontainercolour, 1);
}
$defaultfitsectioncontainertowindow = get_config('format_grid', 'defaultfitsectioncontainertowindow');
$courseconfig = get_config('moodlecourse');
$courseformatoptions = array(
'numsections' => array(
'default' => $courseconfig->numsections,
'type' => PARAM_INT
),
'hiddensections' => array(
'default' => $courseconfig->hiddensections,
'type' => PARAM_INT
),
'coursedisplay' => array(
'default' => $courseconfig->coursedisplay,
'type' => PARAM_INT
),
'imagecontainerwidth' => array(
'default' => get_config('format_grid', 'defaultimagecontainerwidth'),
'type' => PARAM_INT
),
'imagecontainerratio' => array(
'default' => get_config('format_grid', 'defaultimagecontainerratio'),
'type' => PARAM_ALPHANUM
),
'imageresizemethod' => array(
'default' => get_config('format_grid', 'defaultimageresizemethod'),
'type' => PARAM_INT
),
'bordercolour' => array(
'default' => $defaultbordercolour,
'type' => PARAM_ALPHANUM
),
'borderwidth' => array(
'default' => get_config('format_grid', 'defaultborderwidth'),
'type' => PARAM_INT
),
'borderradius' => array(
'default' => get_config('format_grid', 'defaultborderradius'),
'type' => PARAM_INT
),
'imagecontainerbackgroundcolour' => array(
'default' => $defaultimagecontainerbackgroundcolour,
'type' => PARAM_ALPHANUM
),
'currentselectedsectioncolour' => array(
'default' => $defaultcurrentselectedsectioncolour,
'type' => PARAM_ALPHANUM
),
'currentselectedimagecontainercolour' => array(
'default' => $defaultcurrentselectedimagecontainercolour,
'type' => PARAM_ALPHANUM
),
'newactivity' => array(
'default' => get_config('format_grid', 'defaultnewactivity'),
'type' => PARAM_INT
),
'fitsectioncontainertowindow' => array(
'default' => get_config('format_grid', 'defaultfitsectioncontainertowindow'),
'type' => PARAM_INT
)
);
}
if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
/* Note: Because 'admin_setting_configcolourpicker' in 'settings.php' needs to use a prefixing '#'
this needs to be stripped off here if it's there for the format's specific colour picker. */
$defaultbordercolour = get_config('format_grid', 'defaultbordercolour');
if ($defaultbordercolour[0] == '#') {
$defaultbordercolour = substr($defaultbordercolour, 1);
}
$defaultimagecontainerbackgroundcolour = get_config('format_grid', 'defaultimagecontainerbackgroundcolour');
if ($defaultimagecontainerbackgroundcolour[0] == '#') {
$defaultimagecontainerbackgroundcolour = substr($defaultimagecontainerbackgroundcolour, 1);
}
$defaultcurrentselectedsectioncolour = get_config('format_grid', 'defaultcurrentselectedsectioncolour');
if ($defaultcurrentselectedsectioncolour[0] == '#') {
$defaultcurrentselectedsectioncolour = substr($defaultcurrentselectedsectioncolour, 1);
}
$defaultcurrentselectedimagecontainercolour = get_config('format_grid', 'defaultcurrentselectedimagecontainercolour');
if ($defaultcurrentselectedimagecontainercolour[0] == '#') {
$defaultcurrentselectedimagecontainercolour = substr($defaultcurrentselectedimagecontainercolour, 1);
}
$coursecontext = context_course::instance($this->courseid);
$courseconfig = get_config('moodlecourse');
$sectionmenu = array();
for ($i = 0; $i <= $courseconfig->maxsections; $i++) {
$sectionmenu[$i] = "$i";
}
$courseformatoptionsedit = array(
'numsections' => array(
'label' => new lang_string('numbersections', 'format_grid'),
'element_type' => 'select',
'element_attributes' => array($sectionmenu),
),
'hiddensections' => array(
'label' => new lang_string('hiddensections'),
'help' => 'hiddensections',
'help_component' => 'moodle',
'element_type' => 'select',
'element_attributes' => array(
array(
0 => new lang_string('hiddensectionscollapsed'),
1 => new lang_string('hiddensectionsinvisible')
)
),
),
'coursedisplay' => array(
'label' => new lang_string('coursedisplay'),
'element_type' => 'select',
'element_attributes' => array(
array(
COURSE_DISPLAY_SINGLEPAGE => new lang_string('coursedisplay_single'),
COURSE_DISPLAY_MULTIPAGE => new lang_string('coursedisplay_multi')
)
),
'help' => 'coursedisplay',
'help_component' => 'moodle',
)
);
if (has_capability('format/grid:changeimagecontainersize', $coursecontext)) {
$courseformatoptionsedit['imagecontainerwidth'] = array(
'label' => new lang_string('setimagecontainerwidth', 'format_grid'),
'help' => 'setimagecontainerwidth',
'help_component' => 'format_grid',
'element_type' => 'select',
'element_attributes' => array(self::$imagecontainerwidths)
);
$courseformatoptionsedit['imagecontainerratio'] = array(
'label' => new lang_string('setimagecontainerratio', 'format_grid'),
'help' => 'setimagecontainerratio',
'help_component' => 'format_grid',
'element_type' => 'select',
'element_attributes' => array(self::$imagecontainerratios)
);
} else {
$courseformatoptionsedit['imagecontainerwidth'] = array('label' =>
get_config('format_grid', 'defaultimagecontainerwidth'), 'element_type' => 'hidden');
$courseformatoptionsedit['imagecontainerratio'] = array('label' =>
get_config('format_grid', 'defaultimagecontainerratio'), 'element_type' => 'hidden');
}
if (has_capability('format/grid:changeimageresizemethod', $coursecontext)) {
$courseformatoptionsedit['imageresizemethod'] = array(
'label' => new lang_string('setimageresizemethod', 'format_grid'),
'help' => 'setimageresizemethod',
'help_component' => 'format_grid',
'element_type' => 'select',
'element_attributes' => array(
array(
1 => new lang_string('scale', 'format_grid'), // Scale.
2 => new lang_string('crop', 'format_grid') // Crop.
)
)
);
} else {
$courseformatoptionsedit['imageresizemethod'] = array('label' =>
get_config('format_grid', 'defaultimageresizemethod'), 'element_type' => 'hidden');
}
if (has_capability('format/grid:changeimagecontainerstyle', $coursecontext)) {
$courseformatoptionsedit['bordercolour'] = array(
'label' => new lang_string('setbordercolour', 'format_grid'),
'help' => 'setbordercolour',
'help_component' => 'format_grid',
'element_type' => 'gfcolourpopup',
'element_attributes' => array(
array('tabindex' => -1, 'value' => get_config('format_grid', 'defaultbordercolour'))
)
);
$courseformatoptionsedit['borderwidth'] = array(
'label' => new lang_string('setborderwidth', 'format_grid'),
'help' => 'setborderwidth',
'help_component' => 'format_grid',
'element_type' => 'select',
'element_attributes' => array(self::$borderwidths)
);
$courseformatoptionsedit['borderradius'] = array(
'label' => new lang_string('setborderradius', 'format_grid'),
'help' => 'setborderradius',
'help_component' => 'format_grid',
'element_type' => 'select',
'element_attributes' => array(
array(1 => new lang_string('off', 'format_grid'),
2 => new lang_string('on', 'format_grid'))
)
);
$courseformatoptionsedit['imagecontainerbackgroundcolour'] = array(
'label' => new lang_string('setimagecontainerbackgroundcolour', 'format_grid'),
'help' => 'setimagecontainerbackgroundcolour',
'help_component' => 'format_grid',
'element_type' => 'gfcolourpopup',
'element_attributes' => array(
array('tabindex' => -1, 'value' => get_config('format_grid', 'defaultimagecontainerbackgroundcolour'))
)
);
$courseformatoptionsedit['currentselectedsectioncolour'] = array(
'label' => new lang_string('setcurrentselectedsectioncolour', 'format_grid'),
'help' => 'setcurrentselectedsectioncolour',
'help_component' => 'format_grid',
'element_type' => 'gfcolourpopup',
'element_attributes' => array(
array('tabindex' => -1, 'value' => get_config('format_grid', 'defaultcurrentselectedsectioncolour'))
)
);
$courseformatoptionsedit['currentselectedimagecontainercolour'] = array(
'label' => new lang_string('setcurrentselectedimagecontainercolour', 'format_grid'),
'help' => 'setcurrentselectedimagecontainercolour',
'help_component' => 'format_grid',
'element_type' => 'gfcolourpopup',
'element_attributes' => array(
array('tabindex' => -1, 'value' => get_config('format_grid', 'defaultcurrentselectedimagecontainercolour'))
)
);
} else {
$courseformatoptionsedit['bordercolour'] = array('label' => $defaultbordercolour,
'element_type' => 'hidden');
$courseformatoptionsedit['borderwidth'] = array('label' => get_config('format_grid', 'defaultborderwidth'),
'element_type' => 'hidden');
$courseformatoptionsedit['borderradius'] = array('label' => get_config('format_grid', 'defaultborderradius'),
'element_type' => 'hidden');
$courseformatoptionsedit['imagecontainerbackgroundcolour'] =
array('label' => $defaultimagecontainerbackgroundcolour, 'element_type' => 'hidden');
$courseformatoptionsedit['currentselectedsectioncolour'] =
array('label' => $defaultcurrentselectedsectioncolour, 'element_type' => 'hidden');
$courseformatoptionsedit['currentselectedimagecontainercolour'] =
array('label' => $defaultcurrentselectedimagecontainercolour, 'element_type' => 'hidden');
}
$courseformatoptionsedit['newactivity'] = array(
'label' => new lang_string('setnewactivity', 'format_grid'),
'element_type' => 'select',
'element_attributes' => array(
array(
1 => new lang_string('no'), // No.
2 => new lang_string('yes') // Yes.
)
),
'help' => 'setnewactivity',
'help_component' => 'format_grid'
);
$courseformatoptionsedit['fitsectioncontainertowindow'] = array(
'label' => new lang_string('setfitsectioncontainertowindow', 'format_grid'),
'help' => 'setfitsectioncontainertowindow',
'help_component' => 'format_grid',
'element_type' => 'select',
'element_attributes' => array(
array(
1 => new lang_string('no'), // No.
2 => new lang_string('yes') // Yes.
)
)
);
$courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
}
return $courseformatoptions;
}
/**
* Adds format options elements to the course/section edit form.
*
* This function is called from {@link course_edit_form::definition_after_data()}.
*
* @param MoodleQuickForm $mform form the elements are added to.
* @param bool $forsection 'true' if this is a section edit form, 'false' if this is course edit form.
* @return array array of references to the added form elements.
*/
public function create_edit_form_elements(&$mform, $forsection = false) {
global $CFG, $OUTPUT;
MoodleQuickForm::registerElementType('gfcolourpopup', "$CFG->dirroot/course/format/grid/js/gf_colourpopup.php",
'MoodleQuickForm_gfcolourpopup');
$elements = parent::create_edit_form_elements($mform, $forsection);
if ($forsection == false) {
global $USER;
/*
Increase the number of sections combo box values if the user has increased the number of sections
using the imagecontainer on the course page beyond course 'maxsections' or course 'maxsections' has been
reduced below the number of sections already set for the course on the site administration course
defaults page. This is so that the number of sections is not reduced leaving unintended orphaned
activities / resources.
*/
$maxsections = get_config('moodlecourse', 'maxsections');
$numsections = $mform->getElementValue('numsections');
$numsections = $numsections[0];
if ($numsections > $maxsections) {
$element = $mform->getElement('numsections');
for ($i = $maxsections + 1; $i <= $numsections; $i++) {
$element->addOption("$i", $i);
}
}
$coursecontext = context_course::instance($this->courseid);
$changeimagecontainersize = has_capability('format/grid:changeimagecontainersize', $coursecontext);
$changeimageresizemethod = has_capability('format/grid:changeimageresizemethod', $coursecontext);
$changeimagecontainerstyle = has_capability('format/grid:changeimagecontainerstyle', $coursecontext);
$resetall = is_siteadmin($USER); // Site admins only.
$elements[] = $mform->addElement('header', 'gfreset', get_string('gfreset', 'format_grid'));
$mform->addHelpButton('gfreset', 'gfreset', 'format_grid', '', true);
$resetelements = array();
if ((($changeimagecontainersize) || ($changeimageresizemethod) || ($changeimagecontainerstyle))) {
if ($changeimagecontainersize) {
$checkboxname = get_string('resetimagecontainersize', 'format_grid') .
$OUTPUT->help_icon('resetimagecontainersize', 'format_grid');
$resetelements[] = & $mform->createElement('checkbox', 'resetimagecontainersize', '', $checkboxname);
}
if ($changeimageresizemethod) {
$checkboxname = get_string('resetimageresizemethod', 'format_grid') .
$OUTPUT->help_icon('resetimageresizemethod', 'format_grid');
$resetelements[] = & $mform->createElement('checkbox', 'resetimageresizemethod', '', $checkboxname);
}
if ($changeimagecontainerstyle) {
$checkboxname = get_string('resetimagecontainerstyle', 'format_grid') .
$OUTPUT->help_icon('resetimagecontainerstyle', 'format_grid');
$resetelements[] = & $mform->createElement('checkbox', 'resetimagecontainerstyle', '', $checkboxname);
}
}
$checkboxname = get_string('resetnewactivity', 'format_grid') .
$OUTPUT->help_icon('resetnewactivity', 'format_grid');
$resetelements[] = & $mform->createElement('checkbox', 'resetnewactivity', '', $checkboxname);
$checkboxname = get_string('resetfitpopup', 'format_grid') .
$OUTPUT->help_icon('resetfitpopup', 'format_grid');
$resetelements[] = & $mform->createElement('checkbox', 'resetfitpopup', '', $checkboxname);
$elements[] = $mform->addGroup($resetelements, 'resetgroup', get_string('resetgrp', 'format_grid'), null,
false);
if ($resetall) {
$resetallelements = array();
$checkboxname = get_string('resetallimagecontainersize', 'format_grid') .
$OUTPUT->help_icon('resetallimagecontainersize', 'format_grid');
$resetallelements[] = & $mform->createElement('checkbox', 'resetallimagecontainersize', '', $checkboxname);
$checkboxname = get_string('resetallimageresizemethod', 'format_grid') .
$OUTPUT->help_icon('resetallimageresizemethod', 'format_grid');
$resetallelements[] = & $mform->createElement('checkbox', 'resetallimageresizemethod', '', $checkboxname);
$checkboxname = get_string('resetallimagecontainerstyle', 'format_grid') .
$OUTPUT->help_icon('resetallimagecontainerstyle', 'format_grid');
$resetallelements[] = & $mform->createElement('checkbox', 'resetallimagecontainerstyle', '', $checkboxname);
$checkboxname = get_string('resetallnewactivity', 'format_grid') .
$OUTPUT->help_icon('resetallnewactivity', 'format_grid');
$resetallelements[] = & $mform->createElement('checkbox', 'resetallnewactivity', '', $checkboxname);
$checkboxname = get_string('resetallfitpopup', 'format_grid') .
$OUTPUT->help_icon('resetallfitpopup', 'format_grid');
$resetallelements[] = & $mform->createElement('checkbox', 'resetallfitpopup', '', $checkboxname);
$elements[] = $mform->addGroup($resetallelements, 'resetallgroup', get_string('resetallgrp', 'format_grid'),
null, false);
}
}
return $elements;
}
/**
* Override if you need to perform some extra validation of the format options
*
* @param array $data array of ("fieldname"=>value) of submitted data
* @param array $files array of uploaded files "element_name"=>tmp_file_path
* @param array $errors errors already discovered in edit form validation
* @return array of "element_name"=>"error_description" if there are errors,
* or an empty array if everything is OK.
* Do not repeat errors from $errors param here
*/
public function edit_form_validation($data, $files, $errors) {
$retr = array();
if ($this->validate_colour($data['bordercolour']) === false) {
$retr['bordercolour'] = get_string('colourrule', 'format_grid');
}
if ($this->validate_colour($data['imagecontainerbackgroundcolour']) === false) {
$retr['imagecontainerbackgroundcolour'] = get_string('colourrule', 'format_grid');
}
if ($this->validate_colour($data['currentselectedsectioncolour']) === false) {
$retr['currentselectedsectioncolour'] = get_string('colourrule', 'format_grid');
}
if ($this->validate_colour($data['currentselectedimagecontainercolour']) === false) {
$retr['currentselectedimagecontainercolour'] = get_string('colourrule', 'format_grid');
}
return $retr;
}
/**
* Validates the colour that was entered by the user.
* Borrowed from 'admin_setting_configcolourpicker' in '/lib/adminlib.php'.
*
* I'm not completely happy with this solution as would rather embed in the colour
* picker code in the form, however I find this area rather fraut and I hear that
* Dan Poltawski (via MDL-42270) will be re-writing the forms lib so hopefully more
* developer friendly.
*
* Note: Colour names removed, but might consider putting them back in if asked, but
* at the moment that would require quite a few changes and coping with existing
* settings. Either convert the names to hex or allow them as valid values and
* fix the colour picker code and the CSS code in 'format.php' for the setting.
*
* Colour name to hex on: http://www.w3schools.com/cssref/css_colornames.asp.
*
* @param string $data the colour string to validate.
* @return true|false
*/
private function validate_colour($data) {
if (preg_match('/^#?([[:xdigit:]]{3}){1,2}$/', $data)) {
return true;
} else {
return false;
}
}
/**
* Updates format options for a course
*
* In case if course format was changed to 'Grid', we try to copy options
* 'coursedisplay', 'numsections' and 'hiddensections' from the previous format.
* If previous course format did not have 'numsections' option, we populate it with the
* current number of sections. The layout and colour defaults will come from 'course_format_options'.
*
* @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data.
* @param stdClass $oldcourse if this function is called from {@link update_course()}
* this object contains information about the course before update.
* @return bool whether there were any changes to the options values.
*/
public function update_course_format_options($data, $oldcourse = null) {
global $DB; // MDL-37976.
if (self::is_developer_debug()) {
error_log('update_course_format_options - called data: ' . print_r($data, true));
}
/*
* Notes: Using 'unset' to really ensure that the reset form elements never get into the database.
* This has to be done here so that the reset occurs after we have done updates such that the
* reset itself is not seen as an update.
*/
$resetimagecontainersize = false;
$resetimageresizemethod = false;
$resetimagecontainerstyle = false;
$resetnewactivity = false;
$resetfitpopup = false;
$resetallimagecontainersize = false;
$resetallimageresizemethod = false;
$resetallimagecontainerstyle = false;
$resetallnewactivity = false;
$resetallfitpopup = false;
if (isset($data->resetimagecontainersize) == true) {
$resetimagecontainersize = true;
unset($data->resetimagecontainersize);
}
if (isset($data->resetimageresizemethod) == true) {
$resetimageresizemethod = true;
unset($data->resetimageresizemethod);
}
if (isset($data->resetimagecontainerstyle) == true) {
$resetimagecontainerstyle = true;
unset($data->resetimagecontainerstyle);
}
if (isset($data->resetnewactivity) == true) {
$resetnewactivity = true;
unset($data->resetnewactivity);
}
if (isset($data->resetfitpopup) == true) {
$resetfitpopup = true;
unset($data->resetfitpopup);
}
if (isset($data->resetallimagecontainersize) == true) {
$resetallimagecontainersize = true;
unset($data->resetallimagecontainersize);
}
if (isset($data->resetallimageresizemethod) == true) {
$resetallimageresizemethod = true;
unset($data->resetallimageresizemethod);
}
if (isset($data->resetallimagecontainerstyle) == true) {
$resetallimagecontainerstyle = true;
unset($data->resetallimagecontainerstyle);
}
if (isset($data->resetallnewactivity) == true) {
$resetallnewactivity = true;
unset($data->resetallnewactivity);
}
if (isset($data->resetallfitpopup) == true) {
$resetfitpopup = true;
unset($data->resetallfitpopup);
}
$settings = $this->get_settings();
$changedisplayedimages = false;
if (isset($data->imagecontainerwidth)) {
// We are have the CONTRIB-4099 options and this is not from a pre-CONTRIB-4099 backup file.
if (((!(($resetimagecontainersize) || ($resetallimagecontainersize))) &&
(($settings['imagecontainerwidth'] != $data->imagecontainerwidth)
|| ($settings['imagecontainerratio'] != $data->imagecontainerratio))) ||
((!(($resetimageresizemethod) || ($resetallimageresizemethod))) &&
($settings['imageresizemethod'] != $data->imageresizemethod))) {
/* Detect now and action later as 'setup_displayed_image' when called from 'update_displayed_images()' will need to
use the new values. */
$changedisplayedimages = true;
if (self::is_developer_debug()) {
error_log('update_course_format_options: Settings before: ' . print_r($settings, true));
error_log('update_course_format_options: Data : ' . print_r($data, true));
}
}
}
$data = (array) $data;
if ($oldcourse !== null) {
$oldcourse = (array) $oldcourse;
$options = $this->course_format_options();
foreach ($options as $key => $unused) {
if (!array_key_exists($key, $data)) {
if (array_key_exists($key, $oldcourse)) {
$data[$key] = $oldcourse[$key];
} else if ($key === 'numsections') {
/* If previous format does not have the field 'numsections'
and $data['numsections'] is not set, we fill it with the
maximum section number from the DB. */
$maxsection = $DB->get_field_sql(
'SELECT max(section) from {course_sections} WHERE course = ?',
array($this->courseid)
);
if ($maxsection) {
// If there are no sections, or just default 0-section, 'numsections' will be set to default.
$data['numsections'] = $maxsection;
}
}
}
}
}
$changes = $this->update_format_options($data);
if ($changes && array_key_exists('numsections', $data)) {
// If the numsections was decreased, try to completely delete the orphaned sections (unless they are not empty).
$numsections = (int)$data['numsections'];
$maxsection = $DB->get_field_sql('SELECT max(section) from {course_sections}
WHERE course = ?', array($this->courseid));
for ($sectionnum = $maxsection; $sectionnum > $numsections; $sectionnum--) {
if (!$this->delete_section($sectionnum, false)) {
break;
}
}
}
// Now we can change the displayed images if needed.
if ($changedisplayedimages) {
$this->settings = null; // Invalidate as changed.
$settings = $this->get_settings();
if (self::is_developer_debug()) {
error_log('update_course_format_options: Settings after: ' . print_r($settings, true));
}
$this->update_displayed_images($this->courseid, $this, $settings, true);
}
// Now we can do the reset.
if (($resetallimagecontainersize) || ($resetallimageresizemethod) || ($resetallimagecontainerstyle) || ($resetallnewactivity) || ($resetallfitpopup)) {
$this->reset_grid_setting(0, $resetallimagecontainersize, $resetallimageresizemethod, $resetallimagecontainerstyle, $resetallnewactivity, $resetallfitpopup);
$changes = true;
} else if (($resetimagecontainersize) || ($resetimageresizemethod) || ($resetimagecontainerstyle) || ($resetnewactivity) || ($resetfitpopup)) {
$this->reset_grid_setting($this->courseid, $resetimagecontainersize, $resetimageresizemethod, $resetimagecontainerstyle, $resetnewactivity, $resetfitpopup);
$changes = true;
}
return $changes;
}
/**
* Deletes a section
*
* Do not call this function directly, instead call {@link course_delete_section()}
*
* @param int|stdClass|section_info $section
* @param bool $forcedeleteifnotempty if set to false section will not be deleted if it has modules in it.
* @return bool whether section was deleted
*/
public function delete_section($section, $forcedeleteifnotempty = false) {
global $DB;
if (!is_object($section)) {
$section = $DB->get_record('course_sections', array('course' => $this->get_courseid(), 'section' => $section),
'id,section,sequence');
}
if (parent::delete_section($section, $forcedeleteifnotempty)) {
$coursecontext = context_course::instance($this->courseid);
$this->delete_image($section->id, $coursecontext->id);
return true;
}
return false;
}
/**
* Whether this format allows to delete sections
*
* Do not call this function directly, instead use {@link course_can_delete_section()}
*
* @param int|stdClass|section_info $section
* @return bool
*/
public function can_delete_section($section) {
return true;
}
/**
* Updates format options for a section
*
* Section id is expected in $data->id (or $data['id'])
* If $data does not contain property with the option name, the option will not be updated
*
* @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data
* @return bool whether there were any changes to the options values
*/
public function update_section_format_options($data) {
$data = (array)$data;
// Resets the displayed image because changing the section name / details deletes the file.
// See CONTRIB-4784.
global $DB;
$DB->set_field('format_grid_icon', 'displayedimageindex', 0, array('sectionid' => $data['id']));
return parent::update_section_format_options($data);
}
/**
* Resets the format setting to the default.
* @param int $courseid If not 0, then a specific course to reset.
* @param int $imagecontainersizereset If true, reset the layout to the default in the settings for the format.
* @param int $imageresizemethodreset If true, reset the image resize method to the default in the settings for the format.
* @param int $imagecontainerstylereset If true, reset the colour to the default in the settings for the format.
* @param int $newactivity If true, reset the new activity to the default in the settings for the format.
* @param int $fitpopupreset If true, reset the fit popup to the default in the settings for the format.
*/
public function reset_grid_setting($courseid, $imagecontainersizereset, $imageresizemethodreset, $imagecontainerstylereset, $newactivityreset, $fitpopupreset) {
global $DB, $USER;
$coursecontext = context_course::instance($this->courseid);
$currentcourseid = 0;
if ($courseid == 0) {
$records = $DB->get_records('course', array('format' => $this->format), '', 'id');
} else {
$records = $DB->get_records('course', array('id' => $courseid, 'format' => $this->format), '',
'id');
}
$resetallifall = ((is_siteadmin($USER)) || ($courseid != 0)); // Will be true if reset all capability or a single course.
$updatedata = array();
$updateimagecontainersize = false;