You are previewing Moodle 1.9 Extension Development.
O'Reilly logo
Moodle 1.9 Extension Development

Book Description

Customize and extend Moodle using its robust plug-in systems

  • Develop your own blocks, activities, filters, and organize your content with secure code

  • Thoroughly covers key libraries of Moodle and best practices to use them

  • Explore the Moodle architectural concepts, how it is structured, and how it works

  • Detailed examples and screenshots for easy learning

  • In Detail

    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. A practical guide to creating feature-rich extensions for your website using Moodle plug-ins

    Table of Contents

    1. Moodle 1.9 Extension Development
    2. Credits
    3. About the Authors
    4. About the Reviewer
    5. Preface
      1. Approach
      2. What this book covers
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Errata
        2. Piracy
        3. Questions
    6. 1. Moodle Architecture
      1. Understanding the stack
        1. Database
        2. Operating system
        3. Web server
      2. Directory and system structure
        1. Moodle code
        2. Moodle database
        3. Moodle data
      3. Installing Moodle
      4. Moodle program execution
        1. Executing config.php
        2. Including Moodle libraries
        3. Getting our data
        4. Displaying in Moodle
      5. Configuring Moodle
        1. Setting configuration variables
      6. Moodle's API
      7. Other important libraries
        1. PEAR
        2. ADOdb
        3. YUI
        4. XMLDB
      8. Access control, logins, and roles
        1. Moodle permission contexts
        2. Capabilities
        3. Moodle roles
          1. Assigning roles
      9. Summary
    7. 2. Creating and Modifying Blocks
      1. Creating a block from scratch
        1. Assigning content to our block
      2. Adding a language file
      3. Working with capabilities
        1. Testing for built-in capabilities
        2. Hiding a block based on capabilities
        3. Adding your own capability
        4. Checking for our new capability
      4. Adding instance configuration
        1. Capturing your configuration content
        2. Displaying your content
      5. Adding scheduled actions to our block
      6. Reviewing a real world block
        1. Reviewing block_instructor_contact.php
        2. Configuring the instructor contact block
      7. Using a block as a code container
        1. Creating a block stub for our container
        2. Creating capabilities for our container
      8. Summary
    8. 3. Creating and Modifying Filters
      1. How a filter works
      2. Building a filter
        1. Getting started
        2. Creating the language file
        3. Creating the filter code
        4. Activating the filter
        5. Adding configuration settings
        6. Using our settings
      3. Summary
    9. 4. Creating and Modifying Activity Modules
      1. The scope of creating an activity module
      2. Our module—activity Foo!
      3. Getting started with the NEWMODULE template
        1. Completing mod_form.php
          1. Defining input values
          2. Using common form elements
      4. Making version.php
      5. Setting icon.gif
      6. Generating install.xml
        1. Using the XMLDB editor
        2. Defining our activity database
        3. Defining the foo_responses table
        4. Defining log types
      7. Creating access.php
      8. Updating index.php
        1. Adding clean language strings
        2. Looking up response data
          1. Displaying our table of activities
          2. Displaying course sections
          3. Committing output
      9. Finishing view.php
        1. Including submit_form.php
        2. Your code goes here
        3. Context control
        4. Developing a form
          1. Cancel option
          2. Submitted data
        5. Setting grades
        6. Displaying submission form
        7. Displaying submissions
        8. Group support
      10. Coding lib.php
        1. Function add_instance
        2. Function foo_delete_instance
        3. Function foo_user_outline
        4. Function foo_user_complete
        5. Function foo_get_participants
        6. Function reset_course_form_definition
        7. Function foo_reset_course_form_defaults
        8. Function foo_reset_userdata
        9. Inserting grades into the gradebook
          1. Using grade_update
            1. Setting the grade type
      11. Upgrading our activity database
        1. Using the XMLDB editor for database upgrades
        2. Updating upgrade.php
      12. Creating backup and restore support
        1. Exploring backuplib.php functions
          1. Common backup functions
          2. Internal backup functions
        2. Exploring restorelib.php functions
          1. Common restore functions
          2. Internal restore functions
      13. Summary
    10. 5. Customizing the Look and Feel
      1. Themes
        1. How themes work
        2. Headers and footers
          1. header.html
          2. footer.html
        3. Project 1: Changing the navigation breadcrumbs
          1. Creating our own theme
          2. Adding the category link
          3. The navigation string
        4. Project 2: Adding a standard footer with site links
          1. Creating the code
          2. Running the code
      2. Course formats
        1. How course formats work
        2. Creating our own course format
          1. Creating the course format language file
          2. Modifying our course format function
          3. Changing the forum display
      3. Summary
    11. 6. Developer's Guide to the Database
      1. The database structure
      2. Maintaining Moodle tables
        1. install.xml
        2. upgrade.php
        3. Maintaining local database customization
        4. The XMLDB editor
      3. Using the Moodle database in your code
        1. Retrieving data
          1. Query by parameters
          2. Query by "where" string
          3. Query by full SQL
        2. Storing data
          1. set_field and set_field_select
          2. insert_record and update_record
      4. Common tables and relationships
        1. Course and module relationships
      5. Programming best practices
        1. Take only what you need
        2. Limiting your returned data
        3. Using recordsets
        4. Optimizing carefully with joins
        5. Testing on more than one database engine
      6. Summary
    12. 7. Developing Pluggable Core Modules
      1. Assignment types
        1. What is an assignment type
        2. Identifying assignment types
        3. Assignment type basics
        4. Building our own assignment type
          1. Getting started
          2. Planning our data
          3. Adding custom settings
          4. Making it do something useful
            1. Viewing the journal
            2. Getting the submission
            3. Adding an entry
            4. Viewing a submission
            5. Reviewing students' entries
            6. Backup and restore functions
            7. Finishing up
      2. Resource types
        1. What is a resource type?
        2. Identifying resource types
        3. Resource type basics
      3. Question types
        1. What is a question type?
        2. Identifying question types
        3. Question type basics
          1. The main class
          2. The editing form
          3. The main language file
        4. Other important question constructs
          1. Grading
          2. Backup and restore functions
          3. Import and export function support
          4. CSS and JavaScript support
      4. Where to get more help
      5. Summary
    13. 8. Creating Moodle Reports
      1. Creating course reports
        1. Defining the non-participants report
        2. Getting started
        3. Updating mod.php
        4. Controlling access
        5. Language strings
        6. Changes to Moodle's API
        7. Exploring the People block
          1. Setting $select
          2. Setting $from
          3. Setting $where
          4. Making the query call
        8. Modifying index.php
          1. Making text substitutions
          2. Changing the logical structure
          3. Tracking active users using a hash array
          4. Displaying the report
          5. Cleaning up the final display
          6. Using flexible_table
        9. An exercise for you
      2. Creating gradebook reports
        1. Required components
          1. Creating our report folder
          2. Creating access.php
          3. Assigning language strings
          4. index.php requirements
          5. Using version.php
        2. Using the grade_report class
        3. Fixing the outcomes report
          1. Digging into the code
          2. Reviewing our new report
      3. Creating administrator reports
        1. Generating the Hello World administrator report
        2. Tweaking index.php
          1. Viewing our administrator report
        3. An exercise for you
      4. Other output formats
      5. Summary
    14. 9. Integrating Moodle with Other Systems
      1. Built-in services and plugins
        1. Batch upload
        2. Built-in authentication plugins
          1. Manual/Internal authentication
        3. LDAP/Active directory authentication
        4. External database authentication
          1. E-mail account based authentication plugins
        5. Built-in enrollment plugins
          1. LDAP enrollment plugin
          2. External database enrollment plugin
        6. E-commerce based enrollment plugins
        7. Web services
          1. Activity plugin as an integration bridge
      2. Creating user authentication plugins
        1. Exploring authentication requirements
          1. Looking at auth.php
          2. Making the language file
          3. Testing simple authentication
          4. Adding optional functionality to authentication
        2. Creating the RLLDAP authentication plugin
          1. Coding RLLDAP
            1. Changing LDAP string to RLLDAP
              1. Mass change auth/ldap to auth/rlldap
              2. Mass change 'ldap' to 'rlldap'
              3. Mass change role assignment
            2. Updating and renaming the auth_plugin class
            3. Creating our language file
            4. Modifying config.html
              1. Storing administrator group
      3. Implementing Single Sign On
        1. Creating a URL-based SSO
          1. Digging into the code
          2. Finding the user's account
          3. Confirming that the login criteria have been met
          4. Completing login
          5. Redirecting the user to the correct location
        2. Testing the URL SSO
      4. Creating user enrollment plugins
        1. Reviewing plugin requirements
          1. Creating enrol.php
          2. Creating the setup_enrolments function
          3. Making the language file
          4. Other requirements
          5. Making the 'simple' enrollment plugin
        2. Using optional enrollment functions
        3. Assigning enrollment plugins to courses
      5. Grade or completion passing
        1. Working with the built-in import/export plugins
        2. Creating grade import/export plugins
          1. An exercise for you
      6. Summary
    15. 10. Writing Secure Code
      1. User access issues
        1. Making sure that a user has logged in
        2. Making sure that the user's session is the current one
        3. Making sure that the user has appropriate capabilities
      2. SQL issues
        1. Taking a look at vulnerable code
        2. Quote protection
        3. Database API
        4. Use of addslashes
      3. Form issues
        1. The Moodle formslib library
          1. Specifying a type
          2. Validation rules
        2. 'param' functions
      4. File system issues
        1. Course file areas
        2. User file areas
        3. Other file areas
        4. Getting a file URL
      5. Screen output issues
        1. The p() and s() functions
        2. The format_text() and format_string() functions
      6. Logging your actions
      7. Summary
    16. 11. Sending Notifications to Users
      1. Requirements
      2. Setup
      3. Using e-mail
        1. Send e-mail API
        2. Creating the subject text
        3. Creating the message text
        4. Sending the e-mail
      4. Using Moodle messaging
        1. Moodle messaging internals
        2. Creating the message
        3. Sending the message
      5. Using RSS feeds
        1. Setup
        2. Creating the RSS XML file
        3. Viewing the RSS feed
      6. Summary
    17. 12. Constructing and Displaying Pages by Using the pagelib Library
      1. Introduction to pagelib
        1. Class page_base
        2. Class page_course
        3. Class page_generic_activity
      2. Core modules that use pagelib
      3. Using pagelib factory functions
        1. page_import_types
        2. page_create_instance
        3. page_create_object
        4. page_map_class
      4. page_base member functions
        1. page_base() and construct()
        2. get_id()
        3. get_body_class()
        4. get_body_id()
        5. edit_always()
        6. init_quick()
        7. init_full()
        8. user_allowed_editing()
        9. user_is_editing()
        10. print_header()
        11. get_type()
        12. get_format_name()
        13. url_get_path()
        14. url_get_parameters()
        15. blocks_get_positions()
        16. blocks_default_position()
        17. blocks_get_default()
        18. blocks_move_position()
      5. Meeting pagelib requirements
        1. Looking at page setup
        2. Examining requirements by reviewing quiz's view.php
          1. Working with quiz blocks
          2. Investigating quiz page display
      6. Converting activity Foo! to pagelib
        1. Creating a local pagelib.php
        2. Updating view.php
          1. Initializing $PAGE
          2. Outputting blocks
          3. Outputting the middle column
          4. Viewing the results
      7. Related libraries
        1. weblib
        2. blocklib
        3. graphlib
      8. Summary
    18. 13. Building Forms with formslib
      1. Using formslib
      2. Form definition and elements
        1. addElement and createElement
          1. Buttons
            1. Button function
            2. Submit, reset, and cancel functions
            3. add_action_buttons function
          2. Checkboxes
            1. Checkbox function:
            2. Advanced checkbox and checkbox controller
          3. Uploading and/or choosing a file
            1. choosecoursefile function
          4. Date and time
            1. date_selector function
            2. date_time_selector function
          5. htmleditor, textarea, and text
            1. htmleditor function
            2. text function
          6. modgrade
          7. password and passwordunmask
          8. radio
          9. select, multi-select, and selectyesno
          10. hidden, html, and static text
          11. Groups
      3. Rules and validation
        1. Rules
          1. addRule
          2. registerRule
        2. Validation
      4. Summary
    19. 14. Development for the Adventuresome: Web Services
      1. Using Remote-Learner Web Services
        1. Advantages
        2. Disadvantages
        3. Installation
          1. Server-side setup
          2. Client setup
          3. Testing with ppdemo_trace.php
        4. Setting up your Web Services connection
          1. Debugging setup
        5. Managing users
          1. Creating users
            1. Processing arguments
            2. Connecting to Web Services
            3. Using edit_user() to create a new account
          2. Updating users
          3. Deleting users
        6. Creating a Web Services based Single Sign On
          1. Creating our HTML wrapper
          2. Web service connection setup
          3. Processing SSO parameters
          4. Using authenticate_user()
          5. Closing wrapper
        7. Enrollment management
          1. Creating enrollstudent.php
          2. Using enrol_students()
          3. Creating unenrollstudent.php
        8. Grade passing
          1. Creating the CSV grade script
            1. Generating the header line
            2. Course loop
      2. Using Moodle Networking
        1. Advantages
        2. Disadvantages
        3. What is XML-RPC?
          1. MNet XML-RPC supported service types
          2. MNet XML-RPC security model
          3. Using MNet in activity modules
          4. Using MNet in auth/enrol plugins
          5. MNet SSO model
      3. Summary