39,59 €
Moodle gives you the power to create and customize feature-rich plug-ins. If you can write Moodle plug-ins, you can make it do just about anything. From making the site easier to administer, to new features, to completely changing the way it looks; plug-ins are the method Moodle offers to customize and extend its functionality. This book will show you how to build all sorts of Moodle plug-ins: admin plug-ins, Blocks, Activities, Grading components, Reports, Fliters that change the way your site works and looks. You will develop standard Moodle plug-ins such as Activities, Filters, and Blocks by creating functioning code that you can execute in your own Moodle installation. Writing modular plug-ins for Moodle will be a large focus of this book.This book will take you inside Moodle and provide you with the ability to develop code the “Moodle way”.This book will expose you to all of the core code functions in Moodle, in a progressive, understandable way. You will learn what libraries are available, what the API calls are, how it is structured and how it can be expanded beyond the plug-in system.You will begin by getting an understanding of the basic architecture that Moodle uses to operate in. Next you will build your first plug-in; a block. You will carry on building other Moodle plug-ins, gaining knowledge of the “Moodle way” of coding, before plunging deeper into the API and inner libraries. Lastly, you will learn how to integrate Moodle with other systems using a variety of methods.When you have completed, you will have a solid understanding of Moodle programming and knowledge of how to extend its functionality in whatever way you want.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 388
Veröffentlichungsjahr: 2010
Copyright © 2010 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: April 2010
Production Reference: 1220410
Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK.
ISBN 978-1-847194-24-4
www.packtpub.com
Cover Image by Parag Kadam (<[email protected]>)
Authors
Jonathan Moore
Michael Churchward
Reviewer
Anthony Borrow
Acquisition Editor
Rashmi Phadnis
Development Editor
Ved Prakash Jha
Technical Editors
Chris Rodrigues
Hithesh Uchil
Indexer
Hemangini Bari
Editorial Team Leader
Akshara Aware
Project Team Leader
Priya Mukherji
Project Coordinator
Ashwin Shetty
Proofreader
Dirk Manuel
Graphics
Geetanjali Sawant
Production Coordinator
Aparna Bhagat
Cover Work
Aparna Bhagat
Michael Churchward is currently the President and CTO of Remote Learner, a Moodle partner based in the U.S. and Canada. Mike has been a developer of Moodle since 2004, and contributes both core code and add-on functions to the open source project. Mike and his development team are currently working on other extensions to Moodle, and helping to define the new Moodle 2.0 architecture.
Michael began his career building computer-based simulations of internal aircraft systems used in the training of ground and maintenance crews. Since then, he has been involved in many types of software and computer-based learning. Programming Moodle became a natural extension of that career.
Michael lives with his family in a small town in south-western Ontario, and thanks all of them for their patience while he was writing this book.
I thank the Moodle community for their energy and input; my wife, Janet; my sons, William, Cole, and Janson for their constant encouragement; and lastly my parents, Bob and Lorane for pointing me in the right direction.
Jonathan Moore is the Vice-President for Business Development at Remote Learner. Jonathan began working with Moodle in 2003. Over the past four years, Jonathan has worked with dozens of clients, delivering Moodle customizations. Jonathan has a Bachelor of Science degree in Information Technology.
Jonathan formerly served as the Director of Technology for Winfield Public Schools. He served in this capacity for almost ten years, and has developed a broad set of educational technology and project management skills over the course of his career. His student technology program, Student Technology Assistance Team (STAT), was recognized nationally by CompTIA as the best rural student technology program. Earlier in his career, Jonathan worked as a full-time developer, and studied computer science prior to pursuing Information Technology. Jonathan started working with open source technology in 1993 with an early release of Slackware Linux, and has used and supported a variety of open source technologies in educational settings.
Jonathan lives with his wife, in Lenexa, Kansas, a suburb of Kansas City.
I wish to thank my co-author, Michael Churchward, for taking on this crazy project to write a book while growing a company. Thanks for taking this leap of faith. I would also like to thank my wife for putting up with a cranky writer this past year, my parents for emphasizing the importance of literacy, Bryan Williams and the rest of our leadership team for their support, and finally the staff of Winfield Public Schools for providing a wonderful place to grow.
Anthony Borrow, S.J. is a Jesuit of the New Orleans Province, who has been active in the Moodle community for five years. Anthony has an M.A. in Counseling from Saint Louis University. Anthony has worked on the design and implementation of various database systems since 1992.
Anthony serves the Moodle community as its CONTRIB Coordinator. In that role, Anthony has presented at various MoodleMoots (conferences) across the United States, and has provided in-house training opportunities for institutions learning how to implement Moodle. Anthony has taught at Dallas Jesuit College Preparatory and provides technical advice to the Jesuit Secondary Education Association (http://jsea.org) and the Jesuit Virtual Learning Academy (http://jvla.org/).
Anthony is the co-author of the Honduras chapter of Teen Gangs: A Global View.
Moodle has evolved into one of the most widely-used Learning Management Systems in the world, with over 35,000 installed sites and 25 million users. Much of its popularity is due to its ability to be extended by developers, by using its open source architecture. Understanding how to develop on the Moodle platform provides an organization using Moodle the flexibility to expand its functions.
This book will teach you the inner workings of Moodle, and provide you with the ability to develop code the "Moodle way". You will learn to develop standard Moodle plugins, such as activities and blocks, by creating functioning code that you can execute in your own Moodle installation.
This book will expose you to all of the core code functions in Moodle in a progressive, understandable way. You will learn what libraries are available, what the API calls are, how Moodle is structured, and how it can be expanded beyond the plugin system.
This is a practical, hands-on book that will allow you to build plugins as you learn. All of the technologies that you need are freely-available in the open source world.
You will begin by gaining an understanding of the basic architecture that Moodle uses to operate in. Next, you will build your first plugin, a block. You will carry on building other Moodle plugins, gaining knowledge of the "Moodle way" of coding, before plunging deeper into the API and inner libraries. Lastly, you will learn how to integrate Moodle with other systems, by using a variety of methods.
By the time you have finished this book, you will have a solid understanding of Moodle programming, and will have the knowledge to extend its functionality in whatever way you want.
Chapter 1, Moodle Architecture, introduces some of the important concepts of Moodle architecture: how Moodle is structured, and how Moodle works.
Chapter 2, Creating and Modifying Blocks, covers how to create a basic block in Moodle, one of the easiest and most common customizations. Moodle blocks are plugins that display content in the right-hand or left-hand side column of a Moodle site. Several basic Moodle programming concepts that are used throughout the book are introduced.
Chapter 3, Creating and Modifying Filters, covers the basic concepts of creating a Moodle filter. The Moodle filter system is a method of processing the contents of the Moodle database (typically, user-entered content) prior to display, so that it can be modified in some beneficial way.
Chapter 4, Creating and Modifying Activity Modules, covers how to create a Moodle activity that can send results to the gradebook. Moodle activities are plugins that provide instructional activities for learners. One or more activities of each type can be added to courses, and each activity typically has a graded component that sends its results to the Moodle gradebook.
Chapter 5, Customizing the Look and Feel, elaborates on the common methods of customizing the look and feel of a Moodle site. This chapter will discuss how to tell the difference between a requirement that needs programming and one that can be completed by a theme designer.
Chapter 6, Developer's Guide to the Database, covers both the overall structure of the Moodle database and the coding methods used to access and store information there. Moodle supports a variety of SQL databases to store program information.
Chapter 7, Developing Pluggable Core Modules, covers useful programming concepts and considerations for the core Moodle system and modules. Some common ways by which popular modules can be modified are explored in this chapter.
Chapter 8, Creating Moodle Reports, covers a variety of methods of adding to Moodle's built-in reports. Moodle has an excellent logging system, but often users find they need to write a custom reporting module in order to get the data in the exact format that they desire.
Chapter 9, Integrating Moodle with Other Systems, covers four common types of integrations: user authentication, user enrollment, performance results, and Single Sign On. A common customization request for Moodle is to integrate with a third-party system.
Chapter 10, Writing Secure Code, covers various security concerns and best practices when developing code for Moodle.
Chapter 11, Sending Notifications to Users, discusses methods of messaging and notifying users through e-mail, RSS, and others.
Chapter 12, Constructing and Displaying Pages by Using the pagelib Library, covers the basic concepts of using pagelib. The pagelib library is used in the construction of display pages in Moodle.
Chapter 13, Building Forms with formslib, covers how to use the formslib library for customizations. The formslib library controls Moodle forms, user input, and input sanitation.
Chapter 14, Development for the Adventuresome: Web Services, covers two separate Moodle implementations for web services: one SOAP-based and one XML-RPC-based. The SOAP library allows remote manipulation and creation of courses, users, user enrollments, and user grades. The XML-RPC library provides a secure channel and full Moodle API exposure to the trusted site. This chapter will explore basic concepts of using both libraries.
This book is written for technologists who are interested in expanding Moodle's functions through programming, either for their own organizations or to contribute to the open source project.
This book is aimed at programmers already familiar with Moodle's basic technologies: PHP, MySQL, and HTML/CSS.
You will need an understanding of PHP in order to follow along with coding activities in the book.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title in the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or send an e-mail to <[email protected]>.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book on, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code for the book
Visit https://www.packtpub.com//sites/default/files/downloads/4244_Code.zip to directly download the example code.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us to improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately, so that we can pursue a remedy.
Please contact us at <[email protected]>with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
You can contact us at <[email protected]>if you are having a problem with any aspect of the book, and we will do our best to address it.
Moodle is an open source Learning Management System (LMS). It can be used to deliver online learning in a variety of settings. These settings include virtual schools, K12, higher education, corporate universities, charter schools, and commercial training, to name but a few. Moodle is designed to be used primarily as an asynchronous learning tool, where learners study at different times. However, it also includes synchronous tools. Moodle is used both as the primary delivery vehicle for courses as well as a supplemental tool for face-to-face learning. To put it simply, Moodle is a teacher's toolkit to help improve learning. Moodle is designed in a community with teachers interacting directly with programmers. Moodle's intuitive and simple interface is the result of this collaboration.
Moodle originally stood for Modular Object-Oriented Dynamic Learning Environment. From a programmer's perspective, the "M" in Moodle is a very important concept. Modularity is designed throughout Moodle. This lets a developer make significant modifications to Moodle without having to modify its code. This is a very important capability in terms of reducing the amount of time taken to make modifications when new versions of Moodle are released. Writing modular plugins for Moodle will be a large focus of this book.
You can learn more about general Moodle functionality and history on the Moodle Documentation site at http://docs.moodle.org/en/About_Moodle.
This chapter introduces some of the important concepts of Moodle architecture; how Moodle is structured and how Moodle works. This chapter introduces the following concepts:
As you can see from the list, we will be covering a lot of ground in this chapter. Let's get started with a discussion of the technology stack that drives Moodle.
Moodle is an example of a "LAMP" application. LAMP originally stood for Linux, Apache, MySQL, and Perl. Over time, the various components of the acronym have shifted. For example, PHP has become the predominate language for "LAMP" applications. In truth, any of the components can be exchanged for another. However, the title has stuck to refer to applications written in web scripting languages, using an SQL database to store information. With the increasing popularity of running open source web applications on both Windows and Mac OS X, two new terms have been coined, respectively: WAMP and MAMP. See the following figure, which illustrates components of the system in Moodle:
Moodle is written in PHP, and the current version as of this writing (Version 1.9) requires PHP Version 4.3.0 or higher. Version 2.0 of Moodle, which is currently in development, will require PHP Version 5.2.8 or higher.
Moodle's database layer is written using the PHP ADOdb library, which was created to provide a standardized method of accessing various database systems, using a consistent programming interface. PHP native database libraries are database specific and as a result are difficult to use to write a program that can support multiple database servers. Thanks to its use of ADOdb, Moodle provides support for a variety of databases including: MySQL, PostgreSQL, Microsoft SQL, and Oracle. Moodle supports a much longer list of databases for the purpose of external system integrations, by using its plugin architecture. While Moodle does enjoy broad database support, in practice, most systems are deployed using MySQL. As a practical result of this, more eyes are looking at MySQL installations; it has the fewest bugs, with the broadest set of third-party additions. PostgreSQL is the second most popular backend and has a strong following amongst performance enthusiasts and large deployments (tens of thousands or more users). Microsoft SQL and Oracle installations are primarily used by organizations with a pre-existing investment in one of these architectures. Because of their smaller user base, they are not as well tested. It is also more difficult to find good support for using these database types with Moodle. Many third-party add-on modules do not work under these systems without patching.
Database version requirements for Moodle 1.9:
Proposed database requirements for Moodle 2.0:
The latest version of these requirements can be viewed at the following URL: http://docs.moodle.org/en/Environment#Moodle_version_1.9.
Note that the specifications for the Moodle 2.0 database layer can be found at the following location: http://docs.moodle.org/en/Development:DB_layer_2.0.
Moodle can run on any operating system that supports the required version of PHP and the database. Moodle is generally installed on one of the three major operating systems: Windows, Mac OS X, or Linux (or Unix/Unix-like operating systems). As compared to Linux, both Mac OS X and Windows operating systems suffer from less efficient performance for large-scale deployments. For Windows, this is due to a less-optimized PHP stack, and for Mac OS X it is due to poor process forking performance, which both Apache and MySQL rely on for high concurrency. Windows Server 2008 is reported in press releases by both Microsoft and Zend, the creator of PHP, as having improved PHP performance. In a development environment, you can use any of these operating systems as an excellent platform. In a production environment, each operating system will have different performance characteristics.
Moodle will typically work with any web server that supports running the appropriate versions of PHP. In practice, the most used web server is Apache, which is available for most operating systems. Internet Information Services (IIS) is another popular web server for hosting Moodle. There are also a growing number of advocates for Lighthttpd as a web server, and this is reported in the Moodle community forums (http://moodle.org/forums/) as working well with Moodle. Lighthttpd has a growing number of advocates due to its low memory design. It is extremely popular for its use with virtual environments (VMware, Xen, and Amazon Elastic Compute Cloud), where memory footprint is more of a concern.
Moodle as a web application has support and development constraints that are different from the norm. This is due to the way in which Moodle is used. A user interacting with Moodle will have a higher than normal amount of clicks, and Moodle generates many SQL queries as it builds a page. Moodle is very efficient at what it does. However, what it does is fairly complex. This means, as developers, we need to be aware of the type of architectures that our modifications will likely be used within. It also means that we need to be aware of the performance implications of our coding. The following figure illustrates several common configurations used for Moodle in a production environment. Moodle is deployed in a broad range of settings—anywhere from a single teacher running it on his or her desktop all the way up to multi-machine clusters of high performance servers:
Moodle, as is common with the standard PHP application model, scales at particular points. The first scaling point is the database, which can easily be moved to a separate physical server. After that, we can bring in additional frontend web servers by using a load balancer. When using multiple web servers we will also need shared storage for our Moodle data. Session data can be stored in either Moodle data or in the database. The database server is the point where Moodle's scalability is most limited. To scale the database it is necessary to bring in a faster database server, replacing the older server. Currently, there is no standard method for scaling a single Moodle installation across multiple master database servers, as we do with the web application servers. It is also common practice to use Moodle with a PHP accelerator such as eAccelerator or APC. It's important to test your code in these environments to make sure that it functions correctly.
There are many ways to install Moodle. One of the simplest ways is to download one of the all-in-one installers from http://moodle.org. These are labeled as Moodle for Windows and Moodle for Mac OS X under the Downloads menu at http://moodle.org. These all-in-one installers contain both Moodle and all of the necessary server software, including Apache, PHP, and MySQL. While these packages are not recommended for production use, they can provide a convenient starting point for a developer new to Moodle.
To manually install Moodle, download the Moodle source code files from http://moodle.org. You will find a variety of download packages. Each package is labeled with the version number. Major versions of Moodle are numbered in tenths: 1.6, 1.7, 1.8, and so on. Major versions focus on adding new features and bug fixes, which require major system changes to be implemented. Each major release has a number of minor point releases. For example: 1.9.1, 1.9.2, 1.9.3, and so on. Minor point releases focus on bug fixes and security updates. Finally, there are 'plus' releases that contain nightly or weekly fixes.
From a developer's perspective, it makes sense to use the official CVS feeds to get Moodle's source code. This allows for easier updates and code merges, as well as integrating with many popular development tools. See http://docs.moodle.org/en/CVS_for_Administrators.
Once you have the source code, it needs to be copied to your server's web root. You should then create a folder (outside of the web root) for Moodle data, which is writable by the web server software. From your SQL server, create an empty database for Moodle to use. Finally, once this is done, you should enter the server's address and path to Moodle into your web browser. This will initiate the Moodle web installer and walk you through the rest of the installation options. During the installation you will be asked to enter details for your database. Once these have been entered, Moodle will connect to the database and create all 200+ tables. At the end of the installation it will write a config.php file to your Moodle directory with the values that you entered during installation. The following screenshot illustrates the first screen of the Moodle installer, where we select our default language:
Upgrades follow the same basic pattern. Download the new code, copy it on the server in place of the old code (note that in most cases, you can copy the new code over the top of the old folder contents), and then visit the Moodle site in your web browser. Moodle will detect a version change by comparing the version tags stored in its configuration database against the ones reported in the module code, and then initiate the upgrade. Each module is responsible for its own database upgrades.
The final consideration for our development environment is to have a development tool for writing our code. While it is perfectly possible to write PHP code in any text editor (and many do), a popular option is to use the PHP plugin for Eclipse, or NetBeans. Detailed instructions for optimizing this setup can be found at http://docs.moodle.org/en/Setting_up_Eclipse_for_Moodle_development.
Moodle has many entry points—scripts that are called for execution. These are the files that are called by the browser to execute Moodle's functions. While there are many, the main ones are:
Let's walk through a typical script, and follow the execution path. For the walkthrough, we will use the main course display script, /course/view.php. For this example, let's assume that we are using http://localhost/course/view.php?id=23.
Before we jump in, let's analyze the URL a bit. The portion up to /course will be the URL of your Moodle site. The /course/ portion is the directory in Moodle that contains the course-handling scripts. The meat of the URL is the portion view.php?id=23. This script displays a course page. The course it displays is identified by the id=23 parameter, where 23 is the data ID of the course in question. If you look in the course data table in the database for your Moodle site, you will find an entry with the 'id' field equal to 23. The very first executable line we see in the script is:
All of the entry scripts include the same important file: config.php. This is the only file in Moodle that must be included by specifying the exact literal path. This file performs a number of initial parameter assignments in the global $CFG variable. This provides the minimal amount of information to run the rest of the system; among this information are the database, web URL, script directory, and data storage directory definitions.
Lastly, and most importantly, config.php includes /lib/setup.php.
config.php performs some basic, important variable assignment, whereas setup.php performs all of the initial program execution required to complete the execution environment setup. This includes defining several other important global variables, including $SESSION, $COURSE, $THEME, and $db.
Next, it sets up and connects your database according to the settings defined in config.php. Moodle uses the open source ADOdb libraries to manage database functions. These functions are loaded through the inclusion of /lib/adodb/adodb.inc.php.
Next, some critical library files are included that will be used by pretty much every other function in Moodle. These libraries include the functions required to manage multibyte strings, HTML output, Moodle data, access controls, events, groups, and general purpose Moodle and PEAR libraries.
The remainder of the file sets up some other critical global variables, loads configuration variables from the database, sets up caching, sessions, environment variables, themes, language, and locales.
After including config.php, our script includes some other library files:
This uses the PHP construct require_once, in case any of the files were included elsewhere first. Any file not located in the same directory is fully specified using directory definitions defined in config.php. You will notice lib.php has no directory specification, as it is located in the same directory as view.php. Likewise, the remaining files have full paths using directory definitions set up by config.php. You will notice the use of $CFG->libdir and $CFG->dirroot. The first is the defined file path to the main library directory (usually the lib
