Note: You are currently viewing documentation for Moodle 1.9. Up-to-date documentation for the latest stable version is available here: AMFPHP.

Development:AMFPHP

From MoodleDocs

What is AMFPHP

AMFPHP is a widely used OS remoting server that allows your Flash movies to send and receive data to and from your web server and interact with your PHP scripts. Data to/from PHP is automatically converted into the appropriate Flash variable type and communication is fast because the format data is sent in is compact and is handled efficiently by the Flash player (large XML files can sometimes take time to be processed).

Using AMFPHP with Moodle

  • I downloaded AMFPHP 1.2.6 and installed it in my moodle site under MOODLEROOT/lib/amfphp/ So that gateway.php is at MOODLEROOT/lib/amfphp/gateway.php
  • In gateway.php I :
    • added include_once "../../config.php"; to the beginning of the file.
    • I edited the call to $gateway->setBaseClassPath so it reads $gateway->setBaseClassPath($CFG->libdir."/amfphp/services/");

Installing Flash Remoting Components in Your Flash Editor

You must download an extension to the Flash editor here. This installs the necessary code libraries for Flash to communicate with a Flash remoting server. Restart your Flash editor after installing the components. You should then see NetConnection Debugger under the Window menu->Other Panels. If necessary more info can be found on the amfphp site here.

Testing, testing, 1, 2, 3

Test service

  • Create a new file under lib/amfphp/services/ called UserName.php
  • Copy and paste the following code :
<?php
class UserName {
    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');
        }
    }
}
?>

Notice we are using the Moodle API here (the full Moodle API is available to us!) and also accessing the SESSION data for the currently logged in user in order to fetch their user name. I recommend the use of eclipse for editing your code or vi, you can find info about using both of these editors on Moodle docs and you will find info about browsing the Moodle source code which is the best way to become familiar with the moodle api. In particular see files lib/dmllib.php, lib/moodlelib.php, lib/access.php and possibly lib/weblib.php

  • Open MOODLEROOT/lib/amfphp/browser/ in your browser. You should see UserName listed in the left pane of the service browser.
  • Hit 'mt' to automatically generate code for a method table to tell AMFPHP what methods are available in your class and their access level. The generated code will look like this :
$this->methodTable = array(
	"loggedInAs" => array(
		"description" => "No description given.",
		"arguments" => array(),
		"access" => "private"
	)
);
  • As the service browser says we must copy and paste this code into the constructor of our service class. Our service class code will look like this after adding a constructor function and editing the automatically generated method table :
<?php
class UserName {
    function UserName(){
        $this->methodTable = array(
        "loggedInAs" => array(
            "description" => "Returns string indicating whether user is logged in.",
            "arguments" => array(),
            "access" => "remote"
            )
        );
    }
    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');
        }
    }
}
?>

Test Movie

To create a test movie that will access this service function then :

  • open the Flash IDE.
  • make sure you have installed the remoting extensions as detailed as above.
  • add a 'dynamic' text box with instance name 'displaytext' to the stage on Frame 1.
  • open the service browser at MOODLEROOT/lib/amfphp/browser/ and click on 'code'
  • in the text box at the bottom of the page enter UserName and then press 'Save to Disk'. If this doesn't work it is because your web server does not have write access to write to the directory, instead you can copy and paste the code into an editor and save it as MOODLEROOT/lib/src/UserName/UserName.as
  • edit the code in MOODLEROOT/lib/src/UserName/UserName.as and you need to tell the service what to do when Moodle returns it's results. Replace the handleGet function with :
	function handleGet(re:ResultEvent)
	{
		_root.displaytext.text = re.result;
	}
  • Copy and paste this code onto frame 1.
import UserName;
un = new UserName();
un.loggedInAs();