Ab sofort ist der Login auf der Weboberfläche von git.fh-muenster.de bevorzugt über FH Muenster SSO möglich.

Commit 55c756ce authored by Bruno Burke's avatar Bruno Burke 😁

Merge branch 'master' into bulma-css

parents 28a4cc33 46ff0ce5
Pipeline #44988 passed with stages
in 1 minute and 53 seconds
......@@ -26,69 +26,84 @@
"" 0
nil 0})
(def unit-mapping {"m" [{:factor 1 :oom 0 :unit "m" :expo 1}]
"g" [{:factor 1 :oom 0 :unit "g" :expo 1}]
"s" [{:factor 1 :oom 0 :unit "s" :expo 1}]
"A" [{:factor 1 :oom 0 :unit "A" :expo 1}]
"K" [{:factor 1 :oom 0 :unit "K" :expo 1}]
"mol" [{:factor 1 :oom 0 :unit "mol" :expo 1}]
"cd" [{:factor 1 :oom 0 :unit "cd" :expo 1}]
"rad" []
"sr" []
"Hz" [{:factor 1 :oom 0 :unit "s" :expo -1}]
"N" [{:factor 1 :oom 0 :unit "m" :expo 1}
{:factor 1 :oom 3 :unit "g" :expo 1}
{:factor 1 :oom 0 :unit "s" :expo -2}]
"Pa" [{:factor 1 :oom 0 :unit "m" :expo -1}
{:factor 1 :oom 3 :unit "g" :expo 1}
{:factor 1 :oom 0 :unit "s" :expo -2}]
"J" [{:factor 1 :oom 0 :unit "m" :expo 2}
{:factor 1 :oom 3 :unit "g" :expo 1}
{:factor 1 :oom 0 :unit "s" :expo -2}]
"W" [{:factor 1 :oom 0 :unit "m" :expo 2}
{:factor 1 :oom 3 :unit "g" :expo 1}
{:factor 1 :oom 0 :unit "s" :expo -3}]
"C" [{:factor 1 :oom 0 :unit "s" :expo 1}
{:factor 1 :oom 0 :unit "A" :expo 1}]
"V" [{:factor 1 :oom 0 :unit "m" :expo 2}
{:factor 1 :oom 3 :unit "g" :expo 1}
{:factor 1 :oom 0 :unit "s" :expo -3}
{:factor 1 :oom 0 :unit "A" :expo -1}]
"F" [{:factor 1 :oom 0 :unit "m" :expo -2}
{:factor 1 :oom 3 :unit "g" :expo -1}
{:factor 1 :oom 0 :unit "s" :expo 4}
{:factor 1 :oom 0 :unit "A" :expo 2}]
"Ω" [{:factor 1 :oom 0 :unit "m" :expo 2}
{:factor 1 :oom 3 :unit "g" :expo 1}
{:factor 1 :oom 0 :unit "s" :expo -3}
{:factor 1 :oom 0 :unit "A" :expo -2}]
"S" [{:factor 1 :oom 0 :unit "m" :expo -2}
{:factor 1 :oom 3 :unit "g" :expo -1}
{:factor 1 :oom 0 :unit "s" :expo 3}
{:factor 1 :oom 0 :unit "A" :expo 2}]
"Wb" [{:factor 1 :oom 0 :unit "m" :expo 2}
{:factor 1 :oom 3 :unit "g" :expo 1}
{:factor 1 :oom 0 :unit "s" :expo -2}
{:factor 1 :oom 0 :unit "A" :expo -2}]
"T" [{:factor 1 :oom 3 :unit "g" :expo 1}
{:factor 1 :oom 0 :unit "s" :expo -2}
{:factor 1 :oom 0 :unit "A" :expo -1}]
"H" [{:factor 1 :oom 0 :unit "m" :expo 2}
{:factor 1 :oom 3 :unit "g" :expo 1}
{:factor 1 :oom 0 :unit "s" :expo -2}
{:factor 1 :oom 0 :unit "A" :expo -2}]
"lm" [{:factor 1 :oom 0 :unit "cd" :expo 1}]
"lx" [{:factor 1 :oom 0 :unit "m" :expo -2}
{:factor 1 :oom 0 :unit "cd" :expo 1}]
"Bq" [{:factor 1 :oom 0 :unit "s" :expo -1}]
"Gy" [{:factor 1 :oom 0 :unit "m" :expo 2}
{:factor 1 :oom 0 :unit "s" :expo -2}]
"Sv" [{:factor 1 :oom 0 :unit "m" :expo 2}
{:factor 1 :oom 0 :unit "s" :expo -2}]
"kat" [{:factor 1 :oom 0 :unit "s" :expo -1}
{:factor 1 :oom 0 :unit "mol" :expo 1}]
"h" [{:factor 3.6 :oom 3 :unit "s" :expo 1}]
"l" [{:factor 1 :oom -1 :unit "m" :expo 3}]})
(def si-unit-mapping {"m" [{:factor 1 :oom 0 :unit "m" :expo 1}]
"g" [{:factor 1 :oom -3 :unit "kg" :expo 1}]
"s" [{:factor 1 :oom 0 :unit "s" :expo 1}]
"A" [{:factor 1 :oom 0 :unit "A" :expo 1}]
"K" [{:factor 1 :oom 0 :unit "K" :expo 1}]
"mol" [{:factor 1 :oom 0 :unit "mol" :expo 1}]
"cd" [{:factor 1 :oom 0 :unit "cd" :expo 1}]
"rad" []
"sr" []
"Hz" [{:factor 1 :oom 0 :unit "s" :expo -1}]
"N" [{:factor 1 :oom 0 :unit "m" :expo 1}
{:factor 1 :oom 0 :unit "kg" :expo 1}
{:factor 1 :oom 0 :unit "s" :expo -2}]
"Pa" [{:factor 1 :oom 0 :unit "m" :expo -1}
{:factor 1 :oom 0 :unit "kg" :expo 1}
{:factor 1 :oom 0 :unit "s" :expo -2}]
"J" [{:factor 1 :oom 0 :unit "m" :expo 2}
{:factor 1 :oom 0 :unit "kg" :expo 1}
{:factor 1 :oom 0 :unit "s" :expo -2}]
"W" [{:factor 1 :oom 0 :unit "m" :expo 2}
{:factor 1 :oom 0 :unit "kg" :expo 1}
{:factor 1 :oom 0 :unit "s" :expo -3}]
"C" [{:factor 1 :oom 0 :unit "s" :expo 1}
{:factor 1 :oom 0 :unit "A" :expo 1}]
"V" [{:factor 1 :oom 0 :unit "m" :expo 2}
{:factor 1 :oom 0 :unit "kg" :expo 1}
{:factor 1 :oom 0 :unit "s" :expo -3}
{:factor 1 :oom 0 :unit "A" :expo -1}]
"F" [{:factor 1 :oom 0 :unit "m" :expo -2}
{:factor 1 :oom 0 :unit "kg" :expo -1}
{:factor 1 :oom 0 :unit "s" :expo 4}
{:factor 1 :oom 0 :unit "A" :expo 2}]
"Ω" [{:factor 1 :oom 0 :unit "m" :expo 2}
{:factor 1 :oom 0 :unit "kg" :expo 1}
{:factor 1 :oom 0 :unit "s" :expo -3}
{:factor 1 :oom 0 :unit "A" :expo -2}]
"S" [{:factor 1 :oom 0 :unit "m" :expo -2}
{:factor 1 :oom 0 :unit "kg" :expo -1}
{:factor 1 :oom 0 :unit "s" :expo 3}
{:factor 1 :oom 0 :unit "A" :expo 2}]
"Wb" [{:factor 1 :oom 0 :unit "m" :expo 2}
{:factor 1 :oom 0 :unit "kg" :expo 1}
{:factor 1 :oom 0 :unit "s" :expo -2}
{:factor 1 :oom 0 :unit "A" :expo -2}]
"T" [{:factor 1 :oom 0 :unit "kg" :expo 1}
{:factor 1 :oom 0 :unit "s" :expo -2}
{:factor 1 :oom 0 :unit "A" :expo -1}]
"H" [{:factor 1 :oom 0 :unit "m" :expo 2}
{:factor 1 :oom 0 :unit "kg" :expo 1}
{:factor 1 :oom 0 :unit "s" :expo -2}
{:factor 1 :oom 0 :unit "A" :expo -2}]
"lm" [{:factor 1 :oom 0 :unit "cd" :expo 1}]
"lx" [{:factor 1 :oom 0 :unit "m" :expo -2}
{:factor 1 :oom 0 :unit "cd" :expo 1}]
"Bq" [{:factor 1 :oom 0 :unit "s" :expo -1}]
"Gy" [{:factor 1 :oom 0 :unit "m" :expo 2}
{:factor 1 :oom 0 :unit "s" :expo -2}]
"Sv" [{:factor 1 :oom 0 :unit "m" :expo 2}
{:factor 1 :oom 0 :unit "s" :expo -2}]
"kat" [{:factor 1 :oom 0 :unit "s" :expo -1}
{:factor 1 :oom 0 :unit "mol" :expo 1}]
"h" [{:factor 3.6 :oom 3 :unit "s" :expo 1}]
"d" [{:factor 8.64 :oom 4 :unit "s" :expo 1}]
"l" [{:factor 1 :oom -1 :unit "m" :expo 3}]})
(def additional-unit-mappings {"B" [{:factor 1 :oom 1 :unit "dB" :expo 1}]
"°" [{:factor 1 :oom 0 :unit "°" :expo 1}]})
(def base-unit-set #{"m" "kg" "s" "A" "K" "mol" "cd" "dB" "°"})
(def unit-replace-map {"kg" {:unit "g" :prefix "k"}
"dB" {:unit "B" :prefix "d"}})
(def unit-replace-set (set (keys unit-replace-map)))
(def base-unit-map (reduce (fn [res-map base-unit] (assoc res-map (keyword base-unit) 0)) {} base-unit-set))
(def unit-mapping (merge si-unit-mapping additional-unit-mappings))
(def prefix-set (set (keys prefix-mapping)))
......@@ -127,7 +142,7 @@
(get unit-mapping (:unit unit))))))
(defn reduce-units [units]
(let [result-map {:m 0 :g 0 :s 0 :A 0 :K 0 :mol 0 :cd 0 :factor 1 :oom 0}]
(let [result-map (merge base-unit-map {:factor 1 :oom 0})]
(reduce reduce-unit-mapping
result-map
units)))
......@@ -135,7 +150,8 @@
(defn get-units-part [units with-units target result]
(if with-units
(let [reduced-units (reduce-units units)
base-units (select-keys reduced-units [:m :g :s :A :K :mol :cd])
base-units (select-keys reduced-units
(reduce (fn [res base-unit] (conj res (keyword base-unit))) [] base-unit-set))
units-factor (:factor reduced-units)
units-oom (:oom reduced-units)]
(update result target merge {:base-units base-units :units-factor units-factor :units-oom units-oom}))
......
......@@ -6,10 +6,9 @@
[reagent.dom :as rdom]
[lernmeister.components.helper :refer [vec-remove]]))
(defn parse-unit [string]
(when (string? string)
(when-let [matched-string (re-matches #"^[a-zA-z]*" string)]
(when-let [matched-string (re-matches #"^[a-zA-ZΩ°]*" string)]
(replace (subs matched-string 0 3) #"Ohm|ohm" "Ω"))))
(defn parse-prefix [string]
......@@ -35,7 +34,6 @@
(defn append-to-string-vec [arg append-string]
(update arg :string-vec conj append-string))
(defn add-number [arg]
(let [number (:number arg)]
(when (not-empty-or-minus number)
......@@ -59,36 +57,47 @@
(append-to-string-vec arg (build-oom-string oom number))))
(append-to-string-vec arg ""))))
(defn build-units-latex-string [unit-list]
(if ((complement empty?) unit-list)
(letfn [(append-unit-str [frac-map position unit expo]
(update frac-map position conj (if (= "1" expo) unit (str unit "^{" expo "}"))))
(join-with-sep [fraction-map map-key]
(join "{\\cdot}" (map-key fraction-map)))]
(let [fraction-map (reduce
(fn [frac-map unit-map]
(let [prefix (replace (:prefix unit-map) "µ" "\\mu ")
unit (replace (:unit unit-map) "Ω" "\\Omega ")
combined-unit (str prefix unit)
expo (:expo unit-map)]
(if (= "-" (first expo))
(append-unit-str frac-map :denominator combined-unit (subs expo 1))
(append-unit-str frac-map :numerator combined-unit expo))))
{:numerator [] :denominator []} unit-list)
numerator-str (join-with-sep fraction-map :numerator)
num-str-mod (if (empty? numerator-str) "1" numerator-str)
denominator-str (join-with-sep fraction-map :denominator)]
(if (empty? denominator-str)
numerator-str
(str "\\dfrac{" num-str-mod "}{" denominator-str "}"))))
""))
(defn append-unit-str [frac-map position unit expo]
(update frac-map position conj (if (= "1" expo) unit (str unit "^{" expo "}"))))
(defn join-with-sep [fraction-map map-key]
(join "{\\cdot}" (map-key fraction-map)))
(defn latex-replace-unit [unit]
(-> unit
(replace "Ω" "\\Omega ")
(replace "°" "\\degree ")))
(defn build-fraction-map [unit-list]
(reduce
(fn [res-map unit-map]
(let [prefix (replace (:prefix unit-map) "µ" "\\mu ")
unit (latex-replace-unit (:unit unit-map))
combined-unit (str prefix unit)
expo (:expo unit-map)]
(if (= "-" (first expo))
(append-unit-str res-map :denominator combined-unit (subs expo 1))
(append-unit-str res-map :numerator combined-unit expo))))
{:numerator [] :denominator []} unit-list))
(defn add-units [arg]
(if (every-pred :with-units)
(append-to-string-vec arg (build-units-latex-string (:units arg)))
(if ((every-pred :with-units #(not-empty (:units %))) arg)
(let [fraction-map (build-fraction-map (:units arg))
numerator-str (join-with-sep fraction-map :numerator)
num-str-mod (if (empty? numerator-str) "1" numerator-str)
denominator-str (join-with-sep fraction-map :denominator)]
(if (empty? denominator-str)
(append-to-string-vec arg numerator-str)
(append-to-string-vec arg (str "\\dfrac{" num-str-mod "}{" denominator-str "}"))))
(append-to-string-vec arg "")))
(defn build-units-latex-string [arg]
(if (and (vector? arg) (not-empty arg))
(-> (add-units (assoc {:with-units true :string-vec []} :units arg))
:string-vec
first)
""))
(defn join-string-vec [arg]
(let [string-vec (:string-vec arg)
reduced-vec [(join (subvec string-vec 0 2)) (last string-vec)]
......@@ -126,8 +135,6 @@
:component-did-update
#(katex-render %)})))
(defn input-text-inline [answer & {:keys [value width label class disabled on-change]}]
(fn [answer & {:keys [value width label class disabled on-change]}]
[:div.input-field.inline
......@@ -138,4 +145,3 @@
:disabled disabled
:on-change #(on-change answer (-> % .-target .-value))}]
[:label.active label]]))
......@@ -8,33 +8,41 @@
[clojure.string :refer [trim replace join]]
[lernmeister.components.helper :refer [vec-remove]]
[lernmeister.components.exercise-types.calculation.check
:refer [prefix-mapping prefix-set unit-set not-empty-or-minus]]
:refer [prefix-mapping prefix-set unit-set not-empty-or-minus unit-replace-map unit-replace-set]]
[lernmeister.components.exercise-types.calculation.views.helper :refer [input-text-inline parse-int
latex-span
build-units-latex-string
parse-prefix parse-unit]]))
(defn update-units [units & {:keys [prefix unit expo]}]
(let [units-new (conj units {:prefix prefix :unit unit :expo expo})]
(if-let [index (first (keep-indexed (fn [idx unit-map] (when (= unit (:unit unit-map)) idx)) units))]
(vec-remove units-new index)
units-new)))
(defn check-and-replace-expo [unit-map]
(let [expo (:expo unit-map)]
(when (not (#{"-" "0" "-0"} expo))
(if (contains? #{nil ""} expo) (assoc unit-map :expo "1") unit-map))))
(defn check-and-replace-unit [unit-map]
(when (not (and (unit-replace-set (:unit unit-map)) (not-empty (:prefix unit-map))))
(let [replaced-map (if-let [unit-mapping (get unit-replace-map (:unit unit-map))]
(-> unit-map
(assoc :unit (:unit unit-mapping))
(assoc :prefix (:prefix unit-mapping)))
unit-map)]
(when (unit-set (:unit replaced-map)) replaced-map))))
(defn check-unit [{prefix :prefix unit :unit expo :expo}]
(when (and ((complement contains?) #{"-" "0" "-0"} expo)
(not (and (= "kg" unit) ((complement empty?) prefix))))
(let [replaced-expo (if (contains? #{nil ""} expo) "1" expo)
replaced-unit (if (= "kg" unit) "g" unit)
replaced-prefix (if (= "kg" unit) "k" (or prefix ""))]
(if (contains? unit-set replaced-unit)
{:prefix replaced-prefix :unit replaced-unit :expo replaced-expo}
nil))))
(defn check-and-replace-prefix [unit-map]
(let [replaced-map (assoc unit-map :prefix (or (:prefix unit-map) ""))]
(when (prefix-set (:prefix replaced-map)) replaced-map)))
(defn check-unit [unit-map]
(some-> unit-map
check-and-replace-expo
check-and-replace-unit
check-and-replace-prefix))
(defn unit-chip [& {:keys [:prefix :unit :expo :answer :on-change]}]
(fn [& {:keys [:prefix :unit :expo :answer :on-change-fn]}]
......@@ -43,11 +51,10 @@
(when-not (= "1" expo) [:sup expo])
[:i.material-icons.red-text {:on-click #(on-change answer)} "close"]]))
(defn units-builder [id & {:keys [answer on-change-fn]}]
(let [cur-unit (reagent/atom {:prefix nil :unit nil :expo nil})
prefixes (sort-by second > (remove (comp nil? first) prefix-mapping))
unit-options (apply (into sorted-set) unit-set)
unit-options (apply (into sorted-set) (conj unit-set "Ohm" "kg" "dB"))
add-unit? (reagent/atom nil)]
(fn [id & {:keys [answer on-change-fn]}]
(let [units (get-in answer [id :units])]
......@@ -55,11 +62,10 @@
[:div.row
[:div.col.s12
[:div.section
[:b "Einheit: "]
(if (pos? (count units))
[latex-span (build-units-latex-string units)]
[:p "Bitte definieren Sie mindestens einen Einheitenbestandteil"]
)]]
[:b "Einheit: "]
(if (pos? (count units))
[latex-span (build-units-latex-string units)]
[:p "Bitte definieren Sie mindestens einen Einheitenbestandteil"])]]
[:div.col.s12
[:div.section
[:b "Bestandtteile:"]
......@@ -78,13 +84,13 @@
:answer answer
:on-change (fn [answer]
(on-change-fn (update-in answer [id :units] #(vec-remove % index))))])) units))
#_(when-not @add-unit?
[:div.col.s12. [:a.btn.btn-small {:on-click #(reset! add-unit? true)} "+ Einheit"]])
(when (:invalid @cur-unit)
[:div.col.s12
[ui/icon "warning" :class "orange-text"]
[:span " Ungültige Einheit"]])]
[:div
#_(when-not @add-unit?
[:div.col.s12. [:a.btn.btn-small {:on-click #(reset! add-unit? true)} "+ Einheit"]])
(when (:invalid @cur-unit)
[:div.col.s12
[ui/icon "warning" :class "orange-text"]
[:span " Ungültige Einheit"]])]
[:div
[:div.row
[:div.col.s4.expo-padding
[ui/select
......@@ -153,8 +159,6 @@
[:span
"Brüche können mit negativem Exponent beschrieben werden." [:br]]]]]]]]]))))
(defn calculation-unit-input [exercise id & {:keys [answer on-change-fn]}]
(fn [exercise id & {:keys [answer on-change-fn]}]
(when (:with-units exercise)
......
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