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 array $usersToSend array('everyone') or a list of user/group ids
* @param bool $addAsAnnouncement event as a *course* announcement * @param bool $addAsAnnouncement event as a *course* announcement
* @param int $parentEventId * @param int $parentEventId
* @param array $attachmentArray $_FILES[''] * @param array $attachmentArray array of $_FILES['']
* @param string $attachmentComment * @param array $attachmentCommentList
* @param string $eventComment * @param string $eventComment
* @param string $color * @param string $color
* *
@ -139,7 +139,7 @@ class Agenda
$addAsAnnouncement = false, $addAsAnnouncement = false,
$parentEventId = null, $parentEventId = null,
$attachmentArray = array(), $attachmentArray = array(),
$attachmentComment = null, $attachmentCommentList = array(),
$eventComment = null, $eventComment = null,
$color = '' $color = ''
) { ) {
@ -304,12 +304,16 @@ class Agenda
// Add attachment. // Add attachment.
if (isset($attachmentArray) && !empty($attachmentArray)) { if (isset($attachmentArray) && !empty($attachmentArray)) {
$this->addAttachment( $counter = 0;
$id, foreach ($attachmentArray as $attachmentItem) {
$attachmentArray, $this->addAttachment(
$attachmentComment, $id,
$this->course $attachmentItem,
); $attachmentCommentList[$counter],
$this->course
);
$counter++;
}
} }
} }
break; break;
@ -562,7 +566,7 @@ class Agenda
* @param string $content * @param string $content
* @param array $usersToSend * @param array $usersToSend
* @param array $attachmentArray * @param array $attachmentArray
* @param string $attachmentComment * @param array $attachmentCommentList
* @param string $comment * @param string $comment
* @param string $color * @param string $color
* @param bool $addAnnouncement * @param bool $addAnnouncement
@ -578,7 +582,7 @@ class Agenda
$content, $content,
$usersToSend = array(), $usersToSend = array(),
$attachmentArray = array(), $attachmentArray = array(),
$attachmentComment = null, $attachmentCommentList = array(),
$comment = null, $comment = null,
$color = '', $color = '',
$addAnnouncement = false $addAnnouncement = false
@ -801,12 +805,17 @@ class Agenda
// Add attachment. // Add attachment.
if (isset($attachmentArray) && !empty($attachmentArray)) { if (isset($attachmentArray) && !empty($attachmentArray)) {
$this->updateAttachment( $counter = 0;
$id, foreach ($attachmentArray as $attachmentItem) {
$attachmentArray, $this->updateAttachment(
$attachmentComment, $attachmentItem['id'],
$this->course $id,
); $attachmentItem,
$attachmentCommentList[$counter],
$this->course
);
$counter++;
}
} }
} }
break; break;
@ -848,10 +857,11 @@ class Agenda
break; break;
case 'course': case 'course':
$course_id = api_get_course_int_id(); $course_id = api_get_course_int_id();
if (!empty($course_id) && api_is_allowed_to_edit(null, true)) { if (!empty($course_id) && api_is_allowed_to_edit(null, true)) {
// Delete // Delete
$eventInfo = $this->get_event($id);
if ($deleteAllItemsFromSerie) { if ($deleteAllItemsFromSerie) {
$eventInfo = $this->get_event($id);
/* This is one of the children. /* This is one of the children.
Getting siblings and delete 'Em all + the father! */ Getting siblings and delete 'Em all + the father! */
if (isset($eventInfo['parent_event_id']) && !empty($eventInfo['parent_event_id'])) { if (isset($eventInfo['parent_event_id']) && !empty($eventInfo['parent_event_id'])) {
@ -895,6 +905,12 @@ class Agenda
$this->table_repeat, $this->table_repeat,
array('cal_id = ? AND c_id = ?' => array($id, $course_id)) 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; break;
case 'admin': case 'admin':
@ -1152,7 +1168,7 @@ class Agenda
$event['parent_info'] = $this->get_event($event['parent_event_id']); $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; break;
@ -1439,13 +1455,16 @@ class Agenda
} }
$eventsAdded[] = $event['unique_id']; $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 { } else {
$event['attachment'] = ''; $event['attachment'] = '';
} }
@ -1944,23 +1963,36 @@ class Agenda
$form->addElement('textarea', 'comment', get_lang('Comment')); $form->addElement('textarea', 'comment', get_lang('Comment'));
} }
$form->addElement('file', 'user_upload', get_lang('AddAnAttachment')); //$form->addElement('file', 'user_upload', get_lang('AddAnAttachment'));
if ($showAttachmentForm) {
$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'])) { if (isset($params['attachment']) && !empty($params['attachment'])) {
$attachment = $params['attachment']; $attachmentList = $params['attachment'];
$params['file_comment'] = $attachment['comment']; foreach ($attachmentList as $attachment) {
if (!empty($attachment['path'])) { $params['file_comment'] = $attachment['comment'];
$form->addElement( if (!empty($attachment['path'])) {
'checkbox', $form->addElement(
'delete_attachment', 'checkbox',
null, 'delete_attachment['.$attachment['id'].']',
get_lang('DeleteAttachment').' '.$attachment['filename'] 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)) { if (!empty($id)) {
$form->addElement( $form->addElement(
@ -2079,18 +2111,50 @@ class Agenda
* @param array $courseInfo * @param array $courseInfo
* @return array with the post info * @return array with the post info
*/ */
public function getAttachment($eventId, $courseInfo) public function getAttachmentList($eventId, $courseInfo)
{ {
$tableAttachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT); $tableAttachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
$courseId = intval($courseInfo['real_id']); $courseId = intval($courseInfo['real_id']);
$eventId = intval($eventId); $eventId = intval($eventId);
$row = array();
$sql = "SELECT id, path, filename, comment $sql = "SELECT id, path, filename, comment
FROM $tableAttachment FROM $tableAttachment
WHERE WHERE
c_id = $courseId AND c_id = $courseId AND
agenda_id = $eventId"; agenda_id = $eventId";
$result = Database::query($sql); $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) { if (Database::num_rows($result) != 0) {
$row = Database::fetch_array($result, 'ASSOC'); $row = Database::fetch_array($result, 'ASSOC');
} }
@ -2162,16 +2226,17 @@ class Agenda
} }
/** /**
* @param int $attachmentId
* @param int $eventId * @param int $eventId
* @param array $fileUserUpload * @param array $fileUserUpload
* @param string $comment * @param string $comment
* @param array $courseInfo * @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)) { if (!empty($attachment)) {
$this->deleteAttachmentFile($attachment['id'], $courseInfo); $this->deleteAttachmentFile($attachmentId, $courseInfo);
} }
$this->addAttachment($eventId, $fileUserUpload, $comment, $courseInfo); $this->addAttachment($eventId, $fileUserUpload, $comment, $courseInfo);
} }

@ -8,7 +8,7 @@
use \ChamiloSession as Session; use \ChamiloSession as Session;
// name of the language file that needs to be included // 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 mode when accessing this course tool
$use_anonymous = true; $use_anonymous = true;
@ -61,6 +61,33 @@ function plus_repeated_event() {
</script> </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 // setting the name of the tool
$nameTools = get_lang('Agenda'); $nameTools = get_lang('Agenda');
@ -110,10 +137,9 @@ if (api_is_allowed_to_edit(false, true) OR
$sendEmail = isset($values['add_announcement']) ? true : false; $sendEmail = isset($values['add_announcement']) ? true : false;
$allDay = isset($values['all_day']) ? 'true' : 'false'; $allDay = isset($values['all_day']) ? 'true' : 'false';
$sendAttachment = isset($_FILES['user_upload']) ? true : false; $sendAttachment = isset($_FILES) && !empty($_FILES) ? true : false;
$attachment = $sendAttachment ? $_FILES['user_upload'] : null; $attachmentList = $sendAttachment ? $_FILES : null;
$attachmentComment = isset($values['file_comment']) ? $values['file_comment'] : null; $attachmentCommentList = isset($values['legend']) ? $values['legend'] : null;
$comment = isset($values['comment']) ? $values['comment'] : null; $comment = isset($values['comment']) ? $values['comment'] : null;
$startDate = $values['date_range_start']; $startDate = $values['date_range_start'];
@ -128,8 +154,8 @@ if (api_is_allowed_to_edit(false, true) OR
$values['users_to_send'], $values['users_to_send'],
$sendEmail, $sendEmail,
null, null,
$attachment, $attachmentList,
$attachmentComment, $attachmentCommentList,
$comment $comment
); );
@ -175,9 +201,10 @@ if (api_is_allowed_to_edit(false, true) OR
$startDate = $values['date_range_start']; $startDate = $values['date_range_start'];
$endDate = $values['date_range_end']; $endDate = $values['date_range_end'];
$sendAttachment = isset($_FILES['user_upload']) ? true : false; $sendAttachment = isset($_FILES) && !empty($_FILES) ? true : false;
$attachment = $sendAttachment ? $_FILES['user_upload'] : null; $attachmentList = $sendAttachment ? $_FILES : null;
$attachmentComment = isset($values['file_comment']) ? $values['file_comment'] : null; $attachmentCommentList = isset($values['legend']) ? $values['legend'] : null;
$comment = isset($values['comment']) ? $values['comment'] : null; $comment = isset($values['comment']) ? $values['comment'] : null;
// This is a sub event. Delete the current and create another BT#7803 // 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'], $values['users_to_send'],
false, false,
null, null,
$attachment, $attachmentList,
$attachmentComment, $attachmentCommentList,
$comment $comment
); );
@ -214,8 +241,8 @@ if (api_is_allowed_to_edit(false, true) OR
$values['title'], $values['title'],
$values['content'], $values['content'],
$values['users_to_send'], $values['users_to_send'],
$attachment, $attachmentList,
$attachmentComment, $attachmentCommentList,
$comment, $comment,
'', '',
$sendEmail $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'])) { if (!empty($deleteAttachmentList)) {
$agenda->deleteAttachmentFile( foreach ($deleteAttachmentList as $deleteAttachmentId => $value) {
$event['attachment']['id'], $agenda->deleteAttachmentFile(
$agenda->course $deleteAttachmentId,
); $agenda->course
);
}
} }
$message = Display::return_message(get_lang('Updated'), 'confirmation'); $message = Display::return_message(get_lang('Updated'), 'confirmation');

Loading…
Cancel
Save