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