MDL-49888 navigation: Added missing reports to user profile.

Added statistics and grade to the reports section in the user
profile.
This commit is contained in:
Adrian Greeve 2015-04-16 15:57:01 +08:00
parent d5d23acdf5
commit 12261113a9
8 changed files with 145 additions and 5 deletions

View File

@ -146,14 +146,19 @@ switch ($mode) {
$url = new moodle_url('/course/user.php', array('id' => $id, 'user' => $user->id, 'mode' => $mode));
$reportnode = $activenode->add(get_string('pluginname', 'gradereport_user'), $url);
} else {
if ($course->id == SITEID) {
$activenode = $PAGE->navigation->find('user' . $user->id, null);
} else {
$currentcoursenode = $PAGE->navigation->find('currentcourse', null);
$activenode = $currentcoursenode->find_active_node();
}
// Check to see if the active node is a user name.
$currentcoursenode = $PAGE->navigation->find('currentcourse', null);
$activenode = $currentcoursenode->find_active_node();
if (strpos($activenode->key, 'user') === false) { // No user name found.
if (!preg_match('/^user\d{0,}$/', $activenode->key)) { // No user name found.
$userurl = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id));
// Add the user name.
$PAGE->navbar->add(fullname($user), $userurl, navigation_node::TYPE_SETTING);
}
$PAGE->navbar->add(get_string('report'));
$gradeurl = new moodle_url('/course/user.php', array('id' => $id, 'user' => $user->id, 'mode' => $mode));
// Add the 'grades' node to the navbar.
$navbar = $PAGE->navbar->add(get_string('grades', 'grades'), $gradeurl, navigation_node::TYPE_SETTING);

View File

@ -1171,4 +1171,47 @@ function grade_report_user_profilereport($course, $user, $viewasuser = false) {
}
}
/**
* Add nodes to myprofile page.
*
* @param \core_user\output\myprofile\tree $tree Tree object
* @param stdClass $user user object
* @param bool $iscurrentuser
* @param stdClass $course Course object
*/
function gradereport_user_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
global $CFG, $USER;
if (empty($course)) {
// We want to display these reports under the site context.
$course = get_fast_modinfo(SITEID)->get_course();
}
$usercontext = context_user::instance($user->id);
$anyreport = has_capability('moodle/user:viewuseractivitiesreport', $usercontext);
// Start capability checks.
if ($anyreport || ($course->showreports && $user->id == $USER->id)) {
// Add grade hardcoded grade report if necessary.
$gradeaccess = false;
$coursecontext = context_course::instance($course->id);
if (has_capability('moodle/grade:viewall', $coursecontext)) {
// Can view all course grades.
$gradeaccess = true;
} else if ($course->showgrades) {
if ($iscurrentuser && has_capability('moodle/grade:view', $coursecontext)) {
// Can view own grades.
$gradeaccess = true;
} else if (has_capability('moodle/grade:viewall', $usercontext)) {
// Can view grades of this user - parent most probably.
$gradeaccess = true;
} else if ($anyreport) {
// Can view grades of this user - parent most probably.
$gradeaccess = true;
}
}
if ($gradeaccess) {
$url = new moodle_url('/course/user.php', array('mode' => 'grade', 'id' => $course->id, 'user' => $user->id));
$node = new core_user\output\myprofile\node('reports', 'grade', get_string('grade'), null, $url);
$tree->add_node($node);
}
}
}

View File

@ -4226,7 +4226,7 @@ class settings_navigation extends navigation_node {
// Add the user profile to the dashboard.
$profilenode = $dashboard->add(get_string('myprofile'), new moodle_url('/user/profile.php',
array('id' => $user->id)), null, 'myprofile');
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

View File

@ -1927,4 +1927,40 @@ class moodle_page {
}
return $region;
}
/**
* Add a report node and a specific report to the navigation.
*
* @param int $userid The user ID that we are looking to add this report node to.
* @param array $nodeinfo Name and url of the final node that we are creating.
*/
public function add_report_nodes($userid, $nodeinfo) {
global $USER;
// Try to find the specific user node.
$newusernode = $this->navigation->find('user' . $userid, null);
$reportnode = null;
$navigationnodeerror =
'Could not find the navigation node requested. Please check that the node you are looking for exists.';
if ($userid != $USER->id) {
// Check that we have a valid node.
if (empty($newusernode)) {
// Throw an error if we ever reach here.
throw new coding_exception($navigationnodeerror);
}
// Add 'Reports' to the user node.
$reportnode = $newusernode->add(get_string('reports'));
} else {
// We are looking at our own profile.
$myprofilenode = $this->settingsnav->find('myprofile', null);
// Check that we do end up with a valid node.
if (empty($myprofilenode)) {
// Throw an error if we ever reach here.
throw new coding_exception($navigationnodeerror);
}
// Add 'Reports' to our node.
$reportnode = $myprofilenode->add(get_string('reports'));
}
// Finally add the report to the navigation tree.
$reportnode->add($nodeinfo['name'], $nodeinfo['url'], navigation_node::TYPE_COURSE);
}
}

View File

@ -39,6 +39,9 @@ information provided here is intended especially for developers.
* The outdated lib/google/Google_Client.php and related files have been completely removed. To use
the new client, read lib/google/readme_moodle.txt, please.
* profile_display_badges() has been deprecated. See MDL-48935 for details.
* Added a new method add_report_nodes() to pagelib.php. If you are looking to add links to the user profile page under the heading "Reports"
then please use this function to ensure that the breadcrumb and navigation block are created properly for all user profile pages.
=== 2.8 ===

View File

@ -127,3 +127,28 @@ function report_stats_supports_logstore($instance) {
}
return false;
}
/**
* Add nodes to myprofile page.
*
* @param \core_user\output\myprofile\tree $tree Tree object
* @param stdClass $user user object
* @param bool $iscurrentuser
* @param stdClass $course Course object
* @return bool
*/
function report_stats_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
global $CFG;
if (empty($CFG->enablestats)) {
return false;
}
if (empty($course)) {
// We want to display these reports under the site context.
$course = get_fast_modinfo(SITEID)->get_course();
}
if (report_stats_can_access_user_report($user, $course)) {
$url = new moodle_url('/report/stats/user.php', array('id' => $user->id, 'course' => $course->id));
$node = new core_user\output\myprofile\node('reports', 'stats', get_string('stats'), null, $url);
$tree->add_node($node);
}
}

View File

@ -35,6 +35,13 @@ $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
$coursecontext = context_course::instance($course->id);
$personalcontext = context_user::instance($user->id);
$pageheading = $course->fullname;
$userfullname = fullname($user);
if ($courseid == SITEID) {
$PAGE->set_context($personalcontext);
$pageheading = $userfullname;
}
if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
//TODO: do not require parents to be enrolled in courses - this is a hack!
@ -55,9 +62,24 @@ $PAGE->set_pagelayout('report');
$PAGE->set_url('/report/stats/user.php', array('id'=>$user->id, 'course'=>$course->id));
$PAGE->navigation->extend_for_user($user);
$PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
// Breadcrumb stuff.
$navigationnode = array(
'name' => get_string('stats'),
'url' => new moodle_url('/report/stats/user.php', array('id' => $user->id, 'course' => $course->id))
);
$PAGE->add_report_nodes($user->id, $navigationnode);
$PAGE->set_title("$course->shortname: $stractivityreport");
$PAGE->set_heading($course->fullname);
$PAGE->set_heading($pageheading);
echo $OUTPUT->header();
if ($courseid != SITEID) {
echo $OUTPUT->context_header(
array(
'heading' => $userfullname,
'user' => $user,
'usercontext' => $personalcontext
), 2);
}
// Trigger a user report viewed event.
$event = \report_stats\event\user_report_viewed::create(array('context' => $coursecontext, 'relateduserid' => $user->id));

View File

@ -55,6 +55,12 @@ if ($delete and confirm_sesskey()) {
redirect($PAGE->url);
}
// Create the breadcrumb.
$PAGE->add_report_nodes($USER->id, array(
'name' => get_string('navigationlink', 'report_usersessions'),
'url' => new moodle_url('/report/usersessions/user.php')
));
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('mysessions', 'report_usersessions'));