public function test_node_remove() { $this->setup_node(); $remove1 = $this->node->add('child to remove 1', null, navigation_node::TYPE_CUSTOM, null, 'remove1'); $remove2 = $this->node->add('child to remove 2', null, navigation_node::TYPE_CUSTOM, null, 'remove2'); $remove3 = $remove2->add('child to remove 3', null, navigation_node::TYPE_CUSTOM, null, 'remove3'); $this->assertInstanceOf('navigation_node', $remove1); $this->assertInstanceOf('navigation_node', $remove2); $this->assertInstanceOf('navigation_node', $remove3); $this->assertInstanceOf('navigation_node', $this->node->get('remove1')); $this->assertInstanceOf('navigation_node', $this->node->get('remove2')); $this->assertInstanceOf('navigation_node', $remove2->get('remove3')); // Remove element and make sure this is no longer a child. $this->assertTrue($remove1->remove()); $this->assertFalse($this->node->get('remove1')); $this->assertFalse(in_array('remove1', $this->node->get_children_key_list(), true)); // Make sure that we can insert element after removal. $insertelement = navigation_node::create('extra element 4', null, navigation_node::TYPE_CUSTOM, null, 'element4'); $this->node->add_node($insertelement, 'remove2'); $this->assertNotEmpty($this->node->get('element4')); // Remove more elements. $this->assertTrue($this->node->get('remove2')->remove()); $this->assertFalse($this->node->get('remove2')); // Make sure that we can add element after removal. $this->node->add('extra element 5', null, navigation_node::TYPE_CUSTOM, null, 'element5'); $this->assertNotEmpty($this->node->get('element5')); $this->assertTrue($remove2->get('remove3')->remove()); $this->assertFalse($this->node->get('remove1')); $this->assertFalse($this->node->get('remove2')); }
/** * This function extends the navigation with the tool items * * @param navigation_node $navigation The navigation node to extend * @param stdClass $course The course to object for the tool * @param context $context The context of the course */ function tool_monitor_extend_navigation_frontpage($navigation, $course, $context) { if (has_capability('tool/monitor:managerules', $context)) { $url = new moodle_url('/admin/tool/monitor/managerules.php', array('courseid' => $course->id)); $settingsnode = navigation_node::create(get_string('managerules', 'tool_monitor'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/settings', '')); $reportnode = $navigation->get('frontpagereports'); if (isset($settingsnode) && !empty($reportnode)) { $reportnode->add_node($settingsnode); } } }
public function test_remove() { $remove1 = $this->node->add('child to remove 1', null, navigation_node::TYPE_CUSTOM, null, 'remove1'); $remove2 = $this->node->add('child to remove 2', null, navigation_node::TYPE_CUSTOM, null, 'remove2'); $remove3 = $remove2->add('child to remove 3', null, navigation_node::TYPE_CUSTOM, null, 'remove3'); $this->assertIsA($remove1, 'navigation_node'); $this->assertIsA($remove2, 'navigation_node'); $this->assertIsA($remove3, 'navigation_node'); $this->assertIsA($this->node->get('remove1'), 'navigation_node'); $this->assertIsA($this->node->get('remove2'), 'navigation_node'); $this->assertIsA($remove2->get('remove3'), 'navigation_node'); $this->assertTrue($remove1->remove()); $this->assertTrue($this->node->get('remove2')->remove()); $this->assertTrue($remove2->get('remove3')->remove()); $this->assertFalse($this->node->get('remove1')); $this->assertFalse($this->node->get('remove2')); }
/** * Change the navigation block and bar only for external users * Force course or activity navigation and modify CSS also * Please note that this function is only called in pages where the navigation block is present * * @global moodle_user $USER * @global moodle_database $DB * @param navigation_node $nav Current navigation object */ function local_ltiprovider_extend_navigation($nav) { global $USER, $PAGE, $SESSION; // Check capabilities for tool providers // TODO Change this for Moodle 2.3: // use extend_navigation_course and extend_navigation_user if ($PAGE->course->id and $PAGE->course->id != SITEID and has_capability('local/ltiprovider:view', $PAGE->context)) { $ltiurl = new moodle_url('/local/ltiprovider/index.php', array('courseid' => $PAGE->course->id)); $coursenode = $nav->find($PAGE->course->id, $nav::TYPE_COURSE); $coursenode->add(get_string('pluginname', 'local_ltiprovider'), $ltiurl, $nav::TYPE_CONTAINER, null, 'ltiprovider' . $PAGE->course->id); } if (isset($USER) and isset($USER->auth) and $USER->auth == 'nologin' and strpos($USER->username, 'ltiprovider') === 0) { // Force course or activity navigation if (isset($SESSION->ltiprovider) and $SESSION->ltiprovider->forcenavigation) { $context = $SESSION->ltiprovider->context; $urltogo = ''; if ($context->contextlevel == CONTEXT_COURSE and $PAGE->course->id != $SESSION->ltiprovider->courseid) { $urltogo = new moodle_url('/course/view.php', array('id' => $SESSION->ltiprovider->courseid)); } else if ($context->contextlevel == CONTEXT_MODULE and $PAGE->context->id != $context->id) { $cm = get_coursemodule_from_id(false, $context->instanceid, 0, false, MUST_EXIST); $urltogo = new moodle_url('/mod/' . $cm->modname . '/view.php', array('id' => $cm->id)); } if ($urltogo) { redirect($urltogo); } } // Delete all the navigation nodes except the course one $coursenode = $nav->find($PAGE->course->id, $nav::TYPE_COURSE); foreach (array('myprofile', 'users', 'site', 'home', 'myhome', 'mycourses', 'courses', '1') as $nodekey) { if ($node = $nav->get($nodekey)) { $node->remove(); } } $nav->children->add($coursenode); // Custom CSS if (isset($SESSION->ltiprovider)) { $PAGE->requires->css(new moodle_url('/local/ltiprovider/styles.php', array('id' => $SESSION->ltiprovider->id))); } } }
/** * Attempts to get the navigation with the given key from this nodes children. * * This function only looks at this nodes children, it does NOT look recursivily. * If the node can't be found then false is returned. * * If you need to search recursivily then use the {@link global_navigation::find()} method. * * Note: If you are trying to set the active node {@link navigation_node::override_active_url()} * may be of more use to you. * * @param string|int $key The key of the node you wish to receive. * @param int $type One of navigation_node::TYPE_* * @return navigation_node|false */ public function get($key, $type = null) { if (!$this->initialised) { $this->initialise(); } return parent::get($key, $type); }
/** * Extends the course administration navigation with the Badges page * * @param navigation_node $coursenode * @param object $course */ function badges_add_course_navigation(navigation_node $coursenode, stdClass $course) { global $CFG, $SITE; $coursecontext = context_course::instance($course->id); $isfrontpage = !$coursecontext || $course->id == $SITE->id; $canmanage = has_any_capability(array('moodle/badges:viewawarded', 'moodle/badges:createbadge', 'moodle/badges:awardbadge', 'moodle/badges:configurecriteria', 'moodle/badges:configuremessages', 'moodle/badges:configuredetails', 'moodle/badges:deletebadge'), $coursecontext); if (!empty($CFG->enablebadges) && !empty($CFG->badges_allowcoursebadges) && !$isfrontpage && $canmanage) { $coursenode->add(get_string('coursebadges', 'badges'), null, navigation_node::TYPE_CONTAINER, null, 'coursebadges', new pix_icon('i/badge', get_string('coursebadges', 'badges'))); $url = new moodle_url('/badges/index.php', array('type' => BADGE_TYPE_COURSE, 'id' => $course->id)); $coursenode->get('coursebadges')->add(get_string('managebadges', 'badges'), $url, navigation_node::TYPE_SETTING, null, 'coursebadges'); if (has_capability('moodle/badges:createbadge', $coursecontext)) { $url = new moodle_url('/badges/newbadge.php', array('type' => BADGE_TYPE_COURSE, 'id' => $course->id)); $coursenode->get('coursebadges')->add(get_string('newbadge', 'badges'), $url, navigation_node::TYPE_SETTING, null, 'newbadge'); } } }
/** * @param navigation_node $contextnode * @param $context context object * @return navigation_node contextnode passed with any modifications made. useful for chaining. */ function get_context_nav(navigation_node $contextnode, $context) { global $COURSE; // Default link for now, change for each node. $url = new moodle_url('/local/contextadmin/index.php', array('contextid' => '1')); // My Categories. if (!$contextnode->get('mycat')) { $contextnode->add(get_string('mycat', 'local_contextadmin'), $url, navigation_node::TYPE_SETTING, null, 'mycat', new pix_icon('i/settings', '')); } // Add custom search page for category admins. $url = new moodle_url('/local/contextadmin/cat_search.php', array()); // Category Search tool (search restricted to categories that the user has access to). if (!$contextnode->get('search')) { $contextnode->add(get_string('search', 'local_contextadmin'), $url, navigation_node::TYPE_SETTING, null, 'search', new pix_icon('i/settings', '')); } if (has_capability('local/contextadmin:renameroles', $context)) { if ($COURSE->category == 0 && $context->contextlevel != CONTEXT_COURSECAT) { // If we are at a system course (site course) we have no category context...so return FALSE. return false; } else { // Course variable has category as 0 but the context is CONTEXT_COURSECAT so we take instanceid. // Lower contexts can use $COURSE->category. if ($context->contextlevel == CONTEXT_COURSECAT) { $catid = $context->instanceid; } else { if ($context->contextlevel > CONTEXT_COURSECAT) { $catid = $COURSE->category; } else { return false; } } } // Add custom edit page for category admins. $url = new moodle_url('/local/contextadmin/categoryroles.php', array('id' => $catid)); // Category Search tool (search restricted to categories that the user has access to). if (!$contextnode->get('roleedit')) { $contextnode->add(get_string('rolerenaming'), $url, navigation_node::TYPE_SETTING, null, 'roleedit', new pix_icon('i/settings', '')); } } if (has_capability('local/contextadmin:changevisibilty', $context) or has_capability('local/contextadmin:editowncatsettings', $context)) { // There is a scenario where the context is at the course level and the parent category is a system context (not Category). // We need to catch it. if ($COURSE->category == 0 && $context->contextlevel != CONTEXT_COURSECAT) { // If we are at a system course (site course) we have no category context...so return FALSE. return false; } else { // Course variable has category as 0 but the context is CONTEXT_COURSECAT so we take instanceid. // Lower contexts can use $COURSE->category. if ($context->contextlevel == CONTEXT_COURSECAT) { $catid = $context->instanceid; } else { if ($context->contextlevel > CONTEXT_COURSECAT) { $catid = $COURSE->category; } else { return false; } } } // Todo Advanced Features. // Todo Courses. // Plugins Node. if (!$contextnode->get('plugins')) { $pluginnode = $contextnode->add(get_string('plugins', 'local_contextadmin'), null, navigation_node::TYPE_SETTING, null, 'plugins', new pix_icon('i/settings', '')); create_plugin_node($pluginnode, $context->id, $catid); } } return $contextnode; }
/** * Change the navigation block and bar only for external users * Force course or activity navigation and modify CSS also * Please note that this function is only called in pages where the navigation block is present * * @global moodle_user $USER * @global moodle_database $DB * @param navigation_node $nav Current navigation object */ function local_ltiprovider_extends_navigation($nav) { global $CFG, $USER, $PAGE, $SESSION, $ME; // Check capabilities for tool providers // Only for Moodle < 2.3 versions if ($CFG->version < 2012062500 and $PAGE->course->id and $PAGE->course->id != SITEID and has_capability('local/ltiprovider:view', $PAGE->context)) { $ltiurl = new moodle_url('/local/ltiprovider/index.php', array('courseid' => $PAGE->course->id)); $coursenode = $nav->find($PAGE->course->id, $nav::TYPE_COURSE); $coursenode->add(get_string('pluginname', 'local_ltiprovider'), $ltiurl, $nav::TYPE_CONTAINER, null, 'ltiprovider' . $PAGE->course->id); } if (isset($USER) and isset($USER->auth) and strpos($USER->username, 'ltiprovider') === 0) { // Force course or activity navigation if (isset($SESSION->ltiprovider) and $SESSION->ltiprovider->forcenavigation) { $context = $SESSION->ltiprovider->context; $urltogo = ''; if ($context->contextlevel == CONTEXT_COURSE and $PAGE->course->id != $SESSION->ltiprovider->courseid) { $urltogo = new moodle_url('/course/view.php', array('id' => $SESSION->ltiprovider->courseid)); } else { if ($context->contextlevel == CONTEXT_MODULE and $PAGE->context->id != $context->id) { $cm = get_coursemodule_from_id(false, $context->instanceid, 0, false, MUST_EXIST); $urltogo = new moodle_url('/mod/' . $cm->modname . '/view.php', array('id' => $cm->id)); } } // Special case, user policy, we don't have to do nothing to avoid infinites loops. if (strpos($ME, 'user/policy.php')) { return; } if ($urltogo) { redirect($urltogo); } } // Delete all the navigation nodes except the course one if ($coursenode = $nav->find($PAGE->course->id, $nav::TYPE_COURSE)) { foreach (array('myprofile', 'users', 'site', 'home', 'myhome', 'mycourses', 'courses', '1') as $nodekey) { if ($node = $nav->get($nodekey)) { $node->remove(); } } $nav->children->add($coursenode); } // Custom CSS if (isset($SESSION->ltiprovider) and !$PAGE->requires->is_head_done()) { $PAGE->requires->css(new moodle_url('/local/ltiprovider/styles.php', array('id' => $SESSION->ltiprovider->id))); } else { $url = new moodle_url('/local/ltiprovider/styles.js.php', array('id' => $SESSION->ltiprovider->id, 'rand' => rand(0, 1000))); $PAGE->requires->js($url); } } }
/** * Loads all of the activities for a section into the navigation structure. * * @param navigation_node $sectionnode * @param int $sectionnumber * @param stdClass $modinfo Object returned from {@see get_fast_modinfo()} * @return array Array of activity nodes */ protected function load_section_activities(navigation_node $sectionnode, $sectionnumber, $modinfo) { if (!array_key_exists($sectionnumber, $modinfo->sections)) { return true; } $activities = array(); foreach ($modinfo->sections[$sectionnumber] as $cmid) { $cm = $modinfo->cms[$cmid]; if (!$cm->uservisible) { continue; } if ($sectionnode->get($cm->id, navigation_node::TYPE_ACTIVITY) !== false) { continue; } if ($cm->icon) { $icon = new pix_icon($cm->icon, get_string('modulename', $cm->modname), $cm->iconcomponent); } else { $icon = new pix_icon('icon', get_string('modulename', $cm->modname), $cm->modname); } $url = new moodle_url('/mod/'.$cm->modname.'/view.php', array('id'=>$cm->id)); $activitynode = $sectionnode->add(format_string($cm->name), $url, navigation_node::TYPE_ACTIVITY, null, $cm->id, $icon); $activitynode->title(get_string('modulename', $cm->modname)); $activitynode->hidden = (!$cm->visible); if ($cm->modname == 'label') { $activitynode->display = false; } else if ($this->module_extends_navigation($cm->modname)) { $activitynode->nodetype = navigation_node::NODETYPE_BRANCH; } $activities[$cmid] = $activitynode; } return $activities; }