From 141f4fdba5aa775b54c67efb070bf754b73fc549 Mon Sep 17 00:00:00 2001 From: DRoarty Date: Thu, 4 Dec 2014 23:56:42 -0600 Subject: [PATCH] Updated backend for multi user effect Also created api for routing data to server --- data/getStudentAnswer.php | 86 +++++++++++++---- data/settings.php | 6 ++ index.html | 2 + javascript/js/app.js | 2 +- javascript/js/controllers.js | 69 +++++++------ javascript/js/controllers_1.js | 165 ++++++++++++++++++++++++++++++++ javascript/js/datacontroller.js | 16 ++++ 7 files changed, 299 insertions(+), 47 deletions(-) create mode 100644 data/settings.php create mode 100644 javascript/js/controllers_1.js create mode 100644 javascript/js/datacontroller.js diff --git a/data/getStudentAnswer.php b/data/getStudentAnswer.php index 6fe9298..aefab4b 100644 --- a/data/getStudentAnswer.php +++ b/data/getStudentAnswer.php @@ -1,41 +1,91 @@ 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(); ?> diff --git a/data/settings.php b/data/settings.php new file mode 100644 index 0000000..67ed0c0 --- /dev/null +++ b/data/settings.php @@ -0,0 +1,6 @@ + diff --git a/index.html b/index.html index 2088d37..eb89907 100644 --- a/index.html +++ b/index.html @@ -19,10 +19,12 @@ + + diff --git a/javascript/js/app.js b/javascript/js/app.js index 2339f75..1cf23a9 100644 --- a/javascript/js/app.js +++ b/javascript/js/app.js @@ -1,4 +1,4 @@ -var bpApp=angular.module("bpApp",["ngRoute","bpControllers"]); +var bpApp=angular.module("bpApp",["ngRoute","bpControllers", "DataServices"]); bpApp.config(['$routeProvider', function($routeProvider){ diff --git a/javascript/js/controllers.js b/javascript/js/controllers.js index e2e2956..0fd4abb 100644 --- a/javascript/js/controllers.js +++ b/javascript/js/controllers.js @@ -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=""; @@ -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=""; @@ -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"); } ); @@ -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... @@ -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"); } ); @@ -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;i1) $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. "; @@ -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"); } ]); diff --git a/javascript/js/controllers_1.js b/javascript/js/controllers_1.js new file mode 100644 index 0000000..e2e2956 --- /dev/null +++ b/javascript/js/controllers_1.js @@ -0,0 +1,165 @@ +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) { + var rt=$rootScope; + activateTab("#/name"); + $scope.msg=""; + $scope.btnMsg="Create New Student Record"; + window.setTimeout(function(){$("input[ng-model='name']").focus();},500); + if(rt.sa) $scope.name=rt.sa.name; + + //watch for changes to name, check if it exists on backend... + $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( + function(data) { + if(data.data){ + //if data exist, indicate to student, add data to scope... + $scope.msg="Welcome Back "+$scope.name; + $scope.btnMsg="Continue"; + $rootScope.sa=data.data; + } + else { + $scope.msg=""; + $scope.btnMsg="Create New Student Record" + } + + } + ); + } + }) + + //create or save record, move on to next page... + $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}; + //create or save record... + $http.post('data/getStudentAnswer.php',{"action":"save", "name":$scope.name, "value":rt.sa}) + .success( + function(data) { + $location.path("/predict"); + } + ); + } + } + + //provide a way to clear all students from demo... + $scope.clear=function(){ + $http.post('data/getStudentAnswer.php',{"action":"clearAll"}) + .success( + function(data) { + alert("All Cleared"); + } + ); + } + } +]); + +/* get student prediction and save it */ +bpControllers.controller('PredictCtrl', ['$scope','$rootScope', '$location','$http', + function ($scope,$rootScope,$location,$http) { + var rt=$rootScope; + activateTab("#/predict"); + //redirect to home page if we don't have a student answeo object... + if(!rt.sa) $location.path("/name"); + //if we already have a prediction, draw it... + if(rt.sa&&rt.sa.prediction) $("#fulcrum").attr("transform","translate("+(parseInt(rt.sa.prediction)+300)+",106)"); + //wired up to the svg element on mouse move to capture x coordinate... + $scope.movePoint=function($event){ + //allowmove is set on click events on the fulcrum element, true on mousedown, false on mouse up or leave... + if($scope.allowmove){ + var svg=$("#pointPicker");//this is the svg element with the drawing of the weights + var x=svg.offset().left;//gets left edge of svg element + if(!rt.sa) rt.sa={}; + //uses event x coordinate to calculate distance from center of drawing... + rt.sa.prediction=$event.clientX-x-300; + //changes posistion of the fulcrum... + if(rt.sa.prediction!=undefined) $("#fulcrum").attr("transform","translate("+(parseInt(rt.sa.prediction)+300)+",106)"); + } + } + + //create or save record, move on to next page... + $scope.savePrediction=function(){ + if(!rt.sa){ + $location.path("/name"); + } + else { + 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( + function(data) { + $location.path("/comparePrediction"); + } + ); + } + } + } + + } +]); + +/* fetch all student answers and display */ +bpControllers.controller('ComparePredictionCtrl', ['$scope','$rootScope', '$location','$http', + function ($scope,$rootScope, $location,$http) { + var rt=$rootScope; + activateTab("#/comparePredictions"); + //redirect to home page if we don't have a student answeo object... + if(!rt.sa) $location.path("/name"); + //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( + 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;i1) $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. "; + else $scope.prompt="Good job, your answer is the red triangle below. None of your classmates have submitted thier prediction yet. Refresh this page when more students have submitted their prediction, then decide on the best guess."; + + } + ); + $scope.prompt="Loading..."; + + } +]); + + +/* allow student to choose information needed to solve */ +bpControllers.controller('MoreInfoCtrl', ['$scope','$rootScope', '$location','$http', + function ($scope,$rootScope, $location,$http) { + activateTab("#/moreInfo"); + } +]); + +/* Student documents and draws solution */ +bpControllers.controller('SolveItCtrl', ['$scope','$rootScope', '$location','$http', + function ($scope,$rootScope, $location,$http) { + activateTab("#/solveIt"); + } +]); + +/* fetch all student solutions and display */ +bpControllers.controller('CompareSolutionCtrl', ['$scope','$rootScope', '$location','$http', + function ($scope,$rootScope, $location,$http) { + activateTab("#/compareSolution"); + } +]); + +//sets active nav link in the top nav bar... +var activateTab=function(href){ + $("ul.nav li.active").removeClass("active"); + $("ul.nav a[href='"+href+"']").parent().addClass("active"); +} \ No newline at end of file diff --git a/javascript/js/datacontroller.js b/javascript/js/datacontroller.js new file mode 100644 index 0000000..4be28c7 --- /dev/null +++ b/javascript/js/datacontroller.js @@ -0,0 +1,16 @@ +var DataServices = angular.module('DataServices', ['ngResource']); + +//create a single service for fetching and setting data. +DataServices.factory('DataSrc', ['$resource', + function($resource){ + return $resource('data/getStudentAnswer.php',{}, + { + 'fetch':{method:'PUT',params:{"action":"fetch"}}, + 'save':{method:'PUT',params:{"action":"save"}}, + 'fetchAll':{method:'PUT',params:{"action":"fetchAll"}, "isArray":true}, + 'clearAll':{method:'PUT',params:{"action":"clearAll"}} + } + ) + } +]); +