-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlib.php
1466 lines (1319 loc) · 62.9 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/>.
/**
* This file contains main class for Multitopic course format.
*
* @since Moodle 2.0
* @package format_multitopic
* @copyright 2019 onwards James Calder and Otago Polytechnic
* @copyright based on work by 2009 Sam Hemelryk
* @copyright based on work by 2012 onwards Marina Glancy
* @copyright based on work by 2012 David Herney Bernal - cirano
* @copyright based on work by 2020 Ferran Recio <ferran@moodle.com>
* @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');
// ADDED.
require_once(__DIR__ . '/locallib.php');
require_once(__DIR__ . '/classes/global_navigation_wrapper.php');
require_once(__DIR__ . '/classes/courseheader.php');
require_once(__DIR__ . '/classes/coursecontentheaderfooter.php');
// END ADDED.
use core\output\inplace_editable;
// ADDED.
/** @var int The level of the General section, which represents the course as a whole.
* Set to -1, to be a level above the top-level sections in OneTopic format, which are numbered 0.
* NOTE: None of these constants can be changed anymore, as parts of the code assume these values.
*/
const FORMAT_MULTITOPIC_SECTION_LEVEL_ROOT = -1;
/** @var int Deepest level of page to let users create. Must be between the root level and the topic level. */
const FORMAT_MULTITOPIC_SECTION_LEVEL_PAGE_USE = 1;
/** @var int Level of topics, which are displayed within pages. */
const FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC = 2;
// END ADDED.
/**
* Main class for the Multitopic course format.
*
* @package format_multitopic
* @copyright 2019 onwards James Calder and Otago Polytechnic
* @copyright based on work by 2009 Sam Hemelryk
* @copyright based on work by 2012 onwards Marina Glancy
* @copyright based on work by 2012 David Herney Bernal - cirano
* @copyright based on work by 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class format_multitopic extends core_courseformat\base {
// ADDED.
/** @var int ID of section 0 / the General section, treated as the section root by the Multitopic format */
public $fmtrootsectionid;
/** @var stdClass Multitopic-specific section information */
private $fmtsectionsextra = null;
/** @var course_modinfo|null Mod info the Multitopic-specific section information was based on */
private $fmtmodinfo = null;
/** @var bool Multitopic-specific section information is complete*/
private $fmtsectionsextracomplete = false;
/** @var int|null the sectionid selected */
protected $singlesectionid = null;
/** @var int|null the sectionid as specified */
public $originalsinglesectionid = null;
// END ADDED.
// INCLUDED declaration /course/format/classes/base.php class base function __construct.
/**
* Creates a new instance of class
*
* Please use course_get_format($courseorid) to get an instance of the format class
*
* @param string $format
* @param int $courseid
* @return format_multitopic
*/
protected function __construct($format, $courseid) {
global $DB;
parent::__construct($format, $courseid);
if ($courseid) {
$this->fmtrootsectionid = $DB->get_field('course_sections', 'id', ['section' => 0, 'course' => $courseid]);
// TODO: Check if this is set correctly for new courses?
}
}
// END INCLUDED.
// INCLUDED /course/format/classes/base.php class base function get_course_display.
/**
* Get the course display value for the current course.
*
* Formats extending topics or weeks will use coursedisplay as this setting name
* so they don't need to override the method. However, if the format uses a different
* display logic it must override this method to ensure the core renderers know
* if a COURSE_DISPLAY_MULTIPAGE or COURSE_DISPLAY_SINGLEPAGE is being used.
*
* @return int The current value (COURSE_DISPLAY_MULTIPAGE or COURSE_DISPLAY_SINGLEPAGE)
*/
public function get_course_display(): int {
return COURSE_DISPLAY_SINGLEPAGE;
}
// END INCLUDED.
/**
* Generate the title for this section page.
*
* @return string the page title
*/
public function page_title(): string {
return get_string_manager()->string_exists('sectionoutline', 'format_multitopic') ?
get_string('sectionoutline', 'format_multitopic') : get_string('topicoutline');
}
/**
* Returns true if this course format uses sections.
*
* @return bool
*/
public function uses_sections(): bool {
return true;
}
/**
* Returns true if this course format uses course index
*
* This function may be called without specifying the course id
* i.e. in course_index_drawer()
*
* @return bool
*/
public function uses_course_index(): bool {
return true;
}
/**
* Returns true if this course format uses activity indentation.
*
* @return bool if the course format uses indentation.
*/
public function uses_indentation(): bool {
global $CFG;
return (($CFG->version >= 2022041907.09 && $CFG->version < 2022042000
|| $CFG->version >= 2022112802.09 && $CFG->version < 2022112900
|| $CFG->version >= 2023031400)
&& get_config('format_multitopic', 'indentation')) ? true : false;
}
// ADDED.
/**
* Returns a list of Multitopic-specific section information.
*
* @param bool $needall do we need all properties
* @return \format_multitopic\section_info_extra[]
*/
final public function fmt_get_sections_extra($needall = true): array {
$course = $this->get_course();
$modinfo = $course ? $this->get_modinfo() : null;
if (isset($this->fmtsectionsextra) && $this->fmtmodinfo == $modinfo) {
$fmtsectionsextra = $this->fmtsectionsextra;
} else {
$this->fmtsectionsextracomplete = false;
if ($course) {
$sections = $modinfo->get_section_info_all();
} else {
$sections = [];
}
$timenow = time();
$courseperioddays = format_multitopic_duration_as_days($course->periodduration);
// Forward pass.
// Initialise generated list of sections.
$fmtsectionsextra = [];
// The previous section at, or above, each level.
$sectionextraprevatlevel = array_fill(FORMAT_MULTITOPIC_SECTION_LEVEL_ROOT,
FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC - FORMAT_MULTITOPIC_SECTION_LEVEL_ROOT + 1, null);
// The current section at, or above, each level.
$sectionextraatlevel = array_fill(FORMAT_MULTITOPIC_SECTION_LEVEL_ROOT,
FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC - FORMAT_MULTITOPIC_SECTION_LEVEL_ROOT + 1, null);
foreach ($sections as $thissection) {
// Create new object.
$thissectionextra = new \format_multitopic\section_info_extra($thissection);
if (!empty($thissection->component)) {
$thissectionextra->levelsan = 0;
$fmtsectionsextra[$thissection->id] = $thissectionextra;
continue;
}
// Fix the section's level within appropriate bounds.
$levelsan = ($sectionextraatlevel[FORMAT_MULTITOPIC_SECTION_LEVEL_ROOT] == null) ?
FORMAT_MULTITOPIC_SECTION_LEVEL_ROOT
: max(FORMAT_MULTITOPIC_SECTION_LEVEL_ROOT + 1,
min($thissection->level ?? FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC, FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC));
$thissectionextra->levelsan = $levelsan;
// Update remembered sections.
for ($sublevel = $levelsan; $sublevel <= FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC; $sublevel++) {
$sectionextraprevatlevel[$sublevel] = $sectionextraatlevel[$sublevel];
$sectionextraatlevel[$sublevel] = $thissectionextra;
}
// The previous section at or above this section's level.
$thissectionextra->prevupid = $sectionextraprevatlevel[$levelsan] ? $sectionextraprevatlevel[$levelsan]->id : null;
// The previous page.
$thissectionextra->prevpageid = $sectionextraprevatlevel[FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC - 1] ?
$sectionextraprevatlevel[FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC - 1]->id : null;
// The previous section at any level.
$thissectionextra->prevanyid = $sectionextraprevatlevel[FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC] ?
$sectionextraprevatlevel[FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC]->id : null;
// The section's parent.
$thissectionextra->parentid = ($levelsan > FORMAT_MULTITOPIC_SECTION_LEVEL_ROOT) ?
$sectionextraatlevel[$levelsan - 1]->id : null;
// Initialise tree-related properties to be set in the reverse pass.
$thissectionextra->hassubsections = false; // Whether this section has any subsections (page or topic).
$thissectionextra->pagedepth = $levelsan; // The lowest level of all sub-pages.
$thissectionextra->pagedepthdirect = $levelsan; // The lowest level of direct sub-pages.
// Set visibility properties.
$thissectionextra->parentvisiblesan = ($levelsan <= FORMAT_MULTITOPIC_SECTION_LEVEL_ROOT) ?
true : $sectionextraatlevel[$levelsan - 1]->visiblesan;
$thissectionextra->visiblesan = ($levelsan <= FORMAT_MULTITOPIC_SECTION_LEVEL_ROOT) ?
true
: ($sectionextraatlevel[$levelsan - 1]->visiblesan && $thissection->visible);
// Set date-start property from previous section.
$thissectionextra->datestart = $sectionextraprevatlevel[FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC] ?
$sectionextraprevatlevel[FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC]->dateend
: (is_null($courseperioddays) ? null : $course->startdate);
// Set date-end property.
if ($levelsan < FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC) {
$sectionperioddays = 0;
} else {
$sectionperioddays = format_multitopic_duration_as_days($thissection->periodduration);
if ($sectionperioddays === null) {
$sectionperioddays = $courseperioddays;
}
}
$thissectionextra->dateend = (is_null($thissectionextra->datestart) || is_null($sectionperioddays)) ?
null
: ($thissectionextra->datestart + $sectionperioddays * 24 * 60 * 60);
// The level down to which this section contains the current section.
// Initialise for reverse pass.
$iscurrent = $thissectionextra->dateend
&& ($thissectionextra->datestart <= $timenow) && ($timenow < $thissectionextra->dateend);
$thissectionextra->currentnestedlevel = $iscurrent ? FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC
: FORMAT_MULTITOPIC_SECTION_LEVEL_ROOT - 1;
// Add this section to the list.
$fmtsectionsextra[$thissection->id] = $thissectionextra;
}
$this->fmtsectionsextra = $fmtsectionsextra;
$this->fmtmodinfo = $modinfo;
}
if ($needall && !$this->fmtsectionsextracomplete) {
// Reverse pass.
// Remembered sections.
$sectionnextatlevel = array_fill(FORMAT_MULTITOPIC_SECTION_LEVEL_ROOT,
FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC - FORMAT_MULTITOPIC_SECTION_LEVEL_ROOT + 1, null);
foreach (array_reverse($fmtsectionsextra) as $thissectionextra) {
$thissection = $thissectionextra->sectionbase;
if (!empty($thissection->component)) {
continue;
}
$levelsan = $thissectionextra->levelsan;
// Tree properties from next sections.
$thissectionextra->nextupid = $sectionnextatlevel[$levelsan] ?
$sectionnextatlevel[$levelsan]->id : null;
$thissectionextra->nextpageid = $sectionnextatlevel[FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC - 1] ?
$sectionnextatlevel[FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC - 1]->id : null;
$thissectionextra->nextanyid = $sectionnextatlevel[FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC] ?
$sectionnextatlevel[FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC]->id : null;
// Parent's tree properties.
if ($thissectionextra->parentid) {
$parent = $fmtsectionsextra[$thissectionextra->parentid];
$parent->hassubsections = true;
if ($levelsan < FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC) {
$parent->pagedepth = max($parent->pagedepth, $thissectionextra->pagedepth);
$showsection = $thissection->uservisible || ($thissection->section == 0) ||
($parent->uservisible || ($parent->section == 0))
&& ($thissection->visible || !$course->hiddensections)
&& ($thissection->available || !empty($thissection->availableinfo));
if ($showsection) {
$parent->pagedepthdirect = max($parent->pagedepthdirect, $levelsan);
}
}
if ($thissectionextra->currentnestedlevel >= FORMAT_MULTITOPIC_SECTION_LEVEL_ROOT) {
$parent->currentnestedlevel = max($parent->currentnestedlevel, $levelsan - 1);
}
}
// Update remembered next sections.
for ($sublevel = $levelsan; $sublevel <= FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC; $sublevel++) {
$sectionnextatlevel[$sublevel] = $thissectionextra;
}
}
$this->fmtsectionsextra = $fmtsectionsextra;
$this->fmtsectionsextracomplete = true;
}
return $fmtsectionsextra;
}
/**
* Returns Multitopic-specific section information.
*
* @param int|stdClass|\section_info $section either section number or row from course_section table
* @param int $strictness
* @return \format_multitopic\section_info_extra
*/
final public function fmt_get_section_extra($section, int $strictness = IGNORE_MISSING) {
if (!is_object($section)) {
$section = (object)['section' => $section];
}
$sectionsextra = $this->fmt_get_sections_extra();
if (isset($section->id)) {
if (array_key_exists($section->id, $sectionsextra)) {
return $sectionsextra[$section->id];
}
} else if (isset($section->section)) {
foreach ($sectionsextra as $thissectionextra) {
if ($thissectionextra->section == $section->section) {
return $thissectionextra;
}
}
}
if ($strictness == MUST_EXIST) {
throw new moodle_exception('sectionnotexist');
}
return null;
}
// END ADDED.
/**
* Returns the display name of the given section that the course prefers.
*
* Use section name is specified by user. Otherwise use default.
*
* @param int|stdClass|\section_info $section Section object from database.
* @return string Display name that the course format prefers, e.g. "Section 2"
*/
public function get_section_name($section): string {
// ADDED.
if (!is_object($section)) {
$section = $this->get_section($section);
}
$sectionextra = $this->fmt_get_section_extra($section);
$weekword = new lang_string('week');
$weeksword = get_string_manager()->string_exists('weeks_capitalised', 'format_multitopic') ?
get_string('weeks_capitalised', 'format_multitopic') : get_string('weeks');
// Figure out the string for the week number.
$daystring = '';
if ($sectionextra->dateend && ($sectionextra->datestart < $sectionextra->dateend)) {
$currentyear = format_multitopic_week_date(time())->o;
$datestart = format_multitopic_week_date($sectionextra->datestart + 12 * 60 * 60);
$dateend = format_multitopic_week_date($sectionextra->dateend - 12 * 60 * 60);
if ($datestart->o == $dateend->o) {
// Within one year.
$yearstring = $datestart->o != $currentyear ? ($datestart->o . ' ') : '';
if ($datestart->W == $dateend->W) {
// Within one week.
$weekstring = $yearstring . $weekword . ' ' . $datestart->W;
if ($datestart->N == $dateend->N) {
// One day.
$daystring = $weekstring . ' ' . $datestart->D;
} else if (($datestart->N == 1) && ($dateend->N == 7)) {
// Whole week.
$daystring = $weekstring;
} else {
// Partial week.
$daystring = $weekstring . ' ' . $datestart->D . '–' . $dateend->D;
}
} else if (($datestart->N == 1) && ($dateend->N == 7)) {
// Spans whole weeks.
$daystring = $yearstring . $weeksword . ' ' . $datestart->W . '–' . $dateend->W;
} else {
// Spans partial weeks.
$daystring = $yearstring . $weekword . ' ' . $datestart->W . ' ' . $datestart->D
. '–' . $weekword . ' ' . $dateend->W . ' ' . $dateend->D;
}
} else {
// Spans years.
$yearstartstring = $datestart->o != $currentyear ? ($datestart->o . ' ') : '';
$yearendstring = $dateend->o != $currentyear ? ($dateend->o . ' ') : '';
if (($datestart->N == 1) && ($dateend->N == 7)) {
// Spans whole weeks.
$daystring = $yearstartstring . $weekword . ' ' . $datestart->W
. '–' . $yearendstring . $weekword . ' ' . $dateend->W;
} else {
// Spans partial weeks.
$daystring = $yearstartstring . $weekword . ' ' . $datestart->W . ' ' . $datestart->D
. '–' . $yearendstring . $weekword . ' ' . $dateend->W . ' ' . $dateend->D;
}
}
$daystring = '<span class="section-title-prefix">' . $daystring . ': ' . '</span>';
}
// END ADDED.
if ((string)$section->name !== '') {
return $daystring . format_string($section->name, true,
['context' => context_course::instance($this->courseid)]); // CHANGED.
} else {
return $daystring . $this->get_default_section_name($section);
}
}
/**
* Returns the default section name for the Multitopic course format.
*
* If the section number is 0, it will use the string with key = section0name from the course format's lang file.
* If the section number is not 0, the base implementation of course_format::get_default_section_name which uses
* the string with the key = 'sectionname' from the course format's lang file + the section number will be used.
*
* @param stdClass $section Section object from database or just field course_sections section
* @return string The default value for the section name.
*/
public function get_default_section_name($section): string {
if ($section->section == 0) {
// Return the general section.
return get_string('section0name', 'format_multitopic');
} else {
// Use course_format::get_default_section_name implementation which
// will display the section name in "Section n" format.
return parent::get_default_section_name($section);
}
}
/**
* Set if the current format instance will show all pages or an individual one.
*
* @param int|null $sectionid null for all pages, or a page's sectionid.
*/
public function set_sectionid(?int $sectionid): void {
$this->set_sectionnum(isset($sectionid) ? (object)['id' => $sectionid] : null);
}
/**
* Get if the current format instance will show all pages or an individual one.
*
* @return int|null null for all pages or the page's sectionid.
*/
public function get_sectionid(): ?int {
return $this->singlesectionid;
}
/**
* Set which section page the current format instance will show.
*
* @param int $singlesection a page's section number
* @deprecated
*/
public function set_section_number(int $singlesection): void {
$this->set_sectionnum($singlesection);
}
/**
* Set the current page to display.
*
* @param \section_info|stdClass|int|null $section null for all pages or a page's section info or number.
*/
public function set_sectionnum($section): void {
if ($section === null) {
$this->singlesection = null;
$this->singlesectionid = null;
$this->originalsinglesectionid = null;
return;
}
$sectionextra = $this->fmt_get_section_extra($section);
$this->originalsinglesectionid = $sectionextra->id;
// If display section is a topic, get the page it is on instead.
if ($sectionextra->levelsan >= FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC) {
$sectionsextra = $this->fmt_get_sections_extra();
$sectionextra = $sectionsextra[$sectionextra->parentid];
}
$this->singlesectionid = $sectionextra->id;
$this->singlesection = $sectionextra->section;
}
/**
* Get the current page's section number to display.
*
* @return int|null the current page's section number or null when there is no single page.
*/
public function get_sectionnum(): ?int {
return $this->singlesection;
}
/**
* Get the section number of the page the current format instance will show.
*
* @return int the page's section number
* @deprecated
*/
public function get_section_number(): int {
if ($this->singlesection === null) {
return 0;
}
return $this->singlesection;
}
/**
* Return the format section preferences.
*
* @return array of preferences indexed by section ID
*/
public function get_sections_preferences(): array {
$result = [];
$course = $this->get_course();
$sectionsextra = $this->fmt_get_sections_extra();
$coursesectionscache = cache::make('core', 'coursesectionspreferences');
$coursesections = $coursesectionscache->get($course->id);
$collapsedset = ($coursesections && count($coursesections) > 0) ? max(array_keys($coursesections)) : 0;
$collapsedset = ($collapsedset > 0 && isset($coursesections[$collapsedset]->fmtcollapsedset)) ?
$collapsedset : 0;
if ($collapsedset >= max(array_keys($sectionsextra))) {
return $coursesections;
}
$sectionpreferences = $this->fmt_set_get_sections_preferences();
foreach ($sectionpreferences as $preference => $sectionids) {
if (!empty($sectionids) && is_array($sectionids)) {
foreach ($sectionids as $sectionid) {
if (!isset($result[$sectionid])) {
$result[$sectionid] = new stdClass();
}
$result[$sectionid]->$preference = true;
}
}
}
$coursesectionscache->set($course->id, $result);
return $result;
}
/**
* Return the format section preferences.
*
* @return array of preferences indexed by preference name
*/
public function get_sections_preferences_by_preference(): array {
return $this->fmt_set_get_sections_preferences();
}
/**
* Set the format section preferences.
*
* @param string $preferencename preference name
* @param int[] $sectionids affected section IDs
*
*/
public function set_sections_preference(string $preferencename, array $sectionids): void {
$this->fmt_set_get_sections_preferences($preferencename, $sectionids);
}
/**
* Set and return the format section preferences.
*
* @param string|null $preferencename preference name
* @param int[]|null $sectionids affected section IDs
* @return array of preferences indexed by section ID
*
*/
protected function fmt_set_get_sections_preferences(?string $preferencename = null, ?array $sectionids = null): array {
$course = $this->get_course();
$sectionsextra = $this->fmt_get_sections_extra();
$sectionpreferences = parent::get_sections_preferences_by_preference();
$collapsedsetold = (count($sectionpreferences['fmtcollapsedset'] ?? []) > 0) ?
max($sectionpreferences['fmtcollapsedset']) : 0;
// Clean, and index content collapsed.
$contentcollapsedindexed = [];
foreach ($sectionpreferences as $prefname => $sectids) {
if ($prefname != 'fmtcollapsedset') {
foreach ($sectids as $i => $sectionid) {
if (!isset($sectionsextra[$sectionid])) {
unset($sectids[$i]);
} else if ($prefname == 'contentcollapsed') {
$contentcollapsedindexed[$sectionid] = true;
}
}
}
}
// Try autocollapsing content.
$sectionidsold = $sectionpreferences['contentcollapsed'] ?? null;
$collapsedset = $collapsedsetold;
$autocollapsedchanged = false;
foreach ($sectionsextra as $sectionid => $sectionextra) {
$section = $sectionextra->sectionbase;
if ($sectionid > $collapsedsetold
&& $sectionextra->levelsan >= FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC
&& ((($section->collapsible != '') ? $section->collapsible : $course->collapsible) != '0')
&& !isset($contentcollapsedindexed[$sectionid])) {
if (!isset($sectionpreferences['contentcollapsed'])) {
$sectionpreferences['contentcollapsed'] = [];
}
$sectionpreferences['contentcollapsed'][] = $sectionid;
$autocollapsedchanged = true;
}
$collapsedset = max($collapsedset, $sectionid);
}
$sectionpreferences['fmtcollapsedset'] = [ $collapsedset ];
$collapsedsetupdated = false;
if ($autocollapsedchanged) {
try {
$this->fmt_set_sections_preferences_sub($sectionpreferences);
$collapsedsetupdated = true;
} catch (coding_exception $e) {
if (isset($sectionidsold)) {
$sectionpreferences['contentcollapsed'] = $sectionidsold;
} else {
unset($sectionpreferences['contentcollapsed']);
}
}
}
// Try writing specified changes.
$error = null;
if ($preferencename) {
$sectionidsold = $sectionpreferences[$preferencename] ?? null;
$sectionpreferences[$preferencename] = $sectionids;
try {
$this->fmt_set_sections_preferences_sub($sectionpreferences);
$collapsedsetupdated = true;
} catch (coding_exception $e) {
$error = $e; // We may want to rethrow this.
}
// Try writing specified changes without last seen section.
if ($error && isset($sectionidsold) && count($sectionids) < count($sectionidsold)
&& !$collapsedsetupdated && $collapsedset > $collapsedsetold) {
$error = null;
if ($collapsedsetold > 0) {
$sectionpreferences['fmtcollapsedset'] = [ $collapsedsetold ];
} else {
unset($sectionpreferences['fmtcollapsedset']);
}
try {
$this->fmt_set_sections_preferences_sub($sectionpreferences);
} catch (coding_exception $e) {
$error = $e; // We will want to rethrow this.
}
}
if ($error) {
if (isset($sectionidsold)) {
$sectionpreferences[$preferencename] = $sectionidsold;
} else {
unset($sectionpreferences[$preferencename]);
}
}
}
// If nothing else, try writing last seen section.
if (!$collapsedsetupdated && $collapsedset > $collapsedsetold) {
$sectionpreferences['fmtcollapsedset'] = [ $collapsedset ];
try {
$this->fmt_set_sections_preferences_sub($sectionpreferences);
$collapsedsetupdated = true;
} catch (coding_exception $e) {
// Do nothing, because there's nothing we can do.
$collapsedsetupdated = false;
}
}
if ($error) {
throw $error;
}
return $sectionpreferences;
}
/**
* Set the format section preferences, given all preferences indexed by preference.
*
* @param array $sectionpreferences of preferences indexed by preference
*
*/
protected function fmt_set_sections_preferences_sub(array $sectionpreferences): void {
global $USER;
$course = $this->get_course();
set_user_preference('coursesectionspreferences_' . $course->id, json_encode($sectionpreferences), $USER->id);
// Invalidate section preferences cache.
$coursesectionscache = cache::make('core', 'coursesectionspreferences');
$coursesectionscache->delete($course->id);
}
/**
* The URL to use for the specified course (with section).
*
* @param int|stdClass $section Section object from database or just field course_sections.section
* Should specify fmt calculated properties,
* specifically levelsan, and parentid where levelsan is topic level.
* @param array $options options for view URL. At the moment core uses:
* 'fmtedit' (bool) if true, return URL for edit page rather than view page
* 'navigation' (bool) if true and section has no separate page, the function returns null
* @return null|moodle_url
*/
public function get_view_url($section, $options = []) {
global $CFG;
$course = $this->get_course();
$url = new moodle_url( ($options['fmtedit'] ?? false) ? '/course/format/multitopic/_course_view.php'
: '/course/view.php', ['id' => $course->id]); // CHANGED.
// REMOVED section return.
// REMOVED convert sectioninfo to number.
$sectionextra = ($section === null) ? null : $this->fmt_get_section_extra($section); // ADDED.
if ($sectionextra !== null) { // CHANGED.
$pageid = $sectionextra->id;
if (!empty($sectionextra->sectionbase->component)
&& $sectionextra->sectionbase->component == 'mod_subsection') {
$modinfo = get_fast_modinfo($course);
$pageid = $modinfo->get_instances_of('subsection')[$sectionextra->sectionbase->itemid]->section;
}
// CHANGED.
$pageextra = ($pageid == $sectionextra->id) ?
$sectionextra : $this->fmt_get_section_extra((object)['id' => $pageid]);
$pageid = ($pageextra->levelsan < FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC) ?
$pageextra->id : $pageextra->parentid;
if ($pageid && $pageid != $this->fmtrootsectionid) {
if (!empty($pageextra->sectionbase->component)) {
$url = new moodle_url( '/course/section.php', ['id' => $pageid]);
} else {
$url->param('sectionid', $pageid);
}
}
if ($sectionextra && $sectionextra->id != $pageid) {
if (empty($CFG->linkcoursesections) && !empty($options['navigation']) && $CFG->version < 2023120700) {
return null;
}
$url->set_anchor('sectionid-' . $sectionextra->id . '-title');
}
// END CHANGED.
}
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.
*
* @return stdClass
*/
public function supports_ajax(): stdClass {
$ajaxsupport = new stdClass();
$ajaxsupport->capable = true;
return $ajaxsupport;
}
/**
* Returns true if this course format is compatible with content components.
*
* Using components means the content elements can watch the frontend course state and
* react to the changes. Formats with component compatibility can have more interactions
* without refreshing the page, like having drag and drop from the course index to reorder
* sections and activities.
*
* @return bool if the format is compatible with components.
*/
public function supports_components(): bool {
return true;
}
/**
* Loads all of the course sections into the navigation.
*
* @param global_navigation $navigation
* @param navigation_node $node The course node within the navigation
* @return void
*/
public function extend_course_navigation($navigation, navigation_node $node) {
global $PAGE;
$navigationwrapper = new \format_multitopic\global_navigation_wrapper($navigation); // ADDED.
// If section is specified in course/view.php, make sure it is expanded in navigation.
if ($navigation->includesectionnum === false) {
// CHANGED.
$selectedsectionid = optional_param('sectionid', null, PARAM_INT);
if ($selectedsectionid !== null && (!defined('AJAX_SCRIPT') || AJAX_SCRIPT == '0') &&
$PAGE->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
$navigationwrapper->innerincludesectionid = $selectedsectionid;
}
// END CHANGED.
}
// Check if there are callbacks to extend course navigation.
// REMOVED function call.
// INCLUDED instead /course/format/classes/base.php function extend_course_navigation body.
if ($course = $this->get_course()) {
$navigationwrapper->load_generic_course_sections($course, $node); // CHANGED: Wrapped navigation object.
}
// END INCLUDED.
// We want to remove the general section if it is empty.
// REMOVED.
}
// INCLUDED instead /course/format/weeks/lib.php function ajax_section_move .
/**
* 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(): array {
global $PAGE;
$titles = [];
$current = -1;
$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);
if ($this->is_section_current($section)) {
$current = $number;
}
}
}
return ['sectiontitles' => $titles, 'current' => $current, 'action' => 'move'];
}
// END INCLUDED.
/**
* 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(): array {
return [
BLOCK_POS_LEFT => [],
BLOCK_POS_RIGHT => [],
];
}
/**
* Definitions of the additional options that this course format uses for courses.
*
* Multitopic format uses the following options:
* - periodduration (from Periods format): how long each topic takes. (Only 1 day, 1 week, or null are currently supported.)
* - hiddensections (from the standard Topics format): whether hidden sections are shown collapsed, or not shown at all.
* - bannerslice (custom option): how far down the course image to take the banner slice from (0-100).
*
* @param bool $foreditform
* @return array of options
*/
public function course_format_options($foreditform = false): array {
static $courseformatoptions = false;
if ($courseformatoptions === false) {
$courseconfig = get_config('moodlecourse');
$courseformatoptions = [
// INCLUDED /course/format/periods/lib.php function course_format_options 'periodduration'.
'periodduration' => [
'default' => null, // CHANGED.
'type' => PARAM_NOTAGS,
],
// END INCLUDED.
// ADDED.
'collapsible' => [
'default' => '1',
'type' => PARAM_ALPHANUM,
],
// END ADDED.
'hiddensections' => [
'default' => $courseconfig->hiddensections,
'type' => PARAM_INT,
],
// REMOVED: course display.
// ADDED.
'bannerslice' => [
'default' => 0,
'type' => PARAM_INT,
],
// END ADDED.
];
}
if ($foreditform && !isset($courseformatoptions['hiddensections']['label'])) { // CHANGED.
$courseformatoptionsedit = [
// INCLUDED /course/format/periods/lib.php function course_format_options $foreditform 'periodduration' .
'periodduration' => [
'label' => new lang_string('perioddurationdefault', 'format_multitopic'), // CHANGED.
'help' => 'perioddurationdefault',
'help_component' => 'format_multitopic', // CHANGED.
'element_type' => 'select', // CHANGED.
// REMOVED: Replaced periodduration type.
// ADDED.
'element_attributes' => [[
null => new lang_string('period_undefined', 'format_multitopic'),
'1 day' => new lang_string('numday', '', 1),
'1 week' => new lang_string('numweek', '', 1),
], ],
// END ADDED.
],
// END INCLUDED.
// ADDED.
'collapsible' => [
'label' => get_string('collapsibledefault', 'format_multitopic'),
'element_type' => 'select',
'element_attributes' => [
[
'0' => get_string('no'),
'1' => get_string('yes'),
],
],
'help' => 'collapsibledefault',
'help_component' => 'format_multitopic',
],
// END ADDED.
'hiddensections' => [
'label' => new lang_string('hiddensections'),
'help' => 'hiddensections',
'help_component' => 'moodle',
'element_type' => 'select',
'element_attributes' => [
[
0 => new lang_string('hiddensectionscollapsed'),
1 => new lang_string('hiddensectionsinvisible'),
],
],
],
// REMOVED: coursedisplay .
// ADDED.
'bannerslice' => [
'label' => new lang_string('bannerslice', 'format_multitopic'),
'help' => 'bannerslice',
'help_component' => 'format_multitopic',
'element_type' => 'select',