From d7146fa44e89b40b7b6e5865857e4d2544aec72e Mon Sep 17 00:00:00 2001
From: Kathrin Osswald <kathrin.osswald@uni-ulm.de>
Date: Fri, 11 Sep 2020 08:25:58 +0200
Subject: [PATCH] Fixed bug that multiple imagearea rows are displayed
 dependent on the amount of uploaded images.

---
 CHANGES.md                                                | 3 +++
 layout/includes/imagearea.php                             | 4 ++++
 locallib.php                                              | 3 ++-
 templates/imagearea.mustache                              | 6 ++++--
 .../theme_boost_campus_additional_layout_settings.feature | 8 +++++++-
 5 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index a22086d..a3e2829 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -4,6 +4,9 @@ moodle-theme_boost_campus
 Changes
 -------
 
+### Unreleased
+* 2020-09-01 - Fixed bug that multiple imagearea rows are displayed dependent on the amount of uploaded images.
+
 ### Release v3.8-r4
 
 * 2020-08-27 - Fixed bug in image area feature if link attribute was left empty.
diff --git a/layout/includes/imagearea.php b/layout/includes/imagearea.php
index 2478973..7afe48b 100755
--- a/layout/includes/imagearea.php
+++ b/layout/includes/imagearea.php
@@ -24,6 +24,7 @@
  */
 
 defined('MOODLE_INTERNAL') || die();
+global $CFG;
 require_once($CFG->dirroot . '/theme/boost_campus/locallib.php');
 
 $imageareafiles = theme_boost_campus_get_imageareacontent();
@@ -31,4 +32,7 @@ $imageareafiles = theme_boost_campus_get_imageareacontent();
 // Only proceed if received array is not empty.
 if (!empty($imageareafiles)) {
     $templatecontext['imageareafiles'] = $imageareafiles;
+    $templatecontext['hasimagearea'] = true;
+} else {
+    $templatecontext['hasimagearea'] = false;
 }
diff --git a/locallib.php b/locallib.php
index a215e66..f74eaef 100644
--- a/locallib.php
+++ b/locallib.php
@@ -258,7 +258,8 @@ function theme_boost_campus_get_imageareacontent() {
             $themeboostcampuscache->set('imageareadata', $imageareacache);
             return $imageareacache;
         } else { // If no images are uploaded, then cache an empty array.
-            return $themeboostcampuscache->set('imageareadata', array());
+            $themeboostcampuscache->set('imageareadata', array());
+            return array();
         }
     }
 }
diff --git a/templates/imagearea.mustache b/templates/imagearea.mustache
index 4d8ad44..c008db9 100644
--- a/templates/imagearea.mustache
+++ b/templates/imagearea.mustache
@@ -20,6 +20,7 @@
     Boost_campus imagearea layout template.
 
     Context variables required for this template:
+    * hasimagearea - true if images have been uploaded.
     * imageareafiles - array of uploaded images.
     * linkpath - true if image has a link defined in setting imageareaitemslink.
     * filepath - the path where the uploaded image is stored.
@@ -27,6 +28,7 @@
 
      Example context (json):
     {
+        "hasimagearea" : true,
         "imageareafiles": {
             "filepath": "/pix/moodlelogo.png",
             "linkpath": "http://moodle.org"
@@ -36,10 +38,10 @@
 
 }}
 
-{{# imageareafiles }}
+{{#hasimagearea}}
 <div class="imagearea d-flex justify-content-center flex-wrap pb-3 bg-dark">
     {{#imageareafiles}}
         {{#linkpath}}<a href="{{linkpath}}">{{/linkpath}}<img src="{{filepath}}" class="mt-3" {{#alttext}} alt="{{alttext}}"{{/alttext}}>{{#linkpath}}</a>{{/linkpath}}
     {{/imageareafiles}}
 </div>
-{{/ imageareafiles }}
+{{/hasimagearea}}
diff --git a/tests/behat/theme_boost_campus_additional_layout_settings.feature b/tests/behat/theme_boost_campus_additional_layout_settings.feature
index e7b1723..f32bcaf 100644
--- a/tests/behat/theme_boost_campus_additional_layout_settings.feature
+++ b/tests/behat/theme_boost_campus_additional_layout_settings.feature
@@ -16,13 +16,19 @@ Feature: Configuring the theme_boost_campus plugin for the "Additional Layout Se
       | teacher1 | C1     | editingteacher |
 
   @javascript @_file_upload
-  Scenario: Add "Image area items"
+  Scenario: Add "Image area items" and delete them again
     When I log in as "admin"
     And I navigate to "Appearance > Boost Campus" in site administration
     And I click on "Additional Layout Settings" "link"
     And I upload "theme/boost_campus/tests/fixtures/moodle_logo.jpg" file to "Image area items" filemanager
     And I press "Save changes"
     Then ".imagearea img" "css_element" should exist
+    And "//div[contains(concat(' ',normalize-space(@class),' '),' imagearea ')]//img[contains(@src, '/pluginfile.php/1/theme_boost_campus/imageareaitems/0/moodle_logo.jpg')]" "xpath_element" should exist
+    When I navigate to "Appearance > Boost Campus" in site administration
+    And I click on "Additional Layout Settings" "link"
+    And I delete "moodle_logo.jpg" from "Image area items" filemanager
+    And I press "Save changes"
+    Then ".imagearea" "css_element" should not exist
 
   # Dependent on setting "Image area items"
   @javascript @_file_upload
-- 
GitLab