MDL-35339 Deprecate add_mod_to_section(), create new function course_add_cm_to_section()
[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');
ce4dfd27 8 require_once($CFG->libdir.'/conditionlib.php');
516c5eca 9 require_once($CFG->libdir.'/completionlib.php');
f9903ed0 10
2335781f 11 $id = optional_param('id', 0, PARAM_INT);
0cdae0dc 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);
5249ba74 17 $sectionid = optional_param('sectionid', 0, PARAM_INT);
0cdae0dc 18 $section = optional_param('section', 0, PARAM_INT);
19 $move = optional_param('move', 0, PARAM_INT);
f4f0bc84 20 $marker = optional_param('marker',-1 , PARAM_INT);
3a52e764 21 $switchrole = optional_param('switchrole',-1, PARAM_INT);
01e0e704 22 $modchooser = optional_param('modchooser', -1, PARAM_BOOL);
3a52e764 23
599a52fe 24 $params = array();
2335781f 25 if (!empty($name)) {
599a52fe 26 $params = array('shortname' => $name);
816acb46 27 } else if (!empty($idnumber)) {
599a52fe
DP
28 $params = array('idnumber' => $idnumber);
29 } else if (!empty($id)) {
30 $params = array('id' => $id);
31 }else {
32 print_error('unspecifycourseid', 'error');
f9903ed0 33 }
34
599a52fe
DP
35 $course = $DB->get_record('course', $params, '*', MUST_EXIST);
36
45774bdd 37 $urlparams = array('id' => $course->id);
5249ba74 38
2c7a72df 39 // Sectionid should get priority over section number
5249ba74
AA
40 if ($sectionid) {
41 $section = $DB->get_field('course_sections', 'section', array('id' => $sectionid, 'course' => $course->id), MUST_EXIST);
42 }
45774bdd
DP
43 if ($section) {
44 $urlparams['section'] = $section;
45 }
46
47 $PAGE->set_url('/course/view.php', $urlparams); // Defined here to avoid notices on errors etc
088e3363 48
fbb4c959
SH
49 // Prevent caching of this page to stop confusion when changing page after making AJAX changes
50 $PAGE->set_cacheable(false);
51
00653161 52 preload_course_contexts($course->id);
599a52fe 53 $context = context_course::instance($course->id, MUST_EXIST);
3a52e764 54
d7d4b0e5 55 // Remove any switched roles before checking login
56 if ($switchrole == 0 && confirm_sesskey()) {
ac4c51be 57 role_switch($switchrole, $context);
58 }
59
a6af674a 60 require_login($course);
388f8911 61
d7d4b0e5 62 // Switchrole - sanity check in cost-order...
46bd37bf 63 $reset_user_allowed_editing = false;
d7d4b0e5 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...
82701e24 68 $aroles = get_switchable_roles($context);
d7d4b0e5 69 if (is_array($aroles) && isset($aroles[$switchrole])) {
70 role_switch($switchrole, $context);
71 // Double check that this role is allowed here
cdbea7ee 72 require_login($course);
d7d4b0e5 73 }
46bd37bf 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;
3a52e764 81 }
82
542a0435 83 //If course is hosted on an external server, redirect to corresponding
aa6c1ced 84 //url with appropriate authentication attached as parameter
892c0825 85 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
86 include $CFG->dirroot .'/course/externservercourse.php';
4d2401d5 87 if (function_exists('extern_server_course')) {
542a0435 88 if ($extern_url = extern_server_course($course)) {
89 redirect($extern_url);
90 }
91 }
92 }
93
3a52e764 94
08cebf19 95 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
af62781b 96
15a848ba
RW
97 $logparam = 'id='. $course->id;
98 $loglabel = 'view';
34b84d45 99 $infoid = $course->id;
9e586967 100 if ($section and $section > 0) {
15a848ba 101 $loglabel = 'view section';
c069dacf 102
103 // Get section details and check it exists.
104 $modinfo = get_fast_modinfo($course);
105 $coursesections = $modinfo->get_section_info($section, MUST_EXIST);
106
107 // Check user is allowed to see it.
134e28b7 108 if (!$coursesections->uservisible) {
c069dacf 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);
113 }
336d8c49
AA
114 $infoid = $coursesections->id;
115 $logparam .= '&sectionid='. $infoid;
34b84d45
RW
116 }
117 add_to_log($course->id, 'course', $loglabel, "view.php?". $logparam, $infoid);
f9903ed0 118
e069b3e2 119 $course->format = clean_param($course->format, PARAM_ALPHA);
0f3fe4b6 120 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
121 $course->format = 'weeks'; // Default format is weeks
122 }
123
78946b9b 124 $PAGE->set_pagelayout('course');
66b10689 125 $PAGE->set_pagetype('course-view-' . $course->format);
cfcfb9f3 126 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
b7b2d0f3 127
46bd37bf 128 if ($reset_user_allowed_editing) {
129 // ugly hack
130 unset($PAGE->_user_allowed_editing);
131 }
daa27ce4 132
0f3fe4b6 133 if (!isset($USER->editing)) {
d2b23346 134 $USER->editing = 0;
0f3fe4b6 135 }
a3f24f7c 136 if ($PAGE->user_allowed_editing()) {
0cdae0dc 137 if (($edit == 1) and confirm_sesskey()) {
d2b23346 138 $USER->editing = 1;
ad96cc52
AKA
139 // Redirect to site root if Editing is toggled on frontpage
140 if ($course->id == SITEID) {
141 redirect($CFG->wwwroot .'/?redirect=0');
142 } else {
3970a471 143 $url = new moodle_url($PAGE->url, array('notifyeditingon' => 1));
6c0ae99b 144 redirect($url);
ad96cc52 145 }
0cdae0dc 146 } else if (($edit == 0) and confirm_sesskey()) {
d2b23346 147 $USER->editing = 0;
a3f24f7c 148 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
149 $USER->activitycopy = false;
150 $USER->activitycopycourse = NULL;
9c9f7d77 151 }
ad96cc52
AKA
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);
157 }
f9903ed0 158 }
01e0e704
ARN
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);
163 }
8223d271 164
643b1de8 165 if (has_capability('moodle/course:sectionvisibility', $context)) {
af189935
PS
166 if ($hide && confirm_sesskey()) {
167 set_section_visible($course->id, $hide, '0');
e3c171e1 168 redirect($PAGE->url);
af189935 169 }
7d99d695 170
af189935
PS
171 if ($show && confirm_sesskey()) {
172 set_section_visible($course->id, $show, '1');
e3c171e1 173 redirect($PAGE->url);
af189935 174 }
643b1de8 175 }
12905134 176
643b1de8 177 if (has_capability('moodle/course:update', $context)) {
af189935 178 if (!empty($section)) {
ad778c84 179 if (!empty($move) and has_capability('moodle/course:movesections', $context) and confirm_sesskey()) {
ac5f6414
RT
180 $destsection = $section + $move;
181 if (move_section_to($course, $section, $destsection)) {
182 // Rebuild course cache, after moving section
183 rebuild_course_cache($course->id, true);
93d46f48
RK
184 if ($course->id == SITEID) {
185 redirect($CFG->wwwroot . '/?redirect=0');
186 } else {
e3c171e1 187 redirect(course_get_url($course));
93d46f48
RK
188 }
189 } else {
af189935
PS
190 echo $OUTPUT->notification('An error occurred while moving a section');
191 }
56e34ee4 192 }
12905134 193 }
194 }
48d72fa7 195 } else {
d2b23346 196 $USER->editing = 0;
7d99d695 197 }
198
45774bdd 199 $SESSION->fromdiscussion = $PAGE->url->out(false);
4c701e6f 200
f45cc76f 201
202 if ($course->id == SITEID) {
203 // This course is not a real course.
892c0825 204 redirect($CFG->wwwroot .'/');
83f3c62d 205 }
0a127169 206
cf615522 207 $completion = new completion_info($course);
208 if ($completion->is_enabled() && ajaxenabled()) {
76c0123b
PS
209 $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
210 $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
211 $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
212 $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
213
214 $PAGE->requires->js_init_call('M.core_completion.init');
cf615522 215 }
216
8a7934f7
PS
217 // We are currently keeping the button here from 1.x to help new teachers figure out
218 // what to do, even though the link also appears in the course admin block. It also
2b3d1ac7 219 // means you can back out of a situation where you removed the admin block. :)
5d3e9d9f 220 if ($PAGE->user_allowed_editing()) {
d628215d 221 $buttons = $OUTPUT->edit_button($PAGE->url);
2b3d1ac7 222 $PAGE->set_button($buttons);
5d3e9d9f 223 }
224
2b9e3bac 225 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
0a122046 226 $PAGE->set_heading($course->fullname);
0a122046 227 echo $OUTPUT->header();
f45cc76f 228
cf615522 229 if ($completion->is_enabled() && ajaxenabled()) {
9a5a0694 230 // This value tracks whether there has been a dynamic change to the page.
231 // It is used so that if a user does this - (a) set some tickmarks, (b)
232 // go to another page, (c) clicks Back button - the page will
233 // automatically reload. Otherwise it would start with the wrong tick
234 // values.
47e75d04
SH
235 echo html_writer::start_tag('form', array('action'=>'.', 'method'=>'get'));
236 echo html_writer::start_tag('div');
237 echo html_writer::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
238 echo html_writer::end_tag('div');
239 echo html_writer::end_tag('form');
4e781c7b 240 }
241
f45cc76f 242 // Course wrapper start.
47e75d04 243 echo html_writer::start_tag('div', array('class'=>'course-content'));
f45cc76f 244
f20edd52 245 $modinfo = get_fast_modinfo($COURSE);
9c9f7d77 246 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
a6af674a 247 foreach($mods as $modid=>$unused) {
248 if (!isset($modinfo->cms[$modid])) {
249 rebuild_course_cache($course->id);
f20edd52 250 $modinfo = get_fast_modinfo($COURSE);
a6af674a 251 debugging('Rebuilding course cache', DEBUG_DEVELOPER);
252 break;
253 }
254 }
19801b2b 255
94dc3c7d 256 if (!$sections = $modinfo->get_section_info_all()) { // No sections found
ce4dfd27 257 $section = new stdClass;
258 $section->course = $course->id; // Create a default section.
259 $section->section = 0;
260 $section->visible = 1;
261 $section->summaryformat = FORMAT_HTML;
262 $section->id = $DB->insert_record('course_sections', $section);
263 rebuild_course_cache($course->id);
264 $modinfo = get_fast_modinfo($COURSE);
94dc3c7d 265 if (!$sections = $modinfo->get_section_info_all()) { // Try again
dbf91d9d 266 print_error('cannotcreateorfindstructs', 'error');
19801b2b 267 }
268 }
7468bf01 269
45774bdd
DP
270 // CAUTION, hacky fundamental variable defintion to follow!
271 // Note that because of the way course fromats are constructed though
272 // inclusion we pass parameters around this way..
273 $displaysection = $section;
274
f45cc76f 275 // Include the actual course format.
276 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
277 // Content wrapper end.
47e75d04
SH
278
279 echo html_writer::end_tag('div');
f45cc76f 280
01e0e704
ARN
281 // Include course AJAX
282 if (include_course_ajax($course, $modnamesused)) {
283 // Add the module chooser
284 $renderer = $PAGE->get_renderer('core', 'course');
a41b1d96 285 echo $renderer->course_modchooser(get_module_metadata($course, $modnames, $displaysection), $course);
01e0e704 286 }
f45cc76f 287
d60c1124 288 echo $OUTPUT->footer();