MDL-70801 core_my: Add a new courses page

This commit is contained in:
Mathew May 2021-10-21 12:13:36 +08:00
parent c69c33b14d
commit 6ca9c2154a
33 changed files with 417 additions and 51 deletions

View file

@ -29,41 +29,70 @@
define('MY_PAGE_PUBLIC', 0);
define('MY_PAGE_PRIVATE', 1);
define('MY_PAGE_DEFAULT', '__default');
define('MY_PAGE_COURSES', '__courses');
require_once("$CFG->libdir/blocklib.php");
/*
/**
* For a given user, this returns the $page information for their My Moodle page
*
* @param int|null $userid the id of the user whose page should be retrieved
* @param int|null $private either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC
* @param string|null $pagename Differentiate between standard /my or /courses pages.
*/
function my_get_page($userid, $private=MY_PAGE_PRIVATE) {
function my_get_page(?int $userid, int $private = MY_PAGE_PRIVATE, string $pagename = MY_PAGE_DEFAULT) {
global $DB, $CFG;
if (empty($CFG->forcedefaultmymoodle) && $userid) { // Ignore custom My Moodle pages if admin has forced them
// Does the user have their own page defined? If so, return it.
if ($customised = $DB->get_record('my_pages', array('userid' => $userid, 'private' => $private))) {
if ($customised = $DB->get_record(
'my_pages',
array('userid' => $userid, 'private' => $private, 'name' => $pagename),
'*',
IGNORE_MULTIPLE
)) {
return $customised;
}
}
// Otherwise return the system default page
return $DB->get_record('my_pages', array('userid' => null, 'name' => '__default', 'private' => $private));
return $DB->get_record('my_pages', array('userid' => null, 'name' => $pagename, 'private' => $private), '*', IGNORE_MULTIPLE);
}
/*
/**
* This copies a system default page to the current user
*
* @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
* @param string $pagename Differentiate between standard /my or /courses pages.
*/
function my_copy_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index') {
function my_copy_page(
int $userid,
int $private = MY_PAGE_PRIVATE,
string $pagetype = 'my-index',
string $pagename = MY_PAGE_DEFAULT
) {
global $DB;
if ($customised = $DB->get_record('my_pages', array('userid' => $userid, 'private' => $private))) {
if ($customised = $DB->get_record(
'my_pages',
array('userid' => $userid, 'name' => $pagename, 'private' => $private),
'*',
IGNORE_MULTIPLE
)) {
return $customised; // We're done!
}
// Get the system default page
if (!$systempage = $DB->get_record('my_pages', array('userid' => null, 'name' => '__default', 'private' => $private))) {
if (!$systempage = $DB->get_record(
'my_pages',
array('userid' => null, 'name' => $pagename, 'private' => $private),
'*',
IGNORE_MULTIPLE
)) {
return false; // error
}
@ -117,18 +146,24 @@ 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
* @param string $pagename Differentiate between standard /my or /courses pages.
* @return mixed system page, or false on error
*/
function my_reset_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index') {
function my_reset_page(
int $userid,
int $private = MY_PAGE_PRIVATE,
string $pagetype='my-index',
string $pagename = MY_PAGE_DEFAULT
) {
global $DB, $CFG;
$page = my_get_page($userid, $private);
$page = my_get_page($userid, $private, $pagename);
if ($page->userid == $userid) {
$context = context_user::instance($userid);
if ($blocks = $DB->get_records('block_instances', array('parentcontextid' => $context->id,
@ -140,11 +175,16 @@ function my_reset_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index')
}
}
$DB->delete_records('block_positions', ['subpage' => $page->id, 'pagetype' => $pagetype, 'contextid' => $context->id]);
$DB->delete_records('my_pages', array('id' => $page->id));
$DB->delete_records('my_pages', array('id' => $page->id, 'name' => $pagename));
}
// Get the system default page
if (!$systempage = $DB->get_record('my_pages', array('userid' => null, 'name' => '__default', 'private' => $private))) {
if (!$systempage = $DB->get_record(
'my_pages',
array('userid' => null, 'name' => $pagename, 'private' => $private),
'*',
IGNORE_MULTIPLE
)) {
return false; // error
}
@ -166,10 +206,16 @@ function my_reset_page($userid, $private=MY_PAGE_PRIVATE, $pagetype='my-index')
*
* @param int $private Either MY_PAGE_PRIVATE or MY_PAGE_PUBLIC.
* @param string $pagetype Either my-index or user-profile.
* @param progress_bar $progressbar A progress bar to update.
* @param progress_bar|null $progressbar A progress bar to update.
* @param string $pagename Differentiate between standard /my or /courses pages.
* @return void
*/
function my_reset_page_for_all_users($private = MY_PAGE_PRIVATE, $pagetype = 'my-index', $progressbar = null) {
function my_reset_page_for_all_users(
int $private = MY_PAGE_PRIVATE,
string $pagetype = 'my-index',
?progress_bar $progressbar = null,
string $pagename = MY_PAGE_DEFAULT
) {
global $DB;
// This may take a while. Raise the execution time limit.
@ -199,12 +245,14 @@ function my_reset_page_for_all_users($private = MY_PAGE_PRIVATE, $pagetype = 'my
AND bi.pagetypepattern = :pagetypepattern
AND (bi.subpagepattern IS NULL OR bi.subpagepattern = " . $DB->sql_concat("''", 'p.id') . ")
WHERE p.private = :private
AND p.name = :name
AND p.userid $infragment";
$params = array_merge([
'private' => $private,
'usercontextlevel' => CONTEXT_USER,
'pagetypepattern' => $pagetype
'pagetypepattern' => $pagetype,
'name' => $pagename
], $inparams);
$blockids = $DB->get_fieldset_sql($sql, $params);