Skip to content

Instantly share code, notes, and snippets.

@yireo
Last active December 29, 2015 12:59
Show Gist options
  • Save yireo/7674072 to your computer and use it in GitHub Desktop.
Save yireo/7674072 to your computer and use it in GitHub Desktop.
Sample code to allow a Moodle theme to detect IFRAMES, for instance when wrapping the Moodle theme into Joomla! using Joomdle.

Detect IFRAME in Moodle theme

This gist contains code-suggestions to allow for a Moodle-theme to detect IFRAMES, and accordingly use a PHP-flag to hide blocks within the theme. Using that PHP-flag, allows you to use the same theme for Moodle-administration (by accessing the Moodle application directly), as well as for wrapping things within Joomla!.

The technique involves setting a cookie from both the Moodle-theme (using JavaScript) as well as the Joomdle component (using PHP). The cookie will then be used within the PHP-code of the Moodle-theme to hide columns - how to do that part depends entirely on the code of your specific Moodle theme. Note that the cookie is initialized on purpose with a lifetime of 0 (browser-session related) and a path of / (assuming both Joomla! as Moodle are installed in the same domain).

Place this PHP-code within the components/com_joomdle/joomdle.php file just after the defined()-check

setcookie('joomdle_iframe', 1, 0, '/');

Place this JavaScript-code within the layout/general.php file of your Moodle theme, somewhere in the bottom:

<script>
var iframe = false;
if(window.parent.frames.length > 0) { iframe = true; }
if(top !== self) { iframe = true; }
if(iframe) {
    document.cookie = "joomdle_iframe=1; path=/;";
} else {
    document.cookie = "joomdle_iframe=0; path=/;";
}
</script>

Next, within the PHP-code of the same theme-file layout/general.php you can set a simple PHP-flag like this:

$wrapper = (isset($_COOKIE['joomdle_iframe']) && (int)$_COOKIE['joomdle_iframe'] === 0) ? false : true;

When switching between the Moodle administration-pages and the Joomla! pages, the cookie will only be changed after a refresh. Probably it is best to work with different browsers because of this. It is not perfect, but hey, it works.

@innoads
Copy link

innoads commented Jul 22, 2014

Thanks for sharing. I am new to php, so I have a question. When the "Clean" theme of moodle is used there is no layout/general.php. There are embedded.php, maintenance.php, secure.php, columns1.php, columns2.php and columns3.php. Which one should be used?
Would you give an example with dummy html on how the PHP-flag must be used?
I would really appreciate a reply! Thanks, Andy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment