| Total Complexity | 140 |
| Total Lines | 1109 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SystemAnnouncementManager 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 SystemAnnouncementManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class SystemAnnouncementManager |
||
| 9 | { |
||
| 10 | public const VISIBLE_GUEST = 'visible_guest'; |
||
| 11 | public const VISIBLE_STUDENT = 'visible_student'; |
||
| 12 | public const VISIBLE_TEACHER = 'visible_teacher'; |
||
| 13 | // Requires DB change |
||
| 14 | public const VISIBLE_DRH = 'visible_drh'; |
||
| 15 | public const VISIBLE_SESSION_ADMIN = 'visible_session_admin'; |
||
| 16 | public const VISIBLE_STUDENT_BOSS = 'visible_boss'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @return array |
||
| 20 | */ |
||
| 21 | public static function getVisibilityList() |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param string $visibility |
||
| 42 | * |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | public static function getVisibilityCondition($visibility) |
||
| 46 | { |
||
| 47 | $list = self::getVisibilityList(); |
||
| 48 | $visibilityCondition = " AND ".self::VISIBLE_GUEST." = 1 "; |
||
| 49 | if (in_array($visibility, array_keys($list))) { |
||
| 50 | $visibilityCondition = " AND $visibility = 1 "; |
||
| 51 | } |
||
| 52 | |||
| 53 | return $visibilityCondition; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param string $visibility |
||
| 58 | * @param int $id |
||
| 59 | * @param int $start |
||
| 60 | * @param string $user_id |
||
| 61 | * |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | public static function displayAllAnnouncements( |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param int $user_id |
||
| 155 | * |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | public static function display_arrow($user_id) |
||
| 159 | { |
||
| 160 | $start = (int) $_GET['start']; |
||
| 161 | $nb_announcement = self::count_nb_announcement($start, $user_id); |
||
| 162 | $next = ((int) $_GET['start'] + 19); |
||
| 163 | $prev = ((int) $_GET['start'] - 19); |
||
| 164 | $content = ''; |
||
| 165 | if (!isset($_GET['start']) || $_GET['start'] == 0) { |
||
| 166 | if ($nb_announcement > 20) { |
||
| 167 | $content .= '<a href="news_list.php?start='.$next.'">'.get_lang('NextBis').' >> </a>'; |
||
| 168 | } |
||
| 169 | } else { |
||
| 170 | echo '<a href="news_list.php?start='.$prev.'"> << '.get_lang('Prev').'</a>'; |
||
| 171 | if ($nb_announcement > 20) { |
||
| 172 | $content .= '<a href="news_list.php?start='.$next.'">'.get_lang('NextBis').' >> </a>'; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | return $content; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param int $start |
||
| 181 | * @param string $user_id |
||
| 182 | * |
||
| 183 | * @return int |
||
| 184 | */ |
||
| 185 | public static function count_nb_announcement($start = 0, $user_id = '') |
||
| 186 | { |
||
| 187 | $start = (int) $start; |
||
| 188 | $user_selected_language = api_get_interface_language(); |
||
| 189 | $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); |
||
| 190 | $sql = 'SELECT id FROM '.$db_table.' |
||
| 191 | WHERE (lang="'.$user_selected_language.'" OR lang IS NULL) '; |
||
| 192 | |||
| 193 | $visibility = self::getCurrentUserVisibility(); |
||
| 194 | $sql .= self::getVisibilityCondition($visibility); |
||
| 195 | |||
| 196 | $current_access_url_id = 1; |
||
| 197 | if (api_is_multiple_url_enabled()) { |
||
| 198 | $current_access_url_id = api_get_current_access_url_id(); |
||
| 199 | } |
||
| 200 | $sql .= " AND access_url_id = '$current_access_url_id' "; |
||
| 201 | $sql .= 'LIMIT '.$start.', 21'; |
||
| 202 | $announcements = Database::query($sql); |
||
| 203 | $i = 0; |
||
| 204 | while ($rows = Database::fetch_array($announcements)) { |
||
| 205 | $i++; |
||
| 206 | } |
||
| 207 | |||
| 208 | return $i; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get all announcements. |
||
| 213 | * |
||
| 214 | * @return array An array with all available system announcements (as php |
||
| 215 | * objects) |
||
| 216 | */ |
||
| 217 | public static function get_all_announcements() |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Adds an announcement to the database. |
||
| 242 | * |
||
| 243 | * @param string $title Title of the announcement |
||
| 244 | * @param string $content Content of the announcement |
||
| 245 | * @param string $date_start Start date (YYYY-MM-DD HH:II: SS) |
||
| 246 | * @param string $date_end End date (YYYY-MM-DD HH:II: SS) |
||
| 247 | * @param array $visibility |
||
| 248 | * @param string $lang The language for which the announvement should be shown. Leave null for all langages |
||
| 249 | * @param int $send_mail Whether to send an e-mail to all users (1) or not (0) |
||
| 250 | * @param bool $add_to_calendar |
||
| 251 | * @param bool $sendEmailTest |
||
| 252 | * @param int $careerId |
||
| 253 | * @param int $promotionId |
||
| 254 | * @param array $groups |
||
| 255 | * |
||
| 256 | * @return mixed insert_id on success, false on failure |
||
| 257 | */ |
||
| 258 | public static function add_announcement( |
||
| 259 | $title, |
||
| 260 | $content, |
||
| 261 | $date_start, |
||
| 262 | $date_end, |
||
| 263 | $visibility, |
||
| 264 | $lang = '', |
||
| 265 | $send_mail = 0, |
||
| 266 | $add_to_calendar = false, |
||
| 267 | $sendEmailTest = false, |
||
| 268 | $careerId = 0, |
||
| 269 | $promotionId = 0, |
||
| 270 | $groups = [] |
||
| 271 | ) { |
||
| 272 | $original_content = $content; |
||
| 273 | $a_dateS = explode(' ', $date_start); |
||
| 274 | $a_arraySD = explode('-', $a_dateS[0]); |
||
| 275 | $a_arraySH = explode(':', $a_dateS[1]); |
||
| 276 | $date_start_to_compare = array_merge($a_arraySD, $a_arraySH); |
||
| 277 | |||
| 278 | $a_dateE = explode(' ', $date_end); |
||
| 279 | $a_arrayED = explode('-', $a_dateE[0]); |
||
| 280 | $a_arrayEH = explode(':', $a_dateE[1]); |
||
| 281 | $date_end_to_compare = array_merge($a_arrayED, $a_arrayEH); |
||
| 282 | |||
| 283 | $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); |
||
| 284 | |||
| 285 | if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) { |
||
| 286 | Display::addFlash( |
||
| 287 | Display::return_message(get_lang('InvalidStartDate'), 'warning') |
||
| 288 | ); |
||
| 289 | |||
| 290 | return false; |
||
| 291 | } |
||
| 292 | |||
| 293 | if (($date_end_to_compare[1] || |
||
| 294 | $date_end_to_compare[2] || |
||
| 295 | $date_end_to_compare[0]) && |
||
| 296 | !checkdate($date_end_to_compare[1], $date_end_to_compare[2], $date_end_to_compare[0]) |
||
| 297 | ) { |
||
| 298 | Display::addFlash( |
||
| 299 | Display::return_message(get_lang('InvalidEndDate'), 'warning') |
||
| 300 | ); |
||
| 301 | |||
| 302 | return false; |
||
| 303 | } |
||
| 304 | |||
| 305 | if (strlen(trim($title)) == 0) { |
||
| 306 | Display::addFlash( |
||
| 307 | Display::return_message(get_lang('InvalidTitle'), 'warning') |
||
| 308 | ); |
||
| 309 | |||
| 310 | return false; |
||
| 311 | } |
||
| 312 | |||
| 313 | $start = api_get_utc_datetime($date_start); |
||
| 314 | $end = api_get_utc_datetime($date_end); |
||
| 315 | |||
| 316 | //Fixing urls that are sent by email |
||
| 317 | //$content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content); |
||
| 318 | //$content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content); |
||
| 319 | $content = str_replace( |
||
| 320 | 'src=\"'.api_get_path(REL_HOME_PATH), |
||
| 321 | 'src=\"'.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH), |
||
| 322 | $content |
||
| 323 | ); |
||
| 324 | $content = str_replace( |
||
| 325 | 'file='.api_get_path(REL_HOME_PATH), |
||
| 326 | 'file='.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH), |
||
| 327 | $content |
||
| 328 | ); |
||
| 329 | $lang = is_null($lang) ? '' : $lang; |
||
| 330 | |||
| 331 | $current_access_url_id = 1; |
||
| 332 | if (api_is_multiple_url_enabled()) { |
||
| 333 | $current_access_url_id = api_get_current_access_url_id(); |
||
| 334 | } |
||
| 335 | |||
| 336 | $params = [ |
||
| 337 | 'title' => $title, |
||
| 338 | 'content' => $content, |
||
| 339 | 'date_start' => $start, |
||
| 340 | 'date_end' => $end, |
||
| 341 | 'lang' => $lang, |
||
| 342 | 'access_url_id' => $current_access_url_id, |
||
| 343 | ]; |
||
| 344 | |||
| 345 | if (api_get_configuration_value('allow_careers_in_global_announcements') && !empty($careerId)) { |
||
| 346 | $params['career_id'] = (int) $careerId; |
||
| 347 | $params['promotion_id'] = (int) $promotionId; |
||
| 348 | } |
||
| 349 | |||
| 350 | foreach ($visibility as $key => $value) { |
||
| 351 | $params[$key] = $value; |
||
| 352 | } |
||
| 353 | |||
| 354 | $resultId = Database::insert($db_table, $params); |
||
| 355 | |||
| 356 | if ($resultId) { |
||
|
|
|||
| 357 | if ($sendEmailTest) { |
||
| 358 | self::send_system_announcement_by_email( |
||
| 359 | $resultId, |
||
| 360 | $visibility, |
||
| 361 | true |
||
| 362 | ); |
||
| 363 | } else { |
||
| 364 | if ($send_mail == 1) { |
||
| 365 | self::send_system_announcement_by_email( |
||
| 366 | $resultId, |
||
| 367 | $visibility, |
||
| 368 | false, |
||
| 369 | $groups |
||
| 370 | ); |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | if ($add_to_calendar) { |
||
| 375 | $agenda = new Agenda('admin'); |
||
| 376 | $agenda->addEvent( |
||
| 377 | $date_start, |
||
| 378 | $date_end, |
||
| 379 | false, |
||
| 380 | $title, |
||
| 381 | $original_content, |
||
| 382 | [], |
||
| 383 | false, |
||
| 384 | null, |
||
| 385 | [], |
||
| 386 | [], |
||
| 387 | null, |
||
| 388 | '', |
||
| 389 | [], |
||
| 390 | false, |
||
| 391 | [], |
||
| 392 | $params['career_id'] ?? 0, |
||
| 393 | $params['promotion_id'] ?? 0 |
||
| 394 | ); |
||
| 395 | } |
||
| 396 | |||
| 397 | return $resultId; |
||
| 398 | } |
||
| 399 | |||
| 400 | return false; |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Makes the announcement id visible only for groups in groups_array. |
||
| 405 | * |
||
| 406 | * @param int $announcement_id |
||
| 407 | * @param array $groups array of group id |
||
| 408 | * |
||
| 409 | * @return bool |
||
| 410 | */ |
||
| 411 | public static function announcement_for_groups($announcement_id, $groups) |
||
| 412 | { |
||
| 413 | $tbl_announcement_group = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS); |
||
| 414 | $announcement_id = (int) $announcement_id; |
||
| 415 | |||
| 416 | if (empty($announcement_id)) { |
||
| 417 | return false; |
||
| 418 | } |
||
| 419 | |||
| 420 | // First delete all group associations for this announcement |
||
| 421 | $sql = "DELETE FROM $tbl_announcement_group WHERE announcement_id= $announcement_id"; |
||
| 422 | Database::query($sql); |
||
| 423 | |||
| 424 | if (!empty($groups)) { |
||
| 425 | foreach ($groups as $group_id) { |
||
| 426 | if (intval($group_id) != 0) { |
||
| 427 | $sql = "INSERT INTO $tbl_announcement_group SET |
||
| 428 | announcement_id=".$announcement_id.", |
||
| 429 | group_id=".intval($group_id); |
||
| 430 | Database::query($sql); |
||
| 431 | } |
||
| 432 | } |
||
| 433 | } |
||
| 434 | |||
| 435 | return true; |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Gets the groups of this announce. |
||
| 440 | * |
||
| 441 | * @param int announcement id |
||
| 442 | * |
||
| 443 | * @return array array of group id |
||
| 444 | */ |
||
| 445 | public static function get_announcement_groups($announcement_id) |
||
| 446 | { |
||
| 447 | $tbl_announcement_group = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS); |
||
| 448 | $tbl_group = Database::get_main_table(TABLE_USERGROUP); |
||
| 449 | //first delete all group associations for this announcement |
||
| 450 | $sql = "SELECT |
||
| 451 | g.id as group_id, |
||
| 452 | g.name as group_name |
||
| 453 | FROM $tbl_group g , $tbl_announcement_group ag |
||
| 454 | WHERE |
||
| 455 | announcement_id =".intval($announcement_id)." AND |
||
| 456 | ag.group_id = g.id"; |
||
| 457 | $res = Database::query($sql); |
||
| 458 | |||
| 459 | return Database::store_result($res); |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Updates an announcement to the database. |
||
| 464 | * |
||
| 465 | * @param int $id of the announcement |
||
| 466 | * @param string $title title of the announcement |
||
| 467 | * @param string $content content of the announcement |
||
| 468 | * @param array $date_start start date (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute) |
||
| 469 | * @param array $date_end end date of (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute) |
||
| 470 | * @param array $visibility |
||
| 471 | * @param array $lang |
||
| 472 | * @param int $send_mail |
||
| 473 | * @param bool $sendEmailTest |
||
| 474 | * @param int $careerId |
||
| 475 | * @param int $promotionId |
||
| 476 | * @param array $groups |
||
| 477 | * |
||
| 478 | * @return bool True on success, false on failure |
||
| 479 | */ |
||
| 480 | public static function update_announcement( |
||
| 481 | $id, |
||
| 482 | $title, |
||
| 483 | $content, |
||
| 484 | $date_start, |
||
| 485 | $date_end, |
||
| 486 | $visibility, |
||
| 487 | $lang = null, |
||
| 488 | $send_mail = 0, |
||
| 489 | $sendEmailTest = false, |
||
| 490 | $careerId = 0, |
||
| 491 | $promotionId = 0, |
||
| 492 | $groups = [] |
||
| 493 | ) { |
||
| 494 | $em = Database::getManager(); |
||
| 495 | $announcement = $em->find('ChamiloCoreBundle:SysAnnouncement', $id); |
||
| 496 | if (!$announcement) { |
||
| 497 | return false; |
||
| 498 | } |
||
| 499 | |||
| 500 | $a_dateS = explode(' ', $date_start); |
||
| 501 | $a_arraySD = explode('-', $a_dateS[0]); |
||
| 502 | $a_arraySH = explode(':', $a_dateS[1]); |
||
| 503 | $date_start_to_compare = array_merge($a_arraySD, $a_arraySH); |
||
| 504 | |||
| 505 | $a_dateE = explode(' ', $date_end); |
||
| 506 | $a_arrayED = explode('-', $a_dateE[0]); |
||
| 507 | $a_arrayEH = explode(':', $a_dateE[1]); |
||
| 508 | $date_end_to_compare = array_merge($a_arrayED, $a_arrayEH); |
||
| 509 | |||
| 510 | $lang = is_null($lang) ? '' : $lang; |
||
| 511 | |||
| 512 | if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) { |
||
| 513 | echo Display::return_message(get_lang('InvalidStartDate')); |
||
| 514 | |||
| 515 | return false; |
||
| 516 | } |
||
| 517 | |||
| 518 | if (($date_end_to_compare[1] || |
||
| 519 | $date_end_to_compare[2] || |
||
| 520 | $date_end_to_compare[0]) && |
||
| 521 | !checkdate($date_end_to_compare[1], $date_end_to_compare[2], $date_end_to_compare[0]) |
||
| 522 | ) { |
||
| 523 | echo Display::return_message(get_lang('InvalidEndDate')); |
||
| 524 | |||
| 525 | return false; |
||
| 526 | } |
||
| 527 | |||
| 528 | if (strlen(trim($title)) == 0) { |
||
| 529 | echo Display::return_message(get_lang('InvalidTitle')); |
||
| 530 | |||
| 531 | return false; |
||
| 532 | } |
||
| 533 | |||
| 534 | $start = api_get_utc_datetime($date_start); |
||
| 535 | $end = api_get_utc_datetime($date_end); |
||
| 536 | |||
| 537 | //Fixing urls that are sent by email |
||
| 538 | //$content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content); |
||
| 539 | //$content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content); |
||
| 540 | $content = str_replace( |
||
| 541 | 'src=\"'.api_get_path(REL_HOME_PATH), |
||
| 542 | 'src=\"'.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH), |
||
| 543 | $content |
||
| 544 | ); |
||
| 545 | $content = str_replace( |
||
| 546 | 'file='.api_get_path(REL_HOME_PATH), |
||
| 547 | 'file='.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH), |
||
| 548 | $content |
||
| 549 | ); |
||
| 550 | |||
| 551 | $dateStart = new DateTime($start, new DateTimeZone('UTC')); |
||
| 552 | $dateEnd = new DateTime($end, new DateTimeZone('UTC')); |
||
| 553 | |||
| 554 | $announcement |
||
| 555 | ->setLang($lang) |
||
| 556 | ->setTitle($title) |
||
| 557 | ->setContent($content) |
||
| 558 | ->setDateStart($dateStart) |
||
| 559 | ->setDateEnd($dateEnd) |
||
| 560 | //->setVisibleTeacher($visible_teacher) |
||
| 561 | //->setVisibleStudent($visible_student) |
||
| 562 | //->setVisibleGuest($visible_guest) |
||
| 563 | ->setAccessUrlId(api_get_current_access_url_id()); |
||
| 564 | |||
| 565 | $em->persist($announcement); |
||
| 566 | $em->flush(); |
||
| 567 | |||
| 568 | // Update visibility |
||
| 569 | $list = self::getVisibilityList(); |
||
| 570 | $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); |
||
| 571 | |||
| 572 | if (api_get_configuration_value('allow_careers_in_global_announcements') && !empty($careerId)) { |
||
| 573 | $params = []; |
||
| 574 | $params['career_id'] = (int) $careerId; |
||
| 575 | $params['promotion_id'] = (int) $promotionId; |
||
| 576 | Database::update( |
||
| 577 | $table, |
||
| 578 | $params, |
||
| 579 | ['id = ? ' => $id] |
||
| 580 | ); |
||
| 581 | } |
||
| 582 | |||
| 583 | foreach ($list as $key => $title) { |
||
| 584 | $value = isset($visibility[$key]) && $visibility[$key] ? 1 : 0; |
||
| 585 | $sql = "UPDATE $table SET $key = '$value' WHERE id = $id"; |
||
| 586 | Database::query($sql); |
||
| 587 | } |
||
| 588 | |||
| 589 | if ($sendEmailTest) { |
||
| 590 | self::send_system_announcement_by_email( |
||
| 591 | $id, |
||
| 592 | $visibility, |
||
| 593 | true |
||
| 594 | ); |
||
| 595 | } else { |
||
| 596 | if ($send_mail == 1) { |
||
| 597 | self::send_system_announcement_by_email( |
||
| 598 | $id, |
||
| 599 | $visibility, |
||
| 600 | false, |
||
| 601 | $groups |
||
| 602 | ); |
||
| 603 | } |
||
| 604 | } |
||
| 605 | |||
| 606 | return true; |
||
| 607 | } |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Deletes an announcement. |
||
| 611 | * |
||
| 612 | * @param int $id The identifier of the announcement that should be |
||
| 613 | * |
||
| 614 | * @return bool True on success, false on failure |
||
| 615 | */ |
||
| 616 | public static function delete_announcement($id) |
||
| 627 | } |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Gets an announcement. |
||
| 631 | * |
||
| 632 | * @param int $id The identifier of the announcement that should be |
||
| 633 | * |
||
| 634 | * @return object Object of class StdClass or the required class, containing the query result row |
||
| 635 | */ |
||
| 636 | public static function get_announcement($id) |
||
| 637 | { |
||
| 638 | $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); |
||
| 639 | $id = (int) $id; |
||
| 640 | $sql = "SELECT * FROM ".$table." WHERE id = ".$id; |
||
| 641 | $announcement = Database::fetch_object(Database::query($sql)); |
||
| 642 | |||
| 643 | return $announcement; |
||
| 644 | } |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Change the visibility of an announcement. |
||
| 648 | * |
||
| 649 | * @param int $id |
||
| 650 | * @param int $user For who should the visibility be changed |
||
| 651 | * @param bool $visible |
||
| 652 | * |
||
| 653 | * @return bool True on success, false on failure |
||
| 654 | */ |
||
| 655 | public static function set_visibility($id, $user, $visible) |
||
| 676 | } |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Send a system announcement by e-mail to all teachers/students depending on parameters. |
||
| 680 | * |
||
| 681 | * @param int $id |
||
| 682 | * @param array $visibility |
||
| 683 | * @param bool $sendEmailTest |
||
| 684 | * @param array $groups |
||
| 685 | * |
||
| 686 | * @return bool True if the message was sent or there was no destination matching. |
||
| 687 | * False on database or e-mail sending error. |
||
| 688 | */ |
||
| 689 | public static function send_system_announcement_by_email($id, $visibility, $sendEmailTest = false, $groups = []) |
||
| 831 | } |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Returns the group announcements where the user is subscribed. |
||
| 835 | * |
||
| 836 | * @param $userId |
||
| 837 | * @param $visible |
||
| 838 | * |
||
| 839 | * @throws \Exception |
||
| 840 | * |
||
| 841 | * @return array |
||
| 842 | */ |
||
| 843 | public static function getAnnouncementsForGroups($userId, $visible) |
||
| 867 | } |
||
| 868 | |||
| 869 | public static function isVisibleAnnouncementForUser( |
||
| 870 | int $userId, |
||
| 871 | string $userVisibility, |
||
| 872 | int $careerId, |
||
| 873 | int $promotionId |
||
| 874 | ): bool { |
||
| 875 | $objPromotion = new Promotion(); |
||
| 876 | |||
| 877 | $promotionList = []; |
||
| 878 | |||
| 879 | if (!empty($promotionId)) { |
||
| 880 | $promotionList[] = $promotionId; |
||
| 881 | } else { |
||
| 882 | $promotionList = $objPromotion->get_all_promotions_by_career_id($careerId); |
||
| 883 | |||
| 884 | if (!empty($promotionList)) { |
||
| 885 | $promotionList = array_column($promotionList, 'id'); |
||
| 886 | } |
||
| 887 | } |
||
| 888 | |||
| 889 | foreach ($promotionList as $promotionId) { |
||
| 890 | $sessionList = SessionManager::get_all_sessions_by_promotion($promotionId); |
||
| 891 | |||
| 892 | foreach ($sessionList as $session) { |
||
| 893 | $sessionId = $session['id']; |
||
| 894 | // Check student |
||
| 895 | if (self::VISIBLE_STUDENT == $userVisibility && |
||
| 896 | SessionManager::isUserSubscribedAsStudent($sessionId, $userId) |
||
| 897 | ) { |
||
| 898 | return true; |
||
| 899 | } |
||
| 900 | |||
| 901 | if (self::VISIBLE_TEACHER == $userVisibility |
||
| 902 | && SessionManager::user_is_general_coach($userId, $sessionId) |
||
| 903 | ) { |
||
| 904 | return true; |
||
| 905 | } |
||
| 906 | |||
| 907 | // Check course coach |
||
| 908 | $coaches = SessionManager::getCoachesBySession($sessionId); |
||
| 909 | |||
| 910 | if (self::VISIBLE_TEACHER == $userVisibility |
||
| 911 | && in_array($userId, $coaches) |
||
| 912 | ) { |
||
| 913 | return true; |
||
| 914 | } |
||
| 915 | } |
||
| 916 | } |
||
| 917 | |||
| 918 | return false; |
||
| 919 | } |
||
| 920 | |||
| 921 | /** |
||
| 922 | * Displays announcements as an slideshow. |
||
| 923 | * |
||
| 924 | * @param string $visible see self::VISIBLE_* constants |
||
| 925 | * @param int $id The identifier of the announcement to display |
||
| 926 | * |
||
| 927 | * @return string |
||
| 928 | */ |
||
| 929 | public static function displayAnnouncementsSlider($visible, $id = null) |
||
| 930 | { |
||
| 931 | $user_selected_language = Database::escape_string(api_get_interface_language()); |
||
| 932 | $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); |
||
| 933 | $tblGrpAnnouncements = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS); |
||
| 934 | |||
| 935 | $cut_size = 500; |
||
| 936 | $now = api_get_utc_datetime(); |
||
| 937 | //Exclude announcement to groups |
||
| 938 | $sql = "SELECT sys_announcement.* |
||
| 939 | FROM $table as sys_announcement |
||
| 940 | LEFT JOIN $tblGrpAnnouncements AS announcement_rel_group |
||
| 941 | ON sys_announcement.id = announcement_rel_group.announcement_id |
||
| 942 | WHERE |
||
| 943 | (sys_announcement.lang = '$user_selected_language' OR sys_announcement.lang = '') AND |
||
| 944 | ('$now' >= sys_announcement.date_start AND '$now' <= sys_announcement.date_end) AND |
||
| 945 | announcement_rel_group.group_id is null"; |
||
| 946 | |||
| 947 | $sql .= self::getVisibilityCondition($visible); |
||
| 948 | |||
| 949 | if (isset($id) && !empty($id)) { |
||
| 950 | $id = (int) $id; |
||
| 951 | $sql .= " AND id = $id "; |
||
| 952 | } |
||
| 953 | |||
| 954 | if (api_is_multiple_url_enabled()) { |
||
| 955 | $current_url_id = api_get_current_access_url_id(); |
||
| 956 | $sql .= " AND access_url_id IN ('1', '$current_url_id') "; |
||
| 957 | } |
||
| 958 | |||
| 959 | $checkCareers = api_get_configuration_value('allow_careers_in_global_announcements') === true; |
||
| 960 | |||
| 961 | $userId = api_get_user_id(); |
||
| 962 | |||
| 963 | $sql .= ' ORDER BY date_start DESC'; |
||
| 964 | $result = Database::query($sql); |
||
| 965 | $announcements = []; |
||
| 966 | if (Database::num_rows($result) > 0) { |
||
| 967 | while ($announcement = Database::fetch_object($result)) { |
||
| 968 | if ($checkCareers && !empty($announcement->career_id)) { |
||
| 969 | $show = self::isVisibleAnnouncementForUser( |
||
| 970 | $userId, |
||
| 971 | $visible, |
||
| 972 | (int) $announcement->career_id, |
||
| 973 | (int) $announcement->promotion_id |
||
| 974 | ); |
||
| 975 | |||
| 976 | if (false === $show) { |
||
| 977 | continue; |
||
| 978 | } |
||
| 979 | } |
||
| 980 | |||
| 981 | $announcementData = [ |
||
| 982 | 'id' => $announcement->id, |
||
| 983 | 'title' => $announcement->title, |
||
| 984 | 'content' => $announcement->content, |
||
| 985 | 'readMore' => null, |
||
| 986 | ]; |
||
| 987 | |||
| 988 | if (empty($id)) { |
||
| 989 | if (api_strlen(strip_tags($announcement->content)) > $cut_size) { |
||
| 990 | //$announcementData['content'] = cut($announcement->content, $cut_size); |
||
| 991 | $announcementData['readMore'] = true; |
||
| 992 | } |
||
| 993 | } |
||
| 994 | |||
| 995 | $announcements[] = $announcementData; |
||
| 996 | } |
||
| 997 | } |
||
| 998 | |||
| 999 | /** Show announcement of group */ |
||
| 1000 | $announcementToGroup = self::getAnnouncementsForGroups($userId, $visible); |
||
| 1001 | $totalAnnouncementToGroup = count($announcementToGroup); |
||
| 1002 | for ($i = 0; $i < $totalAnnouncementToGroup; $i++) { |
||
| 1003 | $announcement = $announcementToGroup[$i]; |
||
| 1004 | $announcementData = [ |
||
| 1005 | 'id' => $announcement['id'], |
||
| 1006 | 'title' => $announcement['title'], |
||
| 1007 | 'content' => $announcement['content'], |
||
| 1008 | 'readMore' => null, |
||
| 1009 | ]; |
||
| 1010 | $content = $announcement['content']; |
||
| 1011 | if (api_strlen(strip_tags($content)) > $cut_size) { |
||
| 1012 | //$announcementData['content'] = cut($content, $cut_size); |
||
| 1013 | $announcementData['readMore'] = true; |
||
| 1014 | } |
||
| 1015 | $announcements[] = $announcementData; |
||
| 1016 | } |
||
| 1017 | |||
| 1018 | if (count($announcements) === 0) { |
||
| 1019 | return null; |
||
| 1020 | } |
||
| 1021 | |||
| 1022 | $template = new Template(null, false, false); |
||
| 1023 | $template->assign('announcements', $announcements); |
||
| 1024 | $layout = $template->get_template('announcement/slider.tpl'); |
||
| 1025 | |||
| 1026 | return $template->fetch($layout); |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | /** |
||
| 1030 | * Get the HTML code for an announcement. |
||
| 1031 | * |
||
| 1032 | * @param int $announcementId The announcement ID |
||
| 1033 | * @param int $visibility The announcement visibility |
||
| 1034 | * |
||
| 1035 | * @return string The HTML code |
||
| 1036 | */ |
||
| 1037 | public static function displayAnnouncement($announcementId, $visibility) |
||
| 1038 | { |
||
| 1039 | $selectedUserLanguage = Database::escape_string(api_get_interface_language()); |
||
| 1040 | $announcementTable = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); |
||
| 1041 | $now = api_get_utc_datetime(); |
||
| 1042 | $announcementId = (int) $announcementId; |
||
| 1043 | |||
| 1044 | $whereConditions = [ |
||
| 1045 | "(lang = ? OR lang IS NULL OR lang = '') " => $selectedUserLanguage, |
||
| 1046 | "AND (? >= date_start AND ? <= date_end) " => [$now, $now], |
||
| 1047 | "AND id = ? " => $announcementId, |
||
| 1048 | ]; |
||
| 1049 | |||
| 1050 | $condition = self::getVisibilityCondition($visibility); |
||
| 1051 | $whereConditions[$condition] = 1; |
||
| 1052 | |||
| 1053 | if (api_is_multiple_url_enabled()) { |
||
| 1054 | $whereConditions["AND access_url_id IN (1, ?) "] = api_get_current_access_url_id(); |
||
| 1055 | } |
||
| 1056 | |||
| 1057 | $announcement = Database::select( |
||
| 1058 | '*', |
||
| 1059 | $announcementTable, |
||
| 1060 | [ |
||
| 1061 | 'where' => $whereConditions, |
||
| 1062 | 'order' => 'date_start', |
||
| 1063 | ], |
||
| 1064 | 'first' |
||
| 1065 | ); |
||
| 1066 | |||
| 1067 | $template = new Template(null, false, false); |
||
| 1068 | $template->assign('announcement', $announcement); |
||
| 1069 | $layout = $template->get_template('announcement/view.tpl'); |
||
| 1070 | |||
| 1071 | return $template->fetch($layout); |
||
| 1072 | } |
||
| 1073 | |||
| 1074 | /** |
||
| 1075 | * @return bool |
||
| 1076 | */ |
||
| 1077 | public static function newRolesActivated() |
||
| 1085 | } |
||
| 1086 | |||
| 1087 | /** |
||
| 1088 | * @return string |
||
| 1089 | */ |
||
| 1090 | public static function getCurrentUserVisibility() |
||
| 1120 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: