/** * Test course_get_user_navigation_options for students in a normal course. */ public function test_course_get_user_navigation_options_for_students() { global $DB, $CFG; $this->resetAfterTest(); $course = $this->getDataGenerator()->create_course(); $context = context_course::instance($course->id); $user = $this->getDataGenerator()->create_user(); $roleid = $DB->get_field('role', 'id', array('shortname' => 'student')); $this->getDataGenerator()->enrol_user($user->id, $course->id, $roleid); $this->setUser($user); $navoptions = course_get_user_navigation_options($context); $this->assertTrue($navoptions->blogs); $this->assertFalse($navoptions->notes); $this->assertTrue($navoptions->participants); $this->assertTrue($navoptions->badges); // Disable some options. $CFG->badges_allowcoursebadges = 0; $CFG->enableblogs = 0; // Disable view participants capability. assign_capability('moodle/course:viewparticipants', CAP_PROHIBIT, $roleid, $context); $context->mark_dirty(); $navoptions = course_get_user_navigation_options($context); $this->assertFalse($navoptions->blogs); $this->assertFalse($navoptions->notes); $this->assertFalse($navoptions->participants); $this->assertFalse($navoptions->badges); }
/** * Return a list of navigation options in a set of courses that are avaialable or not for the current user. * * @param array $courseids a list of course ids * @return array of warnings and the options availability * @since Moodle 3.2 * @throws moodle_exception */ public static function get_user_navigation_options($courseids) { global $CFG; require_once $CFG->dirroot . '/course/lib.php'; // Parameter validation. $params = self::validate_parameters(self::get_user_navigation_options_parameters(), array('courseids' => $courseids)); $courseoptions = array(); list($courses, $warnings) = external_util::validate_courses($params['courseids'], array(), true); if (!empty($courses)) { foreach ($courses as $course) { // Fix the context for the frontpage. if ($course->id == SITEID) { $course->context = context_system::instance(); } $navoptions = course_get_user_navigation_options($course->context, $course); $options = array(); foreach ($navoptions as $name => $available) { $options[] = array('name' => $name, 'available' => $available); } $courseoptions[] = array('id' => $course->id, 'options' => $options); } } $result = array('courses' => $courseoptions, 'warnings' => $warnings); return $result; }
/** * This generates the structure of the course that won't be generated when * the modules and sections are added. * * Things such as the reports branch, the participants branch, blogs... get * added to the course node by this method. * * @param navigation_node $coursenode * @param stdClass $course * @return bool True for successfull generation */ public function add_front_page_course_essentials(navigation_node $coursenode, stdClass $course) { global $CFG; require_once $CFG->dirroot . '/course/lib.php'; if ($coursenode == false || $coursenode->get('frontpageloaded', navigation_node::TYPE_CUSTOM)) { return true; } $sitecontext = context_system::instance(); $navoptions = course_get_user_navigation_options($sitecontext, $course); // Hidden node that we use to determine if the front page navigation is loaded. // This required as there are not other guaranteed nodes that may be loaded. $coursenode->add('frontpageloaded', null, self::TYPE_CUSTOM, null, 'frontpageloaded')->display = false; // Participants. if ($navoptions->participants) { $coursenode->add(get_string('participants'), new moodle_url('/user/index.php?id=' . $course->id), self::TYPE_CUSTOM, get_string('participants'), 'participants'); } // Blogs. if ($navoptions->blogs) { $blogsurls = new moodle_url('/blog/index.php'); $coursenode->add(get_string('blogssite', 'blog'), $blogsurls->out(), self::TYPE_SYSTEM, null, 'siteblog'); } $filterselect = 0; // Badges. if ($navoptions->badges) { $url = new moodle_url($CFG->wwwroot . '/badges/view.php', array('type' => 1)); $coursenode->add(get_string('sitebadges', 'badges'), $url, navigation_node::TYPE_CUSTOM); } // Notes. if ($navoptions->notes) { $coursenode->add(get_string('notes', 'notes'), new moodle_url('/notes/index.php', array('filtertype' => 'course', 'filterselect' => $filterselect)), self::TYPE_SETTING, null, 'notes'); } // Tags if ($navoptions->tags) { $node = $coursenode->add(get_string('tags', 'tag'), new moodle_url('/tag/search.php'), self::TYPE_SETTING, null, 'tags'); } // Search. if ($navoptions->search) { $node = $coursenode->add(get_string('search', 'search'), new moodle_url('/search/index.php'), self::TYPE_SETTING, null, 'search'); } if ($navoptions->calendar) { // Calendar $calendarurl = new moodle_url('/calendar/view.php', array('view' => 'month')); $coursenode->add(get_string('calendar', 'calendar'), $calendarurl, self::TYPE_CUSTOM, null, 'calendar'); } return true; }