Converting Site policy URL for guests into a Contact Form

Converting Site policy URL for guests into a Contact Form

by Alex Legg -
Number of replies: 0
I've stripped out the code that embeds the iFrame content, and replaced it with a contact form. 

Cancelling the form redirects the user to the home page, as did the site policy so great.

My only challenge now submitting the form, and at the same time emulating that the user has accepted the Site Policy. 

I haven't checked if the input submits to the Database yet, it's beyond what I know how to do at this stage. In the original site policy code, it accepts "agree" arrays, so I included this into the "Submit" and "Cancel". 


This is my form:

require_once($CFG->libdir.'/formslib.php');


class guest_access extends moodleform {

    public function definition() {

        global $CFG;


        $mform = $this->_form;


        $mform->addElement('text', 'guestName', get_string('guestName', 'moodle'));

        $mform->setType('guestName', PARAM_NOTAGS);

        $mform->addRule('guestName', get_string('Please enter your name'), 'required', null, 'server');


        $mform->addElement('text', 'guestEmail', get_string('guestEmail', 'moodle'));

        $mform->setType('guestEmail', PARAM_NOTAGS);

        $mform->addRule('guestEmail', get_string('Please enter your email'), 'required', null, 'server');


        $mform->addElement('text', 'guestTelephone', get_string('guestTelephone', 'moodle'));

        $mform->setType('guestTelephone', PARAM_NOTAGS);

        $mform->addRule('guestTelephone', get_string('Please enter your telephone number'), 'required', null, 'server');


        $mform->addElement('text', 'guestOrganisation', get_string('guestOrganisation', 'moodle'));

        $mform->setType('guestOrganisation', PARAM_NOTAGS);

        $mform->addRule('guestOrganisation', get_string('Please enter your organisation'), 'required', null, 'server');


        $mform->addElement('text', 'guestJobRole', get_string('guestJobRole', 'moodle'));

        $mform->setType('guestJobRole', PARAM_NOTAGS);

        $mform->addRule('guestJobRole', get_string('Please enter your job role'), 'required', null, 'server');


        $mform->addElement('text', 'guestNoOfStaff', get_string('guestNoOfStaff', 'moodle'));

        $mform->setType('guestNoOfStaff', PARAM_NOTAGS);

        $mform->addRule('guestNoOfStaff', get_string('Please enter the number of staff in your organisation'), 'required', null, 'server');


        $mform->addElement('textarea', 'guestNotes', get_string('guestNotes', 'moodle'), 'wrap="virtual" rows="20" cols="50"');

        $mform->setType('guestNotes', PARAM_NOTAGS);


        $buttonarray=array();

        $buttonarray[] = &$mform->createElement('submit', 'guestformyes', get_string('submit'), array('agree' => 1));

        $buttonarray[] = &$mform->createElement('cancel', 'guestformno', get_string('cancel'), array('agree' => 0));

        $mform->addGroup($buttonarray, 'buttonar', '', array('agree' => $agree), false);

    }


    //Custom validation should be added here

    function validation($data, $files) {

        return array();

    }

}


At the end of the following code at moodle/user/policy.php, the comments show the original code. I added the "$mform = new guest_access();" section. I managed to get the redirection working without the need for "agree".

<?php


require_once('../config.php');

require_once('../guest_access.php');

require_once($CFG->libdir.'/filelib.php');

require_once($CFG->libdir.'/resourcelib.php');


$agree = optional_param('agree', 0, PARAM_BOOL);


$PAGE->set_url('/user/policy.php');

$PAGE->set_popup_notification_allowed(false);


if (!isloggedin()) {

    require_login();

}


if (isguestuser()) {

    $sitepolicy = $CFG->sitepolicyguest;

} else {

    $sitepolicy = $CFG->sitepolicy;

}


if (!empty($SESSION->wantsurl)) {

    $return = $SESSION->wantsurl;

} else {

    $return = $CFG->wwwroot.'/';

}


if (empty($sitepolicy)) {

    // Nothing to agree to, sorry, hopefully we will not get to infinite loop.

    redirect($return);

}


if ($agree and confirm_sesskey()) {    // User has agreed.

    if (!isguestuser()) {              // Don't remember guests.

        $DB->set_field('user', 'policyagreed', 1, array('id' => $USER->id));

    }

    $USER->policyagreed = 1;

    unset($SESSION->wantsurl);

    redirect($return);

}


$strpolicyagree = get_string('policyagree');

$strpolicyagreement = get_string('policyagreement');

$strpolicyagreementclick = get_string('policyagreementclick');


$PAGE->set_context(context_system::instance());

$PAGE->set_title($strpolicyagreement);

$PAGE->set_heading($SITE->fullname);

$PAGE->navbar->add($strpolicyagreement);


echo $OUTPUT->header();

echo $OUTPUT->heading($strpolicyagreement);


//$mimetype = mimeinfo('type', $sitepolicy);

//if ($mimetype == 'document/unknown') {

//    // Fallback for missing index.php, index.html.

//    $mimetype = 'text/html';

//}


$mform = new guest_access();

if ($mform->is_cancelled()) {

    redirect($CFG->wwwroot.'/');

} else if ($fromform = $mform->get_data()) {


} else {

    $mform->set_data($toform);

    $mform->display();

}


//We can not use our popups here, because the url may be arbitrary, see MDL-9823.

//$clicktoopen = '<a href="'.$sitepolicy.'" onclick="this.target=\'_blank\'">'.$strpolicyagreementclick.'</a>';


//echo '<div class="noticebox">';

//echo resourcelib_embed_general($sitepolicy, $strpolicyagreement, $clicktoopen, $mimetype);

//echo '</div>';


//$formcontinue = new single_button(new moodle_url('policy.php', array('agree' => 1)), get_string('yes'));

//$formcancel = new single_button(new moodle_url($CFG->wwwroot.'/login/logout.php', array('agree' => 0)), get_string('no'));

//echo $OUTPUT->confirm($strpolicyagree, $formcontinue, $formcancel);


echo $OUTPUT->footer();



I hope this all makes sense. I've hit a dead end, and so any pointers will be most welcome... Thanks!

Average of ratings: -