Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Sep 27, 2024
1 parent c91f73a commit 881e9d0
Show file tree
Hide file tree
Showing 8 changed files with 403 additions and 90 deletions.
165 changes: 141 additions & 24 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,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 array $courses Course to set up enrolment method. If not set, the no enrolment method will be created.
* @param array $courseids 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, array $courses = []): void {
public static function add_item(int $itemid, string $itemtype, string $itemname, array $courseids = []): void {
$cohort = self::get_cohort_by_item($itemid, $itemtype);

if (empty($cohort)) {
Expand All @@ -114,15 +114,8 @@ public static function add_item(int $itemid, string $itemtype, string $itemname,
self::add_rule($cohort, $itemtype);
// Add a tag to a custom profile field.
self::add_profile_field_item($itemtype, $itemname);

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

/**
Expand Down Expand Up @@ -151,6 +144,8 @@ public static function remove_item(int $itemid, string $itemtype, string $itemna
cohort_delete_cohort($cohort);
// Delete tag value from custom profile field list.
self::delete_profile_field_item($itemtype, $itemname);
// Clean up presets.
self::clean_up_presets($itemid, $itemtype);
// Clean up user data?
}
}
Expand Down Expand Up @@ -244,26 +239,34 @@ public static function get_cohort_by_item(int $itemid, string $itemtype): ?stdCl
/**
* Helper method to add enrolment method to a course.
*
* @param stdClass $course Course.
* @param stdClass $cohort Cohort.
* @param array $courseids A list of course ids.
*
* @return void
*/
public static function add_enrolment_method(stdClass $course, stdClass $cohort): void {
public static function add_enrolment_method(stdClass $cohort, array $courseids): void {
global $DB;

$studentrole = self::get_student_role();

$fields = [
'enrol' => 'cohort',
'customint1' => $cohort->id,
'roleid' => $studentrole->id,
'courseid' => $course->id,
];
if (!empty($courseids)) {
list($sql, $params) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED);
$select = 'id ' . $sql;
$courses = $DB->get_records_select('course', $select, $params);

if (!$DB->record_exists('enrol', $fields)) {
$enrol = enrol_get_plugin('cohort');
$enrol->add_instance($course, $fields);
foreach ($courses as $course) {
$fields = [
'enrol' => 'cohort',
'customint1' => $cohort->id,
'roleid' => $studentrole->id,
'courseid' => $course->id,
];

if (!$DB->record_exists('enrol', $fields)) {
$enrol = enrol_get_plugin('cohort');
$enrol->add_instance($course, $fields);
}
}
}
}

Expand Down Expand Up @@ -510,8 +513,7 @@ public static function update_course_category(int $courseid, int $newcategoryid)

$cohort = self::get_cohort_by_item($newcategoryid, self::ITEM_TYPE_CATEGORY);
if ($cohort) {
$course = get_course($courseid);
self::add_enrolment_method($course, $cohort);
self::add_enrolment_method($cohort, [$courseid]);
}
}

Expand Down Expand Up @@ -610,4 +612,119 @@ public static function get_courses_by_tags(array $tagids): array {

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

/**
* A tiny helper method to convert list of items from string to array.
*
* @param string|null $data
* @return array
*/
public static function explode_data(?string $data): array {
return !empty($data) ? explode(',', $data) : [];
}

/**
* Get list of presets by item type and ID.
*
* @param int $itemid Item ID.
* @param string $itemtype Itenm type.
*
* @return preset[]
*/
public static function get_presets_by_item(int $itemid, string $itemtype): array {
$results = [];

if (in_array($itemtype, self::get_field_types())) {
foreach (preset::get_records() as $preset) {
if (in_array($itemid, self::explode_data($preset->get($itemtype)))) {
$results[$preset->get('id')] = $preset;
}
}
}

return $results;
}

/**
* Get a list of field types.
*
* @return string[]
*/
public static function get_field_types(): array {
return [self::ITEM_TYPE_CATEGORY, self::ITEM_TYPE_COURSE, self::ITEM_TYPE_TAG];
}

/**
* Get list of course IDs for a given preset.
*
* @param preset $preset Preset
* @return array
*/
public static function get_course_ids_from_preset(preset $preset): array {
$courseids = [];

if (!empty($preset->get('category'))) {
$catcourses = array_keys(
self::get_courses_by_categories(explode(',', $preset->get('category')))
);

$courseids = array_unique(array_merge($courseids, $catcourses));
}

if (!empty($preset->get('course'))) {
$courses = explode(',', $preset->get('course'));
$courseids = array_unique(array_merge($courseids, $courses));
}

if (!empty($preset->get('tag'))) {
$tagcourses = array_keys(
self::get_courses_by_tags(explode(',', $preset->get('tag')))
);
$courseids = array_values(array_unique(array_merge($courseids, $tagcourses)));
}

return $courseids;
}

public static function clean_up_presets(int $itemid, string $itemtype): void {
global $DB;

$presets = helper::get_presets_by_item($itemid, $itemtype);

foreach ($presets as $preset) {
$items = self::explode_data($preset->get($itemtype));
if (($key = array_search($itemid, $items)) !== false) {
unset($items[$key]);
}
$preset->set($itemtype, implode(',', $items));
$preset->save();
}

foreach ($presets as $preset) {
$cohort = self::get_cohort_by_item($preset->get('id'), self::ITEM_TYPE_PRESET);
if ($cohort) {
$presetcourses = self::get_course_ids_from_preset($preset);
$studentrole = self::get_student_role();

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

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

if ($instances) {
$enrol = enrol_get_plugin('cohort');
foreach ($instances as $instance) {
// If enrolment instance belongs to a course that is no longer part of preset,
// them delete this enrolment instance.
if (!in_array($instance->courseid, $presetcourses)) {
$enrol->delete_instance($instance);
}
}
}
}
}
}
}
50 changes: 25 additions & 25 deletions classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public static function tag_removed(tag_removed $event): void {
'itemtype' => helper::ITEM_TYPE_TAG,
'courseid' => $event->other['itemid'],
]);

// Find all presets based on item type and id.
// For each preset get cohort of that preset
// Remove that cohort to the course.
}

/**
Expand All @@ -106,6 +110,11 @@ public static function tag_deleted(tag_deleted $event): void {
'itemtype' => helper::ITEM_TYPE_TAG,
'itemname' => $tagname,
]);


// Find all presets based on item type and id.
// For each preset get cohort of that preset
// Remove that cohort from the course.
}

/**
Expand Down Expand Up @@ -169,6 +178,14 @@ public static function course_updated(course_updated $event): void {
'categoryid' => $event->other['updatedfields']['category'],
]);
}

// Find all presets based on item type (category) and id.
// For each preset get cohort of that preset
// Add that cohort from the course.

// Find all presets based on item type (category) and old id.
// For each preset get cohort of that preset
// Remove that cohort from the course.
}

/**
Expand Down Expand Up @@ -234,6 +251,10 @@ public static function course_category_deleted(course_category_deleted $event):
'itemtype' => helper::ITEM_TYPE_CATEGORY,
'itemname' => $categoryname,
]);

// Find all presets based on item type and id.
// For each preset get cohort of that preset
// Remove that cohort from the all courses.
}

/**
Expand All @@ -242,34 +263,13 @@ public static function course_category_deleted(course_category_deleted $event):
* @param preset_created $event The event.
*/
public static function preset_created(preset_created $event): void {
$courseids = [];

if (!empty($event->other['categories'])) {
$catcourses = array_keys(
helper::get_courses_by_categories(explode(',', $event->other['categories']))
);

$courseids = array_unique(array_merge($courseids, $catcourses));
}

if (!empty($event->other['courses'])) {
$courses = explode(',', $event->other['courses']);
$courseids = array_unique(array_merge($courseids, $courses));

}

if (!empty($event->other['tags'])) {
$tagcourses = array_keys(
helper::get_courses_by_tags(explode(',', $event->other['tags']))
);
$courseids = array_values(array_unique(array_merge($courseids, $tagcourses)));
}
$preset = preset::get_record(['id' => $event->other['presetid']]);

self::queue_adhoc_task('add_item', [
'itemid' => $event->other['presetid'],
'itemid' => $preset->get('id'),
'itemtype' => helper::ITEM_TYPE_PRESET,
'itemname' => $event->other['presetname'],
'courseids' => $courseids,
'itemname' => $preset->get('name'),
'courseids' => helper::get_course_ids_from_preset($preset),
]);
}

Expand Down
21 changes: 6 additions & 15 deletions classes/preset_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function definition() {

$this->_form->addElement('static', 'error', '');

foreach ($this->get_field_types() as $type) {
foreach (helper::get_field_types() as $type) {
$methodname = 'get_' . $type . '_options';
$this->_form->addElement(
'autocomplete',
Expand All @@ -59,15 +59,6 @@ protected function definition() {
}
}

/**
* Get a list of field types.
*
* @return string[]
*/
protected function get_field_types(): array {
return ['category', 'course', 'tag'];
}

/**
* Gets categories options.
*
Expand Down Expand Up @@ -132,13 +123,13 @@ public function validation($data, $files) {
$errors = parent::validation($data, $files);

$empty = 0;
foreach ($this->get_field_types() as $type) {
foreach (helper::get_field_types() as $type) {
if (empty($data[$type])) {
$empty++;
}
}

if ($empty == count($this->get_field_types())) {
if ($empty == count(helper::get_field_types())) {
$errors['error'] = get_string('mustselectentities', 'tool_enrolprofile');
}

Expand Down Expand Up @@ -178,7 +169,7 @@ public function process_dynamic_submission(): \stdClass {
$preset = new preset();
}

foreach ($this->get_field_types() as $type) {
foreach (helper::get_field_types() as $type) {
if (!empty($data->$type)) {
$data->$type = implode(',', $data->$type);
} else {
Expand All @@ -197,7 +188,7 @@ public function process_dynamic_submission(): \stdClass {
'presetname' => $preset->get('name'),
];

foreach ($this->get_field_types() as $type) {
foreach (helper::get_field_types() as $type) {
$other[$type] = $data->$type;
$other['old' . $type] = $olddata->$type;
}
Expand All @@ -208,7 +199,7 @@ public function process_dynamic_submission(): \stdClass {
'other' => $other,
])->trigger();
} else {
foreach ($this->get_field_types() as $type) {
foreach (helper::get_field_types() as $type) {
$other['old' . $type] = $olddata->$type;
}
preset_updated::create([
Expand Down
Loading

0 comments on commit 881e9d0

Please sign in to comment.