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