| Total Complexity | 217 |
| Total Lines | 1910 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| 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 |
||
| 17 | class AnnouncementManager |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Constructor. |
||
| 21 | */ |
||
| 22 | public function __construct() |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param int $sessionId |
||
| 28 | * |
||
| 29 | * @return array |
||
| 30 | */ |
||
| 31 | public static function getTags($sessionId = 0) |
||
| 32 | { |
||
| 33 | $tags = [ |
||
| 34 | '((user_name))', |
||
| 35 | '((user_email))', |
||
| 36 | '((user_firstname))', |
||
| 37 | '((user_lastname))', |
||
| 38 | '((user_official_code))', |
||
| 39 | '((course_title))', |
||
| 40 | '((course_link))', |
||
| 41 | ]; |
||
| 42 | |||
| 43 | $tags[] = '((teachers))'; |
||
| 44 | |||
| 45 | $extraField = new ExtraField('user'); |
||
| 46 | $extraFields = $extraField->get_all(['filter = ?' => 1]); |
||
| 47 | if (!empty($extraFields)) { |
||
| 48 | foreach ($extraFields as $extra) { |
||
| 49 | $tags[] = "((extra_".$extra['variable']."))"; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | $sessionId = $sessionId ?: api_get_session_id(); |
||
| 53 | if (!empty($sessionId)) { |
||
| 54 | $tags[] = '((coaches))'; |
||
| 55 | $tags[] = '((general_coach))'; |
||
| 56 | $tags[] = '((general_coach_email))'; |
||
| 57 | } |
||
| 58 | |||
| 59 | return $tags; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param int $userId |
||
| 64 | * @param string $content |
||
| 65 | * @param string $courseCode |
||
| 66 | * @param int $sessionId |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public static function parseContent( |
||
| 71 | $userId, |
||
| 72 | $content, |
||
| 73 | $courseCode, |
||
| 74 | $sessionId = 0 |
||
| 75 | ) { |
||
| 76 | $readerInfo = api_get_user_info($userId, false, false, true, true); |
||
| 77 | $courseInfo = api_get_course_info($courseCode); |
||
| 78 | $teacherList = CourseManager::getTeacherListFromCourseCodeToString($courseInfo['code']); |
||
| 79 | |||
| 80 | $data = []; |
||
| 81 | $data['user_name'] = ''; |
||
| 82 | $data['user_firstname'] = ''; |
||
| 83 | $data['user_lastname'] = ''; |
||
| 84 | $data['user_official_code'] = ''; |
||
| 85 | $data['user_email'] = ''; |
||
| 86 | if (!empty($readerInfo)) { |
||
| 87 | $data['user_name'] = $readerInfo['username']; |
||
| 88 | $data['user_email'] = $readerInfo['email']; |
||
| 89 | $data['user_firstname'] = $readerInfo['firstname']; |
||
| 90 | $data['user_lastname'] = $readerInfo['lastname']; |
||
| 91 | $data['user_official_code'] = $readerInfo['official_code']; |
||
| 92 | } |
||
| 93 | |||
| 94 | $data['course_title'] = $courseInfo['name']; |
||
| 95 | $courseLink = api_get_course_url($courseCode, $sessionId); |
||
| 96 | $data['course_link'] = Display::url($courseLink, $courseLink); |
||
| 97 | $data['teachers'] = $teacherList; |
||
| 98 | |||
| 99 | if (!empty($readerInfo)) { |
||
| 100 | $extraField = new ExtraField('user'); |
||
| 101 | $extraFields = $extraField->get_all(['filter = ?' => 1]); |
||
| 102 | if (!empty($extraFields)) { |
||
| 103 | foreach ($extraFields as $extra) { |
||
| 104 | $data['extra_'.$extra['variable']] = ''; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | if (!empty($readerInfo['extra'])) { |
||
| 109 | foreach ($readerInfo['extra'] as $extra) { |
||
| 110 | if (isset($extra['value'])) { |
||
| 111 | /** @var \Chamilo\CoreBundle\Entity\ExtraFieldValues $value */ |
||
| 112 | $value = $extra['value']; |
||
| 113 | if ($value instanceof ExtraFieldValues) { |
||
| 114 | $field = $value->getField(); |
||
| 115 | if ($field) { |
||
| 116 | $data['extra_'.$field->getVariable()] = $value->getValue(); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | if (!empty($sessionId)) { |
||
| 125 | $sessionInfo = api_get_session_info($sessionId); |
||
| 126 | $coaches = CourseManager::get_coachs_from_course_to_string( |
||
| 127 | $sessionId, |
||
| 128 | $courseInfo['real_id'] |
||
| 129 | ); |
||
| 130 | |||
| 131 | $generalCoach = api_get_user_info($sessionInfo['id_coach']); |
||
| 132 | |||
| 133 | $data['coaches'] = $coaches; |
||
| 134 | $data['general_coach'] = $generalCoach['complete_name']; |
||
| 135 | $data['general_coach_email'] = $generalCoach['email']; |
||
| 136 | } |
||
| 137 | |||
| 138 | $tags = self::getTags($sessionId); |
||
| 139 | foreach ($tags as $tag) { |
||
| 140 | $simpleTag = str_replace(['((', '))'], '', $tag); |
||
| 141 | $value = isset($data[$simpleTag]) ? $data[$simpleTag] : ''; |
||
| 142 | $content = str_replace($tag, $value, $content); |
||
| 143 | } |
||
| 144 | |||
| 145 | return $content; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Gets all announcements from a course. |
||
| 150 | * |
||
| 151 | * @param array $course_info |
||
| 152 | * @param int $session_id |
||
| 153 | * |
||
| 154 | * @return array html with the content and count of announcements or false otherwise |
||
| 155 | */ |
||
| 156 | public static function get_all_annoucement_by_course($course_info, $session_id = 0) |
||
| 157 | { |
||
| 158 | $session_id = intval($session_id); |
||
| 159 | $courseId = $course_info['real_id']; |
||
| 160 | |||
| 161 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 162 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 163 | |||
| 164 | $sql = "SELECT DISTINCT |
||
| 165 | announcement.id, |
||
| 166 | announcement.title, |
||
| 167 | announcement.content |
||
| 168 | FROM $tbl_announcement announcement |
||
| 169 | INNER JOIN $tbl_item_property i |
||
| 170 | ON (announcement.id = i.ref AND announcement.c_id = i.c_id) |
||
| 171 | WHERE |
||
| 172 | i.tool='announcement' AND |
||
| 173 | announcement.session_id = '$session_id' AND |
||
| 174 | announcement.c_id = $courseId AND |
||
| 175 | i.c_id = $courseId |
||
| 176 | ORDER BY display_order DESC"; |
||
| 177 | $rs = Database::query($sql); |
||
| 178 | $num_rows = Database::num_rows($rs); |
||
| 179 | if ($num_rows > 0) { |
||
| 180 | $list = []; |
||
| 181 | while ($row = Database::fetch_array($rs)) { |
||
| 182 | $list[] = $row; |
||
| 183 | } |
||
| 184 | |||
| 185 | return $list; |
||
| 186 | } |
||
| 187 | |||
| 188 | return false; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * This functions switches the visibility a course resource |
||
| 193 | * using the visibility field in 'item_property'. |
||
| 194 | * |
||
| 195 | * @param array $courseInfo |
||
| 196 | * @param int $id ID of the element of the corresponding type |
||
| 197 | * |
||
| 198 | * @return bool False on failure, True on success |
||
| 199 | */ |
||
| 200 | public static function change_visibility_announcement($courseInfo, $id) |
||
| 201 | { |
||
| 202 | $session_id = api_get_session_id(); |
||
| 203 | $item_visibility = api_get_item_visibility( |
||
| 204 | $courseInfo, |
||
| 205 | TOOL_ANNOUNCEMENT, |
||
| 206 | $id, |
||
| 207 | $session_id |
||
| 208 | ); |
||
| 209 | if ($item_visibility == '1') { |
||
| 210 | api_item_property_update( |
||
| 211 | $courseInfo, |
||
| 212 | TOOL_ANNOUNCEMENT, |
||
| 213 | $id, |
||
| 214 | 'invisible', |
||
| 215 | api_get_user_id() |
||
| 216 | ); |
||
| 217 | } else { |
||
| 218 | api_item_property_update( |
||
| 219 | $courseInfo, |
||
| 220 | TOOL_ANNOUNCEMENT, |
||
| 221 | $id, |
||
| 222 | 'visible', |
||
| 223 | api_get_user_id() |
||
| 224 | ); |
||
| 225 | } |
||
| 226 | |||
| 227 | return true; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Deletes an announcement. |
||
| 232 | * |
||
| 233 | * @param array $courseInfo the course array |
||
| 234 | * @param int $id the announcement id |
||
| 235 | */ |
||
| 236 | public static function delete_announcement($courseInfo, $id) |
||
| 237 | { |
||
| 238 | api_item_property_update( |
||
| 239 | $courseInfo, |
||
| 240 | TOOL_ANNOUNCEMENT, |
||
| 241 | $id, |
||
| 242 | 'delete', |
||
| 243 | api_get_user_id() |
||
| 244 | ); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Deletes all announcements by course. |
||
| 249 | * |
||
| 250 | * @param array $courseInfo the course array |
||
| 251 | */ |
||
| 252 | public static function delete_all_announcements($courseInfo) |
||
| 266 | ); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param string $title |
||
| 273 | * @param int $courseId |
||
| 274 | * @param int $sessionId |
||
| 275 | * @param int $visibility 1 or 0 |
||
| 276 | * |
||
| 277 | * @return mixed |
||
| 278 | */ |
||
| 279 | public static function getAnnouncementsByTitle( |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param int $announcementId |
||
| 312 | * @param int $courseId |
||
| 313 | * @param int $userId |
||
| 314 | * |
||
| 315 | * @return array |
||
| 316 | */ |
||
| 317 | public static function getAnnouncementInfoById( |
||
| 318 | $announcementId, |
||
| 319 | $courseId, |
||
| 320 | $userId |
||
| 321 | ) { |
||
| 322 | if (api_is_allowed_to_edit(false, true) || |
||
| 323 | (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous()) |
||
| 324 | ) { |
||
| 325 | $dql = "SELECT a, ip |
||
| 326 | FROM ChamiloCourseBundle:CAnnouncement a |
||
| 327 | JOIN ChamiloCourseBundle:CItemProperty ip |
||
| 328 | WITH a.id = ip.ref AND a.cId = ip.course |
||
| 329 | WHERE |
||
| 330 | a.id = :announcement AND |
||
| 331 | ip.tool = 'announcement' AND |
||
| 332 | a.cId = :course |
||
| 333 | ORDER BY a.displayOrder DESC"; |
||
| 334 | } else { |
||
| 335 | $group_list = GroupManager::get_group_ids($courseId, api_get_user_id()); |
||
| 336 | |||
| 337 | if (empty($group_list)) { |
||
| 338 | $group_list[] = 0; |
||
| 339 | } |
||
| 340 | |||
| 341 | if (api_get_user_id() != 0) { |
||
| 342 | $dql = "SELECT a, ip |
||
| 343 | FROM ChamiloCourseBundle:CAnnouncement a |
||
| 344 | JOIN ChamiloCourseBundle:CItemProperty ip |
||
| 345 | WITH a.id = ip.ref AND a.cId = ip.course |
||
| 346 | WHERE |
||
| 347 | a.id = :announcement AND |
||
| 348 | ip.tool='announcement' AND |
||
| 349 | ( |
||
| 350 | ip.toUser = $userId OR |
||
| 351 | ip.group IN ('0', '".implode("', '", $group_list)."') OR |
||
| 352 | ip.group IS NULL |
||
| 353 | ) AND |
||
| 354 | ip.visibility = '1' AND |
||
| 355 | ip.course = :course |
||
| 356 | ORDER BY a.displayOrder DESC"; |
||
| 357 | } else { |
||
| 358 | $dql = "SELECT a, ip |
||
| 359 | FROM ChamiloCourseBundle:CAnnouncement a |
||
| 360 | JOIN ChamiloCourseBundle:CItemProperty ip |
||
| 361 | WITH a.id = ip.ref AND a.cId = ip.course |
||
| 362 | WHERE |
||
| 363 | a.id = :announcement AND |
||
| 364 | ip.tool = 'announcement' AND |
||
| 365 | (ip.group = '0' OR ip.group IS NULL) AND |
||
| 366 | ip.visibility = '1' AND |
||
| 367 | ip.course = :course"; |
||
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | $qb = Database::getManager()->createQuery($dql); |
||
| 372 | $result = $qb->execute( |
||
| 373 | [ |
||
| 374 | 'announcement' => $announcementId, |
||
| 375 | 'course' => $courseId, |
||
| 376 | ] |
||
| 377 | ); |
||
| 378 | |||
| 379 | if (empty($result)) { |
||
| 380 | return []; |
||
| 381 | } |
||
| 382 | |||
| 383 | return [ |
||
| 384 | 'announcement' => $result[0], |
||
| 385 | 'item_property' => $result[1], |
||
| 386 | ]; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Displays one specific announcement. |
||
| 391 | * |
||
| 392 | * @param int $id, the id of the announcement you want to display |
||
| 393 | * |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | public static function displayAnnouncement($id) |
||
| 397 | { |
||
| 398 | if ($id != strval(intval($id))) { |
||
| 399 | return null; |
||
| 400 | } |
||
| 401 | |||
| 402 | global $charset; |
||
| 403 | |||
| 404 | $html = ''; |
||
| 405 | $result = self::getAnnouncementInfoById( |
||
| 406 | $id, |
||
| 407 | api_get_course_int_id(), |
||
| 408 | api_get_user_id() |
||
| 409 | ); |
||
| 410 | /** @var CAnnouncement $announcement */ |
||
| 411 | $announcement = $result['announcement']; |
||
| 412 | /** @var CItemProperty $itemProperty */ |
||
| 413 | $itemProperty = $result['item_property']; |
||
| 414 | |||
| 415 | if (empty($announcement) || empty($itemProperty)) { |
||
| 416 | return ''; |
||
| 417 | } |
||
| 418 | |||
| 419 | $title = $announcement->getTitle(); |
||
| 420 | $content = $announcement->getContent(); |
||
| 421 | |||
| 422 | $html .= "<table height=\"100\" width=\"100%\" cellpadding=\"5\" cellspacing=\"0\" class=\"data_table\">"; |
||
| 423 | $html .= "<tr><td><h2>".$title."</h2></td></tr>"; |
||
| 424 | |||
| 425 | if (api_is_allowed_to_edit(false, true) || |
||
| 426 | (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous()) |
||
| 427 | ) { |
||
| 428 | $modify_icons = "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=modify&id=".$id."\">". |
||
| 429 | Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL)."</a>"; |
||
| 430 | |||
| 431 | $image_visibility = 'invisible'; |
||
| 432 | $alt_visibility = get_lang('Visible'); |
||
| 433 | if ($itemProperty->getVisibility() === 1) { |
||
| 434 | $image_visibility = 'visible'; |
||
| 435 | $alt_visibility = get_lang('Hide'); |
||
| 436 | } |
||
| 437 | global $stok; |
||
| 438 | $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=showhide&id=".$id."&sec_token=".$stok."\">". |
||
| 439 | Display::return_icon($image_visibility.'.png', $alt_visibility, '', ICON_SIZE_SMALL)."</a>"; |
||
| 440 | |||
| 441 | if (api_is_allowed_to_edit(false, true)) { |
||
| 442 | $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;\">". |
||
| 443 | Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL). |
||
| 444 | "</a>"; |
||
| 445 | } |
||
| 446 | $html .= "<tr><th style='text-align:right'>$modify_icons</th></tr>"; |
||
| 447 | } |
||
| 448 | |||
| 449 | //$toUser = $itemProperty->getToUser(); |
||
| 450 | $toUserId = api_get_user_id(); |
||
| 451 | $content = self::parseContent( |
||
| 452 | $toUserId, |
||
| 453 | $content, |
||
| 454 | api_get_course_id(), |
||
| 455 | api_get_session_id() |
||
| 456 | ); |
||
| 457 | |||
| 458 | $html .= "<tr><td>$content</td></tr>"; |
||
| 459 | $html .= "<tr>"; |
||
| 460 | $html .= "<td class=\"announcements_datum\">".get_lang('LastUpdateDate')." : "; |
||
| 461 | $lastEdit = $itemProperty->getLasteditDate(); |
||
| 462 | $html .= Display::dateToStringAgoAndLongDate($lastEdit); |
||
| 463 | $html .= "</td></tr>"; |
||
| 464 | |||
| 465 | $allow = !api_get_configuration_value('hide_announcement_sent_to_users_info'); |
||
| 466 | if (api_is_allowed_to_edit(false, true) && $allow) { |
||
| 467 | $sent_to = self::sent_to('announcement', $id); |
||
| 468 | $sentToForm = self::sent_to_form($sent_to); |
||
| 469 | $html .= Display::tag( |
||
| 470 | 'td', |
||
| 471 | get_lang('SentTo').': '.$sentToForm, |
||
| 472 | ['class' => 'announcements_datum'] |
||
| 473 | ); |
||
| 474 | } |
||
| 475 | $attachment_list = self::get_attachment($id); |
||
| 476 | |||
| 477 | if (count($attachment_list) > 0) { |
||
| 478 | $html .= "<tr><td>"; |
||
| 479 | $realname = $attachment_list['path']; |
||
| 480 | $user_filename = $attachment_list['filename']; |
||
| 481 | $full_file_name = 'download.php?'.api_get_cidreq().'&file='.$realname; |
||
| 482 | $html .= '<br/>'; |
||
| 483 | $html .= Display::return_icon('attachment.gif', get_lang('Attachment')); |
||
| 484 | $html .= '<a href="'.$full_file_name.' "> '.$user_filename.' </a>'; |
||
| 485 | $html .= ' - <span class="forum_attach_comment" >'.$attachment_list['comment'].'</span>'; |
||
| 486 | if (api_is_allowed_to_edit(false, true)) { |
||
| 487 | $url = api_get_self()."?".api_get_cidreq(). |
||
| 488 | "&action=delete_attachment&id_attach=".$attachment_list['id']."&sec_token=".$stok; |
||
| 489 | $html .= Display::url( |
||
| 490 | Display::return_icon( |
||
| 491 | 'delete.png', |
||
| 492 | get_lang('Delete'), |
||
| 493 | '', |
||
| 494 | 16 |
||
| 495 | ), |
||
| 496 | $url |
||
| 497 | ); |
||
| 498 | } |
||
| 499 | $html .= '</td></tr>'; |
||
| 500 | } |
||
| 501 | $html .= "</table>"; |
||
| 502 | |||
| 503 | return $html; |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param array $courseInfo |
||
| 508 | * |
||
| 509 | * @return int |
||
| 510 | */ |
||
| 511 | public static function getLastAnnouncementOrder($courseInfo) |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Store an announcement in the database (including its attached file if any). |
||
| 539 | * |
||
| 540 | * @param array $courseInfo |
||
| 541 | * @param int $sessionId |
||
| 542 | * @param string $title Announcement title (pure text) |
||
| 543 | * @param string $newContent Content of the announcement (can be HTML) |
||
| 544 | * @param array $sentTo Array of users and groups to send the announcement to |
||
| 545 | * @param array $file uploaded file $_FILES |
||
| 546 | * @param string $file_comment Comment describing the attachment |
||
| 547 | * @param string $end_date |
||
| 548 | * @param bool $sendToUsersInSession |
||
| 549 | * @param int $authorId |
||
| 550 | * |
||
| 551 | * @return int false on failure, ID of the announcement on success |
||
| 552 | */ |
||
| 553 | public static function add_announcement( |
||
| 554 | $courseInfo, |
||
| 555 | $sessionId, |
||
| 556 | $title, |
||
| 557 | $newContent, |
||
| 558 | $sentTo, |
||
| 559 | $file = [], |
||
| 560 | $file_comment = null, |
||
| 561 | $end_date = null, |
||
| 562 | $sendToUsersInSession = false, |
||
| 563 | $authorId = 0 |
||
| 564 | ) { |
||
| 565 | if (empty($courseInfo)) { |
||
| 566 | return false; |
||
| 567 | } |
||
| 568 | |||
| 569 | if (!isset($courseInfo['real_id'])) { |
||
| 570 | return false; |
||
| 571 | } |
||
| 572 | |||
| 573 | $courseId = $courseInfo['real_id']; |
||
| 574 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 575 | $authorId = empty($authorId) ? api_get_user_id() : $authorId; |
||
| 576 | |||
| 577 | if (empty($end_date)) { |
||
| 578 | $end_date = api_get_utc_datetime(); |
||
| 579 | } |
||
| 580 | |||
| 581 | $order = self::getLastAnnouncementOrder($courseInfo); |
||
| 582 | |||
| 583 | // store in the table announcement |
||
| 584 | $params = [ |
||
| 585 | 'c_id' => $courseId, |
||
| 586 | 'content' => $newContent, |
||
| 587 | 'title' => $title, |
||
| 588 | 'end_date' => $end_date, |
||
| 589 | 'display_order' => $order, |
||
| 590 | 'session_id' => (int) $sessionId, |
||
| 591 | ]; |
||
| 592 | |||
| 593 | $last_id = Database::insert($tbl_announcement, $params); |
||
| 594 | |||
| 595 | if (empty($last_id)) { |
||
| 596 | return false; |
||
| 597 | } else { |
||
| 598 | $sql = "UPDATE $tbl_announcement SET id = iid WHERE iid = $last_id"; |
||
| 599 | Database::query($sql); |
||
| 600 | |||
| 601 | if (!empty($file)) { |
||
| 602 | self::add_announcement_attachment_file( |
||
| 603 | $last_id, |
||
| 604 | $file_comment, |
||
| 605 | $_FILES['user_upload'] |
||
| 606 | ); |
||
| 607 | } |
||
| 608 | |||
| 609 | // store in item_property (first the groups, then the users |
||
| 610 | if (empty($sentTo) || |
||
| 611 | (!empty($sentTo) && isset($sentTo[0]) && $sentTo[0] == 'everyone') |
||
| 612 | ) { |
||
| 613 | // The message is sent to EVERYONE, so we set the group to 0 |
||
| 614 | api_item_property_update( |
||
| 615 | $courseInfo, |
||
| 616 | TOOL_ANNOUNCEMENT, |
||
| 617 | $last_id, |
||
| 618 | 'AnnouncementAdded', |
||
| 619 | $authorId, |
||
| 620 | '0', |
||
| 621 | null, |
||
| 622 | null, |
||
| 623 | null, |
||
| 624 | $sessionId |
||
| 625 | ); |
||
| 626 | } else { |
||
| 627 | $send_to = CourseManager::separateUsersGroups($sentTo); |
||
| 628 | $batchSize = 20; |
||
| 629 | $em = Database::getManager(); |
||
| 630 | // Storing the selected groups |
||
| 631 | if (is_array($send_to['groups']) && |
||
| 632 | !empty($send_to['groups']) |
||
| 633 | ) { |
||
| 634 | $counter = 1; |
||
| 635 | foreach ($send_to['groups'] as $group) { |
||
| 636 | $groupInfo = GroupManager::get_group_properties($group); |
||
| 637 | api_item_property_update( |
||
| 638 | $courseInfo, |
||
| 639 | TOOL_ANNOUNCEMENT, |
||
| 640 | $last_id, |
||
| 641 | 'AnnouncementAdded', |
||
| 642 | $authorId, |
||
| 643 | $groupInfo |
||
| 644 | ); |
||
| 645 | |||
| 646 | if (($counter % $batchSize) === 0) { |
||
| 647 | $em->flush(); |
||
| 648 | $em->clear(); |
||
| 649 | } |
||
| 650 | $counter++; |
||
| 651 | } |
||
| 652 | } |
||
| 653 | |||
| 654 | // Storing the selected users |
||
| 655 | if (is_array($send_to['users'])) { |
||
| 656 | $counter = 1; |
||
| 657 | foreach ($send_to['users'] as $user) { |
||
| 658 | api_item_property_update( |
||
| 659 | $courseInfo, |
||
| 660 | TOOL_ANNOUNCEMENT, |
||
| 661 | $last_id, |
||
| 662 | 'AnnouncementAdded', |
||
| 663 | $authorId, |
||
| 664 | '', |
||
| 665 | $user |
||
| 666 | ); |
||
| 667 | |||
| 668 | if (($counter % $batchSize) === 0) { |
||
| 669 | $em->flush(); |
||
| 670 | $em->clear(); |
||
| 671 | } |
||
| 672 | $counter++; |
||
| 673 | } |
||
| 674 | } |
||
| 675 | } |
||
| 676 | |||
| 677 | if ($sendToUsersInSession) { |
||
| 678 | self::addAnnouncementToAllUsersInSessions($last_id); |
||
| 679 | } |
||
| 680 | |||
| 681 | return $last_id; |
||
| 682 | } |
||
| 683 | } |
||
| 684 | |||
| 685 | /** |
||
| 686 | * @param string $title |
||
| 687 | * @param string $newContent |
||
| 688 | * @param int $groupId |
||
| 689 | * @param array $to_users |
||
| 690 | * @param array $file |
||
| 691 | * @param string $file_comment |
||
| 692 | * @param bool $sendToUsersInSession |
||
| 693 | * |
||
| 694 | * @return bool|int |
||
| 695 | */ |
||
| 696 | public static function addGroupAnnouncement( |
||
| 794 | } |
||
| 795 | |||
| 796 | /** |
||
| 797 | * This function stores the announcement item in the announcement table |
||
| 798 | * and updates the item_property table. |
||
| 799 | * |
||
| 800 | * @param int $id id of the announcement |
||
| 801 | * @param string $title |
||
| 802 | * @param string $newContent |
||
| 803 | * @param array $to users that will receive the announcement |
||
| 804 | * @param mixed $file attachment |
||
| 805 | * @param string $file_comment file comment |
||
| 806 | * @param bool $sendToUsersInSession |
||
| 807 | */ |
||
| 808 | public static function edit_announcement( |
||
| 809 | $id, |
||
| 810 | $title, |
||
| 811 | $newContent, |
||
| 812 | $to, |
||
| 813 | $file = [], |
||
| 814 | $file_comment = '', |
||
| 815 | $sendToUsersInSession = false |
||
| 816 | ) { |
||
| 817 | $courseInfo = api_get_course_info(); |
||
| 818 | $courseId = api_get_course_int_id(); |
||
| 819 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 820 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 821 | $id = (int) $id; |
||
| 822 | |||
| 823 | $params = [ |
||
| 824 | 'title' => $title, |
||
| 825 | 'content' => $newContent, |
||
| 826 | ]; |
||
| 827 | |||
| 828 | Database::update( |
||
| 829 | $table, |
||
| 830 | $params, |
||
| 831 | ['c_id = ? AND id = ?' => [$courseId, $id]] |
||
| 832 | ); |
||
| 833 | |||
| 834 | // save attachment file |
||
| 835 | $row_attach = self::get_attachment($id); |
||
| 836 | |||
| 837 | $id_attach = 0; |
||
| 838 | if ($row_attach) { |
||
| 839 | $id_attach = (int) $row_attach['id']; |
||
| 840 | } |
||
| 841 | |||
| 842 | if (!empty($file)) { |
||
| 843 | if (empty($id_attach)) { |
||
| 844 | self::add_announcement_attachment_file( |
||
| 845 | $id, |
||
| 846 | $file_comment, |
||
| 847 | $file |
||
| 848 | ); |
||
| 849 | } else { |
||
| 850 | self::edit_announcement_attachment_file( |
||
| 851 | $id_attach, |
||
| 852 | $file, |
||
| 853 | $file_comment |
||
| 854 | ); |
||
| 855 | } |
||
| 856 | } |
||
| 857 | |||
| 858 | // We remove everything from item_property for this |
||
| 859 | $sql = "DELETE FROM $tbl_item_property |
||
| 860 | WHERE c_id = $courseId AND ref='$id' AND tool='announcement'"; |
||
| 861 | Database::query($sql); |
||
| 862 | |||
| 863 | if ($sendToUsersInSession) { |
||
| 864 | self::addAnnouncementToAllUsersInSessions($id); |
||
| 865 | } |
||
| 866 | |||
| 867 | // store in item_property (first the groups, then the users |
||
| 868 | if (!empty($to)) { |
||
| 869 | // !is_null($to): when no user is selected we send it to everyone |
||
| 870 | $send_to = CourseManager::separateUsersGroups($to); |
||
| 871 | |||
| 872 | // storing the selected groups |
||
| 873 | if (is_array($send_to['groups'])) { |
||
| 874 | foreach ($send_to['groups'] as $group) { |
||
| 875 | $groupInfo = GroupManager::get_group_properties($group); |
||
| 876 | if (empty($groupInfo)) { |
||
| 877 | // Probably the group id and iid are different try checking the iid |
||
| 878 | $groupInfo = GroupManager::get_group_properties($group, true); |
||
| 879 | } |
||
| 880 | if ($groupInfo) { |
||
| 881 | api_item_property_update( |
||
| 882 | $courseInfo, |
||
| 883 | TOOL_ANNOUNCEMENT, |
||
| 884 | $id, |
||
| 885 | 'AnnouncementUpdated', |
||
| 886 | api_get_user_id(), |
||
| 887 | $groupInfo |
||
| 888 | ); |
||
| 889 | } |
||
| 890 | } |
||
| 891 | } |
||
| 892 | |||
| 893 | // storing the selected users |
||
| 894 | if (is_array($send_to['users'])) { |
||
| 895 | foreach ($send_to['users'] as $user) { |
||
| 896 | api_item_property_update( |
||
| 897 | $courseInfo, |
||
| 898 | TOOL_ANNOUNCEMENT, |
||
| 899 | $id, |
||
| 900 | 'AnnouncementUpdated', |
||
| 901 | api_get_user_id(), |
||
| 902 | 0, |
||
| 903 | $user |
||
| 904 | ); |
||
| 905 | } |
||
| 906 | } |
||
| 907 | |||
| 908 | // Send to everyone |
||
| 909 | if (isset($to[0]) && $to[0] === 'everyone') { |
||
| 910 | api_item_property_update( |
||
| 911 | $courseInfo, |
||
| 912 | TOOL_ANNOUNCEMENT, |
||
| 913 | $id, |
||
| 914 | 'AnnouncementUpdated', |
||
| 915 | api_get_user_id(), |
||
| 916 | 0 |
||
| 917 | ); |
||
| 918 | } |
||
| 919 | } else { |
||
| 920 | // the message is sent to everyone, so we set the group to 0 |
||
| 921 | api_item_property_update( |
||
| 922 | $courseInfo, |
||
| 923 | TOOL_ANNOUNCEMENT, |
||
| 924 | $id, |
||
| 925 | 'AnnouncementUpdated', |
||
| 926 | api_get_user_id(), |
||
| 927 | 0 |
||
| 928 | ); |
||
| 929 | } |
||
| 930 | } |
||
| 931 | |||
| 932 | /** |
||
| 933 | * @param int $announcementId |
||
| 934 | */ |
||
| 935 | public static function addAnnouncementToAllUsersInSessions($announcementId) |
||
| 936 | { |
||
| 937 | $courseCode = api_get_course_id(); |
||
| 938 | $courseInfo = api_get_course_info(); |
||
| 939 | $sessionList = SessionManager::get_session_by_course(api_get_course_int_id()); |
||
| 940 | |||
| 941 | if (!empty($sessionList)) { |
||
| 942 | foreach ($sessionList as $sessionInfo) { |
||
| 943 | $sessionId = $sessionInfo['id']; |
||
| 944 | $userList = CourseManager::get_user_list_from_course_code( |
||
| 945 | $courseCode, |
||
| 946 | $sessionId |
||
| 947 | ); |
||
| 948 | |||
| 949 | if (!empty($userList)) { |
||
| 950 | foreach ($userList as $user) { |
||
| 951 | api_item_property_update( |
||
| 952 | $courseInfo, |
||
| 953 | TOOL_ANNOUNCEMENT, |
||
| 954 | $announcementId, |
||
| 955 | 'AnnouncementUpdated', |
||
| 956 | api_get_user_id(), |
||
| 957 | 0, |
||
| 958 | $user['user_id'], |
||
| 959 | 0, |
||
| 960 | 0, |
||
| 961 | $sessionId |
||
| 962 | ); |
||
| 963 | } |
||
| 964 | } |
||
| 965 | } |
||
| 966 | } |
||
| 967 | } |
||
| 968 | |||
| 969 | /** |
||
| 970 | * @param int $insert_id |
||
| 971 | * |
||
| 972 | * @return bool |
||
| 973 | */ |
||
| 974 | public static function update_mail_sent($insert_id) |
||
| 975 | { |
||
| 976 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 977 | if ($insert_id != strval(intval($insert_id))) { |
||
| 978 | return false; |
||
| 979 | } |
||
| 980 | $insert_id = intval($insert_id); |
||
| 981 | $courseId = api_get_course_int_id(); |
||
| 982 | // store the modifications in the table tbl_annoucement |
||
| 983 | $sql = "UPDATE $table SET email_sent='1' |
||
| 984 | WHERE c_id = $courseId AND id = $insert_id"; |
||
| 985 | Database::query($sql); |
||
| 986 | } |
||
| 987 | |||
| 988 | /** |
||
| 989 | * Gets all announcements from a user by course. |
||
| 990 | * |
||
| 991 | * @param string course db |
||
| 992 | * @param int user id |
||
| 993 | * |
||
| 994 | * @return array html with the content and count of announcements or false otherwise |
||
| 995 | */ |
||
| 996 | public static function get_all_annoucement_by_user_course($course_code, $user_id) |
||
| 997 | { |
||
| 998 | $course_info = api_get_course_info($course_code); |
||
| 999 | $courseId = $course_info['real_id']; |
||
| 1000 | |||
| 1001 | if (empty($user_id)) { |
||
| 1002 | return false; |
||
| 1003 | } |
||
| 1004 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 1005 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 1006 | if (!empty($user_id) && is_numeric($user_id)) { |
||
| 1007 | $user_id = (int) $user_id; |
||
| 1008 | $sql = "SELECT DISTINCT |
||
| 1009 | announcement.title, |
||
| 1010 | announcement.content, |
||
| 1011 | display_order |
||
| 1012 | FROM $tbl_announcement announcement |
||
| 1013 | INNER JOIN $tbl_item_property ip |
||
| 1014 | ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id) |
||
| 1015 | WHERE |
||
| 1016 | announcement.c_id = $courseId AND |
||
| 1017 | ip.c_id = $courseId AND |
||
| 1018 | ip.tool='announcement' AND |
||
| 1019 | ( |
||
| 1020 | ip.insert_user_id='$user_id' AND |
||
| 1021 | (ip.to_group_id='0' OR ip.to_group_id IS NULL) |
||
| 1022 | ) |
||
| 1023 | AND ip.visibility='1' |
||
| 1024 | AND announcement.session_id = 0 |
||
| 1025 | ORDER BY display_order DESC"; |
||
| 1026 | $rs = Database::query($sql); |
||
| 1027 | $num_rows = Database::num_rows($rs); |
||
| 1028 | $content = ''; |
||
| 1029 | $i = 0; |
||
| 1030 | $result = []; |
||
| 1031 | if ($num_rows > 0) { |
||
| 1032 | while ($myrow = Database::fetch_array($rs)) { |
||
| 1033 | $content .= '<strong>'.$myrow['title'].'</strong><br /><br />'; |
||
| 1034 | $content .= $myrow['content']; |
||
| 1035 | $i++; |
||
| 1036 | } |
||
| 1037 | $result['content'] = $content; |
||
| 1038 | $result['count'] = $i; |
||
| 1039 | |||
| 1040 | return $result; |
||
| 1041 | } |
||
| 1042 | |||
| 1043 | return false; |
||
| 1044 | } |
||
| 1045 | |||
| 1046 | return false; |
||
| 1047 | } |
||
| 1048 | |||
| 1049 | /** |
||
| 1050 | * Returns announcement info from its id. |
||
| 1051 | * |
||
| 1052 | * @param int $courseId |
||
| 1053 | * @param int $id |
||
| 1054 | * |
||
| 1055 | * @return array |
||
| 1056 | */ |
||
| 1057 | public static function get_by_id($courseId, $id) |
||
| 1058 | { |
||
| 1059 | $id = intval($id); |
||
| 1060 | $courseId = $courseId ? intval($courseId) : api_get_course_int_id(); |
||
| 1061 | |||
| 1062 | $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT); |
||
| 1063 | $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 1064 | |||
| 1065 | $sql = "SELECT DISTINCT |
||
| 1066 | announcement.id, |
||
| 1067 | announcement.title, |
||
| 1068 | announcement.content, |
||
| 1069 | ip.to_group_id |
||
| 1070 | FROM $tbl_announcement announcement |
||
| 1071 | INNER JOIN $tbl_item_property ip |
||
| 1072 | ON |
||
| 1073 | announcement.id = ip.ref AND |
||
| 1074 | announcement.c_id = ip.c_id |
||
| 1075 | WHERE |
||
| 1076 | announcement.c_id = $courseId AND |
||
| 1077 | ip.tool='announcement' AND |
||
| 1078 | announcement.id = $id |
||
| 1079 | "; |
||
| 1080 | |||
| 1081 | $result = Database::query($sql); |
||
| 1082 | if (Database::num_rows($result)) { |
||
| 1083 | return Database::fetch_array($result); |
||
| 1084 | } |
||
| 1085 | |||
| 1086 | return []; |
||
| 1087 | } |
||
| 1088 | |||
| 1089 | /** |
||
| 1090 | * this function gets all the groups of the course, |
||
| 1091 | * not including linked courses. |
||
| 1092 | */ |
||
| 1093 | public static function get_course_groups() |
||
| 1094 | { |
||
| 1095 | $session_id = api_get_session_id(); |
||
| 1096 | if ($session_id != 0) { |
||
| 1097 | $new_group_list = CourseManager::get_group_list_of_course( |
||
| 1098 | api_get_course_id(), |
||
| 1099 | $session_id, |
||
| 1100 | 1 |
||
| 1101 | ); |
||
| 1102 | } else { |
||
| 1103 | $new_group_list = CourseManager::get_group_list_of_course( |
||
| 1104 | api_get_course_id(), |
||
| 1105 | 0, |
||
| 1106 | 1 |
||
| 1107 | ); |
||
| 1108 | } |
||
| 1109 | |||
| 1110 | return $new_group_list; |
||
| 1111 | } |
||
| 1112 | |||
| 1113 | /** |
||
| 1114 | * This tools loads all the users and all the groups who have received |
||
| 1115 | * a specific item (in this case an announcement item). |
||
| 1116 | * |
||
| 1117 | * @param string $tool |
||
| 1118 | * @param int $id |
||
| 1119 | * @param bool $includeGroupWhenLoadingUser |
||
| 1120 | * |
||
| 1121 | * @return array |
||
| 1122 | */ |
||
| 1123 | public static function loadEditUsers($tool, $id, $includeGroupWhenLoadingUser = false) |
||
| 1124 | { |
||
| 1125 | $table = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 1126 | $tool = Database::escape_string($tool); |
||
| 1127 | $id = (int) $id; |
||
| 1128 | $courseId = api_get_course_int_id(); |
||
| 1129 | $groupId = api_get_group_id(); |
||
| 1130 | |||
| 1131 | $sql = "SELECT to_user_id, to_group_id FROM $table |
||
| 1132 | WHERE c_id = $courseId AND tool='$tool' AND ref = $id"; |
||
| 1133 | |||
| 1134 | $result = Database::query($sql); |
||
| 1135 | $to = []; |
||
| 1136 | while ($row = Database::fetch_array($result)) { |
||
| 1137 | // This is the iid of c_group_info |
||
| 1138 | $toGroup = $row['to_group_id']; |
||
| 1139 | if (empty($row['to_user_id']) && !empty($groupId) && $groupId != $toGroup) { |
||
| 1140 | //continue; |
||
| 1141 | } |
||
| 1142 | switch ($toGroup) { |
||
| 1143 | // it was send to one specific user |
||
| 1144 | case null: |
||
| 1145 | if (isset($row['to_user_id']) && !empty($row['to_user_id'])) { |
||
| 1146 | if (!in_array('USER:'.$row['to_user_id'], $to)) { |
||
| 1147 | $to[] = 'USER:'.$row['to_user_id']; |
||
| 1148 | } |
||
| 1149 | } |
||
| 1150 | break; |
||
| 1151 | // it was sent to everyone |
||
| 1152 | case 0: |
||
| 1153 | return 'everyone'; |
||
| 1154 | break; |
||
|
|
|||
| 1155 | default: |
||
| 1156 | if (isset($row['to_user_id']) && !empty($row['to_user_id'])) { |
||
| 1157 | if (!in_array('USER:'.$row['to_user_id'], $to)) { |
||
| 1158 | $to[] = 'USER:'.$row['to_user_id']; |
||
| 1159 | } |
||
| 1160 | } else { |
||
| 1161 | if (!in_array('GROUP:'.$toGroup, $to)) { |
||
| 1162 | $to[] = 'GROUP:'.$toGroup; |
||
| 1163 | } |
||
| 1164 | } |
||
| 1165 | |||
| 1166 | if ($includeGroupWhenLoadingUser) { |
||
| 1167 | if (!in_array('GROUP:'.$toGroup, $to)) { |
||
| 1168 | $to[] = 'GROUP:'.$toGroup; |
||
| 1169 | } |
||
| 1170 | } |
||
| 1171 | break; |
||
| 1172 | } |
||
| 1173 | } |
||
| 1174 | |||
| 1175 | return $to; |
||
| 1176 | } |
||
| 1177 | |||
| 1178 | /** |
||
| 1179 | * constructs the form to display all the groups and users the message has been sent to. |
||
| 1180 | * |
||
| 1181 | * @param array $sent_to_array |
||
| 1182 | * input: |
||
| 1183 | * $sent_to_array is a 2 dimensional array containing the groups and the users |
||
| 1184 | * the first level is a distinction between groups and users: |
||
| 1185 | * $sent_to_array['groups'] * and $sent_to_array['users'] |
||
| 1186 | * $sent_to_array['groups'] (resp. $sent_to_array['users']) is also an array |
||
| 1187 | * containing all the id's of the groups (resp. users) who have received this message. |
||
| 1188 | * |
||
| 1189 | * @return string |
||
| 1190 | * |
||
| 1191 | * @author Patrick Cool <patrick.cool@> |
||
| 1192 | */ |
||
| 1193 | public static function sent_to_form($sent_to_array) |
||
| 1194 | { |
||
| 1195 | // we find all the names of the groups |
||
| 1196 | $group_names = self::get_course_groups(); |
||
| 1197 | |||
| 1198 | // we count the number of users and the number of groups |
||
| 1199 | $number_users = 0; |
||
| 1200 | if (isset($sent_to_array['users'])) { |
||
| 1201 | $number_users = count($sent_to_array['users']); |
||
| 1202 | } |
||
| 1203 | $number_groups = 0; |
||
| 1204 | if (isset($sent_to_array['groups'])) { |
||
| 1205 | $number_groups = count($sent_to_array['groups']); |
||
| 1206 | } |
||
| 1207 | |||
| 1208 | $total_numbers = $number_users + $number_groups; |
||
| 1209 | |||
| 1210 | // starting the form if there is more than one user/group |
||
| 1211 | $output = []; |
||
| 1212 | if ($total_numbers > 1) { |
||
| 1213 | // outputting the name of the groups |
||
| 1214 | if (is_array($sent_to_array['groups'])) { |
||
| 1215 | foreach ($sent_to_array['groups'] as $group_id) { |
||
| 1216 | $users = GroupManager::getStudents($group_id); |
||
| 1217 | $userToArray = []; |
||
| 1218 | foreach ($users as $student) { |
||
| 1219 | $userToArray[] = $student['complete_name_with_username']; |
||
| 1220 | } |
||
| 1221 | $output[] = |
||
| 1222 | '<br />'. |
||
| 1223 | Display::label($group_names[$group_id]['name'], 'info'). |
||
| 1224 | ' '.implode(', ', $userToArray); |
||
| 1225 | } |
||
| 1226 | } |
||
| 1227 | |||
| 1228 | if (isset($sent_to_array['users'])) { |
||
| 1229 | if (is_array($sent_to_array['users'])) { |
||
| 1230 | $usersToArray = []; |
||
| 1231 | foreach ($sent_to_array['users'] as $user_id) { |
||
| 1232 | $user_info = api_get_user_info($user_id); |
||
| 1233 | $usersToArray[] = $user_info['complete_name_with_username']; |
||
| 1234 | } |
||
| 1235 | $output[] = '<br />'.Display::label(get_lang('Users')).' '.implode(', ', $usersToArray); |
||
| 1236 | } |
||
| 1237 | } |
||
| 1238 | } else { |
||
| 1239 | // there is only one user/group |
||
| 1240 | if (isset($sent_to_array['users']) && is_array($sent_to_array['users'])) { |
||
| 1241 | $user_info = api_get_user_info($sent_to_array['users'][0]); |
||
| 1242 | $output[] = api_get_person_name($user_info['firstname'], $user_info['lastname']); |
||
| 1243 | } |
||
| 1244 | if (isset($sent_to_array['groups']) && |
||
| 1245 | is_array($sent_to_array['groups']) && |
||
| 1246 | isset($sent_to_array['groups'][0]) && |
||
| 1247 | $sent_to_array['groups'][0] !== 0 |
||
| 1248 | ) { |
||
| 1249 | $group_id = $sent_to_array['groups'][0]; |
||
| 1250 | |||
| 1251 | $users = GroupManager::getStudents($group_id); |
||
| 1252 | $userToArray = []; |
||
| 1253 | foreach ($users as $student) { |
||
| 1254 | $userToArray[] = $student['complete_name_with_username']; |
||
| 1255 | } |
||
| 1256 | $output[] = |
||
| 1257 | '<br />'. |
||
| 1258 | Display::label($group_names[$group_id]['name'], 'info'). |
||
| 1259 | ' '.implode(', ', $userToArray); |
||
| 1260 | } |
||
| 1261 | if (empty($sent_to_array['groups']) && empty($sent_to_array['users'])) { |
||
| 1262 | $output[] = " ".get_lang('Everybody'); |
||
| 1263 | } |
||
| 1264 | } |
||
| 1265 | |||
| 1266 | if (!empty($output)) { |
||
| 1267 | $output = array_filter($output); |
||
| 1268 | if (count($output) > 0) { |
||
| 1269 | $output = implode('<br />', $output); |
||
| 1270 | } |
||
| 1271 | |||
| 1272 | return $output; |
||
| 1273 | } |
||
| 1274 | } |
||
| 1275 | |||
| 1276 | /** |
||
| 1277 | * Returns all the users and all the groups a specific announcement item |
||
| 1278 | * has been sent to. |
||
| 1279 | * |
||
| 1280 | * @param string The tool (announcement, agenda, ...) |
||
| 1281 | * @param int ID of the element of the corresponding type |
||
| 1282 | * |
||
| 1283 | * @return array Array of users and groups to whom the element has been sent |
||
| 1284 | */ |
||
| 1285 | public static function sent_to($tool, $id) |
||
| 1286 | { |
||
| 1287 | $table = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 1288 | $tool = Database::escape_string($tool); |
||
| 1289 | $id = (int) $id; |
||
| 1290 | |||
| 1291 | $sent_to_group = []; |
||
| 1292 | $sent_to = []; |
||
| 1293 | $courseId = api_get_course_int_id(); |
||
| 1294 | |||
| 1295 | $sql = "SELECT to_group_id, to_user_id |
||
| 1296 | FROM $table |
||
| 1297 | WHERE c_id = $courseId AND tool = '$tool' AND ref=".$id; |
||
| 1298 | $result = Database::query($sql); |
||
| 1299 | |||
| 1300 | while ($row = Database::fetch_array($result)) { |
||
| 1301 | // if to_user_id <> 0 then it is sent to a specific user |
||
| 1302 | if ($row['to_user_id'] != 0) { |
||
| 1303 | $sent_to_user[] = $row['to_user_id']; |
||
| 1304 | continue; |
||
| 1305 | } |
||
| 1306 | |||
| 1307 | // if to_group_id is null then it is sent to a specific user |
||
| 1308 | // if to_group_id = 0 then it is sent to everybody |
||
| 1309 | if ($row['to_group_id'] != 0) { |
||
| 1310 | $sent_to_group[] = $row['to_group_id']; |
||
| 1311 | } |
||
| 1312 | } |
||
| 1313 | |||
| 1314 | if (isset($sent_to_group)) { |
||
| 1315 | $sent_to['groups'] = $sent_to_group; |
||
| 1316 | } |
||
| 1317 | |||
| 1318 | if (isset($sent_to_user)) { |
||
| 1319 | $sent_to['users'] = $sent_to_user; |
||
| 1320 | } |
||
| 1321 | |||
| 1322 | return $sent_to; |
||
| 1323 | } |
||
| 1324 | |||
| 1325 | /** |
||
| 1326 | * Show a list with all the attachments according to the post's id. |
||
| 1327 | * |
||
| 1328 | * @param int $announcementId |
||
| 1329 | * |
||
| 1330 | * @return array with the post info |
||
| 1331 | * |
||
| 1332 | * @author Arthur Portugal |
||
| 1333 | * |
||
| 1334 | * @version November 2009, dokeos 1.8.6.2 |
||
| 1335 | */ |
||
| 1336 | public static function get_attachment($announcementId) |
||
| 1337 | { |
||
| 1338 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); |
||
| 1339 | $announcementId = intval($announcementId); |
||
| 1340 | $courseId = api_get_course_int_id(); |
||
| 1341 | $row = []; |
||
| 1342 | $sql = 'SELECT id, path, filename, comment |
||
| 1343 | FROM '.$table.' |
||
| 1344 | WHERE c_id = '.$courseId.' AND announcement_id = '.$announcementId; |
||
| 1345 | $result = Database::query($sql); |
||
| 1346 | if (Database::num_rows($result) != 0) { |
||
| 1347 | $row = Database::fetch_array($result, 'ASSOC'); |
||
| 1348 | } |
||
| 1349 | |||
| 1350 | return $row; |
||
| 1351 | } |
||
| 1352 | |||
| 1353 | /** |
||
| 1354 | * This function add a attachment file into announcement. |
||
| 1355 | * |
||
| 1356 | * @param int announcement id |
||
| 1357 | * @param string file comment |
||
| 1358 | * @param array uploaded file $_FILES |
||
| 1359 | * |
||
| 1360 | * @return int -1 if failed, 0 if unknown (should not happen), 1 if success |
||
| 1361 | */ |
||
| 1362 | public static function add_announcement_attachment_file( |
||
| 1363 | $announcement_id, |
||
| 1364 | $file_comment, |
||
| 1365 | $file |
||
| 1366 | ) { |
||
| 1367 | $courseInfo = api_get_course_info(); |
||
| 1368 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); |
||
| 1369 | $return = 0; |
||
| 1370 | $announcement_id = intval($announcement_id); |
||
| 1371 | $courseId = api_get_course_int_id(); |
||
| 1372 | |||
| 1373 | if (is_array($file) && $file['error'] == 0) { |
||
| 1374 | // TODO: This path is obsolete. The new document repository scheme should be kept in mind here. |
||
| 1375 | $courseDir = $courseInfo['path'].'/upload/announcements'; |
||
| 1376 | $sys_course_path = api_get_path(SYS_COURSE_PATH); |
||
| 1377 | $updir = $sys_course_path.$courseDir; |
||
| 1378 | |||
| 1379 | // Try to add an extension to the file if it hasn't one |
||
| 1380 | $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']); |
||
| 1381 | // user's file name |
||
| 1382 | $file_name = $file['name']; |
||
| 1383 | |||
| 1384 | if (!filter_extension($new_file_name)) { |
||
| 1385 | $return = -1; |
||
| 1386 | echo Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error'); |
||
| 1387 | } else { |
||
| 1388 | $new_file_name = uniqid(''); |
||
| 1389 | $new_path = $updir.'/'.$new_file_name; |
||
| 1390 | |||
| 1391 | // This file is copy here but its cleaned in api_mail_html in api.lib.php |
||
| 1392 | copy($file['tmp_name'], $new_path); |
||
| 1393 | |||
| 1394 | $params = [ |
||
| 1395 | 'c_id' => $courseId, |
||
| 1396 | 'filename' => $file_name, |
||
| 1397 | 'comment' => $file_comment, |
||
| 1398 | 'path' => $new_file_name, |
||
| 1399 | 'announcement_id' => $announcement_id, |
||
| 1400 | 'size' => intval($file['size']), |
||
| 1401 | ]; |
||
| 1402 | |||
| 1403 | $insertId = Database::insert($table, $params); |
||
| 1404 | if ($insertId) { |
||
| 1405 | $sql = "UPDATE $table SET id = iid |
||
| 1406 | WHERE iid = $insertId"; |
||
| 1407 | Database::query($sql); |
||
| 1408 | } |
||
| 1409 | |||
| 1410 | $return = 1; |
||
| 1411 | } |
||
| 1412 | } |
||
| 1413 | |||
| 1414 | return $return; |
||
| 1415 | } |
||
| 1416 | |||
| 1417 | /** |
||
| 1418 | * This function edit a attachment file into announcement. |
||
| 1419 | * |
||
| 1420 | * @param int attach id |
||
| 1421 | * @param array uploaded file $_FILES |
||
| 1422 | * @param string file comment |
||
| 1423 | * |
||
| 1424 | * @return int |
||
| 1425 | */ |
||
| 1426 | public static function edit_announcement_attachment_file( |
||
| 1484 | } |
||
| 1485 | |||
| 1486 | /** |
||
| 1487 | * This function delete a attachment file by id. |
||
| 1488 | * |
||
| 1489 | * @param int $id attachment file Id |
||
| 1490 | * |
||
| 1491 | * @return bool |
||
| 1492 | */ |
||
| 1493 | public static function delete_announcement_attachment_file($id) |
||
| 1494 | { |
||
| 1495 | $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); |
||
| 1496 | $id = intval($id); |
||
| 1497 | $courseId = api_get_course_int_id(); |
||
| 1498 | if (empty($courseId) || empty($id)) { |
||
| 1499 | return false; |
||
| 1500 | } |
||
| 1501 | $sql = "DELETE FROM $table |
||
| 1502 | WHERE c_id = $courseId AND id = $id"; |
||
| 1503 | Database::query($sql); |
||
| 1504 | |||
| 1505 | return true; |
||
| 1506 | } |
||
| 1507 | |||
| 1508 | /** |
||
| 1509 | * @param array $courseInfo |
||
| 1510 | * @param int $sessionId |
||
| 1511 | * @param int $announcementId |
||
| 1512 | * @param bool $sendToUsersInSession |
||
| 1513 | * @param bool $sendToDrhUsers |
||
| 1514 | * @param Monolog\Handler\HandlerInterface logger |
||
| 1515 | * @param int $senderId |
||
| 1516 | * |
||
| 1517 | * @return array |
||
| 1518 | */ |
||
| 1519 | public static function sendEmail( |
||
| 1520 | $courseInfo, |
||
| 1521 | $sessionId, |
||
| 1522 | $announcementId, |
||
| 1523 | $sendToUsersInSession = false, |
||
| 1524 | $sendToDrhUsers = false, |
||
| 1525 | $logger = null, |
||
| 1526 | $senderId = 0 |
||
| 1527 | ) { |
||
| 1528 | $email = new AnnouncementEmail($courseInfo, $sessionId, $announcementId, $logger); |
||
| 1529 | |||
| 1530 | return $email->send($sendToUsersInSession, $sendToDrhUsers, $senderId); |
||
| 1531 | } |
||
| 1532 | |||
| 1533 | /** |
||
| 1534 | * @param $stok |
||
| 1535 | * @param $announcement_number |
||
| 1536 | * @param bool $getCount |
||
| 1537 | * @param null $start |
||
| 1538 | * @param null $limit |
||
| 1539 | * @param string $sidx |
||
| 1540 | * @param string $sord |
||
| 1541 | * @param string $titleToSearch |
||
| 1542 | * @param int $userIdToSearch |
||
| 1543 | * @param int $userId |
||
| 1544 | * @param int $courseId |
||
| 1545 | * @param int $sessionId |
||
| 1546 | * |
||
| 1547 | * @return array |
||
| 1548 | */ |
||
| 1549 | public static function getAnnouncements( |
||
| 1550 | $stok, |
||
| 1551 | $announcement_number, |
||
| 1552 | $getCount = false, |
||
| 1553 | $start = null, |
||
| 1554 | $limit = null, |
||
| 1555 | $sidx = '', |
||
| 1556 | $sord = '', |
||
| 1557 | $titleToSearch = '', |
||
| 1558 | $userIdToSearch = 0, |
||
| 1559 | $userId = 0, |
||
| 1560 | $courseId = 0, |
||
| 1561 | $sessionId = 0 |
||
| 1562 | ) { |
||
| 1563 | $user_id = $userId ?: api_get_user_id(); |
||
| 1564 | $group_id = api_get_group_id(); |
||
| 1565 | $session_id = $sessionId ?: api_get_session_id(); |
||
| 1566 | if (empty($courseId)) { |
||
| 1567 | $courseInfo = api_get_course_info(); |
||
| 1568 | $courseId = $courseInfo['real_id']; |
||
| 1569 | } else { |
||
| 1570 | $courseId = (int) $courseId; |
||
| 1571 | $courseInfo = api_get_course_info_by_id($courseId); |
||
| 1572 | } |
||
| 1573 | |||
| 1574 | if (empty($courseInfo)) { |
||
| 1575 | return []; |
||
| 1576 | } |
||
| 1577 | |||
| 1578 | if (!empty($titleToSearch)) { |
||
| 1579 | $titleToSearch = Database::escape_string($titleToSearch); |
||
| 1580 | } |
||
| 1581 | |||
| 1582 | $result = Container::$container |
||
| 1583 | ->get('chamilo_course.entity.manager.announcement_manager') |
||
| 1584 | ->getAnnouncements( |
||
| 1585 | api_get_user_entity($user_id), |
||
| 1586 | api_get_course_entity($courseId), |
||
| 1587 | api_get_group_entity($group_id), |
||
| 1588 | api_get_session_entity($session_id), |
||
| 1589 | api_get_course_setting('allow_user_edit_announcement') === 'true', |
||
| 1590 | api_get_configuration_value('hide_base_course_announcements_in_group') === true, |
||
| 1591 | $getCount, |
||
| 1592 | $start, |
||
| 1593 | $limit, |
||
| 1594 | $titleToSearch, |
||
| 1595 | $userIdToSearch ? api_get_user_entity($userIdToSearch) : null |
||
| 1596 | ); |
||
| 1597 | |||
| 1598 | if ($getCount) { |
||
| 1599 | return $result; |
||
| 1600 | } |
||
| 1601 | |||
| 1602 | $iterator = 1; |
||
| 1603 | $bottomAnnouncement = $announcement_number; |
||
| 1604 | $displayed = []; |
||
| 1605 | $results = []; |
||
| 1606 | $actionUrl = api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.api_get_cidreq(); |
||
| 1607 | $emailIcon = '<i class="fa fa-envelope-o" title="'.get_lang('AnnounceSentByEmail').'"></i>'; |
||
| 1608 | $attachmentIcon = '<i class="fa fa-paperclip" title="'.get_lang('Attachment').'"></i>'; |
||
| 1609 | |||
| 1610 | $editIcon = Display::return_icon( |
||
| 1611 | 'edit.png', |
||
| 1612 | get_lang('Edit'), |
||
| 1613 | '', |
||
| 1614 | ICON_SIZE_SMALL |
||
| 1615 | ); |
||
| 1616 | |||
| 1617 | $editIconDisable = Display::return_icon( |
||
| 1618 | 'edit_na.png', |
||
| 1619 | get_lang('Edit'), |
||
| 1620 | '', |
||
| 1621 | ICON_SIZE_SMALL |
||
| 1622 | ); |
||
| 1623 | $deleteIcon = Display::return_icon( |
||
| 1624 | 'delete.png', |
||
| 1625 | get_lang('Delete'), |
||
| 1626 | '', |
||
| 1627 | ICON_SIZE_SMALL |
||
| 1628 | ); |
||
| 1629 | |||
| 1630 | $deleteIconDisable = Display::return_icon( |
||
| 1631 | 'delete_na.png', |
||
| 1632 | get_lang('Delete'), |
||
| 1633 | '', |
||
| 1634 | ICON_SIZE_SMALL |
||
| 1635 | ); |
||
| 1636 | |||
| 1637 | $isTutor = false; |
||
| 1638 | if (!empty($group_id)) { |
||
| 1639 | $groupInfo = GroupManager::get_group_properties(api_get_group_id()); |
||
| 1640 | //User has access in the group? |
||
| 1641 | $isTutor = GroupManager::is_tutor_of_group( |
||
| 1642 | api_get_user_id(), |
||
| 1643 | $groupInfo |
||
| 1644 | ); |
||
| 1645 | } |
||
| 1646 | |||
| 1647 | for ($z = 0; $z < count($result); $z += 2) { |
||
| 1648 | /** @var CAnnouncement $a */ |
||
| 1649 | $a = $result[$z]; |
||
| 1650 | /** @var CItemProperty $i */ |
||
| 1651 | $i = $result[$z + 1]; |
||
| 1652 | |||
| 1653 | if (in_array($a->getId(), $displayed)) { |
||
| 1654 | continue; |
||
| 1655 | } |
||
| 1656 | |||
| 1657 | $sent_to_icon = $a->getEmailSent() ? " $emailIcon" : ''; |
||
| 1658 | |||
| 1659 | $groupReference = $i->getGroup() !== null |
||
| 1660 | ? ' <span class="label label-info">'.get_lang('Group').'</span> ' |
||
| 1661 | : ''; |
||
| 1662 | $title = $a->getTitle().$groupReference.$sent_to_icon; |
||
| 1663 | $item_visibility = api_get_item_visibility($courseInfo, TOOL_ANNOUNCEMENT, $a->getId(), $sessionId); |
||
| 1664 | $i->setVisibility($item_visibility); |
||
| 1665 | |||
| 1666 | // show attachment list |
||
| 1667 | $attachment_list = self::get_attachment($a->getId()); |
||
| 1668 | |||
| 1669 | $attachment_icon = ''; |
||
| 1670 | if (count($attachment_list) > 0) { |
||
| 1671 | $attachment_icon = ' '.$attachmentIcon; |
||
| 1672 | } |
||
| 1673 | |||
| 1674 | /* TITLE */ |
||
| 1675 | $username_span = Display::tag( |
||
| 1676 | 'span', |
||
| 1677 | UserManager::formatUserFullName($i->getInsertUser()), |
||
| 1678 | ['title' => $i->getInsertUser()->getUsername()] |
||
| 1679 | ); |
||
| 1680 | |||
| 1681 | $title = Display::url( |
||
| 1682 | $title.$attachment_icon, |
||
| 1683 | $actionUrl.'&action=view&id='.$a->getId() |
||
| 1684 | ); |
||
| 1685 | |||
| 1686 | $aGroupId = $i->getGroup() ? $i->getGroup()->getId() : 0; |
||
| 1687 | |||
| 1688 | // we can edit if : we are the teacher OR the element belongs to |
||
| 1689 | // the session we are coaching OR the option to allow users to edit is on |
||
| 1690 | if (api_is_allowed_to_edit(false, true) || |
||
| 1691 | (api_is_session_general_coach() && api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $a->getId())) || |
||
| 1692 | (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous()) || |
||
| 1693 | ($aGroupId == $group_id && $isTutor) |
||
| 1694 | ) { |
||
| 1695 | $modify_icons = "<a href=\"".$actionUrl."&action=modify&id=".$a->getId()."\">".$editIcon."</a>"; |
||
| 1696 | if ($i->getVisibility() == 1) { |
||
| 1697 | $image_visibility = "visible"; |
||
| 1698 | $alt_visibility = get_lang('Hide'); |
||
| 1699 | } else { |
||
| 1700 | $image_visibility = "invisible"; |
||
| 1701 | $alt_visibility = get_lang('Visible'); |
||
| 1702 | } |
||
| 1703 | $modify_icons .= "<a href=\"".$actionUrl."&action=showhide&id=".$a->getId()."&sec_token=".$stok."\">". |
||
| 1704 | Display::return_icon($image_visibility.'.png', $alt_visibility, '', ICON_SIZE_SMALL)."</a>"; |
||
| 1705 | |||
| 1706 | // DISPLAY MOVE UP COMMAND only if it is not the top announcement |
||
| 1707 | if ($iterator != 1) { |
||
| 1708 | $modify_icons .= "<a href=\"".$actionUrl."&action=move&up=".$a->getId()."&sec_token=".$stok."\">". |
||
| 1709 | Display::return_icon('up.gif', get_lang('Up'))."</a>"; |
||
| 1710 | } else { |
||
| 1711 | $modify_icons .= Display::return_icon('up_na.gif', get_lang('Up')); |
||
| 1712 | } |
||
| 1713 | if ($iterator < $bottomAnnouncement) { |
||
| 1714 | $modify_icons .= "<a href=\"".$actionUrl."&action=move&down=".$a->getId()."&sec_token=".$stok."\">". |
||
| 1715 | Display::return_icon('down.gif', get_lang('Down'))."</a>"; |
||
| 1716 | } else { |
||
| 1717 | $modify_icons .= Display::return_icon('down_na.gif', get_lang('Down')); |
||
| 1718 | } |
||
| 1719 | if (api_is_allowed_to_edit(false, true)) { |
||
| 1720 | $modify_icons .= "<a href=\"".$actionUrl."&action=delete&id=".$a->getId()."&sec_token=".$stok."\" onclick=\"javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, api_get_system_encoding()))."')) return false;\">". |
||
| 1721 | $deleteIcon."</a>"; |
||
| 1722 | } |
||
| 1723 | $iterator++; |
||
| 1724 | } else { |
||
| 1725 | $modify_icons = Display::url( |
||
| 1726 | Display::return_icon('default.png'), |
||
| 1727 | $actionUrl.'&action=view&id='.$a->getId() |
||
| 1728 | ); |
||
| 1729 | } |
||
| 1730 | |||
| 1731 | $announcement = [ |
||
| 1732 | 'id' => $a->getId(), |
||
| 1733 | 'title' => $title, |
||
| 1734 | 'username' => $username_span, |
||
| 1735 | 'insert_date' => api_convert_and_format_date( |
||
| 1736 | $i->getInsertDate(), |
||
| 1737 | DATE_TIME_FORMAT_LONG |
||
| 1738 | ), |
||
| 1739 | 'lastedit_date' => api_convert_and_format_date( |
||
| 1740 | $i->getLasteditDate(), |
||
| 1741 | DATE_TIME_FORMAT_LONG |
||
| 1742 | ), |
||
| 1743 | 'actions' => $modify_icons, |
||
| 1744 | ]; |
||
| 1745 | |||
| 1746 | $results[] = $announcement; |
||
| 1747 | $displayed[] = $a->getId(); |
||
| 1748 | } |
||
| 1749 | |||
| 1750 | return $results; |
||
| 1751 | } |
||
| 1752 | |||
| 1753 | /** |
||
| 1754 | * @return int |
||
| 1755 | */ |
||
| 1756 | public static function getNumberAnnouncements() |
||
| 1927 | } |
||
| 1928 | } |
||
| 1929 |
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.