/** * 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; // This may take a while. Raise the execution time limit. core_php_time_limit::raise(); // Find all the user pages and all block instances in them. $sql = "SELECT bi.id\n FROM {my_pages} p\n JOIN {context} ctx ON ctx.instanceid = p.userid AND ctx.contextlevel = :usercontextlevel\n JOIN {block_instances} bi ON bi.parentcontextid = ctx.id AND\n bi.pagetypepattern = :pagetypepattern AND\n (bi.subpagepattern IS NULL OR bi.subpagepattern = " . $DB->sql_concat("''", 'p.id') . ")\n WHERE p.private = :private"; $params = array('private' => $private, 'usercontextlevel' => CONTEXT_USER, 'pagetypepattern' => $pagetype); $blockids = $DB->get_fieldset_sql($sql, $params); // 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. $DB->delete_records_select('my_pages', 'userid IS NOT NULL AND private = :private', ['private' => $private]); // We should be good to go now. $transaction->allow_commit(); // Trigger dashboard has been reset event. $eventparams = array('context' => context_system::instance(), 'other' => array('private' => $private, 'pagetype' => $pagetype)); $event = \core\event\dashboards_reset::create($eventparams); $event->trigger(); }
public function test_delete_instances() { global $DB; $this->purge_blocks(); $this->setAdminUser(); $regionname = 'a-region'; $blockname = $this->get_a_known_block_type(); $context = context_system::instance(); list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname), $context, 'page-type'); $blockmanager->add_blocks(array($regionname => array($blockname, $blockname, $blockname)), null, null, false, 3); $blockmanager->load_blocks(); $blocks = $blockmanager->get_blocks_for_region($regionname); $blockids = array(); $preferences = array(); // Create block related data. foreach ($blocks as $block) { $instance = $block->instance; $pref = 'block' . $instance->id . 'hidden'; set_user_preference($pref, '123', 123); $preferences[] = $pref; $pref = 'docked_block_instance_' . $instance->id; set_user_preference($pref, '123', 123); $preferences[] = $pref; blocks_set_visibility($instance, $page, 1); $blockids[] = $instance->id; } // Confirm what has been set. $this->assertCount(3, $blockids); list($insql, $inparams) = $DB->get_in_or_equal($blockids); $this->assertEquals(3, $DB->count_records_select('block_positions', "blockinstanceid {$insql}", $inparams)); list($insql, $inparams) = $DB->get_in_or_equal($preferences); $this->assertEquals(6, $DB->count_records_select('user_preferences', "name {$insql}", $inparams)); // Keep a block on the side. $allblockids = $blockids; $tokeep = array_pop($blockids); // Delete and confirm what should have happened. blocks_delete_instances($blockids); // Reload the manager. list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname), $context, 'page-type'); $blockmanager->load_blocks(); $blocks = $blockmanager->get_blocks_for_region($regionname); $this->assertCount(1, $blocks); list($insql, $inparams) = $DB->get_in_or_equal($allblockids); $this->assertEquals(1, $DB->count_records_select('block_positions', "blockinstanceid {$insql}", $inparams)); list($insql, $inparams) = $DB->get_in_or_equal($preferences); $this->assertEquals(2, $DB->count_records_select('user_preferences', "name {$insql}", $inparams)); $this->assertFalse(context_block::instance($blockids[0], IGNORE_MISSING)); $this->assertFalse(context_block::instance($blockids[1], IGNORE_MISSING)); context_block::instance($tokeep); // Would throw an exception if it was deleted. }
/** * 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; // This may take a while. Raise the execution time limit. core_php_time_limit::raise(); // 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'); $blockids = array(); foreach ($pages as $page) { $usercontext = context_user::instance($page->userid); // Find all block instances in that page. $blockswhere = 'parentcontextid = :parentcontextid AND pagetypepattern = :pagetypepattern AND (subpagepattern IS NULL OR subpagepattern = :subpagepattern)'; $blockswhereparams = [ 'parentcontextid' => $usercontext->id, 'pagetypepattern' => $pagetype, 'subpagepattern' => $page->id ]; if ($pageblockids = $DB->get_fieldset_select('block_instances', 'id', $blockswhere, $blockswhereparams)) { $blockids = array_merge($blockids, $pageblockids); } } $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($pages)) { $DB->delete_records_select('my_pages', $where, $params); } // We should be good to go now. $transaction->allow_commit(); }
/** * 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(); }