1 | <?php |
||
2 | /* For licensing terms, see /license.txt */ |
||
3 | |||
4 | use Chamilo\CoreBundle\Entity\ExtraField as ExtraFieldEntity; |
||
5 | use Chamilo\CoreBundle\Entity\ExtraFieldValues; |
||
6 | use Chamilo\CourseBundle\Entity\CAnnouncement; |
||
7 | use Chamilo\CourseBundle\Entity\CItemProperty; |
||
8 | |||
9 | /** |
||
10 | * Include file with functions for the announcements module. |
||
11 | * |
||
12 | * @author jmontoya |
||
13 | * |
||
14 | * @package chamilo.announcements |
||
15 | * |
||
16 | * @todo use OOP |
||
17 | */ |
||
18 | class AnnouncementManager |
||
19 | { |
||
20 | /** |
||
21 | * Constructor. |
||
22 | */ |
||
23 | public function __construct() |
||
24 | { |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * @return array |
||
29 | */ |
||
30 | public static function getTags() |
||
31 | { |
||
32 | $tags = [ |
||
33 | '((user_name))', |
||
34 | '((user_email))', |
||
35 | '((user_firstname))', |
||
36 | '((user_lastname))', |
||
37 | '((user_official_code))', |
||
38 | '((course_title))', |
||
39 | '((course_link))', |
||
40 | ]; |
||
41 | |||
42 | $tags[] = '((teachers))'; |
||
43 | |||
44 | $extraField = new ExtraField('user'); |
||
45 | $extraFields = $extraField->get_all(['filter = ?' => 1]); |
||
46 | if (!empty($extraFields)) { |
||
47 | foreach ($extraFields as $extra) { |
||
48 | $tags[] = "((extra_".$extra['variable']."))"; |
||
49 | } |
||
50 | } |
||
51 | $sessionId = api_get_session_id(); |
||
52 | if (!empty($sessionId)) { |
||
53 | $tags[] = '((coaches))'; |
||
54 | $tags[] = '((general_coach))'; |
||
55 | $tags[] = '((general_coach_email))'; |
||
56 | } |
||
57 | |||
58 | return $tags; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param int $userId |
||
63 | * @param string $content |
||
64 | * @param string $courseCode |
||
65 | * @param int $sessionId |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | public static function parseContent( |
||
70 | $userId, |
||
71 | $content, |
||
72 | $courseCode, |
||
73 | $sessionId = 0 |
||
74 | ) { |
||
75 | $readerInfo = api_get_user_info($userId, false, false, true, true, false, true); |
||
76 | $courseInfo = api_get_course_info($courseCode); |
||
77 | $teacherList = ''; |
||
78 | if ($courseInfo) { |
||
79 | $teacherList = CourseManager::getTeacherListFromCourseCodeToString($courseInfo['code']); |
||
80 | } |
||
81 | $generalCoachName = ''; |
||
82 | $generalCoachEmail = ''; |
||
83 | $coaches = ''; |
||
84 | if (!empty($sessionId)) { |
||
85 | $sessionInfo = api_get_session_info($sessionId); |
||
86 | $coaches = CourseManager::get_coachs_from_course_to_string( |
||
87 | $sessionId, |
||
88 | $courseInfo['real_id'] |
||
89 | ); |
||
90 | |||
91 | $generalCoach = api_get_user_info($sessionInfo['id_coach']); |
||
92 | $generalCoachName = $generalCoach['complete_name']; |
||
93 | $generalCoachEmail = $generalCoach['email']; |
||
94 | } |
||
95 | |||
96 | $data = []; |
||
97 | $data['user_name'] = ''; |
||
98 | $data['user_firstname'] = ''; |
||
99 | $data['user_lastname'] = ''; |
||
100 | $data['user_official_code'] = ''; |
||
101 | $data['user_email'] = ''; |
||
102 | if (!empty($readerInfo)) { |
||
103 | $data['user_name'] = $readerInfo['username']; |
||
104 | $data['user_email'] = $readerInfo['email']; |
||
105 | $data['user_firstname'] = $readerInfo['firstname']; |
||
106 | $data['user_lastname'] = $readerInfo['lastname']; |
||
107 | $data['user_official_code'] = $readerInfo['official_code']; |
||
108 | } |
||
109 | |||
110 | $data['course_title'] = $courseInfo['name'] ?? ''; |
||
111 | $courseLink = api_get_course_url($courseCode, $sessionId); |
||
112 | $data['course_link'] = Display::url($courseLink, $courseLink); |
||
113 | $data['teachers'] = $teacherList; |
||
114 | |||
115 | if (!empty($readerInfo)) { |
||
116 | $extraField = new ExtraField('user'); |
||
117 | $extraFields = $extraField->get_all(['filter = ?' => 1]); |
||
118 | if (!empty($extraFields)) { |
||
119 | foreach ($extraFields as $extra) { |
||
120 | $data['extra_'.$extra['variable']] = ''; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | if (!empty($readerInfo['extra'])) { |
||
125 | foreach ($readerInfo['extra'] as $extra) { |
||
126 | if (isset($extra['value'])) { |
||
127 | /** @var \Chamilo\CoreBundle\Entity\ExtraFieldValues $value */ |
||
128 | $value = $extra['value']; |
||
129 | if ($value instanceof ExtraFieldValues) { |
||
130 | $field = $value->getField(); |
||
131 | if ($field instanceof ExtraFieldEntity) { |
||
132 | $data['extra_'.$field->getVariable()] = $value->getValue(); |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 | |||
140 | if (!empty($sessionId)) { |
||
141 | $data['coaches'] = $coaches; |
||
142 | $data['general_coach'] = $generalCoachName; |
||
143 | $data['general_coach_email'] = $generalCoachEmail; |
||
144 | } |
||
145 | |||
146 | $tags = self::getTags(); |
||
147 | foreach ($tags as $tag) { |
||
148 | $simpleTag = str_replace(['((', '))'], '', $tag); |
||
149 | $value = isset($data[$simpleTag]) ? $data[$simpleTag] : ''; |
||
150 | $content = str_replace($tag, $value, $content); |
||
151 | } |
||
152 | |||
153 | return $content; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Gets all announcements from a course. |
||
158 | * |
||
159 | * @param array $course_info |
||
160 | * @param int $session_id |
||
161 | * |
||
162 | * @return array html with the content and count of announcements or false otherwise |
||
163 | */ |
||
164 | public static function get_all_annoucement_by_course($course_info, $session_id = 0) |
||
165 | { |
||
166 | $session_id = (int) $session_id; |
||
167 | $courseId = $course_info['real_id']; |
||
168 | |||
169 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
170 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
171 | |||
172 | $sql = "SELECT DISTINCT |
||
173 | announcement.id, |
||
174 | announcement.title, |
||
175 | announcement.content |
||
176 | FROM $tbl_announcement announcement |
||
177 | INNER JOIN $tbl_item_property i |
||
178 | ON (announcement.id = i.ref AND announcement.c_id = i.c_id) |
||
179 | WHERE |
||
180 | i.tool='announcement' AND |
||
181 | announcement.session_id = '$session_id' AND |
||
182 | announcement.c_id = $courseId AND |
||
183 | i.c_id = $courseId |
||
184 | ORDER BY display_order DESC"; |
||
185 | $rs = Database::query($sql); |
||
186 | $num_rows = Database::num_rows($rs); |
||
187 | if ($num_rows > 0) { |
||
188 | $list = []; |
||
189 | while ($row = Database::fetch_array($rs)) { |
||
190 | $list[] = $row; |
||
191 | } |
||
192 | |||
193 | return $list; |
||
194 | } |
||
195 | |||
196 | return false; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * This functions switches the visibility a course resource |
||
201 | * using the visibility field in 'item_property'. |
||
202 | * |
||
203 | * @param array $courseInfo |
||
204 | * @param int $id ID of the element of the corresponding type |
||
205 | * |
||
206 | * @return bool False on failure, True on success |
||
207 | */ |
||
208 | public static function change_visibility_announcement($courseInfo, $id) |
||
209 | { |
||
210 | $session_id = api_get_session_id(); |
||
211 | $item_visibility = api_get_item_visibility( |
||
212 | $courseInfo, |
||
213 | TOOL_ANNOUNCEMENT, |
||
214 | $id, |
||
215 | $session_id |
||
216 | ); |
||
217 | if ($item_visibility == '1') { |
||
218 | api_item_property_update( |
||
219 | $courseInfo, |
||
220 | TOOL_ANNOUNCEMENT, |
||
221 | $id, |
||
222 | 'invisible', |
||
223 | api_get_user_id() |
||
224 | ); |
||
225 | } else { |
||
226 | api_item_property_update( |
||
227 | $courseInfo, |
||
228 | TOOL_ANNOUNCEMENT, |
||
229 | $id, |
||
230 | 'visible', |
||
231 | api_get_user_id() |
||
232 | ); |
||
233 | } |
||
234 | |||
235 | return true; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Deletes an announcement. |
||
240 | * |
||
241 | * @param array $courseInfo the course array |
||
242 | * @param int $id the announcement id |
||
243 | */ |
||
244 | public static function delete_announcement($courseInfo, $id) |
||
245 | { |
||
246 | api_item_property_update( |
||
247 | $courseInfo, |
||
248 | TOOL_ANNOUNCEMENT, |
||
249 | $id, |
||
250 | 'delete', |
||
251 | api_get_user_id() |
||
252 | ); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Deletes all announcements by course. |
||
257 | * |
||
258 | * @param array $courseInfo the course array |
||
259 | */ |
||
260 | public static function delete_all_announcements($courseInfo) |
||
261 | { |
||
262 | $announcements = self::get_all_annoucement_by_course( |
||
263 | $courseInfo, |
||
264 | api_get_session_id() |
||
265 | ); |
||
266 | if (!empty($announcements)) { |
||
267 | foreach ($announcements as $annon) { |
||
268 | api_item_property_update( |
||
269 | $courseInfo, |
||
270 | TOOL_ANNOUNCEMENT, |
||
271 | $annon['id'], |
||
272 | 'delete', |
||
273 | api_get_user_id() |
||
274 | ); |
||
275 | } |
||
276 | } |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * @param string $title |
||
281 | * @param int $courseId |
||
282 | * @param int $sessionId |
||
283 | * @param int $visibility 1 or 0 |
||
284 | * |
||
285 | * @return mixed |
||
286 | */ |
||
287 | public static function getAnnouncementsByTitle( |
||
288 | $title, |
||
289 | $courseId, |
||
290 | $sessionId = 0, |
||
291 | $visibility = 1 |
||
292 | ) { |
||
293 | $dql = "SELECT a |
||
294 | FROM ChamiloCourseBundle:CAnnouncement a |
||
295 | JOIN ChamiloCourseBundle:CItemProperty ip |
||
296 | WITH a.id = ip.ref AND a.cId = ip.course |
||
297 | WHERE |
||
298 | ip.tool = 'announcement' AND |
||
299 | a.cId = :course AND |
||
300 | a.sessionId = :session AND |
||
301 | a.title like :title AND |
||
302 | ip.visibility = :visibility |
||
303 | ORDER BY a.displayOrder DESC"; |
||
304 | |||
305 | $qb = Database::getManager()->createQuery($dql); |
||
306 | $result = $qb->execute( |
||
307 | [ |
||
308 | 'course' => $courseId, |
||
309 | 'session' => $sessionId, |
||
310 | 'visibility' => $visibility, |
||
311 | 'title' => "%$title%", |
||
312 | ] |
||
313 | ); |
||
314 | |||
315 | return $result; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @param int $announcementId |
||
320 | * @param int $courseId |
||
321 | * @param int $userId |
||
322 | * @param int $groupId |
||
323 | * |
||
324 | * @return array |
||
325 | */ |
||
326 | public static function getAnnouncementInfoById( |
||
327 | $announcementId, |
||
328 | $courseId, |
||
329 | $userId, |
||
330 | $groupId = 0 |
||
331 | ) { |
||
332 | $announcementId = (int) $announcementId; |
||
333 | $courseId = (int) $courseId; |
||
334 | $userId = (int) $userId; |
||
335 | $groupId = (int) $groupId; |
||
336 | |||
337 | if (api_is_allowed_to_edit(false, true) || |
||
338 | (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous()) |
||
339 | ) { |
||
340 | $dql = "SELECT a, ip |
||
341 | FROM ChamiloCourseBundle:CAnnouncement a |
||
342 | JOIN ChamiloCourseBundle:CItemProperty ip |
||
343 | WITH a.id = ip.ref AND a.cId = ip.course |
||
344 | WHERE |
||
345 | a.id = :announcement AND |
||
346 | ip.tool = 'announcement' AND |
||
347 | a.cId = :course |
||
348 | ORDER BY a.displayOrder DESC"; |
||
349 | } else { |
||
350 | $groupList[] = $groupId; |
||
351 | |||
352 | if (api_get_user_id() != 0) { |
||
353 | $extraGroupCondition = ''; |
||
354 | if (!empty($groupId)) { |
||
355 | $groupProperties = GroupManager::get_group_properties($groupId); |
||
356 | if ($groupProperties['announcements_state'] == GroupManager::TOOL_PRIVATE_BETWEEN_USERS) { |
||
357 | $extraGroupCondition = " AND ( |
||
358 | ip.toUser = $userId AND ip.group = $groupId OR |
||
359 | (ip.group IN ('0') OR ip.group IS NULL) OR |
||
360 | (ip.group = $groupId AND (ip.toUser IS NULL OR ip.toUser = 0)) |
||
361 | )"; |
||
362 | } |
||
363 | } |
||
364 | |||
365 | $dql = "SELECT a, ip |
||
366 | FROM ChamiloCourseBundle:CAnnouncement a |
||
367 | JOIN ChamiloCourseBundle:CItemProperty ip |
||
368 | WITH a.id = ip.ref AND a.cId = ip.course |
||
369 | WHERE |
||
370 | a.id = :announcement AND |
||
371 | ip.tool='announcement' AND |
||
372 | ( |
||
373 | ip.toUser = $userId OR |
||
374 | ip.group IN ('0', '".$groupId."') OR |
||
375 | ip.group IS NULL |
||
376 | ) AND |
||
377 | ip.visibility = '1' AND |
||
378 | ip.course = :course |
||
379 | $extraGroupCondition |
||
380 | ORDER BY a.displayOrder DESC"; |
||
381 | } else { |
||
382 | $dql = "SELECT a, ip |
||
383 | FROM ChamiloCourseBundle:CAnnouncement a |
||
384 | JOIN ChamiloCourseBundle:CItemProperty ip |
||
385 | WITH a.id = ip.ref AND a.cId = ip.course |
||
386 | WHERE |
||
387 | a.id = :announcement AND |
||
388 | ip.tool = 'announcement' AND |
||
389 | (ip.group = '0' OR ip.group IS NULL) AND |
||
390 | ip.visibility = '1' AND |
||
391 | ip.course = :course"; |
||
392 | } |
||
393 | } |
||
394 | |||
395 | $qb = Database::getManager()->createQuery($dql); |
||
396 | $result = $qb->execute( |
||
397 | [ |
||
398 | 'announcement' => $announcementId, |
||
399 | 'course' => $courseId, |
||
400 | ] |
||
401 | ); |
||
402 | |||
403 | if (!empty($result)) { |
||
404 | return [ |
||
405 | 'announcement' => $result[0], |
||
406 | 'item_property' => $result[1], |
||
407 | ]; |
||
408 | } |
||
409 | |||
410 | return []; |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Displays one specific announcement. |
||
415 | * |
||
416 | * @param int $id the id of the announcement you want to display |
||
417 | * |
||
418 | * @return string |
||
419 | */ |
||
420 | public static function displayAnnouncement($id) |
||
421 | { |
||
422 | $id = (int) $id; |
||
423 | |||
424 | if (empty($id)) { |
||
425 | return ''; |
||
426 | } |
||
427 | |||
428 | global $charset; |
||
429 | |||
430 | $html = ''; |
||
431 | $result = self::getAnnouncementInfoById( |
||
432 | $id, |
||
433 | api_get_course_int_id(), |
||
434 | api_get_user_id(), |
||
435 | api_get_group_id() |
||
436 | ); |
||
437 | |||
438 | if (empty($result)) { |
||
439 | return ''; |
||
440 | } |
||
441 | |||
442 | /** @var CAnnouncement $announcement */ |
||
443 | $announcement = $result['announcement']; |
||
444 | /** @var CItemProperty $itemProperty */ |
||
445 | $itemProperty = $result['item_property']; |
||
446 | |||
447 | if (empty($announcement) || empty($itemProperty)) { |
||
448 | return ''; |
||
449 | } |
||
450 | |||
451 | $title = Security::remove_XSS($announcement->getTitle()); |
||
452 | $content = $announcement->getContent(); |
||
453 | |||
454 | $html .= "<table height=\"100\" width=\"100%\" cellpadding=\"5\" cellspacing=\"0\" class=\"table table-hover table-striped data_table\">"; |
||
455 | $html .= "<tr><td><h2>".$title."</h2></td></tr>"; |
||
456 | |||
457 | if (api_is_allowed_to_edit(false, true) || |
||
458 | (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous()) |
||
459 | ) { |
||
460 | $modify_icons = "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=modify&id=".$id."\">". |
||
461 | Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL)."</a>"; |
||
462 | |||
463 | $image_visibility = 'invisible'; |
||
464 | $alt_visibility = get_lang('Visible'); |
||
465 | if ($itemProperty->getVisibility() === 1) { |
||
466 | $image_visibility = 'visible'; |
||
467 | $alt_visibility = get_lang('Hide'); |
||
468 | } |
||
469 | global $stok; |
||
470 | $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=showhide&id=".$id."&sec_token=".$stok."\">". |
||
471 | Display::return_icon($image_visibility.'.png', $alt_visibility, '', ICON_SIZE_SMALL)."</a>"; |
||
472 | |||
473 | if (api_is_allowed_to_edit(false, true)) { |
||
474 | $modify_icons .= "<a |
||
475 | href=\"".api_get_self()."?".api_get_cidreq()."&action=delete&id=".$id."&sec_token=".$stok."\" |
||
476 | onclick=\"javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset))."')) return false;\">". |
||
477 | Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL). |
||
478 | "</a>"; |
||
479 | } |
||
480 | $html .= "<tr><th style='text-align:right'>$modify_icons</th></tr>"; |
||
481 | } |
||
482 | |||
483 | // The user id is always the current one. |
||
484 | $toUserId = api_get_user_id(); |
||
485 | $content = Security::remove_XSS(self::parseContent( |
||
486 | $toUserId, |
||
487 | $content, |
||
488 | api_get_course_id(), |
||
489 | api_get_session_id() |
||
490 | )); |
||
491 | |||
492 | $html .= "<tr><td>$content</td></tr>"; |
||
493 | $html .= "<tr>"; |
||
494 | $html .= "<td class=\"announcements_datum\">".get_lang('LastUpdateDate')." : "; |
||
495 | $lastEdit = $itemProperty->getLasteditDate(); |
||
496 | $html .= Display::dateToStringAgoAndLongDate($lastEdit); |
||
497 | $html .= "</td></tr>"; |
||
498 | |||
499 | $allow = !api_get_configuration_value('hide_announcement_sent_to_users_info'); |
||
500 | if ($allow && api_is_allowed_to_edit(false, true)) { |
||
501 | $sent_to = self::sent_to('announcement', $id); |
||
502 | $sentToForm = self::sent_to_form($sent_to); |
||
503 | $html .= Display::tag( |
||
504 | 'td', |
||
505 | get_lang('SentTo').': '.$sentToForm, |
||
506 | ['class' => 'announcements_datum'] |
||
507 | ); |
||
508 | } |
||
509 | $attachment_list = self::get_attachment($id); |
||
510 | |||
511 | if (count($attachment_list) > 0) { |
||
512 | $html .= "<tr><td>"; |
||
513 | $realname = $attachment_list['path']; |
||
514 | $user_filename = $attachment_list['filename']; |
||
515 | $full_file_name = 'download.php?'.api_get_cidreq().'&file='.$realname; |
||
516 | $html .= '<br/>'; |
||
517 | $html .= Display::return_icon('attachment.gif', get_lang('Attachment')); |
||
518 | $html .= '<a href="'.$full_file_name.' "> '.$user_filename.' </a>'; |
||
519 | $html .= ' - <span class="forum_attach_comment" >'.Security::remove_XSS($attachment_list['comment']).'</span>'; |
||
520 | if (api_is_allowed_to_edit(false, true)) { |
||
521 | $url = api_get_self()."?".api_get_cidreq(). |
||
522 | "&action=delete_attachment&id_attach=".$attachment_list['id']."&sec_token=".$stok; |
||
523 | $html .= Display::url( |
||
524 | Display::return_icon( |
||
525 | 'delete.png', |
||
526 | get_lang('Delete'), |
||
527 | '', |
||
528 | 16 |
||
529 | ), |
||
530 | $url |
||
531 | ); |
||
532 | } |
||
533 | $html .= '</td></tr>'; |
||
534 | } |
||
535 | $html .= '</table>'; |
||
536 | |||
537 | return $html; |
||
538 | } |
||
539 | |||
540 | /** |
||
541 | * @param array $courseInfo |
||
542 | * |
||
543 | * @return int |
||
544 | */ |
||
545 | public static function getLastAnnouncementOrder($courseInfo) |
||
546 | { |
||
547 | if (empty($courseInfo)) { |
||
548 | return 0; |
||
549 | } |
||
550 | |||
551 | if (!isset($courseInfo['real_id'])) { |
||
552 | return false; |
||
553 | } |
||
554 | |||
555 | $courseId = $courseInfo['real_id']; |
||
556 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
557 | $sql = "SELECT MAX(display_order) |
||
558 | FROM $table |
||
559 | WHERE c_id = $courseId "; |
||
560 | $result = Database::query($sql); |
||
561 | |||
562 | $order = 0; |
||
563 | if (Database::num_rows($result)) { |
||
564 | $row = Database::fetch_array($result); |
||
565 | $order = (int) $row[0] + 1; |
||
566 | } |
||
567 | |||
568 | return $order; |
||
569 | } |
||
570 | |||
571 | /** |
||
572 | * Store an announcement in the database (including its attached file if any). |
||
573 | * |
||
574 | * @param array $courseInfo |
||
575 | * @param int $sessionId |
||
576 | * @param string $title Announcement title (pure text) |
||
577 | * @param string $newContent Content of the announcement (can be HTML) |
||
578 | * @param array $sentTo Array of users and groups to send the announcement to |
||
579 | * @param array $file uploaded file $_FILES |
||
580 | * @param string $file_comment Comment describing the attachment |
||
581 | * @param string $end_date |
||
582 | * @param bool $sendToUsersInSession |
||
583 | * @param int $authorId |
||
584 | * |
||
585 | * @return int false on failure, ID of the announcement on success |
||
586 | */ |
||
587 | public static function add_announcement( |
||
588 | $courseInfo, |
||
589 | $sessionId, |
||
590 | $title, |
||
591 | $newContent, |
||
592 | $sentTo, |
||
593 | $file = [], |
||
594 | $file_comment = null, |
||
595 | $end_date = null, |
||
596 | $sendToUsersInSession = false, |
||
597 | $authorId = 0 |
||
598 | ) { |
||
599 | if (empty($courseInfo)) { |
||
600 | return false; |
||
601 | } |
||
602 | |||
603 | if (!isset($courseInfo['real_id'])) { |
||
604 | return false; |
||
605 | } |
||
606 | |||
607 | $courseId = $courseInfo['real_id']; |
||
608 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
609 | $authorId = empty($authorId) ? api_get_user_id() : $authorId; |
||
610 | |||
611 | if (empty($end_date)) { |
||
612 | $end_date = api_get_utc_datetime(); |
||
613 | } |
||
614 | |||
615 | $order = self::getLastAnnouncementOrder($courseInfo); |
||
616 | |||
617 | // store in the table announcement |
||
618 | $params = [ |
||
619 | 'c_id' => $courseId, |
||
620 | 'content' => $newContent, |
||
621 | 'title' => $title, |
||
622 | 'end_date' => $end_date, |
||
623 | 'display_order' => $order, |
||
624 | 'session_id' => (int) $sessionId, |
||
625 | ]; |
||
626 | |||
627 | $last_id = Database::insert($tbl_announcement, $params); |
||
628 | |||
629 | if (empty($last_id)) { |
||
630 | return false; |
||
631 | } else { |
||
632 | $sql = "UPDATE $tbl_announcement SET id = iid WHERE iid = $last_id"; |
||
633 | Database::query($sql); |
||
634 | |||
635 | if (!empty($file)) { |
||
636 | self::add_announcement_attachment_file( |
||
637 | $last_id, |
||
638 | $file_comment, |
||
639 | $_FILES['user_upload'] |
||
640 | ); |
||
641 | } |
||
642 | |||
643 | // store in item_property (first the groups, then the users |
||
644 | if (empty($sentTo) || |
||
645 | (!empty($sentTo) && isset($sentTo[0]) && $sentTo[0] == 'everyone') |
||
646 | ) { |
||
647 | // The message is sent to EVERYONE, so we set the group to 0 |
||
648 | api_item_property_update( |
||
649 | $courseInfo, |
||
650 | TOOL_ANNOUNCEMENT, |
||
651 | $last_id, |
||
652 | 'AnnouncementAdded', |
||
653 | $authorId, |
||
654 | '0', |
||
655 | null, |
||
656 | null, |
||
657 | null, |
||
658 | $sessionId |
||
659 | ); |
||
660 | } else { |
||
661 | $send_to = CourseManager::separateUsersGroups($sentTo); |
||
662 | $batchSize = 20; |
||
663 | $em = Database::getManager(); |
||
664 | // Storing the selected groups |
||
665 | if (is_array($send_to['groups']) && |
||
666 | !empty($send_to['groups']) |
||
667 | ) { |
||
668 | $counter = 1; |
||
669 | foreach ($send_to['groups'] as $group) { |
||
670 | $groupInfo = GroupManager::get_group_properties($group); |
||
671 | api_item_property_update( |
||
672 | $courseInfo, |
||
673 | TOOL_ANNOUNCEMENT, |
||
674 | $last_id, |
||
675 | 'AnnouncementAdded', |
||
676 | $authorId, |
||
677 | $groupInfo |
||
678 | ); |
||
679 | |||
680 | if (($counter % $batchSize) === 0) { |
||
681 | $em->flush(); |
||
682 | $em->clear(); |
||
683 | } |
||
684 | $counter++; |
||
685 | } |
||
686 | } |
||
687 | |||
688 | // Storing the selected users |
||
689 | if (is_array($send_to['users'])) { |
||
690 | $counter = 1; |
||
691 | foreach ($send_to['users'] as $user) { |
||
692 | api_item_property_update( |
||
693 | $courseInfo, |
||
694 | TOOL_ANNOUNCEMENT, |
||
695 | $last_id, |
||
696 | 'AnnouncementAdded', |
||
697 | $authorId, |
||
698 | '', |
||
699 | $user |
||
700 | ); |
||
701 | |||
702 | if (($counter % $batchSize) === 0) { |
||
703 | $em->flush(); |
||
704 | $em->clear(); |
||
705 | } |
||
706 | $counter++; |
||
707 | } |
||
708 | } |
||
709 | } |
||
710 | |||
711 | if ($sendToUsersInSession) { |
||
712 | self::addAnnouncementToAllUsersInSessions($last_id); |
||
713 | } |
||
714 | |||
715 | return $last_id; |
||
716 | } |
||
717 | } |
||
718 | |||
719 | /** |
||
720 | * @param string $title |
||
721 | * @param string $newContent |
||
722 | * @param int $groupId |
||
723 | * @param array $to_users |
||
724 | * @param array $file |
||
725 | * @param string $file_comment |
||
726 | * @param bool $sendToUsersInSession |
||
727 | * |
||
728 | * @return bool|int |
||
729 | */ |
||
730 | public static function addGroupAnnouncement( |
||
731 | $title, |
||
732 | $newContent, |
||
733 | $groupId, |
||
734 | $to_users, |
||
735 | $file = [], |
||
736 | $file_comment = '', |
||
737 | $sendToUsersInSession = false |
||
738 | ) { |
||
739 | $courseInfo = api_get_course_info(); |
||
740 | |||
741 | // Database definitions |
||
742 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
743 | $order = self::getLastAnnouncementOrder($courseInfo); |
||
744 | |||
745 | $now = api_get_utc_datetime(); |
||
746 | $courseId = api_get_course_int_id(); |
||
747 | |||
748 | // store in the table announcement |
||
749 | $params = [ |
||
750 | 'c_id' => $courseId, |
||
751 | 'content' => $newContent, |
||
752 | 'title' => $title, |
||
753 | 'end_date' => $now, |
||
754 | 'display_order' => $order, |
||
755 | 'session_id' => api_get_session_id(), |
||
756 | ]; |
||
757 | |||
758 | $last_id = Database::insert($table, $params); |
||
759 | |||
760 | // Store the attach file |
||
761 | if ($last_id) { |
||
762 | $sql = "UPDATE $table SET id = iid |
||
763 | WHERE iid = $last_id"; |
||
764 | Database::query($sql); |
||
765 | |||
766 | if (!empty($file)) { |
||
767 | self::add_announcement_attachment_file( |
||
768 | $last_id, |
||
769 | $file_comment, |
||
770 | $file |
||
771 | ); |
||
772 | } |
||
773 | |||
774 | $send_to_users = CourseManager::separateUsersGroups($to_users); |
||
775 | |||
776 | // if nothing was selected in the menu then send to all the group |
||
777 | $sentToAllGroup = false; |
||
778 | if (empty($send_to_users['groups']) && empty($send_to_users['users'])) { |
||
779 | $groupInfo = GroupManager::get_group_properties($groupId); |
||
780 | api_item_property_update( |
||
781 | $courseInfo, |
||
782 | TOOL_ANNOUNCEMENT, |
||
783 | $last_id, |
||
784 | 'AnnouncementAdded', |
||
785 | api_get_user_id(), |
||
786 | $groupInfo |
||
787 | ); |
||
788 | $sentToAllGroup = true; |
||
789 | } |
||
790 | |||
791 | if ($sentToAllGroup === false) { |
||
792 | if (!empty($send_to_users['groups'])) { |
||
793 | foreach ($send_to_users['groups'] as $group) { |
||
794 | $groupInfo = GroupManager::get_group_properties($group); |
||
795 | api_item_property_update( |
||
796 | $courseInfo, |
||
797 | TOOL_ANNOUNCEMENT, |
||
798 | $last_id, |
||
799 | 'AnnouncementAdded', |
||
800 | api_get_user_id(), |
||
801 | $groupInfo |
||
802 | ); |
||
803 | } |
||
804 | } |
||
805 | |||
806 | $groupInfo = GroupManager::get_group_properties($groupId); |
||
807 | if (!empty($send_to_users['users'])) { |
||
808 | foreach ($send_to_users['users'] as $user) { |
||
809 | api_item_property_update( |
||
810 | $courseInfo, |
||
811 | TOOL_ANNOUNCEMENT, |
||
812 | $last_id, |
||
813 | 'AnnouncementAdded', |
||
814 | api_get_user_id(), |
||
815 | $groupInfo, |
||
816 | $user |
||
817 | ); |
||
818 | } |
||
819 | } |
||
820 | } |
||
821 | |||
822 | if ($sendToUsersInSession) { |
||
823 | self::addAnnouncementToAllUsersInSessions($last_id); |
||
824 | } |
||
825 | } |
||
826 | |||
827 | return $last_id; |
||
828 | } |
||
829 | |||
830 | /** |
||
831 | * This function stores the announcement item in the announcement table |
||
832 | * and updates the item_property table. |
||
833 | * |
||
834 | * @param int $id id of the announcement |
||
835 | * @param string $title |
||
836 | * @param string $newContent |
||
837 | * @param array $to users that will receive the announcement |
||
838 | * @param mixed $file attachment |
||
839 | * @param string $file_comment file comment |
||
840 | * @param bool $sendToUsersInSession |
||
841 | */ |
||
842 | public static function edit_announcement( |
||
843 | $id, |
||
844 | $title, |
||
845 | $newContent, |
||
846 | $to, |
||
847 | $file = [], |
||
848 | $file_comment = '', |
||
849 | $sendToUsersInSession = false |
||
850 | ) { |
||
851 | $courseInfo = api_get_course_info(); |
||
852 | $courseId = api_get_course_int_id(); |
||
853 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
854 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
855 | $id = (int) $id; |
||
856 | |||
857 | $params = [ |
||
858 | 'title' => $title, |
||
859 | 'content' => $newContent, |
||
860 | ]; |
||
861 | |||
862 | Database::update( |
||
863 | $table, |
||
864 | $params, |
||
865 | ['c_id = ? AND id = ?' => [$courseId, $id]] |
||
866 | ); |
||
867 | |||
868 | // save attachment file |
||
869 | $row_attach = self::get_attachment($id); |
||
870 | |||
871 | $id_attach = 0; |
||
872 | if ($row_attach) { |
||
873 | $id_attach = (int) $row_attach['id']; |
||
874 | } |
||
875 | |||
876 | if (!empty($file)) { |
||
877 | if (empty($id_attach)) { |
||
878 | self::add_announcement_attachment_file( |
||
879 | $id, |
||
880 | $file_comment, |
||
881 | $file |
||
882 | ); |
||
883 | } else { |
||
884 | self::edit_announcement_attachment_file( |
||
885 | $id_attach, |
||
886 | $file, |
||
887 | $file_comment |
||
888 | ); |
||
889 | } |
||
890 | } |
||
891 | |||
892 | // We remove everything from item_property for this |
||
893 | $sql = "DELETE FROM $tbl_item_property |
||
894 | WHERE c_id = $courseId AND ref='$id' AND tool='announcement'"; |
||
895 | Database::query($sql); |
||
896 | |||
897 | if ($sendToUsersInSession) { |
||
898 | self::addAnnouncementToAllUsersInSessions($id); |
||
899 | } |
||
900 | |||
901 | // store in item_property (first the groups, then the users |
||
902 | if (!empty($to)) { |
||
903 | // !is_null($to): when no user is selected we send it to everyone |
||
904 | $send_to = CourseManager::separateUsersGroups($to); |
||
905 | |||
906 | // storing the selected groups |
||
907 | if (is_array($send_to['groups'])) { |
||
908 | foreach ($send_to['groups'] as $group) { |
||
909 | $groupInfo = GroupManager::get_group_properties($group); |
||
910 | if (empty($groupInfo)) { |
||
911 | // Probably the group id and iid are different try checking the iid |
||
912 | $groupInfo = GroupManager::get_group_properties($group, true); |
||
913 | } |
||
914 | if ($groupInfo) { |
||
915 | api_item_property_update( |
||
916 | $courseInfo, |
||
917 | TOOL_ANNOUNCEMENT, |
||
918 | $id, |
||
919 | 'AnnouncementUpdated', |
||
920 | api_get_user_id(), |
||
921 | $groupInfo |
||
922 | ); |
||
923 | } |
||
924 | } |
||
925 | } |
||
926 | |||
927 | // storing the selected users |
||
928 | if (is_array($send_to['users'])) { |
||
929 | foreach ($send_to['users'] as $user) { |
||
930 | api_item_property_update( |
||
931 | $courseInfo, |
||
932 | TOOL_ANNOUNCEMENT, |
||
933 | $id, |
||
934 | 'AnnouncementUpdated', |
||
935 | api_get_user_id(), |
||
936 | 0, |
||
937 | $user |
||
938 | ); |
||
939 | } |
||
940 | } |
||
941 | |||
942 | // Send to everyone |
||
943 | if (isset($to[0]) && $to[0] === 'everyone') { |
||
944 | api_item_property_update( |
||
945 | $courseInfo, |
||
946 | TOOL_ANNOUNCEMENT, |
||
947 | $id, |
||
948 | 'AnnouncementUpdated', |
||
949 | api_get_user_id(), |
||
950 | 0 |
||
951 | ); |
||
952 | } |
||
953 | } else { |
||
954 | // the message is sent to everyone, so we set the group to 0 |
||
955 | api_item_property_update( |
||
956 | $courseInfo, |
||
957 | TOOL_ANNOUNCEMENT, |
||
958 | $id, |
||
959 | 'AnnouncementUpdated', |
||
960 | api_get_user_id(), |
||
961 | 0 |
||
962 | ); |
||
963 | } |
||
964 | } |
||
965 | |||
966 | /** |
||
967 | * @param int $announcementId |
||
968 | */ |
||
969 | public static function addAnnouncementToAllUsersInSessions($announcementId) |
||
970 | { |
||
971 | $courseCode = api_get_course_id(); |
||
972 | $courseInfo = api_get_course_info(); |
||
973 | $sessionList = SessionManager::get_session_by_course(api_get_course_int_id()); |
||
974 | |||
975 | if (!empty($sessionList)) { |
||
976 | foreach ($sessionList as $sessionInfo) { |
||
977 | $sessionId = $sessionInfo['id']; |
||
978 | $userList = CourseManager::get_user_list_from_course_code( |
||
979 | $courseCode, |
||
980 | $sessionId |
||
981 | ); |
||
982 | |||
983 | if (!empty($userList)) { |
||
984 | foreach ($userList as $user) { |
||
985 | api_item_property_update( |
||
986 | $courseInfo, |
||
987 | TOOL_ANNOUNCEMENT, |
||
988 | $announcementId, |
||
989 | 'AnnouncementUpdated', |
||
990 | api_get_user_id(), |
||
991 | 0, |
||
992 | $user['user_id'], |
||
993 | 0, |
||
994 | 0, |
||
995 | $sessionId |
||
996 | ); |
||
997 | } |
||
998 | } |
||
999 | } |
||
1000 | } |
||
1001 | } |
||
1002 | |||
1003 | /** |
||
1004 | * @param int $insert_id |
||
1005 | * |
||
1006 | * @return bool |
||
1007 | */ |
||
1008 | public static function update_mail_sent($insert_id) |
||
1009 | { |
||
1010 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
1011 | if ($insert_id != strval(intval($insert_id))) { |
||
1012 | return false; |
||
1013 | } |
||
1014 | $insert_id = intval($insert_id); |
||
1015 | $courseId = api_get_course_int_id(); |
||
1016 | // store the modifications in the table tbl_annoucement |
||
1017 | $sql = "UPDATE $table SET email_sent='1' |
||
1018 | WHERE c_id = $courseId AND id = $insert_id"; |
||
1019 | Database::query($sql); |
||
1020 | } |
||
1021 | |||
1022 | /** |
||
1023 | * @param int $user_id |
||
1024 | * |
||
1025 | * @return array|bool |
||
1026 | */ |
||
1027 | public static function getAnnoucementCourseTotalByUser($user_id) |
||
1028 | { |
||
1029 | $user_id = (int) $user_id; |
||
1030 | |||
1031 | if (empty($user_id)) { |
||
1032 | return false; |
||
1033 | } |
||
1034 | |||
1035 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
1036 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
1037 | |||
1038 | $sql = "SELECT DISTINCT |
||
1039 | announcement.c_id, |
||
1040 | count(announcement.id) count |
||
1041 | FROM $tbl_announcement announcement |
||
1042 | INNER JOIN $tbl_item_property ip |
||
1043 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
1044 | WHERE |
||
1045 | ip.tool='announcement' AND |
||
1046 | ( |
||
1047 | ip.to_user_id = '$user_id' AND |
||
1048 | (ip.to_group_id='0' OR ip.to_group_id IS NULL) |
||
1049 | ) |
||
1050 | AND ip.visibility='1' |
||
1051 | AND announcement.session_id = 0 |
||
1052 | GROUP BY announcement.c_id"; |
||
1053 | $rs = Database::query($sql); |
||
1054 | $num_rows = Database::num_rows($rs); |
||
1055 | $result = []; |
||
1056 | if ($num_rows > 0) { |
||
1057 | while ($row = Database::fetch_array($rs, 'ASSOC')) { |
||
1058 | if (empty($row['c_id'])) { |
||
1059 | continue; |
||
1060 | } |
||
1061 | $result[] = ['course' => api_get_course_info_by_id($row['c_id']), 'count' => $row['count']]; |
||
1062 | } |
||
1063 | } |
||
1064 | |||
1065 | return $result; |
||
1066 | } |
||
1067 | |||
1068 | /** |
||
1069 | * Returns announcement info from its id. |
||
1070 | * |
||
1071 | * @param int $courseId |
||
1072 | * @param int $id |
||
1073 | * |
||
1074 | * @return array |
||
1075 | */ |
||
1076 | public static function get_by_id($courseId, $id) |
||
1077 | { |
||
1078 | $id = (int) $id; |
||
1079 | $courseId = $courseId ? (int) $courseId : api_get_course_int_id(); |
||
1080 | |||
1081 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
1082 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
1083 | |||
1084 | $sql = "SELECT DISTINCT |
||
1085 | announcement.id, |
||
1086 | announcement.title, |
||
1087 | announcement.content, |
||
1088 | ip.to_group_id |
||
1089 | FROM $tbl_announcement announcement |
||
1090 | INNER JOIN $tbl_item_property ip |
||
1091 | ON |
||
1092 | announcement.id = ip.ref AND |
||
1093 | announcement.c_id = ip.c_id |
||
1094 | WHERE |
||
1095 | announcement.c_id = $courseId AND |
||
1096 | ip.tool='announcement' AND |
||
1097 | announcement.id = $id |
||
1098 | "; |
||
1099 | |||
1100 | $result = Database::query($sql); |
||
1101 | if (Database::num_rows($result)) { |
||
1102 | return Database::fetch_array($result); |
||
1103 | } |
||
1104 | |||
1105 | return []; |
||
1106 | } |
||
1107 | |||
1108 | /** |
||
1109 | * this function gets all the groups of the course, |
||
1110 | * not including linked courses. |
||
1111 | */ |
||
1112 | public static function get_course_groups() |
||
1113 | { |
||
1114 | $session_id = api_get_session_id(); |
||
1115 | if ($session_id != 0) { |
||
1116 | $new_group_list = CourseManager::get_group_list_of_course( |
||
1117 | api_get_course_id(), |
||
1118 | $session_id, |
||
1119 | 1 |
||
1120 | ); |
||
1121 | } else { |
||
1122 | $new_group_list = CourseManager::get_group_list_of_course( |
||
1123 | api_get_course_id(), |
||
1124 | 0, |
||
1125 | 1 |
||
1126 | ); |
||
1127 | } |
||
1128 | |||
1129 | return $new_group_list; |
||
1130 | } |
||
1131 | |||
1132 | /** |
||
1133 | * This tools loads all the users and all the groups who have received |
||
1134 | * a specific item (in this case an announcement item). |
||
1135 | * |
||
1136 | * @param string $tool |
||
1137 | * @param int $id |
||
1138 | * @param bool $includeGroupWhenLoadingUser |
||
1139 | * |
||
1140 | * @return array |
||
1141 | */ |
||
1142 | public static function loadEditUsers($tool, $id, $includeGroupWhenLoadingUser = false) |
||
1143 | { |
||
1144 | $table = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
1145 | $tool = Database::escape_string($tool); |
||
1146 | $id = (int) $id; |
||
1147 | $courseId = api_get_course_int_id(); |
||
1148 | $groupId = api_get_group_id(); |
||
1149 | |||
1150 | $sql = "SELECT to_user_id, to_group_id FROM $table |
||
1151 | WHERE c_id = $courseId AND tool='$tool' AND ref = $id"; |
||
1152 | |||
1153 | $result = Database::query($sql); |
||
1154 | $to = []; |
||
1155 | while ($row = Database::fetch_array($result)) { |
||
1156 | // This is the iid of c_group_info |
||
1157 | $toGroup = $row['to_group_id']; |
||
1158 | if (empty($row['to_user_id']) && !empty($groupId) && $groupId != $toGroup) { |
||
1159 | //continue; |
||
1160 | } |
||
1161 | switch ($toGroup) { |
||
1162 | // it was send to one specific user |
||
1163 | case null: |
||
1164 | if (isset($row['to_user_id']) && !empty($row['to_user_id'])) { |
||
1165 | if (!in_array('USER:'.$row['to_user_id'], $to)) { |
||
1166 | $to[] = 'USER:'.$row['to_user_id']; |
||
1167 | } |
||
1168 | } |
||
1169 | break; |
||
1170 | // it was sent to everyone |
||
1171 | case 0: |
||
1172 | return 'everyone'; |
||
1173 | break; |
||
1174 | default: |
||
1175 | if (isset($row['to_user_id']) && !empty($row['to_user_id'])) { |
||
1176 | if (!in_array('USER:'.$row['to_user_id'], $to)) { |
||
1177 | $to[] = 'USER:'.$row['to_user_id']; |
||
1178 | } |
||
1179 | } else { |
||
1180 | if (!in_array('GROUP:'.$toGroup, $to)) { |
||
1181 | $to[] = 'GROUP:'.$toGroup; |
||
1182 | } |
||
1183 | } |
||
1184 | |||
1185 | if ($includeGroupWhenLoadingUser) { |
||
1186 | if (!in_array('GROUP:'.$toGroup, $to)) { |
||
1187 | $to[] = 'GROUP:'.$toGroup; |
||
1188 | } |
||
1189 | } |
||
1190 | break; |
||
1191 | } |
||
1192 | } |
||
1193 | |||
1194 | return $to; |
||
1195 | } |
||
1196 | |||
1197 | /** |
||
1198 | * constructs the form to display all the groups and users the message has been sent to. |
||
1199 | * |
||
1200 | * @param array $sent_to_array |
||
1201 | * input: |
||
1202 | * $sent_to_array is a 2 dimensional array containing the groups and the users |
||
1203 | * the first level is a distinction between groups and users: |
||
1204 | * $sent_to_array['groups'] * and $sent_to_array['users'] |
||
1205 | * $sent_to_array['groups'] (resp. $sent_to_array['users']) is also an array |
||
1206 | * containing all the id's of the groups (resp. users) who have received this message. |
||
1207 | * |
||
1208 | * @return string |
||
1209 | * |
||
1210 | * @author Patrick Cool <patrick.cool@> |
||
1211 | */ |
||
1212 | public static function sent_to_form($sent_to_array) |
||
1213 | { |
||
1214 | // we find all the names of the groups |
||
1215 | $group_names = self::get_course_groups(); |
||
1216 | |||
1217 | // we count the number of users and the number of groups |
||
1218 | $number_users = 0; |
||
1219 | if (isset($sent_to_array['users'])) { |
||
1220 | $number_users = count($sent_to_array['users']); |
||
1221 | } |
||
1222 | $number_groups = 0; |
||
1223 | if (isset($sent_to_array['groups'])) { |
||
1224 | $number_groups = count($sent_to_array['groups']); |
||
1225 | } |
||
1226 | |||
1227 | $total_numbers = $number_users + $number_groups; |
||
1228 | |||
1229 | // starting the form if there is more than one user/group |
||
1230 | $output = []; |
||
1231 | if ($total_numbers > 1) { |
||
1232 | // outputting the name of the groups |
||
1233 | if (is_array($sent_to_array['groups'])) { |
||
1234 | foreach ($sent_to_array['groups'] as $group_id) { |
||
1235 | $users = GroupManager::getStudents($group_id, true); |
||
1236 | $userToArray = []; |
||
1237 | foreach ($users as $student) { |
||
1238 | $userToArray[] = $student['complete_name_with_username']; |
||
1239 | } |
||
1240 | $output[] = |
||
1241 | '<br />'. |
||
1242 | Display::label($group_names[$group_id]['name'], 'info'). |
||
1243 | ' '.implode(', ', $userToArray); |
||
1244 | } |
||
1245 | } |
||
1246 | |||
1247 | if (isset($sent_to_array['users'])) { |
||
1248 | if (is_array($sent_to_array['users'])) { |
||
1249 | $usersToArray = []; |
||
1250 | foreach ($sent_to_array['users'] as $user_id) { |
||
1251 | $user_info = api_get_user_info($user_id); |
||
1252 | $usersToArray[] = $user_info['complete_name_with_username']; |
||
1253 | } |
||
1254 | $output[] = '<br />'.Display::label(get_lang('Users')).' '.implode(', ', $usersToArray); |
||
1255 | } |
||
1256 | } |
||
1257 | } else { |
||
1258 | // there is only one user/group |
||
1259 | if (isset($sent_to_array['users']) && is_array($sent_to_array['users'])) { |
||
1260 | $user_info = api_get_user_info($sent_to_array['users'][0]); |
||
1261 | $output[] = api_get_person_name($user_info['firstname'], $user_info['lastname']); |
||
1262 | } |
||
1263 | if (isset($sent_to_array['groups']) && |
||
1264 | is_array($sent_to_array['groups']) && |
||
1265 | isset($sent_to_array['groups'][0]) && |
||
1266 | $sent_to_array['groups'][0] !== 0 |
||
1267 | ) { |
||
1268 | $group_id = $sent_to_array['groups'][0]; |
||
1269 | |||
1270 | $users = GroupManager::getStudents($group_id, true); |
||
1271 | $userToArray = []; |
||
1272 | foreach ($users as $student) { |
||
1273 | $userToArray[] = $student['complete_name_with_username']; |
||
1274 | } |
||
1275 | $output[] = |
||
1276 | '<br />'. |
||
1277 | Display::label($group_names[$group_id]['name'], 'info'). |
||
1278 | ' '.implode(', ', $userToArray); |
||
1279 | } |
||
1280 | if (empty($sent_to_array['groups']) && empty($sent_to_array['users'])) { |
||
1281 | $output[] = " ".get_lang('Everybody'); |
||
1282 | } |
||
1283 | } |
||
1284 | |||
1285 | if (!empty($output)) { |
||
1286 | $output = array_filter($output); |
||
1287 | if (count($output) > 0) { |
||
1288 | $output = implode('<br />', $output); |
||
1289 | } |
||
1290 | |||
1291 | return $output; |
||
1292 | } |
||
1293 | } |
||
1294 | |||
1295 | /** |
||
1296 | * Returns all the users and all the groups a specific announcement item |
||
1297 | * has been sent to. |
||
1298 | * |
||
1299 | * @param string The tool (announcement, agenda, ...) |
||
1300 | * @param int ID of the element of the corresponding type |
||
1301 | * |
||
1302 | * @return array Array of users and groups to whom the element has been sent |
||
1303 | */ |
||
1304 | public static function sent_to($tool, $id) |
||
1305 | { |
||
1306 | $table = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
1307 | $tool = Database::escape_string($tool); |
||
1308 | $id = (int) $id; |
||
1309 | |||
1310 | $sent_to_group = []; |
||
1311 | $sent_to = []; |
||
1312 | $courseId = api_get_course_int_id(); |
||
1313 | |||
1314 | $sql = "SELECT to_group_id, to_user_id |
||
1315 | FROM $table |
||
1316 | WHERE c_id = $courseId AND tool = '$tool' AND ref=".$id; |
||
1317 | $result = Database::query($sql); |
||
1318 | |||
1319 | while ($row = Database::fetch_array($result)) { |
||
1320 | // if to_user_id <> 0 then it is sent to a specific user |
||
1321 | if ($row['to_user_id'] != 0) { |
||
1322 | $sent_to_user[] = $row['to_user_id']; |
||
1323 | continue; |
||
1324 | } |
||
1325 | |||
1326 | // if to_group_id is null then it is sent to a specific user |
||
1327 | // if to_group_id = 0 then it is sent to everybody |
||
1328 | if ($row['to_group_id'] != 0) { |
||
1329 | $sent_to_group[] = $row['to_group_id']; |
||
1330 | } |
||
1331 | } |
||
1332 | |||
1333 | if (isset($sent_to_group)) { |
||
1334 | $sent_to['groups'] = $sent_to_group; |
||
1335 | } |
||
1336 | |||
1337 | if (isset($sent_to_user)) { |
||
1338 | $sent_to['users'] = $sent_to_user; |
||
1339 | } |
||
1340 | |||
1341 | return $sent_to; |
||
1342 | } |
||
1343 | |||
1344 | /** |
||
1345 | * Show a list with all the attachments according to the post's id. |
||
1346 | * |
||
1347 | * @param int $announcementId |
||
1348 | * |
||
1349 | * @return array with the post info |
||
1350 | * |
||
1351 | * @author Arthur Portugal |
||
1352 | * |
||
1353 | * @version November 2009, dokeos 1.8.6.2 |
||
1354 | */ |
||
1355 | public static function get_attachment($announcementId) |
||
1356 | { |
||
1357 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); |
||
1358 | $announcementId = (int) $announcementId; |
||
1359 | $courseId = api_get_course_int_id(); |
||
1360 | $row = []; |
||
1361 | $sql = 'SELECT id, path, filename, comment |
||
1362 | FROM '.$table.' |
||
1363 | WHERE c_id = '.$courseId.' AND announcement_id = '.$announcementId; |
||
1364 | $result = Database::query($sql); |
||
1365 | if (Database::num_rows($result) != 0) { |
||
1366 | $row = Database::fetch_array($result, 'ASSOC'); |
||
1367 | } |
||
1368 | |||
1369 | return $row; |
||
1370 | } |
||
1371 | |||
1372 | /** |
||
1373 | * This function add a attachment file into announcement. |
||
1374 | * |
||
1375 | * @param int announcement id |
||
1376 | * @param string file comment |
||
1377 | * @param array uploaded file $_FILES |
||
1378 | * |
||
1379 | * @return int -1 if failed, 0 if unknown (should not happen), 1 if success |
||
1380 | */ |
||
1381 | public static function add_announcement_attachment_file( |
||
1382 | $announcement_id, |
||
1383 | $file_comment, |
||
1384 | $file |
||
1385 | ) { |
||
1386 | $courseInfo = api_get_course_info(); |
||
1387 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); |
||
1388 | $return = 0; |
||
1389 | $announcement_id = intval($announcement_id); |
||
1390 | $courseId = api_get_course_int_id(); |
||
1391 | |||
1392 | if (is_array($file) && $file['error'] == 0) { |
||
1393 | // TODO: This path is obsolete. The new document repository scheme should be kept in mind here. |
||
1394 | $courseDir = $courseInfo['path'].'/upload/announcements'; |
||
1395 | $sys_course_path = api_get_path(SYS_COURSE_PATH); |
||
1396 | $updir = $sys_course_path.$courseDir; |
||
1397 | |||
1398 | // Try to add an extension to the file if it hasn't one |
||
1399 | $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']); |
||
1400 | // user's file name |
||
1401 | $file_name = $file['name']; |
||
1402 | |||
1403 | if (!filter_extension($new_file_name)) { |
||
1404 | $return = -1; |
||
1405 | echo Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error'); |
||
1406 | } else { |
||
1407 | $new_file_name = uniqid(''); |
||
1408 | $new_path = $updir.'/'.$new_file_name; |
||
1409 | |||
1410 | // This file is copy here but its cleaned in api_mail_html in api.lib.php |
||
1411 | copy($file['tmp_name'], $new_path); |
||
1412 | |||
1413 | $params = [ |
||
1414 | 'c_id' => $courseId, |
||
1415 | 'filename' => $file_name, |
||
1416 | 'comment' => $file_comment, |
||
1417 | 'path' => $new_file_name, |
||
1418 | 'announcement_id' => $announcement_id, |
||
1419 | 'size' => (int) $file['size'], |
||
1420 | ]; |
||
1421 | |||
1422 | $insertId = Database::insert($table, $params); |
||
1423 | if ($insertId) { |
||
1424 | $sql = "UPDATE $table SET id = iid |
||
1425 | WHERE iid = $insertId"; |
||
1426 | Database::query($sql); |
||
1427 | } |
||
1428 | |||
1429 | $return = 1; |
||
1430 | } |
||
1431 | } |
||
1432 | |||
1433 | return $return; |
||
1434 | } |
||
1435 | |||
1436 | /** |
||
1437 | * This function edit a attachment file into announcement. |
||
1438 | * |
||
1439 | * @param int attach id |
||
1440 | * @param array uploaded file $_FILES |
||
1441 | * @param string file comment |
||
1442 | * |
||
1443 | * @return int |
||
1444 | */ |
||
1445 | public static function edit_announcement_attachment_file( |
||
1446 | $id_attach, |
||
1447 | $file, |
||
1448 | $file_comment |
||
1449 | ) { |
||
1450 | $courseInfo = api_get_course_info(); |
||
1451 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); |
||
1452 | $return = 0; |
||
1453 | $courseId = api_get_course_int_id(); |
||
1454 | |||
1455 | if (is_array($file) && $file['error'] == 0) { |
||
1456 | // TODO: This path is obsolete. The new document repository scheme should be kept in mind here. |
||
1457 | $courseDir = $courseInfo['path'].'/upload/announcements'; |
||
1458 | $sys_course_path = api_get_path(SYS_COURSE_PATH); |
||
1459 | $updir = $sys_course_path.$courseDir; |
||
1460 | |||
1461 | // Try to add an extension to the file if it hasn't one |
||
1462 | $new_file_name = add_ext_on_mime( |
||
1463 | stripslashes($file['name']), |
||
1464 | $file['type'] |
||
1465 | ); |
||
1466 | // user's file name |
||
1467 | $file_name = $file['name']; |
||
1468 | |||
1469 | if (!filter_extension($new_file_name)) { |
||
1470 | $return = -1; |
||
1471 | echo Display::return_message( |
||
1472 | get_lang('UplUnableToSaveFileFilteredExtension'), |
||
1473 | 'error' |
||
1474 | ); |
||
1475 | } else { |
||
1476 | $new_file_name = uniqid(''); |
||
1477 | $new_path = $updir.'/'.$new_file_name; |
||
1478 | copy($file['tmp_name'], $new_path); |
||
1479 | $safe_file_comment = Database::escape_string($file_comment); |
||
1480 | $safe_file_name = Database::escape_string($file_name); |
||
1481 | $safe_new_file_name = Database::escape_string($new_file_name); |
||
1482 | $id_attach = intval($id_attach); |
||
1483 | $sql = "UPDATE $table SET |
||
1484 | filename = '$safe_file_name', |
||
1485 | comment = '$safe_file_comment', |
||
1486 | path = '$safe_new_file_name', |
||
1487 | size ='".intval($file['size'])."' |
||
1488 | WHERE c_id = $courseId AND id = '$id_attach'"; |
||
1489 | $result = Database::query($sql); |
||
1490 | if ($result === false) { |
||
1491 | $return = -1; |
||
1492 | echo Display::return_message( |
||
1493 | get_lang('UplUnableToSaveFile'), |
||
1494 | 'error' |
||
1495 | ); |
||
1496 | } else { |
||
1497 | $return = 1; |
||
1498 | } |
||
1499 | } |
||
1500 | } |
||
1501 | |||
1502 | return $return; |
||
1503 | } |
||
1504 | |||
1505 | /** |
||
1506 | * This function delete a attachment file by id. |
||
1507 | * |
||
1508 | * @param int $id attachment file Id |
||
1509 | * |
||
1510 | * @return bool |
||
1511 | */ |
||
1512 | public static function delete_announcement_attachment_file($id) |
||
1513 | { |
||
1514 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); |
||
1515 | $id = intval($id); |
||
1516 | $courseId = api_get_course_int_id(); |
||
1517 | if (empty($courseId) || empty($id)) { |
||
1518 | return false; |
||
1519 | } |
||
1520 | $sql = "DELETE FROM $table |
||
1521 | WHERE c_id = $courseId AND id = $id"; |
||
1522 | Database::query($sql); |
||
1523 | |||
1524 | return true; |
||
1525 | } |
||
1526 | |||
1527 | /** |
||
1528 | * @param array $courseInfo |
||
1529 | * @param int $sessionId |
||
1530 | * @param int $announcementId |
||
1531 | * @param bool $sendToUsersInSession |
||
1532 | * @param bool $sendToDrhUsers |
||
1533 | * @param Monolog\Handler\HandlerInterface logger |
||
1534 | * @param int $senderId |
||
1535 | * @param bool $directMessage |
||
1536 | * |
||
1537 | * @return array |
||
1538 | */ |
||
1539 | public static function sendEmail( |
||
1540 | $courseInfo, |
||
1541 | $sessionId, |
||
1542 | $announcementId, |
||
1543 | $sendToUsersInSession = false, |
||
1544 | $sendToDrhUsers = false, |
||
1545 | $logger = null, |
||
1546 | $senderId = 0, |
||
1547 | $directMessage = false |
||
1548 | ) { |
||
1549 | $email = new AnnouncementEmail($courseInfo, $sessionId, $announcementId, $logger); |
||
1550 | |||
1551 | return $email->send($sendToUsersInSession, $sendToDrhUsers, $senderId, $directMessage); |
||
1552 | } |
||
1553 | |||
1554 | /** |
||
1555 | * @param $stok |
||
1556 | * @param $announcement_number |
||
1557 | * @param bool $getCount |
||
1558 | * @param null $start |
||
1559 | * @param null $limit |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
1560 | * @param string $sidx |
||
1561 | * @param string $sord |
||
1562 | * @param string $titleToSearch |
||
1563 | * @param int $userIdToSearch |
||
1564 | * @param int $userId |
||
1565 | * @param int $courseId |
||
1566 | * @param int $sessionId |
||
1567 | * |
||
1568 | * @return array |
||
1569 | */ |
||
1570 | public static function getAnnouncements( |
||
1571 | $stok, |
||
1572 | $announcement_number, |
||
1573 | $getCount = false, |
||
1574 | $start = null, |
||
1575 | $limit = null, |
||
1576 | $sidx = '', |
||
1577 | $sord = '', |
||
1578 | $titleToSearch = '', |
||
1579 | $userIdToSearch = 0, |
||
1580 | $userId = 0, |
||
1581 | $courseId = 0, |
||
1582 | $sessionId = 0 |
||
1583 | ) { |
||
1584 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
1585 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
1586 | |||
1587 | $user_id = $userId ?: api_get_user_id(); |
||
1588 | $group_id = api_get_group_id(); |
||
1589 | $session_id = $sessionId ?: api_get_session_id(); |
||
1590 | if (empty($courseId)) { |
||
1591 | $courseInfo = api_get_course_info(); |
||
1592 | $courseId = $courseInfo['real_id']; |
||
1593 | } else { |
||
1594 | $courseId = (int) $courseId; |
||
1595 | $courseInfo = api_get_course_info_by_id($courseId); |
||
1596 | } |
||
1597 | |||
1598 | if (empty($courseInfo)) { |
||
1599 | return []; |
||
1600 | } |
||
1601 | |||
1602 | $condition_session = api_get_session_condition( |
||
1603 | $session_id, |
||
1604 | true, |
||
1605 | true, |
||
1606 | 'announcement.session_id' |
||
1607 | ); |
||
1608 | |||
1609 | $group_memberships = GroupManager::get_group_ids( |
||
1610 | $courseId, |
||
1611 | api_get_user_id() |
||
1612 | ); |
||
1613 | $allowUserEditSetting = api_get_course_setting('allow_user_edit_announcement'); |
||
1614 | |||
1615 | $select = ' DISTINCT |
||
1616 | announcement.*, |
||
1617 | ip.visibility, |
||
1618 | ip.to_group_id, |
||
1619 | ip.insert_user_id, |
||
1620 | ip.insert_date, |
||
1621 | ip.lastedit_date'; |
||
1622 | $groupBy = ' GROUP BY announcement.iid'; |
||
1623 | if ($getCount) { |
||
1624 | $groupBy = ''; |
||
1625 | $select = ' COUNT(DISTINCT announcement.iid) count'; |
||
1626 | } |
||
1627 | |||
1628 | $searchCondition = ''; |
||
1629 | if (!empty($titleToSearch)) { |
||
1630 | $titleToSearch = Database::escape_string($titleToSearch); |
||
1631 | $searchCondition .= " AND (title LIKE '%$titleToSearch%')"; |
||
1632 | } |
||
1633 | |||
1634 | if (!empty($userIdToSearch)) { |
||
1635 | $userIdToSearch = (int) $userIdToSearch; |
||
1636 | $searchCondition .= " AND (ip.insert_user_id = $userIdToSearch)"; |
||
1637 | } |
||
1638 | |||
1639 | $allowOnlyGroup = api_get_configuration_value('hide_base_course_announcements_in_group'); |
||
1640 | $extraGroupCondition = ''; |
||
1641 | if ($allowOnlyGroup) { |
||
1642 | $extraGroupCondition = " AND ip.to_group_id = $group_id "; |
||
1643 | } |
||
1644 | |||
1645 | $allowDrhAccess = api_get_configuration_value('allow_drh_access_announcement'); |
||
1646 | |||
1647 | if ($allowDrhAccess && api_is_drh()) { |
||
1648 | // DRH only can see visible |
||
1649 | $searchCondition .= ' AND (ip.visibility = 1)'; |
||
1650 | } |
||
1651 | |||
1652 | if (api_is_allowed_to_edit(false, true) || |
||
1653 | ($allowUserEditSetting && !api_is_anonymous()) || |
||
1654 | ($allowDrhAccess && api_is_drh()) || |
||
1655 | ($session_id && api_is_coach() && api_get_configuration_value('allow_coach_to_edit_announcements')) |
||
1656 | ) { |
||
1657 | // A.1. you are a course admin with a USER filter |
||
1658 | // => see only the messages of this specific user + the messages of the group (s)he is member of. |
||
1659 | //if (!empty($user_id)) { |
||
1660 | if (0) { |
||
1661 | if (is_array($group_memberships) && |
||
1662 | count($group_memberships) > 0 |
||
1663 | ) { |
||
1664 | $sql = "SELECT $select |
||
1665 | FROM $tbl_announcement announcement |
||
1666 | INNER JOIN $tbl_item_property ip |
||
1667 | ON (announcement.id = ip.ref AND ip.c_id = announcement.c_id) |
||
1668 | WHERE |
||
1669 | announcement.c_id = $courseId AND |
||
1670 | ip.c_id = $courseId AND |
||
1671 | ip.tool = 'announcement' AND |
||
1672 | ( |
||
1673 | ip.to_user_id = $user_id OR |
||
1674 | ip.to_group_id IS NULL OR |
||
1675 | ip.to_group_id IN (0, ".implode(", ", $group_memberships).") |
||
1676 | ) AND |
||
1677 | ip.visibility IN ('1', '0') |
||
1678 | $condition_session |
||
1679 | $searchCondition |
||
1680 | ORDER BY display_order DESC"; |
||
1681 | } else { |
||
1682 | $sql = "SELECT $select |
||
1683 | FROM $tbl_announcement announcement |
||
1684 | INNER JOIN $tbl_item_property ip |
||
1685 | ON (announcement.id = ip.ref AND ip.c_id = announcement.c_id) |
||
1686 | WHERE |
||
1687 | announcement.c_id = $courseId AND |
||
1688 | ip.c_id = $courseId AND |
||
1689 | ip.tool ='announcement' AND |
||
1690 | (ip.to_user_id = $user_id OR ip.to_group_id='0' OR ip.to_group_id IS NULL) AND |
||
1691 | ip.visibility IN ('1', '0') |
||
1692 | $condition_session |
||
1693 | $searchCondition |
||
1694 | ORDER BY display_order DESC"; |
||
1695 | } |
||
1696 | } elseif ($group_id != 0) { |
||
1697 | // A.2. you are a course admin with a GROUP filter |
||
1698 | // => see only the messages of this specific group |
||
1699 | $sql = "SELECT $select |
||
1700 | FROM $tbl_announcement announcement |
||
1701 | INNER JOIN $tbl_item_property ip |
||
1702 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
1703 | WHERE |
||
1704 | ip.tool='announcement' AND |
||
1705 | announcement.c_id = $courseId AND |
||
1706 | ip.c_id = $courseId AND |
||
1707 | ip.visibility<>'2' AND |
||
1708 | (ip.to_group_id = $group_id OR ip.to_group_id='0' OR ip.to_group_id IS NULL) |
||
1709 | $condition_session |
||
1710 | $searchCondition |
||
1711 | $extraGroupCondition |
||
1712 | $groupBy |
||
1713 | ORDER BY display_order DESC"; |
||
1714 | } else { |
||
1715 | // A.3 you are a course admin without any group or user filter |
||
1716 | // A.3.a you are a course admin without user or group filter but WITH studentview |
||
1717 | // => see all the messages of all the users and groups without editing possibilities |
||
1718 | if (isset($isStudentView) && $isStudentView == 'true') { |
||
1719 | $sql = "SELECT $select |
||
1720 | FROM $tbl_announcement announcement |
||
1721 | INNER JOIN $tbl_item_property ip |
||
1722 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
1723 | WHERE |
||
1724 | ip.tool='announcement' AND |
||
1725 | announcement.c_id = $courseId AND |
||
1726 | ip.c_id = $courseId AND |
||
1727 | ip.visibility='1' |
||
1728 | $condition_session |
||
1729 | $searchCondition |
||
1730 | $groupBy |
||
1731 | ORDER BY display_order DESC"; |
||
1732 | } else { |
||
1733 | // A.3.a you are a course admin without user or group filter and WTIHOUT studentview (= the normal course admin view) |
||
1734 | // => see all the messages of all the users and groups with editing possibilities |
||
1735 | $sql = "SELECT $select |
||
1736 | FROM $tbl_announcement announcement |
||
1737 | INNER JOIN $tbl_item_property ip |
||
1738 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
1739 | WHERE |
||
1740 | ip.tool = 'announcement' AND |
||
1741 | announcement.c_id = $courseId AND |
||
1742 | ip.c_id = $courseId AND |
||
1743 | (ip.visibility='0' OR ip.visibility='1') |
||
1744 | $condition_session |
||
1745 | $searchCondition |
||
1746 | $groupBy |
||
1747 | ORDER BY display_order DESC"; |
||
1748 | } |
||
1749 | } |
||
1750 | } else { |
||
1751 | // STUDENT |
||
1752 | if (is_array($group_memberships) && count($group_memberships) > 0) { |
||
1753 | if ($allowUserEditSetting && !api_is_anonymous()) { |
||
1754 | if ($group_id == 0) { |
||
1755 | // No group |
||
1756 | $cond_user_id = " AND ( |
||
1757 | ip.lastedit_user_id = '".$user_id."' OR ( |
||
1758 | (ip.to_user_id='$user_id' OR ip.to_user_id IS NULL) OR |
||
1759 | (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).")) |
||
1760 | ) |
||
1761 | ) "; |
||
1762 | } else { |
||
1763 | $cond_user_id = " AND ( |
||
1764 | ip.lastedit_user_id = '".$user_id."' OR ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".$group_id.") |
||
1765 | )"; |
||
1766 | $cond_user_id .= $extraGroupCondition; |
||
1767 | } |
||
1768 | } else { |
||
1769 | if ($group_id == 0) { |
||
1770 | $cond_user_id = " AND ( |
||
1771 | (ip.to_user_id='$user_id' OR ip.to_user_id IS NULL) AND |
||
1772 | (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).")) |
||
1773 | ) "; |
||
1774 | } else { |
||
1775 | $cond_user_id = " AND ( |
||
1776 | (ip.to_user_id='$user_id' OR ip.to_user_id IS NULL) AND |
||
1777 | (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".$group_id.")) |
||
1778 | )"; |
||
1779 | $cond_user_id .= $extraGroupCondition; |
||
1780 | } |
||
1781 | } |
||
1782 | |||
1783 | $sql = "SELECT $select |
||
1784 | FROM $tbl_announcement announcement INNER JOIN |
||
1785 | $tbl_item_property ip |
||
1786 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
1787 | WHERE |
||
1788 | announcement.c_id = $courseId AND |
||
1789 | ip.c_id = $courseId AND |
||
1790 | ip.tool='announcement' |
||
1791 | $cond_user_id |
||
1792 | $condition_session |
||
1793 | $searchCondition AND |
||
1794 | ip.visibility='1' |
||
1795 | $groupBy |
||
1796 | ORDER BY display_order DESC"; |
||
1797 | } else { |
||
1798 | if ($user_id) { |
||
1799 | if ($allowUserEditSetting && !api_is_anonymous()) { |
||
1800 | $cond_user_id = " AND ( |
||
1801 | ip.lastedit_user_id = '".api_get_user_id()."' OR |
||
1802 | ((ip.to_user_id='$user_id' OR ip.to_user_id IS NULL) AND |
||
1803 | (ip.to_group_id='0' OR ip.to_group_id IS NULL) |
||
1804 | ) |
||
1805 | ) "; |
||
1806 | } else { |
||
1807 | $cond_user_id = " AND ((ip.to_user_id='$user_id' OR ip.to_user_id IS NULL) AND |
||
1808 | (ip.to_group_id='0' OR ip.to_group_id IS NULL) ) "; |
||
1809 | } |
||
1810 | |||
1811 | $sql = "SELECT $select |
||
1812 | FROM $tbl_announcement announcement |
||
1813 | INNER JOIN $tbl_item_property ip |
||
1814 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
1815 | WHERE |
||
1816 | announcement.c_id = $courseId AND |
||
1817 | ip.c_id = $courseId AND |
||
1818 | ip.tool='announcement' |
||
1819 | $cond_user_id |
||
1820 | $condition_session |
||
1821 | $searchCondition |
||
1822 | AND ip.visibility='1' |
||
1823 | AND announcement.session_id IN(0, ".$session_id.") |
||
1824 | $groupBy |
||
1825 | ORDER BY display_order DESC"; |
||
1826 | } else { |
||
1827 | if (($allowUserEditSetting && !api_is_anonymous())) { |
||
1828 | $cond_user_id = " AND ( |
||
1829 | ip.lastedit_user_id = '".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL |
||
1830 | )"; |
||
1831 | } else { |
||
1832 | $cond_user_id = " AND ip.to_group_id='0' OR ip.to_group_id IS NULL "; |
||
1833 | } |
||
1834 | |||
1835 | $sql = "SELECT $select |
||
1836 | FROM $tbl_announcement announcement |
||
1837 | INNER JOIN $tbl_item_property ip |
||
1838 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
1839 | WHERE |
||
1840 | announcement.c_id = $courseId AND |
||
1841 | ip.c_id = $courseId AND |
||
1842 | ip.tool='announcement' |
||
1843 | $cond_user_id |
||
1844 | $condition_session |
||
1845 | $searchCondition AND |
||
1846 | ip.visibility='1' AND |
||
1847 | announcement.session_id IN ( 0,".api_get_session_id().") |
||
1848 | $groupBy |
||
1849 | "; |
||
1850 | } |
||
1851 | } |
||
1852 | } |
||
1853 | |||
1854 | if (!is_null($start) && !is_null($limit)) { |
||
1855 | $start = (int) $start; |
||
1856 | $limit = (int) $limit; |
||
1857 | $sql .= " LIMIT $start, $limit"; |
||
1858 | } |
||
1859 | |||
1860 | $result = Database::query($sql); |
||
1861 | if ($getCount) { |
||
1862 | $result = Database::fetch_array($result, 'ASSOC'); |
||
1863 | |||
1864 | return $result['count']; |
||
1865 | } |
||
1866 | |||
1867 | $iterator = 1; |
||
1868 | $bottomAnnouncement = $announcement_number; |
||
1869 | $displayed = []; |
||
1870 | $results = []; |
||
1871 | $emailIcon = '<i class="fa fa-envelope-o" title="'.get_lang('AnnounceSentByEmail').'"></i>'; |
||
1872 | $attachmentIcon = '<i class="fa fa-paperclip" title="'.get_lang('Attachment').'"></i>'; |
||
1873 | $editIcon = Display::return_icon( |
||
1874 | 'edit.png', |
||
1875 | get_lang('Edit'), |
||
1876 | '', |
||
1877 | ICON_SIZE_SMALL |
||
1878 | ); |
||
1879 | |||
1880 | $editIconDisable = Display::return_icon( |
||
1881 | 'edit_na.png', |
||
1882 | get_lang('Edit'), |
||
1883 | '', |
||
1884 | ICON_SIZE_SMALL |
||
1885 | ); |
||
1886 | $deleteIcon = Display::return_icon( |
||
1887 | 'delete.png', |
||
1888 | get_lang('Delete'), |
||
1889 | '', |
||
1890 | ICON_SIZE_SMALL |
||
1891 | ); |
||
1892 | |||
1893 | $deleteIconDisable = Display::return_icon( |
||
1894 | 'delete_na.png', |
||
1895 | get_lang('Delete'), |
||
1896 | '', |
||
1897 | ICON_SIZE_SMALL |
||
1898 | ); |
||
1899 | |||
1900 | $isTutor = false; |
||
1901 | if (!empty($group_id)) { |
||
1902 | $groupInfo = GroupManager::get_group_properties(api_get_group_id()); |
||
1903 | //User has access in the group? |
||
1904 | $isTutor = GroupManager::is_tutor_of_group( |
||
1905 | api_get_user_id(), |
||
1906 | $groupInfo |
||
1907 | ); |
||
1908 | } |
||
1909 | |||
1910 | while ($row = Database::fetch_array($result, 'ASSOC')) { |
||
1911 | if (!in_array($row['id'], $displayed)) { |
||
1912 | $actionUrl = api_get_path(WEB_CODE_PATH).'announcements/announcements.php?' |
||
1913 | .api_get_cidreq_params($courseInfo['code'], $session_id, $row['to_group_id']); |
||
1914 | $sent_to_icon = ''; |
||
1915 | // the email icon |
||
1916 | if ($row['email_sent'] == '1') { |
||
1917 | $sent_to_icon = ' '.$emailIcon; |
||
1918 | } |
||
1919 | |||
1920 | $groupReference = $row['to_group_id'] > 0 ? ' <span class="label label-info">'.get_lang('Group').'</span> ' : ''; |
||
1921 | $disableEdit = false; |
||
1922 | $to = self::loadEditUsers('announcement', $row['id'], true); |
||
1923 | $separated = CourseManager::separateUsersGroups($to); |
||
1924 | if (!empty($group_id)) { |
||
1925 | // If the announcement was sent to many groups, disable edition inside a group |
||
1926 | if (isset($separated['groups']) && count($separated['groups']) > 1) { |
||
1927 | $disableEdit = true; |
||
1928 | } |
||
1929 | |||
1930 | // If the announcement was sent only to the course disable edition |
||
1931 | if (empty($separated['groups']) && empty($separated['users'])) { |
||
1932 | $disableEdit = true; |
||
1933 | } |
||
1934 | |||
1935 | // Announcement sent to only a user |
||
1936 | if ($separated['groups'] > 1 && !in_array($group_id, $separated['groups'])) { |
||
1937 | $disableEdit = true; |
||
1938 | } |
||
1939 | } else { |
||
1940 | if (isset($separated['groups']) && count($separated['groups']) > 1) { |
||
1941 | $groupReference = ''; |
||
1942 | } |
||
1943 | } |
||
1944 | |||
1945 | $title = $row['title'].$groupReference.$sent_to_icon; |
||
1946 | $item_visibility = api_get_item_visibility( |
||
1947 | $courseInfo, |
||
1948 | TOOL_ANNOUNCEMENT, |
||
1949 | $row['id'], |
||
1950 | $session_id |
||
1951 | ); |
||
1952 | $row['visibility'] = $item_visibility; |
||
1953 | |||
1954 | // show attachment list |
||
1955 | $attachment_list = self::get_attachment($row['id']); |
||
1956 | $attachment_icon = ''; |
||
1957 | if (count($attachment_list) > 0) { |
||
1958 | $attachment_icon = ' '.$attachmentIcon; |
||
1959 | } |
||
1960 | |||
1961 | $user_info = api_get_user_info($row['insert_user_id']); |
||
1962 | $username = sprintf(get_lang('LoginX'), $user_info['username']); |
||
1963 | $username_span = Display::tag( |
||
1964 | 'span', |
||
1965 | $user_info['complete_name'], |
||
1966 | ['title' => $username] |
||
1967 | ); |
||
1968 | |||
1969 | $title = Display::url( |
||
1970 | $title.$attachment_icon, |
||
1971 | $actionUrl.'&action=view&id='.$row['id'] |
||
1972 | ); |
||
1973 | |||
1974 | // we can edit if : we are the teacher OR the element belongs to |
||
1975 | // the session we are coaching OR the option to allow users to edit is on |
||
1976 | if (api_is_allowed_to_edit(false, true) || |
||
1977 | (api_is_session_general_coach() && api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $row['id'])) || |
||
1978 | (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous()) || |
||
1979 | ($row['to_group_id'] == $group_id && $isTutor) || |
||
1980 | ($session_id && api_is_coach() && api_get_configuration_value('allow_coach_to_edit_announcements')) |
||
1981 | ) { |
||
1982 | if ($disableEdit === true) { |
||
1983 | $modify_icons = "<a href='#'>".$editIconDisable."</a>"; |
||
1984 | } else { |
||
1985 | $modify_icons = "<a href=\"".$actionUrl."&action=modify&id=".$row['id']."\">".$editIcon."</a>"; |
||
1986 | } |
||
1987 | |||
1988 | $image_visibility = 'invisible'; |
||
1989 | $alt_visibility = get_lang('Visible'); |
||
1990 | if ($row['visibility'] == 1) { |
||
1991 | $image_visibility = 'visible'; |
||
1992 | $alt_visibility = get_lang('Hide'); |
||
1993 | } |
||
1994 | |||
1995 | $modify_icons .= "<a |
||
1996 | href=\"".$actionUrl."&action=showhide&id=".$row['id']."&sec_token=".$stok."\">". |
||
1997 | Display::return_icon($image_visibility.'.png', $alt_visibility, '', ICON_SIZE_SMALL). |
||
1998 | "</a>"; |
||
1999 | |||
2000 | // DISPLAY MOVE UP COMMAND only if it is not the top announcement |
||
2001 | if ($iterator != 1) { |
||
2002 | $modify_icons .= "<a href=\"".$actionUrl."&action=move&up=".$row["id"]."&sec_token=".$stok."\">". |
||
2003 | Display::return_icon('up.gif', get_lang('Up'))."</a>"; |
||
2004 | } else { |
||
2005 | $modify_icons .= Display::return_icon('up_na.gif', get_lang('Up')); |
||
2006 | } |
||
2007 | |||
2008 | if ($iterator < $bottomAnnouncement) { |
||
2009 | $modify_icons .= "<a href=\"".$actionUrl."&action=move&down=".$row["id"]."&sec_token=".$stok."\">". |
||
2010 | Display::return_icon('down.gif', get_lang('Down'))."</a>"; |
||
2011 | } else { |
||
2012 | $modify_icons .= Display::return_icon('down_na.gif', get_lang('Down')); |
||
2013 | } |
||
2014 | |||
2015 | if (api_is_allowed_to_edit(false, true)) { |
||
2016 | if ($disableEdit === true) { |
||
2017 | $modify_icons .= Display::url($deleteIconDisable, '#'); |
||
2018 | } else { |
||
2019 | $modify_icons .= "<a href=\"".$actionUrl."&action=delete&id=".$row['id']."&sec_token=".$stok."\" onclick=\"javascript:if(!confirm('".addslashes( |
||
2020 | api_htmlentities( |
||
2021 | get_lang('ConfirmYourChoice'), |
||
2022 | ENT_QUOTES, |
||
2023 | api_get_system_encoding() |
||
2024 | ) |
||
2025 | )."')) return false;\">". |
||
2026 | $deleteIcon."</a>"; |
||
2027 | } |
||
2028 | } |
||
2029 | $iterator++; |
||
2030 | } else { |
||
2031 | $modify_icons = Display::url( |
||
2032 | Display::return_icon('default.png'), |
||
2033 | $actionUrl.'&action=view&id='.$row['id'] |
||
2034 | ); |
||
2035 | } |
||
2036 | |||
2037 | $announcement = [ |
||
2038 | 'id' => $row['id'], |
||
2039 | 'title' => $title, |
||
2040 | 'username' => $username_span, |
||
2041 | 'insert_date' => api_convert_and_format_date( |
||
2042 | $row['insert_date'], |
||
2043 | DATE_TIME_FORMAT_LONG |
||
2044 | ), |
||
2045 | 'lastedit_date' => api_convert_and_format_date( |
||
2046 | $row['lastedit_date'], |
||
2047 | DATE_TIME_FORMAT_LONG |
||
2048 | ), |
||
2049 | 'actions' => $modify_icons, |
||
2050 | ]; |
||
2051 | |||
2052 | $results[] = $announcement; |
||
2053 | } |
||
2054 | $displayed[] = $row['id']; |
||
2055 | } |
||
2056 | |||
2057 | return $results; |
||
2058 | } |
||
2059 | |||
2060 | /** |
||
2061 | * @return int |
||
2062 | */ |
||
2063 | public static function getNumberAnnouncements() |
||
2064 | { |
||
2065 | // Maximum title messages to display |
||
2066 | $maximum = '12'; |
||
2067 | // Database Table Definitions |
||
2068 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
2069 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
2070 | |||
2071 | $session_id = api_get_session_id(); |
||
2072 | $courseInfo = api_get_course_info(); |
||
2073 | $courseId = $courseInfo['real_id']; |
||
2074 | $userId = api_get_user_id(); |
||
2075 | $condition_session = api_get_session_condition( |
||
2076 | $session_id, |
||
2077 | true, |
||
2078 | true, |
||
2079 | 'announcement.session_id' |
||
2080 | ); |
||
2081 | |||
2082 | if (api_is_allowed_to_edit(false, true)) { |
||
2083 | // check teacher status |
||
2084 | if (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath') { |
||
2085 | if (api_get_group_id() == 0) { |
||
2086 | $group_condition = ''; |
||
2087 | } else { |
||
2088 | $group_condition = " AND (ip.to_group_id='".api_get_group_id()."' OR ip.to_group_id = 0 OR ip.to_group_id IS NULL)"; |
||
2089 | } |
||
2090 | |||
2091 | $sql = "SELECT |
||
2092 | announcement.*, |
||
2093 | ip.visibility, |
||
2094 | ip.to_group_id, |
||
2095 | ip.insert_user_id |
||
2096 | FROM $tbl_announcement announcement |
||
2097 | INNER JOIN $tbl_item_property ip |
||
2098 | ON (announcement.c_id = ip.c_id AND announcement.id = ip.ref) |
||
2099 | WHERE |
||
2100 | announcement.c_id = $courseId AND |
||
2101 | ip.c_id = $courseId AND |
||
2102 | ip.tool = 'announcement' AND |
||
2103 | ip.visibility <> '2' |
||
2104 | $group_condition |
||
2105 | $condition_session |
||
2106 | GROUP BY ip.ref |
||
2107 | ORDER BY display_order DESC |
||
2108 | LIMIT 0, $maximum"; |
||
2109 | } |
||
2110 | } else { |
||
2111 | // students only get to see the visible announcements |
||
2112 | if (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath') { |
||
2113 | $group_memberships = GroupManager::get_group_ids( |
||
2114 | $courseInfo['real_id'], |
||
2115 | $userId |
||
2116 | ); |
||
2117 | |||
2118 | if ((api_get_course_setting('allow_user_edit_announcement') && |
||
2119 | !api_is_anonymous()) |
||
2120 | ) { |
||
2121 | if (api_get_group_id() == 0) { |
||
2122 | $cond_user_id = " AND ( |
||
2123 | ip.lastedit_user_id = '".$userId."' OR ( |
||
2124 | ip.to_user_id='".$userId."' OR |
||
2125 | ip.to_group_id IN (0, ".implode(", ", $group_memberships).") OR |
||
2126 | ip.to_group_id IS NULL |
||
2127 | ) |
||
2128 | ) |
||
2129 | "; |
||
2130 | } else { |
||
2131 | $cond_user_id = " AND ( |
||
2132 | ip.lastedit_user_id = '".$userId."'OR |
||
2133 | ip.to_group_id IN (0, ".api_get_group_id().") OR |
||
2134 | ip.to_group_id IS NULL |
||
2135 | )"; |
||
2136 | } |
||
2137 | } else { |
||
2138 | if (api_get_group_id() == 0) { |
||
2139 | $cond_user_id = " AND ( |
||
2140 | ip.to_user_id='".$userId."' OR |
||
2141 | ip.to_group_id IN (0, ".implode(", ", $group_memberships).") OR |
||
2142 | ip.to_group_id IS NULL |
||
2143 | ) "; |
||
2144 | } else { |
||
2145 | $cond_user_id = " AND ( |
||
2146 | ip.to_user_id='".$userId."' OR |
||
2147 | ip.to_group_id IN (0, ".api_get_group_id().") OR |
||
2148 | ip.to_group_id IS NULL |
||
2149 | ) "; |
||
2150 | } |
||
2151 | } |
||
2152 | |||
2153 | // the user is member of several groups => display personal announcements AND |
||
2154 | // his group announcements AND the general announcements |
||
2155 | if (is_array($group_memberships) && count($group_memberships) > 0) { |
||
2156 | $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id |
||
2157 | FROM $tbl_announcement announcement |
||
2158 | INNER JOIN $tbl_item_property ip |
||
2159 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
2160 | WHERE |
||
2161 | announcement.c_id = $courseId AND |
||
2162 | ip.c_id = $courseId AND |
||
2163 | ip.tool='announcement' AND |
||
2164 | ip.visibility='1' |
||
2165 | $cond_user_id |
||
2166 | $condition_session |
||
2167 | GROUP BY ip.ref |
||
2168 | ORDER BY display_order DESC |
||
2169 | LIMIT 0, $maximum"; |
||
2170 | } else { |
||
2171 | // the user is not member of any group |
||
2172 | // this is an identified user => show the general announcements AND his personal announcements |
||
2173 | if ($userId) { |
||
2174 | if ((api_get_course_setting('allow_user_edit_announcement') && |
||
2175 | !api_is_anonymous()) |
||
2176 | ) { |
||
2177 | $cond_user_id = " AND ( |
||
2178 | ip.lastedit_user_id = '".$userId."' OR |
||
2179 | ( ip.to_user_id='".$userId."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL) |
||
2180 | ) "; |
||
2181 | } else { |
||
2182 | $cond_user_id = " AND ( ip.to_user_id='".$userId."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL) "; |
||
2183 | } |
||
2184 | $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id |
||
2185 | FROM $tbl_announcement announcement |
||
2186 | INNER JOIN $tbl_item_property ip |
||
2187 | ON (announcement.c_id = ip.c_id AND announcement.id = ip.ref) |
||
2188 | WHERE |
||
2189 | announcement.c_id = $courseId AND |
||
2190 | ip.c_id = $courseId AND |
||
2191 | ip.tool='announcement' AND |
||
2192 | ip.visibility='1' |
||
2193 | $cond_user_id |
||
2194 | $condition_session |
||
2195 | GROUP BY ip.ref |
||
2196 | ORDER BY display_order DESC |
||
2197 | LIMIT 0, $maximum"; |
||
2198 | } else { |
||
2199 | if (api_get_course_setting('allow_user_edit_announcement')) { |
||
2200 | $cond_user_id = " AND ( |
||
2201 | ip.lastedit_user_id = '".api_get_user_id()."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL |
||
2202 | ) "; |
||
2203 | } else { |
||
2204 | $cond_user_id = " AND ip.to_group_id='0' "; |
||
2205 | } |
||
2206 | |||
2207 | // the user is not identiefied => show only the general announcements |
||
2208 | $sql = "SELECT |
||
2209 | announcement.*, |
||
2210 | ip.visibility, |
||
2211 | ip.to_group_id, |
||
2212 | ip.insert_user_id |
||
2213 | FROM $tbl_announcement announcement |
||
2214 | INNER JOIN $tbl_item_property ip |
||
2215 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
2216 | WHERE |
||
2217 | announcement.c_id = $courseId AND |
||
2218 | ip.c_id = $courseId AND |
||
2219 | ip.tool='announcement' AND |
||
2220 | ip.visibility='1' AND |
||
2221 | ip.to_group_id='0' |
||
2222 | $condition_session |
||
2223 | GROUP BY ip.ref |
||
2224 | ORDER BY display_order DESC |
||
2225 | LIMIT 0, $maximum"; |
||
2226 | } |
||
2227 | } |
||
2228 | } |
||
2229 | } |
||
2230 | |||
2231 | $result = Database::query($sql); |
||
2232 | |||
2233 | return Database::num_rows($result); |
||
2234 | } |
||
2235 | } |
||
2236 |