From 848f15b3cf6a771891b989f932616d7331d6c16e Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Tue, 29 Dec 2020 12:02:18 +0100 Subject: [PATCH] Student publication: Use CStudentPublicationCorrection to save content. --- .../main/template/default/work/view.html.twig | 30 ----- public/main/work/view.php | 107 ++++++++++++------ public/main/work/work.lib.php | 59 ++++------ public/main/work/work_list_all.php | 20 +++- .../EventListener/AssetListener.php | 16 +-- .../EventListener/CourseListener.php | 10 +- .../Resources/views/Work}/comments.html.twig | 6 +- .../Resources/views/Work/view.html.twig | 41 +++++++ src/CoreBundle/Traits/ControllerTrait.php | 25 ++-- .../Entity/CStudentPublication.php | 53 +-------- .../Entity/CStudentPublicationComment.php | 2 +- 11 files changed, 185 insertions(+), 184 deletions(-) delete mode 100644 public/main/template/default/work/view.html.twig rename {public/main/template/default/work => src/CoreBundle/Resources/views/Work}/comments.html.twig (83%) create mode 100644 src/CoreBundle/Resources/views/Work/view.html.twig diff --git a/public/main/template/default/work/view.html.twig b/public/main/template/default/work/view.html.twig deleted file mode 100644 index cb7dcb3224..0000000000 --- a/public/main/template/default/work/view.html.twig +++ /dev/null @@ -1,30 +0,0 @@ -{% autoescape false %} - - -{% if work.description %} -

- {{ 'Description' | get_lang }} -

-

- {{ work.description }} -

-{% endif %} - -{{ form }} - -{% if work.contains_file and work.show_content %} -

- {{ 'Content' | get_lang }} -

-

- {{ work.show_content }} -

-{% endif %} - -{% include template ~ '/work/comments.html.twig' %} - -{% endautoescape %} \ No newline at end of file diff --git a/public/main/work/view.php b/public/main/work/view.php index 47fa3fea11..b19394dd0a 100644 --- a/public/main/work/view.php +++ b/public/main/work/view.php @@ -4,6 +4,8 @@ use Chamilo\CoreBundle\Framework\Container; use Chamilo\CourseBundle\Entity\CStudentPublication; +use Chamilo\CourseBundle\Entity\CStudentPublicationCorrection; +use Symfony\Component\HttpFoundation\File\UploadedFile; require_once __DIR__.'/../inc/global.inc.php'; $current_course_tool = TOOL_STUDENTPUBLICATION; @@ -83,10 +85,10 @@ if (($isDrhOfCourse || $allowEdition || $isDrhOfSession || user_is_author($id)) $workId = $work->getIid(); if (( 0 == $courseInfo['show_score'] && - 1 == $work['active'] && - 1 == $work['accepted'] + 1 == $work->getActive() && + 1 == $work->getAccepted() ) || - $isCourseManager || user_is_author($id) || $isDrhOfCourse || $isDrhOfSession + $isCourseManager || $isDrhOfCourse || $isDrhOfSession || user_is_author($id) ) { if ('edit' === $page) { $url = api_get_path(WEB_CODE_PATH). @@ -117,22 +119,24 @@ if (($isDrhOfCourse || $allowEdition || $isDrhOfSession || user_is_author($id)) if ($allowEdition) { $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION); - $sql = "UPDATE $work_table - SET - qualificator_id = '".api_get_user_id()."', - qualification = '".api_float_val($_POST['qualification'])."', - date_of_qualification = '".api_get_utc_datetime()."' - WHERE c_id = ".$courseInfo['real_id']." AND id = $id"; - Database::query($sql); + + if (isset($_POST['qualification'])) { + $work->setQualificatorId(api_get_user_id()); + $work->setQualification(api_float_val($_POST['qualification'])); + $work->setDateOfQualification(api_get_utc_datetime(null, true, true)); + $repo->update($work); + } + Display::addFlash(Display::return_message(get_lang('Updated'))); - $resultUpload = uploadWork( + /*$resultUpload = uploadWork( $folderData, $courseEntity, true, $work - ); - if ($resultUpload) { + );*/ + + /*if ($resultUpload) { $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION); if (isset($resultUpload['url']) && !empty($resultUpload['url'])) { $title = isset($resultUpload['filename']) && !empty($resultUpload['filename']) ? $resultUpload['filename'] : get_lang('Untitled'); @@ -147,6 +151,26 @@ if (($isDrhOfCourse || $allowEdition || $isDrhOfSession || user_is_author($id)) Display::return_message(get_lang('The file has successfully been uploaded.')) ); } + }*/ + + $request = Container::getRequest(); + $file = $request->files->get('file'); + if (is_array($file)) { + /** @var UploadedFile $file */ + $file = $file[0]; + } + + if (null !== $file) { + $em = Database::getManager(); + $correction = new CStudentPublicationCorrection(); + $correction + ->setParent($work) + ->setTitle($file->getClientOriginalName()); + // @todo improve file upload. + $correctionRepo = Container::getStudentPublicationCorrectionRepository(); + $correctionRepo->create($correction); + $correctionRepo->addFile($correction, $file); + $correctionRepo->update($correction); } } @@ -166,8 +190,8 @@ if (($isDrhOfCourse || $allowEdition || $isDrhOfSession || user_is_author($id)) break; case 'delete_correction': - if ($allowEdition && isset($work['url_correction']) && !empty($work['url_correction'])) { - deleteCorrection($courseInfo, $work); + if ($allowEdition) { + deleteCorrection($work); Display::addFlash(Display::return_message(get_lang('Deleted'))); } @@ -185,7 +209,7 @@ if (($isDrhOfCourse || $allowEdition || $isDrhOfSession || user_is_author($id)) $actions = ''; if ($work->getContainsFile()) { - if (isset($work['download_url']) && !empty($work['download_url'])) { + if ($work->getResourceNode()->hasResourceFile()) { $actions = Display::url( Display::return_icon( 'back.png', @@ -196,23 +220,41 @@ if (($isDrhOfCourse || $allowEdition || $isDrhOfSession || user_is_author($id)) api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq() ); - // Check if file can be downloaded - $file = getFileContents($work['id'], $courseInfo, api_get_session_id(), false); - if (!empty($file)) { - $actions .= Display::url( - Display::return_icon( - 'save.png', - get_lang('Download'), - null, - ICON_SIZE_MEDIUM - ), - $work['download_url'] - ); - } + $router = Container::getRouter(); + $url = $router->generate( + 'chamilo_core_resource_download', + [ + 'id' => $work->getResourceNode()->getId(), + 'tool' => 'student_publication', + 'type' => 'student_publications', + ] + ); + + $actions .= Display::url( + Display::return_icon( + 'save.png', + get_lang('Download'), + null, + ICON_SIZE_MEDIUM + ), + $url + ); } } - if (isset($work['url_correction']) && !empty($work['url_correction']) && !empty($work['download_url'])) { + $correctionNode = $work->getCorrection(); + + if (null !== $correctionNode) { + $router = Container::getRouter(); + $url = $router->generate( + 'chamilo_core_resource_download', + [ + 'id' => $correctionNode->getId(), + 'tool' => 'student_publication', + 'type' => 'student_publications_corrections', + ] + ).api_get_cidreq(); + $actions .= Display::url( Display::return_icon( 'check-circle.png', @@ -220,7 +262,7 @@ if (($isDrhOfCourse || $allowEdition || $isDrhOfSession || user_is_author($id)) null, ICON_SIZE_MEDIUM ), - $work['download_url'].'&correction=1' + $url ); if ($allowEdition) { @@ -247,8 +289,7 @@ if (($isDrhOfCourse || $allowEdition || $isDrhOfSession || user_is_author($id)) $tpl->assign('form', $commentForm); } $tpl->assign('is_allowed_to_edit', api_is_allowed_to_edit()); - $template = $tpl->get_template('work/view.tpl'); - $content = $tpl->fetch($template); + $content = $tpl->fetch('@ChamiloCore/Work/view.html.twig'); $tpl->assign('content', $content); $tpl->display_one_col_template(); } else { diff --git a/public/main/work/work.lib.php b/public/main/work/work.lib.php index 2d848eb782..2905ad9a16 100644 --- a/public/main/work/work.lib.php +++ b/public/main/work/work.lib.php @@ -3678,12 +3678,12 @@ function deleteCommentFile($id, $courseInfo = []) * @param array $courseInfo * @param int $userId * @param array $parentWork - * @param array $work + * @param CStudentPublication $work * @param array $data * * @return int */ -function addWorkComment($courseInfo, $userId, $parentWork, $work, $data) +function addWorkComment($courseInfo, $userId, $parentWork, CStudentPublication $studentPublication, $data) { $fileData = isset($data['attachment']) ? $data['attachment'] : null; // If no attachment and no comment then don't save comment @@ -3693,13 +3693,10 @@ function addWorkComment($courseInfo, $userId, $parentWork, $work, $data) $courseId = $courseInfo['real_id']; $courseEntity = api_get_course_entity($courseId); - /** @var CStudentPublication $studentPublication */ - $studentPublication = Container::getStudentPublicationRepository()->find($work['iid']); - $request = Container::getRequest(); - $fileObject = $request->files->get('attachment'); - if (is_array($fileObject)) { - $fileObject = $fileObject[0]; + $file = $request->files->get('attachment'); + if (is_array($file)) { + $file = $file[0]; } $em = Database::getManager(); @@ -3708,7 +3705,7 @@ function addWorkComment($courseInfo, $userId, $parentWork, $work, $data) ->setCId($courseId) ->setComment($data['comment']) ->setUserId($userId) - ->setWorkId($work['iid']) + ->setWorkId($studentPublication->getIid()) ->setParent($studentPublication) ->addCourseLink( $courseEntity, @@ -3719,8 +3716,8 @@ function addWorkComment($courseInfo, $userId, $parentWork, $work, $data) $repo = Container::getStudentPublicationCommentRepository(); $repo->create($comment); - if ($fileObject) { - $repo->addFile($comment, $fileObject); + if ($file) { + $repo->addFile($comment, $file); $em->flush(); } @@ -3728,7 +3725,7 @@ function addWorkComment($courseInfo, $userId, $parentWork, $work, $data) if (api_is_allowed_to_edit()) { if (isset($data['send_email']) && $data['send_email']) { // Teacher sends a feedback - $userIdListToSend = [$work['user_id']]; + $userIdListToSend = [$studentPublication->getUserId()]; } } else { $sessionId = api_get_session_id(); @@ -3755,10 +3752,11 @@ function addWorkComment($courseInfo, $userId, $parentWork, $work, $data) $userIdListToSend = []; } } - - $url = api_get_path(WEB_CODE_PATH).'work/view.php?'.api_get_cidreq().'&id='.$work['iid']; + $id = $studentPublication->getIid(); + $title = $studentPublication->getTitle(); + $url = api_get_path(WEB_CODE_PATH).'work/view.php?'.api_get_cidreq().'&id='.$id; $subject = sprintf(get_lang('There\'s a new feedback in work: %s'), $parentWork['title']); - $content = sprintf(get_lang('There\'s a new feedback in work: %sInWorkXHere'), $work['title'], $url); + $content = sprintf(get_lang('There\'s a new feedback in work: %sInWorkXHere'), $title, $url); if (!empty($data['comment'])) { $content .= '
'.get_lang('Comment').':
'.$data['comment']; @@ -4858,7 +4856,7 @@ function deleteWorkItem($item_id, $courseInfo) // We found the current user is the author $sql = "SELECT url, contains_file, user_id, session_id, parent_id FROM $work_table - WHERE c_id = $course_id AND id = $item_id"; + WHERE iid = $item_id"; $result = Database::query($sql); $row = Database::fetch_array($result); $count = Database::num_rows($result); @@ -5949,29 +5947,14 @@ function protectWork($courseInfo, $workId) } } -/** - * @param array $courseInfo - * @param array $work - */ -function deleteCorrection($courseInfo, $work) +function deleteCorrection(CStudentPublication $work) { - throw new Exception('deleteCorrection'); - /* - if (isset($work['url_correction']) && !empty($work['url_correction']) && isset($work['iid'])) { - $id = $work['iid']; - $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION); - $sql = "UPDATE $table SET - url_correction = '', - title_correction = '' - WHERE iid = $id"; - Database::query($sql); - $coursePath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/'; - if (file_exists($coursePath.$work['url_correction'])) { - if (Security::check_abs_path($coursePath.$work['url_correction'], $coursePath)) { - unlink($coursePath.$work['url_correction']); - } - } - }*/ + $correctionNode = $work->getCorrection(); + if (null !== $correctionNode) { + $em = Database::getManager(); + $em->remove($correctionNode); + $em->flush(); + } } /** diff --git a/public/main/work/work_list_all.php b/public/main/work/work_list_all.php index ccd9d3f17a..d66b54f127 100644 --- a/public/main/work/work_list_all.php +++ b/public/main/work/work_list_all.php @@ -199,14 +199,26 @@ if (api_is_allowed_to_session_edit(false, true) && !empty($workId) && !$isDrhOfC $count = get_count_work($workId); if ($count > 0) { $display_output .= ''. - Display::return_icon('save_pack.png', get_lang('Download assignments package'), null, ICON_SIZE_MEDIUM).' '.get_lang('Download assignments package').''; + Display::return_icon( + 'save_pack.png', + get_lang('Download assignments package'), + null, + ICON_SIZE_MEDIUM + ).' '.get_lang('Download assignments package').''; } $actionsLeft .= $display_output; $url = api_get_path(WEB_CODE_PATH).'work/upload_corrections.php?'.api_get_cidreq().'&id='.$workId; $actionsLeft .= ''. - Display::return_icon('upload_package.png', get_lang('Upload corrections package'), '', ICON_SIZE_MEDIUM).' '.get_lang('Upload corrections package').''; - $url = api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq().'&id='.$workId.'&action=delete_correction'; - $actionsLeft .= Display::toolbarButton(get_lang('Delete all corrections'), $url, 'remove', 'danger'); + Display::return_icon( + 'upload_package.png', + get_lang('Upload corrections package'), + '', + ICON_SIZE_MEDIUM + ).' '.get_lang('Upload corrections package').''; + + $url = api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq( + ).'&id='.$workId.'&action=delete_correction'; + $actionsLeft .= Display::toolbarButton(get_lang('Delete all corrections'), $url, 'trash', 'danger'); } echo Display::toolbarAction('toolbar-worklist', [$actionsLeft]); diff --git a/src/CoreBundle/EventListener/AssetListener.php b/src/CoreBundle/EventListener/AssetListener.php index 7b777db066..a77d7fefd0 100644 --- a/src/CoreBundle/EventListener/AssetListener.php +++ b/src/CoreBundle/EventListener/AssetListener.php @@ -27,13 +27,15 @@ class AssetListener { /** @var Asset $asset */ $asset = $event->getObject(); - $mapping = $event->getMapping(); - $folder = $mapping->getFile($asset)->getFilename(); - - // Deletes scorm folder: example: assets/scorm/myABC . - if (Asset::SCORM === $asset->getCategory() && !empty($folder)) { - $folder = Asset::SCORM.'/'.$folder; - $this->assetRepository->getFileSystem()->deleteDir($folder); + if ($asset instanceof Asset) { + $mapping = $event->getMapping(); + $folder = $mapping->getFile($asset)->getFilename(); + + // Deletes scorm folder: example: assets/scorm/myABC . + if (Asset::SCORM === $asset->getCategory() && !empty($folder)) { + $folder = Asset::SCORM.'/'.$folder; + $this->assetRepository->getFileSystem()->deleteDir($folder); + } } } } diff --git a/src/CoreBundle/EventListener/CourseListener.php b/src/CoreBundle/EventListener/CourseListener.php index 938748e9f9..db206e89cc 100644 --- a/src/CoreBundle/EventListener/CourseListener.php +++ b/src/CoreBundle/EventListener/CourseListener.php @@ -4,6 +4,7 @@ namespace Chamilo\CoreBundle\EventListener; +use Chamilo\CoreBundle\Controller\EditorController; use Chamilo\CoreBundle\Entity\Course; use Chamilo\CoreBundle\Entity\Session; use Chamilo\CoreBundle\Entity\User; @@ -77,6 +78,7 @@ class CourseListener $courseId = (int) $request->get('cid'); $checker = $container->get('security.authorization_checker'); + //dump("cid value in request: $courseId"); if (!empty($courseId)) { if ($sessionHandler->has('course')) { /** @var Course $courseFromSession */ @@ -84,7 +86,7 @@ class CourseListener if ($courseId === $courseFromSession->getId()) { $course = $courseFromSession; $courseInfo = $sessionHandler->get('_course'); - //dump("Course loaded from Session $courseId"); + //dump("Course #$courseId loaded from Session "); } } $course = null; @@ -97,7 +99,7 @@ class CourseListener throw new NotFoundHttpException($translator->trans('Course does not exist')); } - //dump("Course loaded from DB $courseId"); + //dump("Course loaded from DB #$courseId"); $courseInfo = api_get_course_info($course->getCode()); } @@ -247,9 +249,9 @@ class CourseListener // This controller implements ToolInterface? Then set the course/session if (is_array($controllerList) && ( - $controllerList[0] instanceof CourseControllerInterface + $controllerList[0] instanceof CourseControllerInterface || + $controllerList[0] instanceof EditorController //$controllerList[0] instanceof ResourceController - //|| $controllerList[0] instanceof LegacyController ) ) { diff --git a/public/main/template/default/work/comments.html.twig b/src/CoreBundle/Resources/views/Work/comments.html.twig similarity index 83% rename from public/main/template/default/work/comments.html.twig rename to src/CoreBundle/Resources/views/Work/comments.html.twig index 82ba39a821..30d9f3f7c1 100644 --- a/public/main/template/default/work/comments.html.twig +++ b/src/CoreBundle/Resources/views/Work/comments.html.twig @@ -8,9 +8,9 @@ {% for comment in comments %}
  • {% if comment.comment is not empty %} diff --git a/src/CoreBundle/Resources/views/Work/view.html.twig b/src/CoreBundle/Resources/views/Work/view.html.twig new file mode 100644 index 0000000000..14a2ae9d83 --- /dev/null +++ b/src/CoreBundle/Resources/views/Work/view.html.twig @@ -0,0 +1,41 @@ +{% autoescape false %} +

    + +{% if work.description %} +

    + {{ 'Description' | get_lang }} +

    +

    + {{ work.description }} +

    +{% endif %} + +{{ form }} + +{% if work.getResourceNode and work.getResourceNode.hasResourceFile %} +

    + {{ 'Content' | get_lang }} +

    +

    + + {{ 'Download' | trans}} + +

    +{% endif %} + +{% include '@ChamiloCore/Work/comments.html.twig' %} + +{% endautoescape %} \ No newline at end of file diff --git a/src/CoreBundle/Traits/ControllerTrait.php b/src/CoreBundle/Traits/ControllerTrait.php index ce283245b8..a89e4b4d81 100644 --- a/src/CoreBundle/Traits/ControllerTrait.php +++ b/src/CoreBundle/Traits/ControllerTrait.php @@ -21,6 +21,8 @@ use Chamilo\CourseBundle\Repository\CLpCategoryRepository; use Chamilo\CourseBundle\Repository\CLpRepository; use Chamilo\CourseBundle\Repository\CQuizQuestionCategoryRepository; use Chamilo\CourseBundle\Repository\CQuizQuestionRepository; +use Chamilo\CourseBundle\Repository\CStudentPublicationCommentRepository; +use Chamilo\CourseBundle\Repository\CStudentPublicationCorrectionRepository; use Chamilo\CourseBundle\Repository\CStudentPublicationRepository; use Knp\Menu\FactoryInterface as MenuFactoryInterface; use Sylius\Bundle\SettingsBundle\Form\Factory\SettingsFormFactory; @@ -37,14 +39,19 @@ trait ControllerTrait $services['glide'] = Glide::class; $services['chamilo.settings.manager'] = SettingsManager::class; $services['chamilo_settings.form_factory.settings'] = SettingsFormFactory::class; + $services[] = ResourceFactory::class; + $services[] = ResourceNodeRepository::class; /* The following classes are needed in order to load the resources files when using the /r/ path For example: http://my.chamilomaster.net/r/agenda/event_attachments/96/download?cid=1&sid=0&gid=0 + Then the repository CCalendarEventAttachmentRepository need to be added here, + because it was set in the tools.yml like this: + chamilo_core.tool.agenda: + (...) + event_attachments: + repository: Chamilo\CourseBundle\Repository\CCalendarEventAttachmentRepository */ - $services[] = ResourceFactory::class; - $services[] = ResourceNodeRepository::class; - $services[] = CAnnouncementRepository::class; $services[] = CAnnouncementAttachmentRepository::class; $services[] = CAttendanceRepository::class; @@ -58,19 +65,11 @@ trait ControllerTrait $services[] = CQuizQuestionRepository::class; $services[] = CQuizQuestionCategoryRepository::class; $services[] = CStudentPublicationRepository::class; - + $services[] = CStudentPublicationCommentRepository::class; + $services[] = CStudentPublicationCorrectionRepository::class; $services[] = IllustrationRepository::class; - /*$services[] = CAttendanceRepository::class; - $services[] = CDocumentRepository::class; - $services[] = CDocumentRepository::class; - $services[] = CDocumentRepository::class; - $services[] = CDocumentRepository::class; - $services[] = CDocumentRepository::class; - $services[] = CDocumentRepository::class; - $services[] = CDocumentRepository::class;*/ - return $services; } diff --git a/src/CourseBundle/Entity/CStudentPublication.php b/src/CourseBundle/Entity/CStudentPublication.php index a08d31baff..94a005b6b5 100644 --- a/src/CourseBundle/Entity/CStudentPublication.php +++ b/src/CourseBundle/Entity/CStudentPublication.php @@ -56,20 +56,6 @@ class CStudentPublication extends AbstractResource implements ResourceInterface */ protected $url; - /** - * @var string - * - * @ORM\Column(name="url_correction", type="string", length=500, nullable=true) - */ - protected $urlCorrection; - - /** - * @var string - * - * @ORM\Column(name="title_correction", type="string", length=255, nullable=true) - */ - protected $titleCorrection; - /** * @var string * @@ -721,42 +707,6 @@ class CStudentPublication extends AbstractResource implements ResourceInterface return $this->cId; } - /** - * @return string - */ - public function getUrlCorrection() - { - return $this->urlCorrection; - } - - /** - * @param string $urlCorrection - */ - public function setUrlCorrection($urlCorrection) - { - $this->urlCorrection = $urlCorrection; - - return $this; - } - - /** - * @return string - */ - public function getTitleCorrection() - { - return $this->titleCorrection; - } - - /** - * @param string $titleCorrection - */ - public function setTitleCorrection($titleCorrection) - { - $this->titleCorrection = $titleCorrection; - - return $this; - } - /** * @return int */ @@ -802,7 +752,8 @@ class CStudentPublication extends AbstractResource implements ResourceInterface if ($this->hasResourceNode()) { $children = $this->getResourceNode()->getChildren(); foreach ($children as $child) { - if ($child instanceof CStudentPublicationCorrection) { + $name = $child->getResourceType()->getName(); + if ('student_publications_corrections' === $name) { return $child; } } diff --git a/src/CourseBundle/Entity/CStudentPublicationComment.php b/src/CourseBundle/Entity/CStudentPublicationComment.php index eae7546d5d..ddb6e3b43e 100644 --- a/src/CourseBundle/Entity/CStudentPublicationComment.php +++ b/src/CourseBundle/Entity/CStudentPublicationComment.php @@ -243,7 +243,7 @@ class CStudentPublicationComment extends AbstractResource implements ResourceInt public function getResourceName(): string { - return (string) substr($this->getComment(), 0, 80); + return (string) substr(str_replace('/', '-', strip_tags($this->getComment())), 0, 40); } public function setResourceName(string $name): self