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/survey
|
|
|
|
(:use #:cl)
|
|
|
|
(:import-from #:hunchentoot
|
|
|
|
#:define-easy-handler)
|
2024-07-31 22:34:08 +02:00
|
|
|
(:import-from #:ml-sbt/section
|
2024-08-09 23:07:22 +02:00
|
|
|
#:with-section
|
|
|
|
#:with-title-bar)
|
2024-07-29 22:43:40 +02:00
|
|
|
(:import-from #:ml-sbt/navbar
|
|
|
|
#:with-navbar)
|
2024-07-28 19:25:12 +02:00
|
|
|
(:import-from #:ml-sbt
|
2024-07-14 16:36:25 +02:00
|
|
|
#:*use-cdn*
|
2024-07-09 20:23:19 +02:00
|
|
|
#:with-page
|
2024-07-29 22:43:40 +02:00
|
|
|
#:body-header
|
|
|
|
#:body-main)
|
2024-08-18 15:06:42 +02:00
|
|
|
(:import-from #:ml-survey/assessment
|
|
|
|
#:assessment-render-results
|
|
|
|
#:parse-assessment)
|
2024-07-09 20:23:19 +02:00
|
|
|
(:export #:survey-id
|
|
|
|
#:survey
|
|
|
|
#:survey-properties-title
|
|
|
|
#:survey-properties-description))
|
|
|
|
|
|
|
|
(in-package #:ml-survey/survey)
|
2024-06-12 22:24:00 +02:00
|
|
|
|
2024-07-14 16:36:25 +02:00
|
|
|
(defparameter *use-cdn* nil)
|
|
|
|
|
2024-06-11 18:27:17 +02:00
|
|
|
(defclass survey ()
|
|
|
|
((id :initarg :id :reader survey-id)
|
|
|
|
(data-dir :initarg :data-dir :reader survey-data-dir)
|
|
|
|
(properties :initarg :properties :reader survey-properties)))
|
|
|
|
|
|
|
|
(defmethod initialize-instance :after ((survey survey) &key)
|
|
|
|
(with-slots (id data-dir properties) survey
|
|
|
|
(setf data-dir (uiop:merge-pathnames*
|
|
|
|
(format nil "~a/" id)
|
2024-07-09 20:23:19 +02:00
|
|
|
(ml-survey/fileops:ensure-surveys-dir)))
|
2024-06-11 18:27:17 +02:00
|
|
|
(setf properties (first (rest (assoc (parse-integer id)
|
2024-07-09 20:23:19 +02:00
|
|
|
(ml-survey/fileops:read-from-file (ml-survey/fileops:make-surveys-db-file))))))))
|
2024-06-11 18:27:17 +02:00
|
|
|
|
|
|
|
(defgeneric survey-id-p (survey)
|
|
|
|
(:documentation "Check if the survey ID is present in the surveys database."))
|
|
|
|
|
|
|
|
(defgeneric survey-data-dir-files (survey)
|
|
|
|
(:documentation "Get the list of files in the survey's data directory."))
|
|
|
|
|
|
|
|
(defgeneric survey-data-dir-p (survey)
|
|
|
|
(:documentation "Check if the survey's data directory exists."))
|
|
|
|
|
2024-06-23 19:29:39 +02:00
|
|
|
(defgeneric survey-properties-title (survey)
|
|
|
|
(:documentation "Get title property."))
|
|
|
|
|
|
|
|
(defgeneric survey-properties-description (survey)
|
|
|
|
(:documentation "Get description property."))
|
|
|
|
|
2024-06-11 18:27:17 +02:00
|
|
|
(defmethod survey-id-p ((survey survey))
|
2024-07-09 20:23:19 +02:00
|
|
|
(let ((ids (mapcar #'car (read-from-file (ml-survey/fileops:make-surveys-db-file)))))
|
2024-06-11 18:27:17 +02:00
|
|
|
(if (member (parse-integer (survey-id survey)) ids) t nil)))
|
|
|
|
|
|
|
|
(defmethod survey-data-dir-files ((survey survey))
|
|
|
|
(uiop:directory-files (survey-data-dir survey)))
|
|
|
|
|
|
|
|
(defmethod survey-data-dir-p ((survey survey))
|
|
|
|
(uiop:directory-exists-p (survey-data-dir survey)))
|
|
|
|
|
2024-06-23 13:23:14 +02:00
|
|
|
(defmethod survey-properties-title ((survey survey))
|
|
|
|
(cdr (assoc "title" (survey-properties survey) :test #'string-equal)))
|
|
|
|
|
|
|
|
(defmethod survey-properties-description ((survey survey))
|
|
|
|
(cdr (assoc "description" (survey-properties survey) :test #'string-equal)))
|
|
|
|
|
2024-06-11 18:27:17 +02:00
|
|
|
(defun build-questionnaire-link (survey-id resource)
|
2024-06-28 17:39:13 +02:00
|
|
|
(format nil "/survey/~a~a" survey-id resource))
|
2024-06-11 18:27:17 +02:00
|
|
|
|
|
|
|
(defmethod survey-html ((survey survey))
|
|
|
|
(spinneret:with-html
|
2024-06-14 17:47:54 +02:00
|
|
|
(:dl (loop for property in (survey-properties survey)
|
|
|
|
for key = (car property)
|
|
|
|
for value = (cdr property) do
|
|
|
|
(:dt key)
|
|
|
|
(cond ((string= key "questionnaire")
|
2024-06-20 23:29:47 +02:00
|
|
|
(:dd (:a :href (build-questionnaire-link (survey-id survey) value)
|
|
|
|
(format nil "Open Questionnaire ~a" value))))
|
2024-06-14 17:47:54 +02:00
|
|
|
(t (:dd value)))))))
|
2024-07-09 20:23:19 +02:00
|
|
|
|
2024-08-18 15:06:42 +02:00
|
|
|
(defun view (survey &optional assessments)
|
2024-07-09 20:23:19 +02:00
|
|
|
"Generates the view to show the survey created."
|
|
|
|
(check-type survey survey)
|
2024-08-18 15:06:42 +02:00
|
|
|
(with-page (:title "Survey Details")
|
|
|
|
(body-header t "Survey Details"
|
|
|
|
(with-navbar (:brand "ml-survey" :active-item "New Survey")
|
|
|
|
"Home" "/" "New Survey" "/new-survey"))
|
|
|
|
(body-main t
|
|
|
|
(with-section (with-title-bar "Properties")
|
|
|
|
(:p (format nil "ID: ~a" (survey-id survey)))
|
|
|
|
(survey-html survey))
|
|
|
|
|
2024-08-19 18:30:52 +02:00
|
|
|
(with-section (with-title-bar "Assesments")
|
|
|
|
(loop for assessment in assessments
|
|
|
|
when assessments
|
|
|
|
do (assessment-render-results assessment))))))
|
2024-07-10 23:30:27 +02:00
|
|
|
|
2024-07-14 20:55:12 +02:00
|
|
|
(defun nps-calc (time-data)
|
2024-07-14 12:45:04 +02:00
|
|
|
"Calculate the Net Promoter Score (NPS) from a list of SCORES."
|
2024-07-14 20:55:12 +02:00
|
|
|
(check-type time-data list)
|
2024-07-14 12:45:04 +02:00
|
|
|
(let ((promoters 0)
|
|
|
|
(detractors 0)
|
2024-07-15 21:10:54 +02:00
|
|
|
(total-responses (length time-data)))
|
|
|
|
(dolist (data (cdr time-data))
|
2024-07-14 12:45:04 +02:00
|
|
|
(cond
|
2024-07-15 21:10:54 +02:00
|
|
|
((>= data 9) (incf promoters))
|
|
|
|
((<= data 6) (incf detractors))))
|
2024-07-14 12:45:04 +02:00
|
|
|
(let ((nps (* (- (/ promoters total-responses)
|
|
|
|
(/ detractors total-responses))
|
|
|
|
100)))
|
|
|
|
nps)))
|
|
|
|
|
2024-07-09 20:23:19 +02:00
|
|
|
(defstruct questionnaire-result
|
|
|
|
type
|
2024-07-14 12:45:04 +02:00
|
|
|
name
|
2024-07-09 20:23:19 +02:00
|
|
|
timestamp
|
|
|
|
post-data)
|
|
|
|
|
2024-07-14 12:45:04 +02:00
|
|
|
(defun questionnaire-result-list-p (list)
|
|
|
|
"Check if all elements in LIST are of type 'questionnaire-result'."
|
|
|
|
(if (every 'questionnaire-result-p list) t nil))
|
|
|
|
|
|
|
|
(deftype questionnaire-result-list ()
|
|
|
|
"Define a type representing a list containing only questionnaire-result instances."
|
|
|
|
'(and list (satisfies questionnaire-result-list-p)))
|
|
|
|
|
2024-07-09 20:23:19 +02:00
|
|
|
(defun questionnaire-result-from-file (filename)
|
2024-07-14 12:45:04 +02:00
|
|
|
"Create a 'questionnaire-result' instance from data read from the file specified by FILENAME."
|
2024-07-09 20:23:19 +02:00
|
|
|
(check-type filename (or string pathname))
|
|
|
|
(let ((data (ml-survey/fileops:read-from-file filename)))
|
|
|
|
(make-questionnaire-result :type (getf data :type)
|
2024-07-14 12:45:04 +02:00
|
|
|
:name (getf data :name)
|
2024-07-09 20:23:19 +02:00
|
|
|
:timestamp (getf data :timestamp)
|
|
|
|
:post-data (getf data :post-data))))
|
|
|
|
|
2024-07-14 12:45:04 +02:00
|
|
|
(defun string->keyword (string)
|
|
|
|
(intern (string-upcase string) :keyword))
|
|
|
|
|
2024-07-09 20:23:19 +02:00
|
|
|
(defun list-of-categorized-results (result-objs)
|
2024-08-10 11:24:51 +02:00
|
|
|
"Collecting questionnaire results listed in RESULT-OBJs."
|
2024-07-14 12:45:04 +02:00
|
|
|
(declare (type questionnaire-result-list result-objs))
|
2024-08-10 11:24:51 +02:00
|
|
|
|
|
|
|
(let ((categorized-results (list)))
|
|
|
|
|
2024-07-09 20:23:19 +02:00
|
|
|
(dolist (result result-objs categorized-results)
|
2024-08-10 11:24:51 +02:00
|
|
|
|
2024-07-14 12:45:04 +02:00
|
|
|
(let ((name (string->keyword (questionnaire-result-name result)))
|
2024-07-09 20:23:19 +02:00
|
|
|
(data (questionnaire-result-post-data result))
|
|
|
|
(timestamp (questionnaire-result-timestamp result)))
|
2024-08-10 11:24:51 +02:00
|
|
|
|
|
|
|
(setf (getf categorized-results name)
|
|
|
|
(push (cons timestamp data)
|
|
|
|
(getf categorized-results name)))))))
|
2024-07-09 20:23:19 +02:00
|
|
|
|
|
|
|
(defun survey-uri-p (uri)
|
|
|
|
(let ((parts (ml-survey/app:split-uri uri)))
|
|
|
|
(and (= (length parts) 2)
|
|
|
|
(string= (first parts) "survey")
|
|
|
|
(every #'digit-char-p (second parts)))))
|
|
|
|
|
|
|
|
(defun survey-uri (request)
|
|
|
|
(survey-uri-p (hunchentoot:request-uri request)))
|
|
|
|
|
|
|
|
(define-easy-handler (survey-handler :uri #'survey-uri) ()
|
|
|
|
(let* ((s (make-instance 'survey
|
|
|
|
:id (ml-survey/app:extract-from (hunchentoot:request-uri*) :survey-id)))
|
|
|
|
(result-objs (mapcar 'questionnaire-result-from-file
|
2024-08-18 15:06:42 +02:00
|
|
|
(survey-data-dir-files s)))
|
|
|
|
(categorized-results (list-of-categorized-results result-objs))
|
|
|
|
(assessments (parse-assessment categorized-results)))
|
|
|
|
(view s assessments)))
|