3 // Display the course home page.
5 require_once('../config.php');
6 require_once('lib.php');
7 require_once($CFG->dirroot.'/mod/forum/lib.php');
8 require_once($CFG->libdir.'/conditionlib.php');
9 require_once($CFG->libdir.'/completionlib.php');
11 $id = optional_param('id', 0, PARAM_INT);
12 $name = optional_param('name', '', PARAM_RAW);
13 $edit = optional_param('edit', -1, PARAM_BOOL);
14 $hide = optional_param('hide', 0, PARAM_INT);
15 $show = optional_param('show', 0, PARAM_INT);
16 $idnumber = optional_param('idnumber', '', PARAM_RAW);
17 $sectionid = optional_param('sectionid', 0, PARAM_INT);
18 $section = optional_param('section', 0, PARAM_INT);
19 $move = optional_param('move', 0, PARAM_INT);
20 $marker = optional_param('marker',-1 , PARAM_INT);
21 $switchrole = optional_param('switchrole',-1, PARAM_INT);
22 $modchooser = optional_param('modchooser', -1, PARAM_BOOL);
23 $return = optional_param('return', 0, PARAM_LOCALURL);
27 $params = array('shortname' => $name);
28 } else if (!empty($idnumber)) {
29 $params = array('idnumber' => $idnumber);
30 } else if (!empty($id)) {
31 $params = array('id' => $id);
33 print_error('unspecifycourseid', 'error');
36 $course = $DB->get_record('course', $params, '*', MUST_EXIST);
38 $urlparams = array('id' => $course->id);
40 // Sectionid should get priority over section number
42 $section = $DB->get_field('course_sections', 'section', array('id' => $sectionid, 'course' => $course->id), MUST_EXIST);
45 $urlparams['section'] = $section;
48 $PAGE->set_url('/course/view.php', $urlparams); // Defined here to avoid notices on errors etc
50 // Prevent caching of this page to stop confusion when changing page after making AJAX changes
51 $PAGE->set_cacheable(false);
53 preload_course_contexts($course->id);
54 $context = context_course::instance($course->id, MUST_EXIST);
56 // Remove any switched roles before checking login
57 if ($switchrole == 0 && confirm_sesskey()) {
58 role_switch($switchrole, $context);
61 require_login($course);
63 // Switchrole - sanity check in cost-order...
64 $reset_user_allowed_editing = false;
65 if ($switchrole > 0 && confirm_sesskey() &&
66 has_capability('moodle/role:switchroles', $context)) {
67 // is this role assignable in this context?
68 // inquiring minds want to know...
69 $aroles = get_switchable_roles($context);
70 if (is_array($aroles) && isset($aroles[$switchrole])) {
71 role_switch($switchrole, $context);
72 // Double check that this role is allowed here
73 require_login($course);
75 // reset course page state - this prevents some weird problems ;-)
76 $USER->activitycopy = false;
77 $USER->activitycopycourse = NULL;
78 unset($USER->activitycopyname);
79 unset($SESSION->modform);
81 $reset_user_allowed_editing = true;
84 //If course is hosted on an external server, redirect to corresponding
85 //url with appropriate authentication attached as parameter
86 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
87 include $CFG->dirroot .'/course/externservercourse.php';
88 if (function_exists('extern_server_course')) {
89 if ($extern_url = extern_server_course($course)) {
90 redirect($extern_url);
96 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
98 $logparam = 'id='. $course->id;
100 $infoid = $course->id;
101 if ($section and $section > 0) {
102 $loglabel = 'view section';
104 // Get section details and check it exists.
105 $modinfo = get_fast_modinfo($course);
106 $coursesections = $modinfo->get_section_info($section, MUST_EXIST);
108 // Check user is allowed to see it.
109 if (!$coursesections->uservisible) {
110 // Note: We actually already know they don't have this capability
111 // or uservisible would have been true; this is just to get the
112 // correct error message shown.
113 require_capability('moodle/course:viewhiddensections', $context);
115 $infoid = $coursesections->id;
116 $logparam .= '§ionid='. $infoid;
118 add_to_log($course->id, 'course', $loglabel, "view.php?". $logparam, $infoid);
120 $course->format = clean_param($course->format, PARAM_ALPHA);
121 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
122 $course->format = 'weeks'; // Default format is weeks
125 $PAGE->set_pagelayout('course');
126 $PAGE->set_pagetype('course-view-' . $course->format);
127 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
129 if ($reset_user_allowed_editing) {
131 unset($PAGE->_user_allowed_editing);
134 if (!isset($USER->editing)) {
137 if ($PAGE->user_allowed_editing()) {
138 if (($edit == 1) and confirm_sesskey()) {
140 // Redirect to site root if Editing is toggled on frontpage
141 if ($course->id == SITEID) {
142 redirect($CFG->wwwroot .'/?redirect=0');
143 } else if (!empty($return)) {
144 redirect($CFG->wwwroot . $return);
146 $url = new moodle_url($PAGE->url, array('notifyeditingon' => 1));
149 } else if (($edit == 0) and confirm_sesskey()) {
151 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
152 $USER->activitycopy = false;
153 $USER->activitycopycourse = NULL;
155 // Redirect to site root if Editing is toggled on frontpage
156 if ($course->id == SITEID) {
157 redirect($CFG->wwwroot .'/?redirect=0');
158 } else if (!empty($return)) {
159 redirect($CFG->wwwroot . $return);
161 redirect($PAGE->url);
164 if (($modchooser == 1) && confirm_sesskey()) {
165 set_user_preference('usemodchooser', $modchooser);
166 } else if (($modchooser == 0) && confirm_sesskey()) {
167 set_user_preference('usemodchooser', $modchooser);
170 if (has_capability('moodle/course:sectionvisibility', $context)) {
171 if ($hide && confirm_sesskey()) {
172 set_section_visible($course->id, $hide, '0');
173 redirect($PAGE->url);
176 if ($show && confirm_sesskey()) {
177 set_section_visible($course->id, $show, '1');
178 redirect($PAGE->url);
182 if (has_capability('moodle/course:update', $context)) {
183 if (!empty($section)) {
184 if (!empty($move) and has_capability('moodle/course:movesections', $context) and confirm_sesskey()) {
185 $destsection = $section + $move;
186 if (move_section_to($course, $section, $destsection)) {
187 if ($course->id == SITEID) {
188 redirect($CFG->wwwroot . '/?redirect=0');
190 redirect(course_get_url($course));
193 echo $OUTPUT->notification('An error occurred while moving a section');
202 $SESSION->fromdiscussion = $PAGE->url->out(false);
205 if ($course->id == SITEID) {
206 // This course is not a real course.
207 redirect($CFG->wwwroot .'/');
210 $completion = new completion_info($course);
211 if ($completion->is_enabled() && ajaxenabled()) {
212 $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
213 $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
214 $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
215 $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
217 $PAGE->requires->js_init_call('M.core_completion.init');
220 // We are currently keeping the button here from 1.x to help new teachers figure out
221 // what to do, even though the link also appears in the course admin block. It also
222 // means you can back out of a situation where you removed the admin block. :)
223 if ($PAGE->user_allowed_editing()) {
224 $buttons = $OUTPUT->edit_button($PAGE->url);
225 $PAGE->set_button($buttons);
228 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
229 $PAGE->set_heading($course->fullname);
230 echo $OUTPUT->header();
232 if ($completion->is_enabled() && ajaxenabled()) {
233 // This value tracks whether there has been a dynamic change to the page.
234 // It is used so that if a user does this - (a) set some tickmarks, (b)
235 // go to another page, (c) clicks Back button - the page will
236 // automatically reload. Otherwise it would start with the wrong tick
238 echo html_writer::start_tag('form', array('action'=>'.', 'method'=>'get'));
239 echo html_writer::start_tag('div');
240 echo html_writer::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
241 echo html_writer::end_tag('div');
242 echo html_writer::end_tag('form');
245 // Course wrapper start.
246 echo html_writer::start_tag('div', array('class'=>'course-content'));
248 // make sure that section 0 exists (this function will create one if it is missing)
249 course_create_sections_if_missing($course, 0);
251 // get information about course modules and existing module types
252 // format.php in course formats may rely on presence of these variables
253 $modinfo = get_fast_modinfo($course);
254 $modnames = get_module_types_names();
255 $modnamesplural = get_module_types_names(true);
256 $modnamesused = $modinfo->get_used_module_names();
257 $mods = $modinfo->get_cms();
258 $sections = $modinfo->get_section_info_all();
260 // CAUTION, hacky fundamental variable defintion to follow!
261 // Note that because of the way course fromats are constructed though
262 // inclusion we pass parameters around this way..
263 $displaysection = $section;
265 // Include the actual course format.
266 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
267 // Content wrapper end.
269 echo html_writer::end_tag('div');
271 // Include course AJAX
272 include_course_ajax($course, $modnamesused);
274 echo $OUTPUT->footer();