@@ -24,10 +24,12 @@ class Udoit
2424{
2525 /**
2626 * Retrieves an entire group of content by type and scans it
27- * @param string api_key - API Key of the user were acting as
28- * @param string content_type - The group of content types we'll retrieve EX: 'pages' or 'assignments'
27+ * @param string $api_key API Key of the user were acting as
28+ * @param string $canvas_api_url The base URL of the Canvas API
29+ * @param string $course_id The Canvas course id
30+ * @param string $content_type The group of content types we'll retrieve EX: 'pages' or 'assignments'
2931 *
30- * @return array - Results of the scan
32+ * @return array Results of the scan
3133 */
3234 public static function retrieveAndScan ($ api_key , $ canvas_api_url , $ course_id , $ content_type )
3335 {
@@ -107,9 +109,9 @@ public static function retrieveAndScan($api_key, $canvas_api_url, $course_id, $c
107109
108110 /**
109111 * Calls the Quail library to generate a UDOIT report
110- * @param array $scanned_content - The items from whatever type of Canvas content was scanned
112+ * @param array $content_items The items from whatever type of Canvas content was scanned
111113 *
112- * @return array - The report results
114+ * @return array The report results
113115 */
114116 public static function scanContent (array $ content_items )
115117 {
@@ -172,11 +174,12 @@ public static function scanContent(array $content_items)
172174
173175 /**
174176 * Communicates with the Canvas API to retrieve course content
175- * @param string $api_key - Api key for the user we're acting as
176- * @param string $canvas_api_url - Base uri for your canvas api
177- * @param string $type - The type of course content to be scanned
177+ * @param string $api_key Api key for the user we're acting as
178+ * @param string $canvas_api_url Base uri for your canvas api
179+ * @param string $course_id The canvas course id
180+ * @param string $type The type of course content to be scanned
178181 *
179- * @return array - The report results
182+ * @return array The report results
180183 */
181184 public static function getCourseContent ($ api_key , $ canvas_api_url , $ course_id , $ type )
182185 {
@@ -226,6 +229,14 @@ public static function getCourseContent($api_key, $canvas_api_url, $course_id, $
226229 break ;
227230
228231 case 'files ' :
232+ // Gather all modules out here so we can attach them to files later
233+ $ all_modules = static ::apiGet ("{$ api_url }modules " , $ api_key )->send ()->body ;
234+ if (is_array ($ all_modules ) || is_object ($ all_modules )) {
235+ foreach ($ all_modules as $ m ) {
236+ $ m ->items = static ::apiGet ($ m ->items_url , $ api_key )->send ()->body ;
237+ }
238+ }
239+
229240 $ contents = static ::apiGetAllLinks ($ api_key , "{$ api_url }files? " );
230241 foreach ($ contents as $ c ) {
231242 if (substr ($ c ->display_name , 0 , 2 ) === '._ ' ) {
@@ -250,14 +261,10 @@ public static function getCourseContent($api_key, $canvas_api_url, $course_id, $
250261 // saves modules item is in for unscannable section
251262 unset($ modules );
252263 $ modules = [];
253- $ all_modules = static ::apiGet ("{$ api_url }modules " , $ api_key )->send ()->body ;
254- if (is_array ($ all_modules ) || is_object ($ all_modules )) {
255- foreach ($ all_modules as $ m ) {
256- $ items = static ::apiGet ($ m ->items_url , $ api_key )->send ()->body ;
257- foreach ($ items as $ i ) {
258- if ($ i ->title == $ c ->display_name ) {
259- $ modules [] = $ m ->name ;
260- }
264+ foreach ($ all_modules as $ m ) {
265+ foreach ($ m ->items as $ i ) {
266+ if ($ i ->title == $ c ->display_name ) {
267+ $ modules [] = $ m ->name ;
261268 }
262269 }
263270 }
@@ -357,7 +364,13 @@ public static function getCourseContent($api_key, $canvas_api_url, $course_id, $
357364 return $ content_result ;
358365 }
359366
360- // abstraction of Request::get that adds the api key to the header
367+ /**
368+ * An abstraction of Request::get that adds the api key to the header
369+ * @param string $url The Canvas API URL
370+ * @param string $key The Canvas API key
371+ *
372+ * @return The results of the request
373+ **/
361374 protected static function apiGet ($ url , $ key = null )
362375 {
363376 global $ logger ;
@@ -371,7 +384,13 @@ protected static function apiGet($url, $key = null)
371384 return $ req ;
372385 }
373386
374- // get every page from the Canvas API till there's none left
387+ /**
388+ * Gets every page from the Canvas API till there's none left
389+ * @param string $api_key The Canvas API Key
390+ * @param string $url The Canvas API URL
391+ *
392+ * @return The results of the request
393+ **/
375394 protected static function apiGetAllLinks ($ api_key , $ url )
376395 {
377396 global $ logger ;
@@ -387,6 +406,12 @@ protected static function apiGetAllLinks($api_key, $url)
387406 }
388407
389408 $ links = static ::apiParseLinks ($ response ->headers ->toArray ()['link ' ]);
409+
410+ if (empty ($ response ->body )) {
411+ $ logger ->addError ("Canvas API returned empty body for {$ filtered_url }" );
412+ break ;
413+ }
414+
390415 foreach ($ response ->body as $ thing ) {
391416 $ results [] = $ thing ;
392417 }
@@ -403,9 +428,9 @@ protected static function apiGetAllLinks($api_key, $url)
403428
404429 /**
405430 * Parses pagination links returned from Canvas
406- * @param string $links - The pagination links
431+ * @param string $links The pagination links
407432 *
408- * @return array - An array of the separated links
433+ * @return array An array of the separated links
409434 */
410435 protected static function apiParseLinks ($ links )
411436 {
0 commit comments