From 1fcc63d1ebaf81452371b725cbb69c68da507548 Mon Sep 17 00:00:00 2001 From: Derick Turner Date: Fri, 5 Apr 2024 15:38:45 +0100 Subject: [PATCH] IOMAD: set up two way sing for department from profile to company, allowed option for Institution to be company name or shortname and changed scheduled tasks to batch up acccounts instead of trying to do everyone --- .../lang/en/block_iomad_company_admin.php | 2 + local/iomad/classes/task/cron_task.php | 73 ++++++++++--------- local/iomad/lib/company.php | 47 ++++++++++++ .../lang/en/local_iomad_settings.php | 5 +- local/iomad_settings/settings.php | 20 +++-- 5 files changed, 103 insertions(+), 44 deletions(-) diff --git a/blocks/iomad_company_admin/lang/en/block_iomad_company_admin.php b/blocks/iomad_company_admin/lang/en/block_iomad_company_admin.php index 9a91a294712..b2ac7369011 100644 --- a/blocks/iomad_company_admin/lang/en/block_iomad_company_admin.php +++ b/blocks/iomad_company_admin/lang/en/block_iomad_company_admin.php @@ -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'] = '

Your license has either expired or you have allocated all your available places. Please contact your account manager to discuss.

'; diff --git a/local/iomad/classes/task/cron_task.php b/local/iomad/classes/task/cron_task.php index 144eb1f4ebd..c878d97b651 100644 --- a/local/iomad/classes/task/cron_task.php +++ b/local/iomad/classes/task/cron_task.php @@ -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} @@ -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} @@ -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. @@ -132,7 +134,6 @@ public function execute() { } } - $companies = array(); $users = array(); } diff --git a/local/iomad/lib/company.php b/local/iomad/lib/company.php index 3be94e76e03..a4c98955e10 100644 --- a/local/iomad/lib/company.php +++ b/local/iomad/lib/company.php @@ -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; } diff --git a/local/iomad_settings/lang/en/local_iomad_settings.php b/local/iomad_settings/lang/en/local_iomad_settings.php index 41ec7a4c2e1..5e5cae7e5a5 100644 --- a/local/iomad_settings/lang/en/local_iomad_settings.php +++ b/local/iomad_settings/lang/en/local_iomad_settings.php @@ -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'; diff --git a/local/iomad_settings/settings.php b/local/iomad_settings/settings.php index c3cb3111df1..1cc490c7be4 100644 --- a/local/iomad_settings/settings.php +++ b/local/iomad_settings/settings.php @@ -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'),