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');
9 $id = optional_param('id', 0, PARAM_INT
);
10 $name = optional_param('name', '', PARAM_RAW
);
11 $edit = optional_param('edit', -1, PARAM_BOOL
);
12 $hide = optional_param('hide', 0, PARAM_INT
);
13 $show = optional_param('show', 0, PARAM_INT
);
14 $idnumber = optional_param('idnumber', '', PARAM_RAW
);
15 $section = optional_param('section', 0, PARAM_INT
);
16 $move = optional_param('move', 0, PARAM_INT
);
17 $marker = optional_param('marker',-1 , PARAM_INT
);
18 $switchrole = optional_param('switchrole',-1, PARAM_INT
);
20 if (empty($id) && empty($name) && empty($idnumber)) {
21 print_error('unspecifycourseid', 'error');
25 if (! ($course = $DB->get_record('course', array('shortname'=>$name)))) {
26 print_error('invalidcoursenameshort', 'error');
28 } else if (!empty($idnumber)) {
29 if (! ($course = $DB->get_record('course', array('idnumber'=>$idnumber)))) {
30 print_error('invalidcourseid', 'error');
33 if (! ($course = $DB->get_record('course', array('id'=>$id)))) {
34 print_error('invalidcourseid', 'error');
38 $PAGE->set_url('/course/view.php', array('id' => $course->id
)); // Defined here to avoid notices on errors etc
40 preload_course_contexts($course->id
);
41 if (!$context = get_context_instance(CONTEXT_COURSE
, $course->id
)) {
42 print_error('nocontext');
45 // Remove any switched roles before checking login
46 if ($switchrole == 0 && confirm_sesskey()) {
47 role_switch($switchrole, $context);
50 require_login($course);
52 // Switchrole - sanity check in cost-order...
53 $reset_user_allowed_editing = false;
54 if ($switchrole > 0 && confirm_sesskey() &&
55 has_capability('moodle/role:switchroles', $context)) {
56 // is this role assignable in this context?
57 // inquiring minds want to know...
58 $aroles = get_switchable_roles($context);
59 if (is_array($aroles) && isset($aroles[$switchrole])) {
60 role_switch($switchrole, $context);
61 // Double check that this role is allowed here
62 require_login($course->id
);
64 // reset course page state - this prevents some weird problems ;-)
65 $USER->activitycopy
= false;
66 $USER->activitycopycourse
= NULL;
67 unset($USER->activitycopyname
);
68 unset($SESSION->modform
);
70 $reset_user_allowed_editing = true;
73 //If course is hosted on an external server, redirect to corresponding
74 //url with appropriate authentication attached as parameter
75 if (file_exists($CFG->dirroot
.'/course/externservercourse.php')) {
76 include $CFG->dirroot
.'/course/externservercourse.php';
77 if (function_exists('extern_server_course')) {
78 if ($extern_url = extern_server_course($course)) {
79 redirect($extern_url);
85 require_once($CFG->dirroot
.'/calendar/lib.php'); /// This is after login because it needs $USER
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) {
100 unset($PAGE->_user_allowed_editing
);
103 if (!isset($USER->editing
)) {
106 if ($PAGE->user_allowed_editing()) {
107 if (($edit == 1) and confirm_sesskey()) {
109 redirect($PAGE->url
);
110 } else if (($edit == 0) and confirm_sesskey()) {
112 if(!empty($USER->activitycopy
) && $USER->activitycopycourse
== $course->id
) {
113 $USER->activitycopy
= false;
114 $USER->activitycopycourse
= NULL;
116 redirect($PAGE->url
);
119 if ($hide && confirm_sesskey()) {
120 set_section_visible($course->id
, $hide, '0');
123 if ($show && confirm_sesskey()) {
124 set_section_visible($course->id
, $show, '1');
127 if (!empty($section)) {
128 if (!empty($move) and confirm_sesskey()) {
129 if (!move_section($course, $section, $move)) {
130 echo $OUTPUT->notification('An error occurred while moving a section');
138 $SESSION->fromdiscussion
= $CFG->wwwroot
.'/course/view.php?id='. $course->id
;
141 if ($course->id
== SITEID
) {
142 // This course is not a real course.
143 redirect($CFG->wwwroot
.'/');
147 // AJAX-capable course format?
149 $ajaxformatfile = $CFG->dirroot
.'/course/format/'.$course->format
.'/ajax.php';
152 if (empty($CFG->disablecourseajax
) and file_exists($ajaxformatfile)) { // Needs to exist otherwise no AJAX by default
154 // TODO: stop abusing CFG global here
155 $CFG->ajaxcapable
= false; // May be overridden later by ajaxformatfile
156 $CFG->ajaxtestedbrowsers
= array(); // May be overridden later by ajaxformatfile
158 require_once($ajaxformatfile);
160 if (!empty($USER->editing
) && $CFG->ajaxcapable
&& has_capability('moodle/course:manageactivities', $context)) {
161 // Course-based switches
163 if (ajaxenabled($CFG->ajaxtestedbrowsers
)) { // Browser, user and site-based switches
164 $PAGE->requires
->yui2_lib('dragdrop');
165 $PAGE->requires
->yui2_lib('connection');
166 $PAGE->requires
->yui2_lib('selector');
167 $PAGE->requires
->js('/lib/ajax/block_classes.js', true);
168 $PAGE->requires
->js('/lib/ajax/section_classes.js', true);
170 // Okay, global variable alert. VERY UGLY. We need to create
171 // this object here before the <blockname>_print_block()
172 // function is called, since that function needs to set some
173 // stuff in the javascriptportal object.
174 $COURSE->javascriptportal
= new jsportal();
180 $CFG->blocksdrag
= $useajax; // this will add a new class to the header so we can style differently
182 $completion = new completion_info($course);
183 if ($completion->is_enabled() && ajaxenabled()) {
184 $PAGE->requires
->string_for_js('completion-title-manual-y', 'completion');
185 $PAGE->requires
->string_for_js('completion-title-manual-n', 'completion');
186 $PAGE->requires
->string_for_js('completion-alt-manual-y', 'completion');
187 $PAGE->requires
->string_for_js('completion-alt-manual-n', 'completion');
189 $PAGE->requires
->js_init_call('M.core_completion.init');
192 // We are currently keeping the button here from 1.x to help new teachers figure out
193 // what to do, even though the link also appears in the course admin block. It also
194 // means you can back out of a situation where you removed the admin block. :)
195 if ($PAGE->user_allowed_editing()) {
196 $buttons = $OUTPUT->edit_button(new moodle_url('/course/view.php', array('id' => $course->id
)));
197 $PAGE->set_button($buttons);
200 $PAGE->set_title(get_string('course') . ': ' . $course->fullname
);
201 $PAGE->set_heading($course->fullname
);
202 echo $OUTPUT->header();
204 if ($completion->is_enabled() && ajaxenabled()) {
205 // This value tracks whether there has been a dynamic change to the page.
206 // It is used so that if a user does this - (a) set some tickmarks, (b)
207 // go to another page, (c) clicks Back button - the page will
208 // automatically reload. Otherwise it would start with the wrong tick
210 echo html_writer
::start_tag('form', array('action'=>'.', 'method'=>'get'));
211 echo html_writer
::start_tag('div');
212 echo html_writer
::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
213 echo html_writer
::end_tag('div');
214 echo html_writer
::end_tag('form');
217 // Course wrapper start.
218 echo html_writer
::start_tag('div', array('class'=>'course-content'));
220 $modinfo =& get_fast_modinfo($COURSE);
221 get_all_mods($course->id
, $mods, $modnames, $modnamesplural, $modnamesused);
222 foreach($mods as $modid=>$unused) {
223 if (!isset($modinfo->cms
[$modid])) {
224 rebuild_course_cache($course->id
);
225 $modinfo =& get_fast_modinfo($COURSE);
226 debugging('Rebuilding course cache', DEBUG_DEVELOPER
);
231 if (! $sections = get_all_sections($course->id
)) { // No sections found
232 // Double-check to be extra sure
233 if (! $section = $DB->get_record('course_sections', array('course'=>$course->id
, 'section'=>0))) {
234 $section->course
= $course->id
; // Create a default section.
235 $section->section
= 0;
236 $section->visible
= 1;
237 $section->summaryformat
= FORMAT_HTML
;
238 $section->id
= $DB->insert_record('course_sections', $section);
240 if (! $sections = get_all_sections($course->id
) ) { // Try again
241 print_error('cannotcreateorfindstructs', 'error');
245 // Include the actual course format.
246 require($CFG->dirroot
.'/course/format/'. $course->format
.'/format.php');
247 // Content wrapper end.
249 echo html_writer
::end_tag('div');
252 if ($useajax && has_capability('moodle/course:manageactivities', $context)) {
253 // At the bottom because we want to process sections and activities
254 // after the relevant html has been generated. We're forced to do this
255 // because of the way in which lib/ajax/ajaxcourse.js is written.
256 echo html_writer
::script(false, new moodle_url('/lib/ajax/ajaxcourse.js'));
257 $COURSE->javascriptportal
->print_javascript($course->id
);
261 echo $OUTPUT->footer();