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 the course format singleactivity
|
|
|
19 |
*
|
|
|
20 |
* @package format_singleactivity
|
|
|
21 |
* @copyright 2012 Marina Glancy
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
defined('MOODLE_INTERNAL') || die();
|
|
|
26 |
require_once($CFG->dirroot. '/course/format/lib.php');
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Main class for the singleactivity course format
|
|
|
30 |
*
|
|
|
31 |
* @package format_singleactivity
|
|
|
32 |
* @copyright 2012 Marina Glancy
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
|
|
35 |
class format_singleactivity extends core_courseformat\base {
|
|
|
36 |
/** @var cm_info the current activity. Use get_activity() to retrieve it. */
|
|
|
37 |
private $activity = false;
|
|
|
38 |
|
|
|
39 |
/** @var int The category ID guessed from the form data. */
|
|
|
40 |
private $categoryid = false;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* The URL to use for the specified course
|
|
|
44 |
*
|
|
|
45 |
* @param int|stdClass $section Section object from database or just field course_sections.section
|
|
|
46 |
* if null the course view page is returned
|
|
|
47 |
* @param array $options options for view URL. At the moment core uses:
|
|
|
48 |
* 'navigation' (bool) ignored by this format
|
|
|
49 |
* 'sr' (int) ignored by this format
|
|
|
50 |
* @return null|moodle_url
|
|
|
51 |
*/
|
|
|
52 |
public function get_view_url($section, $options = array()) {
|
|
|
53 |
return new moodle_url('/course/view.php', ['id' => $this->courseid]);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Loads all of the course sections into the navigation
|
|
|
58 |
*
|
|
|
59 |
* @param global_navigation $navigation
|
|
|
60 |
* @param navigation_node $node The course node within the navigation
|
|
|
61 |
*/
|
|
|
62 |
public function extend_course_navigation($navigation, navigation_node $node) {
|
|
|
63 |
// Display orphaned activities for the users who can see them.
|
|
|
64 |
$context = context_course::instance($this->courseid);
|
|
|
65 |
if (has_capability('moodle/course:viewhiddensections', $context)) {
|
|
|
66 |
$modinfo = get_fast_modinfo($this->courseid);
|
|
|
67 |
if (!empty($modinfo->sections[1])) {
|
|
|
68 |
$section1 = $modinfo->get_section_info(1);
|
|
|
69 |
// Show orphaned activities.
|
|
|
70 |
$orphanednode = $node->add(get_string('orphaned', 'format_singleactivity'),
|
|
|
71 |
$this->get_view_url(1), navigation_node::TYPE_SECTION, null, $section1->id);
|
|
|
72 |
$orphanednode->nodetype = navigation_node::NODETYPE_BRANCH;
|
|
|
73 |
$orphanednode->add_class('orphaned');
|
|
|
74 |
foreach ($modinfo->sections[1] as $cmid) {
|
|
|
75 |
if (has_capability('moodle/course:viewhiddenactivities', context_module::instance($cmid))) {
|
|
|
76 |
$this->navigation_add_activity($orphanednode, $modinfo->cms[$cmid]);
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Adds a course module to the navigation node
|
|
|
85 |
*
|
|
|
86 |
* This is basically copied from function global_navigation::load_section_activities()
|
|
|
87 |
* because it is not accessible from outside.
|
|
|
88 |
*
|
|
|
89 |
* @param navigation_node $node
|
|
|
90 |
* @param cm_info $cm
|
|
|
91 |
* @return null|navigation_node
|
|
|
92 |
*/
|
|
|
93 |
protected function navigation_add_activity(navigation_node $node, $cm) {
|
|
|
94 |
if (!$cm->uservisible) {
|
|
|
95 |
return null;
|
|
|
96 |
}
|
|
|
97 |
$action = $cm->url;
|
|
|
98 |
if (!$action) {
|
|
|
99 |
// Do not add to navigation activity without url (i.e. labels).
|
|
|
100 |
return null;
|
|
|
101 |
}
|
|
|
102 |
$activityname = format_string($cm->name, true, array('context' => context_module::instance($cm->id)));
|
|
|
103 |
if ($cm->icon) {
|
|
|
104 |
$icon = new pix_icon($cm->icon, $cm->modfullname, $cm->iconcomponent);
|
|
|
105 |
} else {
|
|
|
106 |
$icon = new pix_icon('monologo', $cm->modfullname, $cm->modname);
|
|
|
107 |
}
|
|
|
108 |
$activitynode = $node->add($activityname, $action, navigation_node::TYPE_ACTIVITY, null, $cm->id, $icon);
|
|
|
109 |
if (global_navigation::module_extends_navigation($cm->modname)) {
|
|
|
110 |
$activitynode->nodetype = navigation_node::NODETYPE_BRANCH;
|
|
|
111 |
} else {
|
|
|
112 |
$activitynode->nodetype = navigation_node::NODETYPE_LEAF;
|
|
|
113 |
}
|
|
|
114 |
return $activitynode;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Returns the list of blocks to be automatically added for the newly created course
|
|
|
119 |
*
|
|
|
120 |
* @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
|
|
|
121 |
* each of values is an array of block names (for left and right side columns)
|
|
|
122 |
*/
|
|
|
123 |
public function get_default_blocks() {
|
|
|
124 |
// No blocks for this format because course view page is not displayed anyway.
|
|
|
125 |
return array(
|
|
|
126 |
BLOCK_POS_LEFT => array(),
|
|
|
127 |
BLOCK_POS_RIGHT => array()
|
|
|
128 |
);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Definitions of the additional options that this course format uses for course
|
|
|
133 |
*
|
|
|
134 |
* Singleactivity course format uses one option 'activitytype'
|
|
|
135 |
*
|
|
|
136 |
* @param bool $foreditform
|
|
|
137 |
* @return array of options
|
|
|
138 |
*/
|
|
|
139 |
public function course_format_options($foreditform = false) {
|
|
|
140 |
static $courseformatoptions = false;
|
|
|
141 |
|
|
|
142 |
$fetchtypes = $courseformatoptions === false;
|
|
|
143 |
$fetchtypes = $fetchtypes || ($foreditform && !isset($courseformatoptions['activitytype']['label']));
|
|
|
144 |
|
|
|
145 |
if ($fetchtypes) {
|
|
|
146 |
$availabletypes = $this->get_supported_activities();
|
|
|
147 |
if ($this->courseid) {
|
|
|
148 |
// The course exists. Test against the course.
|
|
|
149 |
$testcontext = context_course::instance($this->courseid);
|
|
|
150 |
} else if ($this->categoryid) {
|
|
|
151 |
// The course does not exist yet, but we have a category ID that we can test against.
|
|
|
152 |
$testcontext = context_coursecat::instance($this->categoryid);
|
|
|
153 |
} else {
|
|
|
154 |
// The course does not exist, and we somehow do not have a category. Test capabilities against the system context.
|
|
|
155 |
$testcontext = context_system::instance();
|
|
|
156 |
}
|
|
|
157 |
foreach (array_keys($availabletypes) as $activity) {
|
|
|
158 |
$capability = "mod/{$activity}:addinstance";
|
|
|
159 |
if (!has_capability($capability, $testcontext)) {
|
11 |
efrain |
160 |
if (!$this->categoryid) {
|
|
|
161 |
unset($availabletypes[$activity]);
|
|
|
162 |
} else {
|
|
|
163 |
// We do not have a course yet, so we guess if the user will have the capability to add the activity after
|
|
|
164 |
// creating the course.
|
|
|
165 |
$categorycontext = \context_coursecat::instance($this->categoryid);
|
|
|
166 |
if (!guess_if_creator_will_have_course_capability($capability, $categorycontext)) {
|
|
|
167 |
unset($availabletypes[$activity]);
|
|
|
168 |
}
|
|
|
169 |
}
|
1 |
efrain |
170 |
}
|
|
|
171 |
}
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
if ($courseformatoptions === false) {
|
|
|
175 |
$config = get_config('format_singleactivity');
|
|
|
176 |
$courseformatoptions = array(
|
|
|
177 |
'activitytype' => array(
|
|
|
178 |
'default' => $config->activitytype,
|
|
|
179 |
'type' => PARAM_TEXT,
|
|
|
180 |
),
|
|
|
181 |
);
|
|
|
182 |
|
|
|
183 |
if (!empty($availabletypes) && !isset($availabletypes[$config->activitytype])) {
|
|
|
184 |
$courseformatoptions['activitytype']['default'] = array_keys($availabletypes)[0];
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
if ($foreditform && !isset($courseformatoptions['activitytype']['label'])) {
|
|
|
189 |
$courseformatoptionsedit = array(
|
|
|
190 |
'activitytype' => array(
|
|
|
191 |
'label' => new lang_string('activitytype', 'format_singleactivity'),
|
|
|
192 |
'help' => 'activitytype',
|
|
|
193 |
'help_component' => 'format_singleactivity',
|
|
|
194 |
'element_type' => 'select',
|
|
|
195 |
'element_attributes' => array($availabletypes),
|
|
|
196 |
),
|
|
|
197 |
);
|
|
|
198 |
$courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
|
|
|
199 |
}
|
|
|
200 |
return $courseformatoptions;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Adds format options elements to the course/section edit form
|
|
|
205 |
*
|
|
|
206 |
* This function is called from {@link course_edit_form::definition_after_data()}
|
|
|
207 |
*
|
|
|
208 |
* Format singleactivity adds a warning when format of the course is about to be changed.
|
|
|
209 |
*
|
|
|
210 |
* @param MoodleQuickForm $mform form the elements are added to
|
|
|
211 |
* @param bool $forsection 'true' if this is a section edit form, 'false' if this is course edit form
|
|
|
212 |
* @return array array of references to the added form elements
|
|
|
213 |
*/
|
|
|
214 |
public function create_edit_form_elements(&$mform, $forsection = false) {
|
|
|
215 |
global $PAGE;
|
|
|
216 |
|
|
|
217 |
if (!$this->course && $submitvalues = $mform->getSubmitValues()) {
|
|
|
218 |
$this->categoryid = $submitvalues['category'];
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
$elements = parent::create_edit_form_elements($mform, $forsection);
|
|
|
222 |
if (!$forsection && ($course = $PAGE->course) && !empty($course->format) &&
|
|
|
223 |
$course->format !== 'site' && $course->format !== 'singleactivity') {
|
|
|
224 |
// This is the existing course in other format, display a warning.
|
|
|
225 |
$element = $mform->addElement('static', '', '',
|
|
|
226 |
html_writer::tag('span', get_string('warningchangeformat', 'format_singleactivity'),
|
|
|
227 |
array('class' => 'error')));
|
|
|
228 |
array_unshift($elements, $element);
|
|
|
229 |
}
|
|
|
230 |
return $elements;
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
/**
|
|
|
234 |
* Make sure that current active activity is in section 0
|
|
|
235 |
*
|
|
|
236 |
* All other activities are moved to section 1 that will be displayed as 'Orphaned'.
|
|
|
237 |
* It may be needed after the course format was changed or activitytype in
|
|
|
238 |
* course settings has been changed.
|
|
|
239 |
*
|
|
|
240 |
* @return null|cm_info current activity
|
|
|
241 |
*/
|
|
|
242 |
public function reorder_activities() {
|
|
|
243 |
course_create_sections_if_missing($this->courseid, array(0, 1));
|
|
|
244 |
foreach ($this->get_sections() as $sectionnum => $section) {
|
|
|
245 |
if (($sectionnum && $section->visible) ||
|
|
|
246 |
(!$sectionnum && !$section->visible)) {
|
|
|
247 |
// Make sure that 0 section is visible and all others are hidden.
|
|
|
248 |
set_section_visible($this->courseid, $sectionnum, $sectionnum == 0);
|
|
|
249 |
}
|
|
|
250 |
}
|
|
|
251 |
$modinfo = get_fast_modinfo($this->courseid);
|
|
|
252 |
|
|
|
253 |
// Find the current activity (first activity with the specified type in all course activities).
|
|
|
254 |
$activitytype = $this->get_activitytype();
|
|
|
255 |
$activity = null;
|
|
|
256 |
if (!empty($activitytype)) {
|
|
|
257 |
foreach ($modinfo->sections as $sectionnum => $cmlist) {
|
|
|
258 |
foreach ($cmlist as $cmid) {
|
|
|
259 |
if ($modinfo->cms[$cmid]->modname === $activitytype) {
|
|
|
260 |
$activity = $modinfo->cms[$cmid];
|
|
|
261 |
break 2;
|
|
|
262 |
}
|
|
|
263 |
}
|
|
|
264 |
}
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
// Make sure the current activity is in the 0-section.
|
|
|
268 |
$changed = false;
|
|
|
269 |
if ($activity && $activity->sectionnum != 0) {
|
|
|
270 |
moveto_module($activity, $modinfo->get_section_info(0));
|
|
|
271 |
$changed = true;
|
|
|
272 |
}
|
|
|
273 |
if ($activity && !$activity->visible) {
|
|
|
274 |
set_coursemodule_visible($activity->id, 1);
|
|
|
275 |
$changed = true;
|
|
|
276 |
}
|
|
|
277 |
if ($changed) {
|
|
|
278 |
// Cache was reset so get modinfo again.
|
|
|
279 |
$modinfo = get_fast_modinfo($this->courseid);
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
// Move all other activities into section 1 (the order must be kept).
|
|
|
283 |
$hasvisibleactivities = false;
|
|
|
284 |
$firstorphanedcm = null;
|
|
|
285 |
foreach ($modinfo->sections as $sectionnum => $cmlist) {
|
|
|
286 |
if ($sectionnum && !empty($cmlist) && $firstorphanedcm === null) {
|
|
|
287 |
$firstorphanedcm = reset($cmlist);
|
|
|
288 |
}
|
|
|
289 |
foreach ($cmlist as $cmid) {
|
|
|
290 |
if ($sectionnum > 1) {
|
|
|
291 |
moveto_module($modinfo->get_cm($cmid), $modinfo->get_section_info(1));
|
|
|
292 |
} else if (!$hasvisibleactivities && $sectionnum == 1 && $modinfo->get_cm($cmid)->visible) {
|
|
|
293 |
$hasvisibleactivities = true;
|
|
|
294 |
}
|
|
|
295 |
}
|
|
|
296 |
}
|
|
|
297 |
if (!empty($modinfo->sections[0])) {
|
|
|
298 |
foreach ($modinfo->sections[0] as $cmid) {
|
|
|
299 |
if (!$activity || $cmid != $activity->id) {
|
|
|
300 |
moveto_module($modinfo->get_cm($cmid), $modinfo->get_section_info(1), $firstorphanedcm);
|
|
|
301 |
}
|
|
|
302 |
}
|
|
|
303 |
}
|
|
|
304 |
if ($hasvisibleactivities) {
|
|
|
305 |
set_section_visible($this->courseid, 1, false);
|
|
|
306 |
}
|
|
|
307 |
return $activity;
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
/**
|
|
|
311 |
* Returns the name of activity type used for this course
|
|
|
312 |
*
|
|
|
313 |
* @return string|null
|
|
|
314 |
*/
|
|
|
315 |
protected function get_activitytype() {
|
|
|
316 |
$options = $this->get_format_options();
|
|
|
317 |
$availabletypes = $this->get_supported_activities();
|
|
|
318 |
if (!empty($options['activitytype']) &&
|
|
|
319 |
array_key_exists($options['activitytype'], $availabletypes)) {
|
|
|
320 |
return $options['activitytype'];
|
|
|
321 |
} else {
|
|
|
322 |
return null;
|
|
|
323 |
}
|
|
|
324 |
}
|
|
|
325 |
|
|
|
326 |
/**
|
|
|
327 |
* Returns the current activity if exists
|
|
|
328 |
*
|
|
|
329 |
* @return null|cm_info
|
|
|
330 |
*/
|
|
|
331 |
protected function get_activity() {
|
|
|
332 |
if ($this->activity === false) {
|
|
|
333 |
$this->activity = $this->reorder_activities();
|
|
|
334 |
}
|
|
|
335 |
return $this->activity;
|
|
|
336 |
}
|
|
|
337 |
|
|
|
338 |
/**
|
|
|
339 |
* Get the activities supported by the format.
|
|
|
340 |
*
|
|
|
341 |
* Here we ignore the modules that do not have a page of their own, like the label.
|
|
|
342 |
*
|
|
|
343 |
* @return array array($module => $name of the module).
|
|
|
344 |
*/
|
|
|
345 |
public static function get_supported_activities() {
|
|
|
346 |
$availabletypes = get_module_types_names();
|
|
|
347 |
foreach ($availabletypes as $module => $name) {
|
|
|
348 |
if (plugin_supports('mod', $module, FEATURE_NO_VIEW_LINK, false)) {
|
|
|
349 |
unset($availabletypes[$module]);
|
|
|
350 |
}
|
|
|
351 |
}
|
|
|
352 |
return $availabletypes;
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
/**
|
|
|
356 |
* Checks if the current user can add the activity of the specified type to this course.
|
|
|
357 |
*
|
|
|
358 |
* @return bool
|
|
|
359 |
*/
|
|
|
360 |
protected function can_add_activity() {
|
|
|
361 |
global $CFG;
|
|
|
362 |
if (!($modname = $this->get_activitytype())) {
|
|
|
363 |
return false;
|
|
|
364 |
}
|
|
|
365 |
if (!has_capability('moodle/course:manageactivities', context_course::instance($this->courseid))) {
|
|
|
366 |
return false;
|
|
|
367 |
}
|
|
|
368 |
if (!course_allowed_module($this->get_course(), $modname)) {
|
|
|
369 |
return false;
|
|
|
370 |
}
|
|
|
371 |
$libfile = "$CFG->dirroot/mod/$modname/lib.php";
|
|
|
372 |
if (!file_exists($libfile)) {
|
|
|
373 |
return null;
|
|
|
374 |
}
|
|
|
375 |
return true;
|
|
|
376 |
}
|
|
|
377 |
|
|
|
378 |
/**
|
|
|
379 |
* Checks if the activity type has multiple items in the activity chooser.
|
|
|
380 |
*
|
|
|
381 |
* @return bool|null (null if the check is not possible)
|
|
|
382 |
*/
|
|
|
383 |
public function activity_has_subtypes() {
|
|
|
384 |
global $USER;
|
|
|
385 |
if (!($modname = $this->get_activitytype())) {
|
|
|
386 |
return null;
|
|
|
387 |
}
|
|
|
388 |
$contentitemservice = \core_course\local\factory\content_item_service_factory::get_content_item_service();
|
|
|
389 |
$metadata = $contentitemservice->get_content_items_for_user_in_course($USER, $this->get_course());
|
|
|
390 |
|
|
|
391 |
// If there are multiple items originating from this mod_xxx component, then it's deemed to have subtypes.
|
|
|
392 |
// If there is only 1 item, but it's not a reference to the core content item for the module, then it's also deemed to
|
|
|
393 |
// have subtypes.
|
|
|
394 |
$count = 0;
|
|
|
395 |
foreach ($metadata as $key => $moduledata) {
|
|
|
396 |
if ('mod_'.$modname === $moduledata->componentname) {
|
|
|
397 |
$count ++;
|
|
|
398 |
}
|
|
|
399 |
}
|
|
|
400 |
if ($count > 1) {
|
|
|
401 |
return true;
|
|
|
402 |
} else {
|
|
|
403 |
// Get the single item.
|
|
|
404 |
$itemmetadata = $metadata[array_search('mod_' . $modname, array_column($metadata, 'componentname'))];
|
|
|
405 |
$urlbase = new \moodle_url('/course/mod.php', ['id' => $this->get_course()->id]);
|
|
|
406 |
$referenceurl = new \moodle_url($urlbase, ['add' => $modname]);
|
|
|
407 |
if ($referenceurl->out(false) != $itemmetadata->link) {
|
|
|
408 |
return true;
|
|
|
409 |
}
|
|
|
410 |
}
|
|
|
411 |
return false;
|
|
|
412 |
}
|
|
|
413 |
|
|
|
414 |
/**
|
|
|
415 |
* Allows course format to execute code on moodle_page::set_course()
|
|
|
416 |
*
|
|
|
417 |
* This function is executed before the output starts.
|
|
|
418 |
*
|
|
|
419 |
* If everything is configured correctly, user is redirected from the
|
|
|
420 |
* default course view page to the activity view page.
|
|
|
421 |
*
|
|
|
422 |
* "Section 1" is the administrative page to manage orphaned activities
|
|
|
423 |
*
|
|
|
424 |
* If user is on course view page and there is no module added to the course
|
|
|
425 |
* and the user has 'moodle/course:manageactivities' capability, redirect to create module
|
|
|
426 |
* form.
|
|
|
427 |
*
|
|
|
428 |
* @param moodle_page $page instance of page calling set_course
|
|
|
429 |
*/
|
|
|
430 |
public function page_set_course(moodle_page $page) {
|
|
|
431 |
global $PAGE;
|
|
|
432 |
$page->add_body_class('format-'. $this->get_format());
|
|
|
433 |
if ($PAGE == $page && $page->has_set_url() &&
|
|
|
434 |
$page->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
|
|
|
435 |
$edit = optional_param('edit', -1, PARAM_BOOL);
|
|
|
436 |
if (($edit == 0 || $edit == 1) && confirm_sesskey()) {
|
|
|
437 |
// This is a request to turn editing mode on or off, do not redirect here, /course/view.php will do redirection.
|
|
|
438 |
return;
|
|
|
439 |
}
|
|
|
440 |
$cm = $this->get_activity();
|
|
|
441 |
$cursection = optional_param('section', null, PARAM_INT);
|
|
|
442 |
if (!empty($cursection) && has_capability('moodle/course:viewhiddensections',
|
|
|
443 |
context_course::instance($this->courseid))) {
|
|
|
444 |
// Display orphaned activities (course view page, section 1).
|
|
|
445 |
return;
|
|
|
446 |
}
|
|
|
447 |
if (!$this->get_activitytype()) {
|
|
|
448 |
if (has_capability('moodle/course:update', context_course::instance($this->courseid))) {
|
|
|
449 |
// Teacher is redirected to edit course page.
|
|
|
450 |
$url = new moodle_url('/course/edit.php', array('id' => $this->courseid));
|
|
|
451 |
redirect($url, get_string('erroractivitytype', 'format_singleactivity'));
|
|
|
452 |
} else {
|
|
|
453 |
// Student sees an empty course page.
|
|
|
454 |
return;
|
|
|
455 |
}
|
|
|
456 |
}
|
|
|
457 |
if ($cm === null) {
|
|
|
458 |
if ($this->can_add_activity()) {
|
|
|
459 |
// This is a user who has capability to create an activity.
|
|
|
460 |
if ($this->activity_has_subtypes()) {
|
|
|
461 |
// Activity has multiple items in the activity chooser, it can not be added automatically.
|
|
|
462 |
if (optional_param('addactivity', 0, PARAM_INT)) {
|
|
|
463 |
return;
|
|
|
464 |
} else {
|
|
|
465 |
$url = new moodle_url('/course/view.php', array('id' => $this->courseid, 'addactivity' => 1));
|
|
|
466 |
redirect($url);
|
|
|
467 |
}
|
|
|
468 |
}
|
|
|
469 |
// Redirect to the add activity form.
|
|
|
470 |
$url = new moodle_url('/course/mod.php', array('id' => $this->courseid,
|
|
|
471 |
'section' => 0, 'sesskey' => sesskey(), 'add' => $this->get_activitytype()));
|
|
|
472 |
redirect($url);
|
|
|
473 |
} else {
|
|
|
474 |
// Student views an empty course page.
|
|
|
475 |
return;
|
|
|
476 |
}
|
|
|
477 |
} else if (!$cm->uservisible || !$cm->url) {
|
|
|
478 |
// Activity is set but not visible to current user or does not have url.
|
|
|
479 |
// Display course page (either empty or with availability restriction info).
|
|
|
480 |
return;
|
|
|
481 |
} else {
|
|
|
482 |
// Everything is set up and accessible, redirect to the activity page!
|
|
|
483 |
redirect($cm->url);
|
|
|
484 |
}
|
|
|
485 |
}
|
|
|
486 |
}
|
|
|
487 |
|
|
|
488 |
/**
|
|
|
489 |
* Allows course format to execute code on moodle_page::set_cm()
|
|
|
490 |
*
|
|
|
491 |
* If we are inside the main module for this course, remove extra node level
|
|
|
492 |
* from navigation: substitute course node with activity node, move all children
|
|
|
493 |
*
|
|
|
494 |
* @param moodle_page $page instance of page calling set_cm
|
|
|
495 |
*/
|
|
|
496 |
public function page_set_cm(moodle_page $page) {
|
|
|
497 |
global $PAGE;
|
|
|
498 |
parent::page_set_cm($page);
|
|
|
499 |
if ($PAGE == $page && ($cm = $this->get_activity()) &&
|
|
|
500 |
$cm->uservisible &&
|
|
|
501 |
($cm->id === $page->cm->id) &&
|
|
|
502 |
($activitynode = $page->navigation->find($cm->id, navigation_node::TYPE_ACTIVITY)) &&
|
|
|
503 |
($node = $page->navigation->find($page->course->id, navigation_node::TYPE_COURSE))) {
|
|
|
504 |
// Substitute course node with activity node, move all children.
|
|
|
505 |
$node->action = $activitynode->action;
|
|
|
506 |
$node->type = $activitynode->type;
|
|
|
507 |
$node->id = $activitynode->id;
|
|
|
508 |
$node->key = $activitynode->key;
|
|
|
509 |
$node->isactive = $node->isactive || $activitynode->isactive;
|
|
|
510 |
$node->icon = null;
|
|
|
511 |
if ($activitynode->children->count()) {
|
|
|
512 |
foreach ($activitynode->children as &$child) {
|
|
|
513 |
$child->remove();
|
|
|
514 |
$node->add_node($child);
|
|
|
515 |
}
|
|
|
516 |
} else {
|
|
|
517 |
$node->search_for_active_node();
|
|
|
518 |
}
|
|
|
519 |
$activitynode->remove();
|
|
|
520 |
}
|
|
|
521 |
}
|
|
|
522 |
|
|
|
523 |
/**
|
|
|
524 |
* Returns true if the course has a front page.
|
|
|
525 |
*
|
|
|
526 |
* @return boolean false
|
|
|
527 |
*/
|
|
|
528 |
public function has_view_page() {
|
|
|
529 |
return false;
|
|
|
530 |
}
|
|
|
531 |
|
|
|
532 |
/**
|
|
|
533 |
* Return the plugin configs for external functions.
|
|
|
534 |
*
|
|
|
535 |
* @return array the list of configuration settings
|
|
|
536 |
* @since Moodle 3.5
|
|
|
537 |
*/
|
|
|
538 |
public function get_config_for_external() {
|
|
|
539 |
// Return everything (nothing to hide).
|
|
|
540 |
return $this->get_format_options();
|
|
|
541 |
}
|
|
|
542 |
}
|