Internal - Fix add links to resource in agenda/announcements.

pull/3570/head
Julio Montoya 5 years ago
parent db53d71935
commit b9675b2d4b
  1. 13
      public/main/announcements/announcements.php
  2. 43
      public/main/inc/lib/AnnouncementEmail.php
  3. 166
      public/main/inc/lib/AnnouncementManager.php
  4. 102
      public/main/inc/lib/agenda.lib.php
  5. 95
      src/CoreBundle/Entity/AbstractResource.php
  6. 31
      src/CourseBundle/Entity/CAnnouncement.php
  7. 12
      src/CourseBundle/Entity/CAnnouncementAttachment.php
  8. 26
      src/CourseBundle/Entity/CCalendarEvent.php

@ -612,14 +612,18 @@ switch ($action) {
);
if (!$announcementAttachmentIsDisabled) {
$form->addElement('file', 'user_upload', get_lang('AddAnAttachment'));
$form->addElement('textarea', 'file_comment', get_lang('FileComment'));
$form->addElement('file', 'user_upload', get_lang('Add attachment'));
$form->addElement('textarea', 'file_comment', get_lang('File comment'));
}
$form->addHidden('sec_token', $token);
if (empty($sessionId)) {
$form->addCheckBox('send_to_users_in_session', null, get_lang('Send to users in all sessions of this course'));
$form->addCheckBox(
'send_to_users_in_session',
null,
get_lang('Send to users in all sessions of this course')
);
}
$config = api_get_configuration_value('announcement.hide_send_to_hrm_users');
@ -659,7 +663,6 @@ switch ($action) {
// there is an Id => the announcement already exists => update mode
$file_comment = $announcementAttachmentIsDisabled ? null : $_POST['file_comment'];
$file = $announcementAttachmentIsDisabled ? [] : $_FILES['user_upload'];
$announcement = AnnouncementManager::edit_announcement(
$id,
$data['title'],
@ -765,7 +768,7 @@ if (!empty($_GET['remind_inactive'])) {
$to[] = 'USER:'.(int) ($_GET['remind_inactive']);
}
if (empty($_GET['origin']) or 'learnpath' !== $_GET['origin']) {
if (empty($_GET['origin']) || 'learnpath' !== $_GET['origin']) {
// We are not in the learning path
Display::display_header($nameTools, get_lang('Announcements'));
}

@ -62,45 +62,6 @@ class AnnouncementEmail
return $userList;
}
/**
* Returns users and groups an announcement item has been sent to.
*
* @return array Array of users and groups to whom the element has been sent
*/
public function sent_to_info()
{
return AnnouncementManager::getSenders($this->announcement);
$sql = "SELECT to_group_id, to_user_id
FROM $table
WHERE
c_id = $course_id AND
tool = '$tool' AND
ref = $id
$sessionCondition";
$rs = Database::query($sql);
while ($row = Database::fetch_array($rs, 'ASSOC')) {
// if to_user_id <> 0 then it is sent to a specific user
$user_id = $row['to_user_id'];
if (!empty($user_id)) {
$result['users'][] = (int) $user_id;
// If user is set then skip the group
continue;
}
// if to_group_id is null then it is sent to a specific user
// if to_group_id = 0 then it is sent to everybody
$group_id = $row['to_group_id'];
if (!empty($group_id)) {
$result['groups'][] = (int) $group_id;
}
}
return $result;
}
/**
* Returns the list of user info to which an announcement was sent.
* This function returns a list of actual users even when recipient
@ -110,7 +71,7 @@ class AnnouncementEmail
*/
public function sent_to()
{
$sent_to = $this->sent_to_info();
$sent_to = $this->announcement->getUsersAndGroupSubscribedToResource();
$users = $sent_to['users'];
$users = $users ? $users : [];
$groups = $sent_to['groups'];
@ -139,7 +100,7 @@ class AnnouncementEmail
$newListUsers = [];
if (!empty($users)) {
foreach ($users as $user) {
$newListUsers[$user['user_id']] = ['user_id' => $user['user_id']];
$newListUsers[$user['id']] = ['user_id' => $user['id']];
}
}

@ -531,7 +531,7 @@ class AnnouncementManager
$allow = !api_get_configuration_value('hide_announcement_sent_to_users_info');
if ($allow && api_is_allowed_to_edit(false, true)) {
$sent_to = self::sent_to('announcement', $id);
$sent_to = $announcement->getUsersAndGroupSubscribedToResource();
$sentToForm = self::sent_to_form($sent_to);
$html .= Display::tag(
'td',
@ -908,61 +908,35 @@ class AnnouncementManager
$repo->getEntityManager()->persist($announcement);
$repo->getEntityManager()->flush();
// save attachment file
$row_attach = self::get_attachment($id);
$id_attach = 0;
if ($row_attach) {
$id_attach = (int) $row_attach['id'];
}
if (!empty($file)) {
if (empty($id_attach)) {
self::add_announcement_attachment_file(
$announcement,
$file_comment,
$file
);
self::add_announcement_attachment_file(
$announcement,
$file_comment,
$file
);
/*if (empty($id_attach)) {
} else {
self::edit_announcement_attachment_file(
$id_attach,
$file,
$file_comment
);
}
}*/
}
// We remove everything from item_property for this
/*$sql = "DELETE FROM $tbl_item_property
WHERE c_id = $courseId AND ref='$id' AND tool='announcement'";
Database::query($sql);*/
if ($sendToUsersInSession) {
self::addAnnouncementToAllUsersInSessions($announcement);
}
// store in item_property (first the groups, then the users
// store, first the groups, then the users
if (!empty($to)) {
// !is_null($to): when no user is selected we send it to everyone
$send_to = CourseManager::separateUsersGroups($to);
$resourceNode = $announcement->getResourceNode();
// storing the selected groups
if (is_array($send_to['groups'])) {
foreach ($send_to['groups'] as $group) {
$groupInfo = api_get_group_entity($group);
$announcement->addGroupLink($course, $session, $groupInfo);
/*
if ($groupInfo) {
api_item_property_update(
$courseInfo,
TOOL_ANNOUNCEMENT,
$id,
'AnnouncementUpdated',
api_get_user_id(),
$groupInfo
);
}*/
foreach ($send_to['groups'] as $groupId) {
$announcement->addGroupLink($course, $session, api_get_group_entity($groupId));
}
}
@ -971,43 +945,18 @@ class AnnouncementManager
foreach ($send_to['users'] as $user) {
$user = api_get_user_entity($user);
$announcement->addUserLink($user, $course, $session, $group);
/*api_item_property_update(
$courseInfo,
TOOL_ANNOUNCEMENT,
$id,
'AnnouncementUpdated',
api_get_user_id(),
0,
$user
);*/
}
}
// Send to everyone
if (isset($to[0]) && 'everyone' === $to[0]) {
/*api_item_property_update(
$courseInfo,
TOOL_ANNOUNCEMENT,
$id,
'AnnouncementUpdated',
api_get_user_id(),
0
);*/
$announcement->addCourseLink($course, $session, $group);
}
} else {
// the message is sent to everyone, so we set the group to 0
/*api_item_property_update(
$courseInfo,
TOOL_ANNOUNCEMENT,
$id,
'AnnouncementUpdated',
api_get_user_id(),
0
);*/
$announcement->addCourseLink($course, $session);
}
$repo->getEntityManager()->persist($announcement);
$repo->getEntityManager()->flush();
$repo->updateResource($announcement);
return $announcement;
}
@ -1128,31 +1077,6 @@ class AnnouncementManager
return $result;*/
}
public static function getSenders(CAnnouncement $announcement)
{
$result = [];
$result['groups'] = [];
$result['users'] = [];
$links = $announcement->getResourceNode()->getResourceLinks();
if (empty($links)) {
return $result;
}
/** @var ResourceLink $link */
foreach ($links as $link) {
if ($link->getUser()) {
$result['users'][] = $link->getUser()->getId();
}
if ($link->getGroup()) {
$result['groups'][] = $link->getGroup()->getIid();
}
}
return $result;
}
/**
* This tools loads all the users and all the groups who have received
* a specific item (in this case an announcement item).
@ -1164,7 +1088,7 @@ class AnnouncementManager
*/
public static function loadEditUsers($announcement, $includeGroupWhenLoadingUser = false)
{
$result = self::getSenders($announcement);
$result = $announcement->getUsersAndGroupSubscribedToResource();
$to = [];
foreach ($result['users'] as $itemId) {
@ -1240,7 +1164,7 @@ class AnnouncementManager
}
} else {
// there is only one user/group
if (isset($sent_to_array['users']) && is_array($sent_to_array['users'])) {
if (isset($sent_to_array['users']) && !empty($sent_to_array['users'])) {
$user_info = api_get_user_info($sent_to_array['users'][0]);
$output[] = api_get_person_name($user_info['firstname'], $user_info['lastname']);
}
@ -1276,57 +1200,6 @@ class AnnouncementManager
}
}
/**
* Returns all the users and all the groups a specific announcement item
* has been sent to.
*
* @param string The tool (announcement, agenda, ...)
* @param int ID of the element of the corresponding type
*
* @return array Array of users and groups to whom the element has been sent
*/
public static function sent_to($tool, $id)
{
return [];
$table = Database::get_course_table(TABLE_ITEM_PROPERTY);
$tool = Database::escape_string($tool);
$id = (int) $id;
$sent_to_group = [];
$sent_to = [];
$courseId = api_get_course_int_id();
$sql = "SELECT to_group_id, to_user_id
FROM $table
WHERE c_id = $courseId AND tool = '$tool' AND ref=".$id;
$result = Database::query($sql);
while ($row = Database::fetch_array($result)) {
// if to_user_id <> 0 then it is sent to a specific user
if (0 != $row['to_user_id']) {
$sent_to_user[] = $row['to_user_id'];
continue;
}
// if to_group_id is null then it is sent to a specific user
// if to_group_id = 0 then it is sent to everybody
if (0 != $row['to_group_id']) {
$sent_to_group[] = $row['to_group_id'];
}
}
if (isset($sent_to_group)) {
$sent_to['groups'] = $sent_to_group;
}
if (isset($sent_to_user)) {
$sent_to['users'] = $sent_to_user;
}
return $sent_to;
}
/**
* Show a list with all the attachments according to the post's id.
*
@ -1363,11 +1236,8 @@ class AnnouncementManager
*
* @return int -1 if failed, 0 if unknown (should not happen), 1 if success
*/
public static function add_announcement_attachment_file(
CAnnouncement $announcement,
$file_comment,
$file
) {
public static function add_announcement_attachment_file(CAnnouncement $announcement, $file_comment, $file)
{
$return = 0;
$courseId = api_get_course_int_id();

@ -337,7 +337,7 @@ class Agenda
if ($id) {
// Add announcement.
if ($addAsAnnouncement) {
$this->storeAgendaEventAsAnnouncement($id, $usersToSend);
$this->storeAgendaEventAsAnnouncement($event, $usersToSend);
}
// Add attachment.
@ -554,50 +554,31 @@ class Agenda
return true;
}
/**
* @param int $item_id
* @param array $sentTo
*
* @return int
*/
public function storeAgendaEventAsAnnouncement($item_id, $sentTo = [])
public function storeAgendaEventAsAnnouncement(CCalendarEvent $event, array $sentTo = [])
{
$table_agenda = Database::get_course_table(TABLE_AGENDA);
$courseId = api_get_course_int_id();
// Get the agenda item.
$item_id = (int) $item_id;
$sql = "SELECT * FROM $table_agenda
WHERE c_id = $courseId AND id = ".$item_id;
$res = Database::query($sql);
if (Database::num_rows($res) > 0) {
$row = Database::fetch_array($res, 'ASSOC');
// Sending announcement
if (!empty($sentTo)) {
$id = AnnouncementManager::add_announcement(
api_get_course_info(),
api_get_session_id(),
$row['title'],
$row['content'],
$sentTo,
null,
null,
$row['end_date']
);
// Sending announcement
if (!empty($sentTo)) {
$id = AnnouncementManager::add_announcement(
api_get_course_info(),
api_get_session_id(),
$event->getTitle(),
$event->getContent(),
$sentTo,
null,
null,
$event->getEndDate()->format('Y-m-d H:i:s')
);
AnnouncementManager::sendEmail(
api_get_course_info(),
api_get_session_id(),
$id
);
AnnouncementManager::sendEmail(
api_get_course_info(),
api_get_session_id(),
$id
);
return $id;
}
return true;
}
return -1;
return false;
}
/**
@ -680,7 +661,7 @@ class Agenda
return false;
}
$sentToEvent = $event->getUsersAndGroupSubscribedToEvent();
$sentToEvent = $event->getUsersAndGroupSubscribedToResource();
$courseId = $this->course['real_id'];
if (empty($courseId)) {
@ -794,7 +775,7 @@ class Agenda
// Add announcement.
if (isset($addAnnouncement) && !empty($addAnnouncement)) {
$this->storeAgendaEventAsAnnouncement($id, $usersToSend);
$this->storeAgendaEventAsAnnouncement($event, $usersToSend);
}
// Add attachment.
@ -916,7 +897,7 @@ class Agenda
$this->table_repeat,
[
'cal_id = ?' => [
$id
$id,
],
]
);
@ -1290,7 +1271,7 @@ class Agenda
$event['description'] = $eventEntity->getComment();
// Getting send to array
$event['send_to'] = $eventEntity->getUsersAndGroupSubscribedToEvent();
$event['send_to'] = $eventEntity->getUsersAndGroupSubscribedToResource();
// Getting repeat info
$event['repeat_info'] = $eventEntity->getRepeatEvents();
@ -1697,18 +1678,6 @@ class Agenda
$event['unique_id'] = $eventId;
$eventsAdded[] = $eventId;
//$eventId = $row['ref'];
/*$items = $this->getUsersAndGroupSubscribedToEvent(
$eventId,
$courseId,
$this->sessionId
);
$group_to_array = $items['groups'];
$user_to_array = $items['users'];*/
/*$attachmentList = $this->getAttachmentList(
$eventId,
$courseInfo
);*/
$attachmentList = $row->getAttachments();
$event['attachment'] = '';
if (!empty($attachmentList)) {
@ -2290,23 +2259,18 @@ class Agenda
if (isset($params['attachment']) && !empty($params['attachment'])) {
$attachmentList = $params['attachment'];
/** @var CCalendarEventAttachment $attachment */
foreach ($attachmentList as $attachment) {
$params['file_comment'] = $attachment['comment'];
if (!empty($attachment['path'])) {
$form->addElement(
'checkbox',
'delete_attachment['.$attachment['id'].']',
null,
get_lang(
'DeleteAttachment'
).': '.$attachment['filename']
);
}
$form->addElement(
'checkbox',
'delete_attachment['.$attachment->getIid().']',
null,
get_lang('Delete attachment').': '.$attachment->getFilename()
);
}
}
$form->addElement(
'textarea',
$form->addTextarea(
'file_comment',
get_lang('File comment')
);

@ -112,6 +112,22 @@ abstract class AbstractResource
throw new \Exception('addCourseLink requires to set the parent.');
}
if ($this->hasResourceNode()) {
$resourceNode = $this->getResourceNode();
$exists = $resourceNode->getResourceLinks()->exists(
function ($key, $element) use ($course, $session, $group) {
return
$course === $element->getCourse() &&
$session === $element->getSession() &&
$group === $element->getGroup();
}
);
if ($exists) {
return $this;
}
}
$resourceLink = new ResourceLink();
$resourceLink
->setVisibility($visibility)
@ -147,32 +163,51 @@ abstract class AbstractResource
public function addGroupLink(Course $course, Session $session = null, CGroup $group = null)
{
$resourceNode = $this->getResourceNode();
$exists = $resourceNode->getResourceLinks()->exists(
function ($key, $element) use ($group) {
if ($element->getGroup()) {
return $group->getIid() == $element->getGroup()->getIid();
if ($this->hasResourceNode()) {
$resourceNode = $this->getResourceNode();
$exists = $resourceNode->getResourceLinks()->exists(
function ($key, $element) use ($group) {
if ($element->getGroup()) {
return $group->getIid() === $element->getGroup()->getIid();
}
}
);
if ($exists) {
return $this;
}
);
if (false === $exists) {
$resourceLink = new ResourceLink();
$resourceLink
->setResourceNode($resourceNode)
->setCourse($course)
->setSession($session)
->setGroup($group)
->setVisibility(ResourceLink::VISIBILITY_PUBLISHED)
;
$this->addLink($resourceLink);
}
$resourceLink = new ResourceLink();
$resourceLink
->setResourceNode($resourceNode)
->setCourse($course)
->setSession($session)
->setGroup($group)
->setVisibility(ResourceLink::VISIBILITY_PUBLISHED)
;
$this->addLink($resourceLink);
return $this;
}
public function addUserLink(User $user, Course $course = null, Session $session = null, CGroup $group = null)
{
if ($this->hasResourceNode()) {
$resourceNode = $this->getResourceNode();
$exists = $resourceNode->getResourceLinks()->exists(
function ($key, $element) use ($user) {
if ($element->getUser()) {
return $user->getId() === $element->getUser()->getId();
}
}
);
if ($exists) {
return $this;
}
}
$resourceLink = new ResourceLink();
$resourceLink
->setVisibility(ResourceLink::VISIBILITY_PUBLISHED)
@ -367,4 +402,30 @@ abstract class AbstractResource
return null;
}
public function getUsersAndGroupSubscribedToResource(): array
{
$users = [];
$groups = [];
$everyone = false;
$links = $this->getResourceNode()->getResourceLinks();
foreach ($links as $link) {
if ($link->getUser()) {
$users[] = $link->getUser()->getId();
}
if ($link->getGroup()) {
$groups[] = $link->getGroup()->getIid();
}
}
if (empty($users) && empty($groups)) {
$everyone = true;
}
return [
'everyone' => $everyone,
'users' => $users,
'groups' => $groups,
];
}
}

@ -6,6 +6,7 @@ namespace Chamilo\CourseBundle\Entity;
use Chamilo\CoreBundle\Entity\AbstractResource;
use Chamilo\CoreBundle\Entity\ResourceInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
@ -64,7 +65,7 @@ class CAnnouncement extends AbstractResource implements ResourceInterface
protected $emailSent;
/**
* @var CAnnouncementAttachment[]
* @var ArrayCollection|CAnnouncementAttachment[]
*
* @ORM\OneToMany(
* targetEntity="CAnnouncementAttachment",
@ -73,14 +74,16 @@ class CAnnouncement extends AbstractResource implements ResourceInterface
*/
protected $attachments;
public function __construct()
{
$this->attachments = new ArrayCollection();
}
public function __toString(): string
{
return $this->getTitle();
}
/**
* @return CAnnouncementAttachment[]
*/
public function getAttachments()
{
return $this->attachments;
@ -100,10 +103,8 @@ class CAnnouncement extends AbstractResource implements ResourceInterface
* Set title.
*
* @param string $title
*
* @return CAnnouncement
*/
public function setTitle($title)
public function setTitle($title): self
{
$this->title = $title;
@ -124,10 +125,8 @@ class CAnnouncement extends AbstractResource implements ResourceInterface
* Set content.
*
* @param string $content
*
* @return CAnnouncement
*/
public function setContent($content)
public function setContent($content): self
{
$this->content = $content;
@ -148,10 +147,8 @@ class CAnnouncement extends AbstractResource implements ResourceInterface
* Set endDate.
*
* @param \DateTime $endDate
*
* @return CAnnouncement
*/
public function setEndDate($endDate)
public function setEndDate($endDate): self
{
$this->endDate = $endDate;
@ -172,10 +169,8 @@ class CAnnouncement extends AbstractResource implements ResourceInterface
* Set displayOrder.
*
* @param int $displayOrder
*
* @return CAnnouncement
*/
public function setDisplayOrder($displayOrder)
public function setDisplayOrder($displayOrder): self
{
$this->displayOrder = $displayOrder;
@ -196,10 +191,8 @@ class CAnnouncement extends AbstractResource implements ResourceInterface
* Set emailSent.
*
* @param bool $emailSent
*
* @return CAnnouncement
*/
public function setEmailSent($emailSent)
public function setEmailSent($emailSent): self
{
$this->emailSent = $emailSent;

@ -94,10 +94,8 @@ class CAnnouncementAttachment extends AbstractResource implements ResourceInterf
* Set comment.
*
* @param string $comment
*
* @return CAnnouncementAttachment
*/
public function setComment($comment)
public function setComment($comment): self
{
$this->comment = $comment;
@ -118,10 +116,8 @@ class CAnnouncementAttachment extends AbstractResource implements ResourceInterf
* Set size.
*
* @param int $size
*
* @return CAnnouncementAttachment
*/
public function setSize($size)
public function setSize($size): self
{
$this->size = $size;
@ -147,10 +143,8 @@ class CAnnouncementAttachment extends AbstractResource implements ResourceInterf
* Set filename.
*
* @param string $filename
*
* @return CAnnouncementAttachment
*/
public function setFilename($filename)
public function setFilename($filename): self
{
$this->filename = $filename;

@ -385,32 +385,6 @@ class CCalendarEvent extends AbstractResource implements ResourceInterface
return $this;
}
public function getUsersAndGroupSubscribedToEvent(): array
{
$users = [];
$groups = [];
$everyone = false;
$links = $this->getResourceNode()->getResourceLinks();
foreach ($links as $link) {
if ($link->getUser()) {
$users[] = $link->getUser()->getId();
}
if ($link->getGroup()) {
$groups[] = $link->getGroup()->getIid();
}
}
if (empty($users) && empty($groups)) {
$everyone = true;
}
return [
'everyone' => $everyone,
'users' => $users,
'groups' => $groups,
];
}
/**
* Resource identifier.
*/

Loading…
Cancel
Save