NOBUG xmldb editor - make php code generated to follow the 2-slash coding style
[moodle.git] / enrol / mnet / allowed_courses.php
CommitLineData
bf1a3d20 1<?php
2
3/*
4 * Created on 12/12/2006
5 *
6 * MNET enrol allowed courses and categories form
7 */
8
9require_once dirname(dirname(dirname(__FILE__))) . '/config.php';
10require_once $CFG->libdir . '/adminlib.php';
11include_once $CFG->dirroot . '/mnet/lib.php';
12
1ae083e4 13
14admin_externalpage_setup('ssoaccesscontrol');
61ef8f9f 15echo $OUTPUT->header();
bf1a3d20 16
17$addcategory = optional_param('addcategory', 0, PARAM_BOOL);
18$removecategory = optional_param('removecategory', 0, PARAM_BOOL);
19$addcourse = optional_param('addcourse', 0, PARAM_BOOL);
20$removecourse = optional_param('removecourse', 0, PARAM_BOOL);
21
1ae083e4 22$sitecontext = get_context_instance(CONTEXT_SYSTEM);
bf1a3d20 23$sesskey = sesskey();
24$formerror = array();
25
26require_capability('moodle/user:delete', $sitecontext);
27
28// process returned form data
29if ($form = data_submitted() and confirm_sesskey()) {
30
31 // add and remove categories as needed
32 if (!empty($CFG->enrol_mnet_allowed_categories)) {
33 $allowedcategories = explode(',', $CFG->enrol_mnet_allowed_categories);
34 }
35 if ($addcategory and !empty($form->addcategories)) {
36 foreach ($form->addcategories as $category) {
37 if (!$category = clean_param($category, PARAM_INT)) {
38 continue;
39 }
40 $allowedcategories[] = $category;
41 }
42 }
43 if ($removecategory and !empty($form->removecategories)) {
44 foreach ($form->removecategories as $category) {
45 if ($category = clean_param($category, PARAM_INT)) {
46 $removedcategories[] = $category;
47 }
48 }
49 $allowedcategories = array_diff($allowedcategories, $removedcategories);
50 }
51
52 // add and remove courses as needed
53 if (!empty($CFG->enrol_mnet_allowed_courses)) {
54 $allowedcourses = explode(',', $CFG->enrol_mnet_allowed_courses);
55 }
56 if ($addcourse and !empty($form->addcourses)) {
57 foreach ($form->addcourses as $course) {
58 if ($course = clean_param($course, PARAM_INT)) {
59 $allowedcourses[] = $course;
60 }
61 }
62 }
63 if ($removecourse and !empty($form->removecourses)) {
64 foreach ($form->removecourses as $course) {
65 if (!$course = clean_param($course, PARAM_INT)) {
66 continue;
67 }
68 $removedcourses[] = $course;
69 }
70 $allowedcourses = array_diff($allowedcourses, $removedcourses);
71 }
72
73 // save config
74 $cfg = empty($allowedcategories) ? '' : implode(',', $allowedcategories);
75 set_config('enrol_mnet_allowed_categories', $cfg);
76 $cfg = empty($allowedcourses) ? '' : implode(',', $allowedcourses);
77 set_config('enrol_mnet_allowed_courses', $cfg);
78
79 // redirect('allowed_courses.php', get_string('changessaved'));
80}
81
82
83
84// setup arrays for allowed categories and courses
85$categories = array();
50c5bef4 86if ($categories = $DB->get_records('course_categories', null, 'name', 'id, name')) {
0d098fbb 87 $allowedcategories = array();
bf1a3d20 88 if (empty($CFG->enrol_mnet_allowed_categories)) {
bf1a3d20 89 $potentialcategories = $categories;
90 } else {
0d098fbb 91 $potentialcategories = array();
92 $explode_categories = explode(',', $CFG->enrol_mnet_allowed_categories);
93 foreach($categories as $category) {
9dfe778f 94 if (in_array($category->id, $explode_categories)) {
0d098fbb 95 $allowedcategories[] = $category;
96 } else {
97 $potentialcategories[] = $category;
98 }
99 }
bf1a3d20 100 }
101}
102$courses = array();
50c5bef4 103if ($courses = $DB->get_records('course', null, 'shortname', 'id, shortname')) {
9b0633ac 104 unset($courses[SITEID]); // never list or offer the siteid
0d098fbb 105 $allowedcourses = array();
bf1a3d20 106 if (empty($CFG->enrol_mnet_allowed_courses)) {
bf1a3d20 107 $potentialcourses = $courses;
108 } else {
0d098fbb 109 $potentialcourses = array();
110 $explode_courses = explode(',', $CFG->enrol_mnet_allowed_courses);
111 foreach($courses as $course) {
9dfe778f 112 if (in_array($course->id, $explode_courses)) {
0d098fbb 113 $allowedcourses[] = $course;
114 } else {
115 $potentialcourses[] = $course;
116 }
117 }
bf1a3d20 118 }
119}
120
121
122
123// output the form
c60e66ae 124echo $OUTPUT->box_start();
bf1a3d20 125
126?>
4317f92f 127 <div class="allowedcoursesdiv">
b7dc2256 128 <form id="allowedcoursesform" method="post">
9feaec8f 129 <input type="hidden" name="sesskey" value="<?php echo $sesskey; ?>" />
bf1a3d20 130<?php
131
132// display course category selector
133if (count($categories) < 1) {
134 echo get_string('nocategoriesdefined', 'enrol_mnet', "$CFG->wwwroot/course/index.php?categoryedit=on");
135} else {
136
137?>
138 <table align="center" border="0" cellpadding="5" cellspacing="0">
139 <tr>
140 <td valign="top">
141 <?php print_string('allowedcategories', 'enrol_mnet', count($allowedcategories)); ?>
142 </td>
143 <td></td>
144 <td valign="top">
145 <?php print_string('allcategories', 'enrol_mnet', count($potentialcategories)); ?>
146 </td>
147 </tr>
148
149 <tr>
150 <td valign="top">
9feaec8f 151 <select name="removecategories[]" size="20" id="removecategories" multiple="multiple"
152 onfocus="getElementById('allowedcoursesform').addcategory.disabled=true;
b7dc2256 153 getElementById('allowedcoursesform').removecategory.disabled=false;
9feaec8f 154 getElementById('allowedcoursesform').addcategories.selectedIndex=-1;" >
bf1a3d20 155 <?php
156 foreach ($allowedcategories as $category) {
6ba65fa0 157 echo "<option value=\"$category->id\"> " . format_string($category->name) . " </option>\n";
bf1a3d20 158 }
159 ?>
160 </select>
161 </td>
162
163 <td valign="top">
164 <br />
165 <input name="addcategory" type="submit" id="add" value="&larr;" />
166 <br />
167 <input name="removecategory" type="submit" id="remove" value="&rarr;" />
168 <br />
169 </td>
170
171 <td valign="top">
9feaec8f 172 <select name="addcategories[]" size="20" id="addcategories" multiple="multiple"
b7dc2256 173 onFocus="getElementById('allowedcoursesform').addcategory.disabled=false;
174 getElementById('allowedcoursesform').removecategory.disabled=true;
175 getElementById('allowedcoursesform').removecategories.selectedIndex=-1;">
bf1a3d20 176 <?php
177 foreach ($potentialcategories as $category) {
6ba65fa0 178 echo "<option value=\"$category->id\"> " . format_string($category->name) . " </option>\n";
bf1a3d20 179 }
180 ?>
181 </select>
182 </td>
183 </tr>
184 </table>
185<?php
186
187}
188
189// display course selector
190if (count($courses) < 1) {
191 echo get_string('nocoursesdefined', 'enrol_mnet', "TODO: $course_admin_url"); // TODO
192} else {
193
194?>
195 <table align="center" border="0" cellpadding="5" cellspacing="0">
196 <tr>
197 <td valign="top">
198 <?php print_string('allowedcourses', 'enrol_mnet', count($allowedcourses)); ?>
199 </td>
200 <td></td>
201 <td valign="top">
202 <?php print_string('allcourses', 'enrol_mnet', count($potentialcourses)); ?>
203 </td>
204 </tr>
205
206 <tr>
207 <td valign="top">
9feaec8f 208 <select name="removecourses[]" size="20" id="removecourses" multiple="multiple"
b7dc2256 209 onFocus="getElementById('allowedcoursesform').addcourse.disabled=true;
210 getElementById('allowedcoursesform').removecourse.disabled=false;
9feaec8f 211 getElementById('allowedcoursesform').addcourses.selectedIndex=-1;">
bf1a3d20 212 <?php
213 foreach ($allowedcourses as $course) {
6ba65fa0 214 echo "<option value=\"$course->id\"> " . format_string($course->shortname) . " </option>\n";
bf1a3d20 215 }
216 ?>
217 </select>
218 </td>
219
220 <td valign="top">
221 <br />
222 <input name="addcourse" type="submit" id="add" value="&larr;" />
223 <br />
224 <input name="removecourse" type="submit" id="remove" value="&rarr;" />
225 <br />
226 </td>
227
228 <td valign="top">
9feaec8f 229 <select name="addcourses[]" size="20" id="addcourses" multiple="multiple"
b7dc2256 230 onFocus="getElementById('allowedcoursesform').addcourse.disabled=false;
231 getElementById('allowedcoursesform').removecourse.disabled=true;
232 getElementById('allowedcoursesform').removecourses.selectedIndex=-1;">
bf1a3d20 233 <?php
234 foreach ($potentialcourses as $course) {
6ba65fa0 235 echo "<option value=\"$course->id\"> " . format_string($course->shortname) . " </option>\n";
bf1a3d20 236 }
237 ?>
238 </select>
239 </td>
240 </tr>
241 </table>
242<?php
243
244}
245
246?>
247 </form>
248 </div>
249<?php
250
c60e66ae 251echo $OUTPUT->box_end();
745da501 252echo $OUTPUT->footer();
bf1a3d20 253
254?>