Skip to content

Commit

Permalink
IOMAD: set up two way sing for department from profile to company, al…
Browse files Browse the repository at this point in the history
…lowed option for Institution to be company name or shortname and changed scheduled tasks to batch up acccounts instead of trying to do everyone
  • Loading branch information
turf212 committed Apr 5, 2024
1 parent 573a76c commit ad6e754
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 29 deletions.
Expand Up @@ -557,6 +557,8 @@
$string['postcode'] = 'Postcode';
$string['programleft2'] = ' program slots left to allocate on this license';
$string['purgeselectedentries'] = 'Purge selected entries';
$string['setfromcompany'] = 'Set from company department';
$string['settocompany'] = 'Set to company department';
$string['showexpiredlicenses'] = 'Show expired licenses';
$string['showvalidcourses'] = 'Include expired course results';
$string['nolicenses'] = '<p> Your license has either expired or you have allocated all your available places. Please contact your account manager to discuss. </p>';
Expand Down
57 changes: 35 additions & 22 deletions local/iomad/classes/task/cron_task.php
Expand Up @@ -46,42 +46,55 @@ public function execute() {
$runtime = time();
// Are we copying Company to institution?
if (!empty($CFG->iomad_sync_institution)) {
mtrace("Copying company shortnames to user institution fields\n");
// Get the users where it's wrong.
$users = $DB->get_records_sql("SELECT u.*, c.id as companyid
FROM {user} u
JOIN {company_users} cu ON cu.userid = u.id
JOIN {company} c ON cu.companyid = c.id
WHERE u.institution != c.shortname
AND c.parentid = 0");
// Get all of the companies.
$companies = $DB->get_records('company', array(), '', 'id,shortname');
if ($CFG->iomad_sync_institution == 1) {
mtrace("Copying company shortnames to user institution fields\n");

// Get the users where it's wrong.
$users = $DB->get_records_sql("SELECT u.id, c.shortname as targetname
FROM {user} u
JOIN {company_users} cu ON cu.userid = u.id
JOIN {company} c ON cu.companyid = c.id
WHERE u.institution != c.shortname
AND c.parentid = 0
LIMIT 1000");
} else if ($CFG->iomad_sync_institution == 2) {
mtrace("Copying company name to user institution fields\n");

// Get the users where it's wrong.
$users = $DB->get_records_sql("SELECT u.id, c.name as targetname
FROM {user} u
JOIN {company_users} cu ON cu.userid = u.id
JOIN {company} c ON cu.companyid = c.id
WHERE u.institution != c.name
AND c.parentid = 0
LIMIT 1000");
}

// Update the users.
foreach ($users as $user) {
$user->institution = $companies[$user->companyid]->shortname;
$DB->update_record('user', $user);
$DB->set_field('user', 'institution', $user->targetname, ['id' => $user->id]);
}
$companies = array();
$users = array();
$users = [];
}

// Are we copying department to department?
if (!empty($CFG->iomad_sync_department)) {
if (!empty($CFG->iomad_sync_department &&
$CFG->iomad_sync_department == 1)) {
mtrace("Copying company department name to user department fields\n");

// Get the users where it's wrong.
$users = $DB->get_records_sql("SELECT u.*, d.id as departmentid
$users = $DB->get_records_sql("SELECT u.*, d.name as targetname
FROM {user} u
JOIN {company_users} cu ON cu.userid = u.id
JOIN {company} c ON cu.companyid = c.id
JOIN {department} d ON cu.departmentid = d.id
WHERE u.department != d.name
AND c.parentid = 0");
// Get all of the companies.
$departments = $DB->get_records('department', array(), '', 'id,name');
AND c.parentid = 0
LIMIT 1000");
// Update the users.
foreach ($users as $user) {
$user->department = $departments[$user->departmentid]->name;
$DB->update_record('user', $user);
$DB->set_field('user', 'department', $user->targetname, ['id' => $user->id]);
}
$companies = array();
$users = array();
}

Expand Down
47 changes: 47 additions & 0 deletions local/iomad/lib/company.php
Expand Up @@ -4269,6 +4269,53 @@ public static function user_updated(\core\event\user_updated $event) {
iomad_commerce::update_user($user, $company->id);
}

// Check if we are assigning department by profile field.
if (!empty($CFG->iomad_sync_department) &&
$CFG->iomad_sync_department == 2) {
// Check if there is a department with the name given.
$current = $DB->count_records('department', ['company' => $company->id, 'name' => $user->department]);
if ($current == 1) {
// Assign them to the department.
$department = $DB->get_record('department', ['company' => $company->id, 'name' => $user->department]);
if ($currentdepartments = $DB->get_records('company_users', ['companyid' => $company->id, 'userid' => $user->id])) {
// We only do anything if they are in one department.
if (count($currentdepartments) == 1) {
foreach ($currentdepartments as $currentdepartment) {
// Only move them if they are not a company manager.
if ($currentdepartment->managertype != 1) {
$DB->set_field('company_users', 'departmentid', $department->id, ['id' => $currentdepartment->id]);
}
}
}
} else {
// Assign them to this department as they aren't in any yet.
self::assign_user_to_department($department->id, $user->id);
}
} else if ($current == 0) {
// Department doesn't exist yet. Create it!
$shortname = str_replace(' ', '-', $user->department);
$shortname = preg_replace('/[^A-Za-z0-9\-]/', '', $shortname);
$topdepartment = self::get_company_parentnode($company->id);
self::create_department(0, $company->id, $user->department, $shortname, $topdepartment->id);
// Get the new department.
$department = $DB->get_record('department', ['company' => $company->id, 'shortname' => $shortname]);
if ($currentdepartments = $DB->get_records('company_users', ['companyid' => $company->id, 'userid' => $user->id])) {
// We only do anything if they are in one department.
if (count($currentdepartments) == 1) {
foreach ($currentdepartments as $currentdepartment) {
// Only move them if they are not a company manager.
if ($currentdepartment->managertype != 1) {
$DB->set_field('company_users', 'departmentid', $department->id, ['id' => $currentdepartment->id]);
}
}
}
} else {
// Assign them to this department as they aren't in any yet.
self::assign_user_to_department($department->id, $user->id);
}
}
}

return true;
}

Expand Down
4 changes: 2 additions & 2 deletions local/iomad_settings/lang/en/local_iomad_settings.php
Expand Up @@ -78,9 +78,9 @@
$string['iomad_showcharts'] = 'Show course completion charts as default';
$string['iomad_showcharts_help'] = 'If checked the charts will be shown fist with an option to show as text instead';
$string['iomad_sync_department'] = 'Sync company department with profile';
$string['iomad_sync_department_help'] = 'Selecting this will keep the user profile field for department in sync with the name of the company department that the user is in.';
$string['iomad_sync_department_help'] = 'Selecting this will either keep the user profile field for department in sync with the name of the company department that the user is in (Set from company department), or will assign the user to a company department which matches (Set to company department).';
$string['iomad_sync_institution'] = 'Sync company name with profile';
$string['iomad_sync_institution_help'] = 'Selecting this will keep the user profile field for institution in sync with the shortname of the company that the user is in.';
$string['iomad_sync_institution_help'] = 'Selecting this will keep the user profile field for institution in sync with either the shortname or name of the company that the user is in.';
$string['iomad_use_email_as_username'] = 'Use email address as user name';
$string['iomad_use_email_as_username_help'] = 'Selecting this will change the way a user name is automatically created for a new user account in IOMAD so that it simply uses the email address.';
$string['iomad_useicons'] = 'Use icons in IOMAD dashboard';
Expand Down
20 changes: 15 additions & 5 deletions local/iomad_settings/settings.php
Expand Up @@ -41,15 +41,25 @@
get_string('iomad_allow_username_help', 'local_iomad_settings'),
0));

$settings->add(new admin_setting_configcheckbox('iomad_sync_institution',
$institutionsync = [get_string('no'),
get_string('companyshortname', 'block_iomad_company_admin'),
get_string('companyname', 'block_iomad_company_admin')];

$settings->add(new admin_setting_configselect('iomad_sync_institution',
get_string('iomad_sync_institution', 'local_iomad_settings'),
get_string('iomad_sync_institution_help', 'local_iomad_settings'),
1));
1,
$institutionsync));

$settings->add(new admin_setting_configcheckbox('iomad_sync_department',
get_string('iomad_sync_department', 'local_iomad_settings'),
$departmentsync = [get_string('no'),
get_string('setfromcompany', 'block_iomad_company_admin'),
get_string('settocompany', 'block_iomad_company_admin')];

$settings->add(new admin_setting_configselect('iomad_sync_department',
get_string('iomad_sync_department', 'local_iomad_settings'),
1));
get_string('iomad_sync_department_help', 'local_iomad_settings'),
1,
$departmentsync));

$settings->add(new admin_setting_configcheckbox('iomad_autoenrol_managers',
get_string('iomad_autoenrol_managers', 'local_iomad_settings'),
Expand Down

0 comments on commit ad6e754

Please sign in to comment.