Flash module add-on: Accessing globals from flash (moodle 2.0)

Flash module add-on: Accessing globals from flash (moodle 2.0)

by Christian Peters -
Number of replies: 2
Hi folks,

i'm experimenting with zend_amf; trying to push my moodle 1.x coding to the new version.

Thus I wrote an own service in lib/moodle/zend/Zend/Service.

I've manged to establish connection and I can exchange all kinds of vars with zend, but I fail to access globals.

I've expierenced a lotof problems with including config.php:

http://moodle.org/mod/forum/discuss.php?d=152063#p665091

Now I've included config.php directly into my gateway.

My gateway is located in lib/moodle/zend/Zend/Service/FlashAPI and works fine for the exchange:

<?php
require_once "../../../../../config.php";
require_once '/../../Amf/Server.php';
require_once 'CourseControl.php';

$server = new Zend_Amf_Server();
$server->setClass('CourseControl');

echo($server->handle());
?>

CourseControl a class, that contains lot's of functions that come in handy when working with moodle, one of them is:

/**
* Returns global $USER
*
* @return $USER object in actionscript-ready charset
*/
public function get_user() {
 
global $USER;

$myUser = $this->switch_object_to_as3_charset($USER);
return $myUser;
}

Now my (shortened) as3 code:

import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.Responder;


var nc:NetConnection = new NetConnection();
nc.connect("http://localhost/moodle/lib/zend/Zend/Service/FlashAPI/Gateway.php");

nc.call("CourseControl.get_user", new Responder(onResult, onFault));

function onResult(res):void {
test_txt.text = res.firstname;
}
 

function onFault(res:Object):void {
test_txt.text = "Error";
}

This gives me an as3 error of
TypeError: Error #2007: Parameter text cannot be 'null'.
at flash.text::TextField/set text()
at cyclicks_fla::MainTimeline/onResult()

If I give back a custom object with a firstname in it, everything works, so does when I write a test.php.

My guess is, that it has something to do with how the config.php is included.

Anyone with experience in this topic?

Greetz!
Average of ratings: -
In reply to Christian Peters

Re: Flash module add-on: Accessing globals from flash (moodle 2.0)

by Christian Peters -
I've done some debugging.

By deleting config.php include from gateway.php, I receive NetConnection.Call.BadVersion Error (that is expected, cause $USER becomes null and that causes some fatal errors in my php-class)

If I include it in CourseControl.php BEFORE the class definition, Flash does nothing (not even throw an error). But now a test.php works fine:

<?php
require_once('CourseControl.php');
$test = new CourseControl();
$userobj = $test->get_user();
echo($userobj->firstname);
?>

By including it directly into get_user, Flash throws NetConnection.Call.Failed & test.php throws - strangely - Fatal error: $CFG->dataroot is not specified in config.php! Exiting.
In reply to Christian Peters

Re: Flash module add-on: Accessing globals from flash (moodle 2.0)

by Christian Peters -
I'm not sure why, but now it works. Strange, I'll do some research.