forked from krakowski/ilias
-
Notifications
You must be signed in to change notification settings - Fork 0
/
members_list.go
78 lines (60 loc) · 1.7 KB
/
members_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
package ilias
import (
"net/http"
"strings"
"github.com/PuerkitoBio/goquery"
)
const (
memberListPath string = "ilias.php?cmdClass=ilcoursemembershipgui&cmdNode=xv:mt:95&baseClass=ilrepositorygui"
)
type MemberParams struct {
Reference string `schema:"ref_id"`
}
func (members *MemberService) List(params *MemberParams) ([]CourseMember, error) {
// Prepare request url
path, err := addQueryParams(memberListPath, params)
if err != nil {
return nil, err
}
// Create request
req, err := members.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, err
}
// Retrieve the HTML source
resp, err := members.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
}
return readMembers(doc), nil
}
func readMembers(doc *goquery.Document) []CourseMember {
// Find participant table
selection := doc.Find("#participants .table-responsive table tbody tr")
// Iterate over all table rows
var members []CourseMember
selection.Each(func(i int, selection *goquery.Selection) {
// Select all columns within the current row
nodes := selection.Find("td")
identifier, exists := nodes.Eq(0).Find("input[type=checkbox]").Eq(0).Attr("value")
if !exists {
return
}
// Split the name and extract information
splitName := strings.Split(nodes.Eq(1).Text(), ",")
member := CourseMember{
Identifier: identifier,
Lastname: strings.TrimSpace(splitName[0]),
Firstname: strings.TrimSpace(splitName[1]),
Username: strings.TrimSpace(nodes.Eq(2).Text()),
Role: strings.TrimSpace(nodes.Eq(3).Text()),
}
members = append(members, member)
})
return members
}