Skip to content

Commit

Permalink
MDL-26680 my page My Moodle "reset to default" button
Browse files Browse the repository at this point in the history
  • Loading branch information
micaherne committed Jan 21, 2014
1 parent f05e25d commit 41dcfbf
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lang/en/my.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@
$string['addpage'] = 'Add page';
$string['delpage'] = 'Delete page';
$string['managepages'] = 'Manage pages';
$string['resetpage'] = 'Reset page to default';
$string['reseterror'] = 'There was an error resetting your page';
19 changes: 16 additions & 3 deletions my/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

// TODO Add sesskey check to edit
$edit = optional_param('edit', null, PARAM_BOOL); // Turn editing on and off
$reset = optional_param('reset', null, PARAM_BOOL);

require_login();

Expand All @@ -53,7 +54,7 @@
redirect(new moodle_url('/', array('redirect' => 0)));
}

$userid = NULL;
$userid = null;
$USER->editing = $edit = 0; // Just in case
$context = context_system::instance();
$PAGE->set_blocks_editing_capability('moodle/my:configsyspages'); // unlikely :)
Expand Down Expand Up @@ -98,7 +99,14 @@

// Toggle the editing state and switches
if ($PAGE->user_allowed_editing()) {
if ($edit !== null) { // Editing state was specified
if ($reset !== null) {
if (!is_null($userid)) {
if(!$currentpage = my_reset_page($userid, MY_PAGE_PRIVATE)){
print_error('reseterror', 'my');
}
redirect(new moodle_url('/my'));
}
} else if ($edit !== null) { // Editing state was specified
$USER->editing = $edit; // Change editing state
if (!$currentpage->userid && $edit) {
// If we are viewing a system page as ordinary user, and the user turns
Expand Down Expand Up @@ -126,6 +134,10 @@
// Add button for editing page
$params = array('edit' => !$edit);

$resetbutton = '';
$resetstring = get_string('resetpage', 'my');
$reseturl = new moodle_url("$CFG->wwwroot/my/index.php", array('edit' => 1, 'reset' => 1));

if (!$currentpage->userid) {
// viewing a system page -- let the user customise it
$editstring = get_string('updatemymoodleon');
Expand All @@ -134,11 +146,12 @@
$editstring = get_string('updatemymoodleon');
} else {
$editstring = get_string('updatemymoodleoff');
$resetbutton = $OUTPUT->single_button($reseturl, $resetstring);
}

$url = new moodle_url("$CFG->wwwroot/my/index.php", $params);
$button = $OUTPUT->single_button($url, $editstring);
$PAGE->set_button($button);
$PAGE->set_button($resetbutton . $button);

} else {
$USER->editing = $edit = 0;
Expand Down
32 changes: 32 additions & 0 deletions my/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,38 @@ function my_copy_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index') {
return $page;
}

/*
* For a given user, this deletes their My Moodle page and returns them to the system default.
*
* @param int $userid the id of the user whose page should be reset
* @param int $private either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC
* @param string $pagetype either my-index or user-profile
* @return mixed system page, or false on error
*/
function my_reset_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index') {
global $DB, $CFG;

$page = my_get_page($userid, $private);
if ($page->userid == $userid) {
$context = context_user::instance($userid);
if ($blocks = $DB->get_records('block_instances', array('parentcontextid' => $context->id,
'pagetypepattern' => $pagetype))) {
foreach ($blocks as $block) {
if (is_null($block->subpagepattern) || $block->subpagepattern == $page->id) {
blocks_delete_instance($block);
}
}
}
$DB->delete_records('my_pages', array('id' => $page->id));
}

// Get the system default page
if (!$systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => $private))) {
return false; // error
}
return $systempage;
}

class my_syspage_block_manager extends block_manager {
// HACK WARNING!
// TODO: figure out a better way to do this
Expand Down
27 changes: 27 additions & 0 deletions my/tests/behat/add_blocks.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@core @core_my
Feature: Add blocks to my home page
In order to add more functionality to my home page
As a user
I need to add blocks to my home page

Background:
Given the following "users" exists:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@asd.com |
| student2 | Student | 2 | student2@asd.com |
And the following "courses" exists:
| fullname | shortname | format |
| Course 1 | C1 | topics |
And the following "course enrolments" exists:
| user | course | role |
| student1 | C1 | student |
| student2 | C1 | student |
And I log in as "student1"
And I follow "My home"

Scenario: Add blocks to page
When I press "Customise this page"
And I add the "Latest news" block
And I add the "My latest badges" block
Then I should see "Latest news"
And I should see "My latest badges"
29 changes: 29 additions & 0 deletions my/tests/behat/reset_page.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@core @core_my
Feature: Reset my home page to default
In order to remove customisations from my home page
As a user
I need to reset my home page

Background:
Given the following "users" exists:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@asd.com |
| student2 | Student | 2 | student2@asd.com |
And the following "courses" exists:
| fullname | shortname | format |
| Course 1 | C1 | topics |
And the following "course enrolments" exists:
| user | course | role |
| student1 | C1 | student |
| student2 | C1 | student |
And I log in as "student1"
And I follow "My home"

Scenario: Add blocks to page and reset
When I press "Customise this page"
And I add the "Latest news" block
And I add the "My latest badges" block
And I press "Reset page to default"
Then I should not see "Latest news"
And I should not see "My latest badges"
And I should not see "Reset page to default"
2 changes: 2 additions & 0 deletions theme/base/style/core.css
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ a.skip:active {position: static;display: block;}
.navbutton {text-align:right;}
.breadcrumb ul {padding:0;margin:0;text-indent:0;list-style:none;}
.navbutton {float: right;}
.navbutton .singlebutton {margin-left: 4px}
.breadcrumb li,
.navbutton div,
.navbutton form {display:inline;}
Expand Down Expand Up @@ -758,6 +759,7 @@ body.tag .managelink {padding: 5px;}
.dir-rtl .headermenu {float:left;}
.dir-rtl .breadcrumb {float:right;}
.dir-rtl .navbutton {float: left;}
.dir-rtl .navbutton .singlebutton {margin-right: 4px}
.dir-rtl .breadcrumb ul li { float: right; margin-left: 5px;}
.dir-rtl .mform .fitem .fitemtitle {float:right;}
.dir-rtl .loginbox .loginform .form-label {float:right;text-align:left;}
Expand Down
10 changes: 10 additions & 0 deletions theme/bootstrapbase/less/moodle/modules.less
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ select {
margin-top: 4px;
}

.breadcrumb-button .singlebutton {
float: left;
margin-left: 4px;
}

.dir-rtl {
.nav-tabs > li,
.nav-pills > li {
Expand Down Expand Up @@ -142,6 +147,11 @@ select {
float: left;
}

.dir-rtl .breadcrumb-button .singlebutton {
float: right;
margin-right: 4px;
}

.ie .row-fluid .desktop-first-column {
margin-left: 0;
}
Expand Down
2 changes: 1 addition & 1 deletion theme/bootstrapbase/style/moodle.css

Large diffs are not rendered by default.

18 changes: 16 additions & 2 deletions user/profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

$userid = optional_param('id', 0, PARAM_INT);
$edit = optional_param('edit', null, PARAM_BOOL); // Turn editing on and off
$reset = optional_param('reset', null, PARAM_BOOL);

$PAGE->set_url('/user/profile.php', array('id'=>$userid));

Expand Down Expand Up @@ -149,7 +150,14 @@

// Toggle the editing state and switches
if ($PAGE->user_allowed_editing()) {
if ($edit !== null) { // Editing state was specified
if ($reset !== null) {
if (!is_null($userid)) {
if (!$currentpage = my_reset_page($userid, MY_PAGE_PUBLIC, 'user-profile')){
print_error('reseterror', 'my');
}
redirect(new moodle_url('/user/profile.php'));
}
} else if ($edit !== null) { // Editing state was specified
$USER->editing = $edit; // Change editing state
if (!$currentpage->userid && $edit) {
// If we are viewing a system page as ordinary user, and the user turns
Expand All @@ -176,19 +184,25 @@
// Add button for editing page
$params = array('edit' => !$edit);

$resetbutton = '';
$resetstring = get_string('resetpage', 'my');
$reseturl = new moodle_url("$CFG->wwwroot/user/profile.php", array('edit' => 1, 'reset' => 1));

if (!$currentpage->userid) {
// viewing a system page -- let the user customise it
$editstring = get_string('updatemymoodleon');
$params['edit'] = 1;
} else if (empty($edit)) {
$editstring = get_string('updatemymoodleon');
$resetbutton = $OUTPUT->single_button($reseturl, $resetstring);
} else {
$editstring = get_string('updatemymoodleoff');
$resetbutton = $OUTPUT->single_button($reseturl, $resetstring);
}

$url = new moodle_url("$CFG->wwwroot/user/profile.php", $params);
$button = $OUTPUT->single_button($url, $editstring);
$PAGE->set_button($button);
$PAGE->set_button($resetbutton . $button);

} else {
$USER->editing = $edit = 0;
Expand Down
25 changes: 25 additions & 0 deletions user/tests/behat/add_blocks.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@core @core_user
Feature: Add blocks to my profile page
In order to add more functionality to my profile page
As a user
I need to add blocks to my profile page

Background:
Given the following "users" exists:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@asd.com |
| student2 | Student | 2 | student2@asd.com |
And the following "courses" exists:
| fullname | shortname | format |
| Course 1 | C1 | topics |
And the following "course enrolments" exists:
| user | course | role |
| student1 | C1 | student |
| student2 | C1 | student |
And I log in as "admin"
And I follow "View profile"

Scenario: Add blocks to page
When I press "Customise this page"
And I add the "Latest news" block
Then I should see "Latest news"
27 changes: 27 additions & 0 deletions user/tests/behat/reset_page.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@core @core_user
Feature: Reset my profile page to default
In order to remove customisations from my profile page
As a user
I need to reset my profile page

Background:
Given the following "users" exists:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@asd.com |
| student2 | Student | 2 | student2@asd.com |
And the following "courses" exists:
| fullname | shortname | format |
| Course 1 | C1 | topics |
And the following "course enrolments" exists:
| user | course | role |
| student1 | C1 | student |
| student2 | C1 | student |
And I log in as "admin"
And I follow "View profile"

Scenario: Add blocks to page and reset
When I press "Customise this page"
And I add the "Latest news" block
And I press "Reset page to default"
Then I should not see "Latest news"
And I should not see "Reset page to default"

0 comments on commit 41dcfbf

Please sign in to comment.