admin: Can list independant exercices as theme

This commit is contained in:
nemunaire 2025-03-27 16:25:48 +01:00
parent d26333c5e2
commit 5e262b75a3
3 changed files with 27 additions and 8 deletions

View file

@ -70,13 +70,17 @@ func ThemeHandler(c *gin.Context) {
return
}
theme, err := fic.GetTheme(thid)
if err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Theme not found"})
return
}
if thid == 0 {
c.Set("theme", &fic.Theme{Name: "Exercices indépendants", Path: sync.StandaloneExercicesDirectory})
} else {
theme, err := fic.GetTheme(thid)
if err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Theme not found"})
return
}
c.Set("theme", theme)
c.Set("theme", theme)
}
c.Next()
}
@ -127,6 +131,10 @@ func listThemes(c *gin.Context) {
return
}
if has, _ := fic.HasStandaloneExercice(); has {
themes = append([]*fic.Theme{&fic.Theme{Name: "Exercices indépendants", Path: sync.StandaloneExercicesDirectory}}, themes...)
}
c.JSON(http.StatusOK, themes)
}

View file

@ -8,7 +8,7 @@
</div>
<div class="row">
<form ng-submit="saveTheme()" class="col-4">
<form ng-submit="saveTheme()" class="col-4" ng-if="!(theme.id === 0 && theme.path)">
<div ng-class="{'form-group': field != 'locked', 'form-check': field == 'locked'}" ng-repeat="field in fields">
<input type="checkbox" class="form-check-input" id="{{ field }}" ng-model="theme[field]" ng-if="field == 'locked'">
<label for="{{ field }}">{{ field | capitalize }}</label>
@ -25,7 +25,7 @@
</div>
</form>
<div ng-if="theme.id" class="col-md-8" ng-controller="ExercicesListController">
<div ng-if="theme.id || theme.path" class="col-md-8" ng-class="{'offset-md-2': theme.id === 0 && theme.path}" ng-controller="ExercicesListController">
<h3>
Exercices ({{ exercices.length }})
<button type="button" ng-click="show('new')" class="float-right btn btn-sm btn-primary ml-2"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Ajouter un exercice</button>

View file

@ -645,3 +645,14 @@ func (e *Exercice) IsSolved() (int, *time.Time) {
return *nb, tm
}
}
func HasStandaloneExercice() (bool, error) {
var nb int
err := DBQueryRow("SELECT COUNT(id_exercice) FROM exercices WHERE id_theme IS NULL").Scan(&nb)
if err != nil {
return false, err
} else {
return nb > 0, nil
}
}