6 use ceLTIc\LTI\HttpMessage;
23 private static $DEFAULT_PER_PAGE = 50;
33 private $token =
null;
38 private $courseId =
null;
43 private $sourceObject =
null;
52 private function get($withGroups)
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;
61 $perPage = intval($perPage);
63 $prefix = $consumer->getSetting(
'moodle.grouping_prefix');
64 if ($this->url && $this->token && $this->courseId) {
66 $this->setGroupings($prefix);
68 $users = $this->getUsers($perPage, $withGroups);
70 $this->setGroups($users);
85 private function setGroupings($prefix)
87 $this->sourceObject->groupSets = array();
88 $this->sourceObject->groups = array();
90 'courseid' => $this->courseId
92 $courseGroupings = $this->callMoodleApi(
'core_group_get_course_groupings', $params);
93 if ($courseGroupings) {
94 $groupingIds = array_map(
function($grouping) {
98 'groupingids' => $groupingIds,
101 $groupings = $this->callMoodleApi(
'core_group_get_groupings', $params);
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);
127 private function getUsers($perPage, $withGroups)
132 'courseid' => $this->courseId,
135 'name' =>
'onlyactive',
139 'name' =>
'userfields',
143 'name' =>
'withcapability',
144 'value' =>
'mod/lti:manage' 149 $enrolments = $this->callMoodleApi(
'core_enrol_get_enrolled_users', $params);
151 foreach ($enrolments as $enrolment) {
152 $teachers[] = $enrolment->id;
155 $userFields =
'id, username, firstname, lastname, email, roles';
157 $userFields .=
', groups';
160 'courseid' => $this->courseId,
163 'name' =>
'onlyactive',
167 'name' =>
'userfields',
168 'value' => $userFields
173 array_push($params[
'options'],
175 'name' =>
'limitnumber',
183 array_push($params[
'options'],
185 'name' =>
'limitfrom',
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')) {
198 $user->ltiUserId = $userId;
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;
211 if (in_array($enrolment->id, $teachers)) {
212 $user->roles[] =
'urn:lti:role:ims/lis/Instructor';
214 $user->roles[] =
'urn:lti:role:ims/lis/Learner';
216 $users[$userId] = $user;
219 $n += count($enrolments);
220 array_pop($params[
'options']);
226 }
while ($enrolments);
236 private function setGroups($users)
238 foreach ($users as $user) {
240 foreach ($user->groups as $group) {
241 if (array_key_exists($group, $this->sourceObject->groups)) {
242 $setId = $this->sourceObject->groups[$group][
'set'];
244 if (in_array($setId, $sets)) {
246 foreach ($users as $user2) {
247 foreach ($user2->groups as $groupId) {
248 if ($this->sourceObject->groups[$groupId][
'set'] === $setId) {
249 unset($user2->groups[$groupId]);
253 foreach ($this->sourceObject->groupSets[$setId][
'groups'] as $groupId) {
254 unset($this->sourceObject->groups[$groupId]);
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'] ++;
262 if ($user->isLearner()) {
263 $this->sourceObject->groupSets[$setId][
'num_learners'] ++;
280 private function callMoodleApi($method, $params)
283 $serviceUrl = $this->url .
'/webservice/rest/server.php';
284 $params = array_merge(array(
285 'wstoken' => $this->token,
286 'wsfunction' => $method,
287 'moodlewsrestformat' =>
'json' 289 $http =
new HttpMessage($serviceUrl,
'POST', $params);
292 $json = json_decode($http->response);
293 $http->ok = !is_null($json) && is_array($json);
static fromResourceLink($resourceLink, $ltiUserId)
Class constructor from resource link.
Class to represent a tool consumer user.
Class to handle Moodle web service requests.