Monday, September 05, 2022

Moodle page and requiring login

We can create a new custom page in a Moodle site by following the template below,

<?php
require_once(dirname(__FILE__) . '/../config.php');
$PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
$PAGE->set_url('/ourfolder/ourfile.php');
echo $OUTPUT->header();
?>

<p>Our content</p>

<?php
echo $OUTPUT->footer();
?>

Another page example is at

https://moodledev.io/docs/apis/subsystems/output

Then for requiring login, we can add require_login() and then use the conditional  isguestuser() for allowing or not allowing guest users.

https://docs.moodle.org/dev/Access_API

https://moodledev.io/docs/apis/subsystems/access

<?php
require_once(dirname(__FILE__) . '/../config.php');
$PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
$PAGE->set_url('/ourfolder/ourfile.php');
$PAGE->set_title('Our page title');
$PAGE->set_pagelayout('standard');
require_login();
echo $OUTPUT->header();
if (isguestuser()) {
echo "<h2>Please log in to see this page!</h2>";
}
else {
?>
<iframe
src="https://our.iframe.source.pg/"
name="search_result_frame" width="100%" height="800px">
</iframe>
<?php
}
echo $OUTPUT->footer();
?>

Then we can add this newly created page to our Moodle navigation using various methods. One way would be to add it to the custom menu settings box,
https://oursite.org/admin/settings.php?section=themesettings

and then also adding it to the custom menu items on the mobile app,
https://oursite.org/admin/settings.php?section=mobilefeatures

Edit: Our page worked only in the "open in browser" mode because it had javascript and a form inside an iframe, and we had not included this page in the app code. Simple pages would probably work in the in-app browser mode also.

No comments:

Post a Comment