2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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();
46 $courseid = required_param('courseid', PARAM_INT
);
47 $outcome->outcome
= \core_course\management\helper
::action_course_change_sortorder_up_one_by_record($courseid);
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);
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);
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;
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);
85 $courseid = required_param('courseid', PARAM_INT
);
86 $outcome->outcome
= \core_course\management\helper
::action_course_hide_by_record($courseid);
89 $courseid = required_param('courseid', PARAM_INT
);
90 $outcome->outcome
= \core_course\management\helper
::action_course_show_by_record($courseid);
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);
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);
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)
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)
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;
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;
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;
153 echo json_encode($outcome);
154 echo $OUTPUT->footer();
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.