Merge branch 'MDL-52018-28-enfix' of git://github.com/mudrd8mz/moodle into MOODLE_28_...
[moodle.git] / course / view.php
blob6acd0f83a648d81fe1f194e0a8169f9e04b99c4f
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 // Must set layout before gettting section info. See MDL-47555.
98 $PAGE->set_pagelayout('course');
100 if ($section and $section > 0) {
102 // Get section details and check it exists.
103 $modinfo = get_fast_modinfo($course);
104 $coursesections = $modinfo->get_section_info($section, MUST_EXIST);
106 // Check user is allowed to see it.
107 if (!$coursesections->uservisible) {
108 // Note: We actually already know they don't have this capability
109 // or uservisible would have been true; this is just to get the
110 // correct error message shown.
111 require_capability('moodle/course:viewhiddensections', $context);
115 // Fix course format if it is no longer installed
116 $course->format = course_get_format($course)->get_format();
118 $PAGE->set_pagetype('course-view-' . $course->format);
119 $PAGE->set_other_editing_capability('moodle/course:update');
120 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
121 $PAGE->set_other_editing_capability('moodle/course:activityvisibility');
122 if (course_format_uses_sections($course->format)) {
123 $PAGE->set_other_editing_capability('moodle/course:sectionvisibility');
124 $PAGE->set_other_editing_capability('moodle/course:movesections');
127 // Preload course format renderer before output starts.
128 // This is a little hacky but necessary since
129 // format.php is not included until after output starts
130 if (file_exists($CFG->dirroot.'/course/format/'.$course->format.'/renderer.php')) {
131 require_once($CFG->dirroot.'/course/format/'.$course->format.'/renderer.php');
132 if (class_exists('format_'.$course->format.'_renderer')) {
133 // call get_renderer only if renderer is defined in format plugin
134 // otherwise an exception would be thrown
135 $PAGE->get_renderer('format_'. $course->format);
139 if ($reset_user_allowed_editing) {
140 // ugly hack
141 unset($PAGE->_user_allowed_editing);
144 if (!isset($USER->editing)) {
145 $USER->editing = 0;
147 if ($PAGE->user_allowed_editing()) {
148 if (($edit == 1) and confirm_sesskey()) {
149 $USER->editing = 1;
150 // Redirect to site root if Editing is toggled on frontpage
151 if ($course->id == SITEID) {
152 redirect($CFG->wwwroot .'/?redirect=0');
153 } else if (!empty($return)) {
154 redirect($CFG->wwwroot . $return);
155 } else {
156 $url = new moodle_url($PAGE->url, array('notifyeditingon' => 1));
157 redirect($url);
159 } else if (($edit == 0) and confirm_sesskey()) {
160 $USER->editing = 0;
161 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
162 $USER->activitycopy = false;
163 $USER->activitycopycourse = NULL;
165 // Redirect to site root if Editing is toggled on frontpage
166 if ($course->id == SITEID) {
167 redirect($CFG->wwwroot .'/?redirect=0');
168 } else if (!empty($return)) {
169 redirect($CFG->wwwroot . $return);
170 } else {
171 redirect($PAGE->url);
174 if (($modchooser == 1) && confirm_sesskey()) {
175 set_user_preference('usemodchooser', $modchooser);
176 } else if (($modchooser == 0) && confirm_sesskey()) {
177 set_user_preference('usemodchooser', $modchooser);
180 if (has_capability('moodle/course:sectionvisibility', $context)) {
181 if ($hide && confirm_sesskey()) {
182 set_section_visible($course->id, $hide, '0');
183 redirect($PAGE->url);
186 if ($show && confirm_sesskey()) {
187 set_section_visible($course->id, $show, '1');
188 redirect($PAGE->url);
192 if (!empty($section) && !empty($move) &&
193 has_capability('moodle/course:movesections', $context) && confirm_sesskey()) {
194 $destsection = $section + $move;
195 if (move_section_to($course, $section, $destsection)) {
196 if ($course->id == SITEID) {
197 redirect($CFG->wwwroot . '/?redirect=0');
198 } else {
199 redirect(course_get_url($course));
201 } else {
202 echo $OUTPUT->notification('An error occurred while moving a section');
205 } else {
206 $USER->editing = 0;
209 $SESSION->fromdiscussion = $PAGE->url->out(false);
212 if ($course->id == SITEID) {
213 // This course is not a real course.
214 redirect($CFG->wwwroot .'/');
217 $completion = new completion_info($course);
218 if ($completion->is_enabled()) {
219 $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
220 $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
221 $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
222 $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
224 $PAGE->requires->js_init_call('M.core_completion.init');
227 // We are currently keeping the button here from 1.x to help new teachers figure out
228 // what to do, even though the link also appears in the course admin block. It also
229 // means you can back out of a situation where you removed the admin block. :)
230 if ($PAGE->user_allowed_editing()) {
231 $buttons = $OUTPUT->edit_button($PAGE->url);
232 $PAGE->set_button($buttons);
235 // If viewing a section, make the title more specific
236 if ($section and $section > 0 and course_format_uses_sections($course->format)) {
237 $sectionname = get_string('sectionname', "format_$course->format");
238 $sectiontitle = get_section_name($course, $section);
239 $PAGE->set_title(get_string('coursesectiontitle', 'moodle', array('course' => $course->fullname, 'sectiontitle' => $sectiontitle, 'sectionname' => $sectionname)));
240 } else {
241 $PAGE->set_title(get_string('coursetitle', 'moodle', array('course' => $course->fullname)));
244 $PAGE->set_heading($course->fullname);
245 echo $OUTPUT->header();
247 if ($completion->is_enabled()) {
248 // This value tracks whether there has been a dynamic change to the page.
249 // It is used so that if a user does this - (a) set some tickmarks, (b)
250 // go to another page, (c) clicks Back button - the page will
251 // automatically reload. Otherwise it would start with the wrong tick
252 // values.
253 echo html_writer::start_tag('form', array('action'=>'.', 'method'=>'get'));
254 echo html_writer::start_tag('div');
255 echo html_writer::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
256 echo html_writer::end_tag('div');
257 echo html_writer::end_tag('form');
260 // Course wrapper start.
261 echo html_writer::start_tag('div', array('class'=>'course-content'));
263 // make sure that section 0 exists (this function will create one if it is missing)
264 course_create_sections_if_missing($course, 0);
266 // get information about course modules and existing module types
267 // format.php in course formats may rely on presence of these variables
268 $modinfo = get_fast_modinfo($course);
269 $modnames = get_module_types_names();
270 $modnamesplural = get_module_types_names(true);
271 $modnamesused = $modinfo->get_used_module_names();
272 $mods = $modinfo->get_cms();
273 $sections = $modinfo->get_section_info_all();
275 // CAUTION, hacky fundamental variable defintion to follow!
276 // Note that because of the way course fromats are constructed though
277 // inclusion we pass parameters around this way..
278 $displaysection = $section;
280 // Include the actual course format.
281 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
282 // Content wrapper end.
284 echo html_writer::end_tag('div');
286 // Trigger course viewed event.
287 // We don't trust $context here. Course format inclusion above executes in the global space. We can't assume
288 // anything after that point.
289 $eventdata = array('context' => context_course::instance($course->id));
290 if (!empty($section) && (int)$section == $section) {
291 $eventdata['other'] = array('coursesectionid' => $section);
293 $event = \core\event\course_viewed::create($eventdata);
294 $event->trigger();
296 // Include course AJAX
297 include_course_ajax($course, $modnamesused);
299 echo $OUTPUT->footer();