Seeking proper method to create a stand-alone page in Moodle

Seeking proper method to create a stand-alone page in Moodle

by Perry Way -
Number of replies: 9

Hello everyone,

I'm using Moodle 3.8.1+

I am looking for the right way to create a stand-alone page that is in Moodle but not associated with any course, module, report or admin/local plugin.

We have a folder (/pages) with .php pages in them that are utilizing a method that used to work nicely without any debug messages but now it shows a debug message of the following:

Coding problem: $PAGE->context was not set. You may have forgotten to call require_login() or $PAGE->set_context(). The page may not display correctly as a result
line 503 of \lib\pagelib.php: call to debugging()
line 830 of \lib\pagelib.php: call to moodle_page->magic_get_context()
line 1456 of \lib\weblib.php: call to moodle_page->__get()
line 1233 of \lib\pagelib.php: call to format_string()
line 11 of \pages\libraryresources.php: call to moodle_page->set_title()

I found some old references to this being thrown by improper plugins, but nothing reported in the past was related to my specific case, of a stand-alone page.

I've tried setting $PAGE->set_context() passing the instance of the application (front page is course id 1). 

The advice provided in the past for this debug message (which is related to an errant plugin) said to comment out lines 501-503 in /lib/pagelib.php to make the debug message go away but I'm actually looking for a real cure to this. I don't quite understand yet the reason why this is being triggered since this page code used to work just fine even when debug messages were being shown. I'm talking about previous to Moodle version 3.6.x

Here's an example of my one of my stand-alone page code for the top portion of a page. Can anyone tell me if I'm doing something I should not be doing or if this scenario of a stand-alone page is not able to be supported by Moodle? Thanks so much!

Code of /pages/libraryresources.php:

<?php
require_once("../config.php");
global $CFG, $SITE, $PAGE, $OUTPUT, $DB;
require_login();
$PAGE->set_pagetype('site-index');
$PAGE->set_pagelayout('incourse');
$PAGE->set_title("Library Resources");
$PAGE->set_heading($SITE->fullname);
$PAGE->set_url("/");
$context = context_system::instance();
$PAGE->set_context($context);
echo $OUTPUT->header();
echo $OUTPUT->heading("Library Resources");
echo $OUTPUT->box_start('maintenance_message generalbox boxwidthwide boxaligncenter');
?>

after this code, custom html is provided, and then the footer code (below) is called:

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

Average of ratings: -
In reply to Perry Way

Re: Seeking proper method to create a stand-alone page in Moodle

by Visvanath Ratnaweera -
Picture of Particularly helpful Moodlers Picture of Translators
In reply to Visvanath Ratnaweera

Re: Seeking proper method to create a stand-alone page in Moodle

by Perry Way -
Hello Visvanath,

I tried this plugin years ago and found it to be unable to achieve what I could do very simply with a single .php file. We are not using Apache web server. This plugin will not make nice URL's if you can't use Apache. Also I think I found that it mangled some HTML in the past, if my memory is correct.

Also, I have other things besides .html pages that are outside the realm of Moodle specifically, so it makes sense to just keep everything segmented from Moodle. Oh yeah and one more important thing is these pages I am putting in this /pages path, half of them require the user to log in, and this plugin does not have that feature available since it's a global setting.
In reply to Perry Way

Re: Seeking proper method to create a stand-alone page in Moodle

by Visvanath Ratnaweera -
Picture of Particularly helpful Moodlers Picture of Translators
Hi Perry

It could well be that I don't understand your requirement. Web design is not my dept. Just reading your post this plug-in came to my mind. If you have looked at it, and it is not the right thing for you, then just forget my post.
In reply to Visvanath Ratnaweera

Re: Seeking proper method to create a stand-alone page in Moodle

by Perry Way -

Hello again Visvanath, 

I responded to your post for others who might read this thread more than for you. I hope you didn't think I was complaining. I find your input quite valuable on many subjects.

Part of my requirements provided to me is that these pages need to have nice descriptive URL's without query parameters. 

After a nice active weekend I believe I know what I need to do now. I need to create a module that provides a basic wrapper for what Moodle expects in order to integrate fully. Then I can put whatever standalone pages I want in the module root path and have a nice readable URL.

In reply to Perry Way

Re: Seeking proper method to create a stand-alone page in Moodle

by Richard Jones -
Picture of Plugin developers Picture of Testers

Just in case someone is looking for a simple solution, you can get away with this code for a simple page:

<?php
require_once("../config.php");
global $CFG, $SITE, $PAGE, $OUTPUT, $DB;

$PAGE->set_pagetype('site-index');
$PAGE->set_pagelayout('course');
$context = context_system::instance();
$PAGE->set_context($context);
$PAGE->set_url('/pages/sample.php');

require_login();

echo $OUTPUT->header();
echo $OUTPUT->heading('Library Resources', 1);
echo $OUTPUT->box_start('maintenance_message generalbox boxwidthwide boxaligncenter');
?>

<h3>Custom page</h3>
<p>Blah, blah blah</p>

<?php
echo $OUTPUT->box_end();
echo $OUTPUT->footer();
Seems to work and re-direct to login page where necessary.  However, with no permissions checks it is available to guests too.

Seems like an interesting use case, let us know what else you find out.

Average of ratings: Useful (2)
In reply to Richard Jones

Re: Seeking proper method to create a stand-alone page in Moodle

by Perry Way -
I found a cure yesterday afternoon.

I made a local plugin with close to bare-bones functionality and then put my pages in the root folder and now those pages automagically have reference to the plugin system's ability to transfer the context over to $PAGE. At least that's what I think is going on, as I did not follow the rabbit hole forever until the end. But now when pagelib.php's function magic_get_context() is called, $this->_context is assigned whereas the same page outside the folder of the plugin does not transfer the $PAGE->context value. This is probably due to inherited class functions.

You can do this easily yourself, designing in features if you want them, by following the directions in /local/readme.txt. In my case, I wanted absolutely no features whatsoever. I just want stand alone pages that I can make that have the header and footer of Moodle to them allowing me unlimited ability to create custom content within the body of Moodle.

If others would find a bare bones plugin allowing them to do the same thing I will upload it to the Plugins directory, but honestly this is very simple and easy to do and I have confidence that most people could actually pull this off with someone at the lead showing the way.

By the way, you can do whatever you like in these pages with respect to role level access. I do that in many settings in other modules I've created, like one I created for Adobe Connect, which integrates our courses one to one with a meeting in Adobe Connect. Students have one kind of experience and Teachers have another and Admin staff have yet another. Here is a brief example of a system wide role I created in our Moodle to give a limited level of access to staff who aren't instructors. You can easily build upon this to customize for your own environment.

function UserIsReportReader()
{
    global $DB;
    global $USER;
    $Role = $DB->get_record('role', array('shortname' => 'report_reader'));
    $Context = context_system::instance();
    $ReportReaders = get_role_users($Role->id, $Context);
    if (array_key_exists($USER->id, $ReportReaders))
    {
        return true;
    }
    return false;
}


Average of ratings: Useful (2)
In reply to Perry Way

Re: Seeking proper method to create a stand-alone page in Moodle

by Richard Jones -
Picture of Plugin developers Picture of Testers
Thanks Perry

It did occur to me later that a simple plugin type (eg report) could be adapted to this purpose and therefore include all the Moodle functions you would require.

Best
Richard
In reply to Perry Way

Re: Seeking proper method to create a stand-alone page in Moodle

by Visvanath Ratnaweera -
Picture of Particularly helpful Moodlers Picture of Translators
Hi Perry

No offense! It is the other way, I noticed some neat tricks in the continuing discussion with Richard.
wink
In reply to Perry Way

Re: Seeking proper method to create a stand-alone page in Moodle

by Perry Way -

I decided to make my plugin available to others, in case someone want's a ready-made solution for information pages within Moodle.

Despite authoring numerous plugins, I have never uploaded a plugin here to Moodle.org before and poked around to see if I could tell what that process entails and I'm a bit lost, so if anyone reading this thread is interested in having this feature available and just needs a little working example to get started with, here's a zip file.

Unzip this file and put the contents in the /local folder and then log in to your admin account and then Moodle will take you through the installation process.

At the end you will be able to see the sample index.php content by going to the url of <your site url>/local/info


Average of ratings: Useful (2)