first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+314
View File
@@ -0,0 +1,314 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Blocks external API
*
* @package core_block
* @category external
* @copyright 2017 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.3
*/
use core_external\external_api;
use core_external\external_files;
use core_external\external_format_value;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
defined('MOODLE_INTERNAL') || die;
require_once("$CFG->dirroot/my/lib.php");
/**
* Blocks external functions
*
* @package core_block
* @category external
* @copyright 2015 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.3
*/
class core_block_external extends external_api {
/**
* Returns a block structure.
*
* @return external_single_structure a block single structure.
* @since Moodle 3.6
*/
private static function get_block_structure() {
return new external_single_structure(
array(
'instanceid' => new external_value(PARAM_INT, 'Block instance id.'),
'name' => new external_value(PARAM_PLUGIN, 'Block name.'),
'region' => new external_value(PARAM_ALPHANUMEXT, 'Block region.'),
'positionid' => new external_value(PARAM_INT, 'Position id.'),
'collapsible' => new external_value(PARAM_BOOL, 'Whether the block is collapsible.'),
'dockable' => new external_value(PARAM_BOOL, 'Whether the block is dockable.'),
'weight' => new external_value(PARAM_INT, 'Used to order blocks within a region.', VALUE_OPTIONAL),
'visible' => new external_value(PARAM_BOOL, 'Whether the block is visible.', VALUE_OPTIONAL),
'contents' => new external_single_structure(
array(
'title' => new external_value(PARAM_RAW, 'Block title.'),
'content' => new external_value(PARAM_RAW, 'Block contents.'),
'contentformat' => new external_format_value('content'),
'footer' => new external_value(PARAM_RAW, 'Block footer.'),
'files' => new external_files('Block files.'),
),
'Block contents (if required).', VALUE_OPTIONAL
),
'configs' => new external_multiple_structure(
new external_single_structure(
array(
'name' => new external_value(PARAM_RAW, 'Name.'),
'value' => new external_value(PARAM_RAW, 'JSON encoded representation of the config value.'),
'type' => new external_value(PARAM_ALPHA, 'Type (instance or plugin).'),
)
),
'Block instance and plugin configuration settings.', VALUE_OPTIONAL
),
), 'Block information.'
);
}
/**
* Convenience function for getting all the blocks of the current $PAGE.
*
* @param bool $includeinvisible Whether to include not visible blocks or not
* @param bool $returncontents Whether to return the block contents
* @return array Block information
* @since Moodle 3.6
*/
private static function get_all_current_page_blocks($includeinvisible = false, $returncontents = false) {
global $PAGE, $OUTPUT;
// Set page URL to a fake URL to avoid errors.
$PAGE->set_url(new \moodle_url('/webservice/core_block_external/'));
// Load the block instances for all the regions.
$PAGE->blocks->load_blocks($includeinvisible);
$PAGE->blocks->create_all_block_instances();
$allblocks = array();
$blocks = $PAGE->blocks->get_content_for_all_regions($OUTPUT);
foreach ($blocks as $region => $regionblocks) {
$regioninstances = $PAGE->blocks->get_blocks_for_region($region);
// Index block instances to retrieve required info.
$blockinstances = array();
foreach ($regioninstances as $ri) {
$blockinstances[$ri->instance->id] = $ri;
}
foreach ($regionblocks as $bc) {
$block = [
'instanceid' => $bc->blockinstanceid,
'name' => $blockinstances[$bc->blockinstanceid]->instance->blockname,
'region' => $region,
'positionid' => $bc->blockpositionid,
'collapsible' => (bool) $bc->collapsible,
'dockable' => (bool) $bc->dockable,
'weight' => $blockinstances[$bc->blockinstanceid]->instance->weight,
'visible' => $blockinstances[$bc->blockinstanceid]->instance->visible,
];
if ($returncontents) {
$block['contents'] = (array) $blockinstances[$bc->blockinstanceid]->get_content_for_external($OUTPUT);
}
$configs = (array) $blockinstances[$bc->blockinstanceid]->get_config_for_external();
foreach ($configs as $type => $data) {
foreach ((array) $data as $name => $value) {
$block['configs'][] = [
'name' => $name,
'value' => json_encode($value), // Always JSON encode, we may receive non-scalar values.
'type' => $type,
];
}
}
$allblocks[] = $block;
}
}
return $allblocks;
}
/**
* Returns description of get_course_blocks parameters.
*
* @return external_function_parameters
* @since Moodle 3.3
*/
public static function get_course_blocks_parameters() {
return new external_function_parameters(
array(
'courseid' => new external_value(PARAM_INT, 'course id'),
'returncontents' => new external_value(PARAM_BOOL, 'Whether to return the block contents.', VALUE_DEFAULT, false),
)
);
}
/**
* Returns blocks information for a course.
*
* @param int $courseid The course id
* @param bool $returncontents Whether to return the block contents
* @return array Blocks list and possible warnings
* @throws moodle_exception
* @since Moodle 3.3
*/
public static function get_course_blocks($courseid, $returncontents = false) {
global $PAGE;
$warnings = array();
$params = self::validate_parameters(self::get_course_blocks_parameters(),
['courseid' => $courseid, 'returncontents' => $returncontents]);
$course = get_course($params['courseid']);
$context = context_course::instance($course->id);
self::validate_context($context);
// Specific layout for frontpage course.
if ($course->id == SITEID) {
$PAGE->set_pagelayout('frontpage');
$PAGE->set_pagetype('site-index');
} else {
$PAGE->set_pagelayout('course');
// Ensure course format is set (view course/view.php).
$course->format = course_get_format($course)->get_format();
$PAGE->set_pagetype('course-view-' . $course->format);
}
$allblocks = self::get_all_current_page_blocks(false, $params['returncontents']);
return array(
'blocks' => $allblocks,
'warnings' => $warnings
);
}
/**
* Returns description of get_course_blocks result values.
*
* @return external_single_structure
* @since Moodle 3.3
*/
public static function get_course_blocks_returns() {
return new external_single_structure(
array(
'blocks' => new external_multiple_structure(self::get_block_structure(), 'List of blocks in the course.'),
'warnings' => new external_warnings(),
)
);
}
/**
* Returns description of get_dashboard_blocks parameters.
*
* @return external_function_parameters
* @since Moodle 3.6
*/
public static function get_dashboard_blocks_parameters() {
return new external_function_parameters(
array(
'userid' => new external_value(PARAM_INT, 'User id (optional), default is current user.', VALUE_DEFAULT, 0),
'returncontents' => new external_value(PARAM_BOOL, 'Whether to return the block contents.', VALUE_DEFAULT, false),
'mypage' => new external_value(PARAM_TEXT, 'What my page to return blocks of', VALUE_DEFAULT, MY_PAGE_DEFAULT),
)
);
}
/**
* Returns blocks information for the given user dashboard.
*
* @param int $userid The user id to retrieve the blocks from, optional, default is to current user.
* @param bool $returncontents Whether to return the block contents
* @param string $mypage The page to get blocks of within my
* @return array Blocks list and possible warnings
* @throws moodle_exception
* @since Moodle 3.6
*/
public static function get_dashboard_blocks($userid = 0, $returncontents = false, $mypage = MY_PAGE_DEFAULT) {
global $CFG, $USER, $PAGE;
require_once($CFG->dirroot . '/my/lib.php');
$warnings = array();
$params = self::validate_parameters(self::get_dashboard_blocks_parameters(),
['userid' => $userid, 'returncontents' => $returncontents, 'mypage' => $mypage]);
$userid = $params['userid'];
if (empty($userid)) {
$userid = $USER->id;
}
if ($USER->id != $userid) {
// We must check if the current user can view other users dashboard.
require_capability('moodle/site:config', context_system::instance());
$user = core_user::get_user($userid, '*', MUST_EXIST);
core_user::require_active_user($user);
}
$context = context_user::instance($userid);;
self::validate_context($context);
$currentpage = null;
if ($params['mypage'] === MY_PAGE_DEFAULT) {
$currentpage = my_get_page($userid);
} else if ($params['mypage'] === MY_PAGE_COURSES) {
$currentpage = my_get_page($userid, MY_PAGE_PUBLIC, MY_PAGE_COURSES);
}
if (!$currentpage) {
throw new moodle_exception('mymoodlesetup');
}
$PAGE->set_context($context);
$PAGE->set_pagelayout('mydashboard');
$PAGE->set_pagetype('my-index');
$PAGE->blocks->add_region('content'); // Need to add this special regition to retrieve the central blocks.
$PAGE->set_subpage($currentpage->id);
// Load the block instances in the current $PAGE for all the regions.
$returninvisible = has_capability('moodle/my:manageblocks', $context) ? true : false;
$allblocks = self::get_all_current_page_blocks($returninvisible, $params['returncontents']);
return array(
'blocks' => $allblocks,
'warnings' => $warnings
);
}
/**
* Returns description of get_dashboard_blocks result values.
*
* @return external_single_structure
* @since Moodle 3.6
*/
public static function get_dashboard_blocks_returns() {
return new external_single_structure(
array(
'blocks' => new external_multiple_structure(self::get_block_structure(), 'List of blocks in the dashboard.'),
'warnings' => new external_warnings(),
)
);
}
}
+131
View File
@@ -0,0 +1,131 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_block\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
/**
* This is the external method used for fetching the addable blocks in a given page.
*
* @package core_block
* @since Moodle 3.11
* @copyright 2020 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class fetch_addable_blocks extends external_api {
/**
* Describes the parameters for execute.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters(
[
'pagecontextid' => new external_value(PARAM_INT, 'The context ID of the page.'),
'pagetype' => new external_value(PARAM_ALPHANUMEXT, 'The type of the page.'),
'pagelayout' => new external_value(PARAM_ALPHA, 'The layout of the page.'),
'subpage' => new external_value(PARAM_TEXT, 'The subpage identifier', VALUE_DEFAULT, ''),
'pagehash' => new external_value(PARAM_ALPHANUMEXT, 'Page hash', VALUE_DEFAULT, ''),
]
);
}
/**
* Fetch the addable blocks in a given page.
*
* @param int $pagecontextid The context ID of the page
* @param string $pagetype The type of the page
* @param string $pagelayout The layout of the page
* @param string $subpage The subpage identifier
* @param string $pagehash Page hash that can be provided instead of all parameters above
* @return array The blocks list
*/
public static function execute(int $pagecontextid, string $pagetype, string $pagelayout,
string $subpage = '', string $pagehash = ''): array {
global $PAGE;
$params = self::validate_parameters(self::execute_parameters(),
[
'pagecontextid' => $pagecontextid,
'pagetype' => $pagetype,
'pagelayout' => $pagelayout,
'subpage' => $subpage,
'pagehash' => $pagehash,
]
);
if ($params['pagehash']) {
// If pagehash is specified, all other parameters are ignored, all information
// about the page is stored in the session.
$page = \moodle_page::retrieve_edited_page($params['pagehash'], MUST_EXIST);
self::validate_context($page->context);
} else {
// For backward-compatibility and Mobile App instead of pagehash
// we can specify context, pagelayout, pagetype and subtype.
$context = \context::instance_by_id($params['pagecontextid']);
// Validate the context. This will also set the context in $PAGE.
self::validate_context($context);
// We need to manually set the page layout and page type.
$PAGE->set_pagelayout($params['pagelayout']);
$PAGE->set_pagetype($params['pagetype']);
$PAGE->set_subpage($params['subpage']);
$page = $PAGE;
}
// Firstly, we need to load all currently existing page blocks to later determine which blocks are addable.
$page->blocks->load_blocks(false);
$page->blocks->create_all_block_instances();
$addableblocks = $page->blocks->get_addable_blocks();
return array_map(function($block) use ($page) {
$classname = \block_manager::get_block_edit_form_class($block->name);
return [
'name' => $block->name,
'title' => get_string('pluginname', "block_{$block->name}"),
'blockform' => $classname::display_form_when_adding() ? $classname : null,
];
}, $addableblocks);
}
/**
* Describes the execute return value.
*
* @return external_multiple_structure
*/
public static function execute_returns(): external_multiple_structure {
return new external_multiple_structure(
new external_single_structure(
[
'name' => new external_value(PARAM_PLUGIN, 'The name of the block.'),
'title' => new external_value(PARAM_RAW, 'The title of the block.'),
'blockform' => new external_value(PARAM_RAW,
'If this block type has a form when it is being added then the classname of the form')
]
),
'List of addable blocks in a given page.'
);
}
}
@@ -0,0 +1,32 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_block\navigation\views;
/**
* Class secondary
*
* @package core_block
* @category navigation
*/
class secondary extends \core\navigation\views\secondary {
/**
* Blocks don't require secondary navs as they can be accessed from multiple places and in different contexts.
*/
public function initialise(): void {
}
}
+270
View File
@@ -0,0 +1,270 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Data provider.
*
* @package core_block
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <fred@branchup.tech>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_block\privacy;
defined('MOODLE_INTERNAL') || die();
use context;
use context_block;
use core_privacy\local\metadata\collection;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\transform;
use core_privacy\local\request\writer;
/**
* Data provider class.
*
* @package core_block
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <fred@branchup.tech>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\subsystem\provider,
\core_privacy\local\request\user_preference_provider,
\core_privacy\local\request\core_userlist_provider {
/**
* Returns metadata.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection): collection {
$collection->add_user_preference('blockIDhidden', 'privacy:metadata:userpref:hiddenblock');
$collection->add_user_preference('docked_block_instance_ID', 'privacy:metadata:userpref:dockedinstance');
return $collection;
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid The user to search.
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid(int $userid): \core_privacy\local\request\contextlist {
global $DB;
$contextlist = new \core_privacy\local\request\contextlist();
// Fetch the block instance IDs.
$likehidden = $DB->sql_like('name', ':hidden', false, false);
$likedocked = $DB->sql_like('name', ':docked', false, false);
$sql = "userid = :userid AND ($likehidden OR $likedocked)";
$params = [
'userid' => $userid,
'hidden' => 'block%hidden',
'docked' => 'docked_block_instance_%',
];
$prefs = $DB->get_fieldset_select('user_preferences', 'name', $sql, $params);
$instanceids = array_unique(array_map(function($prefname) {
if (preg_match('/^block(\d+)hidden$/', $prefname, $matches)) {
return $matches[1];
} else if (preg_match('/^docked_block_instance_(\d+)$/', $prefname, $matches)) {
return $matches[1];
}
return 0;
}, $prefs));
// Find the context of the instances.
if (!empty($instanceids)) {
list($insql, $inparams) = $DB->get_in_or_equal($instanceids, SQL_PARAMS_NAMED);
$sql = "
SELECT ctx.id
FROM {context} ctx
WHERE ctx.instanceid $insql
AND ctx.contextlevel = :blocklevel";
$params = array_merge($inparams, ['blocklevel' => CONTEXT_BLOCK]);
$contextlist->add_from_sql($sql, $params);
}
return $contextlist;
}
/**
* Get the list of users who have data within a context.
*
* @param \core_privacy\local\request\userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
*/
public static function get_users_in_context(\core_privacy\local\request\userlist $userlist) {
global $DB;
$context = $userlist->get_context();
if ($context->contextlevel != CONTEXT_BLOCK) {
return;
}
$params = ['docked' => 'docked_block_instance_' . $context->instanceid,
'hidden' => 'block' . $context->instanceid . 'hidden'];
$sql = "SELECT userid
FROM {user_preferences}
WHERE name = :hidden OR name = :docked";
$userlist->add_from_sql('userid', $sql, $params);
}
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
global $DB;
$userid = $contextlist->get_user()->id;
// Extract the block instance IDs.
$instanceids = array_reduce($contextlist->get_contexts(), function($carry, $context) {
if ($context->contextlevel == CONTEXT_BLOCK) {
$carry[] = $context->instanceid;
}
return $carry;
}, []);
if (empty($instanceids)) {
return;
}
// Query the blocks and their preferences.
list($insql, $inparams) = $DB->get_in_or_equal($instanceids, SQL_PARAMS_NAMED);
$hiddenkey = $DB->sql_concat("'block'", 'bi.id', "'hidden'");
$dockedkey = $DB->sql_concat("'docked_block_instance_'", 'bi.id');
$sql = "
SELECT bi.id, h.value AS prefhidden, d.value AS prefdocked
FROM {block_instances} bi
LEFT JOIN {user_preferences} h
ON h.userid = :userid1
AND h.name = $hiddenkey
LEFT JOIN {user_preferences} d
ON d.userid = :userid2
AND d.name = $dockedkey
WHERE bi.id $insql
AND (h.id IS NOT NULL
OR d.id IS NOT NULL)";
$params = array_merge($inparams, [
'userid1' => $userid,
'userid2' => $userid,
]);
// Export all the things.
$dockedstr = get_string('privacy:request:blockisdocked', 'core_block');
$hiddenstr = get_string('privacy:request:blockishidden', 'core_block');
$recordset = $DB->get_recordset_sql($sql, $params);
foreach ($recordset as $record) {
$context = context_block::instance($record->id);
if ($record->prefdocked !== null) {
writer::with_context($context)->export_user_preference(
'core_block',
'block_is_docked',
transform::yesno($record->prefdocked),
$dockedstr
);
}
if ($record->prefhidden !== null) {
writer::with_context($context)->export_user_preference(
'core_block',
'block_is_hidden',
transform::yesno($record->prefhidden),
$hiddenstr
);
}
}
$recordset->close();
}
/**
* Export all user preferences for the plugin.
*
* @param int $userid The userid of the user whose data is to be exported.
*/
public static function export_user_preferences(int $userid) {
// Our preferences aren't site-wide so they are exported in export_user_data.
}
/**
* Delete all data for all users in the specified context.
*
* @param context $context The specific context to delete data for.
*/
public static function delete_data_for_all_users_in_context(context $context) {
global $DB;
if ($context->contextlevel != CONTEXT_BLOCK) {
return;
}
// Delete the user preferences.
$instanceid = $context->instanceid;
$DB->delete_records_list('user_preferences', 'name', [
"block{$instanceid}hidden",
"docked_block_instance_{$instanceid}"
]);
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
global $DB;
$userid = $contextlist->get_user()->id;
$prefnames = array_reduce($contextlist->get_contexts(), function($carry, $context) {
if ($context->contextlevel == CONTEXT_BLOCK) {
$carry[] = "block{$context->instanceid}hidden";
$carry[] = "docked_block_instance_{$context->instanceid}";
}
return $carry;
}, []);
if (empty($prefnames)) {
return;
}
list($insql, $inparams) = $DB->get_in_or_equal($prefnames, SQL_PARAMS_NAMED);
$sql = "userid = :userid AND name $insql";
$params = array_merge($inparams, ['userid' => $userid]);
$DB->delete_records_select('user_preferences', $sql, $params);
}
/**
* Delete multiple users within a single context.
*
* @param \core_privacy\local\request\approved_userlist $userlist The approved context and user information to delete
* information for.
*/
public static function delete_data_for_users(\core_privacy\local\request\approved_userlist $userlist) {
global $DB;
$context = $userlist->get_context();
if ($context->contextlevel != CONTEXT_BLOCK) {
return;
}
list($insql, $params) = $DB->get_in_or_equal($userlist->get_userids(), SQL_PARAMS_NAMED);
$params['hidden'] = 'block' . $context->instanceid . 'hidden';
$params['docked'] = 'docked_block_instance_' . $context->instanceid;
$DB->delete_records_select('user_preferences', "(name = :hidden OR name = :docked) AND userid $insql", $params);
}
}