From 78bb332e99d91f88cfa20abb0d8ae25fb907a9e4 Mon Sep 17 00:00:00 2001 From: jmontoyaa Date: Tue, 16 May 2017 09:26:45 +0200 Subject: [PATCH] Improve UI, fix update process see BT#12723 --- main/cron/import_csv.php | 24 +++++- plugin/studentfollowup/Entity/CarePost.php | 14 +++- .../studentfollowup/StudentFollowUpPlugin.php | 46 ++++++++++- plugin/studentfollowup/plugin.php | 2 +- plugin/studentfollowup/post.php | 76 ++++-------------- plugin/studentfollowup/posts.php | 78 +++++++++++++++++++ plugin/studentfollowup/view/post.html.twig | 33 +++++--- plugin/studentfollowup/view/posts.html.twig | 13 ++++ 8 files changed, 209 insertions(+), 77 deletions(-) create mode 100644 plugin/studentfollowup/posts.php create mode 100644 plugin/studentfollowup/view/posts.html.twig diff --git a/main/cron/import_csv.php b/main/cron/import_csv.php index eca6231a37..2ea0ef3915 100755 --- a/main/cron/import_csv.php +++ b/main/cron/import_csv.php @@ -3,6 +3,7 @@ use Chamilo\CourseBundle\Entity\CCalendarEvent; use Chamilo\CourseBundle\Entity\CItemProperty; +use Chamilo\PluginBundle\Entity\StudentFollowUp\CarePost; if (PHP_SAPI != 'cli') { die('Run this script through the command line or comment this line in the code'); @@ -2097,14 +2098,15 @@ class ImportCsv ksort($items); foreach ($items as $row) { + // Insert user $insertUserInfo = api_get_user_info_from_username($row['Added_by']); if (empty($insertUserInfo)) { $this->logger->addInfo("User does '".$row['Added_by']."' not exists skip this entry."); continue; } - $insertUserInfo = api_get_user_entity($insertUserInfo['user_id']); + // User about the post $userId = UserManager::get_user_id_from_original_id( $row['External_user_id'], $this->extraFieldIdNameList['user'] @@ -2117,8 +2119,12 @@ class ImportCsv } } $userInfo = api_get_user_entity($userId); + + // Dates $createdAt = $this->createDateTime($row['Added_On']); $updatedAt = $this->createDateTime($row['Edited_on']); + + // Parent $parent = null; if (!empty($row['Parent_id'])) { $parentId = $items[$row['Parent_id']]; @@ -2128,8 +2134,21 @@ class ImportCsv $parent = $em->getRepository('ChamiloPluginBundle:StudentFollowUp\CarePost')->findOneBy($criteria); } + // Tags $tags = explode(',', $row['Tags']); - $post = new \Chamilo\PluginBundle\Entity\StudentFollowUp\CarePost(); + + // Check if post already was added: + $criteria = [ + 'externalCareId' => $row['External_care_id'] + ]; + var_dump($criteria); + $post = $em->getRepository('ChamiloPluginBundle:StudentFollowUp\CarePost')->findOneBy($criteria); + + if (empty($post)) { + var_dump('empty'); + $post = new CarePost(); + } + $post ->setTitle($row['Title']) ->setContent($row['Article']) @@ -2145,6 +2164,7 @@ class ImportCsv ; $em->persist($post); $em->flush(); + if (($counter % $batchSize) === 0) { $em->flush(); $em->clear(); // Detaches all objects from Doctrine! diff --git a/plugin/studentfollowup/Entity/CarePost.php b/plugin/studentfollowup/Entity/CarePost.php index 85e054bfc3..97851aad0d 100644 --- a/plugin/studentfollowup/Entity/CarePost.php +++ b/plugin/studentfollowup/Entity/CarePost.php @@ -41,7 +41,7 @@ class CarePost /** * @var string * - * @ORM\Column(name="external_care_id", type="integer", nullable=true) + * @ORM\Column(name="external_care_id", type="bigint", nullable=true) */ protected $externalCareId; @@ -59,13 +59,13 @@ class CarePost /** * @Gedmo\Timestampable(on="create") - * @ORM\Column(name="created_at", type="datetime") + * @ORM\Column(name="created_at", type="datetime", nullable=true) */ protected $createdAt; /** * @Gedmo\Timestampable(on="update") - * @ORM\Column(name="updated_at", type="datetime") + * @ORM\Column(name="updated_at", type="datetime", nullable=true) */ protected $updatedAt; @@ -318,6 +318,14 @@ class CarePost return $this; } + /** + * @return int + */ + public function hasParent() + { + return !empty($this->parent) ? 1 : 0; + } + /** * @return mixed */ diff --git a/plugin/studentfollowup/StudentFollowUpPlugin.php b/plugin/studentfollowup/StudentFollowUpPlugin.php index d92f4d08b3..519a4b75b6 100644 --- a/plugin/studentfollowup/StudentFollowUpPlugin.php +++ b/plugin/studentfollowup/StudentFollowUpPlugin.php @@ -46,8 +46,7 @@ class StudentFollowUpPlugin extends Plugin $fs = new Filesystem(); $fs->mirror(__DIR__.'/Entity/', $pluginEntityPath, null, ['override']); - $table = Database::get_main_table('sfu_post'); - $sql = "CREATE TABLE sfu_post (id INT AUTO_INCREMENT NOT NULL, insert_user_id INT NOT NULL, user_id INT NOT NULL, parent_id INT DEFAULT NULL, title VARCHAR(255) NOT NULL, content LONGTEXT DEFAULT NULL, external_care_id INT DEFAULT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, private TINYINT(1) NOT NULL, external_source TINYINT(1) NOT NULL, tags LONGTEXT NOT NULL COMMENT '(DC2Type:array)', attachment VARCHAR(255) NOT NULL, lft INT DEFAULT NULL, rgt INT DEFAULT NULL, lvl INT DEFAULT NULL, root INT DEFAULT NULL, INDEX IDX_35F9473C9C859CC3 (insert_user_id), INDEX IDX_35F9473CA76ED395 (user_id), INDEX IDX_35F9473C727ACA70 (parent_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;"; + $sql = "CREATE TABLE sfu_post (id INT AUTO_INCREMENT NOT NULL, insert_user_id INT NOT NULL, user_id INT NOT NULL, parent_id INT DEFAULT NULL, title VARCHAR(255) NOT NULL, content LONGTEXT DEFAULT NULL, external_care_id BIGINT DEFAULT NULL, created_at DATETIME DEFAULT NULL, updated_at DATETIME DEFAULT NULL, private TINYINT(1) NOT NULL, external_source TINYINT(1) NOT NULL, tags LONGTEXT NOT NULL COMMENT '(DC2Type:array)', attachment VARCHAR(255) NOT NULL, lft INT DEFAULT NULL, rgt INT DEFAULT NULL, lvl INT DEFAULT NULL, root INT DEFAULT NULL, INDEX IDX_35F9473C9C859CC3 (insert_user_id), INDEX IDX_35F9473CA76ED395 (user_id), INDEX IDX_35F9473C727ACA70 (parent_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;"; Database::query($sql); $sql = "ALTER TABLE sfu_post ADD CONSTRAINT FK_35F9473C9C859CC3 FOREIGN KEY (insert_user_id) REFERENCES user (id);"; Database::query($sql); @@ -79,4 +78,47 @@ class StudentFollowUpPlugin extends Plugin { return api_get_path(SYS_PATH).'src/Chamilo/PluginBundle/Entity/'.$this->getCamelCaseName(); } + + /** + * @param int $studentId + * @param int $currentUserId + * @return array + */ + public static function getPermissions($studentId, $currentUserId) + { + $isAllow = false; + $showPrivate = false; + if ($studentId === $currentUserId) { + $isAllow = true; + } else { + // Only admins and DRH that follow the user + $isAdminOrDrh = UserManager::is_user_followed_by_drh($studentId, $currentUserId) || api_is_platform_admin(); + + // Check if course session coach + $sessions = SessionManager::get_sessions_by_user($studentId); + + $isCourseCoach = false; + if (!empty($sessions)) { + foreach ($sessions as $session) { + $sessionId = $session['session_id']; + foreach ($session['courses'] as $course) { + //$isCourseCoach = api_is_coach($sessionId, $course['real_id']); + $coachList = SessionManager::getCoachesByCourseSession($sessionId, $course['real_id']); + if (!empty($coachList) && in_array($currentUserId, $coachList)) { + $isCourseCoach = true; + break(2); + } + } + } + } + + $isAllow = $isAdminOrDrh || $isCourseCoach; + $showPrivate = $isAdminOrDrh; + } + + return [ + 'is_allow' => $isAllow, + 'show_private' => $showPrivate, + ]; + } } diff --git a/plugin/studentfollowup/plugin.php b/plugin/studentfollowup/plugin.php index 8f61bea213..a5acda6559 100644 --- a/plugin/studentfollowup/plugin.php +++ b/plugin/studentfollowup/plugin.php @@ -18,7 +18,7 @@ if ($form->validate()) { } -$plugin_info['templates'] = array('view/post.html.twig'); +$plugin_info['templates'] = array('view/post.html.twig', 'view/posts.html.twig'); $plugin_info['settings_form'] = $form; diff --git a/plugin/studentfollowup/post.php b/plugin/studentfollowup/post.php index 6f36f38391..61c419ffa4 100644 --- a/plugin/studentfollowup/post.php +++ b/plugin/studentfollowup/post.php @@ -10,42 +10,15 @@ $plugin = StudentFollowUpPlugin::create(); $currentUserId = api_get_user_id(); $studentId = isset($_GET['student_id']) ? (int) $_GET['student_id'] : api_get_user_id(); -$currentPage = isset($_GET['page']) ? (int) $_GET['page'] : 1; $postId = isset($_GET['post_id']) ? (int) $_GET['post_id'] : 1; if (empty($studentId)) { api_not_allowed(true); } -$isAllow = false; -$showPrivate = false; -if ($studentId === $currentUserId) { - $isAllow = true; -} else { - // Only admins and DRH that follow the user - $isAdminOrDrh = UserManager::is_user_followed_by_drh($studentId, $currentUserId) || api_is_platform_admin(); - - // Check if course session coach - $sessions = SessionManager::get_sessions_by_user($studentId); - - $isCourseCoach = false; - if (!empty($sessions)) { - foreach ($sessions as $session) { - $sessionId = $session['session_id']; - foreach ($session['courses'] as $course) { - //$isCourseCoach = api_is_coach($sessionId, $course['real_id']); - $coachList = SessionManager::getCoachesByCourseSession($sessionId, $course['real_id']); - if (!empty($coachList) && in_array($currentUserId, $coachList)) { - $isCourseCoach = true; - break(2); - } - } - } - } - - $isAllow = $isAdminOrDrh || $isCourseCoach; - $showPrivate = $isAdminOrDrh; -} +$permissions = StudentFollowUpPlugin::getPermissions($studentId, $currentUserId); +$isAllow = $permissions['is_allow']; +$showPrivate = $permissions['show_private']; if ($isAllow === false) { api_not_allowed(true); @@ -60,45 +33,30 @@ if ($showPrivate == false) { $criteria->andWhere(Criteria::expr()->eq('private', false)); } -if (!empty($postId)) { - //$criteria->andWhere(Criteria::expr()->eq('private', false)); -} - -$pageSize = 2; - +$criteria->andWhere(Criteria::expr()->eq('id', $postId)); $qb ->select('p') ->from('ChamiloPluginBundle:StudentFollowUp\CarePost', 'p') ->addCriteria($criteria) - ->setFirstResult($pageSize * ($currentPage-1)) - ->setMaxResults($pageSize) - ->orderBy('p.createdAt', 'desc') + ->setMaxResults(1) ; $query = $qb->getQuery(); - -$posts = new Paginator($query, $fetchJoinCollection = true); - -$totalItems = count($posts); -$pagesCount = ceil($totalItems / $pageSize); -$pagination = ''; -$url = api_get_self().'?student_id='.$studentId; -$pagination .= ''; +$post = $query->getOneOrNullResult(); $tpl = new Template($plugin->get_lang('plugin_title')); -$tpl->assign('posts', $posts); -$tpl->assign('current_url', $url); +$tpl->assign('post', $post); +$url = api_get_path(WEB_PLUGIN_PATH).'/studentfollowup/post.php?student_id='.$studentId; +$tpl->assign('post_url', $url); +$tpl->assign( + 'back_link', + Display::url( + Display::return_icon('back.png'), + api_get_path(WEB_PLUGIN_PATH).'studentfollowup/posts.php?student_id='.$studentId + ) +); +$tpl->assign('information_icon', Display::return_icon('info.png')); -$tpl->assign('pagination', $pagination); $content = $tpl->fetch('/'.$plugin->get_name().'/view/post.html.twig'); // Assign into content $tpl->assign('content', $content); diff --git a/plugin/studentfollowup/posts.php b/plugin/studentfollowup/posts.php new file mode 100644 index 0000000000..ca455dddd0 --- /dev/null +++ b/plugin/studentfollowup/posts.php @@ -0,0 +1,78 @@ +createQueryBuilder(); +$criteria = Criteria::create(); +$criteria->where(Criteria::expr()->eq('user', $studentId)); + +if ($showPrivate == false) { + $criteria->andWhere(Criteria::expr()->eq('private', false)); +} + +$pageSize = 2; + +$qb + ->select('p') + ->from('ChamiloPluginBundle:StudentFollowUp\CarePost', 'p') + ->addCriteria($criteria) + ->setFirstResult($pageSize * ($currentPage-1)) + ->setMaxResults($pageSize) + ->orderBy('p.createdAt', 'desc') +; + +$query = $qb->getQuery(); + +$posts = new Paginator($query, $fetchJoinCollection = true); + +$totalItems = count($posts); +$pagesCount = ceil($totalItems / $pageSize); +$pagination = ''; +$url = api_get_self().'?student_id='.$studentId; +$pagination .= ''; + +$tpl = new Template($plugin->get_lang('plugin_title')); +$tpl->assign('posts', $posts); +$tpl->assign('current_url', $url); + +$url = api_get_path(WEB_PLUGIN_PATH).'studentfollowup/post.php?student_id='.$studentId; +$tpl->assign('post_url', $url); +$tpl->assign('information_icon', Display::return_icon('info.png')); + +$tpl->assign('pagination', $pagination); +$content = $tpl->fetch('/'.$plugin->get_name().'/view/posts.html.twig'); +// Assign into content +$tpl->assign('content', $content); +// Display +$tpl->display_one_col_template(); diff --git a/plugin/studentfollowup/view/post.html.twig b/plugin/studentfollowup/view/post.html.twig index 6559b9adb4..222d280408 100644 --- a/plugin/studentfollowup/view/post.html.twig +++ b/plugin/studentfollowup/view/post.html.twig @@ -1,6 +1,5 @@ -

Care detail view

-{% if posts %} - {% for post in posts %} +{% macro post_template(post, information_icon, post_url, current_url) %} + {% if post %}

{{ post.title }}

{{ post.content }}

{{ post.createdAt |date('d/m/Y') }}

@@ -20,9 +19,19 @@ {% endfor %} {% endif %} + {% set countElements = post.hasParent + post.children.count %} + + {% if countElements %} + {% if countElements > 1%} +

{{ information_icon }} + {{ countElements }}

+ {% else %} +

{{ information_icon }}

+ {% endif %} + {% endif %} + {% if post.parent %}

Parent

- + {{ post.parent.title }} {% endif %} @@ -31,15 +40,19 @@

Children

{% for child in post.children %}

- + {{ child.title }}

{% endfor %} {% endif %} + {% endif %} +{% endmacro %} + +{% import _self as template %} - {% endfor %} -
- {{ pagination }} -
-{% endif %} \ No newline at end of file +
+ {{ back_link }} +
+

Care detail view

+{{ template.post_template(post, information_icon, post_url, current_url) }} \ No newline at end of file diff --git a/plugin/studentfollowup/view/posts.html.twig b/plugin/studentfollowup/view/posts.html.twig new file mode 100644 index 0000000000..6f88d68fc1 --- /dev/null +++ b/plugin/studentfollowup/view/posts.html.twig @@ -0,0 +1,13 @@ +{% import 'studentfollowup/view/post.html.twig' as template %} + +

Care detail view

+{% if posts %} + {% for post in posts %} + {{ template.post_template(post, information_icon, post_url, current_url) }} + + + {% endfor %} +
+ {{ pagination }} +
+{% endif %} \ No newline at end of file