From ba5642da8fa368139e377d46451ec9cd83d6fda1 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Fri, 12 Jul 2019 19:17:17 +0200 Subject: [PATCH] admin: new form to update history coefficient --- admin/api/exercice.go | 11 ++++++++ admin/static/js/app.js | 44 +++++++++++++++++++++++++++++ admin/static/views/exercice.html | 46 +++++++++++++++++++++++++++++-- admin/static/views/team-edit.html | 46 +++++++++++++++++++++++++++++-- libfic/exercice_history.go | 31 +++++++++++++++++++++ 5 files changed, 174 insertions(+), 4 deletions(-) diff --git a/admin/api/exercice.go b/admin/api/exercice.go index b7f36487..c9c3d9c7 100644 --- a/admin/api/exercice.go +++ b/admin/api/exercice.go @@ -23,6 +23,7 @@ func init() { router.DELETE("/api/exercices/:eid", apiHandler(exerciceHandler(deleteExercice))) router.GET("/api/exercices/:eid/history.json", apiHandler(exerciceHandler(getExerciceHistory))) + router.PATCH("/api/exercices/:eid/history.json", apiHandler(exerciceHandler(updateExerciceHistory))) router.DELETE("/api/exercices/:eid/history.json", apiHandler(exerciceHandler(delExerciceHistory))) router.GET("/api/exercices/:eid/files", apiHandler(exerciceHandler(listExerciceFiles))) @@ -145,6 +146,16 @@ type uploadedExerciceHistory struct { Kind string Time time.Time Secondary *int64 + Coeff float32 +} + +func updateExerciceHistory(exercice fic.Exercice, body []byte) (interface{}, error) { + var uh uploadedExerciceHistory + if err := json.Unmarshal(body, &uh); err != nil { + return nil, err + } + + return exercice.UpdateHistoryItem(uh.Coeff, uh.IdTeam, uh.Kind, uh.Time, uh.Secondary) } func delExerciceHistory(exercice fic.Exercice, body []byte) (interface{}, error) { diff --git a/admin/static/js/app.js b/admin/static/js/app.js index 7806f5fb..79c4cb44 100644 --- a/admin/static/js/app.js +++ b/admin/static/js/app.js @@ -1289,6 +1289,28 @@ angular.module("FICApp") .controller("ExerciceHistoryController", function($scope, ExerciceHistory, $routeParams, $http, $rootScope) { $scope.history = ExerciceHistory.query({ exerciceId: $routeParams.exerciceId }); + $scope.updHistory = function() { + var target = { + team_id: $("#updHistory").data("idteam"), + kind: $("#updHistory").data("kind"), + time: $("#updHistory").data("time"), + secondary: $("#updHistory").data("secondary") != "" ? $("#updHistory").data("secondary") : null, + coeff: parseFloat($('#historycoeff').val()), + }; + if (target) { + $http({ + url: "/api/exercices/" + $routeParams.exerciceId + "/history.json", + method: "PATCH", + data: target + }).then(function(response) { + $rootScope.staticFilesNeedUpdate++; + $scope.history = ExerciceHistory.query({ exerciceId: $routeParams.exerciceId }); + $('#updHistory').modal('hide'); + }, function(response) { + $rootScope.newBox('danger', 'An error occurs when updating history item: ', response.data.errmsg); + }); + } + } $scope.delHistory = function(row) { $http({ url: "/api/exercices/" + $routeParams.exerciceId + "/history.json", @@ -1594,6 +1616,28 @@ angular.module("FICApp") }) .controller("TeamHistoryController", function($scope, TeamHistory, $routeParams, $http, $rootScope) { $scope.history = TeamHistory.query({ teamId: $routeParams.teamId }); + $scope.updHistory = function() { + var target = { + team_id: parseInt($routeParams.teamId), + kind: $("#updHistory").data("kind"), + time: $("#updHistory").data("time"), + secondary: $("#updHistory").data("secondary") != "" ? $("#updHistory").data("secondary") : null, + coeff: parseFloat($('#historycoeff').val()), + }; + if (target) { + $http({ + url: "/api/exercices/" + $("#updHistory").data("primary") + "/history.json", + method: "PATCH", + data: target + }).then(function(response) { + $rootScope.staticFilesNeedUpdate++; + $scope.history = TeamHistory.query({ teamId: $routeParams.teamId }); + $('#updHistory').modal('hide'); + }, function(response) { + $rootScope.newBox('danger', 'An error occurs when updating history item: ', response.data.errmsg); + }); + } + } $scope.delHistory = function(row) { $http({ url: "/api/teams/" + $routeParams.teamId + "/history.json", diff --git a/admin/static/views/exercice.html b/admin/static/views/exercice.html index 7c72891a..f9a4e6b5 100644 --- a/admin/static/views/exercice.html +++ b/admin/static/views/exercice.html @@ -221,9 +221,9 @@ -
+

Historique

- +
@@ -242,13 +242,55 @@ : {{ row.secondary }} +
+ + +
+ + diff --git a/admin/static/views/team-edit.html b/admin/static/views/team-edit.html index 26a6b1c7..e730ad31 100644 --- a/admin/static/views/team-edit.html +++ b/admin/static/views/team-edit.html @@ -129,8 +129,8 @@ -
- +
+
@@ -151,10 +151,52 @@ : {{ row.secondary }} +
+ + +
+ + diff --git a/libfic/exercice_history.go b/libfic/exercice_history.go index 29873345..fccc7618 100644 --- a/libfic/exercice_history.go +++ b/libfic/exercice_history.go @@ -56,6 +56,37 @@ func (e Exercice) GetHistory() ([]map[string]interface{}, error) { return hist, nil } +// UpdateHistoryItem sets values an entry from the history. +func (e Exercice) UpdateHistoryItem(coeff float32, tId int64, kind string, h time.Time, secondary *int64) (interface{}, error) { + if kind == "hint" && secondary != nil { + if res, err := DBExec("UPDATE team_hints SET coefficient = ?, time = ? WHERE id_team = ? AND time = ? AND id_hint = ?", coeff, h, tId, h, *secondary); err != nil { + return 0, err + } else if nb, err := res.RowsAffected(); err != nil { + return 0, err + } else { + return nb, err + } + } else if kind == "wchoices" && secondary != nil { + if res, err := DBExec("UPDATE team_wchoices SET coefficient = ?, time = ? WHERE id_team = ? AND time = ? AND id_flag = ?", coeff, h, tId, h, *secondary); err != nil { + return 0, err + } else if nb, err := res.RowsAffected(); err != nil { + return 0, err + } else { + return nb, err + } + } else if kind == "solved" { + if res, err := DBExec("UPDATE exercice_solved SET coefficient = ?, time = ? WHERE id_team = ? AND time = ? AND id_exercice = ?", coeff, h, tId, h, e.Id); err != nil { + return 0, err + } else if nb, err := res.RowsAffected(); err != nil { + return 0, err + } else { + return nb, err + } + } else { + return nil, nil + } +} + // DelHistoryItem removes from the database an entry from the history. func (e Exercice) DelHistoryItem(tId int64, kind string, h time.Time, secondary *int64) (interface{}, error) { if kind == "tries" {