Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Sep 22, 2024
1 parent 8ea496b commit 7d0fcfe
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 5 deletions.
38 changes: 34 additions & 4 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|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)) {
Expand All @@ -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);
}
}
}

Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion classes/task/add_item.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions db/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
110 changes: 110 additions & 0 deletions tests/helper_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 7d0fcfe

Please sign in to comment.