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 be62f3d commit 1fcc63d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 44 deletions.
Expand Up @@ -570,6 +570,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
73 changes: 37 additions & 36 deletions local/iomad/classes/task/cron_task.php
Expand Up @@ -46,7 +46,7 @@ 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 in multiple companies
$multiusers = $DB->get_records_sql("SELECT userid
FROM {company_users}
Expand All @@ -58,42 +58,45 @@ public function execute() {
$notmultisql = " AND u.id NOT IN (" . implode(',', array_keys($multiusers)) . ")";
$multisql = " WHERE u.id IN (" . implode(',', array_keys($multiusers)) . ")";
}
// 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
$notmultisql
");
// Get all of the companies.
$companies = $DB->get_records('company', array(), '', 'id,shortname');
foreach ($users as $user) {
$user->institution = $companies[$user->companyid]->shortname;
$DB->update_record('user', $user);
}
if ($CFG->iomad_sync_institution == 1) {
mtrace("Copying company shortnames to user institution fields\n");


// Deal with those in multiple companies.
if (!empty($multiusers)) {
$users = $DB->get_records_sql("SELECT DISTINCT u.*
// Get the users where it's wrong.
$users = $DB->get_records_sql("SELECT u.*, c.shortname as targetname
FROM {user} u
$multisql");
foreach ($users as $user) {
$string = get_string_manager()->get_string('blockmultiple', 'admin', '', $user->lang);
$user->institution = $string;
$DB->update_record('user', $user);
}
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
$notmultisql
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
$notmultisql
LIMIT 1000");
}

$companies = array();
$users = array();
// Update the users.
foreach ($users as $user) {
$DB->set_field('user', 'institution', $user->targetname, ['id' => $user->id]);
}
$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.
$multiusers = $DB->get_records_sql("SELECT userid
FROM {company_users}
Expand All @@ -105,19 +108,18 @@ public function execute() {
$notmultisql = " AND u.id NOT IN (" . implode(',', array_keys($multiusers)) . ")";
$multisql = " WHERE u.id IN (" . implode(',', array_keys($multiusers)) . ")";
}
$users = $DB->get_records_sql("SELECT DISTINCT u.*, d.id as departmentid
$users = $DB->get_records_sql("SELECT DISTINCT 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
$notmultisql");
// Get all of the companies.
$departments = $DB->get_records('department', array(), '', 'id,name');
$notmultisql
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]);
}

// Deal with those in multiple departments.
Expand All @@ -132,7 +134,6 @@ public function execute() {
}
}

$companies = array();
$users = array();
}

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

// 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
5 changes: 2 additions & 3 deletions local/iomad_settings/lang/en/local_iomad_settings.php
Expand Up @@ -78,9 +78,8 @@
$string['iomad_showcharts'] = 'Show course completion charts as default';
$string['iomad_showcharts_help'] = 'If checked, the charts will be shown first 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\'s department profile field in sync with the name of the company department that the user is allocated. If the user is in multiple departments, then this will show \'Multiple\' instead.';
$string['iomad_sync_institution'] = 'Sync company name with profile';
$string['iomad_sync_institution_help'] = 'Selecting this will keep the user\'s institution profile field in sync with the shortname of the company that the user is allocated to. If the user is in multiple companies, then this will show \'Multiple\' instead.';
$string['iomad_sync_department_help'] = 'Selecting this will either keep the user\'s profile field for department in sync with the name of the company department that the user is allocated to (Set from company department), or will assign the user to a company department which matches (Set to company department). If the user is in multiple departments, then this will show \'Multiple\' instead.';
$string['iomad_sync_institution_help'] = 'Selecting this will keep the user\'s institution profile field in sync with either the shortname or name of the company that the user is allocated to. If the user is in multiple companies, then this will show \'Multiple\' instead.';
$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\'s username is automatically created for a new user account in IOMAD so that it simply uses their 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 1fcc63d

Please sign in to comment.