| Total Complexity | 70 |
| Total Lines | 541 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Notification 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 Notification, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Notification extends Model |
||
| 14 | { |
||
| 15 | // mail_notify_message ("At once", "Daily", "No") |
||
| 16 | const NOTIFY_MESSAGE_AT_ONCE = 1; |
||
| 17 | const NOTIFY_MESSAGE_DAILY = 8; |
||
| 18 | const NOTIFY_MESSAGE_WEEKLY = 12; |
||
| 19 | const NOTIFY_MESSAGE_NO = 0; |
||
| 20 | |||
| 21 | // mail_notify_invitation ("At once", "Daily", "No") |
||
| 22 | const NOTIFY_INVITATION_AT_ONCE = 1; |
||
| 23 | const NOTIFY_INVITATION_DAILY = 8; |
||
| 24 | const NOTIFY_INVITATION_WEEKLY = 12; |
||
| 25 | const NOTIFY_INVITATION_NO = 0; |
||
| 26 | |||
| 27 | // mail_notify_group_message ("At once", "Daily", "No") |
||
| 28 | const NOTIFY_GROUP_AT_ONCE = 1; |
||
| 29 | const NOTIFY_GROUP_DAILY = 8; |
||
| 30 | const NOTIFY_GROUP_WEEKLY = 12; |
||
| 31 | const NOTIFY_GROUP_NO = 0; |
||
| 32 | |||
| 33 | // Notification types |
||
| 34 | const NOTIFICATION_TYPE_MESSAGE = 1; |
||
| 35 | const NOTIFICATION_TYPE_INVITATION = 2; |
||
| 36 | const NOTIFICATION_TYPE_GROUP = 3; |
||
| 37 | const NOTIFICATION_TYPE_WALL_MESSAGE = 4; |
||
| 38 | const NOTIFICATION_TYPE_DIRECT_MESSAGE = 5; |
||
| 39 | public $table; |
||
| 40 | public $columns = [ |
||
| 41 | 'id', |
||
| 42 | 'dest_user_id', |
||
| 43 | 'dest_mail', |
||
| 44 | 'title', |
||
| 45 | 'content', |
||
| 46 | 'send_freq', |
||
| 47 | 'created_at', |
||
| 48 | 'sent_at', |
||
| 49 | ]; |
||
| 50 | |||
| 51 | //Max length of the notification.content field |
||
| 52 | public $max_content_length = 254; |
||
| 53 | public $debug = false; |
||
| 54 | |||
| 55 | /* message, invitation, group messages */ |
||
| 56 | public $type; |
||
| 57 | public $adminName; |
||
| 58 | public $adminEmail; |
||
| 59 | public $titlePrefix; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Constructor. |
||
| 63 | */ |
||
| 64 | public function __construct() |
||
| 65 | { |
||
| 66 | $this->table = Database::get_main_table(TABLE_NOTIFICATION); |
||
| 67 | // Default no-reply email |
||
| 68 | $this->adminEmail = api_get_setting('noreply_email_address'); |
||
| 69 | $this->adminName = api_get_setting('siteName'); |
||
| 70 | $this->titlePrefix = '['.api_get_setting('siteName').'] '; |
||
| 71 | |||
| 72 | // If no-reply email doesn't exist use the admin name/email |
||
| 73 | if (empty($this->adminEmail)) { |
||
| 74 | $this->adminEmail = api_get_setting('emailAdministrator'); |
||
| 75 | $this->adminName = api_get_person_name( |
||
| 76 | api_get_setting('administratorName'), |
||
| 77 | api_get_setting('administratorSurname'), |
||
| 78 | null, |
||
| 79 | PERSON_NAME_EMAIL_ADDRESS |
||
| 80 | ); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public function getTitlePrefix() |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | public function getDefaultPlatformSenderEmail() |
||
| 96 | { |
||
| 97 | return $this->adminEmail; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public function getDefaultPlatformSenderName() |
||
| 104 | { |
||
| 105 | return $this->adminName; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Send the notifications. |
||
| 110 | * |
||
| 111 | * @param int $frequency notification frequency |
||
| 112 | */ |
||
| 113 | public function send($frequency = 8) |
||
| 114 | { |
||
| 115 | $notifications = $this->find( |
||
| 116 | 'all', |
||
| 117 | ['where' => ['sent_at IS NULL AND send_freq = ?' => $frequency]] |
||
| 118 | ); |
||
| 119 | |||
| 120 | if (!empty($notifications)) { |
||
| 121 | foreach ($notifications as $item_to_send) { |
||
| 122 | // Sending email |
||
| 123 | api_mail_html( |
||
| 124 | $item_to_send['dest_mail'], |
||
| 125 | $item_to_send['dest_mail'], |
||
| 126 | Security::filter_terms($item_to_send['title']), |
||
| 127 | Security::filter_terms($item_to_send['content']), |
||
| 128 | $this->adminName, |
||
| 129 | $this->adminEmail |
||
| 130 | ); |
||
| 131 | if ($this->debug) { |
||
| 132 | error_log('Sending message to: '.$item_to_send['dest_mail']); |
||
| 133 | } |
||
| 134 | |||
| 135 | // Updating |
||
| 136 | $item_to_send['sent_at'] = api_get_utc_datetime(); |
||
| 137 | $this->update($item_to_send); |
||
| 138 | if ($this->debug) { |
||
| 139 | error_log('Updating record : '.print_r($item_to_send, 1)); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $title |
||
| 147 | * @param array $senderInfo |
||
| 148 | * @param bool $forceTitleWhenSendingEmail force the use of $title as subject instead of "You have a new message" |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | public function formatTitle($title, $senderInfo, $forceTitleWhenSendingEmail = false) |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Save message notification. |
||
| 224 | * |
||
| 225 | * @param int $type message type |
||
| 226 | * NOTIFICATION_TYPE_MESSAGE, |
||
| 227 | * NOTIFICATION_TYPE_INVITATION, |
||
| 228 | * NOTIFICATION_TYPE_GROUP |
||
| 229 | * @param int $messageId |
||
| 230 | * @param array $userList recipients: user list of ids |
||
| 231 | * @param string $title |
||
| 232 | * @param string $content |
||
| 233 | * @param array $senderInfo result of api_get_user_info() or GroupPortalManager:get_group_data() |
||
| 234 | * @param array $attachments |
||
| 235 | * @param array $smsParameters |
||
| 236 | * @param bool $forceTitleWhenSendingEmail force the use of $title as subject instead of "You have a new message" |
||
| 237 | */ |
||
| 238 | public function saveNotification( |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Formats the content in order to add the welcome message, |
||
| 362 | * the notification preference, etc. |
||
| 363 | * |
||
| 364 | * @param int $messageId |
||
| 365 | * @param string $content |
||
| 366 | * @param array $senderInfo result of api_get_user_info() or |
||
| 367 | * GroupPortalManager:get_group_data() |
||
| 368 | * |
||
| 369 | * @return string |
||
| 370 | * */ |
||
| 371 | public function formatContent($messageId, $content, $senderInfo) |
||
| 372 | { |
||
| 373 | $hook = Container::instantiateHook(HookNotificationContent::class); |
||
| 374 | if (!empty($hook)) { |
||
| 375 | $hook->setEventData(['content' => $content]); |
||
| 376 | $data = $hook->notifyNotificationContent(HOOK_EVENT_TYPE_PRE); |
||
| 377 | if (isset($data['content'])) { |
||
| 378 | $content = $data['content']; |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | $newMessageText = $linkToNewMessage = ''; |
||
| 383 | $showEmail = api_get_configuration_value('show_user_email_in_notification'); |
||
| 384 | $senderInfoName = ''; |
||
| 385 | if (!empty($senderInfo) && isset($senderInfo['complete_name'])) { |
||
| 386 | $senderInfoName = $senderInfo['complete_name']; |
||
| 387 | if ($showEmail && isset($senderInfo['complete_name_with_email_forced'])) { |
||
| 388 | $senderInfoName = $senderInfo['complete_name_with_email_forced']; |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | switch ($this->type) { |
||
| 393 | case self::NOTIFICATION_TYPE_DIRECT_MESSAGE: |
||
| 394 | $newMessageText = ''; |
||
| 395 | $linkToNewMessage = Display::url( |
||
| 396 | get_lang('See message'), |
||
| 397 | api_get_path(WEB_CODE_PATH).'messages/view_message.php?id='.$messageId |
||
| 398 | ); |
||
| 399 | break; |
||
| 400 | case self::NOTIFICATION_TYPE_MESSAGE: |
||
| 401 | $allow = api_get_configuration_value('messages_hide_mail_content'); |
||
| 402 | if ($allow) { |
||
| 403 | $content = ''; |
||
| 404 | } |
||
| 405 | if (!empty($senderInfo)) { |
||
| 406 | $newMessageText = sprintf( |
||
| 407 | get_lang('You have a new message from %s'), |
||
| 408 | $senderInfoName |
||
| 409 | ); |
||
| 410 | } |
||
| 411 | $linkToNewMessage = Display::url( |
||
| 412 | get_lang('See message'), |
||
| 413 | api_get_path(WEB_CODE_PATH).'messages/view_message.php?id='.$messageId |
||
| 414 | ); |
||
| 415 | break; |
||
| 416 | case self::NOTIFICATION_TYPE_INVITATION: |
||
| 417 | if (!empty($senderInfo)) { |
||
| 418 | $newMessageText = sprintf( |
||
| 419 | get_lang('You have a new invitation from %s'), |
||
| 420 | $senderInfoName |
||
| 421 | ); |
||
| 422 | } |
||
| 423 | $linkToNewMessage = Display::url( |
||
| 424 | get_lang('See invitation'), |
||
| 425 | api_get_path(WEB_CODE_PATH).'social/invitations.php' |
||
| 426 | ); |
||
| 427 | break; |
||
| 428 | case self::NOTIFICATION_TYPE_GROUP: |
||
| 429 | $topicPage = isset($_REQUEST['topics_page_nr']) ? (int) $_REQUEST['topics_page_nr'] : 0; |
||
| 430 | if (!empty($senderInfo)) { |
||
| 431 | $senderName = $senderInfo['group_info']['name']; |
||
| 432 | $newMessageText = sprintf(get_lang('You have received a new message in group %s'), $senderName); |
||
| 433 | $senderName = Display::url( |
||
| 434 | $senderInfoName, |
||
| 435 | api_get_path(WEB_CODE_PATH).'social/profile.php?'.$senderInfo['user_info']['user_id'] |
||
| 436 | ); |
||
| 437 | $newMessageText .= '<br />'.get_lang('User').': '.$senderName; |
||
| 438 | } |
||
| 439 | $groupUrl = api_get_path(WEB_CODE_PATH).'social/group_topics.php?id='.$senderInfo['group_info']['id'].'&topic_id='.$senderInfo['group_info']['topic_id'].'&msg_id='.$senderInfo['group_info']['msg_id'].'&topics_page_nr='.$topicPage; |
||
| 440 | $linkToNewMessage = Display::url(get_lang('See message'), $groupUrl); |
||
| 441 | break; |
||
| 442 | } |
||
| 443 | $preferenceUrl = api_get_path(WEB_CODE_PATH).'auth/profile.php'; |
||
| 444 | |||
| 445 | // You have received a new message text |
||
| 446 | if (!empty($newMessageText)) { |
||
| 447 | $content = $newMessageText.'<br /><hr><br />'.$content; |
||
| 448 | } |
||
| 449 | |||
| 450 | // See message with link text |
||
| 451 | if (!empty($linkToNewMessage) && 'true' == api_get_setting('allow_message_tool')) { |
||
| 452 | $content = $content.'<br /><br />'.$linkToNewMessage; |
||
| 453 | } |
||
| 454 | |||
| 455 | // You have received this message because you are subscribed text |
||
| 456 | $content = $content.'<br /><hr><i>'. |
||
| 457 | sprintf( |
||
| 458 | get_lang('You have received this notification because you are subscribed or involved in it to change your notification preferences please click here: %s'), |
||
| 459 | Display::url($preferenceUrl, $preferenceUrl) |
||
| 460 | ).'</i>'; |
||
| 461 | |||
| 462 | if (!empty($hook)) { |
||
| 463 | $hook->setEventData(['content' => $content]); |
||
| 464 | $data = $hook->notifyNotificationContent(HOOK_EVENT_TYPE_POST); |
||
| 465 | if (isset($data['content'])) { |
||
| 466 | $content = $data['content']; |
||
| 467 | } |
||
| 468 | } |
||
| 469 | |||
| 470 | return $content; |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Send the push notifications to Chamilo Mobile app. |
||
| 475 | * |
||
| 476 | * @param array $userIds The IDs of users who will be notified |
||
| 477 | * @param string $title The notification title |
||
| 478 | * @param string $content The notification content |
||
| 479 | * |
||
| 480 | * @return int The number of success notifications. Otherwise returns false |
||
| 481 | */ |
||
| 482 | public static function sendPushNotification(array $userIds, $title, $content) |
||
| 554 | } |
||
| 555 | } |
||
| 556 |