Skip to content

Commit 6ddb0ff

Browse files
author
daniel-abbey
committed
Merge pull request #679 from LearningLocker/develop
v1.6.0
2 parents c3328a9 + 4cdf4f2 commit 6ddb0ff

File tree

6 files changed

+93
-52
lines changed

6 files changed

+93
-52
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.5.3
1+
1.6.0

app/controllers/api/Reports.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function run($id) {
3737
*/
3838
public function graph($id) {
3939
$report = $this->repo->show($id, $this->getOptions());
40-
$data = $this->query->aggregateTime($report->lrs, $report->match);
40+
$data = $this->query->aggregateTime($this->getOptions(), $report->match);
4141
return IlluminateResponse::json($data['result']);
4242
}
4343

app/controllers/api/Statements.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function where() {
3030
*/
3131
public function aggregate() {
3232
$pipeline = $this->getParam('pipeline');
33-
return \Response::json($this->query->aggregate($this->lrs->_id, $pipeline));
33+
return \Response::json($this->query->aggregate($this->getOptions(), $pipeline));
3434
}
3535

3636
/**
@@ -39,7 +39,7 @@ public function aggregate() {
3939
*/
4040
public function aggregateTime() {
4141
$match = $this->getParam('match');
42-
return \Response::json($this->query->aggregateTime($this->lrs->_id, $match));
42+
return \Response::json($this->query->aggregateTime($this->getOptions(), $match));
4343
}
4444

4545
/**
@@ -48,7 +48,7 @@ public function aggregateTime() {
4848
*/
4949
public function aggregateObject() {
5050
$match = $this->getParam('match');
51-
return \Response::json($this->query->aggregateObject($this->lrs->_id, $match));
51+
return \Response::json($this->query->aggregateObject($this->getOptions(), $match));
5252
}
5353

5454
/**
@@ -68,6 +68,15 @@ public function index(){
6868
return $this->returnJson($data);
6969
}
7070

71+
/**
72+
* Inserts new statements based on existing ones in one query using our existing aggregation.
73+
* @return Json<[String]> Ids of the inserted statements.
74+
*/
75+
public function insert() {
76+
$pipeline = $this->getParam('pipeline');
77+
return \Response::json($this->query->insert($pipeline, $this->getOptions()));
78+
}
79+
7180
public function void() {
7281
$match = $this->getParam('match');
7382
return \Response::json($this->query->void($match, $this->getOptions()));

app/locker/data/dashboards/AdminDashboard.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php namespace app\locker\data\dashboards;
22

3-
use App\Locker\Repository\Query\EloquentQueryRepository as Query;
4-
53
class AdminDashboard extends \app\locker\data\BaseData {
64

75
private $user;
@@ -60,10 +58,10 @@ public function lrsCount(){
6058
*
6159
**/
6260
public function actorCount(){
63-
$mbox = intval( \Statement::distinct('statement.actor.mbox')->remember(5)->get()->count() );
64-
$openid = intval( \Statement::distinct('statement.actor.openid')->remember(5)->get()->count() );
65-
$mbox_sha1sum = intval( \Statement::distinct('statement.actor.mbox_sha1sum')->remember(5)->get()->count() );
66-
$account = intval( \Statement::distinct('statement.actor.account.name')->remember(5)->get()->count() );
61+
$mbox = intval( \Statement::distinct('statement.actor.mbox')->remember(5)->count() );
62+
$openid = intval( \Statement::distinct('statement.actor.openid')->remember(5)->count() );
63+
$mbox_sha1sum = intval( \Statement::distinct('statement.actor.mbox_sha1sum')->remember(5)->count() );
64+
$account = intval( \Statement::distinct('statement.actor.account.name')->remember(5)->count() );
6765
return ($mbox + $openid + $mbox_sha1sum + $account);
6866
}
6967

app/locker/repository/Query/EloquentQueryRepository.php

Lines changed: 74 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php namespace Locker\Repository\Query;
2+
use \Cache as IlluminateCache;
3+
use \Carbon\Carbon as Carbon;
24
use \Locker\Helpers\Helpers as Helpers;
35
use \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
/**

app/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@
390390
});
391391

392392
Route::group(['prefix' => 'api/v2', 'before' => 'auth.statement'], function () {
393+
Route::get('statements/insert', ['uses' => 'Controllers\API\Statements@insert']);
393394
Route::get('statements/void', ['uses' => 'Controllers\API\Statements@void']);
394395
});
395396

0 commit comments

Comments
 (0)