Commit bc144aac authored by Bruno Burke's avatar Bruno Burke 😁

add unit-lists for calculation exercise, confirmation for options checkboxes,...

add unit-lists for calculation exercise, confirmation for options checkboxes, add info if value is not set in show
parent a357a794
......@@ -6,7 +6,7 @@
(defn select-core-keys [exercise]
(update exercise :core
#(select-keys % [:with-oom :with-units :type :task-description :calculations])))
#(select-keys % [:with-oom :with-units :type :task-description :calculations :unit-list])))
(defn shuffle-calculations [exercise]
(if (get-in exercise [:core :shuffled])
......
(ns lernmeister.components.exercise-types.calculation.views.common
(:require [lernmeister.components.exercise-types.multistep-calculation.views.common :refer [add-to-selectable-units]]))
(defn convert-unit-list [unit-list]
(reduce merge (mapv
#(add-to-selectable-units [:units] (atom {}) %)
unit-list)))
......@@ -14,6 +14,8 @@
[lernmeister.components.exercise-types.calculation.migrate
:refer
[exercise-scheme-version]]
[lernmeister.components.content-elements.core :as ce-core]
[lernmeister.components.exercise-types.calculation.views.common :refer [convert-unit-list]]
[lernmeister.components.exercise-types.calculation.views.show :as show]
[lernmeister.components.exercise-types.edit :as e-edit]
[lernmeister.components.helper :refer [set-migration-status vec-remove]]
......@@ -21,6 +23,7 @@
[lernmeister.components.modal-editor.core :refer [modal-editor]]
[lernmeister.components.modal-editor.renderers :as modal-renderers]
[lernmeister.components.ui :as ui]
[clj-helper.vector :as vhelper]
[reagent.core :as reagent]))
(def modal-state-atom (reagent/atom {}))
......@@ -105,7 +108,13 @@
(let [exercise @exercise-atom
exercise-core (:core exercise)
with-units (:with-units exercise-core)
calculations (:calculations exercise-core)]
calculations (:calculations exercise-core)
options (ce-core/receive-options (:core @exercise-atom))
unit-list-loader (:load-unit-list options)
selected-unit-list (:unit-list exercise-core)
selectable-units (when (and with-units
selected-unit-list)
(convert-unit-list (unit-list-loader selected-unit-list)))]
[:<>
[ui/card
{:title "Berechnungen"
......@@ -139,14 +148,16 @@
:type (:type @modal-state-atom)
:with-oom (:with-oom @modal-state-atom)
:with-units (:with-units @modal-state-atom)
:collats-fn (:collats-fn @modal-state-atom)}]]]]))
:collats-fn (:collats-fn @modal-state-atom)
:used-units selectable-units}]]]]))
(defn new-exercise-options [{:keys [exercise change-fn]}]
(defn new-exercise-options [{:keys [exercise options change-fn]}]
(let [exercise-core (:core exercise)
number-of-calculations (count (:calculations exercise-core))
shuffled? (boolean (:shuffled exercise-core))
with-oom? (boolean (:with-oom exercise-core))
with-units? (boolean (:with-units exercise-core))]
with-units? (boolean (:with-units exercise-core))
selected-unit-list (get-in exercise [:core :unit-list])]
[:div.row
[:div.col.s12 [:p [:b "Optionen:"]]]
[:div.col.s3
......@@ -156,28 +167,53 @@
:label "Zufällige Reihenfolge"}]]
[:div.col.s3
[ui/checkbox
{:on-change #(change-fn [:core :with-oom] (not with-oom?)
(when with-oom?
(get-collats-for-vec [:core :calculations] (range number-of-calculations)
{[:number :order-of-magnitude] "0"})))
{:on-change #(let [confirmed? (if with-oom?
(js/confirm "Beachten Sie, dass bisher eingetragene Exponenten entfernt werden.")
true)]
(when confirmed?
(change-fn [:core :with-oom] (not with-oom?)
(when with-oom?
(get-collats-for-vec [:core :calculations] (range number-of-calculations)
{[:number :order-of-magnitude] "0"})))))
:checked? with-oom?
:label "Exponentialdarstellung"}]]
[:div.col.s3
[ui/checkbox
{:on-change #(change-fn [:core :with-units] (not with-units?)
(get-collats-for-vec [:core :calculations] (range number-of-calculations)
(if with-units? [:units] {:units []})))
{:on-change #(let [confirmed? (if with-units?
(js/confirm "Beachten Sie, dass bisher eingetragene Einheiten entfernt werden.")
true)]
(when confirmed?
(change-fn [:core :with-units] (not with-units?)
(get-collats-for-vec [:core :calculations] (range number-of-calculations)
(if with-units? [:units] {:units []})))))
:checked? with-units?
:label "Maßeinheiten"}]]]))
:label "Maßeinheiten"}]]
(when-let [unit-lists (and with-units?
(:available-unit-lists options))]
[:div
[ui/field {:label "Vorgegebene Einheitenliste"}
[ui/select {:value (:id selected-unit-list)
:options (conj (mapv #(assoc % :key (:id %))
unit-lists)
{:key :none :label " Keine Einheitenliste"})
:on-change #(if-not (= % :none)
(change-fn [:core :unit-list]
(vhelper/get-by (:available-unit-lists options) :id (name %)))
(change-fn [:core :unit-list]
nil))}]]])
]))
(defn settings [_]
(reagent/create-class
{:display-name "calculation-settings"
:reagent-render
(fn [exercise-atom]
(let [migration-status (get-in @exercise-atom [:authoring :migration-status])]
(let [migration-status (get-in @exercise-atom [:authoring :migration-status])
options (ce-core/receive-options (:core @exercise-atom))]
(if (= :current migration-status)
[new-exercise-options {:exercise @exercise-atom
:options options
:change-fn (partial calc-change-fn
(fn [exercise] (reset! exercise-atom exercise))
@exercise-atom)}]
......
......@@ -26,6 +26,8 @@
[lernmeister.components.helper :refer [migrate-if-necessary]]
[lernmeister.components.modal-editor.core :refer [modal-editor]]
[lernmeister.components.modal-editor.renderers :as modal-renderers]
[lernmeister.components.exercise-types.calculation.views.common :refer [convert-unit-list]]
[lernmeister.components.content-elements.core :as ce-core]
[lernmeister.components.ui :as ui]
[reagent.core :as reagent]))
......@@ -59,7 +61,11 @@
:style {:width "0.5rem" :align-self "stretch"}}]
[:div.column
(if do-render
[:div.is-inline-block.mr-1 [latex-span name] " = " [latex-span latex-str]]
[:div.is-inline-block.mr-1 [latex-span name] " = "
(if (empty? latex-str)
[:span.has-text-grey-light
"Bitte Wert eingeben."]
[latex-span latex-str])]
[:div.is-inline-block.mr-1 name " = " latex-str])
(when result
(if (contains? (:correct-calculations result) id)
......@@ -72,24 +78,24 @@
[:a.button
{:disabled result
:on-click (fn [] (reset! modal-state-atom
{:modal-visible? true
:path path
:with-oom (:with-oom exercise)
:with-units with-units
:type :number-only
:symbol-data {:sign name :subtext ""}}))}
{:modal-visible? true
:path path
:with-oom (:with-oom exercise)
:with-units with-units
:type :number-only
:symbol-data {:sign name :subtext ""}}))}
[:span.icon [:i.fas.fa-edit]]
[:span "Wert"]]]
(when with-units
[:div.column.is-half
[:a.button {:disabled result
:on-click (fn [] (reset! modal-state-atom
{:modal-visible? true
:path path
:with-oom (:with-oom exercise)
:with-units with-units
:type :units-only
:symbol-data {:sign name :subtext ""}}))}
{:modal-visible? true
:path path
:with-oom (:with-oom exercise)
:with-units with-units
:type :units-only
:symbol-data {:sign name :subtext ""}}))}
[:span.icon [:i.fas.fa-edit]]
[:span "Einheit"]]])]])]]))
......@@ -111,7 +117,7 @@
calculations)]
[:span "No calculations available"])))
(defn print-calculation-exercise [{:keys [answer exercise change-fn result]}]
(defn print-calculation-exercise [{:keys [answer exercise change-fn result options]}]
[:<>
[ui/card
{:title (str (:title exercise))}
......@@ -125,7 +131,6 @@
(when result
[:div.has-text-right {:class (get-result-color-class result)}
[point-result {:result result}]])]]
[ui/modal-panel
{:state (reagent/cursor modal-state-atom [:modal-visible?])
:title [modal-renderers/editor-heading {:text "Größe editieren"
......@@ -135,25 +140,34 @@
(swap! modal-state-atom update :modal-visible? not))}
"Schließen"]}
[:div
[modal-editor {:subject answer
:change-fn change-fn
:visible? (:modal-visible? @modal-state-atom)
:path (:path @modal-state-atom)
:type (:type @modal-state-atom)
:with-oom (:with-oom @modal-state-atom)
:with-units (:with-units @modal-state-atom)
:collats-fn (:collats-fn @modal-state-atom)}]]]])
(let [unit-list-loader (:load-unit-list options)
selected-unit-list (:unit-list (:core exercise))
selectable-units (when (and
(:with-units @modal-state-atom)
selected-unit-list)
(convert-unit-list (unit-list-loader selected-unit-list)))]
[modal-editor {:subject answer
:change-fn change-fn
:visible? (:modal-visible? @modal-state-atom)
:path (:path @modal-state-atom)
:type (:type @modal-state-atom)
:with-oom (:with-oom @modal-state-atom)
:with-units (:with-units @modal-state-atom)
:collats-fn (:collats-fn @modal-state-atom)
:used-units selectable-units}])]]])
(defn exercise-renderer [exercise {:keys [result answer on-change]}]
(let [calculation-ids (get-calculation-ids exercise)
ans-obj (or answer (fresh-answer-obj calculation-ids (get-in exercise [:core :with-units])))]
ans-obj (or answer (fresh-answer-obj calculation-ids (get-in exercise [:core :with-units])))
options (ce-core/receive-options (:core exercise))]
(if-let [migrated-answer (migrate-if-necessary ans-obj :answer-scheme "calculation" answer-scheme-version
(partial migrate-answer calculation-ids))]
[print-calculation-exercise
{:answer migrated-answer
:exercise exercise
:change-fn (partial calc-change-fn on-change migrated-answer)
:result result}]
:result result
:options options}]
[unmigrateable-answer])))
(defmethod ce-ex/render-exercise :calculation [_ _]
......
......@@ -3,11 +3,36 @@
[lernmeister.components.exercise-editor.general :as ee-general]
[lernmeister.components.exercise-types.core :refer [explain-exercise]]
[lernmeister.components.sample-data :as data]
[lernmeister.components.content-elements.core :as ce-core]
[reagent.core :as reagent]))
(defonce exercise-data
data/calc-exercise)
(defmethod ce-core/receive-options :calculation [element]
{:available-unit-lists [{:id "a" :title "Super Liste" :type :atom}
{:id "b" :title "Yocto Liste" :type :atom}]
:load-unit-list (fn [unit-list]
(js/console.log "Load Unit-List type " (:type unit-list))
(case (:id unit-list)
"a" [{:units
[{:prefix "M", :unit "g", :expo "1"}
{:prefix "K", :unit "A", :expo "23"}]}
{:units [{:prefix "k", :unit "g", :expo "1"}]}
{:units [{:prefix "G", :unit "°", :expo "5"}]}
{:units [{:prefix "P", :unit "B", :expo "5"}]}]
"b" [{:units
[{:prefix "y", :unit "g", :expo "1"}
{:prefix "y", :unit "A", :expo "23"}]}
{:units [{:prefix "y", :unit "g", :expo "1"}]}
{:units [{:prefix "y", :unit "°", :expo "5"}]}
{:units [{:prefix "y", :unit "B", :expo "5"}]}]
[]
))})
(defonce state (reagent/atom {:exercise-data @exercise-data :answer nil}))
(add-watch state :watch-exercise-core
......@@ -27,7 +52,9 @@
[:div
[ee-general/exercise-dialog
:exercise (reagent/cursor state [:exercise-data])
:answer (reagent/cursor state [:answer])]
:answer (reagent/cursor state [:answer])
:options {:a 5454}
]
[spec-check (:exercise-data @state)]])
state
{:inspect-data true
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment