Skip to content

Commit

Permalink
issue #6: process a new category
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Sep 4, 2024
1 parent 1819a94 commit cb6588a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 16 deletions.
14 changes: 9 additions & 5 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@ class helper {
/**
* Set up configuration item.
*
* @param stdClass $course Course to set up it for.
* @param int $itemid Item ID number
* @param string $itemtype Item type (tag, course, category)/
* @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.
* @return void
*/
public static function set_up_item(stdClass $course, int $itemid, string $itemtype, string $itemname): void {
public static function set_up_item(int $itemid, string $itemtype, string $itemname, ?stdClass $course = null): void {
$cohort = self::get_cohort_by_item($itemid, $itemtype);

if (empty($cohort)) {
Expand All @@ -105,8 +106,11 @@ public static function set_up_item(stdClass $course, int $itemid, string $itemty
self::add_rule($cohort, $itemtype);
// Add a tag to a custom profile field.
self::update_profile_field($itemtype, $itemname);
// If yes, create enrolment method for the cohort for a given course.
self::add_enrolment_method($course, $cohort);

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

/**
Expand Down
19 changes: 16 additions & 3 deletions classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace tool_enrolprofile;

use core\event\course_category_created;
use core\event\course_created;
use core\event\tag_added;

Expand Down Expand Up @@ -44,7 +45,7 @@ public static function tag_added(tag_added $event): void {
$tagname = $event->other['tagrawname'];
$course = get_course($event->other['itemid']);

helper::set_up_item($course, $tagid, 'tag', $tagname);
helper::set_up_item($tagid, helper::ITEM_TYPE_TAG, $tagname, $course);
}

/**
Expand All @@ -56,9 +57,21 @@ public static function course_created(course_created $event): void {
global $DB;

$course = get_course($event->courseid);
helper::set_up_item($course, $course->id, 'course', $course->{helper::COURSE_NAME});
helper::set_up_item($course->id, helper::ITEM_TYPE_COURSE, $course->{helper::COURSE_NAME}, $course);

$category = $DB->get_record('course_categories', ['id' => $course->category]);
helper::set_up_item($course, $category->id, 'category', $category->name);
helper::set_up_item($category->id, helper::ITEM_TYPE_CATEGORY, $category->name, $course);
}

/**
* Process course_category_created event.
*
* @param course_category_created $event The event.
*/
public static function course_category_created(course_category_created $event): void {
global $DB;

$category = $DB->get_record('course_categories', ['id' => $event->objectid]);
helper::set_up_item($category->id, helper::ITEM_TYPE_CATEGORY, $category->name);
}
}
4 changes: 4 additions & 0 deletions db/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@
'eventname' => '\core\event\course_created',
'callback' => '\tool_enrolprofile\observer::course_created',
],
[
'eventname' => '\core\event\course_category_created',
'callback' => '\tool_enrolprofile\observer::course_category_created',
],
];
52 changes: 44 additions & 8 deletions tests/observer_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ protected function create_cohort_custom_field(string $shortname, string $datatyp

/**
* Check logic when adding a tag.
* @return void
*/
public function test_tag_added() {
public function test_tag_added(): void {
global $DB;

$course = $this->getDataGenerator()->create_course();
Expand Down Expand Up @@ -155,7 +154,7 @@ public function test_tag_added() {
$this->assertSame($tag->id, $customfield->export_value());
}
if ($customfield->get_field()->get('shortname') == helper::COHORT_FIELD_TYPE) {
$this->assertSame('tag', $customfield->export_value());
$this->assertSame(helper::ITEM_TYPE_TAG, $customfield->export_value());
}
}

Expand All @@ -177,15 +176,13 @@ public function test_tag_added() {

/**
* Check logic when creating a course.
* @return void
*/
public function test_course_created() {
public function test_course_created(): void {
global $DB;

$coursename = 'Course name';
$this->assertEmpty($DB->get_record('cohort', ['name' => $coursename]));
$this->assertEmpty($DB->get_field('user_info_field', 'param1', ['id' => $this->courseprofilefield->id]));
$this->assertEmpty($DB->get_record('tag', ['rawname' => $coursename]));
$this->assertEmpty($DB->get_record('tool_dynamic_cohorts', ['name' => $coursename]));

$course = $this->getDataGenerator()->create_course(['fullname' => $coursename]);
Expand All @@ -203,7 +200,7 @@ public function test_course_created() {
$this->assertSame($course->id, $customfield->export_value());
}
if ($customfield->get_field()->get('shortname') == helper::COHORT_FIELD_TYPE) {
$this->assertSame('course', $customfield->export_value());
$this->assertSame(helper::ITEM_TYPE_COURSE, $customfield->export_value());
}
}

Expand Down Expand Up @@ -232,7 +229,7 @@ public function test_course_created() {
$this->assertSame($category->id, $customfield->export_value());
}
if ($customfield->get_field()->get('shortname') == helper::COHORT_FIELD_TYPE) {
$this->assertSame('category', $customfield->export_value());
$this->assertSame(helper::ITEM_TYPE_CATEGORY, $customfield->export_value());
}
}

Expand All @@ -250,4 +247,43 @@ public function test_course_created() {
$enrol = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'cohort', 'customint1' => $cohort->id]);
$this->assertNotEmpty($enrol);
}

/**
* Check logic when creating a course category.
*/
public function test_course_category_created(): void {
global $DB;

$categoryname = 'Category name';
$this->assertEmpty($DB->get_record('cohort', ['name' => $categoryname]));
$this->assertEmpty($DB->get_field('user_info_field', 'param1', ['id' => $this->categoryprofilefield->id]));
$this->assertEmpty($DB->get_record('tool_dynamic_cohorts', ['name' => $categoryname]));

$category = $this->getDataGenerator()->create_category(['name' => $categoryname]);

// Check everything about category cohort.
$categorycohort = $DB->get_record('cohort', ['name' => $category->name]);
$this->assertNotEmpty($categorycohort);

$cohort = cohort_get_cohort($categorycohort->id, \context_coursecat::instance($category->id), true);
foreach ($cohort->customfields as $customfield) {
if ($customfield->get_field()->get('shortname') == helper::COHORT_FIELD_ID) {
$this->assertSame($category->id, $customfield->export_value());
}
if ($customfield->get_field()->get('shortname') == helper::COHORT_FIELD_TYPE) {
$this->assertSame(helper::ITEM_TYPE_CATEGORY, $customfield->export_value());
}
}

$profilefielddata = $DB->get_field('user_info_field', 'param1', ['id' => $this->categoryprofilefield->id]);
$this->assertNotEmpty($profilefielddata);
$this->assertTrue(in_array($categoryname, explode("\n", $profilefielddata)));

$rule = $DB->get_record('tool_dynamic_cohorts', ['name' => $categoryname]);
$this->assertNotEmpty($rule);
$this->assertEquals($cohort->id, $rule->cohortid);
$this->assertEquals(1, $rule->enabled);
$conditions = $DB->get_records('tool_dynamic_cohorts_c', ['ruleid' => $rule->id]);
$this->assertCount(2, $conditions);
}
}

0 comments on commit cb6588a

Please sign in to comment.