Skip to content

Commit e0157f7

Browse files
committed
Fixes report graph.
1 parent 561968f commit e0157f7

File tree

2 files changed

+71
-54
lines changed

2 files changed

+71
-54
lines changed

app/models/Report.php

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,62 @@ 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+
protected $instructorQuery = [
17+
'statement.context.instructor.account',
18+
'statement.context.instructor.mbox',
19+
'statement.context.instructor.openid',
20+
'statement.context.instructor.mbox_sha1sum'
21+
];
1622

1723
public function getFilterAttribute() {
1824
$reportArr = $this->toArray();
1925
$filter = [];
2026

2127
if (isset($reportArr['query'])) $filter['filter'] = json_encode($reportArr['query']);
2228
if (isset($reportArr['since'])) {
23-
$filter['since'] = (new \Carbon\Carbon($reportArr['since']))->toIso8601String();
29+
$filter['since'] = $this->constructDate($reportArr['since']);
2430
}
2531
if (isset($reportArr['until'])) {
26-
$filter['until'] = (new \Carbon\Carbon($reportArr['until']))->toIso8601String();
32+
$filter['until'] = $this->constructDate($reportArr['until']);
2733
}
2834

2935
return $filter;
3036
}
3137

38+
private function constructOr($key, $value) {
39+
$query = [];
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+
return $filter;
48+
} else {
49+
return [$key => $actor_id];
50+
}
51+
}
52+
return $query;
53+
}
54+
55+
private function constructDate($date) {
56+
return (new \Carbon\Carbon($date))->toIso8601String();
57+
}
58+
3259
public function getMatchAttribute() {
3360
$reportArr = $this->toArray();
3461
$match = [];
62+
$actorMatch = [];
63+
$instructorMatch = [];
3564
$query = isset($reportArr['query']) ? (array) $reportArr['query'] : null;
3665

3766
if (is_array($query) && count($query) > 0 && !isset($query[0])) {
3867
foreach ($query as $key => $value) {
3968
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-
}
69+
$actorMatch['$or'][] = $this->constructOr($key, $value);
70+
} else if (in_array($key, $this->instructorQuery)) {
71+
$instructorMatch['$or'][] = $this->constructOr($key, $value);
5272
} else {
5373
if (is_array($value)) {
5474
$match[$key] = ['$in' => $value];
@@ -59,8 +79,8 @@ public function getMatchAttribute() {
5979
}
6080
}
6181

62-
$since = isset($reportArr['since']) ? (new \Carbon\Carbon($reportArr['since']))->toIso8601String() : null;
63-
$until = isset($reportArr['until']) ? (new \Carbon\Carbon($reportArr['until']))->toIso8601String() : null;
82+
$since = isset($reportArr['since']) ? $this->constructDate($reportArr['since']) : null;
83+
$until = isset($reportArr['until']) ? $this->constructDate($reportArr['until']) : null;
6484

6585
if ($since || $until) {
6686
$match['statement.timestamp'] = [];
@@ -71,8 +91,13 @@ public function getMatchAttribute() {
7191
if ($until) {
7292
$match['statement.timestamp']['$lte'] = $until;
7393
}
94+
$andMatch = ['$and' => []];
95+
if (!empty($actorMatch)) $andMatch['$and'][] = $actorMatch;
96+
if (!empty($instructorMatch)) $andMatch['$and'][] = $instructorMatch;
97+
if (!empty($match)) $andMatch['$and'][] = $match;
98+
//echo(json_encode($andMatch));die;
7499

75-
return $match;
100+
return $andMatch;
76101
}
77102

78103
public function getWhereAttribute() {
@@ -96,8 +121,8 @@ public function getWhereAttribute() {
96121
array_push($wheres, ['orArray', 'or', $actorArray]);
97122
}
98123

99-
$since = isset($reportArr['since']) ? (new \Carbon\Carbon($reportArr['since']))->toIso8601String() : null;
100-
$until = isset($reportArr['until']) ? (new \Carbon\Carbon($reportArr['until']))->toIso8601String() : null;
124+
$since = isset($reportArr['since']) ? $this->constructDate($reportArr['since']) : null;
125+
$until = isset($reportArr['until']) ? $this->constructDate($reportArr['until']) : null;
101126

102127
if ($since && $until) {
103128
$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)