Skip to content

Commit 6990959

Browse files
committed
Add multiple attachments when adding an agenda event see BT#10952
1 parent 1706412 commit 6990959

File tree

2 files changed

+157
-63
lines changed

2 files changed

+157
-63
lines changed

main/calendar/agenda.lib.php

Lines changed: 108 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ public function getTypes()
122122
* @param array $usersToSend array('everyone') or a list of user/group ids
123123
* @param bool $addAsAnnouncement event as a *course* announcement
124124
* @param int $parentEventId
125-
* @param array $attachmentArray $_FILES['']
126-
* @param string $attachmentComment
125+
* @param array $attachmentArray array of $_FILES['']
126+
* @param array $attachmentCommentList
127127
* @param string $eventComment
128128
* @param string $color
129129
*
@@ -139,7 +139,7 @@ public function add_event(
139139
$addAsAnnouncement = false,
140140
$parentEventId = null,
141141
$attachmentArray = array(),
142-
$attachmentComment = null,
142+
$attachmentCommentList = array(),
143143
$eventComment = null,
144144
$color = ''
145145
) {
@@ -304,12 +304,16 @@ public function add_event(
304304

305305
// Add attachment.
306306
if (isset($attachmentArray) && !empty($attachmentArray)) {
307-
$this->addAttachment(
308-
$id,
309-
$attachmentArray,
310-
$attachmentComment,
311-
$this->course
312-
);
307+
$counter = 0;
308+
foreach ($attachmentArray as $attachmentItem) {
309+
$this->addAttachment(
310+
$id,
311+
$attachmentItem,
312+
$attachmentCommentList[$counter],
313+
$this->course
314+
);
315+
$counter++;
316+
}
313317
}
314318
}
315319
break;
@@ -562,7 +566,7 @@ public function store_agenda_item_as_announcement($item_id, $sentTo = array())
562566
* @param string $content
563567
* @param array $usersToSend
564568
* @param array $attachmentArray
565-
* @param string $attachmentComment
569+
* @param array $attachmentCommentList
566570
* @param string $comment
567571
* @param string $color
568572
* @param bool $addAnnouncement
@@ -578,7 +582,7 @@ public function edit_event(
578582
$content,
579583
$usersToSend = array(),
580584
$attachmentArray = array(),
581-
$attachmentComment = null,
585+
$attachmentCommentList = array(),
582586
$comment = null,
583587
$color = '',
584588
$addAnnouncement = false
@@ -801,12 +805,17 @@ public function edit_event(
801805

802806
// Add attachment.
803807
if (isset($attachmentArray) && !empty($attachmentArray)) {
804-
$this->updateAttachment(
805-
$id,
806-
$attachmentArray,
807-
$attachmentComment,
808-
$this->course
809-
);
808+
$counter = 0;
809+
foreach ($attachmentArray as $attachmentItem) {
810+
$this->updateAttachment(
811+
$attachmentItem['id'],
812+
$id,
813+
$attachmentItem,
814+
$attachmentCommentList[$counter],
815+
$this->course
816+
);
817+
$counter++;
818+
}
810819
}
811820
}
812821
break;
@@ -848,10 +857,11 @@ public function delete_event($id, $deleteAllItemsFromSerie = false)
848857
break;
849858
case 'course':
850859
$course_id = api_get_course_int_id();
860+
851861
if (!empty($course_id) && api_is_allowed_to_edit(null, true)) {
852862
// Delete
863+
$eventInfo = $this->get_event($id);
853864
if ($deleteAllItemsFromSerie) {
854-
$eventInfo = $this->get_event($id);
855865
/* This is one of the children.
856866
Getting siblings and delete 'Em all + the father! */
857867
if (isset($eventInfo['parent_event_id']) && !empty($eventInfo['parent_event_id'])) {
@@ -895,6 +905,12 @@ public function delete_event($id, $deleteAllItemsFromSerie = false)
895905
$this->table_repeat,
896906
array('cal_id = ? AND c_id = ?' => array($id, $course_id))
897907
);
908+
909+
if (isset($eventInfo['attachment']) && !empty($eventInfo['attachment'])) {
910+
foreach ($eventInfo['attachment'] as $attachment) {
911+
self::deleteAttachmentFile($attachment['id'], $this->course);
912+
}
913+
}
898914
}
899915
break;
900916
case 'admin':
@@ -1152,7 +1168,7 @@ public function get_event($id)
11521168
$event['parent_info'] = $this->get_event($event['parent_event_id']);
11531169
}
11541170

1155-
$event['attachment'] = $this->getAttachment($id, $this->course);
1171+
$event['attachment'] = $this->getAttachmentList($id, $this->course);
11561172
}
11571173
}
11581174
break;
@@ -1439,13 +1455,16 @@ public function get_course_events($start, $end, $courseInfo, $groupId = 0, $sess
14391455
}
14401456

14411457
$eventsAdded[] = $event['unique_id'];
1442-
$attachment = $this->getAttachment($row['id'], $courseInfo);
1458+
$attachmentList = $this->getAttachmentList($row['id'], $courseInfo);
1459+
1460+
if (!empty($attachmentList)) {
1461+
foreach ($attachmentList as $attachment) {
1462+
$has_attachment = Display::return_icon('attachment.gif', get_lang('Attachment'));
1463+
$user_filename = $attachment['filename'];
1464+
$url = api_get_path(WEB_CODE_PATH).'calendar/download.php?file='.$attachment['path'].'&course_id='.$course_id.'&'.api_get_cidreq();
1465+
$event['attachment'] .= $has_attachment.Display::url($user_filename, $url).'<br />';
1466+
}
14431467

1444-
if (!empty($attachment)) {
1445-
$has_attachment = Display::return_icon('attachment.gif', get_lang('Attachment'));
1446-
$user_filename = $attachment['filename'];
1447-
$url = api_get_path(WEB_CODE_PATH).'calendar/download.php?file='.$attachment['path'].'&course_id='.$course_id.'&'.api_get_cidreq();
1448-
$event['attachment'] = $has_attachment.Display::url($user_filename, $url);
14491468
} else {
14501469
$event['attachment'] = '';
14511470
}
@@ -1944,23 +1963,36 @@ public function getForm($params = array())
19441963
$form->addElement('textarea', 'comment', get_lang('Comment'));
19451964
}
19461965

1947-
$form->addElement('file', 'user_upload', get_lang('AddAnAttachment'));
1948-
if ($showAttachmentForm) {
1966+
//$form->addElement('file', 'user_upload', get_lang('AddAnAttachment'));
1967+
1968+
$form->addElement('advanced_settings', get_lang('FilesAttachment').'<span id="filepaths">
1969+
<div id="filepath_1">
1970+
<input type="file" name="attach_1"/><br />
1971+
'.get_lang('Description').'&nbsp;&nbsp;<input type="text" name="legend[]" /><br /><br />
1972+
</div>
1973+
</span>');
1974+
$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'))).')');
1975+
1976+
//if ($showAttachmentForm) {
1977+
19491978
if (isset($params['attachment']) && !empty($params['attachment'])) {
1950-
$attachment = $params['attachment'];
1951-
$params['file_comment'] = $attachment['comment'];
1952-
if (!empty($attachment['path'])) {
1953-
$form->addElement(
1954-
'checkbox',
1955-
'delete_attachment',
1956-
null,
1957-
get_lang('DeleteAttachment').' '.$attachment['filename']
1958-
);
1979+
$attachmentList = $params['attachment'];
1980+
foreach ($attachmentList as $attachment) {
1981+
$params['file_comment'] = $attachment['comment'];
1982+
if (!empty($attachment['path'])) {
1983+
$form->addElement(
1984+
'checkbox',
1985+
'delete_attachment['.$attachment['id'].']',
1986+
null,
1987+
get_lang('DeleteAttachment').': '.$attachment['filename']
1988+
);
1989+
}
19591990
}
1991+
19601992
}
1961-
}
1993+
// }
19621994

1963-
$form->addElement('textarea', 'file_comment', get_lang('FileComment'));
1995+
//$form->addElement('textarea', 'file_comment', get_lang('FileComment'));
19641996

19651997
if (!empty($id)) {
19661998
$form->addElement(
@@ -2079,18 +2111,50 @@ public static function getRepeatTypes()
20792111
* @param array $courseInfo
20802112
* @return array with the post info
20812113
*/
2082-
public function getAttachment($eventId, $courseInfo)
2114+
public function getAttachmentList($eventId, $courseInfo)
20832115
{
20842116
$tableAttachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
20852117
$courseId = intval($courseInfo['real_id']);
20862118
$eventId = intval($eventId);
2087-
$row = array();
2119+
20882120
$sql = "SELECT id, path, filename, comment
20892121
FROM $tableAttachment
20902122
WHERE
20912123
c_id = $courseId AND
20922124
agenda_id = $eventId";
20932125
$result = Database::query($sql);
2126+
$list = array();
2127+
if (Database::num_rows($result) != 0) {
2128+
$list = Database::store_result($result, 'ASSOC');
2129+
}
2130+
2131+
return $list;
2132+
}
2133+
2134+
2135+
/**
2136+
* Show a list with all the attachments according to the post's id
2137+
* @param int $attachmentId
2138+
* @param int $eventId
2139+
* @param array $courseInfo
2140+
* @return array with the post info
2141+
*/
2142+
public function getAttachment($attachmentId, $eventId, $courseInfo)
2143+
{
2144+
$tableAttachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
2145+
$courseId = intval($courseInfo['real_id']);
2146+
$eventId = intval($eventId);
2147+
$attachmentId = intval($attachmentId);
2148+
2149+
$row = array();
2150+
$sql = "SELECT id, path, filename, comment
2151+
FROM $tableAttachment
2152+
WHERE
2153+
c_id = $courseId AND
2154+
agenda_id = $eventId AND
2155+
id = $attachmentId
2156+
";
2157+
$result = Database::query($sql);
20942158
if (Database::num_rows($result) != 0) {
20952159
$row = Database::fetch_array($result, 'ASSOC');
20962160
}
@@ -2162,16 +2226,17 @@ public function addAttachment($eventId, $fileUserUpload, $comment, $courseInfo)
21622226
}
21632227

21642228
/**
2229+
* @param int $attachmentId
21652230
* @param int $eventId
21662231
* @param array $fileUserUpload
21672232
* @param string $comment
21682233
* @param array $courseInfo
21692234
*/
2170-
public function updateAttachment($eventId, $fileUserUpload, $comment, $courseInfo)
2235+
public function updateAttachment($attachmentId, $eventId, $fileUserUpload, $comment, $courseInfo)
21712236
{
2172-
$attachment = $this->getAttachment($eventId, $courseInfo);
2237+
$attachment = $this->getAttachment($attachmentId, $eventId, $courseInfo);
21732238
if (!empty($attachment)) {
2174-
$this->deleteAttachmentFile($attachment['id'], $courseInfo);
2239+
$this->deleteAttachmentFile($attachmentId, $courseInfo);
21752240
}
21762241
$this->addAttachment($eventId, $fileUserUpload, $comment, $courseInfo);
21772242
}

main/calendar/agenda.php

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use \ChamiloSession as Session;
99

1010
// name of the language file that needs to be included
11-
$language_file = array('agenda', 'group');
11+
$language_file = array('agenda', 'group', 'messages', 'userInfo', 'admin');
1212

1313
// use anonymous mode when accessing this course tool
1414
$use_anonymous = true;
@@ -61,6 +61,33 @@ function plus_repeated_event() {
6161
</script>
6262
";
6363

64+
65+
$htmlHeadXtra[] = '<script type="text/javascript">
66+
var counter_image = 1;
67+
function add_image_form() {
68+
// Multiple filepaths for image form
69+
var filepaths = document.getElementById("filepaths");
70+
if (document.getElementById("filepath_"+counter_image)) {
71+
counter_image = counter_image + 1;
72+
} else {
73+
counter_image = counter_image;
74+
}
75+
var elem1 = document.createElement("div");
76+
elem1.setAttribute("id","filepath_"+counter_image);
77+
filepaths.appendChild(elem1);
78+
id_elem1 = "filepath_"+counter_image;
79+
id_elem1 = "\'"+id_elem1+"\'";
80+
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 />";
81+
if (filepaths.childNodes.length == 6) {
82+
var link_attach = document.getElementById("link-more-attach");
83+
if (link_attach) {
84+
link_attach.innerHTML="";
85+
}
86+
}
87+
}
88+
</script>';
89+
90+
6491
// setting the name of the tool
6592
$nameTools = get_lang('Agenda');
6693

@@ -110,10 +137,9 @@ function plus_repeated_event() {
110137
$sendEmail = isset($values['add_announcement']) ? true : false;
111138
$allDay = isset($values['all_day']) ? 'true' : 'false';
112139

113-
$sendAttachment = isset($_FILES['user_upload']) ? true : false;
114-
$attachment = $sendAttachment ? $_FILES['user_upload'] : null;
115-
$attachmentComment = isset($values['file_comment']) ? $values['file_comment'] : null;
116-
140+
$sendAttachment = isset($_FILES) && !empty($_FILES) ? true : false;
141+
$attachmentList = $sendAttachment ? $_FILES : null;
142+
$attachmentCommentList = isset($values['legend']) ? $values['legend'] : null;
117143
$comment = isset($values['comment']) ? $values['comment'] : null;
118144

119145
$startDate = $values['date_range_start'];
@@ -128,8 +154,8 @@ function plus_repeated_event() {
128154
$values['users_to_send'],
129155
$sendEmail,
130156
null,
131-
$attachment,
132-
$attachmentComment,
157+
$attachmentList,
158+
$attachmentCommentList,
133159
$comment
134160
);
135161

@@ -175,9 +201,10 @@ function plus_repeated_event() {
175201
$startDate = $values['date_range_start'];
176202
$endDate = $values['date_range_end'];
177203

178-
$sendAttachment = isset($_FILES['user_upload']) ? true : false;
179-
$attachment = $sendAttachment ? $_FILES['user_upload'] : null;
180-
$attachmentComment = isset($values['file_comment']) ? $values['file_comment'] : null;
204+
$sendAttachment = isset($_FILES) && !empty($_FILES) ? true : false;
205+
$attachmentList = $sendAttachment ? $_FILES : null;
206+
$attachmentCommentList = isset($values['legend']) ? $values['legend'] : null;
207+
181208
$comment = isset($values['comment']) ? $values['comment'] : null;
182209

183210
// This is a sub event. Delete the current and create another BT#7803
@@ -194,8 +221,8 @@ function plus_repeated_event() {
194221
$values['users_to_send'],
195222
false,
196223
null,
197-
$attachment,
198-
$attachmentComment,
224+
$attachmentList,
225+
$attachmentCommentList,
199226
$comment
200227
);
201228

@@ -214,8 +241,8 @@ function plus_repeated_event() {
214241
$values['title'],
215242
$values['content'],
216243
$values['users_to_send'],
217-
$attachment,
218-
$attachmentComment,
244+
$attachmentList,
245+
$attachmentCommentList,
219246
$comment,
220247
'',
221248
$sendEmail
@@ -232,13 +259,15 @@ function plus_repeated_event() {
232259
);
233260
}
234261

235-
$deleteAttachment = isset($values['delete_attachment']) ? true : false;
262+
$deleteAttachmentList = isset($values['delete_attachment']) ? $values['delete_attachment'] : array();
236263

237-
if ($deleteAttachment && isset($event['attachment']) && !empty($event['attachment'])) {
238-
$agenda->deleteAttachmentFile(
239-
$event['attachment']['id'],
240-
$agenda->course
241-
);
264+
if (!empty($deleteAttachmentList)) {
265+
foreach ($deleteAttachmentList as $deleteAttachmentId => $value) {
266+
$agenda->deleteAttachmentFile(
267+
$deleteAttachmentId,
268+
$agenda->course
269+
);
270+
}
242271
}
243272

244273
$message = Display::return_message(get_lang('Updated'), 'confirmation');

0 commit comments

Comments
 (0)