Merge branch 'MDL-22504_drag_and_drop_upload_final' of https://github.com/davosmith...
[moodle.git] / course / view.php
blobb14befe4cead799446c926a6e8ad0d459d1779a7
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.'/completionlib.php');
10 $id = optional_param('id', 0, PARAM_INT);
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);
16 $section = optional_param('section', 0, PARAM_INT);
17 $move = optional_param('move', 0, PARAM_INT);
18 $marker = optional_param('marker',-1 , PARAM_INT);
19 $switchrole = optional_param('switchrole',-1, PARAM_INT);
21 $params = array();
22 if (!empty($name)) {
23 $params = array('shortname' => $name);
24 } else if (!empty($idnumber)) {
25 $params = array('idnumber' => $idnumber);
26 } else if (!empty($id)) {
27 $params = array('id' => $id);
28 }else {
29 print_error('unspecifycourseid', 'error');
32 $course = $DB->get_record('course', $params, '*', MUST_EXIST);
34 $urlparams = array('id' => $course->id);
35 if ($section) {
36 $urlparams['section'] = $section;
39 $PAGE->set_url('/course/view.php', $urlparams); // Defined here to avoid notices on errors etc
41 // Prevent caching of this page to stop confusion when changing page after making AJAX changes
42 $PAGE->set_cacheable(false);
44 preload_course_contexts($course->id);
45 $context = context_course::instance($course->id, MUST_EXIST);
47 // Remove any switched roles before checking login
48 if ($switchrole == 0 && confirm_sesskey()) {
49 role_switch($switchrole, $context);
52 require_login($course);
54 // Switchrole - sanity check in cost-order...
55 $reset_user_allowed_editing = false;
56 if ($switchrole > 0 && confirm_sesskey() &&
57 has_capability('moodle/role:switchroles', $context)) {
58 // is this role assignable in this context?
59 // inquiring minds want to know...
60 $aroles = get_switchable_roles($context);
61 if (is_array($aroles) && isset($aroles[$switchrole])) {
62 role_switch($switchrole, $context);
63 // Double check that this role is allowed here
64 require_login($course);
66 // reset course page state - this prevents some weird problems ;-)
67 $USER->activitycopy = false;
68 $USER->activitycopycourse = NULL;
69 unset($USER->activitycopyname);
70 unset($SESSION->modform);
71 $USER->editing = 0;
72 $reset_user_allowed_editing = true;
75 //If course is hosted on an external server, redirect to corresponding
76 //url with appropriate authentication attached as parameter
77 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
78 include $CFG->dirroot .'/course/externservercourse.php';
79 if (function_exists('extern_server_course')) {
80 if ($extern_url = extern_server_course($course)) {
81 redirect($extern_url);
87 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
89 //TODO: danp do we need different urls?
90 add_to_log($course->id, 'course', 'view', "view.php?id=$course->id", "$course->id");
92 $course->format = clean_param($course->format, PARAM_ALPHA);
93 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
94 $course->format = 'weeks'; // Default format is weeks
97 $PAGE->set_pagelayout('course');
98 $PAGE->set_pagetype('course-view-' . $course->format);
99 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
101 if ($reset_user_allowed_editing) {
102 // ugly hack
103 unset($PAGE->_user_allowed_editing);
106 if (!isset($USER->editing)) {
107 $USER->editing = 0;
109 if ($PAGE->user_allowed_editing()) {
110 if (($edit == 1) and confirm_sesskey()) {
111 $USER->editing = 1;
112 // Redirect to site root if Editing is toggled on frontpage
113 if ($course->id == SITEID) {
114 redirect($CFG->wwwroot .'/?redirect=0');
115 } else {
116 redirect($PAGE->url);
118 } else if (($edit == 0) and confirm_sesskey()) {
119 $USER->editing = 0;
120 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
121 $USER->activitycopy = false;
122 $USER->activitycopycourse = NULL;
124 // Redirect to site root if Editing is toggled on frontpage
125 if ($course->id == SITEID) {
126 redirect($CFG->wwwroot .'/?redirect=0');
127 } else {
128 redirect($PAGE->url);
132 if (has_capability('moodle/course:update', $context)) {
133 if ($hide && confirm_sesskey()) {
134 set_section_visible($course->id, $hide, '0');
135 redirect($PAGE->url);
138 if ($show && confirm_sesskey()) {
139 set_section_visible($course->id, $show, '1');
140 redirect($PAGE->url);
143 if (!empty($section)) {
144 if (!empty($move) and confirm_sesskey()) {
145 if (move_section($course, $section, $move)) {
146 if ($course->id == SITEID) {
147 redirect($CFG->wwwroot . '/?redirect=0');
148 } else {
149 redirect(course_get_url($course));
151 } else {
152 echo $OUTPUT->notification('An error occurred while moving a section');
157 } else {
158 $USER->editing = 0;
161 $SESSION->fromdiscussion = $PAGE->url->out(false);
164 if ($course->id == SITEID) {
165 // This course is not a real course.
166 redirect($CFG->wwwroot .'/');
169 $completion = new completion_info($course);
170 if ($completion->is_enabled() && ajaxenabled()) {
171 $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
172 $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
173 $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
174 $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
176 $PAGE->requires->js_init_call('M.core_completion.init');
179 // We are currently keeping the button here from 1.x to help new teachers figure out
180 // what to do, even though the link also appears in the course admin block. It also
181 // means you can back out of a situation where you removed the admin block. :)
182 if ($PAGE->user_allowed_editing()) {
183 $buttons = $OUTPUT->edit_button($PAGE->url);
184 $PAGE->set_button($buttons);
187 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
188 $PAGE->set_heading($course->fullname);
189 echo $OUTPUT->header();
191 if ($completion->is_enabled() && ajaxenabled()) {
192 // This value tracks whether there has been a dynamic change to the page.
193 // It is used so that if a user does this - (a) set some tickmarks, (b)
194 // go to another page, (c) clicks Back button - the page will
195 // automatically reload. Otherwise it would start with the wrong tick
196 // values.
197 echo html_writer::start_tag('form', array('action'=>'.', 'method'=>'get'));
198 echo html_writer::start_tag('div');
199 echo html_writer::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
200 echo html_writer::end_tag('div');
201 echo html_writer::end_tag('form');
204 // Course wrapper start.
205 echo html_writer::start_tag('div', array('class'=>'course-content'));
207 $modinfo = get_fast_modinfo($COURSE);
208 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
209 foreach($mods as $modid=>$unused) {
210 if (!isset($modinfo->cms[$modid])) {
211 rebuild_course_cache($course->id);
212 $modinfo = get_fast_modinfo($COURSE);
213 debugging('Rebuilding course cache', DEBUG_DEVELOPER);
214 break;
218 if (! $sections = get_all_sections($course->id)) { // No sections found
219 // Double-check to be extra sure
220 if (! $section = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>0))) {
221 $section->course = $course->id; // Create a default section.
222 $section->section = 0;
223 $section->visible = 1;
224 $section->summaryformat = FORMAT_HTML;
225 $section->id = $DB->insert_record('course_sections', $section);
227 if (! $sections = get_all_sections($course->id) ) { // Try again
228 print_error('cannotcreateorfindstructs', 'error');
232 // CAUTION, hacky fundamental variable defintion to follow!
233 // Note that because of the way course fromats are constructed though
234 // inclusion we pass parameters around this way..
235 $displaysection = $section;
237 // Include the actual course format.
238 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
239 // Content wrapper end.
241 echo html_writer::end_tag('div');
243 // Include the command toolbox YUI module
244 include_course_ajax($course, $modnamesused, $modnames);
246 echo $OUTPUT->footer();