automation using scripting and the canvas api

31
Automation using Scripting and the Canvas API David Lippman Pierce College Lumen Learning

Upload: david-lippman

Post on 14-Jun-2015

1.106 views

Category:

Education


1 download

DESCRIPTION

A talk from the Washington Canvas User Group 2014 meeting, about using PHP to automate tasks using the Canvas LMS API. Sample code is available at https://github.com/drlippman/canvas-scripts

TRANSCRIPT

Page 1: Automation using Scripting and the Canvas API

Automation using Scripting and the Canvas API

David LippmanPierce College

Lumen Learning

Page 2: Automation using Scripting and the Canvas API

Outline

• Quick overview of the Canvas API

• Why would we want to use it?

• Examples of scripting against the API

• Examples of scripting against export files

Page 3: Automation using Scripting and the Canvas API

API

API = Application Programming Interface

A way for other programs to access data or make changes in Canvas courses

https://canvas.instructure.com/doc/api/

Page 4: Automation using Scripting and the Canvas API

API

“REST”: Basic GET/PUT/POST HTTP calls

JSON format returnhttps://domain.instructure.com/api/v1/courses

Page 5: Automation using Scripting and the Canvas API

Authentication

HTTP Authorization Header

Send access token in the query string

https://domain.instructure.com/api/v1/courses?access_token=<ACCESS-TOKEN>

Page 6: Automation using Scripting and the Canvas API

Generating a Token

Page 7: Automation using Scripting and the Canvas API

Generating a TokenScroll down…

Page 8: Automation using Scripting and the Canvas API

Generating a Token

Page 9: Automation using Scripting and the Canvas API

Generating a Token

Page 10: Automation using Scripting and the Canvas API
Page 11: Automation using Scripting and the Canvas API

So where does that get us?

or

Why should we care?

Page 12: Automation using Scripting and the Canvas API

Use Case 1: Adding Attribution

Course with 100+ text pagesAll needed a Creative Commons attribution

statement added

Option 1: Edit each by handOption 2: Modify an export fileOption 3: Use the API!

Page 13: Automation using Scripting and the Canvas API

The General Idea

Call the page list.

https://domain.instructure.com/api/v1/courses/12345/pages

Page 14: Automation using Scripting and the Canvas API

The General Idea

Call the page list. Repeat if needed.

$endpoint = “/api/v1/courses/$courseid/pages/”;$itemlist = json_decode( file_get_contents( ‘https://’.$domain.$endpoint. ‘?per_page=50&page=‘.$pagecnt. ‘&access_token=‘.$token ));

foreach ($itemlist as $item) { $url = $item->url;

Page 15: Automation using Scripting and the Canvas API

The General Idea

Grab the wiki page body.

Page 16: Automation using Scripting and the Canvas API

The General Idea

Grab the wiki page body. Add attribution.

$endpoint = “/api/v1/courses/$courseid/pages/$url”;$page = json_decode( file_get_contents( ‘https://’.$domain.$endpoint. ‘?access_token='.$token ));$html = $page->body;$html .= $attribution;

Page 17: Automation using Scripting and the Canvas API

The General Idea

Send back

Page 18: Automation using Scripting and the Canvas API

The General Idea

Send back using CURL

$endpoint = “/api/v1/courses/$courseid/pages/$url”;$ch = curl_init(‘https://’.$domain.$endpoint. ‘?access_token=’.$token );curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");curl_setopt($ch, CURLOPT_POSTFIELDS, ‘wiki_page[body]=’.urlencode($html));$response = curl_exec($ch);

Page 19: Automation using Scripting and the Canvas API

Time to Library-ize

require("canvaslib.php");

$api = new CanvasLMS($token,$domain);

$pages = $api->getPageList($courseid);

foreach ($pages as $id=>$name) {

$body = $api->getPageData($courseid, $id, 'body');

$body .= $attribution;

$api->updatePage($courseid, $id,

array("body"=>$body));

}

Page 20: Automation using Scripting and the Canvas API

Use Case 2: Assignment Settings

Adding Instructions to all Assignments in a course

Page 21: Automation using Scripting and the Canvas API

Use Case 2: Assignment Settings

$assn = $api->getAssignmentList($courseid);

foreach ($assn as $id=>$name) {

$api->updateAssignment($courseid, $id,

array(“description”=>$text));

}

Page 22: Automation using Scripting and the Canvas API

Use Case 3: Fixing links

foreach ($pages as $id=>$name) {

$body = $api->getPageData($courseid, $id, 'body');

$body = str_replace(“oldsite.com”,“newsite.com”,

$body);

$api->updatePage($courseid, $id,

array("body"=>$body));

}

Page 23: Automation using Scripting and the Canvas API

Use Case 4: Removing Links

foreach ($pages as $id=>$name) {

$body = $api->getPageData($courseid, $id, 'body');

$body = preg_replace(

'/<a[^>]*badsite[^>]*>(.*?)<\/a>/sm',

' $1 ', $body);

$api->updatePage($courseid, $id,

array("body"=>$body));

}

Page 24: Automation using Scripting and the Canvas API

Use Case 5: Rehosting Images

preg_match_all(

‘/images\.badsite\.com[^>]*(gif|png|jpg)/‘,

$str, $matches);

foreach ($matches[0] as $m) {

$bn = basename($m);

cp(‘http://’.$m, ’./imgs/’.$bn);

$str = str_replace($m, ‘newhost.com/’.$bn, $str);

}

Page 25: Automation using Scripting and the Canvas API

General purpose, web-based tool

AppendReplaceSearch-and-replaceRegex search-and-replace

http://www.imathas.com/canvas/canvassearch.html

Alternate Approach

Page 26: Automation using Scripting and the Canvas API

Working with Exports

Upsides:• Can be faster for large numbers of pages• Can look at changes before uploading

Downsides• Can’t be done on a live class• Have to make sense of the file format

Page 27: Automation using Scripting and the Canvas API

Working with Exports

• Canvas exports are based on Common Cartridge

• Exports are renamed zip files• Exports contain XML, HTML, and files• Main file is imsmanifest.xml– <resource> shows type and location of items– <item> shows item structure (modules)

Page 28: Automation using Scripting and the Canvas API
Page 29: Automation using Scripting and the Canvas API

require("phpQuery-onefile.php");

$zip = new ZipArchive;$zip->open($file);

phpQuery::newDocumentXML( $zip->getFromName("imsmanifest.xml"));

$ref = pq("resource");foreach ($ref as $r) {

$reflist[pq($r)->attr("identifier")] = pq($r)->attr("href");

$reftype[pq($r)->attr("identifier")] = pq($r)->attr("type");}

Page 30: Automation using Scripting and the Canvas API

$items = pq(“item”);

foreach ($items as $item) {

$iref = pq($item)->attr("identifierref");

if (isset($reftype[$iref]) &&

$reftype[$iref]=="webcontent") {

$filename = $reflist[$iref]);

$html = $zip->getFromName($filename);

$html = str_replace(‘</body>’,

$attribution.’</body>’, $html);

$zip->addFromString($filename, $html);

}

}

Page 31: Automation using Scripting and the Canvas API

Sample Code

https://github.com/drlippman/canvas-scripts

• A simple library for doing API calls• A sample program using the library• The web-based general purpose tool• Search-and-replace in a cartridge example