Skip to content

Commit

Permalink
Badge: Show error message on badge image upload error (ILIAS-eLearnin…
Browse files Browse the repository at this point in the history
…g#7337)

(cherry picked from commit 939514d)
  • Loading branch information
mBeym authored and mjansenDatabay committed Apr 11, 2024
1 parent ed99077 commit e9ee5c1
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 8 deletions.
18 changes: 15 additions & 3 deletions Services/Badge/classes/class.ilBadge.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*
*********************************************************************/

use ILIAS\Services\Badge\BadgeException;

/**
* @author Jörg Lützenkirchen <[email protected]>
*/
Expand Down Expand Up @@ -317,7 +319,7 @@ public function getImage(): string
}

/**
* @throws ilException
* @throws BadgeException
*/
public function uploadImage(
array $a_upload_meta
Expand All @@ -327,12 +329,20 @@ public function uploadImage(
$this->setImage($a_upload_meta["name"]);
$path = $this->getImagePath();

if (ilFileUtils::moveUploadedFile($a_upload_meta["tmp_name"], $this->getImagePath(false), $path)) {
$this->update();
try {
if (ilFileUtils::moveUploadedFile($a_upload_meta['tmp_name'], $this->getImagePath(false), $path)) {
$this->update();
}
} catch (ilException $e) {
throw BadgeException::moveUploadedBadgeImageFailed($this, $e);
}

}
}

/**
* @throws BadgeException
*/
public function importImage(
string $a_name,
string $a_file
Expand All @@ -342,6 +352,8 @@ public function importImage(
copy($a_file, $this->getImagePath()); // #18280

$this->update();
} else {
throw BadgeException::uploadedBadgeImageFileNotFound($this);
}
}

Expand Down
40 changes: 35 additions & 5 deletions Services/Badge/classes/class.ilBadgeManagementGUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*
*********************************************************************/

use ILIAS\Services\Badge\BadgeException;

/**
* Class ilBadgeManagementGUI
*
Expand Down Expand Up @@ -384,11 +386,32 @@ protected function saveBadge(): void

$badge->create();

if ($form->getInput('img_mode') === 'up') {
$badge->uploadImage($_FILES['img']);
} else {


try {
if ($form->getInput('img_mode') === 'up') {
$badge->uploadImage($_FILES['img']);
} else {
$tmpl = new ilBadgeImageTemplate($form->getInput('tmpl'));
$badge->importImage($tmpl->getImage(), $tmpl->getImagePath());
$badge->importImage($tmpl->getImage(), $tmpl->getImagePath());
}
} catch (BadgeException $e) {
$delete = false;
switch ($e->getCode()) {
case BadgeException::EXCEPTION_FILE_NOT_FOUND:
$this->tpl->setOnScreenMessage('failure', $lng->txt('badge_uploaded_image_file_not_found'), true);
$delete = true;
break;
case BadgeException::EXCEPTION_MOVE_UPLOADED_IMAGE_FAILED:
$this->tpl->setOnScreenMessage('failure', $lng->txt('badge_create_image_processing_failed'), true);
$delete = true;
break;
}

if ($delete) {
$badge->delete();
$ilCtrl->redirect($this, "listBadges");
}
}

$this->tpl->setOnScreenMessage('success', $lng->txt('settings_saved'), true);
Expand Down Expand Up @@ -483,7 +506,14 @@ protected function updateBadge(): void

$badge->update();

$badge->uploadImage($_FILES['img']);
try {
$badge->uploadImage($_FILES["img"]);
} catch (BadgeException $e) {
if ($e->getCode() === BadgeException::EXCEPTION_MOVE_UPLOADED_IMAGE_FAILED) {
$this->tpl->setOnScreenMessage('failure', $lng->txt('badge_update_image_processing_failed'), true);
$ilCtrl->redirect($this, "listBadges");
}
}

$this->tpl->setOnScreenMessage('success', $lng->txt('settings_saved'), true);
$ilCtrl->redirect($this, 'listBadges');
Expand Down
49 changes: 49 additions & 0 deletions Services/Badge/classes/ilBadgeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace ILIAS\Services\Badge;

use ilBadge;
use ilException;

class BadgeException extends ilException
{
public const EXCEPTION_FILE_NOT_FOUND = 1;
public const EXCEPTION_MOVE_UPLOADED_IMAGE_FAILED = 2;

private ilBadge $badge;

public function __construct(int $code, ilBadge $badge, ?ilException $previous_exception = null)
{
parent::__construct('', $code, $previous_exception);
$this->badge = $badge;

}

public static function uploadedBadgeImageFileNotFound(ilBadge $badge, ?ilException $previous_exception = null): self
{
return new self(self::EXCEPTION_FILE_NOT_FOUND, $badge, $previous_exception);
}

public static function moveUploadedBadgeImageFailed(ilBadge $badge, ?ilException $previous_exception = null): self
{
return new self(self::EXCEPTION_MOVE_UPLOADED_IMAGE_FAILED, $badge, $previous_exception);
}
}
3 changes: 3 additions & 0 deletions lang/ilias_de.lang
Original file line number Diff line number Diff line change
Expand Up @@ -2184,6 +2184,7 @@ badge#:#badge_badge#:#Badge
badge#:#badge_course_lp#:#Nach Leistung vergebenes Badge
badge#:#badge_course_lp_invalid#:#Die folgenden Objekte verwenden einen nicht unterstützten Lernfortschrittsmodus: %s.
badge#:#badge_create#:#Badge erstellen
badge#:#badge_create_image_processing_failed#:#Der Badge konnte nicht angelegt werden, beim Verarbeiten des Bildes trat ein Fehler auf.
badge#:#badge_criteria#:#Kriterien
badge#:#badge_crs_merit#:#Händisch vergebenes Badge
badge#:#badge_deletion_confirmation#:#Wollen Sie wirklich die folgenden Badges und alle ihre jeweiligen Zuweisungen löschen?
Expand Down Expand Up @@ -2232,6 +2233,8 @@ badge#:#badge_template_types#:#Gültige Typen
badge#:#badge_template_types_all#:#Alle
badge#:#badge_template_types_specific#:#Bestimmte
badge#:#badge_types#:#Typen
badge#:#badge_update_image_processing_failed#:#Der Badge konnte nicht aktualisiert werden, beim verarbeiten des Bildes trat ein Fehler auf.
badge#:#badge_uploaded_image_file_not_found#:#Es konnte kein hochgeladenes Bild für den Badge gefunden werden.
badge#:#badge_user_profile#:#Profil veröffentlicht###Modified as part of gender mainstreaming activities for ILIAS 8
badge#:#badge_valid#:#Gültig bis
badge#:#criteria#:#Kriterium
Expand Down
3 changes: 3 additions & 0 deletions lang/ilias_en.lang
Original file line number Diff line number Diff line change
Expand Up @@ -2185,6 +2185,7 @@ badge#:#badge_badge#:#Badge
badge#:#badge_course_lp#:#Course LP
badge#:#badge_course_lp_invalid#:#The following objects have a LP mode which is not supported: %s.
badge#:#badge_create#:#Create Badge
badge#:#badge_create_image_processing_failed#:#The badge could not be created, an error occurred while processing the image.
badge#:#badge_criteria#:#Criteria
badge#:#badge_crs_merit#:#Merit
badge#:#badge_deletion_confirmation#:#Are you sure you want to delete the following badges and all their respective assignments?
Expand Down Expand Up @@ -2233,6 +2234,8 @@ badge#:#badge_template_types#:#Valid Types
badge#:#badge_template_types_all#:#All
badge#:#badge_template_types_specific#:#Specific
badge#:#badge_types#:#Types
badge#:#badge_update_image_processing_failed#:#The badge could not be updated, an error occurred while processing the image.
badge#:#badge_uploaded_image_file_not_found#:#No uploaded image for the badge could be found.
badge#:#badge_user_profile#:#User Profil
badge#:#badge_valid#:#Valid until
badge#:#criteria#:#Criteria
Expand Down

0 comments on commit e9ee5c1

Please sign in to comment.