LTI Integration Library  3.1.0
PHP class library for building LTI integrations
MoodleApi.php
Go to the documentation of this file.
1 <?php
2 
4 
6 use ceLTIc\LTI\HttpMessage;
7 
17 trait MoodleApi
18 {
19 
23  private static $DEFAULT_PER_PAGE = 50;
24 
28  private $url = null;
29 
33  private $token = null;
34 
38  private $courseId = null;
39 
43  private $sourceObject = null;
44 
52  private function get($withGroups)
53  {
54  $consumer = $this->sourceObject->getConsumer();
55  $this->url = $consumer->getSetting('moodle.url');
56  $this->token = $consumer->getSetting('moodle.token');
57  $perPage = $consumer->getSetting('moodle.per_page', '');
58  if (!is_numeric($perPage)) {
59  $perPage = self::$DEFAULT_PER_PAGE;
60  } else {
61  $perPage = intval($perPage);
62  }
63  $prefix = $consumer->getSetting('moodle.grouping_prefix');
64  if ($this->url && $this->token && $this->courseId) {
65  if ($withGroups) {
66  $this->setGroupings($prefix);
67  }
68  $users = $this->getUsers($perPage, $withGroups);
69  if ($withGroups) {
70  $this->setGroups($users);
71  }
72  } else {
73  $users = false;
74  }
75 
76  return $users;
77  }
78 
85  private function setGroupings($prefix)
86  {
87  $this->sourceObject->groupSets = array();
88  $this->sourceObject->groups = array();
89  $params = array(
90  'courseid' => $this->courseId
91  );
92  $courseGroupings = $this->callMoodleApi('core_group_get_course_groupings', $params);
93  if ($courseGroupings) {
94  $groupingIds = array_map(function($grouping) {
95  return $grouping->id;
96  }, $courseGroupings);
97  $params = array(
98  'groupingids' => $groupingIds,
99  'returngroups' => 1
100  );
101  $groupings = $this->callMoodleApi('core_group_get_groupings', $params);
102  if ($groupings) {
103  foreach ($groupings as $grouping) {
104  if (!empty($grouping->groups) && (empty($prefix) || (strpos($grouping->name, $prefix) === 0))) {
105  $groupingId = strval($grouping->id);
106  $this->sourceObject->groupSets[$groupingId] = array('title' => $grouping->name, 'groups' => array(),
107  'num_members' => 0, 'num_staff' => 0, 'num_learners' => 0);
108  foreach ($grouping->groups as $group) {
109  $groupId = strval($group->id);
110  $this->sourceObject->groupSets[$groupingId]['groups'][] = $groupId;
111  $this->sourceObject->groups[$groupId] = array('title' => $group->name, 'set' => $groupingId);
112  }
113  }
114  }
115  }
116  }
117  }
118 
127  private function getUsers($perPage, $withGroups)
128  {
129  $users = array();
130 
131  $params = array(
132  'courseid' => $this->courseId,
133  'options' => array(
134  array(
135  'name' => 'onlyactive',
136  'value' => 1
137  ),
138  array(
139  'name' => 'userfields',
140  'value' => 'id'
141  ),
142  array(
143  'name' => 'withcapability',
144  'value' => 'mod/lti:manage'
145  )
146  )
147  );
148  $teachers = array();
149  $enrolments = $this->callMoodleApi('core_enrol_get_enrolled_users', $params);
150  if ($enrolments) {
151  foreach ($enrolments as $enrolment) {
152  $teachers[] = $enrolment->id;
153  }
154  }
155  $userFields = 'id, username, firstname, lastname, email, roles';
156  if ($withGroups) {
157  $userFields .= ', groups';
158  }
159  $params = array(
160  'courseid' => $this->courseId,
161  'options' => array(
162  array(
163  'name' => 'onlyactive',
164  'value' => 1
165  ),
166  array(
167  'name' => 'userfields',
168  'value' => $userFields
169  )
170  )
171  );
172  if ($perPage > 0) {
173  array_push($params['options'],
174  array(
175  'name' => 'limitnumber',
176  'value' => $perPage
177  )
178  );
179  }
180  $n = 0;
181  do {
182  if ($perPage > 0) {
183  array_push($params['options'],
184  array(
185  'name' => 'limitfrom',
186  'value' => $n
187  )
188  );
189  }
190  $enrolments = $this->callMoodleApi('core_enrol_get_enrolled_users', $params);
191  if (!is_null($enrolments)) {
192  foreach ($enrolments as $enrolment) {
193  $userId = strval($enrolment->id);
194  if (is_a($this->sourceObject, 'ceLTIc\LTI\ResourceLink')) {
195  $user = UserResult::fromResourceLink($this->sourceObject, $userId);
196  } else {
197  $user = new UserResult();
198  $user->ltiUserId = $userId;
199  }
200  $user->setEmail($enrolment->email, $this->sourceObject->getConsumer()->defaultEmail);
201  $user->setNames($enrolment->firstname, $enrolment->lastname, $enrolment->fullname);
202  if (!empty($enrolment->groups)) {
203  foreach ($enrolment->groups as $group) {
204  $groupId = strval($group->id);
205  if (array_key_exists($groupId, $this->sourceObject->groups)) {
206  $user->groups[] = $groupId;
207  }
208  }
209  }
210  // Add Instructor or Learner role - NB no check is made for the Administrator role
211  if (in_array($enrolment->id, $teachers)) {
212  $user->roles[] = 'urn:lti:role:ims/lis/Instructor';
213  } else {
214  $user->roles[] = 'urn:lti:role:ims/lis/Learner';
215  }
216  $users[$userId] = $user;
217  }
218  if ($perPage > 0) {
219  $n += count($enrolments);
220  array_pop($params['options']);
221  }
222  } else {
223  $users = false;
224  break;
225  }
226  } while ($enrolments);
227 
228  return $users;
229  }
230 
236  private function setGroups($users)
237  {
238  foreach ($users as $user) {
239  $sets = array();
240  foreach ($user->groups as $group) {
241  if (array_key_exists($group, $this->sourceObject->groups)) {
242  $setId = $this->sourceObject->groups[$group]['set'];
243  // Check that user is not a member of another group in the same grouping
244  if (in_array($setId, $sets)) {
245  // Remove grouping and groups
246  foreach ($users as $user2) {
247  foreach ($user2->groups as $groupId) {
248  if ($this->sourceObject->groups[$groupId]['set'] === $setId) {
249  unset($user2->groups[$groupId]);
250  }
251  }
252  }
253  foreach ($this->sourceObject->groupSets[$setId]['groups'] as $groupId) {
254  unset($this->sourceObject->groups[$groupId]);
255  }
256  unset($this->sourceObject->groupSets[$setId]);
257  } else if (array_key_exists($group, $this->sourceObject->groups)) {
258  $this->sourceObject->groupSets[$setId]['num_members'] ++;
259  if ($user->isStaff()) {
260  $this->sourceObject->groupSets[$setId]['num_staff'] ++;
261  }
262  if ($user->isLearner()) {
263  $this->sourceObject->groupSets[$setId]['num_learners'] ++;
264  }
265  $sets[] = $setId;
266  }
267  }
268  }
269  }
270  }
271 
280  private function callMoodleApi($method, $params)
281  {
282  $json = null;
283  $serviceUrl = $this->url . '/webservice/rest/server.php';
284  $params = array_merge(array(
285  'wstoken' => $this->token,
286  'wsfunction' => $method,
287  'moodlewsrestformat' => 'json'
288  ), $params);
289  $http = new HttpMessage($serviceUrl, 'POST', $params);
290  $http->send();
291  if ($http->ok) {
292  $json = json_decode($http->response);
293  $http->ok = !is_null($json) && is_array($json);
294  }
295 
296  return $json;
297  }
298 
299 }
static fromResourceLink($resourceLink, $ltiUserId)
Class constructor from resource link.
Definition: UserResult.php:255
Class to represent a tool consumer user.
Definition: UserResult.php:15
Class to handle Moodle web service requests.
Definition: MoodleApi.php:17