dev.metalisp.sbt/docs/pattern/questionnaire.org
2024-04-21 22:01:47 +02:00

4.5 KiB

Creating a Questionnaire using dev.metalisp.sbt/component/questionnaire Macros in a Web Application

Introduction

Questionnaires are powerful tools for gathering information and insights from users. They are crucial for understanding user behaviors, preferences, and requirements. This document will demonstrate how to build a web-based questionnaire using the cl-sbt/questionnaire macros in a Common Lisp application.

The Importance of Questionnaires

Questionnaires help in collecting data for various purposes including market research, user experience study, and many more. They can be in multiple formats like single-choice, multiple-choice, or open-ended questions.

Types of Questions

  • Single Choice: Questions that allow one answer.
  • Multiple Choice: Questions that allow multiple answers.
  • Text: Open-ended questions for free text input.

Integrating dev.metalisp.sbt/component/questionnaire Macros

To create a questionnaire in your Common Lisp web application, cl-sbt/questionnaire macros can be employed. These macros generate the HTML required for different types of questions in a questionnaire.

  (ql:quickload '(:dev.metalisp.sbt :hunchentoot))
:DEV.METALISP.SBT
  (defpackage my-web-questionnaire-app
    (:use :cl)
    (:import-from :dev.metalisp.sbt :with-page)
    (:import-from :dev.metalisp.sbt/questionnaire :questionnaire)
    (:export :generate-questionnaire-page))
#<PACKAGE "MY-WEB-QUESTIONNAIRE-APP">
  (in-package :my-web-questionnaire-app)

  (defun generate-questionnaire-page ()
    "Generates an HTML page with questionnaires using questionnaire macros."
    (with-output-to-string (spinneret:*html*)
      (with-page (:title "Questionnaire Example" :main-con t)
        (questionnaire "#" "none"
          (:ask "How old are you?"
           :group "age"
           :choices (:radio "18-24" "25-34" "35-44" "45-54" "55+"))

          (:ask "Gender"
           :group "gender"
           :choices (:radio "Male" "Female" "Non-binary" "Prefer not to say" "Other" :text "Please specify"))

          (:ask "How many hours per day, on average, do you spend browsing the internet?"
           :group "webbrowsing"
           :choices (:radio "Less than 1 hour" "1-3 hours" "3-5 hours" "5+ hours"))

          (:ask "What are your favorite brands?"
           :group "brands"
           :choices (:checkbox "Brand A" "Brand B" "Brand C" "None" "Other" :text "Please specify"))

          (:ask "How often do you shop online?"
           :group "shopping"
           :choices (:radio "Daily" "Weekly" "Monthly" "Rarely" "Never"))

          (:ask "Which of the following devices do you own?"
           :group "devices"
           :choices (:checkbox "Smartphone" "Laptop/PC" "Tablet" "Smartwatch" "None"))

          (:ask "What is your preferred method of payment for online purchases?"
           :group "payment"
           :choices (:radio "Credit/Debit Card" "PayPal" "Bank Transfer" "Cash on Delivery" "Cryptocurrency"))

          (:ask "Which social media platforms do you use regularly?"
           :group "socialmedia"
           :choices (:checkbox "Facebook" "Twitter" "Instagram" "LinkedIn" "TikTok" "None" "Other" :text "Please specify"))

          (:ask "How do you usually consume news?"
           :group "newsconsumption"
           :choices (:radio "Online News Websites" "Social Media" "Television" "Newspapers/Magazines" "Radio" "None"))))))

  (format t (generate-questionnaire-page))

/marcuskammer/dev.metalisp.sbt/src/branch/main/docs/pattern/questionnaire-page.html

This example demonstrates the integration of the cl-sbt/questionnaire macros into a web application. The macros assist in generating the required HTML for different types of Bootstrap questionnaires.

  (in-package :my-web-questionnaire-app)

  (defvar *basic-server* nil)

  (defun start-server ()
    (setf *basic-server* (make-instance 'hunchentoot:easy-acceptor :name "basic-server" :port 8080))
    (hunchentoot:start *basic-server*))

  (defun stop-server (server)
    (when server
      (hunchentoot:stop server)
      (setf *basic-server* nil)))

  (defun restart-server (server)
    (stop-server server)
    (start-server))

  (hunchentoot:define-easy-handler (submit :uri "/submit")
      ((data :parameter-type :post))
    (format nil "Hello, ~a!" data))
SUBMIT