Skip to content

Commit

Permalink
Updated frontent for activities
Browse files Browse the repository at this point in the history
+ Changed sessions to activities
+ Classes are no longer needed
+ Toolbar access fixed
+ Routes updated
+ Improved visuals
+ And many other things...
  • Loading branch information
Victorma committed May 29, 2017
1 parent bc43856 commit 840798f
Show file tree
Hide file tree
Showing 20 changed files with 827 additions and 570 deletions.
245 changes: 245 additions & 0 deletions app/public/js/controllers/activities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
/*
* Copyright 2016 e-UCM (http://www.e-ucm.es/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* This project has received funding from the European Union’s Horizon
* 2020 research and innovation programme under grant agreement No 644187.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0 (link is external)
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

angular.module('activitiesApp', ['ngStorage', 'services'])
.controller('ActivitiesCtrl', ['$scope', '$location', '$http', '$window', '$localStorage', 'Games', 'Versions',
'Activities', 'Role', 'CONSTANTS', 'QueryParams',
function ($scope, $location, $http, $window, $localStorage, Games, Versions, Activities, Role, CONSTANTS, QueryParams) {
$scope.$storage = $localStorage;
$scope.activity = {};
$scope.class = {};
$scope.game = {};

var getActivities = function () {
var queryClass = QueryParams.getQueryParam('class');
var queryGame = QueryParams.getQueryParam('game');
var queryVersion = QueryParams.getQueryParam('version');

var route = '/activities/my';
if (queryClass) {
route = '/classes/' + queryClass + '/activities/my';
} else if (queryGame && queryVersion) {
route = '/games/' + queryGame + '/versions/' + queryVersion + '/activities/my';
}

$http.get(CONSTANTS.PROXY + route).success(function (data) {
$scope.activities = data;
}).error(function (data, status) {
console.error('Error on get ' + route + ' ' + JSON.stringify(data) + ', status: ' + status);
});
};

getActivities();
$scope.classId = QueryParams.getQueryParam('class');
$scope.loading = false;

// LTI
$scope.lti = {};
$scope.lti.key = '';
$scope.lti.secret = '';

var myPrefix = $location.$$path.split('/')[3];
$scope.lti.launch = $location.$$protocol + '://' + $location.$$host + ':' + $location.$$port +
'/api/login/launch/' + myPrefix + '/' + CONSTANTS.PREFIX;

$http.get(CONSTANTS.PROXY + '/lti/keyid/' + $scope.gameId + '/' + $scope.versionId + '/' + $scope.classId).success(function (data) {
if (data && data.length > 0) {
$scope.lti.key = data[0]._id;
$scope.lti.secret = data[0].secret;
}
});

$scope.createLtiKey = function () {
if ($scope.lti.secret) {
$http.post(CONSTANTS.PROXY + '/lti', {
secret: $scope.lti.secret,
classId: $scope.classId,
versionId: $scope.versionId,
gameId: $scope.gameId
}).success(function (data) {
$scope.lti.key = data._id;
}).error(function (data, status) {
console.error('Error on get /lti' + JSON.stringify(data) + ', status: ' + status);
});
}
};

// FIN LTI

var doCreateActivity = function(name, gameId, versionId, classId) {
$http.post(CONSTANTS.PROXY + '/activities', {name: name, gameId: gameId,
versionId: versionId, classId: classId})
.success(function (activity) {
$http.get(CONSTANTS.PROXY + '/kibana/visualization/list/tch/' + gameId)
.success(function (data) {
var panels = [];
var uiStates = {};

// Add index
$http.post(CONSTANTS.PROXY + '/kibana/index/' + gameId + '/' + activity._id, {})
.success(function (data) {

}).error(function (data, status) {
console.error('Error on post /kibana/index/' + gameId + '/' + activity._id + ' ' +
JSON.stringify(data) + ', status: ' + status);
});

// Add dashboard
var numPan = 1;
if (data.length > 0) {
data.forEach(function (visualizationId) {
$http.post(CONSTANTS.PROXY + '/kibana/visualization/activity/' + gameId +
'/' + visualizationId + '/' + activity._id, {}).success(function (result) {
panels.push('{\"id\":\"' + visualizationId + '_' + activity._id +
'\",\"type\":\"visualization\",\"panelIndex\":' + numPan + ',' +
'\"size_x\":6,\"size_y\":4,\"col\":' + (1 + (numPan - 1 % 2)) + ',\"row\":' +
(numPan + 1 / 2) + '}');
uiStates['P-' + numPan] = {vis: {legendOpen: false}};
numPan++;

if (numPan > data.length) {
var dashboard = {
title: 'dashboard_' + activity._id,
hits: 0,
description: '',
panelsJSON: '[' + panels.toString() + ']',
optionsJSON: '{"darkTheme":false}',
uiStateJSON: JSON.stringify(uiStates),
version: 1,
timeRestore: true,
timeTo: 'now',
timeFrom: 'now-1h',
refreshInterval: {
display: '5 seconds',
pause: false,
section: 1,
value: 5000
},
kibanaSavedObjectMeta: {
searchSourceJSON: '{"filter":[{"query":{"query_string":{"query":"*","analyze_wildcard":true}}}]}'
}
};
$http.post(CONSTANTS.PROXY + '/kibana/dashboard/activity/' + activity._id, dashboard)
.success(function (data) {
goToActivity(activity);
}).error(function (data, status) {
console.error('Error on post /kibana/dashboard/activity/' + activity._id + ' ' +
JSON.stringify(data) + ', status: ' + status);
});
}
}).error(function (data, status) {
console.error('Error on post /kibana/visualization/activity/' + visualizationId + '/' + activity._id + ' ' +
JSON.stringify(data) + ', status: ' + status);
});
});
} else {
goToActivity(activity);
}
}).error(function (data, status) {
console.error('Error on post /kibana/visualization/list/' + gameId + ' ' +
JSON.stringify(data) + ', status: ' + status);
});
});
};

$scope.createActivity = function () {
if (!$scope.activity.gameId && !QueryParams.getQueryParam('game')) {
// It's necessary to pick a game
return;
}

if (!$scope.activity.classId && !QueryParams.getQueryParam('class')) {
// It's necessary to pick a class
return;
}

var activityName = $scope.activity.name ? $scope.activity.name : 'New activity';
$scope.gameId = $scope.activity.gameId ? $scope.activity.gameId : QueryParams.getQueryParam('game');
$scope.versionId = QueryParams.getQueryParam('version');
$scope.classId = $scope.activity.classId ? $scope.activity.classId : QueryParams.getQueryParam('class');

if (!$scope.versionId) {
$http.get(CONSTANTS.PROXY + '/games/' + $scope.gameId + '/versions')
.success(function (data) {
$scope.versionId = data[0]._id;
doCreateActivity(activityName, $scope.gameId, $scope.versionId, $scope.classId);
}).error(function (data, status) {
console.error('Error on get /games/' + QueryParams.getQueryParam('game') + '/versions/' +
QueryParams.getQueryParam('version') + '/activities' + JSON.stringify(data) + ', status: ' + status);
});
} else {
doCreateActivity(activityName, $scope.gameId, $scope.versionId, $scope.classId);
}
};

var goToActivity = function(activity) {
$window.location = 'data' + '?activity=' + activity._id;
};

$scope.isTeacher = function () {
return Role.isTeacher();
};

$scope.startActivity = function (activity) {
$scope.loading = true;
$http.post(CONSTANTS.PROXY + '/activities/' + activity._id + '/event/start').success(function (s) {
$scope.loading = false;
$scope.selectedActivity = s;
getActivities();
}).error(function (data, status) {
console.error('Error on get /activities/' + activity._id + '/event/start ' +
JSON.stringify(data) + ', status: ' + status);
$scope.loading = false;
});
};

$scope.endActivity = function (activity) {
$scope.loading = true;
$http.post(CONSTANTS.PROXY + '/activities/' + activity._id + '/event/end').success(function (s) {
$scope.loading = false;
$scope.selectedActivity = s;
getActivities();
}).error(function (data, status) {
console.error('Error on get /activities/' + activity._id + '/event/end ' +
JSON.stringify(data) + ', status: ' + status);
$scope.loading = false;
});
};

$scope.activityOpenedError = '';
$scope.deleteActivity = function (activityObj) {
if (activityObj) {
$http.delete(CONSTANTS.PROXY + '/activities/' + activityObj._id).success(function () {
$scope.activityOpenedError = '';
getActivities();
}).error(function (data, status) {
$scope.activityOpenedError = data;
console.error('Error on delete /activities/' + activityObj._id + ' ' +
JSON.stringify(data) + ', status: ' + status);
});
}
};

$scope.activityState = function (activity) {
return activity && activity.start && !activity.end;
};

}
]);
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

'use strict';

angular.module('sessionApp', ['myApp', 'ngStorage', 'services'])
angular.module('activityApp', ['myApp', 'ngStorage', 'services'])
.config(['$locationProvider',
function ($locationProvider) {
$locationProvider.html5Mode(false);
Expand All @@ -30,37 +30,40 @@ angular.module('sessionApp', ['myApp', 'ngStorage', 'services'])
new ColumnProgress(angular.element(element).children('.score-marker')[0], scope.result.score);
};
})
.controller('SessionCtrl', ['$scope', '$location', '$http', 'SessionsId', 'Results', 'Versions', 'QueryParams', '$sce', 'CONSTANTS', 'Role',
function ($scope, $location, $http, SessionsId, Results, Versions, QueryParams, $sce, CONSTANTS, Role) {
.controller('ActivityCtrl', ['$scope', '$location', '$http', 'ActivitiesId', 'Results', 'Versions', 'QueryParams', '$sce', 'CONSTANTS', 'Role',
function ($scope, $location, $http, ActivitiesId, Results, Versions, QueryParams, $sce, CONSTANTS, Role) {

$scope.isTeacher = function () {
return Role.isTeacher();
};

$scope.refreshResults = function () {
var rawResults = Results.query({
id: $scope.session._id
id: $scope.activity._id
},
function () {
calculateResults(rawResults);
});
};

$scope.session = SessionsId.get({
id: QueryParams.getQueryParam('session')
}, function () {
$scope.version = Versions.get({
gameId: $scope.session.gameId,
versionId: $scope.session.versionId
var activityId = QueryParams.getQueryParam('activity');
if (activityId) {
$scope.activity = ActivitiesId.get({
id: activityId
}, function () {
$scope.refreshResults();
if (!$scope.session.end) {
setInterval(function () {
$scope.refreshResults();
}, 10000);
}
$scope.version = Versions.get({
gameId: $scope.activity.gameId,
versionId: $scope.activity.versionId
}, function () {
$scope.refreshResults();
if (!$scope.activity.end) {
setInterval(function () {
$scope.refreshResults();
}, 10000);
}
});
});
});
}

var evalExpression = function (expression, defaultValue) {
try {
Expand All @@ -73,7 +76,7 @@ angular.module('sessionApp', ['myApp', 'ngStorage', 'services'])

var dashboardLink = function (userName) {
var url = CONSTANTS.KIBANA + '/app/kibana#/dashboard/dashboard_' +
QueryParams.getQueryParam('session') + '?embed=true_g=(refreshInterval:(display:\'5%20seconds\',' +
QueryParams.getQueryParam('activity') + '?embed=true_g=(refreshInterval:(display:\'5%20seconds\',' +
'pause:!f,section:1,value:5000),time:(from:now-1h,mode:quick,to:now))';
if (url.startsWith('localhost')) {
url = 'http://' + url;
Expand Down Expand Up @@ -167,14 +170,14 @@ angular.module('sessionApp', ['myApp', 'ngStorage', 'services'])
});
delete player.alerts;
player.levels = levels;
player.$save({id: $scope.session._id}, function () {
player.$save({id: $scope.activity._id}, function () {
$scope.player = null;
$scope.refreshResults();
});
};

$scope.deleteUserData = function (name) {
$http.delete(CONSTANTS.PROXY + '/sessions/data/' + $scope.session._id + '/' + name).success(function () {
$http.delete(CONSTANTS.PROXY + '/activities/data/' + $scope.activity._id + '/' + name).success(function () {
}).error(function (err) {
console.error(err);
});
Expand Down
Loading

0 comments on commit 840798f

Please sign in to comment.