File

src/app/models/cabri-activity.ts

Constructor

constructor(dataService: CabriDataService)

Methods

Public buildVariables
buildVariables()
Returns: void
initParamsVariables
initParamsVariables()
Returns: void
saveActivity
saveActivity()
Returns: void
restoreProblemFromParam
restoreProblemFromParam(resoParams: any)

Restore resolution probleme

Returns: void
isResolutionProblem
isResolutionProblem()
Returns: void
removeProblemFromParam
removeProblemFromParam()

Remove problem from param before saving into localstorage

Returns: void
getParam
getParam(name: string)
Returns: void
setParamValue
setParamValue(name: string, value: any)
Returns: void
paramHidden
paramHidden(name: string, items: any)
Returns: void
resetExerciceVariable
resetExerciceVariable()
Returns: void
updateVariables
updateVariables(currentExercice: Exercise, additionalVariables: any)

Calculate variables for exercice

Returns: void
restoreActivity
restoreActivity(savedActivity: any)
Returns: void

Properties

Public _params
_params: Param[]
Public className
className: string
Default value: CabriActivity
Public currentVariables
currentVariables: any
dataService
dataService: CabriDataService
Public exercicesParam
exercicesParam: any
Public file
file: string
Public hasPyramide
hasPyramide: boolean
Default value: false
Public journey
journey: Journey
Public maxPlayer
maxPlayer: number
params
params: Param[]
Public shownParamsLength
shownParamsLength: any
Public slug
slug: any
Public story
story: { exo?: ExerciceChapterHistory; chapter?: Chapter; position?: number; bilan?: boolean; journeyId?...
Private tempExerciceVariable
tempExerciceVariable: any[]
Public variables
variables: any
import { Activity } from "./activity";
import { Journey } from "./journey";
import { Param } from "./params/param";
import { Chapter } from "./chapter";
import { ExerciceChapterHistory } from "./exercice-chapter-history";
import { cloneDeep } from "lodash";
import { Exercise } from "./exercise";
import { Fraction, AppUtils } from "../app-utils";
import { CabriDataService } from "../services/cabri-data.service";
declare var window: { params: any };

export enum gameMode {
	discovery = "0",
	training = "1",
	challenge = "2",
	assessment = "3"
}

export class CabriActivity extends Activity {
	dataService: CabriDataService;
	// public currentExercice: any;

	constructor(dataService: CabriDataService = null) {
		super();
		this.dataService = dataService;
	}
	public className = "CabriActivity";
	public file: string;
	public journey?: Journey;
	public variables: any;
	public maxPlayer: number;
	public _params: Array<Param>;
	public slug;
	public shownParamsLength;
	public currentVariables;
	public hasPyramide = false;
	private tempExerciceVariable = [];
	public story: { exo?: ExerciceChapterHistory; chapter?: Chapter; position?: number; bilan?: boolean; journeyId?: string };	
	public buildVariables() {
		this.initParamsVariables();
        if (localStorage.getItem("team")) {
            const studentsLength = JSON.parse(localStorage.getItem("team")).length;
            if (studentsLength <= 1) {
                this.maxPlayer = 1;
            } else {
                this.maxPlayer = studentsLength;
            }
        } else {
            this.maxPlayer = 1;
        }
        // reset temp variable each time we build a new gabarit expect to work in all case
        this.tempExerciceVariable = [];
        this.saveActivity();
    }
    initParamsVariables() {
        this.variables = {};
        this._params.forEach(param => {
            if (param.destination === "CabriVariable") {
                this.variables[param.name] = param.value;
            }
        });
    }

	saveActivity() {
		let resoParams;
		const resoProblem = this.isResolutionProblem();
		if (resoProblem) {
			resoParams = cloneDeep(this.params);
			this.removeProblemFromParam();
		}
		//localStorage.setItem("savedActivity", JSON.stringify(this));
		const saveCabri = { ...this };
		delete saveCabri.dataService;
		localStorage.setItem("savedActivity", JSON.stringify(saveCabri));
		if (resoProblem) {
			this.restoreProblemFromParam(resoParams);
		}
	}

	/**
	 * Restore resolution probleme
	 */
	restoreProblemFromParam(resoParams) {
		if (resoParams) {
			const paramSelected = this.params.find(param => {
				return param.exercices && !param.value.probleme;
			});
			resoParams.forEach((resoParam: Param) => {
				if (resoParam.exercices) {
					if (paramSelected.value?.hasOwnProperty("probleme") && resoParam.value?.probleme) {
						paramSelected.value.probleme = resoParam.value.probleme;
					}

					if (paramSelected.defaultValue?.hasOwnProperty("probleme")) {
						paramSelected.defaultValue.probleme = resoParam.defaultValue.probleme;
					}
					resoParam.selectionList.forEach((eachResoParam, index) => {
						if (eachResoParam.probleme && paramSelected[index]) {
							paramSelected[index].probleme = eachResoParam.probleme;
						}
					});
				}
			});
		}
	}

	isResolutionProblem() {
		return this.params.some(param => {
			return (
				param.value?.probleme ||
				param.defaultValue?.probleme ||
				param.selectionList?.some(ex => {
					return ex.probleme;
				})
			);
		});
	}

	/**
	 * Remove problem from param before saving into localstorage
	 */
	removeProblemFromParam() {
		this.params.forEach((param: Param) => {
			if (param.exercices) {
				param.selectionList.forEach(ex => {
					if (ex.probleme) {
						ex.probleme = null;
					}
				});

				if (param.value?.probleme) {
					param.value.probleme = null;
				}
				if (param.defaultValue?.probleme) {
					param.defaultValue.probleme = null;
				}
			}
		});
	}

	getParam(name: string) {
		return this._params.find(param => param.name === name);
	}
	setParamValue(name: string, value: any) {
		let defaultValue;
		this._params.forEach(param => {
			if (param.name === name) {
				param.value = value;
				if (!param.exercices) {
					defaultValue = param.selectionList.find(selectionList => {
						return selectionList[1] === value;
					});

					if (defaultValue) {
						param.defaultValue = defaultValue;
					}
				} else {
					defaultValue = param.selectionList.find(ex => {
						return Number(ex.id) === Number(value.id);
					});
				}

				if (defaultValue) {
					param.defaultValue = defaultValue;
				}
			}
		});
	}

	paramHidden(name: string, items) {
		this._params.forEach(param => {
			if (param.selectionList && param.selectionList.length > 0) {
				if (param.name === name) {
					Object.entries(items).forEach((currentItem: any) => {
						//  [0] = param name to select ... [1] = hide or not
						if (currentItem[0]) {
							const value = param.selectionList.find(selectionList => {
								return selectionList[1] === currentItem[0];
							});

							if (value && typeof currentItem[1] === "boolean") {
								value[3] = currentItem[1];
							}
						}
					});
				}
			}
		});
	}

	resetExerciceVariable() {
		this.tempExerciceVariable = [];
	}

	/**
	 * Calculate variables for exercice
	 */
	// allowed letters for eval("operation")
	// tslint:disable: prefer-const (necessary for eval)
	updateVariables(currentExercice: Exercise, additionalVariables: any = null) {
		this.initParamsVariables();
		if (!this.tempExerciceVariable[currentExercice.id]) {
			this.tempExerciceVariable[currentExercice.id] = [];
		}
		let variables: any;
		variables = new Array();
		variables.operation = currentExercice.operation;
		variables.result_mode = currentExercice.resultMode;
		if (additionalVariables) {
			for (let i in additionalVariables) {
				variables[i] = additionalVariables[i];
			}
		}
		const activityVariables = JSON.parse(currentExercice.variables);

		// console.error(activityVariables);
		// allowed letters for eval("operation")
		// tslint:disable: prefer-const (necessary for eval)
		let k: number, i: number, j: number, m: number, x: number, y: number, z: number;
		let kk: number, ii: number, jj: number, mm: number, xx: number, yy: number, zz: number;
		let t: number, u: number, v: number, w: number;
		let tt: number, uu: number, vv: number, ww: number;

		// results
		let nb1: number, nb2: number, nb3: number, nb4: number, nb5: number, nb6: number, nb7: number, nb8: number, pas: number;
		let r1: number, r2: number, r3: number, r4: number;
		let numerator: number, denominator: number;
		let maxQuestions: number;
		let nbAnswers: number;
		let activity: number,
			noRoadHelp: number,
			chooseExactNumber: number,
			difficulty: number,
			operation: number,
			nbMax: number,
			nbMin: number;
		// variables droite graduée 
		let pos_debut_min : number, //position et resolution de depart
			pos_debut_max : number,
			reso_debut : number,
			pos_min : number, // positions et resolutions limites
			pos_max : number,
			res_min: number,
			res_max: number,
			etiquettes_suplementaires: string[], // etiquettes en + et en -
			etiquettes_en_moins: string[], // valeur ou :resolution ou x<:resolution<y
			type_etiquettes: string, // decimale, fraction, litherale
			type_epreuve: string, // type d epreuve placer ou reperer
			ecart_max_tolere: number, // l ecart maximal toléré a la réponse. si valeur négative, on cherche juste a voir si plus grand
			nb_tentative_max: number, // nombre de tentative autorisé
			fuel_translation: number, // la quantite de translation autorise en longueur d'affiché
			fuel_zoom: number, // la quantité de zoom autorisé en longueur d affichage, pour les deux si 0.0 alors illimité
			limite_temps:number, // nombre de seconde maximal pour repondre, 0.0 si illimité
			valeur_a_trouver:number, // la valeur a trouver
			nb_joker:number // le nombre de joker autorisé

		function variable_de_droite_gradue(variable)
		{
			return (variable==="pos_debut_min"||variable==="pos_debut_min"||variable==="pos_debut_max"||variable==="reso_debut"||variable==="pos_min"||variable==="pos_max"||variable==="res_min"||variable==="res_max"||variable==="etiquettes_suplementaires"||variable==="etiquettes_en_moins"||variable==="type_etiquettes"||variable==="type_epreuve"||variable==="ecart_max_tolere"||variable==="nb_tentative_max"||variable==="fuel_translation"||variable==="fuel_zoom"||variable==="limite_temps"||variable==="valeur_a_trouver"||variable==="nb_joker");
		}

		function evalUnit(variable, value) {
			let result;
			if(variable_de_droite_gradue(variable))
				{
					return value;
				}
			value = new String(value);
			if (value.indexOf("frac:") > -1) {
				const tmp = value.replace("frac:", "").split("|");
				return new Fraction(evalUnit("numerator", tmp[0]), evalUnit("denominator", tmp[1]));
			} else {
				if (value.indexOf("<") > 0) {
					const tmp = value.split("<");
					let min;
					let max;
					if (
						Number(currentExercice.gabarit) === 10 &&
						(variable === "nb1" || variable === "nb2" || variable === "nb3" || variable === "nb4") &&
						String(tmp[0]).trim() === "0"
					) {
						// nb1 and nb2 can't be equal to 0 in resolution de problèmes
						tmp[0] = "1";
					}
					eval("min = " + tmp[0]);
					eval("max = " + tmp[2]);
					eval(
						" " + variable + " = " + (Number(min) + 1) + " + Math.floor(Math.random()*" + (Number(max) - Number(min) - 1) + ")"
					);
				} else {
					eval(" " + variable + " = " + value);
				}
				// assign variable value

				eval("result = " + variable);
			}
			return result;
		}

		function evalArray(tempExerciceVariable: { array: any[]; frozen: string }[], variable, value) {
			if(variable_de_droite_gradue(variable))
			{
				return value;
			}
			if (!tempExerciceVariable[currentExercice.id][variable]) {
				tempExerciceVariable[currentExercice.id][variable] = { array: [], frozen: null };
			}
			let result = null;
			if (tempExerciceVariable[currentExercice.id][variable].array.length === 0) {
				tempExerciceVariable[currentExercice.id][variable].array = [...value];
			}
			result = evalUnit(variable, tempExerciceVariable[currentExercice.id][variable].array.shift());
			return result;
		}

		function evalFrozenVariable(tempExerciceVariable: { array: any[]; frozen: string }[], variable, value) {
			if (!tempExerciceVariable[currentExercice.id][variable]) {
				tempExerciceVariable[currentExercice.id][variable] = { array: [], frozen: null };
			}
			if (tempExerciceVariable[currentExercice.id][variable].frozen) {
				eval(" " + variable + " = " + tempExerciceVariable[currentExercice.id][variable].frozen);
			} else {
				tempExerciceVariable[currentExercice.id][variable].frozen = evalUnit(variable, value);
			}
			return tempExerciceVariable[currentExercice.id][variable].frozen;
		}

		if (activityVariables["#exercices"]) {
			if (!this.tempExerciceVariable[currentExercice.id]["exercices"] || this.tempExerciceVariable[currentExercice.id]["exercices"].length === 0) {
				const shuffled = [...activityVariables["#exercices"]];
				AppUtils.shuffleArray(shuffled);
				activityVariables.exercices = shuffled;
			} else {
				activityVariables.exercices = this.tempExerciceVariable[currentExercice.id]["exercices"];
			}
		}


		if (activityVariables.exercices) {
			if (!this.tempExerciceVariable[currentExercice.id]["exercices"] || this.tempExerciceVariable[currentExercice.id]["exercices"].length == 0) {
				this.tempExerciceVariable[currentExercice.id]["exercices"] = activityVariables["exercices"];
			}
			
			const nextTypePb = 
				1101 <= currentExercice.id && currentExercice.id <= 1105 ? 8 :
				1106 <= currentExercice.id && currentExercice.id <= 1110 ? 7 :
				1201 <= currentExercice.id && currentExercice.id <= 1205 ? 3 :
				1206 <= currentExercice.id && currentExercice.id <= 1210 ? 2 :
				1211 <= currentExercice.id && currentExercice.id <= 1215 ? 1 :
				1216 <= currentExercice.id && currentExercice.id <= 1220 ? 6 :
				1221 <= currentExercice.id && currentExercice.id <= 1225 ? 5 :
				1226 <= currentExercice.id && currentExercice.id <= 1230 ? 4 :
				1301 <= currentExercice.id && currentExercice.id <= 1310 ? 9 :		
				1311 <= currentExercice.id && currentExercice.id <= 1320 ? 10 :
				1401 <= currentExercice.id && currentExercice.id <= 1410 ? 11 :
				701 <= currentExercice.id && currentExercice.id <= 728 ? 11 :
				2011 <= currentExercice.id && currentExercice.id <= 2017 ? 12 :
				2021 <= currentExercice.id && currentExercice.id <= 2025 ? 13 :
				2031 <= currentExercice.id && currentExercice.id <= 2035 ? 14 :
				2041 <= currentExercice.id && currentExercice.id <= 2043 ? 15 :
				2051 <= currentExercice.id && currentExercice.id <= 2052 ? 16 :
				2061 <= currentExercice.id && currentExercice.id <= 2063 ? 17 :
				2071 <= currentExercice.id && currentExercice.id <= 2074 ? 18 :
				2081 <= currentExercice.id && currentExercice.id <= 2084 ? 19 :
				2091 <= currentExercice.id && currentExercice.id <= 2095 ? 20 :
				2101 <= currentExercice.id && currentExercice.id <= 2103 ? 21 :
				2111 <= currentExercice.id && currentExercice.id <= 2112 ? 22 :
				2121 <= currentExercice.id && currentExercice.id <= 2124 ? 23 :	
				801 <= currentExercice.id && currentExercice.id <= 803 ? 23 : 0;
				
			const nextDiscordant = 
				(1201 <= currentExercice.id && currentExercice.id <= 1210) ||
				(1216 <= currentExercice.id && currentExercice.id <= 1220) ||
				(1306 <= currentExercice.id && currentExercice.id <= 1310) ||
				(1316 <= currentExercice.id && currentExercice.id <= 1320) ||
				currentExercice.id == 2012 ? "2" : "1";

			const nextExerciceId = this.tempExerciceVariable[currentExercice.id]["exercices"].shift();
			const nextExercice = this.dataService.getExerciseById(nextExerciceId);
			this.dataService.childExercice = nextExercice;
			return this.updateVariables(nextExercice, {"discordant": nextDiscordant, "type_pb": nextTypePb});
		} else {
			for (const variable in activityVariables) {
				if (activityVariables.hasOwnProperty(variable)) {
					try {
						if (variable.startsWith("_")) {
							variables[variable] = activityVariables[variable];
						} else if (Array.isArray(activityVariables[variable]) && variable[0] === '#') {
							AppUtils.shuffleArray(activityVariables[variable])
							variables[variable.slice(1)] = evalArray(this.tempExerciceVariable, variable.slice(1), activityVariables[variable]);
						} else if (Array.isArray(activityVariables[variable])) {
							variables[variable] = evalArray(this.tempExerciceVariable, variable, activityVariables[variable]);
						} else if (activityVariables[variable].startsWith("$")) {
							variables[variable] = evalFrozenVariable(this.tempExerciceVariable, variable, activityVariables[variable].slice(1));
						} else {
							variables[variable] = evalUnit(variable, activityVariables[variable]);
						}
					} catch (e) {
						variables[variable] = activityVariables[variable];
					}
				}
			}
			this.variables = Object.assign(this.variables, variables);

			const nextTypePb = 
				1101 <= currentExercice.id && currentExercice.id <= 1105 ? 8 :
				1106 <= currentExercice.id && currentExercice.id <= 1110 ? 7 :
				1201 <= currentExercice.id && currentExercice.id <= 1205 ? 3 :
				1206 <= currentExercice.id && currentExercice.id <= 1210 ? 2 :
				1211 <= currentExercice.id && currentExercice.id <= 1215 ? 1 :
				1216 <= currentExercice.id && currentExercice.id <= 1220 ? 6 :
				1221 <= currentExercice.id && currentExercice.id <= 1225 ? 5 :
				1226 <= currentExercice.id && currentExercice.id <= 1230 ? 4 :
				1301 <= currentExercice.id && currentExercice.id <= 1310 ? 9 :		
				1311 <= currentExercice.id && currentExercice.id <= 1320 ? 10 :
				1401 <= currentExercice.id && currentExercice.id <= 1410 ? 11 :
				701 <= currentExercice.id && currentExercice.id <= 728 ? 11 :
				2011 <= currentExercice.id && currentExercice.id <= 2017 ? 12 :
				2021 <= currentExercice.id && currentExercice.id <= 2025 ? 13 :
				2031 <= currentExercice.id && currentExercice.id <= 2035 ? 14 :
				2041 <= currentExercice.id && currentExercice.id <= 2043 ? 15 :
				2051 <= currentExercice.id && currentExercice.id <= 2052 ? 16 :
				2061 <= currentExercice.id && currentExercice.id <= 2063 ? 17 :
				2071 <= currentExercice.id && currentExercice.id <= 2074 ? 18 :
				2081 <= currentExercice.id && currentExercice.id <= 2084 ? 19 :
				2091 <= currentExercice.id && currentExercice.id <= 2095 ? 20 :
				2101 <= currentExercice.id && currentExercice.id <= 2103 ? 21 :
				2111 <= currentExercice.id && currentExercice.id <= 2112 ? 22 :
				2121 <= currentExercice.id && currentExercice.id <= 2124 ? 23 :
				801 <= currentExercice.id && currentExercice.id <= 803 ? 23 : 0;
				
			const nextDiscordant = 
				(1201 <= currentExercice.id && currentExercice.id <= 1210) ||
				(1216 <= currentExercice.id && currentExercice.id <= 1220) ||
				(1306 <= currentExercice.id && currentExercice.id <= 1310) ||
				(1316 <= currentExercice.id && currentExercice.id <= 1320) ||
				currentExercice.id == 2012 ? "2" : "1";

			variables["type_pb"] = nextTypePb;
			variables["discordant"] = nextDiscordant;

			this.currentVariables = variables;

			return variables;
		}
	}
	// tslint:enable: prefer-const (necessary for eval)

	restoreActivity(savedActivity) {
		if (!localStorage.getItem("currentUserJourney")) {
			const cursorMod = savedActivity._params.find(param => {
				return param.name === "v-cursor-mod";
			});
			// lors du raffraîchissement changer le mode bilan par entraînement
			if (cursorMod && cursorMod.value === "3") {
				cursorMod.value = "1";
			}
		}
		this._params = savedActivity._params;
		this.routingPath = savedActivity.routingPath;
		this.name = savedActivity.name;
		this.id = savedActivity.id;
		this.className = savedActivity.className;
		this.file = savedActivity.file;
		this.variables = savedActivity.variables;
		this.maxPlayer = savedActivity.maxPlayers;
		this.slug = savedActivity.slug;
		this.shownParamsLength = savedActivity.shownParamsLength;
		this.currentVariables = savedActivity.currentVariables;
		this.description = savedActivity.description;
		this.thumbnail = savedActivity.thumbnail;
		this.customThumbnail = savedActivity.customThumbnail;
		this.hasExercice = savedActivity.hasExercice;
		this.selected = savedActivity.selected;
	}

	get params() {
		return this._params;
	}

	set params(value) {
		this._params = value;

		this.shownParamsLength = 0;
		if (Array.isArray(this._params)) {
			this._params.forEach(param => {
				if (!param.hidden) {
					this.shownParamsLength++;
				}
			});
		}
	}

	public get exercicesParam() {
		return this.params.find(param => param.exercices === true);
	}
}

results matching ""

    No results matching ""