Skip to content

Commit

Permalink
CRU-119: Update local registration plugin and groups to allow for mul…
Browse files Browse the repository at this point in the history
…tiple coaches

CRU-119 Documentation and unit test
  • Loading branch information
mgardener authored and ojnadjarm committed Jul 22, 2024
1 parent e48fc2c commit 02558f4
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 3 deletions.
22 changes: 22 additions & 0 deletions MLC-CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Changelog
All notable changes to this project by My Learning Consultants will be documented in this file. Commit hash is the original local_autogroup commit this code is based on.

## Commit hash
6b2a10cc130dcd77a96714e5e4b8620f12e610da

## [CRU-119 - 2021-03-12]
### Changed
- Added option for profile field to contain multiple groups
### Unit test
- The multiple_profile_values_test in local_autogroup_lib_testcase in local/autogroup/tests/lib_test.php
### Manual test instructions
- Create a profile text field
- On Site Admin -> Plugins -> Local Plugins
1. Check Enabled
2. Set Group by to be the new profile field
3. Under Event Triggers, enable user profiles
- Create a user
- Create a course
- Enroll the user in the course
- Add a value to the user's profile field that contains comma separated values (eg Test 1, Test 2, Test 3)
- Confirm the user is added to multiple groups based on the values in the field
19 changes: 16 additions & 3 deletions classes/sort_module/user_info_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,23 @@ public function eligible_groups_for_user(stdClass $user) {

$field = $this->field;
$data = $DB->get_record('user_info_data', ['fieldid' => $field, 'userid' => $user->id]);
if ($data && !empty($data->data)) {
return [$data->data];
if ($data && !empty($data->data)){
// Check if the field has comma separated values.
if ($fieldvalues = explode(',', $data->data)) {
$fields = [];
foreach ($fieldvalues as $fieldvalue) {
if ($fieldvalue = trim($fieldvalue)) {
$fields[] = $fieldvalue;
}
}
return $fields;
} else {
return [$data->data];
}
}
else {
return [];
}
return [];
}

/**
Expand Down
122 changes: 122 additions & 0 deletions tests/lib_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
// This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>.
/**
* Tests for local_autogroup.
*
* @package local_autogroup
* @category test
* @copyright 2021 My Learning Consultants
* @author David Saylor <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

global $CFG;

require_once($CFG->dirroot.'/user/profile/lib.php');
require_once($CFG->dirroot.'/user/profile/definelib.php');
require_once($CFG->dirroot.'/user/lib.php');

class local_autogroup_lib_testcase extends advanced_testcase {
/**
* Test setup.
*/
public function setUp() {
$this->resetAfterTest();
}

/**
* A completed course with no equivalents being complete and triggering recompletion notifications
* and also expiration..
*/
public function test_multiple_profile_values() {
global $DB;

$this->resetAfterTest();
$this->setAdminUser();

$fieldid = $this->create_profile_field();

set_config('enabled', true, 'local_autogroup');
set_config('addtonewcourses', true, 'local_autogroup');
set_config('filter', $fieldid, 'local_autogroup');

$course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
$user = $this->getDataGenerator()->create_user();

$this->getDataGenerator()->enrol_user($user->id, $course->id, 'student');

profile_save_custom_fields($user->id, array('test' => 'Test 1, Test 2, Test 3'));
user_update_user($user, false, true);

$groups = groups_get_all_groups($course->id, $user->id);
$this->assertCount(3, $groups);

for ($i = 0; $i < count($groups); $i++) {
$this->assertEquals($groups[$i], 'Test ' . $i);
}

}

private function create_profile_field() {
global $CFG, $DB, $PAGE;

$PAGE->set_context(context_system::instance());

$data = (object) [
'name' => 'Test'
];
$data->sortorder = $DB->count_records('user_info_category') + 1;
$data->id = $DB->insert_record('user_info_category', $data, true);
$createdcategory = $DB->get_record('user_info_category', array('id' => $data->id));
\core\event\user_info_category_created::create_from_category($createdcategory)->trigger();

$field = (object) [
'shortname' => 'test',
'name' => 'Test',
'datatype' => 'text',
'description' => 'A test field',
'descriptionformat' => 1,
'categoryid' => 6,
'sortorder' => 2,
'required' => 0,
'locked' => 1,
'visible' => 0,
'forceunique' => 0,
'signup' => 1,
'defaultdata' => '',
'defaultdataformat' => 0,
'param1' => '30',
'param2' => '100',
'param3' => '0',
'param4' => '',
'param5' => '',
];

$field->id = $DB->get_field('user_info_field', 'id', ['shortname' => $field->shortname]);
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/define.class.php');
$newfield = 'profile_define_'.$field->datatype;
$formfield = new $newfield();
$field->categoryid = $createdcategory->id;
$formfield->define_save($field);

profile_reorder_fields();
profile_reorder_categories();

return $field->id;
}
}

0 comments on commit 02558f4

Please sign in to comment.