Skip to content

Commit

Permalink
MDL-37329 Navigation: Navigation node My Courses will only show enrol…
Browse files Browse the repository at this point in the history
…led courses
  • Loading branch information
Rajesh Taneja committed Feb 15, 2013
1 parent 6319737 commit db47f68
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
2 changes: 1 addition & 1 deletion blocks/navigation/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2012112900; // The current plugin version (Date: YYYYMMDDXX)
$plugin->version = 2013020800; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2012112900; // Requires this Moodle version
$plugin->component = 'block_navigation'; // Full name of the plugin (used for diagnostics)
8 changes: 6 additions & 2 deletions blocks/navigation/yui/navigation/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ var NODETYPE = {
SYSTEM : 1,
/** @type int Course category = 10 */
CATEGORY : 10,
/** @type int MYCATEGORY = 11 */
MYCATEGORY : 11,
/** @type int Course = 20 */
COURSE : 20,
/** @type int Course section = 30 */
Expand Down Expand Up @@ -454,7 +456,8 @@ BRANCH.prototype = {
this.addChild(object.children[i]);
}
}
if ((this.get('type') == NODETYPE.CATEGORY || this.get('type') == NODETYPE.ROOTNODE) && coursecount >= M.block_navigation.courselimit) {
if ((this.get('type') == NODETYPE.CATEGORY || this.get('type') == NODETYPE.ROOTNODE || this.get('type') == NODETYPE.MYCATEGORY)
&& coursecount >= M.block_navigation.courselimit) {
this.addViewAllCoursesChild(this);
}
this.get('tree').toggleExpansion({target:this.node});
Expand Down Expand Up @@ -486,7 +489,8 @@ BRANCH.prototype = {
branch.addChild(children[i]);
}
}
if (branch.get('type') == NODETYPE.CATEGORY && count >= M.block_navigation.courselimit) {
if ((branch.get('type') == NODETYPE.CATEGORY || branch.get('type') == NODETYPE.MYCATEGORY)
&& count >= M.block_navigation.courselimit) {
this.addViewAllCoursesChild(branch);
}
}
Expand Down
50 changes: 34 additions & 16 deletions lib/navigationlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class navigation_node implements renderable {
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 */
Expand Down Expand Up @@ -322,7 +324,7 @@ public function add_node(navigation_node $childnode, $beforekey=null) {
// 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
Expand Down Expand Up @@ -1515,7 +1517,7 @@ protected function can_add_more_courses_to_category($category) {
}
$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));
Expand Down Expand Up @@ -1684,17 +1686,19 @@ protected function load_all_categories($categoryid = self::LOAD_ROOT_CATEGORIES,
/**
* 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;
Expand Down Expand Up @@ -2350,7 +2354,7 @@ public function add_course(stdClass $course, $forcegeneric = false, $coursetype
$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'];
Expand Down Expand Up @@ -2697,6 +2701,9 @@ public function initialise() {
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);
Expand Down Expand Up @@ -2775,7 +2782,7 @@ protected function load_courses_enrolled() {
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 {
Expand All @@ -2801,9 +2808,11 @@ protected function load_courses_other() {
* 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;
Expand All @@ -2825,7 +2834,7 @@ protected function load_category($categoryid) {
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;
Expand All @@ -2835,15 +2844,23 @@ protected function load_category($categoryid) {

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();
}

/**
Expand Down Expand Up @@ -4273,6 +4290,7 @@ protected function convert_child($child, $depth=1) {
}
$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();
Expand Down

0 comments on commit db47f68

Please sign in to comment.