/** * Renders the list of subcategories in a category * * @param coursecat_helper $chelper various display options * @param coursecat $coursecat * @param int $depth depth of the category in the current tree * @return string */ protected function coursecat_subcategories(coursecat_helper $chelper, $coursecat, $depth) { global $CFG; $subcategories = array(); if (!$chelper->get_categories_display_option('nodisplay')) { $subcategories = $coursecat->get_children($chelper->get_categories_display_options()); } $totalcount = $coursecat->get_children_count(); if (!$totalcount) { // Note that we call coursecat::get_children_count() AFTER coursecat::get_children() to avoid extra DB requests. // Categories count is cached during children categories retrieval. return ''; } // prepare content of paging bar or more link if it is needed $paginationurl = $chelper->get_categories_display_option('paginationurl'); $paginationallowall = $chelper->get_categories_display_option('paginationallowall'); if ($totalcount > count($subcategories)) { if ($paginationurl) { // the option 'paginationurl was specified, display pagingbar $perpage = $chelper->get_categories_display_option('limit', $CFG->coursesperpage); $page = $chelper->get_categories_display_option('offset') / $perpage; $pagingbar = $this->paging_bar($totalcount, $page, $perpage, $paginationurl->out(false, array('perpage' => $perpage))); if ($paginationallowall) { $pagingbar .= html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => 'all')), get_string('showall', '', $totalcount)), array('class' => 'paging paging-showall')); } } else { if ($viewmoreurl = $chelper->get_categories_display_option('viewmoreurl')) { // the option 'viewmoreurl' was specified, display more link (if it is link to category view page, add category id) if ($viewmoreurl->compare(new moodle_url('/course/index.php'), URL_MATCH_BASE)) { $viewmoreurl->param('categoryid', $coursecat->id); } $viewmoretext = $chelper->get_categories_display_option('viewmoretext', new lang_string('viewmore')); $morelink = html_writer::tag('div', html_writer::link($viewmoreurl, $viewmoretext), array('class' => 'paging paging-morelink')); } } } else { if ($totalcount > $CFG->coursesperpage && $paginationurl && $paginationallowall) { // there are more than one page of results and we are in 'view all' mode, suggest to go back to paginated view mode $pagingbar = html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => $CFG->coursesperpage)), get_string('showperpage', '', $CFG->coursesperpage)), array('class' => 'paging paging-showperpage')); } } // display list of subcategories $content = html_writer::start_tag('div', array('class' => 'subcategories')); if (!empty($pagingbar)) { $content .= $pagingbar; } foreach ($subcategories as $subcategory) { $content .= $this->coursecat_category($chelper, $subcategory, $depth + 1); } if (!empty($pagingbar)) { $content .= $pagingbar; } if (!empty($morelink)) { $content .= $morelink; } $content .= html_writer::end_tag('div'); return $content; }
/** * Returns the number of courses in the category and sub-categories. * * @param coursecat $coursecat * @return int Count of courses */ protected function get_children_courses_count($coursecat) { $childcoursescount = 0; $coursecatchildren = $coursecat->get_children(); foreach ($coursecatchildren as $coursecatchild) { $childcoursescount += $coursecatchild->get_courses_count(); if ($coursecatchild->get_children_count()) { $childcoursescount += $this->get_children_courses_count($coursecatchild); } } return $childcoursescount; }
/** * Recursive function to print all the categories ready for editing. * * @param html_table $table The table to add data to. * @param coursecat $category The category to render * @param int $depth The depth of the category. * @param bool $up True if this category can be moved up. * @param bool $down True if this category can be moved down. */ function print_category_edit(html_table $table, coursecat $category, $depth = -1, $up = false, $down = false) { global $OUTPUT; static $str = null; if (is_null($str)) { $str = new stdClass; $str->edit = new lang_string('edit'); $str->delete = new lang_string('delete'); $str->moveup = new lang_string('moveup'); $str->movedown = new lang_string('movedown'); $str->edit = new lang_string('editthiscategory'); $str->hide = new lang_string('hide'); $str->show = new lang_string('show'); $str->cohorts = new lang_string('cohorts', 'cohort'); $str->spacer = $OUTPUT->spacer().' '; } if ($category->id) { $categorycontext = context_coursecat::instance($category->id); $attributes = array(); $attributes['class'] = $category->visible ? '' : 'dimmed'; $attributes['title'] = $str->edit; $categoryurl = new moodle_url('/course/manage.php', array('categoryid' => $category->id, 'sesskey' => sesskey())); $categoryname = $category->get_formatted_name(); $categorypadding = str_repeat(' ', $depth); $categoryname = $categorypadding . html_writer::link($categoryurl, $categoryname, $attributes); $icons = array(); if (has_capability('moodle/category:manage', $categorycontext)) { // Edit category. $icons[] = $OUTPUT->action_icon( new moodle_url('/course/editcategory.php', array('id' => $category->id)), new pix_icon('t/edit', $str->edit, 'moodle', array('class' => 'iconsmall')), null, array('title' => $str->edit) ); // Delete category. $icons[] = $OUTPUT->action_icon( new moodle_url('/course/manage.php', array('deletecat' => $category->id, 'sesskey' => sesskey())), new pix_icon('t/delete', $str->delete, 'moodle', array('class' => 'iconsmall')), null, array('title' => $str->delete) ); // Change visibility. if (!empty($category->visible)) { $icons[] = $OUTPUT->action_icon( new moodle_url('/course/manage.php', array('hidecat' => $category->id, 'sesskey' => sesskey())), new pix_icon('t/hide', $str->hide, 'moodle', array('class' => 'iconsmall')), null, array('title' => $str->hide) ); } else { $icons[] = $OUTPUT->action_icon( new moodle_url('/course/manage.php', array('showcat' => $category->id, 'sesskey' => sesskey())), new pix_icon('t/show', $str->show, 'moodle', array('class' => 'iconsmall')), null, array('title' => $str->show) ); } // Cohorts. if (has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:view'), $categorycontext)) { $icons[] = $OUTPUT->action_icon( new moodle_url('/cohort/index.php', array('contextid' => $categorycontext->id)), new pix_icon('t/cohort', $str->cohorts, 'moodle', array('class' => 'iconsmall')), null, array('title' => $str->cohorts) ); } // Move up/down. if ($up) { $icons[] = $OUTPUT->action_icon( new moodle_url('/course/manage.php', array('moveupcat' => $category->id, 'sesskey' => sesskey())), new pix_icon('t/up', $str->moveup, 'moodle', array('class' => 'iconsmall')), null, array('title' => $str->moveup) ); } else { $icons[] = $str->spacer; } if ($down) { $icons[] = $OUTPUT->action_icon( new moodle_url('/course/manage.php', array('movedowncat' => $category->id, 'sesskey' => sesskey())), new pix_icon('t/down', $str->movedown, 'moodle', array('class' => 'iconsmall')), null, array('title' => $str->movedown) ); } else { $icons[] = $str->spacer; } } $actions = ''; if (has_capability('moodle/category:manage', $categorycontext)) { $popupurl = new moodle_url('/course/manage.php', array('movecat' => $category->id, 'sesskey' => sesskey())); $tempdisplaylist = array(0 => get_string('top')) + coursecat::make_categories_list('moodle/category:manage', $category->id); $select = new single_select($popupurl, 'movetocat', $tempdisplaylist, $category->parent, null, "moveform$category->id"); $select->set_label(get_string('frontpagecategorynames'), array('class' => 'accesshide')); $actions = $OUTPUT->render($select); } $table->data[] = new html_table_row(array( // Category name. new html_table_cell($categoryname), // Course count. new html_table_cell($category->coursecount), // Icons. new html_table_cell(join(' ', $icons)), // Actions. new html_table_cell($actions) )); } if ($categories = $category->get_children()) { // Print all the children recursively. $countcats = count($categories); $count = 0; $first = true; $last = false; foreach ($categories as $cat) { $count++; if ($count == $countcats) { $last = true; } $up = $first ? false : true; $down = $last ? false : true; $first = false; print_category_edit($table, $cat, $depth+1, $up, $down); } } }
protected function get_categories(\coursecat $category) { static $categories = array(); $categories[$category->id] = $category->name; foreach ($category->get_children() as $child) { $this->get_categories($child); } return $categories; }