/** * Renders a category list item. * * This function gets called recursively to render sub categories. * * @param coursecat $category The category to render as listitem. * @param coursecat[] $subcategories The subcategories belonging to the category being rented. * @param int $totalsubcategories The total number of sub categories. * @param int $selectedcategory The currently selected category * @param int[] $selectedcategories The path to the selected category and its ID. * @return string */ public function category_listitem(coursecat $category, array $subcategories, $totalsubcategories, $selectedcategory = null, $selectedcategories = array()) { $isexpandable = $totalsubcategories > 0; $isexpanded = !empty($subcategories); $activecategory = $selectedcategory === $category->id; $attributes = array('class' => 'listitem listitem-category', 'data-id' => $category->id, 'data-expandable' => $isexpandable ? '1' : '0', 'data-expanded' => $isexpanded ? '1' : '0', 'data-selected' => $activecategory ? '1' : '0', 'data-visible' => $category->visible ? '1' : '0', 'role' => 'treeitem', 'aria-expanded' => $isexpanded ? 'true' : 'false'); $text = $category->get_formatted_name(); $courseicon = $this->output->pix_icon('i/course', get_string('courses')); $bcatinput = array('type' => 'checkbox', 'name' => 'bcat[]', 'value' => $category->id, 'class' => 'bulk-action-checkbox'); if (!$category->can_resort_subcategories() && !$category->has_manage_capability()) { // Very very hardcoded here. $bcatinput['style'] = 'visibility:hidden'; } $viewcaturl = new moodle_url('/course/management.php', array('categoryid' => $category->id)); if ($isexpanded) { $icon = $this->output->pix_icon('t/switch_minus', get_string('collapse'), 'moodle', array('class' => 'tree-icon')); $icon = html_writer::link($viewcaturl, $icon, array('class' => 'float-left', 'data-action' => 'collapse')); } else { if ($isexpandable) { $icon = $this->output->pix_icon('t/switch_plus', get_string('expand'), 'moodle', array('class' => 'tree-icon')); $icon = html_writer::link($viewcaturl, $icon, array('class' => 'float-left', 'data-action' => 'expand')); } else { $icon = $this->output->pix_icon('i/navigationitem', '', 'moodle', array('class' => 'tree-icon')); $icon = html_writer::link($viewcaturl, $icon, array('class' => 'float-left')); } } $actions = \core_course\management\helper::get_category_listitem_actions($category); $hasactions = !empty($actions) || $category->can_create_course(); $html = html_writer::start_tag('li', $attributes); $html .= html_writer::start_div('clearfix'); $html .= html_writer::start_div('float-left ba-checkbox'); $html .= html_writer::empty_tag('input', $bcatinput) . ' '; $html .= html_writer::end_div(); $html .= $icon; if ($hasactions) { $html .= html_writer::link($viewcaturl, $text, array('class' => 'float-left categoryname')); } else { $html .= html_writer::link($viewcaturl, $text, array('class' => 'float-left categoryname without-actions')); } $html .= html_writer::start_div('float-right'); if ($category->idnumber) { $html .= html_writer::tag('span', s($category->idnumber), array('class' => 'dimmed idnumber')); } if ($hasactions) { $html .= $this->category_listitem_actions($category, $actions); } $countid = 'course-count-' . $category->id; $html .= html_writer::span(get_string('courses'), 'accesshide', array('id' => $countid)); $html .= html_writer::span(html_writer::span($category->get_courses_count()) . $courseicon, 'course-count dimmed', array('aria-labelledby' => $countid)); $html .= html_writer::end_div(); $html .= html_writer::end_div(); if ($isexpanded) { $html .= html_writer::start_tag('ul', array('class' => 'ml', 'role' => 'group')); $catatlevel = \core_course\management\helper::get_expanded_categories($category->path); $catatlevel[] = array_shift($selectedcategories); $catatlevel = array_unique($catatlevel); foreach ($subcategories as $listitem) { $childcategories = in_array($listitem->id, $catatlevel) ? $listitem->get_children() : array(); $html .= $this->category_listitem($listitem, $childcategories, $listitem->get_children_count(), $selectedcategory, $selectedcategories); } $html .= html_writer::end_tag('ul'); } $html .= html_writer::end_tag('li'); return $html; }
/** * Resorts the sub categories of the given category. * * @param \coursecat $category * @param string $sort One of idnumber or name. * @param bool $cleanup If true cleanup will be done, if false you will need to do it manually later. * @return bool * @throws \moodle_exception */ public static function action_category_resort_subcategories(\coursecat $category, $sort, $cleanup = true) { if (!$category->can_resort_subcategories()) { throw new \moodle_exception('permissiondenied', 'error', '', null, 'coursecat::can_resort'); } return $category->resort_subcategories($sort, $cleanup); }