weekly release 2.7.10+
[moodle.git] / course / format / lib.php
blobd3349d276a60a7b578728a8a0edec5c17da3924b
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.
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/>.
17 /**
18 * Base class for course format plugins
20 * @package core_course
21 * @copyright 2012 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die;
27 /**
28 * Returns an instance of format class (extending format_base) for given course
30 * @param int|stdClass $courseorid either course id or
31 * an object that has the property 'format' and may contain property 'id'
32 * @return format_base
34 function course_get_format($courseorid) {
35 return format_base::instance($courseorid);
38 /**
39 * Base class for course formats
41 * Each course format must declare class
42 * class format_FORMATNAME extends format_base {}
43 * in file lib.php
45 * For each course just one instance of this class is created and it will always be returned by
46 * course_get_format($courseorid). Format may store it's specific course-dependent options in
47 * variables of this class.
49 * In rare cases instance of child class may be created just for format without course id
50 * i.e. to check if format supports AJAX.
52 * Also course formats may extend class section_info and overwrite
53 * format_base::build_section_cache() to return more information about sections.
55 * If you are upgrading from Moodle 2.3 start with copying the class format_legacy and renaming
56 * it to format_FORMATNAME, then move the code from your callback functions into
57 * appropriate functions of the class.
59 * @package core_course
60 * @copyright 2012 Marina Glancy
61 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
63 abstract class format_base {
64 /** @var int Id of the course in this instance (maybe 0) */
65 protected $courseid;
66 /** @var string format used for this course. Please note that it can be different from
67 * course.format field if course referes to non-existing of disabled format */
68 protected $format;
69 /** @var stdClass data for course object, please use {@link format_base::get_course()} */
70 protected $course = false;
71 /** @var array caches format options, please use {@link format_base::get_format_options()} */
72 protected $formatoptions = array();
73 /** @var array cached instances */
74 private static $instances = array();
75 /** @var array plugin name => class name. */
76 private static $classesforformat = array('site' => 'site');
78 /**
79 * Creates a new instance of class
81 * Please use {@link course_get_format($courseorid)} to get an instance of the format class
83 * @param string $format
84 * @param int $courseid
85 * @return format_base
87 protected function __construct($format, $courseid) {
88 $this->format = $format;
89 $this->courseid = $courseid;
92 /**
93 * Validates that course format exists and enabled and returns either itself or default format
95 * @param string $format
96 * @return string
98 protected static final function get_format_or_default($format) {
99 if (array_key_exists($format, self::$classesforformat)) {
100 return self::$classesforformat[$format];
103 $plugins = get_sorted_course_formats();
104 foreach ($plugins as $plugin) {
105 self::$classesforformat[$plugin] = $plugin;
108 if (array_key_exists($format, self::$classesforformat)) {
109 return self::$classesforformat[$format];
112 if (PHPUNIT_TEST && class_exists('format_' . $format)) {
113 // Allow unittests to use non-existing course formats.
114 return $format;
117 // Else return default format
118 $defaultformat = get_config('moodlecourse', 'format');
119 if (!in_array($defaultformat, $plugins)) {
120 // when default format is not set correctly, use the first available format
121 $defaultformat = reset($plugins);
123 debugging('Format plugin format_'.$format.' is not found. Using default format_'.$defaultformat, DEBUG_DEVELOPER);
125 self::$classesforformat[$format] = $defaultformat;
126 return $defaultformat;
130 * Get class name for the format
132 * If course format xxx does not declare class format_xxx, format_legacy will be returned.
133 * This function also includes lib.php file from corresponding format plugin
135 * @param string $format
136 * @return string
138 protected static final function get_class_name($format) {
139 global $CFG;
140 static $classnames = array('site' => 'format_site');
141 if (!isset($classnames[$format])) {
142 $plugins = core_component::get_plugin_list('format');
143 $usedformat = self::get_format_or_default($format);
144 if (isset($plugins[$usedformat]) && file_exists($plugins[$usedformat].'/lib.php')) {
145 require_once($plugins[$usedformat].'/lib.php');
147 $classnames[$format] = 'format_'. $usedformat;
148 if (!class_exists($classnames[$format])) {
149 require_once($CFG->dirroot.'/course/format/formatlegacy.php');
150 $classnames[$format] = 'format_legacy';
153 return $classnames[$format];
157 * Returns an instance of the class
159 * @todo MDL-35727 use MUC for caching of instances, limit the number of cached instances
161 * @param int|stdClass $courseorid either course id or
162 * an object that has the property 'format' and may contain property 'id'
163 * @return format_base
165 public static final function instance($courseorid) {
166 global $DB;
167 if (!is_object($courseorid)) {
168 $courseid = (int)$courseorid;
169 if ($courseid && isset(self::$instances[$courseid]) && count(self::$instances[$courseid]) == 1) {
170 $formats = array_keys(self::$instances[$courseid]);
171 $format = reset($formats);
172 } else {
173 $format = $DB->get_field('course', 'format', array('id' => $courseid), MUST_EXIST);
175 } else {
176 $format = $courseorid->format;
177 if (isset($courseorid->id)) {
178 $courseid = clean_param($courseorid->id, PARAM_INT);
179 } else {
180 $courseid = 0;
183 // validate that format exists and enabled, use default otherwise
184 $format = self::get_format_or_default($format);
185 if (!isset(self::$instances[$courseid][$format])) {
186 $classname = self::get_class_name($format);
187 self::$instances[$courseid][$format] = new $classname($format, $courseid);
189 return self::$instances[$courseid][$format];
193 * Resets cache for the course (or all caches)
194 * To be called from {@link rebuild_course_cache()}
196 * @param int $courseid
198 public static final function reset_course_cache($courseid = 0) {
199 if ($courseid) {
200 if (isset(self::$instances[$courseid])) {
201 foreach (self::$instances[$courseid] as $format => $object) {
202 // in case somebody keeps the reference to course format object
203 self::$instances[$courseid][$format]->course = false;
204 self::$instances[$courseid][$format]->formatoptions = array();
206 unset(self::$instances[$courseid]);
208 } else {
209 self::$instances = array();
214 * Returns the format name used by this course
216 * @return string
218 public final function get_format() {
219 return $this->format;
223 * Returns id of the course (0 if course is not specified)
225 * @return int
227 public final function get_courseid() {
228 return $this->courseid;
232 * Returns a record from course database table plus additional fields
233 * that course format defines
235 * @return stdClass
237 public function get_course() {
238 global $DB;
239 if (!$this->courseid) {
240 return null;
242 if ($this->course === false) {
243 $this->course = get_course($this->courseid);
244 $options = $this->get_format_options();
245 $dbcoursecolumns = null;
246 foreach ($options as $optionname => $optionvalue) {
247 if (isset($this->course->$optionname)) {
248 // Course format options must not have the same names as existing columns in db table "course".
249 if (!isset($dbcoursecolumns)) {
250 $dbcoursecolumns = $DB->get_columns('course');
252 if (isset($dbcoursecolumns[$optionname])) {
253 debugging('The option name '.$optionname.' in course format '.$this->format.
254 ' is invalid because the field with the same name exists in {course} table',
255 DEBUG_DEVELOPER);
256 continue;
259 $this->course->$optionname = $optionvalue;
262 return $this->course;
266 * Returns true if the course has a front page.
268 * This function is called to determine if the course has a view page, whether or not
269 * it contains a listing of activities. It can be useful to set this to false when the course
270 * format has only one activity and ignores the course page. Or if there are multiple
271 * activities but no page to see the centralised information.
273 * Initially this was created to know if forms should add a button to return to the course page.
274 * So if 'Return to course' does not make sense in your format your should probably return false.
276 * @return boolean
277 * @since Moodle 2.6
279 public function has_view_page() {
280 return true;
284 * Returns true if this course format uses sections
286 * This function may be called without specifying the course id
287 * i.e. in {@link course_format_uses_sections()}
289 * Developers, note that if course format does use sections there should be defined a language
290 * string with the name 'sectionname' defining what the section relates to in the format, i.e.
291 * $string['sectionname'] = 'Topic';
292 * or
293 * $string['sectionname'] = 'Week';
295 * @return bool
297 public function uses_sections() {
298 return false;
302 * Returns a list of sections used in the course
304 * This is a shortcut to get_fast_modinfo()->get_section_info_all()
305 * @see get_fast_modinfo()
306 * @see course_modinfo::get_section_info_all()
308 * @return array of section_info objects
310 public final function get_sections() {
311 if ($course = $this->get_course()) {
312 $modinfo = get_fast_modinfo($course);
313 return $modinfo->get_section_info_all();
315 return array();
319 * Returns information about section used in course
321 * @param int|stdClass $section either section number (field course_section.section) or row from course_section table
322 * @param int $strictness
323 * @return section_info
325 public final function get_section($section, $strictness = IGNORE_MISSING) {
326 if (is_object($section)) {
327 $sectionnum = $section->section;
328 } else {
329 $sectionnum = $section;
331 $sections = $this->get_sections();
332 if (array_key_exists($sectionnum, $sections)) {
333 return $sections[$sectionnum];
335 if ($strictness == MUST_EXIST) {
336 throw new moodle_exception('sectionnotexist');
338 return null;
342 * Returns the display name of the given section that the course prefers.
344 * @param int|stdClass $section Section object from database or just field course_sections.section
345 * @return Display name that the course format prefers, e.g. "Topic 2"
347 public function get_section_name($section) {
348 if (is_object($section)) {
349 $sectionnum = $section->section;
350 } else {
351 $sectionnum = $section;
353 return get_string('sectionname', 'format_'.$this->format) . ' ' . $sectionnum;
357 * Returns the information about the ajax support in the given source format
359 * The returned object's property (boolean)capable indicates that
360 * the course format supports Moodle course ajax features.
362 * @return stdClass
364 public function supports_ajax() {
365 // no support by default
366 $ajaxsupport = new stdClass();
367 $ajaxsupport->capable = false;
368 return $ajaxsupport;
372 * Custom action after section has been moved in AJAX mode
374 * Used in course/rest.php
376 * @return array This will be passed in ajax respose
378 public function ajax_section_move() {
379 return null;
383 * The URL to use for the specified course (with section)
385 * Please note that course view page /course/view.php?id=COURSEID is hardcoded in many
386 * places in core and contributed modules. If course format wants to change the location
387 * of the view script, it is not enough to change just this function. Do not forget
388 * to add proper redirection.
390 * @param int|stdClass $section Section object from database or just field course_sections.section
391 * if null the course view page is returned
392 * @param array $options options for view URL. At the moment core uses:
393 * 'navigation' (bool) if true and section has no separate page, the function returns null
394 * 'sr' (int) used by multipage formats to specify to which section to return
395 * @return null|moodle_url
397 public function get_view_url($section, $options = array()) {
398 $course = $this->get_course();
399 $url = new moodle_url('/course/view.php', array('id' => $course->id));
401 if (array_key_exists('sr', $options)) {
402 $sectionno = $options['sr'];
403 } else if (is_object($section)) {
404 $sectionno = $section->section;
405 } else {
406 $sectionno = $section;
408 if (!empty($options['navigation']) && $sectionno !== null) {
409 // by default assume that sections are never displayed on separate pages
410 return null;
412 if ($this->uses_sections() && $sectionno !== null) {
413 $url->set_anchor('section-'.$sectionno);
415 return $url;
419 * Loads all of the course sections into the navigation
421 * This method is called from {@link global_navigation::load_course_sections()}
423 * By default the method {@link global_navigation::load_generic_course_sections()} is called
425 * When overwriting please note that navigationlib relies on using the correct values for
426 * arguments $type and $key in {@link navigation_node::add()}
428 * Example of code creating a section node:
429 * $sectionnode = $node->add($sectionname, $url, navigation_node::TYPE_SECTION, null, $section->id);
430 * $sectionnode->nodetype = navigation_node::NODETYPE_BRANCH;
432 * Example of code creating an activity node:
433 * $activitynode = $sectionnode->add($activityname, $action, navigation_node::TYPE_ACTIVITY, null, $activity->id, $icon);
434 * if (global_navigation::module_extends_navigation($activity->modname)) {
435 * $activitynode->nodetype = navigation_node::NODETYPE_BRANCH;
436 * } else {
437 * $activitynode->nodetype = navigation_node::NODETYPE_LEAF;
440 * Also note that if $navigation->includesectionnum is not null, the section with this relative
441 * number needs is expected to be loaded
443 * @param global_navigation $navigation
444 * @param navigation_node $node The course node within the navigation
446 public function extend_course_navigation($navigation, navigation_node $node) {
447 if ($course = $this->get_course()) {
448 $navigation->load_generic_course_sections($course, $node);
450 return array();
454 * Returns the list of blocks to be automatically added for the newly created course
456 * @see blocks_add_default_course_blocks()
458 * @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
459 * each of values is an array of block names (for left and right side columns)
461 public function get_default_blocks() {
462 global $CFG;
463 if (!empty($CFG->defaultblocks)){
464 return blocks_parse_default_blocks_list($CFG->defaultblocks);
466 $blocknames = array(
467 BLOCK_POS_LEFT => array(),
468 BLOCK_POS_RIGHT => array('search_forums', 'news_items', 'calendar_upcoming', 'recent_activity')
470 return $blocknames;
474 * Returns the localised name of this course format plugin
476 * @return lang_string
478 public final function get_format_name() {
479 return new lang_string('pluginname', 'format_'.$this->get_format());
483 * Definitions of the additional options that this course format uses for course
485 * This function may be called often, it should be as fast as possible.
486 * Avoid using get_string() method, use "new lang_string()" instead
487 * It is not recommended to use dynamic or course-dependant expressions here
488 * This function may be also called when course does not exist yet.
490 * Option names must be different from fields in the {course} talbe or any form elements on
491 * course edit form, it may even make sence to use special prefix for them.
493 * Each option must have the option name as a key and the array of properties as a value:
494 * 'default' - default value for this option (assumed null if not specified)
495 * 'type' - type of the option value (PARAM_INT, PARAM_RAW, etc.)
497 * Additional properties used by default implementation of
498 * {@link format_base::create_edit_form_elements()} (calls this method with $foreditform = true)
499 * 'label' - localised human-readable label for the edit form
500 * 'element_type' - type of the form element, default 'text'
501 * 'element_attributes' - additional attributes for the form element, these are 4th and further
502 * arguments in the moodleform::addElement() method
503 * 'help' - string for help button. Note that if 'help' value is 'myoption' then the string with
504 * the name 'myoption_help' must exist in the language file
505 * 'help_component' - language component to look for help string, by default this the component
506 * for this course format
508 * This is an interface for creating simple form elements. If format plugin wants to use other
509 * methods such as disableIf, it can be done by overriding create_edit_form_elements().
511 * Course format options can be accessed as:
512 * $this->get_course()->OPTIONNAME (inside the format class)
513 * course_get_format($course)->get_course()->OPTIONNAME (outside of format class)
515 * All course options are returned by calling:
516 * $this->get_format_options();
518 * @param bool $foreditform
519 * @return array of options
521 public function course_format_options($foreditform = false) {
522 return array();
526 * Definitions of the additional options that this course format uses for section
528 * See {@link format_base::course_format_options()} for return array definition.
530 * Additionally section format options may have property 'cache' set to true
531 * if this option needs to be cached in {@link get_fast_modinfo()}. The 'cache' property
532 * is recommended to be set only for fields used in {@link format_base::get_section_name()},
533 * {@link format_base::extend_course_navigation()} and {@link format_base::get_view_url()}
535 * For better performance cached options are recommended to have 'cachedefault' property
536 * Unlike 'default', 'cachedefault' should be static and not access get_config().
538 * Regardless of value of 'cache' all options are accessed in the code as
539 * $sectioninfo->OPTIONNAME
540 * where $sectioninfo is instance of section_info, returned by
541 * get_fast_modinfo($course)->get_section_info($sectionnum)
542 * or get_fast_modinfo($course)->get_section_info_all()
544 * All format options for particular section are returned by calling:
545 * $this->get_format_options($section);
547 * @param bool $foreditform
548 * @return array
550 public function section_format_options($foreditform = false) {
551 return array();
555 * Returns the format options stored for this course or course section
557 * When overriding please note that this function is called from rebuild_course_cache()
558 * and section_info object, therefore using of get_fast_modinfo() and/or any function that
559 * accesses it may lead to recursion.
561 * @param null|int|stdClass|section_info $section if null the course format options will be returned
562 * otherwise options for specified section will be returned. This can be either
563 * section object or relative section number (field course_sections.section)
564 * @return array
566 public function get_format_options($section = null) {
567 global $DB;
568 if ($section === null) {
569 $options = $this->course_format_options();
570 } else {
571 $options = $this->section_format_options();
573 if (empty($options)) {
574 // there are no option for course/sections anyway, no need to go further
575 return array();
577 if ($section === null) {
578 // course format options will be returned
579 $sectionid = 0;
580 } else if ($this->courseid && isset($section->id)) {
581 // course section format options will be returned
582 $sectionid = $section->id;
583 } else if ($this->courseid && is_int($section) &&
584 ($sectionobj = $DB->get_record('course_sections',
585 array('section' => $section, 'course' => $this->courseid), 'id'))) {
586 // course section format options will be returned
587 $sectionid = $sectionobj->id;
588 } else {
589 // non-existing (yet) section was passed as an argument
590 // default format options for course section will be returned
591 $sectionid = -1;
593 if (!array_key_exists($sectionid, $this->formatoptions)) {
594 $this->formatoptions[$sectionid] = array();
595 // first fill with default values
596 foreach ($options as $optionname => $optionparams) {
597 $this->formatoptions[$sectionid][$optionname] = null;
598 if (array_key_exists('default', $optionparams)) {
599 $this->formatoptions[$sectionid][$optionname] = $optionparams['default'];
602 if ($this->courseid && $sectionid !== -1) {
603 // overwrite the default options values with those stored in course_format_options table
604 // nothing can be stored if we are interested in generic course ($this->courseid == 0)
605 // or generic section ($sectionid === 0)
606 $records = $DB->get_records('course_format_options',
607 array('courseid' => $this->courseid,
608 'format' => $this->format,
609 'sectionid' => $sectionid
610 ), '', 'id,name,value');
611 foreach ($records as $record) {
612 if (array_key_exists($record->name, $this->formatoptions[$sectionid])) {
613 $value = $record->value;
614 if ($value !== null && isset($options[$record->name]['type'])) {
615 // this will convert string value to number if needed
616 $value = clean_param($value, $options[$record->name]['type']);
618 $this->formatoptions[$sectionid][$record->name] = $value;
623 return $this->formatoptions[$sectionid];
627 * Adds format options elements to the course/section edit form
629 * This function is called from {@link course_edit_form::definition_after_data()}
631 * @param MoodleQuickForm $mform form the elements are added to
632 * @param bool $forsection 'true' if this is a section edit form, 'false' if this is course edit form
633 * @return array array of references to the added form elements
635 public function create_edit_form_elements(&$mform, $forsection = false) {
636 $elements = array();
637 if ($forsection) {
638 $options = $this->section_format_options(true);
639 } else {
640 $options = $this->course_format_options(true);
642 foreach ($options as $optionname => $option) {
643 if (!isset($option['element_type'])) {
644 $option['element_type'] = 'text';
646 $args = array($option['element_type'], $optionname, $option['label']);
647 if (!empty($option['element_attributes'])) {
648 $args = array_merge($args, $option['element_attributes']);
650 $elements[] = call_user_func_array(array($mform, 'addElement'), $args);
651 if (isset($option['help'])) {
652 $helpcomponent = 'format_'. $this->get_format();
653 if (isset($option['help_component'])) {
654 $helpcomponent = $option['help_component'];
656 $mform->addHelpButton($optionname, $option['help'], $helpcomponent);
658 if (isset($option['type'])) {
659 $mform->setType($optionname, $option['type']);
661 if (is_null($mform->getElementValue($optionname)) && isset($option['default'])) {
662 $mform->setDefault($optionname, $option['default']);
665 return $elements;
669 * Override if you need to perform some extra validation of the format options
671 * @param array $data array of ("fieldname"=>value) of submitted data
672 * @param array $files array of uploaded files "element_name"=>tmp_file_path
673 * @param array $errors errors already discovered in edit form validation
674 * @return array of "element_name"=>"error_description" if there are errors,
675 * or an empty array if everything is OK.
676 * Do not repeat errors from $errors param here
678 public function edit_form_validation($data, $files, $errors) {
679 return array();
683 * Updates format options for a course or section
685 * If $data does not contain property with the option name, the option will not be updated
687 * @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data
688 * @param null|int null if these are options for course or section id (course_sections.id)
689 * if these are options for section
690 * @return bool whether there were any changes to the options values
692 protected function update_format_options($data, $sectionid = null) {
693 global $DB;
694 if (!$sectionid) {
695 $allformatoptions = $this->course_format_options();
696 $sectionid = 0;
697 } else {
698 $allformatoptions = $this->section_format_options();
700 if (empty($allformatoptions)) {
701 // nothing to update anyway
702 return false;
704 $defaultoptions = array();
705 $cached = array();
706 foreach ($allformatoptions as $key => $option) {
707 $defaultoptions[$key] = null;
708 if (array_key_exists('default', $option)) {
709 $defaultoptions[$key] = $option['default'];
711 $cached[$key] = ($sectionid === 0 || !empty($option['cache']));
713 $records = $DB->get_records('course_format_options',
714 array('courseid' => $this->courseid,
715 'format' => $this->format,
716 'sectionid' => $sectionid
717 ), '', 'name,id,value');
718 $changed = $needrebuild = false;
719 $data = (array)$data;
720 foreach ($defaultoptions as $key => $value) {
721 if (isset($records[$key])) {
722 if (array_key_exists($key, $data) && $records[$key]->value !== $data[$key]) {
723 $DB->set_field('course_format_options', 'value',
724 $data[$key], array('id' => $records[$key]->id));
725 $changed = true;
726 $needrebuild = $needrebuild || $cached[$key];
728 } else {
729 if (array_key_exists($key, $data) && $data[$key] !== $value) {
730 $newvalue = $data[$key];
731 $changed = true;
732 $needrebuild = $needrebuild || $cached[$key];
733 } else {
734 $newvalue = $value;
735 // we still insert entry in DB but there are no changes from user point of
736 // view and no need to call rebuild_course_cache()
738 $DB->insert_record('course_format_options', array(
739 'courseid' => $this->courseid,
740 'format' => $this->format,
741 'sectionid' => $sectionid,
742 'name' => $key,
743 'value' => $newvalue
747 if ($needrebuild) {
748 rebuild_course_cache($this->courseid, true);
750 if ($changed) {
751 // reset internal caches
752 if (!$sectionid) {
753 $this->course = false;
755 unset($this->formatoptions[$sectionid]);
757 return $changed;
761 * Updates format options for a course
763 * If $data does not contain property with the option name, the option will not be updated
765 * @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data
766 * @param stdClass $oldcourse if this function is called from {@link update_course()}
767 * this object contains information about the course before update
768 * @return bool whether there were any changes to the options values
770 public function update_course_format_options($data, $oldcourse = null) {
771 return $this->update_format_options($data);
775 * Updates format options for a section
777 * Section id is expected in $data->id (or $data['id'])
778 * If $data does not contain property with the option name, the option will not be updated
780 * @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data
781 * @return bool whether there were any changes to the options values
783 public function update_section_format_options($data) {
784 $data = (array)$data;
785 return $this->update_format_options($data, $data['id']);
789 * Return an instance of moodleform to edit a specified section
791 * Default implementation returns instance of editsection_form that automatically adds
792 * additional fields defined in {@link format_base::section_format_options()}
794 * Format plugins may extend editsection_form if they want to have custom edit section form.
796 * @param mixed $action the action attribute for the form. If empty defaults to auto detect the
797 * current url. If a moodle_url object then outputs params as hidden variables.
798 * @param array $customdata the array with custom data to be passed to the form
799 * /course/editsection.php passes section_info object in 'cs' field
800 * for filling availability fields
801 * @return moodleform
803 public function editsection_form($action, $customdata = array()) {
804 global $CFG;
805 require_once($CFG->dirroot. '/course/editsection_form.php');
806 $context = context_course::instance($this->courseid);
807 if (!array_key_exists('course', $customdata)) {
808 $customdata['course'] = $this->get_course();
810 return new editsection_form($action, $customdata);
814 * Allows course format to execute code on moodle_page::set_course()
816 * @param moodle_page $page instance of page calling set_course
818 public function page_set_course(moodle_page $page) {
822 * Allows course format to execute code on moodle_page::set_cm()
824 * Current module can be accessed as $page->cm (returns instance of cm_info)
826 * @param moodle_page $page instance of page calling set_cm
828 public function page_set_cm(moodle_page $page) {
832 * Course-specific information to be output on any course page (usually above navigation bar)
834 * Example of usage:
835 * define
836 * class format_FORMATNAME_XXX implements renderable {}
838 * create format renderer in course/format/FORMATNAME/renderer.php, define rendering function:
839 * class format_FORMATNAME_renderer extends plugin_renderer_base {
840 * protected function render_format_FORMATNAME_XXX(format_FORMATNAME_XXX $xxx) {
841 * return html_writer::tag('div', 'This is my header/footer');
845 * Return instance of format_FORMATNAME_XXX in this function, the appropriate method from
846 * plugin renderer will be called
848 * @return null|renderable null for no output or object with data for plugin renderer
850 public function course_header() {
851 return null;
855 * Course-specific information to be output on any course page (usually in the beginning of
856 * standard footer)
858 * See {@link format_base::course_header()} for usage
860 * @return null|renderable null for no output or object with data for plugin renderer
862 public function course_footer() {
863 return null;
867 * Course-specific information to be output immediately above content on any course page
869 * See {@link format_base::course_header()} for usage
871 * @return null|renderable null for no output or object with data for plugin renderer
873 public function course_content_header() {
874 return null;
878 * Course-specific information to be output immediately below content on any course page
880 * See {@link format_base::course_header()} for usage
882 * @return null|renderable null for no output or object with data for plugin renderer
884 public function course_content_footer() {
885 return null;
889 * Returns instance of page renderer used by this plugin
891 * @param moodle_page $page
892 * @return renderer_base
894 public function get_renderer(moodle_page $page) {
895 return $page->get_renderer('format_'. $this->get_format());
899 * Returns true if the specified section is current
901 * By default we analyze $course->marker
903 * @param int|stdClass|section_info $section
904 * @return bool
906 public function is_section_current($section) {
907 if (is_object($section)) {
908 $sectionnum = $section->section;
909 } else {
910 $sectionnum = $section;
912 return ($sectionnum && ($course = $this->get_course()) && $course->marker == $sectionnum);
917 * Pseudo course format used for the site main page
919 * @package core_course
920 * @copyright 2012 Marina Glancy
921 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
923 class format_site extends format_base {
926 * Returns the display name of the given section that the course prefers.
928 * @param int|stdClass $section Section object from database or just field section.section
929 * @return Display name that the course format prefers, e.g. "Topic 2"
931 function get_section_name($section) {
932 return get_string('site');
936 * For this fake course referring to the whole site, the site homepage is always returned
937 * regardless of arguments
939 * @param int|stdClass $section
940 * @param array $options
941 * @return null|moodle_url
943 public function get_view_url($section, $options = array()) {
944 return new moodle_url('/', array('redirect' => 0));
948 * Returns the list of blocks to be automatically added on the site frontpage when moodle is installed
950 * @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
951 * each of values is an array of block names (for left and right side columns)
953 public function get_default_blocks() {
954 return blocks_get_default_site_course_blocks();
958 * Definitions of the additional options that site uses
960 * @param bool $foreditform
961 * @return array of options
963 public function course_format_options($foreditform = false) {
964 static $courseformatoptions = false;
965 if ($courseformatoptions === false) {
966 $courseformatoptions = array(
967 'numsections' => array(
968 'default' => 1,
969 'type' => PARAM_INT,
973 return $courseformatoptions;