11<?php namespace Locker \Repository \Query ;
2+ use \Cache as IlluminateCache ;
3+ use \Carbon \Carbon as Carbon ;
24use \Locker \Helpers \Helpers as Helpers ;
35use \Locker \Repository \Statement \EloquentRepository as StatementsRepo ;
46
@@ -53,33 +55,54 @@ public function where($lrsId, array $filters) {
5355
5456 /**
5557 * Aggregates the statements in the LRS (with the $lrsId) with the $pipeline.
56- * @param string $lrsId
58+ * @param [ string => mixed] $opts
5759 * @param [mixed] $pipeline
5860 * @return [Aggregate] http://php.net/manual/en/mongocollection.aggregate.php#refsect1-mongocollection.aggregate-examples
5961 */
60- public function aggregate ($ lrsId , array $ pipeline ) {
62+ public function aggregate (array $ opts , array $ pipeline ) {
6163 if (strpos (json_encode ($ pipeline ), '$out ' ) !== false ) {
6264 return ;
6365 }
6466
67+ $ match = [
68+ self ::LRS_ID_KEY => $ opts ['lrs_id ' ],
69+ 'active ' => true
70+ ];
71+
72+ $ scopes = $ opts ['scopes ' ];
73+ if (in_array ('all ' , $ scopes ) || in_array ('all/read ' , $ scopes ) || in_array ('statements/read ' , $ scopes )) {
74+ // Get all statements.
75+ } else if (in_array ('statements/read/mine ' , $ scopes )) {
76+ $ match ['client_id ' ] = $ opts ['client ' ]->_id ;
77+ } else {
78+ throw new Exceptions \Exception ('Unauthorized request. ' , 401 );
79+ }
80+
6581 $ pipeline [0 ]['$match ' ] = [
66- '$and ' => [(object ) $ pipeline [0 ]['$match ' ], [
67- self ::LRS_ID_KEY => $ lrsId ,
68- 'active ' => true
69- ]]
82+ '$and ' => [(object ) $ pipeline [0 ]['$match ' ], $ match ]
7083 ];
7184
72- return Helpers::replaceHtmlEntity ($ this ->db ->statements ->aggregate ($ pipeline ), true );
85+ $ cache_key = sha1 (json_encode ($ pipeline ));
86+ $ create_cache = function () use ($ pipeline , $ cache_key ) {
87+ $ expiration = Carbon::now ()->addMinutes (10 );
88+ $ result = Helpers::replaceHtmlEntity ($ this ->db ->statements ->aggregate ($ pipeline ), true );
89+ IlluminateCache::put ($ cache_key , $ result , $ expiration );
90+ return $ result ;
91+ };
92+ //$result = IlluminateCache::get($cache_key, $create_cache);
93+ $ result = $ create_cache ();
94+
95+ return $ result ;
7396 }
7497
7598 /**
7699 * Aggregates statements in the LRS (with the $lrsId) that $match into a timed group.
77- * @param string $lrsId
100+ * @param [ string => mixed] $opts
78101 * @param [mixed] $match
79102 * @return [Aggregate] http://php.net/manual/en/mongocollection.aggregate.php#refsect1-mongocollection.aggregate-examples
80103 */
81- public function aggregateTime ($ lrsId , array $ match ) {
82- return $ this ->aggregate ($ lrsId , [[
104+ public function aggregateTime ($ opts , array $ match ) {
105+ return $ this ->aggregate ($ opts , [[
83106 '$match ' => $ match
84107 ], [
85108 '$group ' => [
@@ -102,12 +125,12 @@ public function aggregateTime($lrsId, array $match) {
102125
103126 /**
104127 * Aggregates statements in the LRS (with the $lrsId) that $match into a object group.
105- * @param string $lrsId
128+ * @param [ string => mixed] $opts
106129 * @param [mixed] $match
107130 * @return [Aggregate] http://php.net/manual/en/mongocollection.aggregate.php#refsect1-mongocollection.aggregate-examples
108131 */
109- public function aggregateObject ($ lrsId , array $ match ) {
110- return $ this ->aggregate ($ lrsId , [[
132+ public function aggregateObject ($ opts , array $ match ) {
133+ return $ this ->aggregate ($ opts , [[
111134 '$match ' => $ match
112135 ], [
113136 '$group ' => [
@@ -124,47 +147,57 @@ public function aggregateObject($lrsId, array $match) {
124147 ]]);
125148 }
126149
150+ /**
151+ * Inserts new statements based on existing ones in one query using our existing aggregation.
152+ * @param [Mixed] $pipeline
153+ * @param [Sting => Mixed] $opts
154+ * @return [String] Ids of the inserted statements.
155+ */
156+ public function insert (array $ pipeline , array $ opts ) {
157+ $ statements = $ this ->aggregate ($ opts ['lrs_id ' ], $ pipeline )['result ' ];
158+
159+ if (count ($ statements ) > 0 ) {
160+ $ opts ['authority ' ] = json_decode (json_encode ($ opts ['client ' ]['authority ' ]));
161+ return (new StatementsRepo ())->store (json_decode (json_encode ($ statements )), [], $ opts );
162+ } else {
163+ return [];
164+ }
165+ }
166+
167+ /**
168+ * Inserts new voiding statements based on existing statements in one query using our aggregation.
169+ * @param [String => Mixed] $match
170+ * @param [String => Mixed] $opts
171+ * @return [String] Ids of the inserted statements.
172+ */
127173 public function void (array $ match , array $ opts ) {
128174 $ void_id = 'http://adlnet.gov/expapi/verbs/voided ' ;
129- $ match = [
130- '$and ' => [$ match , [
131- 'statement.verb.id ' => ['$ne ' => $ void_id ],
132- 'voided ' => false
133- ]]
134- ];
135175
136- $ data = $ this ->aggregate ($ opts ['lrs_id ' ], [[
137- '$match ' => $ match
176+ $ pipeline = [[
177+ '$match ' => [
178+ '$and ' => [$ match , [
179+ 'statement.verb.id ' => ['$ne ' => $ void_id ],
180+ 'voided ' => false
181+ ]]
182+ ]
138183 ], [
139184 '$project ' => [
140185 '_id ' => 0 ,
141- 'statement.id ' => 1 ,
142- ]
143- ]]);
144-
145- $ statements = array_map (function ($ result ) use ($ opts , $ void_id ) {
146- return [
147- 'actor ' => $ opts ['client ' ]['authority ' ],
186+ 'actor ' => ['$literal ' => $ opts ['client ' ]['authority ' ]],
148187 'verb ' => [
149- 'id ' => $ void_id ,
188+ 'id ' => [ ' $literal ' => $ void_id] ,
150189 'display ' => [
151- 'en ' => ' voided '
190+ 'en ' => [ ' $literal ' => ' voided ']
152191 ]
153192 ],
154193 'object ' => [
155- 'objectType ' => ' StatementRef ' ,
156- 'id ' => $ result [ ' statement ' ][ ' id ']
194+ 'objectType ' => [ ' $literal ' => ' StatementRef '] ,
195+ 'id ' => ' $ statement. id '
157196 ]
158- ];
159- }, $ data ['result ' ]);
160-
161- $ opts ['authority ' ] = json_decode (json_encode ($ opts ['client ' ]['authority ' ]));
197+ ]
198+ ]];
162199
163- if ( count ($ statements ) > 0 ){
164- return (new StatementsRepo ())->store (json_decode (json_encode ($ statements )), [], $ opts );
165- } else {
166- return [];
167- }
200+ return $ this ->insert ($ pipeline , $ opts );
168201 }
169202
170203 /**
0 commit comments