Skip to content

Commit

Permalink
navigation MDL-14632 Fixed up user profile navigation and settings af…
Browse files Browse the repository at this point in the history
…ter recent changes.
  • Loading branch information
Sam Hemelryk committed May 17, 2010
1 parent 7928979 commit 87c215d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 29 deletions.
2 changes: 1 addition & 1 deletion course/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
$personalcontext = get_context_instance(CONTEXT_USER, $user->id);

require_login();
$PAGE->set_pagelayout('admin');
if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext) and !has_capability('moodle/course:participate', $coursecontext)) {
// do not require parents to be enrolled in courses ;-)
$PAGE->set_course($course);
Expand Down
64 changes: 42 additions & 22 deletions lib/navigationlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,9 @@ public function initialise() {

// Load for the current user
$this->load_for_user();
if ($this->page->context->contextlevel >= CONTEXT_COURSE) {
$this->load_for_user(null, true);
}
// Load each extending user into the navigation.
foreach ($this->extendforuser as $user) {
if ($user->id !== $USER->id) {
Expand Down Expand Up @@ -1349,7 +1352,7 @@ protected function load_activity(stdClass $cm, stdClass $course, navigation_node
* @param stdClass $user
* @return bool
*/
protected function load_for_user($user=null) {
protected function load_for_user($user=null, $forceforcontext=false) {
global $DB, $CFG, $USER;

$iscurrentuser = false;
Expand All @@ -1370,7 +1373,7 @@ protected function load_for_user($user=null) {
// 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) {
if ($course->id !== SITEID && (!$iscurrentuser || $forceforcontext)) {
if (array_key_exists($course->id, $this->mycourses)) {
$coursenode = $this->mycourses[$course->id]->coursenode;
} else {
Expand All @@ -1389,7 +1392,7 @@ protected function load_for_user($user=null) {
}

// Create a node to add user information under.
if ($iscurrentuser) {
if ($iscurrentuser && !$forceforcontext) {
// If it's the current user the information will go under the profile root node
$usernode = $this->rootnodes['myprofile'];
} else {
Expand All @@ -1412,7 +1415,7 @@ protected function load_for_user($user=null) {
// 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) {
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));
Expand Down Expand Up @@ -1556,6 +1559,7 @@ protected function module_extends_navigation($modname) {
*/
public function extend_for_user($user) {
$this->extendforuser[] = $user;
$this->page->settingsnav->extend_for_user($user->id);
}
/**
* Adds the given course to the navigation structure.
Expand Down Expand Up @@ -1854,6 +1858,7 @@ public function initialise($branchtype, $id) {
$course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
require_course_login($course);
$this->page = $PAGE;
$this->page->set_context(get_context_instance(CONTEXT_COURSE, $course->id));
$coursenode = $this->add_course($course);
$this->add_course_essentials($coursenode, $course);
if ($this->format_display_course_content($course->format)) {
Expand All @@ -1868,6 +1873,7 @@ public function initialise($branchtype, $id) {
$course = $DB->get_record_sql($sql, array($id), MUST_EXIST);
require_course_login($course);
$this->page = $PAGE;
$this->page->set_context(get_context_instance(CONTEXT_COURSE, $course->id));
$coursenode = $this->add_course($course);
$this->add_course_essentials($coursenode, $course);
$sections = $this->load_course_sections($course, $coursenode);
Expand All @@ -1878,6 +1884,7 @@ public function initialise($branchtype, $id) {
$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
require_course_login($course, true, $cm);
$this->page = $PAGE;
$this->page->set_context(get_context_instance(CONTEXT_MODULE, $cm->id));
$coursenode = $this->load_course($course);
$sections = $this->load_course_sections($course, $coursenode);
foreach ($sections as $section) {
Expand Down Expand Up @@ -2133,6 +2140,8 @@ class settings_navigation extends navigation_node {
protected $adminsection;
/** @var bool */
protected $initialised = false;
/** @var array */
protected $userstoextendfor = array();

/**
* Sets up the object with basic settings and preparse it for use
Expand Down Expand Up @@ -2867,31 +2876,42 @@ protected function load_user_settings($courseid=SITEID) {
if (isguestuser() || !isloggedin()) {
return false;
}

// This is terribly ugly code, but I couldn't see a better way around it
// we need to pick up the user id, it can be the current user or someone else
// and the key depends on the current location
// Default to look at id
$userkey='id';
if (strpos($FULLME,'/blog/') || strpos($FULLME, $CFG->admin.'/roles/')) {
// And blog and roles just do thier own thing using `userid`
$userkey = 'userid';
} else if ($this->context->contextlevel >= CONTEXT_COURSECAT && strpos($FULLME, '/message/')===false && strpos($FULLME, '/mod/forum/user')===false && strpos($FULLME, '/user/editadvanced')===false) {
// If we have a course context and we are not in message or forum
// Message and forum both pick the user up from `id`
$userkey = 'user';
}

$userid = optional_param($userkey, $USER->id, PARAM_INT);
if ($userid!=$USER->id) {
$usernode = $this->generate_user_settings($courseid, $userid, 'userviewingsettings');

if (count($this->userstoextendfor) > 0) {
$usernode = null;
foreach ($this->userstoextendfor as $userid) {
$node = $this->generate_user_settings($courseid, $userid, 'userviewingsettings');
if (is_null($usernode)) {
$usernode = $node;
}
}
$this->generate_user_settings($courseid, $USER->id);
} else {
$usernode = $this->generate_user_settings($courseid, $USER->id);
}
return $usernode;
}

public function extend_for_user($userid) {
global $CFG;

if (!in_array($userid, $this->userstoextendfor)) {
$this->userstoextendfor[] = $userid;
if ($this->initialised) {
$this->generate_user_settings($this->page->course->id, $userid, 'userviewingsettings');
$children = array();
foreach ($this->children as $child) {
$children[] = $child;
}
array_unshift($children, array_pop($children));
$this->children = new navigation_node_collection();
foreach ($children as $child) {
$this->children->add($child);
}
}
}
}

/**
* This function gets called by {@link load_user_settings()} and actually works out
* what can be shown/done
Expand Down
15 changes: 10 additions & 5 deletions user/profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,17 @@
$PAGE->set_subpage($currentpage->id);
$PAGE->set_title("$SITE->shortname: $strpublicprofile");
$PAGE->set_heading("$SITE->shortname: $strpublicprofile");
$PAGE->navigation->extend_for_user($user);
if ($node = $PAGE->settingsnav->get('userviewingsettings')) {
$node->forceopen = true;
if ($node = $PAGE->settingsnav->get('root')) {
$node->forceopen = false;

if (!$currentuser) {
$PAGE->navigation->extend_for_user($user);
if ($node = $PAGE->settingsnav->get('userviewingsettings')) {
$node->forceopen = true;
}
} else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
$node->forceopen = true;
}
if ($node = $PAGE->settingsnav->get('root')) {
$node->forceopen = false;
}


Expand Down
13 changes: 12 additions & 1 deletion user/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,18 @@

/// We've established they can see the user's name at least, so what about the rest?
$PAGE->navigation->extend_for_user($user);
if (!$currentuser) {
$PAGE->navigation->extend_for_user($user);
if ($node = $PAGE->settingsnav->get('userviewingsettings')) {
$node->forceopen = true;
}
} else if ($node = $PAGE->settingsnav->get('usercurrentsettings', navigation_node::TYPE_CONTAINER)) {
$node->forceopen = true;
}
if ($node = $PAGE->settingsnav->get('courseadmin')) {
$node->forceopen = false;
}

$PAGE->set_title("$course->fullname: $strpersonalprofile: $fullname");
$PAGE->set_heading($course->fullname);
$PAGE->set_pagelayout('standard');
Expand Down

0 comments on commit 87c215d

Please sign in to comment.