From 7d0fcfe6988f806abcd2d20f5cf6aa90af9019f7 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Sun, 22 Sep 2024 11:33:32 +1000 Subject: [PATCH] WIP --- classes/helper.php | 38 +++++++++++-- classes/task/add_item.php | 2 +- db/install.php | 1 + tests/helper_test.php | 110 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+), 5 deletions(-) diff --git a/classes/helper.php b/classes/helper.php index dac38fc..eef1b4a 100644 --- a/classes/helper.php +++ b/classes/helper.php @@ -53,6 +53,11 @@ class helper { */ public const ITEM_TYPE_COURSE = 'course'; + /** + * Preset item type. + */ + public const ITEM_TYPE_PRESET = 'preset'; + /** * Field shortname */ @@ -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|null $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 = null): void { $cohort = self::get_cohort_by_item($itemid, $itemtype); if (empty($cohort)) { @@ -111,8 +116,10 @@ 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) { + self::add_enrolment_method($course, $cohort); + } } } @@ -565,4 +572,27 @@ public static function get_categories(): array { return $DB->get_records('course_categories', ['visible' => 1], 'name'); } + + 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); + } + + 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); + } } diff --git a/classes/task/add_item.php b/classes/task/add_item.php index 25cfa34..a5db5f4 100644 --- a/classes/task/add_item.php +++ b/classes/task/add_item.php @@ -45,7 +45,7 @@ public function execute() { if (!empty($data->courseid)) { $course = get_course($data->courseid); } - helper::add_item($data->itemid, $data->itemtype, $data->itemname, $course); + helper::add_item($data->itemid, $data->itemtype, $data->itemname, [$course]); $transaction->allow_commit(); } catch (Exception $exception) { $transaction->rollback($exception); diff --git a/db/install.php b/db/install.php index d5ab991..17affe1 100644 --- a/db/install.php +++ b/db/install.php @@ -44,6 +44,7 @@ function xmldb_tool_enrolprofile_install() { helper::ITEM_TYPE_COURSE => 'Course', helper::ITEM_TYPE_CATEGORY => 'Category', helper::ITEM_TYPE_TAG => 'Tag', + helper::ITEM_TYPE_PRESET => 'Preset', ]; foreach ($profilefields as $shortname => $name) { install_helper::add_user_profile_field( diff --git a/tests/helper_test.php b/tests/helper_test.php index 3108001..f30d026 100644 --- a/tests/helper_test.php +++ b/tests/helper_test.php @@ -17,7 +17,9 @@ namespace tool_enrolprofile; use advanced_testcase; +use core\context\course; use core_customfield\field_controller; +use core_tag_tag; /** * Unit tests for helper class. @@ -88,4 +90,112 @@ public function test_get_cohort_by_item(): void { $this->assertEquals($cohort2->id, helper::get_cohort_by_item(13, 'tag')->id); $this->assertEquals($cohort3->id, helper::get_cohort_by_item(13, 'course')->id); } + + /** + * Test getting courses by categories. + */ + public function test_get_courses_by_categories() { + $this->resetAfterTest(); + + $category1 = $this->getDataGenerator()->create_category(); + $category2 = $this->getDataGenerator()->create_category(); + $category3 = $this->getDataGenerator()->create_category(); + + $course11 = $this->getDataGenerator()->create_course(['category' => $category1->id]); + $course12 = $this->getDataGenerator()->create_course(['category' => $category1->id]); + $course13 = $this->getDataGenerator()->create_course(['category' => $category1->id]); + + $course21 = $this->getDataGenerator()->create_course(['category' => $category2->id]); + $course22 = $this->getDataGenerator()->create_course(['category' => $category2->id]); + + $courses = helper::get_courses_by_categories([$category1->id]); + $this->assertCount(3, $courses); + $this->assertArrayHasKey($course11->id, $courses); + $this->assertArrayHasKey($course12->id, $courses); + $this->assertArrayHasKey($course13->id, $courses); + + $courses = helper::get_courses_by_categories([$category2->id]); + $this->assertCount(2, $courses); + $this->assertArrayHasKey($course21->id, $courses); + $this->assertArrayHasKey($course22->id, $courses); + + $courses = helper::get_courses_by_categories([$category3->id]); + $this->assertCount(0, $courses); + + $courses = helper::get_courses_by_categories([$category2->id, $category1->id]); + $this->assertCount(5, $courses); + $this->assertArrayHasKey($course11->id, $courses); + $this->assertArrayHasKey($course12->id, $courses); + $this->assertArrayHasKey($course13->id, $courses); + $this->assertArrayHasKey($course21->id, $courses); + $this->assertArrayHasKey($course12->id, $courses); + } + + /** + * Test getting courses by tags. + */ + public function test_get_courses_by_tags() { + global $DB; + + $this->resetAfterTest(); + + $course1 = $this->getDataGenerator()->create_course(); + $course2 = $this->getDataGenerator()->create_course(); + $course3 = $this->getDataGenerator()->create_course(); + $course4 = $this->getDataGenerator()->create_course(); + $course5 = $this->getDataGenerator()->create_course(); + + core_tag_tag::set_item_tags('core', 'course', $course1->id, course::instance($course1->id), [ + 'tag1', + 'tag2', + 'tag3', + 'tag4', + ]); + + core_tag_tag::set_item_tags('core', 'course', $course2->id, course::instance($course2->id), [ + 'tag2', + 'tag4', + ]); + + core_tag_tag::set_item_tags('core', 'course', $course3->id, course::instance($course3->id), [ + 'tag1', + 'tag2', + 'tag4', + ]); + + core_tag_tag::set_item_tags('core', 'course', $course4->id, course::instance($course4->id), [ + 'tag5', + ]); + + $tag1 = $DB->get_record('tag', ['rawname' => 'tag1']); + $tag2 = $DB->get_record('tag', ['rawname' => 'tag2']); + $tag3 = $DB->get_record('tag', ['rawname' => 'tag3']); + $tag4 = $DB->get_record('tag', ['rawname' => 'tag4']); + $tag5 = $DB->get_record('tag', ['rawname' => 'tag5']); + + $courses = helper::get_courses_by_tags([77777]); + $this->assertCount(0, $courses); + + $courses = helper::get_courses_by_tags([$tag1->id]); + $this->assertCount(2, $courses); + $this->assertArrayHasKey($course1->id, $courses); + $this->assertArrayHasKey($course3->id, $courses); + + $courses = helper::get_courses_by_tags([$tag1->id, $tag2->id]); + $this->assertCount(3, $courses); + $this->assertArrayHasKey($course1->id, $courses); + $this->assertArrayHasKey($course2->id, $courses); + $this->assertArrayHasKey($course3->id, $courses); + + $courses = helper::get_courses_by_tags([$tag4->id, $tag5->id]); + $this->assertCount(4, $courses); + $this->assertArrayHasKey($course1->id, $courses); + $this->assertArrayHasKey($course2->id, $courses); + $this->assertArrayHasKey($course3->id, $courses); + $this->assertArrayHasKey($course4->id, $courses); + + $courses = helper::get_courses_by_tags([$tag3->id]); + $this->assertCount(1, $courses); + $this->assertArrayHasKey($course1->id, $courses); + } }