Using PHP In PLace of Page Content

Using PHP In PLace of Page Content

by PLAYlive Admin -
Number of replies: 3
Hello Moodle community! I've been using Moodle for the past 2 years now and I love it! I'm using it to provide training to all my stores.

There's one feature however that I continually find myself longing for and that is the ability to use PHP based content in my course pages instead of HTML which is what I've been doing thusfar. So far searching Google and these forums haven't yielded results. In a perfect world I'd create a page in a course and somehow tell Moodle where it can find my PHP script that will output all the content that's seen on the page itself. Even if the "page" seen is just a link to a PHP file somewhere on my server, that's fine as long as I can load the header/footer/sidebar and all that Moodle goodness.

Reason I want to do this: I want to use variables over and over and be able to change content in many areas without needing to edit each page or doing a SQL replace script on my content in the database or anything like that. I also want to use some fancier features that only server-side scripting can provide such as cURL requests and such. I understand there's a feature for custom content blocks but what I'm after seems to be a bit different. Honestly even allowing PHP in the HTML editor would be sufficient.

P.S. Feel free to use big words in responses! I'm a web designer + server admin of many years who's comfortable with PHP/ASP.net/MySQL so feel free to get all technical if you have to. smile


Thanks guys. Looking forward to your reply.

Average of ratings: -
In reply to PLAYlive Admin

Re: Using PHP In PLace of Page Content

by Colin Fraser -
Picture of Documentation writers Picture of Testers

OK, seems like you are trying to create dynamic content. You might find it more helpful to have this question moved to the General Developers forum. 

I am pretty sure this has been done before, at least I recall it being discussed - but I wasn't interested at the time, so didn't pay any attention. With HTML 5 now widely implemented in browsers, it is probably time to revisit this.  

In reply to PLAYlive Admin

Re: Using PHP In PLace of Page Content

by Justin Hunt -
Picture of Particularly helpful Moodlers Picture of Plugin developers

The plugin Generico filter will help with some of what you are trying to do, ie reuse content across the site.

https://moodle.org/plugins/view.php?plugin=filter_generico

If you want to use custom php, I think you will need to :

a) write a plugin
Or
b) use an iframe.

I am not aware of anything that lets you write php in the html editor, and then runs it on demand. 


In reply to PLAYlive Admin

Re: Using PHP In PLace of Page Content

by PLAYlive Admin -

Thanks for the replies. I didn't check back in this thread early enough before continuing on with my own implementation. It's messy but it works. Here's what I did:

1. Open /moodle/mod/page/view.php
2. Somewhere early in the page add this function:

function safe_string_sanitize($str) {
    $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $str);
    $clean = strtolower(trim($clean, '-'));
    $clean = preg_replace("/[\/_|+ -]+/", '-', $clean);

    return $clean;
}

3. Find the line "echo $OUTPUT->box($content, "generalbox center clearfix");"

4. Replace with:

$dyn_content = $CFG->dirroot . '/dynamic/' . safe_string_sanitize($course->shortname) . '/' . safe_string_sanitize($page->name) . '.php';
if (file_exists($dyn_content)) {
    include($dyn_content);
} else {
    echo $OUTPUT->box($content, "generalbox center clearfix");
}

5. Place the content for your page in "/moodle/dynamic/{course-shortname}/{course-name}.php"

This is just my quick hackish method. Maybe in the future I'll take the time to write a legitimate plugin. I'd love to be able to drop PHP files in course folders and have the site list them as pages but that's for another day.