ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilSkillNotifications.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2020 ILIAS open source, Extended GPL, see docs/LICENSE */
4
11{
15 protected $lng;
16
20 protected $user;
21
25 protected $client_ini;
26
30 protected $tree;
31
32
36 public function __construct()
37 {
38 global $DIC;
39
40 $this->lng = $DIC->language();
41 if (isset($DIC["ilUser"])) {
42 $this->user = $DIC->user();
43 }
44 if (isset($DIC["ilClientIniFile"])) {
45 $this->client_ini = $DIC["ilClientIniFile"];
46 }
47 if (isset($DIC["tree"])) {
48 $this->tree = $DIC->repositoryTree();
49 }
50 }
51
52 public function getId()
53 {
54 return "skll_notification";
55 }
56
57 public function getTitle()
58 {
60 $lng->loadLanguageModule("skll");
61 return $lng->txt("skll_skill_notification");
62 }
63
64 public function getDescription()
65 {
67 $lng->loadLanguageModule("skll");
68 return $lng->txt("skll_skill_notification_desc");
69 }
70
71 public function getDefaultScheduleType()
72 {
74 }
75
76 public function getDefaultScheduleValue()
77 {
78 return;
79 }
80
81 public function hasAutoActivation()
82 {
83 return false;
84 }
85
86 public function hasFlexibleSchedule()
87 {
88 return true;
89 }
90
91 public function run()
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 // $last_run = "2017-07-20 12:00:00";
125
126 // get latest course/group skill changes per user
127 $achievements = ilBasicSkill::getNewAchievementsPerUser($last_run);
128
129 foreach ($achievements as $user_id => $a) {
130 $this->sendMail($user_id, $a, $last_run);
131 }
132
133 // mails were sent - set cron job status accordingly
135
136 // reset language/date
138 $lng = $old_lng;
139
140 $log->debug("save run");
141
142 // save last run
143 $setting->set(get_class($this), date("Y-m-d H:i:s"));
144
145 $result = new ilCronJobResult();
146 $result->setStatus($status);
147
148 if ($status_details) {
149 $result->setMessage($status_details);
150 }
151
152 $log->debug("===Skill Notifications=== done");
153
154 return $result;
155 }
156
157
165 protected function sendMail($a_user_id, array $a_achievements, $a_last_run)
166 {
167 $ilClientIniFile = $this->client_ini;
169
170 $ntf = new ilSystemNotification();
171 $ntf->setLangModules(array("skll"));
172
173 // user specific language
174 $lng = $ntf->getUserLanguage($a_user_id);
175
176 $tmp = array();
177 $txt = "";
178 $last_obj_id = 0;
179
180 // order skill achievements per virtual skill tree
181 $vtree = new ilVirtualSkillTree();
182 $a_achievements = $vtree->getOrderedNodeset($a_achievements, "skill_id", "tref_id");
183
184 foreach ($a_achievements as $skill_level) {
185 $parent = array();
186
187 // path
188 $path = array();
189 foreach ($tree->getPathId($skill_level["trigger_ref_id"]) as $node) {
190 $path[] = $node;
191 }
192 $path = implode("-", $path);
193
194 $ref_id = $skill_level["trigger_ref_id"];
195 $obj_id = $skill_level["trigger_obj_id"];
196 $type = $skill_level["trigger_obj_type"];
197 $title = $skill_level["trigger_title"];
198
199 if ($skill_level["trigger_obj_id"] != $last_obj_id) {
200 $last_obj_id = $skill_level["trigger_obj_id"];
201 $txt .= "\n\n" . $lng->txt("obj_" . $type) . ": " . $title;
202 if ($tree->isInTree($ref_id)) {
203 $txt .= "\n" . ilLink::_getStaticLink($ref_id);
204 }
205 }
206
207 $date = ilDatePresentation::formatDate(new ilDateTime($skill_level["status_date"], IL_CAL_DATETIME));
208 $txt .= "\n $date, " . ilBasicSkill::_lookupTitle($skill_level["skill_id"], $skill_level["tref_id"]) . ": " .
209 ilBasicSkill::lookupLevelTitle($skill_level["level_id"]);
210 }
211
212 $ntf->setIntroductionLangId("skll_intro_skill_notification_for");
213
214 // index
215 $period = sprintf(
216 $lng->txt("skll_new_skill_achievements"),
219 );
220
221 // text
222 $ntf->addAdditionalInfo(
223 "",
224 trim($txt),
225 true
226 );
227
228 // :TODO: does it make sense to add client to subject?
229 $client = $ilClientIniFile->readVariable('client', 'name');
230 $subject = sprintf($lng->txt("skll_competence_achievements"), $client);
231
232 //die($ntf->composeAndGetMessage($a_user_id, null, "read", true));
233
234 // #10044
235 $mail = new ilMail(ANONYMOUS_USER_ID);
236 $mail->enqueue(
237 ilObjUser::_lookupLogin($a_user_id),
238 null,
239 null,
240 $subject,
241 $ntf->composeAndGetMessage($a_user_id, null, "read", true),
242 []
243 );
244 }
245}
$result
user()
Definition: user.php:4
An exception for terminatinating execution or to throw for unit testing.
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)
Cron job result data container.
Cron job application base class.
const SCHEDULE_TYPE_DAILY
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date @access public.
static setUseRelativeDates($a_status)
set use relative dates
static useRelativeDates()
check if relative dates are used
@classDescription Date and time handling
static getLogger($a_component_id)
Get component logger.
static _lookupLogin($a_user_id)
lookup login
ILIAS Setting Class.
Course/group skill notification.
hasFlexibleSchedule()
Can the schedule be configured?
sendMail($a_user_id, array $a_achievements, $a_last_run)
Send news mail for 1 user and n objects.
getDefaultScheduleValue()
Get schedule value.
getDefaultScheduleType()
Get schedule type.
hasAutoActivation()
Is to be activated on "installation".
static _lookupTitle($a_obj_id, $a_tref_id=0)
Lookup Title.
Wrapper classes for system notifications.
const ANONYMOUS_USER_ID
Definition: constants.php:25
if($_SERVER['argc']< 4) $client
Definition: cron.php:12
$txt
Definition: error.php:13
global $DIC
Definition: goto.php:24
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$type
$log
Definition: result.php:15