<?php
require_once("$CFG->libdir/formslib.php");
require_once("$CFG->dirroot/local/treestudyplan/lib.php");

class reportinvite_form extends moodleform {
    //Add elements to form
	const GOALS_EDITOR_OPTIONS = array('trusttext'=>true, 'subdirs'=>true, 'maxfiles'=>0,'maxbytes'=>5*1024*1025);

    public function definition() {
        global $CFG;
 
	    // 'code', 'revision', 'description', 'goals', 'complexity', 'points', 'studyhours'
        $mform = $this->_form; // Don't forget the underscore! 
		
        $mform->addElement('hidden', 'add', 0);
        $mform->setType('add', PARAM_ALPHANUM);

        $mform->addElement('hidden', 'update', 0);
        $mform->setType('update', PARAM_INT);

//		$mform->addElement('static', 'desc_new', get_string('invite_desc_new','local_treestudyplan')); // Add elements to your form
//		$mform->addElement('static', 'desc_edit', get_string('invite_desc_edit','local_treestudyplan')); // Add elements to your form

        $mform->addElement('text', 'name', get_string('invite_name','local_treestudyplan'), array('size' => 50)); // Add elements to your form
        $mform->setType('name', PARAM_NOTAGS);                   //Set type of element
        $mform->setDefault('name', '');        //Default value
		$mform->addRule('name', get_string('required'), 'required', null, 'client');

        $mform->addElement('text', 'email', get_string('invite_email','local_treestudyplan'), array('size' => 20)); // Add elements to your form
        $mform->setType('email', PARAM_NOTAGS);                   //Set type of element
        $mform->setDefault('email', '');        //Default value
		$mform->addRule('email', get_string('required'), 'required', null, 'client');
		$mform->addRule('email', get_string('email'), 'email', null, 'client');

        $mform->addElement('static', get_string('invite_email','local_treestudyplan') ); // Add elements to your form

		$this->add_action_buttons();
    }
    //Custom validation should be added here
    function validation($data, $files) {
        return array();
    }

    function set_data($data) {

        parent::set_data($data);
    }

	function get_data()
	{
		global $DB,$USER;

		$data = parent::get_data();
		if($data != NULL)
		{
			if(empty($data->user_id))
			{
				$data->user_id = $USER->id;
			}

			if(empty($data->update))
			{
				$date = new DateTime("now", core_date::get_user_timezone_object());
				$date->setTime(0, 0, 0);	
				
				$data->idate = $date->getTimeStamp();
			}

			if(empty($data->update))
			{
				//create a new random key for the invite
				do {
					$length = 20;
					$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
					$charactersLength = strlen($characters);
					$randomkey = '';
					for ($i = 0; $i < $length; $i++) {
						$randomkey .= $characters[rand(0, $charactersLength - 1)];
					}

					// Double check that the key is unique before inserting
				} while($DB->record_exists_select("local_treestudyplan_invit", $DB->sql_compare_text("invitekey"). " = " . $DB->sql_compare_text(":invitekey"), ['invitekey' => $randomkey]));

				$data->invitekey = $randomkey;

			}
		}
		return $data;
	}

}