PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/course/view.php

https://github.com/kpike/moodle
PHP | 257 lines | 180 code | 43 blank | 34 comment | 55 complexity | 4df268a6faf486cd7f3c3ec5b3cb73fa MD5 | raw file
  1. <?php
  2. // Display the course home page.
  3. require_once('../config.php');
  4. require_once('lib.php');
  5. require_once($CFG->dirroot.'/mod/forum/lib.php');
  6. require_once($CFG->libdir.'/completionlib.php');
  7. $id = optional_param('id', 0, PARAM_INT);
  8. $name = optional_param('name', '', PARAM_RAW);
  9. $edit = optional_param('edit', -1, PARAM_BOOL);
  10. $hide = optional_param('hide', 0, PARAM_INT);
  11. $show = optional_param('show', 0, PARAM_INT);
  12. $idnumber = optional_param('idnumber', '', PARAM_RAW);
  13. $section = optional_param('section', 0, PARAM_INT);
  14. $move = optional_param('move', 0, PARAM_INT);
  15. $marker = optional_param('marker',-1 , PARAM_INT);
  16. $switchrole = optional_param('switchrole',-1, PARAM_INT);
  17. if (empty($id) && empty($name) && empty($idnumber)) {
  18. print_error('unspecifycourseid', 'error');
  19. }
  20. if (!empty($name)) {
  21. if (! ($course = $DB->get_record('course', array('shortname'=>$name)))) {
  22. print_error('invalidcoursenameshort', 'error');
  23. }
  24. } else if (!empty($idnumber)) {
  25. if (! ($course = $DB->get_record('course', array('idnumber'=>$idnumber)))) {
  26. print_error('invalidcourseid', 'error');
  27. }
  28. } else {
  29. if (! ($course = $DB->get_record('course', array('id'=>$id)))) {
  30. print_error('invalidcourseid', 'error');
  31. }
  32. }
  33. $PAGE->set_url('/course/view.php', array('id' => $course->id)); // Defined here to avoid notices on errors etc
  34. preload_course_contexts($course->id);
  35. if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
  36. print_error('nocontext');
  37. }
  38. // Remove any switched roles before checking login
  39. if ($switchrole == 0 && confirm_sesskey()) {
  40. role_switch($switchrole, $context);
  41. }
  42. require_login($course);
  43. // Switchrole - sanity check in cost-order...
  44. $reset_user_allowed_editing = false;
  45. if ($switchrole > 0 && confirm_sesskey() &&
  46. has_capability('moodle/role:switchroles', $context)) {
  47. // is this role assignable in this context?
  48. // inquiring minds want to know...
  49. $aroles = get_switchable_roles($context);
  50. if (is_array($aroles) && isset($aroles[$switchrole])) {
  51. role_switch($switchrole, $context);
  52. // Double check that this role is allowed here
  53. require_login($course->id);
  54. }
  55. // reset course page state - this prevents some weird problems ;-)
  56. $USER->activitycopy = false;
  57. $USER->activitycopycourse = NULL;
  58. unset($USER->activitycopyname);
  59. unset($SESSION->modform);
  60. $USER->editing = 0;
  61. $reset_user_allowed_editing = true;
  62. }
  63. //If course is hosted on an external server, redirect to corresponding
  64. //url with appropriate authentication attached as parameter
  65. if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
  66. include $CFG->dirroot .'/course/externservercourse.php';
  67. if (function_exists('extern_server_course')) {
  68. if ($extern_url = extern_server_course($course)) {
  69. redirect($extern_url);
  70. }
  71. }
  72. }
  73. require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
  74. add_to_log($course->id, 'course', 'view', "view.php?id=$course->id", "$course->id");
  75. $course->format = clean_param($course->format, PARAM_ALPHA);
  76. if (!file_exists($CFG->dirroot.'/course/format/'.$course->format.'/format.php')) {
  77. $course->format = 'weeks'; // Default format is weeks
  78. }
  79. $PAGE->set_pagelayout('course');
  80. $PAGE->set_pagetype('course-view-' . $course->format);
  81. $PAGE->set_other_editing_capability('moodle/course:manageactivities');
  82. if ($reset_user_allowed_editing) {
  83. // ugly hack
  84. unset($PAGE->_user_allowed_editing);
  85. }
  86. if (!isset($USER->editing)) {
  87. $USER->editing = 0;
  88. }
  89. if ($PAGE->user_allowed_editing()) {
  90. if (($edit == 1) and confirm_sesskey()) {
  91. $USER->editing = 1;
  92. redirect($PAGE->url);
  93. } else if (($edit == 0) and confirm_sesskey()) {
  94. $USER->editing = 0;
  95. if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
  96. $USER->activitycopy = false;
  97. $USER->activitycopycourse = NULL;
  98. }
  99. redirect($PAGE->url);
  100. }
  101. if ($hide && confirm_sesskey()) {
  102. set_section_visible($course->id, $hide, '0');
  103. }
  104. if ($show && confirm_sesskey()) {
  105. set_section_visible($course->id, $show, '1');
  106. }
  107. if (!empty($section)) {
  108. if (!empty($move) and confirm_sesskey()) {
  109. if (!move_section($course, $section, $move)) {
  110. echo $OUTPUT->notification('An error occurred while moving a section');
  111. }
  112. // Clear the navigation cache at this point so that the affects
  113. // are seen immediately on the navigation.
  114. $PAGE->navigation->clear_cache();
  115. }
  116. }
  117. } else {
  118. $USER->editing = 0;
  119. }
  120. $SESSION->fromdiscussion = $CFG->wwwroot .'/course/view.php?id='. $course->id;
  121. if ($course->id == SITEID) {
  122. // This course is not a real course.
  123. redirect($CFG->wwwroot .'/');
  124. }
  125. // AJAX-capable course format?
  126. $useajax = false;
  127. $formatajax = course_format_ajax_support($course->format);
  128. if (!empty($CFG->enablecourseajax)
  129. and $formatajax->capable
  130. and !empty($USER->editing)
  131. and ajaxenabled($formatajax->testedbrowsers)
  132. and $PAGE->theme->enablecourseajax
  133. and has_capability('moodle/course:manageactivities', $context)) {
  134. $PAGE->requires->yui2_lib('dragdrop');
  135. $PAGE->requires->yui2_lib('connection');
  136. $PAGE->requires->yui2_lib('selector');
  137. $PAGE->requires->js('/lib/ajax/block_classes.js', true);
  138. $PAGE->requires->js('/lib/ajax/section_classes.js', true);
  139. // Okay, global variable alert. VERY UGLY. We need to create
  140. // this object here before the <blockname>_print_block()
  141. // function is called, since that function needs to set some
  142. // stuff in the javascriptportal object.
  143. $COURSE->javascriptportal = new jsportal();
  144. $useajax = true;
  145. }
  146. $CFG->blocksdrag = $useajax; // this will add a new class to the header so we can style differently
  147. $completion = new completion_info($course);
  148. if ($completion->is_enabled() && ajaxenabled()) {
  149. $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
  150. $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
  151. $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
  152. $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
  153. $PAGE->requires->js_init_call('M.core_completion.init');
  154. }
  155. // We are currently keeping the button here from 1.x to help new teachers figure out
  156. // what to do, even though the link also appears in the course admin block. It also
  157. // means you can back out of a situation where you removed the admin block. :)
  158. if ($PAGE->user_allowed_editing()) {
  159. $buttons = $OUTPUT->edit_button(new moodle_url('/course/view.php', array('id' => $course->id)));
  160. $PAGE->set_button($buttons);
  161. }
  162. $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
  163. $PAGE->set_heading($course->fullname);
  164. echo $OUTPUT->header();
  165. if ($completion->is_enabled() && ajaxenabled()) {
  166. // This value tracks whether there has been a dynamic change to the page.
  167. // It is used so that if a user does this - (a) set some tickmarks, (b)
  168. // go to another page, (c) clicks Back button - the page will
  169. // automatically reload. Otherwise it would start with the wrong tick
  170. // values.
  171. echo html_writer::start_tag('form', array('action'=>'.', 'method'=>'get'));
  172. echo html_writer::start_tag('div');
  173. echo html_writer::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
  174. echo html_writer::end_tag('div');
  175. echo html_writer::end_tag('form');
  176. }
  177. // Course wrapper start.
  178. echo html_writer::start_tag('div', array('class'=>'course-content'));
  179. $modinfo =& get_fast_modinfo($COURSE);
  180. get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
  181. foreach($mods as $modid=>$unused) {
  182. if (!isset($modinfo->cms[$modid])) {
  183. rebuild_course_cache($course->id);
  184. $modinfo =& get_fast_modinfo($COURSE);
  185. debugging('Rebuilding course cache', DEBUG_DEVELOPER);
  186. break;
  187. }
  188. }
  189. if (! $sections = get_all_sections($course->id)) { // No sections found
  190. // Double-check to be extra sure
  191. if (! $section = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>0))) {
  192. $section->course = $course->id; // Create a default section.
  193. $section->section = 0;
  194. $section->visible = 1;
  195. $section->summaryformat = FORMAT_HTML;
  196. $section->id = $DB->insert_record('course_sections', $section);
  197. }
  198. if (! $sections = get_all_sections($course->id) ) { // Try again
  199. print_error('cannotcreateorfindstructs', 'error');
  200. }
  201. }
  202. // Include the actual course format.
  203. require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
  204. // Content wrapper end.
  205. echo html_writer::end_tag('div');
  206. // Use AJAX?
  207. if ($useajax && has_capability('moodle/course:manageactivities', $context)) {
  208. // At the bottom because we want to process sections and activities
  209. // after the relevant html has been generated. We're forced to do this
  210. // because of the way in which lib/ajax/ajaxcourse.js is written.
  211. echo html_writer::script(false, new moodle_url('/lib/ajax/ajaxcourse.js'));
  212. $COURSE->javascriptportal->print_javascript($course->id);
  213. }
  214. echo $OUTPUT->footer();