aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichaela Brückner <brueckner@data-quest.de>2025-06-26 09:58:00 +0200
committerDavid Siegfried <david.siegfried@uni-vechta.de>2025-06-26 07:58:00 +0000
commitf3227d8b503261676472829c4bccd9f97a9d4fff (patch)
tree0c6803e09323d64602b6831f149646f8a80259fe
parentd87fff87cd24cfcd7ad72a02af251ad890cf646e (diff)
Resolve "Import von Fragebögen existiert nicht"
Closes #5156 Merge request studip/studip!3857
-rw-r--r--app/controllers/questionnaire.php82
-rw-r--r--app/views/questionnaire/import_dialog.php16
-rw-r--r--app/views/questionnaire/overview.php7
-rw-r--r--lib/models/Questionnaire.php9
4 files changed, 110 insertions, 4 deletions
diff --git a/app/controllers/questionnaire.php b/app/controllers/questionnaire.php
index 98cfeb4..96baaa9 100644
--- a/app/controllers/questionnaire.php
+++ b/app/controllers/questionnaire.php
@@ -1004,4 +1004,86 @@ class QuestionnaireController extends AuthenticatedController
$this->response->add_header('Content-Length', strlen($file_data));
$this->render_json($rawdata);
}
+
+ public function import_dialog_action()
+ {
+ $this->range_id = Request::option('range_id', '');
+ $this->range_type = Request::get('range_type', '');
+ }
+ public function import_file_action()
+ {
+ CSRFProtection::verifyUnsafeRequest();
+
+ $this->range_id = Request::option('range_id', '');
+ $this->range_type = Request::get('range_type', '');
+
+ $num_questionnaires = 0;
+
+ if (empty($_FILES['upload']['name'])) {
+ PageLayout::postWarning(_('Es wurde keine Datei zum Importieren ausgewählt.'));
+ $this->redirect("questionnaire/overview");
+ return;
+ }
+
+ for ($i = 0; $i < count($_FILES['upload']['name']); ++$i) {
+ $file_content = file_get_contents($_FILES['upload']['tmp_name'][$i]);
+ $questionnaire_data = json_decode($file_content);
+
+ $new_questionnaire = new Questionnaire();
+ $new_questionnaire = Questionnaire::create([
+ 'title' => $questionnaire_data->questionnaire->title ?? '',
+ 'user_id' => User::findCurrent()->id,
+ 'anonymous' => $questionnaire_data->questionnaire->anonymous ?? false,
+ 'resultvisibility' => $questionnaire_data->questionnaire->resultvisibility,
+ 'editanswers' => $questionnaire_data->questionnaire->editanswers,
+ 'copyable' => $questionnaire_data->questionnaire->copyable ?? true,
+ ]);
+
+ if (!$new_questionnaire) {
+ continue;
+ }
+
+ $num_questionnaires +=1;
+
+ foreach ($questionnaire_data->questions_data as $index => $value) {
+ QuestionnaireQuestion::create([
+ 'questionnaire_id' => $new_questionnaire->id,
+ 'questiontype' => $value->questiontype,
+ 'questiondata' => $value->questiondata,
+ 'position' => $index,
+ ]);
+ }
+ }
+
+ if ($this->range_id && $this->range_type) {
+ if (
+ ($this->range_id === 'start' && !$GLOBALS['perm']->have_perm('root'))
+ || ($this->range_type === 'course' && !$GLOBALS['perm']->have_studip_perm('tutor', $this->range_id))
+ || ($this->range_type === 'user' && $this->range_id !== User::findCurrent()->id)
+ ) {
+ throw new AccessDeniedException();
+ }
+
+ QuestionnaireAssignment::create([
+ 'questionnaire_id' => $new_questionnaire->id,
+ 'range_id' => $this->range_id,
+ 'range_type' => $this->range_type,
+ 'user_id' => User::findCurrent()->id,
+ ]);
+ }
+
+ if ($num_questionnaires === 1) {
+ PageLayout::postSuccess(sprintf(_('1 Fragebogen wurde importiert.')));
+ } else {
+ PageLayout::postSuccess(sprintf(_('%d Fragebögen wurden importiert.'), $num_questionnaires));
+ }
+
+ if ($this->range_type === '') {
+ $this->redirect("questionnaire/overview");
+ } elseif ($this->range_type === 'course') {
+ $this->redirect("questionnaire/courseoverview");
+ }
+
+
+ }
}
diff --git a/app/views/questionnaire/import_dialog.php b/app/views/questionnaire/import_dialog.php
new file mode 100644
index 0000000..b811363
--- /dev/null
+++ b/app/views/questionnaire/import_dialog.php
@@ -0,0 +1,16 @@
+<form class="default" action="<?= $controller->link_for('questionnaire/import_file',
+ compact('range_type', 'range_id')) ?>"
+ method="POST"
+ enctype="multipart/form-data">
+ <?= CSRFProtection::tokenTag() ?>
+ <fieldset>
+ <legend><?= _('Fragebögen aus Datei(en) importieren') ?></legend>
+ <label>
+ <?= _('Datei(en):') ?>
+ <input type="file" name="upload[]" required accept="application/json" multiple style="min-width: 40em;">
+ </label>
+ </fieldset>
+ <footer data-dialog-button>
+ <?= Studip\Button::createAccept(_('Importieren')) ?>
+ </footer>
+</form>
diff --git a/app/views/questionnaire/overview.php b/app/views/questionnaire/overview.php
index c7e14fa..36b4d7e 100644
--- a/app/views/questionnaire/overview.php
+++ b/app/views/questionnaire/overview.php
@@ -53,4 +53,11 @@ if (!empty($statusgruppen)) {
['data-dialog' => 'size=big']
);
}
+
+$actions->addLink(
+ _('Vorlage importieren'),
+ $controller->url_for('questionnaire/import_dialog', compact('range_type', 'range_id')),
+ Icon::create('import'),
+ ['data-dialog' => 'size=auto']
+);
Sidebar::Get()->addWidget($actions);
diff --git a/lib/models/Questionnaire.php b/lib/models/Questionnaire.php
index 3135dbd..0194e00 100644
--- a/lib/models/Questionnaire.php
+++ b/lib/models/Questionnaire.php
@@ -266,10 +266,11 @@ class Questionnaire extends SimpleORMap implements PrivacyObject
{
$data = [
'questionnaire' => [
- 'title' => $this['title'],
- 'anonymous' => $this['anonymous'],
- 'resultvisibility' => $this['resultvisibility'],
- 'editanswers' => $this['editanswers']
+ 'title' => $this['title'],
+ 'anonymous' => $this['anonymous'],
+ 'resultvisibility' => $this['resultvisibility'],
+ 'editanswers' => $this['editanswers'],
+ 'copyable' => $this['copyable']
],
'questions_data' => []
];