PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/course/edit.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 244 lines | 158 code | 29 blank | 57 comment | 39 complexity | 45b2d8c00f17ee9ca0a50faf0d1ebe05 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Edit course settings
  18. *
  19. * @package core_course
  20. * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require_once('../config.php');
  24. require_once('lib.php');
  25. require_once('edit_form.php');
  26. $id = optional_param('id', 0, PARAM_INT); // Course id.
  27. $categoryid = optional_param('category', 0, PARAM_INT); // Course category - can be changed in edit form.
  28. $returnto = optional_param('returnto', 0, PARAM_ALPHANUM); // Generic navigation return page switch.
  29. $returnurl = optional_param('returnurl', '', PARAM_LOCALURL); // A return URL. returnto must also be set to 'url'.
  30. if ($returnto === 'url' && confirm_sesskey() && $returnurl) {
  31. // If returnto is 'url' then $returnurl may be used as the destination to return to after saving or cancelling.
  32. // Sesskey must be specified, and would be set by the form anyway.
  33. $returnurl = new moodle_url($returnurl);
  34. } else {
  35. if (!empty($id)) {
  36. $returnurl = new moodle_url($CFG->wwwroot . '/course/view.php', array('id' => $id));
  37. } else {
  38. $returnurl = new moodle_url($CFG->wwwroot . '/course/');
  39. }
  40. if ($returnto !== 0) {
  41. switch ($returnto) {
  42. case 'category':
  43. $returnurl = new moodle_url($CFG->wwwroot . '/course/index.php', array('categoryid' => $categoryid));
  44. break;
  45. case 'catmanage':
  46. $returnurl = new moodle_url($CFG->wwwroot . '/course/management.php', array('categoryid' => $categoryid));
  47. break;
  48. case 'topcatmanage':
  49. $returnurl = new moodle_url($CFG->wwwroot . '/course/management.php');
  50. break;
  51. case 'topcat':
  52. $returnurl = new moodle_url($CFG->wwwroot . '/course/');
  53. break;
  54. case 'pending':
  55. $returnurl = new moodle_url($CFG->wwwroot . '/course/pending.php');
  56. break;
  57. }
  58. }
  59. }
  60. $PAGE->set_pagelayout('admin');
  61. if ($id) {
  62. $pageparams = array('id' => $id);
  63. } else {
  64. $pageparams = array('category' => $categoryid);
  65. }
  66. if ($returnto !== 0) {
  67. $pageparams['returnto'] = $returnto;
  68. if ($returnto === 'url' && $returnurl) {
  69. $pageparams['returnurl'] = $returnurl;
  70. }
  71. }
  72. $PAGE->set_url('/course/edit.php', $pageparams);
  73. // Basic access control checks.
  74. if ($id) {
  75. // Editing course.
  76. if ($id == SITEID){
  77. // Don't allow editing of 'site course' using this from.
  78. print_error('cannoteditsiteform');
  79. }
  80. // Login to the course and retrieve also all fields defined by course format.
  81. $course = get_course($id);
  82. require_login($course);
  83. $course = course_get_format($course)->get_course();
  84. $category = $DB->get_record('course_categories', array('id'=>$course->category), '*', MUST_EXIST);
  85. $coursecontext = context_course::instance($course->id);
  86. require_capability('moodle/course:update', $coursecontext);
  87. } else if ($categoryid) {
  88. // Creating new course in this category.
  89. $course = null;
  90. require_login();
  91. $category = $DB->get_record('course_categories', array('id'=>$categoryid), '*', MUST_EXIST);
  92. $catcontext = context_coursecat::instance($category->id);
  93. require_capability('moodle/course:create', $catcontext);
  94. $PAGE->set_context($catcontext);
  95. } else {
  96. require_login();
  97. print_error('needcoursecategroyid');
  98. }
  99. // Prepare course and the editor.
  100. $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true);
  101. $overviewfilesoptions = course_overviewfiles_options($course);
  102. if (!empty($course)) {
  103. // Add context for editor.
  104. $editoroptions['context'] = $coursecontext;
  105. $editoroptions['subdirs'] = file_area_contains_subdirs($coursecontext, 'course', 'summary', 0);
  106. $course = file_prepare_standard_editor($course, 'summary', $editoroptions, $coursecontext, 'course', 'summary', 0);
  107. if ($overviewfilesoptions) {
  108. file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, $coursecontext, 'course', 'overviewfiles', 0);
  109. }
  110. // Inject current aliases.
  111. $aliases = $DB->get_records('role_names', array('contextid'=>$coursecontext->id));
  112. foreach($aliases as $alias) {
  113. $course->{'role_'.$alias->roleid} = $alias->name;
  114. }
  115. // Populate course tags.
  116. $course->tags = core_tag_tag::get_item_tags_array('core', 'course', $course->id);
  117. } else {
  118. // Editor should respect category context if course context is not set.
  119. $editoroptions['context'] = $catcontext;
  120. $editoroptions['subdirs'] = 0;
  121. $course = file_prepare_standard_editor($course, 'summary', $editoroptions, null, 'course', 'summary', null);
  122. if ($overviewfilesoptions) {
  123. file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, null, 'course', 'overviewfiles', 0);
  124. }
  125. }
  126. // First create the form.
  127. $args = array(
  128. 'course' => $course,
  129. 'category' => $category,
  130. 'editoroptions' => $editoroptions,
  131. 'returnto' => $returnto,
  132. 'returnurl' => $returnurl
  133. );
  134. $editform = new course_edit_form(null, $args);
  135. if ($editform->is_cancelled()) {
  136. // The form has been cancelled, take them back to what ever the return to is.
  137. redirect($returnurl);
  138. } else if ($data = $editform->get_data()) {
  139. // Process data if submitted.
  140. if (empty($course->id)) {
  141. // In creating the course.
  142. $course = create_course($data, $editoroptions);
  143. // Get the context of the newly created course.
  144. $context = context_course::instance($course->id, MUST_EXIST);
  145. if (!empty($CFG->creatornewroleid) and !is_viewing($context, NULL, 'moodle/role:assign') and !is_enrolled($context, NULL, 'moodle/role:assign')) {
  146. // Deal with course creators - enrol them internally with default role.
  147. enrol_try_internal_enrol($course->id, $USER->id, $CFG->creatornewroleid);
  148. }
  149. // The URL to take them to if they chose save and display.
  150. $courseurl = new moodle_url('/course/view.php', array('id' => $course->id));
  151. // If they choose to save and display, and they are not enrolled take them to the enrolments page instead.
  152. if (!is_enrolled($context) && isset($data->saveanddisplay)) {
  153. // Redirect to manual enrolment page if possible.
  154. $instances = enrol_get_instances($course->id, true);
  155. foreach($instances as $instance) {
  156. if ($plugin = enrol_get_plugin($instance->enrol)) {
  157. if ($plugin->get_manual_enrol_link($instance)) {
  158. // We know that the ajax enrol UI will have an option to enrol.
  159. $courseurl = new moodle_url('/user/index.php', array('id' => $course->id, 'newcourse' => 1));
  160. break;
  161. }
  162. }
  163. }
  164. }
  165. } else {
  166. // Save any changes to the files used in the editor.
  167. update_course($data, $editoroptions);
  168. // Set the URL to take them too if they choose save and display.
  169. $courseurl = new moodle_url('/course/view.php', array('id' => $course->id));
  170. }
  171. if (isset($data->saveanddisplay)) {
  172. // Redirect user to newly created/updated course.
  173. redirect($courseurl);
  174. } else {
  175. // Save and return. Take them back to wherever.
  176. redirect($returnurl);
  177. }
  178. }
  179. // Print the form.
  180. $site = get_site();
  181. $streditcoursesettings = get_string("editcoursesettings");
  182. $straddnewcourse = get_string("addnewcourse");
  183. $stradministration = get_string("administration");
  184. $strcategories = get_string("categories");
  185. if (!empty($course->id)) {
  186. // Navigation note: The user is editing a course, the course will exist within the navigation and settings.
  187. // The navigation will automatically find the Edit settings page under course navigation.
  188. $pagedesc = $streditcoursesettings;
  189. $title = $streditcoursesettings;
  190. $fullname = $course->fullname;
  191. } else {
  192. // The user is adding a course, this page isn't presented in the site navigation/admin.
  193. // Adding a new course is part of course category management territory.
  194. // We'd prefer to use the management interface URL without args.
  195. $managementurl = new moodle_url('/course/management.php');
  196. // These are the caps required in order to see the management interface.
  197. $managementcaps = array('moodle/category:manage', 'moodle/course:create');
  198. if ($categoryid && !has_any_capability($managementcaps, context_system::instance())) {
  199. // If the user doesn't have either manage caps then they can only manage within the given category.
  200. $managementurl->param('categoryid', $categoryid);
  201. }
  202. // Because the course category management interfaces are buried in the admin tree and that is loaded by ajax
  203. // we need to manually tell the navigation we need it loaded. The second arg does this.
  204. navigation_node::override_active_url($managementurl, true);
  205. $pagedesc = $straddnewcourse;
  206. $title = "$site->shortname: $straddnewcourse";
  207. $fullname = $site->fullname;
  208. $PAGE->navbar->add($pagedesc);
  209. }
  210. $PAGE->set_title($title);
  211. $PAGE->set_heading($fullname);
  212. echo $OUTPUT->header();
  213. echo $OUTPUT->heading($pagedesc);
  214. $editform->display();
  215. echo $OUTPUT->footer();