Skip to content

Commit f2a10b4

Browse files
committed
Merge pull request #667 from LearningLocker/issue/655
Fixes #655
2 parents d2eb905 + 2f114e7 commit f2a10b4

File tree

2 files changed

+70
-64
lines changed

2 files changed

+70
-64
lines changed

app/models/Report.php

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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];

public/assets/js/reports/reports/model.js

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ define([
3131
password: window.lrs.secret
3232
},
3333
_queryResponseMap: {
34-
'actor.mbox': 'actors',
34+
'actor': 'actors',
3535
'verb.id': 'verbs',
3636
'object.id': 'activities',
3737
'object.definition.type': 'activityTypes',
@@ -41,34 +41,26 @@ define([
4141
'context.instructor': 'instructors',
4242
'context.language': 'languages'
4343
},
44+
_mapActorToResponse: function (query, response, responseKey, queryKey) {
45+
var identifierKeys = ['mbox', 'account', 'openid', 'mbox_sha1sum'];
46+
47+
return identifierKeys.reduce(function (actors, identifierKey) {
48+
var xapiKey = 'statement.' + queryKey + '.' + identifierKey;
49+
if (query[xapiKey] != null) {
50+
return actors.concat(query[xapiKey].map(function (identifier) {
51+
var display = identifierKey === 'account' ? identifier.homePage + ' / ' + identifier.name : identifier;
52+
return (identifierKey === 'mbox' ? '' : identifierKey + ':') + display;
53+
}));
54+
} else {
55+
return actors;
56+
}
57+
}, []);
58+
},
4459
_mapQueryToResponse: function (query, response) {
4560
Object.keys(this._queryResponseMap).forEach(function (queryKey) {
4661
var responseKey = this._queryResponseMap[queryKey];
47-
if (responseKey === 'actors') {
48-
var actorValues = ['actor.mbox', 'actor.account', 'actor.openid', 'actor.mbox_sha1sum'];
49-
var consolidatedActors = [];
50-
actorValues.forEach(function(value,index, originalArray) {
51-
queryKey = 'statement.' + value;
52-
if (typeof query[queryKey] !== "undefined") {
53-
var tempArray = [];
54-
var queryLabel = '';
55-
switch (value) {
56-
case 'actor.mbox': queryLabel = ''; break;
57-
case 'actor.account': queryLabel = 'account:'; break;
58-
case 'actor.openid': queryLabel = 'openid:'; break;
59-
case 'actor.mbox_sha1sum': queryLabel = 'mbox_sha1sum:'; break;
60-
}
61-
query[queryKey].forEach(function(val, ind, orgArr) {
62-
if (queryKey === 'statement.actor.account') {
63-
tempArray.push(queryLabel + val.homePage + ' / ' + val.name);
64-
} else {
65-
tempArray.push(queryLabel + query[queryKey][ind]);
66-
}
67-
});
68-
consolidatedActors = consolidatedActors.concat(tempArray);
69-
}
70-
});
71-
response[responseKey] = consolidatedActors;
62+
if (responseKey === 'actors' || responseKey === 'instructors') {
63+
response[responseKey] = this._mapActorToResponse(query, response, responseKey, queryKey);
7264
} else {
7365
queryKey = 'statement.' + queryKey;
7466
response[responseKey] = query[queryKey];
@@ -79,7 +71,7 @@ define([
7971
var query = this.get('query');
8072
Object.keys(this._queryResponseMap).forEach(function (queryKey) {
8173
var responseKey = this._queryResponseMap[queryKey];
82-
if (responseKey == 'actors') {
74+
if (responseKey === 'actors' || responseKey === 'instructors') {
8375
//consolidate actor query values by type of IFI
8476
var combined = {'account':[], 'openid':[], 'mbox_sha1sum':[], 'mailto':[]};
8577
(this.get(responseKey) || []).map(function (model) {
@@ -125,17 +117,17 @@ define([
125117
Object.keys(combined).forEach(function(value, index, originalArray) {
126118
if (combined[value].length > 0) {
127119
switch (value) {
128-
case 'account': query['statement.actor.account'] = combined[value]; break;
129-
case 'mailto': query['statement.actor.mbox'] = combined[value]; break;
130-
case 'openid': query['statement.actor.openid'] = combined[value]; break;
131-
case 'mbox_sha1sum': query['statement.actor.mbox_sha1sum'] = combined[value]; break;
120+
case 'account': query['statement.' + queryKey + '.account'] = combined[value]; break;
121+
case 'mailto': query['statement.' + queryKey + '.mbox'] = combined[value]; break;
122+
case 'openid': query['statement.' + queryKey + '.openid'] = combined[value]; break;
123+
case 'mbox_sha1sum': query['statement.' + queryKey + '.mbox_sha1sum'] = combined[value]; break;
132124
}
133125
} else {
134126
switch (value) {
135-
case 'account': query['statement.actor.account'] = undefined; break;
136-
case 'mailto': query['statement.actor.mbox'] = undefined; break;
137-
case 'openid': query['statement.actor.openid'] = undefined; break;
138-
case 'mbox_sha1sum': query['statement.actor.mbox_sha1sum'] = undefined; break;
127+
case 'account': query['statement.' + queryKey + '.account'] = undefined; break;
128+
case 'mailto': query['statement.' + queryKey + '.mbox'] = undefined; break;
129+
case 'openid': query['statement.' + queryKey + '.openid'] = undefined; break;
130+
case 'mbox_sha1sum': query['statement.' + queryKey + '.mbox_sha1sum'] = undefined; break;
139131
}
140132
}
141133
});

0 commit comments

Comments
 (0)