[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/course/ -> category.php (source)

   1  <?php // $Id: category.php,v 1.119.2.9 2008/10/06 22:29:00 skodak Exp $
   2        // Displays the top level category or all courses
   3        // In editing mode, allows the admin to edit a category,
   4        // and rearrange courses
   5  
   6      require_once("../config.php");
   7      require_once ("lib.php");
   8  
   9      $id           = required_param('id', PARAM_INT);          // Category id
  10      $page         = optional_param('page', 0, PARAM_INT);     // which page to show
  11      $perpage      = optional_param('perpage', $CFG->coursesperpage, PARAM_INT); // how many per page
  12      $categoryedit = optional_param('categoryedit', -1, PARAM_BOOL);
  13      $hide         = optional_param('hide', 0, PARAM_INT);
  14      $show         = optional_param('show', 0, PARAM_INT);
  15      $moveup       = optional_param('moveup', 0, PARAM_INT);
  16      $movedown     = optional_param('movedown', 0, PARAM_INT);
  17      $moveto       = optional_param('moveto', 0, PARAM_INT);
  18      $rename       = optional_param('rename', '', PARAM_TEXT);
  19      $resort       = optional_param('resort', 0, PARAM_BOOL);
  20      $categorytheme= optional_param('categorytheme', false, PARAM_SAFEDIR);
  21  
  22      if ($CFG->forcelogin) {
  23          require_login();
  24      }
  25  
  26      if (!$site = get_site()) {
  27          error("Site isn't defined!");
  28      }
  29  
  30      if (empty($id)) {
  31          error("Category not known!");
  32      }
  33  
  34      if (!$context = get_context_instance(CONTEXT_COURSECAT, $id)) {
  35          error("Category not known!");
  36      }
  37  
  38      if (!$category = get_record("course_categories", "id", $id)) {
  39          error("Category not known!");
  40      }
  41  
  42      if (has_capability('moodle/course:create', $context)) {
  43          if ($categoryedit !== -1) {
  44              $USER->categoryediting = $categoryedit;
  45          }
  46          $navbaritem = update_category_button($category->id);
  47          $creatorediting = !empty($USER->categoryediting);
  48          $adminediting = (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)) and $creatorediting);
  49  
  50      } else {
  51          if (!$category->visible) {
  52              print_error('notavailable', 'error');
  53          }
  54          $navbaritem = print_course_search("", true, "navbar");
  55          $adminediting = false;
  56          $creatorediting = false;
  57      }
  58  
  59      if (has_capability('moodle/category:update', $context)) {
  60          /// Rename the category if requested
  61          if (!empty($rename) and confirm_sesskey()) {
  62              if (! set_field("course_categories", "name", $rename, "id", $category->id)) {
  63                  notify("An error occurred while renaming the category");
  64              }
  65              $category->name = stripslashes($rename);
  66              //trigger events
  67              events_trigger('course_category_updated', $category);
  68          }
  69  
  70          /// Set the category theme if requested
  71          if (($categorytheme !== false) and confirm_sesskey()) {
  72              $category->theme = $categorytheme;
  73              if (! set_field('course_categories', 'theme', $category->theme, 'id', $category->id)) {
  74                  notify('An error occurred while setting the theme');
  75              }
  76          }
  77  
  78          /// Resort the category if requested
  79  
  80          if ($resort and confirm_sesskey()) {
  81              if ($courses = get_courses($category->id, "fullname ASC", 'c.id,c.fullname,c.sortorder')) {
  82                  // move it off the range
  83                  $count = get_record_sql('SELECT MAX(sortorder) AS max, 1
  84                                           FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
  85                  $count = $count->max + 100;
  86                  begin_sql();
  87                  foreach ($courses as $course) {
  88                      set_field('course', 'sortorder', $count, 'id', $course->id);
  89                      $count++;
  90                  }
  91                  commit_sql();
  92                  fix_course_sortorder($category->id);
  93              }
  94          }
  95      }
  96  
  97      if(! empty($CFG->allowcategorythemes) ){
  98          if(isset($category->theme)){
  99              // specifying theme here saves us some dbqs
 100              theme_setup($category->theme);
 101          }
 102      }
 103  
 104  /// Print headings
 105  
 106      $numcategories = count_records("course_categories");
 107  
 108      $stradministration = get_string("administration");
 109      $strcategories = get_string("categories");
 110      $strcategory = get_string("category");
 111      $strcourses = get_string("courses");
 112  
 113      $navlinks = array();
 114      $navlinks[] = array('name' => $strcategories, 'link' => 'index.php', 'type' => 'misc');
 115      $navlinks[] = array('name' => format_string($category->name), 'link' => null, 'type' => 'misc');
 116      $navigation = build_navigation($navlinks);
 117  
 118      if ($creatorediting) {
 119          if ($adminediting) {
 120              // modify this to treat this as an admin page
 121  
 122              require_once($CFG->libdir.'/adminlib.php');
 123              admin_externalpage_setup('coursemgmt');
 124              admin_externalpage_print_header();
 125          } else {
 126              print_header("$site->shortname: $category->name", "$site->fullname: $strcourses", $navigation, "", "", true, $navbaritem);
 127          }
 128      } else {
 129          print_header("$site->shortname: $category->name", "$site->fullname: $strcourses", $navigation, "", "", true, $navbaritem);
 130      }
 131  
 132  /// Print button to turn editing off
 133      if ($adminediting) {
 134          echo '<div class="categoryediting button">'.update_category_button($category->id).'</div>';
 135      }
 136  
 137  /// Print link to roles
 138  
 139      if (has_capability('moodle/role:assign', $context)) {
 140          echo '<div class="rolelink"><a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.
 141           $context->id.'">'.get_string('assignroles','role').'</a></div>';
 142      }
 143  /// Print the category selector
 144  
 145      $displaylist = array();
 146      $parentlist = array();
 147  
 148      make_categories_list($displaylist, $parentlist, "");
 149  
 150      echo '<div class="categorypicker">';
 151      popup_form('category.php?id=', $displaylist, 'switchcategory', $category->id, '', '', '', false, 'self', $strcategories.':');
 152      echo '</div>';
 153  
 154  /// Print current category description
 155      if (!$creatorediting && $category->description) {
 156          print_box_start();
 157          echo format_text($category->description); // for multilang filter
 158          print_box_end();
 159      }
 160  
 161  /// Editing functions
 162  
 163      if ($creatorediting) {
 164      /// Move a specified course to a new category
 165  
 166          if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) {   // Some courses are being moved
 167  
 168              // user must have category update in both cats to perform this
 169              require_capability('moodle/category:update', $context);
 170              require_capability('moodle/category:update', get_context_instance(CONTEXT_COURSECAT, $moveto));
 171  
 172              if (! $destcategory = get_record("course_categories", "id", $data->moveto)) {
 173                  error("Error finding the category");
 174              }
 175  
 176  
 177              $courses = array();
 178              foreach ( $data as $key => $value ) {
 179                  if (preg_match('/^c\d+$/', $key)) {
 180                      array_push($courses, substr($key, 1));
 181                  }
 182              }
 183              move_courses($courses, $data->moveto);
 184          }
 185  
 186      /// Hide or show a course
 187  
 188          if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
 189              require_capability('moodle/course:visibility', $context);
 190              if (!empty($hide)) {
 191                  $course = get_record("course", "id", $hide);
 192                  $visible = 0;
 193              } else {
 194                  $course = get_record("course", "id", $show);
 195                  $visible = 1;
 196              }
 197              if ($course) {
 198                  if (! set_field("course", "visible", $visible, "id", $course->id)) {
 199                      notify("Could not update that course!");
 200                  }
 201              }
 202          }
 203  
 204  
 205      /// Move a course up or down
 206  
 207          if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
 208              require_capability('moodle/category:update', $context);
 209              $movecourse = NULL;
 210              $swapcourse = NULL;
 211  
 212              // ensure the course order has no gaps
 213              // and isn't at 0
 214              fix_course_sortorder($category->id);
 215  
 216              // we are going to need to know the range
 217              $max = get_record_sql('SELECT MAX(sortorder) AS max, 1
 218                                           FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
 219              $max = $max->max + 100;
 220  
 221              if (!empty($moveup)) {
 222                  $movecourse = get_record('course', 'id', $moveup);
 223                  $swapcourse = get_record('course',
 224                                           'category',  $category->id,
 225                                           'sortorder', $movecourse->sortorder - 1);
 226              } else {
 227                  $movecourse = get_record('course', 'id', $movedown);
 228                  $swapcourse = get_record('course',
 229                                           'category',  $category->id,
 230                                           'sortorder', $movecourse->sortorder + 1);
 231              }
 232  
 233              if ($swapcourse and $movecourse) {        // Renumber everything for robustness
 234                  begin_sql();
 235                  if (!(    set_field("course", "sortorder", $max, "id", $swapcourse->id)
 236                         && set_field("course", "sortorder", $swapcourse->sortorder, "id", $movecourse->id)
 237                         && set_field("course", "sortorder", $movecourse->sortorder, "id", $swapcourse->id)
 238                      )) {
 239                      notify("Could not update that course!");
 240                  }
 241                  commit_sql();
 242              }
 243  
 244          }
 245  
 246      } // End of editing stuff
 247  
 248  
 249      if ($creatorediting) {   
 250          echo '<div class="buttons">'; 
 251          if (has_capability('moodle/category:update', $context)) {   // Print button to update this category
 252              unset($options);
 253              $options['id'] = $category->id;
 254              print_single_button('editcategory.php', $options, get_string('editcategorythis'), 'get');
 255          }
 256  
 257          if (has_capability('moodle/category:create', $context)) {   // Print button for creating new categories
 258              unset($options);
 259              $options['categoryadd'] = 1;
 260              $options['id'] = $id;
 261              print_single_button('editcategory.php', $options, get_string('addsubcategory'), 'get');
 262          }
 263          echo '</div>';
 264      }
 265  
 266  /// Print out all the sub-categories
 267      if ($subcategories = get_records("course_categories", "parent", $category->id, "sortorder ASC")) {
 268          $firstentry = true;
 269          foreach ($subcategories as $subcategory) {
 270              if ($subcategory->visible or has_capability('moodle/course:create', $context)) {
 271                  $subcategorieswereshown = true;
 272                  if ($firstentry) {
 273                      echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter">';
 274                      echo '<tr><th scope="col">'.get_string('subcategories').'</th></tr>';
 275                      echo '<tr><td style="white-space: nowrap">';
 276                      $firstentry = false;
 277                  }
 278                  $catlinkcss = $subcategory->visible ? "" : " class=\"dimmed\" ";
 279                  echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id.'">'.
 280                       format_string($subcategory->name).'</a><br />';
 281              }
 282          }
 283          if (!$firstentry) {
 284              echo "</td></tr></table>";
 285              echo "<br />";
 286          }
 287      }
 288  
 289  
 290  /// Print out all the courses
 291      unset($course);    // To avoid unwanted language effects later
 292  
 293      $courses = get_courses_page($category->id, 'c.sortorder ASC',
 294                                  'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible,c.teacher,c.guest,c.password',
 295                                  $totalcount, $page*$perpage, $perpage);
 296      $numcourses = count($courses);
 297  
 298      if (!$courses) {
 299          if (empty($subcategorieswereshown)) {
 300              print_heading(get_string("nocoursesyet"));
 301          }
 302  
 303      } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE and !$page and !$creatorediting) {
 304          print_box_start('courseboxes');
 305          print_courses($category);
 306          print_box_end();
 307  
 308      } else {
 309          print_paging_bar($totalcount, $page, $perpage, "category.php?id=$category->id&amp;perpage=$perpage&amp;");
 310  
 311          $strcourses  = get_string("courses");
 312          $strselect   = get_string("select");
 313          $stredit     = get_string("edit");
 314          $strdelete   = get_string("delete");
 315          $strbackup   = get_string("backup");
 316          $strrestore  = get_string("restore");
 317          $strmoveup   = get_string("moveup");
 318          $strmovedown = get_string("movedown");
 319          $strupdate   = get_string("update");
 320          $strhide     = get_string("hide");
 321          $strshow     = get_string("show");
 322          $strsummary  = get_string("summary");
 323          $strsettings = get_string("settings");
 324          $strassignteachers  = get_string("assignteachers");
 325          $strallowguests     = get_string("allowguests");
 326          $strrequireskey     = get_string("requireskey");
 327  
 328  
 329          echo '<form id="movecourses" action="category.php" method="post"><div>';
 330          echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
 331          echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
 332          echo '<th class="header" scope="col">'.$strcourses.'</th>';
 333          if ($creatorediting) {
 334              echo '<th class="header" scope="col">'.$stredit.'</th>';
 335              if ($adminediting) {
 336                  echo '<th class="header" scope="col">'.$strselect.'</th>';
 337              }
 338          } else {
 339              echo '<th class="header" scope="col">&nbsp;</th>';
 340          }
 341          echo '</tr>';
 342  
 343  
 344          $count = 0;
 345          $abletomovecourses = false;  // for now
 346  
 347          // Checking if we are at the first or at the last page, to allow courses to
 348          // be moved up and down beyond the paging border
 349          if ($totalcount > $perpage) {
 350              $atfirstpage = ($page == 0);
 351              if ($perpage > 0) {
 352                  $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
 353              } else {
 354                  $atlastpage = true;
 355              }
 356          } else {
 357              $atfirstpage = true;
 358              $atlastpage = true;
 359          }
 360  
 361          foreach ($courses as $acourse) {
 362              if (isset($acourse->context)) {
 363                  $coursecontext = $acourse->context;
 364              } else {
 365                  $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
 366              }
 367  
 368              $count++;
 369              $up = ($count > 1 || !$atfirstpage);
 370              $down = ($count < $numcourses || !$atlastpage);
 371  
 372              $linkcss = $acourse->visible ? "" : ' class="dimmed" ';
 373              echo '<tr>';
 374              echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($acourse->fullname) .'</a></td>';
 375              if ($creatorediting) {
 376                  echo "<td>";
 377                  if (has_capability('moodle/course:update', $coursecontext)) {
 378                      echo '<a title="'.$strsettings.'" href="'.$CFG->wwwroot.'/course/edit.php?id='.
 379                           $acourse->id.'">'.
 380                           '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" /></a> ';        }
 381  
 382                  // role assignment link
 383                  if (has_capability('moodle/role:assign', $coursecontext)) {
 384                      echo'<a title="'.get_string('assignroles', 'role').'" href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$coursecontext->id.'"><img src="'.$CFG->pixpath.'/i/roles.gif" class="iconsmall" alt="'.get_string('assignroles', 'role').'" /></a>';
 385                  }
 386  
 387                  if (can_delete_course($acourse->id)) {
 388                      echo '<a title="'.$strdelete.'" href="delete.php?id='.$acourse->id.'">'.
 389                              '<img src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$strdelete.'" /></a> ';
 390                  }
 391  
 392                  // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
 393                  if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
 394                      if (!empty($acourse->visible)) {
 395                          echo '<a title="'.$strhide.'" href="category.php?id='.$category->id.'&amp;page='.$page.
 396                              '&amp;perpage='.$perpage.'&amp;hide='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
 397                              '<img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" /></a> ';
 398                      } else {
 399                          echo '<a title="'.$strshow.'" href="category.php?id='.$category->id.'&amp;page='.$page.
 400                              '&amp;perpage='.$perpage.'&amp;show='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
 401                              '<img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$strshow.'" /></a> ';
 402                      }
 403                  }
 404  
 405                  if (has_capability('moodle/site:backup', $coursecontext)) {
 406                      echo '<a title="'.$strbackup.'" href="../backup/backup.php?id='.$acourse->id.'">'.
 407                              '<img src="'.$CFG->pixpath.'/t/backup.gif" class="iconsmall" alt="'.$strbackup.'" /></a> ';
 408                  }
 409  
 410                  if (has_capability('moodle/site:restore', $coursecontext)) {
 411                      echo '<a title="'.$strrestore.'" href="../files/index.php?id='.$acourse->id.
 412                           '&amp;wdir=/backupdata">'.
 413                           '<img src="'.$CFG->pixpath.'/t/restore.gif" class="iconsmall" alt="'.$strrestore.'" /></a> ';
 414                  }
 415  
 416                  if (has_capability('moodle/category:update', $context)) {
 417                      if ($up) {
 418                          echo '<a title="'.$strmoveup.'" href="category.php?id='.$category->id.'&amp;page='.$page.
 419                               '&amp;perpage='.$perpage.'&amp;moveup='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
 420                               '<img src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" alt="'.$strmoveup.'" /></a> ';
 421                      } else {
 422                          echo '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
 423                      }
 424  
 425                      if ($down) {
 426                          echo '<a title="'.$strmovedown.'" href="category.php?id='.$category->id.'&amp;page='.$page.
 427                               '&amp;perpage='.$perpage.'&amp;movedown='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
 428                               '<img src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" alt="'.$strmovedown.'" /></a> ';
 429                      } else {
 430                          echo '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
 431                      }
 432                      $abletomovecourses = true;
 433                  }
 434  
 435                  echo '</td>';
 436                  echo '<td align="center">';
 437                  echo '<input type="checkbox" name="c'.$acourse->id.'" />';
 438                  echo '</td>';
 439              } else {
 440                  echo '<td align="right">';
 441                  if (!empty($acourse->guest)) {
 442                      echo '<a href="view.php?id='.$acourse->id.'"><img title="'.
 443                           $strallowguests.'" class="icon" src="'.
 444                           $CFG->pixpath.'/i/guest.gif" alt="'.$strallowguests.'" /></a>';
 445                  }
 446                  if (!empty($acourse->password)) {
 447                      echo '<a href="view.php?id='.$acourse->id.'"><img title="'.
 448                           $strrequireskey.'" class="icon" src="'.
 449                           $CFG->pixpath.'/i/key.gif" alt="'.$strrequireskey.'" /></a>';
 450                  }
 451                  if (!empty($acourse->summary)) {
 452                      link_to_popup_window ("/course/info.php?id=$acourse->id", "courseinfo",
 453                                            '<img alt="'.get_string('info').'" class="icon" src="'.$CFG->pixpath.'/i/info.gif" />',
 454                                             400, 500, $strsummary);
 455                  }
 456                  echo "</td>";
 457              }
 458              echo "</tr>";
 459          }
 460  
 461          if ($abletomovecourses) {
 462              echo '<tr><td colspan="3" align="right">';
 463              echo '<br />';
 464              unset($displaylist[$category->id]);
 465  
 466              // loop and unset categories the user can't move into
 467  
 468              foreach ($displaylist as $did=>$dlist) {
 469                  if (!has_capability('moodle/category:update', get_context_instance(CONTEXT_COURSECAT, $did))) {
 470                      unset($displaylist[$did]);
 471                  }
 472              }
 473  
 474              choose_from_menu ($displaylist, "moveto", "", get_string("moveselectedcoursesto"), "javascript: submitFormById('movecourses')");
 475              echo '<input type="hidden" name="id" value="'.$category->id.'" />';
 476              echo '</td></tr>';
 477          }
 478  
 479          echo '</table>';
 480          echo '</div></form>';
 481          echo '<br />';
 482      }
 483  
 484      echo '<div class="buttons">'; 
 485      if (has_capability('moodle/category:update', get_context_instance(CONTEXT_SYSTEM)) and $numcourses > 1) {           /// Print button to re-sort courses by name
 486          unset($options);
 487          $options['id'] = $category->id;
 488          $options['resort'] = 'name';
 489          $options['sesskey'] = $USER->sesskey;
 490          print_single_button('category.php', $options, get_string('resortcoursesbyname'), 'get');
 491      }
 492  
 493      if (has_capability('moodle/course:create', $context)) {         /// Print button to create a new course
 494          unset($options);
 495          $options['category'] = $category->id;
 496          print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
 497      }
 498      echo '</div>'; 
 499  
 500  
 501  
 502      print_course_search();
 503  
 504      print_footer();
 505  
 506  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7