2024-07-07 13:46:09 +02:00
|
|
|
;;;; -*- mode: common-lisp; coding: utf-8; -*-
|
|
|
|
|
2024-07-09 20:23:19 +02:00
|
|
|
(defpackage ml-survey/new-survey
|
|
|
|
(:use :cl)
|
|
|
|
(:import-from #:hunchentoot
|
|
|
|
#:define-easy-handler)
|
2024-07-29 22:43:40 +02:00
|
|
|
(:import-from #:ml-sbt/navbar
|
|
|
|
#:with-navbar)
|
|
|
|
(:import-from #:ml-sbt/btn
|
|
|
|
#:btn)
|
2024-07-28 19:25:12 +02:00
|
|
|
(:import-from #:ml-sbt
|
2024-07-09 20:23:19 +02:00
|
|
|
#:with-page
|
2024-07-29 22:43:40 +02:00
|
|
|
#:body-header
|
|
|
|
#:body-main))
|
2024-05-31 15:18:13 +02:00
|
|
|
|
2024-07-09 20:23:19 +02:00
|
|
|
(in-package :ml-survey/new-survey)
|
|
|
|
|
|
|
|
(defun list-questionnaires ()
|
|
|
|
(mapcar #'ml-survey/fileops:extract-lang-and-filename
|
|
|
|
(ml-survey/fileops:questionnaires-list-files)))
|
|
|
|
|
|
|
|
(defun view (&optional survey-id)
|
2024-05-31 15:18:13 +02:00
|
|
|
"Generates the view to create a new survey."
|
2024-10-20 08:25:12 +02:00
|
|
|
(let ((questionnaires (list-questionnaires))
|
|
|
|
(container "container-fluid"))
|
2024-06-27 23:31:47 +02:00
|
|
|
(with-page (:title "New Survey")
|
2024-10-23 07:51:48 +02:00
|
|
|
(body-header "fluid" "New Survey"
|
|
|
|
(with-navbar "fluid" "New Survey"
|
2024-08-03 09:20:32 +02:00
|
|
|
"Home" "/" "New Survey" "/new-survey"))
|
2024-10-23 07:51:48 +02:00
|
|
|
(body-main "fluid"
|
2024-08-03 09:20:32 +02:00
|
|
|
;; If `questionnaires' is an empty list, show the user an warning
|
|
|
|
;; message.
|
|
|
|
(unless questionnaires
|
|
|
|
(:div :class "alert alert-warning"
|
|
|
|
:role "alert"
|
|
|
|
(format nil "There are no questionnaires available.~%
|
2024-07-09 20:23:19 +02:00
|
|
|
The folder: ~a is empty." (ml-survey/fileops:questionnaires-dir))))
|
2024-06-24 18:21:03 +02:00
|
|
|
|
2024-08-03 09:20:32 +02:00
|
|
|
;; When a new survey was created, show the user an info message.
|
|
|
|
(when survey-id
|
|
|
|
(:div :class "alert alert-info"
|
|
|
|
:role "alert"
|
|
|
|
(format nil "Your new survey: ~A is created." survey-id)))
|
2024-05-31 15:18:13 +02:00
|
|
|
|
2024-08-03 09:20:32 +02:00
|
|
|
(:form :action "/new-survey"
|
|
|
|
:method "post"
|
2024-05-31 15:18:13 +02:00
|
|
|
|
2024-07-29 22:43:40 +02:00
|
|
|
(:fieldset
|
2024-08-03 09:20:32 +02:00
|
|
|
(:legend "Metadata")
|
|
|
|
(:div :class "mb-3"
|
|
|
|
(:label :class "form-label"
|
|
|
|
:for "title" "Title")
|
|
|
|
(:input :class "form-control"
|
|
|
|
:type "text"
|
|
|
|
:id "title"
|
|
|
|
:required ""
|
|
|
|
:name "title"))
|
2024-07-29 22:43:40 +02:00
|
|
|
(:div :class "mb-3"
|
2024-08-03 09:20:32 +02:00
|
|
|
(:label :class "form-label"
|
|
|
|
:for "description" "Description")
|
|
|
|
(:textarea :class "form-control"
|
|
|
|
:rows "3"
|
|
|
|
:id "description"
|
|
|
|
:name "description")))
|
|
|
|
|
|
|
|
(when questionnaires
|
|
|
|
(:fieldset
|
|
|
|
(:legend "Questionnaires")
|
|
|
|
(:div :class "mb-3"
|
|
|
|
(loop for q in questionnaires
|
|
|
|
do (:div :class "form-check"
|
|
|
|
(:input :class "form-check-input"
|
|
|
|
:type "checkbox"
|
|
|
|
:value q
|
|
|
|
:id q
|
|
|
|
:name "questionnaire"
|
|
|
|
(:label :class "form-check-label"
|
|
|
|
:for q
|
|
|
|
q)))))))
|
2024-06-27 23:31:47 +02:00
|
|
|
|
2024-08-03 09:20:32 +02:00
|
|
|
(:button :type"Submit"
|
|
|
|
:class "btn btn-primary"
|
|
|
|
"Create Survey"))))))
|
2024-07-09 20:23:19 +02:00
|
|
|
|
|
|
|
(defun process-new-survey-get ()
|
|
|
|
(view))
|
|
|
|
|
|
|
|
(defun process-new-survey-post (request)
|
|
|
|
(let ((post-params (hunchentoot:post-parameters* request))
|
|
|
|
(uid (ml-survey/app:generate-uuid))
|
|
|
|
(stored-surveys (ml-survey/fileops:read-from-file (ml-survey/fileops:make-surveys-db-file))))
|
|
|
|
(ml-survey/fileops:write-to-file (ml-survey/fileops:make-surveys-db-file)
|
|
|
|
(push (list uid post-params) stored-surveys))
|
|
|
|
(view uid)))
|
|
|
|
|
|
|
|
(define-easy-handler (new-survey-handler :uri "/new-survey") nil
|
|
|
|
(cond ((eq (hunchentoot:request-method*) :get)
|
|
|
|
(process-new-survey-get))
|
|
|
|
((eq (hunchentoot:request-method*) :post)
|
|
|
|
(process-new-survey-post hunchentoot:*request*))))
|