MForm help: using form to emulate a message with "understand" button

MForm help: using form to emulate a message with "understand" button

by Jock Coats -
Number of replies: 1

As I think I mentioned the other day I'm trying to create a "message" that requires a confirmation from the user and on confirmation loges them out and sends them back to site home logged out.

Basically for people our SRS tells us have delinquent accounts of some kind (which we are storing as hidden user profile fields in Moodle) blocking them from Moodle with an explanation is our "last chance" to get them to correct whatever the problem is with their enrolment.

An _after_require_login callback function in my lib.php checks the profile field and if found sends it to the main routine which loads the following as a form where I want to react to the "I understand" button by logging them out of Moodle and sending them to the site home page.

It *very* nearly works - but its one quirk is that it requires the "form" to be loaded twice before it will actually to the logout. I'm sure it has something to do with the order in which I am loading instantiating and then reacting to the "understand" button but have tried moving these all over the place in all three files to no avail (often, indeed, making it worse). Can anyone help me fathom out what I'm doing wrong. I kind of feel drawn toward "validation" somehow, but since there's nothing to actually fill in in the form I didn't think I would need any. So am stuck...

Moodle screen displaying output of a "fake" form for getting user to acknowledge understanding why they are blocked.

The code is here on Github (though we develop using more private BitBucket areas I commit to github when ready to release to our hosting company etc so it's up to date at the moment when the problem is occurring).

Cheers,

Jock

Average of ratings: -
In reply to Jock Coats

Re: MForm help: using form to emulate a message with "understand" button

by Jock Coats -
We've fixed it...a bit of jiggery pokery switching the order in which things happen in the main banner_holds.php script:

 // require_once($CFG->dirroot. '/local/banner_messages/lib.php');
 require_once($CFG->dirroot. '/local/banner_messages/message_form.php');

-
-$context = context_system::instance();
-$PAGE->set_context($context);
-
if ($USER->id == 0) {
redirect(new moodle_url('/'));
}

+$PAGE->set_context(context_system::instance());
$PAGE->set_url(new moodle_url('/local/banner_messages/banner_holds.php'));
+$messageform = new local_banner_messages_message_form();

-$PAGE->set_pagelayout('base');
+if($data = $messageform->get_data()) {
+ if($data->understand == 'yes') {
+ $GLOBALS['banner_holds'] = 0;
+ require_logout();
+ redirect(new moodle_url('/'));
+ }
+}

+$PAGE->set_pagelayout('base');
$PAGE->set_title($SITE->fullname);
$PAGE->set_heading(get_string('bannerholdsheader', 'local_banner_messages'));

-$messageform = new local_banner_messages_message_form();

echo $OUTPUT->header();
-
$messageform->display();
-
-$data = $messageform->get_data();
-
-if ($data->understand == 'yes') {
- $GLOBALS['banner_holds'] = 0;
-// \core\session\manager::kill_user_sessions($USER->id);
- require_logout();
-// $home = new moodle_url('/');
-}
-
echo $OUTPUT->footer();