ElearningWorld.org

For the online learning world

Elearning WorldMoodleTechnical

The first step – part one

Introduction

So you want to modify your Moodle installation? You’ve learnt a little about HTML (www.w3schools.com/html/html_intro.asp) and CSS (www.w3schools.com/Css), but you’re not really a programmer. As Moodle’s dynamic element is orchestrated by PHP (www.php.net) which is a scripting language (similar to a programming one). How can you make that leap?

In the first of a series of posts I’ll walk you through getting on that first step so that you have the confidence to access all of the other material that is out there but at this time completely confusing to you. Then you’ll be in a position to have the skill to learn how to modify and create Moodle code with more confidence.

I must stress that we won’t be using Moodle for the actual code, that’s far more complex!

Disclaimers

Firefox® is a registered trademark of the Mozilla Foundation – www.mozilla.org/en-US/foundation/trademarks/policy.

Ubuntu® is a registered trademark of Canonical Ltd – www.ubuntu.com/legal/terms-and-policies/intellectual-property-policy

“Raspberry Pi” is a trademark of the Raspberry Pi Foundation – www.raspberrypi.org/trademark-rules.

Apache and Apache HTTP Server are trademarks of The Apache Software Foundation – www.apache.org.

Moodle™ is a registered trademark of ‘Martin Dougiamas’ – moodle.com/trademarks.

VirtualBox® and Oracle® are registered trademarks – www.oracle.com/legal/trademarks.html and www.virtualbox.org/wiki/Licensing_FAQ.

Other names / logos can be trademarks of their respective owners. Please review their website for details.

I am independent from the organisations listed above and am in no way writing for or endorsed by them.

All example ‘code’ presented is written by myself, please feel free to copy for educational purposes only.

What we will cover

  1. Setup with web-server setup and test.
  2. Output.
  3. Variables.
  4. Functions.
  5. Scope and globals.
  6. Classes and methods.
  7. Class inheritance and overloading.

In this first post, I’ll cover 1 and 2.

Client/Server architecture

To understand what we will be doing, we first need to understand what is actually going on when we visit a web page.

Moodle is a ‘server side’ application, this means that it runs on a special type of machine called a ‘server’ that is typically setup to handle many ‘clients’. The client is you using your web browser. This allows many people to use the same Moodle application at once. PHP is a ‘server’ side language that has the purpose of generating dynamic content depending on inputs given to it. The inputs can be in the form of a user login and associated data sent by the browser and / or data stored in a database. Moodle uses a database to remember things for each user.

So, if we look at the following drawing:

Note: Adapted from my iMoot 2014 presentation on ‘My Own Moodle’ – www.slideshare.net/gb2048/my-own-moodle.

Then imagine that you’re sitting on a desert island with your laptop running a web browser, you are the client. You enter in a URL for a web page. Through a sequence of events (not described here) the server’s address is found on the network (this can be local, the internet etc.). The browser sends a ‘request’ to the server asking for the contents of the requested web page. It does this using the Hypertext Transport Protocol (HTTP) or Hypertext Transport Protocol Secure (HTTPS). The request arrives at the server having travelled down a wire or WiFi, down and back up the TCP/IP protocol stack at the ‘port’ the ‘web-server’ is listening on (an application that serves web pages and is waiting for requests). The web-server the processes the request and if the page contains PHP, asks the PHP application that’s running to process the script passed to it, then the result is sent back to the client ‘you’ as a ‘response’. Thus you are presented with a web page.

So Moodle itself is really just (not 100%) PHP code that the web-server with the assistance of the PHP application processes to create the web pages you see on your browser.

The server itself does not have to be remote though. It could simply be the web-server application running on your own machine. The underlying operating system and networking infrastructure will work out how to handle the location given the URL you use in the browser.

This is a simplified example, for more information, please see: en.wikipedia.org/wiki/Client%E2%80%93server_model.

Setup

There are lots of ways we can do this. Here are a few you can use:

  1. If you have an existing ‘test’ and I must stress ‘test’ Moodle installation, then create a sub-folder called ‘mycode’ and then you’ll be able to access the files within using the URL you use to access that Moodle site in the browser followed by ‘/mycode’. You can get complete Moodle packages from: download.moodle.org/windows and download.moodle.org/macosx.
  2. Your own AMP (en.wikipedia.org/wiki/List_of_Apache%E2%80%93MySQL%E2%80%93PHP_packages) stack server setup. Either a pre-built package or all three manually installed. Again, create a folder called ‘mycode’ in the ‘root’ web server folder.
  3. You could install a version of Ubuntu (Desktop or Server will be ok – ubuntu.com/download) and run it on VirtualBox (www.virtualbox.org/wiki/Downloads), if you have a processor that supports virtualisation and that’s enabled in the BIOS.
  4. A Raspberry Pi running Raspbian (www.raspbian.org).

The key thing here is that we must have a web server that can process and serve PHP files, and that if you break it then it does not matter.

I will be using number four. And if you use numbers three and four then you can to install Apache and PHP with the following instructions. For the first then that’s already happened and the second depends on which one you choose and its specific instructions.

Web-server setup

Assuming that you’ve logged into to your Raspberry Pi (or Ubuntu virtual machine), type the following (see ‘Reference’ at the end) on a bash shell (one by one):

sudo apt update
sudo apt install apache2 -y
sudo apt install php libapache2-mod-php -y

These commands will install the Apache web-server and PHP application, along with telling Apache about PHP.

Because the web-server is running on port 80 then it needs on a Linux machine to have permission to do this (not for port 8080, the alternative HTTP port). The ‘apache2’ service is running under the user and group of ‘www-data’, but for some reason the default directory is owned by the user ‘root’ in the group of ‘root’. So what we need to do is have a place where we can write to without using ‘sudo’ all the time and yet ‘apache2’ can still server the files. Therefore, type the following commands:

sudo mkdir /var/www/html/mycode
sudo usermod -a -G www-data pi
sudo chown www-data:www-data /var/www/html/mycode
sudo chmod ug=+rwx /var/www/html/mycode

They will create a ‘mycode’ folder for us to use, add our user ‘pi’ to the ‘www-data’ group, make ‘www-data’ user and group the owner of our folder and then finally allow anybody in the ‘www-data’ group the permission to write to our folder.

If we now type ‘id’ then we will find that the ‘www-data’ group is not listed as one of our groups. So we need to log out and log in again.

When we create files in the ‘/var/www/html/mycode’ folder then they will belong to the user and group of ‘pi’, however because their ‘others’ permission will be ‘read’ then ‘apache’ will still be able to read what we make.

Web-server test

For all ways, to test that everything is running as it should, change to the ‘mycode’ folder we have created (type ‘cd /var/www/html/mycode’). Then type ‘nano phpinfo.php’ and enter in the editor:

<?php phpinfo(); ?>

So that we have:

Save it with Ctrl-W and quit with Ctrl-X.

We will then be able to test that the server is working by navigating to the web page:

Here my Raspberry Pi is called ‘matilda’ which I’ve configured (in the ‘/etc/hosts’ file) to be associated with its IP address (so I can do screen shots without giving away the IP). You can use the IP address instead, like ‘http://192.168.0.2/mycode/phpinfo.php’. If you don’t know the IP address of the Pi, type ‘ifconfig’ and look for the ‘wlan0’ or ‘eth0’ entry depending on how your Pi is connected to your network.

To explain, when the ‘phpinfo.php’ file is processed by the web-server before being sent to the client it sees ‘<?php’ it gets the PHP application to process what is between that and the closing ‘?>’. PHP will do this and potentially produce an output. In this case, the ‘phpinfo()’ function is a called with no parameters (www.php.net/manual/en/function.phpinfo.php).

The function ‘phpinfo()’ should really only be used for testing as it will give out sensitive information. In Moodle you can see this if you’re an administrator and visit ‘Site administration → Server → PHP Info’.

All being well, you should have something similar.

What happens if it does not work?

If something goes wrong, then you need to look in the error log file. On the Pi, this will be located in the ‘/var/log/apache2’ folder as ‘error.log’ and you can use the command ‘tail’ to see the most recent events as the file is appended to. In other systems, hunt around for ‘error.log’.

Output

The whole point of PHP is to generate HTML output to be served to the client (you the user). This is achieved through interleaving of the static (HTML) and the dynamic (PHP) in a file. The ratio of dynamic to static depends on the problem to be solved and the style of the developer.

So, using ‘nano’ as we did before, create a file in the ‘mycode’ folder called ‘output.php’:

<!doctype html>
<html>
    <head>
        <title>Example one</title>
    </head>
    <body>
        <?php
        echo "<h1>Hello</h1>"; 
        ?>

    </body>
</html>

there is only one small bit of PHP and that outputs using the ‘echo’ command the heading ‘Hello’. Ok, its not very dynamic, but it does illustrate PHP being used. Again use a URL like ‘http://192.168.0.2/mycode/output.php’ in a web browser to see the result.

So lets make things dynamic by asking the server for the time with the file ‘time.php’:

<!doctype html>
<html>
    <head>
        <title>Time</title>
    </head>
    <body>
<?php
    echo '<h1>Time is: '.gmdate('G\h i\m \a\n\d s\s', time()).'.</h1>';
?>
    </body>
</html>

Again use the web browser to see the result.

More reading

To learn more about PHP, you could choose to visit:

Conclusion

In this post we have:

  1. Learnt that Moodle uses PHP to create some of its dynamic content.
  2. Discovered the client / server architecture and how it relates to Moodle.
  3. Understood how to create a setup for us to learn PHP on.
  4. Tested out web-server is using PHP.
  5. Learnt about output of web-pages in PHP.

I hope this gets you started with learning PHP, next time we will continue on our journey.

Reference

Setting up an Apache Web Server on a Raspberry Pi – www.raspberrypi.org/documentation/remote-access/web-server/apache.md – accessed 28th January 2020.

Gareth Barnard
Latest posts by Gareth Barnard (see all)
blank

Gareth Barnard

Gareth is a developer of numerous Moodle Themes including Essential (the most popular Moodle Theme ever), Foundation, and other plugins such as course formats, including Collapsed Topics.

7 thoughts on “The first step – part one

Add a reply or comment...