uccdev

Can I check each of my courses for a certain folder more efficiently than this?

Posted 2 months ago by uccdev

Hi, I am checking all the courses in my Canvas database to see which ones have a certain folder. Let's call this folder "BB_Direct."

At the moment, using cUrl requests like below, I check the accounts/number/courses endpoint to retrieve all the courses:

  $loop = 1; //increments with each loop
  $validReturn = true;

while($validReturn) {
  curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => true,
    CURLINFO_HEADER_OUT => true,
    CURLOPT_URL => "https://my.test.instructure.com/api/v1/accounts/$loop/courses?by_subaccounts[100]&per_page=100",//?by_subaccounts[95]&per_page=100/",//'https://ucc.test.instructure.com/api/v1/courses',
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_SSL_VERIFYPEER => true,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_CUSTOMREQUEST, 'GET',
    CURLOPT_RETURNTRANSFER, true
  ]);
 $resp = curl_exec($curl);
  $data = json_decode($resp, true);
  if (array_key_exists("errors", $data)) { //assuming that there are NO valid pages after the latest retrieved one that returns "error"
    $validReturn = false;
    echo "Invalid page found. Ending after loop $loop</br>";
  }
  else {
    echo "Loop $loop <br>";
    //dd($data);
  //dd($data);
    foreach ($data as $d) {
      if((array_key_exists("id", $d)) && (self::hasBBDirect($d["id"]))) { //important line
        echo "BB_Direct folder FOUND in course " . $d["id"] . "</br>";
      } else {
        echo "No BB_Direct folder found in course " . $d["id"] . "<br>";
      }
    }
    $loop++;
  }

See the "self::hasBBDirect($d["id"])" line? That directs to a helper function to check if any courses under the returned course ID have the folder I'm looking for:

   public function hasBBDirect($courseID) {
       $token = "insert_token_here";
       $headers = ['Authorization: Bearer ' . $token];
       $curl = curl_init();
       $url = "https://my.test.instructure.com/api/v1/courses/$courseID/folders/by_path/BB_Direct"; //"BB_Direct" = the folder I'm looking for

       curl_setopt_array($curl, [
  CURLOPT_RETURNTRANSFER => true,
  CURLINFO_HEADER_OUT => true,
  CURLOPT_URL => $url,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_SSL_VERIFYPEER => true,
  CURLOPT_HTTPHEADER => $headers,
  CURLOPT_RETURNTRANSFER, true
]);

       $resp = curl_exec($curl);
       $data = json_decode($resp, true);
       if(array_key_exists("errors", $data)) {
         return False;
       } else { //presumably, the given course has a BB_Direct folder after all then
         return True;
       }
 }

Now, the code I have listed works, it does what I want it to do. But the dataset I am working on has 7000 entries in it, so this takes a lot of time to compute. My question is, how can I OPTIMISE this? Can I find a certain folder in my list of courses any more efficiently?

Any thoughts anyone could lend me would be greatly appreciated

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.