Moodle JavaScript Cookbook Over 50 recipes for making your Moodle system more dynamic and responsive with JavaScript Alastair Hole BIRMINGHAM - MUMBAI Moodle JavaScript Cookbook Copyright © 2011 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 author, 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 2011 Production Reference: 1180411 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-849511-90-2 www.packtpub.com Cover Image by Rakesh Shejwal ([email protected]) Credits Author Editorial Team Leader Alastair Hole Akshara Aware Reviewers Project Team Leader Anthony Borrow Priya Mukherji Mauno Korpelainen Project Coordinator Susan Smith Nash Srimoyee Ghoshal Acquisition Editor Proofreader Chaitanya Apte Joel T. Johnson Development Editor Production Coordinator Roger D'souza Kruthika Bangera Technical Editor Cover Work Azharuddin Sheikh Kruthika Bangera Copy Editor Neha Shetty Indexer Hemangini Bari About the Author Alastair Hole is a web software developer, who is currently specializing in educational software, particularly that which pertains to Further and Higher Education in the UK. His web development experience began in the post dot-com boom era, working on business- to-business e-commerce web applications in the publishing industry with a focus on back- office integration. He has since transferred his talents to the educational sector and has created projects which have gone on to receive awards from organizations such as The Times Educational Supplement and the IMS Global Learning Consortium. Alastair is the author of the award-winning Moodle IMS Content Package repository plug- in "MrCUTE—Moodle Repository: Create, Upload, Tag, & Embed", which is an Open Source project commissioned by the Joint Information Systems Committee (JISC) that has seen significant use in Moodle sites worldwide, from Scotland to Australia. Thank you to everyone who has contributed to this book's completion, be it patience with missed deadlines or encouragement when all seemed overwhelming. Thank you, Ma and Pa Hole for their foresight in recognizing that one day I would teach myself how to reassemble everything that I have ever unscrewed, detached, disassembled, or otherwise fiddled with. Thank you to The Internet for being an ever expanding well of autodidactic opportunity. And remember, "It's just a ride. And we can change it any time we want." About the Reviewers 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, and a Masters of Divinity from the Jesuit School of Theology of Santa Clara 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 provided in-house training opportunities for institutions learning how to implement Moodle. Anthony is currently the Campus Minister at Cristo Rey Jesuit College Preparatory (http://cristoreyjesuit.org/) 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, and also has been a technical reviewer of various Packt books. I am grateful to the Moodle community for continually inspiring me to learn more about educational technologies and fostering an environment where every voice contributes to building that community. Mauno Korpelainen teaches Mathematics for high school and adult students in Hyvinkää, Finland, has been a PHM (Particularly Helpful Moodler) for several years, and is one of the moderators of the moodle.org forums. Susan Smith Nash is currently the Director of Education and Professional Development for the American Association of Petroleum Geologists (AAPG) in Tulsa, Oklahoma, and an adjunct professor at The University of Oklahoma. She was the Associate Dean for graduate programs at Excelsior College (Albany, NY). Previous to that, she was the Online Courses Manager at the Institute for Exploration and Development Geosciences and Director of Curriculum Development for the College of Liberal Studies, University of Oklahoma, Norman, OK, where she developed an online degree program curriculum for online courses at The University of Oklahoma. She also developed an interface for courses, as well as administrative and procedural support, support programmers, protocol and training manuals, and marketing approaches. She obtained her Ph.D. and M.A. in English and a B.S. in Geology from the University of Oklahoma. Nash blogs at E-Learning Queen (http://www.elearningqueen.com) and E-Learners, and has written articles and chapters on mobile learning, poetics, contemporary culture, and e-learning for numerous publications, including Trends and Issues in Instructional Design and Technology (3rd ed), Mobile Information Communication Technologies Adoption in Developing Countries: Effects and Implications, Talisman, Press1, International Journal of Learning Objects, GHR, World Literature, Gargoyle. Her latest books include Moodle 1.9 Teaching Techniques (Packt Publishing, 2010), E-Learners Survival Guide (Texture Press, 2009), and Klub Dobrih Dejanj (2008). www.PacktPub.com Support files, eBooks, discount offers and more You might want to visit www.PacktPub.com for support files and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. Why Subscribe? f Fully searchable across every book published by Packt f Copy and paste, print and bookmark content f On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. Table of Contents Preface 1 Chapter 1: Combining Moodle and JavaScript 7 Introduction 7 Creating a new Moodle PHP page 8 Loading a JavaScript file 11 Loading a JavaScript file in <head> 13 Generating a JavaScript function call from PHP 14 Passing variables from PHP to JavaScript 15 Ensuring compliance with XHTML Strict 17 Retrieving language strings from Moodle 18 Chapter 2: Moodle and Yahoo! User Interface Library (YUI) 21 Introduction 21 Initializing the YUI 3 library 22 Loading additional YUI modules 24 Loading YUI 2 modules from YUI 3 25 Attaching basic event handlers 27 Attaching advanced DOM event handlers 29 Implementing event delegation 33 Debugging with the YUI console 36 Chapter 3: Moodle Forms Validation 39 Introduction 39 Adding a required field 40 Adding a field with a maximum length 43 Adding a field with a minimum length 44 Adding a field length within in a specified range 45 Adding a valid e-mail address field 46 Adding custom validation with a Regular Expression 47
Description: