| [ Index ] |
PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008] |
[Summary view] [Print] [Text view]
1 <?php // $Id: index.php,v 1.89.2.5 2008/05/21 12:46:00 thepurpleblob Exp $ 2 // For most people, just lists the course categories 3 // Allows the admin to create, delete and rename course categories 4 5 require_once("../config.php"); 6 require_once ("lib.php"); 7 8 $categoryedit = optional_param('categoryedit', -1,PARAM_BOOL); 9 $delete = optional_param('delete',0,PARAM_INT); 10 $hide = optional_param('hide',0,PARAM_INT); 11 $show = optional_param('show',0,PARAM_INT); 12 $move = optional_param('move',0,PARAM_INT); 13 $moveto = optional_param('moveto',-1,PARAM_INT); 14 $moveup = optional_param('moveup',0,PARAM_INT); 15 $movedown = optional_param('movedown',0,PARAM_INT); 16 17 $sysctx = get_context_instance(CONTEXT_SYSTEM); 18 $context = $sysctx; 19 20 if (!$site = get_site()) { 21 error('Site isn\'t defined!'); 22 } 23 24 if ($CFG->forcelogin) { 25 require_login(); 26 } 27 28 if (has_capability('moodle/category:update', $sysctx)) { 29 if ($categoryedit !== -1) { 30 $USER->categoryediting = $categoryedit; 31 } 32 $adminediting = !empty($USER->categoryediting); 33 } else { 34 $adminediting = false; 35 } 36 37 $stradministration = get_string('administration'); 38 $strcategories = get_string('categories'); 39 $strcategory = get_string('category'); 40 $strcourses = get_string('courses'); 41 $stredit = get_string('edit'); 42 $strdelete = get_string('delete'); 43 $straction = get_string('action'); 44 45 46 /// Unless it's an editing admin, just print the regular listing of courses/categories 47 48 if (!$adminediting) { 49 50 /// Print form for creating new categories 51 52 $countcategories = count_records('course_categories'); 53 54 if ($countcategories > 1 || ($countcategories == 1 && count_records('course') > 200)) { 55 $strcourses = get_string('courses'); 56 $strcategories = get_string('categories'); 57 58 $navlinks = array(); 59 $navlinks[] = array('name'=>$strcategories,'link'=>'','type'=>'misc'); 60 $navigation = build_navigation($navlinks); 61 print_header("$site->shortname: $strcategories", $strcourses, $navigation, '', '', true, update_categories_button()); 62 print_heading($strcategories); 63 echo skip_main_destination(); 64 print_box_start('categorybox'); 65 print_whole_category_list(); 66 print_box_end(); 67 print_course_search(); 68 } else { 69 $strfulllistofcourses = get_string('fulllistofcourses'); 70 print_header("$site->shortname: $strfulllistofcourses", $strfulllistofcourses, 71 build_navigation(array(array('name'=>$strfulllistofcourses, 'link'=>'','type'=>'misc'))), 72 '', '', true, update_categories_button()); 73 echo skip_main_destination(); 74 print_box_start('courseboxes'); 75 print_courses(0); 76 print_box_end(); 77 } 78 79 /// I am not sure this context in the next has_capability call is correct. 80 if (isloggedin() and !isguest() and !has_capability('moodle/course:create', $sysctx) and $CFG->enablecourserequests) { // Print link to request a new course 81 print_single_button('request.php', NULL, get_string('courserequest'), 'get'); 82 } 83 if (has_capability('moodle/course:create', $sysctx)) { // Print link to create a new course 84 /// Get the 1st available category 85 $options = array('category' => get_field('course_categories', 'id', 'parent', '0')); 86 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get'); 87 } 88 if (has_capability('moodle/site:approvecourse', $sysctx) and !empty($CFG->enablecourserequests)) { 89 print_single_button('pending.php',NULL, get_string('coursespending'),'get'); 90 } 91 print_footer(); 92 exit; 93 } 94 95 /// From now on is all the admin/course creator functions 96 97 /// Delete a category if necessary 98 99 if (!empty($delete) and confirm_sesskey()) { 100 require_once ('delete_category_form.php'); 101 102 if (!$deletecat = get_record('course_categories', 'id', $delete)) { 103 error('Incorrect category id', 'index.php'); 104 } 105 106 $heading = get_string('deletecategory', '', format_string($deletecat->name)); 107 108 $context = get_context_instance(CONTEXT_COURSECAT, $delete); 109 require_capability('moodle/category:delete', $context); 110 111 $mform = new delete_category_form(null, $deletecat); 112 $mform->set_data(array('delete'=>$delete)); 113 114 if ($mform->is_cancelled()) { 115 redirect('index.php'); 116 117 } else if (!$data= $mform->get_data(false)) { 118 require_once($CFG->libdir . '/questionlib.php'); 119 print_category_edit_header(); 120 print_heading($heading); 121 print_box(get_string('deletecategorycheck2'), 'generalbox boxwidthnormal boxaligncenter'); 122 if (question_context_has_any_questions($context)) { 123 print_box(get_string('deletecoursecategorywithquestions', 'question'), 124 'generalbox boxwidthnormal boxaligncenter'); 125 } 126 $mform->display(); 127 print_footer(); 128 exit(); 129 } 130 131 print_category_edit_header(); 132 print_heading($heading); 133 134 if ($data->fulldelete) { 135 category_delete_full($deletecat, true); 136 } else { 137 category_delete_move($deletecat, $data->newparent, true); 138 } 139 140 print_continue('index.php'); 141 142 print_footer(); 143 die; 144 } 145 146 /// Print headings 147 print_category_edit_header(); 148 print_heading($strcategories); 149 150 151 /// Create a default category if necessary 152 if (!$categories = get_categories()) { /// No category yet! 153 // Try and make one 154 unset($tempcat); 155 $tempcat->name = get_string('miscellaneous'); 156 if (!$tempcat->id = insert_record('course_categories', $tempcat)) { 157 error('Serious error: Could not create a default category!'); 158 } 159 $tempcat->context = get_context_instance(CONTEXT_COURSECAT, $tempcat->id); 160 mark_context_dirty('/'.SYSCONTEXTID); 161 } 162 163 164 /// Move a category to a new parent if required 165 166 if (!empty($move) and ($moveto>=0) and confirm_sesskey()) { 167 if ($tempcat = get_record('course_categories', 'id', $move)) { 168 if ($tempcat->parent != $moveto) { 169 $newp = get_record('course_categories', 'id', $moveto); 170 if (! move_category($tempcat, $newp)) { 171 notify('Could not update that category!'); 172 } 173 } 174 } 175 } 176 177 178 /// Hide or show a category 179 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) { 180 if (!empty($hide)) { 181 $tempcat = get_record('course_categories', 'id', $hide); 182 $visible = 0; 183 } else { 184 $tempcat = get_record('course_categories', 'id', $show); 185 $visible = 1; 186 } 187 if ($tempcat) { 188 if (! set_field('course_categories', 'visible', $visible, 'id', $tempcat->id)) { 189 notify('Could not update that category!'); 190 } 191 if (! set_field('course', 'visible', $visible, 'category', $tempcat->id)) { 192 notify('Could not hide/show any courses in this category !'); 193 } 194 } 195 } 196 197 198 /// Move a category up or down 199 200 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) { 201 202 $swapcategory = NULL; 203 $movecategory = NULL; 204 205 if (!empty($moveup)) { 206 if ($movecategory = get_record('course_categories', 'id', $moveup)) { 207 $categories = get_categories($movecategory->parent); 208 209 foreach ($categories as $category) { 210 if ($category->id == $movecategory->id) { 211 break; 212 } 213 $swapcategory = $category; 214 } 215 unset($category); 216 } 217 } 218 if (!empty($movedown)) { 219 if ($movecategory = get_record('course_categories', 'id', $movedown)) { 220 $categories = get_categories($movecategory->parent); 221 222 $choosenext = false; 223 foreach ($categories as $category) { 224 if ($choosenext) { 225 $swapcategory = $category; 226 break; 227 } 228 if ($category->id == $movecategory->id) { 229 $choosenext = true; 230 } 231 } 232 unset($category); 233 } 234 } 235 if ($swapcategory and $movecategory) { // Renumber everything for robustness 236 $count=0; 237 foreach ($categories as $category) { 238 $count++; 239 if ($category->id == $swapcategory->id) { 240 $category = $movecategory; 241 } else if ($category->id == $movecategory->id) { 242 $category = $swapcategory; 243 } 244 if (! set_field('course_categories', 'sortorder', $count, 'id', $category->id)) { 245 notify('Could not update that category!'); 246 } 247 } 248 unset($category); 249 } 250 } 251 252 /// Find any orphan courses that don't yet have a valid category and set to default 253 fix_coursecategory_orphans(); 254 255 /// Should be a no-op 99% of the cases 256 fix_course_sortorder(); 257 258 /// Print out the categories with all the knobs 259 260 $strcategories = get_string('categories'); 261 $strcourses = get_string('courses'); 262 $strmovecategoryto = get_string('movecategoryto'); 263 $stredit = get_string('edit'); 264 265 $displaylist = array(); 266 $parentlist = array(); 267 268 $displaylist[0] = get_string('top'); 269 make_categories_list($displaylist, $parentlist, ''); 270 271 echo '<table class="generalbox editcourse boxaligncenter"><tr class="header">'; 272 echo '<th class="header" scope="col">'.$strcategories.'</th>'; 273 echo '<th class="header" scope="col">'.$strcourses.'</th>'; 274 echo '<th class="header" scope="col">'.$stredit.'</th>'; 275 echo '<th class="header" scope="col">'.$strmovecategoryto.'</th>'; 276 echo '</tr>'; 277 278 print_category_edit(NULL, $displaylist, $parentlist); 279 280 echo '</table>'; 281 282 echo '<div class="buttons">'; 283 284 if (!empty($category->id)) { 285 // Print link to create a new course in current category 286 if (has_capability('moodle/course:create', $context)) { 287 $options = array(); 288 $options['category'] = $category->id; 289 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get'); 290 } 291 }else{ 292 if (has_capability('moodle/course:create', $sysctx)) { 293 // print create course link to first category 294 $options = array(); 295 $options = array('category' => get_field('course_categories', 'id', 'parent', '0')); 296 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get'); 297 } 298 } 299 300 // Print button for creating new categories 301 if (has_capability('moodle/category:create', $context)) { 302 unset($options); 303 if (!empty($category->id)) { 304 $options['id'] = $category->id; 305 } else { 306 $options['id'] = 0; 307 } 308 $options['categoryadd'] = 1; 309 print_single_button('editcategory.php', $options, get_string('addnewcategory'), 'get'); 310 } 311 312 if (has_capability('moodle/site:approvecourse', $sysctx) and !empty($CFG->enablecourserequests)) { 313 print_single_button('pending.php',NULL, get_string('coursespending'), 'get'); 314 } 315 // admin page does not allow custom buttons in the navigation bar 316 echo '<div class="singlebutton">'; 317 echo update_categories_button(); 318 echo '</div></div>'; 319 320 print_footer(); 321 322 function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) { 323 /// Recursive function to print all the categories ready for editing 324 325 global $CFG, $USER; 326 327 static $str = ''; 328 329 if (empty($str)) { 330 $str->edit = get_string('edit'); 331 $str->delete = get_string('delete'); 332 $str->moveup = get_string('moveup'); 333 $str->movedown = get_string('movedown'); 334 $str->edit = get_string('editthiscategory'); 335 $str->hide = get_string('hide'); 336 $str->show = get_string('show'); 337 } 338 339 if ($category) { 340 341 if (!isset($category->context)) { 342 $category->context = get_context_instance(CONTEXT_COURSECAT, $category->id); 343 } 344 345 echo '<tr><td align="left" class="name">'; 346 for ($i=0; $i<$depth;$i++) { 347 echo ' '; 348 } 349 $linkcss = $category->visible ? '' : ' class="dimmed" '; 350 echo '<a '.$linkcss.' title="'.$str->edit.'" '. 351 ' href="category.php?id='.$category->id.'&categoryedit=on&sesskey='.sesskey().'">'. 352 format_string($category->name).'</a>'; 353 echo '</td>'; 354 355 echo '<td class="count">'.$category->coursecount.'</td>'; 356 357 echo '<td class="icons">'; /// Print little icons 358 359 if (has_capability('moodle/category:update', $category->context)) { 360 echo '<a title="'.$str->edit.'" href="editcategory.php?id='.$category->id.'&sesskey='.sesskey().'"><img'. 361 ' src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$str->edit.'" /></a> '; 362 } 363 364 if (has_capability('moodle/category:delete', $category->context)) { 365 echo '<a title="'.$str->delete.'" href="index.php?delete='.$category->id.'&sesskey='.sesskey().'"><img'. 366 ' src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$str->delete.'" /></a> '; 367 } 368 369 if (has_capability('moodle/category:visibility', $category->context)) { 370 if (!empty($category->visible)) { 371 echo '<a title="'.$str->hide.'" href="index.php?hide='.$category->id.'&sesskey='.sesskey().'"><img'. 372 ' src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$str->hide.'" /></a> '; 373 } else { 374 echo '<a title="'.$str->show.'" href="index.php?show='.$category->id.'&sesskey='.sesskey().'"><img'. 375 ' src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$str->show.'" /></a> '; 376 } 377 } 378 379 if ($up) { 380 echo '<a title="'.$str->moveup.'" href="index.php?moveup='.$category->id.'&sesskey='.sesskey().'"><img'. 381 ' src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" alt="'.$str->moveup.'" /></a> '; 382 } 383 if ($down) { 384 echo '<a title="'.$str->movedown.'" href="index.php?movedown='.$category->id.'&sesskey='.sesskey().'"><img'. 385 ' src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" alt="'.$str->movedown.'" /></a> '; 386 } 387 echo '</td>'; 388 389 echo '<td align="left">'; 390 $tempdisplaylist = $displaylist; 391 unset($tempdisplaylist[$category->id]); 392 foreach ($parentslist as $key => $parents) { 393 if (in_array($category->id, $parents)) { 394 unset($tempdisplaylist[$key]); 395 } 396 } 397 popup_form ("index.php?move=$category->id&sesskey=$USER->sesskey&moveto=", $tempdisplaylist, "moveform$category->id", $category->parent, '', '', '', false); 398 echo '</td>'; 399 echo '</tr>'; 400 } else { 401 $category->id = '0'; 402 } 403 404 if ($categories = get_categories($category->id)) { // Print all the children recursively 405 $countcats = count($categories); 406 $count = 0; 407 $first = true; 408 $last = false; 409 foreach ($categories as $cat) { 410 $count++; 411 if ($count == $countcats) { 412 $last = true; 413 } 414 $up = $first ? false : true; 415 $down = $last ? false : true; 416 $first = false; 417 418 print_category_edit($cat, $displaylist, $parentslist, $depth+1, $up, $down); 419 } 420 } 421 } 422 423 function print_category_edit_header() { 424 global $CFG; 425 global $SITE; 426 427 if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) { 428 require_once($CFG->libdir.'/adminlib.php'); 429 admin_externalpage_setup('coursemgmt'); 430 admin_externalpage_print_header(); 431 } else { 432 print_header("$SITE->shortname:". get_string('categories'), get_string('courses'), 433 build_navigation(array(array('name'=>get_string('categories'),'link'=>'','type'=>'misc'))), '', '', true, update_categories_button()); 434 } 435 } 436 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Jan 14 11:33:29 2009 | Cross-referenced by PHPXref 0.7 |