Commit fd9b74d4 authored by Nigel McNie's avatar Nigel McNie
Browse files

Use correct validation on institution name when creating via promiscuous mode. Fixes #2092.

Previously, the code was only making sure whitespace was removed, but other parts of Mahara depend on this field having only alphabetical characters.

Now the code makes sure only alphabetical characters are in the string, and will cycle through the letters a - z attempting to generate a new institution name if the one generated is already taken.

If someone needs more than 26 peers all with the same name... well tough :)
parent a6482fe8
......@@ -200,8 +200,28 @@ class Peer {
if (null == $institution) {
$institution = new Institution;
$iname = preg_replace('/\s+/', '', $this->name);
$institution->name = preg_replace('/\s+/', '', $this->name);
$institution->name = preg_replace('/[^a-zA-Z]/', '', $this->name);
// Check that the institution name has not already been taken.
// If it has, we change it until we find a name that works
$existinginstitutionnames = get_column('institution', 'name');
if (in_array($institution->name, $existinginstitutionnames)) {
$success = false;
foreach (range('a', 'z') as $character) {
$testname = $institution->name . $character;
if (!in_array($testname, $existinginstitutionnames)) {
$success = true;
$institution->name = $testname;
if (!$success) {
// We couldn't find a unique name. Noes!
throw new RemoteServerException('Could not create a unique institution name');
$institution->displayname = $this->name;
$this->institution = $institution->name;
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