forked from krakowski/ilias
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_grades.go
173 lines (133 loc) · 3.83 KB
/
exercise_grades.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package ilias
import (
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
)
const (
gradesPath = "ilias.php?vw=1&cmd=post&cmdClass=ilexercisemanagementgui&cmdNode=c8:o5:c9&baseClass=ilExerciseHandlerGUI&fallbackCmd=saveStatusAll"
gradesOverviewPath = "ilias.php?cmd=showGradesOverview&cmdClass=ilexercisemanagementgui&cmdNode=c8:o5:c9&baseClass=ilExerciseHandlerGUI"
)
var (
idRegex = regexp.MustCompile(`(?P<forename>.+),\s(?P<surname>.+)\s\[(?P<id>\w+)\]`)
)
type GradesUpdateQuery struct {
Reference string `schema:"ref_id"`
Assignment string `schema:"ass_id"`
Token string `schema:"rtoken"`
}
type GradesExportQuery struct {
Reference string `schema:"ref_id"`
}
type Grading struct {
Id string
Forename string
Surname string
Grades []string
}
func (grading *Grading) ToRow() []string {
row := []string{grading.Id, grading.Forename, grading.Surname}
for _, value := range grading.Grades {
row = append(row, value)
}
return row
}
func (grading *Grading) ToHeader() []string {
row := []string{"Kennung", "Vorname", "Nachname"}
for index, _ := range grading.Grades {
row = append(row, strconv.Itoa(index))
}
return row
}
func (exercise *ExerciseService) UpdateGrades(params *GradesUpdateQuery, corrections []Correction) error {
// Prepare request url
path, err := addQueryParams(gradesPath, params)
if err != nil {
return err
}
// Magic voodo parameters
values := url.Values{
"selected_cmd": {"saveStatusSelected"},
"selected_cmd2": {"saveStatusSelected"},
"select_cmd2": {"Ausführen"},
}
for _, correction := range corrections {
values.Add("member[" + correction.Student + "]", "1")
values.Add("id[" + correction.Student + "]", "1")
values.Add("idlid[" + correction.Student + "]", "")
values.Add("status[" + correction.Student + "]", "passed")
values.Add("mark[" + correction.Student + "]", strconv.FormatFloat(correction.Points, 'f', -1, 64) + " Punkte")
values.Add("notice[" + correction.Student + "]", "")
}
req, err := exercise.client.NewRequest(http.MethodPost, path, values)
if err != nil {
return err
}
resp, err := exercise.client.Do(req)
if err != nil {
return err
}
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return err
}
if !containsSuccessAlert(doc) {
return ErrUpdate
}
return nil
}
func (exercise *ExerciseService) Export(query *GradesExportQuery) ([]Grading, error) {
// Prepare request url
path, err := addQueryParams(gradesOverviewPath, query)
if err != nil {
return nil, err
}
req, err := exercise.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, err
}
resp, err := exercise.client.Do(req)
if err != nil {
return nil, err
}
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, err
}
return extractGradings(doc)
}
func extractGradings(doc *goquery.Document) ([]Grading, error) {
var gradings []Grading
selection := doc.Find("table[id^='exc_grades_']")
entries := selection.Find("tbody tr")
entries.Each(func(i int, selection *goquery.Selection) {
// Select all columns within the current row
nodes := selection.Find("td")
columns := nodes.Length() - 3
match := idRegex.FindStringSubmatch(strings.TrimSpace(nodes.Eq(0).Text()))
result := make(map[string]string)
for i, name := range idRegex.SubexpNames() {
if i != 0 && name != "" {
result[name] = match[i]
}
}
grading := Grading{
Id: strings.TrimSpace(result["id"]),
Forename: strings.TrimSpace(result["forename"]),
Surname: strings.TrimSpace(result["surname"]),
Grades: []string{},
}
for i := 1; i < columns + 1; i++ {
points := strings.TrimSpace(nodes.Eq(i).Text())
if len(points) == 0 {
points = "0 Punkte"
}
grading.Grades = append(grading.Grades, points)
}
gradings = append(gradings, grading)
})
return gradings, nil
}