From 605d7e7b2cf83a59711815db50ab67dec07f3e81 Mon Sep 17 00:00:00 2001
From: Elmar Ludwig <elmar.ludwig@uni-osnabrueck.de>
Date: Tue, 14 Jun 2022 16:55:57 +0000
Subject: [PATCH] use consistent dialog sizes for questionnaire forms, fixes
 #1148

Closes #1148

Merge request studip/studip!692
---
 app/controllers/questionnaire.php          | 31 +++++++---------------
 app/views/questionnaire/add_to_context.php |  4 +--
 app/views/questionnaire/answer.php         |  8 +++---
 app/views/questionnaire/evaluate.php       |  8 +++---
 app/views/questionnaire/overview.php       | 23 +++++++++++-----
 app/views/questionnaire/widget.php         | 12 ++++++---
 resources/assets/javascripts/lib/dialog.js | 14 +++++-----
 7 files changed, 52 insertions(+), 48 deletions(-)

diff --git a/app/controllers/questionnaire.php b/app/controllers/questionnaire.php
index bab22ab9140..4e24cc6cfe3 100644
--- a/app/controllers/questionnaire.php
+++ b/app/controllers/questionnaire.php
@@ -26,6 +26,8 @@ class QuestionnaireController extends AuthenticatedController
             Navigation::activateItem('/contents/questionnaire/overview');
         }
 
+        $this->range_id = null;
+        $this->range_type = null;
         if (!$GLOBALS['perm']->have_perm('autor')) {
             throw new AccessDeniedException('Only for logged in users.');
         }
@@ -42,21 +44,16 @@ class QuestionnaireController extends AuthenticatedController
 
     public function courseoverview_action()
     {
-        $this->range_type = Course::findCurrent() ? 'course' : 'institute';
-        if (($this->range_type === "institute") && $GLOBALS['perm']->have_perm('admin')) {
-            if (!Context::get()->id) {
-                Navigation::activateItem('/admin/institute/questionnaires');
-            }
-            require_once 'lib/admin_search.inc.php';
-        }
-        if (!$GLOBALS['perm']->have_studip_perm("tutor", Context::get()->id)) {
+        $this->range_id = Context::getId();
+        $this->range_type = Context::getType();
+        if (!$GLOBALS['perm']->have_studip_perm("tutor", $this->range_id)) {
             throw new AccessDeniedException("Only for logged in users.");
         }
         Navigation::activateItem("/course/admin/questionnaires");
-        $this->statusgruppen = Statusgruppen::findByRange_id(Context::get()->id);
+        $this->statusgruppen = Statusgruppen::findByRange_id($this->range_id);
         $this->questionnaires = Questionnaire::findBySQL(
             "INNER JOIN questionnaire_assignments USING (questionnaire_id) WHERE (questionnaire_assignments.range_id = ? AND questionnaire_assignments.range_type = ?) OR (questionnaire_assignments.range_id IN (?) AND questionnaire_assignments.range_type = 'statusgruppe') ORDER BY questionnaires.mkdate DESC",
-            [Context::get()->id, $this->range_type, array_map(function ($g) { return $g->getId(); }, $this->statusgruppen)]
+            [$this->range_id, $this->range_type, array_map(function ($g) { return $g->getId(); }, $this->statusgruppen)]
         );
         foreach ($this->questionnaires as $questionnaire) {
             if (!$questionnaire['visible'] && $questionnaire->isRunning()) {
@@ -77,14 +74,6 @@ class QuestionnaireController extends AuthenticatedController
     public function add_to_context_action()
     {
         $this->statusgruppen = Statusgruppen::findByRange_id(Context::get()->id);
-        if (!count($this->statusgruppen)) {
-            $this->redirect(
-                $this->url_for("questionnaire/edit", [
-                'range_type' => Context::getType(),
-                'range_id' => Context::get()->id
-            ]));
-            return;
-        }
         PageLayout::setTitle(_("Kontext auswählen"));
     }
 
@@ -578,7 +567,7 @@ class QuestionnaireController extends AuthenticatedController
         if (in_array($this->range_id, ["public", "start"])) {
             $this->range_type = "static";
         }
-        $statusgruppen_ids = [];
+        $this->statusgruppen_ids = [];
         if (in_array($this->range_type, ["course", "institute"])) {
             if ($GLOBALS['perm']->have_studip_perm("tutor", $this->range_id)) {
                 $statusgruppen = Statusgruppen::findByRange_id(Context::get()->id);
@@ -588,7 +577,7 @@ class QuestionnaireController extends AuthenticatedController
                     $GLOBALS['user']->id
                 ]);
             }
-            $statusgruppen_ids = array_map(function ($g) { return $g->getId(); }, $statusgruppen);
+            $this->statusgruppen_ids = array_map(function ($g) { return $g->getId(); }, $statusgruppen);
         }
         $statement = DBManager::get()->prepare("
             SELECT questionnaires.*
@@ -607,7 +596,7 @@ class QuestionnaireController extends AuthenticatedController
         $statement->execute([
             'range_id' => $this->range_id,
             'range_type' => $this->range_type,
-            'statusgruppe_id' => $statusgruppen_ids
+            'statusgruppe_id' => $this->statusgruppen_ids
         ]);
         $this->questionnaire_data = $statement->fetchAll(PDO::FETCH_ASSOC);
         $stopped_visible = 0;
diff --git a/app/views/questionnaire/add_to_context.php b/app/views/questionnaire/add_to_context.php
index 885aad58f78..5fdf836f2e3 100644
--- a/app/views/questionnaire/add_to_context.php
+++ b/app/views/questionnaire/add_to_context.php
@@ -8,13 +8,13 @@ $icons = [
 <div class="file_select_possibilities">
     <div>
         <a href="<?= $controller->link_for("questionnaire/edit", ['range_type' => Context::getType(), 'range_id' => Context::get()->id]) ?>"
-           data-dialog="size=big">
+           data-dialog="size=default">
             <?= Icon::create($icons[Context::getType()], Icon::ROLE_CLICKABLE)->asImg(50) ?>
             <?= htmlReady(Context::get()->name) ?>
         </a>
         <? foreach ($statusgruppen as $statusgruppe) : ?>
             <a href="<?= $controller->link_for("questionnaire/edit", ['range_type' => "statusgruppe", 'range_id' => $statusgruppe->getId()]) ?>"
-               data-dialog="size=big">
+               data-dialog="size=default">
                 <?= Icon::create('group2', Icon::ROLE_CLICKABLE)->asImg(50) ?>
                 <?= htmlReady($statusgruppe->name) ?>
             </a>
diff --git a/app/views/questionnaire/answer.php b/app/views/questionnaire/answer.php
index a4468a68533..d08966964bd 100644
--- a/app/views/questionnaire/answer.php
+++ b/app/views/questionnaire/answer.php
@@ -41,16 +41,16 @@
             <?= \Studip\Button::create(_("Speichern"), 'questionnaire_answer', ['onClick' => "return STUDIP.Questionnaire.beforeAnswer.call(this);"]) ?>
         <? endif ?>
         <? if ($questionnaire->resultsVisible()) : ?>
-            <?= \Studip\LinkButton::create(_("Ergebnisse anzeigen"), URLHelper::getURL("dispatch.php/questionnaire/evaluate/".$questionnaire->getId()), ['data-dialog' => "1"]) ?>
+            <?= \Studip\LinkButton::create(_("Ergebnisse anzeigen"), URLHelper::getURL("dispatch.php/questionnaire/evaluate/".$questionnaire->getId()), ['data-dialog' => '']) ?>
         <? endif ?>
         <? if ($questionnaire->isEditable() && (!$questionnaire->isRunning() || !$questionnaire->countAnswers())) : ?>
-            <?= \Studip\LinkButton::create(_("Bearbeiten"), URLHelper::getURL("dispatch.php/questionnaire/edit/".$questionnaire->getId()), ['data-dialog' => "1"]) ?>
+            <?= \Studip\LinkButton::create(_("Bearbeiten"), URLHelper::getURL("dispatch.php/questionnaire/edit/".$questionnaire->getId()), ['data-dialog' => '']) ?>
         <? endif ?>
         <? if ($questionnaire->isEditable()) : ?>
-            <?= \Studip\LinkButton::create(_("Kontext auswählen"), URLHelper::getURL("dispatch.php/questionnaire/context/".$questionnaire->getId(), ['range_type' => $range_type, 'range_id' => $range_id]), ['data-dialog' => "1"]) ?>
+            <?= \Studip\LinkButton::create(_("Kontext auswählen"), URLHelper::getURL("dispatch.php/questionnaire/context/".$questionnaire->getId(), ['range_type' => $range_type, 'range_id' => $range_id]), ['data-dialog' => '']) ?>
         <? endif ?>
         <? if ($questionnaire->isCopyable()) : ?>
-            <?= \Studip\LinkButton::create(_("Kopieren"), URLHelper::getURL("dispatch.php/questionnaire/copy/".$questionnaire->getId()), ['data-dialog' => "1"]) ?>
+            <?= \Studip\LinkButton::create(_("Kopieren"), URLHelper::getURL("dispatch.php/questionnaire/copy/".$questionnaire->getId()), ['data-dialog' => '']) ?>
         <? endif ?>
         <? if ($questionnaire->isEditable() && (!$questionnaire->isRunning())) : ?>
             <?= \Studip\LinkButton::create(_("Starten"), URLHelper::getURL("dispatch.php/questionnaire/start/".$questionnaire->getId(), in_array($range_type, ['course', 'insitute']) ? ['redirect' => $range_type . "/overview"] : [])) ?>
diff --git a/app/views/questionnaire/evaluate.php b/app/views/questionnaire/evaluate.php
index 258dbb01b4e..5ac29a59693 100644
--- a/app/views/questionnaire/evaluate.php
+++ b/app/views/questionnaire/evaluate.php
@@ -34,19 +34,19 @@
     </script>
     <div data-dialog-button style="max-height: none; opacity: 1; text-align: center;">
         <? if ($questionnaire->isAnswerable() && $questionnaire['editanswers']) : ?>
-            <?= \Studip\LinkButton::create($questionnaire->isAnswered() ? _("Antwort ändern") : _("Beantworten"), URLHelper::getURL("dispatch.php/questionnaire/answer/".$questionnaire->getId(), ['range_type' => $range_type, 'range_id' => $range_id]), ['data-dialog' => "1"]) ?>
+            <?= \Studip\LinkButton::create($questionnaire->isAnswered() ? _("Antwort ändern") : _("Beantworten"), URLHelper::getURL("dispatch.php/questionnaire/answer/".$questionnaire->getId(), ['range_type' => $range_type, 'range_id' => $range_id]), ['data-dialog' => '']) ?>
         <? endif ?>
         <? if ($questionnaire->isEditable()) : ?>
             <?= \Studip\LinkButton::create(_("Ergebnisse herunterladen"), URLHelper::getURL("dispatch.php/questionnaire/export/".$questionnaire->getId())) ?>
         <? endif ?>
         <? if ($questionnaire->isEditable() && (!$questionnaire->isRunning() || !$questionnaire->countAnswers())) : ?>
-            <?= \Studip\LinkButton::create(_("Bearbeiten"), URLHelper::getURL("dispatch.php/questionnaire/edit/".$questionnaire->getId(), ['range_type' => $range_type, 'range_id' => $range_id]), ['data-dialog' => "1"]) ?>
+            <?= \Studip\LinkButton::create(_("Bearbeiten"), URLHelper::getURL("dispatch.php/questionnaire/edit/".$questionnaire->getId(), ['range_type' => $range_type, 'range_id' => $range_id]), ['data-dialog' => '']) ?>
         <? endif ?>
         <? if ($questionnaire->isEditable()) : ?>
-            <?= \Studip\LinkButton::create(_("Kontext auswählen"), URLHelper::getURL("dispatch.php/questionnaire/context/".$questionnaire->getId(), ['range_type' => $range_type, 'range_id' => $range_id]), ['data-dialog' => "1"]) ?>
+            <?= \Studip\LinkButton::create(_("Kontext auswählen"), URLHelper::getURL("dispatch.php/questionnaire/context/".$questionnaire->getId(), ['range_type' => $range_type, 'range_id' => $range_id]), ['data-dialog' => '']) ?>
         <? endif ?>
         <? if ($questionnaire->isCopyable()) : ?>
-            <?= \Studip\LinkButton::create(_("Kopieren"), URLHelper::getURL("dispatch.php/questionnaire/copy/".$questionnaire->getId()), ['data-dialog' => "1"]) ?>
+            <?= \Studip\LinkButton::create(_("Kopieren"), URLHelper::getURL("dispatch.php/questionnaire/copy/".$questionnaire->getId()), ['data-dialog' => '']) ?>
         <? endif ?>
         <? if ($questionnaire->isEditable() && !$questionnaire->isRunning()) : ?>
             <?= \Studip\LinkButton::create(_("Starten"), URLHelper::getURL("dispatch.php/questionnaire/start/".$questionnaire->getId(),  in_array($range_type, ['course', 'insitute']) ? ['redirect' => $range_type . "/overview"] : [])) ?>
diff --git a/app/views/questionnaire/overview.php b/app/views/questionnaire/overview.php
index 9844c342e8c..d3f33c811bc 100644
--- a/app/views/questionnaire/overview.php
+++ b/app/views/questionnaire/overview.php
@@ -1,4 +1,4 @@
-<form action="<?= $controller->link_for("questionnaire/bulkdelete", $range_type ? ['range_type' => $range_type, 'range_id' => Context::getId()]: []) ?>"
+<form action="<?= $controller->link_for("questionnaire/bulkdelete", compact('range_type', 'range_id')) ?>"
       method="post">
     <table class="default" id="questionnaire_overview">
         <thead>
@@ -36,10 +36,19 @@
 </form>
 <?php
 $actions = new ActionsWidget();
-$actions->addLink(
-    _('Fragebogen erstellen'),
-    $controller->url_for('questionnaire/add_to_context', $range_type ? ['range_type' => $range_type, 'range_id' => Context::getId()]: []),
-    Icon::create('add'),
-    ['data-dialog' => 'size=auto']
-);
+if (!empty($statusgruppen)) {
+    $actions->addLink(
+        _('Fragebogen erstellen'),
+        $controller->url_for('questionnaire/add_to_context'),
+        Icon::create('add'),
+        ['data-dialog' => 'size=auto']
+    );
+} else {
+    $actions->addLink(
+        _('Fragebogen erstellen'),
+        $controller->url_for('questionnaire/edit', compact('range_type', 'range_id')),
+        Icon::create('add'),
+        ['data-dialog' => '']
+    );
+}
 Sidebar::Get()->addWidget($actions);
diff --git a/app/views/questionnaire/widget.php b/app/views/questionnaire/widget.php
index 393db14a457..6dada584a39 100644
--- a/app/views/questionnaire/widget.php
+++ b/app/views/questionnaire/widget.php
@@ -7,9 +7,15 @@
         </h1>
         <nav>
             <? if ($allowed_to_add) : ?>
-                <a href="<?= $controller->link_for('questionnaire/add_to_context', $range_type ? ['range_type' => $range_type, 'range_id' => Context::getId()]: []) ?>" data-dialog title="<?= _('Fragebogen hinzufügen') ?>">
-                    <?= Icon::create("add", "clickable")->asimg("16px", ['class' => "text-bottom"]) ?>
-                </a>
+                <? if ($statusgruppen_ids): ?>
+                    <a href="<?= $controller->link_for('questionnaire/add_to_context') ?>" data-dialog="size=auto" title="<?= _('Fragebogen hinzufügen') ?>">
+                        <?= Icon::create("add", "clickable")->asimg("16px", ['class' => "text-bottom"]) ?>
+                    </a>
+                <? else: ?>
+                    <a href="<?= $controller->link_for('questionnaire/edit', compact('range_type', 'range_id')) ?>" data-dialog title="<?= _('Fragebogen hinzufügen') ?>">
+                        <?= Icon::create("add", "clickable")->asimg("16px", ['class' => "text-bottom"]) ?>
+                    </a>
+                <? endif ?>
                 <a href="<?= URLHelper::getLink("dispatch.php/questionnaire/" . ($range_type == 'course' || $range_type == 'institute' ? 'course' : ''). "overview") ?>" title="<?= _('Fragebögen verwalten') ?>">
                     <?= Icon::create("edit", "clickable")->asimg("16px", ['class' => "text-bottom"]) ?>
                 </a>
diff --git a/resources/assets/javascripts/lib/dialog.js b/resources/assets/javascripts/lib/dialog.js
index 591d10e1470..8703255402e 100644
--- a/resources/assets/javascripts/lib/dialog.js
+++ b/resources/assets/javascripts/lib/dialog.js
@@ -559,17 +559,17 @@ Dialog.calculateDimensions = function (instance, content, options) {
         // Remove helper element
         helper.remove();
     } else if (options.size === 'big') {
-        width = $('body').width() * 0.9;
-        height = $('body').height() * 0.8;
+        width = $(window).width() * 0.9;
+        height = $(window).height() * 0.8;
     } else if (options.size === 'medium') {
-        width = $('body').width() * 0.6;
-        height = $('body').height() * 0.5;
+        width = $(window).width() * 0.6;
+        height = $(window).height() * 0.5;
     } else if (options.size === 'medium-43') {
         //Medium size in 4:3 aspect ratio
-        height = $('body').height() * 0.8;
+        height = $(window).height() * 0.8;
         width = parseInt(height) * 1.33333333;
-        if (width > $('body').width()) {
-            width = $('body').width() * 0.9;
+        if (width > $(window).width()) {
+            width = $(window).width() * 0.9;
         }
     } else if (options.size === 'small') {
         width = 300;
-- 
GitLab