| Conditions | 22 |
| Paths | 11 |
| Total Lines | 148 |
| Code Lines | 100 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 287 | public function sendPendingMessages($urlId = 0) |
||
| 288 | { |
||
| 289 | if (!$this->allowed()) { |
||
| 290 | return 0; |
||
| 291 | } |
||
| 292 | |||
| 293 | $messagesSent = 0; |
||
| 294 | $now = api_get_utc_datetime(); |
||
| 295 | $result = $this->get_all(); |
||
| 296 | $extraFieldValue = new ExtraFieldValue('scheduled_announcement'); |
||
| 297 | |||
| 298 | foreach ($result as $result) { |
||
| 299 | if (empty($result['sent'])) { |
||
| 300 | if (!empty($result['date']) && $result['date'] < $now) { |
||
| 301 | $sessionId = $result['session_id']; |
||
| 302 | $sessionInfo = api_get_session_info($sessionId); |
||
| 303 | if (empty($sessionInfo)) { |
||
| 304 | continue; |
||
| 305 | } |
||
| 306 | $users = SessionManager::get_users_by_session( |
||
| 307 | $sessionId, |
||
| 308 | 0, |
||
| 309 | false, |
||
| 310 | $urlId |
||
| 311 | ); |
||
| 312 | |||
| 313 | $coachId = $sessionInfo['id_coach']; |
||
| 314 | |||
| 315 | if (empty($users) || empty($coachId)) { |
||
| 316 | continue; |
||
| 317 | } |
||
| 318 | |||
| 319 | $coachList = []; |
||
| 320 | if ($users) { |
||
| 321 | $sendToCoaches = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
| 322 | $result['id'], |
||
| 323 | 'send_to_coaches' |
||
| 324 | ); |
||
| 325 | $courseList = SessionManager::getCoursesInSession($sessionId); |
||
| 326 | if (!empty($sendToCoaches) && !empty($sendToCoaches['value']) && 1 == $sendToCoaches['value']) { |
||
| 327 | foreach ($courseList as $courseItemId) { |
||
| 328 | $coaches = SessionManager::getCoachesByCourseSession( |
||
| 329 | $sessionId, |
||
| 330 | $courseItemId |
||
| 331 | ); |
||
| 332 | $coachList = array_merge($coachList, $coaches); |
||
| 333 | } |
||
| 334 | $coachList = array_unique($coachList); |
||
| 335 | } |
||
| 336 | |||
| 337 | $this->update(['id' => $result['id'], 'sent' => 1]); |
||
| 338 | $attachments = $this->getAttachmentToString($result['id']); |
||
| 339 | $subject = $result['subject']; |
||
| 340 | |||
| 341 | $courseInfo = []; |
||
| 342 | if (!empty($courseList)) { |
||
| 343 | $courseId = current($courseList); |
||
| 344 | $courseInfo = api_get_course_info_by_id($courseId); |
||
| 345 | } |
||
| 346 | |||
| 347 | $message = ''; |
||
| 348 | foreach ($users as $user) { |
||
| 349 | // Take original message |
||
| 350 | $message = $result['message']; |
||
| 351 | $userInfo = api_get_user_info($user['user_id']); |
||
| 352 | |||
| 353 | $progress = ''; |
||
| 354 | if (!empty($sessionInfo) && !empty($courseInfo)) { |
||
| 355 | $progress = Tracking::get_avg_student_progress( |
||
| 356 | $user['user_id'], |
||
| 357 | $courseInfo['code'], |
||
| 358 | [], |
||
| 359 | $sessionId |
||
| 360 | ); |
||
| 361 | } |
||
| 362 | |||
| 363 | if (is_numeric($progress)) { |
||
| 364 | $progress = $progress.'%'; |
||
| 365 | } else { |
||
| 366 | $progress = '0%'; |
||
| 367 | } |
||
| 368 | |||
| 369 | $startTime = api_get_local_time( |
||
| 370 | $sessionInfo['access_start_date'], |
||
| 371 | null, |
||
| 372 | null, |
||
| 373 | true |
||
| 374 | ); |
||
| 375 | $endTime = api_get_local_time( |
||
| 376 | $sessionInfo['access_end_date'], |
||
| 377 | null, |
||
| 378 | null, |
||
| 379 | true |
||
| 380 | ); |
||
| 381 | |||
| 382 | $generalCoach = ''; |
||
| 383 | $generalCoachEmail = ''; |
||
| 384 | if (!empty($sessionInfo['id_coach'])) { |
||
| 385 | $coachInfo = api_get_user_info($sessionInfo['id_coach']); |
||
| 386 | if (!empty($coachInfo)) { |
||
| 387 | $generalCoach = $coachInfo['complete_name']; |
||
| 388 | $generalCoachEmail = $coachInfo['email']; |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | $tags = [ |
||
| 393 | '((session_name))' => $sessionInfo['name'], |
||
| 394 | '((session_start_date))' => $startTime, |
||
| 395 | '((general_coach))' => $generalCoach, |
||
| 396 | '((general_coach_email))' => $generalCoachEmail, |
||
| 397 | '((session_end_date))' => $endTime, |
||
| 398 | '((user_complete_name))' => $userInfo['complete_name'], |
||
| 399 | '((user_firstname))' => $userInfo['firstname'], |
||
| 400 | '((user_lastname))' => $userInfo['lastname'], |
||
| 401 | '((user_first_name))' => $userInfo['firstname'], |
||
| 402 | '((user_last_name))' => $userInfo['lastname'], |
||
| 403 | '((lp_progress))' => $progress, |
||
| 404 | ]; |
||
| 405 | |||
| 406 | $message = str_replace(array_keys($tags), $tags, $message); |
||
| 407 | $message .= $attachments; |
||
| 408 | |||
| 409 | MessageManager::send_message_simple( |
||
| 410 | $userInfo['user_id'], |
||
| 411 | $subject, |
||
| 412 | $message, |
||
| 413 | $coachId |
||
| 414 | ); |
||
| 415 | } |
||
| 416 | |||
| 417 | $message = get_lang('You\'re receiving a copy because, you\'re a course coach'). |
||
| 418 | '<br /><br />'.$message; |
||
| 419 | |||
| 420 | foreach ($coachList as $courseCoachId) { |
||
| 421 | MessageManager::send_message_simple( |
||
| 422 | $courseCoachId, |
||
| 423 | get_lang('You\'re receiving a copy because, you\'re a course coach').' '.$subject, |
||
| 424 | $message, |
||
| 425 | $coachId |
||
| 426 | ); |
||
| 427 | } |
||
| 428 | } |
||
| 429 | $messagesSent++; |
||
| 430 | } |
||
| 431 | } |
||
| 432 | } |
||
| 433 | |||
| 434 | return $messagesSent; |
||
| 435 | } |
||
| 475 |