This commit is contained in:
Andrew Nicols 2022-06-02 08:45:03 +08:00
commit fd376582ac
5 changed files with 156 additions and 5 deletions

View file

@ -27,6 +27,9 @@ namespace tool_uploaduser;
defined('MOODLE_INTERNAL') || die();
use context_system;
use context_coursecat;
use core_course_category;
use tool_uploaduser\local\field_value_validators;
require_once($CFG->dirroot.'/user/profile/lib.php');
@ -1107,6 +1110,48 @@ class process {
continue;
}
if (preg_match('/^categoryrole(?<roleid>\d+)$/', $column, $rolematches)) {
$categoryrolecache = [];
$categorycache = []; // Category cache - do not fetch all categories here, we will not probably use them all.
$categoryfield = "category{$rolematches['roleid']}";
$categoryrolefield = "categoryrole{$rolematches['roleid']}";
if (empty($user->{$categoryfield})) {
continue;
}
$categoryidnumber = $user->{$categoryfield};
if (!array_key_exists($categoryidnumber, $categorycache)) {
$category = $DB->get_record('course_categories', ['idnumber' => $categoryidnumber], 'id, idnumber');
if (empty($category)) {
$this->upt->track('enrolments', get_string('unknowncategory', 'error', s($categoryidnumber)), 'error');
continue;
}
$categoryrolecache[$categoryidnumber] = uu_allowed_roles_cache($category->id);
$categoryobj = core_course_category::get($category->id);
$context = context_coursecat::instance($categoryobj->id);
$categorycache[$categoryidnumber] = $context;
}
// Check the user's category role.
if (!empty($user->{$categoryrolefield})) {
$rolename = $user->{$categoryrolefield};
if (array_key_exists($rolename, $categoryrolecache[$categoryidnumber])) {
$roleid = $categoryrolecache[$categoryidnumber][$rolename]->id;
// Assign a role to user with category context.
role_assign($roleid, $user->id, $categorycache[$categoryidnumber]->id);
} else {
$this->upt->track('enrolments', get_string('unknownrole', 'error', s($rolename)), 'error');
continue;
}
} else {
$this->upt->track('enrolments', get_string('missingcategoryrole', 'error', s($categoryidnumber)), 'error');
continue;
}
}
if (!preg_match('/^course\d+$/', $column)) {
continue;
}

View file

@ -202,6 +202,21 @@ function uu_validate_user_upload_columns(csv_import_reader $cir, $stdfields, $pr
// test columns
$processed = array();
$acceptedfields = [
'category',
'categoryrole',
'cohort',
'course',
'enrolperiod',
'enrolstatus',
'enroltimestart',
'group',
'role',
'sysrole',
'type',
];
$specialfieldsregex = "/^(" . implode('|', $acceptedfields) . ")\d+$/";
foreach ($columns as $key=>$unused) {
$field = $columns[$key];
$field = trim($field);
@ -218,7 +233,7 @@ function uu_validate_user_upload_columns(csv_import_reader $cir, $stdfields, $pr
// hack: somebody wrote uppercase in csv file, but the system knows only lowercase profile field
$newfield = $lcfield;
} else if (preg_match('/^(sysrole|cohort|course|group|type|role|enrolperiod|enrolstatus|enroltimestart)\d+$/', $lcfield)) {
} else if (preg_match($specialfieldsregex, $lcfield)) {
// special fields for enrolments
$newfield = $lcfield;
@ -369,11 +384,17 @@ function uu_allowed_roles() {
}
/**
* Returns mapping of all roles using short role name as index.
* Returns mapping of roles using short role name as index.
*
* @param int|null $categoryid Id of the category to get roles for. Null means all roles.
* @return array
*/
function uu_allowed_roles_cache() {
$allowedroles = get_assignable_roles(context_course::instance(SITEID), ROLENAME_SHORT);
function uu_allowed_roles_cache(?int $categoryid = null): array {
if (is_null($categoryid)) {
$allowedroles = get_assignable_roles(context_course::instance(SITEID), ROLENAME_SHORT);
} else {
$allowedroles = get_assignable_roles(context_coursecat::instance($categoryid), ROLENAME_SHORT);
}
$rolecache = [];
foreach ($allowedroles as $rid=>$rname) {
$rolecache[$rid] = new stdClass();

View file

@ -14,7 +14,7 @@ Feature: Upload users
| Section 1 | math102 | S1 |
| Section 3 | math102 | S3 |
And I log in as "admin"
And I navigate to "Users > Accounts >Upload users" in site administration
And I navigate to "Users > Accounts > Upload users" in site administration
When I upload "lib/tests/fixtures/upload_users.csv" file to "File" filemanager
And I press "Upload users"
Then I should see "Upload users preview"
@ -186,3 +186,77 @@ Feature: Upload users
And I should see "12 January 2020" in the "Enrolment ends" "table_row"
And I click on "Close" "button" in the "Enrolment details" "dialogue"
And I log out
@javascript
Scenario: Upload users enrolling them on courses and assign category roles
Given the following "courses" exist:
| fullname | shortname |
| management1 | management1 |
| film1 | film1 |
And the following "categories" exist:
| name | idnumber |
| MGMT | MGMT |
| Film | Film |
And I log in as "admin"
And I navigate to "Users > Accounts > Upload users" in site administration
When I upload "lib/tests/fixtures/upload_users_category.csv" file to "File" filemanager
And I press "Upload users"
Then I should see "Upload users preview"
And I should see "Tom"
And I should see "Jones"
And I should see "Trent"
And I should see "Reznor"
And I should see "Aurora"
And I should see "Jiang"
And I should see "Federico"
And I should see "Fellini"
And I should see "Ivan"
And I should see "Ivanov"
And I should see "John"
And I should see "Smith"
And I should see "Warm"
And I should see "Cool"
And I should see "James"
And I should see "Bond"
And I should see "MGMT"
And I should see "Film"
And I should see "manager"
And I should see "student"
And I should see "coursecreator"
And I should see "management1"
And I should see "film1"
And I press "Upload users"
And I should see "Unknown category with category ID number \"Movie\""
And I should see "Unknown course named \"movie1\""
And I should see "Unknown role \"notcoursecreator\""
And I should see "Could not assign role to user: missing role for category"
And I press "Continue"
And I navigate to "Users > Accounts > Browse list of users" in site administration
And I should see "Tom Jones"
And I should see "Trent Reznor"
And I should see "reznor@example.com"
And I am on "management1" course homepage
And I should see "Participants"
And I follow "Participants"
And I should see "Tom Jones"
And I should see "Trent Reznor"
And I should see "Aurora Jiang"
And I should see "Student"
And I am on "film1" course homepage
And I should see "Participants"
And I follow "Participants"
And I should see "Federico Fellini"
And I should see "Student"
And I navigate to "Courses > Manage courses and categories" in site administration
And I click on "permissions" action for "MGMT" in management category listing
And I select "Assign roles" from the "jump" singleselect
And I should see "Manager"
And I should see "Tom Jones"
And I should see "Trent Reznor"
And I should see "Course creator"
And I should see "Aurora Jiang"
And I navigate to "Courses > Manage courses and categories" in site administration
And I click on "permissions" action for "Film" in management category listing
And I select "Assign roles" from the "jump" singleselect
And I should see "Course creator"
And I should see "Federico Fellini"