Skip to content

Commit 65a57de

Browse files
committed
Merge pull request #785 from LearningLocker/issue/mongodate
Fix instances of MongoDate that did not correctly use microtime
2 parents 43584a9 + cdfaf1b commit 65a57de

File tree

12 files changed

+36
-33
lines changed

12 files changed

+36
-33
lines changed

app/controllers/LrsController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ public function show($lrs_id) {
134134
$dashboard = new \app\locker\data\dashboards\LrsDashboard($lrs_id);
135135
return View::make('partials.lrs.dashboard', array_merge($this->getLrs($lrs_id), [
136136
'stats' => $dashboard->getStats(),
137-
'graph_data' => $dashboard->getGraphData(),
138137
'dash_nav' => true,
139138
'client' => (new \Client)->where('lrs_id', $lrs_id)->first()
140139
]));

app/controllers/SiteController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function index(){
3838
'site' => $site,
3939
'list' => $list,
4040
'stats' => $admin_dashboard->getFullStats(),
41-
'graph_data' => $admin_dashboard->getGraphData()
41+
'dash_nav' => true
4242
]);
4343

4444
}

app/locker/data/dashboards/BaseDashboard.php

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

33
use Locker\Repository\Lrs\EloquentRepository as LrsRepo;
4+
use Carbon\Carbon as Carbon;
5+
use MongoDate;
46

57
abstract class BaseDashboard extends \app\locker\data\BaseData {
68
protected $lrs_repo;
@@ -144,16 +146,15 @@ public function actorCount(){
144146
*
145147
**/
146148
public function getStatementNumbersByDate(\DateTime $startDate = null, \DateTime $endDate = null) {
147-
// If neither of the dates are set, default to the last 30 days.
148-
if ($startDate === null && $endDate === null) {
149-
$startDate = \Carbon\Carbon::now()->subMonth();
150-
$endDate = \Carbon\Carbon::now();
151-
}
149+
// If neither of the dates are set, default to the last week
150+
$startDate = $startDate ? Carbon::instance($startDate) : Carbon::now()->subWeek();
151+
$endDate = $endDate ? Carbon::instance($endDate) : Carbon::now();
152+
152153

153154
// Create the timestamp filter.
154155
$timestamp = [];
155-
if ($startDate !== null) $timestamp['$gte'] = new \MongoDate($startDate->getTimestamp());
156-
if ($endDate !== null) $timestamp['$lte'] = new \MongoDate($endDate->getTimestamp());
156+
if ($startDate !== null) $timestamp['$gte'] = new MongoDate($startDate->timestamp, $startDate->micro);
157+
if ($endDate !== null) $timestamp['$lte'] = new MongoDate($endDate->timestamp, $endDate->micro);
157158

158159
$match = [
159160
'timestamp'=> $timestamp

app/locker/repository/Statement/EloquentIndexer.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use \Locker\XApi\Helpers as XApiHelpers;
66
use \Jenssegers\Mongodb\Eloquent\Builder as Builder;
77
use \Illuminate\Database\Eloquent\Model as Model;
8+
use Carbon\Carbon as Carbon;
9+
use MongoDate;
810

911
interface IndexerInterface {
1012
public function index(IndexOptions $opts);
@@ -45,20 +47,22 @@ public function index(IndexOptions $opts) {
4547
$key = 'stored';
4648
$op = '>';
4749
return $builder->where(function ($query) use ($key, $value, $op) {
48-
$date = new \MongoDate(strtotime($value));
50+
$date = new Carbon($value);
51+
$mongodate = new MongoDate($date->timestamp, $date->micro);
4952
return $query
50-
->orWhere($key, $op, $date)
51-
->orWhere('refs.'.$key, $op, $date);
53+
->orWhere($key, $op, $mongodate)
54+
->orWhere('refs.'.$key, $op, $mongodate);
5255
});
5356
},
5457
'until' => function ($value, $builder, IndexOptions $opts) {
5558
$key = 'stored';
5659
$op = '<=';
5760
return $builder->where(function ($query) use ($key, $value, $op) {
58-
$date = new \MongoDate(strtotime($value));
61+
$date = new Carbon($value);
62+
$mongodate = new MongoDate($date->timestamp, $date->micro);
5963
return $query
60-
->orWhere($key, $op, $date)
61-
->orWhere('refs.'.$key, $op, $date);
64+
->orWhere($key, $op, $mongodate)
65+
->orWhere('refs.'.$key, $op, $mongodate);
6266
});
6367
},
6468
'active' => function ($value, $builder, IndexOptions $opts) {

app/locker/repository/Statement/EloquentLinker.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use \Illuminate\Database\Eloquent\Model as Model;
44
use \Illuminate\Database\Eloquent\Collection as Collection;
55
use \Locker\Helpers\Helpers as Helpers;
6+
use Carbon\Carbon as Carbon;
7+
use MongoDate;
68

79
interface LinkerInterface {
810
public function updateReferences(array $statements, StoreOptions $opts);
@@ -140,8 +142,10 @@ private function setRefs(\stdClass $statement, array $refs, StoreOptions $opts)
140142
->update([
141143
'refs' => array_map(function ($ref) {
142144
$statement = Helpers::replaceFullStop(json_decode(json_encode($ref->statement), true));
143-
$statement['stored'] = new \MongoDate( strtotime($statement['stored']));
144-
$statement['timestamp'] = new \MongoDate( strtotime($statement['timestamp']));
145+
$stored = new Carbon($statement['stored']);
146+
$timestamp = new Carbon($statement['timestamp']);
147+
$statement['stored'] = new MongoDate($stored->timestamp, $stored->micro);
148+
$statement['timestamp'] = new MongoDate($timestamp->timestamp, $timestamp->micro);
145149
return $statement;
146150
}, $refs)
147151
]);

app/routes.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,10 @@
2222
if( Auth::check() ){
2323
$site = \Site::first();
2424

25-
$admin_dashboard = new \app\locker\data\dashboards\AdminDashboard();
2625

2726
//if super admin, show site dashboard, otherwise show list of LRSs can access
2827
if( Auth::user()->role == 'super' ){
29-
$list = Lrs::all();
30-
return View::make('partials.site.dashboard', array(
31-
'site' => $site,
32-
'list' => $list,
33-
'stats' => $admin_dashboard->getFullStats(),
34-
'graph_data' => $admin_dashboard->getGraphData(),
35-
'dash_nav' => true
36-
));
28+
return Redirect::route('site.index');
3729
}else{
3830
$lrs = Lrs::where('users._id', \Auth::user()->_id)->get();
3931
return View::make('partials.lrs.list', array('lrs' => $lrs, 'list' => $lrs, 'site' => $site));
@@ -130,6 +122,7 @@
130122
|------------------------------------------------------------------
131123
*/
132124
Route::get('site', array(
125+
'as' => 'site.index',
133126
'uses' => 'SiteController@index',
134127
));
135128
Route::get('site/settings', array(

app/views/partials/lrs/dashboard.blade.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,5 @@
3535

3636
<script type='text/javascript'>
3737
window.LL.stats = {{ json_encode( $stats ) }};
38-
window.LL.graph_data = {{ json_encode( $graph_data ) }};
3938
</script>
4039
@stop

app/views/partials/site/dashboard.blade.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,5 @@
3030

3131
<script type='text/javascript'>
3232
window.LL.stats = {{ json_encode( $stats ) }};
33-
window.LL.graph_data = {{ json_encode( $graph_data ) }};
3433
</script>
3534
@stop

public/assets/js/admin/controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ define([
3737
App.layouts.main.mainRegion.show( new LoadingView );
3838

3939
var stats = new StatsModel(window.LL.stats);
40-
var graph_model = new GraphModel(window.LL.graph_data);
40+
var graph_model = new GraphModel();
4141

4242

4343
var statsView = new Stats({ model: stats });

public/assets/js/admin/models/site/GraphModel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ define([
44
], function(_, Backbone) {
55
var end = new Date();
66
var start = new Date();
7-
start.setMonth(end.getMonth() - 1);
7+
start.setDate(end.getDate() - 7);
88

99
var GraphModel = Backbone.Model.extend({
1010
defaults:{

0 commit comments

Comments
 (0)