Skip to content

Commit

Permalink
IOMAD: Add the option to remove hierachies in the company selector to…
Browse files Browse the repository at this point in the history
… speed up process on larger sites. Also made the process more efficient for marking suspended companies
  • Loading branch information
turf212 committed Apr 19, 2024
1 parent 928ef62 commit d277a6c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 27 deletions.
76 changes: 50 additions & 26 deletions local/iomad/lib/company.php
Expand Up @@ -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 = "") {
Expand Down
2 changes: 2 additions & 0 deletions local/iomad_settings/lang/en/local_iomad_settings.php
Expand Up @@ -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';
Expand Down
5 changes: 5 additions & 0 deletions local/iomad_settings/settings.php
Expand Up @@ -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')];
Expand Down
2 changes: 1 addition & 1 deletion local/iomad_settings/version.php
Expand Up @@ -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.

0 comments on commit d277a6c

Please sign in to comment.