admin: implement MCQ edition in interface
This commit is contained in:
parent
92ba880006
commit
3a65363ebb
|
@ -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()
|
||||
}
|
||||
|
||||
|
|
|
@ -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"];
|
||||
|
|
|
@ -110,6 +110,44 @@
|
|||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card border-success mt-2" ng-controller="ExerciceMCQKeysController">
|
||||
<div class="card-header bg-success text-light">
|
||||
<button ng-click="addQuiz()" class="float-right btn btn-sm btn-primary" style="margin-left: 7px"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span></button>
|
||||
<h4>Quizz</h4>
|
||||
</div>
|
||||
<div class="list-group">
|
||||
<form ng-submit="saveQuiz()" class="list-group-item form-horizontal bg-light text-dark" ng-repeat="(qk,q) in quiz">
|
||||
<div class="form-group row" id="quiz-{{q.id}}">
|
||||
<input type="text" id="qlabel{{q.id}}" ng-model="q.title" class="col form-control" placeholder="Intitulé">
|
||||
<div class="col-auto" ng-show="q.id">
|
||||
<button type="button" ng-click="deleteQuiz()" class="btn btn-danger"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group row" ng-repeat="(ck,choice) in q.entries">
|
||||
<div class="col form-check">
|
||||
<label class="custom-control custom-checkbox">
|
||||
<input class="custom-control-input" type="checkbox" ng-model="choice.response">
|
||||
<span class="custom-control-label">
|
||||
<input type="text" id="kchoice{{q.id}}-{{choice.id}}" ng-model="choice.label" class="form-control form-control-sm" placeholder="Intitulé">
|
||||
</span>
|
||||
</label>
|
||||
</div>
|
||||
<div class="col-auto">
|
||||
<button type="button" ng-click="deleteChoice()" class="btn btn-sm btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group row">
|
||||
<div class="col-auto">
|
||||
<button type="button" ng-click="addChoice()" class="btn btn-primary"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Ajouter un choix</button>
|
||||
</div>
|
||||
<div class="col-auto">
|
||||
<button type="submit" class="btn btn-success"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue
Block a user