-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCreateAssignmentCommand.php
114 lines (107 loc) · 3.03 KB
/
CreateAssignmentCommand.php
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace App\CurrikiGo\Canvas\Commands;
use App\CurrikiGo\Canvas\Contracts\Command;
/**
* This class handles fetching Course details in Canvas LMS
*/
class CreateAssignmentCommand 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;
/**
* Course Id
*
* @var int
*/
private $courseId;
/**
* Request Assignment Group Id
*
* @var array
*/
private $assignmentGroupId;
/**
* Request new assignment name
*
* @var array
*/
private $assignmentName;
/**
* Id of activity in curriki
*
* @var array
*/
private $currikiActivityId;
/**
* Creates an instance of the command class
*
* @param int $courseId
* @param string $queryString
* @return void
*/
public function __construct($courseId, $assignmentGroupId, $assignmentName, $currikiActivityId)
{
$this->courseId = $courseId;
$this->assignmentGroupId = $assignmentGroupId;
$this->assignmentName = $assignmentName;
$this->currikiActivityId = $currikiActivityId;
$this->endpoint = config('constants.canvas_api_endpoints.create_assignment');
$this->courseData = $this->prepareCourseData($assignmentGroupId, $assignmentName, $this->currikiActivityId);
}
/**
* Execute an API request for fetching course details
*
* @return string|null
*/
public function execute()
{
$assignment = $this->courseData;
$response = null;
try {
$response = $this->httpClient->request('POST', $this->apiURL . '/courses/' . $this->courseId . '/' . $this->endpoint, [
'headers' => ['Authorization' => "Bearer {$this->accessToken}"],
'json' => ['assignment' => $assignment]
])->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($assignmentGroupId, $assignmentGroupName, $currikiActivityId)
{
$assignment = [];
$assignment["name"] = $assignmentGroupName;
$assignment['assignment_group_id'] = $assignmentGroupId;
$assignment['self_signup'] = 'enabled';
$assignment['position'] = 1;
$assignment['submission_types'][] = 'external_tool';
$assignment['return_type'] = 'lti_launch_url';
$assignment['workflow_state'] = 'published';
$assignment['points_possible'] = 100;
$assignment['published'] = true;
$assignment['external_tool_tag_attributes']['url'] = config('constants.curriki-tsugi-host') . "?activity=" . $currikiActivityId;
return $assignment;
}
}