@@ -33,7 +33,7 @@ public function putGrade(LtiGrade $grade, ?LtiLineitem $lineitem = null)
3333 $ this ->validateScopes ([LtiConstants::AGS_SCOPE_SCORE ]);
3434
3535 $ lineitem = $ this ->ensureLineItemExists ($ lineitem );
36- $ scoreUrl = $ this -> appendLineItemPath ($ lineitem , '/scores ' );
36+ $ scoreUrl = static :: appendLineItemPath ($ lineitem , '/scores ' );
3737
3838 $ request = new ServiceRequest (
3939 ServiceRequest::METHOD_POST ,
@@ -107,10 +107,16 @@ public function findOrCreateLineitem(LtiLineitem $newLineItem): LtiLineitem
107107 return $ this ->findLineItem ($ newLineItem ) ?? $ this ->createLineitem ($ newLineItem );
108108 }
109109
110- public function getGrades (?LtiLineitem $ lineitem = null )
110+ public function getGrades (?LtiLineitem $ lineitem = null , ? string $ userId = null )
111111 {
112112 $ lineitem = $ this ->ensureLineItemExists ($ lineitem );
113- $ resultsUrl = $ this ->appendLineItemPath ($ lineitem , '/results ' );
113+ $ resultsUrl = static ::appendLineItemPath ($ lineitem , '/results ' );
114+
115+ if (isset ($ userId )) {
116+ $ resultsUrl = static ::appendQueryParams ($ resultsUrl , [
117+ 'user_id ' => $ userId ,
118+ ]);
119+ }
114120
115121 $ request = new ServiceRequest (
116122 ServiceRequest::METHOD_GET ,
@@ -190,17 +196,31 @@ private function isMatchingLineitem(array $lineitem, LtiLineitem $newLineItem):
190196 $ newLineItem ->getResourceLinkId () == ($ lineitem ['resourceLinkId ' ] ?? null );
191197 }
192198
193- private function appendLineItemPath (LtiLineitem $ lineItem , string $ suffix ): string
199+ public static function appendLineItemPath (LtiLineitem $ lineItem , string $ suffix ): string
194200 {
195201 $ url = $ lineItem ->getId ();
196- $ pos = strpos ($ url , '? ' );
197202
198- if ($ pos === false ) {
199- $ url = $ url .$ suffix ;
203+ $ path = implode ('' , [
204+ parse_url ($ url , PHP_URL_HOST ),
205+ parse_url ($ url , PHP_URL_PORT ) ? ': ' .parse_url ($ url , PHP_URL_PORT ) : '' ,
206+ parse_url ($ url , PHP_URL_PATH ),
207+ ]);
208+
209+ return str_replace ($ path , $ path .$ suffix , $ url );
210+ }
211+
212+ public static function appendQueryParams (string $ url , array $ params ): string
213+ {
214+ $ existingQueryString = parse_url ($ url , PHP_URL_QUERY );
215+ if ($ existingQueryString ) {
216+ parse_str ($ existingQueryString , $ existingQueryParams );
217+ $ queryString = http_build_query (array_merge ($ existingQueryParams , $ params ));
218+
219+ return str_replace ($ existingQueryString , $ queryString , $ url );
200220 } else {
201- $ url = substr_replace ($ url , $ suffix , $ pos , 0 );
202- }
221+ $ queryString = http_build_query ($ params );
203222
204- return $ url ;
223+ return $ url .'? ' .$ queryString ;
224+ }
205225 }
206226}
0 commit comments