ILIAS  release_8 Revision v8.24
class.ilSkillNotifications.php
Go to the documentation of this file.
1<?php
2
21
28{
29 protected ilLanguage $lng;
30 protected ilObjUser $user;
32 protected ilTree $tree;
34
35 public function __construct()
36 {
37 global $DIC;
38
39 $this->lng = $DIC->language();
40 if (isset($DIC["ilUser"])) {
41 $this->user = $DIC->user();
42 }
43 if (isset($DIC["ilClientIniFile"])) {
44 $this->client_ini = $DIC["ilClientIniFile"];
45 }
46 if (isset($DIC["tree"])) {
47 $this->tree = $DIC->repositoryTree();
48 }
49 $this->tree_service = $DIC->skills()->tree();
50 }
51
52 public function getId(): string
53 {
54 return "skll_notification";
55 }
56
57 public function getTitle(): string
58 {
60 $lng->loadLanguageModule("skll");
61 return $lng->txt("skll_skill_notification");
62 }
63
64 public function getDescription(): string
65 {
67 $lng->loadLanguageModule("skll");
68 return $lng->txt("skll_skill_notification_desc");
69 }
70
71 public function getDefaultScheduleType(): int
72 {
74 }
75
76 public function getDefaultScheduleValue(): ?int
77 {
78 return null;
79 }
80
81 public function hasAutoActivation(): bool
82 {
83 return false;
84 }
85
86 public function hasFlexibleSchedule(): bool
87 {
88 return true;
89 }
90
91 public function run(): ilCronJobResult
92 {
93 global $DIC;
94
95 $lng = $DIC->language();
96
98 $log->debug("===Skill Notifications=== start");
99
101 $status_details = null;
102
103 $setting = new ilSetting("cron");
104 $last_run = $setting->get(get_class($this));
105
106 // no last run?
107 if (!$last_run) {
108 $last_run = date("Y-m-d H:i:s", strtotime("yesterday"));
109
110 $status_details = "No previous run found - starting from yesterday.";
111 } // migration: used to be date-only value
112 elseif (strlen($last_run) == 10) {
113 $last_run .= " 00:00:00";
114
115 $status_details = "Switched from daily runs to open schedule.";
116 }
117
118 // init language/date
119 $old_lng = $lng;
122
123
124 // get latest course/group skill changes per user
125 $achievements = ilBasicSkill::getNewAchievementsPerUser($last_run);
126
127 foreach ($achievements as $user_id => $a) {
128 $this->sendMail($user_id, $a, $last_run);
129 }
130
131 // mails were sent - set cron job status accordingly
133
134 // reset language/date
136 $lng = $old_lng;
137
138 $log->debug("save run");
139
140 // save last run
141 $setting->set(get_class($this), date("Y-m-d H:i:s"));
142
143 $result = new ilCronJobResult();
144 $result->setStatus($status);
145
146 if ($status_details) {
147 $result->setMessage($status_details);
148 }
149
150 $log->debug("===Skill Notifications=== done");
151
152 return $result;
153 }
154
158 protected function sendMail(int $a_user_id, array $a_achievements, string $a_last_run): void
159 {
160 $ilClientIniFile = $this->client_ini;
162
163 $ntf = new ilSystemNotification();
164 $ntf->setLangModules(array("skll"));
165
166 // user specific language
167 $lng = $ntf->getUserLanguage($a_user_id);
168
169 $txt = "";
170 $last_obj_id = 0;
171
172 // order skill achievements per virtual skill tree
173 $vtree = $this->tree_service->getGlobalVirtualSkillTree();
174 $a_achievements = $vtree->getOrderedNodeset($a_achievements, "skill_id", "tref_id");
175
176 foreach ($a_achievements as $skill_level) {
177
178 // path
179 $path = [];
180 foreach ($tree->getPathId($skill_level["trigger_ref_id"]) as $node) {
181 $path[] = $node;
182 }
183 $path = implode("-", $path);
184
185 $ref_id = $skill_level["trigger_ref_id"];
186 $obj_id = $skill_level["trigger_obj_id"];
187 $type = $skill_level["trigger_obj_type"];
188 $title = $skill_level["trigger_title"];
189
190 if ($skill_level["trigger_obj_id"] != $last_obj_id) {
191 $last_obj_id = $skill_level["trigger_obj_id"];
192 $txt .= "\n\n" . $lng->txt("obj_" . $type) . ": " . $title;
193 if ($tree->isInTree($ref_id)) {
195 }
196 }
197
198 $date = ilDatePresentation::formatDate(new ilDateTime($skill_level["status_date"], IL_CAL_DATETIME));
199 $txt .= "\n $date, " . ilBasicSkill::_lookupTitle($skill_level["skill_id"], $skill_level["tref_id"]) . ": " .
200 ilBasicSkill::lookupLevelTitle($skill_level["level_id"]);
201 }
202
203 $ntf->setIntroductionLangId("skll_intro_skill_notification_for");
204
205 // index
206 $period = sprintf(
207 $lng->txt("skll_new_skill_achievements"),
210 );
211
212 // text
213 $ntf->addAdditionalInfo(
214 "",
215 trim($txt),
216 true
217 );
218
219 // :TODO: does it make sense to add client to subject?
220 $client = $ilClientIniFile->readVariable('client', 'name');
221 $subject = sprintf($lng->txt("skll_competence_achievements"), $client);
222
223 //die($ntf->composeAndGetMessage($a_user_id, null, "read", true));
224
225 // #10044
226 $mail = new ilMail(ANONYMOUS_USER_ID);
227 $mail->enqueue(
228 ilObjUser::_lookupLogin($a_user_id),
229 '',
230 '',
231 $subject,
232 $ntf->composeAndGetMessage($a_user_id, null, "read", true),
233 []
234 );
235 }
236}
const IL_CAL_UNIX
const IL_CAL_DATETIME
static getNewAchievementsPerUser(string $a_timestamp, string $a_timestamp_to=null, int $a_user_id=0, int $a_self_eval=0)
static lookupLevelTitle(int $a_id)
const SCHEDULE_TYPE_DAILY
@depracated This will be replaced with an ENUM in ILIAS 9
static setUseRelativeDates(bool $a_status)
set use relative dates
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
@classDescription Date and time handling
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
language handling
loadLanguageModule(string $a_module)
Load language module.
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
getUserLanguage()
Return language of user.
static getLogger(string $a_component_id)
Get component logger.
User class.
static _lookupLogin(int $a_user_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Course/group skill notification.
sendMail(int $a_user_id, array $a_achievements, string $a_last_run)
Send news mail for 1 user and n objects.
hasAutoActivation()
Is to be activated on "installation", does only work for ILIAS core cron jobs.
static _lookupTitle(int $a_obj_id, int $a_tref_id=0)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
isInTree(?int $a_node_id)
get all information of a node.
getPathId(int $a_endnode_id, int $a_startnode_id=0)
get path from a given startnode to a given endnode if startnode is not given the rootnode is startnod...
const ANONYMOUS_USER_ID
Definition: constants.php:27
$client
$txt
Definition: error.php:13
global $DIC
Definition: feed.php:28
$ref_id
Definition: ltiauth.php:67
$path
Definition: ltiservices.php:32
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$type
$log
Definition: result.php:33