AnsweredAssumed Answered

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

Question asked by Lee Newton on Apr 8, 2019
Latest reply on Apr 10, 2019 by Lee Newton

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",
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 there's NO valid pages after latest one 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 thousands of 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 than combing through them all one by one, and checking their ?

Outcomes