| module.controller('surveysController',function($scope,$http,$window,alertService,Page){ | |
| $http.defaults.headers.common['authentication'] = authentication; | |
| $scope.Page = Page; // set the page title | |
| $scope.afterSubmit = false; // hide the submit buttons by default | |
| var survey_id=1; // temp var for survey_id because for moment there is only 1 survey | |
| $http.get(frontbaseurl+'/surveys/'+survey_id+'.json').then(function(response){ | |
| if (typeof response.data.data[0].Question[(id-1)] !== 'undefined') { // only proceed if ID is valid | |
| $scope.makeQuestion(response); | |
| } | |
| else if(id>0){ // if there are no more questions to answer... survey is complete so re-direct to surveys/complete (> 0 prevents the 'complete' method form re-directing to itself) | |
| $window.location.href = frontbaseurl+'surveys/complete/'; | |
| } | |
| else{ | |
| $http.get(frontbaseurl+'/users/auth_user.json').then(function(response){ | |
| if (response.data.data.group_id==3) { | |
| $scope.live = true; | |
| $scope.practice = false; | |
| } | |
| else if (response.data.data.group_id==4) { | |
| $scope.live = false; | |
| $scope.practice = true; | |
| } | |
| }); | |
| } | |
| }); | |
| $scope.makeQuestion=function (response) { | |
| Page.setTitle(id,response.data.data[0].Question[(id-1)].title); | |
| $http.get(frontbaseurl+'/questions/get_type/'+response.data.data[0].Question[(id-1)].id+'.json').then(function (res) { | |
| // initilaise an the answers for the multiple choice (or empty if slider type) | |
| var answers = []; | |
| // if the get_type method returns the 'slider' variable that is not null | |
| if (res.data.data.question_type=='slider') { | |
| $scope.slider = { | |
| value: 1, | |
| options: { | |
| floor: 0, | |
| ceil: 10 | |
| } | |
| }; | |
| $scope.question_type_id=2; | |
| } | |
| else if (res.data.data.question_type=='multiple') { | |
| $scope.slider=null; | |
| angular.forEach(response.data.data[0].Question[(id-1)].QuestionAnswer, function(value, key) { // for each answer append to object for the view | |
| answers[key]={ // important we use the offset for the key so we don't over-write previous for each | |
| id:value.id, | |
| title:value.title | |
| }; | |
| }); | |
| $scope.question_type_id=1; // pass the question types to the view | |
| } | |
| $scope.question_id=response.data.data[0].Question[(id-1)].id; | |
| $scope.survey_id=survey_id; // pass the answers to the view | |
| $scope.answers=answers; // pass the answers to the view | |
| }); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment