Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 13 | class AnnouncementManager |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Constructor |
||
| 17 | */ |
||
| 18 | public function __construct() |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @return array |
||
| 24 | */ |
||
| 25 | View Code Duplication | public static function get_tags() |
|
| 38 | |||
| 39 | /** |
||
| 40 | * @param int $userId |
||
| 41 | * @param string $content |
||
| 42 | * @param string $courseCode |
||
| 43 | * @param int $sessionId |
||
| 44 | * |
||
| 45 | * @return string |
||
| 46 | */ |
||
| 47 | public static function parse_content( |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Gets all announcements from a course |
||
| 89 | * @param array $course_info |
||
| 90 | * @param int $session_id |
||
| 91 | * @return array html with the content and count of announcements or false otherwise |
||
| 92 | */ |
||
| 93 | View Code Duplication | public static function get_all_annoucement_by_course($course_info, $session_id = 0) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * This functions switches the visibility a course resource |
||
| 130 | * using the visibility field in 'item_property' |
||
| 131 | * @param array $_course |
||
| 132 | * @param int $id ID of the element of the corresponding type |
||
| 133 | * @return bool False on failure, True on success |
||
| 134 | */ |
||
| 135 | public static function change_visibility_announcement($_course, $id) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Deletes an announcement |
||
| 167 | * @param array $_course the course array |
||
| 168 | * @param int $id the announcement id |
||
| 169 | */ |
||
| 170 | public static function delete_announcement($_course, $id) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Deletes all announcements by course |
||
| 183 | * @param array $_course the course array |
||
| 184 | */ |
||
| 185 | public static function delete_all_announcements($_course) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param string $title |
||
| 203 | * @param int $courseId |
||
| 204 | * @param int $sessionId |
||
| 205 | * @param int $visibility 1 or 0 |
||
| 206 | * |
||
| 207 | * @return mixed |
||
| 208 | */ |
||
| 209 | public static function getAnnouncementsByTitle($title, $courseId, $sessionId = 0, $visibility = 1) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param int $announcementId |
||
| 238 | * @param int $courseId |
||
| 239 | * @param int $userId |
||
| 240 | * |
||
| 241 | * @return array |
||
| 242 | */ |
||
| 243 | public static function getAnnouncementInfoById($announcementId, $courseId, $userId) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Displays one specific announcement |
||
| 310 | * @param int $id, the id of the announcement you want to display |
||
|
|
|||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public static function displayAnnouncement($id) |
||
| 315 | { |
||
| 316 | if ($id != strval(intval($id))) { |
||
| 317 | return null; |
||
| 318 | } |
||
| 319 | |||
| 320 | global $charset; |
||
| 321 | |||
| 322 | $html = ''; |
||
| 323 | $result = self::getAnnouncementInfoById( |
||
| 324 | $id, |
||
| 325 | api_get_course_int_id(), |
||
| 326 | api_get_user_id() |
||
| 327 | ); |
||
| 328 | /** @var CAnnouncement $announcement */ |
||
| 329 | $announcement = $result['announcement']; |
||
| 330 | /** @var CItemProperty $itemProperty */ |
||
| 331 | $itemProperty = $result['item_property']; |
||
| 332 | |||
| 333 | if (empty($announcement) || empty($itemProperty)) { |
||
| 334 | return ''; |
||
| 335 | } |
||
| 336 | |||
| 337 | $title = $announcement->getTitle(); |
||
| 338 | $content = $announcement->getContent(); |
||
| 339 | |||
| 340 | $html .= "<table height=\"100\" width=\"100%\" cellpadding=\"5\" cellspacing=\"0\" class=\"data_table\">"; |
||
| 341 | $html .= "<tr><td><h2>".$title."</h2></td></tr>"; |
||
| 342 | |||
| 343 | if (api_is_allowed_to_edit(false, true) || |
||
| 344 | (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous()) |
||
| 345 | ) { |
||
| 346 | $modify_icons = "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=modify&id=".$id."\">". |
||
| 347 | Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL)."</a>"; |
||
| 348 | |||
| 349 | View Code Duplication | if ($itemProperty->getVisibility() === 1) { |
|
| 350 | $image_visibility = 'visible'; |
||
| 351 | $alt_visibility = get_lang('Hide'); |
||
| 352 | } else { |
||
| 353 | $image_visibility = 'invisible'; |
||
| 354 | $alt_visibility = get_lang('Visible'); |
||
| 355 | } |
||
| 356 | global $stok; |
||
| 357 | |||
| 358 | $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=showhide&id=".$id."&sec_token=".$stok."\">". |
||
| 359 | Display::return_icon($image_visibility.'.png', $alt_visibility, '', ICON_SIZE_SMALL)."</a>"; |
||
| 360 | |||
| 361 | View Code Duplication | if (api_is_allowed_to_edit(false, true)) { |
|
| 362 | $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=delete&id=".$id."&sec_token=".$stok."\" onclick=\"javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset))."')) return false;\">". |
||
| 363 | Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL). |
||
| 364 | "</a>"; |
||
| 365 | } |
||
| 366 | $html .= "<tr><th style='text-align:right'>$modify_icons</th></tr>"; |
||
| 367 | } |
||
| 368 | |||
| 369 | $toUser = $itemProperty->getToUser(); |
||
| 370 | $toUserId = !empty($toUser) ? $toUser->getId() : 0; |
||
| 371 | |||
| 372 | $content = self::parse_content( |
||
| 373 | $toUserId, |
||
| 374 | $content, |
||
| 375 | api_get_course_id(), |
||
| 376 | api_get_session_id() |
||
| 377 | ); |
||
| 378 | |||
| 379 | $lastEdit = $itemProperty->getLasteditDate(); |
||
| 380 | |||
| 381 | $html .= "<tr><td>$content</td></tr>"; |
||
| 382 | $html .= "<tr><td class=\"announcements_datum\">".get_lang('LastUpdateDate')." : ". |
||
| 383 | Display::dateToStringAgoAndLongDate( |
||
| 384 | !empty($lastEdit) ? $lastEdit->format('Y-m-d h:i:s') : '' |
||
| 385 | )."</td></tr>"; |
||
| 386 | |||
| 387 | if ($itemProperty->getGroup() !== null) { |
||
| 388 | $sent_to_icon = Display::return_icon('group.gif', get_lang('AnnounceSentToUserSelection')); |
||
| 389 | } |
||
| 390 | |||
| 391 | if (api_is_allowed_to_edit(false, true)) { |
||
| 392 | $sent_to = self::sent_to('announcement', $id); |
||
| 393 | $sent_to_form = self::sent_to_form($sent_to); |
||
| 394 | $html .= Display::tag( |
||
| 395 | 'td', |
||
| 396 | get_lang('SentTo').': '.$sent_to_form, |
||
| 397 | array('class' => 'announcements_datum') |
||
| 398 | ); |
||
| 399 | } |
||
| 400 | $attachment_list = self::get_attachment($id); |
||
| 401 | |||
| 402 | if (count($attachment_list) > 0) { |
||
| 403 | $html .= "<tr><td>"; |
||
| 404 | $realname = $attachment_list['path']; |
||
| 405 | $user_filename = $attachment_list['filename']; |
||
| 406 | $full_file_name = 'download.php?'.api_get_cidreq().'&file='.$realname; |
||
| 407 | $html .= '<br/>'; |
||
| 408 | $html .= Display::return_icon('attachment.gif', get_lang('Attachment')); |
||
| 409 | $html .= '<a href="'.$full_file_name.' "> '.$user_filename.' </a>'; |
||
| 410 | $html .= ' - <span class="forum_attach_comment" >'.$attachment_list['comment'].'</span>'; |
||
| 411 | View Code Duplication | if (api_is_allowed_to_edit(false, true)) { |
|
| 412 | $html .= Display::url( |
||
| 413 | Display::return_icon('delete.png', get_lang('Delete'), '', 16), |
||
| 414 | api_get_self()."?".api_get_cidreq()."&action=delete_attachment&id_attach=".$attachment_list['id']."&sec_token=".$stok |
||
| 415 | ); |
||
| 416 | } |
||
| 417 | $html .= '</td></tr>'; |
||
| 418 | } |
||
| 419 | $html .= "</table>"; |
||
| 420 | |||
| 421 | return $html; |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @param array $courseInfo |
||
| 426 | * |
||
| 427 | * @return int |
||
| 428 | */ |
||
| 429 | View Code Duplication | public static function get_last_announcement_order($courseInfo) |
|
| 430 | { |
||
| 431 | if (empty($courseInfo)) { |
||
| 432 | return 0; |
||
| 433 | } |
||
| 434 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 435 | |||
| 436 | $course_id = $courseInfo['real_id']; |
||
| 437 | $sql = "SELECT MAX(display_order) |
||
| 438 | FROM $tbl_announcement |
||
| 439 | WHERE c_id = $course_id "; |
||
| 440 | $res_max = Database::query($sql); |
||
| 441 | |||
| 442 | $order = 0; |
||
| 443 | if (Database::num_rows($res_max)) { |
||
| 444 | $row_max = Database::fetch_array($res_max); |
||
| 445 | $order = intval($row_max[0]) + 1; |
||
| 446 | } |
||
| 447 | |||
| 448 | return $order; |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Store an announcement in the database (including its attached file if any) |
||
| 453 | * @param array $courseInfo |
||
| 454 | * @param int $sessionId |
||
| 455 | * @param string $title Announcement title (pure text) |
||
| 456 | * @param string $newContent Content of the announcement (can be HTML) |
||
| 457 | * @param array $sentTo Array of users and groups to send the announcement to |
||
| 458 | * @param array $file uploaded file $_FILES |
||
| 459 | * @param string $file_comment Comment describing the attachment |
||
| 460 | * @param string $end_date |
||
| 461 | * @param bool $sendToUsersInSession |
||
| 462 | * @param int $authorId |
||
| 463 | * |
||
| 464 | * @return int false on failure, ID of the announcement on success |
||
| 465 | */ |
||
| 466 | public static function add_announcement( |
||
| 467 | $courseInfo, |
||
| 468 | $sessionId, |
||
| 469 | $title, |
||
| 470 | $newContent, |
||
| 471 | $sentTo, |
||
| 472 | $file = array(), |
||
| 473 | $file_comment = null, |
||
| 474 | $end_date = null, |
||
| 475 | $sendToUsersInSession = false, |
||
| 476 | $authorId = 0 |
||
| 477 | ) { |
||
| 478 | if (empty($courseInfo)) { |
||
| 479 | return false; |
||
| 480 | } |
||
| 481 | |||
| 482 | $course_id = $courseInfo['real_id']; |
||
| 483 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 484 | |||
| 485 | $authorId = empty($authorId) ? api_get_user_id() : $authorId; |
||
| 486 | |||
| 487 | if (empty($end_date)) { |
||
| 488 | $end_date = api_get_utc_datetime(); |
||
| 489 | } |
||
| 490 | |||
| 491 | $order = self::get_last_announcement_order($courseInfo); |
||
| 492 | |||
| 493 | // store in the table announcement |
||
| 494 | $params = array( |
||
| 495 | 'c_id' => $course_id, |
||
| 496 | 'content' => $newContent, |
||
| 497 | 'title' => $title, |
||
| 498 | 'end_date' => $end_date, |
||
| 499 | 'display_order' => $order, |
||
| 500 | 'session_id' => (int) $sessionId |
||
| 501 | ); |
||
| 502 | |||
| 503 | $last_id = Database::insert($tbl_announcement, $params); |
||
| 504 | |||
| 505 | if (empty($last_id)) { |
||
| 506 | return false; |
||
| 507 | } else { |
||
| 508 | $sql = "UPDATE $tbl_announcement SET id = iid WHERE iid = $last_id"; |
||
| 509 | Database::query($sql); |
||
| 510 | |||
| 511 | if (!empty($file)) { |
||
| 512 | self::add_announcement_attachment_file( |
||
| 513 | $last_id, |
||
| 514 | $file_comment, |
||
| 515 | $_FILES['user_upload'] |
||
| 516 | ); |
||
| 517 | } |
||
| 518 | |||
| 519 | // store in item_property (first the groups, then the users |
||
| 520 | if (empty($sentTo) || (!empty($sentTo) && isset($sentTo[0]) && $sentTo[0] == 'everyone')) { |
||
| 521 | // The message is sent to EVERYONE, so we set the group to 0 |
||
| 522 | api_item_property_update( |
||
| 523 | $courseInfo, |
||
| 524 | TOOL_ANNOUNCEMENT, |
||
| 525 | $last_id, |
||
| 526 | 'AnnouncementAdded', |
||
| 527 | $authorId, |
||
| 528 | '0', |
||
| 529 | null, |
||
| 530 | null, |
||
| 531 | null, |
||
| 532 | $sessionId |
||
| 533 | ); |
||
| 534 | } else { |
||
| 535 | $send_to = CourseManager::separateUsersGroups($sentTo); |
||
| 536 | $batchSize = 20; |
||
| 537 | $em = Database::getManager(); |
||
| 538 | // Storing the selected groups |
||
| 539 | if (is_array($send_to['groups']) && !empty($send_to['groups'])) { |
||
| 540 | $counter = 1; |
||
| 541 | View Code Duplication | foreach ($send_to['groups'] as $group) { |
|
| 542 | $groupInfo = GroupManager::get_group_properties($group); |
||
| 543 | api_item_property_update( |
||
| 544 | $courseInfo, |
||
| 545 | TOOL_ANNOUNCEMENT, |
||
| 546 | $last_id, |
||
| 547 | 'AnnouncementAdded', |
||
| 548 | $authorId, |
||
| 549 | $groupInfo |
||
| 550 | ); |
||
| 551 | |||
| 552 | if (($counter % $batchSize) === 0) { |
||
| 553 | $em->flush(); |
||
| 554 | $em->clear(); |
||
| 555 | } |
||
| 556 | $counter++; |
||
| 557 | } |
||
| 558 | } |
||
| 559 | |||
| 560 | // Storing the selected users |
||
| 561 | if (is_array($send_to['users'])) { |
||
| 562 | $counter = 1; |
||
| 563 | View Code Duplication | foreach ($send_to['users'] as $user) { |
|
| 564 | api_item_property_update( |
||
| 565 | $courseInfo, |
||
| 566 | TOOL_ANNOUNCEMENT, |
||
| 567 | $last_id, |
||
| 568 | 'AnnouncementAdded', |
||
| 569 | $authorId, |
||
| 570 | '', |
||
| 571 | $user |
||
| 572 | ); |
||
| 573 | |||
| 574 | if (($counter % $batchSize) === 0) { |
||
| 575 | $em->flush(); |
||
| 576 | $em->clear(); |
||
| 577 | } |
||
| 578 | $counter++; |
||
| 579 | } |
||
| 580 | } |
||
| 581 | } |
||
| 582 | |||
| 583 | if ($sendToUsersInSession) { |
||
| 584 | self::addAnnouncementToAllUsersInSessions($last_id); |
||
| 585 | } |
||
| 586 | |||
| 587 | return $last_id; |
||
| 588 | } |
||
| 589 | } |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @param $title |
||
| 593 | * @param $newContent |
||
| 594 | * @param $to |
||
| 595 | * @param $to_users |
||
| 596 | * @param array $file |
||
| 597 | * @param string $file_comment |
||
| 598 | * @param bool $sendToUsersInSession |
||
| 599 | * |
||
| 600 | * @return bool|int |
||
| 601 | */ |
||
| 602 | public static function add_group_announcement( |
||
| 603 | $title, |
||
| 604 | $newContent, |
||
| 605 | $to, |
||
| 606 | $to_users, |
||
| 607 | $file = array(), |
||
| 608 | $file_comment = '', |
||
| 609 | $sendToUsersInSession = false |
||
| 610 | ) { |
||
| 611 | $_course = api_get_course_info(); |
||
| 612 | |||
| 613 | // Database definitions |
||
| 614 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 615 | $order = self::get_last_announcement_order($_course); |
||
| 616 | |||
| 617 | $now = api_get_utc_datetime(); |
||
| 618 | $course_id = api_get_course_int_id(); |
||
| 619 | |||
| 620 | // store in the table announcement |
||
| 621 | $params = [ |
||
| 622 | 'c_id' => $course_id, |
||
| 623 | 'content' => $newContent, |
||
| 624 | 'title' => $title, |
||
| 625 | 'end_date' => $now, |
||
| 626 | 'display_order' => $order, |
||
| 627 | 'session_id' => api_get_session_id() |
||
| 628 | ]; |
||
| 629 | |||
| 630 | $last_id = Database::insert($tbl_announcement, $params); |
||
| 631 | |||
| 632 | // Store the attach file |
||
| 633 | if ($last_id) { |
||
| 634 | $sql = "UPDATE $tbl_announcement SET id = iid WHERE iid = $last_id"; |
||
| 635 | Database::query($sql); |
||
| 636 | |||
| 637 | if (!empty($file)) { |
||
| 638 | self::add_announcement_attachment_file( |
||
| 639 | $last_id, |
||
| 640 | $file_comment, |
||
| 641 | $file |
||
| 642 | ); |
||
| 643 | } |
||
| 644 | |||
| 645 | // Store in item_property (first the groups, then the users) |
||
| 646 | //if (!isset($to_users)) { |
||
| 647 | if (isset($to_users[0]) && $to_users[0] === 'everyone') { |
||
| 648 | // when no user is selected we send it to everyone |
||
| 649 | $send_to = CourseManager::separateUsersGroups($to); |
||
| 650 | // storing the selected groups |
||
| 651 | View Code Duplication | if (is_array($send_to['groups'])) { |
|
| 652 | foreach ($send_to['groups'] as $group) { |
||
| 653 | $groupInfo = GroupManager::get_group_properties($group); |
||
| 654 | api_item_property_update( |
||
| 655 | $_course, |
||
| 656 | TOOL_ANNOUNCEMENT, |
||
| 657 | $last_id, |
||
| 658 | 'AnnouncementAdded', |
||
| 659 | api_get_user_id(), |
||
| 660 | $groupInfo |
||
| 661 | ); |
||
| 662 | } |
||
| 663 | } |
||
| 664 | } else { |
||
| 665 | $send_to_groups = CourseManager::separateUsersGroups($to); |
||
| 666 | $send_to_users = CourseManager::separateUsersGroups($to_users); |
||
| 667 | $to_groups = $send_to_groups['groups']; |
||
| 668 | $to_users = $send_to_users['users']; |
||
| 669 | // storing the selected users |
||
| 670 | if (is_array($to_users) && is_array($to_groups)) { |
||
| 671 | foreach ($to_groups as $group) { |
||
| 672 | $groupInfo = GroupManager::get_group_properties($group); |
||
| 673 | foreach ($to_users as $user) { |
||
| 674 | api_item_property_update( |
||
| 675 | $_course, |
||
| 676 | TOOL_ANNOUNCEMENT, |
||
| 677 | $last_id, |
||
| 678 | 'AnnouncementAdded', |
||
| 679 | api_get_user_id(), |
||
| 680 | $groupInfo, |
||
| 681 | $user |
||
| 682 | ); |
||
| 683 | } |
||
| 684 | } |
||
| 685 | } |
||
| 686 | } |
||
| 687 | |||
| 688 | if ($sendToUsersInSession) { |
||
| 689 | self::addAnnouncementToAllUsersInSessions($last_id); |
||
| 690 | } |
||
| 691 | } |
||
| 692 | |||
| 693 | return $last_id; |
||
| 694 | } |
||
| 695 | |||
| 696 | /** |
||
| 697 | * This function stores the announcement item in the announcement table |
||
| 698 | * and updates the item_property table |
||
| 699 | * |
||
| 700 | * @param int $id id of the announcement |
||
| 701 | * @param string $title |
||
| 702 | * @param string $newContent |
||
| 703 | * @param array $to users that will receive the announcement |
||
| 704 | * @param mixed $file attachment |
||
| 705 | * @param string $file_comment file comment |
||
| 706 | * @param bool $sendToUsersInSession |
||
| 707 | */ |
||
| 708 | public static function edit_announcement( |
||
| 709 | $id, |
||
| 710 | $title, |
||
| 711 | $newContent, |
||
| 712 | $to, |
||
| 713 | $file = array(), |
||
| 714 | $file_comment = '', |
||
| 715 | $sendToUsersInSession = false |
||
| 716 | ) { |
||
| 717 | $_course = api_get_course_info(); |
||
| 718 | $course_id = api_get_course_int_id(); |
||
| 719 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 720 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 721 | |||
| 722 | $id = intval($id); |
||
| 723 | |||
| 724 | $params = [ |
||
| 725 | 'title' => $title, |
||
| 726 | 'content' => $newContent |
||
| 727 | ]; |
||
| 728 | |||
| 729 | Database::update( |
||
| 730 | $tbl_announcement, |
||
| 731 | $params, |
||
| 732 | ['c_id = ? AND id = ?' => [$course_id, $id]] |
||
| 733 | ); |
||
| 734 | |||
| 735 | // save attachment file |
||
| 736 | $row_attach = self::get_attachment($id); |
||
| 737 | |||
| 738 | $id_attach = 0; |
||
| 739 | if ($row_attach) { |
||
| 740 | $id_attach = intval($row_attach['id']); |
||
| 741 | } |
||
| 742 | |||
| 743 | if (!empty($file)) { |
||
| 744 | if (empty($id_attach)) { |
||
| 745 | self::add_announcement_attachment_file($id, $file_comment, $file); |
||
| 746 | } else { |
||
| 747 | self::edit_announcement_attachment_file($id_attach, $file, $file_comment); |
||
| 748 | } |
||
| 749 | } |
||
| 750 | |||
| 751 | // we remove everything from item_property for this |
||
| 752 | $sql = "DELETE FROM $tbl_item_property |
||
| 753 | WHERE c_id = $course_id AND ref='$id' AND tool='announcement'"; |
||
| 754 | Database::query($sql); |
||
| 755 | |||
| 756 | if ($sendToUsersInSession) { |
||
| 757 | self::addAnnouncementToAllUsersInSessions($id); |
||
| 758 | } |
||
| 759 | |||
| 760 | // store in item_property (first the groups, then the users |
||
| 761 | if (!is_null($to)) { |
||
| 762 | // !is_null($to): when no user is selected we send it to everyone |
||
| 763 | $send_to = CourseManager::separateUsersGroups($to); |
||
| 764 | |||
| 765 | // storing the selected groups |
||
| 766 | View Code Duplication | if (is_array($send_to['groups'])) { |
|
| 767 | foreach ($send_to['groups'] as $group) { |
||
| 768 | $groupInfo = GroupManager::get_group_properties($group); |
||
| 769 | api_item_property_update( |
||
| 770 | $_course, |
||
| 771 | TOOL_ANNOUNCEMENT, |
||
| 772 | $id, |
||
| 773 | 'AnnouncementUpdated', |
||
| 774 | api_get_user_id(), |
||
| 775 | $groupInfo |
||
| 776 | ); |
||
| 777 | } |
||
| 778 | } |
||
| 779 | |||
| 780 | // storing the selected users |
||
| 781 | View Code Duplication | if (is_array($send_to['users'])) { |
|
| 782 | foreach ($send_to['users'] as $user) { |
||
| 783 | api_item_property_update( |
||
| 784 | $_course, |
||
| 785 | TOOL_ANNOUNCEMENT, |
||
| 786 | $id, |
||
| 787 | 'AnnouncementUpdated', |
||
| 788 | api_get_user_id(), |
||
| 789 | 0, |
||
| 790 | $user |
||
| 791 | ); |
||
| 792 | } |
||
| 793 | } |
||
| 794 | |||
| 795 | // Send to everyone |
||
| 796 | if (isset($to[0]) && $to[0] === 'everyone') { |
||
| 797 | api_item_property_update( |
||
| 798 | $_course, |
||
| 799 | TOOL_ANNOUNCEMENT, |
||
| 800 | $id, |
||
| 801 | 'AnnouncementUpdated', |
||
| 802 | api_get_user_id(), |
||
| 803 | 0 |
||
| 804 | ); |
||
| 805 | } |
||
| 806 | } else { |
||
| 807 | // the message is sent to everyone, so we set the group to 0 |
||
| 808 | api_item_property_update( |
||
| 809 | $_course, |
||
| 810 | TOOL_ANNOUNCEMENT, |
||
| 811 | $id, |
||
| 812 | 'AnnouncementUpdated', |
||
| 813 | api_get_user_id(), |
||
| 814 | 0 |
||
| 815 | ); |
||
| 816 | } |
||
| 817 | } |
||
| 818 | |||
| 819 | /** |
||
| 820 | * @param int $announcementId |
||
| 821 | */ |
||
| 822 | public static function addAnnouncementToAllUsersInSessions($announcementId) |
||
| 823 | { |
||
| 824 | $courseCode = api_get_course_id(); |
||
| 825 | $_course = api_get_course_info(); |
||
| 826 | $sessionList = SessionManager::get_session_by_course(api_get_course_int_id()); |
||
| 827 | |||
| 828 | if (!empty($sessionList)) { |
||
| 829 | foreach ($sessionList as $sessionInfo) { |
||
| 830 | $sessionId = $sessionInfo['id']; |
||
| 831 | $userList = CourseManager::get_user_list_from_course_code( |
||
| 832 | $courseCode, |
||
| 833 | $sessionId |
||
| 834 | ); |
||
| 835 | |||
| 836 | if (!empty($userList)) { |
||
| 837 | foreach ($userList as $user) { |
||
| 838 | api_item_property_update( |
||
| 839 | $_course, |
||
| 840 | TOOL_ANNOUNCEMENT, |
||
| 841 | $announcementId, |
||
| 842 | "AnnouncementUpdated", |
||
| 843 | api_get_user_id(), |
||
| 844 | 0, |
||
| 845 | $user['user_id'], |
||
| 846 | 0, |
||
| 847 | 0, |
||
| 848 | $sessionId |
||
| 849 | ); |
||
| 850 | } |
||
| 851 | } |
||
| 852 | } |
||
| 853 | } |
||
| 854 | } |
||
| 855 | |||
| 856 | /** |
||
| 857 | * @param int $insert_id |
||
| 858 | * @return bool |
||
| 859 | */ |
||
| 860 | public static function update_mail_sent($insert_id) |
||
| 861 | { |
||
| 862 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 863 | if ($insert_id != strval(intval($insert_id))) { |
||
| 864 | return false; |
||
| 865 | } |
||
| 866 | $insert_id = intval($insert_id); |
||
| 867 | $course_id = api_get_course_int_id(); |
||
| 868 | // store the modifications in the table tbl_annoucement |
||
| 869 | $sql = "UPDATE $tbl_announcement SET email_sent='1' |
||
| 870 | WHERE c_id = $course_id AND id = $insert_id"; |
||
| 871 | Database::query($sql); |
||
| 872 | } |
||
| 873 | |||
| 874 | /** |
||
| 875 | * Gets all announcements from a user by course |
||
| 876 | * @param string course db |
||
| 877 | * @param int user id |
||
| 878 | * @return array html with the content and count of announcements or false otherwise |
||
| 879 | */ |
||
| 880 | public static function get_all_annoucement_by_user_course($course_code, $user_id) |
||
| 881 | { |
||
| 882 | $course_info = api_get_course_info($course_code); |
||
| 883 | $course_id = $course_info['real_id']; |
||
| 884 | |||
| 885 | if (empty($user_id)) { |
||
| 886 | return false; |
||
| 887 | } |
||
| 888 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 889 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 890 | if (!empty($user_id) && is_numeric($user_id)) { |
||
| 891 | $user_id = (int) $user_id; |
||
| 892 | $sql = "SELECT DISTINCT |
||
| 893 | announcement.title, |
||
| 894 | announcement.content, |
||
| 895 | display_order |
||
| 896 | FROM $tbl_announcement announcement |
||
| 897 | INNER JOIN $tbl_item_property ip |
||
| 898 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
| 899 | WHERE |
||
| 900 | announcement.c_id = $course_id AND |
||
| 901 | ip.c_id = $course_id AND |
||
| 902 | ip.tool='announcement' AND |
||
| 903 | ( |
||
| 904 | ip.insert_user_id='$user_id' AND |
||
| 905 | (ip.to_group_id='0' OR ip.to_group_id IS NULL) |
||
| 906 | ) |
||
| 907 | AND ip.visibility='1' |
||
| 908 | AND announcement.session_id = 0 |
||
| 909 | ORDER BY display_order DESC"; |
||
| 910 | $rs = Database::query($sql); |
||
| 911 | $num_rows = Database::num_rows($rs); |
||
| 912 | $content = ''; |
||
| 913 | $i = 0; |
||
| 914 | $result = array(); |
||
| 915 | if ($num_rows > 0) { |
||
| 916 | while ($myrow = Database::fetch_array($rs)) { |
||
| 917 | $content .= '<strong>'.$myrow['title'].'</strong><br /><br />'; |
||
| 918 | $content .= $myrow['content']; |
||
| 919 | $i++; |
||
| 920 | } |
||
| 921 | $result['content'] = $content; |
||
| 922 | $result['count'] = $i; |
||
| 923 | |||
| 924 | return $result; |
||
| 925 | } |
||
| 926 | |||
| 927 | return false; |
||
| 928 | } |
||
| 929 | |||
| 930 | return false; |
||
| 931 | } |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Returns announcement info from its id |
||
| 935 | * |
||
| 936 | * @param int $course_id |
||
| 937 | * @param int $annoucement_id |
||
| 938 | * @return array |
||
| 939 | */ |
||
| 940 | View Code Duplication | public static function get_by_id($course_id, $annoucement_id) |
|
| 941 | { |
||
| 942 | $annoucement_id = intval($annoucement_id); |
||
| 943 | $course_id = $course_id ? intval($course_id) : api_get_course_int_id(); |
||
| 944 | |||
| 945 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 946 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 947 | |||
| 948 | $sql = "SELECT DISTINCT |
||
| 949 | announcement.id, |
||
| 950 | announcement.title, |
||
| 951 | announcement.content |
||
| 952 | FROM $tbl_announcement announcement |
||
| 953 | INNER JOIN $tbl_item_property ip |
||
| 954 | ON |
||
| 955 | announcement.id = ip.ref AND |
||
| 956 | announcement.c_id = ip.c_id |
||
| 957 | WHERE |
||
| 958 | announcement.c_id = $course_id AND |
||
| 959 | ip.tool='announcement' AND |
||
| 960 | announcement.id = $annoucement_id |
||
| 961 | "; |
||
| 962 | $result = Database::query($sql); |
||
| 963 | if (Database::num_rows($result)) { |
||
| 964 | return Database::fetch_array($result); |
||
| 965 | } |
||
| 966 | return []; |
||
| 967 | } |
||
| 968 | |||
| 969 | /** |
||
| 970 | * this function gets all the groups of the course, |
||
| 971 | * not including linked courses |
||
| 972 | */ |
||
| 973 | View Code Duplication | public static function get_course_groups() |
|
| 974 | { |
||
| 975 | $session_id = api_get_session_id(); |
||
| 976 | if ($session_id != 0) { |
||
| 977 | $new_group_list = CourseManager::get_group_list_of_course( |
||
| 978 | api_get_course_id(), |
||
| 979 | $session_id, |
||
| 980 | 1 |
||
| 981 | ); |
||
| 982 | } else { |
||
| 983 | $new_group_list = CourseManager::get_group_list_of_course( |
||
| 984 | api_get_course_id(), |
||
| 985 | 0, |
||
| 986 | 1 |
||
| 987 | ); |
||
| 988 | } |
||
| 989 | return $new_group_list; |
||
| 990 | } |
||
| 991 | |||
| 992 | /** |
||
| 993 | * This tools loads all the users and all the groups who have received |
||
| 994 | * a specific item (in this case an announcement item) |
||
| 995 | * @param string $tool |
||
| 996 | * @param int $id |
||
| 997 | * @return array |
||
| 998 | */ |
||
| 999 | public static function load_edit_users($tool, $id) |
||
| 1000 | { |
||
| 1001 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 1002 | $tool = Database::escape_string($tool); |
||
| 1003 | $id = intval($id); |
||
| 1004 | $course_id = api_get_course_int_id(); |
||
| 1005 | |||
| 1006 | $sql = "SELECT * FROM $tbl_item_property |
||
| 1007 | WHERE c_id = $course_id AND tool='$tool' AND ref = $id"; |
||
| 1008 | $result = Database::query($sql); |
||
| 1009 | $to = array(); |
||
| 1010 | while ($row = Database::fetch_array($result)) { |
||
| 1011 | $to_group = $row['to_group_id']; |
||
| 1012 | switch ($to_group) { |
||
| 1013 | // it was send to one specific user |
||
| 1014 | case null: |
||
| 1015 | $to[] = "USER:".$row['to_user_id']; |
||
| 1016 | break; |
||
| 1017 | // it was sent to everyone |
||
| 1018 | case 0: |
||
| 1019 | return "everyone"; |
||
| 1020 | break; |
||
| 1021 | default: |
||
| 1022 | $to[] = "GROUP:".$row['to_group_id']; |
||
| 1023 | } |
||
| 1024 | } |
||
| 1025 | |||
| 1026 | return $to; |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | /** |
||
| 1030 | * constructs the form to display all the groups and users the message has been sent to |
||
| 1031 | * input: $sent_to_array is a 2 dimensional array containing the groups and the users |
||
| 1032 | * the first level is a distinction between groups and users: |
||
| 1033 | * $sent_to_array['groups'] * and $sent_to_array['users'] |
||
| 1034 | * $sent_to_array['groups'] (resp. $sent_to_array['users']) is also an array |
||
| 1035 | * containing all the id's of the groups (resp. users) who have received this message. |
||
| 1036 | * @author Patrick Cool <patrick.cool@> |
||
| 1037 | */ |
||
| 1038 | public static function sent_to_form($sent_to_array) |
||
| 1039 | { |
||
| 1040 | // we find all the names of the groups |
||
| 1041 | $group_names = self::get_course_groups(); |
||
| 1042 | |||
| 1043 | // we count the number of users and the number of groups |
||
| 1044 | if (isset($sent_to_array['users'])) { |
||
| 1045 | $number_users = count($sent_to_array['users']); |
||
| 1046 | } else { |
||
| 1047 | $number_users = 0; |
||
| 1048 | } |
||
| 1049 | if (isset($sent_to_array['groups'])) { |
||
| 1050 | $number_groups = count($sent_to_array['groups']); |
||
| 1051 | } else { |
||
| 1052 | $number_groups = 0; |
||
| 1053 | } |
||
| 1054 | $total_numbers = $number_users + $number_groups; |
||
| 1055 | |||
| 1056 | // starting the form if there is more than one user/group |
||
| 1057 | $output = array(); |
||
| 1058 | if ($total_numbers > 1) { |
||
| 1059 | // outputting the name of the groups |
||
| 1060 | if (is_array($sent_to_array['groups'])) { |
||
| 1061 | foreach ($sent_to_array['groups'] as $group_id) { |
||
| 1062 | $output[] = $group_names[$group_id]['name']; |
||
| 1063 | } |
||
| 1064 | } |
||
| 1065 | |||
| 1066 | if (isset($sent_to_array['users'])) { |
||
| 1067 | if (is_array($sent_to_array['users'])) { |
||
| 1068 | foreach ($sent_to_array['users'] as $user_id) { |
||
| 1069 | $user_info = api_get_user_info($user_id); |
||
| 1070 | $output[] = $user_info['complete_name_with_username']; |
||
| 1071 | } |
||
| 1072 | } |
||
| 1073 | } |
||
| 1074 | } else { |
||
| 1075 | // there is only one user/group |
||
| 1076 | if (isset($sent_to_array['users']) and is_array($sent_to_array['users'])) { |
||
| 1077 | $user_info = api_get_user_info($sent_to_array['users'][0]); |
||
| 1078 | $output[] = api_get_person_name($user_info['firstname'], $user_info['lastname']); |
||
| 1079 | } |
||
| 1080 | if (isset($sent_to_array['groups']) and |
||
| 1081 | is_array($sent_to_array['groups']) and |
||
| 1082 | isset($sent_to_array['groups'][0]) and |
||
| 1083 | $sent_to_array['groups'][0] !== 0 |
||
| 1084 | ) { |
||
| 1085 | $group_id = $sent_to_array['groups'][0]; |
||
| 1086 | $output[] = " ".$group_names[$group_id]['name']; |
||
| 1087 | } |
||
| 1088 | if (empty($sent_to_array['groups']) and empty($sent_to_array['users'])) { |
||
| 1089 | $output[] = " ".get_lang('Everybody'); |
||
| 1090 | } |
||
| 1091 | } |
||
| 1092 | |||
| 1093 | if (!empty($output)) { |
||
| 1094 | $output = array_filter($output); |
||
| 1095 | if (count($output) > 0) { |
||
| 1096 | $output = implode(', ', $output); |
||
| 1097 | } |
||
| 1098 | return $output; |
||
| 1099 | } |
||
| 1100 | } |
||
| 1101 | |||
| 1102 | /** |
||
| 1103 | * Returns all the users and all the groups a specific announcement item |
||
| 1104 | * has been sent to |
||
| 1105 | * @param string The tool (announcement, agenda, ...) |
||
| 1106 | * @param int ID of the element of the corresponding type |
||
| 1107 | * @return array Array of users and groups to whom the element has been sent |
||
| 1108 | */ |
||
| 1109 | public static function sent_to($tool, $id) |
||
| 1110 | { |
||
| 1111 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 1112 | |||
| 1113 | $tool = Database::escape_string($tool); |
||
| 1114 | $id = (int) $id; |
||
| 1115 | |||
| 1116 | $sent_to_group = array(); |
||
| 1117 | $sent_to = array(); |
||
| 1118 | $course_id = api_get_course_int_id(); |
||
| 1119 | |||
| 1120 | $sql = "SELECT to_group_id, to_user_id |
||
| 1121 | FROM $tbl_item_property |
||
| 1122 | WHERE c_id = $course_id AND tool = '$tool' AND ref=".$id; |
||
| 1123 | $result = Database::query($sql); |
||
| 1124 | |||
| 1125 | while ($row = Database::fetch_array($result)) { |
||
| 1126 | // if to_user_id <> 0 then it is sent to a specific user |
||
| 1127 | if ($row['to_user_id'] <> 0) { |
||
| 1128 | $sent_to_user[] = $row['to_user_id']; |
||
| 1129 | continue; |
||
| 1130 | } |
||
| 1131 | |||
| 1132 | // if to_group_id is null then it is sent to a specific user |
||
| 1133 | // if to_group_id = 0 then it is sent to everybody |
||
| 1134 | if ($row['to_group_id'] != 0) { |
||
| 1135 | $sent_to_group[] = $row['to_group_id']; |
||
| 1136 | } |
||
| 1137 | } |
||
| 1138 | |||
| 1139 | if (isset($sent_to_group)) { |
||
| 1140 | $sent_to['groups'] = $sent_to_group; |
||
| 1141 | } |
||
| 1142 | |||
| 1143 | if (isset($sent_to_user)) { |
||
| 1144 | $sent_to['users'] = $sent_to_user; |
||
| 1145 | } |
||
| 1146 | |||
| 1147 | return $sent_to; |
||
| 1148 | } |
||
| 1149 | |||
| 1150 | /** |
||
| 1151 | * Show a list with all the attachments according to the post's id |
||
| 1152 | * @param int $announcementId |
||
| 1153 | * @return array with the post info |
||
| 1154 | * @author Arthur Portugal |
||
| 1155 | * @version November 2009, dokeos 1.8.6.2 |
||
| 1156 | */ |
||
| 1157 | View Code Duplication | public static function get_attachment($announcementId) |
|
| 1158 | { |
||
| 1159 | $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); |
||
| 1160 | $announcementId = intval($announcementId); |
||
| 1161 | $course_id = api_get_course_int_id(); |
||
| 1162 | $row = array(); |
||
| 1163 | $sql = 'SELECT id, path, filename, comment |
||
| 1164 | FROM ' . $tbl_announcement_attachment.' |
||
| 1165 | WHERE c_id = ' . $course_id.' AND announcement_id = '.$announcementId; |
||
| 1166 | $result = Database::query($sql); |
||
| 1167 | if (Database::num_rows($result) != 0) { |
||
| 1168 | $row = Database::fetch_array($result, 'ASSOC'); |
||
| 1169 | } |
||
| 1170 | return $row; |
||
| 1171 | } |
||
| 1172 | |||
| 1173 | /** |
||
| 1174 | * This function add a attachment file into announcement |
||
| 1175 | * @param int announcement id |
||
| 1176 | * @param string file comment |
||
| 1177 | * @param array uploaded file $_FILES |
||
| 1178 | * @return int -1 if failed, 0 if unknown (should not happen), 1 if success |
||
| 1179 | */ |
||
| 1180 | public static function add_announcement_attachment_file($announcement_id, $file_comment, $file) |
||
| 1181 | { |
||
| 1182 | $_course = api_get_course_info(); |
||
| 1183 | $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); |
||
| 1184 | $return = 0; |
||
| 1185 | $announcement_id = intval($announcement_id); |
||
| 1186 | $course_id = api_get_course_int_id(); |
||
| 1187 | |||
| 1188 | if (is_array($file) && $file['error'] == 0) { |
||
| 1189 | // TODO: This path is obsolete. The new document repository scheme should be kept in mind here. |
||
| 1190 | $courseDir = $_course['path'].'/upload/announcements'; |
||
| 1191 | $sys_course_path = api_get_path(SYS_COURSE_PATH); |
||
| 1192 | $updir = $sys_course_path.$courseDir; |
||
| 1193 | |||
| 1194 | // Try to add an extension to the file if it hasn't one |
||
| 1195 | $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']); |
||
| 1196 | // user's file name |
||
| 1197 | $file_name = $file['name']; |
||
| 1198 | |||
| 1199 | if (!filter_extension($new_file_name)) { |
||
| 1200 | $return = -1; |
||
| 1201 | echo Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error'); |
||
| 1202 | } else { |
||
| 1203 | $new_file_name = uniqid(''); |
||
| 1204 | $new_path = $updir.'/'.$new_file_name; |
||
| 1205 | |||
| 1206 | // This file is copy here but its cleaned in api_mail_html in api.lib.php |
||
| 1207 | copy($file['tmp_name'], $new_path); |
||
| 1208 | |||
| 1209 | $params = [ |
||
| 1210 | 'c_id' => $course_id, |
||
| 1211 | 'filename' => $file_name, |
||
| 1212 | 'comment' => $file_comment, |
||
| 1213 | 'path' => $new_file_name, |
||
| 1214 | 'announcement_id' => $announcement_id, |
||
| 1215 | 'size' => intval($file['size']), |
||
| 1216 | ]; |
||
| 1217 | |||
| 1218 | $insertId = Database::insert($tbl_announcement_attachment, $params); |
||
| 1219 | if ($insertId) { |
||
| 1220 | $sql = "UPDATE $tbl_announcement_attachment SET id = iid WHERE iid = $insertId"; |
||
| 1221 | Database::query($sql); |
||
| 1222 | } |
||
| 1223 | |||
| 1224 | $return = 1; |
||
| 1225 | } |
||
| 1226 | } |
||
| 1227 | |||
| 1228 | return $return; |
||
| 1229 | } |
||
| 1230 | |||
| 1231 | /** |
||
| 1232 | * This function edit a attachment file into announcement |
||
| 1233 | * @param int attach id |
||
| 1234 | * @param array uploaded file $_FILES |
||
| 1235 | * @param string file comment |
||
| 1236 | * @return int |
||
| 1237 | */ |
||
| 1238 | public static function edit_announcement_attachment_file($id_attach, $file, $file_comment) |
||
| 1239 | { |
||
| 1240 | $_course = api_get_course_info(); |
||
| 1241 | $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); |
||
| 1242 | $return = 0; |
||
| 1243 | $course_id = api_get_course_int_id(); |
||
| 1244 | |||
| 1245 | if (is_array($file) && $file['error'] == 0) { |
||
| 1246 | // TODO: This path is obsolete. The new document repository scheme should be kept in mind here. |
||
| 1247 | $courseDir = $_course['path'].'/upload/announcements'; |
||
| 1248 | $sys_course_path = api_get_path(SYS_COURSE_PATH); |
||
| 1249 | $updir = $sys_course_path.$courseDir; |
||
| 1250 | |||
| 1251 | // Try to add an extension to the file if it hasn't one |
||
| 1252 | $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']); |
||
| 1253 | // user's file name |
||
| 1254 | $file_name = $file ['name']; |
||
| 1255 | |||
| 1256 | if (!filter_extension($new_file_name)) { |
||
| 1257 | $return = -1; |
||
| 1258 | echo Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error'); |
||
| 1259 | } else { |
||
| 1260 | $new_file_name = uniqid(''); |
||
| 1261 | $new_path = $updir.'/'.$new_file_name; |
||
| 1262 | copy($file['tmp_name'], $new_path); |
||
| 1263 | $safe_file_comment = Database::escape_string($file_comment); |
||
| 1264 | $safe_file_name = Database::escape_string($file_name); |
||
| 1265 | $safe_new_file_name = Database::escape_string($new_file_name); |
||
| 1266 | $id_attach = intval($id_attach); |
||
| 1267 | $sql = "UPDATE $tbl_announcement_attachment SET |
||
| 1268 | filename = '$safe_file_name', |
||
| 1269 | comment = '$safe_file_comment', |
||
| 1270 | path = '$safe_new_file_name', |
||
| 1271 | size ='".intval($file['size'])."' |
||
| 1272 | WHERE c_id = $course_id AND id = '$id_attach'"; |
||
| 1273 | $result = Database::query($sql); |
||
| 1274 | if ($result === false) { |
||
| 1275 | $return = -1; |
||
| 1276 | echo Display::return_message(get_lang('UplUnableToSaveFile'), 'error'); |
||
| 1277 | } else { |
||
| 1278 | $return = 1; |
||
| 1279 | } |
||
| 1280 | } |
||
| 1281 | } |
||
| 1282 | return $return; |
||
| 1283 | } |
||
| 1284 | |||
| 1285 | /** |
||
| 1286 | * This function delete a attachment file by id |
||
| 1287 | * @param integer $id attachment file Id |
||
| 1288 | * @return bool |
||
| 1289 | */ |
||
| 1290 | View Code Duplication | public static function delete_announcement_attachment_file($id) |
|
| 1291 | { |
||
| 1292 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); |
||
| 1293 | $id = intval($id); |
||
| 1294 | $course_id = api_get_course_int_id(); |
||
| 1295 | if (empty($course_id) || empty($id)) { |
||
| 1296 | return false; |
||
| 1297 | } |
||
| 1298 | $sql = "DELETE FROM $table |
||
| 1299 | WHERE c_id = $course_id AND id = $id"; |
||
| 1300 | Database::query($sql); |
||
| 1301 | |||
| 1302 | return true; |
||
| 1303 | } |
||
| 1304 | |||
| 1305 | /** |
||
| 1306 | * @param array $courseInfo |
||
| 1307 | * @param int $sessionId |
||
| 1308 | * @param int $id |
||
| 1309 | * @param bool $sendToUsersInSession |
||
| 1310 | * @param bool $sendToDrhUsers |
||
| 1311 | */ |
||
| 1312 | public static function sendEmail( |
||
| 1313 | $courseInfo, |
||
| 1314 | $sessionId, |
||
| 1315 | $id, |
||
| 1316 | $sendToUsersInSession = false, |
||
| 1317 | $sendToDrhUsers = false |
||
| 1318 | ) { |
||
| 1319 | $email = AnnouncementEmail::create($courseInfo, $sessionId, $id); |
||
| 1320 | $email->send($sendToUsersInSession, $sendToDrhUsers); |
||
| 1321 | } |
||
| 1322 | |||
| 1323 | /** |
||
| 1324 | * @param $stok |
||
| 1325 | * @param $announcement_number |
||
| 1326 | * @param bool $getCount |
||
| 1327 | * @param null $start |
||
| 1328 | * @param null $limit |
||
| 1329 | * @param string $sidx |
||
| 1330 | * @param string $sord |
||
| 1331 | * @param string $titleToSearch |
||
| 1332 | * @param int $userIdToSearch |
||
| 1333 | * @param int $userId |
||
| 1334 | * @param int $courseId |
||
| 1335 | * @param int $sessionId |
||
| 1336 | * @return array |
||
| 1337 | */ |
||
| 1338 | public static function getAnnouncements( |
||
| 1339 | $stok, |
||
| 1340 | $announcement_number, |
||
| 1341 | $getCount = false, |
||
| 1342 | $start = null, |
||
| 1343 | $limit = null, |
||
| 1344 | $sidx = '', |
||
| 1345 | $sord = '', |
||
| 1346 | $titleToSearch = '', |
||
| 1347 | $userIdToSearch = 0, |
||
| 1348 | $userId = 0, |
||
| 1349 | $courseId = 0, |
||
| 1350 | $sessionId = 0 |
||
| 1351 | ) { |
||
| 1352 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 1353 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 1354 | |||
| 1355 | $user_id = $userId ?: api_get_user_id(); |
||
| 1356 | $group_id = api_get_group_id(); |
||
| 1357 | $session_id = $sessionId ?: api_get_session_id(); |
||
| 1358 | $condition_session = api_get_session_condition( |
||
| 1359 | $session_id, |
||
| 1360 | true, |
||
| 1361 | true, |
||
| 1362 | 'announcement.session_id' |
||
| 1363 | ); |
||
| 1364 | $course_id = $courseId ?: api_get_course_int_id(); |
||
| 1365 | $_course = api_get_course_info(); |
||
| 1366 | |||
| 1367 | $group_memberships = GroupManager::get_group_ids($course_id, api_get_user_id()); |
||
| 1368 | $allowUserEditSetting = api_get_course_setting('allow_user_edit_announcement'); |
||
| 1369 | |||
| 1370 | $select = ' DISTINCT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date'; |
||
| 1371 | if ($getCount) { |
||
| 1372 | $select = ' COUNT(DISTINCT announcement.iid) count'; |
||
| 1373 | } |
||
| 1374 | |||
| 1375 | $searchCondition = ''; |
||
| 1376 | if (!empty($titleToSearch)) { |
||
| 1377 | $titleToSearch = Database::escape_string($titleToSearch); |
||
| 1378 | $searchCondition .= " AND (title LIKE '%$titleToSearch%')"; |
||
| 1379 | } |
||
| 1380 | |||
| 1381 | if (!empty($userIdToSearch)) { |
||
| 1382 | $userIdToSearch = intval($userIdToSearch); |
||
| 1383 | $searchCondition .= " AND (ip.insert_user_id = $userIdToSearch)"; |
||
| 1384 | } |
||
| 1385 | |||
| 1386 | if (api_is_allowed_to_edit(false, true) || |
||
| 1387 | ($allowUserEditSetting && !api_is_anonymous()) |
||
| 1388 | ) { |
||
| 1389 | // A.1. you are a course admin with a USER filter |
||
| 1390 | // => see only the messages of this specific user + the messages of the group (s)he is member of. |
||
| 1391 | |||
| 1392 | //if (!empty($user_id)) { |
||
| 1393 | if (0) { |
||
| 1394 | if (is_array($group_memberships) && count($group_memberships) > 0) { |
||
| 1395 | $sql = "SELECT $select |
||
| 1396 | FROM $tbl_announcement announcement |
||
| 1397 | INNER JOIN $tbl_item_property ip |
||
| 1398 | ON (announcement.id = ip.ref AND ip.c_id = announcement.c_id) |
||
| 1399 | WHERE |
||
| 1400 | announcement.c_id = $course_id AND |
||
| 1401 | ip.c_id = $course_id AND |
||
| 1402 | ip.tool = 'announcement' AND |
||
| 1403 | ( |
||
| 1404 | ip.to_user_id = $user_id OR |
||
| 1405 | ip.to_group_id IS NULL OR |
||
| 1406 | ip.to_group_id IN (0, ".implode(", ", $group_memberships).") |
||
| 1407 | ) AND |
||
| 1408 | ip.visibility IN ('1', '0') |
||
| 1409 | $condition_session |
||
| 1410 | $searchCondition |
||
| 1411 | ORDER BY display_order DESC"; |
||
| 1412 | } else { |
||
| 1413 | $sql = "SELECT $select |
||
| 1414 | FROM $tbl_announcement announcement |
||
| 1415 | INNER JOIN $tbl_item_property ip |
||
| 1416 | ON (announcement.id = ip.ref AND ip.c_id = announcement.c_id) |
||
| 1417 | WHERE |
||
| 1418 | announcement.c_id = $course_id AND |
||
| 1419 | ip.c_id = $course_id AND |
||
| 1420 | ip.tool ='announcement' AND |
||
| 1421 | (ip.to_user_id = $user_id OR ip.to_group_id='0' OR ip.to_group_id IS NULL) AND |
||
| 1422 | ip.visibility IN ('1', '0') |
||
| 1423 | $condition_session |
||
| 1424 | $searchCondition |
||
| 1425 | ORDER BY display_order DESC"; |
||
| 1426 | } |
||
| 1427 | } elseif ($group_id != 0) { |
||
| 1428 | // A.2. you are a course admin with a GROUP filter |
||
| 1429 | // => see only the messages of this specific group |
||
| 1430 | $sql = "SELECT $select |
||
| 1431 | FROM $tbl_announcement announcement |
||
| 1432 | INNER JOIN $tbl_item_property ip |
||
| 1433 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
| 1434 | WHERE |
||
| 1435 | ip.tool='announcement' AND |
||
| 1436 | announcement.c_id = $course_id AND |
||
| 1437 | ip.c_id = $course_id AND |
||
| 1438 | ip.visibility<>'2' AND |
||
| 1439 | (ip.to_group_id = $group_id OR ip.to_group_id='0' OR ip.to_group_id IS NULL) |
||
| 1440 | $condition_session |
||
| 1441 | $searchCondition |
||
| 1442 | ORDER BY display_order DESC"; |
||
| 1443 | //GROUP BY ip.ref |
||
| 1444 | } else { |
||
| 1445 | // A.3 you are a course admin without any group or user filter |
||
| 1446 | // A.3.a you are a course admin without user or group filter but WITH studentview |
||
| 1447 | // => see all the messages of all the users and groups without editing possibilities |
||
| 1448 | if (isset($isStudentView) && $isStudentView == "true") { |
||
| 1449 | $sql = "SELECT $select |
||
| 1450 | FROM $tbl_announcement announcement |
||
| 1451 | INNER JOIN $tbl_item_property ip |
||
| 1452 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
| 1453 | WHERE |
||
| 1454 | ip.tool='announcement' AND |
||
| 1455 | announcement.c_id = $course_id AND |
||
| 1456 | ip.c_id = $course_id AND |
||
| 1457 | ip.visibility='1' |
||
| 1458 | $condition_session |
||
| 1459 | $searchCondition |
||
| 1460 | ORDER BY display_order DESC"; |
||
| 1461 | |||
| 1462 | //GROUP BY ip.ref |
||
| 1463 | } else { |
||
| 1464 | // A.3.a you are a course admin without user or group filter and WTIHOUT studentview (= the normal course admin view) |
||
| 1465 | // => see all the messages of all the users and groups with editing possibilities |
||
| 1466 | $sql = "SELECT $select |
||
| 1467 | FROM $tbl_announcement announcement |
||
| 1468 | INNER JOIN $tbl_item_property ip |
||
| 1469 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
| 1470 | WHERE |
||
| 1471 | ip.tool = 'announcement' AND |
||
| 1472 | announcement.c_id = $course_id AND |
||
| 1473 | ip.c_id = $course_id AND |
||
| 1474 | (ip.visibility='0' OR ip.visibility='1') |
||
| 1475 | $condition_session |
||
| 1476 | $searchCondition |
||
| 1477 | ORDER BY display_order DESC"; |
||
| 1478 | } |
||
| 1479 | } |
||
| 1480 | } else { |
||
| 1481 | // STUDENT |
||
| 1482 | if (is_array($group_memberships) && count($group_memberships) > 0) { |
||
| 1483 | if ($allowUserEditSetting && !api_is_anonymous()) { |
||
| 1484 | if ($group_id == 0) { |
||
| 1485 | // No group |
||
| 1486 | $cond_user_id = " AND ( |
||
| 1487 | ip.lastedit_user_id = '".$user_id."' OR ( |
||
| 1488 | (ip.to_user_id='$user_id' OR ip.to_user_id IS NULL) OR |
||
| 1489 | (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(", ", $group_memberships).")) |
||
| 1490 | ) |
||
| 1491 | ) "; |
||
| 1492 | } else { |
||
| 1493 | $cond_user_id = " AND ( |
||
| 1494 | ip.lastedit_user_id = '".$user_id."' OR ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".$group_id.") |
||
| 1495 | )"; |
||
| 1496 | } |
||
| 1497 | View Code Duplication | } else { |
|
| 1498 | if ($group_id == 0) { |
||
| 1499 | $cond_user_id = " AND ( |
||
| 1500 | (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).")) |
||
| 1501 | ) "; |
||
| 1502 | } else { |
||
| 1503 | $cond_user_id = " AND ( |
||
| 1504 | (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.")) |
||
| 1505 | )"; |
||
| 1506 | } |
||
| 1507 | } |
||
| 1508 | |||
| 1509 | $sql = "SELECT $select |
||
| 1510 | FROM $tbl_announcement announcement INNER JOIN |
||
| 1511 | $tbl_item_property ip |
||
| 1512 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
| 1513 | WHERE |
||
| 1514 | announcement.c_id = $course_id AND |
||
| 1515 | ip.c_id = $course_id AND |
||
| 1516 | ip.tool='announcement' |
||
| 1517 | $cond_user_id |
||
| 1518 | $condition_session |
||
| 1519 | $searchCondition |
||
| 1520 | AND ip.visibility='1' |
||
| 1521 | ORDER BY display_order DESC"; |
||
| 1522 | } else { |
||
| 1523 | if ($user_id) { |
||
| 1524 | View Code Duplication | if ($allowUserEditSetting && !api_is_anonymous()) { |
|
| 1525 | $cond_user_id = " AND ( |
||
| 1526 | ip.lastedit_user_id = '".api_get_user_id()."' OR |
||
| 1527 | ((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)) |
||
| 1528 | ) "; |
||
| 1529 | } else { |
||
| 1530 | $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) ) "; |
||
| 1531 | } |
||
| 1532 | |||
| 1533 | $sql = "SELECT $select |
||
| 1534 | FROM $tbl_announcement announcement |
||
| 1535 | INNER JOIN $tbl_item_property ip |
||
| 1536 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
| 1537 | WHERE |
||
| 1538 | announcement.c_id = $course_id AND |
||
| 1539 | ip.c_id = $course_id AND |
||
| 1540 | ip.tool='announcement' |
||
| 1541 | $cond_user_id |
||
| 1542 | $condition_session |
||
| 1543 | $searchCondition |
||
| 1544 | AND ip.visibility='1' |
||
| 1545 | AND announcement.session_id IN(0, ".$session_id.") |
||
| 1546 | ORDER BY display_order DESC"; |
||
| 1547 | } else { |
||
| 1548 | View Code Duplication | if (($allowUserEditSetting && !api_is_anonymous())) { |
|
| 1549 | $cond_user_id = " AND ( |
||
| 1550 | ip.lastedit_user_id = '".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL |
||
| 1551 | )"; |
||
| 1552 | } else { |
||
| 1553 | $cond_user_id = " AND ip.to_group_id='0' OR ip.to_group_id IS NULL "; |
||
| 1554 | } |
||
| 1555 | |||
| 1556 | $sql = "SELECT $select |
||
| 1557 | FROM $tbl_announcement announcement |
||
| 1558 | INNER JOIN $tbl_item_property ip |
||
| 1559 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
| 1560 | WHERE |
||
| 1561 | announcement.c_id = $course_id AND |
||
| 1562 | ip.c_id = $course_id AND |
||
| 1563 | ip.tool='announcement' |
||
| 1564 | $cond_user_id |
||
| 1565 | $condition_session |
||
| 1566 | $searchCondition AND |
||
| 1567 | ip.visibility='1' AND |
||
| 1568 | announcement.session_id IN ( 0,".api_get_session_id().")"; |
||
| 1569 | } |
||
| 1570 | } |
||
| 1571 | } |
||
| 1572 | |||
| 1573 | if (!is_null($start) && !is_null($limit)) { |
||
| 1574 | $start = intval($start); |
||
| 1575 | $limit = intval($limit); |
||
| 1576 | $sql .= " LIMIT $start, $limit"; |
||
| 1577 | } |
||
| 1578 | |||
| 1579 | $result = Database::query($sql); |
||
| 1580 | if ($getCount) { |
||
| 1581 | $result = Database::fetch_array($result, 'ASSOC'); |
||
| 1582 | |||
| 1583 | return $result['count']; |
||
| 1584 | } |
||
| 1585 | |||
| 1586 | $iterator = 1; |
||
| 1587 | $bottomAnnouncement = $announcement_number; |
||
| 1588 | $displayed = []; |
||
| 1589 | $results = []; |
||
| 1590 | $actionUrl = api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.api_get_cidreq(); |
||
| 1591 | while ($myrow = Database::fetch_array($result, 'ASSOC')) { |
||
| 1592 | if (!in_array($myrow['id'], $displayed)) { |
||
| 1593 | $sent_to_icon = ''; |
||
| 1594 | // the email icon |
||
| 1595 | if ($myrow['email_sent'] == '1') { |
||
| 1596 | $sent_to_icon = ' '.Display::return_icon('email.gif', get_lang('AnnounceSentByEmail')); |
||
| 1597 | } |
||
| 1598 | $groupReference = ($myrow['to_group_id'] > 0) ? ' <span class="label label-info">'.get_lang('Group').'</span> ' : ''; |
||
| 1599 | $title = $myrow['title'].$groupReference.$sent_to_icon; |
||
| 1600 | $item_visibility = api_get_item_visibility($_course, TOOL_ANNOUNCEMENT, $myrow['id'], $session_id); |
||
| 1601 | $myrow['visibility'] = $item_visibility; |
||
| 1602 | |||
| 1603 | // show attachment list |
||
| 1604 | $attachment_list = self::get_attachment($myrow['id']); |
||
| 1605 | |||
| 1606 | $attachment_icon = ''; |
||
| 1607 | if (count($attachment_list) > 0) { |
||
| 1608 | $attachment_icon = ' '.Display::return_icon('attachment.gif', get_lang('Attachment')); |
||
| 1609 | } |
||
| 1610 | |||
| 1611 | /* TITLE */ |
||
| 1612 | $user_info = api_get_user_info($myrow['insert_user_id']); |
||
| 1613 | $username = sprintf(get_lang("LoginX"), $user_info['username']); |
||
| 1614 | $username_span = Display::tag('span', api_get_person_name($user_info['firstName'], $user_info['lastName']), array('title'=>$username)); |
||
| 1615 | $title = Display::url($title.$attachment_icon, $actionUrl.'&action=view&id='.$myrow['id']); |
||
| 1616 | //$html .= Display::tag('td', $username_span, array('class' => 'announcements-list-line-by-user')); |
||
| 1617 | //$html .= Display::tag('td', api_convert_and_format_date($myrow['insert_date'], DATE_TIME_FORMAT_LONG), array('class' => 'announcements-list-line-datetime')); |
||
| 1618 | |||
| 1619 | $modify_icons = ''; |
||
| 1620 | // we can edit if : we are the teacher OR the element belongs to |
||
| 1621 | // the session we are coaching OR the option to allow users to edit is on |
||
| 1622 | if (api_is_allowed_to_edit(false, true) || |
||
| 1623 | (api_is_course_coach() && api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $myrow['id'])) |
||
| 1624 | || (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous()) |
||
| 1625 | ) { |
||
| 1626 | $modify_icons = "<a href=\"".$actionUrl."&action=modify&id=".$myrow['id']."\">". |
||
| 1627 | Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL)."</a>"; |
||
| 1628 | View Code Duplication | if ($myrow['visibility'] == 1) { |
|
| 1629 | $image_visibility = "visible"; |
||
| 1630 | $alt_visibility = get_lang('Hide'); |
||
| 1631 | } else { |
||
| 1632 | $image_visibility = "invisible"; |
||
| 1633 | $alt_visibility = get_lang('Visible'); |
||
| 1634 | } |
||
| 1635 | $modify_icons .= "<a href=\"".$actionUrl."&action=showhide&id=".$myrow['id']."&sec_token=".$stok."\">". |
||
| 1636 | Display::return_icon($image_visibility.'.png', $alt_visibility, '', ICON_SIZE_SMALL)."</a>"; |
||
| 1637 | |||
| 1638 | // DISPLAY MOVE UP COMMAND only if it is not the top announcement |
||
| 1639 | View Code Duplication | if ($iterator != 1) { |
|
| 1640 | $modify_icons .= "<a href=\"".$actionUrl."&action=move&up=".$myrow["id"]."&sec_token=".$stok."\">". |
||
| 1641 | Display::return_icon('up.gif', get_lang('Up'))."</a>"; |
||
| 1642 | } else { |
||
| 1643 | $modify_icons .= Display::return_icon('up_na.gif', get_lang('Up')); |
||
| 1644 | } |
||
| 1645 | if ($iterator < $bottomAnnouncement) { |
||
| 1646 | $modify_icons .= "<a href=\"".$actionUrl."&action=move&down=".$myrow["id"]."&sec_token=".$stok."\">". |
||
| 1647 | Display::return_icon('down.gif', get_lang('Down'))."</a>"; |
||
| 1648 | } else { |
||
| 1649 | $modify_icons .= Display::return_icon('down_na.gif', get_lang('Down')); |
||
| 1650 | } |
||
| 1651 | View Code Duplication | if (api_is_allowed_to_edit(false, true)) { |
|
| 1652 | $modify_icons .= "<a href=\"".$actionUrl."&action=delete&id=".$myrow['id']."&sec_token=".$stok."\" onclick=\"javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, api_get_system_encoding()))."')) return false;\">". |
||
| 1653 | Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL). |
||
| 1654 | "</a>"; |
||
| 1655 | } |
||
| 1656 | $iterator++; |
||
| 1657 | } else { |
||
| 1658 | $modify_icons = Display::url( |
||
| 1659 | Display::return_icon('default.png'), |
||
| 1660 | $actionUrl.'&action=view&id='.$myrow['id'] |
||
| 1661 | ); |
||
| 1662 | } |
||
| 1663 | |||
| 1664 | $announcement = [ |
||
| 1665 | 'id' => $myrow["id"], |
||
| 1666 | 'title' => $title, |
||
| 1667 | 'username' => $username_span, |
||
| 1668 | 'insert_date' => api_convert_and_format_date($myrow['insert_date'], DATE_TIME_FORMAT_LONG), |
||
| 1669 | 'actions' => $modify_icons |
||
| 1670 | ]; |
||
| 1671 | |||
| 1672 | $results[] = $announcement; |
||
| 1673 | } |
||
| 1674 | $displayed[] = $myrow['id']; |
||
| 1675 | } |
||
| 1676 | |||
| 1677 | return $results; |
||
| 1678 | } |
||
| 1679 | |||
| 1680 | /** |
||
| 1681 | * @return int |
||
| 1682 | */ |
||
| 1683 | public static function getNumberAnnouncements() |
||
| 1850 | } |
||
| 1851 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.