/** * Moves the category under another parent category. All associated contexts are moved as well * * This is protected function, use change_parent() or update() from outside of this class * * @see coursecat::change_parent() * @see coursecat::update() * * @param coursecat $newparentcat * @throws moodle_exception */ protected function change_parent_raw(coursecat $newparentcat) { global $DB; $context = $this->get_context(); $hidecat = false; if (empty($newparentcat->id)) { $DB->set_field('course_categories', 'parent', 0, array('id' => $this->id)); $newparent = context_system::instance(); } else { if ($newparentcat->id == $this->id || in_array($this->id, $newparentcat->get_parents())) { // Can not move to itself or it's own child. throw new moodle_exception('cannotmovecategory'); } $DB->set_field('course_categories', 'parent', $newparentcat->id, array('id' => $this->id)); $newparent = context_coursecat::instance($newparentcat->id); if (!$newparentcat->visible and $this->visible) { // Better hide category when moving into hidden category, teachers may unhide afterwards and the hidden children // will be restored properly. $hidecat = true; } } $this->parent = $newparentcat->id; $context->update_moved($newparent); // Now make it last in new category. $DB->set_field('course_categories', 'sortorder', MAX_COURSES_IN_CATEGORY * MAX_COURSE_CATEGORIES, array('id' => $this->id)); if ($hidecat) { fix_course_sortorder(); $this->restore(); // Hide object but store 1 in visibleold, because when parent category visibility changes this category must // become visible again. $this->hide_raw(1); } }
/** * Presents a course category listing. * * @param coursecat $category The currently selected category. Also the category to highlight in the listing. * @return string */ public function category_listing(coursecat $category = null) { if ($category === null) { $selectedparents = array(); $selectedcategory = null; } else { $selectedparents = $category->get_parents(); $selectedparents[] = $category->id; $selectedcategory = $category->id; } $catatlevel = \core_course\management\helper::get_expanded_categories(''); $catatlevel[] = array_shift($selectedparents); $catatlevel = array_unique($catatlevel); $listing = coursecat::get(0)->get_children(); $attributes = array('class' => 'ml', 'role' => 'tree', 'aria-labelledby' => 'category-listing-title'); $html = html_writer::start_div('category-listing'); $html .= html_writer::tag('h3', get_string('categories'), array('id' => 'category-listing-title')); $html .= $this->category_listing_actions($category); $html .= html_writer::start_tag('ul', $attributes); foreach ($listing as $listitem) { // Render each category in the listing. $subcategories = array(); if (in_array($listitem->id, $catatlevel)) { $subcategories = $listitem->get_children(); } $html .= $this->category_listitem($listitem, $subcategories, $listitem->get_children_count(), $selectedcategory, $selectedparents); } $html .= html_writer::end_tag('ul'); $html .= $this->category_bulk_actions($category); $html .= html_writer::end_div(); return $html; }
/** * Records when a category is expanded or collapsed so that when the user * * @param \coursecat $coursecat The category we're working with. * @param bool $expanded True if the category is expanded now. */ public static function record_expanded_category(\coursecat $coursecat, $expanded = true) { // If this ever changes we are going to reset it and reload the categories as required. self::$expandedcategories = null; $categoryid = $coursecat->id; $path = $coursecat->get_parents(); /* @var \cache_session $cache */ $cache = \cache::make('core', 'userselections'); $categories = $cache->get('categorymanagementexpanded'); if (!is_array($categories)) { if (!$expanded) { // No categories recorded, nothing to remove. return; } $categories = array(); } if ($expanded) { $ref =& $categories; foreach ($coursecat->get_parents() as $path) { if (!isset($ref[$path]) || !is_array($ref[$path])) { $ref[$path] = array(); } $ref =& $ref[$path]; } if (!isset($ref[$categoryid])) { $ref[$categoryid] = true; } } else { $found = true; $ref =& $categories; foreach ($coursecat->get_parents() as $path) { if (!isset($ref[$path])) { $found = false; break; } $ref =& $ref[$path]; } if ($found) { $ref[$categoryid] = null; unset($ref[$categoryid]); } } $cache->set('categorymanagementexpanded', $categories); }