-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfunctionsLMS.php
executable file
·214 lines (201 loc) · 11 KB
/
functionsLMS.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Name: Threadz
// Author: Matt Lewis
// Organization: Eastern Washington University - Instructional Technology
// Copyright: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License
// Version: 1.0
// Used by: canvas-data.php
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//NOTES:
//Because the format of the data being returned by Canvas may not be standard to another LMS, specific functions have been
//created for the Canvas data connection and extraction. If you are planning on adding Threadz to a different LMS,
//you will need to create similar data functions and add them to this page.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function getCanvasAPIcurl($authorization, $url){
//// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt($curl, CURLOPT_URL,$url);
//curl_setopt($curl, CURLOPT_PROXY, $_SESSION['proxy']);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json', $authorization ));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
//curl_setopt($curl, CURLOPT_HTTPGET, true);
// Send the request & save response to $resp
$data = curl_exec($curl);
$info = curl_getinfo($curl);
if(curl_errno($curl) !=0){
echo "error numbers:". curl_errno($curl) . "<br>";
echo "error message:". curl_error($curl) . "<br>";
echo '<br>Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
curl_close($curl);
exit();
}
// Close request to clear up some resources
curl_close($curl);
$arrCurl['header'] = substr( $data, 0, $info['header_size']);
$arrCurl['headerLinks'] = getCanvasHeaderLinks($arrCurl['header']);
$arrCurl['body'] = substr($data, $info['header_size']);
$arrCurl['response'] = $data;
$arrCurl['info'] = $info;
return $arrCurl;
}
function getCanvasHeaderLinks($header){
//The header Canvas returns for the curent, next, first, and last links is formated like:
// <https://[domainLMS]/api/v1/courses/[courseID]/enrollments?page=1&per_page=3>; rel="current",
// <https://[domainLMS]/api/v1/courses/[courseID]/enrollments?page=2&per_page=3>; rel="next",
// <https://[domainLMS]/api/v1/courses/[courseID]/enrollments?page=1&per_page=3>; rel="first",
// <https://[domainLMS]/api/v1/courses/[courseID]/enrollments?page=2&per_page=3>; rel="last"
//We need to extract the links to determine if there are more pages of data in the API.
//Note: until we started removing the < and > characters, the content between was hidden (see below). I don't understand why, but removing them solved that issue.
//Split full header into an array with the :
$headerArray= explode(': ',$header);
//find the location in array of the 'next' url.
foreach($headerArray as $key=> $val){
if(strpos($val,'rel=')){
$urlKey = $key;
break;
}
}
$string = $headerArray[$urlKey];
//split this into it's own array on rel=
$links = explode('rel=', $string);
$headerReplace = array('<', '>',';');
foreach($links as $key =>$val){
$pos1a = 0;
$pos2a = 0;
if($key != 0){
//Because the link precedes name, splitting the array at 'rel=' places the name into the following key.
//To place the name in as the key, we find the
$pos1a = strpos($val,'"');
$pos2a = strpos($val,'"', 1);
$title = substr($val, $pos1a+1, $pos2a-1);
if($key == 1){
$headerLinks[$title] = str_replace($headerReplace, "", substr($links[$key-1],$pos1b));
}else{
$headerLinks[$title] = str_replace($headerReplace, "", substr($links[$key-1],$pos2b+2));
}
}
$pos1b= $pos1a;
$pos2b= $pos2a;
}
return $headerLinks;
}
function getCanvasRoster($arrCurlRoster, $authorization){
$jsonRoster = json_decode($arrCurlRoster['body'], true);
$_SESSION['course']['roster']['students'] = array();
//Depending on the role of the users ($_SESSION['roles']) there are some other data points that are available from the enrollment api call.
//Currently there is no use case set for applying this data to the analytics, so it is not being captured. But in the future there might be a need.
//total_activity_time
//last_activity_at
//grades:current_score
//grades:final_score
foreach($jsonRoster as $roster){
$_SESSION['course']['roster'][$roster['user_id']] = array(
'json' => $jsonData,
'user_id'=> $roster['user_id'],
'section_integration_id'=> $roster['section_integration_id'],
'name'=> $roster['user']['name'],
'enrollment_state'=> $roster['enrollment_state'],
'role'=> $roster['type'],
);
$_SESSION['course']['roster']['students'][] = $roster['user_id'];
}
//if there are more records to be returned, Canvas will have the link in the url in the header
if(array_key_exists('next', $arrCurlRoster['headerLinks'])){
$arrCurlRoster=getCanvasAPIcurl($authorization, $arrCurlRoster['headerLinks']['next']);
getCanvasRoster($arrCurlRoster);
}
}
function getCanvasTopicList($arrCurlTopics, $authorization){
//Save json Data of all course discussion topics into SESSION
$jsonTopics = json_decode($arrCurlTopics['body'], true);
$_SESSION['jsonTopics'] = $jsonTopics;
//Step through each json record, saving data into SESSION
foreach($jsonTopics as $topic){
$topic_id = $topic['id'];
$topic_title = $topic['title'];
//echo $topic_id . "<br>";
if($topic['assignment_id']){
$due_at = $topic['assignment']['due_at'];
}else{
$due_at = "";
}
$_SESSION['topicList'][$topic_id] = array(
'topic_id' => $topic_id,
'topic_title'=> $topic_title,
'topic_subentry_count' => $topic['discussion_subentry_count'],
'published'=> $topic['published'],
'require_initial_post'=> $topic['require_initial_post'],
'users_can_see_posts'=> $topic['users_can_see_posts'],
'assignment_id'=> $topic['assignment_id'],
'due_at'=> $due_at,
'message' => $topic['message'],
'author_id' => $topic['author']['id'],
'topic_url' => $topic['url'],
'discussion_type' => $topic['discussion_type'],
'topic_subentry_count' => $topic['discussion_subentry_count'],
'topic_word_count' => 0,
'allow_rating' => $topic['allow_rating'],
'unread_count' => $topic['unread_count'],
'group_category_id' => $topic['group_category_id'],
'topic_children' => $topic['topic_children'],
);
$urlTopic = $_SESSION['domainLMS']."/api/v1/courses/".$_SESSION['courseID']."/discussion_topics/".$topic_id."/view.json";
$arrCurlTopic=getCanvasAPIcurl($authorization, $urlTopic);
getCanvasTopicData($arrCurlTopic, $authorization, $topic_id);
//create HTML display Topic list for published discussions and with discussions with one or more posts.
if($topic['published'] == true && $topic['discussion_subentry_count']>0){
$_SESSION['select_list_option'] .="<option value='".$topic_id."'>".$topic_title."</option>";
//count the number of topics that are published and have more than one post, this count determins what
//initially gets displayed on threadz.php
$_SESSION['countOfTopic']++;
}
}
// var_dump($_SESSION['course']['roster']);
// echo "<br>========<br>";
// var_dump($_SESSION['topicList'][3034092]);
//exit();
//if there are more records to be returned, Canvas will have the link in the url in the header
if(array_key_exists('next', $arrCurlTopics['headerLinks'])){
$arrCurlTopics=getCanvasAPIcurl($authorization, $arrCurlTopics['headerLinks']['next']);
getCanvasTopicList($arrCurlTopics);
}
}
function getCanvasTopicData($arrCurlTopic, $authorization, $topic_id){
//save individual discussion topic post data into SESSION using the topic id as the key name
$jsonData = json_decode($arrCurlTopic['body'], true);
if($jsonData.status != "unauthenticated"){
$_SESSION['json_'.$topic_id] = $jsonData;
$_SESSION['arrTopics'][$topic_id]['json'] = $jsonData;
$_SESSION['arrTopics'][$topic_id]['settings'] = $_SESSION['topicList'][$topic_id];
// 'json' => $jsonData,
//save abridged topic data into SESSION
//$_SESSION['arrTopics'][$topic_id] = array(
// 'json' => $jsonData,
// 'topic_id'=> $topic['id'],
// 'published'=> $topic['published'],
// 'url'=> $topic['url'],
// 'topic_title' => $topic_title,
// 'message' => $topic['message'],
// 'author_id' => $topic['author']['id'],
// 'due_date' => $topic['due_date'],
// 'topic_url' => $topic['url'],
// 'assignment_id' => $topic['assignment_id'],
// 'discussion_type' => $topic['discussion_type'],
// 'topic_subentry_count' => $topic['discussion_subentry_count'],
// 'topic_word_count' => 0,
// 'allow_rating' => $topic['allow_rating'],
// 'require_initial_post' => $topic['allow_rating'],
// 'unread_count' => $topic['unread_count'],
//);
//if there are more records to be returned, Canvas will have the link in the url in the header
if(array_key_exists('next', $arrCurlTopic['headerLinks'])){
$arrCurlTopic=getCanvasAPIcurl($authorization, $arrCurlTopic['headerLinks']['next']);
getCanvasTopicData($arrCurlTopic, $authorization, $topic_id);
}
}
}
?>