forked from MonkoftheFunk/GO_LRS
/
LRSapi.go
980 lines (837 loc) · 29.3 KB
/
LRSapi.go
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
/**
* GO_LRS
* https://github.com/MonkoftheFunk/GO_LRS
*/
package main
import (
"encoding/json"
//"fmt"
"github.com/gorilla/mux"
"github.com/nu7hatch/gouuid"
"io"
"io/ioutil"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"net/http"
"reflect"
"strconv"
"strings"
"time"
)
func main() {
r := mux.NewRouter()
r.Path("/statements/").Methods("POST").HandlerFunc(PostStatement)
r.Path("/statements/").Methods("PUT").HandlerFunc(PutStatement)
r.Path("/statements/").Methods("GET").HandlerFunc(GetStatement)
r.Path("/statements/").Methods("DELETE").HandlerFunc(DelStatement)
http.Handle("/", r)
http.ListenAndServe(":8080", nil)
}
//todo make index
func dbSession() *mgo.Session {
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
return session
}
//todo perhaps convert certain objects to array of single object for
//ease of mapping
func isRootArray(r io.Reader) (bool, []byte, error) {
body, err := ioutil.ReadAll(r)
if err != nil {
return false, body, err
}
isArray := false
for _, c := range body {
if c == ' ' || c == '\t' || c == '\r' || c == '\n' {
continue
}
isArray = c == '['
break
}
return isArray, body, err
}
func convertStatementElementsObjToArray(tempStatement map[string]interface{}) (Statement, error) {
var statement Statement
// todo go through each possible single object elements that are supposed to be arrays
if tempStatement["context"] != nil &&
tempStatement["context"].(map[string]interface{})["contextActivities"] != nil {
ca := convertContextActivitiesToArray(tempStatement["context"].(map[string]interface{})["contextActivities"].(map[string]interface{}))
tempStatement["context"].(map[string]interface{})["contextActivities"] = ca
}
if tempStatement["object"] != nil &&
tempStatement["object"].(map[string]interface{})["context"] != nil &&
tempStatement["object"].(map[string]interface{})["context"].(map[string]interface{})["contextActivities"] != nil {
ca := convertContextActivitiesToArray(tempStatement["object"].(map[string]interface{})["context"].(map[string]interface{})["contextActivities"].(map[string]interface{}))
tempStatement["object"].(map[string]interface{})["context"].(map[string]interface{})["contextActivities"] = ca
}
// turn in back to a string
b, err := json.Marshal(tempStatement)
if err != nil {
return statement, err
}
// then turn that string into the struct
err = json.Unmarshal(b, &statement)
if err != nil {
return statement, err
}
return statement, nil
}
func convertContextActivitiesToArray(tempCA map[string]interface{}) map[string]interface{} {
if tempCA != nil {
// check if the element is a map, then change to array
if tempCA["parent"] != nil && reflect.TypeOf(tempCA["parent"]).Kind() == reflect.Map {
var a []interface{}
tempCA["parent"] = append(a, tempCA["parent"])
}
if tempCA["grouping"] != nil && reflect.TypeOf(tempCA["grouping"]).Kind() == reflect.Map {
var a []interface{}
tempCA["grouping"] = append(a, tempCA["grouping"])
}
if tempCA["category"] != nil && reflect.TypeOf(tempCA["category"]).Kind() == reflect.Map {
var a []interface{}
tempCA["category"] = append(a, tempCA["category"])
}
if tempCA["other"] != nil && reflect.TypeOf(tempCA["other"]).Kind() == reflect.Map {
var a []interface{}
tempCA["other"] = append(a, tempCA["other"])
}
}
return tempCA
}
func PreProcessStatement(r io.Reader) (Statement, error) {
// read whole stream
var statement Statement
body, err := ioutil.ReadAll(r)
if err != nil {
return statement, err
}
// create temporary map for processing
var tempStatement map[string]interface{}
err = json.Unmarshal(body, &tempStatement)
if err != nil {
return statement, err
}
// process temporary map to statement
statement, err = convertStatementElementsObjToArray(tempStatement)
return statement, err
}
func PreProcessStatements(r io.Reader) ([]Statement, error) {
// determin if array of statements or just single statement
isArray, body, err := isRootArray(r)
if err != nil {
return nil, err
}
var statement Statement
var statements []Statement
if isArray {
// create temporary array map for processing
var tempStatements []map[string]interface{}
err = json.Unmarshal(body, &tempStatements)
if err != nil {
return statements, err
}
// loop through each statement to convert
for _, s := range tempStatements {
// process temporary map to statement
statement, err = convertStatementElementsObjToArray(s)
if err != nil {
return statements, err
}
// create statement array used for Post
statements = append(statements, statement)
}
} else {
// create temporary map for processing
var tempStatement map[string]interface{}
err = json.Unmarshal(body, &tempStatement)
if err != nil {
return statements, err
}
// process temporary map to statement
statement, err = convertStatementElementsObjToArray(tempStatement)
if err != nil {
return statements, err
}
// create statement array used for Post
statements = append(statements, statement)
}
return statements, nil
}
func PostStatement(w http.ResponseWriter, r *http.Request) {
// check if post is being used as a put
statementId := r.FormValue("statementId")
if statementId != "" {
PutStatement(w, r)
return
}
w.Header().Add("x-experience-api-version", "1.0")
defer r.Body.Close()
statements, err := PreProcessStatements(r.Body)
if err != nil {
// fmt.Fprint(w, err)
w.WriteHeader(http.StatusBadRequest)
return
}
// connect to db
session := dbSession()
defer session.Close()
statementsC := session.DB("LRS").C("statements")
index := mgo.Index{
Key: []string{"id"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err = statementsC.EnsureIndex(index)
if err != nil {
//fmt.Fprint(w, err)
w.WriteHeader(http.StatusBadRequest)
return
}
// check if trying to replace object with same id
status := checkIdConflictBatch(w, statementsC, statements)
if status != 0 {
w.WriteHeader(status)
// fmt.Fprint(w, status)
return
}
// go through each statement to validate and store
var sids []string
for _, s := range statements {
sid, status := s.Validate()
if status != 0 {
w.WriteHeader(http.StatusBadRequest)
// fmt.Fprint(w, err)
return
}
// output new ids
sids = append(sids, sid)
//check if voided
status = checkSpecialActionVerbs(w, statementsC, s)
if status != 0 {
w.WriteHeader(status)
// fmt.Fprint(w, status)
return
}
// save to db
statementsC.Insert(s)
}
// return 200 with statement id(s), same order index
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
enc := json.NewEncoder(w)
enc.Encode(sids)
return
}
func PutStatement(w http.ResponseWriter, r *http.Request) {
w.Header().Add("x-experience-api-version", "1.0")
defer r.Body.Close()
// verify statementId passed in
statementId := r.FormValue("statementId")
if statementId == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
s, err := PreProcessStatement(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
//fmt.Fprint(w, "decode error")
//fmt.Fprint(w, err)
return
}
// connect to db
session := dbSession()
defer session.Close()
statementsC := session.DB("LRS").C("statements")
index := mgo.Index{
Key: []string{"id"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err = statementsC.EnsureIndex(index)
if err != nil {
//fmt.Fprint(w, err)
w.WriteHeader(http.StatusBadRequest)
return
}
// check if trying to replace object with same id
s.Id = statementId
status := checkIdConflict(w, statementsC, s)
if status != 0 {
w.WriteHeader(status)
//fmt.Fprint(w, "confilct "+string(status))
return
}
_, status = s.Validate()
if status != 0 {
w.WriteHeader(http.StatusBadRequest)
// fmt.Fprint(w, "Validate "+string(status))
return
}
//check if voided/unvoided
status = checkSpecialActionVerbs(w, statementsC, s)
if status != 0 {
w.WriteHeader(status)
// fmt.Fprint(w, "Void "+string(status))
return
}
// save to db
statementsC.Insert(s)
// no response back
w.WriteHeader(http.StatusNoContent)
return
}
func checkSpecialActionVerbs(w http.ResponseWriter, statementsC *mgo.Collection, statement Statement) int {
//voiding
if statement.Verb.Id == "http://adlnet.gov/expapi/verbs/voided" {
// check has statementId to void
if statement.Object.ObjectType != "StatementRef" {
// fmt.Fprint(w, "StatementRef")
return http.StatusBadRequest
}
if statement.Object.Id == "" {
// fmt.Fprint(w, "Object.Id")
return http.StatusBadRequest
}
// find if statement to be voided exists
var result Statement
err := statementsC.Find(bson.M{"id": statement.Object.Id}).One(&result)
if err != nil {
// fmt.Fprint(w, "not found refrence"+statement.Object.Id)
// fmt.Fprint(w, err)
return http.StatusBadRequest
}
// if statement hasn't been voided do so
if result.Void != true {
err = statementsC.Update(bson.M{"id": result.Id}, bson.M{"$set": bson.M{"void": true}})
if err != nil {
// fmt.Fprint(w, "cant update")
//fmt.Fprint(w, err)
return http.StatusBadRequest
}
}
} else if statement.Object.ObjectType == "StatementRef" { //check if statement's ref exists (might be moved to validate)
if statement.Object.Id == "" {
//fmt.Fprint(w, "Object.Id")
return http.StatusBadRequest
}
// find if statement to be voided exists
var result Statement
err := statementsC.Find(bson.M{"id": statement.Object.Id}).One(&result)
if err != nil {
//fmt.Fprint(w, "not found refrence")
return http.StatusBadRequest
}
}
return 0
}
func checkIdConflictBatch(w http.ResponseWriter, statementsC *mgo.Collection, statements []Statement) int {
// build array of IDs to query if statement(s) exist
var IDs []string
Lkup := make(map[string]Statement)
for _, s := range statements {
if s.Id != "" {
IDs = append(IDs, s.Id)
Lkup[s.Id] = s
}
}
// if so then check if they are the same object so as not to throw conflict
if IDs != nil {
// find any statements with those ids
var result []Statement
err := statementsC.Find(bson.M{"id": bson.M{"$in": IDs}}).All(&result)
if err != nil {
return 0
}
for _, s := range result {
// can't compare structs with arrays/maps
// kludge the result has "stored" so add field for comparison
s.Stored = Lkup[s.Id].Stored
rj, _ := json.Marshal(Lkup[s.Id])
sj, _ := json.Marshal(s)
if string(rj) != string(sj) {
return http.StatusConflict
}
}
}
return 0
}
func checkIdConflict(w http.ResponseWriter, statementsC *mgo.Collection, statement Statement) int {
if statement.Id != "" {
// find any statements with this id
var result Statement
err := statementsC.Find(bson.M{"id": statement.Id}).One(&result)
if err != nil {
//fmt.Fprint(w, err)
return 0
}
// kludge the result has "stored" so add field for comparison
statement.Stored = result.Stored
// can't compare structs with arrays/maps
rj, _ := json.Marshal(result)
sj, _ := json.Marshal(statement)
if string(rj) != string(sj) {
return http.StatusConflict
} else {
//same no need to insert
return http.StatusNoContent
}
}
return 0
}
func DelStatement(w http.ResponseWriter, r *http.Request) {
// todo serious auth checks
statementId := r.FormValue("statementId")
if statementId == "" {
//fmt.Fprint(w, "no id")
w.WriteHeader(http.StatusBadRequest)
return
}
// connect to db
session := dbSession()
defer session.Close()
statementsC := session.DB("LRS").C("statements")
// del statement
err := statementsC.Remove(bson.M{"id": statementId})
if err != nil {
// fmt.Fprint(w, err)
w.WriteHeader(http.StatusNotFound)
return
}
}
func GetStatement(w http.ResponseWriter, r *http.Request) {
w.Header().Add("x-experience-api-version", "1.0")
// --validate
// check if format ['exact', 'canonical', 'ids'] default exact
formats_allowed := map[string]bool{"exact": true, "canonical": true, "ids": true}
format := r.FormValue("format")
if format == "" || formats_allowed[format] == false {
format = "exact"
}
// check if contain both statementId and voidedStatementId parameters then 400
statementId := r.FormValue("statementId")
voidedStatementId := r.FormValue("voidedStatementId")
/*if statementId == "" && voidedStatementId == "" {
//fmt.Fprint(w, "no id")
w.WriteHeader(http.StatusBadRequest)
return
}*/
if statementId != "" || voidedStatementId != "" {
singleQuery(w, r, statementId, voidedStatementId)
return
} else {
complexQuery(w, r)
return
}
}
func singleQuery(w http.ResponseWriter, r *http.Request, statementId string, voidedStatementId string) {
parameters_NotAllowed := []string{"agent", "verb", "activity",
"registration", "related_activities",
"since", "until", "limit", "ascending"}
for _, p := range parameters_NotAllowed {
param := r.FormValue(p)
if param != "" {
// fmt.Fprint(w, "not allowed another filter")
w.WriteHeader(http.StatusBadRequest)
return
}
}
voided := false
if voidedStatementId != "" {
statementId = voidedStatementId
voided = true
}
// connect to db
session := dbSession()
defer session.Close()
statementsC := session.DB("LRS").C("statements")
// find statement
var result Statement
err := statementsC.Find(bson.M{"id": statementId}).One(&result)
if err != nil {
//fmt.Fprint(w, err)
w.WriteHeader(http.StatusNotFound)
return
}
// The LRS MUST not return any Statement which has been voided,
// unless that Statement has been requested by voidedStatementId.
if result.Void != voided {
//fmt.Fprint(w, "result: void=")
w.WriteHeader(http.StatusNotFound)
return
}
// return back found statement
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
enc := json.NewEncoder(w)
enc.Encode(result)
return
}
func complexQuery(w http.ResponseWriter, r *http.Request) {
var q = bson.M{}
var tq = []bson.M{}
var and = []bson.M{}
var s = bson.M{}
var u = bson.M{}
if since := r.FormValue("since"); since != "" {
t, _ := time.Parse("RFC3339", since)
tq = append(tq, bson.M{"$gt": t})
s = tq[0]
}
if until := r.FormValue("until"); until != "" {
t, _ := time.Parse("RFC3339", until)
tq = append(tq, bson.M{"$lt": t})
u = tq[1]
}
if len(tq) == 2 {
q["storedVal"] = bson.M{"$and": tq}
} else if len(tq) == 1 {
q["storedVal"] = s
}
findInRef := false
if verb := r.FormValue("verb"); verb != "" {
q["verb.id"] = verb
findInRef = true
}
if registration := r.FormValue("registration"); registration != "" {
q["context.registration"] = registration
findInRef = true
}
if agent := r.FormValue("agent"); agent != "" {
// JSONObject, Compare optional Identifiers
var actor Actor
decoder := json.NewDecoder(strings.NewReader(agent))
err := decoder.Decode(&actor)
if err != nil {
//fmt.Fprint(w, err)
w.WriteHeader(http.StatusBadRequest)
return
}
aq := []bson.M{bson.M{"actor": actor},
bson.M{"actor.member": actor}}
findInRef = true
related_agents := r.FormValue("related_agents")
if related_agents != "" && related_agents == "true" {
aq = append(aq, bson.M{"object": actor})
aq = append(aq, bson.M{"context.instructor": actor})
aq = append(aq, bson.M{"object.context.instructor": actor})
aq = append(aq, bson.M{"object.context.team": actor})
}
and = append(and, bson.M{"$or": aq})
}
if activity := r.FormValue("activity"); activity != "" {
activq := []bson.M{bson.M{"object.id": activity}}
findInRef = true
related_activities := r.FormValue("related_activities")
if related_activities != "" && related_activities == "true" {
// parent, grouping, category, other, for object or array
//sub statements?
activq = append(activq, bson.M{"context.contextActivities.parent.id": activity})
activq = append(activq, bson.M{"context.contextActivities.grouping.id": activity})
activq = append(activq, bson.M{"context.contextActivities.category.id": activity})
activq = append(activq, bson.M{"context.contextActivities.other.id": activity})
activq = append(activq, bson.M{"object.context.contextActivities.parent.id": activity})
activq = append(activq, bson.M{"object.context.contextActivities.grouping.id": activity})
activq = append(activq, bson.M{"object.context.contextActivities.category.id": activity})
activq = append(activq, bson.M{"object.context.contextActivities.other.id": activity})
}
and = append(and, bson.M{"$or": activq})
}
if attachments := r.FormValue("attachments"); attachments != "" {
//todo
}
order := "-storedVal"
if ascending := r.FormValue("ascending"); ascending == "true" {
order = "storedVal"
}
//how can I control formating?
if format := r.FormValue("format"); format != "" {
//todo
}
if len(and) != 0 {
q["$and"] = and
}
// find all statements that refrence the found statments
// requery with original query and appended query
//findInRef = false
if findInRef {
session := dbSession()
defer session.Close()
statementsC := session.DB("LRS").C("statements")
// find those that meet criteria
// to then find those that refrence them within
// the same time frame
var result []Statement
err := statementsC.Find(q).All(&result)
if err != nil {
//fmt.Fprint(w, err)
w.WriteHeader(http.StatusBadRequest)
return
}
qr := bson.M{}
err = findStatementRefs(w, result, s, u, &qr)
if err != nil {
//fmt.Fprint(w, err)
w.WriteHeader(http.StatusBadRequest)
return
}
if len(and) != 0 {
and = append(and, qr)
q["$and"] = and
}
}
limit := r.FormValue("limit")
if limit == "" {
limit = "0"
}
//convert limit string to int
i, err := strconv.Atoi(limit)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// connect to db
session := dbSession()
defer session.Close()
statementsC := session.DB("LRS").C("statements")
var result []Statement //[]map[string] interface{} // []Statement //
err = statementsC.Find(q).Sort(order).
Limit(i).
All(&result)
if err != nil {
//fmt.Fprint(w, err)
w.WriteHeader(http.StatusBadRequest)
return
}
if len(result) == 0 {
w.WriteHeader(http.StatusNotFound)
return
}
// https://github.com/adlnet/ADL_LRS/blob/d86aa83ec5674982a233bae5a90df5288c8209d0/lrs/util/retrieve_statement.py
// https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#stmtapi
// /lrs/util/req_process.py :143
// based on "stored" time, subject to permissions and maximum list length.
// create cache for more statements due to limit
// format StatementResult {statements [], more IRL (link to more via querystring if "limit" set)} with list in newest stored first if "ascending" not set
// return back found statement
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
enc := json.NewEncoder(w)
enc.Encode(result)
}
func findStatementRefs(w http.ResponseWriter, stmtset []Statement, sinceq bson.M, untilq bson.M, q *bson.M) error {
// stop searching for refrence of refrence if none left
if len(stmtset) == 0 {
return nil
}
// go through all statements that match criteria
// and add to query to find anything that refrences' them
qs := []bson.M{}
for _, s := range stmtset {
qs = append(qs, bson.M{"object.objectType": "statementRef", "object.id": s.Id})
}
qs = []bson.M{bson.M{"$or": qs}}
//statements refrenced also must adhere to time frame
if sinceq != nil && untilq != nil {
qs = append(qs, sinceq)
qs = append(qs, untilq)
} else if sinceq != nil {
qs = append(qs, sinceq)
} else if untilq != nil {
qs = append(qs, untilq)
}
// finally weed out voided statements in this lookup
qs = append(qs, bson.M{"void": false})
// connect to db
session := dbSession()
defer session.Close()
statementsC := session.DB("LRS").C("statements")
var result []Statement
err := statementsC.Find(qs).Distinct("id", &result)
if err != nil {
return err
}
err = findStatementRefs(w, result, sinceq, untilq, q)
if err != nil {
return err
}
*q = bson.M{"$or": []bson.M{
bson.M{"$and": qs},
*q}}
return nil
}
// https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#stmtapi
// http://zackpierce.github.io/xAPI-Validator-JS/
// not sure how much if/howmuch I will validate structure
func (s *Statement) Validate() (string, int) {
// generate new ID's
if s.Id == "" {
id, _ := uuid.NewV4()
s.Id = id.String()
}
// generate stored datetime .Format("RFC3339")
t := time.Now()
s.StoredVal = t
s.Stored = t.Format("RFC3339")
return s.Id, 0
}
// -----------------------------------------------------------------
// import github.com/bitly/go-simplejson maybe instead
type Statement struct {
//_Id bson.ObjectId `bson:"_id,-" json:"_id,-"`
Id string `bson:"id,omitempty" json:"id,omitempty"`
Void bool `json:"-"`
Actor *Actor `bson:"actor,omitempty" json:"actor,omitempty"`
Verb Verb `bson:"verb,omitempty" json:"verb,omitempty"`
Object *Object `bson:"object,omitempty" json:"object,omitempty"`
Result *Result `bson:"result,omitempty" json:"result,omitempty"`
Context *Context `bson:"context,omitempty" json:"context,omitempty"`
Timestamp string `bson:"timestamp,omitempty" json:"timestamp,omitempty"`
Stored string `bson:"stored,omitempty" json:"stored,omitempty"`
StoredVal time.Time `json:"-"`
Authority *Actor `bson:"authority,omitempty" json:"authority,omitempty"`
Version string `bson:"version,omitempty" json:"version,omitempty"`
Attachments []*Attachment `bson:"attachments,omitempty" json:"attachments,omitempty"`
}
// statement
type Actor struct {
ObjectType string `bson:"objectType,omitempty" json:"objectType,omitempty"`
Name string `bson:"name,omitempty" json:"name,omitempty"`
Mbox string `bson:"mbox,omitempty" json:"mbox,omitempty"`
Mbox_sha1sum string `bson:"mbox_sha1sum,omitempty" json:"mbox_sha1sum,omitempty"`
OpenID string `bson:"openID,omitempty" json:"openID,omitempty"`
Account *Account `bson:"account,omitempty" json:"account,omitempty"`
// group
Member []*Actor `bson:"member,omitempty" json:"member,omitempty"`
}
// actor
type Agent struct {
ObjectType string `bson:",omitempty" json:",omitempty"`
Name string `bson:",omitempty" json:",omitempty"`
Mbox string `bson:",omitempty" json:",omitempty"`
Mbox_sha1sum string `bson:",omitempty" json:",omitempty"`
OpenID string `bson:",omitempty" json:",omitempty"`
Account *Account `bson:",omitempty" json:",omitempty"`
}
// actor
type Account struct {
HomePage string `bson:"homePage,omitempty" json:"homePage,omitempty"`
Name string `bson:"name,omitempty" json:"name,omitempty"`
}
// statement
type Verb struct {
Id string `bson:"id,omitempty" json:"id,omitempty"`
Display map[string]string `bson:"display,omitempty" json:"display,omitempty"`
}
// activity, Agent/Group, Sub-Statement, StatementReference
type Object struct {
ObjectType string `bson:"objectType,omitempty" json:"objectType,omitempty"`
Id string `bson:"id,omitempty" json:"id,omitempty"`
// Agent
Name string `bson:"name,omitempty" json:"name,omitempty"`
Mbox string `bson:"mbox,omitempty" json:"mbox,omitempty"`
Mbox_sha1sum string `bson:"mbox_sha1sum,omitempty" json:"mbox_sha1sum,omitempty"`
OpenID string `bson:"openID,omitempty" json:"openID,omitempty"`
Account *Account `bson:"account,omitempty" json:"account,omitempty"`
// group
Member []*Actor `bson:"member,omitempty" json:"member,omitempty"`
// substatement
Definition *Definition `bson:"definition,omitempty" json:"definition,omitempty"`
Actor *Actor `bson:"actor,omitempty" json:"actor,omitempty"`
Verb *Verb `bson:"verb,omitempty" json:"verb,omitempty"`
Object *StatementRef `bson:"object,omitempty" json:"object,omitempty"`
Result *Result `bson:"result,omitempty" json:"result,omitempty"`
Context *Context `bson:"context,omitempty" json:"context,omitempty"`
Timestamp string `bson:"timestamp,omitempty" json:"timestamp,omitempty"`
Stored string `bson:"stored,omitempty" json:"stored,omitempty"`
StoredVal time.Time `bson:"storedVal,omitempty" json:"-"`
Authority *Actor `bson:"authority,omitempty" json:"authority,omitempty"`
Version string `bson:"version,omitempty" json:"version,omitempty"`
Attachments []*Attachment `bson:"attachments,omitempty" json:"attachments,omitempty"`
}
// object
type Definition struct {
Name map[string]string `bson:"name,omitempty" json:"name,omitempty"`
Description map[string]string `bson:"description,omitempty" json:"description,omitempty"`
Type string `bson:"type,omitempty" json:"type,omitempty"`
MoreInfo string `bson:"moreInfo,omitempty" json:"moreInfo,omitempty"`
InteractionType string `bson:"interactionType,omitempty" json:"interactionType,omitempty"`
CorrectResponsesPattern []string `bson:"correctResponsesPattern,omitempty" json:"correctResponsesPattern,omitempty"`
Choices []*InteractionComponents `bson:"choices,omitempty" json:"choices,omitempty"`
Scale []*InteractionComponents `bson:"scale,omitempty" json:"scale,omitempty"`
Source []*InteractionComponents `bson:"source,omitempty" json:"source,omitempty"`
Target []*InteractionComponents `bson:"target,omitempty" json:"target,omitempty"`
Steps []*InteractionComponents `bson:"steps,omitempty" json:"steps,omitempty"`
Extensions map[string]interface{} `bson:"extensions,omitempty" json:"extensions,omitempty"`
}
// definition
type Interaction struct {
InteractionType string `bson:"interactionType,omitempty" json:"interactionType,omitempty"`
CorrectResponsesPattern []string `bson:"correctResponsesPattern,omitempty" json:"correctResponsesPattern,omitempty"`
Choices []*InteractionComponents `bson:"choices,omitempty" json:"choices,omitempty"`
Scale []*InteractionComponents `bson:"scale,omitempty" json:"scale,omitempty"`
Source []*InteractionComponents `bson:"source,omitempty" json:"source,omitempty"`
Target []*InteractionComponents `bson:"target,omitempty" json:"target,omitempty"`
Steps []*InteractionComponents `bson:"steps,omitempty" json:"steps,omitempty"`
}
// interaction
type InteractionComponents struct {
Id string `bson:"id,omitempty" json:"id,omitempty"`
Description map[string]string `bson:"description,omitempty" json:"description,omitempty"`
}
// statement
type Result struct {
Score *Score `bson:"score,omitempty" json:"score,omitempty"`
Success bool `bson:"success,omitempty" json:"success,omitempty"`
Completion bool `bson:"completion,omitempty" json:"completion,omitempty"`
Response string `bson:"response,omitempty" json:"response,omitempty"`
Duration string `bson:"duration,omitempty" json:"duration,omitempty"`
Extensions map[string]interface{} `bson:"extensions,omitempty" json:"extensions,omitempty"`
}
// result
type Score struct {
Scaled float32 `bson:"scaled" json:"scaled"`
Raw float32 `bson:"raw" json:"raw"`
Min float32 `bson:"min" json:"min"`
Max float32 `bson:"max" json:"max"`
}
// statement
type Context struct {
Registration string `bson:"registration,omitempty" json:"registration,omitempty"`
Instructor *Actor `bson:"instructor,omitempty" json:"instructor,omitempty"`
Team *Actor `bson:"team,omitempty" json:"team,omitempty"`
ContextActivities *ContextActivities `bson:"contextActivities,omitempty" json:"contextActivities,omitempty"`
Revision string `bson:"revision,omitempty" json:"revision,omitempty"`
Platform string `bson:"platform,omitempty" json:"platform,omitempty"`
Language string `bson:"language,omitempty" json:"language,omitempty"`
Statement *StatementRef `bson:"statement,omitempty" json:"statement,omitempty"`
Extensions map[string]interface{} `bson:"extensions,omitempty" json:"extensions,omitempty"`
}
// contextActivities
type ContextActivities struct {
Parent []*Object `bson:"parent,omitempty" json:"parent,omitempty"`
Grouping []*Object `bson:"grouping,omitempty" json:"grouping,omitempty"`
Category []*Object `bson:"category,omitempty" json:"category,omitempty"`
Other []*Object `bson:"other,omitempty" json:"other,omitempty"`
}
// context
type StatementRef struct {
ObjectType string `bson:"objectType,omitempty" json:"objectType,omitempty"`
Id string `bson:"id,omitempty" json:"id,omitempty"`
Definition *Definition `bson:"definition,omitempty" json:"definition,omitempty"`
}
// statement
type Attachment struct {
UsageType string `bson:"usageType,omitempty" json:"usageType,omitempty"`
Display map[string]string `bson:"display,omitempty" json:"display,omitempty"`
Description map[string]string `bson:"description,omitempty" json:"description,omitempty"`
ContentType string `bson:"contentType,omitempty" json:"contentType,omitempty"`
Length int `bson:"length,omitempty" json:"length,omitempty"`
Sha2 string `bson:"sha2,omitempty" json:"sha2,omitempty"`
FileUrl string `bson:"fileUrl,omitempty" json:"fileUrl,omitempty"`
}