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