From 1c8439859d99612f73a056e336e8b7d45d18ee1b Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Mon, 20 Aug 2018 09:03:20 +0200 Subject: [PATCH] Fill c_tool table when adding a Course with doctrine. - Check tool visibility from the platform settings. - Add postPersist to update id with iid in CTool entity. --- main/inc/lib/add_course.lib.inc.php | 83 +++++++++++++------ main/inc/lib/api.lib.php | 15 ++++ src/CoreBundle/Entity/Course.php | 13 ++- .../Entity/Listener/CourseListener.php | 28 ++++--- src/CoreBundle/Resources/config/services.yml | 2 +- src/CourseBundle/Entity/CTool.php | 17 +++- src/CourseBundle/ToolChain.php | 11 ++- 7 files changed, 123 insertions(+), 46 deletions(-) diff --git a/main/inc/lib/add_course.lib.inc.php b/main/inc/lib/add_course.lib.inc.php index 4609dfbdf8..2050e47dd0 100755 --- a/main/inc/lib/add_course.lib.inc.php +++ b/main/inc/lib/add_course.lib.inc.php @@ -4,6 +4,7 @@ use Chamilo\CourseBundle\Entity\CTool; use Chamilo\CourseBundle\Entity\CToolIntro; use Chamilo\CourseBundle\Tool\BaseTool; +use Chamilo\CoreBundle\Framework\Container; /** * Class AddCourse. @@ -421,9 +422,9 @@ class AddCourse $authorId = 0 ) { if (is_null($fill_with_exemplary_content)) { - $fill_with_exemplary_content = api_get_setting('example_material_course_creation') != 'false'; + $fill_with_exemplary_content = api_get_setting('example_material_course_creation') !== 'false'; } - $course_id = intval($course_id); + $course_id = (int) $course_id; if (empty($course_id)) { return false; @@ -446,11 +447,11 @@ class AddCourse $course = api_get_course_entity($course_id); $settingsManager = CourseManager::getCourseSettingsManager(); $settingsManager->setCourse($course); - $toolList = CourseManager::getToolList(); - $toolList = $toolList->getTools(); + //$toolList = CourseManager::getToolList(); + //$toolList = $toolList->getTools(); /** @var BaseTool $tool */ - foreach ($toolList as $tool) { + /* foreach ($toolList as $tool) { $toolName = $tool->getName(); $visibility = self::string2binary( api_get_setting_in_list('course.active_tools_on_create', $toolName) @@ -475,12 +476,13 @@ class AddCourse $course->setTools($tools); $em->persist($course); $em->flush($course); - /** @var CTool $tool */ + + foreach ($tools as $tool) { $tool->setId($tool->getIid()); $em->refresh($course); } - $em->flush($course); + $em->flush($course);*/ /* Course tools */ /* @@ -1228,12 +1230,9 @@ class AddCourse 'platformLanguage' ); $user_id = empty($params['user_id']) ? api_get_user_id() : intval($params['user_id']); - $department_name = isset($params['department_name']) ? - $params['department_name'] : null; - $department_url = isset($params['department_url']) ? - $params['department_url'] : null; - $disk_quota = isset($params['disk_quota']) ? - $params['disk_quota'] : null; + $department_name = isset($params['department_name']) ? $params['department_name'] : null; + $department_url = isset($params['department_url']) ? $params['department_url'] : null; + $disk_quota = isset($params['disk_quota']) ? $params['disk_quota'] : null; if (!isset($params['visibility'])) { $default_course_visibility = api_get_setting( @@ -1249,14 +1248,12 @@ class AddCourse } $subscribe = isset($params['subscribe']) ? (int) $params['subscribe'] : $visibility == COURSE_VISIBILITY_OPEN_PLATFORM ? 1 : 0; - $unsubscribe = isset($params['unsubscribe']) ? intval($params['unsubscribe']) : 0; + $unsubscribe = isset($params['unsubscribe']) ? (int) $params['unsubscribe'] : 0; $expiration_date = isset($params['expiration_date']) ? $params['expiration_date'] : null; $teachers = isset($params['teachers']) ? $params['teachers'] : null; $status = isset($params['status']) ? $params['status'] : null; - $TABLECOURSE = Database::get_main_table(TABLE_MAIN_COURSE); $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER); - $ok_to_register_course = true; // Check whether all the needed parameters are present. @@ -1295,8 +1292,6 @@ class AddCourse $disk_quota = api_get_setting('default_document_quotum'); } - $time = api_get_utc_datetime(); - if (stripos($department_url, 'http://') === false && stripos( $department_url, 'https://' @@ -1304,15 +1299,20 @@ class AddCourse ) { $department_url = 'http://'.$department_url; } - //just in case + + // just in case if ($department_url == 'http://') { $department_url = ''; } $course_id = 0; if ($ok_to_register_course) { + $courseManager = Container::$container->get('chamilo_core.entity.manager.course_manager'); + /** @var \Chamilo\CoreBundle\Entity\Course $course */ + $course = $courseManager->create(); + // Here we must add 2 fields. - $course_id = Database::insert( + /*$course_id = Database::insert( $TABLECOURSE, [ 'code' => $code, @@ -1335,7 +1335,37 @@ class AddCourse 'unsubscribe' => intval($unsubscribe), 'visual_code' => $visual_code, ] - ); + );*/ + + $urlId = 1; + if (api_get_current_access_url_id() !== -1) { + $urlId = api_get_current_access_url_id(); + } + + $url = api_get_url_entity($urlId); + $course + ->setCode($code) + ->setDirectory($directory) + ->setCourseLanguage($course_language) + ->setTitle($title) + ->setDescription(get_lang('CourseDescription')) + ->setCategoryCode($category_code) + ->setVisibility($visibility) + ->setShowScore(1) + ->setDiskQuota($disk_quota) + ->setCreationDate(new \DateTime()) + ->setExpirationDate(new \DateTime($expiration_date)) + //->setLastEdit() + ->setDepartmentName($department_name) + ->setDepartmentUrl($department_url) + ->setSubscribe($subscribe) + ->setUnsubscribe($unsubscribe) + ->setVisualCode($visual_code) + ->addUrl($url) + ; + + $courseManager->save($course, true); + $course_id = $course->getId(); if ($course_id) { $sort = api_max_sort_value('0', api_get_user_id()); @@ -1384,7 +1414,8 @@ class AddCourse } // Adding the course to an URL. - if (api_is_multiple_url_enabled()) { + // Already added by when saving the entity + /*if (api_is_multiple_url_enabled()) { $url_id = 1; if (api_get_current_access_url_id() != -1) { $url_id = api_get_current_access_url_id(); @@ -1392,7 +1423,7 @@ class AddCourse UrlManager::add_course_to_url($course_id, $url_id); } else { UrlManager::add_course_to_url($course_id, 1); - } + }*/ // Add event to the system log. $user_id = api_get_user_id(); @@ -1405,12 +1436,10 @@ class AddCourse $course_id ); - $send_mail_to_admin = api_get_setting( - 'send_email_to_admin_when_create_course' - ); + $send_mail_to_admin = api_get_setting('send_email_to_admin_when_create_course'); // @todo Improve code to send to all current portal administrators. - if ($send_mail_to_admin == 'true') { + if ($send_mail_to_admin === 'true') { $siteName = api_get_setting('siteName'); $recipient_email = api_get_setting('emailAdministrator'); $recipient_name = api_get_person_name( diff --git a/main/inc/lib/api.lib.php b/main/inc/lib/api.lib.php index 9688884884..6d4894026b 100644 --- a/main/inc/lib/api.lib.php +++ b/main/inc/lib/api.lib.php @@ -2094,6 +2094,21 @@ function api_get_session_entity($id = 0) return Database::getManager()->getRepository('ChamiloCoreBundle:Session')->find($id); } +/** + * @param int $id + * + * @return \Chamilo\CoreBundle\Entity\AccessUrl + */ +function api_get_url_entity($id = 0) +{ + if (empty($id)) { + $id = api_get_current_access_url_id(); + } + + return Database::getManager()->getRepository('ChamiloCoreBundle:AccessUrl')->find($id); +} + + /** * Returns the current course info array. diff --git a/src/CoreBundle/Entity/Course.php b/src/CoreBundle/Entity/Course.php index d4d0555e0a..c944bf7167 100644 --- a/src/CoreBundle/Entity/Course.php +++ b/src/CoreBundle/Entity/Course.php @@ -442,12 +442,23 @@ class Course /** * @param AccessUrlRelCourse $url */ - public function addUrls(AccessUrlRelCourse $url) + public function addUrlRelCourse(AccessUrlRelCourse $url) { $url->setCourse($this); $this->urls[] = $url; } + /** + * @param AccessUrl $url + */ + public function addUrl(AccessUrl $url) + { + $urlRelCourse = new AccessUrlRelCourse(); + $urlRelCourse->setCourse($this); + $urlRelCourse->setUrl($url); + $this->addUrlRelCourse($urlRelCourse); + } + /** * @return ArrayCollection */ diff --git a/src/CoreBundle/Entity/Listener/CourseListener.php b/src/CoreBundle/Entity/Listener/CourseListener.php index 0ca8320246..f73d7d8486 100644 --- a/src/CoreBundle/Entity/Listener/CourseListener.php +++ b/src/CoreBundle/Entity/Listener/CourseListener.php @@ -6,9 +6,9 @@ namespace Chamilo\CoreBundle\Entity\Listener; use Chamilo\CoreBundle\Entity\AccessUrl; use Chamilo\CoreBundle\Entity\AccessUrlRelCourse; use Chamilo\CoreBundle\Entity\Course; -use Chamilo\CoreBundle\Entity\Tool; use Chamilo\CoreBundle\Repository\CourseRepository; use Chamilo\CourseBundle\ToolChain; +use Chamilo\SettingsBundle\Manager\SettingsManager; use Doctrine\ORM\Event\LifecycleEventArgs; /** @@ -20,13 +20,15 @@ use Doctrine\ORM\Event\LifecycleEventArgs; class CourseListener { protected $toolChain; + protected $settingsManager; /** * @param ToolChain $toolChain */ - public function __construct(ToolChain $toolChain) + public function __construct(ToolChain $toolChain, SettingsManager $settingsManager) { $this->toolChain = $toolChain; + $this->settingsManager = $settingsManager; } /** @@ -46,14 +48,14 @@ class CourseListener public function prePersist(Course $course, LifecycleEventArgs $args) { /** @var AccessUrlRelCourse $urlRelCourse */ - $urlRelCourse = $course->getUrls()->first(); - $url = $urlRelCourse->getUrl(); - - $repo = $args->getEntityManager()->getRepository('ChamiloCoreBundle:Course'); - - $this->checkLimit($repo, $course, $url); + if ($course) { + $urlRelCourse = $course->getUrls()->first(); + $url = $urlRelCourse->getUrl(); + $repo = $args->getEntityManager()->getRepository('ChamiloCoreBundle:Course'); + $this->checkLimit($repo, $course, $url); + $this->toolChain->addToolsInCourse($course, $this->settingsManager); + } - $this->toolChain->addToolsInCourse($course); /* error_log('ddd'); $course->setDescription( ' dq sdqs dqs dqs '); @@ -72,11 +74,13 @@ class CourseListener */ public function preUpdate(Course $course, LifecycleEventArgs $args) { - $url = $course->getCurrentUrl(); + if ($course) { + $url = $course->getCurrentUrl(); - $repo = $args->getEntityManager()->getRepository('ChamiloCoreBundle:Course'); + $repo = $args->getEntityManager()->getRepository('ChamiloCoreBundle:Course'); - $this->checkLimit($repo, $course, $url); + $this->checkLimit($repo, $course, $url); + } /*if ($eventArgs->getEntity() instanceof User) { if ($eventArgs->hasChangedField('name') && $eventArgs->getNewValue('name') == 'Alice') { diff --git a/src/CoreBundle/Resources/config/services.yml b/src/CoreBundle/Resources/config/services.yml index 00e1bfaf95..7f40eeaa35 100644 --- a/src/CoreBundle/Resources/config/services.yml +++ b/src/CoreBundle/Resources/config/services.yml @@ -117,7 +117,7 @@ services: # When Course entity is loaded chamilo_core.listener.course: class: Chamilo\CoreBundle\Entity\Listener\CourseListener - arguments: ['@chamilo_course.tool_chain'] + arguments: ['@chamilo_course.tool_chain', '@chamilo.settings.manager'] tags: - {name: doctrine.orm.entity_listener} diff --git a/src/CourseBundle/Entity/CTool.php b/src/CourseBundle/Entity/CTool.php index 8001df582c..ba051dcc40 100644 --- a/src/CourseBundle/Entity/CTool.php +++ b/src/CourseBundle/Entity/CTool.php @@ -4,11 +4,12 @@ namespace Chamilo\CourseBundle\Entity; use Chamilo\CoreBundle\Entity\Course; +use Doctrine\ORM\Event\LifecycleEventArgs; use Doctrine\ORM\Mapping as ORM; /** * CTool. - * + * @ORM\HasLifecycleCallbacks * @ORM\Table( * name="c_tool", * indexes={ @@ -191,7 +192,7 @@ class CTool { $metadata->addPropertyConstraint( 'customIcon', - new Assert\File(['mimeTypes' => ["image/png"]]) + new Assert\File(['mimeTypes' => ['image/png']]) ); $metadata->addPropertyConstraint( 'customIcon', @@ -527,4 +528,16 @@ class CTool return $this; } + + /** + * @ORM\PostPersist() + */ + public function postPersist(LifecycleEventArgs $args) + { + // Update id with iid value + $em = $args->getEntityManager(); + $this->setId($this->iid); + $em->persist($this); + $em->flush($this); + } } diff --git a/src/CourseBundle/ToolChain.php b/src/CourseBundle/ToolChain.php index 7e100e1dad..58492f49b5 100644 --- a/src/CourseBundle/ToolChain.php +++ b/src/CourseBundle/ToolChain.php @@ -9,6 +9,7 @@ use Chamilo\CoreBundle\Entity\ToolResourceRights; use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter; use Chamilo\CourseBundle\Entity\CTool; use Chamilo\CourseBundle\Tool\BaseTool; +use Chamilo\SettingsBundle\Manager\SettingsManager; use Doctrine\Common\Persistence\ObjectManager; /** @@ -104,22 +105,26 @@ class ToolChain } /** - * @param Course $course + * @param Course $course + * @param SettingsManager $settingsManager * * @return Course */ - public function addToolsInCourse(Course $course) + public function addToolsInCourse(Course $course, SettingsManager $settingsManager) { $tools = $this->getTools(); + $toolVisibility = $settingsManager->getSetting('course.active_tools_on_create'); /** @var BaseTool $tool */ foreach ($tools as $tool) { $toolEntity = new CTool(); + $visibility = in_array($tool->getName(), $toolVisibility) ? true : false; + $toolEntity ->setCId($course->getId()) ->setImage($tool->getImage()) ->setName($tool->getName()) - ->setVisibility(1) + ->setVisibility($visibility) ->setLink($tool->getLink()) ->setTarget($tool->getTarget()) ->setCategory($tool->getCategory());