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 * This file contains main class for the course format Topic
21 * @package format_topics
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->dirroot. '/course/format/lib.php');
30 * Main class for the Topics course format
32 * @package format_topics
33 * @copyright 2012 Marina Glancy
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class format_topics extends format_base {
39 * Returns true if this course format uses sections
43 public function uses_sections() {
48 * Returns the display name of the given section that the course prefers.
50 * Use section name is specified by user. Otherwise use default ("Topic #")
52 * @param int|stdClass $section Section object from database or just field section.section
53 * @return string Display name that the course format prefers, e.g. "Topic 2"
55 public function get_section_name($section) {
56 $section = $this->get_section($section);
57 if ((string)$section->name !== '') {
58 return format_string($section->name, true,
59 array('context' => context_course::instance($this->courseid)));
60 } else if ($section->section == 0) {
61 return get_string('section0name', 'format_topics');
63 return get_string('topic').' '.$section->section;
68 * The URL to use for the specified course (with section)
70 * @param int|stdClass $section Section object from database or just field course_sections.section
71 * if omitted the course view page is returned
72 * @param array $options options for view URL. At the moment core uses:
73 * 'navigation' (bool) if true and section has no separate page, the function returns null
74 * 'sr' (int) used by multipage formats to specify to which section to return
75 * @return null|moodle_url
77 public function get_view_url($section, $options = array()) {
78 $course = $this->get_course();
79 $url = new moodle_url('/course/view.php', array('id' => $course->id));
82 if (array_key_exists('sr', $options)) {
85 if (is_object($section)) {
86 $sectionno = $section->section;
88 $sectionno = $section;
90 if ($sectionno !== null) {
93 $usercoursedisplay = COURSE_DISPLAY_MULTIPAGE;
96 $usercoursedisplay = COURSE_DISPLAY_SINGLEPAGE;
99 $usercoursedisplay = $course->coursedisplay;
101 if ($sectionno != 0 && $usercoursedisplay == COURSE_DISPLAY_MULTIPAGE) {
102 $url->param('section', $sectionno);
104 if (!empty($options['navigation'])) {
107 $url->set_anchor('section-'.$sectionno);
114 * Returns the information about the ajax support in the given source format
116 * The returned object's property (boolean)capable indicates that
117 * the course format supports Moodle course ajax features.
121 public function supports_ajax() {
122 $ajaxsupport = new stdClass();
123 $ajaxsupport->capable = true;
128 * Loads all of the course sections into the navigation
130 * @param global_navigation $navigation
131 * @param navigation_node $node The course node within the navigation
133 public function extend_course_navigation($navigation, navigation_node $node) {
135 // if section is specified in course/view.php, make sure it is expanded in navigation
136 if ($navigation->includesectionnum === false) {
137 $selectedsection = optional_param('section', null, PARAM_INT);
138 if ($selectedsection !== null && (!defined('AJAX_SCRIPT') || AJAX_SCRIPT == '0') &&
139 $PAGE->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
140 $navigation->includesectionnum = $selectedsection;
144 // check if there are callbacks to extend course navigation
145 parent::extend_course_navigation($navigation, $node);
149 * Custom action after section has been moved in AJAX mode
151 * Used in course/rest.php
153 * @return array This will be passed in ajax respose
155 function ajax_section_move() {
158 $course = $this->get_course();
159 $modinfo = get_fast_modinfo($course);
160 $renderer = $this->get_renderer($PAGE);
161 if ($renderer && ($sections = $modinfo->get_section_info_all())) {
162 foreach ($sections as $number => $section) {
163 $titles[$number] = $renderer->section_title($section, $course);
166 return array('sectiontitles' => $titles, 'action' => 'move');
170 * Returns the list of blocks to be automatically added for the newly created course
172 * @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
173 * each of values is an array of block names (for left and right side columns)
175 public function get_default_blocks() {
177 BLOCK_POS_LEFT => array(),
178 BLOCK_POS_RIGHT => array('search_forums', 'news_items', 'calendar_upcoming', 'recent_activity')
183 * Definitions of the additional options that this course format uses for course
185 * Topics format uses the following options:
190 * @param bool $foreditform
191 * @return array of options
193 public function course_format_options($foreditform = false) {
194 static $courseformatoptions = false;
195 if ($courseformatoptions === false) {
196 $courseconfig = get_config('moodlecourse');
197 $courseformatoptions = array(
198 'numsections' => array(
199 'default' => $courseconfig->numsections,
202 'hiddensections' => array(
203 'default' => $courseconfig->hiddensections,
206 'coursedisplay' => array(
207 'default' => $courseconfig->coursedisplay,
212 if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
213 $courseconfig = get_config('moodlecourse');
214 $max = $courseconfig->maxsections;
215 if (!isset($max) || !is_numeric($max)) {
218 $sectionmenu = array();
219 for ($i = 0; $i <= $max; $i++) {
220 $sectionmenu[$i] = "$i";
222 $courseformatoptionsedit = array(
223 'numsections' => array(
224 'label' => new lang_string('numberweeks'),
225 'element_type' => 'select',
226 'element_attributes' => array($sectionmenu),
228 'hiddensections' => array(
229 'label' => new lang_string('hiddensections'),
230 'help' => 'hiddensections',
231 'help_component' => 'moodle',
232 'element_type' => 'select',
233 'element_attributes' => array(
235 0 => new lang_string('hiddensectionscollapsed'),
236 1 => new lang_string('hiddensectionsinvisible')
240 'coursedisplay' => array(
241 'label' => new lang_string('coursedisplay'),
242 'element_type' => 'select',
243 'element_attributes' => array(
245 COURSE_DISPLAY_SINGLEPAGE => new lang_string('coursedisplay_single'),
246 COURSE_DISPLAY_MULTIPAGE => new lang_string('coursedisplay_multi')
249 'help' => 'coursedisplay',
250 'help_component' => 'moodle',
253 $courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
255 return $courseformatoptions;
259 * Adds format options elements to the course/section edit form.
261 * This function is called from {@link course_edit_form::definition_after_data()}.
263 * @param MoodleQuickForm $mform form the elements are added to.
264 * @param bool $forsection 'true' if this is a section edit form, 'false' if this is course edit form.
265 * @return array array of references to the added form elements.
267 public function create_edit_form_elements(&$mform, $forsection = false) {
268 $elements = parent::create_edit_form_elements($mform, $forsection);
270 // Increase the number of sections combo box values if the user has increased the number of sections
271 // using the icon on the course page beyond course 'maxsections' or course 'maxsections' has been
272 // reduced below the number of sections already set for the course on the site administration course
273 // defaults page. This is so that the number of sections is not reduced leaving unintended orphaned
274 // activities / resources.
276 $maxsections = get_config('moodlecourse', 'maxsections');
277 $numsections = $mform->getElementValue('numsections');
278 $numsections = $numsections[0];
279 if ($numsections > $maxsections) {
280 $element = $mform->getElement('numsections');
281 for ($i = $maxsections+1; $i <= $numsections; $i++) {
282 $element->addOption("$i", $i);
290 * Updates format options for a course
292 * In case if course format was changed to 'topics', we try to copy options
293 * 'coursedisplay', 'numsections' and 'hiddensections' from the previous format.
294 * If previous course format did not have 'numsections' option, we populate it with the
295 * current number of sections
297 * @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data
298 * @param stdClass $oldcourse if this function is called from {@link update_course()}
299 * this object contains information about the course before update
300 * @return bool whether there were any changes to the options values
302 public function update_course_format_options($data, $oldcourse = null) {
304 if ($oldcourse !== null) {
305 $data = (array)$data;
306 $oldcourse = (array)$oldcourse;
307 $options = $this->course_format_options();
308 foreach ($options as $key => $unused) {
309 if (!array_key_exists($key, $data)) {
310 if (array_key_exists($key, $oldcourse)) {
311 $data[$key] = $oldcourse[$key];
312 } else if ($key === 'numsections') {
313 // If previous format does not have the field 'numsections'
314 // and $data['numsections'] is not set,
315 // we fill it with the maximum section number from the DB
316 $maxsection = $DB->get_field_sql('SELECT max(section) from {course_sections}
317 WHERE course = ?', array($this->courseid));
319 // If there are no sections, or just default 0-section, 'numsections' will be set to default
320 $data['numsections'] = $maxsection;
326 return $this->update_format_options($data);