Merge branch 'MDL-34573_accessibility' of git://github.com/rwijaya/moodle
[moodle.git] / course / view.php
blob525ba52dd86431640589494860e2223bfb87a086
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);
23 $return = optional_param('return', 0, PARAM_LOCALURL);
25 $params = array();
26 if (!empty($name)) {
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);
32 }else {
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
41 if ($sectionid) {
42 $section = $DB->get_field('course_sections', 'section', array('id' => $sectionid, 'course' => $course->id), MUST_EXIST);
44 if ($section) {
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);
80 $USER->editing = 0;
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;
99 $loglabel = 'view';
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 .= '&sectionid='. $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) {
130 // ugly hack
131 unset($PAGE->_user_allowed_editing);
134 if (!isset($USER->editing)) {
135 $USER->editing = 0;
137 if ($PAGE->user_allowed_editing()) {
138 if (($edit == 1) and confirm_sesskey()) {
139 $USER->editing = 1;
140 // Redirect to site root if Editing is toggled on frontpage
141 if ($course->id == SITEID) {
142 redirect($CFG->wwwroot .'/?redirect=0');
143 } else {
144 $url = new moodle_url($PAGE->url, array('notifyeditingon' => 1));
145 redirect($url);
147 } else if (($edit == 0) and confirm_sesskey()) {
148 $USER->editing = 0;
149 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
150 $USER->activitycopy = false;
151 $USER->activitycopycourse = NULL;
153 // Redirect to site root if Editing is toggled on frontpage
154 if ($course->id == SITEID) {
155 redirect($CFG->wwwroot .'/?redirect=0');
156 } else if (!empty($return)) {
157 redirect($CFG->wwwroot . $return);
158 } else {
159 redirect($PAGE->url);
162 if (($modchooser == 1) && confirm_sesskey()) {
163 set_user_preference('usemodchooser', $modchooser);
164 } else if (($modchooser == 0) && confirm_sesskey()) {
165 set_user_preference('usemodchooser', $modchooser);
168 if (has_capability('moodle/course:sectionvisibility', $context)) {
169 if ($hide && confirm_sesskey()) {
170 set_section_visible($course->id, $hide, '0');
171 redirect($PAGE->url);
174 if ($show && confirm_sesskey()) {
175 set_section_visible($course->id, $show, '1');
176 redirect($PAGE->url);
180 if (has_capability('moodle/course:update', $context)) {
181 if (!empty($section)) {
182 if (!empty($move) and has_capability('moodle/course:movesections', $context) and confirm_sesskey()) {
183 $destsection = $section + $move;
184 if (move_section_to($course, $section, $destsection)) {
185 // Rebuild course cache, after moving section
186 rebuild_course_cache($course->id, true);
187 if ($course->id == SITEID) {
188 redirect($CFG->wwwroot . '/?redirect=0');
189 } else {
190 redirect(course_get_url($course));
192 } else {
193 echo $OUTPUT->notification('An error occurred while moving a section');
198 } else {
199 $USER->editing = 0;
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
237 // values.
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 $modinfo = get_fast_modinfo($COURSE);
249 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
250 foreach($mods as $modid=>$unused) {
251 if (!isset($modinfo->cms[$modid])) {
252 rebuild_course_cache($course->id);
253 $modinfo = get_fast_modinfo($COURSE);
254 debugging('Rebuilding course cache', DEBUG_DEVELOPER);
255 break;
259 if (!$sections = $modinfo->get_section_info_all()) { // No sections found
260 $section = new stdClass;
261 $section->course = $course->id; // Create a default section.
262 $section->section = 0;
263 $section->visible = 1;
264 $section->summaryformat = FORMAT_HTML;
265 $section->id = $DB->insert_record('course_sections', $section);
266 rebuild_course_cache($course->id);
267 $modinfo = get_fast_modinfo($COURSE);
268 if (!$sections = $modinfo->get_section_info_all()) { // Try again
269 print_error('cannotcreateorfindstructs', 'error');
273 // CAUTION, hacky fundamental variable defintion to follow!
274 // Note that because of the way course fromats are constructed though
275 // inclusion we pass parameters around this way..
276 $displaysection = $section;
278 // Include the actual course format.
279 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
280 // Content wrapper end.
282 echo html_writer::end_tag('div');
284 // Include course AJAX
285 if (include_course_ajax($course, $modnamesused)) {
286 // Add the module chooser
287 $renderer = $PAGE->get_renderer('core', 'course');
288 echo $renderer->course_modchooser(get_module_metadata($course, $modnames, $displaysection), $course);
291 echo $OUTPUT->footer();