/**
  * 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;
 }
 /**
  * Converts a raw database record to a full-fat message object
  * @param object $stdObject Database record object
  * @return MoodletxtInboundMessage Constructed message object
  * @TODO Store and retrieve destination txttools accounts
  * @version 2012060101
  * @since 2011080401
  */
 private function convertStandardClassToBeans($stdObject)
 {
     $inboundMessage = new MoodletxtInboundMessage($stdObject->ticket, $stdObject->messagetext, new MoodletxtPhoneNumber($stdObject->source), $stdObject->timereceived, $stdObject->hasbeenread, $stdObject->id);
     if (isset($stdObject->owneruser)) {
         $inboundMessage->setOwner(new MoodletxtBiteSizedUser($stdObject->userid, $stdObject->owneruser, $stdObject->ownerfirst, $stdObject->ownerlast));
     }
     if (isset($stdObject->sourceusername)) {
         $userObject = new MoodletxtBiteSizedUser($stdObject->sourceuserid, $stdObject->sourceusername, $stdObject->sourceuserfirst, $stdObject->sourceuserlast);
         try {
             $userNumber = new MoodletxtPhoneNumber($stdObject->sourceuserphone);
             $userObject->setRecipientNumber($userNumber);
         } catch (InvalidPhoneNumberException $ex) {
             // Phone number in user's Moodle profile is invalid - ignore it
         }
         $inboundMessage->setAssociatedSource($userObject);
     } else {
         if (isset($stdObject->contactid)) {
             $inboundMessage->setAssociatedSource(new MoodletxtAddressbookRecipient(new MoodletxtPhoneNumber($stdObject->contactphone), $stdObject->contactfirst, $stdObject->contactlast, $stdObject->contactcompany, $stdObject->contactid));
         }
     }
     $inboundMessage->setSourceFirstName($stdObject->sourcefirstname);
     $inboundMessage->setSourceLastName($stdObject->sourcelastname);
     return $inboundMessage;
 }