From dc677dde0e3bbf92537d85c2fdeeb7d3591bc1a4 Mon Sep 17 00:00:00 2001
From: Elmar Ludwig <elmar.ludwig@uni-osnabrueck.de>
Date: Wed, 21 Aug 2024 23:54:18 +0200
Subject: [PATCH] Revert "switch assignment payload type to integer, fixes
 #265"

This reverts commit 4cbbbf747bd9f45e31c608bc5f577e8528bdf990.
---
 controllers/api.php                  | 11 ++++++-----
 js/test-block.js                     |  8 +++-----
 lib/TestBlock.php                    | 15 ++-------------
 views/exercises/courseware_block.php |  2 +-
 views/sheets/show_exercise.php       |  2 +-
 5 files changed, 13 insertions(+), 25 deletions(-)

diff --git a/controllers/api.php b/controllers/api.php
index 207dbb1..5e79332 100644
--- a/controllers/api.php
+++ b/controllers/api.php
@@ -24,7 +24,8 @@ class ApiController extends StudipController
         foreach ($assignments as $assignment) {
             if ($assignment->type !== 'exam') {
                 $data[] = [
-                    'id'     => (int) $assignment->id,
+                    'id'     => $assignment->id,
+                    'link'   => $this->url_for('api/assignment', $assignment->id),
                     'title'  => $assignment->test->title,
                     'type'   => $assignment->type,
                     'icon'   => $assignment->getTypeIcon()->getShape(),
@@ -62,7 +63,7 @@ class ApiController extends StudipController
         }
 
         $data = [
-            'id'             => (int) $assignment->id,
+            'id'             => $assignment->id,
             'title'          => $assignment->test->title,
             'type'           => $assignment->type,
             'icon'           => $assignment->getTypeIcon()->getShape(),
@@ -81,7 +82,8 @@ class ApiController extends StudipController
             $exercise = $exercise_ref->exercise;
 
             $data['exercises'][] = [
-                'id'            => (int) $exercise->id,
+                'id'            => $exercise->id,
+                'link'          => $this->url_for('api/exercise', $assignment_id, $exercise->id),
                 'type'          => $exercise->type,
                 'title'         => $exercise->title,
                 'template'      => $template->render(),
@@ -120,7 +122,7 @@ class ApiController extends StudipController
         $exercise = $exercise_ref->exercise;
 
         $data = [
-            'id'            => (int) $exercise->id,
+            'id'            => $exercise->id,
             'type'          => $exercise->type,
             'title'         => $exercise->title,
             'template'      => $template->render(),
@@ -138,7 +140,6 @@ class ApiController extends StudipController
         $solution = $assignment->getSolution($user_id, $exercise->id);
         $max_tries = $assignment->getMaxTries();
         $max_points = $exercise_ref->points;
-        $sample_solution = false;
         $show_solution = false;
         $tries_left = null;
 
diff --git a/js/test-block.js b/js/test-block.js
index c03bdae..25efc11 100644
--- a/js/test-block.js
+++ b/js/test-block.js
@@ -84,9 +84,7 @@ const CoursewareTestBlock = {
                             </template>
                             <template #selected-option="{title, icon, start, end}">
                                 <studip-icon :shape="icon" role="info"/>
-                                <template v-if="title">
-                                    {{title}} ({{start}} - {{end}})
-                                </template>
+                                {{title}} ({{start}} - {{end}})
                             </template>
                             <template #option="{title, icon, start, end, block}">
                                 <studip-icon :shape="icon" role="info"/>
@@ -117,7 +115,7 @@ const CoursewareTestBlock = {
     data() {
         return {
             assignments: [],
-            assignment_id: 0,
+            assignment_id: '',
             assignment: null,
             errorMessage: null,
             exercises: [],
@@ -165,7 +163,7 @@ const CoursewareTestBlock = {
                 });
         },
         loadSelectedAssignment() {
-            if (!this.assignment_id) {
+            if (this.assignment_id === '') {
                 this.errorMessage = this.$gettext('Es wurde noch kein Aufgabenblatt ausgewählt.');
                 return;
             }
diff --git a/lib/TestBlock.php b/lib/TestBlock.php
index 290246a..031f5f1 100644
--- a/lib/TestBlock.php
+++ b/lib/TestBlock.php
@@ -64,7 +64,7 @@ class TestBlock extends BlockType
      */
     public function initialPayload(): array
     {
-        return ['assignment' => 0];
+        return ['assignment' => ''];
     }
 
     /**
@@ -76,7 +76,7 @@ class TestBlock extends BlockType
             'type' => 'object',
             'properties' => [
                 'assignment' => [
-                    'type' => 'integer'
+                    'type' => 'string'
                 ]
             ]
         ];
@@ -108,17 +108,6 @@ class TestBlock extends BlockType
         return [];
     }
 
-    /**
-     * Returns the decoded payload of the block associated with this instance.
-     */
-    public function getPayload()
-    {
-        $payload = parent::getPayload();
-        $payload['assignment'] = (int) $payload['assignment'];
-
-        return $payload;
-    }
-
     /**
      * Copy the payload of this block into the given range id.
      */
diff --git a/views/exercises/courseware_block.php b/views/exercises/courseware_block.php
index 3153455..8212b78 100644
--- a/views/exercises/courseware_block.php
+++ b/views/exercises/courseware_block.php
@@ -56,7 +56,7 @@
 <? else: ?>
     <?= $this->render_partial($exercise->getSolveTemplate($solution, $assignment, $user_id)) ?>
 
-    <? if (!empty($exercise->options['comment'])): ?>
+    <? if ($exercise->options['comment']): ?>
         <label>
             <?= _vips('Bemerkungen zur Lösung (optional)') ?>
             <textarea name="student_comment"><?= htmlReady($solution->student_comment) ?></textarea>
diff --git a/views/sheets/show_exercise.php b/views/sheets/show_exercise.php
index 8e7633a..3ee5fc9 100644
--- a/views/sheets/show_exercise.php
+++ b/views/sheets/show_exercise.php
@@ -96,7 +96,7 @@
 
             <?= $this->render_partial($exercise->getSolveTemplate($solution, $assignment, $solver_id)) ?>
 
-            <? if (!empty($exercise->options['comment'])) : ?>
+            <? if (isset($exercise->options['comment']) && $exercise->options['comment']) : ?>
                 <label>
                     <?= _vips('Bemerkungen zur Lösung (optional)') ?>
                     <textarea name="student_comment"><?= $solution ? htmlReady($solution->student_comment) : '' ?></textarea>
-- 
GitLab