Development:AMF3: Difference between revisions
- New Moodle Webservices for use with Flex/AS/ (dec 2009) WS TRACKER 2.0
- OLD ActionScript 3.0 (Flash CS3 and Flex) page
- Original ActionScript 2.0 tutorial Development:AMFPHP
(Updated to link to Moodle 2.0 development of webservices (for use with various including Flex, Flash builder, AS) |
|||
Line 474: | Line 474: | ||
==See Also== | ==See Also== | ||
[[Moodle 2.0 Webservices]] - PLease refer to [http://tracker.moodle.org/browse/MDL-12886 WS TRACKER 2.0] for updates on forthcoming AMF service for use with Moodle | |||
[[Development:SWF]] - SWF Activity Module deploys Flash and Flex Framework (.swf) applications as learning interactions. Unlike the Flash Activity Module, it supports all versions of Flash and Flex Framework, XML, 3rd Party APIs and will eventually support Flash Remoting (AMF). | [[Development:SWF]] - SWF Activity Module deploys Flash and Flex Framework (.swf) applications as learning interactions. Unlike the Flash Activity Module, it supports all versions of Flash and Flex Framework, XML, 3rd Party APIs and will eventually support Flash Remoting (AMF). |
Revision as of 18:35, 6 December 2009
Updates Dec 2009 - Marcus Potter
AMF for use with FLex/FlashBuilder/AS is being developed through web service infrastructure in Moodle 2.0 dev version. Developers please see WS TRACKER 2.0
The information contained below is for reference only.
History: What is AMFPHP?
Action Message Format PHP
AMFPHP is a widely used open source remoting server that allows Flash and Flex client-side applications to call PHP methods directly, as if they were native Flash/Flex ActionScript methods. It is fast and lightweight and presents an efficient, simple and easy to implement method of communicating with PHP and databases.
Preserved Data Types
AMFPHP preserves the following data types between ActionScript and PHP:
- Array
- Bitmap
- ByteArray
- int
- Number
- Object
- Recordset (mysql_result)
- String
- XML (ActionScript 3.0 also supports E4X notation)
Note: Please add to this list if you have successfully tested data types using AMFPHP 1.9.beta+ and ActionScript 3.0.
AMFPHP automatically converts data types between ActionScript and PHP to their native equivalents. For example, it can convert a PHP array into and ActionScript array or a PHP resource, such as a mysql_result into an Actionscript Recordset.
AMF0 and AMF3
Previous versions of Flash, using ActionScript 2.0 (versions 6, 7 and 8), use AMF0. ActionScript 3.0, Flash CS3+ and Flex, use AMF3 by default but can also use AMF0. What's the difference? AMF3 is compressed and therefore lighter and faster. For more details look here. AMFPHP version (1.9) supports AMF3
What is AMF Moodle?
AMF Moodle is a new project (9th October 2008) which aims to integrate Flash and Flex with Moodle's API. The aim is to build a library of services that Flash and Flex developers can use to create secure client-side applications that can interact with Moodle. The first step will be to create a Moodle modules that facilitates deploying custom made, generic Flash and Flex e-learning interactions in Moodle courses and recording user interaction results in the Moodle gradebook.
AMF Moodle Outline
There is a summary of some of the specifications for the new AMF Moodle on the Talk:AMF_Moodle comments page that are under discussion. Once we have a more definite specification, it will be transferred to here. Please feel free to comment, make requests, ask questions etc. on the comments page. It'll really help us alot.
AMF Moodle Participants
The project's participants at the moment are: Matt Bury, Jamie Pratt and Marcus Potter.
You can find the project home page at code.google.com.
Find the AMF Moodle discussion thread here.
For further discussion, please use the Talk:AMF_Moodle page comments tab for this wiki page.
What Are the Differences between Using AMFPHP and SCORM?
Several Moodle developers and contributors have asked the question, "If you want to deploy Flash in Moodle, why not use SCORM?" There is a discussion in the General Developers Forum on the advantages and disadvantages of SCORM.
Installing AMFPHP in Moodle
Download
- Download the latest version of AMFPHP, currently version 1.9.beta (SourceForge repository).
- Unzip the file and find the directory amfphp.
- Upload the amfphp directory to ***MOODLEROOT***/lib/
Edit gateway.php
- Find the file ***MOODLEROOT***/lib/amfphp/gateway.php and open it in your favourite text/PHP editor
- At the beginning of the code, add the line: include_once "../../config.php";
- Find and edit the line: $gateway->setClassPath($servicesPath); and change it to: $gateway->setClassPath($CFG->dirroot."/lib/amfphp/services/");
- Upload the edited ***MOODLEROOT***/lib/amfphp/gateway.php file.
- That's it!
Setting PHP 5 as Default
Please note: AMFPHP requires PHP 5 to work seamlessly, although some report success with version 4. Many servers run both PHP 4 and 5, but have them set to run PHP 4 by default. If you experience problems, you may have to change the default PHP version in the ***MOODLEROOT***/lib/amfphp/ directory with an .htaccess file. The following is an example only. Please check that it is correct for your server configuration. If you have a hosted server, they'll probably have an example in their help files. For example:
- Find the AMFPHP .htaccess file at ***MOODLEROOT***/lib/amfphp/.htaccess
- Open it with a text editor. You'll see:
#If you're working with a server which doesn't seem to display errors and you don't #have access to httpd.conf and you have a good reason to develop remotely instead of #locally, you may have luck with uploading this configuration file to the server php_flag display_errors on php_flag display_startup_errors on php_value error_reporting 2047
- Add another line of code to change the default PHP version setting for the amfphp directory:
#If you're working with a server which doesn't seem to display errors and you don't #have access to httpd.conf and you have a good reason to develop remotely instead of #locally, you may have luck with uploading this configuration file to the server php_flag display_errors on php_flag display_startup_errors on php_value error_reporting 2047 SetEnv DEFAULT_PHP_VERSION 5
Testing AMFPHP in Moodle
Here is an example of a simple AMF Moodle service that checks the login status of your browser.
- Create a new document in your preferred text/PHP editor
- Copy and paste the code and save it as UserName.php
- Upload it to ***MOODLEROOT***/lib/amfphp/services/
Now is a good time to find out if your server is running the correct PHP version. If your server is running PHP 4 by default, you'll get error messages caused by the "public" namespace.
- Navigate to the PHP page you just uploaded in your browser: ***MOODLEROOT***/lib/amfphp/services/UserName.php
- If there are no error messages then you can continue to the "Testing AMFPHP in Flash" section
<?php class UserName { public function __construct() { } /** *checks whether you're logged in to Moodle *@returns user login status */ public function loggedInAs() { global $USER; if (isguestuser()){ return get_string('loggedinasguest', 'moodle'); }else if (isloggedin()){ return get_string('loggedinas', 'moodle', fullname($USER)); } else { return get_string('loggedinnot'); } } } ?>
Testing AMFPHP in Flash
Please note: Flash CS3 and Flex come with the necessary classes for using AMFPHP already installed. It is not necessary to install any new classes!
- Open the Flash CS3 IDE
- Create a new ActionScript 3.0 class document
- Copy and past the code below and save it as UserName.as
- Create a new ActionScript 3.0 FLA document
- In Properties > Document class: type UserName.as
- Save the FLA file as UserName.fla in the same directory as UserName.as
- Publish the UserName.swf, AC_RunActiveContent.js and UserName.html files
- Upload the published files to your server
- In your browser, navigate to the location of the UserName.html page
- Try logging in and out of Moodle to see the change in your login status
ActionScript 3.0 Document Class
package { import flash.display.Sprite; import flash.text.*; import flash.events.MouseEvent; import flash.events.NetStatusEvent; import flash.net.NetConnection; import flash.net.Responder; public class UserName extends Sprite { private var _format:TextFormat; private var _display:TextField; private var _call:Sprite; private var _gateway:NetConnection; private var _responder:Responder; public function UserName() { initFormat(); initDisplay(); initCall(); initGateway(); } // create text format object private function initFormat():void { _format = new TextFormat(); _format.font = "Trebuchet MS"; _format.size = 15; _format.bold = true; } // create text field to display results private function initDisplay():void { _display = new TextField(); _display.autoSize = TextFieldAutoSize.LEFT; _display.multiline = true; _display.x = 10; _display.y = 10; _display.defaultTextFormat = _format; _display.text = "Click on \"Call UserName.php\" to test."; addChild(_display); } // create text button to call UserName.php private function initCall():void { _call = new Sprite(); _call.mouseChildren = false; _call.buttonMode = true; _call.addEventListener(MouseEvent.MOUSE_DOWN, callDownHandler); var btn:TextField = new TextField(); btn.autoSize = TextFieldAutoSize.LEFT; btn.border = true; btn.background = true; btn.backgroundColor = 0xdddddd; btn.defaultTextFormat = _format; btn.text = " Call UserName.php "; btn.x = (stage.stageWidth - btn.width) - 10; btn.y = stage.stageHeight - 30; _call.addChild(btn); addChild(_call); } // depress call button and call UserName.loggedInAs method private function callDownHandler(event:MouseEvent):void { _call.removeEventListener(MouseEvent.MOUSE_DOWN, callDownHandler); stage.addEventListener(MouseEvent.MOUSE_UP, callUpHandler); _call.x += 2; _call.y += 2; _gateway.call("UserName.loggedInAs",_responder); _display.appendText("\n Calling UserName.loggedInAs ... "); } // reset call button private function callUpHandler(event:MouseEvent):void { _call.addEventListener(MouseEvent.MOUSE_DOWN, callDownHandler); stage.removeEventListener(MouseEvent.MOUSE_UP, callUpHandler); _call.x -= 2; _call.y -= 2; } // connect to AMFPHP gateway private function initGateway():void { _gateway = new NetConnection(); // Edit the following line to reflect your server configuration _gateway.connect("http://***MOODLEROOT***/lib/amfphp/gateway.php"); _gateway.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); _responder = new Responder(onResult,onFault); } // show returned results private function onResult(res:Object):void { _display.appendText("\n " + String(res)); } // show details if call is unsuccessful private function onFault(res:Object):void { for(var i:String in res) { _display.appendText("\n " + String(res[i])); } } // show all net status events (can be status or error events) function netStatusHandler(event:NetStatusEvent):void { for(var i:String in event.info) { _display.appendText("\n" + String(event.info[i])); } } } }
AMF Moodle Service Library
AMFPHP Service Browser
AMFPHP 1.9.beta comes with a Flex service browser ready installed. It allows you to see your library of services in the ***MOODLEROOT***/lib/amfphp/services/ directory and call them. It also displays error messages very well. It's an ideal tool for checking out your services before your write Flash and Flex applications that call them.
VERY IMPORTANT!
DO NOT leave the service browser installed on a production server (i.e. public). It will leave your service library and therefore your Moodle API and databases exposed to the public!
List Of AMF Moodle Services
- User.php - returns user login status, returns an array of config settings you can set, returns a value of a config setting, sets a value of a config setting
- UserName.php - returns user login status
User.php Updated
If you've already seen the tutorials in Development:AMFPHP, here's an updated version of User.php that works with AMFPHP 1.9.beta. As with UserName.php, upload it tothe services directory at ***MOODLEROOT***/lib/amfphp/services/. You can test it by navigating to ***MOODLEROOT***/lib/amfphp/browser/index.html and selecting User from the menu of services.
User.php Script
<?php class User { public function __construct() { } /** * Returns string indicating whether a user is logged in. * @access remote */ public function loggedInAs() { global $USER; if (isguestuser()){ return get_string('loggedinasguest', 'moodle'); }else if (isloggedin()){ return get_string('loggedinas', 'moodle', fullname($USER)); } else { return get_string('loggedinnot'); } } /** * Returns an array of config settings you can set. * @access remote */ public function configSettings() { return array_keys(get_user_preferences()); } /** * Returns a value of a config setting. * @param string name * @access remote */ public function getConfigSetting($name) { $name = clean_param($name, PARAM_ALPHAEXT); return get_user_preferences($name); } /** * sets a value of a config setting. * @param string name * @param string value * @access remote */ public function setConfigSetting($name, $value) { $name = clean_param($name, PARAM_ALPHAEXT); $value = clean_param($value, PARAM_NOTAGS); return set_user_preference($name, $value); } } ?>
MDL.php
The following is a simple AMFPHP service that retrieves current user session data from Moodle. Save this file in the services directory of AMFPHP, i.e. moodle/lib/amfphp/services/MDL.php.
This class is PHP5 Object Oriented code and acts as a handy repository for accessing user session data. I haven't forgotten the closing ?> PHP tag. It just isn't recommended for OOP PHP5 classes. (It probably should use getter and setter methods of Moodle's API... but I can't find the relevant documentation!)
MDL.php script
<?php class MDL { public $logged_in = false; public $course_id; // course id of current login public $course_name; // course name public $user_id; // current user's id public $data_path; // URL to moodledata directory via moodle/file.php public function __construct() { global $CFG; require_once('../../../config.php'); if (isguestuser()){ $this->logged_in = true; }else if (isloggedin()){ $this->logged_in = true; global $USER; global $SESSION; // set variables $this->course_id = $SESSION->cal_course_referer; $this->course_name = 'Not yet set!'; // TODO - find out how to get this $this->user_id = $USER->id; $this->data_path = $CFG->wwwroot . '/file.php/' . $this->course_id . '/'; } else { $this->logged_in = false; } } /** *cleans up objects and variables for garbage collector *@returns nothing */ public function __destruct() { unset($this->logged_in); unset($this->course_id); unset($this->course_name); unset($this->user_id); unset($this->data_path); } }
You can create an instance of this class like this:
<?php class YourClassName { private $mdl; public function __construct() { // create moodle authentication and user variables object require('MDL.php'); $this->mdl = new MDL(); }
You can then access the user session data like this:
$this->mdl->course_id; $this->mdl->course_name; $this->mdl->user_id; $this->mdl->data_path;
Useful Links
AMFPHP
- AMFPHP home page amfphp.org
- AMFPHP code repository SourceForge.net
AMF Moodle
Sir Lee Brimelow's video tutorials
Learn how to install AMFPHP and create a simple service that sends email.
In this tutorial he shows you how to directly return database records to Flash using AMFPHP.
Alessandro Crugnola's tutorials
- Flex RemoteObject and AMFPHP 1.9
- Send and Receive ByteArray to AMFPHP
- Flex2/Actionscript3.0 and AMFPHP
Adobe ActionScript 3.0 Language Reference
The two most important classes related to AMFPHP are:
Other useful resources
MISC AMFPHP/Flex/AS3/AIR Tutorials
AS3/AMF
- How to set up a simple connection from Actionscript 3 to call your services in AMFPHP
- Flash Remoting with AS3
- Writing private attributes to a database using AMF in AS3 (IExternalizable)
- Communication between Flash and AMFPHP made easy
- AMFPHP in Flash CS3 with AS3 - jQuery.post style
- Writing private attributes to a database using AMF in AS3
- Turning Flash CS3 assets into Flex components using Flex Component Kit
Sample Flex,AS3(&AIR) Sourcecode & Applications
Misc Example AMFPHP Framework Examples
- Combining Drupal, AMFPHP, SWFAddress and Flash
- Displaying Paypal IPN payment data in Adobe Flex via AMFPHP
AS3 APIS
AMFPHP Is Fast
There's always someone who'll play with a new toy and push it until it breaks. AMFPHP can go pretty fast! Check out this blog article.
See Also
Moodle 2.0 Webservices - PLease refer to WS TRACKER 2.0 for updates on forthcoming AMF service for use with Moodle
Development:SWF - SWF Activity Module deploys Flash and Flex Framework (.swf) applications as learning interactions. Unlike the Flash Activity Module, it supports all versions of Flash and Flex Framework, XML, 3rd Party APIs and will eventually support Flash Remoting (AMF).