Skip to content

Commit

Permalink
Merge pull request #29 from dmitriim/issue8-processing
Browse files Browse the repository at this point in the history
issue #8: process presets
  • Loading branch information
dmitriim authored Sep 26, 2024
2 parents 8ea496b + b5338de commit f6ce246
Show file tree
Hide file tree
Showing 15 changed files with 1,192 additions and 15 deletions.
82 changes: 82 additions & 0 deletions classes/event/preset_created.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.

namespace tool_enrolprofile\event;

use core\event\base;
use moodle_url;

/**
* Event triggered when a preset created.
*
* @package tool_enrolprofile
* @copyright 2024 Dmitrii Metelkin <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class preset_created extends base {

/**
* Initialise the rule data.
*/
protected function init() {
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['crud'] = 'c';
}

/**
* Return localised event name.
*
* @return string
*/
public static function get_name(): string {
return get_string('event:preset_created', 'tool_enrolprofile');
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description(): string {
return "User with id '{$this->userid}' created preset with id '{$this->other['presetid']}'";
}

/**
* Get URL related to the action.
*
* @return moodle_url
*/
public function get_url(): moodle_url {
return new moodle_url("/admin/tool/enrolprofile/index.php", ['id' => $this->other['presetid']]);
}

/**
* Validates the custom data.
*
* @throws \coding_exception if missing required data.
*/
protected function validate_data() {
parent::validate_data();

if (!isset($this->other['presetid'])) {
throw new \coding_exception('The \'presetid\' value must be set in other.');
}

if (!isset($this->other['presetname'])) {
throw new \coding_exception('The \'presetname\' value must be set in other.');
}
}
}
82 changes: 82 additions & 0 deletions classes/event/preset_deleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.

namespace tool_enrolprofile\event;

use core\event\base;
use moodle_url;

/**
* Event triggered when a preset deleted.
*
* @package tool_enrolprofile
* @copyright 2024 Dmitrii Metelkin <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class preset_deleted extends base {

/**
* Initialise the rule data.
*/
protected function init() {
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['crud'] = 'd';
}

/**
* Return localised event name.
*
* @return string
*/
public static function get_name(): string {
return get_string('event:preset_deleted', 'tool_enrolprofile');
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description(): string {
return "User with id '{$this->userid}' deleted preset with id '{$this->other['presetid']}'";
}

/**
* Get URL related to the action.
*
* @return moodle_url
*/
public function get_url(): moodle_url {
return new moodle_url("/admin/tool/enrolprofile/index.php", ['id' => $this->other['presetid']]);
}

/**
* Validates the custom data.
*
* @throws \coding_exception if missing required data.
*/
protected function validate_data() {
parent::validate_data();

if (!isset($this->other['presetid'])) {
throw new \coding_exception('The \'presetid\' value must be set in other.');
}

if (!isset($this->other['presetname'])) {
throw new \coding_exception('The \'presetname\' value must be set in other.');
}
}
}
82 changes: 82 additions & 0 deletions classes/event/preset_updated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.

namespace tool_enrolprofile\event;

use core\event\base;
use moodle_url;

/**
* Event triggered when a preset updated.
*
* @package tool_enrolprofile
* @copyright 2024 Dmitrii Metelkin <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class preset_updated extends base {

/**
* Initialise the rule data.
*/
protected function init() {
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['crud'] = 'u';
}

/**
* Return localised event name.
*
* @return string
*/
public static function get_name(): string {
return get_string('event:preset_updated', 'tool_enrolprofile');
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description(): string {
return "User with id '{$this->userid}' updated preset with id '{$this->other['presetid']}'";
}

/**
* Get URL related to the action.
*
* @return moodle_url
*/
public function get_url(): moodle_url {
return new moodle_url("/admin/tool/enrolprofile/index.php", ['id' => $this->other['presetid']]);
}

/**
* Validates the custom data.
*
* @throws \coding_exception if missing required data.
*/
protected function validate_data() {
parent::validate_data();

if (!isset($this->other['presetid'])) {
throw new \coding_exception('The \'presetid\' value must be set in other.');
}

if (!isset($this->other['presetname'])) {
throw new \coding_exception('The \'presetname\' value must be set in other.');
}
}
}
11 changes: 11 additions & 0 deletions classes/external/delete_preset.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

namespace tool_enrolprofile\external;

use core\context\system;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_value;
use invalid_parameter_exception;
use tool_enrolprofile\event\preset_deleted;
use tool_enrolprofile\preset;

/**
Expand Down Expand Up @@ -57,7 +59,16 @@ public static function execute(int $id): void {
throw new invalid_parameter_exception('Invalid preset');
}

$presetid = $preset->get('id');
$preset->delete();

preset_deleted::create([
'context' => system::instance(),
'other' => [
'presetid' => $presetid,
'presetname' => $preset->get('name'),
]
])->trigger();
}

/**
Expand Down
61 changes: 53 additions & 8 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class helper {
*/
public const ITEM_TYPE_COURSE = 'course';

/**
* Preset item type.
*/
public const ITEM_TYPE_PRESET = 'preset';

/**
* Field shortname
*/
Expand Down Expand Up @@ -84,10 +89,10 @@ class helper {
* @param int $itemid Item ID number
* @param string $itemtype Item type (tag, course, category).
* @param string $itemname Item name.
* @param stdClass|null $course Course to set up enrolment method. If not set, the no enrolment method will be created.
* @param array $courses Course to set up enrolment method. If not set, the no enrolment method will be created.
* @return void
*/
public static function add_item(int $itemid, string $itemtype, string $itemname, ?stdClass $course = null): void {
public static function add_item(int $itemid, string $itemtype, string $itemname, array $courses = []): void {
$cohort = self::get_cohort_by_item($itemid, $itemtype);

if (empty($cohort)) {
Expand All @@ -111,8 +116,12 @@ public static function add_item(int $itemid, string $itemtype, string $itemname,
self::add_profile_field_item($itemtype, $itemname);

// Create enrolment method for the cohort for a given course.
if (!empty($course)) {
self::add_enrolment_method($course, $cohort);
if (!empty($courses)) {
foreach ($courses as $course) {
if (!empty($course)) {
self::add_enrolment_method($course, $cohort);
}
}
}
}

Expand Down Expand Up @@ -515,7 +524,7 @@ public static function update_course_category(int $courseid, int $newcategoryid)
*/
public static function validate_task_custom_data(stdClass $data, array $fields = ['itemid', 'itemtype', 'itemname']): void {
foreach ($fields as $field) {
if (empty($data->$field)) {
if (!object_property_exists($data, $field)) {
throw new coding_exception('Missing required field: ' . $field);
}
}
Expand All @@ -538,9 +547,10 @@ public static function get_course_tags(int $courseid = 0): array {
}

$sql = "SELECT DISTINCT t.id, t.rawname
FROM {tag} t
JOIN {tag_instance} ti ON t.id = ti.tagid
WHERE ti.itemtype = 'course' $where ORDER BY t.id, t.rawname";
FROM {tag} t
JOIN {tag_instance} ti ON t.id = ti.tagid
WHERE ti.itemtype = 'course' $where
ORDER BY t.id, t.rawname";
return $DB->get_records_sql($sql, $params);
}

Expand All @@ -565,4 +575,39 @@ public static function get_categories(): array {

return $DB->get_records('course_categories', ['visible' => 1], 'name');
}

/**
* Return a list of courses for a given categories.
*
* @param array $categoryids A list of category IDs.
* @return array
*/
public static function get_courses_by_categories(array $categoryids): array {
global $DB;

list($sql, $params) = $DB->get_in_or_equal($categoryids, SQL_PARAMS_NAMED);
$select = 'category ' . $sql;

return $DB->get_records_select('course', $select, $params);
}

/**
* Return a list of courses for a given tags.
*
* @param array $tagids A list of tag IDs.
* @return array
*/
public static function get_courses_by_tags(array $tagids): array {
global $DB;

list($select, $params) = $DB->get_in_or_equal($tagids, SQL_PARAMS_NAMED);
$where = 'ti.tagid '. $select;

$sql = "SELECT DISTINCT c.*
FROM {course} c
JOIN {tag_instance} ti ON ti.itemid = c.id
WHERE ti.itemtype = 'course' AND $where";

return $DB->get_records_sql($sql, $params);
}
}
Loading

0 comments on commit f6ce246

Please sign in to comment.