forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
management.php
159 lines (151 loc) · 7.31 KB
/
management.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Performs course category management ajax actions.
*
* Please note functions may throw exceptions, please ensure your JS handles them as well as the outcome objects.
*
* @package core_course
* @copyright 2013 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('AJAX_SCRIPT', true);
require_once('../../config.php');
require_once($CFG->dirroot.'/lib/coursecatlib.php');
require_once($CFG->dirroot.'/course/lib.php');
$action = required_param('action', PARAM_ALPHA);
require_sesskey(); // Gotta have the sesskey.
require_login(); // Gotta be logged in (of course).
$PAGE->set_context(context_system::instance());
// Prepare an outcome object. We always use this.
$outcome = new stdClass;
$outcome->error = false;
$outcome->outcome = false;
echo $OUTPUT->header();
switch ($action) {
case 'movecourseup' :
$courseid = required_param('courseid', PARAM_INT);
$outcome->outcome = \core_course\management\helper::action_course_change_sortorder_up_one_by_record($courseid);
break;
case 'movecoursedown' :
$courseid = required_param('courseid', PARAM_INT);
$outcome->outcome = \core_course\management\helper::action_course_change_sortorder_down_one_by_record($courseid);
break;
case 'movecourseintocategory':
$courseid = required_param('courseid', PARAM_INT);
$categoryid = required_param('categoryid', PARAM_INT);
$course = get_course($courseid);
$oldcategory = coursecat::get($course->category);
$category = coursecat::get($categoryid);
$outcome->outcome = \core_course\management\helper::move_courses_into_category($category, $courseid);
$perpage = (int)get_user_preferences('coursecat_management_perpage', $CFG->coursesperpage);
$totalcourses = $oldcategory->get_courses_count();
$totalpages = ceil($totalcourses / $perpage);
if ($totalpages == 0) {
$str = get_string('nocoursesyet');
} else if ($totalpages == 1) {
$str = get_string('showingacourses', 'moodle', $totalcourses);
} else {
$a = new stdClass;
$a->start = ($page * $perpage) + 1;
$a->end = min((($page + 1) * $perpage), $totalcourses);
$a->total = $totalcourses;
$str = get_string('showingxofycourses', 'moodle', $a);
}
$outcome->totalcatcourses = $category->get_courses_count();
$outcome->fromcatcoursecount = $totalcourses;
$outcome->paginationtotals = $str;
break;
case 'movecourseafter' :
$courseid = required_param('courseid', PARAM_INT);
$moveaftercourseid = required_param('moveafter', PARAM_INT);
$outcome->outcome = \core_course\management\helper::action_course_change_sortorder_after_course(
$courseid, $moveaftercourseid);
break;
case 'hidecourse' :
$courseid = required_param('courseid', PARAM_INT);
$outcome->outcome = \core_course\management\helper::action_course_hide_by_record($courseid);
break;
case 'showcourse' :
$courseid = required_param('courseid', PARAM_INT);
$outcome->outcome = \core_course\management\helper::action_course_show_by_record($courseid);
break;
case 'movecategoryup' :
$categoryid = required_param('categoryid', PARAM_INT);
$outcome->outcome = \core_course\management\helper::action_category_change_sortorder_up_one_by_id($categoryid);
break;
case 'movecategorydown' :
$categoryid = required_param('categoryid', PARAM_INT);
$outcome->outcome = \core_course\management\helper::action_category_change_sortorder_down_one_by_id($categoryid);
break;
case 'hidecategory' :
$categoryid = required_param('categoryid', PARAM_INT);
$selectedcategoryid = optional_param('selectedcategory', null, PARAM_INT);
$outcome->outcome = \core_course\management\helper::action_category_hide_by_id($categoryid);
$outcome->categoryvisibility = \core_course\management\helper::get_category_children_visibility($categoryid);
$outcome->coursevisibility = \core_course\management\helper::get_category_courses_visibility($categoryid);
if ($selectedcategoryid !== null) {
$outcome->coursevisibility = array_merge(
$outcome->coursevisibility,
\core_course\management\helper::get_category_courses_visibility($selectedcategoryid)
);
}
break;
case 'showcategory' :
$categoryid = required_param('categoryid', PARAM_INT);
$selectedcategoryid = optional_param('selectedcategory', null, PARAM_INT);
$outcome->outcome = \core_course\management\helper::action_category_show_by_id($categoryid);
$outcome->categoryvisibility = \core_course\management\helper::get_category_children_visibility($categoryid);
$outcome->coursevisibility = \core_course\management\helper::get_category_courses_visibility($categoryid);
if ($selectedcategoryid !== null) {
$outcome->coursevisibility = array_merge(
$outcome->coursevisibility,
\core_course\management\helper::get_category_courses_visibility($selectedcategoryid)
);
}
break;
case 'expandcategory':
$categoryid = required_param('categoryid', PARAM_INT);
$coursecat = coursecat::get($categoryid);
\core_course\management\helper::record_expanded_category($coursecat);
$outcome->outcome = true;
break;
case 'collapsecategory':
$categoryid = required_param('categoryid', PARAM_INT);
$coursecat = coursecat::get($categoryid);
\core_course\management\helper::record_expanded_category($coursecat, false);
$outcome->outcome = true;
break;
case 'getsubcategorieshtml' :
$categoryid = required_param('categoryid', PARAM_INT);
/* @var core_course_management_renderer $renderer */
$renderer = $PAGE->get_renderer('core_course', 'management');
$outcome->html = html_writer::start_tag('ul',
array('class' => 'ml', 'role' => 'group', 'id' => 'subcategoriesof'.$categoryid));
$coursecat = coursecat::get($categoryid);
foreach ($coursecat->get_children() as $subcat) {
$outcome->html .= $renderer->category_listitem($subcat, array(), $subcat->get_children_count());
}
$outcome->html .= html_writer::end_tag('ul');
$outcome->outcome = true;
break;
}
echo json_encode($outcome);
echo $OUTPUT->footer();
// Thats all folks.
// Don't ever even consider putting anything after this. It just wouldn't make sense.
// But you already knew that, you smart developer you.
exit;