| Total Complexity | 130 |
| Total Lines | 983 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 | const VISIBLE_GUEST = 'visible_guest'; |
||
| 11 | const VISIBLE_STUDENT = 'visible_student'; |
||
| 12 | const VISIBLE_TEACHER = 'visible_teacher'; |
||
| 13 | // Requires DB change |
||
| 14 | const VISIBLE_DRH = 'visible_drh'; |
||
| 15 | const VISIBLE_SESSION_ADMIN = 'visible_session_admin'; |
||
| 16 | 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( |
||
| 65 | $visibility, |
||
| 66 | $id = -1, |
||
|
|
|||
| 67 | $start = 0, |
||
| 68 | $user_id = '' |
||
| 69 | ) { |
||
| 70 | $user_selected_language = api_get_interface_language(); |
||
| 71 | $start = (int) $start; |
||
| 72 | $userGroup = new UserGroup(); |
||
| 73 | $tbl_announcement_group = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS_GROUPS); |
||
| 74 | $temp_user_groups = $userGroup->get_groups_by_user(api_get_user_id(), 0); |
||
| 75 | $groups = []; |
||
| 76 | foreach ($temp_user_groups as $user_group) { |
||
| 77 | $groups = array_merge($groups, [$user_group['id']]); |
||
| 78 | $groups = array_merge($groups, $userGroup->get_parent_groups($user_group['id'])); |
||
| 79 | } |
||
| 80 | |||
| 81 | // Checks if tables exists to not break platform not updated |
||
| 82 | $groups_string = '('.implode($groups, ',').')'; |
||
| 83 | |||
| 84 | $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); |
||
| 85 | $now = api_get_utc_datetime(); |
||
| 86 | |||
| 87 | $sql = "SELECT * FROM $table |
||
| 88 | WHERE |
||
| 89 | (lang = '$user_selected_language' OR lang IS NULL) AND |
||
| 90 | ( '$now' >= date_start AND '$now' <= date_end) "; |
||
| 91 | |||
| 92 | $sql .= self::getVisibilityCondition($visibility); |
||
| 93 | |||
| 94 | if (count($groups) > 0) { |
||
| 95 | $sql .= " OR id IN ( |
||
| 96 | SELECT announcement_id FROM $tbl_announcement_group |
||
| 97 | WHERE group_id in $groups_string |
||
| 98 | ) "; |
||
| 99 | } |
||
| 100 | |||
| 101 | if (api_is_multiple_url_enabled()) { |
||
| 102 | $current_access_url_id = api_get_current_access_url_id(); |
||
| 103 | $sql .= " AND access_url_id IN ('1', '$current_access_url_id')"; |
||
| 104 | } |
||
| 105 | |||
| 106 | if (!isset($_GET['start']) || $_GET['start'] == 0) { |
||
| 107 | $sql .= " ORDER BY date_start DESC LIMIT ".$start.",20"; |
||
| 108 | } else { |
||
| 109 | $sql .= " ORDER BY date_start DESC LIMIT ".($start + 1).",20"; |
||
| 110 | } |
||
| 111 | $announcements = Database::query($sql); |
||
| 112 | $content = ''; |
||
| 113 | if (Database::num_rows($announcements) > 0) { |
||
| 114 | $content .= '<div class="system_announcements">'; |
||
| 115 | $content .= '<h3>'.get_lang('SystemAnnouncements').'</h3>'; |
||
| 116 | $content .= '<table align="center">'; |
||
| 117 | $content .= '<tr>'; |
||
| 118 | $content .= '<td>'; |
||
| 119 | $content .= self::display_arrow($user_id); |
||
| 120 | $content .= '</td>'; |
||
| 121 | $content .= '</tr>'; |
||
| 122 | $content .= '</table>'; |
||
| 123 | $content .= '<table align="center" border="0" width="900px">'; |
||
| 124 | while ($announcement = Database::fetch_object($announcements)) { |
||
| 125 | $display_date = api_convert_and_format_date($announcement->display_date, DATE_FORMAT_LONG); |
||
| 126 | $content .= '<tr><td>'; |
||
| 127 | $content .= '<a name="'.$announcement->id.'"></a> |
||
| 128 | <div class="system_announcement"> |
||
| 129 | <h2>'.$announcement->title.'</h2> |
||
| 130 | <div class="system_announcement_date">'.$display_date.'</div> |
||
| 131 | <br /> |
||
| 132 | <div class="system_announcement_content">' |
||
| 133 | .$announcement->content.' |
||
| 134 | </div> |
||
| 135 | </div><br />'; |
||
| 136 | $content .= '</tr></td>'; |
||
| 137 | } |
||
| 138 | $content .= '</table>'; |
||
| 139 | |||
| 140 | $content .= '<table align="center">'; |
||
| 141 | $content .= '<tr>'; |
||
| 142 | $content .= '<td>'; |
||
| 143 | $content .= self::display_arrow($user_id); |
||
| 144 | $content .= '</td>'; |
||
| 145 | $content .= '</tr>'; |
||
| 146 | $content .= '</table>'; |
||
| 147 | $content .= '</div>'; |
||
| 148 | } |
||
| 149 | |||
| 150 | return $content; |
||
| 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() |
||
| 218 | { |
||
| 219 | $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); |
||
| 220 | $now = api_get_utc_datetime(); |
||
| 221 | $sql = "SELECT *, IF ( '$now' >= date_start AND '$now' <= date_end, '1', '0') AS visible |
||
| 222 | FROM $table"; |
||
| 223 | |||
| 224 | $current_access_url_id = 1; |
||
| 225 | if (api_is_multiple_url_enabled()) { |
||
| 226 | $current_access_url_id = api_get_current_access_url_id(); |
||
| 227 | } |
||
| 228 | $sql .= " WHERE access_url_id = '$current_access_url_id' "; |
||
| 229 | $sql .= " ORDER BY date_start ASC"; |
||
| 230 | |||
| 231 | $result = Database::query($sql); |
||
| 232 | $announcements = []; |
||
| 233 | while ($announcement = Database::fetch_object($result)) { |
||
| 234 | $announcements[] = $announcement; |
||
| 235 | } |
||
| 236 | |||
| 237 | return $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 | * |
||
| 255 | * @return mixed insert_id on success, false on failure |
||
| 256 | */ |
||
| 257 | public static function add_announcement( |
||
| 258 | $title, |
||
| 259 | $content, |
||
| 260 | $date_start, |
||
| 261 | $date_end, |
||
| 262 | $visibility, |
||
| 263 | $lang = '', |
||
| 264 | $send_mail = 0, |
||
| 265 | $add_to_calendar = false, |
||
| 266 | $sendEmailTest = false, |
||
| 267 | $careerId = 0, |
||
| 268 | $promotionId = 0 |
||
| 269 | ) { |
||
| 270 | $original_content = $content; |
||
| 271 | $a_dateS = explode(' ', $date_start); |
||
| 272 | $a_arraySD = explode('-', $a_dateS[0]); |
||
| 273 | $a_arraySH = explode(':', $a_dateS[1]); |
||
| 274 | $date_start_to_compare = array_merge($a_arraySD, $a_arraySH); |
||
| 275 | |||
| 276 | $a_dateE = explode(' ', $date_end); |
||
| 277 | $a_arrayED = explode('-', $a_dateE[0]); |
||
| 278 | $a_arrayEH = explode(':', $a_dateE[1]); |
||
| 279 | $date_end_to_compare = array_merge($a_arrayED, $a_arrayEH); |
||
| 280 | |||
| 281 | $db_table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); |
||
| 282 | |||
| 283 | if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) { |
||
| 284 | Display::addFlash( |
||
| 285 | Display::return_message(get_lang('InvalidStartDate'), 'warning') |
||
| 286 | ); |
||
| 287 | |||
| 288 | return false; |
||
| 289 | } |
||
| 290 | |||
| 291 | if (($date_end_to_compare[1] || |
||
| 292 | $date_end_to_compare[2] || |
||
| 293 | $date_end_to_compare[0]) && |
||
| 294 | !checkdate($date_end_to_compare[1], $date_end_to_compare[2], $date_end_to_compare[0]) |
||
| 295 | ) { |
||
| 296 | Display::addFlash( |
||
| 297 | Display::return_message(get_lang('InvalidEndDate'), 'warning') |
||
| 298 | ); |
||
| 299 | |||
| 300 | return false; |
||
| 301 | } |
||
| 302 | |||
| 303 | if (strlen(trim($title)) == 0) { |
||
| 304 | Display::addFlash( |
||
| 305 | Display::return_message(get_lang('InvalidTitle'), 'warning') |
||
| 306 | ); |
||
| 307 | |||
| 308 | return false; |
||
| 309 | } |
||
| 310 | |||
| 311 | $start = api_get_utc_datetime($date_start); |
||
| 312 | $end = api_get_utc_datetime($date_end); |
||
| 313 | |||
| 314 | //Fixing urls that are sent by email |
||
| 315 | //$content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content); |
||
| 316 | //$content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content); |
||
| 317 | $content = str_replace( |
||
| 318 | 'src=\"'.api_get_path(REL_HOME_PATH), |
||
| 319 | 'src=\"'.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH), |
||
| 320 | $content |
||
| 321 | ); |
||
| 322 | $content = str_replace( |
||
| 323 | 'file='.api_get_path(REL_HOME_PATH), |
||
| 324 | 'file='.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH), |
||
| 325 | $content |
||
| 326 | ); |
||
| 327 | $lang = is_null($lang) ? '' : $lang; |
||
| 328 | |||
| 329 | $current_access_url_id = 1; |
||
| 330 | if (api_is_multiple_url_enabled()) { |
||
| 331 | $current_access_url_id = api_get_current_access_url_id(); |
||
| 332 | } |
||
| 333 | |||
| 334 | $params = [ |
||
| 335 | 'title' => $title, |
||
| 336 | 'content' => $content, |
||
| 337 | 'date_start' => $start, |
||
| 338 | 'date_end' => $end, |
||
| 339 | 'lang' => $lang, |
||
| 340 | 'access_url_id' => $current_access_url_id, |
||
| 341 | ]; |
||
| 342 | |||
| 343 | if (api_get_configuration_value('allow_careers_in_global_announcements') && !empty($careerId)) { |
||
| 344 | $params['career_id'] = (int) $careerId; |
||
| 345 | $params['promotion_id'] = (int) $promotionId; |
||
| 346 | } |
||
| 347 | |||
| 348 | foreach ($visibility as $key => $value) { |
||
| 349 | $params[$key] = $value; |
||
| 350 | } |
||
| 351 | |||
| 352 | $resultId = Database::insert($db_table, $params); |
||
| 353 | |||
| 354 | if ($resultId) { |
||
| 355 | if ($sendEmailTest) { |
||
| 356 | self::send_system_announcement_by_email( |
||
| 357 | $resultId, |
||
| 358 | $visibility, |
||
| 359 | true |
||
| 360 | ); |
||
| 361 | } else { |
||
| 362 | if ($send_mail == 1) { |
||
| 363 | self::send_system_announcement_by_email( |
||
| 364 | $resultId, |
||
| 365 | $visibility |
||
| 366 | ); |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | if ($add_to_calendar) { |
||
| 371 | $agenda = new Agenda('admin'); |
||
| 372 | $agenda->addEvent( |
||
| 373 | $date_start, |
||
| 374 | $date_end, |
||
| 375 | false, |
||
| 376 | $title, |
||
| 377 | $original_content |
||
| 378 | ); |
||
| 379 | } |
||
| 380 | |||
| 381 | return $resultId; |
||
| 382 | } |
||
| 383 | |||
| 384 | return false; |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Makes the announcement id visible only for groups in groups_array. |
||
| 389 | * |
||
| 390 | * @param int $announcement_id |
||
| 391 | * @param array $group_array array of group id |
||
| 392 | * |
||
| 393 | * @return bool |
||
| 394 | */ |
||
| 395 | public static function announcement_for_groups($announcement_id, $group_array) |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Gets the groups of this announce. |
||
| 427 | * |
||
| 428 | * @param int announcement id |
||
| 429 | * |
||
| 430 | * @return array array of group id |
||
| 431 | */ |
||
| 432 | public static function get_announcement_groups($announcement_id) |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Updates an announcement to the database. |
||
| 452 | * |
||
| 453 | * @param int $id of the announcement |
||
| 454 | * @param string $title title of the announcement |
||
| 455 | * @param string $content content of the announcement |
||
| 456 | * @param array $date_start start date (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute) |
||
| 457 | * @param array $date_end end date of (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute) |
||
| 458 | * @param array $visibility |
||
| 459 | * @param array $lang |
||
| 460 | * @param int $send_mail |
||
| 461 | * @param bool $sendEmailTest |
||
| 462 | * @param int $careerId |
||
| 463 | * @param int $promotionId |
||
| 464 | * |
||
| 465 | * @return bool True on success, false on failure |
||
| 466 | */ |
||
| 467 | public static function update_announcement( |
||
| 468 | $id, |
||
| 469 | $title, |
||
| 470 | $content, |
||
| 471 | $date_start, |
||
| 472 | $date_end, |
||
| 473 | $visibility, |
||
| 474 | $lang = null, |
||
| 475 | $send_mail = 0, |
||
| 476 | $sendEmailTest = false, |
||
| 477 | $careerId = 0, |
||
| 478 | $promotionId = 0 |
||
| 479 | ) { |
||
| 480 | $em = Database::getManager(); |
||
| 481 | $announcement = $em->find('ChamiloCoreBundle:SysAnnouncement', $id); |
||
| 482 | if (!$announcement) { |
||
| 483 | return false; |
||
| 484 | } |
||
| 485 | |||
| 486 | $a_dateS = explode(' ', $date_start); |
||
| 487 | $a_arraySD = explode('-', $a_dateS[0]); |
||
| 488 | $a_arraySH = explode(':', $a_dateS[1]); |
||
| 489 | $date_start_to_compare = array_merge($a_arraySD, $a_arraySH); |
||
| 490 | |||
| 491 | $a_dateE = explode(' ', $date_end); |
||
| 492 | $a_arrayED = explode('-', $a_dateE[0]); |
||
| 493 | $a_arrayEH = explode(':', $a_dateE[1]); |
||
| 494 | $date_end_to_compare = array_merge($a_arrayED, $a_arrayEH); |
||
| 495 | |||
| 496 | $lang = is_null($lang) ? '' : $lang; |
||
| 497 | |||
| 498 | if (!checkdate($date_start_to_compare[1], $date_start_to_compare[2], $date_start_to_compare[0])) { |
||
| 499 | echo Display::return_message(get_lang('InvalidStartDate')); |
||
| 500 | |||
| 501 | return false; |
||
| 502 | } |
||
| 503 | |||
| 504 | if (($date_end_to_compare[1] || |
||
| 505 | $date_end_to_compare[2] || |
||
| 506 | $date_end_to_compare[0]) && |
||
| 507 | !checkdate($date_end_to_compare[1], $date_end_to_compare[2], $date_end_to_compare[0]) |
||
| 508 | ) { |
||
| 509 | echo Display::return_message(get_lang('InvalidEndDate')); |
||
| 510 | |||
| 511 | return false; |
||
| 512 | } |
||
| 513 | |||
| 514 | if (strlen(trim($title)) == 0) { |
||
| 515 | echo Display::return_message(get_lang('InvalidTitle')); |
||
| 516 | |||
| 517 | return false; |
||
| 518 | } |
||
| 519 | |||
| 520 | $start = api_get_utc_datetime($date_start); |
||
| 521 | $end = api_get_utc_datetime($date_end); |
||
| 522 | |||
| 523 | //Fixing urls that are sent by email |
||
| 524 | //$content = str_replace('src=\"/home/', 'src=\"'.api_get_path(WEB_PATH).'home/', $content); |
||
| 525 | //$content = str_replace('file=/home/', 'file='.api_get_path(WEB_PATH).'home/', $content); |
||
| 526 | $content = str_replace( |
||
| 527 | 'src=\"'.api_get_path(REL_HOME_PATH), |
||
| 528 | 'src=\"'.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH), |
||
| 529 | $content |
||
| 530 | ); |
||
| 531 | $content = str_replace( |
||
| 532 | 'file='.api_get_path(REL_HOME_PATH), |
||
| 533 | 'file='.api_get_path(WEB_PATH).api_get_path(REL_HOME_PATH), |
||
| 534 | $content |
||
| 535 | ); |
||
| 536 | |||
| 537 | $dateStart = new DateTime($start, new DateTimeZone('UTC')); |
||
| 538 | $dateEnd = new DateTime($end, new DateTimeZone('UTC')); |
||
| 539 | |||
| 540 | $announcement |
||
| 541 | ->setLang($lang) |
||
| 542 | ->setTitle($title) |
||
| 543 | ->setContent($content) |
||
| 544 | ->setDateStart($dateStart) |
||
| 545 | ->setDateEnd($dateEnd) |
||
| 546 | //->setVisibleTeacher($visible_teacher) |
||
| 547 | //->setVisibleStudent($visible_student) |
||
| 548 | //->setVisibleGuest($visible_guest) |
||
| 549 | ->setAccessUrlId(api_get_current_access_url_id()); |
||
| 550 | |||
| 551 | $em->merge($announcement); |
||
| 552 | $em->flush(); |
||
| 553 | |||
| 554 | // Update visibility |
||
| 555 | $list = self::getVisibilityList(); |
||
| 556 | $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); |
||
| 557 | |||
| 558 | if (api_get_configuration_value('allow_careers_in_global_announcements') && !empty($careerId)) { |
||
| 559 | $params = []; |
||
| 560 | $params['career_id'] = (int) $careerId; |
||
| 561 | $params['promotion_id'] = (int) $promotionId; |
||
| 562 | Database::update( |
||
| 563 | $table, |
||
| 564 | $params, |
||
| 565 | ['id = ? ' => $id] |
||
| 566 | ); |
||
| 567 | } |
||
| 568 | |||
| 569 | foreach ($list as $key => $title) { |
||
| 570 | $value = isset($visibility[$key]) && $visibility[$key] ? 1 : 0; |
||
| 571 | $sql = "UPDATE $table SET $key = '$value' WHERE id = $id"; |
||
| 572 | Database::query($sql); |
||
| 573 | } |
||
| 574 | |||
| 575 | if ($sendEmailTest) { |
||
| 576 | self::send_system_announcement_by_email( |
||
| 577 | $id, |
||
| 578 | $visibility, |
||
| 579 | true |
||
| 580 | ); |
||
| 581 | } else { |
||
| 582 | if ($send_mail == 1) { |
||
| 583 | self::send_system_announcement_by_email( |
||
| 584 | $id, |
||
| 585 | $visibility |
||
| 586 | ); |
||
| 587 | } |
||
| 588 | } |
||
| 589 | |||
| 590 | return true; |
||
| 591 | } |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Deletes an announcement. |
||
| 595 | * |
||
| 596 | * @param int $id The identifier of the announcement that should be |
||
| 597 | * |
||
| 598 | * @return bool True on success, false on failure |
||
| 599 | */ |
||
| 600 | public static function delete_announcement($id) |
||
| 611 | } |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Gets an announcement. |
||
| 615 | * |
||
| 616 | * @param int $id The identifier of the announcement that should be |
||
| 617 | * |
||
| 618 | * @return object Object of class StdClass or the required class, containing the query result row |
||
| 619 | */ |
||
| 620 | public static function get_announcement($id) |
||
| 621 | { |
||
| 622 | $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); |
||
| 623 | $id = (int) $id; |
||
| 624 | $sql = "SELECT * FROM ".$table." WHERE id = ".$id; |
||
| 625 | $announcement = Database::fetch_object(Database::query($sql)); |
||
| 626 | |||
| 627 | return $announcement; |
||
| 628 | } |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Change the visibility of an announcement. |
||
| 632 | * |
||
| 633 | * @param int $id |
||
| 634 | * @param int $user For who should the visibility be changed |
||
| 635 | * @param bool $visible |
||
| 636 | * |
||
| 637 | * @return bool True on success, false on failure |
||
| 638 | */ |
||
| 639 | public static function set_visibility($id, $user, $visible) |
||
| 660 | } |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Send a system announcement by e-mail to all teachers/students depending on parameters. |
||
| 664 | * |
||
| 665 | * @param int $id |
||
| 666 | * @param array $visibility |
||
| 667 | * @param bool $sendEmailTest |
||
| 668 | * |
||
| 669 | * @return bool True if the message was sent or there was no destination matching. |
||
| 670 | * False on database or e-mail sending error. |
||
| 671 | */ |
||
| 672 | public static function send_system_announcement_by_email( |
||
| 799 | } |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Displays announcements as an slideshow. |
||
| 803 | * |
||
| 804 | * @param string $visible see self::VISIBLE_* constants |
||
| 805 | * @param int $id The identifier of the announcement to display |
||
| 806 | * |
||
| 807 | * @return string |
||
| 808 | */ |
||
| 809 | public static function displayAnnouncementsSlider($visible, $id = null) |
||
| 810 | { |
||
| 811 | $user_selected_language = Database::escape_string(api_get_interface_language()); |
||
| 812 | $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); |
||
| 813 | |||
| 814 | $cut_size = 500; |
||
| 815 | $now = api_get_utc_datetime(); |
||
| 816 | $sql = "SELECT * FROM $table |
||
| 817 | WHERE |
||
| 818 | (lang = '$user_selected_language' OR lang = '') AND |
||
| 819 | ('$now' >= date_start AND '$now' <= date_end) "; |
||
| 820 | |||
| 821 | $sql .= self::getVisibilityCondition($visible); |
||
| 822 | |||
| 823 | if (isset($id) && !empty($id)) { |
||
| 824 | $id = (int) $id; |
||
| 825 | $sql .= " AND id = $id "; |
||
| 826 | } |
||
| 827 | |||
| 828 | if (api_is_multiple_url_enabled()) { |
||
| 829 | $current_url_id = api_get_current_access_url_id(); |
||
| 830 | $sql .= " AND access_url_id IN ('1', '$current_url_id') "; |
||
| 831 | } |
||
| 832 | |||
| 833 | $checkCareers = api_get_configuration_value('allow_careers_in_global_announcements') === true; |
||
| 834 | |||
| 835 | $userId = api_get_user_id(); |
||
| 836 | |||
| 837 | $promotion = new Promotion(); |
||
| 838 | $sql .= ' ORDER BY date_start DESC'; |
||
| 839 | $result = Database::query($sql); |
||
| 840 | $announcements = []; |
||
| 841 | if (Database::num_rows($result) > 0) { |
||
| 842 | while ($announcement = Database::fetch_object($result)) { |
||
| 843 | if ($checkCareers && !empty($announcement->career_id)) { |
||
| 844 | if (!empty($announcement->promotion_id)) { |
||
| 845 | $promotionList[] = $announcement->promotion_id; |
||
| 846 | } else { |
||
| 847 | $promotionList = $promotion->get_all_promotions_by_career_id($announcement->career_id); |
||
| 848 | if (!empty($promotionList)) { |
||
| 849 | $promotionList = array_column($promotionList, 'id'); |
||
| 850 | } |
||
| 851 | } |
||
| 852 | |||
| 853 | $show = false; |
||
| 854 | foreach ($promotionList as $promotionId) { |
||
| 855 | $sessionList = SessionManager::get_all_sessions_by_promotion($promotionId); |
||
| 856 | foreach ($sessionList as $session) { |
||
| 857 | $status = (int) SessionManager::getUserStatusInSession($userId, $session['id']); |
||
| 858 | if ($visible === self::VISIBLE_TEACHER && $status === 2) { |
||
| 859 | $show = true; |
||
| 860 | break 2; |
||
| 861 | } |
||
| 862 | |||
| 863 | if ($visible === self::VISIBLE_STUDENT && $status === 1) { |
||
| 864 | $show = true; |
||
| 865 | break 2; |
||
| 866 | } |
||
| 867 | } |
||
| 868 | } |
||
| 869 | |||
| 870 | if (false === $show) { |
||
| 871 | continue; |
||
| 872 | } |
||
| 873 | } |
||
| 874 | $announcementData = [ |
||
| 875 | 'id' => $announcement->id, |
||
| 876 | 'title' => $announcement->title, |
||
| 877 | 'content' => $announcement->content, |
||
| 878 | 'readMore' => null, |
||
| 879 | ]; |
||
| 880 | |||
| 881 | if (empty($id)) { |
||
| 882 | if (api_strlen(strip_tags($announcement->content)) > $cut_size) { |
||
| 883 | $announcementData['content'] = cut($announcement->content, $cut_size); |
||
| 884 | $announcementData['readMore'] = true; |
||
| 885 | } |
||
| 886 | } |
||
| 887 | |||
| 888 | $announcements[] = $announcementData; |
||
| 889 | } |
||
| 890 | } |
||
| 891 | |||
| 892 | if (count($announcements) === 0) { |
||
| 893 | return null; |
||
| 894 | } |
||
| 895 | |||
| 896 | $template = new Template(null, false, false); |
||
| 897 | $template->assign('announcements', $announcements); |
||
| 898 | $layout = $template->get_template('announcement/slider.tpl'); |
||
| 899 | |||
| 900 | return $template->fetch($layout); |
||
| 901 | } |
||
| 902 | |||
| 903 | /** |
||
| 904 | * Get the HTML code for an announcement. |
||
| 905 | * |
||
| 906 | * @param int $announcementId The announcement ID |
||
| 907 | * @param int $visibility The announcement visibility |
||
| 908 | * |
||
| 909 | * @return string The HTML code |
||
| 910 | */ |
||
| 911 | public static function displayAnnouncement($announcementId, $visibility) |
||
| 912 | { |
||
| 913 | $selectedUserLanguage = Database::escape_string(api_get_interface_language()); |
||
| 914 | $announcementTable = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); |
||
| 915 | $now = api_get_utc_datetime(); |
||
| 916 | $announcementId = (int) $announcementId; |
||
| 917 | |||
| 918 | $whereConditions = [ |
||
| 919 | "(lang = ? OR lang IS NULL OR lang = '') " => $selectedUserLanguage, |
||
| 920 | "AND (? >= date_start AND ? <= date_end) " => [$now, $now], |
||
| 921 | "AND id = ? " => $announcementId, |
||
| 922 | ]; |
||
| 923 | |||
| 924 | $condition = self::getVisibilityCondition($visibility); |
||
| 925 | $whereConditions[$condition] = 1; |
||
| 926 | |||
| 927 | if (api_is_multiple_url_enabled()) { |
||
| 928 | $whereConditions["AND access_url_id IN (1, ?) "] = api_get_current_access_url_id(); |
||
| 929 | } |
||
| 930 | |||
| 931 | $announcement = Database::select( |
||
| 932 | '*', |
||
| 933 | $announcementTable, |
||
| 934 | [ |
||
| 935 | 'where' => $whereConditions, |
||
| 936 | 'order' => 'date_start', |
||
| 937 | ], |
||
| 938 | 'first' |
||
| 939 | ); |
||
| 940 | |||
| 941 | $template = new Template(null, false, false); |
||
| 942 | $template->assign('announcement', $announcement); |
||
| 943 | $layout = $template->get_template('announcement/view.tpl'); |
||
| 944 | |||
| 945 | return $template->fetch($layout); |
||
| 946 | } |
||
| 947 | |||
| 948 | /** |
||
| 949 | * @return bool |
||
| 950 | */ |
||
| 951 | public static function newRolesActivated() |
||
| 959 | } |
||
| 960 | |||
| 961 | /** |
||
| 962 | * @return string |
||
| 963 | */ |
||
| 964 | public static function getCurrentUserVisibility() |
||
| 994 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.