From 3a65363ebb906b36a5af5e55b8e759d4f2181c17 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Sun, 24 Jun 2018 18:12:26 +0200 Subject: [PATCH] admin: implement MCQ edition in interface --- admin/api/exercice.go | 65 ++++++++++++++++++++++++++++++++ admin/static/js/app.js | 34 +++++++++++++++++ admin/static/views/exercice.html | 38 +++++++++++++++++++ 3 files changed, 137 insertions(+) diff --git a/admin/api/exercice.go b/admin/api/exercice.go index 8e68c9dd..0ca0a008 100644 --- a/admin/api/exercice.go +++ b/admin/api/exercice.go @@ -38,6 +38,7 @@ func init() { router.GET("/api/exercices/:eid/quiz", apiHandler(exerciceHandler(listExerciceQuiz))) router.GET("/api/exercices/:eid/quiz/:qid", apiHandler(quizHandler(showExerciceQuiz))) + router.PUT("/api/exercices/:eid/quiz/:qid", apiHandler(quizHandler(updateExerciceQuiz))) router.DELETE("/api/exercices/:eid/quiz/:qid", apiHandler(quizHandler(deleteExerciceQuiz))) // Synchronize @@ -239,7 +240,71 @@ func showExerciceQuiz(quiz fic.MCQ, _ fic.Exercice, body []byte) (interface{}, e return quiz, nil } +func updateExerciceQuiz(quiz fic.MCQ, exercice fic.Exercice, body []byte) (interface{}, error) { + var uq fic.MCQ + if err := json.Unmarshal(body, &uq); err != nil { + return nil, err + } + + quiz.Title = uq.Title + + if _, err := quiz.Update(); err != nil { + return nil, err + } + + // Update and remove old entries + var delete []int + for i, cur := range quiz.Entries { + seen := false + for _, next := range uq.Entries { + if cur.Id == next.Id { + seen = true + + if cur.Label != next.Label || cur.Response != next.Response { + cur.Label = next.Label + cur.Response = next.Response + if _, err := cur.Update(); err != nil { + return nil, err + } + } + + break + } + } + + if seen == false { + if _, err := cur.Delete(); err != nil { + return nil, err + } else { + delete = append(delete, i) + } + } + } + for n, i := range delete { + quiz.Entries = append(quiz.Entries[:i-n-1], quiz.Entries[:i-n+1]...) + } + + // Add new choices + for _, choice := range uq.Entries { + if choice.Id == 0 { + if c, err := quiz.AddEntry(choice.Label, choice.Response); err != nil { + return nil, err + } else { + quiz.Entries = append(quiz.Entries, c) + } + } + } + + return quiz, nil +} + func deleteExerciceQuiz(quiz fic.MCQ, _ fic.Exercice, _ []byte) (interface{}, error) { + for _, choice := range quiz.Entries { + if _, err := choice.Delete(); err != nil { + return nil, err + } + } + return quiz.Delete() } diff --git a/admin/static/js/app.js b/admin/static/js/app.js index 7dfc401a..d3abae03 100644 --- a/admin/static/js/app.js +++ b/admin/static/js/app.js @@ -222,6 +222,11 @@ angular.module("FICApp") return $resource("/api/exercices/:exerciceId/keys/:keyId", { exerciceId: '@idExercice', keyId: '@id' }, { update: {method: 'PUT'} }) + }) + .factory("ExerciceMCQKey", function($resource) { + return $resource("/api/exercices/:exerciceId/quiz/:mcqId", { exerciceId: '@idExercice', mcqId: '@id' }, { + update: {method: 'PUT'} + }) }); String.prototype.capitalize = function() { @@ -1127,6 +1132,35 @@ angular.module("FICApp") }; }) + .controller("ExerciceMCQKeysController", function($scope, ExerciceMCQKey, $routeParams, $rootScope, $http) { + $scope.quiz = ExerciceMCQKey.query({ exerciceId: $routeParams.exerciceId }); + + $scope.addQuiz = function() { + $scope.quiz.push(new ExerciceMCQKey()); + } + $scope.deleteQuiz = function() { + this.q.$delete(function() { + $scope.quiz.splice($scope.quiz.indexOf(this.q), 1); + }, function(response) { + $rootScope.newBox('danger', 'An error occurs when trying to delete flag:', response.data); + }); + } + $scope.saveQuiz = function() { + if (this.q.id) { + this.q.$update(); + } else { + this.q.$save({ exerciceId: $routeParams.exerciceId }); + } + } + + $scope.addChoice = function() { + this.quiz[this.qk].entries.push({label: "", response: false}) + } + $scope.deleteChoice = function() { + this.quiz[this.qk].entries.splice(this.quiz[this.qk].entries.indexOf(this.choice), 1); + } + }) + .controller("TeamsListController", function($scope, Team, $location) { $scope.teams = Team.query(); $scope.fields = ["id", "name"]; diff --git a/admin/static/views/exercice.html b/admin/static/views/exercice.html index 335c4b23..a220e6d0 100644 --- a/admin/static/views/exercice.html +++ b/admin/static/views/exercice.html @@ -110,6 +110,44 @@ + +
+
+ +

Quizz

+
+
+
+
+ +
+ +
+
+
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+
+
+