Commit 8a1c897a authored by Aaron Wells's avatar Aaron Wells

Base institution.name value on the institution's display name

Bug 1518796

behatnotneeded: Covered by existing tests

Change-Id: I996e4562441140a6163dbf0b9dff4ee2894ffd83
parent 8e4e4e3f
......@@ -653,26 +653,9 @@ EOF;
exit;
}
// Generate random lower-case alpha-only institution name.
function generate_institution_name() {
$i = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, 6);
if ($institution = get_record('institution', 'name', $i)) {
// try again
generate_institution_name();
}
else {
return $i;
}
}
function institution_validate(Pieform $form, $values) {
global $USER, $institution, $add;
// Automatically generate institution name when adding new institution
if ($add) {
$institution = generate_institution_name();
}
if ($USER->get('admin') || get_config_plugin('artefact', 'file', 'institutionaloverride')) {
if (get_config_plugin('artefact', 'file', 'maxquotaenabled') && get_config_plugin('artefact', 'file', 'maxquota') < $values['defaultquota']) {
$form->set_error('defaultquota', get_string('maxquotatoolow', 'artefact.file'));
......@@ -724,6 +707,7 @@ function institution_submit(Pieform $form, $values) {
db_begin();
// Update the basic institution record...
if ($add) {
$institution = generate_institution_name($values['displayname']);
$newinstitution = new Institution();
$newinstitution->initialise($institution, $values['displayname']);
$institution = $newinstitution->name;
......
......@@ -4432,6 +4432,41 @@ function generate_urlid($dirty, $default, $minlength=3, $maxlength=100) {
return $s;
}
/**
* Generate a valid name for the institution.name column, based on the specified display name
*
* @param string $displayname
* @return string
*/
function generate_institution_name($displayname) {
// iconv can crash on strings that are too long, so truncate before converting
$basename = mb_substr($displayname, 0, 255);
$basename = iconv('UTF-8', 'ASCII//TRANSLIT', $displayname);
$basename = strtolower($basename);
$basename = preg_replace('/[^a-z]/', '', $basename);
if (strlen($basename) < 2) {
$basename = 'inst' . $basename;
}
else {
$basename = substr($basename, 0, 255);
}
// Make sure the name is unique. If it is not, add a suffix and see if
// that makes it unique
$finalname = $basename;
$suffix = 'a';
while (record_exists('institution', 'name', $finalname)) {
// Add the suffix but make sure the name length doesn't go over 255
$finalname = substr($basename, 0, 255 - strlen($suffix)) . $suffix;
// Will iterate a-z, aa-az, ba-bz, etc.
// See: http://php.net/manual/en/language.operators.increment.php
$suffix++;
}
return $finalname;
}
/**
* Sorts an array by one of the value fields
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment