ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilCronManagerGUI.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5include_once "Services/Cron/classes/class.ilCronManager.php";
6
17{
21 protected $lng;
22
26 protected $ctrl;
27
31 protected $settings;
32
36 protected $tpl;
38 private $rbac;
40 private $error;
42 private $dic;
43
47 public function __construct()
48 {
49 global $DIC;
50
51 $this->lng = $DIC->language();
52 $this->ctrl = $DIC->ctrl();
53 $this->settings = $DIC->settings();
54 $this->tpl = $DIC->ui()->mainTemplate();
55 $this->dic = $DIC;
56 $this->rbac = $DIC->rbac();
57 $this->error = $DIC['ilErr'];
58
59 $this->lng->loadLanguageModule('cron');
60 }
61
62 public function executeCommand()
63 {
64 if (!$this->rbac->system()->checkAccess('visible,read', SYSTEM_FOLDER_ID)) {
65 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
66 }
67
68 $class = $this->ctrl->getNextClass($this);
69
70 switch ($class) {
71 case "ilpropertyformgui":
72 $form = $this->initEditForm($_REQUEST['jid']);
73 $this->ctrl->forwardCommand($form);
74 break;
75 }
76 $cmd = $this->ctrl->getCmd("render");
77 $this->$cmd();
78
79 return true;
80 }
81
82 protected function render()
83 {
84 if ($this->settings->get('last_cronjob_start_ts')) {
85 $tstamp = ilDatePresentation::formatDate(new ilDateTime($this->settings->get('last_cronjob_start_ts'), IL_CAL_UNIX));
86 } else {
87 $tstamp = $this->lng->txt('cronjob_last_start_unknown');
88 }
89 ilUtil::sendInfo($this->lng->txt('cronjob_last_start') . ": " . $tstamp);
90
91 include_once "Services/Cron/classes/class.ilCronManagerTableGUI.php";
92 $tbl = new ilCronManagerTableGUI(
93 $this,
94 "render",
95 $this->dic,
96 $this->rbac->system()->checkAccess('write', SYSTEM_FOLDER_ID)
97 );
98 $this->tpl->setContent($tbl->getHTML());
99 }
100
101 public function edit(ilPropertyFormGUI $a_form = null)
102 {
103 if (!$this->rbac->system()->checkAccess('write', SYSTEM_FOLDER_ID)) {
104 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
105 }
106
107 $id = $_REQUEST["jid"];
108 if (!$id) {
109 $this->ctrl->redirect($this, "render");
110 }
111
112 if (!$a_form) {
113 $a_form = $this->initEditForm($id);
114 }
115
116 $this->tpl->setContent($a_form->getHTML());
117 }
118
124 protected function getScheduleTypeFormElementName(int $scheduleTypeId)
125 {
126 switch ($scheduleTypeId) {
128 return $this->lng->txt('cron_schedule_daily');
129
131 return $this->lng->txt('cron_schedule_weekly');
132
134 return $this->lng->txt('cron_schedule_monthly');
135
137 return $this->lng->txt('cron_schedule_quarterly');
138
140 return $this->lng->txt('cron_schedule_yearly');
141
143 return sprintf($this->lng->txt('cron_schedule_in_minutes'), 'x');
144
146 return sprintf($this->lng->txt('cron_schedule_in_hours'), 'x');
147
149 return sprintf($this->lng->txt('cron_schedule_in_days'), 'x');
150 }
151
152 throw new \InvalidArgumentException(sprintf('The passed argument %s is invalid!', var_export($scheduleTypeId, 1)));
153 }
154
160 protected function getScheduleValueFormElementName(int $scheduleTypeId)
161 {
162 switch ($scheduleTypeId) {
164 return 'smini';
165
167 return 'shri';
168
170 return 'sdyi';
171 }
172
173 throw new \InvalidArgumentException(sprintf('The passed argument %s is invalid!', var_export($scheduleTypeId, 1)));
174 }
175
180 protected function hasScheduleValue(int $scheduleTypeId) : bool
181 {
182 return in_array(
183 $scheduleTypeId,
184 [
188 ]
189 );
190 }
191
192 protected function initEditForm($a_job_id)
193 {
194 $job = ilCronManager::getJobInstanceById($a_job_id);
195 if (!$job) {
196 $this->ctrl->redirect($this, "render");
197 }
198
199 $this->ctrl->setParameter($this, "jid", $a_job_id);
200
201 $data = array_pop(ilCronManager::getCronJobData($job->getId()));
202
203 include_once("Services/Cron/classes/class.ilCronJob.php");
204 include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
205 $form = new ilPropertyFormGUI();
206 $form->setFormAction($this->ctrl->getFormAction($this, "update"));
207 $form->setTitle($this->lng->txt("cron_action_edit") . ': "' . $job->getTitle() . '"');
208
209 if ($job->hasFlexibleSchedule()) {
210 $type = new ilRadioGroupInputGUI($this->lng->txt('cron_schedule_type'), 'type');
211 $type->setRequired(true);
212 $type->setValue($data['schedule_type']);
213
214 foreach ($job->getAllScheduleTypes() as $typeId) {
215 if (!in_array($typeId, $job->getValidScheduleTypes())) {
216 continue;
217 }
218
219 $option = new ilRadioOption(
220 $this->getScheduleTypeFormElementName($typeId),
221 $typeId
222 );
223 $type->addOption($option);
224
225 if (in_array($typeId, $job->getScheduleTypesWithValues())) {
226 $scheduleValue = new ilNumberInputGUI(
227 $this->lng->txt('cron_schedule_value'),
228 $this->getScheduleValueFormElementName($typeId)
229 );
230 $scheduleValue->allowDecimals(false);
231 $scheduleValue->setRequired(true);
232 $scheduleValue->setSize(5);
233 if ($data['schedule_type'] == $typeId) {
234 $scheduleValue->setValue($data['schedule_value']);
235 }
236 $option->addSubItem($scheduleValue);
237 }
238 }
239
240 $form->addItem($type);
241 }
242
243 if ($job->hasCustomSettings()) {
244 $job->addCustomSettingsToForm($form);
245 }
246
247 $form->addCommandButton("update", $this->lng->txt("save"));
248 $form->addCommandButton("render", $this->lng->txt("cancel"));
249
250 return $form;
251 }
252
253 public function update()
254 {
255 if (!$this->rbac->system()->checkAccess('write', SYSTEM_FOLDER_ID)) {
256 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
257 }
258
259 $id = $_REQUEST["jid"];
260 if (!$id) {
261 $this->ctrl->redirect($this, "render");
262 }
263
264 $form = $this->initEditForm($id);
265 if ($form->checkInput()) {
267 if ($job) {
268 $valid = true;
269 if ($job->hasCustomSettings() &&
270 !$job->saveCustomSettings($form)) {
271 $valid = false;
272 }
273
274 if ($valid && $job->hasFlexibleSchedule()) {
275 $type = $form->getInput("type");
276 switch (true) {
277 case $this->hasScheduleValue($type):
278 $value = $form->getInput($this->getScheduleValueFormElementName($type));
279 break;
280
281 default:
282 $value = null;
283 break;
284 }
285
287 }
288 if ($valid) {
289 ilUtil::sendSuccess($this->lng->txt("cron_action_edit_success"), true);
290 $this->ctrl->redirect($this, "render");
291 }
292 }
293 }
294
295 $form->setValuesByPost();
296 $this->edit($form);
297 }
298
299 public function run()
300 {
301 $this->confirm("run");
302 }
303
304 public function confirmedRun()
305 {
306 if (!$this->rbac->system()->checkAccess('write', SYSTEM_FOLDER_ID)) {
307 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
308 }
309
310 $job_id = $_GET["jid"];
311 if ($job_id) {
312 if (ilCronManager::runJobManual($job_id)) {
313 ilUtil::sendSuccess($this->lng->txt("cron_action_run_success"), true);
314 } else {
315 ilUtil::sendFailure($this->lng->txt("cron_action_run_fail"), true);
316 }
317 }
318
319 $this->ctrl->redirect($this, "render");
320 }
321
322 public function activate()
323 {
324 $this->confirm("activate");
325 }
326
327 public function confirmedActivate()
328 {
329 if (!$this->rbac->system()->checkAccess('write', SYSTEM_FOLDER_ID)) {
330 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
331 }
332
333 $jobs = $this->getMultiActionData();
334 if ($jobs) {
335 foreach ($jobs as $job) {
336 if (ilCronManager::isJobInactive($job->getId())) {
338 ilCronManager::activateJob($job, true);
339 }
340 }
341
342 ilUtil::sendSuccess($this->lng->txt("cron_action_activate_success"), true);
343 }
344
345 $this->ctrl->redirect($this, "render");
346 }
347
348 public function deactivate()
349 {
350 $this->confirm("deactivate");
351 }
352
353 public function confirmedDeactivate()
354 {
355 if (!$this->rbac->system()->checkAccess('write', SYSTEM_FOLDER_ID)) {
356 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
357 }
358
359 $jobs = $this->getMultiActionData();
360 if ($jobs) {
361 foreach ($jobs as $job) {
362 if (ilCronManager::isJobActive($job->getId())) {
364 }
365 }
366
367 ilUtil::sendSuccess($this->lng->txt("cron_action_deactivate_success"), true);
368 }
369
370 $this->ctrl->redirect($this, "render");
371 }
372
373 public function reset()
374 {
375 $this->confirm("reset");
376 }
377
378 public function confirmedReset()
379 {
380 if (!$this->rbac->system()->checkAccess('write', SYSTEM_FOLDER_ID)) {
381 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
382 }
383
384 $jobs = $this->getMultiActionData();
385 if ($jobs) {
386 foreach ($jobs as $job) {
388 }
389 ilUtil::sendSuccess($this->lng->txt("cron_action_reset_success"), true);
390 }
391
392 $this->ctrl->redirect($this, "render");
393 }
394
395 protected function getMultiActionData()
396 {
397 $res = array();
398
399 if ($_REQUEST["jid"]) {
400 $job_id = trim($_REQUEST["jid"]);
401 $job = ilCronManager::getJobInstanceById($job_id);
402 if ($job) {
403 $res[$job_id] = $job;
404 }
405 } elseif (is_array($_REQUEST["mjid"])) {
406 foreach ($_REQUEST["mjid"] as $job_id) {
407 $job = ilCronManager::getJobInstanceById($job_id);
408 if ($job) {
409 $res[$job_id] = $job;
410 }
411 }
412 }
413
414 return $res;
415 }
416
417 protected function confirm($a_action)
418 {
419 if (!$this->rbac->system()->checkAccess('write', SYSTEM_FOLDER_ID)) {
420 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
421 }
422
423 $jobs = $this->getMultiActionData();
424 if (!$jobs) {
425 $this->ctrl->redirect($this, "render");
426 }
427
428 if ('run' == $a_action) {
429 // Filter jobs which are not indented to be executed manually
430 $jobs = array_filter($jobs, function ($job) {
434 return $job->isManuallyExecutable();
435 });
436
437 if (0 == count($jobs)) {
438 ilUtil::sendFailure($this->lng->txt('cron_no_executable_job_selected'), true);
439 $this->ctrl->redirect($this, 'render');
440 }
441 }
442
443 include_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
444 $cgui = new ilConfirmationGUI();
445
446 if (sizeof($jobs) == 1) {
447 $job_id = array_pop(array_keys($jobs));
448 $job = array_pop($jobs);
449 $title = $job->getTitle();
450 if (!$title) {
451 $title = preg_replace("[^A-Za-z0-9_\-]", "", $job->getId());
452 }
453
454 $cgui->setHeaderText(sprintf(
455 $this->lng->txt("cron_action_" . $a_action . "_sure"),
456 $title
457 ));
458
459 $this->ctrl->setParameter($this, "jid", $job_id);
460 } else {
461 $cgui->setHeaderText($this->lng->txt("cron_action_" . $a_action . "_sure_multi"));
462
463 foreach ($jobs as $job_id => $job) {
464 $cgui->addItem("mjid[]", $job_id, $job->getTitle());
465 }
466 }
467
468 $cgui->setFormAction($this->ctrl->getFormAction($this, "confirmed" . ucfirst($a_action)));
469 $cgui->setCancel($this->lng->txt("cancel"), "render");
470 $cgui->setConfirm($this->lng->txt("cron_action_" . $a_action), "confirmed" . ucfirst($a_action));
471
472 $this->tpl->setContent($cgui->getHTML());
473 }
474
475 public function addToExternalSettingsForm($a_form_id)
476 {
477 $fields = array();
478
480 foreach ($data as $item) {
482 $item["job_id"],
483 $item["component"],
484 $item["class"],
485 $item["path"]
486 );
487
488 if (method_exists($job, "addToExternalSettingsForm")) {
489 $job->addToExternalSettingsForm($a_form_id, $fields, $item["job_status"]);
490 }
491 }
492
493 if (sizeof($fields)) {
494 return array("cron_jobs" => array("jumpToCronJobs", $fields));
495 }
496 }
497}
$_GET["client_id"]
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_UNIX
error($a_errmsg)
set error message @access public
Confirmation screen class.
const SCHEDULE_TYPE_IN_DAYS
const SCHEDULE_TYPE_IN_HOURS
const SCHEDULE_TYPE_IN_MINUTES
const SCHEDULE_TYPE_WEEKLY
const SCHEDULE_TYPE_YEARLY
const SCHEDULE_TYPE_DAILY
const SCHEDULE_TYPE_QUARTERLY
const SCHEDULE_TYPE_MONTHLY
Class ilCronManagerGUI.
addToExternalSettingsForm($a_form_id)
getScheduleTypeFormElementName(int $scheduleTypeId)
edit(ilPropertyFormGUI $a_form=null)
getScheduleValueFormElementName(int $scheduleTypeId)
__construct()
ilCronManagerGUI constructor.
hasScheduleValue(int $scheduleTypeId)
List all active cron jobs.
static getCronJobData($a_id=null, $a_include_inactive=true)
Get cron job configuration/execution data.
static isJobInactive($a_job_id)
Check if given job is currently inactive.
static getJobInstanceById($a_job_id)
Get job instance (by job id)
static runJobManual($a_job_id)
Run single job manually.
static activateJob(ilCronJob $a_job, $a_manual=false)
Activate cron job.
static getJobInstance($a_id, $a_component, $a_class, $a_path=null)
Get job instance (by job data)
static isJobActive($a_job_id)
Check if given job is currently active.
static deactivateJob(ilCronJob $a_job, $a_manual=false)
Deactivate cron job.
static resetJob(ilCronJob $a_job)
Reset job.
static updateJobSchedule(ilCronJob $a_job, $a_schedule_type, $a_schedule_value)
Update job schedule.
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date @access public.
@classDescription Date and time handling
This class represents a number property in a property form.
This class represents a property form user interface.
This class represents a property in a property form.
This class represents an option in a radio group.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
$valid
$type
foreach($_POST as $key=> $value) $res
settings()
Definition: settings.php:2
$data
Definition: storeScorm.php:23
$DIC
Definition: xapitoken.php:46