/** * Initialises the navigation object. * * This causes the navigation object to look at the current state of the page * that it is associated with and then load the appropriate content. * * This should only occur the first time that the navigation structure is utilised * which will normally be either when the navbar is called to be displayed or * when a block makes use of it. * * @return bool */ public function initialise() { global $CFG, $SITE, $USER; // Check if it has already been initialised if ($this->initialised || during_initial_install()) { return true; } $this->initialised = true; // Set up the five base root nodes. These are nodes where we will put our // content and are as follows: // site: Navigation for the front page. // myprofile: User profile information goes here. // currentcourse: The course being currently viewed. // mycourses: The users courses get added here. // courses: Additional courses are added here. // users: Other users information loaded here. $this->rootnodes = array(); if (get_home_page() == HOMEPAGE_SITE) { // The home element should be my moodle because the root element is the site if (isloggedin() && !isguestuser()) { // Makes no sense if you aren't logged in $this->rootnodes['home'] = $this->add(get_string('myhome'), new moodle_url('/my/'), self::TYPE_SETTING, null, 'home'); } } else { // The home element should be the site because the root node is my moodle $this->rootnodes['home'] = $this->add(get_string('sitehome'), new moodle_url('/'), self::TYPE_SETTING, null, 'home'); if (!empty($CFG->defaulthomepage) && $CFG->defaulthomepage == HOMEPAGE_MY) { // We need to stop automatic redirection $this->rootnodes['home']->action->param('redirect', '0'); } } $this->rootnodes['site'] = $this->add_course($SITE); $this->rootnodes['myprofile'] = $this->add(get_string('profile'), null, self::TYPE_USER, null, 'myprofile'); $this->rootnodes['currentcourse'] = $this->add(get_string('currentcourse'), null, self::TYPE_ROOTNODE, null, 'currentcourse'); $this->rootnodes['mycourses'] = $this->add(get_string('mycourses'), null, self::TYPE_ROOTNODE, null, 'mycourses'); $this->rootnodes['courses'] = $this->add(get_string('courses'), new moodle_url('/course/index.php'), self::TYPE_ROOTNODE, null, 'courses'); $this->rootnodes['users'] = $this->add(get_string('users'), null, self::TYPE_ROOTNODE, null, 'users'); // We always load the frontpage course to ensure it is available without // JavaScript enabled. $this->add_front_page_course_essentials($this->rootnodes['site'], $SITE); $this->load_course_sections($SITE, $this->rootnodes['site']); $course = $this->page->course; // $issite gets set to true if the current pages course is the sites frontpage course $issite = $this->page->course->id == $SITE->id; // Determine if the user is enrolled in any course. $enrolledinanycourse = enrol_user_sees_own_courses(); $this->rootnodes['currentcourse']->mainnavonly = true; if ($enrolledinanycourse) { $this->rootnodes['mycourses']->isexpandable = true; if ($CFG->navshowallcourses) { // When we show all courses we need to show both the my courses and the regular courses branch. $this->rootnodes['courses']->isexpandable = true; } } else { $this->rootnodes['courses']->isexpandable = true; } // Load the users enrolled courses if they are viewing the My Moodle page AND the admin has not // set that they wish to keep the My Courses branch collapsed by default. if (!empty($CFG->navexpandmycourses) && $this->page->pagelayout === 'mydashboard') { $this->rootnodes['mycourses']->forceopen = true; $this->load_courses_enrolled(); } else { $this->rootnodes['mycourses']->collapse = true; $this->rootnodes['mycourses']->make_inactive(); } $canviewcourseprofile = true; // Next load context specific content into the navigation switch ($this->page->context->contextlevel) { case CONTEXT_SYSTEM: // Nothing left to do here I feel. break; case CONTEXT_COURSECAT: // This is essential, we must load categories. $this->load_all_categories($this->page->context->instanceid, true); break; case CONTEXT_BLOCK: case CONTEXT_COURSE: if ($issite) { // Nothing left to do here. break; } // Load the course associated with the current page into the navigation. $coursenode = $this->add_course($course, false, self::COURSE_CURRENT); // If the course wasn't added then don't try going any further. if (!$coursenode) { $canviewcourseprofile = false; break; } // If the user is not enrolled then we only want to show the // course node and not populate it. // Not enrolled, can't view, and hasn't switched roles if (!can_access_course($course, null, '', true)) { if ($coursenode->isexpandable === true) { // Obviously the situation has changed, update the cache and adjust the node. // This occurs if the user access to a course has been revoked (one way or another) after // initially logging in for this session. $this->get_expand_course_cache()->set($course->id, 1); $coursenode->isexpandable = true; $coursenode->nodetype = self::NODETYPE_BRANCH; } // Very ugly hack - do not force "parents" to enrol into course their child is enrolled in, // this hack has been propagated from user/view.php to display the navigation node. (MDL-25805) if (!$this->current_user_is_parent_role()) { $coursenode->make_active(); $canviewcourseprofile = false; break; } } else { if ($coursenode->isexpandable === false) { // Obviously the situation has changed, update the cache and adjust the node. // This occurs if the user has been granted access to a course (one way or another) after initially // logging in for this session. $this->get_expand_course_cache()->set($course->id, 1); $coursenode->isexpandable = true; $coursenode->nodetype = self::NODETYPE_BRANCH; } } // Add the essentials such as reports etc... $this->add_course_essentials($coursenode, $course); // Extend course navigation with it's sections/activities $this->load_course_sections($course, $coursenode); if (!$coursenode->contains_active_node() && !$coursenode->search_for_active_node()) { $coursenode->make_active(); } break; case CONTEXT_MODULE: if ($issite) { // If this is the site course then most information will have // already been loaded. // However we need to check if there is more content that can // yet be loaded for the specific module instance. $activitynode = $this->rootnodes['site']->find($this->page->cm->id, navigation_node::TYPE_ACTIVITY); if ($activitynode) { $this->load_activity($this->page->cm, $this->page->course, $activitynode); } break; } $course = $this->page->course; $cm = $this->page->cm; // Load the course associated with the page into the navigation $coursenode = $this->add_course($course, false, self::COURSE_CURRENT); // If the course wasn't added then don't try going any further. if (!$coursenode) { $canviewcourseprofile = false; break; } // If the user is not enrolled then we only want to show the // course node and not populate it. if (!can_access_course($course, null, '', true)) { $coursenode->make_active(); $canviewcourseprofile = false; break; } $this->add_course_essentials($coursenode, $course); // Load the course sections into the page $this->load_course_sections($course, $coursenode, null, $cm); $activity = $coursenode->find($cm->id, navigation_node::TYPE_ACTIVITY); if (!empty($activity)) { // Finally load the cm specific navigaton information $this->load_activity($cm, $course, $activity); // Check if we have an active ndoe if (!$activity->contains_active_node() && !$activity->search_for_active_node()) { // And make the activity node active. $activity->make_active(); } } break; case CONTEXT_USER: if ($issite) { // The users profile information etc is already loaded // for the front page. break; } $course = $this->page->course; // Load the course associated with the user into the navigation $coursenode = $this->add_course($course, false, self::COURSE_CURRENT); // If the course wasn't added then don't try going any further. if (!$coursenode) { $canviewcourseprofile = false; break; } // If the user is not enrolled then we only want to show the // course node and not populate it. if (!can_access_course($course, null, '', true)) { $coursenode->make_active(); $canviewcourseprofile = false; break; } $this->add_course_essentials($coursenode, $course); $this->load_course_sections($course, $coursenode); break; } // Load for the current user $this->load_for_user(); if ($this->page->context->contextlevel >= CONTEXT_COURSE && $this->page->context->instanceid != $SITE->id && $canviewcourseprofile) { $this->load_for_user(null, true); } // Load each extending user into the navigation. foreach ($this->extendforuser as $user) { if ($user->id != $USER->id) { $this->load_for_user($user); } } // Give the local plugins a chance to include some navigation if they want. foreach (get_plugin_list_with_function('local', 'extend_navigation') as $function) { $function($this); } // Remove any empty root nodes foreach ($this->rootnodes as $node) { // Dont remove the home node /** @var navigation_node $node */ if ($node->key !== 'home' && !$node->has_children() && !$node->isactive) { $node->remove(); } } if (!$this->contains_active_node()) { $this->search_for_active_node(); } // If the user is not logged in modify the navigation structure as detailed // in {@link http://docs.moodle.org/dev/Navigation_2.0_structure} if (!isloggedin()) { $activities = clone $this->rootnodes['site']->children; $this->rootnodes['site']->remove(); $children = clone $this->children; $this->children = new navigation_node_collection(); foreach ($activities as $child) { $this->children->add($child); } foreach ($children as $child) { $this->children->add($child); } } return true; }
/** * Initialise the navigation given the type and id for the branch to expand. * * @param int $branchtype One of navigation_node::TYPE_* * @param int $id * @return array The expandable nodes */ public function initialise() { global $CFG, $DB, $SITE; if ($this->initialised || during_initial_install()) { return $this->expandable; } $this->initialised = true; $this->rootnodes = array(); $this->rootnodes['site'] = $this->add_course($SITE); $this->rootnodes['mycourses'] = $this->add(get_string('mycourses'), new moodle_url('/my'), self::TYPE_ROOTNODE, null, 'mycourses'); $this->rootnodes['courses'] = $this->add(get_string('courses'), null, self::TYPE_ROOTNODE, null, 'courses'); if (function_exists('enrol_user_sees_own_courses')) { // Determine if the user is enrolled in any course. $enrolledinanycourse = enrol_user_sees_own_courses(); if ($enrolledinanycourse) { $this->rootnodes['mycourses']->isexpandable = true; if ($CFG->navshowallcourses) { // When we show all courses we need to show both the my courses and the regular courses branch. $this->rootnodes['courses']->isexpandable = true; } } else { $this->rootnodes['courses']->isexpandable = true; } } $this->expand($this->branchtype, $this->instanceid); }
public function test_enrol_user_sees_own_courses() { global $DB, $CFG; $this->resetAfterTest(); $studentrole = $DB->get_record('role', array('shortname' => 'student')); $this->assertNotEmpty($studentrole); $teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); $this->assertNotEmpty($teacherrole); $admin = get_admin(); $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); $user3 = $this->getDataGenerator()->create_user(); $user4 = $this->getDataGenerator()->create_user(); $user5 = $this->getDataGenerator()->create_user(); $user6 = $this->getDataGenerator()->create_user(); $category1 = $this->getDataGenerator()->create_category(array('visible' => 0)); $category2 = $this->getDataGenerator()->create_category(); $course1 = $this->getDataGenerator()->create_course(array('category' => $category1->id)); $course2 = $this->getDataGenerator()->create_course(array('category' => $category2->id)); $course3 = $this->getDataGenerator()->create_course(array('category' => $category2->id, 'visible' => 0)); $course4 = $this->getDataGenerator()->create_course(array('category' => $category2->id)); $maninstance1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST); $DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED, array('id' => $maninstance1->id)); $maninstance1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST); $maninstance2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'manual'), '*', MUST_EXIST); $maninstance3 = $DB->get_record('enrol', array('courseid' => $course3->id, 'enrol' => 'manual'), '*', MUST_EXIST); $maninstance4 = $DB->get_record('enrol', array('courseid' => $course4->id, 'enrol' => 'manual'), '*', MUST_EXIST); $manual = enrol_get_plugin('manual'); $this->assertNotEmpty($manual); $manual->enrol_user($maninstance1, $admin->id, $studentrole->id); $manual->enrol_user($maninstance3, $user1->id, $teacherrole->id); $manual->enrol_user($maninstance2, $user2->id, $studentrole->id); $manual->enrol_user($maninstance1, $user3->id, $studentrole->id, 1, time() + 60 * 60); $manual->enrol_user($maninstance2, $user3->id, 0, 1, time() - 60 * 60); $manual->enrol_user($maninstance3, $user2->id, $studentrole->id); $manual->enrol_user($maninstance4, $user2->id, 0, 0, 0, ENROL_USER_SUSPENDED); $manual->enrol_user($maninstance1, $user4->id, $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED); $manual->enrol_user($maninstance3, $user4->id, 0, 0, 0, ENROL_USER_SUSPENDED); $this->assertFalse(enrol_user_sees_own_courses($CFG->siteguest)); $this->assertFalse(enrol_user_sees_own_courses(0)); $this->assertFalse(enrol_user_sees_own_courses($admin)); $this->assertFalse(enrol_user_sees_own_courses(-222)); // Nonexistent user. $this->assertTrue(enrol_user_sees_own_courses($user1)); $this->assertTrue(enrol_user_sees_own_courses($user2->id)); $this->assertFalse(enrol_user_sees_own_courses($user3->id)); $this->assertFalse(enrol_user_sees_own_courses($user4)); $this->assertFalse(enrol_user_sees_own_courses($user5)); $this->setAdminUser(); $this->assertFalse(enrol_user_sees_own_courses()); $this->setGuestUser(); $this->assertFalse(enrol_user_sees_own_courses()); $this->setUser(0); $this->assertFalse(enrol_user_sees_own_courses()); $this->setUser($user1); $this->assertTrue(enrol_user_sees_own_courses()); $this->setUser($user2); $this->assertTrue(enrol_user_sees_own_courses()); $this->setUser($user3); $this->assertFalse(enrol_user_sees_own_courses()); $this->setUser($user4); $this->assertFalse(enrol_user_sees_own_courses()); $this->setUser($user5); $this->assertFalse(enrol_user_sees_own_courses()); $user1 = $DB->get_record('user', array('id' => $user1->id)); $this->setUser($user1); $reads = $DB->perf_get_reads(); $this->assertTrue(enrol_user_sees_own_courses()); $this->assertGreaterThan($reads, $DB->perf_get_reads()); $user1 = $DB->get_record('user', array('id' => $user1->id)); $this->setUser($user1); require_login($course3); $reads = $DB->perf_get_reads(); $this->assertTrue(enrol_user_sees_own_courses()); $this->assertEquals($reads, $DB->perf_get_reads()); }
/** * Initialise the navigation given the type and id for the branch to expand. * * @param int $branchtype One of navigation_node::TYPE_* * @param int $id * @return array The expandable nodes */ public function initialise() { global $CFG, $DB, $SITE, $PAGE; if ($this->initialised || during_initial_install()) { return $this->expandable; } $this->initialised = true; $this->rootnodes = array(); $this->rootnodes['site'] = $this->add_course($SITE); $this->rootnodes['currentcourse'] = $this->add(get_string('currentcourse'), null, self::TYPE_ROOTNODE, null, 'currentcourse'); $this->rootnodes['mycourses'] = $this->add(get_string('mycourses'), new moodle_url('/my'), self::TYPE_ROOTNODE, null, 'mycourses'); $this->rootnodes['courses'] = $this->add(get_string('courses'), null, self::TYPE_ROOTNODE, null, 'courses'); if (!empty($PAGE->theme->settings->coursesleafonly) || !empty($PAGE->theme->settings->coursesloggedinonly) && !isloggedin()) { $this->expandtocourses = false; } if (function_exists('enrol_user_sees_own_courses')) { // Determine if the user is enrolled in any course. $enrolledinanycourse = enrol_user_sees_own_courses(); if ($enrolledinanycourse) { $this->rootnodes['mycourses']->isexpandable = true; if ($CFG->navshowallcourses) { // When we show all courses we need to show both the my courses and the regular courses branch. $this->rootnodes['courses']->isexpandable = true; } } else { $this->rootnodes['courses']->isexpandable = true; } } $PAGE->requires->data_for_js('siteadminexpansion', false); $this->expand($this->branchtype, $this->instanceid); }