public function doMembershipsService()
 {
     $users = array();
     $old_users = $this->getUserResultSourcedIDs(TRUE, BasicLTI_Tool_Provider::ID_SCOPE_RESOURCE);
     $this->ext_response = NULL;
     $url = $this->getSetting('ext_ims_lis_memberships_url');
     $params = array();
     $params['id'] = $this->getSetting('ext_ims_lis_memberships_id');
     if ($this->doService('basic-lis-readmembershipsforcontext', $url, $params)) {
         if (!isset($this->ext_nodes['memberships']['member'])) {
             $members = array();
         } else {
             if (!isset($this->ext_nodes['memberships']['member'][0])) {
                 $members = array();
                 $members[0] = $this->ext_nodes['memberships']['member'];
             } else {
                 $members = $this->ext_nodes['memberships']['member'];
             }
         }
         for ($i = 0; $i < count($members); $i++) {
             $user = new LTI_User($this, $members[$i]['user_id']);
             #
             ### Set the user name
             #
             $firstname = isset($members[$i]['person_name_given']) ? $members[$i]['person_name_given'] : '';
             $lastname = isset($members[$i]['person_name_family']) ? $members[$i]['person_name_family'] : '';
             $fullname = isset($members[$i]['person_name_full']) ? $members[$i]['person_name_full'] : '';
             $user->setNames($firstname, $lastname, $fullname);
             #
             ### Set the user email
             #
             $email = isset($members[$i]['person_contact_email_primary']) ? $members[$i]['person_contact_email_primary'] : '';
             $user->setEmail($email, $this->consumer_instance->defaultEmail);
             #
             ### Set the user roles
             #
             if (isset($members[$i]['roles'])) {
                 $user->roles = explode(',', $members[$i]['roles']);
             }
             #
             ### If a result sourcedid is provided save the user
             #
             if (isset($members[$i]['lis_result_sourcedid'])) {
                 $user->lti_result_sourcedid = $members[$i]['lis_result_sourcedid'];
                 $user->save();
             }
             $users[] = $user;
             #
             ### Remove old user (if it exists)
             #
             unset($old_users[$user->getId(BasicLTI_Tool_Provider::ID_SCOPE_RESOURCE)]);
         }
         #
         ### Delete any old users which were not in the latest list from the tool consumer
         #
         foreach ($old_users as $id => $user) {
             $user->delete();
         }
     } else {
         $users = array_values($old_users);
     }
     return $users;
 }
 /**
  * Perform a Memberships service request.
  *
  * The user table is updated with the new list of user objects.
  *
  * @param boolean $withGroups True is group information is to be requested as well
  *
  * @return mixed Array of LTI_User objects or False if the request was not successful
  */
 public function doMembershipsService($withGroups = FALSE)
 {
     $users = array();
     $old_users = $this->getUserResultSourcedIDs(TRUE, LTI_Tool_Provider::ID_SCOPE_RESOURCE);
     $this->ext_response = NULL;
     $url = $this->getSetting('ext_ims_lis_memberships_url');
     $params = array();
     $params['id'] = $this->getSetting('ext_ims_lis_memberships_id');
     $ok = FALSE;
     if ($withGroups) {
         $ok = $this->doService('basic-lis-readmembershipsforcontextwithgroups', $url, $params);
     }
     if ($ok) {
         $this->group_sets = array();
         $this->groups = array();
     } else {
         $ok = $this->doService('basic-lis-readmembershipsforcontext', $url, $params);
     }
     if ($ok) {
         if (!isset($this->ext_nodes['memberships']['member'])) {
             $members = array();
         } else {
             if (!isset($this->ext_nodes['memberships']['member'][0])) {
                 $members = array();
                 $members[0] = $this->ext_nodes['memberships']['member'];
             } else {
                 $members = $this->ext_nodes['memberships']['member'];
             }
         }
         for ($i = 0; $i < count($members); $i++) {
             $user = new LTI_User($this, $members[$i]['user_id']);
             #
             ### Set the user name
             #
             $firstname = isset($members[$i]['person_name_given']) ? $members[$i]['person_name_given'] : '';
             $lastname = isset($members[$i]['person_name_family']) ? $members[$i]['person_name_family'] : '';
             $fullname = isset($members[$i]['person_name_full']) ? $members[$i]['person_name_full'] : '';
             $user->setNames($firstname, $lastname, $fullname);
             #
             ### Set the user email
             #
             $email = isset($members[$i]['person_contact_email_primary']) ? $members[$i]['person_contact_email_primary'] : '';
             $user->setEmail($email, $this->consumer->defaultEmail);
             #
             ### Set the user roles
             #
             if (isset($members[$i]['roles'])) {
                 $user->roles = LTI_Tool_Provider::parseRoles($members[$i]['roles']);
             }
             #
             ### Set the user groups
             #
             if (!isset($members[$i]['groups']['group'])) {
                 $groups = array();
             } else {
                 if (!isset($members[$i]['groups']['group'][0])) {
                     $groups = array();
                     $groups[0] = $members[$i]['groups']['group'];
                 } else {
                     $groups = $members[$i]['groups']['group'];
                 }
             }
             for ($j = 0; $j < count($groups); $j++) {
                 $group = $groups[$j];
                 if (isset($group['set'])) {
                     $set_id = $group['set']['id'];
                     if (!isset($this->group_sets[$set_id])) {
                         $this->group_sets[$set_id] = array('title' => $group['set']['title'], 'groups' => array(), 'num_members' => 0, 'num_staff' => 0, 'num_learners' => 0);
                     }
                     $this->group_sets[$set_id]['num_members']++;
                     if ($user->isStaff()) {
                         $this->group_sets[$set_id]['num_staff']++;
                     }
                     if ($user->isLearner()) {
                         $this->group_sets[$set_id]['num_learners']++;
                     }
                     if (!in_array($group['id'], $this->group_sets[$set_id]['groups'])) {
                         $this->group_sets[$set_id]['groups'][] = $group['id'];
                     }
                     $this->groups[$group['id']] = array('title' => $group['title'], 'set' => $set_id);
                 } else {
                     $this->groups[$group['id']] = array('title' => $group['title']);
                 }
                 $user->groups[] = $group['id'];
             }
             #
             ### If a result sourcedid is provided save the user
             #
             if (isset($members[$i]['lis_result_sourcedid'])) {
                 $user->lti_result_sourcedid = $members[$i]['lis_result_sourcedid'];
                 $user->save();
             }
             $users[] = $user;
             #
             ### Remove old user (if it exists)
             #
             unset($old_users[$user->getId(LTI_Tool_Provider::ID_SCOPE_RESOURCE)]);
         }
         #
         ### Delete any old users which were not in the latest list from the tool consumer
         #
         foreach ($old_users as $id => $user) {
             $user->delete();
         }
     } else {
         $users = FALSE;
     }
     return $users;
 }