Your Community is getting an upgrade!
Read about our partnership with Higher Logic and how we will build the next generation of the Instructure Community.
Found this content helpful? Log in or sign up to leave a like!
I am looking for a way to apply the "Restrict students from viewing course after term end date" to a select list of Canvas courses. You can set this for an account using the account Settings option, but I don't want to apply this for all courses in the account, only for a select list.
Any advice on how I might be able to accomplish this programmatically would be appreciated.
Hi,
I have done this via the API. I had a csv file of the course ids, and one by one via a php script I called /api/v1/courses/:course_id/settings with restrict_student_past_view=true. See https://canvas.instructure.com/doc/api/courses.html#method.courses.update_settings for details.
The PHP code snippet looked like this:
$courses = file('courses.csv', FILE_SKIP_EMPTY_LINES);
unset($courses[0]); //first line has a header row, not an id to process
foreach ($courses as $course) {
$course = trim($course);
$url = "courses/sis_course_id:$course/settings";
$json = "restrict_student_past_view=true";
$response = $api->curlPut($url, $json);
}
The curlPut method is from code I found in the community here and rewrote as a class method. It looks like this:
function curlPut($url, $data){
$data = trim($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$this->canvasDomain.'/api/v1/'.$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->tokenHeader);
curl_setopt($ch, CURLOPT_SSLVERSION, 6); //Force requsts to use TLS 1.2
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // ask for results to be returned
// Send to remote and return data to caller.
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
The construct method for this class contains our Canvas domain, an access token and creates the token header: $this->tokenHeader = array("Authorization: Bearer ".$this->token);
It all worked earlier this year when I did this. There might be a more efficient way to do this, but this ought to work.
-S
To interact with Panda Bot, our automated chatbot, you need to sign up or log in:
Sign InTo interact with Panda Bot, our automated chatbot, you need to sign up or log in:
Sign In