diff --git a/local/iomad/lib/company.php b/local/iomad/lib/company.php index 5c4c2987efe..5ca9037ffb9 100644 --- a/local/iomad/lib/company.php +++ b/local/iomad/lib/company.php @@ -285,45 +285,69 @@ public static function get_companies_rs($page=0, $perpage=0) { * */ public static function get_companies_select($showsuspended=false) { - global $DB, $USER; + global $CFG, $DB, $USER; // Is this an admin, or a normal user? if (iomad::has_capability('block/iomad_company_admin:company_view_all', context_system::instance())) { - if ($showsuspended) { - $companies = $DB->get_recordset('company', ['parentid' => 0], 'name', '*'); - } else { - $companies = $DB->get_recordset('company', ['suspended' => 0, 'parentid' => 0], 'name', '*'); + $sqlparams = []; + $sqlwhere = ""; + if (!empty($CFG->iomad_show_company_structure)) { + $sqlparams['parentid'] = 0; + $sqlwhere .= " AND parentid = :parentid "; } + if (!$showsuspended) { + $sqlparams['suspended'] = 0; + $sqlwhere .= " AND suspended = :suspended "; + } + $companies = $DB->get_records_sql_menu("SELECT id, IF (suspended=0, name, concat(name, ' (S)')) AS name FROM {company} + WHERE 1 = 1 + $sqlwhere + ORDER BY name", + $sqlparams); } else { if ($showsuspended) { $suspendedsql = ''; } else { $suspendedsql = "AND suspended = 0"; } - $companies = $DB->get_recordset_sql("SELECT * FROM {company} - WHERE id IN ( - SELECT companyid FROM {company_users} - WHERE userid = :userid ) - AND parentid NOT IN ( - SELECT companyid FROM {company_users} - WHERE userid = :userid2 ) - $suspendedsql - ORDER BY name", - ['userid' => $USER->id, - 'userid2' => $USER->id, - 'suspended' => $showsuspended]); - } - $companyselect = array(); - foreach ($companies as $company) { - if (empty($company->suspended)) { - $companyselect[$company->id] = format_string($company->name); + // Show the hierarchy if required. + if (!empty($CFG->iomad_show_company_structure)) { + $companies = $DB->get_records_sql_menu("SELECT id, IF (suspended=0, name, concat(name, ' (S)')) AS name FROM {company} + WHERE id IN ( + SELECT companyid FROM {company_users} + WHERE userid = :userid ) + AND parentid NOT IN ( + SELECT companyid FROM {company_users} + WHERE userid = :userid2 ) + $suspendedsql + ORDER BY name", + ['userid' => $USER->id, + 'userid2' => $USER->id, + 'suspended' => $showsuspended]); } else { - $companyselect[$company->id] = format_string($company->name . '(S)'); + $companies = $DB->get_records_sql_menu("SELECT id, IF (suspended=0, name, concat(name, ' (S)')) AS name FROM {company} + WHERE id IN ( + SELECT companyid FROM {company_users} + WHERE userid = :userid ) + $suspendedsql + ORDER BY name", + ['userid' => $USER->id, + 'userid2' => $USER->id, + 'suspended' => $showsuspended]); } - $allchildren = self::get_formatted_child_companies_select($company->id); - $companyselect = $companyselect + $allchildren; } - return $companyselect; + // Show the hierarchy if required. + if (!empty($CFG->iomad_show_company_structure)) { + $companyselect = array(); + foreach ($companies as $id => $companyname) { + $companyselect[$id] = $companyname; + $allchildren = self::get_formatted_child_companies_select($id); + $companyselect = $companyselect + $allchildren; + } + return $companyselect; + } else { + return $companies; + } } private static function get_formatted_child_companies_select($companyid, &$companyarray = [], $prepend = "") { diff --git a/local/iomad_settings/lang/en/local_iomad_settings.php b/local/iomad_settings/lang/en/local_iomad_settings.php index ef7f145c1d5..b8fb4976e18 100644 --- a/local/iomad_settings/lang/en/local_iomad_settings.php +++ b/local/iomad_settings/lang/en/local_iomad_settings.php @@ -77,6 +77,8 @@ $string['iomad_settings:addinstance'] = 'Add a new IOMAD Settings'; $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_show_company_structure'] = 'Show company hierarchy in selector'; +$string['iomad_show_company_structure_help'] = 'If checked child companies will appear indented under the parent company in the company selector. This may cause performance issues for larger sites.'; $string['iomad_sync_department'] = 'Sync company department with profile'; $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'; diff --git a/local/iomad_settings/settings.php b/local/iomad_settings/settings.php index 72830c74d0e..2dc0b2bad2c 100644 --- a/local/iomad_settings/settings.php +++ b/local/iomad_settings/settings.php @@ -41,6 +41,11 @@ get_string('iomad_allow_username_help', 'local_iomad_settings'), 0)); + $settings->add(new admin_setting_configcheckbox('iomad_show_company_structure', + get_string('iomad_show_company_structure', 'local_iomad_settings'), + get_string('iomad_show_company_structure_help', 'local_iomad_settings'), + 1)); + $institutionsync = [get_string('no'), get_string('companyshortname', 'block_iomad_company_admin'), get_string('companyname', 'block_iomad_company_admin')]; diff --git a/local/iomad_settings/version.php b/local/iomad_settings/version.php index 9f45f8940be..60bbe45c252 100644 --- a/local/iomad_settings/version.php +++ b/local/iomad_settings/version.php @@ -23,5 +23,5 @@ $plugin->release = '4.1.9 (Build: 20240212)'; // Human-friendly version name $plugin->component = 'local_iomad_settings'; -$plugin->version = 2023021800; // The (date) version of this plugin. +$plugin->version = 2024041900; // The (date) version of this plugin. $plugin->requires = 2019052000; // Requires this Moodle version.