diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 9a9d2cb1b03..668823bfcda 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -467,6 +467,8 @@ $string['defaultcoursesummary'] = 'Write a concise and interesting paragraph her $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'; diff --git a/lib/classes/user.php b/lib/classes/user.php index f95b1a25d05..15d905fe0ba 100644 --- a/lib/classes/user.php +++ b/lib/classes/user.php @@ -986,6 +986,13 @@ class core_user { 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. diff --git a/lib/navigationlib.php b/lib/navigationlib.php index bae521fa628..5aac0ed596a 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -5020,6 +5020,16 @@ class settings_navigation extends navigation_node { $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)) { diff --git a/user/classes/form/defaulthomepage_form.php b/user/classes/form/defaulthomepage_form.php new file mode 100644 index 00000000000..f01ec3e77ed --- /dev/null +++ b/user/classes/form/defaulthomepage_form.php @@ -0,0 +1,61 @@ +. + +/** + * Form to allow user to set their default home page + * + * @package core_user + * @copyright 2019 Paul Holden + * @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 + * @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')); + } +} diff --git a/user/defaulthomepage.php b/user/defaulthomepage.php new file mode 100644 index 00000000000..91bcfe9503c --- /dev/null +++ b/user/defaulthomepage.php @@ -0,0 +1,68 @@ +. + +/** + * Allow user to set their default home page + * + * @package core_user + * @copyright 2019 Paul Holden + * @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(); diff --git a/user/tests/behat/set_default_homepage.feature b/user/tests/behat/set_default_homepage.feature index 0497985483b..750ca5cd025 100644 --- a/user/tests/behat/set_default_homepage.feature +++ b/user/tests/behat/set_default_homepage.feature @@ -2,14 +2,19 @@ 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 @@ -17,10 +22,6 @@ Feature: Set the site home page and dashboard as the default home page 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" @@ -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 "" + And I press "Save changes" + Then "" "text" should exist in the ".breadcrumb" "css_element" + Examples: + | preference | breadcrumb | + | Site | Home | + | Dashboard | Dashboard |