PageRenderTime 54ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/course/edit.php

https://gitlab.com/JrLucena/moodle
PHP | 241 lines | 155 code | 29 blank | 57 comment | 39 complexity | 78bb96d29ad6eb93ef34cc8ea3d23394 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. }
  55. }
  56. }
  57. $PAGE->set_pagelayout('admin');
  58. if ($id) {
  59. $pageparams = array('id' => $id);
  60. } else {
  61. $pageparams = array('category' => $categoryid);
  62. }
  63. if ($returnto !== 0) {
  64. $pageparams['returnto'] = $returnto;
  65. if ($returnto === 'url' && $returnurl) {
  66. $pageparams['returnurl'] = $returnurl;
  67. }
  68. }
  69. $PAGE->set_url('/course/edit.php', $pageparams);
  70. // Basic access control checks.
  71. if ($id) {
  72. // Editing course.
  73. if ($id == SITEID){
  74. // Don't allow editing of 'site course' using this from.
  75. print_error('cannoteditsiteform');
  76. }
  77. // Login to the course and retrieve also all fields defined by course format.
  78. $course = get_course($id);
  79. require_login($course);
  80. $course = course_get_format($course)->get_course();
  81. $category = $DB->get_record('course_categories', array('id'=>$course->category), '*', MUST_EXIST);
  82. $coursecontext = context_course::instance($course->id);
  83. require_capability('moodle/course:update', $coursecontext);
  84. } else if ($categoryid) {
  85. // Creating new course in this category.
  86. $course = null;
  87. require_login();
  88. $category = $DB->get_record('course_categories', array('id'=>$categoryid), '*', MUST_EXIST);
  89. $catcontext = context_coursecat::instance($category->id);
  90. require_capability('moodle/course:create', $catcontext);
  91. $PAGE->set_context($catcontext);
  92. } else {
  93. require_login();
  94. print_error('needcoursecategroyid');
  95. }
  96. // Prepare course and the editor.
  97. $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true);
  98. $overviewfilesoptions = course_overviewfiles_options($course);
  99. if (!empty($course)) {
  100. // Add context for editor.
  101. $editoroptions['context'] = $coursecontext;
  102. $editoroptions['subdirs'] = file_area_contains_subdirs($coursecontext, 'course', 'summary', 0);
  103. $course = file_prepare_standard_editor($course, 'summary', $editoroptions, $coursecontext, 'course', 'summary', 0);
  104. if ($overviewfilesoptions) {
  105. file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, $coursecontext, 'course', 'overviewfiles', 0);
  106. }
  107. // Inject current aliases.
  108. $aliases = $DB->get_records('role_names', array('contextid'=>$coursecontext->id));
  109. foreach($aliases as $alias) {
  110. $course->{'role_'.$alias->roleid} = $alias->name;
  111. }
  112. // Populate course tags.
  113. $course->tags = core_tag_tag::get_item_tags_array('core', 'course', $course->id);
  114. } else {
  115. // Editor should respect category context if course context is not set.
  116. $editoroptions['context'] = $catcontext;
  117. $editoroptions['subdirs'] = 0;
  118. $course = file_prepare_standard_editor($course, 'summary', $editoroptions, null, 'course', 'summary', null);
  119. if ($overviewfilesoptions) {
  120. file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, null, 'course', 'overviewfiles', 0);
  121. }
  122. }
  123. // First create the form.
  124. $args = array(
  125. 'course' => $course,
  126. 'category' => $category,
  127. 'editoroptions' => $editoroptions,
  128. 'returnto' => $returnto,
  129. 'returnurl' => $returnurl
  130. );
  131. $editform = new course_edit_form(null, $args);
  132. if ($editform->is_cancelled()) {
  133. // The form has been cancelled, take them back to what ever the return to is.
  134. redirect($returnurl);
  135. } else if ($data = $editform->get_data()) {
  136. // Process data if submitted.
  137. if (empty($course->id)) {
  138. // In creating the course.
  139. $course = create_course($data, $editoroptions);
  140. // Get the context of the newly created course.
  141. $context = context_course::instance($course->id, MUST_EXIST);
  142. if (!empty($CFG->creatornewroleid) and !is_viewing($context, NULL, 'moodle/role:assign') and !is_enrolled($context, NULL, 'moodle/role:assign')) {
  143. // Deal with course creators - enrol them internally with default role.
  144. enrol_try_internal_enrol($course->id, $USER->id, $CFG->creatornewroleid);
  145. }
  146. // The URL to take them to if they chose save and display.
  147. $courseurl = new moodle_url('/course/view.php', array('id' => $course->id));
  148. // If they choose to save and display, and they are not enrolled take them to the enrolments page instead.
  149. if (!is_enrolled($context) && isset($data->saveanddisplay)) {
  150. // Redirect to manual enrolment page if possible.
  151. $instances = enrol_get_instances($course->id, true);
  152. foreach($instances as $instance) {
  153. if ($plugin = enrol_get_plugin($instance->enrol)) {
  154. if ($plugin->get_manual_enrol_link($instance)) {
  155. // We know that the ajax enrol UI will have an option to enrol.
  156. $courseurl = new moodle_url('/enrol/users.php', array('id' => $course->id, 'newcourse' => 1));
  157. break;
  158. }
  159. }
  160. }
  161. }
  162. } else {
  163. // Save any changes to the files used in the editor.
  164. update_course($data, $editoroptions);
  165. // Set the URL to take them too if they choose save and display.
  166. $courseurl = new moodle_url('/course/view.php', array('id' => $course->id));
  167. }
  168. if (isset($data->saveanddisplay)) {
  169. // Redirect user to newly created/updated course.
  170. redirect($courseurl);
  171. } else {
  172. // Save and return. Take them back to wherever.
  173. redirect($returnurl);
  174. }
  175. }
  176. // Print the form.
  177. $site = get_site();
  178. $streditcoursesettings = get_string("editcoursesettings");
  179. $straddnewcourse = get_string("addnewcourse");
  180. $stradministration = get_string("administration");
  181. $strcategories = get_string("categories");
  182. if (!empty($course->id)) {
  183. // Navigation note: The user is editing a course, the course will exist within the navigation and settings.
  184. // The navigation will automatically find the Edit settings page under course navigation.
  185. $pagedesc = $streditcoursesettings;
  186. $title = $streditcoursesettings;
  187. $fullname = $course->fullname;
  188. } else {
  189. // The user is adding a course, this page isn't presented in the site navigation/admin.
  190. // Adding a new course is part of course category management territory.
  191. // We'd prefer to use the management interface URL without args.
  192. $managementurl = new moodle_url('/course/management.php');
  193. // These are the caps required in order to see the management interface.
  194. $managementcaps = array('moodle/category:manage', 'moodle/course:create');
  195. if ($categoryid && !has_any_capability($managementcaps, context_system::instance())) {
  196. // If the user doesn't have either manage caps then they can only manage within the given category.
  197. $managementurl->param('categoryid', $categoryid);
  198. }
  199. // Because the course category management interfaces are buried in the admin tree and that is loaded by ajax
  200. // we need to manually tell the navigation we need it loaded. The second arg does this.
  201. navigation_node::override_active_url($managementurl, true);
  202. $pagedesc = $straddnewcourse;
  203. $title = "$site->shortname: $straddnewcourse";
  204. $fullname = $site->fullname;
  205. $PAGE->navbar->add($pagedesc);
  206. }
  207. $PAGE->set_title($title);
  208. $PAGE->set_heading($fullname);
  209. echo $OUTPUT->header();
  210. echo $OUTPUT->heading($pagedesc);
  211. $editform->display();
  212. echo $OUTPUT->footer();