<?php

/**
 * User: wernerpe
 * Date: 19.01.2015
 * Time: 10:43
 */
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->libdir . '/formslib.php');

class contact_form extends moodleform {

    function definition() {

        global $CFG, $DB, $USER;
        $mform = & $this->_form;

        $mform->addElement('header', 'personaldetails', get_string('personaldetails', 'local_contactform'));

        $mform->addElement('text', 'firstname', get_string('firstname', 'local_contactform'), 'maxlength="254" size="34"');
        $mform->addRule('firstname', get_string('firstname_mis', 'local_contactform'), 'required', null, 'client');
        $mform->setType('firstname', PARAM_TEXT);

        $mform->addElement('text', 'lastname', get_string('lastname', 'local_contactform'), 'maxlength="254" size="34"');
        $mform->addRule('lastname', get_string('lastname_mis', 'local_contactform'), 'required', null, 'client');
        $mform->setType('lastname', PARAM_TEXT);

        $mform->addElement('text', 'email', get_string('email', 'local_contactform'), 'maxlength="254" size="34"');
        $mform->addRule('email', get_string('email_mis', 'local_contactform'), 'required', null, 'client');
        $mform->addRule('email', get_string('email_mis', 'local_contactform'), 'email', null, 'client');
        $mform->setType('email', PARAM_EMAIL);

        $mform->addElement('text', 'telephone', get_string('telephone', 'local_contactform'), 'maxlength="254" size="34"');

        $mform->setType('telephone', PARAM_TEXT);

        $mform->addElement('header', 'messageheader', get_string('message', 'local_contactform'));

        $contactto = array();
        //  $contactto[] = get_string('pleasechoose', 'local_contactform');
        $contactto[] = get_string('contacttomoodle', 'local_contactform');
        $contactto[] = get_string('contacttoelc', 'local_contactform');

        $mform->addElement('select', 'contacttoselect', get_string('contactto', 'local_contactform'), $contactto, ' style="width: 20em;"');
        $mform->addRule('contacttoselect', get_string('contactto_mis', 'local_contactform'), 'required', null, 'client');
        $values = array(get_string('improvement', 'local_contactform') => get_string('improvement', 'local_contactform'),
            get_string('technical', 'local_contactform') => get_string('technical', 'local_contactform'),
            get_string('usercreate', 'local_contactform') => get_string('usercreate', 'local_contactform'),
<<<<<<< HEAD
=======
            get_string('userdelete', 'local_contactform') => get_string('userdelete', 'local_contactform'),
            get_string('coursedelete', 'local_contactform') => get_string('coursedelete', 'local_contactform'),
>>>>>>> hsh/develop
            get_string('loginprob', 'local_contactform') => get_string('loginprob', 'local_contactform'),
            get_string('dataloss', 'local_contactform') => get_string('dataloss', 'local_contactform'));
        sort($values);
        array_unshift($values, get_string('pleasechoose', 'local_contactform'));
        $values[get_string('other', 'local_contactform')] = get_string('other', 'local_contactform');
        $mform->addElement('select', 'subject', get_string('subject', 'local_contactform'), $values, ' style="width: 20em;"');

        $mform->addElement('textarea', 'message', get_string('message', 'local_contactform'), 'style="width: 20em;height: 20em"');
        $mform->addRule('message', get_string('message_mis', 'local_contactform'), 'required', null, 'client');

        if (isloggedin()) {
            $mform->setDefault('firstname', $USER->firstname);
            $mform->setDefault('lastname', $USER->lastname);
            $mform->setDefault('email', $USER->email);
            $mform->setDefault('telephone', $USER->phone1);
        }
        if (!empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey)) {
            $mform->addElement('recaptcha', 'recaptcha_element', 'style="margin: 10em"');

        }


        $this->add_action_buttons(true, get_string('submit', 'local_contactform'));
    }

    function validation($data, $files) {
        if(empty($data["lastname"])){
            $errors['lastname'] = get_string('missinglastname');
        }
        if(empty($data["firstname"])){
            $errors['firstname'] = get_string('missingfirstname');            
        }  
        if(empty($data["message"])){
            $errors['message'] = get_string('missingdescription');           
        }
        if (! validate_email($data['email'])) {
            $errors['email'] = get_string('invalidemail');
        }
        $recaptcha_element = $this->_form->getElement('recaptcha_element');
        if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) {
            $challenge_field = $this->_form->_submitValues['recaptcha_challenge_field'];
            $response_field = $this->_form->_submitValues['recaptcha_response_field'];
            if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
                $errors['recaptcha'] = $result;
            }
        } else {
            $errors['recaptcha'] = get_string('missingrecaptchachallengefield');
        }
        return $errors;
    }

}