MDL-46878 my: Allow admins to reset everyone's dashboard
This commit is contained in:
@@ -30,7 +30,11 @@ $string['pinblocksexplan'] = 'Any block settings you configure here will be visi
|
||||
$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';
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
+11
-1
@@ -39,7 +39,7 @@ require_once(dirname(__FILE__) . '/../config.php');
|
||||
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();
|
||||
|
||||
@@ -48,6 +48,11 @@ $header = "$SITE->shortname: ".get_string('myhome')." (".get_string('mypage', 'a
|
||||
$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');
|
||||
|
||||
@@ -61,6 +66,11 @@ if (!$currentpage = my_get_page(null, MY_PAGE_PRIVATE)) {
|
||||
}
|
||||
$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');
|
||||
|
||||
+51
@@ -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
|
||||
|
||||
+9
-1
@@ -31,7 +31,7 @@ require_once(dirname(__FILE__) . '/../config.php');
|
||||
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();
|
||||
|
||||
@@ -40,6 +40,11 @@ $header = "$SITE->shortname: ".get_string('publicprofile')." (".get_string('mypr
|
||||
$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');
|
||||
|
||||
@@ -53,6 +58,9 @@ if (!$currentpage = my_get_page(null, MY_PAGE_PUBLIC)) {
|
||||
}
|
||||
$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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user