From 20735c3cf1e32419292e39bd7a44540520663b36 Mon Sep 17 00:00:00 2001
From: Maria Torres <maria.torres@totaralms.com>
Date: Fri, 4 Apr 2014 14:12:58 +1300
Subject: [PATCH] T-9800 reportbuilder: Move include_js for filters into the
 filter classes

Change-Id: I4e9aab59dad8cd9a8adce289d3e12088f8aa36fc
---
 totara/reportbuilder/filters/cohort.php          | 27 +++++++++
 totara/reportbuilder/filters/hierarchy.php       | 33 ++++++++++-
 totara/reportbuilder/filters/hierarchy_multi.php | 28 +++++++++
 totara/reportbuilder/lib.php                     | 72 ++++++------------------
 4 files changed, 105 insertions(+), 55 deletions(-)

diff --git a/totara/reportbuilder/filters/cohort.php b/totara/reportbuilder/filters/cohort.php
index a713619..31ef1cf 100644
--- a/totara/reportbuilder/filters/cohort.php
+++ b/totara/reportbuilder/filters/cohort.php
@@ -195,6 +195,33 @@ class rb_filter_cohort extends rb_filter_type {
 
         return get_string('selectlabelnoop', 'filters', $a);
     }
+
+    /**
+     * Include Js for this filter
+     *
+     */
+    public function include_js() {
+        global $PAGE;
+
+        $code = array();
+        $code[] = TOTARA_JS_DIALOG;
+        $code[] = TOTARA_JS_TREEVIEW;
+        local_js($code);
+
+        $jsdetails = new stdClass();
+        $jsdetails->initcall = 'M.totara_reportbuilder_filterdialogs.init';
+        $jsdetails->jsmodule = array('name' => 'totara_reportbuilder_filterdialogs',
+            'fullpath' => '/totara/reportbuilder/filter_dialogs.js');
+        $jsdetails->strings = array(
+            'totara_cohort' => array('choosecohorts')
+        );
+
+        foreach ($jsdetails->strings as $scomponent => $sstrings) {
+            $PAGE->requires->strings_for_js($sstrings, $scomponent);
+        }
+
+        $PAGE->requires->js_init_call($jsdetails->initcall, null, false, $jsdetails->jsmodule);
+    }
 }
 
 /**
diff --git a/totara/reportbuilder/filters/hierarchy.php b/totara/reportbuilder/filters/hierarchy.php
index 7b39b81..1c79280 100644
--- a/totara/reportbuilder/filters/hierarchy.php
+++ b/totara/reportbuilder/filters/hierarchy.php
@@ -238,5 +238,36 @@ class rb_filter_hierarchy extends rb_filter_type {
 
         return get_string('selectlabel', 'filters', $a);
     }
-}
 
+    /**
+     * Include Js for this filter
+     *
+     */
+    public function include_js() {
+        global $PAGE;
+
+        $code = array();
+        $code[] = TOTARA_JS_DIALOG;
+        $code[] = TOTARA_JS_TREEVIEW;
+        local_js($code);
+
+        $jsdetails = new stdClass();
+        $jsdetails->initcall = 'M.totara_reportbuilder_filterdialogs.init';
+        $jsdetails->jsmodule = array('name' => 'totara_reportbuilder_filterdialogs',
+            'fullpath' => '/totara/reportbuilder/filter_dialogs.js');
+        $jsdetails->strings = array(
+            'totara_hierarchy' => array('chooseposition', 'selected', 'chooseorganisation', 'currentlyselected', 'selectcompetency'),
+            'totara_reportbuilder' => array('chooseorgplural', 'chooseposplural', 'choosecompplural'),
+        );
+        $title = $this->type . '-' . $this->value;
+        $currentlyselected = json_encode(dialog_display_currently_selected(get_string('currentlyselected', 'totara_hierarchy'), $title));
+        $carg = "\"{$title}-currentlyselected\":{$currentlyselected}";
+        $jsdetails->args = array('args' => '{' . $carg . '}');
+
+        foreach ($jsdetails->strings as $scomponent => $sstrings) {
+            $PAGE->requires->strings_for_js($sstrings, $scomponent);
+        }
+
+        $PAGE->requires->js_init_call($jsdetails->initcall, $jsdetails->args, false, $jsdetails->jsmodule);
+    }
+}
diff --git a/totara/reportbuilder/filters/hierarchy_multi.php b/totara/reportbuilder/filters/hierarchy_multi.php
index 92d52da..a525a76 100644
--- a/totara/reportbuilder/filters/hierarchy_multi.php
+++ b/totara/reportbuilder/filters/hierarchy_multi.php
@@ -186,6 +186,34 @@ class rb_filter_hierarchy_multi extends rb_filter_type {
 
         return get_string('selectlabelnoop', 'filters', $a);
     }
+
+    /**
+     * Include Js for this filter
+     *
+     */
+    public function include_js() {
+        global $PAGE;
+
+        $code = array();
+        $code[] = TOTARA_JS_DIALOG;
+        $code[] = TOTARA_JS_TREEVIEW;
+        local_js($code);
+
+        $jsdetails = new stdClass();
+        $jsdetails->initcall = 'M.totara_reportbuilder_filterdialogs.init';
+        $jsdetails->jsmodule = array('name' => 'totara_reportbuilder_filterdialogs',
+            'fullpath' => '/totara/reportbuilder/filter_dialogs.js');
+        $jsdetails->strings = array(
+            'totara_hierarchy' => array('chooseposition', 'selected', 'chooseorganisation', 'currentlyselected', 'selectcompetency'),
+            'totara_reportbuilder' => array('chooseorgplural', 'chooseposplural', 'choosecompplural')
+        );
+
+        foreach ($jsdetails->strings as $scomponent => $sstrings) {
+            $PAGE->requires->strings_for_js($sstrings, $scomponent);
+        }
+
+        $PAGE->requires->js_init_call($jsdetails->initcall, null, false, $jsdetails->jsmodule);
+    }
 }
 
 
diff --git a/totara/reportbuilder/lib.php b/totara/reportbuilder/lib.php
index 3156518..a20a06a 100644
--- a/totara/reportbuilder/lib.php
+++ b/totara/reportbuilder/lib.php
@@ -356,19 +356,18 @@ class reportbuilder {
      * Include javascript code needed by report builder
      */
     function include_js() {
-        global $CFG, $PAGE, $SESSION;
+        global $CFG, $PAGE;
 
-        $dialog = false;
-        $treeview = false;
+        // Array of options for local_js
+        $code = array();
 
         // Get any required js files that are specified by the source.
         $js = $this->src->get_required_jss();
 
         // Only include show/hide code for tabular reports.
-        $graph = (substr($this->source, 0,
-            strlen('graphical_feedback_questions')) ==
-            'graphical_feedback_questions');
+        $graph = (substr($this->source, 0, strlen('graphical_feedback_questions')) == 'graphical_feedback_questions');
         if (!$graph) {
+            $code[] = TOTARA_JS_DIALOG;
             $jsdetails = new stdClass();
             $jsdetails->initcall = 'M.totara_reportbuilder_showhide.init';
             $jsdetails->jsmodule = array('name' => 'totara_reportbuilder_showhide',
@@ -390,56 +389,8 @@ class reportbuilder {
                 'form' => array('close')
             );
             $js[] = $jsdetails;
-
-            $dialog = true;
         }
 
-        // include JS for dialogs if required for filters
-        foreach ($this->filters as $filter) {
-            if (in_array($filter->filtertype, array('hierarchy', 'hierarchy_multi', 'cohort'))) {
-                $jsdetails = new stdClass();
-                $jsdetails->initcall = 'M.totara_reportbuilder_filterdialogs.init';
-                $jsdetails->jsmodule = array('name' => 'totara_reportbuilder_filterdialogs',
-                    'fullpath' => '/totara/reportbuilder/filter_dialogs.js');
-                $jsdetails->strings = array(
-                    'totara_hierarchy' => array('chooseposition', 'selected', 'chooseorganisation', 'currentlyselected', 'selectcompetency'),
-                    'totara_reportbuilder' => array('chooseorgplural', 'chooseposplural', 'choosecompplural'),
-                    'totara_cohort' => array('choosecohorts')
-                );
-
-                // add currently selected as args
-                $cargs = array();
-                foreach ($this->filters as $f) {
-                    if ($f->filtertype != 'hierarchy') {
-                        continue;
-                    }
-                    $title = $f->type . '-' . $f->value;
-                    $currentlyselected = json_encode(dialog_display_currently_selected(
-                        get_string('currentlyselected', 'totara_hierarchy'), $title));
-                    $cargs[] = "\"{$title}-currentlyselected\":{$currentlyselected}";
-                }
-                if (!empty($cargs)) {
-                    $cargs = implode(', ', $cargs);
-                    $jsdetails->args = array('args' => '{' . $cargs . '}');
-                }
-
-                $js[] = $jsdetails;
-
-                $dialog = $treeview = true;
-                break;
-            }
-        }
-
-
-        $code = array();
-        if ($dialog) {
-            $code[] = TOTARA_JS_DIALOG;
-        }
-        if ($treeview) {
-            $code[] = TOTARA_JS_TREEVIEW;
-        }
-
-
         local_js($code);
         foreach ($js as $jsdetails) {
             if (!empty($jsdetails->strings)) {
@@ -453,6 +404,19 @@ class reportbuilder {
                 false, $jsdetails->jsmodule);
         }
 
+
+        // Load Js for these filters.
+        foreach ($this->filters as $filter) {
+            $classname = get_class($filter);
+            $filtertype = $filter->filtertype;
+            $filterpath = $CFG->dirroot.'/totara/reportbuilder/filters/'.$filtertype.'.php';
+            if (file_exists($filterpath)) {
+                require_once $filterpath;
+                if (method_exists($classname, 'include_js')) {
+                    call_user_func(array($filter, 'include_js'));
+                }
+            }
+        }
     }
 
 
-- 
1.8.3.2

