/**
  * Updates a contact's details in the DB according to the
  * values passed via AJAX
  * @global object $USER Moodle user object (session persistent)
  * @param int $addressbookId ID of addressbook to update
  * @param int $contactId ID of contact to update
  * @param string $contactFirstName Contact's first name
  * @param string $contactLastName Contact's last name
  * @param string $contactCompany Contact's employer
  * @param string $contactPhoneNumber Contact's phone number
  * @return string JSON-encoded response to request
  * @throws MoodletxtAJAXException
  * @version 2012061801
  * @since 2012090501
  */
 private function updateContact($addressbookId, $contactId, $contactFirstName, $contactLastName, $contactCompany, $contactPhoneNumber)
 {
     global $USER;
     // Check that user owns DB
     if (!$this->addressbookDAO->checkAddressbookOwnership($addressbookId, $USER->id)) {
         throw new MoodletxtAJAXException(get_string('errorbooknotowned', 'block_moodletxt'), MoodletxtAJAXException::$ERROR_NOT_ADDRESSBOOK_OWNER, null, false);
     }
     // Get record from DB and update it
     try {
         $contact = $this->addressbookDAO->getAddressbookContactById($addressbookId, $contactId);
         $contact->setFirstName($contactFirstName);
         $contact->setLastName($contactLastName);
         $contact->setCompanyName($contactCompany);
         $contact->setRecipientNumber(new MoodletxtPhoneNumber($contactPhoneNumber));
         $this->addressbookDAO->saveContact($contact);
     } catch (InvalidArgumentException $ex) {
         throw new MoodletxtAJAXException(get_string('errorbadcontactid', 'block_moodletxt'), MoodletxtAJAXException::$ERROR_CODE_BAD_CONTACT_ID, null, false);
     }
     return $this->buildResponse($contact);
 }
    $customFormData['potentialGroups'][$group->getId()] = $group->getName();
}
$GLOBALS['_HTML_QuickForm_default_renderer'] = new QuickFormRendererWithSlides();
// Override renderer for multi-select
$contactForm = new MoodletxtContactAddForm(null, $customFormData);
$notifications = '';
// POST PROCESSING
$formData = $contactForm->get_data();
if ($formData != null) {
    $formData = $contactForm->cleanupFormData($formData);
    // Create contact, add groups, save to DB
    $newContact = new MoodletxtAddressbookRecipient(new MoodletxtPhoneNumber($formData->phoneNumber), $formData->firstName, $formData->lastName, $formData->company, 0, $addressbook->getId());
    foreach ($formData->groups as $groupId) {
        $newContact->addGroup($addressbookDAO->getAddressbookGroupById($formData->addressbook, $groupId));
    }
    $addressbookDAO->saveContact($newContact);
    // Based on which button was pressed, we either send the
    // user back to the addressbook or clear the form
    if ($formData->submitButton == get_string('buttoncontactaddreturn', 'block_moodletxt')) {
        $addressbookPageUrl = new moodle_url('/blocks/moodletxt/addressbook_view.php', array('course' => $courseId, 'instance' => $instanceId, 'addressbook' => $addressbookId));
        redirect($addressbookPageUrl, get_string('redirectcontactadded', 'block_moodletxt'));
    } else {
        if ($formData->submitButton == get_string('buttoncontactadd', 'block_moodletxt')) {
            $contactForm->clearSubmittedValues();
            $notifications .= $output->notification(get_string('notifycontactadded', 'block_moodletxt'), 'notifysuccess');
        }
    }
}
// Initialise form defaults
$contactForm->set_data($initialFormData);
// BEGIN PAGE OUTPUT