MDL-32709 book: fix send_stored_file() call
[moodle.git] / course / view.php
bloba7b1784b1c59feb7203d1ad14a5c44b9789c6b5a
1 <?php
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 $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);
21 $modchooser = optional_param('modchooser', -1, PARAM_BOOL);
23 $params = array();
24 if (!empty($name)) {
25 $params = array('shortname' => $name);
26 } else if (!empty($idnumber)) {
27 $params = array('idnumber' => $idnumber);
28 } else if (!empty($id)) {
29 $params = array('id' => $id);
30 }else {
31 print_error('unspecifycourseid', 'error');
34 $course = $DB->get_record('course', $params, '*', MUST_EXIST);
36 $urlparams = array('id' => $course->id);
37 if ($section) {
38 $urlparams['section'] = $section;
41 $PAGE->set_url('/course/view.php', $urlparams); // Defined here to avoid notices on errors etc
43 // Prevent caching of this page to stop confusion when changing page after making AJAX changes
44 $PAGE->set_cacheable(false);
46 preload_course_contexts($course->id);
47 $context = context_course::instance($course->id, MUST_EXIST);
49 // Remove any switched roles before checking login
50 if ($switchrole == 0 && confirm_sesskey()) {
51 role_switch($switchrole, $context);
54 require_login($course);
56 // Switchrole - sanity check in cost-order...
57 $reset_user_allowed_editing = false;
58 if ($switchrole > 0 && confirm_sesskey() &&
59 has_capability('moodle/role:switchroles', $context)) {
60 // is this role assignable in this context?
61 // inquiring minds want to know...
62 $aroles = get_switchable_roles($context);
63 if (is_array($aroles) && isset($aroles[$switchrole])) {
64 role_switch($switchrole, $context);
65 // Double check that this role is allowed here
66 require_login($course);
68 // reset course page state - this prevents some weird problems ;-)
69 $USER->activitycopy = false;
70 $USER->activitycopycourse = NULL;
71 unset($USER->activitycopyname);
72 unset($SESSION->modform);
73 $USER->editing = 0;
74 $reset_user_allowed_editing = true;
77 //If course is hosted on an external server, redirect to corresponding
78 //url with appropriate authentication attached as parameter
79 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
80 include $CFG->dirroot .'/course/externservercourse.php';
81 if (function_exists('extern_server_course')) {
82 if ($extern_url = extern_server_course($course)) {
83 redirect($extern_url);
89 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
91 //TODO: danp do we need different urls?
92 add_to_log($course->id, 'course', 'view', "view.php?id=$course->id", "$course->id");
94 $course->format = clean_param($course->format, PARAM_ALPHA);
95 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
96 $course->format = 'weeks'; // Default format is weeks
99 $PAGE->set_pagelayout('course');
100 $PAGE->set_pagetype('course-view-' . $course->format);
101 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
103 if ($reset_user_allowed_editing) {
104 // ugly hack
105 unset($PAGE->_user_allowed_editing);
108 if (!isset($USER->editing)) {
109 $USER->editing = 0;
111 if ($PAGE->user_allowed_editing()) {
112 if (($edit == 1) and confirm_sesskey()) {
113 $USER->editing = 1;
114 // Redirect to site root if Editing is toggled on frontpage
115 if ($course->id == SITEID) {
116 redirect($CFG->wwwroot .'/?redirect=0');
117 } else {
118 redirect($PAGE->url);
120 } else if (($edit == 0) and confirm_sesskey()) {
121 $USER->editing = 0;
122 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
123 $USER->activitycopy = false;
124 $USER->activitycopycourse = NULL;
126 // Redirect to site root if Editing is toggled on frontpage
127 if ($course->id == SITEID) {
128 redirect($CFG->wwwroot .'/?redirect=0');
129 } else {
130 redirect($PAGE->url);
133 if (($modchooser == 1) && confirm_sesskey()) {
134 set_user_preference('usemodchooser', $modchooser);
135 } else if (($modchooser == 0) && confirm_sesskey()) {
136 set_user_preference('usemodchooser', $modchooser);
139 if (has_capability('moodle/course:update', $context)) {
140 if ($hide && confirm_sesskey()) {
141 set_section_visible($course->id, $hide, '0');
142 redirect($PAGE->url);
145 if ($show && confirm_sesskey()) {
146 set_section_visible($course->id, $show, '1');
147 redirect($PAGE->url);
150 if (!empty($section)) {
151 if (!empty($move) and confirm_sesskey()) {
152 if (move_section($course, $section, $move)) {
153 if ($course->id == SITEID) {
154 redirect($CFG->wwwroot . '/?redirect=0');
155 } else {
156 redirect(course_get_url($course));
158 } else {
159 echo $OUTPUT->notification('An error occurred while moving a section');
164 } else {
165 $USER->editing = 0;
168 $SESSION->fromdiscussion = $PAGE->url->out(false);
171 if ($course->id == SITEID) {
172 // This course is not a real course.
173 redirect($CFG->wwwroot .'/');
176 $completion = new completion_info($course);
177 if ($completion->is_enabled() && ajaxenabled()) {
178 $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
179 $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
180 $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
181 $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
183 $PAGE->requires->js_init_call('M.core_completion.init');
186 // We are currently keeping the button here from 1.x to help new teachers figure out
187 // what to do, even though the link also appears in the course admin block. It also
188 // means you can back out of a situation where you removed the admin block. :)
189 if ($PAGE->user_allowed_editing()) {
190 $buttons = $OUTPUT->edit_button($PAGE->url);
191 $PAGE->set_button($buttons);
194 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
195 $PAGE->set_heading($course->fullname);
196 echo $OUTPUT->header();
198 if ($completion->is_enabled() && ajaxenabled()) {
199 // This value tracks whether there has been a dynamic change to the page.
200 // It is used so that if a user does this - (a) set some tickmarks, (b)
201 // go to another page, (c) clicks Back button - the page will
202 // automatically reload. Otherwise it would start with the wrong tick
203 // values.
204 echo html_writer::start_tag('form', array('action'=>'.', 'method'=>'get'));
205 echo html_writer::start_tag('div');
206 echo html_writer::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
207 echo html_writer::end_tag('div');
208 echo html_writer::end_tag('form');
211 // Course wrapper start.
212 echo html_writer::start_tag('div', array('class'=>'course-content'));
214 $modinfo = get_fast_modinfo($COURSE);
215 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
216 foreach($mods as $modid=>$unused) {
217 if (!isset($modinfo->cms[$modid])) {
218 rebuild_course_cache($course->id);
219 $modinfo = get_fast_modinfo($COURSE);
220 debugging('Rebuilding course cache', DEBUG_DEVELOPER);
221 break;
225 if (!$sections = $modinfo->get_section_info_all()) { // No sections found
226 $section = new stdClass;
227 $section->course = $course->id; // Create a default section.
228 $section->section = 0;
229 $section->visible = 1;
230 $section->summaryformat = FORMAT_HTML;
231 $section->id = $DB->insert_record('course_sections', $section);
232 rebuild_course_cache($course->id);
233 $modinfo = get_fast_modinfo($COURSE);
234 if (!$sections = $modinfo->get_section_info_all()) { // Try again
235 print_error('cannotcreateorfindstructs', 'error');
239 // CAUTION, hacky fundamental variable defintion to follow!
240 // Note that because of the way course fromats are constructed though
241 // inclusion we pass parameters around this way..
242 $displaysection = $section;
244 // Include the actual course format.
245 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
246 // Content wrapper end.
248 echo html_writer::end_tag('div');
250 // Include course AJAX
251 if (include_course_ajax($course, $modnamesused)) {
252 // Add the module chooser
253 $renderer = $PAGE->get_renderer('core', 'course');
254 echo $renderer->course_modchooser(get_module_metadata($course, $modnames), $course);
257 echo $OUTPUT->footer();