Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/course/editcategory.php
Go to the documentation of this file.
00001 <?php
00009 require_once('../config.php');
00010 require_once('lib.php');
00011 require_once('editcategory_form.php');
00012 
00013 require_login();
00014 
00015 $id = optional_param('id', 0, PARAM_INT);
00016 $itemid = 0; //initalise itemid, as all files in category description has item id 0
00017 
00018 if ($id) {
00019     if (!$category = $DB->get_record('course_categories', array('id' => $id))) {
00020         print_error('unknowcategory');
00021     }
00022     $PAGE->set_url('/course/editcategory.php', array('id' => $id));
00023     $categorycontext = get_context_instance(CONTEXT_COURSECAT, $id);
00024     $PAGE->set_context($categorycontext);
00025     require_capability('moodle/category:manage', $categorycontext);
00026     $strtitle = get_string('editcategorysettings');
00027     $editorcontext = $categorycontext;
00028     $title = $strtitle;
00029     $fullname = $category->name;
00030 } else {
00031     $parent = required_param('parent', PARAM_INT);
00032     $PAGE->set_url('/course/editcategory.php', array('parent' => $parent));
00033     if ($parent) {
00034         if (!$DB->record_exists('course_categories', array('id' => $parent))) {
00035             print_error('unknowcategory');
00036         }
00037         $context = get_context_instance(CONTEXT_COURSECAT, $parent);
00038     } else {
00039         $context = get_system_context();
00040     }
00041     $PAGE->set_context($context);
00042     $category = new stdClass();
00043     $category->id = 0;
00044     $category->parent = $parent;
00045     require_capability('moodle/category:manage', $context);
00046     $strtitle = get_string("addnewcategory");
00047     $editorcontext = $context;
00048     $itemid = null; //set this explicitly, so files for parent category should not get loaded in draft area.
00049     $title = "$SITE->shortname: ".get_string('addnewcategory');
00050     $fullname = $SITE->fullname;
00051 }
00052 
00053 $PAGE->set_pagelayout('admin');
00054 
00055 $editoroptions = array(
00056     'maxfiles'  => EDITOR_UNLIMITED_FILES,
00057     'maxbytes'  => $CFG->maxbytes,
00058     'trusttext' => true,
00059     'context'   => $editorcontext
00060 );
00061 $category = file_prepare_standard_editor($category, 'description', $editoroptions, $editorcontext, 'coursecat', 'description', $itemid);
00062 
00063 $mform = new editcategory_form('editcategory.php', compact('category', 'editoroptions'));
00064 $mform->set_data($category);
00065 
00066 if ($mform->is_cancelled()) {
00067     if ($id) {
00068         redirect($CFG->wwwroot . '/course/category.php?id=' . $id . '&categoryedit=on');
00069     } else if ($parent) {
00070         redirect($CFG->wwwroot .'/course/category.php?id=' . $parent . '&categoryedit=on');
00071     } else {
00072         redirect($CFG->wwwroot .'/course/index.php?categoryedit=on');
00073     }
00074 } else if ($data = $mform->get_data()) {
00075     $newcategory = new stdClass();
00076     $newcategory->name = $data->name;
00077     $newcategory->idnumber = $data->idnumber;
00078     $newcategory->description_editor = $data->description_editor;
00079     $newcategory->parent = $data->parent; // if $data->parent = 0, the new category will be a top-level category
00080 
00081     if (isset($data->theme) && !empty($CFG->allowcategorythemes)) {
00082         $newcategory->theme = $data->theme;
00083     }
00084 
00085     if ($id) {
00086         // Update an existing category.
00087         $newcategory->id = $category->id;
00088         if ($newcategory->parent != $category->parent) {
00089             // check category manage capability if parent changed
00090             require_capability('moodle/category:manage', get_category_or_system_context((int)$newcategory->parent));
00091             $parent_cat = $DB->get_record('course_categories', array('id' => $newcategory->parent));
00092             move_category($newcategory, $parent_cat);
00093         }
00094     } else {
00095         // Create a new category.
00096         $newcategory->description = $data->description_editor['text'];
00097         $newcategory->sortorder = 999;
00098         $newcategory->id = $DB->insert_record('course_categories', $newcategory);
00099         $newcategory->context = get_context_instance(CONTEXT_COURSECAT, $newcategory->id);
00100         $categorycontext = $newcategory->context;
00101         mark_context_dirty($newcategory->context->path);
00102     }
00103 
00104     $newcategory = file_postupdate_standard_editor($newcategory, 'description', $editoroptions, $categorycontext, 'coursecat', 'description', 0);
00105     $DB->update_record('course_categories', $newcategory);
00106     fix_course_sortorder();
00107 
00108     redirect('category.php?id='.$newcategory->id.'&categoryedit=on');
00109 }
00110 
00111 $PAGE->set_title($title);
00112 $PAGE->set_heading($fullname);
00113 echo $OUTPUT->header();
00114 echo $OUTPUT->heading($strtitle);
00115 $mform->display();
00116 echo $OUTPUT->footer();
00117 
 All Data Structures Namespaces Files Functions Variables Enumerations