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