1 |
efrain |
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 |
/**
|
|
|
18 |
* This file contains main class for Topics course format.
|
|
|
19 |
*
|
|
|
20 |
* @since Moodle 2.0
|
|
|
21 |
* @package format_topics
|
|
|
22 |
* @copyright 2009 Sam Hemelryk
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
defined('MOODLE_INTERNAL') || die();
|
|
|
27 |
require_once($CFG->dirroot. '/course/format/lib.php');
|
|
|
28 |
|
|
|
29 |
use core\output\inplace_editable;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Main class for the Topics course format.
|
|
|
33 |
*
|
|
|
34 |
* @package format_topics
|
|
|
35 |
* @copyright 2012 Marina Glancy
|
|
|
36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
37 |
*/
|
|
|
38 |
class format_topics extends core_courseformat\base {
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Returns true if this course format uses sections.
|
|
|
42 |
*
|
|
|
43 |
* @return bool
|
|
|
44 |
*/
|
|
|
45 |
public function uses_sections() {
|
|
|
46 |
return true;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
public function uses_course_index() {
|
|
|
50 |
return true;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
public function uses_indentation(): bool {
|
|
|
54 |
return (get_config('format_topics', 'indentation')) ? true : false;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Returns the display name of the given section that the course prefers.
|
|
|
59 |
*
|
|
|
60 |
* Use section name is specified by user. Otherwise use default ("Topic #").
|
|
|
61 |
*
|
|
|
62 |
* @param int|stdClass $section Section object from database or just field section.section
|
|
|
63 |
* @return string Display name that the course format prefers, e.g. "Topic 2"
|
|
|
64 |
*/
|
|
|
65 |
public function get_section_name($section) {
|
|
|
66 |
$section = $this->get_section($section);
|
|
|
67 |
if ((string)$section->name !== '') {
|
|
|
68 |
return format_string($section->name, true,
|
|
|
69 |
['context' => context_course::instance($this->courseid)]);
|
|
|
70 |
} else {
|
|
|
71 |
return $this->get_default_section_name($section);
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Returns the default section name for the topics course format.
|
|
|
77 |
*
|
|
|
78 |
* If the section number is 0, it will use the string with key = section0name from the course format's lang file.
|
|
|
79 |
* If the section number is not 0, it will consistently return the name 'newsection', disregarding the specific section number.
|
|
|
80 |
*
|
|
|
81 |
* @param int|stdClass $section Section object from database or just field course_sections section
|
|
|
82 |
* @return string The default value for the section name.
|
|
|
83 |
*/
|
|
|
84 |
public function get_default_section_name($section) {
|
|
|
85 |
$section = $this->get_section($section);
|
|
|
86 |
if ($section->sectionnum == 0) {
|
|
|
87 |
return get_string('section0name', 'format_topics');
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
return get_string('newsection', 'format_topics');
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Generate the title for this section page.
|
|
|
95 |
*
|
|
|
96 |
* @return string the page title
|
|
|
97 |
*/
|
|
|
98 |
public function page_title(): string {
|
|
|
99 |
return get_string('sectionoutline');
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* The URL to use for the specified course (with section).
|
|
|
104 |
*
|
|
|
105 |
* @param int|stdClass $section Section object from database or just field course_sections.section
|
|
|
106 |
* if omitted the course view page is returned
|
|
|
107 |
* @param array $options options for view URL. At the moment core uses:
|
|
|
108 |
* 'navigation' (bool) if true and section not empty, the function returns section page; otherwise, it returns course page.
|
|
|
109 |
* 'sr' (int) used by course formats to specify to which section to return
|
|
|
110 |
* @return null|moodle_url
|
|
|
111 |
*/
|
|
|
112 |
public function get_view_url($section, $options = []) {
|
|
|
113 |
$course = $this->get_course();
|
|
|
114 |
if (array_key_exists('sr', $options) && !is_null($options['sr'])) {
|
|
|
115 |
$sectionno = $options['sr'];
|
|
|
116 |
} else if (is_object($section)) {
|
|
|
117 |
$sectionno = $section->section;
|
|
|
118 |
} else {
|
|
|
119 |
$sectionno = $section;
|
|
|
120 |
}
|
|
|
121 |
if ((!empty($options['navigation']) || array_key_exists('sr', $options)) && $sectionno !== null) {
|
|
|
122 |
// Display section on separate page.
|
|
|
123 |
$sectioninfo = $this->get_section($sectionno);
|
|
|
124 |
return new moodle_url('/course/section.php', ['id' => $sectioninfo->id]);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
return new moodle_url('/course/view.php', ['id' => $course->id]);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Returns the information about the ajax support in the given source format.
|
|
|
132 |
*
|
|
|
133 |
* The returned object's property (boolean)capable indicates that
|
|
|
134 |
* the course format supports Moodle course ajax features.
|
|
|
135 |
*
|
|
|
136 |
* @return stdClass
|
|
|
137 |
*/
|
|
|
138 |
public function supports_ajax() {
|
|
|
139 |
$ajaxsupport = new stdClass();
|
|
|
140 |
$ajaxsupport->capable = true;
|
|
|
141 |
return $ajaxsupport;
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
public function supports_components() {
|
|
|
145 |
return true;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* Loads all of the course sections into the navigation.
|
|
|
150 |
*
|
|
|
151 |
* @param global_navigation $navigation
|
|
|
152 |
* @param navigation_node $node The course node within the navigation
|
|
|
153 |
* @return void
|
|
|
154 |
*/
|
|
|
155 |
public function extend_course_navigation($navigation, navigation_node $node) {
|
|
|
156 |
global $PAGE;
|
|
|
157 |
// If section is specified in course/view.php, make sure it is expanded in navigation.
|
|
|
158 |
if ($navigation->includesectionnum === false) {
|
|
|
159 |
$selectedsection = optional_param('section', null, PARAM_INT);
|
|
|
160 |
if ($selectedsection !== null && (!defined('AJAX_SCRIPT') || AJAX_SCRIPT == '0') &&
|
|
|
161 |
$PAGE->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
|
|
|
162 |
$navigation->includesectionnum = $selectedsection;
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
// Check if there are callbacks to extend course navigation.
|
|
|
167 |
parent::extend_course_navigation($navigation, $node);
|
|
|
168 |
|
|
|
169 |
// We want to remove the general section if it is empty.
|
|
|
170 |
$modinfo = get_fast_modinfo($this->get_course());
|
|
|
171 |
$sections = $modinfo->get_sections();
|
|
|
172 |
if (!isset($sections[0])) {
|
|
|
173 |
// The general section is empty to find the navigation node for it we need to get its ID.
|
|
|
174 |
$section = $modinfo->get_section_info(0);
|
|
|
175 |
$generalsection = $node->get($section->id, navigation_node::TYPE_SECTION);
|
|
|
176 |
if ($generalsection) {
|
|
|
177 |
// We found the node - now remove it.
|
|
|
178 |
$generalsection->remove();
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* Custom action after section has been moved in AJAX mode.
|
|
|
185 |
*
|
|
|
186 |
* Used in course/rest.php
|
|
|
187 |
*
|
|
|
188 |
* @return array This will be passed in ajax respose
|
|
|
189 |
*/
|
|
|
190 |
public function ajax_section_move() {
|
|
|
191 |
global $PAGE;
|
|
|
192 |
$titles = [];
|
|
|
193 |
$course = $this->get_course();
|
|
|
194 |
$modinfo = get_fast_modinfo($course);
|
|
|
195 |
$renderer = $this->get_renderer($PAGE);
|
|
|
196 |
if ($renderer && ($sections = $modinfo->get_section_info_all())) {
|
|
|
197 |
foreach ($sections as $number => $section) {
|
|
|
198 |
$titles[$number] = $renderer->section_title($section, $course);
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
return ['sectiontitles' => $titles, 'action' => 'move'];
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
/**
|
|
|
205 |
* Returns the list of blocks to be automatically added for the newly created course.
|
|
|
206 |
*
|
|
|
207 |
* @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
|
|
|
208 |
* each of values is an array of block names (for left and right side columns)
|
|
|
209 |
*/
|
|
|
210 |
public function get_default_blocks() {
|
|
|
211 |
return [
|
|
|
212 |
BLOCK_POS_LEFT => [],
|
|
|
213 |
BLOCK_POS_RIGHT => [],
|
|
|
214 |
];
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
/**
|
|
|
218 |
* Definitions of the additional options that this course format uses for course.
|
|
|
219 |
*
|
|
|
220 |
* Topics format uses the following options:
|
|
|
221 |
* - coursedisplay
|
|
|
222 |
* - hiddensections
|
|
|
223 |
*
|
|
|
224 |
* @param bool $foreditform
|
|
|
225 |
* @return array of options
|
|
|
226 |
*/
|
|
|
227 |
public function course_format_options($foreditform = false) {
|
|
|
228 |
static $courseformatoptions = false;
|
|
|
229 |
if ($courseformatoptions === false) {
|
|
|
230 |
$courseconfig = get_config('moodlecourse');
|
|
|
231 |
$courseformatoptions = [
|
|
|
232 |
'hiddensections' => [
|
|
|
233 |
'default' => $courseconfig->hiddensections,
|
|
|
234 |
'type' => PARAM_INT,
|
|
|
235 |
],
|
|
|
236 |
'coursedisplay' => [
|
|
|
237 |
'default' => $courseconfig->coursedisplay,
|
|
|
238 |
'type' => PARAM_INT,
|
|
|
239 |
],
|
|
|
240 |
];
|
|
|
241 |
}
|
|
|
242 |
if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
|
|
|
243 |
$courseformatoptionsedit = [
|
|
|
244 |
'hiddensections' => [
|
|
|
245 |
'label' => new lang_string('hiddensections'),
|
|
|
246 |
'help' => 'hiddensections',
|
|
|
247 |
'help_component' => 'moodle',
|
|
|
248 |
'element_type' => 'select',
|
|
|
249 |
'element_attributes' => [
|
|
|
250 |
[
|
|
|
251 |
|
|
|
252 |
1 => new lang_string('hiddensectionsinvisible')
|
|
|
253 |
],
|
|
|
254 |
],
|
|
|
255 |
],
|
|
|
256 |
'coursedisplay' => [
|
|
|
257 |
'label' => new lang_string('coursedisplay'),
|
|
|
258 |
'element_type' => 'select',
|
|
|
259 |
'element_attributes' => [
|
|
|
260 |
[
|
|
|
261 |
COURSE_DISPLAY_SINGLEPAGE => new lang_string('coursedisplay_single'),
|
|
|
262 |
COURSE_DISPLAY_MULTIPAGE => new lang_string('coursedisplay_multi'),
|
|
|
263 |
],
|
|
|
264 |
],
|
|
|
265 |
'help' => 'coursedisplay',
|
|
|
266 |
'help_component' => 'moodle',
|
|
|
267 |
],
|
|
|
268 |
];
|
|
|
269 |
$courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
|
|
|
270 |
}
|
|
|
271 |
return $courseformatoptions;
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
/**
|
|
|
275 |
* Adds format options elements to the course/section edit form.
|
|
|
276 |
*
|
|
|
277 |
* This function is called from {@link course_edit_form::definition_after_data()}.
|
|
|
278 |
*
|
|
|
279 |
* @param MoodleQuickForm $mform form the elements are added to.
|
|
|
280 |
* @param bool $forsection 'true' if this is a section edit form, 'false' if this is course edit form.
|
|
|
281 |
* @return array array of references to the added form elements.
|
|
|
282 |
*/
|
|
|
283 |
public function create_edit_form_elements(&$mform, $forsection = false) {
|
|
|
284 |
global $COURSE;
|
|
|
285 |
$elements = parent::create_edit_form_elements($mform, $forsection);
|
|
|
286 |
|
|
|
287 |
if (!$forsection && (empty($COURSE->id) || $COURSE->id == SITEID)) {
|
|
|
288 |
// Add "numsections" element to the create course form - it will force new course to be prepopulated
|
|
|
289 |
// with empty sections.
|
|
|
290 |
// The "Number of sections" option is no longer available when editing course, instead teachers should
|
|
|
291 |
// delete and add sections when needed.
|
|
|
292 |
$courseconfig = get_config('moodlecourse');
|
|
|
293 |
$max = (int)$courseconfig->maxsections;
|
|
|
294 |
$element = $mform->addElement('select', 'numsections', get_string('numberweeks'), range(0, $max ?: 52));
|
|
|
295 |
$mform->setType('numsections', PARAM_INT);
|
|
|
296 |
if (is_null($mform->getElementValue('numsections'))) {
|
|
|
297 |
$mform->setDefault('numsections', $courseconfig->numsections);
|
|
|
298 |
}
|
|
|
299 |
array_unshift($elements, $element);
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
return $elements;
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
/**
|
|
|
306 |
* Updates format options for a course.
|
|
|
307 |
*
|
|
|
308 |
* In case if course format was changed to 'topics', we try to copy options
|
|
|
309 |
* 'coursedisplay' and 'hiddensections' from the previous format.
|
|
|
310 |
*
|
|
|
311 |
* @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data
|
|
|
312 |
* @param stdClass $oldcourse if this function is called from {@link update_course()}
|
|
|
313 |
* this object contains information about the course before update
|
|
|
314 |
* @return bool whether there were any changes to the options values
|
|
|
315 |
*/
|
|
|
316 |
public function update_course_format_options($data, $oldcourse = null) {
|
|
|
317 |
$data = (array)$data;
|
|
|
318 |
if ($oldcourse !== null) {
|
|
|
319 |
$oldcourse = (array)$oldcourse;
|
|
|
320 |
$options = $this->course_format_options();
|
|
|
321 |
foreach ($options as $key => $unused) {
|
|
|
322 |
if (!array_key_exists($key, $data)) {
|
|
|
323 |
if (array_key_exists($key, $oldcourse)) {
|
|
|
324 |
$data[$key] = $oldcourse[$key];
|
|
|
325 |
}
|
|
|
326 |
}
|
|
|
327 |
}
|
|
|
328 |
}
|
|
|
329 |
return $this->update_format_options($data);
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
/**
|
|
|
333 |
* Whether this format allows to delete sections.
|
|
|
334 |
*
|
|
|
335 |
* Do not call this function directly, instead use {@link course_can_delete_section()}
|
|
|
336 |
*
|
|
|
337 |
* @param int|stdClass|section_info $section
|
|
|
338 |
* @return bool
|
|
|
339 |
*/
|
|
|
340 |
public function can_delete_section($section) {
|
|
|
341 |
return true;
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
/**
|
|
|
345 |
* Indicates whether the course format supports the creation of a news forum.
|
|
|
346 |
*
|
|
|
347 |
* @return bool
|
|
|
348 |
*/
|
|
|
349 |
public function supports_news() {
|
|
|
350 |
return true;
|
|
|
351 |
}
|
|
|
352 |
|
|
|
353 |
/**
|
|
|
354 |
* Returns whether this course format allows the activity to
|
|
|
355 |
* have "triple visibility state" - visible always, hidden on course page but available, hidden.
|
|
|
356 |
*
|
|
|
357 |
* @param stdClass|cm_info $cm course module (may be null if we are displaying a form for adding a module)
|
|
|
358 |
* @param stdClass|section_info $section section where this module is located or will be added to
|
|
|
359 |
* @return bool
|
|
|
360 |
*/
|
|
|
361 |
public function allow_stealth_module_visibility($cm, $section) {
|
|
|
362 |
// Allow the third visibility state inside visible sections or in section 0.
|
|
|
363 |
return !$section->section || $section->visible;
|
|
|
364 |
}
|
|
|
365 |
|
|
|
366 |
/**
|
|
|
367 |
* Callback used in WS core_course_edit_section when teacher performs an AJAX action on a section (show/hide).
|
|
|
368 |
*
|
|
|
369 |
* Access to the course is already validated in the WS but the callback has to make sure
|
|
|
370 |
* that particular action is allowed by checking capabilities
|
|
|
371 |
*
|
|
|
372 |
* Course formats should register.
|
|
|
373 |
*
|
|
|
374 |
* @param section_info|stdClass $section
|
|
|
375 |
* @param string $action
|
|
|
376 |
* @param int $sr
|
|
|
377 |
* @return null|array any data for the Javascript post-processor (must be json-encodeable)
|
|
|
378 |
*/
|
|
|
379 |
public function section_action($section, $action, $sr) {
|
|
|
380 |
global $PAGE;
|
|
|
381 |
|
|
|
382 |
if ($section->section && ($action === 'setmarker' || $action === 'removemarker')) {
|
|
|
383 |
// Format 'topics' allows to set and remove markers in addition to common section actions.
|
|
|
384 |
require_capability('moodle/course:setcurrentsection', context_course::instance($this->courseid));
|
|
|
385 |
course_set_marker($this->courseid, ($action === 'setmarker') ? $section->section : 0);
|
|
|
386 |
return null;
|
|
|
387 |
}
|
|
|
388 |
|
|
|
389 |
// For show/hide actions call the parent method and return the new content for .section_availability element.
|
|
|
390 |
$rv = parent::section_action($section, $action, $sr);
|
|
|
391 |
$renderer = $PAGE->get_renderer('format_topics');
|
|
|
392 |
|
|
|
393 |
if (!($section instanceof section_info)) {
|
|
|
394 |
$modinfo = course_modinfo::instance($this->courseid);
|
|
|
395 |
$section = $modinfo->get_section_info($section->section);
|
|
|
396 |
}
|
|
|
397 |
$elementclass = $this->get_output_classname('content\\section\\availability');
|
|
|
398 |
$availability = new $elementclass($this, $section);
|
|
|
399 |
|
|
|
400 |
$rv['section_availability'] = $renderer->render($availability);
|
|
|
401 |
return $rv;
|
|
|
402 |
}
|
|
|
403 |
|
|
|
404 |
/**
|
|
|
405 |
* Return the plugin configs for external functions.
|
|
|
406 |
*
|
|
|
407 |
* @return array the list of configuration settings
|
|
|
408 |
* @since Moodle 3.5
|
|
|
409 |
*/
|
|
|
410 |
public function get_config_for_external() {
|
|
|
411 |
// Return everything (nothing to hide).
|
|
|
412 |
$formatoptions = $this->get_format_options();
|
|
|
413 |
$formatoptions['indentation'] = get_config('format_topics', 'indentation');
|
|
|
414 |
return $formatoptions;
|
|
|
415 |
}
|
|
|
416 |
|
|
|
417 |
/**
|
|
|
418 |
* Get the required javascript files for the course format.
|
|
|
419 |
*
|
|
|
420 |
* @return array The list of javascript files required by the course format.
|
|
|
421 |
*/
|
|
|
422 |
public function get_required_jsfiles(): array {
|
|
|
423 |
return [];
|
|
|
424 |
}
|
|
|
425 |
}
|
|
|
426 |
|
|
|
427 |
/**
|
|
|
428 |
* Implements callback inplace_editable() allowing to edit values in-place.
|
|
|
429 |
*
|
|
|
430 |
* @param string $itemtype
|
|
|
431 |
* @param int $itemid
|
|
|
432 |
* @param mixed $newvalue
|
|
|
433 |
* @return inplace_editable
|
|
|
434 |
*/
|
|
|
435 |
function format_topics_inplace_editable($itemtype, $itemid, $newvalue) {
|
|
|
436 |
global $DB, $CFG;
|
|
|
437 |
require_once($CFG->dirroot . '/course/lib.php');
|
|
|
438 |
if ($itemtype === 'sectionname' || $itemtype === 'sectionnamenl') {
|
|
|
439 |
$section = $DB->get_record_sql(
|
|
|
440 |
'SELECT s.* FROM {course_sections} s JOIN {course} c ON s.course = c.id WHERE s.id = ? AND c.format = ?',
|
|
|
441 |
[$itemid, 'topics'], MUST_EXIST);
|
|
|
442 |
return course_get_format($section->course)->inplace_editable_update_section_name($section, $itemtype, $newvalue);
|
|
|
443 |
}
|
|
|
444 |
}
|