From f4e41db9fa7e074eff445553c8eb7f03453727dc Mon Sep 17 00:00:00 2001
From: Elmar Ludwig <elmar.ludwig@uni-osnabrueck.de>
Date: Fri, 12 Aug 2022 14:54:36 +0200
Subject: [PATCH] add escape syntax for cloze_exercise, fix white space in
 layout, fixes #136

---
 css/vips.css                               |  1 +
 exercises/cloze_exercise.php               | 10 ++++++----
 views/exercises/correct_cloze_exercise.php | 16 ++++++++--------
 views/exercises/print_cloze_exercise.php   |  6 +++---
 4 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/css/vips.css b/css/vips.css
index 5a4a2d4..7651819 100644
--- a/css/vips.css
+++ b/css/vips.css
@@ -313,6 +313,7 @@ button.vips_file_upload:hover {
     min-height: 36px;
     min-width: 80px;
     vertical-align: middle;
+    white-space: normal;
 }
 
 .cloze_drop.hover {
diff --git a/exercises/cloze_exercise.php b/exercises/cloze_exercise.php
index 920df64..8684189 100644
--- a/exercises/cloze_exercise.php
+++ b/exercises/cloze_exercise.php
@@ -370,15 +370,13 @@ class cloze_exercise extends Exercise
                         $answers[] = '*' . $answer['text'];
                     } else if ($answer['score'] == 0.5) {
                         $answers[] = '~' . $answer['text'];
+                    } else if (preg_match('/^([":*~]|$)/', $answer['text'])) {
+                        $answers[] = '"' . $answer['text'] . '"';
                     } else {
                         $answers[] = $answer['text'];
                     }
                 }
 
-                if (count($answers) === 1 && $answers[0] === '') {
-                    $answers = [' '];
-                }
-
                 $blank = '[[' . $select . implode('|', $answers) . ']]';
 
                 if ($is_html) {
@@ -435,6 +433,10 @@ class cloze_exercise extends Exercise
                             }
                         }
 
+                        if (preg_match('/^"(.*)"$/', $answer, $matches)) {
+                            $answer = $matches[1];
+                        }
+
                         $answers[] = ['text' => $answer, 'score' => $points];
                     }
                 }
diff --git a/views/exercises/correct_cloze_exercise.php b/views/exercises/correct_cloze_exercise.php
index 4bca0d8..477cf81 100644
--- a/views/exercises/correct_cloze_exercise.php
+++ b/views/exercises/correct_cloze_exercise.php
@@ -5,20 +5,20 @@
         <? if (isset($exercise->task['answers'][$blank])) : ?>
             <? if ($solution->id): ?>
                 <? if ($results[$blank]['points'] == 1): ?>
-                 --><span class="correct_item"><?= htmlReady($response[$blank]) ?>
-                        <?= Icon::create('accept', 'status-green', ['class' => 'correction_inline', 'title' => _vips('richtig')]) ?><!--
+                 --><span class="correct_item"><?= htmlReady($response[$blank]) ?><!--
+                     --><?= Icon::create('accept', 'status-green', ['class' => 'correction_inline', 'title' => _vips('richtig')]) ?><!--
                  --></span><!--
                 <? elseif ($results[$blank]['points'] == 0.5): ?>
-                 --><span class="fuzzy_item"><?= htmlReady($response[$blank]) ?>
-                        <?= Icon::create('decline', 'status-yellow', ['class' => 'correction_inline', 'title' => _vips('fast richtig')]) ?><!--
+                 --><span class="fuzzy_item"><?= htmlReady($response[$blank]) ?><!--
+                     --><?= Icon::create('decline', 'status-yellow', ['class' => 'correction_inline', 'title' => _vips('fast richtig')]) ?><!--
                  --></span><!--
                 <? elseif (!$edit_solution || $results[$blank]['safe']): ?>
-                 --><span class="wrong_item"><?= htmlReady($response[$blank]) ?>
-                        <?= Icon::create('decline', 'status-red', ['class' => 'correction_inline', 'title' => _vips('falsch')]) ?><!--
+                 --><span class="wrong_item"><?= htmlReady($response[$blank]) ?><!--
+                     --><?= Icon::create('decline', 'status-red', ['class' => 'correction_inline', 'title' => _vips('falsch')]) ?><!--
                  --></span><!--
                 <? else: ?>
-                 --><span class="wrong_item"><?= htmlReady($response[$blank]) ?>
-                        <?= Icon::create('question', 'status-red', ['class' => 'correction_inline', 'title' => _vips('unbekannte Antwort')]) ?><!--
+                 --><span class="wrong_item"><?= htmlReady($response[$blank]) ?><!--
+                     --><?= Icon::create('question', 'status-red', ['class' => 'correction_inline', 'title' => _vips('unbekannte Antwort')]) ?><!--
                  --></span><!--
                 <? endif ?>
             <? endif ?>
diff --git a/views/exercises/print_cloze_exercise.php b/views/exercises/print_cloze_exercise.php
index 6933cf6..f7e37c3 100644
--- a/views/exercises/print_cloze_exercise.php
+++ b/views/exercises/print_cloze_exercise.php
@@ -16,9 +16,9 @@
                 <? endif ?>
             <? elseif ($exercise->isSelect($blank)): ?>
                 <? foreach ($exercise->task['answers'][$blank] as $index => $option) : ?>
-                 --><?= $index ? ' | ' : '' ?>
-                    <?= Assets::img(vips_image_url('choice_unchecked.svg'), ['style' => 'vertical-align: text-bottom;']) ?>
-                    <span style="border-bottom: 1px dotted black;"><?= htmlReady($option['text']) ?></span><!--
+                 --><?= $index ? ' | ' : '' ?><!--
+                 --><?= Assets::img(vips_image_url('choice_unchecked.svg'), ['style' => 'vertical-align: text-bottom;']) ?> <!--
+                 --><span style="border-bottom: 1px dotted black;"><?= htmlReady($option['text']) ?></span><!--
                 <? endforeach ?>
             <? else: ?>
              --><?= str_repeat('__', $exercise->getInputWidth($blank)) ?><!--
-- 
GitLab