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
+47
View File
@@ -0,0 +1,47 @@
<?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/>.
/**
* This file contains the dynamic interface.
*
* @package core_table
* @copyright 2020 Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
declare(strict_types=1);
namespace core_table;
/**
* Interface to identify this table as a table which can be dynamically updated via webservice calls.
*
* For a table to be defined as dynamic it must meet the following requirements:
*
* # it must be located with a namespaced class of \[component]\table\[tablename]
* # it must define a \core_table\local\filter\filterset implementation in \[component]\table\[tablename]_filterset
* # it must override the {{guess_base_url}} function and specify a base URL to be used when constructing URLs
* # it must override the {{get_context}} function to specify the correct context
*
* @package core_table
* @copyright 2020 Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
interface dynamic {
// Classes implementing this interface must define function `public has_capability(): bool`
// If it is not defined, the web service `core_table_get_dynamic_table_content` will check capability
// 'moodle/site:config' in the system context, allowing only admins to access the table data.
}
+290
View File
@@ -0,0 +1,290 @@
<?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_table\external\dynamic;
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;
use core_external\external_warnings;
/**
* Core table external functions.
*
* @package core_table
* @category external
* @copyright 2020 Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get extends external_api {
/**
* Describes the parameters for fetching the table html.
*
* @return external_function_parameters
* @since Moodle 3.9
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters ([
'component' => new external_value(
PARAM_COMPONENT,
'Component',
VALUE_REQUIRED
),
'handler' => new external_value(
// Note: We do not have a PARAM_CLASSNAME which would have been ideal.
PARAM_ALPHANUMEXT,
'Handler',
VALUE_REQUIRED
),
'uniqueid' => new external_value(
PARAM_ALPHANUMEXT,
'Unique ID for the container',
VALUE_REQUIRED
),
'sortdata' => new external_multiple_structure(
new external_single_structure([
'sortby' => new external_value(
PARAM_ALPHANUMEXT,
'The name of a sortable column',
VALUE_REQUIRED
),
'sortorder' => new external_value(
PARAM_ALPHANUMEXT,
'The direction that this column should be sorted by',
VALUE_REQUIRED
),
]),
'The combined sort order of the table. Multiple fields can be specified.',
VALUE_OPTIONAL,
[]
),
'filters' => new external_multiple_structure(
new external_single_structure([
'name' => new external_value(PARAM_ALPHANUM, 'Name of the filter', VALUE_REQUIRED),
'jointype' => new external_value(PARAM_INT, 'Type of join for filter values', VALUE_REQUIRED),
'values' => new external_multiple_structure(
new external_value(PARAM_RAW, 'Filter value'),
'The value to filter on',
VALUE_REQUIRED
),
'filteroptions' => new external_multiple_structure(
new external_single_structure([
'name' => new external_value(PARAM_ALPHANUM, 'Name of the filter option', VALUE_REQUIRED),
'value' => new external_value(PARAM_RAW, 'Value of the filter option', VALUE_REQUIRED),
]),
'Additional options for this filter',
VALUE_OPTIONAL,
),
]),
'The filters that will be applied in the request',
VALUE_OPTIONAL
),
'jointype' => new external_value(PARAM_INT, 'Type of join to join all filters together', VALUE_REQUIRED),
'firstinitial' => new external_value(
PARAM_RAW,
'The first initial to sort filter on',
VALUE_REQUIRED,
null
),
'lastinitial' => new external_value(
PARAM_RAW,
'The last initial to sort filter on',
VALUE_REQUIRED,
null
),
'pagenumber' => new external_value(
PARAM_INT,
'The page number',
VALUE_REQUIRED,
null
),
'pagesize' => new external_value(
PARAM_INT,
'The number of records per page',
VALUE_REQUIRED,
null
),
'hiddencolumns' => new external_multiple_structure(
new external_value(
PARAM_ALPHANUMEXT,
'Name of column',
VALUE_REQUIRED,
null
)
),
'resetpreferences' => new external_value(
PARAM_BOOL,
'Whether the table preferences should be reset',
VALUE_REQUIRED,
null
),
]);
}
/**
* External function to get the table view content.
*
* @param string $component The component.
* @param string $handler Dynamic table class name.
* @param string $uniqueid Unique ID for the container.
* @param array $sortdata The columns and order to sort by
* @param array $filters The filters that will be applied in the request.
* @param string $jointype The join type.
* @param string $firstinitial The first name initial to filter on
* @param string $lastinitial The last name initial to filter on
* @param int $pagenumber The page number.
* @param int $pagesize The number of records.
* @param string $jointype The join type.
* @param bool $resetpreferences Whether it is resetting table preferences or not.
*
* @return array
*/
public static function execute(
string $component,
string $handler,
string $uniqueid,
array $sortdata,
?array $filters = null,
?string $jointype = null,
?string $firstinitial = null,
?string $lastinitial = null,
?int $pagenumber = null,
?int $pagesize = null,
?array $hiddencolumns = null,
?bool $resetpreferences = null
) {
global $PAGE;
[
'component' => $component,
'handler' => $handler,
'uniqueid' => $uniqueid,
'sortdata' => $sortdata,
'filters' => $filters,
'jointype' => $jointype,
'firstinitial' => $firstinitial,
'lastinitial' => $lastinitial,
'pagenumber' => $pagenumber,
'pagesize' => $pagesize,
'hiddencolumns' => $hiddencolumns,
'resetpreferences' => $resetpreferences,
] = self::validate_parameters(self::execute_parameters(), [
'component' => $component,
'handler' => $handler,
'uniqueid' => $uniqueid,
'sortdata' => $sortdata,
'filters' => $filters,
'jointype' => $jointype,
'firstinitial' => $firstinitial,
'lastinitial' => $lastinitial,
'pagenumber' => $pagenumber,
'pagesize' => $pagesize,
'hiddencolumns' => $hiddencolumns,
'resetpreferences' => $resetpreferences,
]);
$tableclass = "\\{$component}\\table\\{$handler}";
if (!class_exists($tableclass)) {
throw new \UnexpectedValueException("Table handler class {$tableclass} not found. " .
"Please make sure that your table handler class is under the \\{$component}\\table namespace.");
}
if (!is_subclass_of($tableclass, \core_table\dynamic::class)) {
throw new \UnexpectedValueException("Table handler class {$tableclass} does not support dynamic updating.");
}
$filtersetclass = $tableclass::get_filterset_class();
if (!class_exists($filtersetclass)) {
throw new \UnexpectedValueException("The filter specified ({$filtersetclass}) is invalid.");
}
$filterset = new $filtersetclass();
$filterset->set_join_type($jointype);
foreach ($filters as $rawfilter) {
$filterset->add_filter_from_params(
$rawfilter['name'],
$rawfilter['jointype'],
$rawfilter['values']
);
}
$instance = new $tableclass($uniqueid);
$instance->set_filterset($filterset);
self::validate_context($instance->get_context());
if (!method_exists($instance, 'has_capability')) {
// Method \core_table\dynamic::has_capability() will be added in Moodle 4.5. Until then if it is not
// implemented, we will require the admin capability.
require_capability('moodle/site:config', \context_system::instance());
} else if (!$instance->has_capability()) {
throw new \moodle_exception('nopermissiontoaccesspage');
}
$instance->set_sortdata($sortdata);
$alphabet = get_string('alphabet', 'langconfig');
if ($firstinitial !== null && ($firstinitial === '' || strpos($alphabet, $firstinitial) !== false)) {
$instance->set_first_initial($firstinitial);
}
if ($lastinitial !== null && ($lastinitial === '' || strpos($alphabet, $lastinitial) !== false)) {
$instance->set_last_initial($lastinitial);
}
if ($pagenumber !== null) {
$instance->set_page_number($pagenumber);
}
if ($pagesize === null) {
$pagesize = 20;
}
if ($hiddencolumns !== null) {
$instance->set_hidden_columns($hiddencolumns);
}
if ($resetpreferences === true) {
$instance->mark_table_to_reset();
}
$PAGE->set_url($instance->baseurl);
ob_start();
$instance->out($pagesize, true);
$tablehtml = ob_get_contents();
ob_end_clean();
return [
'html' => $tablehtml,
'warnings' => []
];
}
/**
* Describes the data returned from the external function.
*
* @return external_single_structure
* @since Moodle 3.9
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'html' => new external_value(PARAM_RAW, 'The raw html of the requested table.'),
'warnings' => new external_warnings()
]);
}
}
+271
View File
@@ -0,0 +1,271 @@
<?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/>.
/**
* Table filterset.
*
* @package core
* @category table
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
declare(strict_types=1);
namespace core_table\local\filter;
use Countable;
use JsonSerializable;
use InvalidArgumentException;
use Iterator;
/**
* Class representing a generic filter of any type.
*
* @package core
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class filter implements Countable, Iterator, JsonSerializable {
/** @var int The default filter type (ANY) */
const JOINTYPE_DEFAULT = 1;
/** @var int None of the following match */
const JOINTYPE_NONE = 0;
/** @var int Any of the following match */
const JOINTYPE_ANY = 1;
/** @var int All of the following match */
const JOINTYPE_ALL = 2;
/** @var string The name of this filter */
protected $name = null;
/** @var int The join type currently in use */
protected $jointype = self::JOINTYPE_DEFAULT;
/** @var array The list of active filter values */
protected $filtervalues = [];
/** @var int[] valid join types */
protected $jointypes = [
self::JOINTYPE_NONE,
self::JOINTYPE_ANY,
self::JOINTYPE_ALL,
];
/** @var int The current iterator position */
protected $iteratorposition = null;
/**
* Constructor for the generic filter class.
*
* @param string $name The name of the current filter.
* @param int $jointype The join to use when combining the filters.
* See the JOINTYPE_ constants for further information on the field.
* @param mixed[] $values An array of filter objects to be applied.
*/
public function __construct(string $name, ?int $jointype = null, ?array $values = null) {
$this->name = $name;
if ($jointype !== null) {
$this->set_join_type($jointype);
}
if (!empty($values)) {
foreach ($values as $value) {
$this->add_filter_value($value);
}
}
}
/**
* Reset the iterator position.
*/
public function reset_iterator(): void {
$this->iteratorposition = null;
}
/**
* Return the current filter value.
*/
#[\ReturnTypeWillChange]
public function current() {
if ($this->iteratorposition === null) {
$this->rewind();
}
if ($this->iteratorposition === null) {
return null;
}
return $this->filtervalues[$this->iteratorposition];
}
/**
* Returns the current position of the iterator.
*
* @return int
*/
#[\ReturnTypeWillChange]
public function key() {
if ($this->iteratorposition === null) {
$this->rewind();
}
return $this->iteratorposition;
}
/**
* Rewind the Iterator position to the start.
*/
public function rewind(): void {
if ($this->iteratorposition === null) {
$this->sort_filter_values();
}
if (count($this->filtervalues)) {
$this->iteratorposition = 0;
}
}
/**
* Move to the next value in the list.
*/
public function next(): void {
++$this->iteratorposition;
}
/**
* Check if the current position is valid.
*
* @return bool
*/
public function valid(): bool {
return isset($this->filtervalues[$this->iteratorposition]);
}
/**
* Return the number of contexts.
*
* @return int
*/
public function count(): int {
return count($this->filtervalues);
}
/**
* Return the name of the filter.
*
* @return string
*/
public function get_name(): string {
return $this->name;
}
/**
* Specify the type of join to employ for the filter.
*
* @param int $jointype The join type to use using one of the supplied constants
* @return self
*/
public function set_join_type(int $jointype): self {
if (array_search($jointype, $this->jointypes) === false) {
throw new InvalidArgumentException('Invalid join type specified');
}
$this->jointype = $jointype;
return $this;
}
/**
* Return the currently specified join type.
*
* @return int
*/
public function get_join_type(): int {
return $this->jointype;
}
/**
* Add a value to the filter.
*
* @param mixed $value
* @return self
*/
public function add_filter_value($value): self {
if ($value === null) {
// Null values are usually invalid.
return $this;
}
if ($value === '') {
// Empty strings are invalid.
return $this;
}
if (array_search($value, $this->filtervalues) !== false) {
// Remove duplicates.
return $this;
}
$this->filtervalues[] = $value;
// Reset the iterator position.
$this->reset_iterator();
return $this;
}
/**
* Sort the filter values to ensure reliable, and consistent output.
*/
protected function sort_filter_values(): void {
// Sort the filter values to ensure consistent output.
// Note: This is not a locale-aware sort, but we don't need this.
// It's primarily for consistency, not for actual sorting.
sort($this->filtervalues);
$this->reset_iterator();
}
/**
* Return the current filter values.
*
* @return mixed[]
*/
public function get_filter_values(): array {
$this->sort_filter_values();
return $this->filtervalues;
}
/**
* Serialize filter.
*
* @return mixed|object
*/
#[\ReturnTypeWillChange]
public function jsonSerialize() {
return (object) [
'name' => $this->get_name(),
'jointype' => $this->get_join_type(),
'values' => $this->get_filter_values(),
];
}
}
@@ -0,0 +1,305 @@
<?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/>.
/**
* Table filterset.
*
* @package core
* @category table
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
declare(strict_types=1);
namespace core_table\local\filter;
use InvalidArgumentException;
use JsonSerializable;
use UnexpectedValueException;
use moodle_exception;
/**
* Class representing a set of filters.
*
* @package core
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class filterset implements JsonSerializable {
/** @var int The default filter type (ALL) */
const JOINTYPE_DEFAULT = 2;
/** @var int None of the following match */
const JOINTYPE_NONE = 0;
/** @var int Any of the following match */
const JOINTYPE_ANY = 1;
/** @var int All of the following match */
const JOINTYPE_ALL = 2;
/** @var int The join type currently in use */
protected $jointype = null;
/** @var array The list of combined filter types */
protected $filtertypes = null;
/** @var array The list of active filters */
protected $filters = [];
/** @var int[] valid join types */
protected $jointypes = [
self::JOINTYPE_NONE,
self::JOINTYPE_ANY,
self::JOINTYPE_ALL,
];
/**
* Specify the type of join to employ for the filter.
*
* @param int $jointype The join type to use using one of the supplied constants
* @return self
*/
public function set_join_type(int $jointype): self {
if (array_search($jointype, $this->jointypes) === false) {
throw new InvalidArgumentException('Invalid join type specified');
}
$this->jointype = $jointype;
return $this;
}
/**
* Return the currently specified join type.
*
* @return int
*/
public function get_join_type(): int {
if ($this->jointype === null) {
$this->jointype = self::JOINTYPE_DEFAULT;
}
return $this->jointype;
}
/**
* Add the specified filter.
*
* @param filter $filter
* @return self
*/
public function add_filter(filter $filter): self {
$filtername = $filter->get_name();
if (array_key_exists($filtername, $this->filters)) {
// This filter already exists.
if ($this->filters[$filtername] === $filter) {
// This is the same value as already added.
// Just ignore it.
return $this;
}
// This is a different value to last time. Fail as this is not supported.
throw new UnexpectedValueException(
"A filter of type '{$filtername}' has already been added. Check that you have the correct filter."
);
}
// Ensure that the filter is both known, and is of the correct type.
$validtypes = $this->get_all_filtertypes();
if (!array_key_exists($filtername, $validtypes)) {
// Unknown filter.
throw new InvalidArgumentException(
"The filter '{$filtername}' was not recognised."
);
}
// Check that the filter is of the correct type.
if (!is_a($filter, $validtypes[$filtername])) {
$actualtype = get_class($filter);
$requiredtype = $validtypes[$filtername];
throw new InvalidArgumentException(
"The filter '{$filtername}' was incorrectly specified as a {$actualtype}. It must be a {$requiredtype}."
);
}
// All good. Add the filter.
$this->filters[$filtername] = $filter;
return $this;
}
/**
* Add the specified filter from the supplied params.
*
* @param string $filtername The name of the filter to create
* @param mixed[] ...$args Additional arguments used to create this filter type
* @return self
*/
public function add_filter_from_params(string $filtername, ...$args): self {
// Fetch the list of valid filters by name.
$validtypes = $this->get_all_filtertypes();
if (!array_key_exists($filtername, $validtypes)) {
// Unknown filter.
throw new InvalidArgumentException(
"The filter '{$filtername}' was not recognised."
);
}
$filterclass = $validtypes[$filtername];
if (!class_exists($filterclass)) {
// Filter class cannot be class autoloaded.
throw new InvalidArgumentException(
"The filter class '{$filterclass}' for filter '{$filtername}' could not be found."
);
}
// Pass all supplied arguments to the constructor when adding a new filter.
// This allows for a wider definition of the the filter in child classes.
$this->add_filter(new $filterclass($filtername, ...$args));
return $this;
}
/**
* Return the current set of filters.
*
* @return filter[]
*/
public function get_filters(): array {
// Sort the filters by their name to ensure consistent output.
// Note: This is not a locale-aware sort, but we don't need this.
// It's primarily for consistency, not for actual sorting.
asort($this->filters);
return $this->filters;
}
/**
* Check whether the filter has been added or not.
*
* @param string $filtername
* @return bool
*/
public function has_filter(string $filtername): bool {
// We do not check if the filtername is valid, only that it exists.
// This is an existence check and there is no benefit to doing any more.
return array_key_exists($filtername, $this->filters);
}
/**
* Get the named filter.
*
* @param string $filtername
* @return filter
*/
public function get_filter(string $filtername): filter {
if (!array_key_exists($filtername, $this->get_all_filtertypes())) {
throw new UnexpectedValueException("The filter specified ({$filtername}) is invalid.");
}
if (!array_key_exists($filtername, $this->filters)) {
throw new UnexpectedValueException("The filter specified ({$filtername}) has not been created.");
}
return $this->filters[$filtername];
}
/**
* Confirm whether the filter has been correctly specified.
*
* @throws moodle_exception
*/
public function check_validity(): void {
// Ensure that all required filters are present.
$missing = [];
foreach (array_keys($this->get_required_filters()) as $filtername) {
if (!array_key_exists($filtername, $this->filters)) {
$missing[] = $filtername;
}
}
if (!empty($missing)) {
throw new moodle_exception(
'missingrequiredfields',
'core_table',
'',
implode(get_string('listsep', 'langconfig') . ' ', $missing)
);
}
}
/**
* Get the list of required filters in an array of filtername => filter class type.
*
* @return array
*/
protected function get_required_filters(): array {
return [];
}
/**
* Get the list of optional filters in an array of filtername => filter class type.
*
* @return array
*/
protected function get_optional_filters(): array {
return [];
}
/**
* Get all filter valid types in an array of filtername => filter class type.
*
* @return array
*/
public function get_all_filtertypes(): array {
if ($this->filtertypes === null) {
$required = $this->get_required_filters();
$optional = $this->get_optional_filters();
$conflicts = array_keys(array_intersect_key($required, $optional));
if (!empty($conflicts)) {
throw new InvalidArgumentException(
"Some filter types are both required, and optional: " . implode(', ', $conflicts)
);
}
$this->filtertypes = array_merge($required, $optional);
asort($this->filtertypes);
}
return $this->filtertypes;
}
/**
* Serialize filterset.
*
* @return mixed|object
*/
#[\ReturnTypeWillChange]
public function jsonSerialize() {
return (object) [
'jointype' => $this->get_join_type(),
'filters' => $this->get_filters(),
];
}
}
@@ -0,0 +1,65 @@
<?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/>.
/**
* Integer filter.
*
* @package core
* @category table
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
declare(strict_types=1);
namespace core_table\local\filter;
use TypeError;
/**
* Class representing an integer filter.
*
* @package core
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class integer_filter extends filter {
/**
* Add a value to the filter.
*
* @param int $values
* @return self
*/
public function add_filter_value($value): parent {
if (!is_int($value)) {
$type = gettype($value);
if ($type === 'object') {
$type = get_class($value);
}
throw new TypeError("The value supplied was of type '{$type}'. An integer was expected.");
}
if (array_search($value, $this->filtervalues) !== false) {
// Remove duplicates.
return $this;
}
$this->filtervalues[] = $value;
return $this;
}
}
@@ -0,0 +1,133 @@
<?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/>.
/**
* Integer comparison filter to allow a comparison such as "> 42".
*
* @package core
* @category table
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
declare(strict_types=1);
namespace core_table\local\filter;
use InvalidArgumentException;
use TypeError;
/**
* Class representing an integer filter.
*
* @package core
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class numeric_comparison_filter extends filter {
/**
* Get the authoritative direction.
*
* @param string $direction The supplied direction
* @return string The authoritative direction
*/
protected function get_direction(string $direction): string {
$validdirections = [
'=' => '==',
'==' => '==',
'===' => '===',
'>' => '>',
'=>' => '=>',
'<' => '<',
'<=' => '<=',
];
if (!array_key_exists($direction, $validdirections)) {
throw new InvalidArgumentException("Invalid direction specified '{$direction}'.");
}
return $validdirections[$direction];
}
/**
* Add a value to the filter.
*
* @param string $value A json-encoded array containing a direction, and comparison value
* @return self
*/
public function add_filter_value($value): parent {
if (!is_string($value)) {
$type = gettype($value);
if ($type === 'object') {
$type = get_class($value);
}
throw new TypeError(
"The value supplied was of type '{$type}'. A string representing a json-encoded value was expected."
);
}
$data = json_decode($value);
if ($data === null) {
throw new InvalidArgumentException(
"A json-encoded object containing both a direction, and comparison value was expected."
);
}
if (!is_object($data)) {
$type = gettype($value);
throw new InvalidArgumentException(
"The value supplied was a json encoded '{$type}'. " .
"An object containing both a direction, and comparison value was expected."
);
}
if (!property_exists($data, 'direction')) {
throw new InvalidArgumentException("A 'direction' must be provided.");
}
$direction = $this->get_direction($data->direction);
if (!property_exists($data, 'value')) {
throw new InvalidArgumentException("A 'value' must be provided.");
}
$value = $data->value;
if (!is_numeric($value)) {
$type = gettype($value);
if ($type === 'object') {
$type = get_class($value);
}
throw new TypeError("The value supplied was of type '{$type}'. A numeric value was expected.");
}
$fullvalue = (object) [
'direction' => $direction,
'value' => $value,
];
if (array_search($fullvalue, $this->filtervalues) !== false) {
// Remove duplicates.
return $this;
}
$this->filtervalues[] = $fullvalue;
return $this;
}
}
@@ -0,0 +1,65 @@
<?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/>.
/**
* String filter.
*
* @package core
* @category table
* @copyright 2020 Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
declare(strict_types=1);
namespace core_table\local\filter;
use TypeError;
/**
* Class representing a string filter.
*
* @package core
* @copyright 2020 Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class string_filter extends filter {
/**
* Add a value to the filter.
*
* @param string $values
* @return self
*/
public function add_filter_value($value): parent {
if (!is_string($value)) {
$type = gettype($value);
if ($type === 'object') {
$type = get_class($value);
}
throw new TypeError("The value supplied was of type '{$type}'. A string was expected.");
}
if (array_search($value, $this->filtervalues) !== false) {
// Remove duplicates.
return $this;
}
$this->filtervalues[] = $value;
return $this;
}
}
+50
View File
@@ -0,0 +1,50 @@
<?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/>.
/**
* Privacy Subsystem for core_table API.
*
* @package core_table
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
declare(strict_types=1);
namespace core_table\privacy;
defined('MOODLE_INTERNAL') || die();
use core_privacy\local\metadata\null_provider;
/**
* Privacy Subsystem for core_table API.
*
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}