Skip to content

Commit

Permalink
Updated backend for multi user effect
Browse files Browse the repository at this point in the history
Also created api for routing data to server
  • Loading branch information
droarty committed Dec 5, 2014
1 parent 4ef4c11 commit 141f4fd
Show file tree
Hide file tree
Showing 7 changed files with 299 additions and 47 deletions.
86 changes: 68 additions & 18 deletions data/getStudentAnswer.php
Original file line number Diff line number Diff line change
@@ -1,41 +1,91 @@
<?php
include 'settings.php';

// Create connection
$sql = new mysqli($servername, $username, $password, $db);


//simple script to store and retrieve multiple student answers...
session_start();
$r='{"errMsg":"No Action Provided"}';
if(!isset($_SESSION['saa'])) $_SESSION['saa']= array();
$saa=$_SESSION['saa'];
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
if(!isset($request->action)) {
echo $r. var_dump($request);
if(!isset($_REQUEST['action'])) {
echo $r. var_dump($_REQUEST);
exit();
}
switch($request->action){
switch($_REQUEST['action']){
case 'save':
if(!isset($request->name)) $r='{"errMsg":"No name"}'. var_dump($request);
if(!isset($request->name)) $r='{"errMsg":"No name"}';
else {
$_SESSION['saa'][$request->name]=json_encode($request->value);
$r='{"data":'.$_SESSION['saa'][$request->name].'}';
if(isset($request->id)){
/* prepared statements didnt work on production server??
$q="update bp_answers set name=?, value=? where id=?";
$stmt=$sql->prepare($q);
$stmt->bind_param("ssi", $request->name, json_encode($request->value), $request->id);
$stmt->execute();
*/
$sql->query("update bp_answers set name='".$sql->real_escape_string($request->name)."', value='".$sql->real_escape_string(json_encode($request->value))."' where id=".$sql->real_escape_string($request->id)."");
}
else{
/* prepared statements didnt work on production server??
$q="insert into bp_answers (name,value) values (?,?)";
$stmt=$sql->prepare($q);
$stmt->bind_param("ss", $request->name, json_encode($request->value));
$stmt->execute();
*/
$sql->query("insert into bp_answers (name, value) values ('".$sql->real_escape_string($request->name)."', '".$sql->real_escape_string(json_encode($request->value))."')");
$request->id=$sql->insert_id;
}
$r= json_encode($request);
}
break;
case 'fetch':
if(isset($request->name)&&isset($saa[$request->name])) $r='{"data":'.$saa[$request->name].'}';
if(isset($request->name)){
/* prepared statements didnt work on production server??
$stmt=$sql->prepare("select * from bp_answers where name=?");
$stmt->bind_param("s",$request->name);
$stmt->execute();
$stmt->bind_result($request->id, $request->name, $request->value);
if($stmt->fetch()){
$request->value= json_decode($request->value);
$r= json_encode($request);
}
*
*/
$res=$sql->query("select * from bp_answers where name ='".$sql->real_escape_string($request->name)."'");
if($row=$res->fetch_assoc()){
$row['value']= json_decode($row['value']);
$r= json_encode ($row);
}
else $r='{"errMsg":"No data for that name"}';
}
else $r='{"errMsg":"No data for that name"}';
break;
case 'fetchAll':
$sep="";$r="";
foreach ($saa as $nm=>$data){
$r.=$sep.$data;
$sep=", ";
$r="";
/* prepared statements didnt work on production server??
$stmt=$sql->prepare("select value from bp_answers");
$stmt->execute();
$stmt->bind_result($value);
$sep="";
while($stmt->fetch()){
$r.=$sep.$value;
$sep=",";
}
$r="[".$r."]";
*
*/
$sep="";
$res=$sql->query("select value from bp_answers where value is not null and value !='' ");
while($row=$res->fetch_assoc()){
$r.=$sep.$row['value'];
$sep=",";
}
$r= "[".$r."]";
break;
case 'clearAll':
unset($_SESSION['saa']);
$stmt=$sql->query("delete from bp_answers");
$r='{"msg":"OK"}';
}
echo $r;


$sql->close();
?>
6 changes: 6 additions & 0 deletions data/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
$servername = "dotals.net";
$username = "dotalsne_balance";
$password = "!qoHK[5[9fS3";
$db='dotalsne_balancepoint';
?>
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

<script src="javascript/angular/angular.js"></script>
<script src="javascript/angular/angular-route.js"></script>
<script src="javascript/angular/angular-resource.js"></script>
<script src="javascript/jquery/jquery.min.js"></script>
<script src="javascript/bootstrap/js/bootstrap.min.js"></script>
<script src="javascript/bootstrap/js/ui-bootstrap-tpls-0.12.0.min.js"></script>
<script src="javascript/d3/d3.min.js"></script>
<script src="javascript/js/datacontroller.js"></script>
<script src="javascript/js/controllers.js"></script>
<script src="javascript/js/app.js"></script>
</head>
Expand Down
2 changes: 1 addition & 1 deletion javascript/js/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var bpApp=angular.module("bpApp",["ngRoute","bpControllers"]);
var bpApp=angular.module("bpApp",["ngRoute","bpControllers", "DataServices"]);

bpApp.config(['$routeProvider',
function($routeProvider){
Expand Down
69 changes: 41 additions & 28 deletions javascript/js/controllers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var bpControllers=angular.module('bpControllers',[]);


/* Get name of student, then fetch studentAnswer or create a new one */
bpControllers.controller('NameCtrl', ['$scope','$rootScope', '$location', '$http',
function ($scope,$rootScope,$location, $http) {
bpControllers.controller('NameCtrl', ['$scope','$rootScope', '$location', 'DataSrc',
function ($scope,$rootScope,$location, DataSrc) {
var rt=$rootScope;
activateTab("#/name");
$scope.msg="";
Expand All @@ -14,15 +15,16 @@ bpControllers.controller('NameCtrl', ['$scope','$rootScope', '$location', '$http
$scope.$watch('name',function(newval, oldval){
//check for existing data on backend only if there is no existing .sa or the name is different...
if(newval&&(!rt.sa||rt.sa.name!=newval)){
$scope.btnMsg="Checking Name..."
$http.post('data/getStudentAnswer.php',{"action":"fetch", "name":$scope.name})
.success(
$scope.btnMsg="Checking Name...";
//DataServices.post('data/getStudentAnswer.php',{"action":"fetch", "name":$scope.name})
DataSrc.fetch({name:$scope.name},
function(data) {
if(data.data){
if(data.value){
//if data exist, indicate to student, add data to scope...
$scope.msg="Welcome Back "+$scope.name;
$scope.btnMsg="Continue";
$rootScope.sa=data.data;
$rootScope.sa=data.value;
$rootScope.id=data.id;
}
else {
$scope.msg="";
Expand All @@ -38,11 +40,14 @@ bpControllers.controller('NameCtrl', ['$scope','$rootScope', '$location', '$http
$scope.start=function(){
if(!$scope.name) $scope.msg="You need to enter a name to continue.";
else {
if(!rt.sa||rt.sa.name!=$scope.name) rt.sa={"name":$scope.name};
if(!rt.sa||rt.sa.name!=$scope.name){
rt.sa={"name":$scope.name};
rt.id=null;
}
//create or save record...
$http.post('data/getStudentAnswer.php',{"action":"save", "name":$scope.name, "value":rt.sa})
.success(
DataSrc.save({"name":$scope.name, "value":rt.sa, "id":rt.id},
function(data) {
if(data.id) $rootScope.id=data.id;
$location.path("/predict");
}
);
Expand All @@ -51,19 +56,21 @@ bpControllers.controller('NameCtrl', ['$scope','$rootScope', '$location', '$http

//provide a way to clear all students from demo...
$scope.clear=function(){
$http.post('data/getStudentAnswer.php',{"action":"clearAll"})
.success(
DataSrc.clearAll({},
function(data) {
alert("All Cleared");
}
);
$scope.name="";
rt.sa=null;
rt.id=null;
}
}
]);

/* get student prediction and save it */
bpControllers.controller('PredictCtrl', ['$scope','$rootScope', '$location','$http',
function ($scope,$rootScope,$location,$http) {
bpControllers.controller('PredictCtrl', ['$scope','$rootScope', '$location','DataSrc',
function ($scope,$rootScope,$location,DataSrc) {
var rt=$rootScope;
activateTab("#/predict");
//redirect to home page if we don't have a student answeo object...
Expand Down Expand Up @@ -93,9 +100,9 @@ bpControllers.controller('PredictCtrl', ['$scope','$rootScope', '$location','$ht
if(rt.sa.prediction==undefined) $scope.msg="Can't move forward yet... you need to make a prediction by sliding the triangle to where you think Ben should pick up the bar."
else{
//create or save record...
$http.post('data/getStudentAnswer.php',{"action":"save", "name":rt.sa.name, "value":rt.sa})
.success(
DataSrc.save({"name":rt.sa.name, "value":rt.sa, "id":rt.id},
function(data) {
if(data.id) $rootScope.id=data.id;
$location.path("/comparePrediction");
}
);
Expand All @@ -107,23 +114,29 @@ bpControllers.controller('PredictCtrl', ['$scope','$rootScope', '$location','$ht
]);

/* fetch all student answers and display */
bpControllers.controller('ComparePredictionCtrl', ['$scope','$rootScope', '$location','$http',
function ($scope,$rootScope, $location,$http) {
bpControllers.controller('ComparePredictionCtrl', ['$scope','$rootScope', '$location','DataSrc',
function ($scope,$rootScope, $location,DataSrc) {
var rt=$rootScope;
activateTab("#/comparePredictions");
//redirect to home page if we don't have a student answeo object...
if(!rt.sa) $location.path("/name");
if(!rt.sa) {
$location.path("/name");
return;
}
//redraw my prediction...
if(rt.sa&&rt.sa.prediction) $("#myFulcrum").attr("transform","translate("+(parseInt(rt.sa.prediction)+300)+",106)");
//get all the other student's data...
$http.post('data/getStudentAnswer.php',{"action":"fetchAll"})
.success(
DataSrc.fetchAll({},
function(data) {
//create an array of fulcrum positions...
//TO-DO embellish so identical positions don't pile up on top of each other'
$scope.fulcrumArray=[];
for(var i=0;i<data.length;i++){
if(data[i].prediction!=undefined&&data[i].name!=rt.sa.name) $scope.fulcrumArray.push("translate("+(parseInt(data[i].prediction)+300)+",110)");
if(data[i].prediction!=undefined&&data[i].name!=rt.sa.name) {
var txt="translate("+(parseInt(data[i].prediction)+300)+",110)";
//need to avoid duplicates as ngRepeat does not seem to like them...
if($scope.fulcrumArray.indexOf(txt)==-1) $scope.fulcrumArray.push(txt);
}
}
if($scope.fulcrumArray.length>1) $scope.prompt="Your answer is the red triangle below. Your classmates are blue. Did you get the same answer as other people? With your group, try and decide on the best guess.";
else if($scope.fulcrumArray.length>0) $scope.prompt="Your answer is the red triangle below. One other classmate's is blue. Refresh this page when more students have submitted their prediction, then decide on the best guess. ";
Expand All @@ -138,22 +151,22 @@ bpControllers.controller('ComparePredictionCtrl', ['$scope','$rootScope', '$loca


/* allow student to choose information needed to solve */
bpControllers.controller('MoreInfoCtrl', ['$scope','$rootScope', '$location','$http',
function ($scope,$rootScope, $location,$http) {
bpControllers.controller('MoreInfoCtrl', ['$scope','$rootScope', '$location','DataSrc',
function ($scope,$rootScope, $location,DataSrc) {
activateTab("#/moreInfo");
}
]);

/* Student documents and draws solution */
bpControllers.controller('SolveItCtrl', ['$scope','$rootScope', '$location','$http',
function ($scope,$rootScope, $location,$http) {
bpControllers.controller('SolveItCtrl', ['$scope','$rootScope', '$location','DataSrc',
function ($scope,$rootScope, $location,DataSrc) {
activateTab("#/solveIt");
}
]);

/* fetch all student solutions and display */
bpControllers.controller('CompareSolutionCtrl', ['$scope','$rootScope', '$location','$http',
function ($scope,$rootScope, $location,$http) {
bpControllers.controller('CompareSolutionCtrl', ['$scope','$rootScope', '$location','DataSrc',
function ($scope,$rootScope, $location,DataSrc) {
activateTab("#/compareSolution");
}
]);
Expand Down
Loading

0 comments on commit 141f4fd

Please sign in to comment.