Skip to content

Commit 4e15c09

Browse files
committed
Improve retrieval and displaying of next run and last run in course selection row
1 parent bb366ac commit 4e15c09

File tree

5 files changed

+114
-68
lines changed

5 files changed

+114
-68
lines changed

classes/local/manager/workflow_manager.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
*/
2424
namespace tool_lifecycle\local\manager;
2525

26+
use core\task\manager;
27+
use core_date;
2628
use tool_lifecycle\action;
2729
use tool_lifecycle\local\backup\backup_lifecycle_workflow;
2830
use tool_lifecycle\local\data\manual_trigger_tool;
2931
use tool_lifecycle\local\entity\trigger_subplugin;
3032
use tool_lifecycle\local\entity\workflow;
33+
use tool_lifecycle\local\response\trigger_response;
3134
use tool_lifecycle\settings_type;
3235

3336
/**
@@ -568,4 +571,91 @@ public static function is_deprecated($workflowid) {
568571
return false;
569572
}
570573

574+
/**
575+
* Tries to retrieve the last time and to predict the next time the trigger selection took/takes place.
576+
*
577+
* @param int $workflowid Id of the workflow.
578+
* @return array lastrun, nextrun
579+
* @throws \coding_exception
580+
* @throws \dml_exception
581+
*/
582+
public static function get_lastrun_nextrun($workflowid) {
583+
global $USER;
584+
585+
$lastrun = 0;
586+
$nextrun = false;
587+
588+
// Fetch and evaluate triggers of type triggertime, if there are any.
589+
$triggers = trigger_manager::get_triggers_for_workflow($workflowid);
590+
foreach ($triggers as $trigger) {
591+
$lib = lib_manager::get_trigger_lib($trigger->subpluginname);
592+
$settings = settings_manager::get_settings($trigger->id, settings_type::TRIGGER);
593+
if (!$lib->is_manual_trigger()) {
594+
$response = $lib->default_response();
595+
if ($response == trigger_response::triggertime()) {
596+
if ($nextrunt = $lib->get_next_run_time($trigger->id)) {
597+
if ($nextrun) {
598+
$nextrun = min($nextrun, $nextrunt);
599+
} else {
600+
$nextrun = $nextrunt;
601+
}
602+
}
603+
if ($lastrunt = $settings['timelastrun'] ?? 0) {
604+
$lastrun = max($lastrun, $lastrunt);
605+
}
606+
}
607+
}
608+
}
609+
610+
611+
// If we couldn't get nextrun or lastrun so far, we analyze the lifecycle task.
612+
if (!($nextrun && $lastrun)) {
613+
$task = manager::get_scheduled_task('tool_lifecycle\task\lifecycle_task');
614+
if (!$lastrun) {
615+
$lastrun = $task->get_last_run_time();
616+
if (is_numeric($lastrun)) {
617+
$lastrun = userdate($lastrun,
618+
get_string('strftimedatetimeshort', 'langconfig'),
619+
core_date::get_user_timezone($USER))." (Task run)";
620+
}
621+
}
622+
if (!$nextrun) {
623+
$nextrun = $task->get_next_run_time();
624+
if (!$task->is_component_enabled() && !$task->get_run_if_component_disabled()) {
625+
$nextrun = get_string('plugindisabled', 'tool_task');
626+
} else if ($task->get_disabled()) {
627+
$nextrun = get_string('taskdisabled', 'tool_task');
628+
} else if (!is_numeric($nextrun)) {
629+
$nextrun = get_string('asap', 'tool_task');
630+
} else if (is_numeric($nextrun)) {
631+
$nextrun = userdate($nextrun,
632+
get_string('strftimedatetimeshort', 'langconfig'),
633+
core_date::get_user_timezone($USER))." (Task scheduled)";
634+
}
635+
}
636+
}
637+
638+
// Convert timestamps to human readable dates.
639+
if ($lastrun) {
640+
// Only display lastrun if workflow has been activated before.
641+
$isactive = self::is_active($workflowid);
642+
$isdeactivated = self::is_deactivated($workflowid);
643+
if (!($isactive || $isdeactivated)) {
644+
$lastrun = '--';
645+
} else if (is_numeric($lastrun)) {
646+
$lastrun = userdate($lastrun,
647+
get_string('strftimedatetimeshort', 'langconfig'),
648+
core_date::get_user_timezone($USER));
649+
}
650+
}
651+
if ($nextrun) {
652+
if (is_numeric($nextrun)) {
653+
$nextrun = userdate($nextrun,
654+
get_string('strftimedatetimeshort', 'langconfig'),
655+
core_date::get_user_timezone($USER));
656+
}
657+
}
658+
659+
return [$lastrun, $nextrun];
660+
}
571661
}

classes/processor.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -574,15 +574,14 @@ public function get_count_of_courses_to_trigger_for_workflow($workflow) {
574574
$autotriggers[] = $trigger;
575575
} else if ($obj->response == trigger_response::triggertime()) {
576576
if ($nextrun = $lib->get_next_run_time($trigger->id)) {
577-
if ($obj->lastrun = $settings['timelastrun'] ?? 0) {
578-
$obj->additionalinfo = get_string('lastrun', 'tool_lifecycle',
579-
userdate($settings['timelastrun'], get_string('strftimedatetimeshort', 'langconfig'),
580-
core_date::get_user_timezone($USER)));
581-
} else {
582-
$obj->additionalinfo = "--";
583-
}
584-
} else {
585-
$obj->additionalinfo = "--";
577+
$obj->nextrun = userdate($nextrun,
578+
get_string('strftimedatetimeshort', 'langconfig'),
579+
core_date::get_user_timezone($USER));
580+
}
581+
if ($lastrun = $settings['timelastrun'] ?? 0) {
582+
$obj->lastrun = userdate($lastrun,
583+
get_string('strftimedatetimeshort', 'langconfig'),
584+
core_date::get_user_timezone($USER));
586585
}
587586
$obj->sql = "---";
588587
$autotriggers[] = $trigger;

templates/overview_timetrigger.mustache

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@
4141
{{{ actionmenu }}}
4242
</div>
4343
</div>
44-
{{#showcoursecounts}}
45-
<div class="wf-content">
46-
{{{additionalinfo}}}
47-
</div>
48-
{{/showcoursecounts}}
44+
<div class="mx-2">
45+
--
46+
</div>
4947
</div>
5048
{{/timetrigger}}

templates/workflowoverview.mustache

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,21 +163,21 @@
163163
<br>
164164
<span class="text-muted">{{#str}} lastrun, tool_lifecycle, {{lastrun}} {{/str}}</span>
165165
</div>
166-
{{/nomanualtriggerinvolved}}
167-
{{^nomanualtriggerinvolved}}
168-
<div class="mx-2">
169-
{{#str}} manualtriggerenvolved, tool_lifecycle {{/str}} <a href="#" data-toggle="tooltip" title="{{#str}} manualtriggerenvolved_help, tool_lifecycle{{/str}}"><i class="fa fa-circle-question"></i></a>
170-
</div>
171-
{{/nomanualtriggerinvolved}}
172166
{{#runnable}}
173167
<div class="mb-2">
174168
<a href="{{{runlink}}}">
175-
<span data-toggle="tooltip" title="{{#str}} runtask, tool_lifecycle {{/str}}">
176-
{{#str}} run, tool_lifecycle {{/str}}
177-
</span>
169+
<span data-toggle="tooltip" title="{{#str}} runtask, tool_lifecycle {{/str}}">
170+
{{#str}} run, tool_lifecycle {{/str}}
171+
</span>
178172
</a>
179173
</div>
180174
{{/runnable}}
175+
{{/nomanualtriggerinvolved}}
176+
{{^nomanualtriggerinvolved}}
177+
<div class="mx-2">
178+
{{#str}} manualtriggerenvolved, tool_lifecycle {{/str}} <a href="#" data-toggle="tooltip" title="{{#str}} manualtriggerenvolved_help, tool_lifecycle{{/str}}"><i class="fa fa-circle-question"></i></a>
179+
</div>
180+
{{/nomanualtriggerinvolved}}
181181

182182
{{#counttimetriggers}}
183183
<div class="workflow wf-trigger-wrapper">

workflowoverview.php

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
use core\output\single_button;
3636
use core\task\manager;
3737
use tool_lifecycle\action;
38-
use tool_lifecycle\event\process_rollback;
3938
use tool_lifecycle\event\process_triggered;
4039
use tool_lifecycle\local\manager\delayed_courses_manager;
4140
use tool_lifecycle\local\manager\lib_manager;
@@ -227,7 +226,6 @@
227226
'move_down' => get_string('move_down', 'tool_lifecycle'),
228227
];
229228

230-
$nextrun = false;
231229
$coursestriggered = 0;
232230
$coursesdelayed = 0;
233231
$hasotherwf = 0;
@@ -242,39 +240,9 @@
242240
$coursestriggered = $amounts['all']->coursestriggered ?? 0;
243241
$coursesdelayed = $amounts['all']->delayedcourses ?? 0;
244242
$hasotherwf = $amounts['all']->hasotherwf ?? 0;
245-
$nextrun = $amounts['all']->nextrun == 0 ? false : $amounts['all']->nextrun;
246243
$displaytotaltriggered = !empty($triggers);
247244
}
248245

249-
$task = manager::get_scheduled_task('tool_lifecycle\task\lifecycle_task');
250-
$lastrun = $task->get_last_run_time();
251-
$nextrunt = $task->get_next_run_time();
252-
$nextrunout = "";
253-
if (!$task->is_component_enabled() && !$task->get_run_if_component_disabled()) {
254-
$nextrunt = get_string('plugindisabled', 'tool_task');
255-
} else if ($task->get_disabled()) {
256-
$nextrunt = get_string('taskdisabled', 'tool_task');
257-
} else if (is_numeric($nextrunt) && $nextrunt < time()) {
258-
$nextrunt = get_string('asap', 'tool_task');
259-
}
260-
if (is_numeric($nextrunt) && is_numeric($nextrun)) { // Task nextrun and trigger nextrun are valid times: take the minimum.
261-
$nextrunout = min($nextrunt, $nextrun);
262-
} else if (!is_numeric($nextrunt) && is_numeric($nextrun)) { // Only trigger nextrun is valid time.
263-
$nextrun = $nextrun;
264-
} else if (is_numeric($nextrunt)) { // Only task next run is valid time.
265-
$nextrunout = $nextrunt;
266-
} else { // There is no valid next run time. Print the task message.
267-
$nextrunout = $nextrunt;
268-
}
269-
if (is_numeric($nextrunout)) {
270-
if ($nextrunout) {
271-
$nextrunout = userdate($nextrunout, get_string('strftimedatetimeshort', 'langconfig'),
272-
core_date::get_user_timezone($USER));
273-
} else {
274-
$nextrunout = get_string('statusunknown');
275-
}
276-
}
277-
278246
$nomanualtriggerinvolved = true;
279247
$displaytriggers = [];
280248
$displaytimetriggers = [];
@@ -309,7 +277,6 @@
309277
$sqlresult = trigger_manager::get_trigger_sqlresult($trigger);
310278
if ($sqlresult == "false") {
311279
$trigger->classfires = "border-danger";
312-
$trigger->additionalinfo = $amounts[$trigger->sortindex]->additionalinfo ?? "-";
313280
} else {
314281
$settings = settings_manager::get_settings($trigger->id, settings_type::TRIGGER);
315282
$trigger->exclude = $settings['exclude'] ?? false;
@@ -344,11 +311,6 @@
344311
}
345312
if ($response == trigger_response::triggertime()) {
346313
$displaytimetriggers[] = $trigger;
347-
if (isset($amounts[$trigger->sortindex]->lastrun) && $amounts[$trigger->sortindex]->lastrun) {
348-
$lastrun = $amounts[$trigger->sortindex]->lastrun;
349-
} else {
350-
$lastrun = 0;
351-
}
352314
} else {
353315
$displaytriggers[] = $trigger;
354316
}
@@ -538,9 +500,8 @@
538500
}
539501
}
540502

541-
if (!($isactive || $isdeactivated)) {
542-
$lastrun = 0;
543-
}
503+
list($lastrun, $nextrun) = workflow_manager::get_lastrun_nextrun($workflow->id);
504+
544505
$data = [
545506
'editsettingslink' => (new moodle_url(urls::EDIT_WORKFLOW, ['wf' => $workflow->id]))->out(false),
546507
'title' => $workflow->title,
@@ -565,10 +526,8 @@
565526
'showdetailslink' => $showdetailslink,
566527
'showdetailsicon' => $showdetails == 0,
567528
'isactive' => $isactive || $isdeactivated,
568-
'nextrun' => $nextrunout,
569-
'lastrun' => $lastrun != 0 ?
570-
userdate($lastrun, get_string('strftimedatetimeshort', 'langconfig'),
571-
core_date::get_user_timezone($USER)) : '--',
529+
'nextrun' => $nextrun,
530+
'lastrun' => $lastrun,
572531
'nomanualtriggerinvolved' => $nomanualtriggerinvolved,
573532
'disableworkflowlink' => $disableworkflowlink,
574533
'abortdisableworkflowlink' => $abortdisableworkflowlink,

0 commit comments

Comments
 (0)