original_string: protected function sync_member_information(stdClass $tool, ToolConsumer $consumer, $members) {
          global $DB;
          $usercount = 0;
          $enrolcount = 0;
  
          // Process member information.
          foreach ($members as $member) {
              $usercount++;
  
              // Set the user data.
              $user = new stdClass();
              $user->username = helper::create_username($consumer->getKey(), $member->ltiUserId);
              $user->firstname = core_user::clean_field($member->firstname, 'firstname');
              $user->lastname = core_user::clean_field($member->lastname, 'lastname');
              $user->email = core_user::clean_field($member->email, 'email');
  
              // Get the user data from the LTI consumer.
              $user = helper::assign_user_tool_data($tool, $user);
  
              $dbuser = core_user::get_user_by_username($user->username, 'id');
              if ($dbuser) {
                  // If email is empty remove it, so we don't update the user with an empty email.
                  if (empty($user->email)) {
                      unset($user->email);
                  }
  
                  $user->id = $dbuser->id;
                  user_update_user($user);
  
                  // Add the information to the necessary arrays.
                  if (!in_array($user->id, $this->currentusers)) {
                      $this->currentusers[] = $user->id;
                  }
                  $this->userphotos[$user->id] = $member->image;
              } else {
                  if ($this->should_sync_enrol($tool->membersyncmode)) {
                      // If the email was stripped/not set then fill it with a default one. This
                      // stops the user from being redirected to edit their profile page.
                      if (empty($user->email)) {
                          $user->email = $user->username .  "@example.com";
                      }
  
                      $user->auth = 'lti';
                      $user->id = user_create_user($user);
  
                      // Add the information to the necessary arrays.
                      $this->currentusers[] = $user->id;
                      $this->userphotos[$user->id] = $member->image;
                  }
              }
  
              // Sync enrolments.
              if ($this->should_sync_enrol($tool->membersyncmode)) {
                  // Enrol the user in the course.
                  if (helper::enrol_user($tool, $user->id) === helper::ENROLMENT_SUCCESSFUL) {
                      // Increment enrol count.
                      $enrolcount++;
                  }
  
                  // Check if this user has already been registered in the enrol_lti_users table.
                  if (!$DB->record_exists('enrol_lti_users', ['toolid' => $tool->id, 'userid' => $user->id])) {
                      // Create an initial enrol_lti_user record that we can use later when syncing grades and members.
                      $userlog = new stdClass();
                      $userlog->userid = $user->id;
                      $userlog->toolid = $tool->id;
                      $userlog->consumerkey = $consumer->getKey();
  
                      $DB->insert_record('enrol_lti_users', $userlog);
                  }
              }
          }
  
          return [$usercount, $enrolcount];
      }
code: protected function sync_member_information(stdClass $tool, ToolConsumer $consumer, $members) {
          global $DB;
          $usercount = 0;
          $enrolcount = 0;
  
          // Process member information.
          foreach ($members as $member) {
              $usercount++;
  
              // Set the user data.
              $user = new stdClass();
              $user->username = helper::create_username($consumer->getKey(), $member->ltiUserId);
              $user->firstname = core_user::clean_field($member->firstname, 'firstname');
              $user->lastname = core_user::clean_field($member->lastname, 'lastname');
              $user->email = core_user::clean_field($member->email, 'email');
  
              // Get the user data from the LTI consumer.
              $user = helper::assign_user_tool_data($tool, $user);
  
              $dbuser = core_user::get_user_by_username($user->username, 'id');
              if ($dbuser) {
                  // If email is empty remove it, so we don't update the user with an empty email.
                  if (empty($user->email)) {
                      unset($user->email);
                  }
  
                  $user->id = $dbuser->id;
                  user_update_user($user);
  
                  // Add the information to the necessary arrays.
                  if (!in_array($user->id, $this->currentusers)) {
                      $this->currentusers[] = $user->id;
                  }
                  $this->userphotos[$user->id] = $member->image;
              } else {
                  if ($this->should_sync_enrol($tool->membersyncmode)) {
                      // If the email was stripped/not set then fill it with a default one. This
                      // stops the user from being redirected to edit their profile page.
                      if (empty($user->email)) {
                          $user->email = $user->username .  "@example.com";
                      }
  
                      $user->auth = 'lti';
                      $user->id = user_create_user($user);
  
                      // Add the information to the necessary arrays.
                      $this->currentusers[] = $user->id;
                      $this->userphotos[$user->id] = $member->image;
                  }
              }
  
              // Sync enrolments.
              if ($this->should_sync_enrol($tool->membersyncmode)) {
                  // Enrol the user in the course.
                  if (helper::enrol_user($tool, $user->id) === helper::ENROLMENT_SUCCESSFUL) {
                      // Increment enrol count.
                      $enrolcount++;
                  }
  
                  // Check if this user has already been registered in the enrol_lti_users table.
                  if (!$DB->record_exists('enrol_lti_users', ['toolid' => $tool->id, 'userid' => $user->id])) {
                      // Create an initial enrol_lti_user record that we can use later when syncing grades and members.
                      $userlog = new stdClass();
                      $userlog->userid = $user->id;
                      $userlog->toolid = $tool->id;
                      $userlog->consumerkey = $consumer->getKey();
  
                      $DB->insert_record('enrol_lti_users', $userlog);
                  }
              }
          }
  
          return [$usercount, $enrolcount];
      }
code_tokens: protected function sync_member_information ( stdClass $ tool , ToolConsumer $ consumer , $ members ) { global $ DB ; $ usercount = 0 ; $ enrolcount = 0 ; // Process member information. foreach ( $ members as $ member ) { $ usercount ++ ; // Set the user data. $ user = new stdClass ( ) ; $ user -> username = helper :: create_username ( $ consumer -> getKey ( ) , $ member -> ltiUserId ) ; $ user -> firstname = core_user :: clean_field ( $ member -> firstname , 'firstname' ) ; $ user -> lastname = core_user :: clean_field ( $ member -> lastname , 'lastname' ) ; $ user -> email = core_user :: clean_field ( $ member -> email , 'email' ) ; // Get the user data from the LTI consumer. $ user = helper :: assign_user_tool_data ( $ tool , $ user ) ; $ dbuser = core_user :: get_user_by_username ( $ user -> username , 'id' ) ; if ( $ dbuser ) { // If email is empty remove it, so we don't update the user with an empty email. if ( empty ( $ user -> email ) ) { unset ( $ user -> email ) ; } $ user -> id = $ dbuser -> id ; user_update_user ( $ user ) ; // Add the information to the necessary arrays. if ( ! in_array ( $ user -> id , $ this -> currentusers ) ) { $ this -> currentusers [ ] = $ user -> id ; } $ this -> userphotos [ $ user -> id ] = $ member -> image ; } else { if ( $ this -> should_sync_enrol ( $ tool -> membersyncmode ) ) { // If the email was stripped/not set then fill it with a default one. This // stops the user from being redirected to edit their profile page. if ( empty ( $ user -> email ) ) { $ user -> email = $ user -> username . "@example.com" ; } $ user -> auth = 'lti' ; $ user -> id = user_create_user ( $ user ) ; // Add the information to the necessary arrays. $ this -> currentusers [ ] = $ user -> id ; $ this -> userphotos [ $ user -> id ] = $ member -> image ; } } // Sync enrolments. if ( $ this -> should_sync_enrol ( $ tool -> membersyncmode ) ) { // Enrol the user in the course. if ( helper :: enrol_user ( $ tool , $ user -> id ) === helper :: ENROLMENT_SUCCESSFUL ) { // Increment enrol count. $ enrolcount ++ ; } // Check if this user has already been registered in the enrol_lti_users table. if ( ! $ DB -> record_exists ( 'enrol_lti_users' , [ 'toolid' => $ tool -> id , 'userid' => $ user -> id ] ) ) { // Create an initial enrol_lti_user record that we can use later when syncing grades and members. $ userlog = new stdClass ( ) ; $ userlog -> userid = $ user -> id ; $ userlog -> toolid = $ tool -> id ; $ userlog -> consumerkey = $ consumer -> getKey ( ) ; $ DB -> insert_record ( 'enrol_lti_users' , $ userlog ) ; } } } return [ $ usercount , $ enrolcount ] ; }
docstring: Performs synchronisation of member information and enrolments.
  
  @param stdClass $tool
  @param ToolConsumer $consumer
  @param User[] $members
  @return array An array containing the number of members that were processed and the number of members that were enrolled.
docstring_tokens: Performs synchronisation of member information and enrolments .