-
Notifications
You must be signed in to change notification settings - Fork 0
/
box2dModel.js
1628 lines (1517 loc) · 57.5 KB
/
box2dModel.js
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This is a box2dModel step object that developers can use to create new
* step types.
*
* TODO: Copy this file and rename it to
*
* <new step type>.js
* e.g. for example if you are creating a quiz step it would look
* something like quiz.js
*
* and then put the new file into the new folder
* you created for your new step type
*
* your new folder will look something like
* vlewrapper/WebContent/vle/node/<new step type>/
*
* e.g. for example if you are creating a quiz step it would look something like
* vlewrapper/WebContent/vle/node/quiz/
*
*
* TODO: in this file, change all occurrences of the word 'Box2dModel' to the
* name of your new step type
*
* <new step type>
* e.g. for example if you are creating a quiz step it would look
* something like Quiz
*/
/**
* This is the constructor for the object that will perform the logic for
* the step when the students work on it. An instance of this object will
* be created in the .html for this step (look at box2dModel.html)
*
* TODO: rename Box2dModel
*
* @constructor
*/
class Box2dModel extends WISEAPI {
constructor() {
super();
this.sendGetParametersMessage();
}
}
Box2dModel.prototype.handleParametersMessage = function(messageData) {
const parameters = messageData.parameters;
this.content = parameters;
this.states = [];
var d = new Date();
this.timestamp = d.getTime();
this.parametersReceived = true;
this.renderIfReady();
}
Box2dModel.prototype.handleComponentStateMessage = function(messageData) {
this.componentState = messageData.componentState;
this.componentStateReceived = true;
this.renderIfReady();
}
Box2dModel.prototype.renderIfReady = function() {
if (this.parametersReceived && this.componentStateReceived) {
this.render();
}
}
// function Box2dModel(node) {
// // this.node = node;
// // this.view = node.view;
// this.content = content;
// // if(node.studentWork != null) {
// // this.states = node.studentWork;
// // } else {
// // this.states = [];
// // };
// this.states = [];
// var d = new Date();
// this.timestamp = d.getTime();
// };
/**
* Find the previous models for all the steps that have the given tag and occur
* before the current step in the project
* @param tagName the tag name
* @param functionArgs the arguments to this function
* @returns the results from the check, the result object
* contains an array of previous saved models
*/
Box2dModel.prototype.checkPreviousModelsForTags = function(tagName, functionArgs) {
//default values for the result
var result = {
"previousModels":[]
};
if (typeof this.view.getProject != "undefined")
{
//the node ids of the steps that come before the current step and have the given tag
var nodeIds = this.view.getProject().getPreviousNodeIdsByTag(tagName, this.node.id);
if(nodeIds != null) {
//loop through all the node ids that come before the current step and have the given tag
for(var x=0; x<nodeIds.length; x++) {
//get a node id
var nodeId = nodeIds[x];
if(nodeId != null) {
//get the latest work for the node
var latestWork = this.view.getState().getLatestWorkByNodeId(nodeId);
//console.log(latestWork, latestWork.modelData.savedModels, result.previousModels, result.previousModels.concat(latestWork.modelData.savedModels))
if (typeof latestWork.modelData !== "undefined"){
result.previousModels = result.previousModels.concat(latestWork.modelData.savedModels);
result.custom_objects_made_count = latestWork.modelData.custom_objects_made_count;
}
}
}
}
}
return result;
};
/**
* Find a value from a table
* before the current step in the project
* @param tagName the tag name
* @param functionArgs the arguments to this function
* @returns the results from the check, the result object
* contains an array of previous saved models
*/
Box2dModel.prototype.checkTableForValue = function(tagName, functionArgs) {
//default values for the result
var result = -1;
if (typeof this.view.getProject != "undefined")
{
//the node ids of the steps that come before the current step and have the given tag
var nodeIds = this.view.getProject().getPreviousNodeIdsByTag(tagName, this.node.id);
if(nodeIds != null) {
//loop through all the node ids that come before the current step and have the given tag
for(var x=0; x<nodeIds.length; x++) {
//get a node id
var nodeId = nodeIds[x];
if(nodeId != null) {
//get the latest work for the node
var latestWork = this.view.getState().getLatestWorkByNodeId(nodeId);
if (latestWork != null && latestWork != "" && typeof functionArgs != "undefined" && !isNaN(Number(functionArgs[0])) && !isNaN(Number(functionArgs[1]))){
var text = latestWork.tableData[Number(functionArgs[0])][Number(functionArgs[1])].text;
if (!isNaN(Number(text))) result = Number(text);
}
}
}
}
}
return result;
};
/**
* This function renders everything the student sees when they visit the step.
* This includes setting up the html ui elements as well as reloading any
* previous work the student has submitted when they previously worked on this
* step, if any.
*
* TODO: rename Box2dModel
*
* note: you do not have to use 'promptDiv' or 'studentResponseTextArea', they
* are just provided as examples. you may create your own html ui elements in
* the .html file for this step (look at box2dModel.html).
*/
Box2dModel.prototype.render = function() {
//display any prompts to the student
$('#promptDiv').html(this.content.prompt);
//display the prompt2 which is below the canvas and the student textarea
if (this.content.prompt2 != null && this.content.prompt2.length > 0){
$('#prompt2Div').html(this.content.prompt2);
} else {
// no content to add, remove the div.
$("#response-holder").remove();
}
var previousModels = [];
var custom_objects_made_count = 0;
var density = -2;
this.currentDataPoint = null;
// store details of liquids that were tested for mass, volume
this.liquids_tested = [];
// //process the tag maps if we are not in authoring mode
// if(typeof this.view.authoringMode === "undefined" || this.view.authoringMode == null || !this.view.authoringMode) {
// var tagMapResults = this.processTagMaps();
// //get the result values
// if (typeof tagMapResults.previousModels !== "undefined") previousModels = tagMapResults.previousModels;
// if (typeof tagMapResults.custom_objects_made_count !== "undefined") custom_objects_made_count = tagMapResults.custom_objects_made_count;
// if (typeof tagMapResults.density !== "undefined") density = tagMapResults.density;
// }
//load any previous responses the student submitted for this step
var latestState = this.getLatestState();
if(latestState != null) {
/*
* get the response from the latest state. the response variable is
* just provided as an example. you may use whatever variables you
* would like from the state object (look at box2dModelState.js)
*/
var tableData = latestState.tableData.slice();
var latestModels = latestState.modelData;
if (latestModels != null && latestModels != '' && typeof latestModels != "undefined"){
previousModels = latestModels.savedModels.concat(previousModels);
// remove any models with a repeat id
var model_ids = [];
for (var i = previousModels.length-1; i >= 0; i--){
var match_found = false;
for (var j = 0; j < model_ids.length; j++){
if (model_ids[j] == previousModels[i].id){
previousModels.splice(i, 1);
match_found = true;
break;
}
}
if (!match_found) model_ids.push(previousModels[i].id);
}
custom_objects_made_count = Math.max(custom_objects_made_count, latestModels.custom_objects_made_count);
}
//set the previous student work into the text area
$('#studentResponseTextArea').val(latestState.response);
}
if (this.componentState != null) {
var tableData = this.componentState.studentData.globalParametersTableData;
const latestModels = this.componentState.studentData.modelData;
if (latestModels != null && latestModels != '' && typeof latestModels != "undefined"){
previousModels = latestModels.savedModels.concat(previousModels);
// remove any models with a repeat id
var model_ids = [];
for (var i = previousModels.length-1; i >= 0; i--){
var match_found = false;
for (var j = 0; j < model_ids.length; j++){
if (model_ids[j] == previousModels[i].id){
previousModels.splice(i, 1);
match_found = true;
break;
}
}
if (!match_found) model_ids.push(previousModels[i].id);
}
custom_objects_made_count = Math.max(custom_objects_made_count, latestModels.custom_objects_made_count);
}
//set the previous student work into the text area
//$('#studentResponseTextArea').val(latestState.response);
}
// // setup the event logger and feedbacker
// if (typeof this.content.feedbackEvents != "undefined"){
// this.feedbackManager = new FeedbackManager(this.node, this.content.feedbackEvents, this.node.customEventTypes) ;
// } else {
// this.feedbackManager = new FeedbackManager(this.node, [], this.node.customEventTypes) ;
// this.node.setCompleted();
// }
if (typeof tester == "undefined" || tester == null){ // if we are already in this step, the following is unnecessary
init(box2dModel.content, previousModels, density >= 0 ? density : undefined, tableData, custom_objects_made_count);
}
//eventManager.fire("box2dInit", [{}], this);
//this.view.pushStudentWork(this.node.id, {});
};
/**
* Process the tag maps and obtain the results
* @return an object containing the results from processing the
* tag maps. the object contains two fields
* enableStep
* message
*/
Box2dModel.prototype.processTagMaps = function() {
var previousModels = [];
var custom_objects_made_count = 0;
//the tag maps
var tagMaps = this.node.tagMaps;
//check if there are any tag maps
if(tagMaps != null) {
//loop through all the tag maps
for(var x=0; x<tagMaps.length; x++) {
//get a tag map
var tagMapObject = tagMaps[x];
if(tagMapObject != null) {
//get the variables for the tag map
var tagName = tagMapObject.tagName;
var functionName = tagMapObject.functionName;
var functionArgs = tagMapObject.functionArgs;
if(functionName == "getPreviousModels") {
//get the result of the check
var result = this.checkPreviousModelsForTags(tagName, functionArgs);
previousModels = previousModels.concat(result.previousModels);
custom_objects_made_count += result.custom_objects_made_count;
} else if (functionName == "getValueFromTableForDensity"){
var density = this.checkTableForValue(tagName, functionArgs);
}
}
}
}
var returnObject = {};
if (previousModels.length > 0){
//put the variables in an object so we can return multiple variables
returnObject = {"previousModels":previousModels, "custom_objects_made_count":custom_objects_made_count};
} else if (typeof density != "undefined"){
returnObject = {"density":density};
}
return returnObject;
};
/**
* This function retrieves the latest student work
*
* TODO: rename Box2dModel
*
* @return the latest state object or null if the student has never submitted
* work for this step
*/
Box2dModel.prototype.getLatestState = function() {
var latestState = null;
//check if the states array has any elements
if(this.states != null && this.states.length > 0) {
//get the last state
latestState = this.states[this.states.length - 1];
}
return latestState;
};
/**
* When an event that is exclusive to Box2dModel is fired it is interpreted here.
* For each row creates a "row" of the following data, which is then structured into a table
* id | total_mass | total_volume | total_density | enclosed_mass | enclosed_volume | enclosed_density
* volume_displaced | sink_or_float | percent_submerged | percent_above_ | tested_in_beaker | tested_on_scale | tested_on_balance
* @param type, args, obj
* @return
*/
Box2dModel.prototype.interpretEvent = function(type, args, obj) {
var evt = {};
evt.type = type;
var d = new Date();
evt.time = d.getTime() - this.timestamp;
evt.models = []; // models used in this event
evt.details = {}; // extra details about this event
// update model table so that when we check this event the corresponding models will be on the table
// was orignally in save, but put it here instead - still only doing for make/delete/test
var tableData = GLOBAL_PARAMETERS.tableData;
// mass_vol_determined will be used for graphs
var mass_volume_determined = false;
var isResultsEvent = false;
var includeGraph = false;
var includeTable = false;
// get information pertaining to a results graph or chart from content
if (typeof this.content.results_chart !== "undefined") {
var isSinkFloatNotMaterials = true;
var showLiquids = typeof this.content.results_chart.showLiquids === "boolean" ? this.content.results_chart.showLiquids: true;
if (typeof this.content.results_chart.isGraphNotTable == "boolean" && this.content.results_chart.isGraphNotTable){
includeGraph = true;
if (typeof this.content.results_chart.isSinkFloatNotMaterials == "boolean") {
isSinkFloatNotMaterials = this.content.results_chart.isSinkFloatNotMaterials;
}
} else if (typeof this.content.results_chart.isGraphNotTable == "boolean" && !this.content.results_chart.isGraphNotTable) {
includeTable = true;
var arrColumnNamesToImport = this.content.results_chart.arrColumnNamesToImport != null ? this.content.results_chart.arrColumnNamesToImport : [];
var arrColumnNamesToDisplay = this.content.results_chart.arrColumnNamesToDisplay != null ? this.content.results_chart.arrColumnNamesToDisplay : arrColumnNamesToImport;
var showTestedMassValuesOnly = typeof this.content.results_chart.showTestedMassValuesOnly === "boolean" ? this.content.results_chart.showTestedMassValuesOnly: true;
var showTestedLiquidValuesOnly = typeof this.content.results_chart.showTestedLiquidValuesOnly === "boolean" ? this.content.results_chart.showTestedLiquidValuesOnly : true;
// if reported "sink or float" or "materials", and did not give display arrays, use presets
if (typeof this.content.results_chart.isSinkFloatNotMaterials == "boolean" && arrColumnNamesToImport.length == 0) {
if (this.content.results_chart.isSinkFloatNotMaterials) {
arrColumnNamesToImport = ["Materials", "Total_Volume", "Total_Mass"];
arrColumnNamesToDisplay = ["Materials", "Volume (ml)", "Mass (g)"];
} else {
arrColumnNamesToImport = ["Total_Volume", "Total_Mass", "Sink_in_Water"];
arrColumnNamesToDisplay = ["Volume (ml)", "Mass (g)", "Sinks in water?"];
}
}
}
}
// loop through args looking for "Obj" models (including premades)
for (var a = 0; a < args.length; a++){
if ( (typeof args[a].id !== "undefined" && args[a].id.substr(0,3) == "Obj") || (typeof args[a].premade_name !== "undefined" && args[a].premade_name != null && args[a].premade_name.length > 0)){
var model = {};
model.id = args[a].id;
// if there are multiple materials we'll need to get a percentage of each
if (typeof args[a].unique_materials !== "undefined" && args[a].unique_materials.length > 0){
if (args[a].unique_materials.length == 1){
model.Materials = args[a].unique_materials.slice().toString();
} else {
// multiple materials
var percMat = [];
for (var m = 0; m < args[a].unique_materials.length; m++){
var mat = args[a].unique_materials[m];
// if rectPrismArrays availabe search through and add up volumes
var vol = 0
if (args[a].rectPrismArrays != null && args[a].rectPrismArrays.materials != null && args[a].rectPrismArrays.materials.length > 0){
for (var b = 0; b < args[a].rectPrismArrays.materials.length; b++){
if (mat == args[a].rectPrismArrays.materials[b]){
vol += args[a].rectPrismArrays.widths[b] * args[a].rectPrismArrays.heights[b] * args[a].rectPrismArrays.depths[b];
}
}
} else if (args[a].cylinderArrays != null && args[a].cylinderArrays.materials != null && args[a].cylinderArrays.materials.length > 0){
for (var b = 0; b < args[a].cylinderArrays.materials.length; b++){
if (mat == args[a].cylinderArrays.materials[b]){
vol += Math.pow(args[a].cylinderArrays.diameters[b]/2 ,2) * Math.PI * args[a].rectPrismArrays.heights[b];
}
}
}
if (vol > 0){
vol = vol / args[a].material_volume * 100;
percMat.push(vol);
}
}
if (percMat.length == args[a].unique_materials.length){
model.Materials = "";
for (m = 0; m < args[a].unique_materials.length; m++){
if (m > 0) model.Materials += ", ";
model.Materials += percMat[m].toFixed(0) + "% " + args[a].unique_materials[m];
}
} else {
model.Materials = args[a].unique_materials.slice().sort().toString()
}
}
}
model.Total_Volume = args[a].total_volume;
model.Widths = typeof args[a].widths !== "undefined" ? args[a].widths.toString().replace(/,/g,", ") : undefined;
model.Heights = typeof args[a].heights !== "undefined" ? args[a].heights.toString().replace(/,/g,", ") : undefined;
model.Depths = typeof args[a].depths !== "undefined" ? args[a].depths.toString().replace(/,/g,", ") : undefined;
model.Width = args[a].max_width;
model.Height = args[a].max_height;
model.Depth = args[a].max_depth;
model.Total_Mass = args[a].mass;
model.Total_Density = model.Total_Mass / model.Total_Volume;
model.Material_Mass = args[a].mass;
model.Material_Volume = args[a].material_volume;
model.Material_Density = model.Material_Mass / model.Material_Volume;
model.Open_Mass = 0;
model.Open_Volume = args[a].interior_volume;
model.Open_Density = 0;
model.Tested_on_Scale = 0;
model.Tested_on_Balance = 0;
// cycle through each liquid to gather data
for (var i = 0; i < GLOBAL_PARAMETERS.liquids_in_world.length; i++){
var liquid_name = GLOBAL_PARAMETERS.liquids_in_world[i];
var liquid_density = GLOBAL_PARAMETERS.liquids[liquid_name].density;
if (model.Total_Density / liquid_density > 1.03){
model["Sink_in_"+liquid_name] = "Sink";
} else if (liquid_density / model.Total_Density > 1.03){
model["Sink_in_"+liquid_name] = "Float";
} else {
model["Sink_in_"+liquid_name] = "Divider";
}
model["Percent_Submerged_in_"+liquid_name] = Math.min(1, model.Total_Density / liquid_density);
model["Percent_Above_"+liquid_name] = 1 - model["Percent_Submerged_in_"+liquid_name];
model["Volume_Displaced_in_"+liquid_name] = model.Total_Volume * model["Percent_Submerged_in_"+liquid_name];
model["Mass_Displaced_in_"+liquid_name] = liquid_density * model.Total_Volume * model["Percent_Submerged_in_"+liquid_name];
model["Tested_in_"+liquid_name] = 0;
}
if (evt.type == "add-to-beaker" || evt.type == "test-in-beaker" || evt.type == "remove-from-beaker"){
model["Tested_in_"+args[1].liquid_name] = 1;
} else if (evt.type == "add-to-scale" || evt.type == "test-on-scale" || evt.type == "remove-from-scale"){
model["Tested_on_Scale"] = 1;
} else if (evt.type == "add-to-balance" || evt.type == "test-on-balance" || evt.type == "remove-from-balance"){
model["Tested_on_Balance"] = 1;
}
// determine whether we have all the info we need to plot point
if (evt.type == "test-in-beaker"){
isResultsEvent = true;
if ((typeof args[0].volume_tested === "undefined" || !args[0].volume_tested) && (typeof args[0].mass_tested !== "undefined" && args[0].mass_tested || !showTestedMassValuesOnly)){
mass_volume_determined = true;
}
args[0].volume_tested = true;
}
if (evt.type == "test-on-scale" || evt.type == "test-on-balance") {
isResultsEvent = true;
if ((typeof args[0].mass_tested === "undefined" || !args[0].mass_tested) && (typeof args[0].volume_tested !== "undefined" && args[0].volume_tested || !showTestedLiquidValuesOnly)){
mass_volume_determined = true;
}
args[0].mass_tested = true;
}
// create a new model in tableData if id is not found
if (evt.type == "make-model" || evt.type == "duplicate-model"){
isResultsEvent = true;
var id_found = false;
for (var i = 0; i < tableData.length; i++){
if (tableData[i][0].text == "id"){
for (var j=1; j < tableData[i].length; j++){
if (tableData[i][j].text == model.id){
id_found = true; break;
}
}
break;
}
}
if (!id_found){
for (var i = 0; i < tableData.length; i++){
if (typeof model[tableData[i][0].text] !== "undefined"){
tableData[i].push({"text":model[tableData[i][0].text], "uneditable":true});
} else {
tableData[i].push({"text":"", "uneditable":true});
}
}
}
}
// remove a model
if (evt.type == "delete-model" || evt.type == "revise-model"){
isResultsEvent = true;
for (var i = 0; i < tableData.length; i++){
if (tableData[i][0].text == "id"){
for (var j=1; j < tableData[i].length; j++){
if (tableData[i][j].text == model.id){
for (var k = 0; k < tableData.length; k++){
tableData[k].splice(j, 1)
}
}
}
}
}
}
// on test update the "Tested_in" or "Tested_on" column
if (evt.type.substr(0,4) == "test" || evt.type.substr(0,7) == "add-to-"){
// run through keys of model looking for positive tests, then update column in tableData
for (var key in model){
if (key.substr(0,6) == "Tested" && model[key] == 1){
// find id on table
for (var i=0; i < tableData.length; i++){
if (tableData[i][0].text == "id"){
for (var j=1; j < tableData[i].length; j++){
if (tableData[i][j].text == model.id){
// search for the column matching the test
for (var k=0; k < tableData.length; k++){
if (tableData[k][0].text == key){
tableData[k][j].text = 1;
}
}
}
}
}
}
}
}
}
evt.models.push(model);
} else {
// in cases where the argument is not an "Obj" (object model), just attach all keys to the evt directly, or primitive
if (typeof args[a] !== "undefined" && typeof args[a] !== "object"){
evt.details = args[a];
} else if (typeof args[a] !== "undefined") {
for (var key in args[a]) {
evt.details[key] = args[a][key];
}
}
}
}
// check to see if this is a beaker being tested on a scale (alone)
if (typeof args[0].id !== "undefined" && args[0].id.substr(0,2) == "bk" && evt.type == "test-on-scale" && args[1].Object_count != null && args[1].Object_count == 1){
// when student puts empty beaker on the mass is tested
if (args[0].liquid_volume != null && args[0].liquid_volume == 0){
// set a flag on the saved object itself
args[0].mass_tested = true;
}
// when students put a filled beaker on, its mass
if (args[0].liquid_volume != null && args[0].liquid_volume > 0 && args[0].mass_tested){
// calculate mass and add it to saved object
args[0].liquid_mass = args[0].liquid_volume * args[0].liquid_density;
mass_volume_determined = true;
// test if this is unique
var liquid_unique = true;
for (var k = 0; k < this.liquids_tested.length; k++){
if (this.liquids_tested[k].liquid_name == args[0].liquid_name && this.liquids_tested[k].mass == args[0].liquid_mass && this.liquids_tested[k].volume == args[0].liquid_volume){
liquid_unique = false;
break;
}
}
if (liquid_unique){
this.liquids_tested.push({"liquid_name":args[0].liquid_name, "display_name":args[0].display_name, "mass":args[0].liquid_mass, "volume":args[0].liquid_volume, "density":args[0].liquid_density});
isResultsEvent = true;
}
}
}
// send results to graph
if ((includeGraph || includeTable) && (isResultsEvent || evt.type == "graph-clicked" || evt.type == "table-changed"|| (evt.type == "make-beaker" && ( GLOBAL_PARAMETERS.INCLUDE_GRAPH_BUILDER || GLOBAL_PARAMETERS.INCLUDE_TABLE_BUILDER)))){
if (includeGraph) {
if (evt.type == "graph-clicked"){
this.currentDataPoint = args;
builder.drawMaterial(this.currentDataPoint[1], this.currentDataPoint[0]);
} else if (evt.type == "make-model"){
this.currentDataPoint = null;
}
// get index for total mass
var massIndex = -1;
for (var i = 0; i < tableData.length; i++) {
if (tableData[i][0].text === 'Total_Mass') {
massIndex = i;
break;
}
}
// get index for total volume
var volumeIndex = -1;
for (var i = 0; i < tableData.length; i++) {
if (tableData[i][0].text === 'Total_Volume') {
volumeIndex = i;
break;
}
}
// get index for tested in water
var liquid_name = GLOBAL_PARAMETERS.liquids_in_world.length >= 1 ? GLOBAL_PARAMETERS.liquids_in_world[0] : "Water";
// get index for tested on scale
var sinkIndex = -1;
for (var i = 0; i < tableData.length; i++) {
if (tableData[i][0].text === 'Sink_in_' + liquid_name) {
sinkIndex = i;
break;
}
}
// get index for tested on scale
var beakerIndex = -1;
for (var i = 0; i < tableData.length; i++) {
if (tableData[i][0].text === 'Tested_in_' + liquid_name) {
beakerIndex = i;
break;
}
}
// get index for tested on scale
var scaleIndex = -1;
for (var i = 0; i < tableData.length; i++) {
if (tableData[i][0].text === 'Tested_on_Scale') {
scaleIndex = i;
break;
}
}
// get index for material
var materialIndex = -1;
for (var i = 0; i < tableData.length; i++) {
if (tableData[i][0].text === 'Materials') {
materialIndex = i;
break;
}
}
// make sure we have appropriate indices
if (massIndex >= 0 && volumeIndex >= 0 && beakerIndex >= 0 && scaleIndex >= 0) {
// update max and min axis values to fit the data
var xMin = 0;
var xMax = 50;
var yMin = 0;
var yMax = 50;
var seriesSpecs = [];
if (!isSinkFloatNotMaterials && materialIndex >= 0) {
// find unique materials
if (tableData[0].length > 0) {
// graph
for (var j = 1; j < tableData[0].length; j++) {
var material_name = tableData[materialIndex][j].text;
// is unique?
var thismaterialIndex = -1;
if (seriesSpecs.length > 0) {
for (var k = 0; k < seriesSpecs.length; k++) {
if (seriesSpecs[k].id === material_name) {
thismaterialIndex = k;
break;
}
}
}
if (thismaterialIndex == -1) {
var firstmaterial_name = material_name.replace(/[0-9]+% /g, "");
;
if (typeof /(.*?),|$/.exec(firstmaterial_name)[1] !== 'undefined') {
firstmaterial_name = /(.*?),|$/.exec(firstmaterial_name)[1];
}
seriesSpecs.push(
{
id: material_name,
name: material_name,
color: GLOBAL_PARAMETERS['materials'][firstmaterial_name] != null ? GLOBAL_PARAMETERS['materials'][firstmaterial_name]["fill_colors"][0] : 'rgba(127, 127, 127, .5)',
marker: {
enabled : true,
lineColor : GLOBAL_PARAMETERS['materials'][firstmaterial_name] != null ? GLOBAL_PARAMETERS['materials'][firstmaterial_name]["stroke_colors"][0] : 'rgba(127, 127, 127, .5)'
},
data: []
}
);
thismaterialIndex = seriesSpecs.length - 1;
}
var point;
if (tableData[beakerIndex][j].text === 1 && tableData[scaleIndex][j].text === 1) {
// we have the volume and mass
point = [parseFloat(tableData[volumeIndex][j].text), parseFloat(tableData[massIndex][j].text)];
} else if (tableData[beakerIndex][j].text == 1 && tableData[scaleIndex][j].text === 0) {
// we have the volume and not the mass
point = [parseFloat(tableData[volumeIndex][j].text), -1];
} else if (tableData[beakerIndex][j].text == 0 && tableData[scaleIndex][j].text === 1) {
// we have the mass and not the volume
point = [-1, parseFloat(tableData[massIndex][j].text)];
} else {
// you know nothing Jon Vitale
point = [-1, -1];
}
seriesSpecs[thismaterialIndex].data.push(point);
if (point[0] < xMin && point[0] >= 0) {
xMin = point[0];
yMin = point[0];
}
if (point[1] < yMin && point[1] >= 0) {
xMin = point[1];
yMin = point[1];
}
if (point[0] > xMax) {
xMax = point[0];
yMax = point[0];
}
if (point[1] > yMax) {
xMax = point[1];
yMax = point[1];
}
}
}
// were any liquids tested
if (this.liquids_tested.length > 0) {
for (var i = 0; i < this.liquids_tested.length; i++) {
// is unique?
var thismaterialIndex = -1;
if (seriesSpecs.length > 0) {
for (var k = 0; k < seriesSpecs.length; k++) {
if (seriesSpecs[k].id === this.liquids_tested[i].liquid_name) {
thismaterialIndex = k;
break;
}
}
}
if (thismaterialIndex == -1) {
seriesSpecs.push(
{
id: this.liquids_tested[i].liquid_name,
name: typeof this.liquids_tested[i].display_name === "string" ? this.liquids_tested[i].display_name: this.liquids_tested[i].liquid_name,
color: GLOBAL_PARAMETERS['liquids'][this.liquids_tested[i].liquid_name] != null ? GLOBAL_PARAMETERS['liquids'][this.liquids_tested[i].liquid_name]["stroke_color"] : 'rgba(127, 127, 127, .5)',
marker: {
enabled : true,
lineColor : GLOBAL_PARAMETERS['materials'][firstmaterial_name] != null ? GLOBAL_PARAMETERS['materials'][firstmaterial_name]["stroke_colors"][0] : 'rgba(127, 127, 127, .5)'
},
data: []
}
);
thismaterialIndex = seriesSpecs.length - 1;
}
var point = [this.liquids_tested[i].volume, this.liquids_tested[i].mass];
seriesSpecs[thismaterialIndex].data.push(point);
if (point[0] < xMin && point[0] >= 0) {
xMin = point[0];
yMin = point[0];
}
if (point[1] < yMin && point[1] >= 0) {
xMin = point[1];
yMin = point[1];
}
if (point[0] > xMax) {
xMax = point[0];
yMax = point[0];
}
if (point[1] > yMax) {
xMax = point[1];
yMax = point[1];
}
}
}
} else if (isSinkFloatNotMaterials && sinkIndex >= 0) {
// create arrays for sink, float, liquid
var sinkPoints = [];
var floatPoints = [];
var unknownPoints = [];
var dividerPoints = [];
var includeUnknown = GLOBAL_PARAMETERS.INCLUDE_GRAPH_BUILDER || GLOBAL_PARAMETERS.INCLUDE_TABLE_BUILDER ? true : false;
var displayAll = GLOBAL_PARAMETERS.INCLUDE_GRAPH_BUILDER;
if (this.currentDataPoint != null) unknownPoints.push(this.currentDataPoint);
if (tableData[0].length > 0) {
for (var j = 1; j < tableData[0].length; j++) {
var point;
if ((tableData[beakerIndex][j].text === 1 && tableData[scaleIndex][j].text === 1) || displayAll) {
if (tableData[beakerIndex][j].text === 1) {
if (tableData[sinkIndex][j].text === "Sink") {
point = [parseFloat(tableData[volumeIndex][j].text), parseFloat(tableData[massIndex][j].text)];
sinkPoints.push(point);
} else if (tableData[sinkIndex][j].text === "Float"){
point = [parseFloat(tableData[volumeIndex][j].text), parseFloat(tableData[massIndex][j].text)];
floatPoints.push(point);
} else {
point = [parseFloat(tableData[volumeIndex][j].text), parseFloat(tableData[massIndex][j].text)];
dividerPoints.push(point);
}
} else {
point = [parseFloat(tableData[volumeIndex][j].text), parseFloat(tableData[massIndex][j].text)];
unknownPoints.push(point);
}
} else if (tableData[beakerIndex][j].text == 1 && tableData[scaleIndex][j].text === 0) {
// we have the volume and not the mass
if (tableData[sinkIndex][j].text === "Sink") {
point = [parseFloat(tableData[volumeIndex][j].text), includeUnknown ? -1 : 0];
sinkPoints.push(point);
} else if (tableData[sinkIndex][j].text === "Float"){
point = [parseFloat(tableData[volumeIndex][j].text), includeUnknown ? -1 : 0];
floatPoints.push(point);
} else {
point = [parseFloat(tableData[volumeIndex][j].text), includeUnknown ? -1 : 0];
dividerPoints.push(point);
}
} else if (tableData[beakerIndex][j].text === 0 && tableData[scaleIndex][j].text === 1) {
// we have the mass and not the volume
if (!includeUnknown) {
// we have the volume and not the mass
if (tableData[sinkIndex][j].text === "Sink") {
point = [parseFloat(tableData[volumeIndex][j].text), -1];
sinkPoints.push(point);
} else if (tableData[sinkIndex][j].text === "Float") {
point = [parseFloat(tableData[volumeIndex][j].text), -1];
floatPoints.push(point);
} else {
point = [parseFloat(tableData[volumeIndex][j].text), -1];
dividerPoints.push(point);
}
} else {
point = [0, parseFloat(tableData[massIndex][j].text)];
unknownPoints.push(point);
}
} else {
if (!includeUnknown) {
// you know nothing Jon Vitale
if (tableData[sinkIndex][j].text === "Sink") {
point = [-1, -1];
sinkPoints.push(point);
} else if (tableData[sinkIndex][j].text === "Float"){
point = [parseFloat(tableData[volumeIndex][j].text), -1];
floatPoints.push(point);
} else {
point = [parseFloat(tableData[volumeIndex][j].text), -1];
dividerPoints.push(point);
}
} else {
point = [0, 0];
unknownPoints.push(point);
}
}
if (point[0] < xMin && point[0] >= 0) {
xMin = point[0];
yMin = point[0];
}
if (point[1] < yMin && point[1] >= 0) {
xMin = point[1];
yMin = point[1];
}
if (point[0] > xMax) {
xMax = point[0];
yMax = point[0];
}
if (point[1] > yMax) {
xMax = point[1];
yMax = point[1];
}
}
seriesSpecs = [
{
id: 'sink',
name: 'Sink',
color: 'rgba(255, 0, 0, .5)',
data: sinkPoints
},
{
id: 'float',
name: 'Float',
color: 'rgba(0, 0, 155, .5)',
data: floatPoints
},
{
id: 'divider',
name: 'Divider',
color: 'rgba(0, 155, 0, .5)',
data: dividerPoints
}
]
if (includeUnknown) {
seriesSpecs.push(
{
id: 'unknown',
name: 'Unknown',
color: 'rgba(127, 127, 127, .5)',
data: unknownPoints
}
);
}
}
// were any liquids tested
if (this.liquids_tested.length > 0) {
for (var i = 0; i < this.liquids_tested.length; i++) {
// is unique?
var thismaterialIndex = -1;
if (seriesSpecs.length > 0) {
for (var k = 0; k < seriesSpecs.length; k++) {
if (seriesSpecs[k].id === this.liquids_tested[i].liquid_name) {
thismaterialIndex = k;
break;
}
}
}
if (thismaterialIndex == -1) {
seriesSpecs.push(
{
id: this.liquids_tested[i].liquid_name,
name: typeof this.liquids_tested[i].display_name === "string" ? this.liquids_tested[i].display_name: this.liquids_tested[i].liquid_name,
color: this.liquids_tested[i].liquid_name == 'Water' ? 'rgba(0, 127, 0, 0.5)' : (GLOBAL_PARAMETERS['liquids'][this.liquids_tested[i].liquid_name] != null ? GLOBAL_PARAMETERS['liquids'][this.liquids_tested[i].liquid_name]["stroke_color"] : 'rgba(127, 127, 127, .5)'),
data: []
}
);
thismaterialIndex = seriesSpecs.length - 1;
}
var point = [this.liquids_tested[i].volume, this.liquids_tested[i].mass];
seriesSpecs[thismaterialIndex].data.push(point);
if (point[0] < xMin && point[0] >= 0) {
xMin = point[0];
yMin = point[0];
}
if (point[1] < yMin && point[1] >= 0) {
xMin = point[1];
yMin = point[1];
}
if (point[0] > xMax) {
xMax = point[0];
yMax = point[0];
}
if (point[1] > yMax) {
xMax = point[1];
yMax = point[1];
}
}
}
}
// round max values up to nearest 50
xMax = Math.ceil(xMax / 50) * 50;
yMax = xMax;
// set a tickInterval based on the xMax
var tickInterval = 10;
if (xMax > 500) {
tickInterval = 100;
} else if (xMax > 100) {
tickInterval = 50;
} else if (xMax > 50) {
tickInterval = 20;
}
this.chart = {
chart: {
type: 'scatter',
zoomType: 'xy',
height: 400,
width: 500,
marginRight: 100
},
title: {
text: "Mass vs. Volume"
},
subtitle: {
text: ''
},
xAxis: {
title: {
enabled: true,
text: "Volume (ml)"