From ae4522ff552010ffd3d18117ac1772888d5d51e8 Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Fri, 10 Nov 2017 10:43:51 +0800 Subject: [PATCH] MDL-60675 analytics: Error when selecting invalid insight --- lib/outputcomponents.php | 18 ++++++++++++++++-- lib/templates/single_select.mustache | 7 +++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index 888edfd0512..055e8da2076 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -1109,6 +1109,7 @@ class single_select implements renderable, templatable { if (is_string($this->nothing) && $this->nothing !== '') { $nothing = ['' => $this->nothing]; $hasnothing = true; + $nothingkey = ''; } else if (is_array($this->nothing)) { $nothingvalue = reset($this->nothing); if ($nothingvalue === 'choose' || $nothingvalue === 'choosedots') { @@ -1117,6 +1118,7 @@ class single_select implements renderable, templatable { $nothing = $this->nothing; } $hasnothing = true; + $nothingkey = key($this->nothing); } if ($hasnothing) { $options = $nothing + $this->options; @@ -1129,11 +1131,17 @@ class single_select implements renderable, templatable { foreach ($options[$value] as $optgroupname => $optgroupvalues) { $sublist = []; foreach ($optgroupvalues as $optvalue => $optname) { - $sublist[] = [ + $option = [ 'value' => $optvalue, 'name' => $optname, 'selected' => strval($this->selected) === strval($optvalue), ]; + + if ($hasnothing && $nothingkey == $optvalue) { + $option['ignore'] = 'data-ignore'; + } + + $sublist[] = $option; } $data->options[] = [ 'name' => $optgroupname, @@ -1142,12 +1150,18 @@ class single_select implements renderable, templatable { ]; } } else { - $data->options[] = [ + $option = [ 'value' => $value, 'name' => $options[$value], 'selected' => strval($this->selected) === strval($value), 'optgroup' => false ]; + + if ($hasnothing && $nothingkey == $value) { + $option['ignore'] = 'data-ignore'; + } + + $data->options[] = $option; } } diff --git a/lib/templates/single_select.mustache b/lib/templates/single_select.mustache index 57eb5330a85..90e936a03d6 100644 --- a/lib/templates/single_select.mustache +++ b/lib/templates/single_select.mustache @@ -105,7 +105,7 @@ {{/optgroup}} {{^optgroup}} - + {{/optgroup}} {{/options}} @@ -117,7 +117,10 @@ {{#js}} require(['jquery'], function($) { $('#{{id}}').change(function() { - $('#{{formid}}').submit(); + var ignore = $(this).find(':selected').attr('data-ignore'); + if (typeof ignore === typeof undefined) { + $('#{{formid}}').submit(); + } }); }); {{/js}}