FIxed bug 3383 ... intro texts no longer center-justified in standard
[moodle.git] / course / category.php
blob1ce016f8b2666fa0cb2aeb0b3faa42b42c35e0db
1 <?php // $Id$
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) {
18 require_login();
21 if (!$category = get_record("course_categories", "id", $id)) {
22 error("Category not known!");
25 if (iscreator()) {
26 if (isset($_GET['edit']) and confirm_sesskey()) {
27 if ($edit == "on") {
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);
38 } else {
39 if (!$category->visible) {
40 error(get_string('notavailable', 'error'));
42 $navbaritem = print_course_search("", true, "navbar");
43 $adminediting = false;
44 $creatorediting = false;
48 if (isadmin()) {
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;
65 begin_sql();
66 foreach ($courses as $course) {
67 set_field('course', 'sortorder', $count, 'id', $course->id);
68 $count++;
70 commit_sql();
71 fix_course_sortorder($category->id);
77 /// Print headings
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) {
87 if ($adminediting) {
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);
92 } else {
93 print_header("$site->shortname: $category->name", "$site->fullname: $strcourses",
94 "<a href=\"index.php\">$strcategories</a> -> $category->name", "", "", true, $navbaritem);
96 } else {
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>";
110 echo "</td><td>";
111 popup_form("category.php?id=", $displaylist, "switchcategory", "$category->id", "", "", "", false);
112 echo "</td></tr></table><br />";
115 /// Editing functions
117 if ($adminediting) {
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);
128 unset($data->id);
129 unset($data->sesskey);
131 if ($data) {
132 foreach ($data as $code => $junk) {
134 $courseid = substr($code, 1);
136 if (! record_exists('course', 'id', $courseid)) {
137 notify('Error finding a course');
138 } else {
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()) {
161 if (isset($hide)) {
162 $course = get_record("course", "id", $hide);
163 $visible = 0;
164 } else {
165 $course = get_record("course", "id", $show);
166 $visible = 1;
168 if ($course) {
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()) {
180 $movecourse = NULL;
181 $swapcourse = NULL;
183 // ensure the course order has no gaps
184 // and isn't at 0
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);
197 } else {
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
205 begin_sql();
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)
209 )) {
210 notify("Could not update that course!");
212 commit_sql();
217 } // End of editing stuff
219 /// Print out all the sub-categories
220 if ($subcategories = get_records("course_categories", "parent", $category->id, "sortorder ASC")) {
221 $firstentry = true;
222 foreach ($subcategories as $subcategory) {
223 if ($subcategory->visible or iscreator()) {
224 $subcategorieswereshown = true;
225 if ($firstentry) {
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">';
229 $firstentry = false;
231 $catlinkcss = $subcategory->visible ? "" : " class=\"dimmed\" ";
232 echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id.'">'.
233 $subcategory->name.'</a><br />';
236 if (!$firstentry) {
237 echo "</td></tr></table>";
238 echo "<br />";
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);
251 if (!$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%");
259 } else {
260 print_paging_bar($totalcount, $page, $perpage, "category.php?id=$category->id&amp;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>';
286 if ($adminediting) {
287 echo '<th>'.$strselect.'</th>';
289 } else {
290 echo '<th>&nbsp;</th>';
292 echo '</tr>';
295 $count = 0;
296 $abletomovecourses = false; // for now
298 foreach ($courses as $acourse) {
299 $count++;
300 $up = ($count == 1) ? false : true;
301 $down = ($count == $numcourses) ? false : true;
303 $linkcss = $acourse->visible ? "" : ' class="dimmed" ';
304 echo '<tr>';
305 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'.$acourse->fullname.'</a></td>';
306 if ($creatorediting) {
307 if ($adminediting) {
308 echo "<td>";
309 echo '<a title="'.$strsettings.'" href="'.$CFG->wwwroot.'/course/edit.php?id='.
310 $acourse->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='.
313 $acourse->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.'&amp;page='.$page.
319 '&amp;hide='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
320 '<img src="'.$CFG->pixpath.'/t/hide.gif" height="11" width="11" border="0" alt="" /></a> ';
321 } else {
322 echo '<a title="'.$strshow.'" href="category.php?id='.$category->id.'&amp;page='.$page.
323 '&amp;show='.$acourse->id.'&amp;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 '&amp;wdir=/backupdata">'.
332 '<img src="'.$CFG->pixpath.'/t/restore.gif" height="11" width="11" border="0" alt="" /></a> ';
334 if ($up) {
335 echo '<a title="'.$strmoveup.'" href="category.php?id='.$category->id.'&amp;page='.$page.
336 '&amp;moveup='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
337 '<img src="'.$CFG->pixpath.'/t/up.gif" height="11" width="11" border="0" alt="" /></a> ';
338 } else {
339 echo '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" height="11" width="11" border="0" alt="" /> ';
342 if ($down) {
343 echo '<a title="'.$strmovedown.'" href="category.php?id='.$category->id.'&amp;page='.$page.
344 '&amp;movedown='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
345 '<img src="'.$CFG->pixpath.'/t/down.gif" height="11" width="11" border="0" alt="" /></a> ';
346 } else {
347 echo '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" height="11" width="11" border="0" alt="" /> ';
350 echo '</td>';
351 echo '<td align="center">';
352 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
353 $abletomovecourses = true;
355 } else if (isteacheredit($acourse->id)) {
356 echo '<td>';
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> ';
362 echo '</td>';
363 } else {
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);
380 echo "</td>";
382 echo "</tr>";
385 if ($abletomovecourses) {
386 echo '<tr><td colspan="3" align="right">';
387 echo '<br />';
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.'" />';
391 echo '</td></tr>';
394 echo '</table>';
395 echo '</form>';
396 echo '<br />';
400 echo '<center>';
401 if (isadmin() and $numcourses > 1) { /// Print button to re-sort courses by name
402 unset($options);
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
410 unset($options);
411 $options['category'] = $category->id;
412 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
413 echo '<br />';
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.'" />';
423 echo "</form>";
424 echo "<br />";
426 echo "</center>";
428 print_footer();