forked from krakowski/ilias
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_list.go
102 lines (82 loc) · 2.24 KB
/
exercise_list.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
package ilias
import (
"log"
"net/http"
"strings"
"github.com/PuerkitoBio/goquery"
)
const (
listPath string = "ilias.php?exc_mem_trows=0&cmd=members&cmdClass=ilexercisemanagementgui&cmdNode=c8:o5:c9&baseClass=ilExerciseHandlerGUI"
)
var (
memberReplacer = strings.NewReplacer(
"member[", "",
"]", "",
)
)
type SubmissionMeta struct {
Identifier string
Firstname string
Lastname string
UserId string
Date string
}
func (s *SubmissionMeta) ToRow() []string {
return []string{s.Identifier, s.UserId, s.Lastname, s.Firstname, s.Date}
}
type ListParams struct {
Reference string `schema:"ref_id"`
Assignment string `schema:"ass_id"`
IncludeEmpty bool `schema:"-"`
}
func (exercise *ExerciseService) List(params *ListParams) ([]SubmissionMeta, error) {
// Prepare request url
path, err := addQueryParams(listPath, params)
if err != nil {
return nil, err
}
// Create request
req, err := exercise.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, err
}
// Retrieve the HTML source
resp, err := exercise.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, err
}
table := doc.Find("#exc_mem tbody tr")
if table == nil {
return nil, nil
}
return readSubmissions(table, params.IncludeEmpty), nil
}
func readSubmissions(selction *goquery.Selection, includeEmpty bool) []SubmissionMeta {
var submissions []SubmissionMeta
selction.Each(func(i int, selection *goquery.Selection) {
nodes := selection.Find("td")
// Extract the member id
memberId, exists := nodes.Eq(0).Find("input").Eq(0).Attr("name")
if !exists {
log.Fatal("extracting member id failed")
}
splitName := strings.Split(nodes.Eq(1).Nodes[0].FirstChild.Data, ",")
submission := SubmissionMeta{
Identifier: memberReplacer.Replace(memberId),
Lastname: strings.TrimSpace(splitName[0]),
Firstname: strings.TrimSpace(splitName[1]),
UserId: strings.TrimSpace(nodes.Eq(2).Text()),
Date: strings.TrimSpace(nodes.Eq(3).Text()),
}
if len(submission.Date) == 0 && !includeEmpty {
return
}
submissions = append(submissions, submission)
})
return submissions
}