const TYPE_SYSTEM = 1;
/** @var int Category node type 10 */
const TYPE_CATEGORY = 10;
+ /** var int Category displayed in MyHome navigation node */
+ const TYPE_MY_CATEGORY = 11;
/** @var int Course node type 20 */
const TYPE_COURSE = 20;
/** @var int Course Structure node type 30 */
// If added node is a category node or the user is logged in and it's a course
// then mark added node as a branch (makes it expandable by AJAX)
$type = $childnode->type;
- if (($type==self::TYPE_CATEGORY) || (isloggedin() && $type==self::TYPE_COURSE)) {
+ if (($type == self::TYPE_CATEGORY) || (isloggedin() && ($type == self::TYPE_COURSE)) || ($type == self::TYPE_MY_CATEGORY)) {
$node->nodetype = self::NODETYPE_BRANCH;
}
// If this node is hidden mark it's children as hidden also
}
$coursecount = count($this->addedcategories[$category]->children->type(self::TYPE_COURSE));
} else if ($category instanceof navigation_node) {
- if ($category->type != self::TYPE_CATEGORY) {
+ if (($category->type != self::TYPE_CATEGORY) || ($category->type != self::TYPE_MY_CATEGORY)) {
return false;
}
$coursecount = count($category->children->type(self::TYPE_COURSE));
/**
* Adds a structured category to the navigation in the correct order/place
*
- * @param stdClass $category
- * @param navigation_node $parent
+ * @param stdClass $category category to be added in navigation.
+ * @param navigation_node $parent parent navigation node
+ * @param int $nodetype type of node, if category is under MyHome then it's TYPE_MY_CATEGORY
+ * @return void.
*/
- protected function add_category(stdClass $category, navigation_node $parent) {
+ protected function add_category(stdClass $category, navigation_node $parent, $nodetype = self::TYPE_CATEGORY) {
if (array_key_exists($category->id, $this->addedcategories)) {
return;
}
$url = new moodle_url('/course/category.php', array('id' => $category->id));
$context = context_coursecat::instance($category->id);
$categoryname = format_string($category->name, true, array('context' => $context));
- $categorynode = $parent->add($categoryname, $url, self::TYPE_CATEGORY, $categoryname, $category->id);
+ $categorynode = $parent->add($categoryname, $url, $nodetype, $categoryname, $category->id);
if (empty($category->visible)) {
if (has_capability('moodle/category:viewhiddencategories', get_system_context())) {
$categorynode->hidden = true;
$parent = $this->rootnodes['currentcourse'];
$url = new moodle_url('/course/view.php', array('id'=>$course->id));
} else if ($coursetype == self::COURSE_MY && !$forcegeneric) {
- if (!empty($CFG->navshowmycoursecategories) && ($parent = $this->rootnodes['mycourses']->find($course->category, self::TYPE_CATEGORY))) {
+ if (!empty($CFG->navshowmycoursecategories) && ($parent = $this->rootnodes['mycourses']->find($course->category, self::TYPE_MY_CATEGORY))) {
// Nothing to do here the above statement set $parent to the category within mycourses.
} else {
$parent = $this->rootnodes['mycourses'];
case self::TYPE_CATEGORY :
$this->load_category($this->instanceid);
break;
+ case self::TYPE_MY_CATEGORY :
+ $this->load_category($this->instanceid, self::TYPE_MY_CATEGORY);
+ break;
case self::TYPE_COURSE :
$course = $DB->get_record('course', array('id' => $this->instanceid), '*', MUST_EXIST);
require_course_login($course, true, null, false, true);
list($sql, $params) = $DB->get_in_or_equal($categoryids);
$categories = $DB->get_recordset_select('course_categories', 'id '.$sql.' AND parent = 0', $params, 'sortorder, id');
foreach ($categories as $category) {
- $this->add_category($category, $this->rootnodes['mycourses']);
+ $this->add_category($category, $this->rootnodes['mycourses'], self::TYPE_MY_CATEGORY);
}
$categories->close();
} else {
* request that.
*
* @global moodle_database $DB
- * @param int $categoryid
+ * @param int $categoryid id of category to load in navigation.
+ * @param int $nodetype type of node, if category is under MyHome then it's TYPE_MY_CATEGORY
+ * @return void.
*/
- protected function load_category($categoryid) {
+ protected function load_category($categoryid, $nodetype = self::TYPE_CATEGORY) {
global $CFG, $DB;
$limit = 20;
foreach ($categories as $category) {
context_helper::preload_from_record($category);
if ($category->id == $categoryid) {
- $this->add_category($category, $this);
+ $this->add_category($category, $this, $nodetype);
$basecategory = $this->addedcategories[$category->id];
} else {
$subcategories[] = $category;
if (!is_null($basecategory)) {
foreach ($subcategories as $category) {
- $this->add_category($category, $basecategory);
+ $this->add_category($category, $basecategory, $nodetype);
}
}
- $courses = $DB->get_recordset('course', array('category' => $categoryid), 'sortorder', '*' , 0, $limit);
- foreach ($courses as $course) {
- $this->add_course($course);
+ // If category is shown in MyHome then only show enrolled courses, else show all courses.
+ if ($nodetype === self::TYPE_MY_CATEGORY) {
+ $courses = enrol_get_my_courses();
+ foreach ($courses as $course) {
+ $this->add_course($course, true, self::COURSE_MY);
+ }
+ } else {
+ $courses = $DB->get_recordset('course', array('category' => $categoryid), 'sortorder', '*' , 0, $limit);
+ foreach ($courses as $course) {
+ $this->add_course($course);
+ }
+ $courses->close();
}
- $courses->close();
}
/**
}
$attributes['hidden'] = ($child->hidden);
$attributes['haschildren'] = ($child->children->count()>0 || $child->type == navigation_node::TYPE_CATEGORY);
+ $attributes['haschildren'] = $attributes['haschildren'] || $child->type == navigation_node::TYPE_MY_CATEGORY;
if ($child->children->count() > 0) {
$attributes['children'] = array();