Improve UI, fix update process see BT#12723

pull/2487/head
jmontoyaa 9 years ago
parent 8f46c81032
commit 78bb332e99
  1. 24
      main/cron/import_csv.php
  2. 14
      plugin/studentfollowup/Entity/CarePost.php
  3. 46
      plugin/studentfollowup/StudentFollowUpPlugin.php
  4. 2
      plugin/studentfollowup/plugin.php
  5. 76
      plugin/studentfollowup/post.php
  6. 78
      plugin/studentfollowup/posts.php
  7. 33
      plugin/studentfollowup/view/post.html.twig
  8. 13
      plugin/studentfollowup/view/posts.html.twig

@ -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!

@ -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
*/

@ -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,
];
}
}

@ -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;

@ -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 .= '<ul class="pagination">';
for ($i = 0; $i < $pagesCount; $i++) {
$newPage = $i + 1;
if ($currentPage == $newPage) {
$pagination .= '<li class="active"><a href="'.$url.'&page='.$newPage.'">'.$newPage.'</a></li>';
} else {
$pagination .= '<li><a href="'.$url.'&page='.$newPage.'">'.$newPage.'</a></li>';
}
}
$pagination .= '</ul>';
$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);

@ -0,0 +1,78 @@
<?php
/* For licensing terms, see /license.txt */
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Tools\Pagination\Paginator;
require_once __DIR__.'/../../main/inc/global.inc.php';
$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;
if (empty($studentId)) {
api_not_allowed(true);
}
$permissions = StudentFollowUpPlugin::getPermissions($studentId, $currentUserId);
$isAllow = $permissions['is_allow'];
$showPrivate = $permissions['show_private'];
if ($isAllow === false) {
api_not_allowed(true);
}
$em = Database::getManager();
$qb = $em->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 .= '<ul class="pagination">';
for ($i = 0; $i < $pagesCount; $i++) {
$newPage = $i + 1;
if ($currentPage == $newPage) {
$pagination .= '<li class="active"><a href="'.$url.'&page='.$newPage.'">'.$newPage.'</a></li>';
} else {
$pagination .= '<li><a href="'.$url.'&page='.$newPage.'">'.$newPage.'</a></li>';
}
}
$pagination .= '</ul>';
$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();

@ -1,6 +1,5 @@
<h1>Care detail view</h1>
{% if posts %}
{% for post in posts %}
{% macro post_template(post, information_icon, post_url, current_url) %}
{% if post %}
<h2>{{ post.title }}</h2>
<p>{{ post.content }}</p>
<p>{{ post.createdAt |date('d/m/Y') }}</p>
@ -20,9 +19,19 @@
{% endfor %}
{% endif %}
{% set countElements = post.hasParent + post.children.count %}
{% if countElements %}
{% if countElements > 1%}
<p>{{ information_icon }} + {{ countElements }}</p>
{% else %}
<p>{{ information_icon }} </p>
{% endif %}
{% endif %}
{% if post.parent %}
<h3>Parent</h3>
<a href="{{ current_url }}&post_id={{ post.parent.id }}">
<a href="{{ post_url }}&post_id={{ post.parent.id }}">
{{ post.parent.title }}
</a>
{% endif %}
@ -31,15 +40,19 @@
<h3>Children</h3>
{% for child in post.children %}
<p>
<a href="{{ current_url }}&post_id={{ child.id }}">
<a href="{{ post_url }}&post_id={{ child.id }}">
{{ child.title }}
</a>
</p>
{% endfor %}
{% endif %}
{% endif %}
{% endmacro %}
{% import _self as template %}
{% endfor %}
<div>
{{ pagination }}
</div>
{% endif %}
<div class="actions">
{{ back_link }}
</div>
<h1>Care detail view</h1>
{{ template.post_template(post, information_icon, post_url, current_url) }}

@ -0,0 +1,13 @@
{% import 'studentfollowup/view/post.html.twig' as template %}
<h1>Care detail view</h1>
{% if posts %}
{% for post in posts %}
{{ template.post_template(post, information_icon, post_url, current_url) }}
{% endfor %}
<div>
{{ pagination }}
</div>
{% endif %}
Loading…
Cancel
Save