case FRONTPAGECATEGORYCOMBO: echo html_writer::tag('a', get_string('skipa', 'access', textlib::strtolower(get_string('courses'))), array('href'=>'#skipcourses', 'class'=>'skip-block')); echo $OUTPUT->heading(get_string('courses'), 2, 'headingblock header'); $renderer = $PAGE->get_renderer('core','course'); // if there are too many courses, budiling course category tree could be slow, // users should go to course index page to see the whole list. $coursecount = $DB->count_records('course'); if (empty($CFG->numcoursesincombo)) { // if $CFG->numcoursesincombo hasn't been set, use default value 500 $CFG->numcoursesincombo = 500; } if ($coursecount > $CFG->numcoursesincombo) { $link = new moodle_url('/course/'); echo $OUTPUT->notification(get_string('maxnumcoursesincombo', 'moodle', array('link'=>$link->out(), 'maxnumofcourses'=>$CFG->numcoursesincombo, 'numberofcourses'=>$coursecount))); } else { echo $renderer->course_category_tree(get_course_category_tree()); } print_course_search('', false, 'short'); echo html_writer::tag('span', '', array('class'=>'skip-block-to', 'id'=>'skipcourses')); break; case FRONTPAGETOPICONLY: // Do nothing!! :-) break; } echo '<br />'; } echo $OUTPUT->footer();
/** * Gets an array whose keys are category ids and whose values are arrays of courses in the corresponding category. * * @deprecated since 2.5 * * This function is not used any more in moodle core and course renderer does not have render function for it. * Combo list on the front page is displayed as: * $renderer = $PAGE->get_renderer('core', 'course'); * echo $renderer->frontpage_combo_list() * * The new class {@link coursecat} stores the information about course category tree * To get children categories use: * coursecat::get($id)->get_children() * To get list of courses use: * coursecat::get($id)->get_courses() * * See http://docs.moodle.org/dev/Courses_lists_upgrade_to_2.5 * * @param int $categoryid * @return array */ function get_category_courses_array($categoryid = 0) { debugging('Function get_category_courses_array() is deprecated, please use methods of coursecat class', DEBUG_DEVELOPER); $tree = get_course_category_tree($categoryid); $flattened = array(); foreach ($tree as $category) { get_category_courses_array_recursively($flattened, $category); } return $flattened; }
/** * This function generates a structured array of courses and categories. * * The depth of categories is limited by $CFG->maxcategorydepth however there * is no limit on the number of courses! * * Suitable for use with the course renderers course_category_tree method: * $renderer = $PAGE->get_renderer('core','course'); * echo $renderer->course_category_tree(get_course_category_tree()); * * @global moodle_database $DB * @param int $id * @param int $depth */ function get_course_category_tree($id = 0, $depth = 0) { global $DB, $CFG; $viewhiddencats = has_capability('moodle/category:viewhiddencategories', get_context_instance(CONTEXT_SYSTEM)); $categories = get_child_categories($id); $categoryids = array(); foreach ($categories as $key => &$category) { if (!$category->visible && !$viewhiddencats) { unset($categories[$key]); continue; } $categoryids[$category->id] = $category; if (empty($CFG->maxcategorydepth) || $depth <= $CFG->maxcategorydepth) { list($category->categories, $subcategories) = get_course_category_tree($category->id, $depth + 1); foreach ($subcategories as $subid => $subcat) { $categoryids[$subid] = $subcat; } $category->courses = array(); } } if ($depth > 0) { // This is a recursive call so return the required array return array($categories, $categoryids); } // The depth is 0 this function has just been called so we can finish it off list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); list($catsql, $catparams) = $DB->get_in_or_equal(array_keys($categoryids)); $sql = "SELECT\n c.id,c.sortorder,c.visible,c.fullname,c.shortname,c.summary,c.category\n {$ccselect}\n FROM {course} c\n {$ccjoin}\n WHERE c.category {$catsql} ORDER BY c.sortorder ASC"; if ($courses = $DB->get_records_sql($sql, $catparams)) { // loop throught them foreach ($courses as $course) { if ($course->id == SITEID) { continue; } context_instance_preload($course); if (!empty($course->visible) || has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) { $categoryids[$course->category]->courses[$course->id] = $course; } } } return $categories; }
/** * Gets an array whose keys are category ids and whose values are arrays of courses in the corresponding category. * * @param int $categoryid * @return array */ function get_category_courses_array($categoryid = 0) { $tree = get_course_category_tree($categoryid); $flattened = array(); foreach ($tree as $category) { get_category_courses_array_recursively($flattened, $category); } return $flattened; }