[ Index ]

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

title

Body

[close]

/course/ -> editcategory.php (source)

   1  <?php // $Id: category.php,v 1.115 2007/09/19 07:08:24 martinlanghoff Exp $
   2  /**
   3   * Displays a category and all its sub-categories.
   4   * In editing mode, allows admins to move/delete/hide categories
   5   */
   6  
   7  require_once("../config.php");
   8  require_once ("lib.php");
   9  require_once ('editcategory_form.php');
  10  
  11  $id             = optional_param('id', 0, PARAM_INT);            // Category id: if not given, show Add a Category form. If given and 'categoryupdate': show edit form
  12  $page           = optional_param('page', 0, PARAM_INT);          // which page to show
  13  $perpage        = optional_param('perpage', $CFG->coursesperpage, PARAM_INT); // how many per page
  14  $hide           = optional_param('hide', 0, PARAM_INT);
  15  $show           = optional_param('show', 0, PARAM_INT);
  16  $moveup         = optional_param('moveup', 0, PARAM_INT);
  17  $movedown       = optional_param('movedown', 0, PARAM_INT);
  18  $moveto         = optional_param('moveto', 0, PARAM_INT);
  19  $categoryedit   = optional_param('categoryedit', -1, PARAM_BOOL);    // Enables Move/Delete/Hide icons near each category in the list
  20  $categoryadd    = optional_param('categoryadd', 0, PARAM_BOOL);  // Enables the Add Category form
  21  $categoryupdate = optional_param('categoryupdate', 0, PARAM_BOOL); // Enables the Edit Category form
  22  $resort         = optional_param('resort', 0, PARAM_BOOL);
  23  $parent         = optional_param('parent', 0, PARAM_INT );
  24  
  25  if (!$site = get_site()) {
  26      error("Site isn't defined!");
  27  }
  28  
  29  if ($categoryadd and !$parent) { // Show Add category form: if $id is given, it is used as the parent category 
  30      $strtitle = get_string("addnewcategory");
  31      $context = get_context_instance(CONTEXT_SYSTEM);
  32      $category = null;
  33  } elseif ($categoryadd and $parent) {
  34      $strtitle = get_string("addnewcategory");
  35      $context = get_context_instance(CONTEXT_COURSECAT,$parent);
  36      $category = null;
  37  } elseif (!is_null($id) && !$categoryadd) { // Show Edit category form: $id is given as the identifier of the category being edited
  38      $strtitle = get_string("editcategorysettings");
  39      $context = get_context_instance(CONTEXT_COURSECAT, $id); 
  40      if (!$category = get_record("course_categories", "id", $id)) {
  41          error("Category not known!");
  42      }
  43  }
  44  
  45  $mform = new editcategory_form('editcategory.php', compact(array('category', 'id')));
  46  
  47  if (!empty($category)) {
  48      $mform->set_data($category); 
  49  } elseif (!is_null($id)) {
  50      $data = new stdClass();
  51      $data->parent = $id;
  52      $data->categoryadd = 1;
  53      $mform->set_data($data);
  54  }
  55      
  56  if ($mform->is_cancelled()){
  57      if (empty($category)) {
  58          redirect($CFG->wwwroot .'/course/index.php?categoryedit=on');
  59      } else {
  60          redirect($CFG->wwwroot.'/course/category.php?categoryedit=on&id='.$category->id);
  61      } 
  62  } else if (($data = $mform->get_data())) {
  63      $newcategory = new stdClass();
  64      $newcategory->name = $data->name;
  65      $newcategory->description = $data->description;
  66      $newcategory->sortorder = 999;
  67      $newcategory->parent = $data->parent; // if $id = 0, the new category will be a top-level category
  68  
  69      if (isset($data->theme) && !empty($CFG->allowcategorythemes)) {
  70          $newcategory->theme = $data->theme;
  71          theme_setup(); /// TODO: Do we really want the theme to be changed here? Doesn't look ok IMO. Eloy - 20080828
  72      }
  73  
  74      if (empty($category) && has_capability('moodle/category:create', $context)) { // Create a new category 
  75          if (!$newcategory->id = insert_record('course_categories', $newcategory)) {
  76              notify( "Could not insert the new category '$newcategory->name' ");
  77          } else {
  78              $newcategory->context = get_context_instance(CONTEXT_COURSECAT, $newcategory->id);
  79              mark_context_dirty($newcategory->context->path);
  80              redirect('index.php?categoryedit=on');
  81          }
  82      } elseif (has_capability('moodle/category:update', $context)) {
  83          $newcategory->id = $category->id;
  84  
  85          if ($newcategory->parent != $category->parent) {
  86              $parent_cat = get_record('course_categories', 'id', $newcategory->parent);
  87              move_category($newcategory, $parent_cat);
  88          }
  89  
  90          if (!update_record('course_categories', $newcategory)) {
  91              error( "Could not update the category '$newcategory->name' ");
  92          } else {
  93              if ($newcategory->parent == 0) {
  94                  $redirect_link = 'index.php?categoryedit=on';
  95              } else {
  96                  $redirect_link = 'category.php?id='.$newcategory->id.'&categoryedit=on'; 
  97              }
  98              fix_course_sortorder();
  99              redirect($redirect_link);
 100          }
 101      } 
 102  }
 103  
 104  
 105  
 106  // If id is given, but not categoryadd or categoryupdate, we show the category with its list of subcategories
 107  if ($id && !$categoryadd && !$categoryupdate && false) { 
 108      /* TODO implement
 109  
 110      if ($CFG->forcelogin) {
 111          require_login();
 112      }
 113  
 114      // Determine whether to allow user to see this category
 115      if (has_capability('moodle/course:create', $context)) {
 116          if ($categoryedit !== -1) {
 117              $USER->categoryediting = $categoryedit;
 118          }
 119          $navbaritem = update_category_button($category->id);
 120          $creatorediting = !empty($USER->categoryediting);
 121          $adminediting = (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)) and $creatorediting);
 122  
 123      } else {
 124          if (!$category->visible) {
 125              print_error('notavailable', 'error');
 126          }
 127          $navbaritem = print_course_search("", true, "navbar");
 128          $adminediting = false;
 129          $creatorediting = false;
 130      }
 131  
 132      // Resort the category if requested 
 133      if ($resort and confirm_sesskey()) {
 134          if ($courses = get_courses($id, "fullname ASC", 'c.id,c.fullname,c.sortorder')) {
 135              // move it off the range
 136              $count = get_record_sql('SELECT MAX(sortorder) AS max, 1
 137                                       FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
 138              $count = $count->max + 100;
 139              begin_sql();
 140              foreach ($courses as $course) {
 141                  set_field('course', 'sortorder', $count, 'id', $course->id);
 142                  $count++;
 143              }
 144              commit_sql();
 145              fix_course_sortorder($category->id);
 146          }
 147      }
 148      
 149      // Print headings 
 150      $numcategories = count_records("course_categories");
 151  
 152      $stradministration = get_string("administration");
 153      $strcategories = get_string("categories");
 154      $strcategory = get_string("category");
 155      $strcourses = get_string("courses");
 156  
 157      $navlinks = array();
 158      $navlinks[] = array('name' => $strcategories, 'link' => 'index.php', 'type' => 'misc');
 159      $navlinks[] = array('name' => $category->name, 'link' => null, 'type' => 'misc');
 160      $navigation = build_navigation($navlinks);
 161  
 162      if ($creatorediting) {
 163          if ($adminediting) {
 164              // modify this to treat this as an admin page
 165  
 166              require_once($CFG->libdir.'/adminlib.php');
 167              admin_externalpage_setup('categorymgmt');
 168              admin_externalpage_print_header();
 169          } else {
 170              print_header("$site->shortname: $category->name", "$site->fullname: $strcategories", $navigation, "", "", true, $navbaritem);
 171          }
 172      } else {
 173          print_header("$site->shortname: $category->name", "$site->fullname: $strcategories", $navigation, "", "", true, $navbaritem);
 174      }
 175  
 176      // Print button to turn editing off
 177      if ($adminediting) {
 178          echo '<div class="categoryediting button" align="right">'.update_category_button($category->id).'</div>';
 179      }
 180  
 181      // Print link to roles
 182  
 183      if (has_capability('moodle/role:assign', $context)) {
 184          echo '<div class="rolelink"><a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.
 185           $context->id.'">'.get_string('assignroles','role').'</a></div>';
 186      }
 187      
 188      // Print the category selector
 189  
 190      $displaylist = array();
 191      $parentlist = array();
 192  
 193      make_categories_list($displaylist, $parentlist, "");
 194  
 195      echo '<div class="categorypicker">';
 196      popup_form('category.php?id=', $displaylist, 'switchcategory', $category->id, '', '', '', false, 'self', $strcategories.':');
 197      echo '</div>';
 198  
 199      // Print current category description
 200      if ($category->description) {
 201          print_box_start();
 202          print_heading(get_string('description'));
 203          echo $category->description;
 204          print_box_end();
 205      }
 206      
 207      // Editing functions 
 208      if ($creatorediting) {
 209      // Move a specified category to a new category
 210  
 211          if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) {   // Some courses are being moved
 212  
 213              // user must have category update in both cats to perform this
 214              require_capability('moodle/category:update', $context);
 215              require_capability('moodle/category:update', get_context_instance(CONTEXT_COURSECAT, $moveto));
 216  
 217              if (!$destcategory = get_record("course_categories", "id", $data->moveto)) {
 218                  error("Error finding the destination category");
 219              } 
 220              // TODO function to move the category
 221          }
 222  
 223          // Hide or show a category 
 224          if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
 225              require_capability('moodle/category:visibility', $context);
 226              if (!empty($hide)) {
 227                  $category = get_record("course_categories", "id", $hide);
 228                  $visible = 0;
 229              } else {
 230                  $category = get_record("course_categories", "id", $show);
 231                  $visible = 1;
 232              }
 233              if ($category) {
 234                  if (! set_field("course_categories", "visible", $visible, "id", $category->id)) {
 235                      notify("Could not update that category!");
 236                  }
 237              }
 238          }
 239  
 240  
 241          // Move a category up or down 
 242          if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
 243              require_capability('moodle/category:update', $context);
 244              $movecategory = NULL;
 245              $swapcategory = NULL;
 246  
 247              // TODO something like fix_course_sortorder() ?
 248  
 249              // we are going to need to know the range
 250              $max = get_record_sql('SELECT MAX(sortorder) AS max, 1 FROM ' . $CFG->prefix . 'course_categories WHERE id=' . $category->id);
 251              $max = $max->max + 100;
 252  
 253              if (!empty($moveup)) {
 254                  $movecategory = get_record('course_categories', 'id', $moveup);
 255                  $swapcategory = get_record('course_categories',
 256                                           'category',  $category->id,
 257                                           'sortorder', $movecategory->sortorder - 1);
 258              } else {
 259                  $movecategory = get_record('course_categories', 'id', $movedown);
 260                  $swapcategory = get_record('course_categories',
 261                                           'category',  $category->id,
 262                                           'sortorder', $movecategory->sortorder + 1);
 263              }
 264  
 265              if ($swapcourse and $movecourse) {        // Renumber everything for robustness
 266                  begin_sql();
 267                  if (!(    set_field("course", "sortorder", $max, "id", $swapcourse->id)
 268                         && set_field("course", "sortorder", $swapcourse->sortorder, "id", $movecourse->id)
 269                         && set_field("course", "sortorder", $movecourse->sortorder, "id", $swapcourse->id)
 270                      )) {
 271                      notify("Could not update that course!");
 272                  }
 273                  commit_sql();
 274              }
 275  
 276          }
 277  
 278      } // End of editing stuff
 279  
 280      // Print out all the sub-categories
 281      if ($subcategories = get_records("course_categories", "parent", $category->id, "sortorder ASC")) {
 282          $firstentry = true;
 283          foreach ($subcategories as $subcategory) {
 284              if ($subcategory->visible or has_capability('moodle/course:create', $context)) {
 285                  $subcategorieswereshown = true;
 286                  if ($firstentry) {
 287                      echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter">';
 288                      echo '<tr><th scope="col">'.get_string('subcategories').'</th></tr>';
 289                      echo '<tr><td style="white-space: nowrap">';
 290                      $firstentry = false;
 291                  }
 292                  $catlinkcss = $subcategory->visible ? "" : " class=\"dimmed\" ";
 293                  echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id.'">'.
 294                       format_string($subcategory->name).'</a><br />';
 295              }
 296          }
 297          if (!$firstentry) {
 298              echo "</td></tr></table>";
 299              echo "<br />";
 300          }
 301      }
 302  
 303      // print option to add a subcategory
 304      if (has_capability('moodle/category:create', $context) && $creatorediting) {
 305          $cat->id = $id;
 306          $mform->set_data($cat);
 307          $mform->display();
 308      }
 309      */
 310  } 
 311  // Print the form
 312  
 313  $site = get_site();
 314  
 315  $straddnewcategory = get_string("addnewcategory");
 316  $stradministration = get_string("administration");
 317  $strcategories = get_string("categories");
 318  $navlinks = array();
 319  
 320  if (!empty($category->name)) {
 321      $navlinks[] = array('name' => $strtitle,
 322                          'link' => null,
 323                          'type' => 'misc');
 324      $title = $strtitle;
 325      $fullname = $category->name;
 326  } else {
 327      $navlinks[] = array('name' => $stradministration,
 328                          'link' => "$CFG->wwwroot/$CFG->admin/index.php",
 329                          'type' => 'misc');
 330      $navlinks[] = array('name' => $strcategories,
 331                          'link' => 'index.php',
 332                          'type' => 'misc');
 333      $navlinks[] = array('name' => $straddnewcategory,
 334                          'link' => null,
 335                          'type' => 'misc');
 336      $title = "$site->shortname: $straddnewcategory";
 337      $fullname = $site->fullname;
 338  }
 339  
 340  $navigation = build_navigation($navlinks);
 341  print_header($title, $fullname, $navigation, $mform->focus());
 342  print_heading($strtitle);
 343  
 344  $mform->display();
 345  
 346  print_footer();
 347  ?>


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