Skip to content

Commit

Permalink
MDL-57208 user: select default homepage from user preferences page.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Jul 30, 2019
1 parent f622ee9 commit 719f763
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lang/en/moodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@
$string['defaultcourseteacher'] = 'Teacher';
$string['defaultcourseteacherdescription'] = 'Teachers can do anything within a course, including changing the activities and grading students.';
$string['defaultcourseteachers'] = 'Teachers';
$string['defaulthomepageuser'] = 'Default home page';
$string['defaulthomepageuser_help'] = 'This determines the home page for your account';
$string['delete'] = 'Delete';
$string['deleteablock'] = 'Delete a block';
$string['deleteall'] = 'Delete all';
Expand Down
7 changes: 7 additions & 0 deletions lib/classes/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,13 @@ protected static function fill_preferences_cache() {
global $USER;
return $USER->id == $user->id && has_capability('moodle/blog:view', context_system::instance());
});
$preferences['user_home_page_preference'] = array('type' => PARAM_INT, 'null' => NULL_ALLOWED, 'default' => HOMEPAGE_MY,
'choices' => array(HOMEPAGE_SITE, HOMEPAGE_MY),
'permissioncallback' => function ($user, $preferencename) {
global $CFG;
return (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_USER));
}
);

// Core components that may want to define their preferences.
// List of core components implementing callback is hardcoded here for performance reasons.
Expand Down
10 changes: 10 additions & 0 deletions lib/navigationlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -5020,6 +5020,16 @@ protected function generate_user_settings($courseid, $userid, $gstitle='usercurr
$useraccount->add(get_string("changepassword"), $passwordchangeurl, self::TYPE_SETTING, null, 'changepassword');
}

// Default homepage.
$defaulthomepageuser = (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_USER));
if (isloggedin() && !isguestuser($user) && $defaulthomepageuser) {
if ($currentuser && has_capability('moodle/user:editownprofile', $systemcontext) ||
has_capability('moodle/user:editprofile', $usercontext)) {
$url = new moodle_url('/user/defaulthomepage.php', ['id' => $user->id]);
$useraccount->add(get_string('defaulthomepageuser'), $url, self::TYPE_SETTING, null, 'defaulthomepageuser');
}
}

if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) {
if ($currentuser && has_capability('moodle/user:editownprofile', $systemcontext) ||
has_capability('moodle/user:editprofile', $usercontext)) {
Expand Down
61 changes: 61 additions & 0 deletions user/classes/form/defaulthomepage_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Form to allow user to set their default home page
*
* @package core_user
* @copyright 2019 Paul Holden <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_user\form;

use lang_string;

defined('MOODLE_INTERNAL') || die;

require_once($CFG->dirroot . '/lib/formslib.php');

/**
* Form class
*
* @copyright 2019 Paul Holden <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class defaulthomepage_form extends \moodleform {

/**
* Define the form.
*/
public function definition () {
$mform = $this->_form;

$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);

$options = [
HOMEPAGE_SITE => new lang_string('site'),
HOMEPAGE_MY => new lang_string('mymoodle', 'admin'),
];

$mform->addElement('select', 'defaulthomepage', get_string('defaulthomepageuser'), $options);
$mform->addHelpButton('defaulthomepage', 'defaulthomepageuser');
$mform->setDefault('defaulthomepage', HOMEPAGE_MY);

$this->add_action_buttons(true, get_string('savechanges'));
}
}
68 changes: 68 additions & 0 deletions user/defaulthomepage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Allow user to set their default home page
*
* @package core_user
* @copyright 2019 Paul Holden <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once('../config.php');
require_once($CFG->dirroot . '/user/editlib.php');
require_once($CFG->dirroot . '/user/lib.php');

$userid = optional_param('id', $USER->id, PARAM_INT);

$PAGE->set_url('/user/defaulthomepage.php', ['id' => $userid]);

list($user, $course) = useredit_setup_preference_page($userid, SITEID);

$form = new core_user\form\defaulthomepage_form();

$user->defaulthomepage = get_user_preferences('user_home_page_preference', HOMEPAGE_MY, $user);
$form->set_data($user);

$redirect = new moodle_url('/user/preferences.php', ['userid' => $user->id]);
if ($form->is_cancelled()) {
redirect($redirect);
} else if ($data = $form->get_data()) {
$userupdate = [
'id' => $user->id,
'preference_user_home_page_preference' => $data->defaulthomepage,
];

useredit_update_user_preference($userupdate);

\core\event\user_updated::create_from_userid($userupdate['id'])->trigger();

redirect($redirect);
}

$PAGE->navbar->includesettingsbase = true;

$strdefaulthomepageuser = get_string('defaulthomepageuser');

$PAGE->set_title("$course->shortname: $strdefaulthomepageuser");
$PAGE->set_heading(fullname($user, true));

echo $OUTPUT->header();
echo $OUTPUT->heading($strdefaulthomepageuser);

$form->display();

echo $OUTPUT->footer();
37 changes: 30 additions & 7 deletions user/tests/behat/set_default_homepage.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@
Feature: Set the site home page and dashboard as the default home page
In order to set a page as my default home page
As a user
I need to go to the page I want and set it as my home page
I need to choose which page I want and set it as my home page

Background:
Given the following "courses" exist:
| fullname | shortname | category | groupmode |
| Course 1 | C1 | 0 | 1 |
# TODO MDL-57208 this functionality does not work without administration block.
And I log in as "admin"
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | User | One | user1@example.com |

Scenario: Admin sets the site page and then the dashboard as the default home page
# This functionality does not work without the administration block.
Given I log in as "admin"
And I am on site homepage
And I turn editing mode on
And I add the "Administration" block if not present
And I configure the "Administration" block
And I set the following fields to these values:
| Page contexts | Display throughout the entire site |
And I press "Save changes"
And I log out

Scenario: Admin sets the site page and then the dashboard as the default home page
Given I log in as "admin"
And I navigate to "Appearance > Navigation" in site administration
And I set the field "Default home page for users" to "User preference"
And I press "Save changes"
Expand All @@ -35,3 +36,25 @@ Feature: Set the site home page and dashboard as the default home page
And I should not see "Make this my default home page"
And I am on "Course 1" course homepage
Then "Dashboard" "text" should exist in the ".breadcrumb" "css_element"

Scenario: User cannot configure their preferred default home page unless allowed by admin
Given I log in as "user1"
When I follow "Preferences" in the user menu
Then I should not see "Default home page"

Scenario Outline: User can configure their preferred default home page when allowed by admin
Given I log in as "admin"
And I navigate to "Appearance > Navigation" in site administration
And I set the field "Default home page for users" to "User preference"
And I press "Save changes"
And I log out
When I log in as "user1"
And I follow "Preferences" in the user menu
And I follow "Default home page"
And I set the field "Default home page" to "<preference>"
And I press "Save changes"
Then "<breadcrumb>" "text" should exist in the ".breadcrumb" "css_element"
Examples:
| preference | breadcrumb |
| Site | Home |
| Dashboard | Dashboard |

0 comments on commit 719f763

Please sign in to comment.