ILIAS  trunk Revision v11.0_alpha-1731-gff9cd7e2bd3
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilPRGMail.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 class ilPRGMail
25 {
26  protected const LANGMODULE = 'prg';
27 
31  protected array $languages;
32 
33  public function __construct(
34  protected ilComponentLogger $log,
36  ) {
37  $lng->loadLanguageModule(self::LANGMODULE);
38  $lng->loadLanguageModule("mail");
39  $this->languages[$lng->getLangKey()] = $lng;
40  }
41 
45  protected function getAssignmentAndProgramme(int $assignment_id, int $root_prg_id): array
46  {
47  $prg = ilObjStudyProgramme::getInstanceByObjId($root_prg_id);
48  $ass = $prg->getSpecificAssignment($assignment_id);
49  return [$ass, $prg];
50  }
51 
52  protected function getUserLanguage(int $usr_id): string
53  {
54  return \ilObjUser::_getPreferences($usr_id)['language'];
55  }
56 
57  protected function txt(string $identifier, string $lang): string
58  {
59  if(!array_key_exists($lang, $this->languages)) {
60  $lng = new \ilLanguage($lang);
61  $lng->loadLanguageModule(self::LANGMODULE);
62  $lng->loadLanguageModule("mail");
63  $this->languages[$lang] = $lng;
64  }
65  $lng = $this->languages[$lang];
66  return $lng->txtlng(self::LANGMODULE, $identifier, $lang);
67  }
68 
69  protected function sendMail(
71  ilPRGAssignment $assignment,
72  string $subject,
73  string $body_template
74  ): bool {
75  $user_info = $assignment->getUserInformation();
76  $gender = $user_info->getGender() ?: 'anonymous';
77  $name = implode(' ', [$user_info->getFirstname(), $user_info->getLastname()]);
78  $login = $user_info->getLogin();
79  $prg_link = \ilLink::_getStaticLink(ilObjStudyProgramme::getRefIdFor($assignment->getRootId()), 'prg');
80 
81  $lang = $this->getUserLanguage($assignment->getUserId());
82  $salutation = $this->txt("mail_salutation_" . $gender, $lang);
83  $subject = $this->txt($subject, $lang);
84  $body_template = $this->txt($body_template, $lang);
85 
86  $body = sprintf(
87  $body_template,
88  $salutation,
89  $name,
90  $prg->getTitle()
91  )
92  . '<br /><br />' . $prg_link;
93 
94  $mail = new ilMail(ANONYMOUS_USER_ID);
95  try {
96  $mail->enqueue($login, '', '', $subject, $body, []);
97  return true;
98  } catch (Exception $e) {
99  $this->log->write($e->getMessage());
100  return false;
101  }
102  }
103 
104  public function sendInformToReAssignMail(int $assignment_id, int $root_prg_id): void
105  {
106  list($ass, $prg) = $this->getAssignmentAndProgramme($assignment_id, $root_prg_id);
107 
108  if (! $prg->getSettings()->getAutoMailSettings()->getReminderNotRestartedByUserDays() > 0) {
109  $this->log->write("Send info to re-assign mail is deactivated in study programme settings");
110  return;
111  }
112 
113  $subject = "info_to_re_assign_mail_subject";
114  $body_template = "info_to_re_assign_mail_body";
115  $sent = $this->sendMail($prg, $ass, $subject, $body_template);
116 
117  if ($sent) {
118  $prg->storeExpiryInfoSentFor($ass);
119  }
120  }
121 
122  public function resetExpiryInfoSentFor(int $assignment_id, int $root_prg_id): void
123  {
124  list($ass, $prg) = $this->getAssignmentAndProgramme($assignment_id, $root_prg_id);
125  $now = new \DateTimeImmutable();
126  $vq = $ass->getProgressTree()->getValidityOfQualification();
127 
128  if ($vq && $vq > $now) {
129  $prg->resetExpiryInfoSentFor($ass);
130  }
131  }
132 
133  public function sendRiskyToFailMail(int $assignment_id, int $root_prg_id): void
134  {
135  list($ass, $prg) = $this->getAssignmentAndProgramme($assignment_id, $root_prg_id);
136 
137  if (! $prg->getSettings()->getAutoMailSettings()->getProcessingEndsNotSuccessfulDays() > 0) {
138  $this->log->write("Send risky to fail mail is deactivated in study programme settings");
139  return;
140  }
141 
142  $lang = $this->getUserLanguage($ass->getUserId());
143  $subject = "risky_to_fail_mail_subject";
144  $body_template = "risky_to_fail_mail_body";
145  $sent = $this->sendMail($prg, $ass, $subject, $body_template);
146 
147  if ($sent) {
148  $prg->storeRiskyToFailSentFor($ass);
149  }
150  }
151 
152  public function resetRiskyToFailSentFor(int $assignment_id, int $root_prg_id): void
153  {
154  list($ass, $prg) = $this->getAssignmentAndProgramme($assignment_id, $root_prg_id);
155  $now = new \DateTimeImmutable();
156  $deadline = $ass->getProgressTree()->getDeadline();
157  if ($deadline && $deadline > $now) {
158  $prg->resetRiskyToFailSentFor($ass);
159  }
160  }
161 
162  public function sendReAssignedMail(int $assignment_id, int $root_prg_id): bool
163  {
164  list($ass, $prg) = $this->getAssignmentAndProgramme($assignment_id, $root_prg_id);
165 
166  if (! $prg->getSettings()->getAutoMailSettings()->getSendReAssignedMail()) {
167  $this->log->write("Send re assign mail is deactivated in study programme settings");
168  return false;
169  }
170 
171  $lang = $this->getUserLanguage($ass->getUserId());
172  $subject = "re_assigned_mail_subject";
173  $body_template = "re_assigned_mail_body";
174  $sent = $this->sendMail($prg, $ass, $subject, $body_template);
175 
176  return $sent;
177  }
178 }
sendReAssignedMail(int $assignment_id, int $root_prg_id)
const ANONYMOUS_USER_ID
Definition: constants.php:27
__construct(protected ilComponentLogger $log, ilLanguage $lng)
resetRiskyToFailSentFor(int $assignment_id, int $root_prg_id)
getLangKey()
Return lang key.
Component logger with individual log levels by component id.
loadLanguageModule(string $a_module)
Load language module.
array $languages
var <string, ilLanguage> $languages
resetExpiryInfoSentFor(int $assignment_id, int $root_prg_id)
sendMail(ilObjStudyProgramme $prg, ilPRGAssignment $assignment, string $subject, string $body_template)
getUserLanguage(int $usr_id)
$log
Definition: result.php:32
txt(string $identifier, string $lang)
const LANGMODULE
Send mails to users (usually triggered by cron)
getAssignmentAndProgramme(int $assignment_id, int $root_prg_id)
static getInstanceByObjId(int $obj_id)
$lang
Definition: xapiexit.php:25
global $lng
Definition: privfeed.php:31
static getRefIdFor(int $obj_id)
sendInformToReAssignMail(int $assignment_id, int $root_prg_id)
sendRiskyToFailMail(int $assignment_id, int $root_prg_id)
Assignments are relations of users to a PRG; They hold progress-information for (sub-)nodes of the PR...