Merge branch 'w36_MDL-35145_m22_guestdelete' of git://github.com/skodak/moodle into...
[moodle.git] / course / view.php
blob816265efd18c506ba84e6c3f705297cc555a765e
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 if (empty($id) && empty($name) && empty($idnumber)) {
22 print_error('unspecifycourseid', 'error');
25 if (!empty($name)) {
26 if (! ($course = $DB->get_record('course', array('shortname'=>$name)))) {
27 print_error('invalidcoursenameshort', 'error');
29 } else if (!empty($idnumber)) {
30 if (! ($course = $DB->get_record('course', array('idnumber'=>$idnumber)))) {
31 print_error('invalidcourseid', 'error');
33 } else {
34 if (! ($course = $DB->get_record('course', array('id'=>$id)))) {
35 print_error('invalidcourseid', 'error');
39 $PAGE->set_url('/course/view.php', array('id' => $course->id)); // Defined here to avoid notices on errors etc
41 preload_course_contexts($course->id);
42 if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
43 print_error('nocontext');
46 // Remove any switched roles before checking login
47 if ($switchrole == 0 && confirm_sesskey()) {
48 role_switch($switchrole, $context);
51 require_login($course);
53 // Switchrole - sanity check in cost-order...
54 $reset_user_allowed_editing = false;
55 if ($switchrole > 0 && confirm_sesskey() &&
56 has_capability('moodle/role:switchroles', $context)) {
57 // is this role assignable in this context?
58 // inquiring minds want to know...
59 $aroles = get_switchable_roles($context);
60 if (is_array($aroles) && isset($aroles[$switchrole])) {
61 role_switch($switchrole, $context);
62 // Double check that this role is allowed here
63 require_login($course->id);
65 // reset course page state - this prevents some weird problems ;-)
66 $USER->activitycopy = false;
67 $USER->activitycopycourse = NULL;
68 unset($USER->activitycopyname);
69 unset($SESSION->modform);
70 $USER->editing = 0;
71 $reset_user_allowed_editing = true;
74 //If course is hosted on an external server, redirect to corresponding
75 //url with appropriate authentication attached as parameter
76 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
77 include $CFG->dirroot .'/course/externservercourse.php';
78 if (function_exists('extern_server_course')) {
79 if ($extern_url = extern_server_course($course)) {
80 redirect($extern_url);
86 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
88 add_to_log($course->id, 'course', 'view', "view.php?id=$course->id", "$course->id");
90 $course->format = clean_param($course->format, PARAM_ALPHA);
91 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
92 $course->format = 'weeks'; // Default format is weeks
95 $PAGE->set_pagelayout('course');
96 $PAGE->set_pagetype('course-view-' . $course->format);
97 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
99 if ($reset_user_allowed_editing) {
100 // ugly hack
101 unset($PAGE->_user_allowed_editing);
104 if (!isset($USER->editing)) {
105 $USER->editing = 0;
107 if ($PAGE->user_allowed_editing()) {
108 if (($edit == 1) and confirm_sesskey()) {
109 $USER->editing = 1;
110 // Redirect to site root if Editing is toggled on frontpage
111 if ($course->id == SITEID) {
112 redirect($CFG->wwwroot .'/?redirect=0');
113 } else {
114 redirect($PAGE->url);
116 } else if (($edit == 0) and confirm_sesskey()) {
117 $USER->editing = 0;
118 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
119 $USER->activitycopy = false;
120 $USER->activitycopycourse = NULL;
122 // Redirect to site root if Editing is toggled on frontpage
123 if ($course->id == SITEID) {
124 redirect($CFG->wwwroot .'/?redirect=0');
125 } else {
126 redirect($PAGE->url);
130 if (has_capability('moodle/course:sectionvisibility', $context)) {
131 if ($hide && confirm_sesskey()) {
132 set_section_visible($course->id, $hide, '0');
134 if ($show && confirm_sesskey()) {
135 set_section_visible($course->id, $show, '1');
139 if (has_capability('moodle/course:update', $context)) {
140 if (!empty($section)) {
141 if (!empty($move) and confirm_sesskey()) {
142 if (move_section($course, $section, $move)) {
143 if ($course->id == SITEID) {
144 redirect($CFG->wwwroot . '/?redirect=0');
145 } else {
146 redirect($PAGE->url);
148 } else {
149 echo $OUTPUT->notification('An error occurred while moving a section');
154 } else {
155 $USER->editing = 0;
158 $SESSION->fromdiscussion = $CFG->wwwroot .'/course/view.php?id='. $course->id;
161 if ($course->id == SITEID) {
162 // This course is not a real course.
163 redirect($CFG->wwwroot .'/');
166 // AJAX-capable course format?
167 $useajax = false;
168 $formatajax = course_format_ajax_support($course->format);
170 if (!empty($CFG->enablecourseajax)
171 and $formatajax->capable
172 and !empty($USER->editing)
173 and ajaxenabled($formatajax->testedbrowsers)
174 and $PAGE->theme->enablecourseajax
175 and has_capability('moodle/course:manageactivities', $context)) {
176 $PAGE->requires->yui2_lib('dragdrop');
177 $PAGE->requires->yui2_lib('connection');
178 $PAGE->requires->yui2_lib('selector');
179 $PAGE->requires->js('/lib/ajax/block_classes.js', true);
180 $PAGE->requires->js('/lib/ajax/section_classes.js', true);
182 // Okay, global variable alert. VERY UGLY. We need to create
183 // this object here before the <blockname>_print_block()
184 // function is called, since that function needs to set some
185 // stuff in the javascriptportal object.
186 $COURSE->javascriptportal = new jsportal();
187 $useajax = true;
190 $CFG->blocksdrag = $useajax; // this will add a new class to the header so we can style differently
192 $completion = new completion_info($course);
193 if ($completion->is_enabled() && ajaxenabled()) {
194 $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
195 $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
196 $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
197 $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
199 $PAGE->requires->js_init_call('M.core_completion.init');
202 // We are currently keeping the button here from 1.x to help new teachers figure out
203 // what to do, even though the link also appears in the course admin block. It also
204 // means you can back out of a situation where you removed the admin block. :)
205 if ($PAGE->user_allowed_editing()) {
206 $buttons = $OUTPUT->edit_button(new moodle_url('/course/view.php', array('id' => $course->id)));
207 $PAGE->set_button($buttons);
210 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
211 $PAGE->set_heading($course->fullname);
212 echo $OUTPUT->header();
214 if ($completion->is_enabled() && ajaxenabled()) {
215 // This value tracks whether there has been a dynamic change to the page.
216 // It is used so that if a user does this - (a) set some tickmarks, (b)
217 // go to another page, (c) clicks Back button - the page will
218 // automatically reload. Otherwise it would start with the wrong tick
219 // values.
220 echo html_writer::start_tag('form', array('action'=>'.', 'method'=>'get'));
221 echo html_writer::start_tag('div');
222 echo html_writer::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
223 echo html_writer::end_tag('div');
224 echo html_writer::end_tag('form');
227 // Course wrapper start.
228 echo html_writer::start_tag('div', array('class'=>'course-content'));
230 $modinfo =& get_fast_modinfo($COURSE);
231 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
232 foreach($mods as $modid=>$unused) {
233 if (!isset($modinfo->cms[$modid])) {
234 rebuild_course_cache($course->id);
235 $modinfo =& get_fast_modinfo($COURSE);
236 debugging('Rebuilding course cache', DEBUG_DEVELOPER);
237 break;
241 if (! $sections = get_all_sections($course->id)) { // No sections found
242 // Double-check to be extra sure
243 if (! $section = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>0))) {
244 $section->course = $course->id; // Create a default section.
245 $section->section = 0;
246 $section->visible = 1;
247 $section->summaryformat = FORMAT_HTML;
248 $section->id = $DB->insert_record('course_sections', $section);
250 if (! $sections = get_all_sections($course->id) ) { // Try again
251 print_error('cannotcreateorfindstructs', 'error');
255 // Include the actual course format.
256 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
257 // Content wrapper end.
259 echo html_writer::end_tag('div');
261 // Use AJAX?
262 if ($useajax && has_capability('moodle/course:manageactivities', $context)) {
263 // At the bottom because we want to process sections and activities
264 // after the relevant html has been generated. We're forced to do this
265 // because of the way in which lib/ajax/ajaxcourse.js is written.
266 echo html_writer::script(false, new moodle_url('/lib/ajax/ajaxcourse.js'));
267 $COURSE->javascriptportal->print_javascript($course->id);
271 echo $OUTPUT->footer();