1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Contains the base definition class for any course format plugin.
|
|
|
19 |
*
|
|
|
20 |
* @package core_courseformat
|
|
|
21 |
* @copyright 2020 Ferran Recio <ferran@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace core_courseformat;
|
|
|
26 |
|
|
|
27 |
use navigation_node;
|
|
|
28 |
use moodle_page;
|
|
|
29 |
use cm_info;
|
|
|
30 |
use core_component;
|
|
|
31 |
use course_modinfo;
|
|
|
32 |
use html_writer;
|
|
|
33 |
use section_info;
|
|
|
34 |
use context_course;
|
|
|
35 |
use editsection_form;
|
|
|
36 |
use moodle_exception;
|
|
|
37 |
use coding_exception;
|
|
|
38 |
use moodle_url;
|
|
|
39 |
use lang_string;
|
|
|
40 |
use core_external\external_api;
|
|
|
41 |
use stdClass;
|
|
|
42 |
use cache;
|
|
|
43 |
use core_courseformat\output\legacy_renderer;
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Base class for course formats
|
|
|
47 |
*
|
|
|
48 |
* Each course format must declare class
|
|
|
49 |
* class format_FORMATNAME extends core_courseformat\base {}
|
|
|
50 |
* in file lib.php
|
|
|
51 |
*
|
|
|
52 |
* For each course just one instance of this class is created and it will always be returned by
|
|
|
53 |
* course_get_format($courseorid). Format may store it's specific course-dependent options in
|
|
|
54 |
* variables of this class.
|
|
|
55 |
*
|
|
|
56 |
* In rare cases instance of child class may be created just for format without course id
|
|
|
57 |
* i.e. to check if format supports AJAX.
|
|
|
58 |
*
|
|
|
59 |
* Also course formats may extend class section_info and overwrite
|
|
|
60 |
* course_format::build_section_cache() to return more information about sections.
|
|
|
61 |
*
|
|
|
62 |
* If you are upgrading from Moodle 2.3 start with copying the class format_legacy and renaming
|
|
|
63 |
* it to format_FORMATNAME, then move the code from your callback functions into
|
|
|
64 |
* appropriate functions of the class.
|
|
|
65 |
*
|
|
|
66 |
* @package core_courseformat
|
|
|
67 |
* @copyright 2012 Marina Glancy
|
|
|
68 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
69 |
*/
|
|
|
70 |
abstract class base {
|
|
|
71 |
/** @var int Id of the course in this instance (maybe 0) */
|
|
|
72 |
protected $courseid;
|
|
|
73 |
/** @var string format used for this course. Please note that it can be different from
|
|
|
74 |
* course.format field if course referes to non-existing of disabled format */
|
|
|
75 |
protected $format;
|
|
|
76 |
/** @var stdClass course data for course object, please use course_format::get_course() */
|
|
|
77 |
protected $course = false;
|
|
|
78 |
/** @var array caches format options, please use course_format::get_format_options() */
|
|
|
79 |
protected $formatoptions = array();
|
|
|
80 |
/** @var int|null the section number in single section format, null for multiple section formats. */
|
|
|
81 |
protected $singlesection = null;
|
|
|
82 |
/** @var int|null the sectionid when a single section is selected, null when multiple sections are displayed. */
|
|
|
83 |
protected $singlesectionid = null;
|
|
|
84 |
/** @var course_modinfo the current course modinfo, please use course_format::get_modinfo() */
|
|
|
85 |
private $modinfo = null;
|
|
|
86 |
/** @var array cached instances */
|
|
|
87 |
private static $instances = array();
|
|
|
88 |
/** @var array plugin name => class name. */
|
|
|
89 |
private static $classesforformat = array('site' => 'site');
|
|
|
90 |
/** @var sectionmanager the format section manager. */
|
|
|
91 |
protected $sectionmanager = null;
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Creates a new instance of class
|
|
|
95 |
*
|
|
|
96 |
* Please use course_get_format($courseorid) to get an instance of the format class
|
|
|
97 |
*
|
|
|
98 |
* @param string $format
|
|
|
99 |
* @param int $courseid
|
|
|
100 |
*/
|
|
|
101 |
protected function __construct($format, $courseid) {
|
|
|
102 |
$this->format = $format;
|
|
|
103 |
$this->courseid = $courseid;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Validates that course format exists and enabled and returns either itself or default format
|
|
|
108 |
*
|
|
|
109 |
* @param string $format
|
|
|
110 |
* @return string
|
|
|
111 |
*/
|
|
|
112 |
final protected static function get_format_or_default($format) {
|
|
|
113 |
global $CFG;
|
|
|
114 |
require_once($CFG->dirroot . '/course/lib.php');
|
|
|
115 |
|
|
|
116 |
if (array_key_exists($format, self::$classesforformat)) {
|
|
|
117 |
return self::$classesforformat[$format];
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
$plugins = get_sorted_course_formats();
|
|
|
121 |
foreach ($plugins as $plugin) {
|
|
|
122 |
self::$classesforformat[$plugin] = $plugin;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
if (array_key_exists($format, self::$classesforformat)) {
|
|
|
126 |
return self::$classesforformat[$format];
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
if (PHPUNIT_TEST && class_exists('format_' . $format)) {
|
|
|
130 |
// Allow unittests to use non-existing course formats.
|
|
|
131 |
return $format;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
// Else return default format.
|
|
|
135 |
$defaultformat = get_config('moodlecourse', 'format');
|
|
|
136 |
if (!in_array($defaultformat, $plugins)) {
|
|
|
137 |
// When default format is not set correctly, use the first available format.
|
|
|
138 |
$defaultformat = reset($plugins);
|
|
|
139 |
}
|
|
|
140 |
debugging('Format plugin format_'.$format.' is not found. Using default format_'.$defaultformat, DEBUG_DEVELOPER);
|
|
|
141 |
|
|
|
142 |
self::$classesforformat[$format] = $defaultformat;
|
|
|
143 |
return $defaultformat;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Get class name for the format.
|
|
|
148 |
*
|
|
|
149 |
* If course format xxx does not declare class format_xxx, format_legacy will be returned.
|
|
|
150 |
* This function also includes lib.php file from corresponding format plugin
|
|
|
151 |
*
|
|
|
152 |
* @param string $format
|
|
|
153 |
* @return string
|
|
|
154 |
*/
|
|
|
155 |
final protected static function get_class_name($format) {
|
|
|
156 |
global $CFG;
|
|
|
157 |
static $classnames = array('site' => 'format_site');
|
|
|
158 |
if (!isset($classnames[$format])) {
|
|
|
159 |
$plugins = core_component::get_plugin_list('format');
|
|
|
160 |
$usedformat = self::get_format_or_default($format);
|
|
|
161 |
if (isset($plugins[$usedformat]) && file_exists($plugins[$usedformat].'/lib.php')) {
|
|
|
162 |
require_once($plugins[$usedformat].'/lib.php');
|
|
|
163 |
}
|
|
|
164 |
$classnames[$format] = 'format_'. $usedformat;
|
|
|
165 |
if (!class_exists($classnames[$format])) {
|
|
|
166 |
require_once($CFG->dirroot.'/course/format/formatlegacy.php');
|
|
|
167 |
$classnames[$format] = 'format_legacy';
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
return $classnames[$format];
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
/**
|
|
|
174 |
* Returns an instance of the class
|
|
|
175 |
*
|
|
|
176 |
* @todo MDL-35727 use MUC for caching of instances, limit the number of cached instances
|
|
|
177 |
*
|
|
|
178 |
* @param int|stdClass $courseorid either course id or
|
|
|
179 |
* an object that has the property 'format' and may contain property 'id'
|
|
|
180 |
* @return base
|
|
|
181 |
*/
|
|
|
182 |
final public static function instance($courseorid) {
|
|
|
183 |
global $DB;
|
|
|
184 |
if (!is_object($courseorid)) {
|
|
|
185 |
$courseid = (int)$courseorid;
|
|
|
186 |
if ($courseid && isset(self::$instances[$courseid]) && count(self::$instances[$courseid]) == 1) {
|
|
|
187 |
$formats = array_keys(self::$instances[$courseid]);
|
|
|
188 |
$format = reset($formats);
|
|
|
189 |
} else {
|
|
|
190 |
$format = $DB->get_field('course', 'format', array('id' => $courseid), MUST_EXIST);
|
|
|
191 |
}
|
|
|
192 |
} else {
|
|
|
193 |
$format = $courseorid->format;
|
|
|
194 |
if (isset($courseorid->id)) {
|
|
|
195 |
$courseid = clean_param($courseorid->id, PARAM_INT);
|
|
|
196 |
} else {
|
|
|
197 |
$courseid = 0;
|
|
|
198 |
}
|
|
|
199 |
}
|
|
|
200 |
// Validate that format exists and enabled, use default otherwise.
|
|
|
201 |
$format = self::get_format_or_default($format);
|
|
|
202 |
if (!isset(self::$instances[$courseid][$format])) {
|
|
|
203 |
$classname = self::get_class_name($format);
|
|
|
204 |
self::$instances[$courseid][$format] = new $classname($format, $courseid);
|
|
|
205 |
}
|
|
|
206 |
return self::$instances[$courseid][$format];
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
/**
|
|
|
210 |
* Resets cache for the course (or all caches)
|
|
|
211 |
*
|
|
|
212 |
* To be called from rebuild_course_cache()
|
|
|
213 |
*
|
|
|
214 |
* @param int $courseid
|
|
|
215 |
*/
|
|
|
216 |
final public static function reset_course_cache($courseid = 0) {
|
|
|
217 |
if ($courseid) {
|
|
|
218 |
if (isset(self::$instances[$courseid])) {
|
|
|
219 |
foreach (self::$instances[$courseid] as $format => $object) {
|
|
|
220 |
// In case somebody keeps the reference to course format object.
|
|
|
221 |
self::$instances[$courseid][$format]->course = false;
|
|
|
222 |
self::$instances[$courseid][$format]->formatoptions = array();
|
|
|
223 |
self::$instances[$courseid][$format]->modinfo = null;
|
|
|
224 |
}
|
|
|
225 |
unset(self::$instances[$courseid]);
|
|
|
226 |
}
|
|
|
227 |
} else {
|
|
|
228 |
self::$instances = array();
|
|
|
229 |
}
|
|
|
230 |
}
|
|
|
231 |
/**
|
|
|
232 |
* Reset the current user for all courses.
|
|
|
233 |
*
|
|
|
234 |
* The course format cache resets every time the course cache resets but
|
|
|
235 |
* also when the user changes their language, all course editors
|
|
|
236 |
*
|
|
|
237 |
* @return void
|
|
|
238 |
*/
|
|
|
239 |
public static function session_cache_reset_all(): void {
|
|
|
240 |
$statecache = cache::make('core', 'courseeditorstate');
|
|
|
241 |
$statecache->purge();
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
/**
|
|
|
245 |
* Reset the current user course format cache.
|
|
|
246 |
*
|
|
|
247 |
* The course format cache resets every time the course cache resets but
|
|
|
248 |
* also when the user changes their course format preference, complete
|
|
|
249 |
* an activity...
|
|
|
250 |
*
|
|
|
251 |
* @param stdClass $course the course object
|
|
|
252 |
* @return string the new statekey
|
|
|
253 |
*/
|
|
|
254 |
public static function session_cache_reset(stdClass $course): string {
|
|
|
255 |
$statecache = cache::make('core', 'courseeditorstate');
|
|
|
256 |
$newkey = $course->cacherev . '_' . time();
|
|
|
257 |
$statecache->set($course->id, $newkey);
|
|
|
258 |
return $newkey;
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
/**
|
|
|
262 |
* Return the current user course format cache key.
|
|
|
263 |
*
|
|
|
264 |
* The course format session cache can be used to cache the
|
|
|
265 |
* user course representation. The statekey will be reset when the
|
|
|
266 |
* the course state changes. For example when the course is edited,
|
|
|
267 |
* the user completes an activity or simply some course preference
|
|
|
268 |
* like collapsing a section happens.
|
|
|
269 |
*
|
|
|
270 |
* @param stdClass $course the course object
|
|
|
271 |
* @return string the current statekey
|
|
|
272 |
*/
|
|
|
273 |
public static function session_cache(stdClass $course): string {
|
|
|
274 |
$statecache = cache::make('core', 'courseeditorstate');
|
|
|
275 |
$statekey = $statecache->get($course->id);
|
|
|
276 |
// Validate the statekey code.
|
|
|
277 |
if (preg_match('/^[0-9]+_[0-9]+$/', $statekey)) {
|
|
|
278 |
list($cacherev) = explode('_', $statekey);
|
|
|
279 |
if ($cacherev == $course->cacherev) {
|
|
|
280 |
return $statekey;
|
|
|
281 |
}
|
|
|
282 |
}
|
|
|
283 |
return self::session_cache_reset($course);
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
/**
|
|
|
287 |
* Returns the format name used by this course
|
|
|
288 |
*
|
|
|
289 |
* @return string
|
|
|
290 |
*/
|
|
|
291 |
final public function get_format() {
|
|
|
292 |
return $this->format;
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
/**
|
|
|
296 |
* Returns id of the course (0 if course is not specified)
|
|
|
297 |
*
|
|
|
298 |
* @return int
|
|
|
299 |
*/
|
|
|
300 |
final public function get_courseid() {
|
|
|
301 |
return $this->courseid;
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
/**
|
|
|
305 |
* Returns the course context.
|
|
|
306 |
*
|
|
|
307 |
* @return context_course the course context
|
|
|
308 |
*/
|
|
|
309 |
final public function get_context(): context_course {
|
|
|
310 |
return context_course::instance($this->courseid);
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
/**
|
|
|
314 |
* Returns a record from course database table plus additional fields
|
|
|
315 |
* that course format defines
|
|
|
316 |
*
|
|
|
317 |
* @return ?stdClass
|
|
|
318 |
*/
|
|
|
319 |
public function get_course() {
|
|
|
320 |
global $DB;
|
|
|
321 |
if (!$this->courseid) {
|
|
|
322 |
return null;
|
|
|
323 |
}
|
|
|
324 |
if ($this->course === false) {
|
|
|
325 |
$this->course = get_course($this->courseid);
|
|
|
326 |
$options = $this->get_format_options();
|
|
|
327 |
$dbcoursecolumns = null;
|
|
|
328 |
foreach ($options as $optionname => $optionvalue) {
|
|
|
329 |
if (isset($this->course->$optionname)) {
|
|
|
330 |
// Course format options must not have the same names as existing columns in db table "course".
|
|
|
331 |
if (!isset($dbcoursecolumns)) {
|
|
|
332 |
$dbcoursecolumns = $DB->get_columns('course');
|
|
|
333 |
}
|
|
|
334 |
if (isset($dbcoursecolumns[$optionname])) {
|
|
|
335 |
debugging('The option name '.$optionname.' in course format '.$this->format.
|
|
|
336 |
' is invalid because the field with the same name exists in {course} table',
|
|
|
337 |
DEBUG_DEVELOPER);
|
|
|
338 |
continue;
|
|
|
339 |
}
|
|
|
340 |
}
|
|
|
341 |
$this->course->$optionname = $optionvalue;
|
|
|
342 |
}
|
|
|
343 |
}
|
|
|
344 |
return $this->course;
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
/**
|
|
|
348 |
* Get the course display value for the current course.
|
|
|
349 |
*
|
|
|
350 |
* Formats extending topics or weeks will use coursedisplay as this setting name
|
|
|
351 |
* so they don't need to override the method. However, if the format uses a different
|
|
|
352 |
* display logic it must override this method to ensure the core renderers know
|
|
|
353 |
* if a COURSE_DISPLAY_MULTIPAGE or COURSE_DISPLAY_SINGLEPAGE is being used.
|
|
|
354 |
*
|
|
|
355 |
* @return int The current value (COURSE_DISPLAY_MULTIPAGE or COURSE_DISPLAY_SINGLEPAGE)
|
|
|
356 |
*/
|
|
|
357 |
public function get_course_display(): int {
|
|
|
358 |
return $this->get_course()->coursedisplay ?? COURSE_DISPLAY_SINGLEPAGE;
|
|
|
359 |
}
|
|
|
360 |
|
|
|
361 |
/**
|
|
|
362 |
* Return the current course modinfo.
|
|
|
363 |
*
|
|
|
364 |
* This method is used mainly by the output components to avoid unnecesary get_fast_modinfo calls.
|
|
|
365 |
*
|
|
|
366 |
* @return course_modinfo
|
|
|
367 |
*/
|
|
|
368 |
public function get_modinfo(): course_modinfo {
|
|
|
369 |
global $USER;
|
|
|
370 |
if ($this->modinfo === null) {
|
|
|
371 |
$this->modinfo = course_modinfo::instance($this->courseid, $USER->id);
|
|
|
372 |
}
|
|
|
373 |
return $this->modinfo;
|
|
|
374 |
}
|
|
|
375 |
|
|
|
376 |
/**
|
|
|
377 |
* Method used in the rendered and during backup instead of legacy 'numsections'
|
|
|
378 |
*
|
|
|
379 |
* Default renderer will treat sections with sectionnumber greater that the value returned by this
|
|
|
380 |
* method as "orphaned" and not display them on the course page unless in editing mode.
|
|
|
381 |
* Backup will store this value as 'numsections'.
|
|
|
382 |
*
|
|
|
383 |
* This method ensures that 3rd party course format plugins that still use 'numsections' continue to
|
|
|
384 |
* work but at the same time we no longer expect formats to have 'numsections' property.
|
|
|
385 |
*
|
|
|
386 |
* @return int The last section number, or -1 if sections are entirely missing
|
|
|
387 |
*/
|
|
|
388 |
public function get_last_section_number() {
|
|
|
389 |
$course = $this->get_course();
|
|
|
390 |
if (isset($course->numsections)) {
|
|
|
391 |
return $course->numsections;
|
|
|
392 |
}
|
|
|
393 |
$modinfo = get_fast_modinfo($course);
|
|
|
394 |
$sections = $modinfo->get_section_info_all();
|
|
|
395 |
|
|
|
396 |
// Sections seem to be missing entirely. Avoid subsequent errors and return early.
|
|
|
397 |
if (count($sections) === 0) {
|
|
|
398 |
return -1;
|
|
|
399 |
}
|
|
|
400 |
|
|
|
401 |
return (int)max(array_keys($sections));
|
|
|
402 |
}
|
|
|
403 |
|
|
|
404 |
/**
|
|
|
405 |
* Method used to get the maximum number of sections for this course format.
|
|
|
406 |
* @return int
|
|
|
407 |
*/
|
|
|
408 |
public function get_max_sections() {
|
|
|
409 |
$maxsections = get_config('moodlecourse', 'maxsections');
|
|
|
410 |
if (!isset($maxsections) || !is_numeric($maxsections)) {
|
|
|
411 |
$maxsections = 52;
|
|
|
412 |
}
|
|
|
413 |
return $maxsections;
|
|
|
414 |
}
|
|
|
415 |
|
|
|
416 |
/**
|
|
|
417 |
* Returns true if the course has a front page.
|
|
|
418 |
*
|
|
|
419 |
* This function is called to determine if the course has a view page, whether or not
|
|
|
420 |
* it contains a listing of activities. It can be useful to set this to false when the course
|
|
|
421 |
* format has only one activity and ignores the course page. Or if there are multiple
|
|
|
422 |
* activities but no page to see the centralised information.
|
|
|
423 |
*
|
|
|
424 |
* Initially this was created to know if forms should add a button to return to the course page.
|
|
|
425 |
* So if 'Return to course' does not make sense in your format your should probably return false.
|
|
|
426 |
*
|
|
|
427 |
* @return boolean
|
|
|
428 |
* @since Moodle 2.6
|
|
|
429 |
*/
|
|
|
430 |
public function has_view_page() {
|
|
|
431 |
return true;
|
|
|
432 |
}
|
|
|
433 |
|
|
|
434 |
/**
|
|
|
435 |
* Generate the title for this section page.
|
|
|
436 |
*
|
|
|
437 |
* @return string the page title
|
|
|
438 |
*/
|
|
|
439 |
public function page_title(): string {
|
|
|
440 |
global $PAGE;
|
|
|
441 |
return $PAGE->title;
|
|
|
442 |
}
|
|
|
443 |
|
|
|
444 |
/**
|
|
|
445 |
* Returns true if this course format uses sections
|
|
|
446 |
*
|
|
|
447 |
* This function may be called without specifying the course id
|
|
|
448 |
* i.e. in course_format_uses_sections()
|
|
|
449 |
*
|
|
|
450 |
* Developers, note that if course format does use sections there should be defined a language
|
|
|
451 |
* string with the name 'sectionname' defining what the section relates to in the format, i.e.
|
|
|
452 |
* $string['sectionname'] = 'Topic';
|
|
|
453 |
* or
|
|
|
454 |
* $string['sectionname'] = 'Week';
|
|
|
455 |
*
|
|
|
456 |
* @return bool
|
|
|
457 |
*/
|
|
|
458 |
public function uses_sections() {
|
|
|
459 |
return false;
|
|
|
460 |
}
|
|
|
461 |
|
|
|
462 |
/**
|
|
|
463 |
* Returns true if this course format uses course index
|
|
|
464 |
*
|
|
|
465 |
* This function may be called without specifying the course id
|
|
|
466 |
* i.e. in course_index_drawer()
|
|
|
467 |
*
|
|
|
468 |
* @return bool
|
|
|
469 |
*/
|
|
|
470 |
public function uses_course_index() {
|
|
|
471 |
return false;
|
|
|
472 |
}
|
|
|
473 |
|
|
|
474 |
/**
|
|
|
475 |
* Returns true if this course format uses activity indentation.
|
|
|
476 |
*
|
|
|
477 |
* @return bool if the course format uses indentation.
|
|
|
478 |
*/
|
|
|
479 |
public function uses_indentation(): bool {
|
|
|
480 |
return true;
|
|
|
481 |
}
|
|
|
482 |
|
|
|
483 |
/**
|
|
|
484 |
* Returns a list of sections used in the course
|
|
|
485 |
*
|
|
|
486 |
* This is a shortcut to get_fast_modinfo()->get_section_info_all()
|
|
|
487 |
* @see get_fast_modinfo()
|
|
|
488 |
* @see course_modinfo::get_section_info_all()
|
|
|
489 |
*
|
|
|
490 |
* @return array of section_info objects
|
|
|
491 |
*/
|
|
|
492 |
final public function get_sections() {
|
|
|
493 |
if ($course = $this->get_course()) {
|
|
|
494 |
$modinfo = get_fast_modinfo($course);
|
|
|
495 |
return $modinfo->get_section_info_all();
|
|
|
496 |
}
|
|
|
497 |
return array();
|
|
|
498 |
}
|
|
|
499 |
|
|
|
500 |
/**
|
|
|
501 |
* Returns information about section used in course
|
|
|
502 |
*
|
|
|
503 |
* @param int|stdClass $section either section number (field course_section.section) or row from course_section table
|
|
|
504 |
* @param int $strictness
|
|
|
505 |
* @return ?section_info
|
|
|
506 |
*/
|
|
|
507 |
final public function get_section($section, $strictness = IGNORE_MISSING) {
|
|
|
508 |
if (is_object($section)) {
|
|
|
509 |
$sectionnum = $section->section;
|
|
|
510 |
} else {
|
|
|
511 |
$sectionnum = $section;
|
|
|
512 |
}
|
|
|
513 |
$sections = $this->get_sections();
|
|
|
514 |
if (array_key_exists($sectionnum, $sections)) {
|
|
|
515 |
return $sections[$sectionnum];
|
|
|
516 |
}
|
|
|
517 |
if ($strictness == MUST_EXIST) {
|
|
|
518 |
throw new moodle_exception('sectionnotexist');
|
|
|
519 |
}
|
|
|
520 |
return null;
|
|
|
521 |
}
|
|
|
522 |
|
|
|
523 |
/**
|
|
|
524 |
* Returns the display name of the given section that the course prefers.
|
|
|
525 |
*
|
|
|
526 |
* @param int|stdClass|section_info $section Section object from database or just field course_sections.section
|
|
|
527 |
* @return string Display name that the course format prefers, e.g. "Topic 2"
|
|
|
528 |
*/
|
|
|
529 |
public function get_section_name($section) {
|
|
|
530 |
if (is_object($section)) {
|
|
|
531 |
$sectionnum = $section->section;
|
|
|
532 |
} else {
|
|
|
533 |
$sectionnum = $section;
|
|
|
534 |
}
|
|
|
535 |
|
|
|
536 |
if (get_string_manager()->string_exists('sectionname', 'format_' . $this->format)) {
|
|
|
537 |
return get_string('sectionname', 'format_' . $this->format) . ' ' . $sectionnum;
|
|
|
538 |
}
|
|
|
539 |
|
|
|
540 |
// Return an empty string if there's no available section name string for the given format.
|
|
|
541 |
return '';
|
|
|
542 |
}
|
|
|
543 |
|
|
|
544 |
/**
|
|
|
545 |
* Returns the default section using course_format's implementation of get_section_name.
|
|
|
546 |
*
|
|
|
547 |
* @param int|stdClass $section Section object from database or just field course_sections section
|
|
|
548 |
* @return string The default value for the section name based on the given course format.
|
|
|
549 |
*/
|
|
|
550 |
public function get_default_section_name($section) {
|
|
|
551 |
return self::get_section_name($section);
|
|
|
552 |
}
|
|
|
553 |
|
|
|
554 |
/**
|
|
|
555 |
* Returns the name for the highlighted section.
|
|
|
556 |
*
|
|
|
557 |
* @return string The name for the highlighted section based on the given course format.
|
|
|
558 |
*/
|
|
|
559 |
public function get_section_highlighted_name(): string {
|
|
|
560 |
return get_string('highlighted');
|
|
|
561 |
}
|
|
|
562 |
|
|
|
563 |
/**
|
|
|
564 |
* Set if the current format instance will show multiple sections or an individual one.
|
|
|
565 |
*
|
|
|
566 |
* Some formats has the hability to swith from one section to multiple sections per page.
|
|
|
567 |
*
|
|
|
568 |
* @param int|null $sectionid null for all sections or a sectionid.
|
|
|
569 |
*/
|
|
|
570 |
public function set_sectionid(?int $sectionid): void {
|
|
|
571 |
if ($sectionid === null) {
|
|
|
572 |
$this->singlesection = null;
|
|
|
573 |
$this->singlesectionid = null;
|
|
|
574 |
return;
|
|
|
575 |
}
|
|
|
576 |
|
|
|
577 |
$modinfo = get_fast_modinfo($this->courseid);
|
|
|
578 |
$sectioninfo = $modinfo->get_section_info_by_id($sectionid);
|
|
|
579 |
if ($sectioninfo === null) {
|
|
|
580 |
throw new coding_exception('Invalid sectionid: '. $sectionid);
|
|
|
581 |
}
|
|
|
582 |
|
|
|
583 |
$this->singlesection = $sectioninfo->section;
|
|
|
584 |
$this->singlesectionid = $sectionid;
|
|
|
585 |
}
|
|
|
586 |
|
|
|
587 |
/**
|
|
|
588 |
* Get if the current format instance will show multiple sections or an individual one.
|
|
|
589 |
*
|
|
|
590 |
* Some formats has the hability to swith from one section to multiple sections per page,
|
|
|
591 |
* output components will use this method to know if the current display is a single or
|
|
|
592 |
* multiple sections.
|
|
|
593 |
*
|
|
|
594 |
* @return int|null null for all sections or the sectionid.
|
|
|
595 |
*/
|
|
|
596 |
public function get_sectionid(): ?int {
|
|
|
597 |
return $this->singlesectionid;
|
|
|
598 |
}
|
|
|
599 |
|
|
|
600 |
/**
|
|
|
601 |
* Set if the current format instance will show multiple sections or an individual one.
|
|
|
602 |
*
|
|
|
603 |
* Some formats has the hability to swith from one section to multiple sections per page.
|
|
|
604 |
*
|
|
|
605 |
* @param int $singlesection zero for all sections or a section number
|
|
|
606 |
* @deprecated Since 4.4. Use set_sectionnum instead.
|
|
|
607 |
* @todo MDL-80116 This will be deleted in Moodle 4.8.
|
|
|
608 |
*/
|
|
|
609 |
public function set_section_number(int $singlesection): void {
|
|
|
610 |
|
|
|
611 |
debugging(
|
|
|
612 |
'The method core_courseformat\base::set_section_number() has been deprecated, please use set_sectionnum() instead.',
|
|
|
613 |
DEBUG_DEVELOPER
|
|
|
614 |
);
|
|
|
615 |
|
|
|
616 |
if ($singlesection === 0) {
|
|
|
617 |
// Convert zero to null, to guarantee all the sections are displayed.
|
|
|
618 |
$singlesection = null;
|
|
|
619 |
}
|
|
|
620 |
$this->set_sectionnum($singlesection);
|
|
|
621 |
}
|
|
|
622 |
|
|
|
623 |
/**
|
|
|
624 |
* Set the current section number to display.
|
|
|
625 |
* Some formats has the hability to swith from one section to multiple sections per page.
|
|
|
626 |
*
|
|
|
627 |
* @since Moodle 4.4
|
|
|
628 |
* @param int|null $sectionnum null for all sections or a sectionid.
|
|
|
629 |
*/
|
|
|
630 |
public function set_sectionnum(?int $sectionnum): void {
|
|
|
631 |
if ($sectionnum === null) {
|
|
|
632 |
$this->singlesection = null;
|
|
|
633 |
$this->singlesectionid = null;
|
|
|
634 |
return;
|
|
|
635 |
}
|
|
|
636 |
|
|
|
637 |
$modinfo = get_fast_modinfo($this->courseid);
|
|
|
638 |
$sectioninfo = $modinfo->get_section_info($sectionnum);
|
|
|
639 |
if ($sectioninfo === null) {
|
|
|
640 |
throw new coding_exception('Invalid sectionnum: '. $sectionnum);
|
|
|
641 |
}
|
|
|
642 |
|
|
|
643 |
$this->singlesection = $sectionnum;
|
|
|
644 |
$this->singlesectionid = $sectioninfo->id;
|
|
|
645 |
}
|
|
|
646 |
|
|
|
647 |
/**
|
|
|
648 |
* Get the current section number to display.
|
|
|
649 |
* Some formats has the hability to swith from one section to multiple sections per page.
|
|
|
650 |
*
|
|
|
651 |
* @since Moodle 4.4
|
|
|
652 |
* @return int|null the current section number or null when there is no single section.
|
|
|
653 |
*/
|
|
|
654 |
public function get_sectionnum(): ?int {
|
|
|
655 |
return $this->singlesection;
|
|
|
656 |
}
|
|
|
657 |
|
|
|
658 |
/**
|
|
|
659 |
* Set if the current format instance will show multiple sections or an individual one.
|
|
|
660 |
*
|
|
|
661 |
* Some formats has the hability to swith from one section to multiple sections per page,
|
|
|
662 |
* output components will use this method to know if the current display is a single or
|
|
|
663 |
* multiple sections.
|
|
|
664 |
*
|
|
|
665 |
* @return int zero for all sections or the sectin number
|
|
|
666 |
* @deprecated Since 4.4. Use get_sectionnum instead.
|
|
|
667 |
* @todo MDL-80116 This will be deleted in Moodle 4.8.
|
|
|
668 |
*/
|
|
|
669 |
public function get_section_number(): int {
|
|
|
670 |
|
|
|
671 |
debugging(
|
|
|
672 |
'The method core_courseformat\base::get_section_number() has been deprecated, please use get_sectionnum() instead.',
|
|
|
673 |
DEBUG_DEVELOPER
|
|
|
674 |
);
|
|
|
675 |
|
|
|
676 |
if ($this->singlesection === null) {
|
|
|
677 |
// Convert null to zero, to guarantee all the sections are displayed.
|
|
|
678 |
return 0;
|
|
|
679 |
}
|
|
|
680 |
|
|
|
681 |
return $this->singlesection;
|
|
|
682 |
}
|
|
|
683 |
|
|
|
684 |
/**
|
|
|
685 |
* Return the format section preferences.
|
|
|
686 |
*
|
|
|
687 |
* @return array of preferences indexed by sectionid
|
|
|
688 |
*/
|
|
|
689 |
public function get_sections_preferences(): array {
|
|
|
690 |
global $USER;
|
|
|
691 |
|
|
|
692 |
$result = [];
|
|
|
693 |
|
|
|
694 |
$course = $this->get_course();
|
|
|
695 |
$coursesectionscache = cache::make('core', 'coursesectionspreferences');
|
|
|
696 |
|
|
|
697 |
$coursesections = $coursesectionscache->get($course->id);
|
|
|
698 |
if ($coursesections) {
|
|
|
699 |
return $coursesections;
|
|
|
700 |
}
|
|
|
701 |
|
|
|
702 |
$sectionpreferences = $this->get_sections_preferences_by_preference();
|
|
|
703 |
|
|
|
704 |
foreach ($sectionpreferences as $preference => $sectionids) {
|
|
|
705 |
if (!empty($sectionids) && is_array($sectionids)) {
|
|
|
706 |
foreach ($sectionids as $sectionid) {
|
|
|
707 |
if (!isset($result[$sectionid])) {
|
|
|
708 |
$result[$sectionid] = new stdClass();
|
|
|
709 |
}
|
|
|
710 |
$result[$sectionid]->$preference = true;
|
|
|
711 |
}
|
|
|
712 |
}
|
|
|
713 |
}
|
|
|
714 |
|
|
|
715 |
$coursesectionscache->set($course->id, $result);
|
|
|
716 |
return $result;
|
|
|
717 |
}
|
|
|
718 |
|
|
|
719 |
/**
|
|
|
720 |
* Return the format section preferences.
|
|
|
721 |
*
|
|
|
722 |
* @return array of preferences indexed by preference name
|
|
|
723 |
*/
|
|
|
724 |
public function get_sections_preferences_by_preference(): array {
|
|
|
725 |
global $USER;
|
|
|
726 |
$course = $this->get_course();
|
|
|
727 |
try {
|
|
|
728 |
$sectionpreferences = json_decode(
|
|
|
729 |
get_user_preferences("coursesectionspreferences_{$course->id}", '', $USER->id),
|
|
|
730 |
true,
|
|
|
731 |
);
|
|
|
732 |
if (empty($sectionpreferences)) {
|
|
|
733 |
$sectionpreferences = [];
|
|
|
734 |
}
|
|
|
735 |
} catch (\Throwable $e) {
|
|
|
736 |
$sectionpreferences = [];
|
|
|
737 |
}
|
|
|
738 |
return $sectionpreferences;
|
|
|
739 |
}
|
|
|
740 |
|
|
|
741 |
/**
|
|
|
742 |
* Return the format section preferences.
|
|
|
743 |
*
|
|
|
744 |
* @param string $preferencename preference name
|
|
|
745 |
* @param int[] $sectionids affected section ids
|
|
|
746 |
*
|
|
|
747 |
*/
|
|
|
748 |
public function set_sections_preference(string $preferencename, array $sectionids) {
|
|
|
749 |
$sectionpreferences = $this->get_sections_preferences_by_preference();
|
|
|
750 |
$sectionpreferences[$preferencename] = $sectionids;
|
|
|
751 |
$this->persist_to_user_preference($sectionpreferences);
|
|
|
752 |
}
|
|
|
753 |
|
|
|
754 |
/**
|
|
|
755 |
* Add section preference ids.
|
|
|
756 |
*
|
|
|
757 |
* @param string $preferencename preference name
|
|
|
758 |
* @param array $sectionids affected section ids
|
|
|
759 |
*/
|
|
|
760 |
public function add_section_preference_ids(
|
|
|
761 |
string $preferencename,
|
|
|
762 |
array $sectionids,
|
|
|
763 |
): void {
|
|
|
764 |
$sectionpreferences = $this->get_sections_preferences_by_preference();
|
|
|
765 |
if (!isset($sectionpreferences[$preferencename])) {
|
|
|
766 |
$sectionpreferences[$preferencename] = [];
|
|
|
767 |
}
|
|
|
768 |
foreach ($sectionids as $sectionid) {
|
|
|
769 |
if (!in_array($sectionid, $sectionpreferences[$preferencename])) {
|
|
|
770 |
$sectionpreferences[$preferencename][] = $sectionid;
|
|
|
771 |
}
|
|
|
772 |
}
|
|
|
773 |
$this->persist_to_user_preference($sectionpreferences);
|
|
|
774 |
}
|
|
|
775 |
|
|
|
776 |
/**
|
|
|
777 |
* Remove section preference ids.
|
|
|
778 |
*
|
|
|
779 |
* @param string $preferencename preference name
|
|
|
780 |
* @param array $sectionids affected section ids
|
|
|
781 |
*/
|
|
|
782 |
public function remove_section_preference_ids(
|
|
|
783 |
string $preferencename,
|
|
|
784 |
array $sectionids,
|
|
|
785 |
): void {
|
|
|
786 |
$sectionpreferences = $this->get_sections_preferences_by_preference();
|
|
|
787 |
if (!isset($sectionpreferences[$preferencename])) {
|
|
|
788 |
$sectionpreferences[$preferencename] = [];
|
|
|
789 |
}
|
|
|
790 |
foreach ($sectionids as $sectionid) {
|
|
|
791 |
if (($key = array_search($sectionid, $sectionpreferences[$preferencename])) !== false) {
|
|
|
792 |
unset($sectionpreferences[$preferencename][$key]);
|
|
|
793 |
}
|
|
|
794 |
}
|
|
|
795 |
$this->persist_to_user_preference($sectionpreferences);
|
|
|
796 |
}
|
|
|
797 |
|
|
|
798 |
/**
|
|
|
799 |
* Persist the section preferences to the user preferences.
|
|
|
800 |
*
|
|
|
801 |
* @param array $sectionpreferences the section preferences
|
|
|
802 |
*/
|
|
|
803 |
private function persist_to_user_preference(
|
|
|
804 |
array $sectionpreferences,
|
|
|
805 |
): void {
|
|
|
806 |
global $USER;
|
|
|
807 |
$course = $this->get_course();
|
|
|
808 |
set_user_preference('coursesectionspreferences_' . $course->id, json_encode($sectionpreferences), $USER->id);
|
|
|
809 |
// Invalidate section preferences cache.
|
|
|
810 |
$coursesectionscache = cache::make('core', 'coursesectionspreferences');
|
|
|
811 |
$coursesectionscache->delete($course->id);
|
|
|
812 |
}
|
|
|
813 |
|
|
|
814 |
/**
|
|
|
815 |
* Returns the information about the ajax support in the given source format
|
|
|
816 |
*
|
|
|
817 |
* The returned object's property (boolean)capable indicates that
|
|
|
818 |
* the course format supports Moodle course ajax features.
|
|
|
819 |
*
|
|
|
820 |
* @return stdClass
|
|
|
821 |
*/
|
|
|
822 |
public function supports_ajax() {
|
|
|
823 |
// No support by default.
|
|
|
824 |
$ajaxsupport = new stdClass();
|
|
|
825 |
$ajaxsupport->capable = false;
|
|
|
826 |
return $ajaxsupport;
|
|
|
827 |
}
|
|
|
828 |
|
|
|
829 |
/**
|
|
|
830 |
* Returns true if this course format is compatible with content components.
|
|
|
831 |
*
|
|
|
832 |
* Using components means the content elements can watch the frontend course state and
|
|
|
833 |
* react to the changes. Formats with component compatibility can have more interactions
|
|
|
834 |
* without refreshing the page, like having drag and drop from the course index to reorder
|
|
|
835 |
* sections and activities.
|
|
|
836 |
*
|
|
|
837 |
* @return bool if the format is compatible with components.
|
|
|
838 |
*/
|
|
|
839 |
public function supports_components() {
|
|
|
840 |
return false;
|
|
|
841 |
}
|
|
|
842 |
|
|
|
843 |
|
|
|
844 |
/**
|
|
|
845 |
* Custom action after section has been moved in AJAX mode
|
|
|
846 |
*
|
|
|
847 |
* Used in course/rest.php
|
|
|
848 |
*
|
|
|
849 |
* @return ?array This will be passed in ajax respose
|
|
|
850 |
*/
|
|
|
851 |
public function ajax_section_move() {
|
|
|
852 |
return null;
|
|
|
853 |
}
|
|
|
854 |
|
|
|
855 |
/**
|
|
|
856 |
* The URL to use for the specified course (with section)
|
|
|
857 |
*
|
|
|
858 |
* Please note that course view page /course/view.php?id=COURSEID is hardcoded in many
|
|
|
859 |
* places in core and contributed modules. If course format wants to change the location
|
|
|
860 |
* of the view script, it is not enough to change just this function. Do not forget
|
|
|
861 |
* to add proper redirection.
|
|
|
862 |
*
|
|
|
863 |
* @param int|stdClass $section Section object from database or just field course_sections.section
|
|
|
864 |
* if null the course view page is returned
|
|
|
865 |
* @param array $options options for view URL. At the moment core uses:
|
|
|
866 |
* 'navigation' (bool) if true and section not empty, the function returns section page; otherwise, it returns course page.
|
|
|
867 |
* 'sr' (int) used by course formats to specify to which section to return
|
|
|
868 |
* 'expanded' (bool) if true the section will be shown expanded, true by default
|
|
|
869 |
* @return null|moodle_url
|
|
|
870 |
*/
|
|
|
871 |
public function get_view_url($section, $options = array()) {
|
|
|
872 |
$course = $this->get_course();
|
|
|
873 |
$url = new moodle_url('/course/view.php', ['id' => $course->id]);
|
|
|
874 |
|
|
|
875 |
if (array_key_exists('sr', $options)) {
|
|
|
876 |
$sectionno = $options['sr'];
|
|
|
877 |
} else if (is_object($section)) {
|
|
|
878 |
$sectionno = $section->section;
|
|
|
879 |
} else {
|
|
|
880 |
$sectionno = $section;
|
|
|
881 |
}
|
|
|
882 |
if ((!empty($options['navigation']) || array_key_exists('sr', $options)) && $sectionno !== null) {
|
|
|
883 |
// Display section on separate page.
|
|
|
884 |
$sectioninfo = $this->get_section($sectionno);
|
|
|
885 |
return new moodle_url('/course/section.php', ['id' => $sectioninfo->id]);
|
|
|
886 |
}
|
|
|
887 |
if ($this->uses_sections() && $sectionno !== null) {
|
|
|
888 |
// The url includes the parameter to expand the section by default.
|
|
|
889 |
if (!array_key_exists('expanded', $options)) {
|
|
|
890 |
$options['expanded'] = true;
|
|
|
891 |
}
|
|
|
892 |
if ($options['expanded']) {
|
|
|
893 |
// This parameter is being set by default.
|
|
|
894 |
$url->param('expandsection', $sectionno);
|
|
|
895 |
}
|
|
|
896 |
$url->set_anchor('section-'.$sectionno);
|
|
|
897 |
}
|
|
|
898 |
|
|
|
899 |
return $url;
|
|
|
900 |
}
|
|
|
901 |
|
|
|
902 |
/**
|
|
|
903 |
* Return the old non-ajax activity action url.
|
|
|
904 |
*
|
|
|
905 |
* BrowserKit behats tests cannot trigger javascript events,
|
|
|
906 |
* so we must translate to an old non-ajax url while non-ajax
|
|
|
907 |
* course editing is still supported.
|
|
|
908 |
*
|
|
|
909 |
* @param string $action action name the reactive action
|
|
|
910 |
* @param cm_info $cm course module
|
|
|
911 |
* @return moodle_url
|
|
|
912 |
*/
|
|
|
913 |
public function get_non_ajax_cm_action_url(string $action, cm_info $cm): moodle_url {
|
|
|
914 |
$nonajaxactions = [
|
|
|
915 |
'cmDelete' => 'delete',
|
|
|
916 |
'cmDuplicate' => 'duplicate',
|
|
|
917 |
'cmHide' => 'hide',
|
|
|
918 |
'cmShow' => 'show',
|
|
|
919 |
'cmStealth' => 'stealth',
|
|
|
920 |
];
|
|
|
921 |
if (!isset($nonajaxactions[$action])) {
|
|
|
922 |
throw new coding_exception('Unknown activity action: ' . $action);
|
|
|
923 |
}
|
|
|
924 |
$nonajaxaction = $nonajaxactions[$action];
|
|
|
925 |
$nonajaxurl = new moodle_url(
|
|
|
926 |
'/course/mod.php',
|
|
|
927 |
['sesskey' => sesskey(), $nonajaxaction => $cm->id]
|
|
|
928 |
);
|
|
|
929 |
if (!is_null($this->get_sectionid())) {
|
|
|
930 |
$nonajaxurl->param('sr', $this->get_sectionnum());
|
|
|
931 |
}
|
|
|
932 |
return $nonajaxurl;
|
|
|
933 |
}
|
|
|
934 |
|
|
|
935 |
/**
|
|
|
936 |
* Loads all of the course sections into the navigation
|
|
|
937 |
*
|
|
|
938 |
* This method is called from global_navigation::load_course_sections()
|
|
|
939 |
*
|
|
|
940 |
* By default the method global_navigation::load_generic_course_sections() is called
|
|
|
941 |
*
|
|
|
942 |
* When overwriting please note that navigationlib relies on using the correct values for
|
|
|
943 |
* arguments $type and $key in navigation_node::add()
|
|
|
944 |
*
|
|
|
945 |
* Example of code creating a section node:
|
|
|
946 |
* $sectionnode = $node->add($sectionname, $url, navigation_node::TYPE_SECTION, null, $section->id);
|
|
|
947 |
* $sectionnode->nodetype = navigation_node::NODETYPE_BRANCH;
|
|
|
948 |
*
|
|
|
949 |
* Example of code creating an activity node:
|
|
|
950 |
* $activitynode = $sectionnode->add($activityname, $action, navigation_node::TYPE_ACTIVITY, null, $activity->id, $icon);
|
|
|
951 |
* if (global_navigation::module_extends_navigation($activity->modname)) {
|
|
|
952 |
* $activitynode->nodetype = navigation_node::NODETYPE_BRANCH;
|
|
|
953 |
* } else {
|
|
|
954 |
* $activitynode->nodetype = navigation_node::NODETYPE_LEAF;
|
|
|
955 |
* }
|
|
|
956 |
*
|
|
|
957 |
* Also note that if $navigation->includesectionnum is not null, the section with this relative
|
|
|
958 |
* number needs is expected to be loaded
|
|
|
959 |
*
|
|
|
960 |
* @param \global_navigation $navigation
|
|
|
961 |
* @param navigation_node $node The course node within the navigation
|
|
|
962 |
*/
|
|
|
963 |
public function extend_course_navigation($navigation, navigation_node $node) {
|
|
|
964 |
if ($course = $this->get_course()) {
|
|
|
965 |
$navigation->load_generic_course_sections($course, $node);
|
|
|
966 |
}
|
|
|
967 |
return array();
|
|
|
968 |
}
|
|
|
969 |
|
|
|
970 |
/**
|
|
|
971 |
* Returns the list of blocks to be automatically added for the newly created course
|
|
|
972 |
*
|
|
|
973 |
* @see blocks_add_default_course_blocks()
|
|
|
974 |
*
|
|
|
975 |
* @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
|
|
|
976 |
* each of values is an array of block names (for left and right side columns)
|
|
|
977 |
*/
|
|
|
978 |
public function get_default_blocks() {
|
|
|
979 |
global $CFG;
|
|
|
980 |
if (isset($CFG->defaultblocks)) {
|
|
|
981 |
return blocks_parse_default_blocks_list($CFG->defaultblocks);
|
|
|
982 |
}
|
|
|
983 |
$blocknames = array(
|
|
|
984 |
BLOCK_POS_LEFT => array(),
|
|
|
985 |
BLOCK_POS_RIGHT => array()
|
|
|
986 |
);
|
|
|
987 |
return $blocknames;
|
|
|
988 |
}
|
|
|
989 |
|
|
|
990 |
/**
|
|
|
991 |
* Return custom strings for the course editor.
|
|
|
992 |
*
|
|
|
993 |
* This method is mainly used to translate the "section" related strings into
|
|
|
994 |
* the specific format plugins name such as "Topics" or "Weeks".
|
|
|
995 |
*
|
|
|
996 |
* @return stdClass[] an array of objects with string "component" and "key"
|
|
|
997 |
*/
|
|
|
998 |
public function get_editor_custom_strings(): array {
|
|
|
999 |
$result = [];
|
|
|
1000 |
$stringmanager = get_string_manager();
|
|
|
1001 |
$component = 'format_' . $this->format;
|
|
|
1002 |
$formatoverridbles = [
|
|
|
1003 |
'sectionavailability_title',
|
|
|
1004 |
'sectiondelete_title',
|
|
|
1005 |
'sectiondelete_info',
|
|
|
1006 |
'sectionsdelete_title',
|
|
|
1007 |
'sectionsdelete_info',
|
|
|
1008 |
'sectionmove_title',
|
|
|
1009 |
'sectionmove_info',
|
|
|
1010 |
'sectionsavailability_title',
|
|
|
1011 |
'sectionsmove_title',
|
|
|
1012 |
'sectionsmove_info',
|
|
|
1013 |
'selectsection'
|
|
|
1014 |
];
|
|
|
1015 |
foreach ($formatoverridbles as $key) {
|
|
|
1016 |
if ($stringmanager->string_exists($key, $component)) {
|
|
|
1017 |
$result[] = (object)['component' => $component, 'key' => $key];
|
|
|
1018 |
}
|
|
|
1019 |
}
|
|
|
1020 |
return $result;
|
|
|
1021 |
}
|
|
|
1022 |
|
|
|
1023 |
/**
|
|
|
1024 |
* Get the proper format plugin string if it exists.
|
|
|
1025 |
*
|
|
|
1026 |
* If the format_PLUGINNAME does not provide a valid string,
|
|
|
1027 |
* core_courseformat will be user as the component.
|
|
|
1028 |
*
|
|
|
1029 |
* @param string $key the string key
|
|
|
1030 |
* @param string|object|array|int $data extra data that can be used within translation strings
|
|
|
1031 |
* @return string the get_string result
|
|
|
1032 |
*/
|
|
|
1033 |
public function get_format_string(string $key, $data = null): string {
|
|
|
1034 |
$component = 'format_' . $this->get_format();
|
|
|
1035 |
if (!get_string_manager()->string_exists($key, $component)) {
|
|
|
1036 |
$component = 'core_courseformat';
|
|
|
1037 |
}
|
|
|
1038 |
return get_string($key, $component, $data);
|
|
|
1039 |
}
|
|
|
1040 |
|
|
|
1041 |
/**
|
|
|
1042 |
* Returns the localised name of this course format plugin
|
|
|
1043 |
*
|
|
|
1044 |
* @return lang_string
|
|
|
1045 |
*/
|
|
|
1046 |
final public function get_format_name() {
|
|
|
1047 |
return new lang_string('pluginname', 'format_'.$this->get_format());
|
|
|
1048 |
}
|
|
|
1049 |
|
|
|
1050 |
/**
|
|
|
1051 |
* Definitions of the additional options that this course format uses for course
|
|
|
1052 |
*
|
|
|
1053 |
* This function may be called often, it should be as fast as possible.
|
|
|
1054 |
* Avoid using get_string() method, use "new lang_string()" instead
|
|
|
1055 |
* It is not recommended to use dynamic or course-dependant expressions here
|
|
|
1056 |
* This function may be also called when course does not exist yet.
|
|
|
1057 |
*
|
|
|
1058 |
* Option names must be different from fields in the {course} talbe or any form elements on
|
|
|
1059 |
* course edit form, it may even make sence to use special prefix for them.
|
|
|
1060 |
*
|
|
|
1061 |
* Each option must have the option name as a key and the array of properties as a value:
|
|
|
1062 |
* 'default' - default value for this option (assumed null if not specified)
|
|
|
1063 |
* 'type' - type of the option value (PARAM_INT, PARAM_RAW, etc.)
|
|
|
1064 |
*
|
|
|
1065 |
* Additional properties used by default implementation of
|
|
|
1066 |
* course_format::create_edit_form_elements() (calls this method with $foreditform = true)
|
|
|
1067 |
* 'label' - localised human-readable label for the edit form
|
|
|
1068 |
* 'element_type' - type of the form element, default 'text'
|
|
|
1069 |
* 'element_attributes' - additional attributes for the form element, these are 4th and further
|
|
|
1070 |
* arguments in the moodleform::addElement() method
|
|
|
1071 |
* 'help' - string for help button. Note that if 'help' value is 'myoption' then the string with
|
|
|
1072 |
* the name 'myoption_help' must exist in the language file
|
|
|
1073 |
* 'help_component' - language component to look for help string, by default this the component
|
|
|
1074 |
* for this course format
|
|
|
1075 |
*
|
|
|
1076 |
* This is an interface for creating simple form elements. If format plugin wants to use other
|
|
|
1077 |
* methods such as disableIf, it can be done by overriding create_edit_form_elements().
|
|
|
1078 |
*
|
|
|
1079 |
* Course format options can be accessed as:
|
|
|
1080 |
* $this->get_course()->OPTIONNAME (inside the format class)
|
|
|
1081 |
* course_get_format($course)->get_course()->OPTIONNAME (outside of format class)
|
|
|
1082 |
*
|
|
|
1083 |
* All course options are returned by calling:
|
|
|
1084 |
* $this->get_format_options();
|
|
|
1085 |
*
|
|
|
1086 |
* @param bool $foreditform
|
|
|
1087 |
* @return array of options
|
|
|
1088 |
*/
|
|
|
1089 |
public function course_format_options($foreditform = false) {
|
|
|
1090 |
return array();
|
|
|
1091 |
}
|
|
|
1092 |
|
|
|
1093 |
/**
|
|
|
1094 |
* Definitions of the additional options that this course format uses for section
|
|
|
1095 |
*
|
|
|
1096 |
* See course_format::course_format_options() for return array definition.
|
|
|
1097 |
*
|
|
|
1098 |
* Additionally section format options may have property 'cache' set to true
|
|
|
1099 |
* if this option needs to be cached in get_fast_modinfo(). The 'cache' property
|
|
|
1100 |
* is recommended to be set only for fields used in course_format::get_section_name(),
|
|
|
1101 |
* course_format::extend_course_navigation() and course_format::get_view_url()
|
|
|
1102 |
*
|
|
|
1103 |
* For better performance cached options are recommended to have 'cachedefault' property
|
|
|
1104 |
* Unlike 'default', 'cachedefault' should be static and not access get_config().
|
|
|
1105 |
*
|
|
|
1106 |
* Regardless of value of 'cache' all options are accessed in the code as
|
|
|
1107 |
* $sectioninfo->OPTIONNAME
|
|
|
1108 |
* where $sectioninfo is instance of section_info, returned by
|
|
|
1109 |
* get_fast_modinfo($course)->get_section_info($sectionnum)
|
|
|
1110 |
* or get_fast_modinfo($course)->get_section_info_all()
|
|
|
1111 |
*
|
|
|
1112 |
* All format options for particular section are returned by calling:
|
|
|
1113 |
* $this->get_format_options($section);
|
|
|
1114 |
*
|
|
|
1115 |
* @param bool $foreditform
|
|
|
1116 |
* @return array
|
|
|
1117 |
*/
|
|
|
1118 |
public function section_format_options($foreditform = false) {
|
|
|
1119 |
return array();
|
|
|
1120 |
}
|
|
|
1121 |
|
|
|
1122 |
/**
|
|
|
1123 |
* Returns the format options stored for this course or course section
|
|
|
1124 |
*
|
|
|
1125 |
* When overriding please note that this function is called from rebuild_course_cache()
|
|
|
1126 |
* and section_info object, therefore using of get_fast_modinfo() and/or any function that
|
|
|
1127 |
* accesses it may lead to recursion.
|
|
|
1128 |
*
|
|
|
1129 |
* @param null|int|stdClass|section_info $section if null the course format options will be returned
|
|
|
1130 |
* otherwise options for specified section will be returned. This can be either
|
|
|
1131 |
* section object or relative section number (field course_sections.section)
|
|
|
1132 |
* @return array
|
|
|
1133 |
*/
|
|
|
1134 |
public function get_format_options($section = null) {
|
|
|
1135 |
global $DB;
|
|
|
1136 |
if ($section === null) {
|
|
|
1137 |
$options = $this->course_format_options();
|
|
|
1138 |
} else {
|
|
|
1139 |
$options = $this->section_format_options();
|
|
|
1140 |
}
|
|
|
1141 |
if (empty($options)) {
|
|
|
1142 |
// There are no option for course/sections anyway, no need to go further.
|
|
|
1143 |
return array();
|
|
|
1144 |
}
|
|
|
1145 |
if ($section === null) {
|
|
|
1146 |
// Course format options will be returned.
|
|
|
1147 |
$sectionid = 0;
|
|
|
1148 |
} else if ($this->courseid && isset($section->id)) {
|
|
|
1149 |
// Course section format options will be returned.
|
|
|
1150 |
$sectionid = $section->id;
|
|
|
1151 |
} else if ($this->courseid && is_int($section) &&
|
|
|
1152 |
($sectionobj = $DB->get_record('course_sections',
|
|
|
1153 |
array('section' => $section, 'course' => $this->courseid), 'id'))) {
|
|
|
1154 |
// Course section format options will be returned.
|
|
|
1155 |
$sectionid = $sectionobj->id;
|
|
|
1156 |
} else {
|
|
|
1157 |
// Non-existing (yet) section was passed as an argument
|
|
|
1158 |
// default format options for course section will be returned.
|
|
|
1159 |
$sectionid = -1;
|
|
|
1160 |
}
|
|
|
1161 |
if (!array_key_exists($sectionid, $this->formatoptions)) {
|
|
|
1162 |
$this->formatoptions[$sectionid] = array();
|
|
|
1163 |
// First fill with default values.
|
|
|
1164 |
foreach ($options as $optionname => $optionparams) {
|
|
|
1165 |
$this->formatoptions[$sectionid][$optionname] = null;
|
|
|
1166 |
if (array_key_exists('default', $optionparams)) {
|
|
|
1167 |
$this->formatoptions[$sectionid][$optionname] = $optionparams['default'];
|
|
|
1168 |
}
|
|
|
1169 |
}
|
|
|
1170 |
if ($this->courseid && $sectionid !== -1) {
|
|
|
1171 |
// Overwrite the default options values with those stored in course_format_options table
|
|
|
1172 |
// nothing can be stored if we are interested in generic course ($this->courseid == 0)
|
|
|
1173 |
// or generic section ($sectionid === 0).
|
|
|
1174 |
$records = $DB->get_records('course_format_options',
|
|
|
1175 |
array('courseid' => $this->courseid,
|
|
|
1176 |
'format' => $this->format,
|
|
|
1177 |
'sectionid' => $sectionid
|
|
|
1178 |
), '', 'id,name,value');
|
|
|
1179 |
$indexedrecords = [];
|
|
|
1180 |
foreach ($records as $record) {
|
|
|
1181 |
$indexedrecords[$record->name] = $record->value;
|
|
|
1182 |
}
|
|
|
1183 |
foreach ($options as $optionname => $option) {
|
|
|
1184 |
contract_value($this->formatoptions[$sectionid], $indexedrecords, $option, $optionname);
|
|
|
1185 |
}
|
|
|
1186 |
}
|
|
|
1187 |
}
|
|
|
1188 |
return $this->formatoptions[$sectionid];
|
|
|
1189 |
}
|
|
|
1190 |
|
|
|
1191 |
/**
|
|
|
1192 |
* Adds format options elements to the course/section edit form
|
|
|
1193 |
*
|
|
|
1194 |
* This function is called from course_edit_form::definition_after_data()
|
|
|
1195 |
*
|
|
|
1196 |
* @param \MoodleQuickForm $mform form the elements are added to
|
|
|
1197 |
* @param bool $forsection 'true' if this is a section edit form, 'false' if this is course edit form
|
|
|
1198 |
* @return array array of references to the added form elements
|
|
|
1199 |
*/
|
|
|
1200 |
public function create_edit_form_elements(&$mform, $forsection = false) {
|
|
|
1201 |
$elements = array();
|
|
|
1202 |
if ($forsection) {
|
|
|
1203 |
$options = $this->section_format_options(true);
|
|
|
1204 |
} else {
|
|
|
1205 |
$options = $this->course_format_options(true);
|
|
|
1206 |
}
|
|
|
1207 |
foreach ($options as $optionname => $option) {
|
|
|
1208 |
if (!isset($option['element_type'])) {
|
|
|
1209 |
$option['element_type'] = 'text';
|
|
|
1210 |
}
|
|
|
1211 |
$args = array($option['element_type'], $optionname, $option['label']);
|
|
|
1212 |
if (!empty($option['element_attributes'])) {
|
|
|
1213 |
$args = array_merge($args, $option['element_attributes']);
|
|
|
1214 |
}
|
|
|
1215 |
$elements[] = call_user_func_array(array($mform, 'addElement'), $args);
|
|
|
1216 |
if (isset($option['help'])) {
|
|
|
1217 |
$helpcomponent = 'format_'. $this->get_format();
|
|
|
1218 |
if (isset($option['help_component'])) {
|
|
|
1219 |
$helpcomponent = $option['help_component'];
|
|
|
1220 |
}
|
|
|
1221 |
$mform->addHelpButton($optionname, $option['help'], $helpcomponent);
|
|
|
1222 |
}
|
|
|
1223 |
if (isset($option['type'])) {
|
|
|
1224 |
$mform->setType($optionname, $option['type']);
|
|
|
1225 |
}
|
|
|
1226 |
if (isset($option['default']) && !array_key_exists($optionname, $mform->_defaultValues)) {
|
|
|
1227 |
// Set defaults for the elements in the form.
|
|
|
1228 |
// Since we call this method after set_data() make sure that we don't override what was already set.
|
|
|
1229 |
$mform->setDefault($optionname, $option['default']);
|
|
|
1230 |
}
|
|
|
1231 |
}
|
|
|
1232 |
|
|
|
1233 |
if (!$forsection && empty($this->courseid)) {
|
|
|
1234 |
// Check if course end date form field should be enabled by default.
|
|
|
1235 |
// If a default date is provided to the form element, it is magically enabled by default in the
|
|
|
1236 |
// MoodleQuickForm_date_time_selector class, otherwise it's disabled by default.
|
|
|
1237 |
if (get_config('moodlecourse', 'courseenddateenabled')) {
|
|
|
1238 |
// At this stage (this is called from definition_after_data) course data is already set as default.
|
|
|
1239 |
// We can not overwrite what is in the database.
|
|
|
1240 |
$mform->setDefault('enddate', $this->get_default_course_enddate($mform));
|
|
|
1241 |
}
|
|
|
1242 |
}
|
|
|
1243 |
|
|
|
1244 |
return $elements;
|
|
|
1245 |
}
|
|
|
1246 |
|
|
|
1247 |
/**
|
|
|
1248 |
* Override if you need to perform some extra validation of the format options
|
|
|
1249 |
*
|
|
|
1250 |
* @param array $data array of ("fieldname"=>value) of submitted data
|
|
|
1251 |
* @param array $files array of uploaded files "element_name"=>tmp_file_path
|
|
|
1252 |
* @param array $errors errors already discovered in edit form validation
|
|
|
1253 |
* @return array of "element_name"=>"error_description" if there are errors,
|
|
|
1254 |
* or an empty array if everything is OK.
|
|
|
1255 |
* Do not repeat errors from $errors param here
|
|
|
1256 |
*/
|
|
|
1257 |
public function edit_form_validation($data, $files, $errors) {
|
|
|
1258 |
return array();
|
|
|
1259 |
}
|
|
|
1260 |
|
|
|
1261 |
/**
|
|
|
1262 |
* Prepares values of course or section format options before storing them in DB
|
|
|
1263 |
*
|
|
|
1264 |
* If an option has invalid value it is not returned
|
|
|
1265 |
*
|
|
|
1266 |
* @param array $rawdata associative array of the proposed course/section format options
|
|
|
1267 |
* @param int|null $sectionid null if it is course format option
|
|
|
1268 |
* @return array array of options that have valid values
|
|
|
1269 |
*/
|
|
|
1270 |
protected function validate_format_options(array $rawdata, int $sectionid = null): array {
|
|
|
1271 |
if (!$sectionid) {
|
|
|
1272 |
$allformatoptions = $this->course_format_options(true);
|
|
|
1273 |
} else {
|
|
|
1274 |
$allformatoptions = $this->section_format_options(true);
|
|
|
1275 |
}
|
|
|
1276 |
$data = array_intersect_key($rawdata, $allformatoptions);
|
|
|
1277 |
foreach ($data as $key => $value) {
|
|
|
1278 |
$option = $allformatoptions[$key] + ['type' => PARAM_RAW, 'element_type' => null, 'element_attributes' => [[]]];
|
|
|
1279 |
expand_value($data, $data, $option, $key);
|
|
|
1280 |
if ($option['element_type'] === 'select' && !array_key_exists($data[$key], $option['element_attributes'][0])) {
|
|
|
1281 |
// Value invalid for select element, skip.
|
|
|
1282 |
unset($data[$key]);
|
|
|
1283 |
}
|
|
|
1284 |
}
|
|
|
1285 |
return $data;
|
|
|
1286 |
}
|
|
|
1287 |
|
|
|
1288 |
/**
|
|
|
1289 |
* Validates format options for the course
|
|
|
1290 |
*
|
|
|
1291 |
* @param array $data data to insert/update
|
|
|
1292 |
* @return array array of options that have valid values
|
|
|
1293 |
*/
|
|
|
1294 |
public function validate_course_format_options(array $data): array {
|
|
|
1295 |
return $this->validate_format_options($data);
|
|
|
1296 |
}
|
|
|
1297 |
|
|
|
1298 |
/**
|
|
|
1299 |
* Updates format options for a course or section
|
|
|
1300 |
*
|
|
|
1301 |
* If $data does not contain property with the option name, the option will not be updated
|
|
|
1302 |
*
|
|
|
1303 |
* @param stdClass|array $data return value from moodleform::get_data() or array with data
|
|
|
1304 |
* @param null|int $sectionid null if these are options for course or section id (course_sections.id)
|
|
|
1305 |
* if these are options for section
|
|
|
1306 |
* @return bool whether there were any changes to the options values
|
|
|
1307 |
*/
|
|
|
1308 |
protected function update_format_options($data, $sectionid = null) {
|
|
|
1309 |
global $DB;
|
|
|
1310 |
$data = $this->validate_format_options((array)$data, $sectionid);
|
|
|
1311 |
if (!$sectionid) {
|
|
|
1312 |
$allformatoptions = $this->course_format_options();
|
|
|
1313 |
$sectionid = 0;
|
|
|
1314 |
} else {
|
|
|
1315 |
$allformatoptions = $this->section_format_options();
|
|
|
1316 |
}
|
|
|
1317 |
if (empty($allformatoptions)) {
|
|
|
1318 |
// Nothing to update anyway.
|
|
|
1319 |
return false;
|
|
|
1320 |
}
|
|
|
1321 |
$defaultoptions = array();
|
|
|
1322 |
$cached = array();
|
|
|
1323 |
foreach ($allformatoptions as $key => $option) {
|
|
|
1324 |
$defaultoptions[$key] = null;
|
|
|
1325 |
if (array_key_exists('default', $option)) {
|
|
|
1326 |
$defaultoptions[$key] = $option['default'];
|
|
|
1327 |
}
|
|
|
1328 |
expand_value($defaultoptions, $defaultoptions, $option, $key);
|
|
|
1329 |
$cached[$key] = ($sectionid === 0 || !empty($option['cache']));
|
|
|
1330 |
}
|
|
|
1331 |
$records = $DB->get_records('course_format_options',
|
|
|
1332 |
array('courseid' => $this->courseid,
|
|
|
1333 |
'format' => $this->format,
|
|
|
1334 |
'sectionid' => $sectionid
|
|
|
1335 |
), '', 'name,id,value');
|
|
|
1336 |
$changed = $needrebuild = false;
|
|
|
1337 |
foreach ($defaultoptions as $key => $value) {
|
|
|
1338 |
if (isset($records[$key])) {
|
|
|
1339 |
if (array_key_exists($key, $data) && $records[$key]->value != $data[$key]) {
|
|
|
1340 |
$DB->set_field('course_format_options', 'value',
|
|
|
1341 |
$data[$key], array('id' => $records[$key]->id));
|
|
|
1342 |
$changed = true;
|
|
|
1343 |
$needrebuild = $needrebuild || $cached[$key];
|
|
|
1344 |
}
|
|
|
1345 |
} else {
|
|
|
1346 |
if (array_key_exists($key, $data) && $data[$key] !== $value) {
|
|
|
1347 |
$newvalue = $data[$key];
|
|
|
1348 |
$changed = true;
|
|
|
1349 |
$needrebuild = $needrebuild || $cached[$key];
|
|
|
1350 |
} else {
|
|
|
1351 |
$newvalue = $value;
|
|
|
1352 |
// We still insert entry in DB but there are no changes from user point of
|
|
|
1353 |
// view and no need to call rebuild_course_cache().
|
|
|
1354 |
}
|
|
|
1355 |
$DB->insert_record('course_format_options', array(
|
|
|
1356 |
'courseid' => $this->courseid,
|
|
|
1357 |
'format' => $this->format,
|
|
|
1358 |
'sectionid' => $sectionid,
|
|
|
1359 |
'name' => $key,
|
|
|
1360 |
'value' => $newvalue
|
|
|
1361 |
));
|
|
|
1362 |
}
|
|
|
1363 |
}
|
|
|
1364 |
if ($needrebuild) {
|
|
|
1365 |
if ($sectionid) {
|
|
|
1366 |
// Invalidate the section cache by given section id.
|
|
|
1367 |
course_modinfo::purge_course_section_cache_by_id($this->courseid, $sectionid);
|
|
|
1368 |
// Partial rebuild sections that have been invalidated.
|
|
|
1369 |
rebuild_course_cache($this->courseid, true, true);
|
|
|
1370 |
} else {
|
|
|
1371 |
// Full rebuild if sectionid is null.
|
|
|
1372 |
rebuild_course_cache($this->courseid);
|
|
|
1373 |
}
|
|
|
1374 |
}
|
|
|
1375 |
if ($changed) {
|
|
|
1376 |
// Reset internal caches.
|
|
|
1377 |
if (!$sectionid) {
|
|
|
1378 |
$this->course = false;
|
|
|
1379 |
}
|
|
|
1380 |
unset($this->formatoptions[$sectionid]);
|
|
|
1381 |
}
|
|
|
1382 |
return $changed;
|
|
|
1383 |
}
|
|
|
1384 |
|
|
|
1385 |
/**
|
|
|
1386 |
* Updates format options for a course
|
|
|
1387 |
*
|
|
|
1388 |
* If $data does not contain property with the option name, the option will not be updated
|
|
|
1389 |
*
|
|
|
1390 |
* @param stdClass|array $data return value from moodleform::get_data() or array with data
|
|
|
1391 |
* @param stdClass $oldcourse if this function is called from update_course()
|
|
|
1392 |
* this object contains information about the course before update
|
|
|
1393 |
* @return bool whether there were any changes to the options values
|
|
|
1394 |
*/
|
|
|
1395 |
public function update_course_format_options($data, $oldcourse = null) {
|
|
|
1396 |
return $this->update_format_options($data);
|
|
|
1397 |
}
|
|
|
1398 |
|
|
|
1399 |
/**
|
|
|
1400 |
* Updates format options for a section
|
|
|
1401 |
*
|
|
|
1402 |
* Section id is expected in $data->id (or $data['id'])
|
|
|
1403 |
* If $data does not contain property with the option name, the option will not be updated
|
|
|
1404 |
*
|
|
|
1405 |
* @param stdClass|array $data return value from moodleform::get_data() or array with data
|
|
|
1406 |
* @return bool whether there were any changes to the options values
|
|
|
1407 |
*/
|
|
|
1408 |
public function update_section_format_options($data) {
|
|
|
1409 |
$data = (array)$data;
|
|
|
1410 |
return $this->update_format_options($data, $data['id']);
|
|
|
1411 |
}
|
|
|
1412 |
|
|
|
1413 |
/**
|
|
|
1414 |
* Return an instance of moodleform to edit a specified section
|
|
|
1415 |
*
|
|
|
1416 |
* Default implementation returns instance of editsection_form that automatically adds
|
|
|
1417 |
* additional fields defined in course_format::section_format_options()
|
|
|
1418 |
*
|
|
|
1419 |
* Format plugins may extend editsection_form if they want to have custom edit section form.
|
|
|
1420 |
*
|
|
|
1421 |
* @param mixed $action the action attribute for the form. If empty defaults to auto detect the
|
|
|
1422 |
* current url. If a moodle_url object then outputs params as hidden variables.
|
|
|
1423 |
* @param array $customdata the array with custom data to be passed to the form
|
|
|
1424 |
* /course/editsection.php passes section_info object in 'cs' field
|
|
|
1425 |
* for filling availability fields
|
|
|
1426 |
* @return \moodleform
|
|
|
1427 |
*/
|
|
|
1428 |
public function editsection_form($action, $customdata = array()) {
|
|
|
1429 |
global $CFG;
|
|
|
1430 |
require_once($CFG->dirroot. '/course/editsection_form.php');
|
|
|
1431 |
if (!array_key_exists('course', $customdata)) {
|
|
|
1432 |
$customdata['course'] = $this->get_course();
|
|
|
1433 |
}
|
|
|
1434 |
return new editsection_form($action, $customdata);
|
|
|
1435 |
}
|
|
|
1436 |
|
|
|
1437 |
/**
|
|
|
1438 |
* Allows course format to execute code on moodle_page::set_course()
|
|
|
1439 |
*
|
|
|
1440 |
* @param moodle_page $page instance of page calling set_course
|
|
|
1441 |
*/
|
|
|
1442 |
public function page_set_course(moodle_page $page) {
|
|
|
1443 |
}
|
|
|
1444 |
|
|
|
1445 |
/**
|
|
|
1446 |
* Allows course format to execute code on moodle_page::set_cm()
|
|
|
1447 |
*
|
|
|
1448 |
* Current module can be accessed as $page->cm (returns instance of cm_info)
|
|
|
1449 |
*
|
|
|
1450 |
* @param moodle_page $page instance of page calling set_cm
|
|
|
1451 |
*/
|
|
|
1452 |
public function page_set_cm(moodle_page $page) {
|
|
|
1453 |
}
|
|
|
1454 |
|
|
|
1455 |
/**
|
|
|
1456 |
* Course-specific information to be output on any course page (usually above navigation bar)
|
|
|
1457 |
*
|
|
|
1458 |
* Example of usage:
|
|
|
1459 |
* define
|
|
|
1460 |
* class format_FORMATNAME_XXX implements renderable {}
|
|
|
1461 |
*
|
|
|
1462 |
* create format renderer in course/format/FORMATNAME/renderer.php, define rendering function:
|
|
|
1463 |
* class format_FORMATNAME_renderer extends plugin_renderer_base {
|
|
|
1464 |
* protected function render_format_FORMATNAME_XXX(format_FORMATNAME_XXX $xxx) {
|
|
|
1465 |
* return html_writer::tag('div', 'This is my header/footer');
|
|
|
1466 |
* }
|
|
|
1467 |
* }
|
|
|
1468 |
*
|
|
|
1469 |
* Return instance of format_FORMATNAME_XXX in this function, the appropriate method from
|
|
|
1470 |
* plugin renderer will be called
|
|
|
1471 |
*
|
|
|
1472 |
* @return null|\renderable null for no output or object with data for plugin renderer
|
|
|
1473 |
*/
|
|
|
1474 |
public function course_header() {
|
|
|
1475 |
return null;
|
|
|
1476 |
}
|
|
|
1477 |
|
|
|
1478 |
/**
|
|
|
1479 |
* Course-specific information to be output on any course page (usually in the beginning of
|
|
|
1480 |
* standard footer)
|
|
|
1481 |
*
|
|
|
1482 |
* See course_format::course_header() for usage
|
|
|
1483 |
*
|
|
|
1484 |
* @return null|\renderable null for no output or object with data for plugin renderer
|
|
|
1485 |
*/
|
|
|
1486 |
public function course_footer() {
|
|
|
1487 |
return null;
|
|
|
1488 |
}
|
|
|
1489 |
|
|
|
1490 |
/**
|
|
|
1491 |
* Course-specific information to be output immediately above content on any course page
|
|
|
1492 |
*
|
|
|
1493 |
* See course_format::course_header() for usage
|
|
|
1494 |
*
|
|
|
1495 |
* @return null|\renderable null for no output or object with data for plugin renderer
|
|
|
1496 |
*/
|
|
|
1497 |
public function course_content_header() {
|
|
|
1498 |
return null;
|
|
|
1499 |
}
|
|
|
1500 |
|
|
|
1501 |
/**
|
|
|
1502 |
* Course-specific information to be output immediately below content on any course page
|
|
|
1503 |
*
|
|
|
1504 |
* See course_format::course_header() for usage
|
|
|
1505 |
*
|
|
|
1506 |
* @return null|\renderable null for no output or object with data for plugin renderer
|
|
|
1507 |
*/
|
|
|
1508 |
public function course_content_footer() {
|
|
|
1509 |
return null;
|
|
|
1510 |
}
|
|
|
1511 |
|
|
|
1512 |
/**
|
|
|
1513 |
* Returns instance of page renderer used by this plugin
|
|
|
1514 |
*
|
|
|
1515 |
* @param moodle_page $page
|
|
|
1516 |
* @return \renderer_base
|
|
|
1517 |
*/
|
|
|
1518 |
public function get_renderer(moodle_page $page) {
|
|
|
1519 |
try {
|
|
|
1520 |
$renderer = $page->get_renderer('format_'. $this->get_format());
|
|
|
1521 |
} catch (moodle_exception $e) {
|
|
|
1522 |
$formatname = $this->get_format();
|
|
|
1523 |
$expectedrenderername = 'format_'. $this->get_format() . '\output\renderer';
|
|
|
1524 |
debugging(
|
|
|
1525 |
"The '{$formatname}' course format does not define the {$expectedrenderername} renderer class. This is required since Moodle 4.0.",
|
|
|
1526 |
DEBUG_DEVELOPER
|
|
|
1527 |
);
|
|
|
1528 |
$renderer = new legacy_renderer($page, null);
|
|
|
1529 |
}
|
|
|
1530 |
|
|
|
1531 |
return $renderer;
|
|
|
1532 |
}
|
|
|
1533 |
|
|
|
1534 |
/**
|
|
|
1535 |
* Returns instance of output component used by this plugin
|
|
|
1536 |
*
|
|
|
1537 |
* @throws coding_exception if the format class does not extends the original core one.
|
|
|
1538 |
* @param string $outputname the element to render (section, activity...)
|
|
|
1539 |
* @return string the output component classname
|
|
|
1540 |
*/
|
|
|
1541 |
public function get_output_classname(string $outputname): string {
|
|
|
1542 |
// The core output class.
|
|
|
1543 |
$baseclass = "core_courseformat\\output\\local\\$outputname";
|
|
|
1544 |
|
|
|
1545 |
// Look in this format and any parent formats before we get to the base one.
|
|
|
1546 |
$classes = array_merge([get_class($this)], class_parents($this));
|
|
|
1547 |
foreach ($classes as $component) {
|
|
|
1548 |
if ($component === self::class) {
|
|
|
1549 |
break;
|
|
|
1550 |
}
|
|
|
1551 |
|
|
|
1552 |
// Because course formats are in the root namespace, there is no need to process the
|
|
|
1553 |
// class name - it is already a Frankenstyle component name beginning 'format_'.
|
|
|
1554 |
|
|
|
1555 |
// Check if there is a specific class in this format.
|
|
|
1556 |
$outputclass = "$component\\output\\courseformat\\$outputname";
|
|
|
1557 |
if (class_exists($outputclass)) {
|
|
|
1558 |
// Check that the outputclass is a subclass of the base class.
|
|
|
1559 |
if (!is_subclass_of($outputclass, $baseclass)) {
|
|
|
1560 |
throw new coding_exception("The \"$outputclass\" must extend \"$baseclass\"");
|
|
|
1561 |
}
|
|
|
1562 |
return $outputclass;
|
|
|
1563 |
}
|
|
|
1564 |
}
|
|
|
1565 |
|
|
|
1566 |
return $baseclass;
|
|
|
1567 |
}
|
|
|
1568 |
|
|
|
1569 |
/**
|
|
|
1570 |
* Returns true if the specified section is current
|
|
|
1571 |
*
|
|
|
1572 |
* By default we analyze $course->marker
|
|
|
1573 |
*
|
|
|
1574 |
* @param int|stdClass|section_info $section
|
|
|
1575 |
* @return bool
|
|
|
1576 |
*/
|
|
|
1577 |
public function is_section_current($section) {
|
|
|
1578 |
if (is_object($section)) {
|
|
|
1579 |
$sectionnum = $section->section;
|
|
|
1580 |
} else {
|
|
|
1581 |
$sectionnum = $section;
|
|
|
1582 |
}
|
|
|
1583 |
return ($sectionnum && ($course = $this->get_course()) && $course->marker == $sectionnum);
|
|
|
1584 |
}
|
|
|
1585 |
|
|
|
1586 |
/**
|
|
|
1587 |
* Returns if an specific section is visible to the current user.
|
|
|
1588 |
*
|
|
|
1589 |
* Formats can overrride this method to implement any special section logic.
|
|
|
1590 |
*
|
|
|
1591 |
* @param section_info $section the section modinfo
|
|
|
1592 |
* @return bool;
|
|
|
1593 |
*/
|
|
|
1594 |
public function is_section_visible(section_info $section): bool {
|
|
|
1595 |
// Previous to Moodle 4.0 thas logic was hardcoded. To prevent errors in the contrib plugins
|
|
|
1596 |
// the default logic is the same required for topics and weeks format and still uses
|
|
|
1597 |
// a "hiddensections" format setting.
|
|
|
1598 |
$course = $this->get_course();
|
|
|
1599 |
$hidesections = $course->hiddensections ?? true;
|
|
|
1600 |
// Show the section if the user is permitted to access it, OR if it's not available
|
|
|
1601 |
// but there is some available info text which explains the reason & should display,
|
|
|
1602 |
// OR it is hidden but the course has a setting to display hidden sections as unavailable.
|
|
|
1603 |
return $section->uservisible ||
|
|
|
1604 |
($section->visible && !$section->available && !empty($section->availableinfo)) ||
|
|
|
1605 |
(!$section->visible && !$hidesections);
|
|
|
1606 |
}
|
|
|
1607 |
|
|
|
1608 |
/**
|
|
|
1609 |
* return true if the course editor must be displayed.
|
|
|
1610 |
*
|
|
|
1611 |
* @param array|null $capabilities array of capabilities a user needs to have to see edit controls in general.
|
|
|
1612 |
* If null or not specified, the user needs to have 'moodle/course:manageactivities'.
|
|
|
1613 |
* @return bool true if edit controls must be displayed
|
|
|
1614 |
*/
|
|
|
1615 |
public function show_editor(?array $capabilities = ['moodle/course:manageactivities']): bool {
|
|
|
1616 |
global $PAGE;
|
|
|
1617 |
$course = $this->get_course();
|
|
|
1618 |
$coursecontext = context_course::instance($course->id);
|
|
|
1619 |
if ($capabilities === null) {
|
|
|
1620 |
$capabilities = ['moodle/course:manageactivities'];
|
|
|
1621 |
}
|
|
|
1622 |
return $PAGE->user_is_editing() && has_all_capabilities($capabilities, $coursecontext);
|
|
|
1623 |
}
|
|
|
1624 |
|
|
|
1625 |
/**
|
|
|
1626 |
* Check if the group mode can be displayed.
|
|
|
1627 |
* @param cm_info $cm the activity module
|
|
|
1628 |
* @return bool
|
|
|
1629 |
*/
|
|
|
1630 |
public function show_groupmode(cm_info $cm): bool {
|
|
|
1631 |
if (!plugin_supports('mod', $cm->modname, FEATURE_GROUPS, false)) {
|
|
|
1632 |
return false;
|
|
|
1633 |
}
|
|
|
1634 |
if (!has_capability('moodle/course:manageactivities', $cm->context)) {
|
|
|
1635 |
return false;
|
|
|
1636 |
}
|
|
|
1637 |
return true;
|
|
|
1638 |
}
|
|
|
1639 |
|
|
|
1640 |
/**
|
|
|
1641 |
* Allows to specify for modinfo that section is not available even when it is visible and conditionally available.
|
|
|
1642 |
*
|
|
|
1643 |
* Note: affected user can be retrieved as: $section->modinfo->userid
|
|
|
1644 |
*
|
|
|
1645 |
* Course format plugins can override the method to change the properties $available and $availableinfo that were
|
|
|
1646 |
* calculated by conditional availability.
|
|
|
1647 |
* To make section unavailable set:
|
|
|
1648 |
* $available = false;
|
|
|
1649 |
* To make unavailable section completely hidden set:
|
|
|
1650 |
* $availableinfo = '';
|
|
|
1651 |
* To make unavailable section visible with availability message set:
|
|
|
1652 |
* $availableinfo = get_string('sectionhidden', 'format_xxx');
|
|
|
1653 |
*
|
|
|
1654 |
* @param section_info $section
|
|
|
1655 |
* @param bool $available the 'available' propery of the section_info as it was evaluated by conditional availability.
|
|
|
1656 |
* Can be changed by the method but 'false' can not be overridden by 'true'.
|
|
|
1657 |
* @param string $availableinfo the 'availableinfo' propery of the section_info as it was evaluated by conditional availability.
|
|
|
1658 |
* Can be changed by the method
|
|
|
1659 |
*/
|
|
|
1660 |
public function section_get_available_hook(section_info $section, &$available, &$availableinfo) {
|
|
|
1661 |
}
|
|
|
1662 |
|
|
|
1663 |
/**
|
|
|
1664 |
* Whether this format allows to delete sections
|
|
|
1665 |
*
|
|
|
1666 |
* If format supports deleting sections it is also recommended to define language string
|
|
|
1667 |
* 'deletesection' inside the format.
|
|
|
1668 |
*
|
|
|
1669 |
* Do not call this function directly, instead use course_can_delete_section()
|
|
|
1670 |
*
|
|
|
1671 |
* @param int|stdClass|section_info $section
|
|
|
1672 |
* @return bool
|
|
|
1673 |
*/
|
|
|
1674 |
public function can_delete_section($section) {
|
|
|
1675 |
return false;
|
|
|
1676 |
}
|
|
|
1677 |
|
|
|
1678 |
/**
|
|
|
1679 |
* Deletes a section
|
|
|
1680 |
*
|
|
|
1681 |
* Do not call this function directly, instead call course_delete_section()
|
|
|
1682 |
*
|
|
|
1683 |
* @param int|stdClass|section_info $sectionornum
|
|
|
1684 |
* @param bool $forcedeleteifnotempty if set to false section will not be deleted if it has modules in it.
|
|
|
1685 |
* @return bool whether section was deleted
|
|
|
1686 |
*/
|
|
|
1687 |
public function delete_section($sectionornum, $forcedeleteifnotempty = false) {
|
|
|
1688 |
global $DB;
|
|
|
1689 |
if (!$this->uses_sections()) {
|
|
|
1690 |
// Not possible to delete section if sections are not used.
|
|
|
1691 |
return false;
|
|
|
1692 |
}
|
|
|
1693 |
if (is_object($sectionornum)) {
|
|
|
1694 |
$section = $sectionornum;
|
|
|
1695 |
} else {
|
|
|
1696 |
$section = $DB->get_record(
|
|
|
1697 |
'course_sections',
|
|
|
1698 |
['course' => $this->get_courseid(), 'section' => $sectionornum],
|
|
|
1699 |
'id,section,sequence,summary');
|
|
|
1700 |
}
|
|
|
1701 |
if (!$section || !$section->section) {
|
|
|
1702 |
// Not possible to delete 0-section.
|
|
|
1703 |
return false;
|
|
|
1704 |
}
|
|
|
1705 |
|
|
|
1706 |
if (!$forcedeleteifnotempty && (!empty($section->sequence) || !empty($section->summary))) {
|
|
|
1707 |
return false;
|
|
|
1708 |
}
|
|
|
1709 |
|
|
|
1710 |
$course = $this->get_course();
|
|
|
1711 |
|
|
|
1712 |
// Remove the marker if it points to this section.
|
|
|
1713 |
if ($section->section == $course->marker) {
|
|
|
1714 |
course_set_marker($course->id, 0);
|
|
|
1715 |
}
|
|
|
1716 |
|
|
|
1717 |
$lastsection = $DB->get_field_sql('SELECT max(section) from {course_sections}
|
|
|
1718 |
WHERE course = ?', array($course->id));
|
|
|
1719 |
|
|
|
1720 |
// Find out if we need to descrease the 'numsections' property later.
|
|
|
1721 |
$courseformathasnumsections = array_key_exists('numsections',
|
|
|
1722 |
$this->get_format_options());
|
|
|
1723 |
$decreasenumsections = $courseformathasnumsections && ($section->section <= $course->numsections);
|
|
|
1724 |
|
|
|
1725 |
// Move the section to the end.
|
|
|
1726 |
move_section_to($course, $section->section, $lastsection, true);
|
|
|
1727 |
|
|
|
1728 |
// Delete all modules from the section.
|
|
|
1729 |
foreach (preg_split('/,/', $section->sequence, -1, PREG_SPLIT_NO_EMPTY) as $cmid) {
|
|
|
1730 |
course_delete_module($cmid);
|
|
|
1731 |
}
|
|
|
1732 |
|
|
|
1733 |
// Delete section and it's format options.
|
|
|
1734 |
$DB->delete_records('course_format_options', array('sectionid' => $section->id));
|
|
|
1735 |
$DB->delete_records('course_sections', array('id' => $section->id));
|
|
|
1736 |
// Invalidate the section cache by given section id.
|
|
|
1737 |
course_modinfo::purge_course_section_cache_by_id($course->id, $section->id);
|
|
|
1738 |
// Partial rebuild section cache that has been purged.
|
|
|
1739 |
rebuild_course_cache($course->id, true, true);
|
|
|
1740 |
|
|
|
1741 |
// Delete section summary files.
|
|
|
1742 |
$context = \context_course::instance($course->id);
|
|
|
1743 |
$fs = get_file_storage();
|
|
|
1744 |
$fs->delete_area_files($context->id, 'course', 'section', $section->id);
|
|
|
1745 |
|
|
|
1746 |
// Descrease 'numsections' if needed.
|
|
|
1747 |
if ($decreasenumsections) {
|
|
|
1748 |
$this->update_course_format_options(array('numsections' => $course->numsections - 1));
|
|
|
1749 |
}
|
|
|
1750 |
|
|
|
1751 |
return true;
|
|
|
1752 |
}
|
|
|
1753 |
|
|
|
1754 |
/**
|
|
|
1755 |
* Wrapper for course_delete_module method.
|
|
|
1756 |
*
|
|
|
1757 |
* Format plugins can override this method to provide their own implementation of course_delete_module.
|
|
|
1758 |
*
|
|
|
1759 |
* @param cm_info $cm the course module information
|
|
|
1760 |
* @param bool $async whether or not to try to delete the module using an adhoc task. Async also depends on a plugin hook.
|
|
|
1761 |
* @throws moodle_exception
|
|
|
1762 |
*/
|
|
|
1763 |
public function delete_module(cm_info $cm, bool $async = false) {
|
|
|
1764 |
course_delete_module($cm->id, $async);
|
|
|
1765 |
}
|
|
|
1766 |
|
|
|
1767 |
/**
|
|
|
1768 |
* Moves a section just after the target section.
|
|
|
1769 |
*
|
|
|
1770 |
* @param section_info $section the section to move
|
|
|
1771 |
* @param section_info $destination the section that should be below the moved section
|
|
|
1772 |
* @return boolean if the section can be moved or not
|
|
|
1773 |
*/
|
|
|
1774 |
public function move_section_after(section_info $section, section_info $destination): bool {
|
|
|
1775 |
if ($section->section == $destination->section || $section->section == $destination->section + 1) {
|
|
|
1776 |
return true;
|
|
|
1777 |
}
|
|
|
1778 |
// The move_section_to moves relative to the section to move. However, this
|
|
|
1779 |
// method will move the target section always after the destination.
|
|
|
1780 |
if ($section->section > $destination->section) {
|
|
|
1781 |
$newsectionnumber = $destination->section + 1;
|
|
|
1782 |
} else {
|
|
|
1783 |
$newsectionnumber = $destination->section;
|
|
|
1784 |
}
|
|
|
1785 |
return move_section_to(
|
|
|
1786 |
$this->get_course(),
|
|
|
1787 |
$section->section,
|
|
|
1788 |
$newsectionnumber
|
|
|
1789 |
);
|
|
|
1790 |
}
|
|
|
1791 |
|
|
|
1792 |
/**
|
|
|
1793 |
* Prepares the templateable object to display section name
|
|
|
1794 |
*
|
|
|
1795 |
* @param \section_info|\stdClass $section
|
|
|
1796 |
* @param bool $linkifneeded
|
|
|
1797 |
* @param bool $editable
|
|
|
1798 |
* @param null|lang_string|string $edithint
|
|
|
1799 |
* @param null|lang_string|string $editlabel
|
|
|
1800 |
* @return \core\output\inplace_editable
|
|
|
1801 |
*/
|
|
|
1802 |
public function inplace_editable_render_section_name($section, $linkifneeded = true,
|
|
|
1803 |
$editable = null, $edithint = null, $editlabel = null) {
|
|
|
1804 |
global $USER, $CFG;
|
|
|
1805 |
require_once($CFG->dirroot.'/course/lib.php');
|
|
|
1806 |
|
|
|
1807 |
if ($editable === null) {
|
|
|
1808 |
$editable = !empty($USER->editing) && has_capability('moodle/course:update',
|
|
|
1809 |
context_course::instance($section->course));
|
|
|
1810 |
}
|
|
|
1811 |
|
|
|
1812 |
$displayvalue = $title = get_section_name($section->course, $section);
|
|
|
1813 |
if ($linkifneeded) {
|
|
|
1814 |
// Display link under the section name if the course format setting is to display one section per page.
|
|
|
1815 |
$url = course_get_url($section->course, $section->section, array('navigation' => true));
|
|
|
1816 |
if ($url) {
|
|
|
1817 |
$displayvalue = html_writer::link($url, $title);
|
|
|
1818 |
}
|
|
|
1819 |
$itemtype = 'sectionname';
|
|
|
1820 |
} else {
|
|
|
1821 |
// If $linkifneeded==false, we never display the link (this is used when rendering the section header).
|
|
|
1822 |
// Itemtype 'sectionnamenl' (nl=no link) will tell the callback that link should not be rendered -
|
|
|
1823 |
// there is no other way callback can know where we display the section name.
|
|
|
1824 |
$itemtype = 'sectionnamenl';
|
|
|
1825 |
}
|
|
|
1826 |
if (empty($edithint)) {
|
|
|
1827 |
$edithint = new lang_string('editsectionname');
|
|
|
1828 |
}
|
|
|
1829 |
if (empty($editlabel)) {
|
|
|
1830 |
$editlabel = new lang_string('newsectionname', '', $title);
|
|
|
1831 |
}
|
|
|
1832 |
|
|
|
1833 |
return new \core\output\inplace_editable('format_' . $this->format, $itemtype, $section->id, $editable,
|
|
|
1834 |
$displayvalue, $section->name, $edithint, $editlabel);
|
|
|
1835 |
}
|
|
|
1836 |
|
|
|
1837 |
/**
|
|
|
1838 |
* Updates the value in the database and modifies this object respectively.
|
|
|
1839 |
*
|
|
|
1840 |
* ALWAYS check user permissions before performing an update! Throw exceptions if permissions are not sufficient
|
|
|
1841 |
* or value is not legit.
|
|
|
1842 |
*
|
|
|
1843 |
* @param stdClass $section
|
|
|
1844 |
* @param string $itemtype
|
|
|
1845 |
* @param mixed $newvalue
|
|
|
1846 |
* @return ?\core\output\inplace_editable
|
|
|
1847 |
*/
|
|
|
1848 |
public function inplace_editable_update_section_name($section, $itemtype, $newvalue) {
|
|
|
1849 |
if ($itemtype === 'sectionname' || $itemtype === 'sectionnamenl') {
|
|
|
1850 |
$context = context_course::instance($section->course);
|
|
|
1851 |
external_api::validate_context($context);
|
|
|
1852 |
require_capability('moodle/course:update', $context);
|
|
|
1853 |
|
|
|
1854 |
$newtitle = clean_param($newvalue, PARAM_TEXT);
|
|
|
1855 |
if (strval($section->name) !== strval($newtitle)) {
|
|
|
1856 |
course_update_section($section->course, $section, array('name' => $newtitle));
|
|
|
1857 |
}
|
|
|
1858 |
return $this->inplace_editable_render_section_name($section, ($itemtype === 'sectionname'), true);
|
|
|
1859 |
}
|
|
|
1860 |
}
|
|
|
1861 |
|
|
|
1862 |
|
|
|
1863 |
/**
|
|
|
1864 |
* Returns the default end date value based on the start date.
|
|
|
1865 |
*
|
|
|
1866 |
* This is the default implementation for course formats, it is based on
|
|
|
1867 |
* moodlecourse/courseduration setting. Course formats like format_weeks for
|
|
|
1868 |
* example can overwrite this method and return a value based on their internal options.
|
|
|
1869 |
*
|
|
|
1870 |
* @param \MoodleQuickForm $mform
|
|
|
1871 |
* @param array $fieldnames The form - field names mapping.
|
|
|
1872 |
* @return int
|
|
|
1873 |
*/
|
|
|
1874 |
public function get_default_course_enddate($mform, $fieldnames = array()) {
|
|
|
1875 |
|
|
|
1876 |
if (empty($fieldnames)) {
|
|
|
1877 |
$fieldnames = array('startdate' => 'startdate');
|
|
|
1878 |
}
|
|
|
1879 |
|
|
|
1880 |
$startdate = $this->get_form_start_date($mform, $fieldnames);
|
|
|
1881 |
$courseduration = intval(get_config('moodlecourse', 'courseduration'));
|
|
|
1882 |
if (!$courseduration) {
|
|
|
1883 |
// Default, it should be already set during upgrade though.
|
|
|
1884 |
$courseduration = YEARSECS;
|
|
|
1885 |
}
|
|
|
1886 |
|
|
|
1887 |
return $startdate + $courseduration;
|
|
|
1888 |
}
|
|
|
1889 |
|
|
|
1890 |
/**
|
|
|
1891 |
* Indicates whether the course format supports the creation of the Announcements forum.
|
|
|
1892 |
*
|
|
|
1893 |
* For course format plugin developers, please override this to return true if you want the Announcements forum
|
|
|
1894 |
* to be created upon course creation.
|
|
|
1895 |
*
|
|
|
1896 |
* @return bool
|
|
|
1897 |
*/
|
|
|
1898 |
public function supports_news() {
|
|
|
1899 |
// For backwards compatibility, check if default blocks include the news_items block.
|
|
|
1900 |
$defaultblocks = $this->get_default_blocks();
|
|
|
1901 |
foreach ($defaultblocks as $blocks) {
|
|
|
1902 |
if (in_array('news_items', $blocks)) {
|
|
|
1903 |
return true;
|
|
|
1904 |
}
|
|
|
1905 |
}
|
|
|
1906 |
// Return false by default.
|
|
|
1907 |
return false;
|
|
|
1908 |
}
|
|
|
1909 |
|
|
|
1910 |
/**
|
|
|
1911 |
* Get the start date value from the course settings page form.
|
|
|
1912 |
*
|
|
|
1913 |
* @param \MoodleQuickForm $mform
|
|
|
1914 |
* @param array $fieldnames The form - field names mapping.
|
|
|
1915 |
* @return int
|
|
|
1916 |
*/
|
|
|
1917 |
protected function get_form_start_date($mform, $fieldnames) {
|
|
|
1918 |
$startdate = $mform->getElementValue($fieldnames['startdate']);
|
|
|
1919 |
return $mform->getElement($fieldnames['startdate'])->exportValue($startdate);
|
|
|
1920 |
}
|
|
|
1921 |
|
|
|
1922 |
/**
|
|
|
1923 |
* Returns whether this course format allows the activity to
|
|
|
1924 |
* have "triple visibility state" - visible always, hidden on course page but available, hidden.
|
|
|
1925 |
*
|
|
|
1926 |
* @param stdClass|cm_info $cm course module (may be null if we are displaying a form for adding a module)
|
|
|
1927 |
* @param stdClass|section_info $section section where this module is located or will be added to
|
|
|
1928 |
* @return bool
|
|
|
1929 |
*/
|
|
|
1930 |
public function allow_stealth_module_visibility($cm, $section) {
|
|
|
1931 |
return false;
|
|
|
1932 |
}
|
|
|
1933 |
|
|
|
1934 |
/**
|
|
|
1935 |
* Callback used in WS core_course_edit_section when teacher performs an AJAX action on a section (show/hide)
|
|
|
1936 |
*
|
|
|
1937 |
* Access to the course is already validated in the WS but the callback has to make sure
|
|
|
1938 |
* that particular action is allowed by checking capabilities
|
|
|
1939 |
*
|
|
|
1940 |
* Course formats should register
|
|
|
1941 |
*
|
|
|
1942 |
* @param stdClass|section_info $section
|
|
|
1943 |
* @param string $action
|
|
|
1944 |
* @param int $sr the section return
|
|
|
1945 |
* @return null|array|stdClass any data for the Javascript post-processor (must be json-encodeable)
|
|
|
1946 |
*/
|
|
|
1947 |
public function section_action($section, $action, $sr) {
|
|
|
1948 |
global $PAGE;
|
|
|
1949 |
if (!$this->uses_sections() || !$section->section) {
|
|
|
1950 |
// No section actions are allowed if course format does not support sections.
|
|
|
1951 |
// No actions are allowed on the 0-section by default (overwrite in course format if needed).
|
|
|
1952 |
throw new moodle_exception('sectionactionnotsupported', 'core', null, s($action));
|
|
|
1953 |
}
|
|
|
1954 |
|
|
|
1955 |
$course = $this->get_course();
|
|
|
1956 |
$coursecontext = context_course::instance($course->id);
|
|
|
1957 |
$modinfo = $this->get_modinfo();
|
|
|
1958 |
$renderer = $this->get_renderer($PAGE);
|
|
|
1959 |
|
|
|
1960 |
if (!($section instanceof section_info)) {
|
|
|
1961 |
$section = $modinfo->get_section_info($section->section);
|
|
|
1962 |
}
|
|
|
1963 |
|
|
|
1964 |
if (!is_null($sr)) {
|
|
|
1965 |
$this->set_sectionnum($sr);
|
|
|
1966 |
}
|
|
|
1967 |
|
|
|
1968 |
switch($action) {
|
|
|
1969 |
case 'hide':
|
|
|
1970 |
case 'show':
|
|
|
1971 |
require_capability('moodle/course:sectionvisibility', $coursecontext);
|
|
|
1972 |
$visible = ($action === 'hide') ? 0 : 1;
|
|
|
1973 |
course_update_section($course, $section, array('visible' => $visible));
|
|
|
1974 |
break;
|
|
|
1975 |
case 'refresh':
|
|
|
1976 |
return [
|
|
|
1977 |
'content' => $renderer->course_section_updated($this, $section),
|
|
|
1978 |
];
|
|
|
1979 |
default:
|
|
|
1980 |
throw new moodle_exception('sectionactionnotsupported', 'core', null, s($action));
|
|
|
1981 |
}
|
|
|
1982 |
|
|
|
1983 |
return ['modules' => $this->get_section_modules_updated($section)];
|
|
|
1984 |
}
|
|
|
1985 |
|
|
|
1986 |
/**
|
|
|
1987 |
* Return an array with all section modules content.
|
|
|
1988 |
*
|
|
|
1989 |
* This method is used in section_action method to generate the updated modules content
|
|
|
1990 |
* after a modinfo change.
|
|
|
1991 |
*
|
|
|
1992 |
* @param section_info $section the section
|
|
|
1993 |
* @return string[] the full modules content.
|
|
|
1994 |
*/
|
|
|
1995 |
protected function get_section_modules_updated(section_info $section): array {
|
|
|
1996 |
global $PAGE;
|
|
|
1997 |
|
|
|
1998 |
$modules = [];
|
|
|
1999 |
|
|
|
2000 |
if (!$this->uses_sections() || !$section->section) {
|
|
|
2001 |
return $modules;
|
|
|
2002 |
}
|
|
|
2003 |
|
|
|
2004 |
// Load the cmlist output from the updated modinfo.
|
|
|
2005 |
$renderer = $this->get_renderer($PAGE);
|
|
|
2006 |
$modinfo = $this->get_modinfo();
|
|
|
2007 |
$coursesections = $modinfo->sections;
|
|
|
2008 |
if (array_key_exists($section->section, $coursesections)) {
|
|
|
2009 |
foreach ($coursesections[$section->section] as $cmid) {
|
|
|
2010 |
$cm = $modinfo->get_cm($cmid);
|
|
|
2011 |
$modules[] = $renderer->course_section_updated_cm_item($this, $section, $cm);
|
|
|
2012 |
}
|
|
|
2013 |
}
|
|
|
2014 |
return $modules;
|
|
|
2015 |
}
|
|
|
2016 |
|
|
|
2017 |
/**
|
|
|
2018 |
* Return the plugin config settings for external functions,
|
|
|
2019 |
* in some cases the configs will need formatting or be returned only if the current user has some capabilities enabled.
|
|
|
2020 |
*
|
|
|
2021 |
* @return array the list of configs
|
|
|
2022 |
* @since Moodle 3.5
|
|
|
2023 |
*/
|
|
|
2024 |
public function get_config_for_external() {
|
|
|
2025 |
return array();
|
|
|
2026 |
}
|
|
|
2027 |
|
|
|
2028 |
/**
|
|
|
2029 |
* Course deletion hook.
|
|
|
2030 |
*
|
|
|
2031 |
* Format plugins can override this method to clean any format specific data and dependencies.
|
|
|
2032 |
*
|
|
|
2033 |
*/
|
|
|
2034 |
public function delete_format_data() {
|
|
|
2035 |
global $DB;
|
|
|
2036 |
$course = $this->get_course();
|
|
|
2037 |
// By default, formats store some most display specifics in a user preference.
|
|
|
2038 |
$DB->delete_records('user_preferences', ['name' => 'coursesectionspreferences_' . $course->id]);
|
|
|
2039 |
}
|
|
|
2040 |
|
|
|
2041 |
/**
|
|
|
2042 |
* Duplicate a section
|
|
|
2043 |
*
|
|
|
2044 |
* @param section_info $originalsection The section to be duplicated
|
|
|
2045 |
* @return section_info The new duplicated section
|
|
|
2046 |
* @since Moodle 4.2
|
|
|
2047 |
*/
|
|
|
2048 |
public function duplicate_section(section_info $originalsection): section_info {
|
|
|
2049 |
if (!$this->uses_sections()) {
|
|
|
2050 |
throw new moodle_exception('sectionsnotsupported', 'core_courseformat');
|
|
|
2051 |
}
|
|
|
2052 |
|
|
|
2053 |
$course = $this->get_course();
|
|
|
2054 |
$oldsectioninfo = get_fast_modinfo($course)->get_section_info($originalsection->section);
|
|
|
2055 |
$newsection = course_create_section($course, $oldsectioninfo->section + 1); // Place new section after existing one.
|
|
|
2056 |
|
|
|
2057 |
if (!empty($originalsection->name)) {
|
|
|
2058 |
$newsection->name = get_string('duplicatedsection', 'moodle', $originalsection->name);
|
|
|
2059 |
} else {
|
|
|
2060 |
$newsection->name = $originalsection->name;
|
|
|
2061 |
}
|
|
|
2062 |
$newsection->summary = $originalsection->summary;
|
|
|
2063 |
$newsection->summaryformat = $originalsection->summaryformat;
|
|
|
2064 |
$newsection->visible = $originalsection->visible;
|
|
|
2065 |
$newsection->availability = $originalsection->availability;
|
|
|
2066 |
course_update_section($course, $newsection, $newsection);
|
|
|
2067 |
|
|
|
2068 |
$modinfo = $this->get_modinfo();
|
|
|
2069 |
|
|
|
2070 |
// Duplicate the section modules, should they exist.
|
|
|
2071 |
if (array_key_exists($originalsection->section, $modinfo->sections)) {
|
|
|
2072 |
foreach ($modinfo->sections[$originalsection->section] as $modnumber) {
|
|
|
2073 |
$originalcm = $modinfo->cms[$modnumber];
|
|
|
2074 |
duplicate_module($course, $originalcm, $newsection->id, false);
|
|
|
2075 |
}
|
|
|
2076 |
}
|
|
|
2077 |
|
|
|
2078 |
return get_fast_modinfo($course)->get_section_info_by_id($newsection->id);
|
|
|
2079 |
}
|
|
|
2080 |
|
|
|
2081 |
/**
|
|
|
2082 |
* Get the required javascript files for the course format.
|
|
|
2083 |
*
|
|
|
2084 |
* @return array The list of javascript files required by the course format.
|
|
|
2085 |
*/
|
|
|
2086 |
public function get_required_jsfiles(): array {
|
|
|
2087 |
return [];
|
|
|
2088 |
}
|
|
|
2089 |
|
|
|
2090 |
/**
|
|
|
2091 |
* Determines whether section items can be removed from the navigation, just like the breadcrumb feature seen on activity pages.
|
|
|
2092 |
* By default, it returns false but can be overridden by the course format to change the behaviour.
|
|
|
2093 |
*
|
|
|
2094 |
* @return bool True if sections can be removed, false otherwise.
|
|
|
2095 |
*/
|
|
|
2096 |
public function can_sections_be_removed_from_navigation(): bool {
|
|
|
2097 |
return false;
|
|
|
2098 |
}
|
|
|
2099 |
}
|