| Total Complexity | 223 |
| Total Lines | 2071 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AnnouncementManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AnnouncementManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class AnnouncementManager |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Constructor. |
||
| 20 | */ |
||
| 21 | public function __construct() |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @return array |
||
| 27 | */ |
||
| 28 | public static function getTags() |
||
| 29 | { |
||
| 30 | $tags = [ |
||
| 31 | '((user_name))', |
||
| 32 | '((user_firstname))', |
||
| 33 | '((user_lastname))', |
||
| 34 | '((user_official_code))', |
||
| 35 | '((course_title))', |
||
| 36 | '((course_link))', |
||
| 37 | ]; |
||
| 38 | |||
| 39 | $tags[] = '((teachers))'; |
||
| 40 | |||
| 41 | $extraField = new ExtraField('user'); |
||
| 42 | $extraFields = $extraField->get_all(['filter = ?' => 1]); |
||
| 43 | if (!empty($extraFields)) { |
||
| 44 | foreach ($extraFields as $extra) { |
||
| 45 | $tags[] = "((extra_".$extra['variable']."))"; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | $sessionId = api_get_session_id(); |
||
| 49 | if (!empty($sessionId)) { |
||
| 50 | $tags[] = '((coaches))'; |
||
| 51 | $tags[] = '((general_coach))'; |
||
| 52 | $tags[] = '((general_coach_email))'; |
||
| 53 | } |
||
| 54 | |||
| 55 | return $tags; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param int $userId |
||
| 60 | * @param string $content |
||
| 61 | * @param string $courseCode |
||
| 62 | * @param int $sessionId |
||
| 63 | * |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public static function parseContent( |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Gets all announcements from a course. |
||
| 146 | * |
||
| 147 | * @param array $course_info |
||
| 148 | * @param int $session_id |
||
| 149 | * |
||
| 150 | * @return array html with the content and count of announcements or false otherwise |
||
| 151 | */ |
||
| 152 | public static function get_all_annoucement_by_course($course_info, $session_id = 0) |
||
| 153 | { |
||
| 154 | $session_id = intval($session_id); |
||
| 155 | $courseId = $course_info['real_id']; |
||
| 156 | |||
| 157 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 158 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 159 | |||
| 160 | $sql = "SELECT DISTINCT |
||
| 161 | announcement.id, |
||
| 162 | announcement.title, |
||
| 163 | announcement.content |
||
| 164 | FROM $tbl_announcement announcement |
||
| 165 | INNER JOIN $tbl_item_property i |
||
| 166 | ON (announcement.id = i.ref AND announcement.c_id = i.c_id) |
||
| 167 | WHERE |
||
| 168 | i.tool='announcement' AND |
||
| 169 | announcement.session_id = '$session_id' AND |
||
| 170 | announcement.c_id = $courseId AND |
||
| 171 | i.c_id = $courseId |
||
| 172 | ORDER BY display_order DESC"; |
||
| 173 | $rs = Database::query($sql); |
||
| 174 | $num_rows = Database::num_rows($rs); |
||
| 175 | if ($num_rows > 0) { |
||
| 176 | $list = []; |
||
| 177 | while ($row = Database::fetch_array($rs)) { |
||
| 178 | $list[] = $row; |
||
| 179 | } |
||
| 180 | |||
| 181 | return $list; |
||
| 182 | } |
||
| 183 | |||
| 184 | return false; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * This functions switches the visibility a course resource |
||
| 189 | * using the visibility field in 'item_property'. |
||
| 190 | * |
||
| 191 | * @param array $courseInfo |
||
| 192 | * @param int $id ID of the element of the corresponding type |
||
| 193 | * |
||
| 194 | * @return bool False on failure, True on success |
||
| 195 | */ |
||
| 196 | public static function change_visibility_announcement($courseInfo, $id) |
||
| 197 | { |
||
| 198 | $session_id = api_get_session_id(); |
||
| 199 | $item_visibility = api_get_item_visibility( |
||
| 200 | $courseInfo, |
||
| 201 | TOOL_ANNOUNCEMENT, |
||
| 202 | $id, |
||
| 203 | $session_id |
||
| 204 | ); |
||
| 205 | if ($item_visibility == '1') { |
||
| 206 | api_item_property_update( |
||
| 207 | $courseInfo, |
||
| 208 | TOOL_ANNOUNCEMENT, |
||
| 209 | $id, |
||
| 210 | 'invisible', |
||
| 211 | api_get_user_id() |
||
| 212 | ); |
||
| 213 | } else { |
||
| 214 | api_item_property_update( |
||
| 215 | $courseInfo, |
||
| 216 | TOOL_ANNOUNCEMENT, |
||
| 217 | $id, |
||
| 218 | 'visible', |
||
| 219 | api_get_user_id() |
||
| 220 | ); |
||
| 221 | } |
||
| 222 | |||
| 223 | return true; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Deletes an announcement. |
||
| 228 | * |
||
| 229 | * @param array $courseInfo the course array |
||
| 230 | * @param int $id the announcement id |
||
| 231 | */ |
||
| 232 | public static function delete_announcement($courseInfo, $id) |
||
| 233 | { |
||
| 234 | api_item_property_update( |
||
| 235 | $courseInfo, |
||
| 236 | TOOL_ANNOUNCEMENT, |
||
| 237 | $id, |
||
| 238 | 'delete', |
||
| 239 | api_get_user_id() |
||
| 240 | ); |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Deletes all announcements by course. |
||
| 245 | * |
||
| 246 | * @param array $courseInfo the course array |
||
| 247 | */ |
||
| 248 | public static function delete_all_announcements($courseInfo) |
||
| 249 | { |
||
| 250 | $announcements = self::get_all_annoucement_by_course( |
||
| 251 | $courseInfo, |
||
| 252 | api_get_session_id() |
||
| 253 | ); |
||
| 254 | if (!empty($announcements)) { |
||
| 255 | foreach ($announcements as $annon) { |
||
| 256 | api_item_property_update( |
||
| 257 | $courseInfo, |
||
| 258 | TOOL_ANNOUNCEMENT, |
||
| 259 | $annon['id'], |
||
| 260 | 'delete', |
||
| 261 | api_get_user_id() |
||
| 262 | ); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param string $title |
||
| 269 | * @param int $courseId |
||
| 270 | * @param int $sessionId |
||
| 271 | * @param int $visibility 1 or 0 |
||
| 272 | * |
||
| 273 | * @return mixed |
||
| 274 | */ |
||
| 275 | public static function getAnnouncementsByTitle( |
||
| 276 | $title, |
||
| 277 | $courseId, |
||
| 278 | $sessionId = 0, |
||
| 279 | $visibility = 1 |
||
| 280 | ) { |
||
| 281 | $dql = "SELECT a |
||
| 282 | FROM ChamiloCourseBundle:CAnnouncement a |
||
| 283 | JOIN ChamiloCourseBundle:CItemProperty ip |
||
| 284 | WITH a.id = ip.ref AND a.cId = ip.course |
||
| 285 | WHERE |
||
| 286 | ip.tool = 'announcement' AND |
||
| 287 | a.cId = :course AND |
||
| 288 | a.sessionId = :session AND |
||
| 289 | a.title like :title AND |
||
| 290 | ip.visibility = :visibility |
||
| 291 | ORDER BY a.displayOrder DESC"; |
||
| 292 | |||
| 293 | $qb = Database::getManager()->createQuery($dql); |
||
| 294 | $result = $qb->execute( |
||
| 295 | [ |
||
| 296 | 'course' => $courseId, |
||
| 297 | 'session' => $sessionId, |
||
| 298 | 'visibility' => $visibility, |
||
| 299 | 'title' => "%$title%", |
||
| 300 | ] |
||
| 301 | ); |
||
| 302 | |||
| 303 | return $result; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param int $announcementId |
||
| 308 | * @param int $courseId |
||
| 309 | * @param int $userId |
||
| 310 | * |
||
| 311 | * @return array |
||
| 312 | */ |
||
| 313 | public static function getAnnouncementInfoById( |
||
| 378 | ]; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Displays one specific announcement. |
||
| 383 | * |
||
| 384 | * @param int $id, the id of the announcement you want to display |
||
| 385 | * |
||
| 386 | * @return string |
||
| 387 | */ |
||
| 388 | public static function displayAnnouncement($id) |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @param array $courseInfo |
||
| 501 | * |
||
| 502 | * @return int |
||
| 503 | */ |
||
| 504 | public static function getLastAnnouncementOrder($courseInfo) |
||
| 505 | { |
||
| 506 | if (empty($courseInfo)) { |
||
| 507 | return 0; |
||
| 508 | } |
||
| 509 | |||
| 510 | if (!isset($courseInfo['real_id'])) { |
||
| 511 | return false; |
||
| 512 | } |
||
| 513 | |||
| 514 | $courseId = $courseInfo['real_id']; |
||
| 515 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 516 | $sql = "SELECT MAX(display_order) |
||
| 517 | FROM $table |
||
| 518 | WHERE c_id = $courseId "; |
||
| 519 | $result = Database::query($sql); |
||
| 520 | |||
| 521 | $order = 0; |
||
| 522 | if (Database::num_rows($result)) { |
||
| 523 | $row = Database::fetch_array($result); |
||
| 524 | $order = (int) $row[0] + 1; |
||
| 525 | } |
||
| 526 | |||
| 527 | return $order; |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Store an announcement in the database (including its attached file if any). |
||
| 532 | * |
||
| 533 | * @param array $courseInfo |
||
| 534 | * @param int $sessionId |
||
| 535 | * @param string $title Announcement title (pure text) |
||
| 536 | * @param string $newContent Content of the announcement (can be HTML) |
||
| 537 | * @param array $sentTo Array of users and groups to send the announcement to |
||
| 538 | * @param array $file uploaded file $_FILES |
||
| 539 | * @param string $file_comment Comment describing the attachment |
||
| 540 | * @param string $end_date |
||
| 541 | * @param bool $sendToUsersInSession |
||
| 542 | * @param int $authorId |
||
| 543 | * |
||
| 544 | * @return int false on failure, ID of the announcement on success |
||
| 545 | */ |
||
| 546 | public static function add_announcement( |
||
| 547 | $courseInfo, |
||
| 548 | $sessionId, |
||
| 549 | $title, |
||
| 550 | $newContent, |
||
| 551 | $sentTo, |
||
| 552 | $file = [], |
||
| 553 | $file_comment = null, |
||
| 554 | $end_date = null, |
||
| 555 | $sendToUsersInSession = false, |
||
| 556 | $authorId = 0 |
||
| 557 | ) { |
||
| 558 | if (empty($courseInfo)) { |
||
| 559 | return false; |
||
| 560 | } |
||
| 561 | |||
| 562 | if (!isset($courseInfo['real_id'])) { |
||
| 563 | return false; |
||
| 564 | } |
||
| 565 | |||
| 566 | $courseId = $courseInfo['real_id']; |
||
| 567 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 568 | |||
| 569 | $authorId = empty($authorId) ? api_get_user_id() : $authorId; |
||
| 570 | |||
| 571 | if (empty($end_date)) { |
||
| 572 | $end_date = api_get_utc_datetime(); |
||
| 573 | } |
||
| 574 | |||
| 575 | $order = self::getLastAnnouncementOrder($courseInfo); |
||
| 576 | |||
| 577 | // store in the table announcement |
||
| 578 | $params = [ |
||
| 579 | 'c_id' => $courseId, |
||
| 580 | 'content' => $newContent, |
||
| 581 | 'title' => $title, |
||
| 582 | 'end_date' => $end_date, |
||
| 583 | 'display_order' => $order, |
||
| 584 | 'session_id' => (int) $sessionId, |
||
| 585 | ]; |
||
| 586 | |||
| 587 | $last_id = Database::insert($tbl_announcement, $params); |
||
| 588 | |||
| 589 | if (empty($last_id)) { |
||
| 590 | return false; |
||
| 591 | } else { |
||
| 592 | $sql = "UPDATE $tbl_announcement SET id = iid WHERE iid = $last_id"; |
||
| 593 | Database::query($sql); |
||
| 594 | |||
| 595 | if (!empty($file)) { |
||
| 596 | self::add_announcement_attachment_file( |
||
| 597 | $last_id, |
||
| 598 | $file_comment, |
||
| 599 | $_FILES['user_upload'] |
||
| 600 | ); |
||
| 601 | } |
||
| 602 | |||
| 603 | // store in item_property (first the groups, then the users |
||
| 604 | if (empty($sentTo) || |
||
| 605 | (!empty($sentTo) && isset($sentTo[0]) && $sentTo[0] == 'everyone') |
||
| 606 | ) { |
||
| 607 | // The message is sent to EVERYONE, so we set the group to 0 |
||
| 608 | api_item_property_update( |
||
| 609 | $courseInfo, |
||
| 610 | TOOL_ANNOUNCEMENT, |
||
| 611 | $last_id, |
||
| 612 | 'AnnouncementAdded', |
||
| 613 | $authorId, |
||
| 614 | '0', |
||
| 615 | null, |
||
| 616 | null, |
||
| 617 | null, |
||
| 618 | $sessionId |
||
| 619 | ); |
||
| 620 | } else { |
||
| 621 | $send_to = CourseManager::separateUsersGroups($sentTo); |
||
| 622 | $batchSize = 20; |
||
| 623 | $em = Database::getManager(); |
||
| 624 | // Storing the selected groups |
||
| 625 | if (is_array($send_to['groups']) && |
||
| 626 | !empty($send_to['groups']) |
||
| 627 | ) { |
||
| 628 | $counter = 1; |
||
| 629 | foreach ($send_to['groups'] as $group) { |
||
| 630 | $groupInfo = GroupManager::get_group_properties($group); |
||
| 631 | api_item_property_update( |
||
| 632 | $courseInfo, |
||
| 633 | TOOL_ANNOUNCEMENT, |
||
| 634 | $last_id, |
||
| 635 | 'AnnouncementAdded', |
||
| 636 | $authorId, |
||
| 637 | $groupInfo |
||
| 638 | ); |
||
| 639 | |||
| 640 | if (($counter % $batchSize) === 0) { |
||
| 641 | $em->flush(); |
||
| 642 | $em->clear(); |
||
| 643 | } |
||
| 644 | $counter++; |
||
| 645 | } |
||
| 646 | } |
||
| 647 | |||
| 648 | // Storing the selected users |
||
| 649 | if (is_array($send_to['users'])) { |
||
| 650 | $counter = 1; |
||
| 651 | foreach ($send_to['users'] as $user) { |
||
| 652 | api_item_property_update( |
||
| 653 | $courseInfo, |
||
| 654 | TOOL_ANNOUNCEMENT, |
||
| 655 | $last_id, |
||
| 656 | 'AnnouncementAdded', |
||
| 657 | $authorId, |
||
| 658 | '', |
||
| 659 | $user |
||
| 660 | ); |
||
| 661 | |||
| 662 | if (($counter % $batchSize) === 0) { |
||
| 663 | $em->flush(); |
||
| 664 | $em->clear(); |
||
| 665 | } |
||
| 666 | $counter++; |
||
| 667 | } |
||
| 668 | } |
||
| 669 | } |
||
| 670 | |||
| 671 | if ($sendToUsersInSession) { |
||
| 672 | self::addAnnouncementToAllUsersInSessions($last_id); |
||
| 673 | } |
||
| 674 | |||
| 675 | return $last_id; |
||
| 676 | } |
||
| 677 | } |
||
| 678 | |||
| 679 | /** |
||
| 680 | * @param $title |
||
| 681 | * @param $newContent |
||
| 682 | * @param $to |
||
| 683 | * @param $to_users |
||
| 684 | * @param array $file |
||
| 685 | * @param string $file_comment |
||
| 686 | * @param bool $sendToUsersInSession |
||
| 687 | * |
||
| 688 | * @return bool|int |
||
| 689 | */ |
||
| 690 | public static function add_group_announcement( |
||
| 783 | } |
||
| 784 | |||
| 785 | /** |
||
| 786 | * This function stores the announcement item in the announcement table |
||
| 787 | * and updates the item_property table. |
||
| 788 | * |
||
| 789 | * @param int $id id of the announcement |
||
| 790 | * @param string $title |
||
| 791 | * @param string $newContent |
||
| 792 | * @param array $to users that will receive the announcement |
||
| 793 | * @param mixed $file attachment |
||
| 794 | * @param string $file_comment file comment |
||
| 795 | * @param bool $sendToUsersInSession |
||
| 796 | */ |
||
| 797 | public static function edit_announcement( |
||
| 798 | $id, |
||
| 799 | $title, |
||
| 800 | $newContent, |
||
| 801 | $to, |
||
| 802 | $file = [], |
||
| 803 | $file_comment = '', |
||
| 804 | $sendToUsersInSession = false |
||
| 805 | ) { |
||
| 806 | $courseInfo = api_get_course_info(); |
||
| 807 | $courseId = api_get_course_int_id(); |
||
| 808 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 809 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 810 | $id = intval($id); |
||
| 811 | |||
| 812 | $params = [ |
||
| 813 | 'title' => $title, |
||
| 814 | 'content' => $newContent, |
||
| 815 | ]; |
||
| 816 | |||
| 817 | Database::update( |
||
| 818 | $table, |
||
| 819 | $params, |
||
| 820 | ['c_id = ? AND id = ?' => [$courseId, $id]] |
||
| 821 | ); |
||
| 822 | |||
| 823 | // save attachment file |
||
| 824 | $row_attach = self::get_attachment($id); |
||
| 825 | |||
| 826 | $id_attach = 0; |
||
| 827 | if ($row_attach) { |
||
| 828 | $id_attach = intval($row_attach['id']); |
||
| 829 | } |
||
| 830 | |||
| 831 | if (!empty($file)) { |
||
| 832 | if (empty($id_attach)) { |
||
| 833 | self::add_announcement_attachment_file( |
||
| 834 | $id, |
||
| 835 | $file_comment, |
||
| 836 | $file |
||
| 837 | ); |
||
| 838 | } else { |
||
| 839 | self::edit_announcement_attachment_file( |
||
| 840 | $id_attach, |
||
| 841 | $file, |
||
| 842 | $file_comment |
||
| 843 | ); |
||
| 844 | } |
||
| 845 | } |
||
| 846 | |||
| 847 | // We remove everything from item_property for this |
||
| 848 | $sql = "DELETE FROM $tbl_item_property |
||
| 849 | WHERE c_id = $courseId AND ref='$id' AND tool='announcement'"; |
||
| 850 | Database::query($sql); |
||
| 851 | |||
| 852 | if ($sendToUsersInSession) { |
||
| 853 | self::addAnnouncementToAllUsersInSessions($id); |
||
| 854 | } |
||
| 855 | |||
| 856 | // store in item_property (first the groups, then the users |
||
| 857 | if (!empty($to)) { |
||
| 858 | // !is_null($to): when no user is selected we send it to everyone |
||
| 859 | $send_to = CourseManager::separateUsersGroups($to); |
||
| 860 | |||
| 861 | // storing the selected groups |
||
| 862 | if (is_array($send_to['groups'])) { |
||
| 863 | foreach ($send_to['groups'] as $group) { |
||
| 864 | $groupInfo = GroupManager::get_group_properties($group); |
||
| 865 | if (empty($groupInfo)) { |
||
| 866 | // Probably the group id and iid are different try checking the iid |
||
| 867 | $groupInfo = GroupManager::get_group_properties($group, true); |
||
| 868 | } |
||
| 869 | if ($groupInfo) { |
||
| 870 | api_item_property_update( |
||
| 871 | $courseInfo, |
||
| 872 | TOOL_ANNOUNCEMENT, |
||
| 873 | $id, |
||
| 874 | 'AnnouncementUpdated', |
||
| 875 | api_get_user_id(), |
||
| 876 | $groupInfo |
||
| 877 | ); |
||
| 878 | } |
||
| 879 | } |
||
| 880 | } |
||
| 881 | |||
| 882 | // storing the selected users |
||
| 883 | if (is_array($send_to['users'])) { |
||
| 884 | foreach ($send_to['users'] as $user) { |
||
| 885 | api_item_property_update( |
||
| 886 | $courseInfo, |
||
| 887 | TOOL_ANNOUNCEMENT, |
||
| 888 | $id, |
||
| 889 | 'AnnouncementUpdated', |
||
| 890 | api_get_user_id(), |
||
| 891 | 0, |
||
| 892 | $user |
||
| 893 | ); |
||
| 894 | } |
||
| 895 | } |
||
| 896 | |||
| 897 | // Send to everyone |
||
| 898 | if (isset($to[0]) && $to[0] === 'everyone') { |
||
| 899 | api_item_property_update( |
||
| 900 | $courseInfo, |
||
| 901 | TOOL_ANNOUNCEMENT, |
||
| 902 | $id, |
||
| 903 | 'AnnouncementUpdated', |
||
| 904 | api_get_user_id(), |
||
| 905 | 0 |
||
| 906 | ); |
||
| 907 | } |
||
| 908 | } else { |
||
| 909 | // the message is sent to everyone, so we set the group to 0 |
||
| 910 | api_item_property_update( |
||
| 911 | $courseInfo, |
||
| 912 | TOOL_ANNOUNCEMENT, |
||
| 913 | $id, |
||
| 914 | 'AnnouncementUpdated', |
||
| 915 | api_get_user_id(), |
||
| 916 | 0 |
||
| 917 | ); |
||
| 918 | } |
||
| 919 | } |
||
| 920 | |||
| 921 | /** |
||
| 922 | * @param int $announcementId |
||
| 923 | */ |
||
| 924 | public static function addAnnouncementToAllUsersInSessions($announcementId) |
||
| 925 | { |
||
| 926 | $courseCode = api_get_course_id(); |
||
| 927 | $courseInfo = api_get_course_info(); |
||
| 928 | $sessionList = SessionManager::get_session_by_course(api_get_course_int_id()); |
||
| 929 | |||
| 930 | if (!empty($sessionList)) { |
||
| 931 | foreach ($sessionList as $sessionInfo) { |
||
| 932 | $sessionId = $sessionInfo['id']; |
||
| 933 | $userList = CourseManager::get_user_list_from_course_code( |
||
| 934 | $courseCode, |
||
| 935 | $sessionId |
||
| 936 | ); |
||
| 937 | |||
| 938 | if (!empty($userList)) { |
||
| 939 | foreach ($userList as $user) { |
||
| 940 | api_item_property_update( |
||
| 941 | $courseInfo, |
||
| 942 | TOOL_ANNOUNCEMENT, |
||
| 943 | $announcementId, |
||
| 944 | "AnnouncementUpdated", |
||
| 945 | api_get_user_id(), |
||
| 946 | 0, |
||
| 947 | $user['user_id'], |
||
| 948 | 0, |
||
| 949 | 0, |
||
| 950 | $sessionId |
||
| 951 | ); |
||
| 952 | } |
||
| 953 | } |
||
| 954 | } |
||
| 955 | } |
||
| 956 | } |
||
| 957 | |||
| 958 | /** |
||
| 959 | * @param int $insert_id |
||
| 960 | * |
||
| 961 | * @return bool |
||
| 962 | */ |
||
| 963 | public static function update_mail_sent($insert_id) |
||
| 964 | { |
||
| 965 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 966 | if ($insert_id != strval(intval($insert_id))) { |
||
| 967 | return false; |
||
| 968 | } |
||
| 969 | $insert_id = intval($insert_id); |
||
| 970 | $courseId = api_get_course_int_id(); |
||
| 971 | // store the modifications in the table tbl_annoucement |
||
| 972 | $sql = "UPDATE $table SET email_sent='1' |
||
| 973 | WHERE c_id = $courseId AND id = $insert_id"; |
||
| 974 | Database::query($sql); |
||
| 975 | } |
||
| 976 | |||
| 977 | /** |
||
| 978 | * Gets all announcements from a user by course. |
||
| 979 | * |
||
| 980 | * @param string course db |
||
| 981 | * @param int user id |
||
| 982 | * |
||
| 983 | * @return array html with the content and count of announcements or false otherwise |
||
| 984 | */ |
||
| 985 | public static function get_all_annoucement_by_user_course($course_code, $user_id) |
||
| 986 | { |
||
| 987 | $course_info = api_get_course_info($course_code); |
||
| 988 | $courseId = $course_info['real_id']; |
||
| 989 | |||
| 990 | if (empty($user_id)) { |
||
| 991 | return false; |
||
| 992 | } |
||
| 993 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 994 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 995 | if (!empty($user_id) && is_numeric($user_id)) { |
||
| 996 | $user_id = (int) $user_id; |
||
| 997 | $sql = "SELECT DISTINCT |
||
| 998 | announcement.title, |
||
| 999 | announcement.content, |
||
| 1000 | display_order |
||
| 1001 | FROM $tbl_announcement announcement |
||
| 1002 | INNER JOIN $tbl_item_property ip |
||
| 1003 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
| 1004 | WHERE |
||
| 1005 | announcement.c_id = $courseId AND |
||
| 1006 | ip.c_id = $courseId AND |
||
| 1007 | ip.tool='announcement' AND |
||
| 1008 | ( |
||
| 1009 | ip.insert_user_id='$user_id' AND |
||
| 1010 | (ip.to_group_id='0' OR ip.to_group_id IS NULL) |
||
| 1011 | ) |
||
| 1012 | AND ip.visibility='1' |
||
| 1013 | AND announcement.session_id = 0 |
||
| 1014 | ORDER BY display_order DESC"; |
||
| 1015 | $rs = Database::query($sql); |
||
| 1016 | $num_rows = Database::num_rows($rs); |
||
| 1017 | $content = ''; |
||
| 1018 | $i = 0; |
||
| 1019 | $result = []; |
||
| 1020 | if ($num_rows > 0) { |
||
| 1021 | while ($myrow = Database::fetch_array($rs)) { |
||
| 1022 | $content .= '<strong>'.$myrow['title'].'</strong><br /><br />'; |
||
| 1023 | $content .= $myrow['content']; |
||
| 1024 | $i++; |
||
| 1025 | } |
||
| 1026 | $result['content'] = $content; |
||
| 1027 | $result['count'] = $i; |
||
| 1028 | |||
| 1029 | return $result; |
||
| 1030 | } |
||
| 1031 | |||
| 1032 | return false; |
||
| 1033 | } |
||
| 1034 | |||
| 1035 | return false; |
||
| 1036 | } |
||
| 1037 | |||
| 1038 | /** |
||
| 1039 | * Returns announcement info from its id. |
||
| 1040 | * |
||
| 1041 | * @param int $courseId |
||
| 1042 | * @param int $id |
||
| 1043 | * |
||
| 1044 | * @return array |
||
| 1045 | */ |
||
| 1046 | public static function get_by_id($courseId, $id) |
||
| 1047 | { |
||
| 1048 | $id = intval($id); |
||
| 1049 | $courseId = $courseId ? intval($courseId) : api_get_course_int_id(); |
||
| 1050 | |||
| 1051 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 1052 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 1053 | |||
| 1054 | $sql = "SELECT DISTINCT |
||
| 1055 | announcement.id, |
||
| 1056 | announcement.title, |
||
| 1057 | announcement.content, |
||
| 1058 | ip.to_group_id |
||
| 1059 | FROM $tbl_announcement announcement |
||
| 1060 | INNER JOIN $tbl_item_property ip |
||
| 1061 | ON |
||
| 1062 | announcement.id = ip.ref AND |
||
| 1063 | announcement.c_id = ip.c_id |
||
| 1064 | WHERE |
||
| 1065 | announcement.c_id = $courseId AND |
||
| 1066 | ip.tool='announcement' AND |
||
| 1067 | announcement.id = $id |
||
| 1068 | "; |
||
| 1069 | $result = Database::query($sql); |
||
| 1070 | if (Database::num_rows($result)) { |
||
| 1071 | return Database::fetch_array($result); |
||
| 1072 | } |
||
| 1073 | |||
| 1074 | return []; |
||
| 1075 | } |
||
| 1076 | |||
| 1077 | /** |
||
| 1078 | * this function gets all the groups of the course, |
||
| 1079 | * not including linked courses. |
||
| 1080 | */ |
||
| 1081 | public static function get_course_groups() |
||
| 1082 | { |
||
| 1083 | $session_id = api_get_session_id(); |
||
| 1084 | if ($session_id != 0) { |
||
| 1085 | $new_group_list = CourseManager::get_group_list_of_course( |
||
| 1086 | api_get_course_id(), |
||
| 1087 | $session_id, |
||
| 1088 | 1 |
||
| 1089 | ); |
||
| 1090 | } else { |
||
| 1091 | $new_group_list = CourseManager::get_group_list_of_course( |
||
| 1092 | api_get_course_id(), |
||
| 1093 | 0, |
||
| 1094 | 1 |
||
| 1095 | ); |
||
| 1096 | } |
||
| 1097 | |||
| 1098 | return $new_group_list; |
||
| 1099 | } |
||
| 1100 | |||
| 1101 | /** |
||
| 1102 | * This tools loads all the users and all the groups who have received |
||
| 1103 | * a specific item (in this case an announcement item). |
||
| 1104 | * |
||
| 1105 | * @param string $tool |
||
| 1106 | * @param int $id |
||
| 1107 | * |
||
| 1108 | * @return array |
||
| 1109 | */ |
||
| 1110 | public static function load_edit_users($tool, $id) |
||
| 1111 | { |
||
| 1112 | $table = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 1113 | $tool = Database::escape_string($tool); |
||
| 1114 | $id = (int) $id; |
||
| 1115 | $courseId = api_get_course_int_id(); |
||
| 1116 | |||
| 1117 | $sql = "SELECT to_user_id, to_group_id FROM $table |
||
| 1118 | WHERE c_id = $courseId AND tool='$tool' AND ref = $id"; |
||
| 1119 | $result = Database::query($sql); |
||
| 1120 | $to = []; |
||
| 1121 | while ($row = Database::fetch_array($result)) { |
||
| 1122 | // This is the iid of c_group_info |
||
| 1123 | $toGroup = $row['to_group_id']; |
||
| 1124 | switch ($toGroup) { |
||
| 1125 | // it was send to one specific user |
||
| 1126 | case null: |
||
| 1127 | $to[] = "USER:".$row['to_user_id']; |
||
| 1128 | break; |
||
| 1129 | // it was sent to everyone |
||
| 1130 | case 0: |
||
| 1131 | return 'everyone'; |
||
| 1132 | break; |
||
|
|
|||
| 1133 | default: |
||
| 1134 | $to[] = "GROUP:".$toGroup; |
||
| 1135 | } |
||
| 1136 | } |
||
| 1137 | |||
| 1138 | return $to; |
||
| 1139 | } |
||
| 1140 | |||
| 1141 | /** |
||
| 1142 | * constructs the form to display all the groups and users the message has been sent to. |
||
| 1143 | * |
||
| 1144 | * @param array $sent_to_array |
||
| 1145 | * input: |
||
| 1146 | * $sent_to_array is a 2 dimensional array containing the groups and the users |
||
| 1147 | * the first level is a distinction between groups and users: |
||
| 1148 | * $sent_to_array['groups'] * and $sent_to_array['users'] |
||
| 1149 | * $sent_to_array['groups'] (resp. $sent_to_array['users']) is also an array |
||
| 1150 | * containing all the id's of the groups (resp. users) who have received this message. |
||
| 1151 | * |
||
| 1152 | * @return string |
||
| 1153 | * |
||
| 1154 | * @author Patrick Cool <patrick.cool@> |
||
| 1155 | */ |
||
| 1156 | public static function sent_to_form($sent_to_array) |
||
| 1157 | { |
||
| 1158 | // we find all the names of the groups |
||
| 1159 | $group_names = self::get_course_groups(); |
||
| 1160 | |||
| 1161 | // we count the number of users and the number of groups |
||
| 1162 | if (isset($sent_to_array['users'])) { |
||
| 1163 | $number_users = count($sent_to_array['users']); |
||
| 1164 | } else { |
||
| 1165 | $number_users = 0; |
||
| 1166 | } |
||
| 1167 | if (isset($sent_to_array['groups'])) { |
||
| 1168 | $number_groups = count($sent_to_array['groups']); |
||
| 1169 | } else { |
||
| 1170 | $number_groups = 0; |
||
| 1171 | } |
||
| 1172 | $total_numbers = $number_users + $number_groups; |
||
| 1173 | |||
| 1174 | // starting the form if there is more than one user/group |
||
| 1175 | $output = []; |
||
| 1176 | if ($total_numbers > 1) { |
||
| 1177 | // outputting the name of the groups |
||
| 1178 | if (is_array($sent_to_array['groups'])) { |
||
| 1179 | foreach ($sent_to_array['groups'] as $group_id) { |
||
| 1180 | $output[] = $group_names[$group_id]['name']; |
||
| 1181 | } |
||
| 1182 | } |
||
| 1183 | |||
| 1184 | if (isset($sent_to_array['users'])) { |
||
| 1185 | if (is_array($sent_to_array['users'])) { |
||
| 1186 | foreach ($sent_to_array['users'] as $user_id) { |
||
| 1187 | $user_info = api_get_user_info($user_id); |
||
| 1188 | $output[] = $user_info['complete_name_with_username']; |
||
| 1189 | } |
||
| 1190 | } |
||
| 1191 | } |
||
| 1192 | } else { |
||
| 1193 | // there is only one user/group |
||
| 1194 | if (isset($sent_to_array['users']) and is_array($sent_to_array['users'])) { |
||
| 1195 | $user_info = api_get_user_info($sent_to_array['users'][0]); |
||
| 1196 | $output[] = api_get_person_name($user_info['firstname'], $user_info['lastname']); |
||
| 1197 | } |
||
| 1198 | if (isset($sent_to_array['groups']) and |
||
| 1199 | is_array($sent_to_array['groups']) and |
||
| 1200 | isset($sent_to_array['groups'][0]) and |
||
| 1201 | $sent_to_array['groups'][0] !== 0 |
||
| 1202 | ) { |
||
| 1203 | $group_id = $sent_to_array['groups'][0]; |
||
| 1204 | $output[] = " ".$group_names[$group_id]['name']; |
||
| 1205 | } |
||
| 1206 | if (empty($sent_to_array['groups']) and empty($sent_to_array['users'])) { |
||
| 1207 | $output[] = " ".get_lang('Everybody'); |
||
| 1208 | } |
||
| 1209 | } |
||
| 1210 | |||
| 1211 | if (!empty($output)) { |
||
| 1212 | $output = array_filter($output); |
||
| 1213 | if (count($output) > 0) { |
||
| 1214 | $output = implode(', ', $output); |
||
| 1215 | } |
||
| 1216 | |||
| 1217 | return $output; |
||
| 1218 | } |
||
| 1219 | } |
||
| 1220 | |||
| 1221 | /** |
||
| 1222 | * Returns all the users and all the groups a specific announcement item |
||
| 1223 | * has been sent to. |
||
| 1224 | * |
||
| 1225 | * @param string The tool (announcement, agenda, ...) |
||
| 1226 | * @param int ID of the element of the corresponding type |
||
| 1227 | * |
||
| 1228 | * @return array Array of users and groups to whom the element has been sent |
||
| 1229 | */ |
||
| 1230 | public static function sent_to($tool, $id) |
||
| 1231 | { |
||
| 1232 | $table = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 1233 | $tool = Database::escape_string($tool); |
||
| 1234 | $id = (int) $id; |
||
| 1235 | |||
| 1236 | $sent_to_group = []; |
||
| 1237 | $sent_to = []; |
||
| 1238 | $courseId = api_get_course_int_id(); |
||
| 1239 | |||
| 1240 | $sql = "SELECT to_group_id, to_user_id |
||
| 1241 | FROM $table |
||
| 1242 | WHERE c_id = $courseId AND tool = '$tool' AND ref=".$id; |
||
| 1243 | $result = Database::query($sql); |
||
| 1244 | |||
| 1245 | while ($row = Database::fetch_array($result)) { |
||
| 1246 | // if to_user_id <> 0 then it is sent to a specific user |
||
| 1247 | if ($row['to_user_id'] != 0) { |
||
| 1248 | $sent_to_user[] = $row['to_user_id']; |
||
| 1249 | continue; |
||
| 1250 | } |
||
| 1251 | |||
| 1252 | // if to_group_id is null then it is sent to a specific user |
||
| 1253 | // if to_group_id = 0 then it is sent to everybody |
||
| 1254 | if ($row['to_group_id'] != 0) { |
||
| 1255 | $sent_to_group[] = $row['to_group_id']; |
||
| 1256 | } |
||
| 1257 | } |
||
| 1258 | |||
| 1259 | if (isset($sent_to_group)) { |
||
| 1260 | $sent_to['groups'] = $sent_to_group; |
||
| 1261 | } |
||
| 1262 | |||
| 1263 | if (isset($sent_to_user)) { |
||
| 1264 | $sent_to['users'] = $sent_to_user; |
||
| 1265 | } |
||
| 1266 | |||
| 1267 | return $sent_to; |
||
| 1268 | } |
||
| 1269 | |||
| 1270 | /** |
||
| 1271 | * Show a list with all the attachments according to the post's id. |
||
| 1272 | * |
||
| 1273 | * @param int $announcementId |
||
| 1274 | * |
||
| 1275 | * @return array with the post info |
||
| 1276 | * |
||
| 1277 | * @author Arthur Portugal |
||
| 1278 | * |
||
| 1279 | * @version November 2009, dokeos 1.8.6.2 |
||
| 1280 | */ |
||
| 1281 | public static function get_attachment($announcementId) |
||
| 1282 | { |
||
| 1283 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); |
||
| 1284 | $announcementId = intval($announcementId); |
||
| 1285 | $courseId = api_get_course_int_id(); |
||
| 1286 | $row = []; |
||
| 1287 | $sql = 'SELECT id, path, filename, comment |
||
| 1288 | FROM '.$table.' |
||
| 1289 | WHERE c_id = '.$courseId.' AND announcement_id = '.$announcementId; |
||
| 1290 | $result = Database::query($sql); |
||
| 1291 | if (Database::num_rows($result) != 0) { |
||
| 1292 | $row = Database::fetch_array($result, 'ASSOC'); |
||
| 1293 | } |
||
| 1294 | |||
| 1295 | return $row; |
||
| 1296 | } |
||
| 1297 | |||
| 1298 | /** |
||
| 1299 | * This function add a attachment file into announcement. |
||
| 1300 | * |
||
| 1301 | * @param int announcement id |
||
| 1302 | * @param string file comment |
||
| 1303 | * @param array uploaded file $_FILES |
||
| 1304 | * |
||
| 1305 | * @return int -1 if failed, 0 if unknown (should not happen), 1 if success |
||
| 1306 | */ |
||
| 1307 | public static function add_announcement_attachment_file( |
||
| 1308 | $announcement_id, |
||
| 1309 | $file_comment, |
||
| 1310 | $file |
||
| 1311 | ) { |
||
| 1312 | $courseInfo = api_get_course_info(); |
||
| 1313 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); |
||
| 1314 | $return = 0; |
||
| 1315 | $announcement_id = intval($announcement_id); |
||
| 1316 | $courseId = api_get_course_int_id(); |
||
| 1317 | |||
| 1318 | if (is_array($file) && $file['error'] == 0) { |
||
| 1319 | // TODO: This path is obsolete. The new document repository scheme should be kept in mind here. |
||
| 1320 | $courseDir = $courseInfo['path'].'/upload/announcements'; |
||
| 1321 | $sys_course_path = api_get_path(SYS_COURSE_PATH); |
||
| 1322 | $updir = $sys_course_path.$courseDir; |
||
| 1323 | |||
| 1324 | // Try to add an extension to the file if it hasn't one |
||
| 1325 | $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']); |
||
| 1326 | // user's file name |
||
| 1327 | $file_name = $file['name']; |
||
| 1328 | |||
| 1329 | if (!filter_extension($new_file_name)) { |
||
| 1330 | $return = -1; |
||
| 1331 | echo Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error'); |
||
| 1332 | } else { |
||
| 1333 | $new_file_name = uniqid(''); |
||
| 1334 | $new_path = $updir.'/'.$new_file_name; |
||
| 1335 | |||
| 1336 | // This file is copy here but its cleaned in api_mail_html in api.lib.php |
||
| 1337 | copy($file['tmp_name'], $new_path); |
||
| 1338 | |||
| 1339 | $params = [ |
||
| 1340 | 'c_id' => $courseId, |
||
| 1341 | 'filename' => $file_name, |
||
| 1342 | 'comment' => $file_comment, |
||
| 1343 | 'path' => $new_file_name, |
||
| 1344 | 'announcement_id' => $announcement_id, |
||
| 1345 | 'size' => intval($file['size']), |
||
| 1346 | ]; |
||
| 1347 | |||
| 1348 | $insertId = Database::insert($table, $params); |
||
| 1349 | if ($insertId) { |
||
| 1350 | $sql = "UPDATE $table SET id = iid |
||
| 1351 | WHERE iid = $insertId"; |
||
| 1352 | Database::query($sql); |
||
| 1353 | } |
||
| 1354 | |||
| 1355 | $return = 1; |
||
| 1356 | } |
||
| 1357 | } |
||
| 1358 | |||
| 1359 | return $return; |
||
| 1360 | } |
||
| 1361 | |||
| 1362 | /** |
||
| 1363 | * This function edit a attachment file into announcement. |
||
| 1364 | * |
||
| 1365 | * @param int attach id |
||
| 1366 | * @param array uploaded file $_FILES |
||
| 1367 | * @param string file comment |
||
| 1368 | * |
||
| 1369 | * @return int |
||
| 1370 | */ |
||
| 1371 | public static function edit_announcement_attachment_file( |
||
| 1429 | } |
||
| 1430 | |||
| 1431 | /** |
||
| 1432 | * This function delete a attachment file by id. |
||
| 1433 | * |
||
| 1434 | * @param int $id attachment file Id |
||
| 1435 | * |
||
| 1436 | * @return bool |
||
| 1437 | */ |
||
| 1438 | public static function delete_announcement_attachment_file($id) |
||
| 1439 | { |
||
| 1440 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); |
||
| 1441 | $id = intval($id); |
||
| 1442 | $courseId = api_get_course_int_id(); |
||
| 1443 | if (empty($courseId) || empty($id)) { |
||
| 1444 | return false; |
||
| 1445 | } |
||
| 1446 | $sql = "DELETE FROM $table |
||
| 1447 | WHERE c_id = $courseId AND id = $id"; |
||
| 1448 | Database::query($sql); |
||
| 1449 | |||
| 1450 | return true; |
||
| 1451 | } |
||
| 1452 | |||
| 1453 | /** |
||
| 1454 | * @param array $courseInfo |
||
| 1455 | * @param int $sessionId |
||
| 1456 | * @param int $announcementId |
||
| 1457 | * @param bool $sendToUsersInSession |
||
| 1458 | * @param bool $sendToDrhUsers |
||
| 1459 | * @param Monolog\Handler\HandlerInterface logger |
||
| 1460 | * @param int $senderId |
||
| 1461 | */ |
||
| 1462 | public static function sendEmail( |
||
| 1463 | $courseInfo, |
||
| 1464 | $sessionId, |
||
| 1465 | $announcementId, |
||
| 1466 | $sendToUsersInSession = false, |
||
| 1467 | $sendToDrhUsers = false, |
||
| 1468 | $logger = null, |
||
| 1469 | $senderId = 0 |
||
| 1470 | ) { |
||
| 1471 | $email = new AnnouncementEmail($courseInfo, $sessionId, $announcementId, $logger); |
||
| 1472 | $email->send($sendToUsersInSession, $sendToDrhUsers, $senderId); |
||
| 1473 | } |
||
| 1474 | |||
| 1475 | /** |
||
| 1476 | * @param $stok |
||
| 1477 | * @param $announcement_number |
||
| 1478 | * @param bool $getCount |
||
| 1479 | * @param null $start |
||
| 1480 | * @param null $limit |
||
| 1481 | * @param string $sidx |
||
| 1482 | * @param string $sord |
||
| 1483 | * @param string $titleToSearch |
||
| 1484 | * @param int $userIdToSearch |
||
| 1485 | * @param int $userId |
||
| 1486 | * @param int $courseId |
||
| 1487 | * @param int $sessionId |
||
| 1488 | * |
||
| 1489 | * @return array |
||
| 1490 | */ |
||
| 1491 | public static function getAnnouncements( |
||
| 1492 | $stok, |
||
| 1493 | $announcement_number, |
||
| 1494 | $getCount = false, |
||
| 1495 | $start = null, |
||
| 1496 | $limit = null, |
||
| 1497 | $sidx = '', |
||
| 1498 | $sord = '', |
||
| 1499 | $titleToSearch = '', |
||
| 1500 | $userIdToSearch = 0, |
||
| 1501 | $userId = 0, |
||
| 1502 | $courseId = 0, |
||
| 1503 | $sessionId = 0 |
||
| 1504 | ) { |
||
| 1505 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 1506 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 1507 | |||
| 1508 | $user_id = $userId ?: api_get_user_id(); |
||
| 1509 | $group_id = api_get_group_id(); |
||
| 1510 | $session_id = $sessionId ?: api_get_session_id(); |
||
| 1511 | if (empty($courseId)) { |
||
| 1512 | $courseInfo = api_get_course_info(); |
||
| 1513 | $courseId = $courseInfo['real_id']; |
||
| 1514 | } else { |
||
| 1515 | $courseId = (int) $courseId; |
||
| 1516 | $courseInfo = api_get_course_info_by_id($courseId); |
||
| 1517 | } |
||
| 1518 | |||
| 1519 | if (empty($courseInfo)) { |
||
| 1520 | return []; |
||
| 1521 | } |
||
| 1522 | |||
| 1523 | $condition_session = api_get_session_condition( |
||
| 1524 | $session_id, |
||
| 1525 | true, |
||
| 1526 | true, |
||
| 1527 | 'announcement.session_id' |
||
| 1528 | ); |
||
| 1529 | |||
| 1530 | $group_memberships = GroupManager::get_group_ids( |
||
| 1531 | $courseId, |
||
| 1532 | api_get_user_id() |
||
| 1533 | ); |
||
| 1534 | $allowUserEditSetting = api_get_course_setting('allow_user_edit_announcement'); |
||
| 1535 | |||
| 1536 | $select = ' DISTINCT |
||
| 1537 | announcement.*, |
||
| 1538 | ip.visibility, |
||
| 1539 | ip.to_group_id, |
||
| 1540 | ip.insert_user_id, |
||
| 1541 | ip.insert_date, |
||
| 1542 | ip.lastedit_date'; |
||
| 1543 | $groupBy = ' GROUP BY announcement.iid'; |
||
| 1544 | if ($getCount) { |
||
| 1545 | $groupBy = ''; |
||
| 1546 | $select = ' COUNT(DISTINCT announcement.iid) count'; |
||
| 1547 | } |
||
| 1548 | |||
| 1549 | $searchCondition = ''; |
||
| 1550 | if (!empty($titleToSearch)) { |
||
| 1551 | $titleToSearch = Database::escape_string($titleToSearch); |
||
| 1552 | $searchCondition .= " AND (title LIKE '%$titleToSearch%')"; |
||
| 1553 | } |
||
| 1554 | |||
| 1555 | if (!empty($userIdToSearch)) { |
||
| 1556 | $userIdToSearch = intval($userIdToSearch); |
||
| 1557 | $searchCondition .= " AND (ip.insert_user_id = $userIdToSearch)"; |
||
| 1558 | } |
||
| 1559 | |||
| 1560 | $allowOnlyGroup = api_get_configuration_value('hide_base_course_announcements_in_group'); |
||
| 1561 | $extraGroupCondition = ''; |
||
| 1562 | if ($allowOnlyGroup) { |
||
| 1563 | $extraGroupCondition = " AND ip.to_group_id = $group_id "; |
||
| 1564 | } |
||
| 1565 | |||
| 1566 | if (api_is_allowed_to_edit(false, true) || |
||
| 1567 | ($allowUserEditSetting && !api_is_anonymous()) |
||
| 1568 | ) { |
||
| 1569 | // A.1. you are a course admin with a USER filter |
||
| 1570 | // => see only the messages of this specific user + the messages of the group (s)he is member of. |
||
| 1571 | |||
| 1572 | //if (!empty($user_id)) { |
||
| 1573 | if (0) { |
||
| 1574 | if (is_array($group_memberships) && |
||
| 1575 | count($group_memberships) > 0 |
||
| 1576 | ) { |
||
| 1577 | $sql = "SELECT $select |
||
| 1578 | FROM $tbl_announcement announcement |
||
| 1579 | INNER JOIN $tbl_item_property ip |
||
| 1580 | ON (announcement.id = ip.ref AND ip.c_id = announcement.c_id) |
||
| 1581 | WHERE |
||
| 1582 | announcement.c_id = $courseId AND |
||
| 1583 | ip.c_id = $courseId AND |
||
| 1584 | ip.tool = 'announcement' AND |
||
| 1585 | ( |
||
| 1586 | ip.to_user_id = $user_id OR |
||
| 1587 | ip.to_group_id IS NULL OR |
||
| 1588 | ip.to_group_id IN (0, ".implode(", ", $group_memberships).") |
||
| 1589 | ) AND |
||
| 1590 | ip.visibility IN ('1', '0') |
||
| 1591 | $condition_session |
||
| 1592 | $searchCondition |
||
| 1593 | ORDER BY display_order DESC"; |
||
| 1594 | } else { |
||
| 1595 | $sql = "SELECT $select |
||
| 1596 | FROM $tbl_announcement announcement |
||
| 1597 | INNER JOIN $tbl_item_property ip |
||
| 1598 | ON (announcement.id = ip.ref AND ip.c_id = announcement.c_id) |
||
| 1599 | WHERE |
||
| 1600 | announcement.c_id = $courseId AND |
||
| 1601 | ip.c_id = $courseId AND |
||
| 1602 | ip.tool ='announcement' AND |
||
| 1603 | (ip.to_user_id = $user_id OR ip.to_group_id='0' OR ip.to_group_id IS NULL) AND |
||
| 1604 | ip.visibility IN ('1', '0') |
||
| 1605 | $condition_session |
||
| 1606 | $searchCondition |
||
| 1607 | ORDER BY display_order DESC"; |
||
| 1608 | } |
||
| 1609 | } elseif ($group_id != 0) { |
||
| 1610 | // A.2. you are a course admin with a GROUP filter |
||
| 1611 | // => see only the messages of this specific group |
||
| 1612 | $sql = "SELECT $select |
||
| 1613 | FROM $tbl_announcement announcement |
||
| 1614 | INNER JOIN $tbl_item_property ip |
||
| 1615 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
| 1616 | WHERE |
||
| 1617 | ip.tool='announcement' AND |
||
| 1618 | announcement.c_id = $courseId AND |
||
| 1619 | ip.c_id = $courseId AND |
||
| 1620 | ip.visibility<>'2' AND |
||
| 1621 | (ip.to_group_id = $group_id OR ip.to_group_id='0' OR ip.to_group_id IS NULL) |
||
| 1622 | $condition_session |
||
| 1623 | $searchCondition |
||
| 1624 | $extraGroupCondition |
||
| 1625 | $groupBy |
||
| 1626 | ORDER BY display_order DESC"; |
||
| 1627 | } else { |
||
| 1628 | // A.3 you are a course admin without any group or user filter |
||
| 1629 | // A.3.a you are a course admin without user or group filter but WITH studentview |
||
| 1630 | // => see all the messages of all the users and groups without editing possibilities |
||
| 1631 | if (isset($isStudentView) && $isStudentView == 'true') { |
||
| 1632 | $sql = "SELECT $select |
||
| 1633 | FROM $tbl_announcement announcement |
||
| 1634 | INNER JOIN $tbl_item_property ip |
||
| 1635 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
| 1636 | WHERE |
||
| 1637 | ip.tool='announcement' AND |
||
| 1638 | announcement.c_id = $courseId AND |
||
| 1639 | ip.c_id = $courseId AND |
||
| 1640 | ip.visibility='1' |
||
| 1641 | $condition_session |
||
| 1642 | $searchCondition |
||
| 1643 | $groupBy |
||
| 1644 | ORDER BY display_order DESC"; |
||
| 1645 | } else { |
||
| 1646 | // A.3.a you are a course admin without user or group filter and WTIHOUT studentview (= the normal course admin view) |
||
| 1647 | // => see all the messages of all the users and groups with editing possibilities |
||
| 1648 | $sql = "SELECT $select |
||
| 1649 | FROM $tbl_announcement announcement |
||
| 1650 | INNER JOIN $tbl_item_property ip |
||
| 1651 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
| 1652 | WHERE |
||
| 1653 | ip.tool = 'announcement' AND |
||
| 1654 | announcement.c_id = $courseId AND |
||
| 1655 | ip.c_id = $courseId AND |
||
| 1656 | (ip.visibility='0' OR ip.visibility='1') |
||
| 1657 | $condition_session |
||
| 1658 | $searchCondition |
||
| 1659 | $groupBy |
||
| 1660 | ORDER BY display_order DESC"; |
||
| 1661 | } |
||
| 1662 | } |
||
| 1663 | } else { |
||
| 1664 | // STUDENT |
||
| 1665 | if (is_array($group_memberships) && count($group_memberships) > 0) { |
||
| 1666 | if ($allowUserEditSetting && !api_is_anonymous()) { |
||
| 1667 | if ($group_id == 0) { |
||
| 1668 | // No group |
||
| 1669 | $cond_user_id = " AND ( |
||
| 1670 | ip.lastedit_user_id = '".$user_id."' OR ( |
||
| 1671 | (ip.to_user_id='$user_id' OR ip.to_user_id IS NULL) OR |
||
| 1672 | (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).")) |
||
| 1673 | ) |
||
| 1674 | ) "; |
||
| 1675 | } else { |
||
| 1676 | $cond_user_id = " AND ( |
||
| 1677 | ip.lastedit_user_id = '".$user_id."' OR ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".$group_id.") |
||
| 1678 | )"; |
||
| 1679 | $cond_user_id .= $extraGroupCondition; |
||
| 1680 | } |
||
| 1681 | } else { |
||
| 1682 | if ($group_id == 0) { |
||
| 1683 | $cond_user_id = " AND ( |
||
| 1684 | (ip.to_user_id='$user_id' OR ip.to_user_id IS NULL) AND (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).")) |
||
| 1685 | ) "; |
||
| 1686 | } else { |
||
| 1687 | $cond_user_id = " AND ( |
||
| 1688 | (ip.to_user_id='$user_id' OR ip.to_user_id IS NULL) AND (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".$group_id.")) |
||
| 1689 | )"; |
||
| 1690 | $cond_user_id .= $extraGroupCondition; |
||
| 1691 | } |
||
| 1692 | } |
||
| 1693 | |||
| 1694 | $sql = "SELECT $select |
||
| 1695 | FROM $tbl_announcement announcement INNER JOIN |
||
| 1696 | $tbl_item_property ip |
||
| 1697 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
| 1698 | WHERE |
||
| 1699 | announcement.c_id = $courseId AND |
||
| 1700 | ip.c_id = $courseId AND |
||
| 1701 | ip.tool='announcement' |
||
| 1702 | $cond_user_id |
||
| 1703 | $condition_session |
||
| 1704 | $searchCondition |
||
| 1705 | AND ip.visibility='1' |
||
| 1706 | $groupBy |
||
| 1707 | ORDER BY display_order DESC"; |
||
| 1708 | } else { |
||
| 1709 | if ($user_id) { |
||
| 1710 | if ($allowUserEditSetting && !api_is_anonymous()) { |
||
| 1711 | $cond_user_id = " AND ( |
||
| 1712 | ip.lastedit_user_id = '".api_get_user_id()."' OR |
||
| 1713 | ((ip.to_user_id='$user_id' OR ip.to_user_id IS NULL) AND (ip.to_group_id='0' OR ip.to_group_id IS NULL)) |
||
| 1714 | ) "; |
||
| 1715 | } else { |
||
| 1716 | $cond_user_id = " AND ((ip.to_user_id='$user_id' OR ip.to_user_id IS NULL) AND (ip.to_group_id='0' OR ip.to_group_id IS NULL) ) "; |
||
| 1717 | } |
||
| 1718 | |||
| 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 | announcement.c_id = $courseId AND |
||
| 1725 | ip.c_id = $courseId AND |
||
| 1726 | ip.tool='announcement' |
||
| 1727 | $cond_user_id |
||
| 1728 | $condition_session |
||
| 1729 | $searchCondition |
||
| 1730 | AND ip.visibility='1' |
||
| 1731 | AND announcement.session_id IN(0, ".$session_id.") |
||
| 1732 | $groupBy |
||
| 1733 | ORDER BY display_order DESC"; |
||
| 1734 | } else { |
||
| 1735 | if (($allowUserEditSetting && !api_is_anonymous())) { |
||
| 1736 | $cond_user_id = " AND ( |
||
| 1737 | ip.lastedit_user_id = '".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL |
||
| 1738 | )"; |
||
| 1739 | } else { |
||
| 1740 | $cond_user_id = " AND ip.to_group_id='0' OR ip.to_group_id IS NULL "; |
||
| 1741 | } |
||
| 1742 | |||
| 1743 | $sql = "SELECT $select |
||
| 1744 | FROM $tbl_announcement announcement |
||
| 1745 | INNER JOIN $tbl_item_property ip |
||
| 1746 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
| 1747 | WHERE |
||
| 1748 | announcement.c_id = $courseId AND |
||
| 1749 | ip.c_id = $courseId AND |
||
| 1750 | ip.tool='announcement' |
||
| 1751 | $cond_user_id |
||
| 1752 | $condition_session |
||
| 1753 | $searchCondition AND |
||
| 1754 | ip.visibility='1' AND |
||
| 1755 | announcement.session_id IN ( 0,".api_get_session_id().") |
||
| 1756 | $groupBy |
||
| 1757 | "; |
||
| 1758 | } |
||
| 1759 | } |
||
| 1760 | } |
||
| 1761 | |||
| 1762 | if (!is_null($start) && !is_null($limit)) { |
||
| 1763 | $start = intval($start); |
||
| 1764 | $limit = intval($limit); |
||
| 1765 | $sql .= " LIMIT $start, $limit"; |
||
| 1766 | } |
||
| 1767 | |||
| 1768 | $result = Database::query($sql); |
||
| 1769 | if ($getCount) { |
||
| 1770 | $result = Database::fetch_array($result, 'ASSOC'); |
||
| 1771 | |||
| 1772 | return $result['count']; |
||
| 1773 | } |
||
| 1774 | |||
| 1775 | $iterator = 1; |
||
| 1776 | $bottomAnnouncement = $announcement_number; |
||
| 1777 | $displayed = []; |
||
| 1778 | $results = []; |
||
| 1779 | $actionUrl = api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.api_get_cidreq(); |
||
| 1780 | $emailIcon = '<i class="fa fa-envelope-o" title="'.get_lang('AnnounceSentByEmail').'"></i>'; |
||
| 1781 | $attachmentIcon = '<i class="fa fa-paperclip" title="'.get_lang('Attachment').'"></i>'; |
||
| 1782 | |||
| 1783 | $editIcon = Display::return_icon( |
||
| 1784 | 'edit.png', |
||
| 1785 | get_lang('Edit'), |
||
| 1786 | '', |
||
| 1787 | ICON_SIZE_SMALL |
||
| 1788 | ); |
||
| 1789 | |||
| 1790 | $deleteIcon = Display::return_icon( |
||
| 1791 | 'delete.png', |
||
| 1792 | get_lang('Delete'), |
||
| 1793 | '', |
||
| 1794 | ICON_SIZE_SMALL |
||
| 1795 | ); |
||
| 1796 | |||
| 1797 | $isTutor = false; |
||
| 1798 | if (!empty($group_id)) { |
||
| 1799 | $groupInfo = GroupManager::get_group_properties(api_get_group_id()); |
||
| 1800 | //User has access in the group? |
||
| 1801 | $isTutor = GroupManager::is_tutor_of_group( |
||
| 1802 | api_get_user_id(), |
||
| 1803 | $groupInfo |
||
| 1804 | ); |
||
| 1805 | } |
||
| 1806 | |||
| 1807 | while ($row = Database::fetch_array($result, 'ASSOC')) { |
||
| 1808 | if (!in_array($row['id'], $displayed)) { |
||
| 1809 | $sent_to_icon = ''; |
||
| 1810 | // the email icon |
||
| 1811 | if ($row['email_sent'] == '1') { |
||
| 1812 | $sent_to_icon = ' '.$emailIcon; |
||
| 1813 | } |
||
| 1814 | $groupReference = ($row['to_group_id'] > 0) ? ' <span class="label label-info">'.get_lang('Group').'</span> ' : ''; |
||
| 1815 | $title = $row['title'].$groupReference.$sent_to_icon; |
||
| 1816 | $item_visibility = api_get_item_visibility( |
||
| 1817 | $courseInfo, |
||
| 1818 | TOOL_ANNOUNCEMENT, |
||
| 1819 | $row['id'], |
||
| 1820 | $session_id |
||
| 1821 | ); |
||
| 1822 | $row['visibility'] = $item_visibility; |
||
| 1823 | |||
| 1824 | // show attachment list |
||
| 1825 | $attachment_list = self::get_attachment($row['id']); |
||
| 1826 | |||
| 1827 | $attachment_icon = ''; |
||
| 1828 | if (count($attachment_list) > 0) { |
||
| 1829 | $attachment_icon = ' '.$attachmentIcon; |
||
| 1830 | } |
||
| 1831 | |||
| 1832 | /* TITLE */ |
||
| 1833 | $user_info = api_get_user_info($row['insert_user_id']); |
||
| 1834 | $username = sprintf(get_lang("LoginX"), $user_info['username']); |
||
| 1835 | |||
| 1836 | $username_span = Display::tag( |
||
| 1837 | 'span', |
||
| 1838 | $user_info['complete_name'], |
||
| 1839 | ['title' => $username] |
||
| 1840 | ); |
||
| 1841 | |||
| 1842 | $title = Display::url( |
||
| 1843 | $title.$attachment_icon, |
||
| 1844 | $actionUrl.'&action=view&id='.$row['id'] |
||
| 1845 | ); |
||
| 1846 | |||
| 1847 | // we can edit if : we are the teacher OR the element belongs to |
||
| 1848 | // the session we are coaching OR the option to allow users to edit is on |
||
| 1849 | if (api_is_allowed_to_edit(false, true) || |
||
| 1850 | (api_is_session_general_coach() && api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $row['id'])) || |
||
| 1851 | (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous()) || |
||
| 1852 | ($row['to_group_id'] == $group_id && $isTutor) |
||
| 1853 | ) { |
||
| 1854 | $modify_icons = "<a href=\"".$actionUrl."&action=modify&id=".$row['id']."\">".$editIcon."</a>"; |
||
| 1855 | if ($row['visibility'] == 1) { |
||
| 1856 | $image_visibility = "visible"; |
||
| 1857 | $alt_visibility = get_lang('Hide'); |
||
| 1858 | } else { |
||
| 1859 | $image_visibility = "invisible"; |
||
| 1860 | $alt_visibility = get_lang('Visible'); |
||
| 1861 | } |
||
| 1862 | $modify_icons .= "<a href=\"".$actionUrl."&action=showhide&id=".$row['id']."&sec_token=".$stok."\">". |
||
| 1863 | Display::return_icon($image_visibility.'.png', $alt_visibility, '', ICON_SIZE_SMALL)."</a>"; |
||
| 1864 | |||
| 1865 | // DISPLAY MOVE UP COMMAND only if it is not the top announcement |
||
| 1866 | if ($iterator != 1) { |
||
| 1867 | $modify_icons .= "<a href=\"".$actionUrl."&action=move&up=".$row["id"]."&sec_token=".$stok."\">". |
||
| 1868 | Display::return_icon('up.gif', get_lang('Up'))."</a>"; |
||
| 1869 | } else { |
||
| 1870 | $modify_icons .= Display::return_icon('up_na.gif', get_lang('Up')); |
||
| 1871 | } |
||
| 1872 | if ($iterator < $bottomAnnouncement) { |
||
| 1873 | $modify_icons .= "<a href=\"".$actionUrl."&action=move&down=".$row["id"]."&sec_token=".$stok."\">". |
||
| 1874 | Display::return_icon('down.gif', get_lang('Down'))."</a>"; |
||
| 1875 | } else { |
||
| 1876 | $modify_icons .= Display::return_icon('down_na.gif', get_lang('Down')); |
||
| 1877 | } |
||
| 1878 | if (api_is_allowed_to_edit(false, true)) { |
||
| 1879 | $modify_icons .= "<a href=\"".$actionUrl."&action=delete&id=".$row['id']."&sec_token=".$stok."\" onclick=\"javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, api_get_system_encoding()))."')) return false;\">". |
||
| 1880 | $deleteIcon."</a>"; |
||
| 1881 | } |
||
| 1882 | $iterator++; |
||
| 1883 | } else { |
||
| 1884 | $modify_icons = Display::url( |
||
| 1885 | Display::return_icon('default.png'), |
||
| 1886 | $actionUrl.'&action=view&id='.$row['id'] |
||
| 1887 | ); |
||
| 1888 | } |
||
| 1889 | |||
| 1890 | $announcement = [ |
||
| 1891 | 'id' => $row['id'], |
||
| 1892 | 'title' => $title, |
||
| 1893 | 'username' => $username_span, |
||
| 1894 | 'insert_date' => api_convert_and_format_date( |
||
| 1895 | $row['insert_date'], |
||
| 1896 | DATE_TIME_FORMAT_LONG |
||
| 1897 | ), |
||
| 1898 | 'lastedit_date' => api_convert_and_format_date( |
||
| 1899 | $row['lastedit_date'], |
||
| 1900 | DATE_TIME_FORMAT_LONG |
||
| 1901 | ), |
||
| 1902 | 'actions' => $modify_icons, |
||
| 1903 | ]; |
||
| 1904 | |||
| 1905 | $results[] = $announcement; |
||
| 1906 | } |
||
| 1907 | $displayed[] = $row['id']; |
||
| 1908 | } |
||
| 1909 | |||
| 1910 | return $results; |
||
| 1911 | } |
||
| 1912 | |||
| 1913 | /** |
||
| 1914 | * @return int |
||
| 1915 | */ |
||
| 1916 | public static function getNumberAnnouncements() |
||
| 2087 | } |
||
| 2088 | } |
||
| 2089 |
The
breakstatement is not necessary if it is preceded for example by areturnstatement:If you would like to keep this construct to be consistent with other
casestatements, you can safely mark this issue as a false-positive.