PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/course/mod.php

https://bitbucket.org/moodle/moodle
PHP | 279 lines | 195 code | 59 blank | 25 comment | 38 complexity | 1ea74e73b8f2d72290ff8ded048fb120 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * Moves, adds, updates, duplicates or deletes modules in a course
  18. *
  19. * @copyright 1999 Martin Dougiamas http://dougiamas.com
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package course
  22. */
  23. require("../config.php");
  24. require_once("lib.php");
  25. $sectionreturn = optional_param('sr', null, PARAM_INT);
  26. $add = optional_param('add', '', PARAM_ALPHANUM);
  27. $type = optional_param('type', '', PARAM_ALPHA);
  28. $indent = optional_param('indent', 0, PARAM_INT);
  29. $update = optional_param('update', 0, PARAM_INT);
  30. $duplicate = optional_param('duplicate', 0, PARAM_INT);
  31. $hide = optional_param('hide', 0, PARAM_INT);
  32. $stealth = optional_param('stealth', 0, PARAM_INT);
  33. $show = optional_param('show', 0, PARAM_INT);
  34. $copy = optional_param('copy', 0, PARAM_INT);
  35. $moveto = optional_param('moveto', 0, PARAM_INT);
  36. $movetosection = optional_param('movetosection', 0, PARAM_INT);
  37. $delete = optional_param('delete', 0, PARAM_INT);
  38. $course = optional_param('course', 0, PARAM_INT);
  39. $groupmode = optional_param('groupmode', -1, PARAM_INT);
  40. $cancelcopy = optional_param('cancelcopy', 0, PARAM_BOOL);
  41. $confirm = optional_param('confirm', 0, PARAM_BOOL);
  42. // This page should always redirect
  43. $url = new moodle_url('/course/mod.php');
  44. foreach (compact('indent','update','hide','show','copy','moveto','movetosection','delete','course','cancelcopy','confirm') as $key=>$value) {
  45. if ($value !== 0) {
  46. $url->param($key, $value);
  47. }
  48. }
  49. $url->param('sr', $sectionreturn);
  50. if ($add !== '') {
  51. $url->param('add', $add);
  52. }
  53. if ($type !== '') {
  54. $url->param('type', $type);
  55. }
  56. if ($groupmode !== '') {
  57. $url->param('groupmode', $groupmode);
  58. }
  59. $PAGE->set_url($url);
  60. require_login();
  61. //check if we are adding / editing a module that has new forms using formslib
  62. if (!empty($add)) {
  63. $id = required_param('id', PARAM_INT);
  64. $section = required_param('section', PARAM_INT);
  65. $type = optional_param('type', '', PARAM_ALPHA);
  66. $returntomod = optional_param('return', 0, PARAM_BOOL);
  67. redirect("$CFG->wwwroot/course/modedit.php?add=$add&type=$type&course=$id&section=$section&return=$returntomod&sr=$sectionreturn");
  68. } else if (!empty($update)) {
  69. $cm = get_coursemodule_from_id('', $update, 0, true, MUST_EXIST);
  70. $returntomod = optional_param('return', 0, PARAM_BOOL);
  71. redirect("$CFG->wwwroot/course/modedit.php?update=$update&return=$returntomod&sr=$sectionreturn");
  72. } else if (!empty($duplicate) and confirm_sesskey()) {
  73. $cm = get_coursemodule_from_id('', $duplicate, 0, true, MUST_EXIST);
  74. $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  75. require_login($course, false, $cm);
  76. $modcontext = context_module::instance($cm->id);
  77. require_capability('moodle/course:manageactivities', $modcontext);
  78. // Duplicate the module.
  79. $newcm = duplicate_module($course, $cm);
  80. redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
  81. } else if (!empty($delete)) {
  82. $cm = get_coursemodule_from_id('', $delete, 0, true, MUST_EXIST);
  83. $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  84. require_login($course, false, $cm);
  85. $modcontext = context_module::instance($cm->id);
  86. require_capability('moodle/course:manageactivities', $modcontext);
  87. $return = course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn));
  88. if (!$confirm or !confirm_sesskey()) {
  89. $fullmodulename = get_string('modulename', $cm->modname);
  90. $optionsyes = array('confirm'=>1, 'delete'=>$cm->id, 'sesskey'=>sesskey(), 'sr' => $sectionreturn);
  91. $strdeletecheck = get_string('deletecheck', '', $fullmodulename);
  92. $strparams = (object)array('type' => $fullmodulename, 'name' => $cm->name);
  93. $strdeletechecktypename = get_string('deletechecktypename', '', $strparams);
  94. $PAGE->set_pagetype('mod-' . $cm->modname . '-delete');
  95. $PAGE->set_title($strdeletecheck);
  96. $PAGE->set_heading($course->fullname);
  97. $PAGE->navbar->add($strdeletecheck);
  98. echo $OUTPUT->header();
  99. echo $OUTPUT->box_start('noticebox');
  100. $formcontinue = new single_button(new moodle_url("$CFG->wwwroot/course/mod.php", $optionsyes), get_string('yes'));
  101. $formcancel = new single_button($return, get_string('no'), 'get');
  102. echo $OUTPUT->confirm($strdeletechecktypename, $formcontinue, $formcancel);
  103. echo $OUTPUT->box_end();
  104. echo $OUTPUT->footer();
  105. exit;
  106. }
  107. // Delete the module.
  108. course_delete_module($cm->id);
  109. redirect($return);
  110. }
  111. if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) {
  112. $cm = get_coursemodule_from_id('', $USER->activitycopy, 0, true, MUST_EXIST);
  113. $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  114. require_login($course, false, $cm);
  115. $coursecontext = context_course::instance($course->id);
  116. $modcontext = context_module::instance($cm->id);
  117. require_capability('moodle/course:manageactivities', $modcontext);
  118. if (!empty($movetosection)) {
  119. if (!$section = $DB->get_record('course_sections', array('id'=>$movetosection, 'course'=>$cm->course))) {
  120. print_error('sectionnotexist');
  121. }
  122. $beforecm = NULL;
  123. } else { // normal moveto
  124. if (!$beforecm = get_coursemodule_from_id('', $moveto, $cm->course, true)) {
  125. print_error('invalidcoursemodule');
  126. }
  127. if (!$section = $DB->get_record('course_sections', array('id'=>$beforecm->section, 'course'=>$cm->course))) {
  128. print_error('sectionnotexist');
  129. }
  130. }
  131. if (!ismoving($section->course)) {
  132. print_error('needcopy', '', "view.php?id=$section->course");
  133. }
  134. moveto_module($cm, $section, $beforecm);
  135. $sectionreturn = $USER->activitycopysectionreturn;
  136. unset($USER->activitycopy);
  137. unset($USER->activitycopycourse);
  138. unset($USER->activitycopyname);
  139. unset($USER->activitycopysectionreturn);
  140. redirect(course_get_url($course, $section->section, array('sr' => $sectionreturn)));
  141. } else if (!empty($indent) and confirm_sesskey()) {
  142. $id = required_param('id', PARAM_INT);
  143. $cm = get_coursemodule_from_id('', $id, 0, true, MUST_EXIST);
  144. $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  145. require_login($course, false, $cm);
  146. $coursecontext = context_course::instance($course->id);
  147. $modcontext = context_module::instance($cm->id);
  148. require_capability('moodle/course:manageactivities', $modcontext);
  149. $cm->indent += $indent;
  150. if ($cm->indent < 0) {
  151. $cm->indent = 0;
  152. }
  153. $DB->set_field('course_modules', 'indent', $cm->indent, array('id'=>$cm->id));
  154. rebuild_course_cache($cm->course);
  155. redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
  156. } else if (!empty($hide) and confirm_sesskey()) {
  157. $cm = get_coursemodule_from_id('', $hide, 0, true, MUST_EXIST);
  158. $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  159. require_login($course, false, $cm);
  160. $coursecontext = context_course::instance($course->id);
  161. $modcontext = context_module::instance($cm->id);
  162. require_capability('moodle/course:activityvisibility', $modcontext);
  163. if (set_coursemodule_visible($cm->id, 0)) {
  164. \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
  165. }
  166. redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
  167. } else if (!empty($stealth) and confirm_sesskey()) {
  168. list($course, $cm) = get_course_and_cm_from_cmid($stealth);
  169. require_login($course, false, $cm);
  170. require_capability('moodle/course:activityvisibility', $cm->context);
  171. if (set_coursemodule_visible($cm->id, 1, 0)) {
  172. \core\event\course_module_updated::create_from_cm($cm)->trigger();
  173. }
  174. redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
  175. } else if (!empty($show) and confirm_sesskey()) {
  176. list($course, $cm) = get_course_and_cm_from_cmid($show);
  177. require_login($course, false, $cm);
  178. require_capability('moodle/course:activityvisibility', $cm->context);
  179. $section = $cm->get_section_info();
  180. if (set_coursemodule_visible($cm->id, 1)) {
  181. \core\event\course_module_updated::create_from_cm($cm)->trigger();
  182. }
  183. redirect(course_get_url($course, $section->section, array('sr' => $sectionreturn)));
  184. } else if ($groupmode > -1 and confirm_sesskey()) {
  185. $id = required_param('id', PARAM_INT);
  186. $cm = get_coursemodule_from_id('', $id, 0, true, MUST_EXIST);
  187. $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  188. require_login($course, false, $cm);
  189. $coursecontext = context_course::instance($course->id);
  190. $modcontext = context_module::instance($cm->id);
  191. require_capability('moodle/course:manageactivities', $modcontext);
  192. set_coursemodule_groupmode($cm->id, $groupmode);
  193. \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger();
  194. redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
  195. } else if (!empty($copy) and confirm_sesskey()) { // value = course module
  196. $cm = get_coursemodule_from_id('', $copy, 0, true, MUST_EXIST);
  197. $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  198. require_login($course, false, $cm);
  199. $coursecontext = context_course::instance($course->id);
  200. $modcontext = context_module::instance($cm->id);
  201. require_capability('moodle/course:manageactivities', $modcontext);
  202. $section = $DB->get_record('course_sections', array('id'=>$cm->section), '*', MUST_EXIST);
  203. $USER->activitycopy = $copy;
  204. $USER->activitycopycourse = $cm->course;
  205. $USER->activitycopyname = $cm->name;
  206. $USER->activitycopysectionreturn = $sectionreturn;
  207. redirect(course_get_url($course, $section->section, array('sr' => $sectionreturn)));
  208. } else if (!empty($cancelcopy) and confirm_sesskey()) { // value = course module
  209. $courseid = $USER->activitycopycourse;
  210. $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
  211. $cm = get_coursemodule_from_id('', $USER->activitycopy, 0, true, IGNORE_MISSING);
  212. $sectionreturn = $USER->activitycopysectionreturn;
  213. unset($USER->activitycopy);
  214. unset($USER->activitycopycourse);
  215. unset($USER->activitycopyname);
  216. unset($USER->activitycopysectionreturn);
  217. redirect(course_get_url($course, $cm->sectionnum, array('sr' => $sectionreturn)));
  218. } else {
  219. print_error('unknowaction');
  220. }