-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathCreateCourseCommand.php
More file actions
97 lines (90 loc) · 2.31 KB
/
CreateCourseCommand.php
File metadata and controls
97 lines (90 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace App\CurrikiGo\Canvas\Commands;
use App\CurrikiGo\Canvas\Contracts\Command;
/**
* This class handles course creation via API in Canvas
*/
class CreateCourseCommand implements Command
{
/**
* API URL
*
* @var string
*/
public $apiURL;
/**
* Access Token for api requests
*
* @var string
*/
public $accessToken;
/**
* HTTP Client instance
*
* @var \GuzzleHttp\Client
*/
public $httpClient;
/**
* Account Id
*
* @var int|string
*/
private $accountId;
/**
* Course data
*
* @var array
*/
private $courseData;
/**
* Creates an instance of the command class
*
* @param string|int $accountId
* @param array $courseData
* @param array $sisId
* @return void
*/
public function __construct($courseName, $accountId)
{
$this->accountId = $accountId;
$this->courseData = $this->prepareCourseData($courseName);
}
/**
* Execute an API request for creating a course
*
* @return string|null
*/
public function execute()
{
$response = null;
try {
$response = $this->httpClient->request('POST', $this->apiURL . '/accounts/' . $this->accountId . '/courses', [
'headers' => ['Authorization' => "Bearer {$this->accessToken}", 'Accept' => 'application/json'],
'json' => $this->courseData
])->getBody()->getContents();
$response = json_decode($response);
}
catch (Exception $ex) {
}
return $response;
}
/**
* Prepare course data for API payload
*
* @param array $data
* @return array
*/
public function prepareCourseData($courseName)
{
$course["name"] = $courseName;
$short_name = strtolower(implode('-', explode(' ', $courseName)));
$course["course_code"] = $short_name;
$course["license"] = "public_domain";
$course["public_syllabus_to_auth"] = true;
$course["public_description"] = $course["name"] . " by CurrikiStudio";
$course["default_view"] = "modules";
$course["course_format"] = "online";
$enrollMe = true;
return ["course" => $course, "enroll_me" => $enrollMe];
}
}