Commit 10f7c8f3 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Allow institution admin to upload a replacement site logo



Bug #805780

Adds a logo field to the institution edit form.  On upload, the file
is saved as a 'profileicon' artefact, to ensure that it's publicly
visible, and can be requested in multiple sizes by thumb.php.

Change-Id: I7ea50f75b6e5fed8043c5cd9812e82809a6ef600
Signed-off-by: default avatarRichard Mansfield <richard.mansfield@catalyst.net.nz>
parent 304db881
...@@ -298,6 +298,19 @@ if ($institution || $add) { ...@@ -298,6 +298,19 @@ if ($institution || $add) {
'defaultvalue' => $data->defaultmembershipperiod, 'defaultvalue' => $data->defaultmembershipperiod,
'help' => true, 'help' => true,
); );
$elements['logo'] = array(
'type' => 'file',
'title' => get_string('Logo', 'admin'),
'description' => get_string('logodescription', 'admin'),
'maxfilesize' => get_max_upload_size(false),
);
if (!empty($data->logo)) {
$logourl = get_config('wwwroot') . 'thumb.php?type=logobyid&id=' . $data->logo;
$elements['logohtml'] = array(
'type' => 'html',
'value' => '<img src="' . $logourl . '" alt="' . get_string('Logo', 'admin') . '">',
);
}
$elements['theme'] = array( $elements['theme'] = array(
'type' => 'select', 'type' => 'select',
'title' => get_string('theme'), 'title' => get_string('theme'),
...@@ -461,6 +474,32 @@ function institution_validate(Pieform $form, $values) { ...@@ -461,6 +474,32 @@ function institution_validate(Pieform $form, $values) {
$form->set_error('defaultquota', get_string('maxquotatoolow', 'artefact.file')); $form->set_error('defaultquota', get_string('maxquotatoolow', 'artefact.file'));
} }
} }
// Check uploaded logo
if (!empty($values['logo'])) {
require_once('file.php');
require_once('uploadmanager.php');
$um = new upload_manager('logo');
if ($error = $um->preprocess_file()) {
$form->set_error('logo', $error);
return false;
}
$imageinfo = getimagesize($values['logo']['tmp_name']);
if (!$imageinfo || !is_image_type($imageinfo[2])) {
$form->set_error('logo', get_string('filenotimage'));
return false;
}
// Check the file isn't greater than the max allowable size
$width = $imageinfo[0];
$height = $imageinfo[1];
$imagemaxwidth = get_config('imagemaxwidth');
$imagemaxheight = get_config('imagemaxheight');
if ($width > $imagemaxwidth || $height > $imagemaxheight) {
$form->set_error('logo', get_string('profileiconimagetoobig', 'artefact.file', $width, $height, $imagemaxwidth, $imagemaxheight));
}
}
} }
function institution_submit(Pieform $form, $values) { function institution_submit(Pieform $form, $values) {
...@@ -472,6 +511,9 @@ function institution_submit(Pieform $form, $values) { ...@@ -472,6 +511,9 @@ function institution_submit(Pieform $form, $values) {
if ($add) { if ($add) {
$institution = $newinstitution->name = strtolower($values['name']); $institution = $newinstitution->name = strtolower($values['name']);
} }
else {
$oldinstitution = get_record('institution', 'name', $institution);
}
$newinstitution->displayname = $values['displayname']; $newinstitution->displayname = $values['displayname'];
$newinstitution->authplugin = empty($values['authplugin']) ? null : $values['authplugin']; $newinstitution->authplugin = empty($values['authplugin']) ? null : $values['authplugin'];
...@@ -562,10 +604,49 @@ function institution_submit(Pieform $form, $values) { ...@@ -562,10 +604,49 @@ function institution_submit(Pieform $form, $values) {
else { else {
$where = new StdClass; $where = new StdClass;
$where->name = $institution; $where->name = $institution;
$oldtheme = get_field('institution', 'theme', 'name', $institution);
update_record('institution', $newinstitution, $where); update_record('institution', $newinstitution, $where);
} }
// Set the logo after updating the institution, because the institution
// needs to exist before it can own the logo artefact.
if ($values['logo']) {
safe_require('artefact', 'file');
// Entry in artefact table
$data = (object) array(
'institution' => $institution,
'title' => 'logo',
'description' => 'Institution logo',
'note' => $values['logo']['name'],
'size' => $values['logo']['size'],
);
$imageinfo = getimagesize($values['logo']['tmp_name']);
$data->width = $imageinfo[0];
$data->height = $imageinfo[1];
$data->filetype = $imageinfo['mime'];
$artefact = new ArtefactTypeProfileIcon(0, $data);
if (preg_match("/\.([^\.]+)$/", $values['logo']['name'], $saved)) {
$artefact->set('oldextension', $saved[1]);
}
$artefact->commit();
$id = $artefact->get('id');
// Move the file into the correct place.
$directory = get_config('dataroot') . 'artefact/file/profileicons/originals/' . ($id % 256) . '/';
check_dir_exists($directory);
move_uploaded_file($values['logo']['tmp_name'], $directory . $id);
// Delete the old logo
if (!empty($oldinstitution->logo)) {
$oldlogo = new ArtefactTypeProfileIcon($oldinstitution->logo);
$oldlogo->delete();
}
set_field('institution', 'logo', $id, 'name', $institution);
}
delete_records('institution_locked_profile_field', 'name', $institution); delete_records('institution_locked_profile_field', 'name', $institution);
foreach (ArtefactTypeProfile::get_all_fields() as $field => $type) { foreach (ArtefactTypeProfile::get_all_fields() as $field => $type) {
if ($values[$field]) { if ($values[$field]) {
...@@ -588,8 +669,8 @@ function institution_submit(Pieform $form, $values) { ...@@ -588,8 +669,8 @@ function institution_submit(Pieform $form, $values) {
} }
else { else {
$message = get_string('institutionupdatedsuccessfully', 'admin'); $message = get_string('institutionupdatedsuccessfully', 'admin');
if (isset($values['theme']) && $oldtheme != $values['theme'] if (isset($values['theme']) && $oldinstitution->theme != $values['theme']
&& (!empty($oldtheme) || $values['theme'] != 'sitedefault')) { && (!empty($oldinstitution->theme) || $values['theme'] != 'sitedefault')) {
$USER->update_theme(); $USER->update_theme();
$message .= ' ' . get_string('usersseenewthemeonlogin', 'admin'); $message .= ' ' . get_string('usersseenewthemeonlogin', 'admin');
} }
......
...@@ -628,6 +628,7 @@ abstract class ArtefactType { ...@@ -628,6 +628,7 @@ abstract class ArtefactType {
delete_records_select('artefact_access_role', "artefact IN $idstr"); delete_records_select('artefact_access_role', "artefact IN $idstr");
delete_records_select('artefact_access_usr', "artefact IN $idstr"); delete_records_select('artefact_access_usr', "artefact IN $idstr");
execute_sql("UPDATE {usr} SET profileicon = NULL WHERE profileicon IN $idstr"); execute_sql("UPDATE {usr} SET profileicon = NULL WHERE profileicon IN $idstr");
execute_sql("UPDATE {institution} SET logo = NULL WHERE logo IN $idstr");
delete_records_select('artefact', "id IN $idstr"); delete_records_select('artefact', "id IN $idstr");
......
...@@ -623,6 +623,9 @@ $string['defaultmembershipperioddescription'] = 'How long new members remain ass ...@@ -623,6 +623,9 @@ $string['defaultmembershipperioddescription'] = 'How long new members remain ass
$string['showonlineusers'] = 'Show online users'; $string['showonlineusers'] = 'Show online users';
$string['showonlineusersdesc'] = 'The online users to show to members of this institution. With different settings the most permissive institution settings will be used.'; $string['showonlineusersdesc'] = 'The online users to show to members of this institution. With different settings the most permissive institution settings will be used.';
$string['Logo'] = 'Logo';
$string['logodescription'] = 'You can upload an image here that will be displayed to your institution\'s members in place of the standard header logo. For best results this image should have the same dimensions as the site logo in your institution\'s theme.';
$string['authenticatedby'] = 'Authentication Method'; $string['authenticatedby'] = 'Authentication Method';
$string['authenticatedbydescription'] = 'How this user authenticates to Mahara'; $string['authenticatedbydescription'] = 'How this user authenticates to Mahara';
$string['remoteusername'] = 'Username for external authentication'; $string['remoteusername'] = 'Username for external authentication';
......
...@@ -137,6 +137,24 @@ switch ($type) { ...@@ -137,6 +137,24 @@ switch ($type) {
header('Content-type: ' . 'image/png'); header('Content-type: ' . 'image/png');
readfile_exit($THEME->get_path('images/no_userphoto.png')); readfile_exit($THEME->get_path('images/no_userphoto.png'));
case 'logobyid':
$filedata = get_record('artefact_file_files', 'artefact', param_integer('id'));
if ($path = get_dataroot_image_path('artefact/file/profileicons', $filedata->fileid, get_imagesize_parameters())) {
if ($filedata->filetype) {
header('Content-type: ' . $filedata->filetype);
$maxage = 604800;
header('Expires: '. gmdate('D, d M Y H:i:s', time() + $maxage) .' GMT');
header('Cache-Control: max-age=' . $maxage);
header('Pragma: public');
readfile_exit($path);
}
}
// Nothing found, use the site logo.
header('Content-type: ' . 'image/png');
readfile_exit($THEME->get_path('images/site-logo.png'));
case 'blocktype': case 'blocktype':
$bt = param_alpha('bt'); // blocktype $bt = param_alpha('bt'); // blocktype
$ap = param_alpha('ap', null); // artefact plugin (optional) $ap = param_alpha('ap', null); // artefact plugin (optional)
......
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