/** * Update categories * * @param array $categories The list of categories to update * @return null * @since Moodle 2.3 */ public static function update_categories($categories) { global $CFG, $DB; require_once $CFG->dirroot . "/course/lib.php"; // Validate parameters. $params = self::validate_parameters(self::update_categories_parameters(), array('categories' => $categories)); $transaction = $DB->start_delegated_transaction(); foreach ($params['categories'] as $cat) { if (!($category = $DB->get_record('course_categories', array('id' => $cat['id'])))) { throw new moodle_exception('unknowcategory'); } $categorycontext = context_coursecat::instance($cat['id']); self::validate_context($categorycontext); require_capability('moodle/category:manage', $categorycontext); if (!empty($cat['name'])) { if (textlib::strlen($cat['name']) > 255) { throw new moodle_exception('categorytoolong'); } $category->name = $cat['name']; } if (!empty($cat['idnumber'])) { if (textlib::strlen($cat['idnumber']) > 100) { throw new moodle_exception('idnumbertoolong'); } $category->idnumber = $cat['idnumber']; } if (!empty($cat['description'])) { $category->description = $cat['description']; $category->descriptionformat = external_validate_format($cat['descriptionformat']); } if (!empty($cat['theme'])) { $category->theme = $cat['theme']; } if (!empty($cat['parent']) && $category->parent != $cat['parent']) { // First check if parent exists. if (!($parent_cat = $DB->get_record('course_categories', array('id' => $cat['parent'])))) { throw new moodle_exception('unknowcategory'); } // Then check if we have capability. self::validate_context(get_category_or_system_context((int) $cat['parent'])); require_capability('moodle/category:manage', get_category_or_system_context((int) $cat['parent'])); // Finally move the category. move_category($category, $parent_cat); $category->parent = $cat['parent']; // Get updated path by move_category(). $category->path = $DB->get_field('course_categories', 'path', array('id' => $category->id)); } $DB->update_record('course_categories', $category); } $transaction->allow_commit(); }
$tempcat = new stdClass(); $tempcat->name = get_string('miscellaneous'); $tempcat->id = $DB->insert_record('course_categories', $tempcat); $tempcat->context = get_context_instance(CONTEXT_COURSECAT, $tempcat->id); mark_context_dirty('/' . SYSCONTEXTID); fix_course_sortorder(); // Required to build course_categories.depth and .path. } /// Move a category to a new parent if required if (!empty($move) and $moveto >= 0 and confirm_sesskey()) { if ($cattomove = $DB->get_record('course_categories', array('id' => $move))) { require_capability('moodle/category:manage', get_category_or_system_context($cattomove->parent)); if ($cattomove->parent != $moveto) { $newparent = $DB->get_record('course_categories', array('id' => $moveto)); require_capability('moodle/category:manage', get_category_or_system_context($moveto)); move_category($cattomove, $newparent); } } } /// Hide or show a category if ($hide and confirm_sesskey()) { if ($tempcat = $DB->get_record('course_categories', array('id' => $hide))) { require_capability('moodle/category:manage', get_category_or_system_context($tempcat->parent)); if ($tempcat->visible == 1) { course_category_hide($tempcat); } } } else { if ($show and confirm_sesskey()) { if ($tempcat = $DB->get_record('course_categories', array('id' => $show))) { require_capability('moodle/category:manage', get_category_or_system_context($tempcat->parent));
/** * Delete category, but move contents to another category. * @param object $ccategory * @param int $newparentid category id * @return bool status */ function category_delete_move($category, $newparentid, $showfeedback = true) { global $CFG, $DB, $OUTPUT; require_once $CFG->libdir . '/gradelib.php'; require_once $CFG->libdir . '/questionlib.php'; require_once $CFG->dirroot . '/cohort/lib.php'; if (!($newparentcat = $DB->get_record('course_categories', array('id' => $newparentid)))) { return false; } if ($children = $DB->get_records('course_categories', array('parent' => $category->id), 'sortorder ASC')) { foreach ($children as $childcat) { move_category($childcat, $newparentcat); } } if ($courses = $DB->get_records('course', array('category' => $category->id), 'sortorder ASC', 'id')) { if (!move_courses(array_keys($courses), $newparentid)) { echo $OUTPUT->notification("Error moving courses"); return false; } echo $OUTPUT->notification(get_string('coursesmovedout', '', format_string($category->name)), 'notifysuccess'); } // move or delete cohorts in this context cohort_delete_category($category); // now delete anything that may depend on course category context grade_course_category_delete($category->id, $newparentid, $showfeedback); if (!question_delete_course_category($category, $newparentcat, $showfeedback)) { echo $OUTPUT->notification(get_string('errordeletingquestionsfromcategory', 'question', $category), 'notifysuccess'); return false; } // finally delete the category and it's context $DB->delete_records('course_categories', array('id' => $category->id)); delete_context(CONTEXT_COURSECAT, $category->id); events_trigger('course_category_deleted', $category); echo $OUTPUT->notification(get_string('coursecategorydeleted', '', format_string($category->name)), 'notifysuccess'); return true; }
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ************************************************************************/ define('OPENLD_ROOT', './'); require OPENLD_ROOT . 'admin_header.php'; $req_page = isset($_REQUEST['page']) ? is_numeric($_REQUEST['page']) ? $_REQUEST['page'] : '1' : '1'; $req_id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '0'; $req_cat = isset($_REQUEST['category']) ? $_REQUEST['category'] : ''; ($hook = get_hook('admin_browse_before_sql_input_check')) ? eval($hook) : null; //SQL Operations if (isset($_REQUEST['delete'])) { delete_category($_REQUEST['delete']); } elseif (isset($_REQUEST['delete_link'])) { delete_link($_REQUEST['delete_link']); } elseif (isset($_REQUEST['move_cat_sql'])) { move_category($req_id, $req_page, $_REQUEST['move_cat_sql'], $_REQUEST['to_new_parent']); } elseif (isset($_REQUEST['move_link_sql'])) { move_link($req_id, $req_page, $_REQUEST['move_link_sql'], $_REQUEST['to_new_parent']); } elseif (isset($_REQUEST['edit_cat_sql'])) { edit_a_category($req_id, $req_page, $_REQUEST['cat_id']); } elseif (isset($_REQUEST['edit_link_sql'])) { edit_a_link($req_id, $req_page, $_REQUEST['edit_link_sql']); } elseif (isset($_REQUEST['add_a_link'])) { add_a_link($req_id); } elseif (isset($_REQUEST['add_a_category'])) { add_a_category($req_id); } elseif (isset($_REQUEST['unaccept'])) { unaccept_category($_REQUEST['unaccept']); } elseif (isset($_REQUEST['unaccept_link'])) { unaccept_link($_REQUEST['unaccept_link']); }
theme_setup(); } if (empty($category) && has_capability('moodle/category:create', $context)) { // Create a new category if (!($newcategory->id = insert_record('course_categories', $newcategory))) { notify("Could not insert the new category '{$newcategory->name}' "); } else { $newcategory->context = get_context_instance(CONTEXT_COURSECAT, $newcategory->id); mark_context_dirty($newcategory->context->path); redirect('index.php?categoryedit=on'); } } elseif (has_capability('moodle/category:update', $context)) { $newcategory->id = $category->id; if ($newcategory->parent != $category->parent) { $parent_cat = get_record('course_categories', 'id', $newcategory->parent); move_category($newcategory, $parent_cat); } if (!update_record('course_categories', $newcategory)) { error("Could not update the category '{$newcategory->name}' "); } else { if ($newcategory->parent == 0) { $redirect_link = 'index.php?categoryedit=on'; } else { $redirect_link = 'category.php?id=' . $newcategory->id . '&categoryedit=on'; } fix_course_sortorder(); redirect($redirect_link); } } } }
/** * Delete category, but move contents to another category. * @param object $ccategory * @param int $newparentid category id * @return bool status */ function category_delete_move($category, $newparentid, $showfeedback = true) { global $CFG; require_once $CFG->libdir . '/gradelib.php'; require_once $CFG->libdir . '/questionlib.php'; if (!($newparentcat = get_record('course_categories', 'id', $newparentid))) { return false; } if ($children = get_records('course_categories', 'parent', $category->id, 'sortorder ASC')) { foreach ($children as $childcat) { if (!move_category($childcat, $newparentcat)) { notify("Error moving category {$childcat->name}"); return false; } } } if ($courses = get_records('course', 'category', $category->id, 'sortorder ASC', 'id')) { if (!move_courses(array_keys($courses), $newparentid)) { notify("Error moving courses"); return false; } notify(get_string('coursesmovedout', '', format_string($category->name)), 'notifysuccess'); } // now delete anything that may depend on course category context grade_course_category_delete($category->id, $newparentid, $showfeedback); if (!question_delete_course_category($category, $newparentcat, $showfeedback)) { notify(get_string('errordeletingquestionsfromcategory', 'question', $category), 'notifysuccess'); return false; } // finally delete the category and it's context delete_records('course_categories', 'id', $category->id); delete_context(CONTEXT_COURSECAT, $category->id); events_trigger('course_category_deleted', $category); notify(get_string('coursecategorydeleted', '', format_string($category->name)), 'notifysuccess'); return true; }
protected function move_category($category, $destcategory) { global $CFG; require_once $CFG->dirroot . '/course/lib.php'; move_category($category, $destcategory); }
/// No category yet! // Try and make one unset($tempcat); $tempcat->name = get_string('miscellaneous'); if (!($tempcat->id = insert_record('course_categories', $tempcat))) { error('Serious error: Could not create a default category!'); } $tempcat->context = get_context_instance(CONTEXT_COURSECAT, $tempcat->id); mark_context_dirty('/' . SYSCONTEXTID); } /// Move a category to a new parent if required if (!empty($move) and $moveto >= 0 and confirm_sesskey()) { if ($tempcat = get_record('course_categories', 'id', $move)) { if ($tempcat->parent != $moveto) { $newp = get_record('course_categories', 'id', $moveto); if (!move_category($tempcat, $newp)) { notify('Could not update that category!'); } } } } /// Hide or show a category if ((!empty($hide) or !empty($show)) and confirm_sesskey()) { if (!empty($hide)) { $tempcat = get_record('course_categories', 'id', $hide); $visible = 0; } else { $tempcat = get_record('course_categories', 'id', $show); $visible = 1; } if ($tempcat) {
if ($standardnames) { $oldname = clean_param($coursecategory->oldname, PARAM_MULTILANG); } else { $oldname = $coursecategory->oldname; } // no guessing when looking for old name, it must be exact match if ($oldcoursecategory = $DB->get_record('course_categories', array('name' => trim($coursecategory->oldname), 'parent' => $coursecategory->oldparent))) { $upt->track('id', $oldcoursecategory->id, 'normal', false); require_capability('moodle/category:manage', get_category_or_system_context((int) $oldcoursecategory->parent)); $DB->set_field('course_categories', 'name', $coursecategory->name, array('id' => $oldcoursecategory->id)); // update hierarchy pointer if necessary if ($coursecategory->parent != $coursecategory->oldparent) { require_capability('moodle/category:manage', get_category_or_system_context((int) $coursecategory->parent)); $parent_cat = $DB->get_record('course_categories', array('id' => $coursecategory->parent)); $coursecategory->id = $oldcoursecategory->id; move_category($coursecategory, $parent_cat); $existingcategory->context = context_coursecat::instance($oldcoursecategory->id); $existingcategory->context->mark_dirty(); } $upt->track('name', '', 'normal', false); // clear previous $upt->track('name', s($oldname) . '-->' . s($coursecategory->name), 'info'); $upt->track('status', $strcoursecategoryrenamed); $renames++; } else { $upt->track('status', $strcoursecategorynotrenamedmissing, 'error'); $renameerrors++; continue; } $existingcategory = $oldcoursecategory; $existingcategory->name = $coursecategory->name;