MDL-35770 Include file defining base class in order to work correctly in cron
[moodle.git] / course / format / topics / lib.php
CommitLineData
7d2a0492 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/**
2fb8c9a2 18 * This file contains main class for the course format Topic
7d2a0492 19 *
2fb8c9a2
MG
20 * @since 2.0
21 * @package format_topics
7d2a0492 22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
399ad6bf
MG
26defined('MOODLE_INTERNAL') || die();
27require_once($CFG->dirroot. '/course/format/lib.php');
28
2fb8c9a2
MG
29/**
30 * Main class for the Topics course format
31 *
32 * @package format_topics
33 * @copyright 2012 Marina Glancy
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class format_topics extends format_base {
37
38 /**
39 * Returns true if this course format uses sections
40 *
2fb8c9a2
MG
41 * @return bool
42 */
43 public function uses_sections() {
222cd9c9 44 return true;
2fb8c9a2
MG
45 }
46
47 /**
48 * Returns the display name of the given section that the course prefers.
49 *
222cd9c9 50 * Use section name is specified by user. Otherwise use default ("Topic #")
2fb8c9a2
MG
51 *
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"
54 */
55 public function get_section_name($section) {
222cd9c9
MG
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');
62 } else {
63 return get_string('topic').' '.$section->section;
2fb8c9a2 64 }
2fb8c9a2
MG
65 }
66
67 /**
68 * The URL to use for the specified course (with section)
69 *
2fb8c9a2
MG
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
76 */
77 public function get_view_url($section, $options = array()) {
2fb8c9a2
MG
78 $course = $this->get_course();
79 $url = new moodle_url('/course/view.php', array('id' => $course->id));
80
81 $sr = null;
82 if (array_key_exists('sr', $options)) {
83 $sr = $options['sr'];
84 }
85 if (is_object($section)) {
86 $sectionno = $section->section;
87 } else {
88 $sectionno = $section;
89 }
90 if ($sectionno !== null) {
91 if ($sr !== null) {
92 if ($sr) {
93 $usercoursedisplay = COURSE_DISPLAY_MULTIPAGE;
94 $sectionno = $sr;
95 } else {
96 $usercoursedisplay = COURSE_DISPLAY_SINGLEPAGE;
97 }
98 } else {
99 $usercoursedisplay = $course->coursedisplay;
100 }
101 if ($sectionno != 0 && $usercoursedisplay == COURSE_DISPLAY_MULTIPAGE) {
102 $url->param('section', $sectionno);
103 } else {
104 if (!empty($options['navigation'])) {
105 return null;
106 }
107 $url->set_anchor('section-'.$sectionno);
108 }
109 }
110 return $url;
111 }
112
113 /**
114 * Returns the information about the ajax support in the given source format
115 *
2fb8c9a2
MG
116 * The returned object's property (boolean)capable indicates that
117 * the course format supports Moodle course ajax features.
118 * The property (array)testedbrowsers can be used as a parameter for {@link ajaxenabled()}.
119 *
120 * @return stdClass
121 */
122 public function supports_ajax() {
222cd9c9
MG
123 $ajaxsupport = new stdClass();
124 $ajaxsupport->capable = true;
125 $ajaxsupport->testedbrowsers = array('MSIE' => 6.0, 'Gecko' => 20061111, 'Safari' => 531, 'Chrome' => 6.0);
2fb8c9a2
MG
126 return $ajaxsupport;
127 }
128
129 /**
130 * Loads all of the course sections into the navigation
131 *
2fb8c9a2
MG
132 * @param global_navigation $navigation
133 * @param navigation_node $node The course node within the navigation
134 */
135 public function extend_course_navigation($navigation, navigation_node $node) {
136 global $PAGE;
222cd9c9 137 // if section is specified in course/view.php, make sure it is expanded in navigation
2fb8c9a2
MG
138 if ($navigation->includesectionnum === false) {
139 $selectedsection = optional_param('section', null, PARAM_INT);
140 if ($selectedsection !== null && (!defined('AJAX_SCRIPT') || AJAX_SCRIPT == '0') &&
141 $PAGE->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
142 $navigation->includesectionnum = $selectedsection;
143 }
144 }
145
146 // check if there are callbacks to extend course navigation
222cd9c9 147 parent::extend_course_navigation($navigation, $node);
2fb8c9a2
MG
148 }
149
150 /**
151 * Custom action after section has been moved in AJAX mode
152 *
153 * Used in course/rest.php
154 *
2fb8c9a2
MG
155 * @return array This will be passed in ajax respose
156 */
157 function ajax_section_move() {
222cd9c9
MG
158 global $PAGE;
159 $titles = array();
160 $course = $this->get_course();
161 $modinfo = get_fast_modinfo($course);
162 $renderer = $this->get_renderer($PAGE);
163 if ($renderer && ($sections = $modinfo->get_section_info_all())) {
164 foreach ($sections as $number => $section) {
165 $titles[$number] = $renderer->section_title($section, $course);
166 }
2fb8c9a2 167 }
222cd9c9 168 return array('sectiontitles' => $titles, 'action' => 'move');
2fb8c9a2
MG
169 }
170
171 /**
172 * Returns the list of blocks to be automatically added for the newly created course
173 *
2fb8c9a2
MG
174 * @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
175 * each of values is an array of block names (for left and right side columns)
176 */
177 public function get_default_blocks() {
222cd9c9
MG
178 return array(
179 BLOCK_POS_LEFT => array(),
180 BLOCK_POS_RIGHT => array('search_forums', 'news_items', 'calendar_upcoming', 'recent_activity')
181 );
2fb8c9a2
MG
182 }
183
184 /**
185 * Definitions of the additional options that this course format uses for course
186 *
222cd9c9 187 * Topics format uses the following options:
2fb8c9a2
MG
188 * - coursedisplay
189 * - numsections
190 * - hiddensections
191 *
192 * @param bool $foreditform
193 * @return array of options
194 */
195 public function course_format_options($foreditform = false) {
196 static $courseformatoptions = false;
197 if ($courseformatoptions === false) {
198 $courseconfig = get_config('moodlecourse');
199 $courseformatoptions = array(
200 'numsections' => array(
201 'default' => $courseconfig->numsections,
202 'type' => PARAM_INT,
203 ),
204 'hiddensections' => array(
205 'default' => $courseconfig->hiddensections,
206 'type' => PARAM_INT,
207 ),
208 'coursedisplay' => array(
209 'default' => $courseconfig->coursedisplay,
210 'type' => PARAM_INT,
211 ),
212 );
213 }
214 if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
215 $courseconfig = get_config('moodlecourse');
216 $sectionmenu = array();
217 for ($i = 0; $i <= $courseconfig->maxsections; $i++) {
218 $sectionmenu[$i] = "$i";
219 }
220 $courseformatoptionsedit = array(
221 'numsections' => array(
222 'label' => new lang_string('numberweeks'),
223 'element_type' => 'select',
224 'element_attributes' => array($sectionmenu),
225 ),
226 'hiddensections' => array(
227 'label' => new lang_string('hiddensections'),
228 'help' => 'hiddensections',
229 'help_component' => 'moodle',
230 'element_type' => 'select',
231 'element_attributes' => array(
232 array(
233 0 => new lang_string('hiddensectionscollapsed'),
234 1 => new lang_string('hiddensectionsinvisible')
235 )
236 ),
237 ),
238 'coursedisplay' => array(
239 'label' => new lang_string('coursedisplay'),
240 'element_type' => 'select',
241 'element_attributes' => array(
242 array(
243 COURSE_DISPLAY_SINGLEPAGE => new lang_string('coursedisplay_single'),
244 COURSE_DISPLAY_MULTIPAGE => new lang_string('coursedisplay_multi')
245 )
246 ),
247 'help' => 'coursedisplay',
248 'help_component' => 'moodle',
249 )
250 );
251 $courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
252 }
253 return $courseformatoptions;
254 }
255
256 /**
257 * Updates format options for a course
258 *
222cd9c9
MG
259 * In case if course format was changed to 'topics', we try to copy options
260 * 'coursedisplay', 'numsections' and 'hiddensections' from the previous format.
261 * If previous course format did not have 'numsections' option, we populate it with the
262 * current number of sections
2fb8c9a2
MG
263 *
264 * @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data
265 * @param stdClass $oldcourse if this function is called from {@link update_course()}
266 * this object contains information about the course before update
267 * @return bool whether there were any changes to the options values
268 */
269 public function update_course_format_options($data, $oldcourse = null) {
270 if ($oldcourse !== null) {
271 $data = (array)$data;
272 $oldcourse = (array)$oldcourse;
273 $options = $this->course_format_options();
274 foreach ($options as $key => $unused) {
275 if (!array_key_exists($key, $data)) {
276 if (array_key_exists($key, $oldcourse)) {
277 $data[$key] = $oldcourse[$key];
278 } else if ($key === 'numsections') {
222cd9c9
MG
279 // If previous format does not have the field 'numsections'
280 // and $data['numsections'] is not set,
281 // we fill it with the maximum section number from the DB
2fb8c9a2
MG
282 $maxsection = $DB->get_field_sql('SELECT max(section) from {course_sections}
283 WHERE course = ?', array($this->courseid));
284 if ($maxsection) {
285 // If there are no sections, or just default 0-section, 'numsections' will be set to default
286 $data['numsections'] = $maxsection;
287 }
288 }
289 }
290 }
291 }
292 return $this->update_format_options($data);
293 }
294}