diff --git a/htdocs/js/atsebayt.js b/htdocs/js/atsebayt.js index a8bbb2f..8810178 100644 --- a/htdocs/js/atsebayt.js +++ b/htdocs/js/atsebayt.js @@ -33,6 +33,10 @@ angular.module("AtsebaytApp", ["ngRoute", "ngResource", "ngSanitize"]) controller: "UserController", templateUrl: "views/user.html" }) + .when("/users/:userId/surveys/:surveyId", { + controller: "SurveyController", + templateUrl: "views/survey.html" + }) .when("/", { controller: "SurveysController", templateUrl: "views/home.html" @@ -54,6 +58,9 @@ angular.module("AtsebaytApp") .factory("MyResponse", function($resource) { return $resource("/api/surveys/:surveyId/responses/:respId", { surveyId: '@id', respId: '@id' }) }) + .factory("UserResponse", function($resource) { + return $resource("/api/users/:userId/surveys/:surveyId/responses/:respId", { userId: '@id', surveyId: '@id', respId: '@id' }) + }) .factory("Grades", function($resource) { return $resource("/api/grades") }) @@ -268,6 +275,9 @@ angular.module("AtsebaytApp") data[k].end_availability = Date.parse(data[k].end_availability) }) }) + $scope.showUserSurvey = function() { + $location.url("users/" + this.user.id + "/surveys/" + this.survey.id); + } }) .controller("SurveyController", function($scope, $rootScope, Survey, $routeParams, $location) { @@ -381,7 +391,7 @@ angular.module("AtsebaytApp") $scope.user = User.get({ userId: $routeParams.userId}) }) - .controller("QuestionController", function($scope, Survey, SurveyQuest, SurveyQuest, AllResponses, CorrectionTemplate, $http, $routeParams) { + .controller("QuestionController", function($scope, Survey, SurveyQuest, AllResponses, UserResponses, CorrectionTemplate, $http, $routeParams) { $scope.notCorrected = true $scope.highlight = '' @@ -400,7 +410,13 @@ angular.module("AtsebaytApp") survey.end_availability = Date.parse(survey.end_availability) }) $scope.question = SurveyQuest.get({ surveyId: $routeParams.surveyId, questId: $routeParams.questId }); - $scope.responses = AllResponses.query({ surveyId: $routeParams.surveyId, questId: $routeParams.questId }, function (responses) { + + if ($routeParams.userId == null) { + $scope.responses = AllResponses.query({ surveyId: $routeParams.surveyId, questId: $routeParams.questId }); + } else { + $scope.responses = UserResponses.query({ userId: $routeParams.userId, surveyId: $routeParams.surveyId, questId: $routeParams.questId }); + } + $scope.responses.$promise.then(function (responses) { $scope.users_corrected = {} responses.forEach(function(r) { @@ -506,7 +522,7 @@ angular.module("AtsebaytApp") } }) - .controller("QuestionsController", function($scope, SurveyQuest, MyResponse, $http, $location) { + .controller("QuestionsController", function($scope, SurveyQuest, MyResponse, UserResponse, $http, $routeParams, $location) { $scope.questions = SurveyQuest.query({ surveyId: $scope.survey.id }); $scope.questions.$promise.then(function (questions) {$scope.showSubmit = true;}, function (response) { $scope.addToast({ @@ -516,7 +532,11 @@ angular.module("AtsebaytApp") }); $location.url("surveys/") }) - $scope.myresponses = MyResponse.query({ surveyId: $scope.survey.id }); + if ($routeParams.userId == null) { + $scope.myresponses = MyResponse.query({ surveyId: $scope.survey.id }); + } else { + $scope.myresponses = UserResponse.query({ userId: $routeParams.userId, surveyId: $scope.survey.id }); + } $scope.myresponses.$promise.then(function (responses) { $scope.questions.$promise.then(function (questions) { var idxquestions = {} @@ -564,8 +584,12 @@ angular.module("AtsebaytApp") res.push({"id_question": q.id, "value": values.join(",")}) } }); + var url = "/api/surveys/" + $scope.survey.id; + if ($routeParams.userId != null) { + url = "/api/users/" + $routeParams.userId + "/surveys/" + $scope.survey.id; + } $http({ - url: "/api/surveys/" + $scope.survey.id, + url: url, data: res, method: "POST" }).then(function(response) { diff --git a/htdocs/views/user.html b/htdocs/views/user.html index 08fd4f8..5835ca4 100644 --- a/htdocs/views/user.html +++ b/htdocs/views/user.html @@ -51,7 +51,7 @@ - + {{ survey.id }} {{ survey.title }} {{ avancement * 100 | number:2 }} % diff --git a/responses.go b/responses.go index 2e70f2a..81cc060 100644 --- a/responses.go +++ b/responses.go @@ -25,10 +25,37 @@ func init() { return APIResponse{true} }), loggedUser)) + router.POST("/api/users/:uid/surveys/:sid", apiAuthHandler(func(u *User, ps httprouter.Params, body []byte) HTTPResponse { + return surveyAuthHandler(func(s Survey, u *User, _ []byte) HTTPResponse { + return userHandler(func(u User, _ []byte) HTTPResponse { + var responses []Response + if err := json.Unmarshal(body, &responses); err != nil { + return APIErrorResponse{err: err} + } + + for _, response := range responses { + if len(response.Answer) > 0 { + if _, err := s.NewResponse(response.IdQuestion, u.Id, response.Answer); err != nil { + return APIErrorResponse{err: err} + } + } + } + + return APIResponse{true} + })(ps, body) + })(u, ps, body) + }, adminRestricted)) router.GET("/api/surveys/:sid/responses", apiAuthHandler(surveyAuthHandler( func(s Survey, u *User, _ []byte) HTTPResponse { return formatApiResponse(s.GetMyResponses(u, s.Corrected)) }), loggedUser)) + router.GET("/api/users/:uid/surveys/:sid/responses", apiAuthHandler(func(u *User, ps httprouter.Params, body []byte) HTTPResponse { + return surveyAuthHandler(func(s Survey, u *User, _ []byte) HTTPResponse { + return userHandler(func(u User, _ []byte) HTTPResponse { + return formatApiResponse(s.GetMyResponses(&u, s.Corrected)) + })(ps, body) + })(u, ps, body) + }, adminRestricted)) router.GET("/api/surveys/:sid/responses/:rid", apiAuthHandler(responseAuthHandler( func(r Response, _ *User, _ []byte) HTTPResponse { return APIResponse{r}