@@ -13,42 +13,47 @@ class Report extends Eloquent {
1313 public static $ rules = [];
1414 protected $ fillable = ['name ' , 'description ' , 'query ' , 'lrs ' , 'since ' , 'until ' ];
1515 protected $ actorQuery = [ 'statement.actor.account ' , 'statement.actor.mbox ' , 'statement.actor.openid ' , 'statement.actor.mbox_sha1sum ' ];
16-
17- public function getFilterAttribute () {
18- $ reportArr = $ this ->toArray ();
19- $ filter = [];
20-
21- if (isset ($ reportArr ['query ' ])) $ filter ['filter ' ] = json_encode ($ reportArr ['query ' ]);
22- if (isset ($ reportArr ['since ' ])) {
23- $ filter ['since ' ] = (new \Carbon \Carbon ($ reportArr ['since ' ]))->toIso8601String ();
24- }
25- if (isset ($ reportArr ['until ' ])) {
26- $ filter ['until ' ] = (new \Carbon \Carbon ($ reportArr ['until ' ]))->toIso8601String ();
16+ protected $ instructorQuery = [
17+ 'statement.context.instructor.account ' ,
18+ 'statement.context.instructor.mbox ' ,
19+ 'statement.context.instructor.openid ' ,
20+ 'statement.context.instructor.mbox_sha1sum '
21+ ];
22+
23+ private function constructOr ($ key , $ value ) {
24+ $ query = [];
25+ foreach ($ value as $ actor_id ) {
26+ if (is_object ($ actor_id )) {
27+ $ object_vars = get_object_vars ($ actor_id );
28+ $ filter = [];
29+ foreach ($ object_vars as $ var_key => $ var_val ) {
30+ $ filter [$ key .'. ' .$ var_key ] = $ var_val ;
31+ }
32+ return $ filter ;
33+ } else {
34+ return [$ key => $ actor_id ];
35+ }
2736 }
37+ return $ query ;
38+ }
2839
29- return $ filter ;
40+ private function constructDate ($ date ) {
41+ return (new \Carbon \Carbon ($ date ))->toIso8601String ();
3042 }
3143
3244 public function getMatchAttribute () {
3345 $ reportArr = $ this ->toArray ();
3446 $ match = [];
47+ $ actorMatch = [];
48+ $ instructorMatch = [];
3549 $ query = isset ($ reportArr ['query ' ]) ? (array ) $ reportArr ['query ' ] : null ;
3650
3751 if (is_array ($ query ) && count ($ query ) > 0 && !isset ($ query [0 ])) {
3852 foreach ($ query as $ key => $ value ) {
3953 if (in_array ($ key , $ this ->actorQuery )) {
40- foreach ($ value as $ actor_id ) {
41- if (is_object ($ actor_id )) {
42- $ object_vars = get_object_vars ($ actor_id );
43- $ filter = [];
44- foreach ($ object_vars as $ var_key => $ var_val ) {
45- $ filter [$ key .'. ' .$ var_key ] = $ var_val ;
46- }
47- $ match ['$or ' ][] = $ filter ;
48- } else {
49- $ match ['$or ' ][][$ key ] = $ actor_id ;
50- }
51- }
54+ $ actorMatch ['$or ' ][] = $ this ->constructOr ($ key , $ value );
55+ } else if (in_array ($ key , $ this ->instructorQuery )) {
56+ $ instructorMatch ['$or ' ][] = $ this ->constructOr ($ key , $ value );
5257 } else {
5358 if (is_array ($ value )) {
5459 $ match [$ key ] = ['$in ' => $ value ];
@@ -59,8 +64,8 @@ public function getMatchAttribute() {
5964 }
6065 }
6166
62- $ since = isset ($ reportArr ['since ' ]) ? ( new \ Carbon \ Carbon ( $ reportArr ['since ' ]))-> toIso8601String ( ) : null ;
63- $ until = isset ($ reportArr ['until ' ]) ? ( new \ Carbon \ Carbon ( $ reportArr ['until ' ]))-> toIso8601String ( ) : null ;
67+ $ since = isset ($ reportArr ['since ' ]) ? $ this -> constructDate ( $ reportArr ['since ' ]) : null ;
68+ $ until = isset ($ reportArr ['until ' ]) ? $ this -> constructDate ( $ reportArr ['until ' ]) : null ;
6469
6570 if ($ since || $ until ) {
6671 $ match ['statement.timestamp ' ] = [];
@@ -71,20 +76,28 @@ public function getMatchAttribute() {
7176 if ($ until ) {
7277 $ match ['statement.timestamp ' ]['$lte ' ] = $ until ;
7378 }
79+ $ andMatch = ['$and ' => []];
80+ if (!empty ($ actorMatch )) $ andMatch ['$and ' ][] = $ actorMatch ;
81+ if (!empty ($ instructorMatch )) $ andMatch ['$and ' ][] = $ instructorMatch ;
82+ if (!empty ($ match )) $ andMatch ['$and ' ][] = $ match ;
83+ //echo(json_encode($andMatch));die;
7484
75- return $ match ;
85+ return $ andMatch ;
7686 }
7787
7888 public function getWhereAttribute () {
7989 $ reportArr = $ this ->toArray ();
8090 $ wheres = [];
8191 $ query = isset ($ reportArr ['query ' ]) ? (array ) $ reportArr ['query ' ] : null ;
8292 $ actorArray = [];
93+ $ instructorArray = [];
8394
8495 if (is_array ($ query ) && count ($ query ) > 0 && !isset ($ query [0 ])) {
8596 foreach (array_keys ($ query ) as $ key ) {
8697 if (in_array ($ key , $ this ->actorQuery )) {
8798 array_push ($ actorArray , [$ key , $ query [$ key ]]);
99+ } else if (in_array ($ key , $ this ->instructorQuery )) {
100+ array_push ($ instructorArray , [$ key , $ query [$ key ]]);
88101 } else {
89102 if (is_array ($ query [$ key ])) {
90103 array_push ($ wheres , [$ key , 'in ' , $ query [$ key ]]);
@@ -94,10 +107,11 @@ public function getWhereAttribute() {
94107 }
95108 }
96109 array_push ($ wheres , ['orArray ' , 'or ' , $ actorArray ]);
110+ array_push ($ wheres , ['orArray ' , 'or ' , $ instructorArray ]);
97111 }
98112
99- $ since = isset ($ reportArr ['since ' ]) ? ( new \ Carbon \ Carbon ( $ reportArr ['since ' ]))-> toIso8601String ( ) : null ;
100- $ until = isset ($ reportArr ['until ' ]) ? ( new \ Carbon \ Carbon ( $ reportArr ['until ' ]))-> toIso8601String ( ) : null ;
113+ $ since = isset ($ reportArr ['since ' ]) ? $ this -> constructDate ( $ reportArr ['since ' ]) : null ;
114+ $ until = isset ($ reportArr ['until ' ]) ? $ this -> constructDate ( $ reportArr ['until ' ]) : null ;
101115
102116 if ($ since && $ until ) {
103117 $ wheres [] = ['statement.timestamp ' , 'between ' , $ since , $ until ];
0 commit comments