2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Course format class to allow plugins developed for Moodle 2.3 to work in the new API
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;
28 * Course format class to allow plugins developed for Moodle 2.3 to work in the new API
30 * @package core_course
31 * @copyright 2012 Marina Glancy
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class format_legacy
extends format_base
{
37 * Returns true if this course format uses sections
39 * This function calls function callback_FORMATNAME_uses_sections() if it exists
43 public function uses_sections() {
45 // Note that lib.php in course format folder is already included by now
46 $featurefunction = 'callback_'.$this->format
.'_uses_sections';
47 if (function_exists($featurefunction)) {
48 return $featurefunction();
54 * Returns the display name of the given section that the course prefers.
56 * This function calls function callback_FORMATNAME_get_section_name() if it exists
58 * @param int|stdClass $section Section object from database or just field section.section
59 * @return string Display name that the course format prefers, e.g. "Topic 2"
61 public function get_section_name($section) {
62 // Use course formatter callback if it exists
63 $namingfunction = 'callback_'.$this->format
.'_get_section_name';
64 if (function_exists($namingfunction) && ($course = $this->get_course())) {
65 return $namingfunction($course, $this->get_section($section));
68 // else, default behavior:
69 return parent
::get_section_name($section);
73 * The URL to use for the specified course (with section)
75 * This function calls function callback_FORMATNAME_get_section_url() if it exists
77 * @param int|stdClass $section Section object from database or just field course_sections.section
78 * if omitted the course view page is returned
79 * @param array $options options for view URL. At the moment core uses:
80 * 'navigation' (bool) if true and section has no separate page, the function returns null
81 * 'sr' (int) used by multipage formats to specify to which section to return
82 * @return null|moodle_url
84 public function get_view_url($section, $options = array()) {
85 // Use course formatter callback if it exists
86 $featurefunction = 'callback_'.$this->format
.'_get_section_url';
87 if (function_exists($featurefunction) && ($course = $this->get_course())) {
88 if (is_object($section)) {
89 $sectionnum = $section->section
;
91 $sectionnum = $section;
94 $url = $featurefunction($course, $sectionnum);
95 if ($url ||
!empty($options['navigation'])) {
101 // if function is not defined
102 if (!$this->uses_sections() ||
103 !array_key_exists('coursedisplay', $this->course_format_options())) {
105 return parent
::get_view_url($section, $options);
108 $course = $this->get_course();
109 $url = new moodle_url('/course/view.php', array('id' => $course->id
));
112 if (array_key_exists('sr', $options)) {
113 $sr = $options['sr'];
115 if (is_object($section)) {
116 $sectionno = $section->section
;
118 $sectionno = $section;
120 if ($sectionno !== null) {
123 $usercoursedisplay = COURSE_DISPLAY_MULTIPAGE
;
126 $usercoursedisplay = COURSE_DISPLAY_SINGLEPAGE
;
129 $usercoursedisplay = $course->coursedisplay
;
131 if ($sectionno != 0 && $usercoursedisplay == COURSE_DISPLAY_MULTIPAGE
) {
132 $url->param('section', $sectionno);
134 if (!empty($options['navigation'])) {
137 $url->set_anchor('section-'.$sectionno);
144 * Returns the information about the ajax support in the given source format
146 * This function calls function callback_FORMATNAME_ajax_support() if it exists
148 * The returned object's property (boolean)capable indicates that
149 * the course format supports Moodle course ajax features.
150 * The property (array)testedbrowsers can be used as a parameter for {@link ajaxenabled()}.
154 public function supports_ajax() {
155 // set up default values
156 $ajaxsupport = parent
::supports_ajax();
158 // get the information from the course format library
159 $featurefunction = 'callback_'.$this->format
.'_ajax_support';
160 if (function_exists($featurefunction)) {
161 $formatsupport = $featurefunction();
162 if (isset($formatsupport->capable
)) {
163 $ajaxsupport->capable
= $formatsupport->capable
;
165 if (is_array($formatsupport->testedbrowsers
)) {
166 $ajaxsupport->testedbrowsers
= $formatsupport->testedbrowsers
;
173 * Loads all of the course sections into the navigation
175 * First this function calls callback_FORMATNAME_display_content() if it exists to check
176 * if the navigation should be extended at all
178 * Then it calls function callback_FORMATNAME_load_content() if it exists to actually extend
181 * By default the parent method is called
183 * @param global_navigation $navigation
184 * @param navigation_node $node The course node within the navigation
186 public function extend_course_navigation($navigation, navigation_node
$node) {
188 // if course format displays section on separate pages and we are on course/view.php page
189 // and the section parameter is specified, make sure this section is expanded in
191 if ($navigation->includesectionnum
=== false) {
192 $selectedsection = optional_param('section', null, PARAM_INT
);
193 if ($selectedsection !== null && (!defined('AJAX_SCRIPT') || AJAX_SCRIPT
== '0') &&
194 $PAGE->url
->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE
)) {
195 $navigation->includesectionnum
= $selectedsection;
199 // check if there are callbacks to extend course navigation
200 $displayfunc = 'callback_'.$this->format
.'_display_content';
201 if (function_exists($displayfunc) && !$displayfunc()) {
204 $featurefunction = 'callback_'.$this->format
.'_load_content';
205 if (function_exists($featurefunction) && ($course = $this->get_course())) {
206 $featurefunction($navigation, $course, $node);
208 parent
::extend_course_navigation($navigation, $node);
213 * Custom action after section has been moved in AJAX mode
215 * Used in course/rest.php
217 * This function calls function callback_FORMATNAME_ajax_section_move() if it exists
219 * @return array This will be passed in ajax respose
221 function ajax_section_move() {
222 $featurefunction = 'callback_'.$this->format
.'_ajax_section_move';
223 if (function_exists($featurefunction) && ($course = $this->get_course())) {
224 return $featurefunction($course);
226 return parent
::ajax_section_move();
231 * Returns the list of blocks to be automatically added for the newly created course
233 * This function checks the existence of the file config.php in the course format folder.
234 * If file exists and contains the code
235 * $format['defaultblocks'] = 'leftblock1,leftblock2:rightblock1,rightblock2';
236 * these blocks are used, otherwise parent function is called
238 * @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
239 * each of values is an array of block names (for left and right side columns)
241 public function get_default_blocks() {
243 $formatconfig = $CFG->dirroot
.'/course/format/'.$this->format
.'/config.php';
244 $format = array(); // initialize array in external file
245 if (is_readable($formatconfig)) {
246 include($formatconfig);
248 if (!empty($format['defaultblocks'])) {
249 return blocks_parse_default_blocks_list($format['defaultblocks']);
251 return parent
::get_default_blocks();
255 * Definitions of the additional options that this course format uses for course
257 * By default course formats have the options that existed in Moodle 2.3:
262 * @param bool $foreditform
263 * @return array of options
265 public function course_format_options($foreditform = false) {
266 static $courseformatoptions = false;
267 if ($courseformatoptions === false) {
268 $courseconfig = get_config('moodlecourse');
269 $courseformatoptions = array(
270 'numsections' => array(
271 'default' => $courseconfig->numsections
,
274 'hiddensections' => array(
275 'default' => $courseconfig->hiddensections
,
278 'coursedisplay' => array(
279 'default' => $courseconfig->coursedisplay
,
284 if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
285 $courseconfig = get_config('moodlecourse');
286 $sectionmenu = array();
287 for ($i = 0; $i <= $courseconfig->maxsections
; $i++
) {
288 $sectionmenu[$i] = "$i";
290 $courseformatoptionsedit = array(
291 'numsections' => array(
292 'label' => new lang_string('numberweeks'),
293 'element_type' => 'select',
294 'element_attributes' => array($sectionmenu),
296 'hiddensections' => array(
297 'label' => new lang_string('hiddensections'),
298 'help' => 'hiddensections',
299 'help_component' => 'moodle',
300 'element_type' => 'select',
301 'element_attributes' => array(
303 0 => new lang_string('hiddensectionscollapsed'),
304 1 => new lang_string('hiddensectionsinvisible')
308 'coursedisplay' => array(
309 'label' => new lang_string('coursedisplay'),
310 'element_type' => 'select',
311 'element_attributes' => array(
313 COURSE_DISPLAY_SINGLEPAGE
=> new lang_string('coursedisplay_single'),
314 COURSE_DISPLAY_MULTIPAGE
=> new lang_string('coursedisplay_multi')
317 'help' => 'coursedisplay',
318 'help_component' => 'moodle',
321 $courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
323 return $courseformatoptions;
327 * Updates format options for a course
329 * Legacy course formats may assume that course format options
330 * ('coursedisplay', 'numsections' and 'hiddensections') are shared between formats.
331 * Therefore we make sure to copy them from the previous format
333 * @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data
334 * @param stdClass $oldcourse if this function is called from {@link update_course()}
335 * this object contains information about the course before update
336 * @return bool whether there were any changes to the options values
338 public function update_course_format_options($data, $oldcourse = null) {
340 if ($oldcourse !== null) {
341 $data = (array)$data;
342 $oldcourse = (array)$oldcourse;
343 $options = $this->course_format_options();
344 foreach ($options as $key => $unused) {
345 if (!array_key_exists($key, $data)) {
346 if (array_key_exists($key, $oldcourse)) {
347 $data[$key] = $oldcourse[$key];
348 } else if ($key === 'numsections') {
349 // If previous format does not have the field 'numsections' and this one does,
350 // and $data['numsections'] is not set fill it with the maximum section number from the DB
351 $maxsection = $DB->get_field_sql('SELECT max(section) from {course_sections}
352 WHERE course = ?', array($this->courseid
));
354 // If there are no sections, or just default 0-section, 'numsections' will be set to default
355 $data['numsections'] = $maxsection;
361 return $this->update_format_options($data);