Illustration - Refactor code, add "crop" property in ResourceFile.

pull/3016/head
Julio Montoya 5 years ago
parent 9f183abd0e
commit 8cd85a7c69
  1. 4
      main/auth/courses_categories.php
  2. 42
      main/course_info/infocours.php
  3. 21
      main/create_course/add_course.php
  4. 3
      main/inc/lib/api.lib.php
  5. 81
      main/inc/lib/course.lib.php
  6. 7
      src/CoreBundle/Controller/ResourceController.php
  7. 8
      src/CoreBundle/Entity/Illustration.php
  8. 17
      src/CoreBundle/Entity/Resource/AbstractResource.php
  9. 28
      src/CoreBundle/Entity/Resource/ResourceFile.php
  10. 4
      src/CoreBundle/Migrations/Schema/V200/Version20.php
  11. 10
      src/CoreBundle/Repository/ResourceRepository.php
  12. 4
      src/CourseBundle/Component/CourseCopy/CourseRecycler.php

@ -326,8 +326,8 @@ function returnThumbnail($course)
true,
true
);
$repo = Container::getCourseRepository();
$illustration = $repo->getIllustration($courseEntity);
$illustration = $courseEntity->getResourceNodeIllustration();
if ($illustration) {
$courseMediumImage = Container::getRouter()->generate(
'core_tool_resource',

@ -22,10 +22,13 @@ $nameTools = get_lang('Settings');
api_protect_course_script(true);
api_block_anonymous_users();
$_course = api_get_course_info();
$courseEntity = api_get_course_entity();
$currentCourseRepository = $_course['path'];
$isAllowToEdit = api_is_course_admin() || api_is_platform_admin();
$course_code = api_get_course_id();
$courseId = api_get_course_int_id();
$repo = Container::getCourseRepository();
$em = $repo->getEntityManager();
$isEditable = true;
if (!$isAllowToEdit) {
@ -85,9 +88,13 @@ $form->addHtml(card_settings_open('course_settings', get_lang('Course settings')
$image = '';
// Display course picture
$course_path = api_get_path(SYS_COURSE_PATH).$currentCourseRepository; // course path
if (file_exists($course_path.'/course-pic85x85.png')) {
$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
$illustration = $courseEntity->getResourceNodeIllustration();
if (!empty($illustration)) {
$course_medium_image = Container::getRouter()->generate(
'core_tool_resource',
['id' => $illustration->getId()]
);
$image = '<div class="row"><label class="col-md-2 control-label">'.get_lang('Image').'</label>
<div class="col-md-8"><img class="img-thumbnail" src="'.$course_medium_image.'" /></div></div>';
}
@ -944,20 +951,35 @@ if ($form->validate() && $isEditable) {
// update course picture
$picture = $_FILES['picture'];
if (!empty($picture['name'])) {
$picture_uri = CourseManager::update_course_picture(
$_course,
$picture['name'],
$picture['tmp_name'],
$updateValues['picture_crop_result']
);
$uploadFile = $request->files->get('picture');
// Remove if exists @todo just replace don't delete everything.
if (!empty($courseEntity->getResourceNodeIllustration())) {
$em->remove($courseEntity->getResourceNodeIllustration());
$em->flush();
}
$illustration = new \Chamilo\CoreBundle\Entity\Illustration();
$em->persist($illustration);
$repo->addResourceNode($illustration, api_get_user_entity(api_get_user_id()), $courseEntity);
$file = $repo->addFileToResource($illustration, $uploadFile);
if ($file) {
$file->setCrop($updateValues['picture_crop_result']);
$em->persist($file);
$em->persist($illustration);
$em->flush();
}
}
$visibility = $updateValues['visibility'];
$deletePicture = isset($updateValues['delete_picture']) ? $updateValues['delete_picture'] : '';
if ($deletePicture) {
CourseManager::deleteCoursePicture($course_code);
$image = $courseEntity->getResourceNodeIllustration();
if ($image) {
$em->remove($image);
$em->flush();
}
}
global $_configuration;

@ -361,29 +361,14 @@ if ($form->validate()) {
// @todo add in repository
$course = $repo->find($course_info['real_id']);
$illustration = new \Chamilo\CoreBundle\Entity\Illustration();
$illustration->setName('course_picture');
$repo->addResourceNode($illustration, api_get_user_entity(api_get_user_id()), $course);
$repo->addFileToResource($illustration, $uploadFile);
$file = $repo->addFileToResource($illustration, $uploadFile);
$file->setCrop($course_values['picture_crop_result']);
$em->persist($file);
$em->persist($illustration);
$em->flush();
}
// update course picture
/*$picture = $_FILES['picture'];
if (!empty($picture['name'])) {
$picture_uri = CourseManager::update_course_picture(
$course_info,
$picture['name'],
$picture['tmp_name'],
$course_values['picture_crop_result']
);
}*/
/*$resourceFile = new \Chamilo\CoreBundle\Entity\Resource\ResourceFile();
$resourceFile->setName('course_image')
$picture['tmp_name']*/
$splash = api_get_setting('course_creation_splash_screen');
if ($splash === 'true') {
$url = Container::getRouter()->generate('chamilo_core_course_welcome', ['course' =>$course_info['code']]);

@ -6,6 +6,7 @@ use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\Session as SessionEntity;
use Chamilo\CoreBundle\Entity\SettingsCurrent;
use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CourseBundle\Entity\CGroupInfo;
use Chamilo\CourseBundle\Entity\CItemProperty;
use Chamilo\ThemeBundle\Controller\ExceptionController;
use Chamilo\UserBundle\Entity\User;
@ -2123,7 +2124,7 @@ function api_get_session_entity($id = 0)
/**
* @param int $id
*
* @return \Chamilo\CourseBundle\Entity\CGroupInfo
* @return CGroupInfo
*/
function api_get_group_entity($id = 0)
{

@ -3431,87 +3431,6 @@ class CourseManager
return false;
}
/**
* Update course picture.
*
* @param array $courseInfo
* @param string File name
* @param string 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 update_course_picture(
$courseInfo,
$filename,
$source_file = null,
$cropParameters = null
) {
return false;
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-pic.png';
$course_medium_image = $store_path.'/course-pic85x85.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($source_file);
$image->crop($cropParameters);
//Resize the images in two formats
$medium = new Image($source_file);
$medium->resize(85);
$medium->send_image($course_medium_image, -1, 'png');
$normal = new Image($source_file);
$normal->resize(400);
$normal->send_image($course_image, -1, 'png');
$result = $medium && $normal;
return $result ? $result : false;
}
/**
* Deletes the course picture.
*
* @deprecated resource will be used.
*
* @param string $courseCode
*/
public static function deleteCoursePicture($courseCode)
{
return false;
$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-pic.png';
$courseMediumImage = $storePath.'/course-pic85x85.png';
$courseSmallImage = $storePath.'/course-pic32.png';
if (file_exists($courseImage)) {
unlink($courseImage);
}
if (file_exists($courseMediumImage)) {
unlink($courseMediumImage);
}
if (file_exists($courseSmallImage)) {
unlink($courseSmallImage);
}
}
/**
* Display special courses (and only these) as several HTML divs of class userportal-course-item.
*

@ -440,7 +440,7 @@ class ResourceController extends BaseController implements CourseControllerInter
* @return StreamedResponse
* @throws \League\Flysystem\FileNotFoundException
*/
private function showFile(ResourceNode $resourceNode, $glide, $type, $filter)
private function showFile(ResourceNode $resourceNode, Glide $glide, $type, $filter)
{
$fs = $this-> container->get('oneup_flysystem.resources_filesystem');
@ -471,6 +471,11 @@ class ResourceController extends BaseController implements CourseControllerInter
$server = $glide->getServer();
$filter = $glide->getFilters()[$filter] ?? [];
$crop = $resourceFile->getCrop();
if (!empty($filter)) {
$filter['crop'] = $crop;
}
return $server->getImageResponse($filePath, $filter);
}
break;

@ -34,6 +34,14 @@ class Illustration extends AbstractResource implements ResourceInterface
*/
protected $name;
/**
* Illustration constructor.
*/
public function __construct()
{
$this->name = 'illustration';
}
/**
* @return int
*/

@ -3,6 +3,7 @@
namespace Chamilo\CoreBundle\Entity\Resource;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Resource\Model\ResourceInterface;
@ -65,4 +66,20 @@ abstract class AbstractResource implements ResourceInterface
{
return $this->resourceNode;
}
/**
* @return ResourceNode
*/
public function getResourceNodeIllustration()
{
$node = $this->getResourceNode();
// @todo also filter by the resource type = Illustration
$criteria = Criteria::create()->where(
Criteria::expr()->eq('name', 'illustration')
);
$illustration = $node->getChildren()->matching($criteria)->first();
return $illustration;
}
}

@ -109,6 +109,13 @@ class ResourceFile
*/
protected $file;
/**
* @var string
*
* @ORM\Column(name="crop", type="string", length=255, nullable=true)
*/
protected $crop;
/**
* @var ResourceNode
*
@ -218,6 +225,27 @@ class ResourceFile
return $this;
}
/**
* @return string
*/
public function getCrop(): string
{
return $this->crop;
}
/**
* @param string $crop
*
* @return $this
*/
public function setCrop($crop)
{
$this->crop = $crop;
return $this;
}
/**
* @return string
*/

@ -942,6 +942,10 @@ class Version20 extends AbstractMigrationChamilo
$this->addSql("INSERT INTO settings_options (variable, value, display_text) VALUES ('configure_exercise_visibility_in_course','true','Yes')");
$this->addSql("INSERT INTO settings_options (variable, value, display_text) VALUES ('configure_exercise_visibility_in_course','false','No')");
}
//CREATE TABLE illustration (id INT AUTO_INCREMENT NOT NULL, resource_node_id INT DEFAULT NULL, name VARCHAR(255) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, UNIQUE INDEX UNIQ_D67B9A421BAD783F (resource_node_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB ROW_FORMAT = DYNAMIC;
//ALTER TABLE illustration ADD CONSTRAINT FK_D67B9A421BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE;
}
/**

@ -77,7 +77,7 @@ class ResourceRepository extends EntityRepository
*/
public function getEntityManager(): EntityManager
{
return $this->entityManager;
return $this->getRepository()->getEntityManager();
}
/**
@ -116,7 +116,7 @@ class ResourceRepository extends EntityRepository
*
* @return ResourceNode|mixed
*/
public function getIllustration(AbstractResource $resource)
/*public function getIllustration(AbstractResource $resource)
{
$node = $resource->getResourceNode();
// @todo also filter by the resource type = Illustration
@ -127,11 +127,7 @@ class ResourceRepository extends EntityRepository
$illustration = $node->getChildren()->matching($criteria)->first();
return $illustration;
/** @var ResourceNode $illustration */
//$illustration = $this->getRepository()->findOneBy(['parent' => $node, 'name' => 'course_picture']);
var_dump($illustration);
}
}*/
/**
* @param AbstractResource $resource

@ -85,10 +85,6 @@ class CourseRecycler
}
}
}
if ($backupType === 'full_backup') {
\CourseManager::deleteCoursePicture($this->course_info['code']);
}
}
/**

Loading…
Cancel
Save