Merge branch 'MDL-53281-master' of git://github.com/merrill-oakland/moodle
[moodle.git] / course / format / formatlegacy.php
blob665f583382183540e1ea732d06cd21a002656180
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 * 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;
27 /**
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 {
36 /**
37 * Returns true if this course format uses sections
39 * This function calls function callback_FORMATNAME_uses_sections() if it exists
41 * @return bool
43 public function uses_sections() {
44 global $CFG;
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();
50 return false;
53 /**
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);
72 /**
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;
90 } else {
91 $sectionnum = $section;
93 if ($sectionnum) {
94 $url = $featurefunction($course, $sectionnum);
95 if ($url || !empty($options['navigation'])) {
96 return $url;
101 // if function is not defined
102 if (!$this->uses_sections() ||
103 !array_key_exists('coursedisplay', $this->course_format_options())) {
104 // default behaviour
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));
111 $sr = null;
112 if (array_key_exists('sr', $options)) {
113 $sr = $options['sr'];
115 if (is_object($section)) {
116 $sectionno = $section->section;
117 } else {
118 $sectionno = $section;
120 if ($sectionno !== null) {
121 if ($sr !== null) {
122 if ($sr) {
123 $usercoursedisplay = COURSE_DISPLAY_MULTIPAGE;
124 $sectionno = $sr;
125 } else {
126 $usercoursedisplay = COURSE_DISPLAY_SINGLEPAGE;
128 } else {
129 $usercoursedisplay = $course->coursedisplay;
131 if ($sectionno != 0 && $usercoursedisplay == COURSE_DISPLAY_MULTIPAGE) {
132 $url->param('section', $sectionno);
133 } else {
134 if (!empty($options['navigation'])) {
135 return null;
137 $url->set_anchor('section-'.$sectionno);
140 return $url;
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.
151 * @return stdClass
153 public function supports_ajax() {
154 // set up default values
155 $ajaxsupport = parent::supports_ajax();
157 // get the information from the course format library
158 $featurefunction = 'callback_'.$this->format.'_ajax_support';
159 if (function_exists($featurefunction)) {
160 $formatsupport = $featurefunction();
161 if (isset($formatsupport->capable)) {
162 $ajaxsupport->capable = $formatsupport->capable;
165 return $ajaxsupport;
169 * Loads all of the course sections into the navigation
171 * First this function calls callback_FORMATNAME_display_content() if it exists to check
172 * if the navigation should be extended at all
174 * Then it calls function callback_FORMATNAME_load_content() if it exists to actually extend
175 * navigation
177 * By default the parent method is called
179 * @param global_navigation $navigation
180 * @param navigation_node $node The course node within the navigation
182 public function extend_course_navigation($navigation, navigation_node $node) {
183 global $PAGE;
184 // if course format displays section on separate pages and we are on course/view.php page
185 // and the section parameter is specified, make sure this section is expanded in
186 // navigation
187 if ($navigation->includesectionnum === false) {
188 $selectedsection = optional_param('section', null, PARAM_INT);
189 if ($selectedsection !== null && (!defined('AJAX_SCRIPT') || AJAX_SCRIPT == '0') &&
190 $PAGE->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
191 $navigation->includesectionnum = $selectedsection;
195 // check if there are callbacks to extend course navigation
196 $displayfunc = 'callback_'.$this->format.'_display_content';
197 if (function_exists($displayfunc) && !$displayfunc()) {
198 return;
200 $featurefunction = 'callback_'.$this->format.'_load_content';
201 if (function_exists($featurefunction) && ($course = $this->get_course())) {
202 $featurefunction($navigation, $course, $node);
203 } else {
204 parent::extend_course_navigation($navigation, $node);
209 * Custom action after section has been moved in AJAX mode
211 * Used in course/rest.php
213 * This function calls function callback_FORMATNAME_ajax_section_move() if it exists
215 * @return array This will be passed in ajax respose
217 function ajax_section_move() {
218 $featurefunction = 'callback_'.$this->format.'_ajax_section_move';
219 if (function_exists($featurefunction) && ($course = $this->get_course())) {
220 return $featurefunction($course);
221 } else {
222 return parent::ajax_section_move();
227 * Returns the list of blocks to be automatically added for the newly created course
229 * This function checks the existence of the file config.php in the course format folder.
230 * If file exists and contains the code
231 * $format['defaultblocks'] = 'leftblock1,leftblock2:rightblock1,rightblock2';
232 * these blocks are used, otherwise parent function is called
234 * @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
235 * each of values is an array of block names (for left and right side columns)
237 public function get_default_blocks() {
238 global $CFG;
239 $formatconfig = $CFG->dirroot.'/course/format/'.$this->format.'/config.php';
240 $format = array(); // initialize array in external file
241 if (is_readable($formatconfig)) {
242 include($formatconfig);
244 if (!empty($format['defaultblocks'])) {
245 return blocks_parse_default_blocks_list($format['defaultblocks']);
247 return parent::get_default_blocks();
251 * Definitions of the additional options that this course format uses for course
253 * By default course formats have the options that existed in Moodle 2.3:
254 * - coursedisplay
255 * - numsections
256 * - hiddensections
258 * @param bool $foreditform
259 * @return array of options
261 public function course_format_options($foreditform = false) {
262 static $courseformatoptions = false;
263 if ($courseformatoptions === false) {
264 $courseconfig = get_config('moodlecourse');
265 $courseformatoptions = array(
266 'numsections' => array(
267 'default' => $courseconfig->numsections,
268 'type' => PARAM_INT,
270 'hiddensections' => array(
271 'default' => $courseconfig->hiddensections,
272 'type' => PARAM_INT,
274 'coursedisplay' => array(
275 'default' => $courseconfig->coursedisplay,
276 'type' => PARAM_INT,
280 if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
281 $courseconfig = get_config('moodlecourse');
282 $sectionmenu = array();
283 for ($i = 0; $i <= $courseconfig->maxsections; $i++) {
284 $sectionmenu[$i] = "$i";
286 $courseformatoptionsedit = array(
287 'numsections' => array(
288 'label' => new lang_string('numberweeks'),
289 'element_type' => 'select',
290 'element_attributes' => array($sectionmenu),
292 'hiddensections' => array(
293 'label' => new lang_string('hiddensections'),
294 'help' => 'hiddensections',
295 'help_component' => 'moodle',
296 'element_type' => 'select',
297 'element_attributes' => array(
298 array(
299 0 => new lang_string('hiddensectionscollapsed'),
300 1 => new lang_string('hiddensectionsinvisible')
304 'coursedisplay' => array(
305 'label' => new lang_string('coursedisplay'),
306 'element_type' => 'select',
307 'element_attributes' => array(
308 array(
309 COURSE_DISPLAY_SINGLEPAGE => new lang_string('coursedisplay_single'),
310 COURSE_DISPLAY_MULTIPAGE => new lang_string('coursedisplay_multi')
313 'help' => 'coursedisplay',
314 'help_component' => 'moodle',
317 $courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
319 return $courseformatoptions;
323 * Updates format options for a course
325 * Legacy course formats may assume that course format options
326 * ('coursedisplay', 'numsections' and 'hiddensections') are shared between formats.
327 * Therefore we make sure to copy them from the previous format
329 * @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data
330 * @param stdClass $oldcourse if this function is called from {@link update_course()}
331 * this object contains information about the course before update
332 * @return bool whether there were any changes to the options values
334 public function update_course_format_options($data, $oldcourse = null) {
335 global $DB;
336 if ($oldcourse !== null) {
337 $data = (array)$data;
338 $oldcourse = (array)$oldcourse;
339 $options = $this->course_format_options();
340 foreach ($options as $key => $unused) {
341 if (!array_key_exists($key, $data)) {
342 if (array_key_exists($key, $oldcourse)) {
343 $data[$key] = $oldcourse[$key];
344 } else if ($key === 'numsections') {
345 // If previous format does not have the field 'numsections' and this one does,
346 // and $data['numsections'] is not set fill it with the maximum section number from the DB
347 $maxsection = $DB->get_field_sql('SELECT max(section) from {course_sections}
348 WHERE course = ?', array($this->courseid));
349 if ($maxsection) {
350 // If there are no sections, or just default 0-section, 'numsections' will be set to default
351 $data['numsections'] = $maxsection;
357 return $this->update_format_options($data);