/** * Allows course format to execute code on moodle_page::set_course() * * This function is executed before the output starts. * * If everything is configured correctly, user is redirected from the * default course view page to the activity view page. * * "Section 1" is the administrative page to manage orphaned activities * * If user is on course view page and there is no module added to the course * and the user has 'moodle/course:manageactivities' capability, redirect to create module * form. * * @param moodle_page $page instance of page calling set_course */ public function page_set_course(moodle_page $page) { global $PAGE; $page->add_body_class('format-' . $this->get_format()); if ($PAGE == $page && $page->has_set_url() && $page->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) { $edit = optional_param('edit', -1, PARAM_BOOL); if (($edit == 0 || $edit == 1) && confirm_sesskey()) { // This is a request to turn editing mode on or off, do not redirect here, /course/view.php will do redirection. return; } $cm = $this->get_activity(); $cursection = optional_param('section', null, PARAM_INT); if (!empty($cursection) && has_capability('moodle/course:viewhiddensections', context_course::instance($this->courseid))) { // Display orphaned activities (course view page, section 1). return; } if (!$this->get_activitytype()) { if (has_capability('moodle/course:update', context_course::instance($this->courseid))) { // Teacher is redirected to edit course page. $url = new moodle_url('/course/edit.php', array('id' => $this->courseid)); redirect($url, get_string('erroractivitytype', 'format_singleactivity')); } else { // Student sees an empty course page. return; } } if ($cm === null) { if ($this->can_add_activity()) { // This is a user who has capability to create an activity. if ($this->activity_has_subtypes()) { // Activity that requires subtype can not be added automatically. if (optional_param('addactivity', 0, PARAM_INT)) { return; } else { $url = new moodle_url('/course/view.php', array('id' => $this->courseid, 'addactivity' => 1)); redirect($url); } } // Redirect to the add activity form. $url = new moodle_url('/course/mod.php', array('id' => $this->courseid, 'section' => 0, 'sesskey' => sesskey(), 'add' => $this->get_activitytype())); redirect($url); } else { // Student views an empty course page. return; } } else { if (!$cm->uservisible || !$cm->get_url()) { // Activity is set but not visible to current user or does not have url. // Display course page (either empty or with availability restriction info). return; } else { // Everything is set up and accessible, redirect to the activity page! redirect($cm->get_url()); } } } }