Skip to content

Commit

Permalink
issue #4: process removing a tag
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Sep 5, 2024
1 parent 94feeed commit c2a3a21
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
49 changes: 48 additions & 1 deletion classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ public static function get_cohort_by_item(int $itemid, string $itemtype): ?stdCl
public static function add_enrolment_method(stdClass $course, stdClass $cohort): void {
global $DB;

$studentrole = $DB->get_record('role', ['shortname' => self::STUDENT_ROLE]);
$studentrole = self::get_student_role();

$fields = [
'enrol' => 'cohort',
'customint1' => $cohort->id,
'roleid' => $studentrole->id,
'courseid' => $course->id,
Expand All @@ -169,6 +170,41 @@ public static function add_enrolment_method(stdClass $course, stdClass $cohort):
}
}

/**
* A helper to remove enrolment method from a given course based on item details.
*
* @param stdClass $course Given course.
* @param int $itemid Item ID.
* @param string $itemtype Item type (tag, course, category).
*/
public static function remove_enrolment_method(stdClass $course, int $itemid, string $itemtype): void {
global $DB;

$cohort = self::get_cohort_by_item($itemid, $itemtype);

if ($cohort) {
$studentrole = self::get_student_role();

$fields = [
'enrol' => 'cohort',
'customint1' => $cohort->id,
'roleid' => $studentrole->id,
'courseid' => $course->id,
];

$instances = $DB->get_records('enrol', $fields);

if ($instances) {
$enrol = enrol_get_plugin('cohort');

foreach ($instances as $instance) {
// Remove enrolment method.
$enrol->delete_instance($instance);
}
}
}
}

/**
* Update profile field with new item.
*
Expand Down Expand Up @@ -262,4 +298,15 @@ public static function add_rule(stdClass $cohort, string $fieldshortname): void
$rule->set('enabled', 1);
$rule->save();
}

/**
* Returns a student role.
*
* @return stdClass
*/
public static function get_student_role(): stdClass {
global $DB;

return $DB->get_record('role', ['shortname' => helper::STUDENT_ROLE]);
}
}
18 changes: 18 additions & 0 deletions classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use core\event\course_category_created;
use core\event\course_created;
use core\event\tag_added;
use core\event\tag_removed;

/**
* Event observer class.
Expand Down Expand Up @@ -48,6 +49,23 @@ public static function tag_added(tag_added $event): void {
helper::set_up_item($tagid, helper::ITEM_TYPE_TAG, $tagname, $course);
}

/**
* Process tag_removed event.
*
* @param tag_removed $event The event.
*/
public static function tag_removed(tag_removed $event): void {
// Check context is course context.
$context = $event->get_context();
if ($context->contextlevel != CONTEXT_COURSE && $event->other['itemtype'] != 'course') {
return;
}

$tagid = $event->other['tagid'];
$course = get_course($event->other['itemid']);
helper::remove_enrolment_method($course, $tagid, helper::ITEM_TYPE_TAG);
}

/**
* Process course_created event.
*
Expand Down
4 changes: 4 additions & 0 deletions db/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@
'eventname' => '\core\event\course_category_created',
'callback' => '\tool_enrolprofile\observer::course_category_created',
],
[
'eventname' => '\core\event\tag_removed',
'callback' => '\tool_enrolprofile\observer::tag_removed',
],
];
21 changes: 21 additions & 0 deletions tests/observer_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,27 @@ public function test_tag_added(): void {
$this->assertNotEmpty($enrol);
}

/**
* Check logic when removing a tag.
*/
public function test_tag_removed(): void {
global $DB;

$course = $this->getDataGenerator()->create_course();
$tagname = 'A tag';

core_tag_tag::set_item_tags('core', 'course', $course->id, context_course::instance($course->id), [$tagname]);

$cohort = $DB->get_record('cohort', ['name' => $tagname]);
$enrol = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'cohort', 'customint1' => $cohort->id]);
$this->assertNotEmpty($enrol);

core_tag_tag::remove_item_tag('core', 'course', $course->id, $tagname);

$enrol = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'cohort', 'customint1' => $cohort->id]);
$this->assertEmpty($enrol);
}

/**
* Check logic when creating a course.
*/
Expand Down

0 comments on commit c2a3a21

Please sign in to comment.