MDL-32827 calendar: Bumping the version
[moodle.git] / course / view.php
blobad495dd7a853ef92364aab901534b48eb414efd6
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 $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);
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 preload_course_contexts($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(!empty($section)) {
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 $course->format = clean_param($course->format, PARAM_ALPHA);
120 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
121 $course->format = 'weeks'; // Default format is weeks
124 $PAGE->set_pagelayout('course');
125 $PAGE->set_pagetype('course-view-' . $course->format);
126 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
128 if ($reset_user_allowed_editing) {
129 // ugly hack
130 unset($PAGE->_user_allowed_editing);
133 if (!isset($USER->editing)) {
134 $USER->editing = 0;
136 if ($PAGE->user_allowed_editing()) {
137 if (($edit == 1) and confirm_sesskey()) {
138 $USER->editing = 1;
139 // Redirect to site root if Editing is toggled on frontpage
140 if ($course->id == SITEID) {
141 redirect($CFG->wwwroot .'/?redirect=0');
142 } else {
143 $url = new moodle_url($PAGE->url, array('notifyeditingon' => 1));
144 redirect($url);
146 } else if (($edit == 0) and confirm_sesskey()) {
147 $USER->editing = 0;
148 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
149 $USER->activitycopy = false;
150 $USER->activitycopycourse = NULL;
152 // Redirect to site root if Editing is toggled on frontpage
153 if ($course->id == SITEID) {
154 redirect($CFG->wwwroot .'/?redirect=0');
155 } else {
156 redirect($PAGE->url);
159 if (($modchooser == 1) && confirm_sesskey()) {
160 set_user_preference('usemodchooser', $modchooser);
161 } else if (($modchooser == 0) && confirm_sesskey()) {
162 set_user_preference('usemodchooser', $modchooser);
165 if (has_capability('moodle/course:update', $context)) {
166 if ($hide && confirm_sesskey()) {
167 set_section_visible($course->id, $hide, '0');
168 redirect($PAGE->url);
171 if ($show && confirm_sesskey()) {
172 set_section_visible($course->id, $show, '1');
173 redirect($PAGE->url);
176 if (!empty($section)) {
177 if (!empty($move) and confirm_sesskey()) {
178 $destsection = $section + $move;
179 if (move_section_to($course, $section, $destsection)) {
180 // Rebuild course cache, after moving section
181 rebuild_course_cache($course->id, true);
182 if ($course->id == SITEID) {
183 redirect($CFG->wwwroot . '/?redirect=0');
184 } else {
185 redirect(course_get_url($course));
187 } else {
188 echo $OUTPUT->notification('An error occurred while moving a section');
193 } else {
194 $USER->editing = 0;
197 $SESSION->fromdiscussion = $PAGE->url->out(false);
200 if ($course->id == SITEID) {
201 // This course is not a real course.
202 redirect($CFG->wwwroot .'/');
205 $completion = new completion_info($course);
206 if ($completion->is_enabled() && ajaxenabled()) {
207 $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
208 $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
209 $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
210 $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
212 $PAGE->requires->js_init_call('M.core_completion.init');
215 // We are currently keeping the button here from 1.x to help new teachers figure out
216 // what to do, even though the link also appears in the course admin block. It also
217 // means you can back out of a situation where you removed the admin block. :)
218 if ($PAGE->user_allowed_editing()) {
219 $buttons = $OUTPUT->edit_button($PAGE->url);
220 $PAGE->set_button($buttons);
223 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
224 $PAGE->set_heading($course->fullname);
225 echo $OUTPUT->header();
227 if ($completion->is_enabled() && ajaxenabled()) {
228 // This value tracks whether there has been a dynamic change to the page.
229 // It is used so that if a user does this - (a) set some tickmarks, (b)
230 // go to another page, (c) clicks Back button - the page will
231 // automatically reload. Otherwise it would start with the wrong tick
232 // values.
233 echo html_writer::start_tag('form', array('action'=>'.', 'method'=>'get'));
234 echo html_writer::start_tag('div');
235 echo html_writer::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
236 echo html_writer::end_tag('div');
237 echo html_writer::end_tag('form');
240 // Course wrapper start.
241 echo html_writer::start_tag('div', array('class'=>'course-content'));
243 $modinfo = get_fast_modinfo($COURSE);
244 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
245 foreach($mods as $modid=>$unused) {
246 if (!isset($modinfo->cms[$modid])) {
247 rebuild_course_cache($course->id);
248 $modinfo = get_fast_modinfo($COURSE);
249 debugging('Rebuilding course cache', DEBUG_DEVELOPER);
250 break;
254 if (!$sections = $modinfo->get_section_info_all()) { // No sections found
255 $section = new stdClass;
256 $section->course = $course->id; // Create a default section.
257 $section->section = 0;
258 $section->visible = 1;
259 $section->summaryformat = FORMAT_HTML;
260 $section->id = $DB->insert_record('course_sections', $section);
261 rebuild_course_cache($course->id);
262 $modinfo = get_fast_modinfo($COURSE);
263 if (!$sections = $modinfo->get_section_info_all()) { // Try again
264 print_error('cannotcreateorfindstructs', 'error');
268 // CAUTION, hacky fundamental variable defintion to follow!
269 // Note that because of the way course fromats are constructed though
270 // inclusion we pass parameters around this way..
271 $displaysection = $section;
273 // Include the actual course format.
274 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
275 // Content wrapper end.
277 echo html_writer::end_tag('div');
279 // Include course AJAX
280 if (include_course_ajax($course, $modnamesused)) {
281 // Add the module chooser
282 $renderer = $PAGE->get_renderer('core', 'course');
283 echo $renderer->course_modchooser(get_module_metadata($course, $modnames, $displaysection), $course);
286 echo $OUTPUT->footer();