weekly release 3.8.5+
[moodle.git] / course / ajax / management.php
blob7df273ba2a4b5c8274728137074ea27a8e429236
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.
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/>.
17 /**
18 * Performs course category management ajax actions.
20 * Please note functions may throw exceptions, please ensure your JS handles them as well as the outcome objects.
22 * @package core_course
23 * @copyright 2013 Sam Hemelryk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 define('AJAX_SCRIPT', true);
29 require_once('../../config.php');
30 require_once($CFG->dirroot.'/course/lib.php');
32 $action = required_param('action', PARAM_ALPHA);
33 require_sesskey(); // Gotta have the sesskey.
34 require_login(); // Gotta be logged in (of course).
35 $PAGE->set_context(context_system::instance());
37 // Prepare an outcome object. We always use this.
38 $outcome = new stdClass;
39 $outcome->error = false;
40 $outcome->outcome = false;
42 echo $OUTPUT->header();
44 switch ($action) {
45 case 'movecourseup' :
46 $courseid = required_param('courseid', PARAM_INT);
47 $outcome->outcome = \core_course\management\helper::action_course_change_sortorder_up_one_by_record($courseid);
48 break;
49 case 'movecoursedown' :
50 $courseid = required_param('courseid', PARAM_INT);
51 $outcome->outcome = \core_course\management\helper::action_course_change_sortorder_down_one_by_record($courseid);
52 break;
53 case 'movecourseintocategory':
54 $courseid = required_param('courseid', PARAM_INT);
55 $categoryid = required_param('categoryid', PARAM_INT);
56 $course = get_course($courseid);
57 $oldcategory = core_course_category::get($course->category);
58 $category = core_course_category::get($categoryid);
59 $outcome->outcome = \core_course\management\helper::move_courses_into_category($category, $courseid);
60 $perpage = (int)get_user_preferences('coursecat_management_perpage', $CFG->coursesperpage);
61 $totalcourses = $oldcategory->get_courses_count();
62 $totalpages = ceil($totalcourses / $perpage);
63 if ($totalpages == 0) {
64 $str = get_string('nocoursesyet');
65 } else if ($totalpages == 1) {
66 $str = get_string('showingacourses', 'moodle', $totalcourses);
67 } else {
68 $a = new stdClass;
69 $a->start = ($page * $perpage) + 1;
70 $a->end = min((($page + 1) * $perpage), $totalcourses);
71 $a->total = $totalcourses;
72 $str = get_string('showingxofycourses', 'moodle', $a);
74 $outcome->totalcatcourses = $category->get_courses_count();
75 $outcome->fromcatcoursecount = $totalcourses;
76 $outcome->paginationtotals = $str;
77 break;
78 case 'movecourseafter' :
79 $courseid = required_param('courseid', PARAM_INT);
80 $moveaftercourseid = required_param('moveafter', PARAM_INT);
81 $outcome->outcome = \core_course\management\helper::action_course_change_sortorder_after_course(
82 $courseid, $moveaftercourseid);
83 break;
84 case 'hidecourse' :
85 $courseid = required_param('courseid', PARAM_INT);
86 $outcome->outcome = \core_course\management\helper::action_course_hide_by_record($courseid);
87 break;
88 case 'showcourse' :
89 $courseid = required_param('courseid', PARAM_INT);
90 $outcome->outcome = \core_course\management\helper::action_course_show_by_record($courseid);
91 break;
92 case 'movecategoryup' :
93 $categoryid = required_param('categoryid', PARAM_INT);
94 $outcome->outcome = \core_course\management\helper::action_category_change_sortorder_up_one_by_id($categoryid);
95 break;
96 case 'movecategorydown' :
97 $categoryid = required_param('categoryid', PARAM_INT);
98 $outcome->outcome = \core_course\management\helper::action_category_change_sortorder_down_one_by_id($categoryid);
99 break;
100 case 'hidecategory' :
101 $categoryid = required_param('categoryid', PARAM_INT);
102 $selectedcategoryid = optional_param('selectedcategory', null, PARAM_INT);
103 $outcome->outcome = \core_course\management\helper::action_category_hide_by_id($categoryid);
104 $outcome->categoryvisibility = \core_course\management\helper::get_category_children_visibility($categoryid);
105 $outcome->coursevisibility = \core_course\management\helper::get_category_courses_visibility($categoryid);
106 if ($selectedcategoryid !== null) {
107 $outcome->coursevisibility = array_merge(
108 $outcome->coursevisibility,
109 \core_course\management\helper::get_category_courses_visibility($selectedcategoryid)
112 break;
113 case 'showcategory' :
114 $categoryid = required_param('categoryid', PARAM_INT);
115 $selectedcategoryid = optional_param('selectedcategory', null, PARAM_INT);
116 $outcome->outcome = \core_course\management\helper::action_category_show_by_id($categoryid);
117 $outcome->categoryvisibility = \core_course\management\helper::get_category_children_visibility($categoryid);
118 $outcome->coursevisibility = \core_course\management\helper::get_category_courses_visibility($categoryid);
119 if ($selectedcategoryid !== null) {
120 $outcome->coursevisibility = array_merge(
121 $outcome->coursevisibility,
122 \core_course\management\helper::get_category_courses_visibility($selectedcategoryid)
125 break;
126 case 'expandcategory':
127 $categoryid = required_param('categoryid', PARAM_INT);
128 $coursecat = core_course_category::get($categoryid);
129 \core_course\management\helper::record_expanded_category($coursecat);
130 $outcome->outcome = true;
131 break;
132 case 'collapsecategory':
133 $categoryid = required_param('categoryid', PARAM_INT);
134 $coursecat = core_course_category::get($categoryid);
135 \core_course\management\helper::record_expanded_category($coursecat, false);
136 $outcome->outcome = true;
137 break;
138 case 'getsubcategorieshtml' :
139 $categoryid = required_param('categoryid', PARAM_INT);
140 /* @var core_course_management_renderer $renderer */
141 $renderer = $PAGE->get_renderer('core_course', 'management');
142 $outcome->html = html_writer::start_tag('ul',
143 array('class' => 'ml', 'role' => 'group', 'id' => 'subcategoriesof'.$categoryid));
144 $coursecat = core_course_category::get($categoryid);
145 foreach ($coursecat->get_children() as $subcat) {
146 $outcome->html .= $renderer->category_listitem($subcat, array(), $subcat->get_children_count());
148 $outcome->html .= html_writer::end_tag('ul');
149 $outcome->outcome = true;
150 break;
153 echo json_encode($outcome);
154 echo $OUTPUT->footer();
155 // Thats all folks.
156 // Don't ever even consider putting anything after this. It just wouldn't make sense.
157 // But you already knew that, you smart developer you.
158 exit;