MDL-43175 events: Base event unit tests for JSON encoding / decoding comparison.
[moodle.git] / course / view.php
blob5430b6c1f3e60490919d2e399f14599f76ec6740
1 <?php
3 // Display the course home page.
5 require_once('../config.php');
6 require_once('lib.php');
7 require_once($CFG->libdir.'/conditionlib.php');
8 require_once($CFG->libdir.'/completionlib.php');
10 $id = optional_param('id', 0, PARAM_INT);
11 $name = optional_param('name', '', PARAM_RAW);
12 $edit = optional_param('edit', -1, PARAM_BOOL);
13 $hide = optional_param('hide', 0, PARAM_INT);
14 $show = optional_param('show', 0, PARAM_INT);
15 $idnumber = optional_param('idnumber', '', PARAM_RAW);
16 $sectionid = optional_param('sectionid', 0, PARAM_INT);
17 $section = optional_param('section', 0, PARAM_INT);
18 $move = optional_param('move', 0, PARAM_INT);
19 $marker = optional_param('marker',-1 , PARAM_INT);
20 $switchrole = optional_param('switchrole',-1, PARAM_INT); // Deprecated, use course/switchrole.php instead.
21 $modchooser = optional_param('modchooser', -1, PARAM_BOOL);
22 $return = optional_param('return', 0, PARAM_LOCALURL);
24 $params = array();
25 if (!empty($name)) {
26 $params = array('shortname' => $name);
27 } else if (!empty($idnumber)) {
28 $params = array('idnumber' => $idnumber);
29 } else if (!empty($id)) {
30 $params = array('id' => $id);
31 }else {
32 print_error('unspecifycourseid', 'error');
35 $course = $DB->get_record('course', $params, '*', MUST_EXIST);
37 $urlparams = array('id' => $course->id);
39 // Sectionid should get priority over section number
40 if ($sectionid) {
41 $section = $DB->get_field('course_sections', 'section', array('id' => $sectionid, 'course' => $course->id), MUST_EXIST);
43 if ($section) {
44 $urlparams['section'] = $section;
47 $PAGE->set_url('/course/view.php', $urlparams); // Defined here to avoid notices on errors etc
49 // Prevent caching of this page to stop confusion when changing page after making AJAX changes
50 $PAGE->set_cacheable(false);
52 context_helper::preload_course($course->id);
53 $context = context_course::instance($course->id, MUST_EXIST);
55 // Remove any switched roles before checking login
56 if ($switchrole == 0 && confirm_sesskey()) {
57 role_switch($switchrole, $context);
60 require_login($course);
62 // Switchrole - sanity check in cost-order...
63 $reset_user_allowed_editing = false;
64 if ($switchrole > 0 && confirm_sesskey() &&
65 has_capability('moodle/role:switchroles', $context)) {
66 // is this role assignable in this context?
67 // inquiring minds want to know...
68 $aroles = get_switchable_roles($context);
69 if (is_array($aroles) && isset($aroles[$switchrole])) {
70 role_switch($switchrole, $context);
71 // Double check that this role is allowed here
72 require_login($course);
74 // reset course page state - this prevents some weird problems ;-)
75 $USER->activitycopy = false;
76 $USER->activitycopycourse = NULL;
77 unset($USER->activitycopyname);
78 unset($SESSION->modform);
79 $USER->editing = 0;
80 $reset_user_allowed_editing = true;
83 //If course is hosted on an external server, redirect to corresponding
84 //url with appropriate authentication attached as parameter
85 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
86 include $CFG->dirroot .'/course/externservercourse.php';
87 if (function_exists('extern_server_course')) {
88 if ($extern_url = extern_server_course($course)) {
89 redirect($extern_url);
95 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
97 $logparam = 'id='. $course->id;
98 $loglabel = 'view';
99 $infoid = $course->id;
100 if ($section and $section > 0) {
101 $loglabel = 'view section';
103 // Get section details and check it exists.
104 $modinfo = get_fast_modinfo($course);
105 $coursesections = $modinfo->get_section_info($section, MUST_EXIST);
107 // Check user is allowed to see it.
108 if (!$coursesections->uservisible) {
109 // Note: We actually already know they don't have this capability
110 // or uservisible would have been true; this is just to get the
111 // correct error message shown.
112 require_capability('moodle/course:viewhiddensections', $context);
114 $infoid = $coursesections->id;
115 $logparam .= '&sectionid='. $infoid;
117 add_to_log($course->id, 'course', $loglabel, "view.php?". $logparam, $infoid);
119 // Fix course format if it is no longer installed
120 $course->format = course_get_format($course)->get_format();
122 $PAGE->set_pagelayout('course');
123 $PAGE->set_pagetype('course-view-' . $course->format);
124 $PAGE->set_other_editing_capability('moodle/course:update');
125 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
126 $PAGE->set_other_editing_capability('moodle/course:activityvisibility');
127 if (course_format_uses_sections($course->format)) {
128 $PAGE->set_other_editing_capability('moodle/course:sectionvisibility');
129 $PAGE->set_other_editing_capability('moodle/course:movesections');
132 // Preload course format renderer before output starts.
133 // This is a little hacky but necessary since
134 // format.php is not included until after output starts
135 if (file_exists($CFG->dirroot.'/course/format/'.$course->format.'/renderer.php')) {
136 require_once($CFG->dirroot.'/course/format/'.$course->format.'/renderer.php');
137 if (class_exists('format_'.$course->format.'_renderer')) {
138 // call get_renderer only if renderer is defined in format plugin
139 // otherwise an exception would be thrown
140 $PAGE->get_renderer('format_'. $course->format);
144 if ($reset_user_allowed_editing) {
145 // ugly hack
146 unset($PAGE->_user_allowed_editing);
149 if (!isset($USER->editing)) {
150 $USER->editing = 0;
152 if ($PAGE->user_allowed_editing()) {
153 if (($edit == 1) and confirm_sesskey()) {
154 $USER->editing = 1;
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);
160 } else {
161 $url = new moodle_url($PAGE->url, array('notifyeditingon' => 1));
162 redirect($url);
164 } else if (($edit == 0) and confirm_sesskey()) {
165 $USER->editing = 0;
166 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
167 $USER->activitycopy = false;
168 $USER->activitycopycourse = NULL;
170 // Redirect to site root if Editing is toggled on frontpage
171 if ($course->id == SITEID) {
172 redirect($CFG->wwwroot .'/?redirect=0');
173 } else if (!empty($return)) {
174 redirect($CFG->wwwroot . $return);
175 } else {
176 redirect($PAGE->url);
179 if (($modchooser == 1) && confirm_sesskey()) {
180 set_user_preference('usemodchooser', $modchooser);
181 } else if (($modchooser == 0) && confirm_sesskey()) {
182 set_user_preference('usemodchooser', $modchooser);
185 if (has_capability('moodle/course:sectionvisibility', $context)) {
186 if ($hide && confirm_sesskey()) {
187 set_section_visible($course->id, $hide, '0');
188 redirect($PAGE->url);
191 if ($show && confirm_sesskey()) {
192 set_section_visible($course->id, $show, '1');
193 redirect($PAGE->url);
197 if (!empty($section) && !empty($move) &&
198 has_capability('moodle/course:movesections', $context) && confirm_sesskey()) {
199 $destsection = $section + $move;
200 if (move_section_to($course, $section, $destsection)) {
201 if ($course->id == SITEID) {
202 redirect($CFG->wwwroot . '/?redirect=0');
203 } else {
204 redirect(course_get_url($course));
206 } else {
207 echo $OUTPUT->notification('An error occurred while moving a section');
210 } else {
211 $USER->editing = 0;
214 $SESSION->fromdiscussion = $PAGE->url->out(false);
217 if ($course->id == SITEID) {
218 // This course is not a real course.
219 redirect($CFG->wwwroot .'/');
222 $ajaxenabled = ajaxenabled();
224 $completion = new completion_info($course);
225 if ($completion->is_enabled() && $ajaxenabled) {
226 $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
227 $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
228 $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
229 $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
231 $PAGE->requires->js_init_call('M.core_completion.init');
234 // We are currently keeping the button here from 1.x to help new teachers figure out
235 // what to do, even though the link also appears in the course admin block. It also
236 // means you can back out of a situation where you removed the admin block. :)
237 if ($PAGE->user_allowed_editing()) {
238 $buttons = $OUTPUT->edit_button($PAGE->url);
239 $PAGE->set_button($buttons);
242 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
243 $PAGE->set_heading($course->fullname);
244 echo $OUTPUT->header();
246 if ($completion->is_enabled() && $ajaxenabled) {
247 // This value tracks whether there has been a dynamic change to the page.
248 // It is used so that if a user does this - (a) set some tickmarks, (b)
249 // go to another page, (c) clicks Back button - the page will
250 // automatically reload. Otherwise it would start with the wrong tick
251 // values.
252 echo html_writer::start_tag('form', array('action'=>'.', 'method'=>'get'));
253 echo html_writer::start_tag('div');
254 echo html_writer::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
255 echo html_writer::end_tag('div');
256 echo html_writer::end_tag('form');
259 // Course wrapper start.
260 echo html_writer::start_tag('div', array('class'=>'course-content'));
262 // make sure that section 0 exists (this function will create one if it is missing)
263 course_create_sections_if_missing($course, 0);
265 // get information about course modules and existing module types
266 // format.php in course formats may rely on presence of these variables
267 $modinfo = get_fast_modinfo($course);
268 $modnames = get_module_types_names();
269 $modnamesplural = get_module_types_names(true);
270 $modnamesused = $modinfo->get_used_module_names();
271 $mods = $modinfo->get_cms();
272 $sections = $modinfo->get_section_info_all();
274 // CAUTION, hacky fundamental variable defintion to follow!
275 // Note that because of the way course fromats are constructed though
276 // inclusion we pass parameters around this way..
277 $displaysection = $section;
279 // Include the actual course format.
280 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
281 // Content wrapper end.
283 echo html_writer::end_tag('div');
285 // Include course AJAX
286 include_course_ajax($course, $modnamesused);
288 echo $OUTPUT->footer();