From 053d163fe650b1b4e47130a4189595173e4cb471 Mon Sep 17 00:00:00 2001
From: Philipp Memmel <philipp.memmel@gmail.com>
Date: Tue, 31 Jan 2023 22:51:05 +0100
Subject: [PATCH] MBS-7377: Fix workflowoverview for big instances

---
 lang/de/tool_lifecycle.php          |  4 ++++
 lang/en/tool_lifecycle.php          |  4 ++++
 settings.php                        |  5 +++++
 templates/workflowoverview.mustache | 14 ++++++++-----
 workflowoverview.php                | 32 +++++++++++++++++++----------
 5 files changed, 43 insertions(+), 16 deletions(-)

diff --git a/lang/de/tool_lifecycle.php b/lang/de/tool_lifecycle.php
index 986f7b4..e7a484f 100644
--- a/lang/de/tool_lifecycle.php
+++ b/lang/de/tool_lifecycle.php
@@ -34,6 +34,10 @@ $string['config_delay_duration'] = 'Standardlänge eines Kursausschlusses';
 $string['config_delay_duration_desc'] = 'Diese Einstellung definiert den Standardlänge einer Kursausschlusses in einem Workflow
  falls ein Prozess des Workflows zurückgesetzt oder beendigt wird. Die Länge des Kursausschlusses besagt, wie lange es dauert, bis
  der Kurs wieder vom Workflow bearbeitet wird.';
+$string['config_showcoursecounts'] = 'Zeige Anzahl der Kurse, die getriggert werden';
+$string['config_showcoursecounts_desc'] = 'Die Workflow-Konfigurationsseite zeigt normalerweise die Anzahl an Kursen, die durch
+die konfigurierten Trigger getriggert werden, was Performance-Probleme verursachen kann. Bei Performance-Problemen kann dies hiermit
+deaktiviert werden';
 $string['find_course_list_header'] = 'Kurse finden';
 $string['adminsettings_heading'] = 'Workflow-Einstellungen';
 $string['active_manual_workflows_heading'] = 'Aktive manuelle Workflows';
diff --git a/lang/en/tool_lifecycle.php b/lang/en/tool_lifecycle.php
index 9d0bb75..c88413f 100644
--- a/lang/en/tool_lifecycle.php
+++ b/lang/en/tool_lifecycle.php
@@ -38,6 +38,10 @@ The delay duration determines how long a course will be excepted from being proc
 $string['config_backup_path'] = 'Path of the lifecycle backup folder';
 $string['config_backup_path_desc'] = 'This settings defines the storage location of the backups created by the backup step.
 The path has to be specified as an absolute path on your server.';
+$string['config_showcoursecounts'] = 'Show amount of courses which will be triggered';
+$string['config_showcoursecounts_desc'] = 'The workflow overview page by default shows the amount of courses which will be
+triggered by the configured triggers which can be load heavy. Disable this option if you experience issues loading the workflow
+overview.';
 $string['find_course_list_header'] = 'Find courses';
 $string['adminsettings_heading'] = 'Workflow settings';
 $string['active_manual_workflows_heading'] = 'Active manual workflows';
diff --git a/settings.php b/settings.php
index f1d3791..3f37c08 100644
--- a/settings.php
+++ b/settings.php
@@ -42,6 +42,11 @@ if ($hassiteconfig) {
         get_string('config_backup_path_desc', 'tool_lifecycle'),
         $CFG->dataroot . DIRECTORY_SEPARATOR . 'lifecycle_backups'));
 
+    $settings->add(new admin_setting_configcheckbox('tool_lifecycle/showcoursecounts',
+        get_string('config_showcoursecounts', 'tool_lifecycle'),
+        get_string('config_showcoursecounts_desc', 'tool_lifecycle'),
+        1));
+
     $ADMIN->add('lifecycle_category', new admin_externalpage('tool_lifecycle_workflow_drafts',
         get_string('workflow_drafts_header', 'tool_lifecycle'),
         new moodle_url(\tool_lifecycle\urls::WORKFLOW_DRAFTS)));
diff --git a/templates/workflowoverview.mustache b/templates/workflowoverview.mustache
index 70a0104..70939b4 100644
--- a/templates/workflowoverview.mustache
+++ b/templates/workflowoverview.mustache
@@ -84,12 +84,14 @@
         <div class="mt-5 mb-3">{{{addinstance}}}</div>
         <div class="wf-trigger-block workflow-item">
             <h5 class="my-2">{{#str}} trigger, tool_lifecycle{{/str}} {{{triggerhelp}}}</h5>
-            {{#automatic}}
+            {{#showcoursecounts}}
+              {{#automatic}}
                 <div class="mb-2 mx-2">
                     {{#str}} courses_will_be_triggered_total, tool_lifecycle, {{coursestriggered}} {{/str}}<br>
                     {{#str}} courses_will_be_excluded_total, tool_lifecycle, {{coursesexcluded}} {{/str}}<br>
                 </div>
-            {{/automatic}}
+              {{/automatic}}
+            {{/showcoursecounts}}
             <div class="workflow wf-trigger-wrapper">
                 {{#trigger}}
                     <div class="workflow-trigger">
@@ -103,10 +105,12 @@
                             </div>
                         </div>
                         <div class="wf-content">
+                          {{#showcoursecounts}}
                             {{#automatic}}
-                                <span>{{#str}} courses_will_be_triggered, tool_lifecycle, {{triggeredcourses}} {{/str}}</span><br>
-                                <span>{{#str}} courses_will_be_excluded, tool_lifecycle, {{excludedcourses}} {{/str}}</span>
+                              <span>{{#str}} courses_will_be_triggered, tool_lifecycle, {{triggeredcourses}} {{/str}}</span><br>
+                              <span>{{#str}} courses_will_be_excluded, tool_lifecycle, {{excludedcourses}} {{/str}}</span>
                             {{/automatic}}
+                          {{/showcoursecounts}}
                         </div>
                     </div>
                 {{/trigger}}
@@ -140,4 +144,4 @@
         {{{ table }}}
     </div>
 {{/table}}
-</div>
\ No newline at end of file
+</div>
diff --git a/workflowoverview.php b/workflowoverview.php
index c422992..f00f2e3 100644
--- a/workflowoverview.php
+++ b/workflowoverview.php
@@ -87,8 +87,15 @@ $str = [
     'move_down' => get_string('move_down', 'tool_lifecycle')
 ];
 
-$amounts = (new \tool_lifecycle\processor())->get_count_of_courses_to_trigger_for_workflow($workflow->id);
-$displaytotaltriggered = !empty($triggers);
+$showcoursecounts = get_config('tool_lifecycle', 'showcoursecounts');
+if ($showcoursecounts) {
+    // On moodle instances with many courses the following call can be fatal, because each trigger
+    // check function will be called for every single course of the instance to determine how many
+    // courses will be triggered by the workflow/the specific trigger. This count is only being
+    // used to show the admin how many courses will be triggered, it has no functional aspect.
+    $amounts = (new \tool_lifecycle\processor())->get_count_of_courses_to_trigger_for_workflow($workflow->id);
+    $displaytotaltriggered = !empty($triggers);
+}
 
 foreach ($triggers as $trigger) {
     // The array from the DB Function uses ids as keys.
@@ -108,11 +115,13 @@ foreach ($triggers as $trigger) {
         );
     }
     $trigger->actionmenu = $OUTPUT->render($actionmenu);
-    $trigger->automatic = $amounts[$trigger->sortindex]->automatic;
-    $displaytotaltriggered &= $trigger->automatic;
-    if ($trigger->automatic) {
-        $trigger->triggeredcourses = $amounts[$trigger->sortindex]->triggered;
-        $trigger->excludedcourses = $amounts[$trigger->sortindex]->excluded;
+    if ($showcoursecounts) {
+        $trigger->automatic = $amounts[$trigger->sortindex]->automatic;
+        $displaytotaltriggered &= $trigger->automatic;
+        if ($trigger->automatic) {
+            $trigger->triggeredcourses = $amounts[$trigger->sortindex]->triggered;
+            $trigger->excludedcourses = $amounts[$trigger->sortindex]->excluded;
+        }
     }
 }
 
@@ -176,10 +185,11 @@ $data = [
     'finishdelay' => format_time($workflow->finishdelay),
     'delayglobally' => $workflow->delayforallworkflows,
     'trigger' => array_values($triggers),
-    'automatic' => $displaytotaltriggered,
-    'coursestriggered' => $amounts['all']->triggered,
-    'coursesexcluded' => $amounts['all']->excluded,
-    'coursesetsize' => $amounts['all']->coursesetsize,
+    'showcoursecounts' => $showcoursecounts,
+    'automatic' => $showcoursecounts ?? $displaytotaltriggered,
+    'coursestriggered' => $showcoursecounts ?? $amounts['all']->triggered,
+    'coursesexcluded' => $showcoursecounts ?? $amounts['all']->excluded,
+    'coursesetsize' => $showcoursecounts ?? $amounts['all']->coursesetsize,
     'steps' => array_values($steps),
     'listofcourses' => $arrayofcourses,
     'nosteplink' => $nosteplink,
-- 
GitLab