Conditions | 43 |
Paths | 19 |
Total Lines | 219 |
Code Lines | 149 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
338 | public function sendPendingMessages($urlId = 0) |
||
339 | { |
||
340 | if (!$this->allowed()) { |
||
341 | return 0; |
||
342 | } |
||
343 | |||
344 | $messagesSent = 0; |
||
345 | $now = api_get_utc_datetime(); |
||
346 | $result = $this->get_all(); |
||
347 | $extraFieldValue = new ExtraFieldValue('scheduled_announcement'); |
||
348 | |||
349 | // get user extra fields list (only visible to self and filter-able) |
||
350 | $extraField = new ExtraField('user'); |
||
351 | $extraFields = $extraField->get_all(['filter = ? AND visible_to_self = ?' => [1, 1]]); |
||
352 | |||
353 | foreach ($result as $result) { |
||
354 | if (empty($result['sent'])) { |
||
355 | if (!empty($result['date']) && $result['date'] < $now) { |
||
356 | $sessionId = $result['session_id']; |
||
357 | $sessionInfo = api_get_session_info($sessionId); |
||
358 | if (empty($sessionInfo)) { |
||
359 | continue; |
||
360 | } |
||
361 | $users = SessionManager::get_users_by_session( |
||
362 | $sessionId, |
||
363 | 0, |
||
364 | false, |
||
365 | $urlId |
||
366 | ); |
||
367 | |||
368 | $coachId = $sessionInfo['id_coach']; |
||
369 | |||
370 | if (empty($users) || empty($coachId)) { |
||
371 | continue; |
||
372 | } |
||
373 | |||
374 | $coachList = []; |
||
375 | if ($users) { |
||
376 | $sendToCoaches = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
377 | $result['id'], |
||
378 | 'send_to_coaches' |
||
379 | ); |
||
380 | $courseList = SessionManager::getCoursesInSession($sessionId); |
||
381 | if (!empty($sendToCoaches) && !empty($sendToCoaches['value']) && 1 == $sendToCoaches['value']) { |
||
382 | foreach ($courseList as $courseItemId) { |
||
383 | $coaches = SessionManager::getCoachesByCourseSession( |
||
384 | $sessionId, |
||
385 | $courseItemId |
||
386 | ); |
||
387 | $coachList = array_merge($coachList, $coaches); |
||
388 | } |
||
389 | $coachList = array_unique($coachList); |
||
390 | } |
||
391 | |||
392 | $useBaseProgress = api_get_configuration_value('scheduled_announcements_use_base_progress'); |
||
393 | if ($useBaseProgress) { |
||
394 | $baseProgress = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
395 | $result['id'], |
||
396 | 'use_base_progress' |
||
397 | ); |
||
398 | if (empty($baseProgress) || empty($baseProgress['value']) || $baseProgress['value'] < 1) { |
||
399 | $this->update(['id' => $result['id'], 'sent' => 1]); |
||
400 | } |
||
401 | } else { |
||
402 | $this->update(['id' => $result['id'], 'sent' => 1]); |
||
403 | } |
||
404 | |||
405 | $attachments = $this->getAttachmentToString($result['id']); |
||
406 | $subject = $result['subject']; |
||
407 | |||
408 | $courseInfo = []; |
||
409 | if (!empty($courseList)) { |
||
410 | $courseId = current($courseList); |
||
411 | $courseInfo = api_get_course_info_by_id($courseId); |
||
412 | } |
||
413 | |||
414 | $message = ''; |
||
415 | foreach ($users as $user) { |
||
416 | // Take original message |
||
417 | $message = $result['message']; |
||
418 | $userInfo = api_get_user_info($user['user_id']); |
||
419 | $userPicture = UserManager::getUserPicture($user['user_id'], USER_IMAGE_SIZE_ORIGINAL); |
||
420 | |||
421 | $progress = ''; |
||
422 | if (!empty($sessionInfo) && !empty($courseInfo)) { |
||
423 | $progress = Tracking::get_avg_student_progress( |
||
424 | $user['user_id'], |
||
425 | $courseInfo['code'], |
||
426 | [], |
||
427 | $sessionId |
||
428 | ); |
||
429 | } |
||
430 | |||
431 | if ($useBaseProgress) { |
||
432 | $baseProgress = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
433 | $result['id'], |
||
434 | 'use_base_progress' |
||
435 | ); |
||
436 | if (!empty($baseProgress) && !empty($baseProgress['value']) && $baseProgress['value'] >= 1) { |
||
437 | if ((is_numeric($progress) && $progress > $baseProgress['value']) || !is_numeric($progress)) { |
||
438 | continue; |
||
439 | } else { |
||
440 | $comment = json_decode($baseProgress['comment'], true); |
||
441 | if ($comment !== null && is_array($comment)) { |
||
442 | if (isset($comment['sended']) && is_array($comment['sended'])) { |
||
443 | $userFound = false; |
||
444 | foreach ($comment['sended'] as $item) { |
||
445 | if (isset($item['user']) && $item['user'] === $user['user_id']) { |
||
446 | $userFound = true; |
||
447 | break; |
||
448 | } |
||
449 | } |
||
450 | if ($userFound) { |
||
451 | continue; |
||
452 | } else { |
||
453 | $comment['sended'][] = ['user' => $user['user_id'], 'send_date' => time(), 'progress_user' => $progress, 'progress_mark' => $baseProgress['value']]; |
||
454 | $newExtraFieldParams = $baseProgress; |
||
455 | $newExtraFieldParams['comment'] = json_encode($comment); |
||
456 | $extraFieldValue->save($newExtraFieldParams); |
||
457 | } |
||
458 | } |
||
459 | } else { |
||
460 | $comment['sended'][] = ['user' => $user['user_id'], 'send_date' => time(), 'progress_user' => $progress, 'progress_mark' => $baseProgress['value']]; |
||
461 | $newExtraFieldParams = $baseProgress; |
||
462 | $newExtraFieldParams['comment'] = json_encode($comment); |
||
463 | $extraFieldValue->save($newExtraFieldParams); |
||
464 | } |
||
465 | } |
||
466 | } |
||
467 | } |
||
468 | |||
469 | if (is_numeric($progress)) { |
||
470 | $progress = $progress.'%'; |
||
471 | } else { |
||
472 | $progress = '0%'; |
||
473 | } |
||
474 | |||
475 | $startTime = api_get_local_time( |
||
476 | $sessionInfo['access_start_date'], |
||
477 | null, |
||
478 | null, |
||
479 | true |
||
480 | ); |
||
481 | $endTime = api_get_local_time( |
||
482 | $sessionInfo['access_end_date'], |
||
483 | null, |
||
484 | null, |
||
485 | true |
||
486 | ); |
||
487 | |||
488 | $generalCoach = ''; |
||
489 | $generalCoachEmail = ''; |
||
490 | if (!empty($coachId)) { |
||
491 | $coachInfo = api_get_user_info($coachId); |
||
492 | if (!empty($coachInfo)) { |
||
493 | $generalCoach = $coachInfo['complete_name']; |
||
494 | $generalCoachEmail = $coachInfo['email']; |
||
495 | } |
||
496 | } |
||
497 | |||
498 | $tags = [ |
||
499 | '((session_name))' => $sessionInfo['name'], |
||
500 | '((session_start_date))' => $startTime, |
||
501 | '((general_coach))' => $generalCoach, |
||
502 | '((general_coach_email))' => $generalCoachEmail, |
||
503 | '((session_end_date))' => $endTime, |
||
504 | '((user_username))' => $userInfo['username'], |
||
505 | '((user_complete_name))' => $userInfo['complete_name'], |
||
506 | '((user_firstname))' => $userInfo['firstname'], |
||
507 | '((user_lastname))' => $userInfo['lastname'], |
||
508 | '((user_first_name))' => $userInfo['firstname'], |
||
509 | '((user_last_name))' => $userInfo['lastname'], |
||
510 | '((user_official_code))' => $userInfo['official_code'], |
||
511 | '((user_picture))' => $userPicture, |
||
512 | '((lp_progress))' => $progress, |
||
513 | ]; |
||
514 | |||
515 | if (!empty($extraFields)) { |
||
516 | $efv = new ExtraFieldValue('user'); |
||
517 | |||
518 | foreach ($extraFields as $extraField) { |
||
519 | $valueExtra = $efv->get_values_by_handler_and_field_variable( |
||
520 | $user['user_id'], |
||
521 | $extraField['variable'], |
||
522 | true |
||
523 | ); |
||
524 | $tags['(('.strtolower($extraField['variable']).'))'] = $valueExtra['value']; |
||
525 | } |
||
526 | } |
||
527 | |||
528 | $message = str_replace(array_keys($tags), $tags, $message); |
||
529 | $message .= $attachments; |
||
530 | |||
531 | MessageManager::send_message_simple( |
||
532 | $userInfo['user_id'], |
||
533 | $subject, |
||
534 | $message, |
||
535 | $coachId |
||
536 | ); |
||
537 | } |
||
538 | |||
539 | $message = get_lang('YouAreReceivingACopyBecauseYouAreACourseCoach').'<br /><br />'.$message; |
||
540 | |||
541 | foreach ($coachList as $courseCoachId) { |
||
542 | MessageManager::send_message_simple( |
||
543 | $courseCoachId, |
||
544 | get_lang('YouAreReceivingACopyBecauseYouAreACourseCoach').' '.$subject, |
||
545 | $message, |
||
546 | $coachId |
||
547 | ); |
||
548 | } |
||
549 | } |
||
550 | |||
551 | $messagesSent++; |
||
552 | } |
||
553 | } |
||
554 | } |
||
555 | |||
556 | return $messagesSent; |
||
557 | } |
||
613 |