Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | 6x 146x 1x 145x 45x 38x 38x 38x 38x 38x 38x 38x 38x 4x 4x 34x 34x 34x 34x 38x 38x 3x 118x 118x 118x 107x 11x 118x 118x 22x 41x 41x 41x 41x 18x 18x 3x 3x 3x 24x 140x 31x 4x 3x 1x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 107x 15x 15x 74x 13x 13x 13x 11x 10x 10x 7x 10x 11x 2x 2x 106x 1x 1x 1x | /**
* @flow
* @prettier
*
* Part of Pleiar.no - a collection of tools for nurses
*
* Copyright (C) Fagforbundet 2023
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import cloneDeep from "lodash/cloneDeep";
import shuffle from "lodash/shuffle";
import { asyncLoaderRole } from "./helper.asyncLoader";
import type {
endSlideContent,
quizManagerSingleAnswer,
quizManagerQuestionContainer,
quizRootData,
QuizDataContainer,
} from "./types/data";
type listenerCallback = (quizManagerHandler) => void;
// eslint-disable-next-line flowtype/require-exact-type
type QuizDataManagerType = {
_data: ?QuizDataContainer,
data: () => QuizDataContainer,
onInitialize: (() => mixed) => mixed,
initialize: () => Promise<null>,
hasInitialized: () => boolean,
};
/**
* MedSearcher is the search wrapper for the medication synonyms data. This is
* different from PleiarSearcher. PleiarSearcher uses lunr to index everything
* on the site, except for medications. *This* class uses a fairly dumb
* method of searching through the medication list.
*/
const quizDataManager: QuizDataManagerType = {
...asyncLoaderRole,
/** The data structure, a QuizDataContainer*/
_data: null,
/** Retrieve the quiz data */
data(): QuizDataContainer {
if (
quizDataManager._data === null ||
quizDataManager._data === undefined
) {
throw "Attempt to access quiz data before it has been loaded";
}
return quizDataManager._data;
},
/**
* _performAsyncImport method for {@link asyncLoaderRole}
*/
_performAsyncImport(): Promise<null> {
return import(
/* webpackChunkName: "elearn" */ "../data/quiz/quiz.json"
);
},
/**
* _loadedData method for {@link asyncLoaderRole}
*/
_loadedData(data: QuizDataContainer) {
quizDataManager._data = data;
},
};
/**
* Quiz manager. All of the logic for a quiz.
*/
class quizManagerHandler {
points: number = 0;
questionList: Array<quizManagerQuestionContainer>;
_currentQuestions: Array<quizManagerQuestionContainer>;
_invalidList: Array<quizManagerQuestion> = [];
_listeners: Array<listenerCallback> = [];
_quiz: ?quizRootData = null;
current: quizManagerQuestion | null;
currentID: number = 0;
totalQuestions: number = 0;
exists: boolean;
title: string = "";
// eslint-disable-next-line require-jsdoc
constructor(quiz: string) {
if (quizDataManager.data()[quiz] === undefined) {
this.exists = false;
this.questionList = [];
} else {
this.questionList = quizDataManager.data()[quiz].quiz;
this._quiz = quizDataManager.data()[quiz];
this.title = quizDataManager.data()[quiz].title;
this.exists = true;
}
this.totalQuestions = this.questionList.length;
this.reset();
}
/**
* Returns the number of possible points for a quiz
*/
possiblePoints(): number {
return this.questionList.length;
}
/**
* Returns the next quizManagerQuestion or null if there are no more questions.
*/
next(): quizManagerQuestion | null {
this.currentID++;
const entry = this._currentQuestions.shift();
if (entry !== undefined) {
this.current = new quizManagerQuestion(this, entry, this.currentID);
} else {
this.current = null;
}
this.notifyListeners();
return this.current;
}
/**
* Checks if the current quiz is done or not
*/
isDone(): boolean {
return this.current === null;
}
/**
* Resets the quiz, restarting from the beginning
*/
reset() {
this._currentQuestions = cloneDeep(this.questionList);
this.currentID = 0;
this.points = 0;
this.next();
}
/**
* Registers a correct answer (to the supplied quizManagerQuestion)
*/
registerCorrect(): number {
this.notifyListeners();
return this.points++;
}
/**
* Registers an incorrect answer (to the supplied quizManagerQuestion)
*/
registerIncorrect(question: quizManagerQuestion): number {
this._invalidList.push(question);
this.notifyListeners();
return this.points;
}
/**
* Adds a listener for change events
*/
listen(cb: listenerCallback) {
this._listeners.push(cb);
}
/**
* Calls all change event listeners
*/
notifyListeners() {
for (const cb of this._listeners) {
cb(this);
}
}
/**
* Retrieves the final slide content, or null
*/
getEndSlide(): endSlideContent | null {
if (
this._quiz?.endSlideContent !== undefined &&
this._quiz?.endSlideContent !== null
) {
return this._quiz.endSlideContent;
}
return null;
}
}
/**
* A single quizManager question
*/
class quizManagerQuestion {
question: string;
possibleAnswers: Array<quizManagerSingleAnswer>;
handler: quizManagerHandler;
markdown: ?string;
splashSlide: boolean;
correct: boolean = false;
attemptedAnswers: { [string]: boolean } = {};
numberOfCorrectAnswersRequired: number = 1;
_numberOfCorrectAnswersDone: number = 0;
questionNumber: number;
totalQuestions: number;
// eslint-disable-next-line require-jsdoc
constructor(
handler: quizManagerHandler,
question: quizManagerQuestionContainer,
questionNumber: number,
) {
this.handler = handler;
this.question = question.question;
this.markdown = question.markdown;
this.splashSlide = question.splashSlide === true;
this.possibleAnswers = shuffle(question.answers);
this.questionNumber = questionNumber;
this.totalQuestions = handler.totalQuestions;
if (
question.numberOfCorrectAnswersRequired !== undefined &&
question.numberOfCorrectAnswersRequired !== null
) {
this.numberOfCorrectAnswersRequired =
question.numberOfCorrectAnswersRequired;
}
}
/**
* Forces stable sorting of the possibleAnswers. This should only be used
* for testing, which needs consistent input.
*/
testUseStableQuestionSorting() {
this.possibleAnswers = this.possibleAnswers.sort((a, b) =>
a.entry.localeCompare(b.entry, "no"),
);
}
/**
* Answers the question. Will notify the handler about correctness.
*/
answer(option: quizManagerSingleAnswer): boolean {
const hadAlreadyAnswered = this.hasAnswered(option);
this.attemptedAnswers[option.entry] = true;
if (this.splashSlide || option.correct === true) {
if (!hadAlreadyAnswered) {
this._numberOfCorrectAnswersDone++;
if (
this._numberOfCorrectAnswersDone >=
this.numberOfCorrectAnswersRequired
) {
this.correct = true;
}
this.handler.registerCorrect();
}
return true;
} else {
this.handler.registerIncorrect(this);
return false;
}
}
/**
* Checks if the user has tried to answer the quizManagerSingleAnswer provided
*/
hasAnswered(option: quizManagerSingleAnswer): boolean {
return this.attemptedAnswers[option.entry] === true;
}
/**
* Forces a "correct" answer to this question.
* Primarily useful to continue from a splashSlide without needing to pass
* a valid quizManagerSingleAnswer to answer().
*/
answerCorrect(): boolean {
this.correct = true;
this.handler.registerCorrect();
return true;
}
}
export default quizManagerHandler;
export { quizManagerHandler, quizManagerQuestion, quizDataManager };
|