admin: add exercice history.json

This commit is contained in:
nemunaire 2019-02-03 22:30:29 +01:00
parent 2381fb490b
commit 60ec9704e3
4 changed files with 182 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"strings"
"time"
"srs.epita.fr/fic-server/admin/sync"
"srs.epita.fr/fic-server/libfic"
@ -21,6 +22,9 @@ func init() {
router.PATCH("/api/exercices/:eid", apiHandler(exerciceHandler(partUpdateExercice)))
router.DELETE("/api/exercices/:eid", apiHandler(exerciceHandler(deleteExercice)))
router.GET("/api/exercices/:eid/history.json", apiHandler(exerciceHandler(getExerciceHistory)))
router.DELETE("/api/exercices/:eid/history.json", apiHandler(exerciceHandler(delExerciceHistory)))
router.GET("/api/exercices/:eid/files", apiHandler(exerciceHandler(listExerciceFiles)))
router.POST("/api/exercices/:eid/files", apiHandler(exerciceHandler(createExerciceFile)))
router.GET("/api/exercices/:eid/files/:fid", apiHandler(exerciceFileHandler(showExerciceFile)))
@ -132,6 +136,26 @@ func showExercice(exercice fic.Exercice, body []byte) (interface{}, error) {
return exercice, nil
}
func getExerciceHistory(exercice fic.Exercice, body []byte) (interface{}, error) {
return exercice.GetHistory()
}
type uploadedExerciceHistory struct {
IdTeam int64 `json:"team_id"`
Kind string
Time time.Time
Secondary *int64
}
func delExerciceHistory(exercice fic.Exercice, body []byte) (interface{}, error) {
var uh uploadedExerciceHistory
if err := json.Unmarshal(body, &uh); err != nil {
return nil, err
}
return exercice.DelHistoryItem(uh.IdTeam, uh.Kind, uh.Time, uh.Secondary)
}
func deleteExercice(exercice fic.Exercice, _ []byte) (interface{}, error) {
return exercice.Delete()
}

View File

@ -228,6 +228,9 @@ angular.module("FICApp")
update: {method: 'PUT'}
})
})
.factory("ExerciceHistory", function($resource) {
return $resource("/api/exercices/:exerciceId/history.json", { exerciceId: '@id' })
})
.factory("ExerciceFile", function($resource) {
return $resource("/api/exercices/:exerciceId/files/:fileId", { exerciceId: '@idExercice', fileId: '@id' }, {
update: {method: 'PUT'}
@ -1272,6 +1275,22 @@ angular.module("FICApp")
}
})
.controller("ExerciceHistoryController", function($scope, ExerciceHistory, $routeParams, $http, $rootScope) {
$scope.history = ExerciceHistory.query({ exerciceId: $routeParams.exerciceId });
$scope.delHistory = function(row) {
$http({
url: "/api/exercices/" + $routeParams.exerciceId + "/history.json",
method: "DELETE",
data: row
}).then(function(response) {
$rootScope.staticFilesNeedUpdate++;
$scope.history = ExerciceHistory.query({ exerciceId: $routeParams.exerciceId });
}, function(response) {
$rootScope.newBox('danger', 'An error occurs when removing history item: ', response.data.errmsg);
});
}
})
.controller("ExerciceFilesController", function($scope, ExerciceFile, $routeParams, $rootScope, $http) {
$scope.files = ExerciceFile.query({ exerciceId: $routeParams.exerciceId });

View File

@ -221,6 +221,35 @@
</div>
</div>
<div class="mt-2" style="overflow-y: scroll; height: 450px">
<h3>Historique</h3>
<table ng-controller="ExerciceHistoryController" class="table table-hover table-striped table-bordered bg-primary text-light">
<tbody>
<tr ng-repeat="row in history" ng-class="{'bg-ffound': row.kind == 'flag_found', 'bg-mfound': row.kind == 'mcq_found', 'bg-wchoices': row.kind == 'wchoices', 'bg-success': row.kind == 'solved', 'bg-info': row.kind == 'hint', 'bg-warning': row.kind == 'tries'}">
<td>
<nobr title="{{ row.time }}">{{ row.time | date:"mediumTime" }}</nobr><br>{{ row.kind }}
</td>
<td>
<span ng-if="row.team_id">
<a href="teams/{{ row.team_id }}">{{ row.team_name }}</a>
</span>
<span ng-if="row.secondary_title">
:
<a href="exercices/{{ row.primary }}#key-{{ row.secondary }}" ng-if="row.kind == 'flag_found' || row.kind == 'wchoices'">{{ row.secondary_title }}</a>
<a href="exercices/{{ row.primary }}#quizz-{{ row.secondary }}" ng-if="row.kind == 'mcq_found'">{{ row.secondary_title }}</a>
<a href="exercices/{{ row.primary }}#hint-{{ row.secondary }}" ng-if="row.kind == 'hint'">{{ row.secondary_title }}</a>
</span>
<span ng-if="!row.secondary_title && row.secondary && row.kind == 'solved'">(coeff x{{ row.secondary }})</span>
<span ng-if="!row.secondary_title && row.secondary && row.kind != 'solved'">: {{ row.secondary }}</span>
</td>
<td style="vertical-align: middle; padding: 0; background-color: {{ row.team_color }}">
<button type="button" ng-click="delHistory(row)" class="float-right btn btn-sm btn-danger"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>

110
libfic/exercice_history.go Normal file
View File

@ -0,0 +1,110 @@
package fic
import (
"fmt"
"time"
)
// GetHistory aggregates all sources of events or actions for an Exercice
func (e Exercice) GetHistory() ([]map[string]interface{}, error) {
hist := make([]map[string]interface{}, 0)
if rows, err := DBQuery(`SELECT id_team, U.name, U.color, "tries" AS kind, time, id_exercice, NULL, NULL FROM exercice_tries NATURAL JOIN teams U WHERE id_exercice = ? UNION
SELECT id_team, U.name, U.color, "solved" AS kind, time, id_exercice, coefficient, NULL FROM exercice_solved S NATURAL JOIN teams U WHERE id_exercice = ? UNION
SELECT id_team, U.name, U.color, "hint" AS kind, time, id_exercice, H.id_hint, H.title FROM team_hints T INNER JOIN exercice_hints H ON H.id_hint = T.id_hint NATURAL JOIN teams U WHERE id_exercice = ? UNION
SELECT id_team, U.name, U.color, "wchoices" AS kind, time, id_exercice, F.id_flag, F.type FROM team_wchoices W INNER JOIN exercice_flags F ON F.id_flag = W.id_flag NATURAL JOIN teams U WHERE id_exercice = ? UNION
SELECT id_team, U.name, U.color, "flag_found" AS kind, time, id_exercice, K.id_flag, K.type FROM flag_found F INNER JOIN exercice_flags K ON K.id_flag = F.id_flag NATURAL JOIN teams U WHERE id_exercice = ? UNION
SELECT id_team, U.name, U.color, "mcq_found" AS kind, time, id_exercice, Q.id_mcq, Q.title FROM mcq_found F INNER JOIN exercice_mcq Q ON Q.id_mcq = F.id_mcq NATURAL JOIN teams U WHERE id_exercice = ?
ORDER BY time DESC`, e.Id, e.Id, e.Id, e.Id, e.Id, e.Id); err != nil {
return nil, err
} else {
defer rows.Close()
for rows.Next() {
var id_team int64
var team_name string
var team_color uint32
var kind string
var time time.Time
var exercice int64
var secondary *int64
var secondary_title *string
if err := rows.Scan(&id_team, &team_name, &team_color, &kind, &time, &exercice, &secondary, &secondary_title); err != nil {
return nil, err
}
h := map[string]interface{}{}
h["team_id"] = id_team
h["team_name"] = team_name
h["team_color"] = fmt.Sprintf("#%x", team_color)
h["kind"] = kind
h["time"] = time
h["primary"] = e.Id
if secondary != nil {
h["secondary"] = secondary
h["secondary_title"] = secondary_title
}
hist = append(hist, h)
}
}
return hist, 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" {
if res, err := DBExec("DELETE FROM exercice_tries WHERE id_team = ? AND time = ? AND id_exercice = ?", 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 if kind == "hint" && secondary != nil {
if res, err := DBExec("DELETE FROM team_hints WHERE id_team = ? AND time = ? AND id_hint = ?", 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("DELETE FROM team_wchoices WHERE id_team = ? AND time = ? AND id_flag = ?", 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 == "flag_found" && secondary != nil {
if res, err := DBExec("DELETE FROM flag_found WHERE id_team = ? AND time = ? AND id_flag = ?", 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 == "mcq_found" && secondary != nil {
if res, err := DBExec("DELETE FROM mcq_found WHERE id_team = ? AND time = ? AND id_mcq = ?", 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("DELETE FROM exercice_solved WHERE id_team = ? AND time = ? AND id_exercice = ?", 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
}
}