Skip to content

Commit

Permalink
MDL-82119 core_exemptions: Update exemptions API
Browse files Browse the repository at this point in the history
  • Loading branch information
Fragonite committed Aug 28, 2024
1 parent 00bee0f commit 0fb1310
Show file tree
Hide file tree
Showing 10 changed files with 443 additions and 673 deletions.
8 changes: 1 addition & 7 deletions exemptions/classes/local/entity/exemption.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ class exemption {
/** @var int $contextid the id of the context in which this exemption was created.*/
public $contextid;

/** @var int $ordering the ordering of the exemption within it's exemption area.*/
public $ordering;

/** @var int $timecreated the time at which the exemption was created.*/
public $timecreated;

Expand Down Expand Up @@ -70,17 +67,14 @@ class exemption {
* @param int $contextid the id of the context in which this exemption was created.
* @param string|null $reason the reason for the exemption.
* @param int|null $reasonformat the format of the reason for the exemption.
* @param int|null $usermodified the id of the user who created the exemption.
*/
public function __construct(string $component, string $itemtype, int $itemid, int $contextid,
?string $reason = null, ?int $reasonformat = null, ?int $usermodified = null) {
global $USER;
?string $reason = null, ?int $reasonformat = null) {
$this->component = $component;
$this->itemtype = $itemtype;
$this->itemid = $itemid;
$this->contextid = $contextid;
$this->reason = $reason;
$this->reasonformat = $reasonformat;
$this->usermodified = $usermodified ?? $USER->id;
}
}
25 changes: 19 additions & 6 deletions exemptions/classes/local/repository/exemption_repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ protected function get_exemption_from_record(\stdClass $record): exemption {
$record->contextid,
$record->reason,
$record->reasonformat,
$record->usermodified
);
$exemption->id = $record->id;
$exemption->ordering = $record->ordering ?? null;
$exemption->timecreated = $record->timecreated ?? null;
$exemption->timemodified = $record->timemodified ?? null;
$exemption->usermodified = $record->usermodified ?? null;

return $exemption;
}
Expand Down Expand Up @@ -91,7 +90,6 @@ protected function validate(exemption $exemption) {
'itemtype' => true,
'itemid' => true,
'contextid' => true,
'ordering' => false,
'timecreated' => false,
'timemodified' => false,
'usermodified' => false,
Expand Down Expand Up @@ -124,12 +122,13 @@ protected function validate(exemption $exemption) {
* @throws \moodle_exception if the exemption has missing or invalid properties.
*/
public function add(exemption $exemption): exemption {
global $DB;
global $DB, $USER;
$this->validate($exemption);
$exemption = (array)$exemption;
$time = time();
$exemption['timecreated'] = $time;
$exemption['timemodified'] = $time;
$exemption['usermodified'] = $USER->id;
$id = $DB->insert_record($this->exemptiontable, $exemption);
return $this->find($id);
}
Expand All @@ -143,13 +142,14 @@ public function add(exemption $exemption): exemption {
* @throws \moodle_exception if any of the exemptions have missing or invalid properties.
*/
public function add_all(array $exemptions): array {
global $DB;
global $DB, $USER;
$time = time();
foreach ($exemptions as $item) {
$this->validate($item);
$exemption = (array)$item;
$exemption['timecreated'] = $time;
$exemption['timemodified'] = $time;
$exemption['usermodified'] = $USER->id;
$ids[] = $DB->insert_record($this->exemptiontable, $exemption);
}
list($insql, $params) = $DB->get_in_or_equal($ids);
Expand Down Expand Up @@ -297,7 +297,20 @@ public function delete(int $id) {
*/
public function delete_by(array $criteria) {
global $DB;
$DB->delete_records($this->exemptiontable, $criteria);
$conditions = [];
$params = [];
foreach ($criteria as $field => $value) {
if (is_array($value) && count($value)) {
list($insql, $inparams) = $DB->get_in_or_equal($value, SQL_PARAMS_NAMED);
$conditions[] = "$field $insql";
$params = array_merge($params, $inparams);
} else {
$conditions[] = "$field = :$field";
$params = array_merge($params, [$field => $value]);
}
}

$DB->delete_records_select($this->exemptiontable, implode(' AND ', $conditions), $params);
}

/**
Expand Down
Loading

0 comments on commit 0fb1310

Please sign in to comment.