You are here

public function OpignoAnswerForm::sendEmailToManager in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Form/OpignoAnswerForm.php \Drupal\opigno_module\Form\OpignoAnswerForm::sendEmailToManager()

Send Email to manager.

1 call to OpignoAnswerForm::sendEmailToManager()
OpignoAnswerForm::save in src/Form/OpignoAnswerForm.php
Form submission handler for the 'save' action.

File

src/Form/OpignoAnswerForm.php, line 472

Class

OpignoAnswerForm
Form controller for Answer edit forms.

Namespace

Drupal\opigno_module\Form

Code

public function sendEmailToManager($module, $activity, $answer, $learning_path) {
  if (!$module instanceof OpignoModule || !$activity instanceof OpignoActivity || !$answer instanceof OpignoAnswer || !$learning_path instanceof Group) {
    return;
  }
  $config = \Drupal::config('opigno_learning_path.learning_path_settings');
  $student_activity_notify = $config
    ->get('opigno_learning_path_student_does_activity_notify');
  $student_activity = $config
    ->get('opigno_learning_path_student_does_activity');
  if (!$activity
    ->evaluationMethodManual() || empty($student_activity_notify) || empty($student_activity)) {
    return;
  }

  // Load managers and send email to each.
  $managers = $learning_path
    ->getMembers('learning_path-user_manager');
  $student = \Drupal::currentUser();
  $global_config = \Drupal::config('system.site');
  $options = [
    'absolute' => TRUE,
  ];
  $url = Url::fromRoute('view.opigno_score_modules.opigno_not_evaluated', [], $options);
  $link = Link::fromTextAndUrl(t('link'), $url)
    ->toString();
  foreach ($managers as $manager) {
    $user = $manager
      ->getUser();
    $email = $user
      ->getEmail();
    $lang = $user
      ->getPreferredLangcode();
    $params = [];
    $params['subject'] = t('@sitename Module review', [
      '@sitename' => $global_config
        ->get('name'),
    ]);
    $student_activity = str_replace('[sitename]', $global_config
      ->get('name'), $student_activity);
    $student_activity = str_replace('[user]', $student
      ->getAccountName(), $student_activity);
    $student_activity = str_replace('[manager]', $user
      ->getAccountName(), $student_activity);
    $student_activity = str_replace('[link]', $link, $student_activity);
    $student_activity = str_replace('[module]', $module
      ->getName(), $student_activity);
    $params['message'] = $student_activity;
    \Drupal::service('plugin.manager.mail')
      ->mail('opigno_learning_path', 'opigno_learning_path_managers_notify', $email, $lang, $params);
  }
}