Skip to content

Commit

Permalink
MDL-46878 my: Allow admins to reset everyone's dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic Massart committed Sep 18, 2015
1 parent d230899 commit ad347f6
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lang/en/my.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
$string['defaultpage'] = 'Default My Moodle page';
$string['defaultprofilepage'] = 'Default profile page';
$string['addpage'] = 'Add page';
$string['alldashboardswerereset'] = 'All Dashboard pages have been reset to default.';
$string['allprofileswerereset'] = 'All profile pages have been reset to default.';
$string['delpage'] = 'Delete page';
$string['managepages'] = 'Manage pages';
$string['reseteveryonesdashboard'] = 'Reset Dashboard for all users';
$string['reseteveryonesprofile'] = 'Reset profile for all users';
$string['resetpage'] = 'Reset page to default';
$string['reseterror'] = 'There was an error resetting your page';
25 changes: 25 additions & 0 deletions lib/blocklib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2055,6 +2055,31 @@ function blocks_delete_instance($instance, $nolongerused = false, $skipblockstab
}
}

/**
* Delete multiple blocks at once.
*
* @param array $instanceids A list of block instance ID.
*/
function blocks_delete_instances($instanceids) {
global $DB;

$instances = $DB->get_recordset_list('block_instances', 'id', $instanceids);
foreach ($instances as $instance) {
blocks_delete_instance($instance, false, true);
}
$instances->close();

$DB->delete_records_list('block_positions', 'blockinstanceid', $instanceids);
$DB->delete_records_list('block_instances', 'id', $instanceids);

$preferences = array();
foreach ($instanceids as $instanceid) {
$preferences[] = 'block' . $instanceid . 'hidden';
$preferences[] = 'docked_block_instance_' . $instanceid;
}
$DB->delete_records_list('user_preferences', 'name', $preferences);
}

/**
* Delete all the blocks that belong to a particular context.
*
Expand Down
12 changes: 11 additions & 1 deletion my/indexsys.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
require_once($CFG->dirroot . '/my/lib.php');
require_once($CFG->libdir.'/adminlib.php');

$edit = optional_param('edit', null, PARAM_BOOL); // Turn editing on and off
$resetall = optional_param('resetall', null, PARAM_BOOL);

require_login();

Expand All @@ -48,6 +48,11 @@
$PAGE->set_blocks_editing_capability('moodle/my:configsyspages');
admin_externalpage_setup('mypage', '', null, '', array('pagelayout' => 'mydashboard'));

if ($resetall && confirm_sesskey()) {
my_reset_page_for_all_users(MY_PAGE_PRIVATE, 'my-index');
redirect($PAGE->url, get_string('alldashboardswerereset', 'my'));
}

// Override pagetype to show blocks properly.
$PAGE->set_pagetype('my-index');

Expand All @@ -61,6 +66,11 @@
}
$PAGE->set_subpage($currentpage->id);

// Display a button to reset everyone's dashboard.
$url = new moodle_url($PAGE->url, array('resetall' => 1));
$button = $OUTPUT->single_button($url, get_string('reseteveryonesdashboard', 'my'));
$PAGE->set_button($button . $PAGE->button);

echo $OUTPUT->header();

echo $OUTPUT->custom_block_region('content');
Expand Down
51 changes: 51 additions & 0 deletions my/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,57 @@ function my_reset_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index')
return $systempage;
}

/**
* Resets the page customisations for all users.
*
* @param int $private Either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC.
* @param string $pagetype Either my-index or user-profile.
* @return void
*/
function my_reset_page_for_all_users($private = MY_PAGE_PRIVATE, $pagetype = 'my-index') {
global $DB;

// Find all the user pages.
$where = 'userid IS NOT NULL AND private = :private';
$params = array('private' => $private);
$pages = $DB->get_recordset_select('my_pages', $where, $params, 'id, userid');
$pageids = array();
$blockids = array();

foreach ($pages as $page) {
$pageids[] = $page->id;
$usercontext = context_user::instance($page->userid);

// Find all block instances in that page.
$blocks = $DB->get_recordset('block_instances', array('parentcontextid' => $usercontext->id,
'pagetypepattern' => $pagetype), '', 'id, subpagepattern');
foreach ($blocks as $block) {
if (is_null($block->subpagepattern) || $block->subpagepattern == $page->id) {
$blockids[] = $block->id;
}
}
$blocks->close();
}
$pages->close();

// Wrap the SQL queries in a transaction.
$transaction = $DB->start_delegated_transaction();

// Delete the block instances.
if (!empty($blockids)) {
blocks_delete_instances($blockids);
}

// Finally delete the pages.
if (!empty($pageids)) {
list($insql, $inparams) = $DB->get_in_or_equal($pageids);
$DB->delete_records_select('my_pages', "id $insql", $pageids);
}

// We should be good to go now.
$transaction->allow_commit();
}

class my_syspage_block_manager extends block_manager {
// HACK WARNING!
// TODO: figure out a better way to do this
Expand Down
10 changes: 9 additions & 1 deletion user/profilesys.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
require_once($CFG->dirroot . '/my/lib.php');
require_once($CFG->libdir.'/adminlib.php');

$edit = optional_param('edit', null, PARAM_BOOL); // Turn editing on and off.
$resetall = optional_param('resetall', null, PARAM_BOOL);

require_login();

Expand All @@ -40,6 +40,11 @@
$PAGE->set_blocks_editing_capability('moodle/my:configsyspages');
admin_externalpage_setup('profilepage', '', null, '', array('pagelayout' => 'mypublic'));

if ($resetall && confirm_sesskey()) {
my_reset_page_for_all_users(MY_PAGE_PUBLIC, 'user-profile');
redirect($PAGE->url, get_string('allprofileswerereset', 'my'));
}

// Override pagetype to show blocks properly.
$PAGE->set_pagetype('user-profile');

Expand All @@ -53,6 +58,9 @@
}
$PAGE->set_subpage($currentpage->id);

$url = new moodle_url($PAGE->url, array('resetall' => 1));
$button = $OUTPUT->single_button($url, get_string('reseteveryonesprofile', 'my'));
$PAGE->set_button($button . $PAGE->button);

echo $OUTPUT->header();

Expand Down

0 comments on commit ad347f6

Please sign in to comment.