-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathteacherProjectService.ts
More file actions
3060 lines (2810 loc) · 99.3 KB
/
Copy pathteacherProjectService.ts
File metadata and controls
3060 lines (2810 loc) · 99.3 KB
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
'use strict';
import * as angular from 'angular';
import { ProjectService } from './projectService';
import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
import { BranchService } from './branchService';
import { ComponentServiceLookupService } from './componentServiceLookupService';
import { HttpClient } from '@angular/common/http';
import { ConfigService } from './configService';
import { PathService } from './pathService';
import { copy } from '../common/object/object';
import { generateRandomKey } from '../common/string/string';
@Injectable()
export class TeacherProjectService extends ProjectService {
private componentChangedSource: Subject<void> = new Subject<void>();
public componentChanged$: Observable<void> = this.componentChangedSource.asObservable();
private nodeChangedSource: Subject<boolean> = new Subject<boolean>();
public nodeChanged$: Observable<boolean> = this.nodeChangedSource.asObservable();
private refreshProjectSource: Subject<void> = new Subject<void>();
public refreshProject$ = this.refreshProjectSource.asObservable();
private scrollToBottomOfPageSource: Subject<void> = new Subject<void>();
public scrollToBottomOfPage$ = this.scrollToBottomOfPageSource.asObservable();
private errorSavingProjectSource: Subject<void> = new Subject<void>();
public errorSavingProject$: Observable<void> = this.errorSavingProjectSource.asObservable();
private notAllowedToEditThisProjectSource: Subject<void> = new Subject<void>();
public notAllowedToEditThisProject$: Observable<void> = this.notAllowedToEditThisProjectSource.asObservable();
private projectSavedSource: Subject<void> = new Subject<void>();
public projectSaved$: Observable<void> = this.projectSavedSource.asObservable();
private savingProjectSource: Subject<void> = new Subject<void>();
public savingProject$: Observable<void> = this.savingProjectSource.asObservable();
constructor(
protected branchService: BranchService,
protected componentServiceLookupService: ComponentServiceLookupService,
protected http: HttpClient,
protected configService: ConfigService,
protected pathService: PathService
) {
super(branchService, componentServiceLookupService, http, configService, pathService);
}
getNewProjectTemplate() {
return {
nodes: [
{
id: 'group0',
type: 'group',
title: 'Master',
startId: 'group1',
ids: ['group1']
},
{
id: 'group1',
type: 'group',
title: $localize`First Lesson`,
startId: 'node1',
ids: ['node1'],
icons: {
default: {
color: '#2196F3',
type: 'font',
fontSet: 'material-icons',
fontName: 'info'
}
},
transitionLogic: {
transitions: []
}
},
{
id: 'node1',
type: 'node',
title: $localize`First Step`,
components: [],
constraints: [],
showSaveButton: false,
showSubmitButton: false,
transitionLogic: {
transitions: []
}
}
],
constraints: [],
startGroupId: 'group0',
startNodeId: 'node1',
navigationMode: 'guided',
layout: {
template: 'starmap|leftNav|rightNav'
},
metadata: {
title: ''
},
notebook: {
enabled: false,
label: $localize`Notebook`,
enableAddNew: true,
itemTypes: {
note: {
type: 'note',
enabled: true,
enableLink: true,
enableAddNote: true,
enableClipping: true,
enableStudentUploads: true,
requireTextOnEveryNote: false,
label: {
singular: $localize`note`,
plural: $localize`notes`,
link: $localize`Notes`,
icon: 'note',
color: '#1565C0'
}
},
report: {
enabled: false,
label: {
singular: $localize`report`,
plural: $localize`reports`,
link: $localize`Report`,
icon: 'assignment',
color: '#AD1457'
},
notes: [
{
reportId: 'finalReport',
title: $localize`Final Report`,
description: $localize`Final summary report of what you learned in this unit`,
prompt: $localize`Use this space to write your final report using evidence from your notebook.`,
content: $localize`<h3>This is a heading</h3><p>This is a paragraph.</p>`
}
]
}
}
},
teacherNotebook: {
enabled: true,
label: $localize`Teacher Notebook`,
enableAddNew: true,
itemTypes: {
note: {
type: 'note',
enabled: false,
enableLink: true,
enableAddNote: true,
enableClipping: true,
enableStudentUploads: true,
requireTextOnEveryNote: false,
label: {
singular: $localize`note`,
plural: $localize`notes`,
link: $localize`Notes`,
icon: 'note',
color: '#1565C0'
}
},
report: {
enabled: true,
label: {
singular: $localize`teacher notes`,
plural: $localize`teacher notes`,
link: $localize`Teacher Notes`,
icon: 'assignment',
color: '#AD1457'
},
notes: [
{
reportId: 'teacherReport',
title: $localize`Teacher Notes`,
description: $localize`Notes for the teacher as they're running the WISE unit`,
prompt: $localize`Use this space to take notes for this unit`,
content: $localize`<p>Use this space to take notes for this unit</p>`
}
]
}
}
},
inactiveNodes: []
};
}
notifyAuthorProjectBeginEnd(projectId, isBegin) {
return this.http.post(`/api/author/project/notify/${projectId}/${isBegin}`, null).toPromise();
}
notifyAuthorProjectBegin(projectId) {
return this.notifyAuthorProjectBeginEnd(projectId, true);
}
notifyAuthorProjectEnd(projectId = null) {
return new Promise((resolve, reject) => {
if (projectId == null) {
if (this.project != null) {
projectId = this.configService.getProjectId();
} else {
resolve({});
}
}
this.notifyAuthorProjectBeginEnd(projectId, false).then(() => {
resolve({});
});
});
}
/**
* Retrieve the project JSON
* @param projectId retrieve the project JSON with this id
* @return a promise to return the project JSON
*/
retrieveProjectById(projectId: number): any {
return this.http
.get(`/api/author/config/${projectId}`)
.toPromise()
.then((configJSON: any) => {
return this.http
.get(configJSON.projectURL)
.toPromise()
.then((projectJSON: any) => {
projectJSON.previewProjectURL = configJSON.previewProjectURL;
return projectJSON;
});
});
}
/**
* Registers a new project having the projectJSON content with the server.
* Returns a new project id if the project is successfully registered.
* @param projectJSONString a valid JSON string
*/
registerNewProject(projectName, projectJSONString) {
return this.http
.post(this.configService.getConfigParam('registerNewProjectURL'), {
projectName: projectName,
projectJSONString: projectJSONString
})
.toPromise()
.then((newProjectId) => {
return newProjectId;
});
}
/**
* Replace a component
* @param nodeId the node id
* @param componentId the component id
* @param component the new component
*/
replaceComponent(nodeId, componentId, component) {
const components = this.getComponents(nodeId);
for (let c = 0; c < components.length; c++) {
if (components[c].id === componentId) {
components[c] = component;
break;
}
}
}
/**
* Create a new group
* @param title the title of the group
* @returns the group object
*/
createGroup(title: string): any {
return {
id: this.getNextAvailableGroupId(),
type: 'group',
title: title,
startId: '',
constraints: [],
transitionLogic: {
transitions: []
},
ids: []
};
}
/**
* Create a new node
* @param title the title of the node
* @returns the node object
*/
createNode(title) {
return {
id: this.getNextAvailableNodeId(),
title: title,
type: 'node',
constraints: [],
transitionLogic: {
transitions: []
},
showSaveButton: false,
showSubmitButton: false,
components: []
};
}
getNodesWithNewIds(nodes: any[]): any[] {
const oldToNewIds = this.getOldToNewIds(nodes);
return nodes.map((node: any) => {
return this.replaceOldIds(node, oldToNewIds);
});
}
getOldToNewIds(nodes: any[]): Map<string, string> {
const newNodeIds = [];
const newComponentIds = [];
const oldToNewIds = new Map();
for (const node of nodes) {
const newNodeId = this.getNextAvailableNodeId(newNodeIds);
oldToNewIds.set(node.id, newNodeId);
newNodeIds.push(newNodeId);
for (const component of node.components) {
const newComponentId = this.getUnusedComponentId(newComponentIds);
oldToNewIds.set(component.id, newComponentId);
newComponentIds.push(newComponentId);
}
}
return oldToNewIds;
}
replaceOldIds(node: any, oldToNewIds: Map<string, string>): any {
let nodeString = JSON.stringify(node);
for (const oldId of Array.from(oldToNewIds.keys()).reverse()) {
const newId = oldToNewIds.get(oldId);
nodeString = this.replaceIds(nodeString, oldId, newId);
}
return JSON.parse(nodeString);
}
replaceIds(nodeString: string, oldId: string, newId: string): string {
nodeString = nodeString.replace(new RegExp(`\"${oldId}\"`, 'g'), `"${newId}"`);
nodeString = nodeString.replace(new RegExp(`${oldId}Constraint`, 'g'), `${newId}Constraint`);
return nodeString;
}
/**
* Create a node inside the group
* @param node the new node
* @param nodeId the node id of the group to create the node in
*/
createNodeInside(node, nodeId) {
if (nodeId === 'inactiveNodes' || nodeId === 'inactiveGroups') {
this.addInactiveNodeInsertAfter(node);
this.setIdToNode(node.id, node);
} else {
this.setIdToNode(node.id, node);
if (this.isInactive(nodeId)) {
this.addInactiveNodeInsertInside(node, nodeId);
} else {
this.addNode(node);
this.insertNodeInsideOnlyUpdateTransitions(node.id, nodeId);
this.insertNodeInsideInGroups(node.id, nodeId);
}
}
}
/**
* Create a node after the given node id
* @param node the new node
* @param nodeId the node to add after
*/
createNodeAfter(newNode, nodeId) {
if (this.isInactive(nodeId)) {
this.addInactiveNodeInsertAfter(newNode, nodeId);
this.setIdToNode(newNode.id, newNode);
} else {
this.addNode(newNode);
this.setIdToNode(newNode.id, newNode);
this.insertNodeAfterInGroups(newNode.id, nodeId);
this.insertNodeAfterInTransitions(newNode, nodeId);
}
}
private isInactive(nodeId: string): boolean {
return this.getInactiveNodes().some((node) => node.id === nodeId);
}
/**
* Get the node id that comes after a given node id
* @param nodeId get the node id that comes after this node id
* @return the node id that comes after the one that is passed in as a parameter, or null
* if this is the last node in the sequence
*/
getNodeIdAfter(nodeId) {
const order = this.getOrderById(nodeId);
if (order != null) {
return this.getNodeIdByOrder(order + 1);
} else {
return null;
}
}
getProjectRubric(): any {
return this.project.rubric;
}
setProjectRubric(html) {
this.project.rubric = html;
}
/**
* Get the number of branch paths. This is assuming the node is a branch point.
* @param nodeId The node id of the branch point node.
* @return The number of branch paths for this branch point.
*/
getNumberOfBranchPaths(nodeId) {
const transitions = this.getTransitionsByFromNodeId(nodeId);
if (transitions != null) {
return transitions.length;
}
return 0;
}
/**
* If this step is a branch point, we will return the criteria that is used
* to determine which path the student gets assigned to.
* @param nodeId The node id of the branch point.
* @returns A human readable string containing the criteria of how students
* are assigned branch paths on this branch point.
*/
getBranchCriteriaDescription(nodeId) {
const transitionLogic = this.getTransitionLogicByFromNodeId(nodeId);
for (const transition of transitionLogic.transitions) {
if (transition.criteria != null && transition.criteria.length > 0) {
for (const singleCriteria of transition.criteria) {
if (singleCriteria.name === 'choiceChosen') {
return 'multiple choice';
} else if (singleCriteria.name === 'score') {
return 'score';
}
}
}
}
/*
* None of the transitions had a specific criteria so the branching is just
* based on the howToChooseAmongAvailablePaths field.
*/
if (transitionLogic.howToChooseAmongAvailablePaths === 'workgroupId') {
return 'workgroup ID';
} else if (transitionLogic.howToChooseAmongAvailablePaths === 'random') {
return 'random assignment';
}
}
setProjectScriptFilename(scriptFilename) {
this.project.script = scriptFilename;
}
getProjectScriptFilename() {
if (this.project != null && this.project.script != null) {
return this.project.script;
}
return null;
}
nodeHasRubric(nodeId: string): boolean {
return this.getNumberOfRubricsByNodeId(nodeId) > 0;
}
/**
* Get the total number of rubrics (step + components) for the given nodeId
* @param nodeId the node id
* @return Number of rubrics for the node
*/
getNumberOfRubricsByNodeId(nodeId: string): number {
let numRubrics = 0;
const node = this.getNodeById(nodeId);
if (node) {
const nodeRubric = node.rubric;
if (nodeRubric != null && nodeRubric != '') {
numRubrics++;
}
const components = node.components;
if (components && components.length) {
for (let component of components) {
if (component) {
const componentRubric = component.rubric;
if (componentRubric != null && componentRubric != '') {
numRubrics++;
}
}
}
}
}
return numRubrics;
}
/**
* Delete a component from a node
* @param nodeId the node id containing the node
* @param componentId the component id
*/
deleteComponent(nodeId, componentId) {
const node = this.getNodeById(nodeId);
const components = node.components;
for (let c = 0; c < components.length; c++) {
if (components[c].id === componentId) {
components.splice(c, 1);
break;
}
}
}
deleteTransition(node, transition) {
const nodeTransitions = node.transitionLogic.transitions;
const index = nodeTransitions.indexOf(transition);
if (index > -1) {
nodeTransitions.splice(index, 1);
}
if (nodeTransitions.length <= 1) {
// these settings only apply when there are multiple transitions
node.transitionLogic.howToChooseAmongAvailablePaths = null;
node.transitionLogic.whenToChoosePath = null;
node.transitionLogic.canChangePath = null;
node.transitionLogic.maxPathsVisitable = null;
}
}
/**
* Get the branch path letter
* @param nodeId get the branch path letter for this node if it is in a branch
* @return the branch path letter for the node if it is in a branch
*/
getBranchPathLetter(nodeId) {
return this.nodeIdToBranchPathLetter[nodeId];
}
/**
* Set the node into the project by replacing the existing node with the
* given node id
* @param nodeId the node id of the node
* @param node the node object
*/
setNode(nodeId, node) {
for (let n = 0; n < this.project.nodes.length; n++) {
const tempNode = this.project.nodes[n];
if (tempNode.id == nodeId) {
this.project.nodes[n] = node;
}
}
for (let i = 0; i < this.project.inactiveNodes.length; i++) {
const tempNode = this.project.inactiveNodes[i];
if (tempNode.id == nodeId) {
this.project.inactiveNodes[i] = node;
}
}
this.idToNode[nodeId] = node;
}
getIdToNode() {
return this.idToNode;
}
turnOnSaveButtonForAllComponents(node) {
for (const component of node.components) {
const service = this.componentServiceLookupService.getService(component.type);
if (service.componentUsesSaveButton()) {
component.showSaveButton = true;
}
}
}
turnOffSaveButtonForAllComponents(node) {
for (const component of node.components) {
const service = this.componentServiceLookupService.getService(component.type);
if (service.componentUsesSaveButton()) {
component.showSaveButton = false;
}
}
}
checkPotentialStartNodeIdChangeThenSaveProject() {
this.checkPotentialStartNodeIdChange();
return this.saveProject();
}
checkPotentialStartNodeIdChange() {
const firstLeafNodeId = this.getFirstLeafNodeId();
if (firstLeafNodeId == null) {
this.setStartNodeId('');
} else {
const currentStartNodeId = this.getStartNodeId();
if (currentStartNodeId != firstLeafNodeId) {
this.setStartNodeId(firstLeafNodeId);
}
}
}
/**
* Get the first leaf node by traversing all the start ids
* until a leaf node id is found
*/
private getFirstLeafNodeId(): any {
let firstLeafNodeId = null;
const startGroupId = this.project.startGroupId;
let node = this.getNodeById(startGroupId);
let done = false;
// loop until we have found a leaf node id or something went wrong
while (!done) {
if (node == null) {
done = true;
} else if (this.isGroupNode(node.id)) {
firstLeafNodeId = node.id;
node = this.getNodeById(node.startId);
} else if (this.isApplicationNode(node.id)) {
firstLeafNodeId = node.id;
done = true;
} else {
done = true;
}
}
return firstLeafNodeId;
}
/**
* Remove the node from the active nodes.
* If the node is a group node, also remove its children.
* @param nodeId the node to remove
* @returns the node that was removed
*/
removeNodeFromActiveNodes(nodeId) {
let nodeRemoved = null;
const activeNodes = this.project.nodes;
for (let a = 0; a < activeNodes.length; a++) {
const activeNode = activeNodes[a];
if (activeNode.id === nodeId) {
activeNodes.splice(a, 1);
nodeRemoved = activeNode;
if (activeNode.type === 'group') {
this.removeChildNodesFromActiveNodes(activeNode);
}
break;
}
}
return nodeRemoved;
}
/**
* Move the child nodes of a group from the active nodes.
* @param node The group node.
*/
removeChildNodesFromActiveNodes(node) {
for (const childId of node.ids) {
this.removeNodeFromActiveNodes(childId);
}
}
/**
* Move an active node to the inactive nodes array.
* @param node the node to move
* @param nodeIdToInsertAfter place the node after this
*/
moveToInactive(node, nodeIdToInsertAfter) {
if (this.isActive(node.id)) {
this.removeNodeFromActiveNodes(node.id);
this.addInactiveNodeInsertAfter(node, nodeIdToInsertAfter);
}
}
/**
* Add the node to the inactive nodes array.
* @param node the node to move
* @param nodeIdToInsertAfter place the node after this
*/
addInactiveNodeInsertAfter(node, nodeIdToInsertAfter = null) {
this.clearTransitionsFromNode(node);
if (this.isNodeIdToInsertTargetNotSpecified(nodeIdToInsertAfter)) {
this.insertNodeAtBeginningOfInactiveNodes(node);
} else {
this.insertNodeAfterInactiveNode(node, nodeIdToInsertAfter);
}
if (node.type === 'group') {
this.inactiveGroupNodes.push(node.id);
this.addGroupChildNodesToInactive(node);
} else {
this.inactiveStepNodes.push(node.id);
}
}
clearTransitionsFromNode(node) {
if (node.transitionLogic != null) {
node.transitionLogic.transitions = [];
}
}
insertNodeAtBeginningOfInactiveNodes(node) {
this.project.inactiveNodes.splice(0, 0, node);
}
insertNodeAfterInactiveNode(node, nodeIdToInsertAfter) {
const inactiveNodes = this.getInactiveNodes();
for (let i = 0; i < inactiveNodes.length; i++) {
if (inactiveNodes[i].id === nodeIdToInsertAfter) {
const parentGroup = this.getParentGroup(nodeIdToInsertAfter);
if (parentGroup != null) {
this.insertNodeAfterInGroups(node.id, nodeIdToInsertAfter);
this.insertNodeAfterInTransitions(node, nodeIdToInsertAfter);
}
inactiveNodes.splice(i + 1, 0, node);
}
}
}
isNodeIdToInsertTargetNotSpecified(nodeIdToInsertTarget) {
return [null, 'inactiveNodes', 'inactiveSteps', 'inactiveGroups'].includes(
nodeIdToInsertTarget
);
}
/**
* Move the node from active to inside an inactive group
* @param node the node to move
* @param nodeIdToInsertInside place the node inside this
*/
moveFromActiveToInactiveInsertInside(node, nodeIdToInsertInside) {
this.removeNodeFromActiveNodes(node.id);
this.addInactiveNodeInsertInside(node, nodeIdToInsertInside);
}
/**
* Move the node from inactive to inside an inactive group
* @param node the node to move
* @param nodeIdToInsertInside place the node inside this
*/
moveFromInactiveToInactiveInsertInside(node, nodeIdToInsertInside) {
this.removeNodeFromInactiveNodes(node.id);
if (this.isGroupNode(node.id)) {
/*
* remove the group's child nodes from our data structures so that we can
* add them back in later
*/
for (const childId of node.ids) {
const childNode = this.getNodeById(childId);
const inactiveNodesIndex = this.project.inactiveNodes.indexOf(childNode);
if (inactiveNodesIndex != -1) {
this.project.inactiveNodes.splice(inactiveNodesIndex, 1);
}
const inactiveStepNodesIndex = this.inactiveStepNodes.indexOf(childNode);
if (inactiveStepNodesIndex != -1) {
this.inactiveStepNodes.splice(inactiveStepNodesIndex, 1);
}
}
}
this.addInactiveNodeInsertInside(node, nodeIdToInsertInside);
}
addInactiveNodeInsertInside(node, nodeIdToInsertInside = null) {
this.clearTransitionsFromNode(node);
if (this.isNodeIdToInsertTargetNotSpecified(nodeIdToInsertInside)) {
this.insertNodeAtBeginningOfInactiveNodes(node);
} else {
this.insertNodeInsideInactiveNode(node, nodeIdToInsertInside);
}
if (node.type === 'group') {
this.inactiveGroupNodes.push(node.id);
this.addGroupChildNodesToInactive(node);
} else {
this.inactiveStepNodes.push(node.id);
}
}
insertNodeInsideInactiveNode(node, nodeIdToInsertInside) {
const inactiveNodes = this.getInactiveNodes();
const inactiveGroupNodes = this.getInactiveGroupNodes();
for (const inactiveGroup of inactiveGroupNodes) {
if (nodeIdToInsertInside === inactiveGroup.id) {
this.insertNodeInsideOnlyUpdateTransitions(node.id, nodeIdToInsertInside);
this.insertNodeInsideInGroups(node.id, nodeIdToInsertInside);
for (let i = 0; i < inactiveNodes.length; i++) {
if (inactiveNodes[i].id === nodeIdToInsertInside) {
inactiveNodes.splice(i + 1, 0, node);
}
}
}
}
}
addNodeToGroup(node, group) {
if (this.isGroupHasNode(group)) {
this.insertAfterLastNode(node, group);
} else {
this.insertAsFirstNode(node, group);
}
}
isGroupHasNode(group) {
return group.ids.length != 0;
}
getLastNodeInGroup(group) {
const lastNodeId = group.ids[group.ids.length - 1];
return this.idToNode[lastNodeId];
}
insertAsFirstNode(node, group) {
this.insertNodeInsideOnlyUpdateTransitions(node.id, group.id);
this.insertNodeInsideInGroups(node.id, group.id);
}
insertAfterLastNode(node, group) {
const lastNode = this.getLastNodeInGroup(group);
this.insertNodeAfterInTransitions(node, lastNode.id);
this.insertNodeAfterInGroups(node.id, lastNode.id);
}
createNodeAndAddToLocalStorage(nodeTitle) {
const node = this.createNode(nodeTitle);
this.setIdToNode(node.id, node);
this.addNode(node);
this.applicationNodes.push(node);
return node;
}
getAutomatedAssessmentProjectId(): number {
return this.configService.getConfigParam('automatedAssessmentProjectId') || -1;
}
getSimulationProjectId(): number {
return this.configService.getConfigParam('simulationProjectId') || -1;
}
/**
* Get the branch letter in the node position string if the node is in a branch path
* @param nodeId the node id we want the branch letter for
* @return the branch letter in the node position if the node is in a branch path
*/
getBranchLetter(nodeId) {
const nodePosition = this.getNodePositionById(nodeId);
const branchLetterRegex = /.*([A-Z])/;
const match = branchLetterRegex.exec(nodePosition);
if (match != null) {
return match[1];
}
return null;
}
componentChanged(): void {
this.componentChangedSource.next();
}
nodeChanged(doParseProject: boolean = false): void {
this.nodeChangedSource.next(doParseProject);
}
refreshProject() {
this.refreshProjectSource.next();
}
scrollToBottomOfPage() {
this.scrollToBottomOfPageSource.next();
}
nodeHasConstraint(nodeId: string): boolean {
const constraints = this.getConstraintsOnNode(nodeId);
return constraints.length > 0;
}
getConstraintsOnNode(nodeId: string): any {
const node = this.getNodeById(nodeId);
return node.constraints;
}
/**
* Get the human readable description of the constraint.
* @param constraint The constraint object.
* @returns A human readable text string that describes the constraint.
* example
* 'All steps after this one will not be visitable until the student completes
* "3.7 Revise Your Bowls Explanation"'
*/
getConstraintDescription(constraint: any): string {
let message = '';
for (const singleRemovalCriteria of constraint.removalCriteria) {
if (message != '') {
// this constraint has multiple removal criteria
if (constraint.removalConditional === 'any') {
message += ' or ';
} else if (constraint.removalConditional === 'all') {
message += ' and ';
}
}
message += this.getCriteriaMessage(singleRemovalCriteria);
}
return this.getActionMessage(constraint.action) + message;
}
/**
* Get the constraint action as human readable text.
* @param action A constraint action.
* @return A human readable text string that describes the action
* example
* 'All steps after this one will not be visitable until '
*/
private getActionMessage(action: string): string {
if (action === 'makeAllNodesAfterThisNotVisitable') {
return $localize`All steps after this one will not be visitable until `;
}
if (action === 'makeAllNodesAfterThisNotVisible') {
return $localize`All steps after this one will not be visible until `;
}
if (action === 'makeAllOtherNodesNotVisitable') {
return $localize`All other steps will not be visitable until `;
}
if (action === 'makeAllOtherNodesNotVisible') {
return $localize`All other steps will not be visible until `;
}
if (action === 'makeThisNodeNotVisitable') {
return $localize`This step will not be visitable until `;
}
if (action === 'makeThisNodeNotVisible') {
return $localize`This step will not be visible until `;
}
}
addTeacherRemovalConstraint(node: any, periodId: number) {
const lockConstraint = {
id: generateRandomKey(),
action: 'makeThisNodeNotVisitable',
targetId: node.id,
removalConditional: 'any',
removalCriteria: [
{
name: 'teacherRemoval',
params: {
periodId: periodId
}
}
]
};
this.addConstraintToNode(node, lockConstraint);
}
removeTeacherRemovalConstraint(node: any, periodId: number) {
node.constraints = node.constraints.filter((constraint) => {
return !(
constraint.action === 'makeThisNodeNotVisitable' &&
constraint.targetId === node.id &&
constraint.removalCriteria[0].name === 'teacherRemoval' &&
constraint.removalCriteria[0].params.periodId === periodId
);
});
}
/**
* Saves the project to Config.saveProjectURL and returns commit history promise.
* if Config.saveProjectURL or Config.projectId are undefined, does not save and returns null
*/
saveProject(): any {
if (!this.configService.getConfigParam('canEditProject')) {
this.broadcastNotAllowedToEditThisProject();
return null;
}
this.broadcastSavingProject();
this.cleanupBeforeSave();
this.project.metadata.authors = this.getUniqueAuthors(
this.addCurrentUserToAuthors(this.getAuthors())
);
return this.http
.post(
this.configService.getConfigParam('saveProjectURL'),
angular.toJson(this.project, false)
)
.toPromise()
.then((response: any) => {
this.handleSaveProjectResponse(response);
});
}
getAuthors(): any[] {
return this.project.metadata.authors ? this.project.metadata.authors : [];
}
addCurrentUserToAuthors(authors: any[]): any[] {
let userInfo = this.configService.getMyUserInfo();
if (this.configService.isClassroomMonitor()) {
userInfo = {
id: userInfo.userIds[0],
firstName: userInfo.firstName,
lastName: userInfo.lastName,
username: userInfo.username
};
}
authors.push(userInfo);
return authors;
}
getUniqueAuthors(authors = []): any[] {
const idToAuthor = {};
const uniqueAuthors = [];
for (const author of authors) {
if (idToAuthor[author.id] == null) {
uniqueAuthors.push(author);
idToAuthor[author.id] = author;
}
}
return uniqueAuthors;
}