Merge branch 'wip-mdl-27562' of git://github.com/rajeshtaneja/moodle
[moodle.git] / course / view.php
blob7a4d7aa37ab531834b4b1fe875b115317d058295
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 preload_course_contexts($course->id);
42 $context = context_course::instance($course->id, MUST_EXIST);
44 // Remove any switched roles before checking login
45 if ($switchrole == 0 && confirm_sesskey()) {
46 role_switch($switchrole, $context);
49 require_login($course);
51 // Switchrole - sanity check in cost-order...
52 $reset_user_allowed_editing = false;
53 if ($switchrole > 0 && confirm_sesskey() &&
54 has_capability('moodle/role:switchroles', $context)) {
55 // is this role assignable in this context?
56 // inquiring minds want to know...
57 $aroles = get_switchable_roles($context);
58 if (is_array($aroles) && isset($aroles[$switchrole])) {
59 role_switch($switchrole, $context);
60 // Double check that this role is allowed here
61 require_login($course);
63 // reset course page state - this prevents some weird problems ;-)
64 $USER->activitycopy = false;
65 $USER->activitycopycourse = NULL;
66 unset($USER->activitycopyname);
67 unset($SESSION->modform);
68 $USER->editing = 0;
69 $reset_user_allowed_editing = true;
72 //If course is hosted on an external server, redirect to corresponding
73 //url with appropriate authentication attached as parameter
74 if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
75 include $CFG->dirroot .'/course/externservercourse.php';
76 if (function_exists('extern_server_course')) {
77 if ($extern_url = extern_server_course($course)) {
78 redirect($extern_url);
84 require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
86 //TODO: danp do we need different urls?
87 add_to_log($course->id, 'course', 'view', "view.php?id=$course->id", "$course->id");
89 $course->format = clean_param($course->format, PARAM_ALPHA);
90 if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
91 $course->format = 'weeks'; // Default format is weeks
94 $PAGE->set_pagelayout('course');
95 $PAGE->set_pagetype('course-view-' . $course->format);
96 $PAGE->set_other_editing_capability('moodle/course:manageactivities');
98 if ($reset_user_allowed_editing) {
99 // ugly hack
100 unset($PAGE->_user_allowed_editing);
103 if (!isset($USER->editing)) {
104 $USER->editing = 0;
106 if ($PAGE->user_allowed_editing()) {
107 if (($edit == 1) and confirm_sesskey()) {
108 $USER->editing = 1;
109 // Redirect to site root if Editing is toggled on frontpage
110 if ($course->id == SITEID) {
111 redirect($CFG->wwwroot .'/?redirect=0');
112 } else {
113 redirect($PAGE->url);
115 } else if (($edit == 0) and confirm_sesskey()) {
116 $USER->editing = 0;
117 if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
118 $USER->activitycopy = false;
119 $USER->activitycopycourse = NULL;
121 // Redirect to site root if Editing is toggled on frontpage
122 if ($course->id == SITEID) {
123 redirect($CFG->wwwroot .'/?redirect=0');
124 } else {
125 redirect($PAGE->url);
129 if (has_capability('moodle/course:update', $context)) {
130 if ($hide && confirm_sesskey()) {
131 set_section_visible($course->id, $hide, '0');
132 redirect($PAGE->url);
135 if ($show && confirm_sesskey()) {
136 set_section_visible($course->id, $show, '1');
137 redirect($PAGE->url);
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(course_get_url($course));
148 } else {
149 echo $OUTPUT->notification('An error occurred while moving a section');
154 } else {
155 $USER->editing = 0;
158 $SESSION->fromdiscussion = $PAGE->url->out(false);
161 if ($course->id == SITEID) {
162 // This course is not a real course.
163 redirect($CFG->wwwroot .'/');
166 $completion = new completion_info($course);
167 if ($completion->is_enabled() && ajaxenabled()) {
168 $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
169 $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
170 $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
171 $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
173 $PAGE->requires->js_init_call('M.core_completion.init');
176 // We are currently keeping the button here from 1.x to help new teachers figure out
177 // what to do, even though the link also appears in the course admin block. It also
178 // means you can back out of a situation where you removed the admin block. :)
179 if ($PAGE->user_allowed_editing()) {
180 $buttons = $OUTPUT->edit_button($PAGE->url);
181 $PAGE->set_button($buttons);
184 $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
185 $PAGE->set_heading($course->fullname);
186 echo $OUTPUT->header();
188 if ($completion->is_enabled() && ajaxenabled()) {
189 // This value tracks whether there has been a dynamic change to the page.
190 // It is used so that if a user does this - (a) set some tickmarks, (b)
191 // go to another page, (c) clicks Back button - the page will
192 // automatically reload. Otherwise it would start with the wrong tick
193 // values.
194 echo html_writer::start_tag('form', array('action'=>'.', 'method'=>'get'));
195 echo html_writer::start_tag('div');
196 echo html_writer::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
197 echo html_writer::end_tag('div');
198 echo html_writer::end_tag('form');
201 // Course wrapper start.
202 echo html_writer::start_tag('div', array('class'=>'course-content'));
204 $modinfo = get_fast_modinfo($COURSE);
205 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
206 foreach($mods as $modid=>$unused) {
207 if (!isset($modinfo->cms[$modid])) {
208 rebuild_course_cache($course->id);
209 $modinfo = get_fast_modinfo($COURSE);
210 debugging('Rebuilding course cache', DEBUG_DEVELOPER);
211 break;
215 if (! $sections = get_all_sections($course->id)) { // No sections found
216 // Double-check to be extra sure
217 if (! $section = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>0))) {
218 $section->course = $course->id; // Create a default section.
219 $section->section = 0;
220 $section->visible = 1;
221 $section->summaryformat = FORMAT_HTML;
222 $section->id = $DB->insert_record('course_sections', $section);
224 if (! $sections = get_all_sections($course->id) ) { // Try again
225 print_error('cannotcreateorfindstructs', 'error');
229 // CAUTION, hacky fundamental variable defintion to follow!
230 // Note that because of the way course fromats are constructed though
231 // inclusion we pass parameters around this way..
232 $displaysection = $section;
234 // Include the actual course format.
235 require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
236 // Content wrapper end.
238 echo html_writer::end_tag('div');
240 // Include the command toolbox YUI module
241 include_course_ajax($course, $modnamesused);
243 echo $OUTPUT->footer();