| Total Complexity | 62 |
| Total Lines | 523 |
| 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 |
||
| 9 | class Notification extends Model |
||
| 10 | { |
||
| 11 | // mail_notify_message ("At once", "Daily", "No") |
||
| 12 | const NOTIFY_MESSAGE_AT_ONCE = 1; |
||
| 13 | const NOTIFY_MESSAGE_DAILY = 8; |
||
| 14 | const NOTIFY_MESSAGE_WEEKLY = 12; |
||
| 15 | const NOTIFY_MESSAGE_NO = 0; |
||
| 16 | |||
| 17 | // mail_notify_invitation ("At once", "Daily", "No") |
||
| 18 | const NOTIFY_INVITATION_AT_ONCE = 1; |
||
| 19 | const NOTIFY_INVITATION_DAILY = 8; |
||
| 20 | const NOTIFY_INVITATION_WEEKLY = 12; |
||
| 21 | const NOTIFY_INVITATION_NO = 0; |
||
| 22 | |||
| 23 | // mail_notify_group_message ("At once", "Daily", "No") |
||
| 24 | const NOTIFY_GROUP_AT_ONCE = 1; |
||
| 25 | const NOTIFY_GROUP_DAILY = 8; |
||
| 26 | const NOTIFY_GROUP_WEEKLY = 12; |
||
| 27 | const NOTIFY_GROUP_NO = 0; |
||
| 28 | |||
| 29 | // Notification types |
||
| 30 | const NOTIFICATION_TYPE_MESSAGE = 1; |
||
| 31 | const NOTIFICATION_TYPE_INVITATION = 2; |
||
| 32 | const NOTIFICATION_TYPE_GROUP = 3; |
||
| 33 | const NOTIFICATION_TYPE_WALL_MESSAGE = 4; |
||
| 34 | const NOTIFICATION_TYPE_DIRECT_MESSAGE = 5; |
||
| 35 | public $table; |
||
| 36 | public $columns = [ |
||
| 37 | 'id', |
||
| 38 | 'dest_user_id', |
||
| 39 | 'dest_mail', |
||
| 40 | 'title', |
||
| 41 | 'content', |
||
| 42 | 'send_freq', |
||
| 43 | 'created_at', |
||
| 44 | 'sent_at', |
||
| 45 | ]; |
||
| 46 | |||
| 47 | //Max length of the notification.content field |
||
| 48 | public $max_content_length = 254; |
||
| 49 | public $debug = false; |
||
| 50 | |||
| 51 | /* message, invitation, group messages */ |
||
| 52 | public $type; |
||
| 53 | public $adminName; |
||
| 54 | public $adminEmail; |
||
| 55 | public $titlePrefix; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Constructor. |
||
| 59 | */ |
||
| 60 | public function __construct() |
||
| 61 | { |
||
| 62 | $this->table = Database::get_main_table(TABLE_NOTIFICATION); |
||
| 63 | // Default no-reply email |
||
| 64 | $this->adminEmail = api_get_setting('noreply_email_address'); |
||
| 65 | $this->adminName = api_get_setting('siteName'); |
||
| 66 | $this->titlePrefix = '['.api_get_setting('siteName').'] '; |
||
| 67 | |||
| 68 | // If no-reply email doesn't exist use the admin name/email |
||
| 69 | if (empty($this->adminEmail)) { |
||
| 70 | $this->adminEmail = api_get_setting('emailAdministrator'); |
||
| 71 | $this->adminName = api_get_person_name( |
||
| 72 | api_get_setting('administratorName'), |
||
| 73 | api_get_setting('administratorSurname'), |
||
| 74 | null, |
||
| 75 | PERSON_NAME_EMAIL_ADDRESS |
||
| 76 | ); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | public function getTitlePrefix() |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | public function getDefaultPlatformSenderEmail() |
||
| 92 | { |
||
| 93 | return $this->adminEmail; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | public function getDefaultPlatformSenderName() |
||
| 100 | { |
||
| 101 | return $this->adminName; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Send the notifications. |
||
| 106 | * |
||
| 107 | * @param int $frequency notification frequency |
||
| 108 | */ |
||
| 109 | public function send($frequency = 8) |
||
| 110 | { |
||
| 111 | $notifications = $this->find( |
||
| 112 | 'all', |
||
| 113 | ['where' => ['sent_at IS NULL AND send_freq = ?' => $frequency]] |
||
| 114 | ); |
||
| 115 | |||
| 116 | if (!empty($notifications)) { |
||
| 117 | foreach ($notifications as $item_to_send) { |
||
| 118 | // Sending email |
||
| 119 | api_mail_html( |
||
| 120 | $item_to_send['dest_mail'], |
||
| 121 | $item_to_send['dest_mail'], |
||
| 122 | Security::filter_terms($item_to_send['title']), |
||
| 123 | Security::filter_terms($item_to_send['content']), |
||
| 124 | $this->adminName, |
||
| 125 | $this->adminEmail |
||
| 126 | ); |
||
| 127 | if ($this->debug) { |
||
| 128 | error_log('Sending message to: '.$item_to_send['dest_mail']); |
||
| 129 | } |
||
| 130 | |||
| 131 | // Updating |
||
| 132 | $item_to_send['sent_at'] = api_get_utc_datetime(); |
||
| 133 | $this->update($item_to_send); |
||
| 134 | if ($this->debug) { |
||
| 135 | error_log('Updating record : '.print_r($item_to_send, 1)); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param string $title |
||
| 143 | * @param array $senderInfo |
||
| 144 | * @param bool $forceTitleWhenSendingEmail force the use of $title as subject instead of "You have a new message" |
||
| 145 | * |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public function formatTitle($title, $senderInfo, $forceTitleWhenSendingEmail = false) |
||
| 149 | { |
||
| 150 | $newTitle = $this->getTitlePrefix(); |
||
| 151 | |||
| 152 | switch ($this->type) { |
||
| 153 | case self::NOTIFICATION_TYPE_MESSAGE: |
||
| 154 | if (!empty($senderInfo)) { |
||
| 155 | $senderName = api_get_person_name( |
||
| 156 | $senderInfo['firstname'], |
||
| 157 | $senderInfo['lastname'], |
||
| 158 | null, |
||
| 159 | PERSON_NAME_EMAIL_ADDRESS |
||
| 160 | ); |
||
| 161 | $newTitle .= sprintf(get_lang('You have a new message from %s'), $senderName); |
||
| 162 | } |
||
| 163 | break; |
||
| 164 | case self::NOTIFICATION_TYPE_DIRECT_MESSAGE: |
||
| 165 | $newTitle = $title; |
||
| 166 | break; |
||
| 167 | case self::NOTIFICATION_TYPE_INVITATION: |
||
| 168 | if (!empty($senderInfo)) { |
||
| 169 | $senderName = api_get_person_name( |
||
| 170 | $senderInfo['firstname'], |
||
| 171 | $senderInfo['lastname'], |
||
| 172 | null, |
||
| 173 | PERSON_NAME_EMAIL_ADDRESS |
||
| 174 | ); |
||
| 175 | $newTitle .= sprintf(get_lang('You have a new invitation from %s'), $senderName); |
||
| 176 | } |
||
| 177 | break; |
||
| 178 | case self::NOTIFICATION_TYPE_GROUP: |
||
| 179 | if (!empty($senderInfo)) { |
||
| 180 | $senderName = $senderInfo['group_info']['name']; |
||
| 181 | $newTitle .= sprintf(get_lang('You have received a new message in group %s'), $senderName); |
||
| 182 | $senderName = api_get_person_name( |
||
| 183 | $senderInfo['user_info']['firstname'], |
||
| 184 | $senderInfo['user_info']['lastname'], |
||
| 185 | null, |
||
| 186 | PERSON_NAME_EMAIL_ADDRESS |
||
| 187 | ); |
||
| 188 | $newTitle .= $senderName; |
||
| 189 | } |
||
| 190 | break; |
||
| 191 | } |
||
| 192 | |||
| 193 | // The title won't be changed, it will be used as is |
||
| 194 | if ($forceTitleWhenSendingEmail) { |
||
| 195 | $newTitle = $title; |
||
| 196 | } |
||
| 197 | |||
| 198 | /*if (!empty($hook)) { |
||
| 199 | $hook->setEventData(['title' => $newTitle]); |
||
| 200 | $data = $hook->notifyNotificationTitle(HOOK_EVENT_TYPE_POST); |
||
| 201 | if (isset($data['title'])) { |
||
| 202 | $newTitle = $data['title']; |
||
| 203 | } |
||
| 204 | }*/ |
||
| 205 | |||
| 206 | return $newTitle; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Save message notification. |
||
| 211 | * |
||
| 212 | * @param int $type message type |
||
| 213 | * NOTIFICATION_TYPE_MESSAGE, |
||
| 214 | * NOTIFICATION_TYPE_INVITATION, |
||
| 215 | * NOTIFICATION_TYPE_GROUP |
||
| 216 | * @param int $messageId |
||
| 217 | * @param array $userList recipients: user list of ids |
||
| 218 | * @param string $title |
||
| 219 | * @param string $content |
||
| 220 | * @param array $senderInfo result of api_get_user_info() or GroupPortalManager:get_group_data() |
||
| 221 | * @param array $attachments |
||
| 222 | * @param array $smsParameters |
||
| 223 | * @param bool $forceTitleWhenSendingEmail force the use of $title as subject instead of "You have a new message" |
||
| 224 | */ |
||
| 225 | public function saveNotification( |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Formats the content in order to add the welcome message, |
||
| 349 | * the notification preference, etc. |
||
| 350 | * |
||
| 351 | * @param int $messageId |
||
| 352 | * @param string $content |
||
| 353 | * @param array $senderInfo result of api_get_user_info() or |
||
| 354 | * GroupPortalManager:get_group_data() |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | * */ |
||
| 358 | public function formatContent($messageId, $content, $senderInfo) |
||
| 359 | { |
||
| 360 | $newMessageText = $linkToNewMessage = ''; |
||
| 361 | $showEmail = api_get_configuration_value('show_user_email_in_notification'); |
||
| 362 | $senderInfoName = ''; |
||
| 363 | if (!empty($senderInfo) && isset($senderInfo['complete_name'])) { |
||
| 364 | $senderInfoName = $senderInfo['complete_name']; |
||
| 365 | if ($showEmail && isset($senderInfo['complete_name_with_email_forced'])) { |
||
| 366 | $senderInfoName = $senderInfo['complete_name_with_email_forced']; |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | switch ($this->type) { |
||
| 371 | case self::NOTIFICATION_TYPE_DIRECT_MESSAGE: |
||
| 372 | $newMessageText = ''; |
||
| 373 | $linkToNewMessage = Display::url( |
||
| 374 | get_lang('See message'), |
||
| 375 | api_get_path(WEB_CODE_PATH).'messages/view_message.php?id='.$messageId |
||
| 376 | ); |
||
| 377 | break; |
||
| 378 | case self::NOTIFICATION_TYPE_MESSAGE: |
||
| 379 | $allow = api_get_configuration_value('messages_hide_mail_content'); |
||
| 380 | if ($allow) { |
||
| 381 | $content = ''; |
||
| 382 | } |
||
| 383 | if (!empty($senderInfo)) { |
||
| 384 | $newMessageText = sprintf( |
||
| 385 | get_lang('You have a new message from %s'), |
||
| 386 | $senderInfoName |
||
| 387 | ); |
||
| 388 | } |
||
| 389 | $linkToNewMessage = Display::url( |
||
| 390 | get_lang('See message'), |
||
| 391 | api_get_path(WEB_CODE_PATH).'messages/view_message.php?id='.$messageId |
||
| 392 | ); |
||
| 393 | break; |
||
| 394 | case self::NOTIFICATION_TYPE_INVITATION: |
||
| 395 | if (!empty($senderInfo)) { |
||
| 396 | $newMessageText = sprintf( |
||
| 397 | get_lang('You have a new invitation from %s'), |
||
| 398 | $senderInfoName |
||
| 399 | ); |
||
| 400 | } |
||
| 401 | $linkToNewMessage = Display::url( |
||
| 402 | get_lang('See invitation'), |
||
| 403 | api_get_path(WEB_CODE_PATH).'social/invitations.php' |
||
| 404 | ); |
||
| 405 | break; |
||
| 406 | case self::NOTIFICATION_TYPE_GROUP: |
||
| 407 | $topicPage = isset($_REQUEST['topics_page_nr']) ? (int) $_REQUEST['topics_page_nr'] : 0; |
||
| 408 | if (!empty($senderInfo)) { |
||
| 409 | $senderName = $senderInfo['group_info']['name']; |
||
| 410 | $newMessageText = sprintf(get_lang('You have received a new message in group %s'), $senderName); |
||
| 411 | $senderName = Display::url( |
||
| 412 | $senderInfoName, |
||
| 413 | api_get_path(WEB_CODE_PATH).'social/profile.php?'.$senderInfo['user_info']['user_id'] |
||
| 414 | ); |
||
| 415 | $newMessageText .= '<br />'.get_lang('User').': '.$senderName; |
||
| 416 | } |
||
| 417 | $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; |
||
| 418 | $linkToNewMessage = Display::url(get_lang('See message'), $groupUrl); |
||
| 419 | break; |
||
| 420 | } |
||
| 421 | $preferenceUrl = api_get_path(WEB_CODE_PATH).'auth/profile.php'; |
||
| 422 | |||
| 423 | // You have received a new message text |
||
| 424 | if (!empty($newMessageText)) { |
||
| 425 | $content = $newMessageText.'<br /><hr><br />'.$content; |
||
| 426 | } |
||
| 427 | |||
| 428 | // See message with link text |
||
| 429 | if (!empty($linkToNewMessage) && 'true' == api_get_setting('allow_message_tool')) { |
||
| 430 | $content = $content.'<br /><br />'.$linkToNewMessage; |
||
| 431 | } |
||
| 432 | |||
| 433 | // You have received this message because you are subscribed text |
||
| 434 | $content = $content.'<br /><hr><i>'. |
||
| 435 | sprintf( |
||
| 436 | get_lang('You have received this notification because you are subscribed or involved in it to change your notification preferences please click here: %s'), |
||
| 437 | Display::url($preferenceUrl, $preferenceUrl) |
||
| 438 | ).'</i>'; |
||
| 439 | |||
| 440 | /*if (!empty($hook)) { |
||
| 441 | $hook->setEventData(['content' => $content]); |
||
| 442 | $data = $hook->notifyNotificationContent(HOOK_EVENT_TYPE_POST); |
||
| 443 | if (isset($data['content'])) { |
||
| 444 | $content = $data['content']; |
||
| 445 | } |
||
| 446 | }*/ |
||
| 447 | |||
| 448 | return $content; |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Send the push notifications to Chamilo Mobile app. |
||
| 453 | * |
||
| 454 | * @param array $userIds The IDs of users who will be notified |
||
| 455 | * @param string $title The notification title |
||
| 456 | * @param string $content The notification content |
||
| 457 | * |
||
| 458 | * @return int The number of success notifications. Otherwise returns false |
||
| 459 | */ |
||
| 460 | public static function sendPushNotification(array $userIds, $title, $content) |
||
| 532 | } |
||
| 533 | } |
||
| 534 |