Book Image

Moodle 3.x Developer's Guide

By : Ian Wild, Jaswant Tak
Book Image

Moodle 3.x Developer's Guide

By: Ian Wild, Jaswant Tak

Overview of this book

The new and revamped Moodle is the top choice for developers to create cutting edge e-learning apps that cater to different user’s segments and are visually appealing as well. This book explains how the Moodle 3.x platform provides a framework that allows developers to create a customized e-learning solution. It begins with an exploration of the different types of plugin.. We then continue with an investigation of creating new courses. You will create a custom plugin that pulls in resources from a third-party repository. Then you’ll learn how users can be assigned to courses and granted the necessary permissions. Furthermore, you will develop a custom user home. At the end of the book, we’ll discuss the Web Services API to fully automate Moodle 3.x in real time.
Table of Contents (19 chapters)
Title Page
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Customer Feedback
Dedication
Preface
6
Managing Users - Letting in the Crowds

Our first plugin - a custom string manager


As described earlier, we will be working together to develop plugins for a new global Moodle-based learning management system in this book. For the rest of this chapter, we will be working through the development of a simple plugin, one that modifies the way language strings are displayed on the user interface. Note that the intention isn't to demonstrate the finer details of how to write a Moodle plugin; that is the purpose of the rest of the book. The rest of this chapter is written to outline the kind of thought processes that one would engage in when being tasked with developing a new feature in Moodle. So, let's begin with finding out what the task that we have been set is.

The user story

In true agile fashion, the following user story has been assigned to us:

The client requires two languages to be displayed on the screen at the same time: English and Mandarin Chinese. Moodle supports language switching (refer to https://docs.moodle.org/31/en/Language_settings for details) but doesn't show two languages at the same time. Also, on the face of it, this seems a fairly straightforward problem. However, let's always ensure that we take a methodical approach to any request and avoid the temptation to simply start developing. By being disciplined, we can ensure that the plugin we develop is not only the most effective fit for this immediate requirement, but also that:

  • It is future proof
  • It is reusable
  • It is ready, potentially, to be offered back to the development community (IPR issues not withstanding)

The development process

Let's spend the rest of this section learning about the development process. There are many well-defined development processes, and these will be explored in the rest of the book. The art, however, is knowing where to start, and we should always start by attempting to thoroughly understand the problem in every way.

Step 1: Understanding the problem

It is, perhaps, a statement of the obvious, but before you start developing, ensure that you fully understand the problem you have been asked to solve. The standard journalist's questions are a good framework to aid your understanding. Consider the following:

  • Who: Will the two languages be displayed to all users or only to certain users? In our case, all users.
  • What: Are we talking about US English or UK English? Does the client want "Mandarin", which means mainland Chinese displayed using the simplified character set? Our client has specified UK English and simplified Chinese Mandarin.
  • Why: Is this because our users are bilingual, or is this being included as a teaching aid? The client is wanting two languages on the screen simultaneously as they are expecting the platform to be used in group teaching.
  • Where: Moodle language switching toggles the language on the platform only; it doesn't toggle languages in any teaching materials. This client wants both the platform and the courses to be bilingual.
  • When: Are two languages to be displayed everywhere or only in particular contexts? Dual languages are to be displayed throughout the platform.

Also, having gained an understanding of the problem, it's time to consider the how.

Step 2: Researching potential solutions

Moodle already supports a range of languages through installable language packs, and for version 3.1, these are listed at https://download.moodle.org/langpack/3.1/. The language pack we need is zn_ch.zip, which is simplified Chinese Mandarin. Follow the instructions at https://docs.moodle.org/31/en/Language_packs to install this pack into your local development Moodle:

Having installed the relevant language pack, we can now experiment with creating a test Moodle user whose native language is simplified Chinese:

Having satisfied ourselves that we can switch to the Chinese language, we now need to assess options for displaying two languages on the screen at the same time. For that--and in the spirit of social constructivist learning in general--we need to go out into the world... Let's start with the main Moodle website at https://www.moodle.org.

Moodle plugin directory

Take a look at https://moodle.org/plugins and search for language:

Unfortunately, nothing is available to support dual language display in the plugins directory.

Moodle community forums

Martin Dougiamas created Moodle as a platform not only to support social constructivist teaching, but he also wanted to have development of the platform supported along social constructivist lines. So, the main Moodle website hosts a variety of forums where users and developers can openly discuss issues and address support-related and frequently asked questions. Visit https://moodle.org/course/ for a full list of forums (you will need to create a user account if you want to post any messages). The language forum itself is at https://moodle.org/mod/forum/view.php?id=43.

If you don't find anything relevant in the user forums, we can take a look at the Moodle Tracker next.

Moodle Tracker

Detailed discussions on Moodle developments tend to take place in the Moodle Tracker at https://tracker.moodle.org/secure/Dashboard.jspa. A general search on string management has revealed a discussion on the ability to provide an alternate string manager; Tracker ticket ID MDL-49361 https://tracker.moodle.org/browse/MDL-49361:

String API

For a detailed explanation of the String API, visit https://docs.moodle.org/dev/String_API. The String API documentation reveals that the most effective method of having Moodle display two language strings on the screen at the same time is to author our own string manager, within which we have implemented a custom get_string() function. Luckily, the reporter of ticket MDL-49361 has already created an example, Moodle plugin moodle-local_stringman, and made it available through GitHub (refer to the preceding section, Source control discipline):

You will see that the README notes for the moodle-local_stringman plugin stress that the code provided should be used for illustration purposes only. So, we will need to download a copy of the source and create our own GitHub repository to work on:

That done, it's time to work on our solution.

Step 3: Implementing the solution

We will create a new local plugin. Essentially, local plugins are the place to put enhancements to core Moodle that don't fit into any other obvious place (that is, an enhancement that isn't a new course activity or a new user authentication method). We will learn much more about local plugins in Chapter 2, Moodle Plugins - What Can I plug In? I have called this new plugin duallang. This is publicly available on GitHub at https://github.com/iandavidwild/moodle-local_duallang.

Standard plugin features

Download the plugin from GitHib. Now, let's take a look at the structure of the duallang plugin together. There are three files worth mentioning:

  • version.php: This contains not only the version information of the plugin itself but also the minimum version of Moodle required to run it. It also, optionally, contains a list of dependencies. The following is a copy of the relevant code from our new plugin:
      $plugin->component = 'local_duallang'; 
      $plugin->release = 'alpha1'; 
      $plugin->version = 2016112300; 
      $plugin->requires = 2015030900; 
      $plugin->maturity = MATURITY_ALPHA; 
      $plugin->dependencies = array(); 
  • lang/en/local_duallang.php: This contains the plugin's language strings. There is only one language string required for this basic plugin and that is the name of the plugin itself:
      $string['pluginname'] = 'Dual language string manager'; 
  • classes/duallang_string_manager.php: This script contains the overridden implementation of the core_string_manager class, specifically, the get_string() function, which has been modified to construct a language string from both the UK English and the simplified Chinese Mandarin language packs:
      class duallang_string_manager extends 
      \core_string_manager_standard { 

          /** 
          * Implementation of the get_string() method to display both 
          simplified 
          * Chinese and UK English simultaneously. 
          * 
          * @param string $identifier the identifier of the string to 
          search for 
          * @param string $component the component the string is 
          provided by 
          * @paramstring|object|array $a optional data placeholder 
          * @param string $langmoodle translation language, null means 
          use 
          * current 
          * @return string 
          */ 
          public function get_string($identifier, $component = '', $a = 
          null, 
          $lang = null) { 

              $string = parent::get_string($identifier, $component, $a, 
              'en'); 

              $zh_cn = parent::get_string($identifier, $component, $a, 
              'zh_cn'); 

              if(strlen($zh_cn) > 0) { 
                  $string .= ' | ' . $zh_cn; 
              } 

              return $string; 
          } 
      } 

Note that in order to specifically load UK English and simplified Chinese, we simply call the method in the parent class (refer to the An objected-oriented philosophy section for some elaboration on the parent/child class relationship).

At the top of each PHP script, you will find a general comment header, important copyright, and optional version control information. You will also see the following line of code:

defined('MOODLE_INTERNAL') || die(); 

This prevents someone from outside of Moodle from accessing the PHP script. To install the plugin, we can either copy the files to Moodle's local folder or use Moodle's built-in plugin installer (for details, visit https://docs.moodle.org/31/en/Installing_plugins#Installing_via_uploaded_ZIP_file):

Once installed, we will also need to add a line to Moodle's config.php file (located in Moodle's root directory):

Return to Moodle's home page and you will now see the UK English and simplified Chinese Mandarin language strings displayed side by side: