2 // Displays the top level category or all courses
3 // In editing mode, allows the admin to edit a category,
4 // and rearrange courses
6 require_once("../config.php");
7 require_once("lib.php");
9 require_variable($id); // Category id
10 optional_variable($page, "0"); // which page to show
11 optional_variable($perpage, "20"); // how many per page
13 if (!$site = get_site()) {
14 error("Site isn't defined!");
17 if ($CFG->forcelogin
) {
21 if (!$category = get_record("course_categories", "id", $id)) {
22 error("Category not known!");
26 if (isset($_GET['edit']) and confirm_sesskey()) {
28 $USER->categoryediting
= true;
29 } else if ($edit == "off") {
30 $USER->categoryediting
= false;
33 $navbaritem = update_category_button($category->id
);
35 $creatorediting = !empty($USER->categoryediting
);
36 $adminediting = (isadmin() and $creatorediting);
39 if (!$category->visible
) {
40 error(get_string('notavailable', 'error'));
42 $navbaritem = print_course_search("", true, "navbar");
43 $adminediting = false;
44 $creatorediting = false;
49 /// Rename the category if requested
50 if (!empty($_POST['rename']) and confirm_sesskey()) {
51 $category->name
= $_POST['rename'];
52 if (! set_field("course_categories", "name", $category->name
, "id", $category->id
)) {
53 notify("An error occurred while renaming the category");
57 /// Resort the category if requested
59 if (!empty($_GET['resort']) and confirm_sesskey()) {
60 if ($courses = get_courses($category->id
, "fullname ASC", 'c.id,c.fullname,c.sortorder')) {
61 // move it off the range
62 $count = get_record_sql('SELECT MAX(sortorder) AS max, 1
63 FROM ' . $CFG->prefix
. 'course WHERE category=' . $category->id
);
64 $count = $count->max +
100;
66 foreach ($courses as $course) {
67 set_field('course', 'sortorder', $count, 'id', $course->id
);
71 fix_course_sortorder($category->id
);
79 $numcategories = count_records("course_categories");
81 $stradministration = get_string("administration");
82 $strcategories = get_string("categories");
83 $strcategory = get_string("category");
84 $strcourses = get_string("courses");
86 if ($creatorediting) {
88 print_header("$site->shortname: $category->name", "$site->fullname: $strcourses",
89 "<a href=\"../$CFG->admin/index.php\">$stradministration</a> -> ".
90 "<a href=\"index.php\">$strcategories</a> -> $category->name",
91 "", "", true, $navbaritem);
93 print_header("$site->shortname: $category->name", "$site->fullname: $strcourses",
94 "<a href=\"index.php\">$strcategories</a> -> $category->name", "", "", true, $navbaritem);
97 print_header("$site->shortname: $category->name", "$site->fullname: $strcourses",
98 "<a href=\"index.php\">$strcategories</a> -> $category->name", "", "", true, $navbaritem);
101 /// Print the category selector
103 $displaylist = array();
104 $parentlist = array();
106 make_categories_list($displaylist, $parentlist, "");
108 echo '<table align="center"><tr><td align="right">';
109 echo "<p>$strcategories:</p>";
111 popup_form("category.php?id=", $displaylist, "switchcategory", "$category->id", "", "", "", false);
112 echo "</td></tr></table><br />";
115 /// Editing functions
119 /// Move a specified course to a new category
121 if (isset($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
123 if (! $destcategory = get_record("course_categories", "id", $data->moveto
)) {
124 error("Error finding the category");
127 unset($data->moveto
);
129 unset($data->sesskey
);
132 foreach ($data as $code => $junk) {
134 $courseid = substr($code, 1);
136 if (! record_exists('course', 'id', $courseid)) {
137 notify('Error finding a course');
139 // figure out a sortorder that we can use in the destination category
140 $sortorder = get_field_sql('SELECT MIN(sortorder)-1 AS min
141 FROM ' . $CFG->prefix
. 'course WHERE category=' . $destcategory->id
) ||
1000;
143 $newcourse = new stdClass
;
144 $newcourse->id
= $courseid;
145 $newcourse->category
= $destcategory->id
;
146 $newcourse->sortorder
= $sortorder;
148 if (!update_record('course', $newcourse)) {
149 notify("An error occurred - course not moved!");
151 fix_course_sortorder();
154 $category = get_record('course_categories', 'id', $category->id
); // Refresh it
158 /// Hide or show a course
160 if ((isset($hide) or isset($show)) and confirm_sesskey()) {
162 $course = get_record("course", "id", $hide);
165 $course = get_record("course", "id", $show);
169 if (! set_field("course", "visible", $visible, "id", $course->id
)) {
170 notify("Could not update that course!");
176 /// Move a course up or down
178 if ((isset($moveup) or isset($movedown)) and confirm_sesskey()) {
183 // ensure the course order has no gaps
185 fix_course_sortorder($category->id
);
187 // we are going to need to know the range
188 $max = get_record_sql('SELECT MAX(sortorder) AS max, 1
189 FROM ' . $CFG->prefix
. 'course WHERE category=' . $category->id
);
190 $max = $max->max +
100;
192 if (isset($moveup)) {
193 $movecourse = get_record('course', 'id', $moveup);
194 $swapcourse = get_record('course',
195 'category', $category->id
,
196 'sortorder', $movecourse->sortorder
- 1);
198 $movecourse = get_record('course', 'id', $movedown);
199 $swapcourse = get_record('course',
200 'category', $category->id
,
201 'sortorder', $movecourse->sortorder +
1);
204 if ($swapcourse and $movecourse) { // Renumber everything for robustness
206 if (!( set_field("course", "sortorder", $max, "id", $swapcourse->id
)
207 && set_field("course", "sortorder", $swapcourse->sortorder
, "id", $movecourse->id
)
208 && set_field("course", "sortorder", $movecourse->sortorder
, "id", $swapcourse->id
)
210 notify("Could not update that course!");
217 } // End of editing stuff
219 /// Print out all the sub-categories
220 if ($subcategories = get_records("course_categories", "parent", $category->id
, "sortorder ASC")) {
222 foreach ($subcategories as $subcategory) {
223 if ($subcategory->visible
or iscreator()) {
224 $subcategorieswereshown = true;
226 echo '<table align="center" border="0" cellspacing="2" cellpadding="4" class="generalbox">';
227 echo '<tr><th>'.get_string('subcategories').'</th></tr>';
228 echo '<tr><td nowrap="nowrap">';
231 $catlinkcss = $subcategory->visible ?
"" : " class=\"dimmed\" ";
232 echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id
.'">'.
233 $subcategory->name
.'</a><br />';
237 echo "</td></tr></table>";
243 /// Print out all the courses
244 unset($course); // To avoid unwanted language effects later
246 $courses = get_courses_page($category->id
, 'c.sortorder ASC',
247 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible,c.teacher,c.guest,c.password',
248 $totalcount, $page*$perpage, $perpage);
249 $numcourses = count($courses);
252 if (empty($subcategorieswereshown)) {
253 print_heading(get_string("nocoursesyet"));
256 } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE
and !$page and !$creatorediting) {
257 print_courses($category, "80%");
260 print_paging_bar($totalcount, $page, $perpage, "category.php?id=$category->id&perpage=$perpage&");
262 $strcourses = get_string("courses");
263 $strselect = get_string("select");
264 $stredit = get_string("edit");
265 $strdelete = get_string("delete");
266 $strbackup = get_string("backup");
267 $strrestore = get_string("restore");
268 $strmoveup = get_string("moveup");
269 $strmovedown = get_string("movedown");
270 $strupdate = get_string("update");
271 $strhide = get_string("hide");
272 $strshow = get_string("show");
273 $strsummary = get_string("summary");
274 $strsettings = get_string("settings");
275 $strassignteachers = get_string("assignteachers");
276 $strallowguests = get_string("allowguests");
277 $strrequireskey = get_string("requireskey");
280 echo '<form name="movecourses" action="category.php" method="post">';
281 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
282 echo '<table align="center" border="0" cellspacing="2" cellpadding="4" class="generalbox"><tr>';
283 echo '<th>'.$strcourses.'</th>';
284 if ($creatorediting) {
285 echo '<th>'.$stredit.'</th>';
287 echo '<th>'.$strselect.'</th>';
290 echo '<th> </th>';
296 $abletomovecourses = false; // for now
298 foreach ($courses as $acourse) {
300 $up = ($count == 1) ?
false : true;
301 $down = ($count == $numcourses) ?
false : true;
303 $linkcss = $acourse->visible ?
"" : ' class="dimmed" ';
305 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id
.'">'.$acourse->fullname
.'</a></td>';
306 if ($creatorediting) {
309 echo '<a title="'.$strsettings.'" href="'.$CFG->wwwroot
.'/course/edit.php?id='.
311 '<img src="'.$CFG->pixpath
.'/t/edit.gif" height="11" width="11" border="0" alt="" /></a> ';
312 echo '<a title="'.$strassignteachers.'" href="'.$CFG->wwwroot
.'/course/teacher.php?id='.
314 '<img src="'.$CFG->pixpath
.'/t/user.gif" height="11" width="11" border="0" alt="" /></a> ';
315 echo '<a title="'.$strdelete.'" href="delete.php?id='.$acourse->id
.'">'.
316 '<img src="'.$CFG->pixpath
.'/t/delete.gif" height="11" width="11" border="0" alt="" /></a> ';
317 if (!empty($acourse->visible
)) {
318 echo '<a title="'.$strhide.'" href="category.php?id='.$category->id
.'&page='.$page.
319 '&hide='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
320 '<img src="'.$CFG->pixpath
.'/t/hide.gif" height="11" width="11" border="0" alt="" /></a> ';
322 echo '<a title="'.$strshow.'" href="category.php?id='.$category->id
.'&page='.$page.
323 '&show='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
324 '<img src="'.$CFG->pixpath
.'/t/show.gif" height="11" width="11" border="0" alt="" /></a> ';
327 echo '<a title="'.$strbackup.'" href="../backup/backup.php?id='.$acourse->id
.'">'.
328 '<img src="'.$CFG->pixpath
.'/t/backup.gif" height="11" width="11" border="0" alt="" /></a> ';
330 echo '<a title="'.$strrestore.'" href="../files/index.php?id='.$acourse->id
.
331 '&wdir=/backupdata">'.
332 '<img src="'.$CFG->pixpath
.'/t/restore.gif" height="11" width="11" border="0" alt="" /></a> ';
335 echo '<a title="'.$strmoveup.'" href="category.php?id='.$category->id
.'&page='.$page.
336 '&moveup='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
337 '<img src="'.$CFG->pixpath
.'/t/up.gif" height="11" width="11" border="0" alt="" /></a> ';
339 echo '<img src="'.$CFG->wwwroot
.'/pix/spacer.gif" height="11" width="11" border="0" alt="" /> ';
343 echo '<a title="'.$strmovedown.'" href="category.php?id='.$category->id
.'&page='.$page.
344 '&movedown='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
345 '<img src="'.$CFG->pixpath
.'/t/down.gif" height="11" width="11" border="0" alt="" /></a> ';
347 echo '<img src="'.$CFG->wwwroot
.'/pix/spacer.gif" height="11" width="11" border="0" alt="" /> ';
351 echo '<td align="center">';
352 echo '<input type="checkbox" name="c'.$acourse->id
.'" />';
353 $abletomovecourses = true;
355 } else if (isteacheredit($acourse->id
)) {
357 echo '<a title="'.$strsettings.'" href="'.$CFG->wwwroot
.'/course/edit.php?id='.$acourse->id
.'">'.
358 '<img src="'.$CFG->pixpath
.'/t/edit.gif" height="11" width="11" border="0" alt="" /></a> ';
359 echo '<a title="'.$strassignteachers.'" href="'.$CFG->wwwroot
.'/course/teacher.php?id='.$acourse->id
.'">'.
360 '<img src="'.$CFG->pixpath
.'/t/user.gif" height="11" width="11" border="0" alt="" /></a> ';
364 echo '<td align="right">';
365 if (!empty($acourse->guest
)) {
366 echo '<a href="view.php?id='.$acourse->id
.'"><img hspace="2" title="'.
367 $strallowguests.'" alt="" height="16" width="16" border="0" src="'.
368 $CFG->pixpath
.'/i/user.gif" /></a>';
370 if (!empty($acourse->password
)) {
371 echo '<a href="view.php?id='.$acourse->id
.'"><img hspace="2" title="'.
372 $strrequireskey.'" alt="" height="16" width="16" border="0" src="'.
373 $CFG->pixpath
.'/i/key.gif" /></a>';
375 if (!empty($acourse->summary
)) {
376 link_to_popup_window ("/course/info.php?id=$acourse->id", "courseinfo",
377 '<img hspace="2" alt="info" height="16" width="16" border="0" src="'.$CFG->pixpath
.'/i/info.gif" />',
378 400, 500, $strsummary);
385 if ($abletomovecourses) {
386 echo '<tr><td colspan="3" align="right">';
388 unset($displaylist[$category->id
]);
389 choose_from_menu ($displaylist, "moveto", "", get_string("moveselectedcoursesto"), "javascript:document.movecourses.submit()");
390 echo '<input type="hidden" name="id" value="'.$category->id
.'" />';
401 if (isadmin() and $numcourses > 1) { /// Print button to re-sort courses by name
403 $options['id'] = $category->id
;
404 $options['resort'] = 'name';
405 $options['sesskey'] = $USER->sesskey
;
406 print_single_button('category.php', $options, get_string('resortcoursesbyname'), 'get');
409 if (iscreator()) { /// Print button to create a new course
411 $options['category'] = $category->id
;
412 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
416 if (isadmin()) { /// Print form to rename the category
417 $strrename= get_string('rename');
418 echo '<form name="renameform" action="category.php" method="post">';
419 echo '<input type="hidden" name="id" value="'.$category->id
.'" />';
420 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey
.'" />';
421 echo '<input type="text" size="30" name="rename" value="'.s($category->name
).'" alt="'.$strrename.'" />';
422 echo '<input type="submit" value="'.$strrename.'" />';