/** * Loads user specific information into the navigation in the appropriate place. * * If no user is provided the current user is assumed. * * @param stdClass $user * @param bool $forceforcontext probably force something to be loaded somewhere (ask SamH if not sure what this means) * @return bool */ protected function load_for_user($user = null, $forceforcontext = false) { global $DB, $CFG, $USER, $SITE; if ($user === null) { // We can't require login here but if the user isn't logged in we don't // want to show anything if (!isloggedin() || isguestuser()) { return false; } $user = $USER; } else { if (!is_object($user)) { // If the user is not an object then get them from the database $select = context_helper::get_preload_record_columns_sql('ctx'); $sql = "SELECT u.*, {$select}\n FROM {user} u\n JOIN {context} ctx ON u.id = ctx.instanceid\n WHERE u.id = :userid AND\n ctx.contextlevel = :contextlevel"; $user = $DB->get_record_sql($sql, array('userid' => (int) $user, 'contextlevel' => CONTEXT_USER), MUST_EXIST); context_helper::preload_from_record($user); } } $iscurrentuser = $user->id == $USER->id; $usercontext = get_context_instance(CONTEXT_USER, $user->id); // Get the course set against the page, by default this will be the site $course = $this->page->course; $baseargs = array('id' => $user->id); if ($course->id != $SITE->id && (!$iscurrentuser || $forceforcontext)) { $coursenode = $this->load_course($course); $baseargs['course'] = $course->id; $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); $issitecourse = false; } else { // Load all categories and get the context for the system $coursecontext = get_context_instance(CONTEXT_SYSTEM); $issitecourse = true; } // Create a node to add user information under. if ($iscurrentuser && !$forceforcontext) { // If it's the current user the information will go under the profile root node $usernode = $this->rootnodes['myprofile']; $course = get_site(); $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); $issitecourse = true; } else { if (!$issitecourse) { // Not the current user so add it to the participants node for the current course $usersnode = $coursenode->get('participants', navigation_node::TYPE_CONTAINER); $userviewurl = new moodle_url('/user/view.php', $baseargs); } else { // This is the site so add a users node to the root branch $usersnode = $this->rootnodes['users']; if (has_capability('moodle/course:viewparticipants', $coursecontext)) { $usersnode->action = new moodle_url('/user/index.php', array('id' => $course->id)); } $userviewurl = new moodle_url('/user/profile.php', $baseargs); } if (!$usersnode) { // We should NEVER get here, if the course hasn't been populated // with a participants node then the navigaiton either wasn't generated // for it (you are missing a require_login or set_context call) or // you don't have access.... in the interests of no leaking informatin // we simply quit... return false; } // Add a branch for the current user $canseefullname = has_capability('moodle/site:viewfullnames', $coursecontext); $usernode = $usersnode->add(fullname($user, $canseefullname), $userviewurl, self::TYPE_USER, null, $user->id); if ($this->page->context->contextlevel == CONTEXT_USER && $user->id == $this->page->context->instanceid) { $usernode->make_active(); } } // If the user is the current user or has permission to view the details of the requested // user than add a view profile link. if ($iscurrentuser || has_capability('moodle/user:viewdetails', $coursecontext) || has_capability('moodle/user:viewdetails', $usercontext)) { if ($issitecourse || $iscurrentuser && !$forceforcontext) { $usernode->add(get_string('viewprofile'), new moodle_url('/user/profile.php', $baseargs)); } else { $usernode->add(get_string('viewprofile'), new moodle_url('/user/view.php', $baseargs)); } } if (!empty($CFG->navadduserpostslinks)) { // Add nodes for forum posts and discussions if the user can view either or both // There are no capability checks here as the content of the page is based // purely on the forums the current user has access too. $forumtab = $usernode->add(get_string('forumposts', 'forum')); $forumtab->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php', $baseargs)); $forumtab->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php', array_merge($baseargs, array('mode' => 'discussions')))); } // Add blog nodes if (!empty($CFG->bloglevel)) { if (!$this->cache->cached('userblogoptions' . $user->id)) { require_once $CFG->dirroot . '/blog/lib.php'; // Get all options for the user $options = blog_get_options_for_user($user); $this->cache->set('userblogoptions' . $user->id, $options); } else { $options = $this->cache->{'userblogoptions' . $user->id}; } if (count($options) > 0) { $blogs = $usernode->add(get_string('blogs', 'blog'), null, navigation_node::TYPE_CONTAINER); foreach ($options as $type => $option) { if ($type == "rss") { $blogs->add($option['string'], $option['link'], settings_navigation::TYPE_SETTING, null, null, new pix_icon('i/rss', '')); } else { $blogs->add($option['string'], $option['link']); } } } } if (!empty($CFG->messaging)) { $messageargs = null; if ($USER->id != $user->id) { $messageargs = array('id' => $user->id); } $url = new moodle_url('/message/index.php', $messageargs); $usernode->add(get_string('messages', 'message'), $url, self::TYPE_SETTING, null, 'messages'); } $context = get_context_instance(CONTEXT_USER, $USER->id); if ($iscurrentuser && has_capability('moodle/user:manageownfiles', $context)) { $url = new moodle_url('/user/files.php'); $usernode->add(get_string('myfiles'), $url, self::TYPE_SETTING); } // Add a node to view the users notes if permitted if (!empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $coursecontext)) { $url = new moodle_url('/notes/index.php', array('user' => $user->id)); if ($coursecontext->instanceid) { $url->param('course', $coursecontext->instanceid); } $usernode->add(get_string('notes', 'notes'), $url); } // Add reports node $reporttab = $usernode->add(get_string('activityreports')); $reports = get_plugin_list_with_function('report', 'extend_navigation_user', 'lib.php'); foreach ($reports as $reportfunction) { $reportfunction($reporttab, $user, $course); } $anyreport = has_capability('moodle/user:viewuseractivitiesreport', $usercontext); if ($anyreport || $course->showreports && $iscurrentuser && $forceforcontext) { // Add grade hardcoded grade report if necessary $gradeaccess = false; if (has_capability('moodle/grade:viewall', $coursecontext)) { //ok - can view all course grades $gradeaccess = true; } else { if ($course->showgrades) { if ($iscurrentuser && has_capability('moodle/grade:view', $coursecontext)) { //ok - can view own grades $gradeaccess = true; } else { if (has_capability('moodle/grade:viewall', $usercontext)) { // ok - can view grades of this user - parent most probably $gradeaccess = true; } else { if ($anyreport) { // ok - can view grades of this user - parent most probably $gradeaccess = true; } } } } } if ($gradeaccess) { $reporttab->add(get_string('grade'), new moodle_url('/course/user.php', array('mode' => 'grade', 'id' => $course->id, 'user' => $usercontext->instanceid))); } } // Check the number of nodes in the report node... if there are none remove the node $reporttab->trim_if_empty(); // If the user is the current user add the repositories for the current user $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields)); if ($iscurrentuser) { if (!$this->cache->cached('contexthasrepos' . $usercontext->id)) { require_once $CFG->dirroot . '/repository/lib.php'; $editabletypes = repository::get_editable_types($usercontext); $haseditabletypes = !empty($editabletypes); unset($editabletypes); $this->cache->set('contexthasrepos' . $usercontext->id, $haseditabletypes); } else { $haseditabletypes = $this->cache->{'contexthasrepos' . $usercontext->id}; } if ($haseditabletypes) { $usernode->add(get_string('repositories', 'repository'), new moodle_url('/repository/manage_instances.php', array('contextid' => $usercontext->id))); } } else { if ($course->id == $SITE->id && has_capability('moodle/user:viewdetails', $usercontext) && (!in_array('mycourses', $hiddenfields) || has_capability('moodle/user:viewhiddendetails', $coursecontext))) { // Add view grade report is permitted $reports = get_plugin_list('gradereport'); arsort($reports); // user is last, we want to test it first $userscourses = enrol_get_users_courses($user->id); $userscoursesnode = $usernode->add(get_string('courses')); foreach ($userscourses as $usercourse) { $usercoursecontext = get_context_instance(CONTEXT_COURSE, $usercourse->id); $usercourseshortname = format_string($usercourse->shortname, true, array('context' => $usercoursecontext)); $usercoursenode = $userscoursesnode->add($usercourseshortname, new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $usercourse->id)), self::TYPE_CONTAINER); $gradeavailable = has_capability('moodle/grade:viewall', $usercoursecontext); if (!$gradeavailable && !empty($usercourse->showgrades) && is_array($reports) && !empty($reports)) { foreach ($reports as $plugin => $plugindir) { if (has_capability('gradereport/' . $plugin . ':view', $usercoursecontext)) { //stop when the first visible plugin is found $gradeavailable = true; break; } } } if ($gradeavailable) { $url = new moodle_url('/grade/report/index.php', array('id' => $usercourse->id)); $usercoursenode->add(get_string('grades'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/grades', '')); } // Add a node to view the users notes if permitted if (!empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $usercoursecontext)) { $url = new moodle_url('/notes/index.php', array('user' => $user->id, 'course' => $usercourse->id)); $usercoursenode->add(get_string('notes', 'notes'), $url, self::TYPE_SETTING); } if (can_access_course($usercourse, $user->id)) { $usercoursenode->add(get_string('entercourse'), new moodle_url('/course/view.php', array('id' => $usercourse->id)), self::TYPE_SETTING, null, null, new pix_icon('i/course', '')); } $reporttab = $usercoursenode->add(get_string('activityreports')); $reports = get_plugin_list_with_function('report', 'extend_navigation_user', 'lib.php'); foreach ($reports as $reportfunction) { $reportfunction($reporttab, $user, $usercourse); } $reporttab->trim_if_empty(); } } } return true; }
/** * This function gets all of the options available for the current user in respect * to blogs. * * It loads the following if applicable: * - Module options {@see blog_get_options_for_module} * - Course options {@see blog_get_options_for_course} * - User specific options {@see blog_get_options_for_user} * - General options (BLOG_LEVEL_GLOBAL) * * @param moodle_page $page The page to load for (normally $PAGE) * @param stdClass $userid Load for a specific user * @return array An array of options organised by type. */ function blog_get_all_options(moodle_page $page, stdClass $userid = null) { global $CFG, $DB, $USER; $options = array(); // If blogs are enabled and the user is logged in and not a guest if (blog_is_enabled_for_user()) { // If the context is the user then assume we want to load for the users context if (is_null($userid) && $page->context->contextlevel == CONTEXT_USER) { $userid = $page->context->instanceid; } // Check the userid var if (!is_null($userid) && $userid !== $USER->id) { // Load the user from the userid... it MUST EXIST throw a wobbly if it doesn't! $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST); } else { $user = null; } if ($CFG->useblogassociations && $page->cm !== null) { // Load for the module associated with the page $options[CONTEXT_MODULE] = blog_get_options_for_module($page->cm, $user); } else { if ($CFG->useblogassociations && $page->course->id != SITEID) { // Load the options for the course associated with the page $options[CONTEXT_COURSE] = blog_get_options_for_course($page->course, $user); } } // Get the options for the user if ($user !== null and !isguestuser($user)) { // Load for the requested user $options[CONTEXT_USER + 1] = blog_get_options_for_user($user); } // Load for the current user if (isloggedin() and !isguestuser()) { $options[CONTEXT_USER] = blog_get_options_for_user(); } } // If blog level is global then display a link to view all site entries if (!empty($CFG->bloglevel) && $CFG->bloglevel >= BLOG_GLOBAL_LEVEL && has_capability('moodle/blog:view', get_context_instance(CONTEXT_SYSTEM))) { $options[CONTEXT_SYSTEM] = array('viewsite' => array('string' => get_string('viewsiteentries', 'blog'), 'link' => new moodle_url('/blog/index.php'))); } // Return the options return $options; }
/** * This function gets called by {@link settings_navigation::load_user_settings()} and actually works out * what can be shown/done * * @param int $courseid The current course' id * @param int $userid The user id to load for * @param string $gstitle The string to pass to get_string for the branch title * @return navigation_node|false */ protected function generate_user_settings($courseid, $userid, $gstitle = 'usercurrentsettings') { global $DB, $CFG, $USER, $SITE; if ($courseid != $SITE->id) { if (!empty($this->page->course->id) && $this->page->course->id == $courseid) { $course = $this->page->course; } else { $select = context_helper::get_preload_record_columns_sql('ctx'); $sql = "SELECT c.*, {$select}\n FROM {course} c\n JOIN {context} ctx ON c.id = ctx.instanceid\n WHERE c.id = :courseid AND ctx.contextlevel = :contextlevel"; $params = array('courseid' => $courseid, 'contextlevel' => CONTEXT_COURSE); $course = $DB->get_record_sql($sql, $params, MUST_EXIST); context_helper::preload_from_record($course); } } else { $course = $SITE; } $coursecontext = context_course::instance($course->id); // Course context $systemcontext = context_system::instance(); $currentuser = $USER->id == $userid; if ($currentuser) { $user = $USER; $usercontext = context_user::instance($user->id); // User context } else { $select = context_helper::get_preload_record_columns_sql('ctx'); $sql = "SELECT u.*, {$select}\n FROM {user} u\n JOIN {context} ctx ON u.id = ctx.instanceid\n WHERE u.id = :userid AND ctx.contextlevel = :contextlevel"; $params = array('userid' => $userid, 'contextlevel' => CONTEXT_USER); $user = $DB->get_record_sql($sql, $params, IGNORE_MISSING); if (!$user) { return false; } context_helper::preload_from_record($user); // Check that the user can view the profile $usercontext = context_user::instance($user->id); // User context $canviewuser = has_capability('moodle/user:viewdetails', $usercontext); if ($course->id == $SITE->id) { if ($CFG->forceloginforprofiles && !has_coursecontact_role($user->id) && !$canviewuser) { // Reduce possibility of "browsing" userbase at site level // Teachers can browse and be browsed at site level. If not forceloginforprofiles, allow access (bug #4366) return false; } } else { $canviewusercourse = has_capability('moodle/user:viewdetails', $coursecontext); $userisenrolled = is_enrolled($coursecontext, $user->id, '', true); if (!$canviewusercourse && !$canviewuser || !$userisenrolled) { return false; } $canaccessallgroups = has_capability('moodle/site:accessallgroups', $coursecontext); if (!$canaccessallgroups && groups_get_course_groupmode($course) == SEPARATEGROUPS && !$canviewuser) { // If groups are in use, make sure we can see that group (MDL-45874). That does not apply to parents. if ($courseid == $this->page->course->id) { $mygroups = get_fast_modinfo($this->page->course)->groups; } else { $mygroups = groups_get_user_groups($courseid); } $usergroups = groups_get_user_groups($courseid, $userid); if (!array_intersect_key($mygroups[0], $usergroups[0])) { return false; } } } } $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $this->page->context)); $key = $gstitle; $prefurl = new moodle_url('/user/preferences.php'); if ($gstitle != 'usercurrentsettings') { $key .= $userid; $prefurl->param('userid', $userid); } // Add a user setting branch. if ($gstitle == 'usercurrentsettings') { $dashboard = $this->add(get_string('myhome'), new moodle_url('/my/'), self::TYPE_CONTAINER, null, 'dashboard'); // This should be set to false as we don't want to show this to the user. It's only for generating the correct // breadcrumb. $dashboard->display = false; if (get_home_page() == HOMEPAGE_MY) { $dashboard->mainnavonly = true; } $iscurrentuser = $user->id == $USER->id; $baseargs = array('id' => $user->id); if ($course->id != $SITE->id && !$iscurrentuser) { $baseargs['course'] = $course->id; $issitecourse = false; } else { // Load all categories and get the context for the system. $issitecourse = true; } // Add the user profile to the dashboard. $profilenode = $dashboard->add(get_string('profile'), new moodle_url('/user/profile.php', array('id' => $user->id)), self::TYPE_SETTING, null, 'myprofile'); if (!empty($CFG->navadduserpostslinks)) { // Add nodes for forum posts and discussions if the user can view either or both // There are no capability checks here as the content of the page is based // purely on the forums the current user has access too. $forumtab = $profilenode->add(get_string('forumposts', 'forum')); $forumtab->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php', $baseargs), null, 'myposts'); $forumtab->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php', array_merge($baseargs, array('mode' => 'discussions'))), null, 'mydiscussions'); } // Add blog nodes. if (!empty($CFG->enableblogs)) { if (!$this->cache->cached('userblogoptions' . $user->id)) { require_once $CFG->dirroot . '/blog/lib.php'; // Get all options for the user. $options = blog_get_options_for_user($user); $this->cache->set('userblogoptions' . $user->id, $options); } else { $options = $this->cache->{'userblogoptions' . $user->id}; } if (count($options) > 0) { $blogs = $profilenode->add(get_string('blogs', 'blog'), null, navigation_node::TYPE_CONTAINER); foreach ($options as $type => $option) { if ($type == "rss") { $blogs->add($option['string'], $option['link'], self::TYPE_SETTING, null, null, new pix_icon('i/rss', '')); } else { $blogs->add($option['string'], $option['link'], self::TYPE_SETTING, null, 'blog' . $type); } } } } // Add the messages link. // It is context based so can appear in the user's profile and in course participants information. if (!empty($CFG->messaging)) { $messageargs = array('user1' => $USER->id); if ($USER->id != $user->id) { $messageargs['user2'] = $user->id; } if ($course->id != $SITE->id) { $messageargs['viewing'] = MESSAGE_VIEW_COURSE . $course->id; } $url = new moodle_url('/message/index.php', $messageargs); $dashboard->add(get_string('messages', 'message'), $url, self::TYPE_SETTING, null, 'messages'); } // Add the "My private files" link. // This link doesn't have a unique display for course context so only display it under the user's profile. if ($issitecourse && $iscurrentuser && has_capability('moodle/user:manageownfiles', $usercontext)) { $url = new moodle_url('/user/files.php'); $dashboard->add(get_string('privatefiles'), $url, self::TYPE_SETTING); } // Add a node to view the users notes if permitted. if (!empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $coursecontext)) { $url = new moodle_url('/notes/index.php', array('user' => $user->id)); if ($coursecontext->instanceid != SITEID) { $url->param('course', $coursecontext->instanceid); } $profilenode->add(get_string('notes', 'notes'), $url); } // Show the grades node. if ($issitecourse && $iscurrentuser || has_capability('moodle/user:viewdetails', $usercontext)) { require_once $CFG->dirroot . '/user/lib.php'; // Set the grades node to link to the "Grades" page. if ($course->id == SITEID) { $url = user_mygrades_url($user->id, $course->id); } else { // Otherwise we are in a course and should redirect to the user grade report (Activity report version). $url = new moodle_url('/course/user.php', array('mode' => 'grade', 'id' => $course->id, 'user' => $user->id)); } $dashboard->add(get_string('grades', 'grades'), $url, self::TYPE_SETTING, null, 'mygrades'); } // Let plugins hook into user navigation. $pluginsfunction = get_plugins_with_function('extend_navigation_user', 'lib.php'); foreach ($pluginsfunction as $plugintype => $plugins) { if ($plugintype != 'report') { foreach ($plugins as $pluginfunction) { $pluginfunction($profilenode, $user, $usercontext, $course, $coursecontext); } } } $usersetting = navigation_node::create(get_string('preferences', 'moodle'), $prefurl, self::TYPE_CONTAINER, null, $key); $dashboard->add_node($usersetting); } else { $usersetting = $this->add(get_string('preferences', 'moodle'), $prefurl, self::TYPE_CONTAINER, null, $key); $usersetting->display = false; } $usersetting->id = 'usersettings'; // Check if the user has been deleted. if ($user->deleted) { if (!has_capability('moodle/user:update', $coursecontext)) { // We can't edit the user so just show the user deleted message. $usersetting->add(get_string('userdeleted'), null, self::TYPE_SETTING); } else { // We can edit the user so show the user deleted message and link it to the profile. if ($course->id == $SITE->id) { $profileurl = new moodle_url('/user/profile.php', array('id' => $user->id)); } else { $profileurl = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id)); } $usersetting->add(get_string('userdeleted'), $profileurl, self::TYPE_SETTING); } return true; } $userauthplugin = false; if (!empty($user->auth)) { $userauthplugin = get_auth_plugin($user->auth); } $useraccount = $usersetting->add(get_string('useraccount'), null, self::TYPE_CONTAINER, null, 'useraccount'); // Add the profile edit link. if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) { if (($currentuser || is_siteadmin($USER) || !is_siteadmin($user)) && has_capability('moodle/user:update', $systemcontext)) { $url = new moodle_url('/user/editadvanced.php', array('id' => $user->id, 'course' => $course->id)); $useraccount->add(get_string('editmyprofile'), $url, self::TYPE_SETTING); } else { if (has_capability('moodle/user:editprofile', $usercontext) && !is_siteadmin($user) || $currentuser && has_capability('moodle/user:editownprofile', $systemcontext)) { if ($userauthplugin && $userauthplugin->can_edit_profile()) { $url = $userauthplugin->edit_profile_url(); if (empty($url)) { $url = new moodle_url('/user/edit.php', array('id' => $user->id, 'course' => $course->id)); } $useraccount->add(get_string('editmyprofile'), $url, self::TYPE_SETTING); } } } } // Change password link. if ($userauthplugin && $currentuser && !\core\session\manager::is_loggedinas() && !isguestuser() && has_capability('moodle/user:changeownpassword', $systemcontext) && $userauthplugin->can_change_password()) { $passwordchangeurl = $userauthplugin->change_password_url(); if (empty($passwordchangeurl)) { $passwordchangeurl = new moodle_url('/login/change_password.php', array('id' => $course->id)); } $useraccount->add(get_string("changepassword"), $passwordchangeurl, self::TYPE_SETTING, null, 'changepassword'); } if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) { if ($currentuser && has_capability('moodle/user:editownprofile', $systemcontext) || has_capability('moodle/user:editprofile', $usercontext)) { $url = new moodle_url('/user/language.php', array('id' => $user->id, 'course' => $course->id)); $useraccount->add(get_string('preferredlanguage'), $url, self::TYPE_SETTING, null, 'preferredlanguage'); } } $pluginmanager = core_plugin_manager::instance(); $enabled = $pluginmanager->get_enabled_plugins('mod'); if (isset($enabled['forum']) && isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) { if ($currentuser && has_capability('moodle/user:editownprofile', $systemcontext) || has_capability('moodle/user:editprofile', $usercontext)) { $url = new moodle_url('/user/forum.php', array('id' => $user->id, 'course' => $course->id)); $useraccount->add(get_string('forumpreferences'), $url, self::TYPE_SETTING); } } $editors = editors_get_enabled(); if (count($editors) > 1) { if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) { if ($currentuser && has_capability('moodle/user:editownprofile', $systemcontext) || has_capability('moodle/user:editprofile', $usercontext)) { $url = new moodle_url('/user/editor.php', array('id' => $user->id, 'course' => $course->id)); $useraccount->add(get_string('editorpreferences'), $url, self::TYPE_SETTING); } } } // Add "Course preferences" link. if (isloggedin() && !isguestuser($user)) { if ($currentuser && has_capability('moodle/user:editownprofile', $systemcontext) || has_capability('moodle/user:editprofile', $usercontext)) { $url = new moodle_url('/user/course.php', array('id' => $user->id, 'course' => $course->id)); $useraccount->add(get_string('coursepreferences'), $url, self::TYPE_SETTING, null, 'coursepreferences'); } } // View the roles settings. if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride', 'moodle/role:override', 'moodle/role:manage'), $usercontext)) { $roles = $usersetting->add(get_string('roles'), null, self::TYPE_SETTING); $url = new moodle_url('/admin/roles/usersroles.php', array('userid' => $user->id, 'courseid' => $course->id)); $roles->add(get_string('thisusersroles', 'role'), $url, self::TYPE_SETTING); $assignableroles = get_assignable_roles($usercontext, ROLENAME_BOTH); if (!empty($assignableroles)) { $url = new moodle_url('/admin/roles/assign.php', array('contextid' => $usercontext->id, 'userid' => $user->id, 'courseid' => $course->id)); $roles->add(get_string('assignrolesrelativetothisuser', 'role'), $url, self::TYPE_SETTING); } if (has_capability('moodle/role:review', $usercontext) || count(get_overridable_roles($usercontext, ROLENAME_BOTH)) > 0) { $url = new moodle_url('/admin/roles/permissions.php', array('contextid' => $usercontext->id, 'userid' => $user->id, 'courseid' => $course->id)); $roles->add(get_string('permissions', 'role'), $url, self::TYPE_SETTING); } $url = new moodle_url('/admin/roles/check.php', array('contextid' => $usercontext->id, 'userid' => $user->id, 'courseid' => $course->id)); $roles->add(get_string('checkpermissions', 'role'), $url, self::TYPE_SETTING); } // Repositories. if (!$this->cache->cached('contexthasrepos' . $usercontext->id)) { require_once $CFG->dirroot . '/repository/lib.php'; $editabletypes = repository::get_editable_types($usercontext); $haseditabletypes = !empty($editabletypes); unset($editabletypes); $this->cache->set('contexthasrepos' . $usercontext->id, $haseditabletypes); } else { $haseditabletypes = $this->cache->{'contexthasrepos' . $usercontext->id}; } if ($haseditabletypes) { $repositories = $usersetting->add(get_string('repositories', 'repository'), null, self::TYPE_SETTING); $repositories->add(get_string('manageinstances', 'repository'), new moodle_url('/repository/manage_instances.php', array('contextid' => $usercontext->id))); } // Portfolio. if ($currentuser && !empty($CFG->enableportfolios) && has_capability('moodle/portfolio:export', $systemcontext)) { require_once $CFG->libdir . '/portfoliolib.php'; if (portfolio_has_visible_instances()) { $portfolio = $usersetting->add(get_string('portfolios', 'portfolio'), null, self::TYPE_SETTING); $url = new moodle_url('/user/portfolio.php', array('courseid' => $course->id)); $portfolio->add(get_string('configure', 'portfolio'), $url, self::TYPE_SETTING); $url = new moodle_url('/user/portfoliologs.php', array('courseid' => $course->id)); $portfolio->add(get_string('logs', 'portfolio'), $url, self::TYPE_SETTING); } } $enablemanagetokens = false; if (!empty($CFG->enablerssfeeds)) { $enablemanagetokens = true; } else { if (!is_siteadmin($USER->id) && !empty($CFG->enablewebservices) && has_capability('moodle/webservice:createtoken', context_system::instance())) { $enablemanagetokens = true; } } // Security keys. if ($currentuser && $enablemanagetokens) { $url = new moodle_url('/user/managetoken.php', array('sesskey' => sesskey())); $useraccount->add(get_string('securitykeys', 'webservice'), $url, self::TYPE_SETTING); } // Messaging. if ($currentuser && has_capability('moodle/user:editownmessageprofile', $systemcontext) || !isguestuser($user) && has_capability('moodle/user:editmessageprofile', $usercontext) && !is_primary_admin($user->id)) { $url = new moodle_url('/message/edit.php', array('id' => $user->id)); $useraccount->add(get_string('messaging', 'message'), $url, self::TYPE_SETTING); } // Blogs. if ($currentuser && !empty($CFG->enableblogs)) { $blog = $usersetting->add(get_string('blogs', 'blog'), null, navigation_node::TYPE_CONTAINER, null, 'blogs'); if (has_capability('moodle/blog:view', $systemcontext)) { $blog->add(get_string('preferences', 'blog'), new moodle_url('/blog/preferences.php'), navigation_node::TYPE_SETTING); } if (!empty($CFG->useexternalblogs) && $CFG->maxexternalblogsperuser > 0 && has_capability('moodle/blog:manageexternal', $systemcontext)) { $blog->add(get_string('externalblogs', 'blog'), new moodle_url('/blog/external_blogs.php'), navigation_node::TYPE_SETTING); $blog->add(get_string('addnewexternalblog', 'blog'), new moodle_url('/blog/external_blog_edit.php'), navigation_node::TYPE_SETTING); } // Remove the blog node if empty. $blog->trim_if_empty(); } // Badges. if ($currentuser && !empty($CFG->enablebadges)) { $badges = $usersetting->add(get_string('badges'), null, navigation_node::TYPE_CONTAINER, null, 'badges'); if (has_capability('moodle/badges:manageownbadges', $usercontext)) { $url = new moodle_url('/badges/mybadges.php'); $badges->add(get_string('managebadges', 'badges'), $url, self::TYPE_SETTING); } $badges->add(get_string('preferences', 'badges'), new moodle_url('/badges/preferences.php'), navigation_node::TYPE_SETTING); if (!empty($CFG->badges_allowexternalbackpack)) { $badges->add(get_string('backpackdetails', 'badges'), new moodle_url('/badges/mybackpack.php'), navigation_node::TYPE_SETTING); } } // Let plugins hook into user settings navigation. $pluginsfunction = get_plugins_with_function('extend_navigation_user_settings', 'lib.php'); foreach ($pluginsfunction as $plugintype => $plugins) { foreach ($plugins as $pluginfunction) { $pluginfunction($usersetting, $user, $usercontext, $course, $coursecontext); } } return $usersetting; }
/** * Loads user specific information into the navigation in the appopriate place. * * If no user is provided the current user is assumed. * * @param stdClass $user * @return bool */ protected function load_for_user($user=null, $forceforcontext=false) { global $DB, $CFG, $USER; if ($user === null) { // We can't require login here but if the user isn't logged in we don't // want to show anything if (!isloggedin() || isguestuser()) { return false; } $user = $USER; } else if (!is_object($user)) { // If the user is not an object then get them from the database $user = $DB->get_record('user', array('id'=>(int)$user), '*', MUST_EXIST); } $iscurrentuser = ($user->id == $USER->id); $usercontext = get_context_instance(CONTEXT_USER, $user->id); // Get the course set against the page, by default this will be the site $course = $this->page->course; $baseargs = array('id'=>$user->id); if ($course->id != SITEID && (!$iscurrentuser || $forceforcontext)) { if (array_key_exists($course->id, $this->mycourses)) { $coursenode = $this->mycourses[$course->id]->coursenode; } else { $coursenode = $this->rootnodes['courses']->find($course->id, navigation_node::TYPE_COURSE); if (!$coursenode) { $coursenode = $this->load_course($course); } } $baseargs['course'] = $course->id; $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); $issitecourse = false; } else { // Load all categories and get the context for the system $coursecontext = get_context_instance(CONTEXT_SYSTEM); $issitecourse = true; } // Create a node to add user information under. if ($iscurrentuser && !$forceforcontext) { // If it's the current user the information will go under the profile root node $usernode = $this->rootnodes['myprofile']; } else { if (!$issitecourse) { // Not the current user so add it to the participants node for the current course $usersnode = $coursenode->get('participants', navigation_node::TYPE_CONTAINER); $userviewurl = new moodle_url('/user/view.php', $baseargs); } else { // This is the site so add a users node to the root branch $usersnode = $this->rootnodes['users']; $usersnode->action = new moodle_url('/user/index.php', array('id'=>$course->id)); $userviewurl = new moodle_url('/user/profile.php', $baseargs); } if (!$usersnode) { // We should NEVER get here, if the course hasn't been populated // with a participants node then the navigaiton either wasn't generated // for it (you are missing a require_login or set_context call) or // you don't have access.... in the interests of no leaking informatin // we simply quit... return false; } // Add a branch for the current user $usernode = $usersnode->add(fullname($user, true), $userviewurl, self::TYPE_USER, null, $user->id); if ($this->page->context->contextlevel == CONTEXT_USER && $user->id == $this->page->context->instanceid) { $usernode->make_active(); } } // If the user is the current user or has permission to view the details of the requested // user than add a view profile link. if ($iscurrentuser || has_capability('moodle/user:viewdetails', $coursecontext) || has_capability('moodle/user:viewdetails', $usercontext)) { if ($issitecourse || ($iscurrentuser && !$forceforcontext)) { $usernode->add(get_string('viewprofile'), new moodle_url('/user/profile.php',$baseargs)); } else { $usernode->add(get_string('viewprofile'), new moodle_url('/user/view.php',$baseargs)); } } // Add nodes for forum posts and discussions if the user can view either or both // There are no capability checks here as the content of the page is based // purely on the forums the current user has access too. $forumtab = $usernode->add(get_string('forumposts', 'forum')); $forumtab->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php', $baseargs)); $forumtab->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php', array_merge($baseargs, array('mode'=>'discussions')))); // Add blog nodes if (!empty($CFG->bloglevel)) { require_once($CFG->dirroot.'/blog/lib.php'); // Get all options for the user $options = blog_get_options_for_user($user); if (count($options) > 0) { $blogs = $usernode->add(get_string('blogs', 'blog'), null, navigation_node::TYPE_CONTAINER); foreach ($options as $option) { $blogs->add($option['string'], $option['link']); } } } if (!empty($CFG->messaging)) { $messageargs = null; if ($USER->id!=$user->id) { $messageargs = array('id'=>$user->id); } $url = new moodle_url('/message/index.php',$messageargs); $usernode->add(get_string('messages', 'message'), $url, self::TYPE_SETTING, null, 'messages'); } // TODO: Private file capability check if ($iscurrentuser) { $url = new moodle_url('/user/files.php'); $usernode->add(get_string('myfiles'), $url, self::TYPE_SETTING); } // Add a node to view the users notes if permitted if (!empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $coursecontext)) { $url = new moodle_url('/notes/index.php',array('user'=>$user->id)); if ($coursecontext->instanceid) { $url->param('course', $coursecontext->instanceid); } $usernode->add(get_string('notes', 'notes'), $url); } // Add a reports tab and then add reports the the user has permission to see. $anyreport = has_capability('moodle/user:viewuseractivitiesreport', $usercontext); $viewreports = ($anyreport || ($course->showreports && $iscurrentuser && $forceforcontext)); if ($viewreports) { $reporttab = $usernode->add(get_string('activityreports')); $reportargs = array('user'=>$user->id); if (!empty($course->id)) { $reportargs['id'] = $course->id; } else { $reportargs['id'] = SITEID; } if ($viewreports || has_capability('coursereport/outline:view', $coursecontext)) { $reporttab->add(get_string('outlinereport'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'outline')))); $reporttab->add(get_string('completereport'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'complete')))); } if ($viewreports || has_capability('coursereport/log:viewtoday', $coursecontext)) { $reporttab->add(get_string('todaylogs'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'todaylogs')))); } if ($viewreports || has_capability('coursereport/log:view', $coursecontext)) { $reporttab->add(get_string('alllogs'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'alllogs')))); } if (!empty($CFG->enablestats)) { if ($viewreports || has_capability('coursereport/stats:view', $coursecontext)) { $reporttab->add(get_string('stats'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'stats')))); } } $gradeaccess = false; if (has_capability('moodle/grade:viewall', $coursecontext)) { //ok - can view all course grades $gradeaccess = true; } else if ($course->showgrades) { if ($iscurrentuser && has_capability('moodle/grade:view', $coursecontext)) { //ok - can view own grades $gradeaccess = true; } else if (has_capability('moodle/grade:viewall', $usercontext)) { // ok - can view grades of this user - parent most probably $gradeaccess = true; } else if ($anyreport) { // ok - can view grades of this user - parent most probably $gradeaccess = true; } } if ($gradeaccess) { $reporttab->add(get_string('grade'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'grade')))); } // Check the number of nodes in the report node... if there are none remove // the node if (count($reporttab->children)===0) { $usernode->remove_child($reporttab); } } // If the user is the current user add the repositories for the current user $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields)); if ($iscurrentuser) { require_once($CFG->dirroot . '/repository/lib.php'); $editabletypes = repository::get_editable_types($usercontext); if (!empty($editabletypes)) { $usernode->add(get_string('repositories', 'repository'), new moodle_url('/repository/manage_instances.php', array('contextid' => $usercontext->id))); } } else if ($course->id == SITEID && has_capability('moodle/user:viewdetails', $usercontext) && (!in_array('mycourses', $hiddenfields) || has_capability('moodle/user:viewhiddendetails', $coursecontext))) { // Add view grade report is permitted $reports = get_plugin_list('gradereport'); arsort($reports); // user is last, we want to test it first $userscourses = enrol_get_users_courses($user->id); $userscoursesnode = $usernode->add(get_string('courses')); foreach ($userscourses as $usercourse) { $usercoursecontext = get_context_instance(CONTEXT_COURSE, $usercourse->id); $usercoursenode = $userscoursesnode->add($usercourse->shortname, new moodle_url('/user/view.php', array('id'=>$user->id, 'course'=>$usercourse->id)), self::TYPE_CONTAINER); $gradeavailable = has_capability('moodle/grade:viewall', $usercoursecontext); if (!$gradeavailable && !empty($usercourse->showgrades) && is_array($reports) && !empty($reports)) { foreach ($reports as $plugin => $plugindir) { if (has_capability('gradereport/'.$plugin.':view', $usercoursecontext)) { //stop when the first visible plugin is found $gradeavailable = true; break; } } } if ($gradeavailable) { $url = new moodle_url('/grade/report/index.php', array('id'=>$usercourse->id)); $usercoursenode->add(get_string('grades'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/grades', '')); } // Add a node to view the users notes if permitted if (!empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $usercoursecontext)) { $url = new moodle_url('/notes/index.php',array('user'=>$user->id, 'course'=>$usercourse->id)); $usercoursenode->add(get_string('notes', 'notes'), $url, self::TYPE_SETTING); } if (can_access_course(get_context_instance(CONTEXT_COURSE, $usercourse->id), $user->id)) { $usercoursenode->add(get_string('entercourse'), new moodle_url('/course/view.php', array('id'=>$usercourse->id)), self::TYPE_SETTING, null, null, new pix_icon('i/course', '')); } $outlinetreport = ($anyreport || has_capability('coursereport/outline:view', $usercoursecontext)); $logtodayreport = ($anyreport || has_capability('coursereport/log:viewtoday', $usercoursecontext)); $logreport = ($anyreport || has_capability('coursereport/log:view', $usercoursecontext)); $statsreport = ($anyreport || has_capability('coursereport/stats:view', $usercoursecontext)); if ($outlinetreport || $logtodayreport || $logreport || $statsreport) { $reporttab = $usercoursenode->add(get_string('activityreports')); $reportargs = array('user'=>$user->id, 'id'=>$usercourse->id); if ($outlinetreport) { $reporttab->add(get_string('outlinereport'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'outline')))); $reporttab->add(get_string('completereport'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'complete')))); } if ($logtodayreport) { $reporttab->add(get_string('todaylogs'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'todaylogs')))); } if ($logreport) { $reporttab->add(get_string('alllogs'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'alllogs')))); } if (!empty($CFG->enablestats) && $statsreport) { $reporttab->add(get_string('stats'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'stats')))); } } } } return true; }