/** * Grabs a single Moodle user from the DB by its record ID * @global moodle_database $DB Moodle database manager * @param int $userId User record ID * @param boolean $includeConfig * @return MoodletxtBiteSizedUser Moodle user * @version 2012060101 * @since 2011062701 */ public function getUserById($userId, $includeConfig = false, $includeTemplates = false) { global $DB; $numberSource = get_config('moodletxt', 'Phone_Number_Source'); $numberField = $numberSource == 'phone1' ? 'phone1' : 'phone2'; // Protects against bad DB values and abstracts field name $fields = 'id, username, firstname, lastname, ' . $numberField; $moodleUser = $DB->get_record('user', array('id' => $userId), $fields); if (!is_object($moodleUser)) { throw new InvalidArgumentException(get_string('errorinvalidaccountid', 'block_moodletxt')); } $userObject = new MoodletxtBiteSizedUser($moodleUser->id, $moodleUser->username, $moodleUser->firstname, $moodleUser->lastname); try { $phoneObject = new MoodletxtPhoneNumber($moodleUser->{$numberField}); $userObject->setRecipientNumber($phoneObject); } catch (InvalidPhoneNumberException $ex) { // Profile's phone number is invalid - not necessary to throw further } // Include optionals if requested if ($includeConfig) { $userObject->setConfig($this->getUserConfig($userObject->getId())); } if ($includeTemplates) { $userObject->setTemplates($this->templateDAO->getAllTemplatesForUserId($userObject->getId())); } return $userObject; }
/** * Adds a Moodle user to the group * @param MoodletxtBiteSizedUser $user User to add * @version 2011102501 * @since 2011102501 */ public function addUser(MoodletxtBiteSizedUser $user) { $this->users[$user->getId()] = $user; }
/** * Sets the ID of the Moodle user * who owns this addressbook * @param MoodletxtBiteSizedUser $user User to set as owner * @version 2012080701 * @since 2012080701 */ public function setOwner(MoodletxtBiteSizedUser $user) { $this->ownerId = $user->getId(); }