MDL-32508 course/view.php: action cleanup
[moodle.git] / course / view.php
CommitLineData
aa6c1ced 1<?php
f9903ed0 2
3// Display the course home page.
4
08cebf19 5 require_once('../config.php');
6 require_once('lib.php');
afc58a28 7 require_once($CFG->dirroot.'/mod/forum/lib.php');
516c5eca 8 require_once($CFG->libdir.'/completionlib.php');
f9903ed0 9
2335781f 10 $id = optional_param('id', 0, PARAM_INT);
0cdae0dc 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);
0cdae0dc 16 $section = optional_param('section', 0, PARAM_INT);
17 $move = optional_param('move', 0, PARAM_INT);
f4f0bc84 18 $marker = optional_param('marker',-1 , PARAM_INT);
3a52e764 19 $switchrole = optional_param('switchrole',-1, PARAM_INT);
20
599a52fe 21 $params = array();
2335781f 22 if (!empty($name)) {
599a52fe 23 $params = array('shortname' => $name);
816acb46 24 } else if (!empty($idnumber)) {
599a52fe
DP
25 $params = array('idnumber' => $idnumber);
26 } else if (!empty($id)) {
27 $params = array('id' => $id);
28 }else {
29 print_error('unspecifycourseid', 'error');
f9903ed0 30 }
31
599a52fe
DP
32 $course = $DB->get_record('course', $params, '*', MUST_EXIST);
33
088e3363
MD
34 $PAGE->set_url('/course/view.php', array('id' => $course->id)); // Defined here to avoid notices on errors etc
35
00653161 36 preload_course_contexts($course->id);
599a52fe 37 $context = context_course::instance($course->id, MUST_EXIST);
3a52e764 38
d7d4b0e5 39 // Remove any switched roles before checking login
40 if ($switchrole == 0 && confirm_sesskey()) {
ac4c51be 41 role_switch($switchrole, $context);
42 }
43
a6af674a 44 require_login($course);
388f8911 45
d7d4b0e5 46 // Switchrole - sanity check in cost-order...
46bd37bf 47 $reset_user_allowed_editing = false;
d7d4b0e5 48 if ($switchrole > 0 && confirm_sesskey() &&
49 has_capability('moodle/role:switchroles', $context)) {
50 // is this role assignable in this context?
51 // inquiring minds want to know...
82701e24 52 $aroles = get_switchable_roles($context);
d7d4b0e5 53 if (is_array($aroles) && isset($aroles[$switchrole])) {
54 role_switch($switchrole, $context);
55 // Double check that this role is allowed here
cdbea7ee 56 require_login($course);
d7d4b0e5 57 }
46bd37bf 58 // reset course page state - this prevents some weird problems ;-)
59 $USER->activitycopy = false;
60 $USER->activitycopycourse = NULL;
61 unset($USER->activitycopyname);
62 unset($SESSION->modform);
63 $USER->editing = 0;
64 $reset_user_allowed_editing = true;
3a52e764 65 }
66
542a0435 67 //If course is hosted on an external server, redirect to corresponding
aa6c1ced 68 //url with appropriate authentication attached as parameter
892c0825 69 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
70 include $CFG->dirroot .'/course/externservercourse.php';
4d2401d5 71 if (function_exists('extern_server_course')) {
542a0435 72 if ($extern_url = extern_server_course($course)) {
73 redirect($extern_url);
74 }
75 }
76 }
77
3a52e764 78
08cebf19 79 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
af62781b 80
892c0825 81 add_to_log($course->id, 'course', 'view', "view.php?id=$course->id", "$course->id");
f9903ed0 82
e069b3e2 83 $course->format = clean_param($course->format, PARAM_ALPHA);
0f3fe4b6 84 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
85 $course->format = 'weeks'; // Default format is weeks
86 }
87
78946b9b 88 $PAGE->set_pagelayout('course');
66b10689 89 $PAGE->set_pagetype('course-view-' . $course->format);
cfcfb9f3 90 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
b7b2d0f3 91
46bd37bf 92 if ($reset_user_allowed_editing) {
93 // ugly hack
94 unset($PAGE->_user_allowed_editing);
95 }
daa27ce4 96
0f3fe4b6 97 if (!isset($USER->editing)) {
d2b23346 98 $USER->editing = 0;
0f3fe4b6 99 }
a3f24f7c 100 if ($PAGE->user_allowed_editing()) {
0cdae0dc 101 if (($edit == 1) and confirm_sesskey()) {
d2b23346 102 $USER->editing = 1;
ad96cc52
AKA
103 // Redirect to site root if Editing is toggled on frontpage
104 if ($course->id == SITEID) {
105 redirect($CFG->wwwroot .'/?redirect=0');
106 } else {
107 redirect($PAGE->url);
108 }
0cdae0dc 109 } else if (($edit == 0) and confirm_sesskey()) {
d2b23346 110 $USER->editing = 0;
a3f24f7c 111 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
112 $USER->activitycopy = false;
113 $USER->activitycopycourse = NULL;
9c9f7d77 114 }
ad96cc52
AKA
115 // Redirect to site root if Editing is toggled on frontpage
116 if ($course->id == SITEID) {
117 redirect($CFG->wwwroot .'/?redirect=0');
118 } else {
119 redirect($PAGE->url);
120 }
f9903ed0 121 }
8223d271 122
af189935
PS
123 if (has_capability('moodle/course:update', $context)) {
124 if ($hide && confirm_sesskey()) {
125 set_section_visible($course->id, $hide, '0');
e3c171e1 126 redirect($PAGE->url);
af189935 127 }
7d99d695 128
af189935
PS
129 if ($show && confirm_sesskey()) {
130 set_section_visible($course->id, $show, '1');
e3c171e1 131 redirect($PAGE->url);
af189935 132 }
12905134 133
af189935
PS
134 if (!empty($section)) {
135 if (!empty($move) and confirm_sesskey()) {
93d46f48
RK
136 if (move_section($course, $section, $move)) {
137 if ($course->id == SITEID) {
138 redirect($CFG->wwwroot . '/?redirect=0');
139 } else {
e3c171e1 140 redirect(course_get_url($course));
93d46f48
RK
141 }
142 } else {
af189935
PS
143 echo $OUTPUT->notification('An error occurred while moving a section');
144 }
56e34ee4 145 }
12905134 146 }
147 }
48d72fa7 148 } else {
d2b23346 149 $USER->editing = 0;
7d99d695 150 }
151
892c0825 152 $SESSION->fromdiscussion = $CFG->wwwroot .'/course/view.php?id='. $course->id;
4c701e6f 153
f45cc76f 154
155 if ($course->id == SITEID) {
156 // This course is not a real course.
892c0825 157 redirect($CFG->wwwroot .'/');
83f3c62d 158 }
0a127169 159
cf615522 160 $completion = new completion_info($course);
161 if ($completion->is_enabled() && ajaxenabled()) {
76c0123b
PS
162 $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
163 $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
164 $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
165 $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
166
167 $PAGE->requires->js_init_call('M.core_completion.init');
cf615522 168 }
169
8a7934f7
PS
170 // We are currently keeping the button here from 1.x to help new teachers figure out
171 // what to do, even though the link also appears in the course admin block. It also
2b3d1ac7 172 // means you can back out of a situation where you removed the admin block. :)
5d3e9d9f 173 if ($PAGE->user_allowed_editing()) {
2b3d1ac7
MD
174 $buttons = $OUTPUT->edit_button(new moodle_url('/course/view.php', array('id' => $course->id)));
175 $PAGE->set_button($buttons);
5d3e9d9f 176 }
177
2b9e3bac 178 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
0a122046 179 $PAGE->set_heading($course->fullname);
0a122046 180 echo $OUTPUT->header();
f45cc76f 181
cf615522 182 if ($completion->is_enabled() && ajaxenabled()) {
9a5a0694 183 // This value tracks whether there has been a dynamic change to the page.
184 // It is used so that if a user does this - (a) set some tickmarks, (b)
185 // go to another page, (c) clicks Back button - the page will
186 // automatically reload. Otherwise it would start with the wrong tick
187 // values.
47e75d04
SH
188 echo html_writer::start_tag('form', array('action'=>'.', 'method'=>'get'));
189 echo html_writer::start_tag('div');
190 echo html_writer::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
191 echo html_writer::end_tag('div');
192 echo html_writer::end_tag('form');
4e781c7b 193 }
194
f45cc76f 195 // Course wrapper start.
47e75d04 196 echo html_writer::start_tag('div', array('class'=>'course-content'));
f45cc76f 197
f20edd52 198 $modinfo = get_fast_modinfo($COURSE);
9c9f7d77 199 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
a6af674a 200 foreach($mods as $modid=>$unused) {
201 if (!isset($modinfo->cms[$modid])) {
202 rebuild_course_cache($course->id);
f20edd52 203 $modinfo = get_fast_modinfo($COURSE);
a6af674a 204 debugging('Rebuilding course cache', DEBUG_DEVELOPER);
205 break;
206 }
207 }
19801b2b 208
dfec7b01 209 if (! $sections = get_all_sections($course->id)) { // No sections found
210 // Double-check to be extra sure
6bb08163 211 if (! $section = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>0))) {
dfec7b01 212 $section->course = $course->id; // Create a default section.
213 $section->section = 0;
214 $section->visible = 1;
09eb2151 215 $section->summaryformat = FORMAT_HTML;
6bb08163 216 $section->id = $DB->insert_record('course_sections', $section);
dfec7b01 217 }
218 if (! $sections = get_all_sections($course->id) ) { // Try again
dbf91d9d 219 print_error('cannotcreateorfindstructs', 'error');
19801b2b 220 }
221 }
7468bf01 222
f45cc76f 223 // Include the actual course format.
224 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
225 // Content wrapper end.
47e75d04
SH
226
227 echo html_writer::end_tag('div');
f45cc76f 228
ebaa29d1
ARN
229 // Include the command toolbox YUI module
230 include_course_ajax($course, $modnamesused);
f45cc76f 231
d60c1124 232 echo $OUTPUT->footer();