PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/course/view.php

https://github.com/mchurchward/moodle
PHP | 303 lines | 202 code | 49 blank | 52 comment | 70 complexity | 9983b3606fc4e300a1ba021cc4b83bf1 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->libdir.'/completionlib.php');
  6. $id = optional_param('id', 0, PARAM_INT);
  7. $name = optional_param('name', '', PARAM_TEXT);
  8. $edit = optional_param('edit', -1, PARAM_BOOL);
  9. $hide = optional_param('hide', 0, PARAM_INT);
  10. $show = optional_param('show', 0, PARAM_INT);
  11. $idnumber = optional_param('idnumber', '', PARAM_RAW);
  12. $sectionid = optional_param('sectionid', 0, PARAM_INT);
  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); // Deprecated, use course/switchrole.php instead.
  17. $return = optional_param('return', 0, PARAM_LOCALURL);
  18. $params = array();
  19. if (!empty($name)) {
  20. $params = array('shortname' => $name);
  21. } else if (!empty($idnumber)) {
  22. $params = array('idnumber' => $idnumber);
  23. } else if (!empty($id)) {
  24. $params = array('id' => $id);
  25. }else {
  26. print_error('unspecifycourseid', 'error');
  27. }
  28. $course = $DB->get_record('course', $params, '*', MUST_EXIST);
  29. $urlparams = array('id' => $course->id);
  30. // Sectionid should get priority over section number
  31. if ($sectionid) {
  32. $section = $DB->get_field('course_sections', 'section', array('id' => $sectionid, 'course' => $course->id), MUST_EXIST);
  33. }
  34. if ($section) {
  35. $urlparams['section'] = $section;
  36. }
  37. $PAGE->set_url('/course/view.php', $urlparams); // Defined here to avoid notices on errors etc
  38. // Prevent caching of this page to stop confusion when changing page after making AJAX changes
  39. $PAGE->set_cacheable(false);
  40. context_helper::preload_course($course->id);
  41. $context = context_course::instance($course->id, MUST_EXIST);
  42. // Remove any switched roles before checking login
  43. if ($switchrole == 0 && confirm_sesskey()) {
  44. role_switch($switchrole, $context);
  45. }
  46. require_login($course);
  47. // Switchrole - sanity check in cost-order...
  48. $reset_user_allowed_editing = false;
  49. if ($switchrole > 0 && confirm_sesskey() &&
  50. has_capability('moodle/role:switchroles', $context)) {
  51. // is this role assignable in this context?
  52. // inquiring minds want to know...
  53. $aroles = get_switchable_roles($context);
  54. if (is_array($aroles) && isset($aroles[$switchrole])) {
  55. role_switch($switchrole, $context);
  56. // Double check that this role is allowed here
  57. require_login($course);
  58. }
  59. // reset course page state - this prevents some weird problems ;-)
  60. $USER->activitycopy = false;
  61. $USER->activitycopycourse = NULL;
  62. unset($USER->activitycopyname);
  63. unset($SESSION->modform);
  64. $USER->editing = 0;
  65. $reset_user_allowed_editing = true;
  66. }
  67. //If course is hosted on an external server, redirect to corresponding
  68. //url with appropriate authentication attached as parameter
  69. if (file_exists($CFG->dirroot .'/course/externservercourse.php')) {
  70. include $CFG->dirroot .'/course/externservercourse.php';
  71. if (function_exists('extern_server_course')) {
  72. if ($extern_url = extern_server_course($course)) {
  73. redirect($extern_url);
  74. }
  75. }
  76. }
  77. require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER
  78. // Must set layout before gettting section info. See MDL-47555.
  79. $PAGE->set_pagelayout('course');
  80. if ($section and $section > 0) {
  81. // Get section details and check it exists.
  82. $modinfo = get_fast_modinfo($course);
  83. $coursesections = $modinfo->get_section_info($section, MUST_EXIST);
  84. // Check user is allowed to see it.
  85. if (!$coursesections->uservisible) {
  86. // Check if coursesection has conditions affecting availability and if
  87. // so, output availability info.
  88. if ($coursesections->visible && $coursesections->availableinfo) {
  89. $sectionname = get_section_name($course, $coursesections);
  90. $message = get_string('notavailablecourse', '', $sectionname);
  91. redirect(course_get_url($course), $message, null, \core\output\notification::NOTIFY_ERROR);
  92. } else {
  93. // Note: We actually already know they don't have this capability
  94. // or uservisible would have been true; this is just to get the
  95. // correct error message shown.
  96. require_capability('moodle/course:viewhiddensections', $context);
  97. }
  98. }
  99. }
  100. // Fix course format if it is no longer installed
  101. $course->format = course_get_format($course)->get_format();
  102. $PAGE->set_pagetype('course-view-' . $course->format);
  103. $PAGE->set_other_editing_capability('moodle/course:update');
  104. $PAGE->set_other_editing_capability('moodle/course:manageactivities');
  105. $PAGE->set_other_editing_capability('moodle/course:activityvisibility');
  106. if (course_format_uses_sections($course->format)) {
  107. $PAGE->set_other_editing_capability('moodle/course:sectionvisibility');
  108. $PAGE->set_other_editing_capability('moodle/course:movesections');
  109. }
  110. // Preload course format renderer before output starts.
  111. // This is a little hacky but necessary since
  112. // format.php is not included until after output starts
  113. if (file_exists($CFG->dirroot.'/course/format/'.$course->format.'/renderer.php')) {
  114. require_once($CFG->dirroot.'/course/format/'.$course->format.'/renderer.php');
  115. if (class_exists('format_'.$course->format.'_renderer')) {
  116. // call get_renderer only if renderer is defined in format plugin
  117. // otherwise an exception would be thrown
  118. $PAGE->get_renderer('format_'. $course->format);
  119. }
  120. }
  121. if ($reset_user_allowed_editing) {
  122. // ugly hack
  123. unset($PAGE->_user_allowed_editing);
  124. }
  125. if (!isset($USER->editing)) {
  126. $USER->editing = 0;
  127. }
  128. if ($PAGE->user_allowed_editing()) {
  129. if (($edit == 1) and confirm_sesskey()) {
  130. $USER->editing = 1;
  131. // Redirect to site root if Editing is toggled on frontpage
  132. if ($course->id == SITEID) {
  133. redirect($CFG->wwwroot .'/?redirect=0');
  134. } else if (!empty($return)) {
  135. redirect($CFG->wwwroot . $return);
  136. } else {
  137. $url = new moodle_url($PAGE->url, array('notifyeditingon' => 1));
  138. redirect($url);
  139. }
  140. } else if (($edit == 0) and confirm_sesskey()) {
  141. $USER->editing = 0;
  142. if(!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
  143. $USER->activitycopy = false;
  144. $USER->activitycopycourse = NULL;
  145. }
  146. // Redirect to site root if Editing is toggled on frontpage
  147. if ($course->id == SITEID) {
  148. redirect($CFG->wwwroot .'/?redirect=0');
  149. } else if (!empty($return)) {
  150. redirect($CFG->wwwroot . $return);
  151. } else {
  152. redirect($PAGE->url);
  153. }
  154. }
  155. if (has_capability('moodle/course:sectionvisibility', $context)) {
  156. if ($hide && confirm_sesskey()) {
  157. set_section_visible($course->id, $hide, '0');
  158. redirect($PAGE->url);
  159. }
  160. if ($show && confirm_sesskey()) {
  161. set_section_visible($course->id, $show, '1');
  162. redirect($PAGE->url);
  163. }
  164. }
  165. if (!empty($section) && !empty($move) &&
  166. has_capability('moodle/course:movesections', $context) && confirm_sesskey()) {
  167. $destsection = $section + $move;
  168. if (move_section_to($course, $section, $destsection)) {
  169. if ($course->id == SITEID) {
  170. redirect($CFG->wwwroot . '/?redirect=0');
  171. } else {
  172. redirect(course_get_url($course));
  173. }
  174. } else {
  175. echo $OUTPUT->notification('An error occurred while moving a section');
  176. }
  177. }
  178. } else {
  179. $USER->editing = 0;
  180. }
  181. $SESSION->fromdiscussion = $PAGE->url->out(false);
  182. if ($course->id == SITEID) {
  183. // This course is not a real course.
  184. redirect($CFG->wwwroot .'/');
  185. }
  186. $completion = new completion_info($course);
  187. if ($completion->is_enabled()) {
  188. $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
  189. $PAGE->requires->string_for_js('completion-alt-manual-n', 'completion');
  190. $PAGE->requires->js_init_call('M.core_completion.init');
  191. }
  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($PAGE->url);
  197. $PAGE->set_button($buttons);
  198. }
  199. // If viewing a section, make the title more specific
  200. if ($section and $section > 0 and course_format_uses_sections($course->format)) {
  201. $sectionname = get_string('sectionname', "format_$course->format");
  202. $sectiontitle = get_section_name($course, $section);
  203. $PAGE->set_title(get_string('coursesectiontitle', 'moodle', array('course' => $course->fullname, 'sectiontitle' => $sectiontitle, 'sectionname' => $sectionname)));
  204. } else {
  205. $PAGE->set_title(get_string('coursetitle', 'moodle', array('course' => $course->fullname)));
  206. }
  207. $PAGE->set_heading($course->fullname);
  208. echo $OUTPUT->header();
  209. if ($USER->editing == 1) {
  210. // MDL-65321 The backup libraries are quite heavy, only require the bare minimum.
  211. require_once($CFG->dirroot . '/backup/util/helper/async_helper.class.php');
  212. if (async_helper::is_async_pending($id, 'course', 'backup')) {
  213. echo $OUTPUT->notification(get_string('pendingasyncedit', 'backup'), 'warning');
  214. }
  215. }
  216. if ($completion->is_enabled()) {
  217. // This value tracks whether there has been a dynamic change to the page.
  218. // It is used so that if a user does this - (a) set some tickmarks, (b)
  219. // go to another page, (c) clicks Back button - the page will
  220. // automatically reload. Otherwise it would start with the wrong tick
  221. // values.
  222. echo html_writer::start_tag('form', array('action'=>'.', 'method'=>'get'));
  223. echo html_writer::start_tag('div');
  224. echo html_writer::empty_tag('input', array('type'=>'hidden', 'id'=>'completion_dynamic_change', 'name'=>'completion_dynamic_change', 'value'=>'0'));
  225. echo html_writer::end_tag('div');
  226. echo html_writer::end_tag('form');
  227. }
  228. // Course wrapper start.
  229. echo html_writer::start_tag('div', array('class'=>'course-content'));
  230. // make sure that section 0 exists (this function will create one if it is missing)
  231. course_create_sections_if_missing($course, 0);
  232. // get information about course modules and existing module types
  233. // format.php in course formats may rely on presence of these variables
  234. $modinfo = get_fast_modinfo($course);
  235. $modnames = get_module_types_names();
  236. $modnamesplural = get_module_types_names(true);
  237. $modnamesused = $modinfo->get_used_module_names();
  238. $mods = $modinfo->get_cms();
  239. $sections = $modinfo->get_section_info_all();
  240. // CAUTION, hacky fundamental variable defintion to follow!
  241. // Note that because of the way course fromats are constructed though
  242. // inclusion we pass parameters around this way..
  243. $displaysection = $section;
  244. // Include the actual course format.
  245. require($CFG->dirroot .'/course/format/'. $course->format .'/format.php');
  246. // Content wrapper end.
  247. echo html_writer::end_tag('div');
  248. // Trigger course viewed event.
  249. // We don't trust $context here. Course format inclusion above executes in the global space. We can't assume
  250. // anything after that point.
  251. course_view(context_course::instance($course->id), $section);
  252. // Include course AJAX
  253. include_course_ajax($course, $modnamesused);
  254. echo $OUTPUT->footer();