1+ <?php
2+ namespace IMSGlobal \LTI ;
3+
4+ class LTI_Course_Groups_Service {
5+
6+ private $ service_connector ;
7+ private $ service_data ;
8+
9+ public function __construct (LTI_Service_Connector $ service_connector , $ service_data ) {
10+ $ this ->service_connector = $ service_connector ;
11+ $ this ->service_data = $ service_data ;
12+ }
13+
14+ public function get_groups () {
15+
16+ $ groups = [];
17+
18+ $ next_page = $ this ->service_data ['context_groups_url ' ];
19+
20+ while ($ next_page ) {
21+ $ page = $ this ->service_connector ->make_service_request (
22+ $ this ->service_data ['scope ' ],
23+ 'GET ' ,
24+ $ next_page ,
25+ null ,
26+ null ,
27+ 'application/vnd.ims.lti-gs.v1.contextgroupcontainer+json '
28+ );
29+
30+ $ groups = array_merge ($ groups , $ page ['body ' ]['groups ' ]);
31+
32+ $ next_page = false ;
33+ foreach ($ page ['headers ' ] as $ header ) {
34+ if (preg_match (LTI_Service_Connector::NEXT_PAGE_REGEX , $ header , $ matches )) {
35+ $ next_page = $ matches [1 ];
36+ break ;
37+ }
38+ }
39+ }
40+ return $ groups ;
41+
42+ }
43+
44+ public function get_sets () {
45+
46+ $ sets = [];
47+
48+ // Sets are optional.
49+ if (!isset ($ this ->service_data ['context_group_sets_url ' ])) {
50+ return [];
51+ }
52+
53+ $ next_page = $ this ->service_data ['context_group_sets_url ' ];
54+
55+ while ($ next_page ) {
56+ $ page = $ this ->service_connector ->make_service_request (
57+ $ this ->service_data ['scope ' ],
58+ 'GET ' ,
59+ $ next_page ,
60+ null ,
61+ null ,
62+ 'application/vnd.ims.lti-gs.v1.contextgroupcontainer+json '
63+ );
64+
65+ $ sets = array_merge ($ sets , $ page ['body ' ]['sets ' ]);
66+
67+ $ next_page = false ;
68+ foreach ($ page ['headers ' ] as $ header ) {
69+ if (preg_match (LTI_Service_Connector::NEXT_PAGE_REGEX , $ header , $ matches )) {
70+ $ next_page = $ matches [1 ];
71+ break ;
72+ }
73+ }
74+ }
75+ return $ sets ;
76+
77+ }
78+
79+ public function get_groups_by_set () {
80+ $ groups = $ this ->get_groups ();
81+ $ sets = $ this ->get_sets ();
82+
83+ $ groups_by_set = [];
84+ $ unsetted = [];
85+
86+ foreach ($ sets as $ key => $ set ) {
87+ $ groups_by_set [$ set ['id ' ]] = $ set ;
88+ $ groups_by_set [$ set ['id ' ]]['groups ' ] = [];
89+ }
90+
91+ foreach ($ groups as $ key => $ group ) {
92+ if (isset ($ group ['set_id ' ]) && isset ($ groups_by_set [$ group ['set_id ' ]])) {
93+ $ groups_by_set [$ group ['set_id ' ]]['groups ' ][$ group ['id ' ]] = $ group ;
94+ } else {
95+ $ unsetted [$ group ['id ' ]] = $ group ;
96+ }
97+ }
98+
99+ if (!empty ($ unsetted )) {
100+ $ groups_by_set ['none ' ] = [
101+ "name " => "None " ,
102+ "id " => "none " ,
103+ "groups " => $ unsetted ,
104+ ];
105+ }
106+
107+ return $ groups_by_set ;
108+ }
109+ }
110+ ?>
0 commit comments