Add multiple attachments when adding an agenda event see BT#10952

1.9.x
Julio 10 years ago
parent 1706412fc8
commit 699095912e
  1. 151
      main/calendar/agenda.lib.php
  2. 69
      main/calendar/agenda.php

@ -122,8 +122,8 @@ class Agenda
* @param array $usersToSend array('everyone') or a list of user/group ids
* @param bool $addAsAnnouncement event as a *course* announcement
* @param int $parentEventId
* @param array $attachmentArray $_FILES['']
* @param string $attachmentComment
* @param array $attachmentArray array of $_FILES['']
* @param array $attachmentCommentList
* @param string $eventComment
* @param string $color
*
@ -139,7 +139,7 @@ class Agenda
$addAsAnnouncement = false,
$parentEventId = null,
$attachmentArray = array(),
$attachmentComment = null,
$attachmentCommentList = array(),
$eventComment = null,
$color = ''
) {
@ -304,12 +304,16 @@ class Agenda
// Add attachment.
if (isset($attachmentArray) && !empty($attachmentArray)) {
$this->addAttachment(
$id,
$attachmentArray,
$attachmentComment,
$this->course
);
$counter = 0;
foreach ($attachmentArray as $attachmentItem) {
$this->addAttachment(
$id,
$attachmentItem,
$attachmentCommentList[$counter],
$this->course
);
$counter++;
}
}
}
break;
@ -562,7 +566,7 @@ class Agenda
* @param string $content
* @param array $usersToSend
* @param array $attachmentArray
* @param string $attachmentComment
* @param array $attachmentCommentList
* @param string $comment
* @param string $color
* @param bool $addAnnouncement
@ -578,7 +582,7 @@ class Agenda
$content,
$usersToSend = array(),
$attachmentArray = array(),
$attachmentComment = null,
$attachmentCommentList = array(),
$comment = null,
$color = '',
$addAnnouncement = false
@ -801,12 +805,17 @@ class Agenda
// Add attachment.
if (isset($attachmentArray) && !empty($attachmentArray)) {
$this->updateAttachment(
$id,
$attachmentArray,
$attachmentComment,
$this->course
);
$counter = 0;
foreach ($attachmentArray as $attachmentItem) {
$this->updateAttachment(
$attachmentItem['id'],
$id,
$attachmentItem,
$attachmentCommentList[$counter],
$this->course
);
$counter++;
}
}
}
break;
@ -848,10 +857,11 @@ class Agenda
break;
case 'course':
$course_id = api_get_course_int_id();
if (!empty($course_id) && api_is_allowed_to_edit(null, true)) {
// Delete
$eventInfo = $this->get_event($id);
if ($deleteAllItemsFromSerie) {
$eventInfo = $this->get_event($id);
/* This is one of the children.
Getting siblings and delete 'Em all + the father! */
if (isset($eventInfo['parent_event_id']) && !empty($eventInfo['parent_event_id'])) {
@ -895,6 +905,12 @@ class Agenda
$this->table_repeat,
array('cal_id = ? AND c_id = ?' => array($id, $course_id))
);
if (isset($eventInfo['attachment']) && !empty($eventInfo['attachment'])) {
foreach ($eventInfo['attachment'] as $attachment) {
self::deleteAttachmentFile($attachment['id'], $this->course);
}
}
}
break;
case 'admin':
@ -1152,7 +1168,7 @@ class Agenda
$event['parent_info'] = $this->get_event($event['parent_event_id']);
}
$event['attachment'] = $this->getAttachment($id, $this->course);
$event['attachment'] = $this->getAttachmentList($id, $this->course);
}
}
break;
@ -1439,13 +1455,16 @@ class Agenda
}
$eventsAdded[] = $event['unique_id'];
$attachment = $this->getAttachment($row['id'], $courseInfo);
$attachmentList = $this->getAttachmentList($row['id'], $courseInfo);
if (!empty($attachmentList)) {
foreach ($attachmentList as $attachment) {
$has_attachment = Display::return_icon('attachment.gif', get_lang('Attachment'));
$user_filename = $attachment['filename'];
$url = api_get_path(WEB_CODE_PATH).'calendar/download.php?file='.$attachment['path'].'&course_id='.$course_id.'&'.api_get_cidreq();
$event['attachment'] .= $has_attachment.Display::url($user_filename, $url).'<br />';
}
if (!empty($attachment)) {
$has_attachment = Display::return_icon('attachment.gif', get_lang('Attachment'));
$user_filename = $attachment['filename'];
$url = api_get_path(WEB_CODE_PATH).'calendar/download.php?file='.$attachment['path'].'&course_id='.$course_id.'&'.api_get_cidreq();
$event['attachment'] = $has_attachment.Display::url($user_filename, $url);
} else {
$event['attachment'] = '';
}
@ -1944,23 +1963,36 @@ class Agenda
$form->addElement('textarea', 'comment', get_lang('Comment'));
}
$form->addElement('file', 'user_upload', get_lang('AddAnAttachment'));
if ($showAttachmentForm) {
//$form->addElement('file', 'user_upload', get_lang('AddAnAttachment'));
$form->addElement('advanced_settings', get_lang('FilesAttachment').'<span id="filepaths">
<div id="filepath_1">
<input type="file" name="attach_1"/><br />
'.get_lang('Description').'&nbsp;&nbsp;<input type="text" name="legend[]" /><br /><br />
</div>
</span>');
$form->addElement('advanced_settings','<span id="link-more-attach"><a href="javascript://" onclick="return add_image_form()">'.get_lang('AddOneMoreFile').'</a></span>&nbsp;('.sprintf(get_lang('MaximunFileSizeX'),format_file_size(api_get_setting('message_max_upload_filesize'))).')');
//if ($showAttachmentForm) {
if (isset($params['attachment']) && !empty($params['attachment'])) {
$attachment = $params['attachment'];
$params['file_comment'] = $attachment['comment'];
if (!empty($attachment['path'])) {
$form->addElement(
'checkbox',
'delete_attachment',
null,
get_lang('DeleteAttachment').' '.$attachment['filename']
);
$attachmentList = $params['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('textarea', 'file_comment', get_lang('FileComment'));
//$form->addElement('textarea', 'file_comment', get_lang('FileComment'));
if (!empty($id)) {
$form->addElement(
@ -2079,18 +2111,50 @@ class Agenda
* @param array $courseInfo
* @return array with the post info
*/
public function getAttachment($eventId, $courseInfo)
public function getAttachmentList($eventId, $courseInfo)
{
$tableAttachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
$courseId = intval($courseInfo['real_id']);
$eventId = intval($eventId);
$row = array();
$sql = "SELECT id, path, filename, comment
FROM $tableAttachment
WHERE
c_id = $courseId AND
agenda_id = $eventId";
$result = Database::query($sql);
$list = array();
if (Database::num_rows($result) != 0) {
$list = Database::store_result($result, 'ASSOC');
}
return $list;
}
/**
* Show a list with all the attachments according to the post's id
* @param int $attachmentId
* @param int $eventId
* @param array $courseInfo
* @return array with the post info
*/
public function getAttachment($attachmentId, $eventId, $courseInfo)
{
$tableAttachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
$courseId = intval($courseInfo['real_id']);
$eventId = intval($eventId);
$attachmentId = intval($attachmentId);
$row = array();
$sql = "SELECT id, path, filename, comment
FROM $tableAttachment
WHERE
c_id = $courseId AND
agenda_id = $eventId AND
id = $attachmentId
";
$result = Database::query($sql);
if (Database::num_rows($result) != 0) {
$row = Database::fetch_array($result, 'ASSOC');
}
@ -2162,16 +2226,17 @@ class Agenda
}
/**
* @param int $attachmentId
* @param int $eventId
* @param array $fileUserUpload
* @param string $comment
* @param array $courseInfo
*/
public function updateAttachment($eventId, $fileUserUpload, $comment, $courseInfo)
public function updateAttachment($attachmentId, $eventId, $fileUserUpload, $comment, $courseInfo)
{
$attachment = $this->getAttachment($eventId, $courseInfo);
$attachment = $this->getAttachment($attachmentId, $eventId, $courseInfo);
if (!empty($attachment)) {
$this->deleteAttachmentFile($attachment['id'], $courseInfo);
$this->deleteAttachmentFile($attachmentId, $courseInfo);
}
$this->addAttachment($eventId, $fileUserUpload, $comment, $courseInfo);
}

@ -8,7 +8,7 @@
use \ChamiloSession as Session;
// name of the language file that needs to be included
$language_file = array('agenda', 'group');
$language_file = array('agenda', 'group', 'messages', 'userInfo', 'admin');
// use anonymous mode when accessing this course tool
$use_anonymous = true;
@ -61,6 +61,33 @@ function plus_repeated_event() {
</script>
";
$htmlHeadXtra[] = '<script type="text/javascript">
var counter_image = 1;
function add_image_form() {
// Multiple filepaths for image form
var filepaths = document.getElementById("filepaths");
if (document.getElementById("filepath_"+counter_image)) {
counter_image = counter_image + 1;
} else {
counter_image = counter_image;
}
var elem1 = document.createElement("div");
elem1.setAttribute("id","filepath_"+counter_image);
filepaths.appendChild(elem1);
id_elem1 = "filepath_"+counter_image;
id_elem1 = "\'"+id_elem1+"\'";
document.getElementById("filepath_"+counter_image).innerHTML = "<input type=\"file\" name=\"attach_"+counter_image+"\" />&nbsp; <br />'.get_lang('Description').'&nbsp;&nbsp;<input type=\"text\" name=\"legend[]\" /><br /><br />";
if (filepaths.childNodes.length == 6) {
var link_attach = document.getElementById("link-more-attach");
if (link_attach) {
link_attach.innerHTML="";
}
}
}
</script>';
// setting the name of the tool
$nameTools = get_lang('Agenda');
@ -110,10 +137,9 @@ if (api_is_allowed_to_edit(false, true) OR
$sendEmail = isset($values['add_announcement']) ? true : false;
$allDay = isset($values['all_day']) ? 'true' : 'false';
$sendAttachment = isset($_FILES['user_upload']) ? true : false;
$attachment = $sendAttachment ? $_FILES['user_upload'] : null;
$attachmentComment = isset($values['file_comment']) ? $values['file_comment'] : null;
$sendAttachment = isset($_FILES) && !empty($_FILES) ? true : false;
$attachmentList = $sendAttachment ? $_FILES : null;
$attachmentCommentList = isset($values['legend']) ? $values['legend'] : null;
$comment = isset($values['comment']) ? $values['comment'] : null;
$startDate = $values['date_range_start'];
@ -128,8 +154,8 @@ if (api_is_allowed_to_edit(false, true) OR
$values['users_to_send'],
$sendEmail,
null,
$attachment,
$attachmentComment,
$attachmentList,
$attachmentCommentList,
$comment
);
@ -175,9 +201,10 @@ if (api_is_allowed_to_edit(false, true) OR
$startDate = $values['date_range_start'];
$endDate = $values['date_range_end'];
$sendAttachment = isset($_FILES['user_upload']) ? true : false;
$attachment = $sendAttachment ? $_FILES['user_upload'] : null;
$attachmentComment = isset($values['file_comment']) ? $values['file_comment'] : null;
$sendAttachment = isset($_FILES) && !empty($_FILES) ? true : false;
$attachmentList = $sendAttachment ? $_FILES : null;
$attachmentCommentList = isset($values['legend']) ? $values['legend'] : null;
$comment = isset($values['comment']) ? $values['comment'] : null;
// This is a sub event. Delete the current and create another BT#7803
@ -194,8 +221,8 @@ if (api_is_allowed_to_edit(false, true) OR
$values['users_to_send'],
false,
null,
$attachment,
$attachmentComment,
$attachmentList,
$attachmentCommentList,
$comment
);
@ -214,8 +241,8 @@ if (api_is_allowed_to_edit(false, true) OR
$values['title'],
$values['content'],
$values['users_to_send'],
$attachment,
$attachmentComment,
$attachmentList,
$attachmentCommentList,
$comment,
'',
$sendEmail
@ -232,13 +259,15 @@ if (api_is_allowed_to_edit(false, true) OR
);
}
$deleteAttachment = isset($values['delete_attachment']) ? true : false;
$deleteAttachmentList = isset($values['delete_attachment']) ? $values['delete_attachment'] : array();
if ($deleteAttachment && isset($event['attachment']) && !empty($event['attachment'])) {
$agenda->deleteAttachmentFile(
$event['attachment']['id'],
$agenda->course
);
if (!empty($deleteAttachmentList)) {
foreach ($deleteAttachmentList as $deleteAttachmentId => $value) {
$agenda->deleteAttachmentFile(
$deleteAttachmentId,
$agenda->course
);
}
}
$message = Display::return_message(get_lang('Updated'), 'confirmation');

Loading…
Cancel
Save