-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
380 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
<?php | ||
// This file is part of Moodle - https://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
namespace tool_enrolprofile; | ||
|
||
use context_system; | ||
use stdClass; | ||
use tool_dynamic_cohorts\cohort_manager; | ||
use tool_dynamic_cohorts\condition_base; | ||
use tool_dynamic_cohorts\rule; | ||
|
||
/** | ||
* Helper class. | ||
* | ||
* @package tool_enrolprofile | ||
* @copyright 2024 Dmitrii Metelkin <[email protected]> | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class helper { | ||
|
||
public const FIELD_TAG = 'tag'; | ||
public const STUDENT_ROLE = 'student'; | ||
public const FIELD_CATEGORY = 'category'; | ||
public const PROFILE_CATEGORY = 'Profile field'; | ||
public const FIELD_COURSE = 'course'; | ||
public const FIELD_ENROLLED_UNTIL = 'enrolleduntil'; | ||
public const COURSE_NAME = 'fullname'; | ||
|
||
/** | ||
* A helper function to add custom profile field. | ||
* | ||
* @param string $name Field name. | ||
* @param string $shortname Field shortname. | ||
* @param string $datatype Field data type. | ||
* @param array $extras Extra data. | ||
* | ||
* @return int ID of the record. | ||
*/ | ||
function set_up_add_user_profile_field(string $name, string $shortname, string $datatype, array $extras = []): int { | ||
global $DB; | ||
|
||
$data = new \stdClass(); | ||
$data->shortname = $shortname; | ||
$data->datatype = $datatype; | ||
$data->name = $name; | ||
$data->required = false; | ||
$data->locked = false; | ||
$data->forceunique = false; | ||
$data->signup = false; | ||
$data->visible = '0'; | ||
$data->categoryid = 1; | ||
|
||
foreach ($extras as $name => $value) { | ||
$data->{$name} = $value; | ||
} | ||
|
||
return $DB->insert_record('user_info_field', $data); | ||
} | ||
|
||
/** | ||
* Get cohort by provided item type and item id. | ||
* | ||
* @param int $itemid Item ID. | ||
* @param string $itemtype Item type. | ||
* | ||
* @return stdClass|null | ||
*/ | ||
public static function get_cohort_by_item(int $itemid, string $itemtype): ?stdClass { | ||
$systemcontext = context_system::instance(); | ||
|
||
$allcohorts = cohort_get_cohorts($systemcontext->id, 0, 0, '', true); | ||
// Load custom fields data and filter bby custom field type and id. | ||
$cohorts = array_filter($allcohorts['cohorts'], function ($cohortdata) use ($itemid, $itemtype) { | ||
foreach ($cohortdata->customfields as $customfield) { | ||
$name = 'customfield_' . $customfield->get_field()->get('shortname'); | ||
$cohortdata->$name = $customfield->export_value(); | ||
} | ||
return $cohortdata->customfield_type == $itemtype && $cohortdata->customfield_id == $itemid; | ||
}); | ||
|
||
if (!empty($cohorts)) { | ||
return reset($cohorts); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Helper method to add enrolment method to a course. | ||
* | ||
* @param stdClass $course Course. | ||
* @param stdClass $cohort Cohort. | ||
* | ||
* @return void | ||
*/ | ||
public static function add_enrolment_method(stdClass $course, stdClass $cohort): void { | ||
global $DB; | ||
|
||
$studentrole = $DB->get_record('role', ['shortname' => self::STUDENT_ROLE]); | ||
|
||
$fields = [ | ||
'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); | ||
} | ||
} | ||
|
||
/** | ||
* Update profile field with new item. | ||
* | ||
* @param string $shortname Field short name. | ||
* @param string $newitem A new item to add to the field. | ||
* | ||
* @return void | ||
*/ | ||
public static function update_profile_field(string $shortname, string $newitem): void { | ||
global $DB; | ||
|
||
$field = $DB->get_record('user_info_field', ['shortname' => $shortname]); | ||
$fielddata = explode("\n", $field->param1); | ||
if (!in_array($fielddata, $fielddata)) { | ||
$fielddata[] = $newitem; | ||
sort($fielddata); | ||
$field->param1 = implode("\n", $fielddata); | ||
$DB->update_record('user_info_field', $field); | ||
} | ||
} | ||
|
||
/** | ||
* Add cohort. | ||
* | ||
* @param stdClass $cohort Cohort. | ||
* | ||
* @return int | ||
*/ | ||
public static function add_cohort(stdClass $cohort): int { | ||
global $DB; | ||
if (!$existingcohort = $DB->get_record('cohort', ['name' => $cohort->name])) { | ||
return cohort_add_cohort($cohort); | ||
} else { | ||
return $existingcohort->id; | ||
} | ||
} | ||
|
||
/** | ||
* A helper method to set up rule for given cohort. | ||
* | ||
* @param stdClass $cohort Cohort. | ||
* @param string $fieldshortname Related profile field shortname. | ||
* | ||
* @return void | ||
*/ | ||
public static function add_rule(stdClass $cohort, string $fieldshortname): void { | ||
if (rule::get_record(['cohortid' => $cohort->id])) { | ||
return; | ||
} | ||
|
||
cohort_manager::manage_cohort($cohort->id); | ||
$rule = new rule(0, (object)[ | ||
'name' => $cohort->name, | ||
'cohortid' => $cohort->id, | ||
'description' => $cohort->description, | ||
]); | ||
$rule->save(); | ||
|
||
$condition = condition_base::get_instance(0, (object)[ | ||
'classname' => 'tool_dynamic_cohorts\local\tool_dynamic_cohorts\condition\user_custom_profile', | ||
]); | ||
|
||
$fieldname = 'profile_field_' . $fieldshortname; | ||
$condition->set_config_data([ | ||
'profilefield' => $fieldname, | ||
$fieldname . '_operator' => condition_base::TEXT_IS_EQUAL_TO, | ||
$fieldname . '_value' => $cohort->name, | ||
]); | ||
$condition->get_record()->set('ruleid', $rule->get('id')); | ||
$condition->get_record()->set('sortorder', 0); | ||
$condition->get_record()->save(); | ||
|
||
$condition = condition_base::get_instance(0, (object)[ | ||
'classname' => 'tool_dynamic_cohorts\local\tool_dynamic_cohorts\condition\user_custom_profile', | ||
]); | ||
|
||
$fieldname = 'profile_field_' . helper::FIELD_ENROLLED_UNTIL; | ||
$condition->set_config_data([ | ||
'profilefield' => $fieldname, | ||
$fieldname . '_operator' => condition_base::DATE_IN_THE_FUTURE, | ||
$fieldname . '_value' => 0, | ||
]); | ||
$condition->get_record()->set('ruleid', $rule->get('id')); | ||
$condition->get_record()->set('sortorder', 0); | ||
$condition->get_record()->save(); | ||
|
||
$rule->set('enabled', 1); | ||
$rule->save(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
// This file is part of Moodle - https://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
namespace tool_enrolprofile; | ||
|
||
use core\event\tag_added; | ||
use stdClass; | ||
use context_system; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
require_once($CFG->dirroot . '/cohort/lib.php'); | ||
|
||
/** | ||
* Event observer class. | ||
* | ||
* @package tool_enrolprofile | ||
* @copyright 2024 Dmitrii Metelkin <[email protected]> | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class observer { | ||
|
||
/** | ||
* Process tag_added event. | ||
* | ||
* @param tag_added $event The event. | ||
*/ | ||
public static function tag_added(tag_added $event): void { | ||
// Check context is course context. | ||
$context = $event->get_context(); | ||
if ($context->contextlevel != CONTEXT_COURSE && $event->other['itemtype'] != 'course') { | ||
return; | ||
} | ||
|
||
$systemcontext = context_system::instance(); | ||
$tagid = $event->other['tagid']; | ||
$tagname = $event->other['tagrawname']; | ||
|
||
$courseid = $event->other['itemid']; | ||
$course = get_course($courseid); | ||
|
||
$cohort = new stdClass(); | ||
$cohort->contextid = $systemcontext->id; | ||
$cohort->name = $tagname; | ||
$cohort->idnumber = $tagname; | ||
$cohort->description = 'Tag related'; | ||
$cohort->customfield_type = 'tag'; | ||
$cohort->customfield_id = $tagid; | ||
|
||
// Check if cohort already exists. | ||
$existingcohort = helper::get_cohort_by_item($tagid, 'tag'); | ||
|
||
// If not. | ||
if (empty($existingcohort)) { | ||
// Create a new cohort. | ||
$cohort->id = helper::add_cohort($cohort); | ||
// Create a dynamic cohort rule associated with this cohort | ||
helper::add_rule($cohort, helper::FIELD_TAG); | ||
// Add a tag to a custom profile field | ||
helper::update_profile_field(helper::FIELD_TAG, $tagname); | ||
// Create enrolment method for the cohort for a given course. | ||
helper::add_enrolment_method($course, $cohort); | ||
} else { | ||
// If yes, create enrolment method for the cohort for a given course | ||
helper::add_enrolment_method($course, $existingcohort); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
// This file is part of Moodle - https://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Plugin event observers are registered here. | ||
* | ||
* @package tool_enrolprofile | ||
* @copyright 2024 Dmitrii Metelkin <[email protected]> | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
$observers = [ | ||
[ | ||
'eventname' => '\core\event\tag_added', | ||
'callback' => '\tool_enrolprofile\observer::tag_added', | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
// This file is part of Moodle - https://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
namespace tool_enrolprofile; | ||
|
||
use advanced_testcase; | ||
|
||
/** | ||
* Unit tests for observer class. | ||
* | ||
* @package tool_dynamic_cohorts | ||
* @copyright 2024 Catalyst IT | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
* | ||
* @covers \tool_enrolprofile\observer | ||
*/ | ||
class observer_test extends advanced_testcase { | ||
|
||
/** | ||
* Check logic when adding a new tag. | ||
* @return void | ||
*/ | ||
public function test_tag_added_new_tag() { | ||
$this->resetAfterTest(); | ||
|
||
$course = $this->getDataGenerator()->create_course(); | ||
\core_tag_tag::set_item_tags('core', 'course', $course->id, \context_course::instance($course->id), array('A tag')); | ||
|
||
} | ||
|
||
/** | ||
* Check logic when adding an existing tag. | ||
* @return void | ||
*/ | ||
public function test_tag_added_existing_tag() { | ||
$this->resetAfterTest(); | ||
|
||
} | ||
} |