Display: Fixes to course image header feature PR by @TheTomcat14 - refs #4599

pull/4716/head
Yannick Warnier 2 years ago
commit cf1c3420fc
  1. 2
      .htaccess
  2. 57
      main/course_info/infocours.php
  3. 2
      main/inc/ajax/course.ajax.php
  4. 25
      main/inc/lib/api.lib.php
  5. 22
      main/inc/lib/banner.lib.php
  6. 104
      main/inc/lib/course.lib.php
  7. 11
      main/inc/lib/exercise.lib.php
  8. 12
      main/inc/lib/pdf.lib.php
  9. 35
      main/inc/lib/pear/HTML/QuickForm/file.php
  10. 26
      main/inc/lib/template.lib.php
  11. 6
      main/install/configuration.dist.php
  12. 11
      main/survey/surveyUtil.class.php
  13. 13
      main/wiki/wiki.inc.php

@ -46,6 +46,8 @@ RewriteRule ^courses/([^/]+)/work/(.*)$ main/work/download.php?file=work/$2&cDir
RewriteRule ^courses/([^/]+)/course-pic85x85.png$ main/inc/ajax/course.ajax.php?a=get_course_image&code=$1&image=course_image_source [QSA,L]
RewriteRule ^courses/([^/]+)/course-pic.png$ main/inc/ajax/course.ajax.php?a=get_course_image&code=$1&image=course_image_large_source [QSA,L]
RewriteRule ^courses/([^/]+)/course-email-pic-cropped.png$ main/inc/ajax/course.ajax.php?a=get_course_image&code=$1&image=course_email_image_source [QSA,L]
RewriteRule ^courses/([^/]+)/course-email-pic.png$ main/inc/ajax/course.ajax.php?a=get_course_image&code=$1&image=course_email_image_large_source [QSA,L]
# Redirect all courses/ to app/courses/
RewriteRule ^courses/([^/]+)/(.*)$ app/courses/$1/$2 [QSA,L]

@ -85,14 +85,22 @@ $form->addHtml('
<div class="panel-body">
');
// Display regular image
$image = '';
// Display course picture
$course_path = api_get_path(SYS_COURSE_PATH).$currentCourseRepository; // course path
if (file_exists($course_path.'/course-pic85x85.png')) {
if (file_exists($course_path.'/course-pic85x85.png') || file_exists($course_path.'/course-email-pic-cropped.png')) {
$image = '<div class="row">';
$course_web_path = api_get_path(WEB_COURSE_PATH).$currentCourseRepository; // course web path
$course_medium_image = $course_web_path.'/course-pic85x85.png?'.rand(1, 1000); // redimensioned image 85x85
$image = '<div class="row"><label class="col-md-2 control-label">'.get_lang('Image').'</label>
<div class="col-md-8"><img src="'.$course_medium_image.'" /></div></div>';
if (file_exists($course_path.'/course-pic85x85.png')) {
$course_medium_image = $course_web_path.'/course-pic85x85.png?'.rand(1, 1000); // resized image
$image .= '<label class="col-md-2 control-label">'.get_lang('Image').'</label><div class="col-md-4"><img src="'.$course_medium_image.'" /></div>';
}
if (file_exists($course_path.'/course-email-pic-cropped.png')) {
$course_medium_image = $course_web_path.'/course-email-pic-cropped.png?'.rand(1, 1000); // redimensioned image
$image .= '<label class="col-md-2 control-label">'.get_lang('EmailPicture').'</label><div class="col-md-4"><img src="'.$course_medium_image.'" /></div>';
}
$image .= '</div>';
}
$form->addHtml($image);
$form->addText('title', get_lang('Title'), true);
@ -153,7 +161,7 @@ $(function() {
// Picture
$form->addFile(
'picture',
get_lang('AddPicture'),
[get_lang('AddPicture'), get_lang('AddPictureComment')],
['id' => 'picture', 'class' => 'picture-form', 'crop_image' => true]
);
@ -166,6 +174,24 @@ $form->addRule(
);
$form->addElement('checkbox', 'delete_picture', null, get_lang('DeletePicture'));
// Email Picture
$form->addFile(
'email_picture',
[get_lang('AddEmailPicture'), get_lang('AddEmailPictureComment')],
['id' => 'email_picture', 'class' => 'picture-form', 'crop_image' => true, 'crop_min_ratio' => '250 / 70', 'crop_max_ratio' => '10']
);
$allowed_picture_types = api_get_supported_image_extensions(false);
$form->addRule(
'email_picture',
get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')',
'filetype',
$allowed_picture_types
);
$form->addElement('checkbox', 'delete_email_picture', null, get_lang('DeleteEmailPicture'));
if (api_get_setting('pdf_export_watermark_by_course') === 'true') {
$url = PDF::get_watermark($course_code);
$form->addText('pdf_export_watermark_text', get_lang('PDFExportWatermarkTextTitle'), false, ['size' => '60']);
@ -1111,6 +1137,22 @@ if ($form->validate() && is_settings_editable()) {
);
}
// update email picture
$emailPicture = $_FILES['email_picture'];
if (!empty($emailPicture['name'])) {
CourseManager::updateCourseEmailPicture(
$_course,
$emailPicture['tmp_name'],
$updateValues['email_picture_crop_result']
);
Event::addEvent(
LOG_COURSE_SETTINGS_CHANGED,
'course_email_picture',
$emailPicture['name']
);
}
if ($visibilityChangeable && isset($updateValues['visibility'])) {
$visibility = $updateValues['visibility'];
} else {
@ -1122,6 +1164,11 @@ if ($form->validate() && is_settings_editable()) {
CourseManager::deleteCoursePicture($course_code);
}
$deleteEmailPicture = isset($updateValues['delete_email_picture']) ? $updateValues['delete_email_picture'] : '';
if ($deleteEmailPicture) {
CourseManager::deleteCourseEmailPicture($course_code);
}
global $_configuration;
$urlId = api_get_current_access_url_id();
if (isset($_configuration[$urlId]) &&

@ -33,7 +33,7 @@ switch ($action) {
case 'get_course_image':
$courseId = ChamiloApi::getCourseIdByDirectory($_REQUEST['code']);
$courseInfo = api_get_course_info_by_id($courseId);
$image = isset($_REQUEST['image']) && in_array($_REQUEST['image'], ['course_image_large_source', 'course_image_source']) ? $_REQUEST['image'] : '';
$image = isset($_REQUEST['image']) && in_array($_REQUEST['image'], ['course_image_large_source', 'course_image_source', 'course_email_image_large_source', 'course_email_image_source']) ? $_REQUEST['image'] : '';
if ($courseInfo && $image) {
// Arbitrarily set a cache of 10' for the course image to
// avoid hammering the server with otherwise unfrequently

@ -2535,6 +2535,28 @@ function api_format_course_array($course_data)
$_course['course_image_large'] = $url_image;
// email pictures
// Course image
$url_image = null;
$_course['course_email_image_source'] = '';
$mailPicture = $courseSys.'/course-email-pic-cropped.png';
if (file_exists($mailPicture)) {
$url_image = $webCourseHome.'/course-email-pic-cropped.png';
$_course['course_email_image_source'] = $mailPicture;
}
$_course['course_email_image'] = $url_image;
// Course large image
$url_image = null;
$_course['course_email_image_large_source'] = '';
$mailPicture = $courseSys.'/course-email-pic.png';
if (file_exists($mailPicture)) {
$url_image = $webCourseHome.'/course-email-pic.png';
$_course['course_email_image_large_source'] = $mailPicture;
}
$_course['course_email_image_large'] = $url_image;
return $_course;
}
@ -9636,6 +9658,9 @@ function api_mail_html(
if (isset($additionalParameters['link'])) {
$mailView->assign('link', $additionalParameters['link']);
}
if (isset($additionalParameters['logo'])) {
$mailView->assign('logo', $additionalParameters['logo']);
}
$mailView->assign('mail_header_style', api_get_configuration_value('mail_header_style'));
$mailView->assign('mail_content_style', api_get_configuration_value('mail_content_style'));
$mailView->assign('include_ldjson', (empty(api_get_mail_configuration_value('EXCLUDE_JSON')) ? true : false));

@ -213,6 +213,28 @@ function return_logo($theme = '', $responsive = true)
$class = '';
}
if (api_get_configuration_value('mail_header_from_custom_course_logo') == true) {
// check if current page is a course page
$courseCode = api_get_course_id();
if (!empty($courseCode)) {
$course = api_get_course_info($courseCode);
if (!empty($course) && !empty($course['course_email_image_large'])) {
$image = \Display::img(
$course['course_email_image_large'],
$course['name'],
[
'title' => $course['name'],
'class' => $class,
'id' => 'header-logo',
]
);
return \Display::url($image, $course['course_public_url']);
}
}
}
return ChamiloApi::getPlatformLogo(
$theme,
[

@ -7403,4 +7403,108 @@ class CourseManager
$courseFieldValue = new ExtraFieldValue('course');
$courseFieldValue->saveFieldValues($params);
}
/**
* Update course email picture.
*
* @param array $courseInfo
* @param string $sourceFile the full system name of the image from which course picture will be created
* @param string $cropParameters Optional string that contents "x,y,width,height" of a cropped image format
*
* @return bool Returns the resulting. In case of internal error or negative validation returns FALSE.
*/
public static function updateCourseEmailPicture(
array $courseInfo,
string $sourceFile = null,
string $cropParameters = null
): bool {
if (empty($courseInfo)) {
return false;
}
// Course path
$store_path = api_get_path(SYS_COURSE_PATH).$courseInfo['path'];
// Image name for courses
$course_image = $store_path.'/course-email-pic.png';
$course_medium_image = $store_path.'/course-email-pic-cropped.png';
if (file_exists($course_image)) {
unlink($course_image);
}
if (file_exists($course_medium_image)) {
unlink($course_medium_image);
}
//Crop the image to adjust 4:3 ratio
$image = new Image($sourceFile);
$image->crop($cropParameters);
//Resize the images in two formats
$medium = new Image($sourceFile);
$medium->resize(85);
$medium->send_image($course_medium_image, -1, 'png');
$normal = new Image($sourceFile);
$normal->resize(250);
$normal->send_image($course_image, -1, 'png');
return $medium && $normal; //if both ops were ok, return true, otherwise false
}
/**
* Deletes the course email picture.
*
* @param string $courseCode
*/
public static function deleteCourseEmailPicture(string $courseCode): void
{
$course_info = api_get_course_info($courseCode);
// course path
$storePath = api_get_path(SYS_COURSE_PATH).$course_info['path'];
// image name for courses
$courseImage = $storePath.'/course-email-pic.png';
$courseMediumImage = $storePath.'/course-email-pic-cropped.png';
if (file_exists($courseImage)) {
unlink($courseImage);
}
if (file_exists($courseMediumImage)) {
unlink($courseMediumImage);
}
}
/**
* Get the course logo
*
* @param array $course array containing course info, @see api_get_course_info()
* @param array $attributes Array containing extra attributes for the image tag
*
* @return string|null
*/
public static function getCourseEmailPicture($course, $attributes = null)
{
$logo = null;
if (!empty($course)
&& !empty($course['course_email_image_large_source'])
&& file_exists($course['course_email_image_large_source'])
) {
if (is_null($attributes)) {
$attributes = [
'title' => $course['name'],
'class' => 'img-responsive',
'id' => 'header-logo',
'width' => 250,
];
}
$logo = \Display::url(
\Display::img(
$course['course_email_image_large'],
$course['name'],
$attributes
),
api_get_path(WEB_PATH) . 'index.php'
);
}
return $logo;
}
}

@ -7046,10 +7046,16 @@ EOT;
$exercise_stat_info,
$studentId
);
$extraParameters = [];
if (api_get_configuration_value('mail_header_from_custom_course_logo') == true) {
$extraParameters = ['logo' => CourseManager::getCourseEmailPicture($courseInfo)];
}
foreach ($emailList as $email) {
if (empty($email)) {
continue;
}
api_mail_html(
null,
$email,
@ -7058,7 +7064,10 @@ EOT;
null,
null,
[],
$attachments
$attachments,
false,
$extraParameters,
''
);
}
}

@ -748,7 +748,17 @@ class PDF
}
}
$organization = ChamiloApi::getPlatformLogo('', [], true, true);
$organization = null;
// try getting the course logo
if (api_get_configuration_value('mail_header_from_custom_course_logo') == true) {
$organization = CourseManager::getCourseEmailPicture($courseInfo, []);
}
// only show platform logo in mail if no course photo available
if (empty($organization)) {
$organization = ChamiloApi::getPlatformLogo('', [], false, true);
}
// Use custom logo image.
$pdfLogo = api_get_setting('pdf_logo_header');
if ($pdfLogo === 'true') {

@ -253,6 +253,8 @@ class HTML_QuickForm_file extends HTML_QuickForm_input
{
$id = $this->getAttribute('id');
$ratio = 'aspectRatio: 16 / 9';
$cropMove = '';
if (!empty($param['ratio'])) {
$ratio = 'aspectRatio: '.$param['ratio'].',';
}
@ -262,6 +264,28 @@ class HTML_QuickForm_file extends HTML_QuickForm_input
$scalable = $param['scalable'];
}
if (!empty($param['maxRatio']) && !empty($param['minRatio'])) {
$ratio = 'autoCropArea: 1, ';
$scalable = 'true';
$cropMove = ',cropmove: function (e) {
var cropBoxData = $image.cropper(\'getCropBoxData\');
var minAspectRatio = ' . $param['minRatio'] . ';
var maxAspectRatio = ' . $param['maxRatio'] . ';
var cropBoxWidth = cropBoxData.width;
var aspectRatio = cropBoxWidth / cropBoxData.height;
if (aspectRatio < minAspectRatio) {
$image.cropper(\'setCropBoxData\', {
height: cropBoxWidth / minAspectRatio
});
} else if (aspectRatio > maxAspectRatio) {
$image.cropper(\'setCropBoxData\', {
height: cropBoxWidth / maxAspectRatio
});
}
}';
}
return '<script>
$(function() {
var $inputFile = $(\'#'.$id.'\'),
@ -285,7 +309,6 @@ class HTML_QuickForm_file extends HTML_QuickForm_input
function imageCropper() {
$formGroup.show();
$cropButton.show();
$image
.cropper(\'destroy\')
.cropper({
@ -301,6 +324,7 @@ class HTML_QuickForm_file extends HTML_QuickForm_input
// Output the result data for cropping image.
$input.val(e.x + \',\' + e.y + \',\' + e.width + \',\' + e.height);
}
' . $cropMove . '
});
}
@ -361,7 +385,14 @@ class HTML_QuickForm_file extends HTML_QuickForm_input
if (!empty($this->_attributes['crop_scalable'])) {
$scalable = $this->_attributes['crop_scalable'];
}
$js = $this->getElementJS(array('ratio' => $ratio, 'scalable' => $scalable));
$maxRatio = $minRatio = null;
if (!empty($this->_attributes['crop_min_ratio']) && !empty($this->_attributes['crop_max_ratio'])) {
$minRatio = $this->_attributes['crop_min_ratio'];
$maxRatio = $this->_attributes['crop_max_ratio'];
}
$js = $this->getElementJS(array('ratio' => $ratio, 'scalable' => $scalable, 'minRatio' => $minRatio, 'maxRatio' => $maxRatio));
}
if ($this->isFrozen()) {

@ -2075,11 +2075,27 @@ class Template
$portalImageMeta .= '<meta property="twitter:image:alt" content="'.$imageAlt.'" />'."\n";
}
} else {
$logo = ChamiloApi::getPlatformLogoPath($this->theme);
if (!empty($logo)) {
$portalImageMeta = '<meta property="og:image" content="'.$logo.'" />'."\n";
$portalImageMeta .= '<meta property="twitter:image" content="'.$logo.'" />'."\n";
$portalImageMeta .= '<meta property="twitter:image:alt" content="'.$imageAlt.'" />'."\n";
if (api_get_configuration_value('mail_header_from_custom_course_logo') == true) {
// check if current page is a course page
$courseId = api_get_course_int_id();
if (!empty($courseId)) {
$course = api_get_course_info_by_id($courseId);
if (!empty($course) && !empty($course['course_email_image_large'])) {
$portalImageMeta = '<meta property="og:image" content="'.$course['course_email_image_large'].'" />'."\n";
$portalImageMeta .= '<meta property="twitter:image" content="'.$course['course_email_image_large'].'" />'."\n";
$portalImageMeta .= '<meta property="twitter:image:alt" content="'.$imageAlt.'" />'."\n";
}
}
}
if (empty($portalImageMeta)) {
$logo = ChamiloApi::getPlatformLogoPath($this->theme);
if (!empty($logo)) {
$portalImageMeta = '<meta property="og:image" content="'.$logo.'" />'."\n";
$portalImageMeta .= '<meta property="twitter:image" content="'.$logo.'" />'."\n";
$portalImageMeta .= '<meta property="twitter:image:alt" content="'.$imageAlt.'" />'."\n";
}
}
}

@ -2448,10 +2448,10 @@ INSERT INTO extra_field_options (field_id, option_value, display_text, priority,
//Hides the link to the course catalog in the menu when the catalog is public.
// $_configuration['catalog_hide_public_link'] = false;
// KEEP THIS AT THE END
// -------- Custom DB changes
// Add user activation by confirmation email
// This option prevents the new user to login in the platform if your account is not confirmed via email
// You need add a new option called "confirmation" to the registration settings
//INSERT INTO settings_options (variable, value, display_text) VALUES ('allow_registration', 'confirmation', 'MailConfirmation');
// ------ (End) Custom DB changes
// Enable use of a custom course logo in mail & PDF headers
// $_configuration['mail_header_from_custom_course_logo'] = false;

@ -2993,6 +2993,11 @@ class SurveyUtil
true
);
} else {
$extraParameters = [];
if (api_get_configuration_value('mail_header_from_custom_course_logo') == true) {
$extraParameters = ['logo' => CourseManager::getCourseEmailPicture($_course)];
}
@api_mail_html(
'',
$invitedUser,
@ -3000,7 +3005,11 @@ class SurveyUtil
$full_invitation_text,
$sender_name,
$sender_email,
$replyto
$replyto,
[],
false,
$extraParameters,
''
);
}
}

@ -2253,6 +2253,12 @@ class Wiki
}
///make and send email
if ($allow_send_mail) {
$extraParameters = [];
if (api_get_configuration_value('mail_header_from_custom_course_logo') == true) {
$extraParameters = ['logo' => CourseManager::getCourseEmailPicture($_course)];
}
while ($row = Database::fetch_array($result)) {
$userinfo = api_get_user_info(
$row['user_id']
@ -2289,7 +2295,12 @@ class Wiki
$email_subject,
$email_body,
$sender_name,
$sender_email
$sender_email,
[],
[],
false,
$extraParameters,
''
);
}
}

Loading…
Cancel
Save