Flash module add-on: Getting flash games to send scores to Moodle

Flash module add-on: Getting flash games to send scores to Moodle

by John Rutherford -
Number of replies: 6

I run a website which, amongst other things, allows people to create free flash quiz games by simply filling in the questions and answers they want. It does not make an actual swf for each individual game - the quiz data is stored in a mysql database and requested when the game loads. We have managed to get embed codes working so that people can add the completed games to websites, blogs and Moodles.

An example of one of these user-generated games can be seen at http://www.what2learn.com/games/play/6298/

There are lots of different interactive learning games for people to pick from. The next step for us is obviously to add functionality so that scores could be sent to a student's gradebook within Moodle. I am hopeful that this tool (which will always remain free) could help teachers to make their Moodles more exciting and useful for their students. I have started researching this and have come across the following...

http://docs.moodle.org/en/Flash_module

Is there anybody out there with a bit of experience in this who could lend a hand? Is the Flash Module above the best way to go about this?

Average of ratings: -
In reply to John Rutherford

Re: Flash module add-on: Getting flash games to send scores to Moodle

by Dmitry Pupinin -
Picture of Core developers Picture of Plugin developers
Hi John!
Several days ago I finished first stage of my new project - Flash Question. smile
It's a new question type which allow store flash's data in Moodle's database, show student's attempts to teacher. This support all features which support any standard question types.

Additional features: allow to use Flash quizes created for AuthorWare, Questionmark's support going to come. wink

It's not described yet on docs.moodle.org, but description of programm interface and example of flash included in package.

Get latest: http://download.moodle.org/download.php/plugins/question/type/flash.zip
Demo site: http://train.nspu.ru/course/view.php?id=32
Welcome all!
In reply to Dmitry Pupinin

Re: Flash module add-on: Getting flash games to send scores to Moodle

by Matt Bury -
Picture of Plugin developers
Hi John, Dmitry,

We should get together! I've written a couple of modules for deploying Flash in Moodle:

http://code.google.com/p/moodle-flv-player/ (JW FLV Player module)

http://code.google.com/p/moodle-swf/ (deploys any Flash app.)

You can see them in action here (use guest access):

http://matbury.com/moodle/course/view.php?id=9

I started the SWF module as an update for Jamie Pratt's FAM (Flash activity module). The main problems with the FAM is that it's overly complicated and it isn't forwardly compatible (The FAM was written some time ago, for Flash Player 6 MX 2004). The SWF module supports any version of Flash and could conceivably be adapted to support Silverlight and Java apps. too. The only thing remaining is to include a script to send student results to the gradebook. I'm also going to include database support by leveraging AMFPHP (the demos on my site use this). Future versions should use Zend_AMF for Flash remoting to be compatible with Moodle 2.0's core library code.

I'm a Flash developer and not very good at PHP and I don't know Moodle core code very well at all. Any help with this project would be great. You can check out the code at Google Code and if you want, I can include you in the project so you can contribute to it.

Looking forward to your replies.

All the best,

Matt

P.S. Please post messages to me on the Google Code project pages - I'll get a notification much faster from there.
In reply to Matt Bury

Re: Flash module add-on: Getting flash games to send scores to Moodle

by boule bill -
Hi Matt,

Your module is really great and I'd like to use it properly for my projects.
I've just one problem, I'd like to pass the current user name to my game by flashvars but I don't know how I can do that. Is it possible to transfer dynamic values from moodle into a flash game by flashvars ?

Regards,

Gaël
In reply to boule bill

Re: Flash module add-on: Getting flash games to send scores to Moodle

by Matt Bury -
Picture of Plugin developers
Hi Gaël,

Thanks for your interest in the SWF Activity Module. It doesn't currently allow user data to be passed between the server and client. I think that this is generally a bad idea for security reasons.

I handle all the user data on the server with Flash Remoting so that no sensitive information will ever be passed back and forth over the WWW:

http://docs.moodle.org/en/Development:AMF3

This allows Flash clients to communicate directly with Moodle's back end and do just about anything you like. Something like retrieving user data would be very easy and there are some examples included on the Moodle docs page on the link above. I designed the SWF Activity Module with both Flash Remoting and XML in mind so if you intend to do a lot of database driven work with Flash it's an ideal way to get started.

On the other hand,

If all you want to do is have the user's name appear in your Flash app., then you can customise the moodle/mod/swf/lib.php page to pass that data in as FlashVars. It's only a few lines of code that you need to add. Remember to make the changes in the code in both the swf_print_header_js() and swf_print_body() functions.

I've highlighted the modified code.

I hope this answers your questions. Don't hesitate to contact me if you have any further questions. I'm always looking for ways to improve the module.

Good luck,

Matt

/**
* Construct Javascript SWFObject embed code for <head> section of view.php
* Note: '?'.time() is used to prevent browser caching for XML and SWF files.
*
* @param $swf (mdl_swf DB record for current SWF module instance)
* @return string
*/
function swf_print_header_js($swf) {
global $CFG;
global $COURSE;
global $USER; // add the USER object which contains username

$swf->username = $USER->username; // add username variable to swf object, it then gets passed in in the JavaScript code below and in the object and embed tags in the swf_print_body() function
 
// Build URL to moodledata directory
// This is where SWF files and media should be stored
$swf_moodledata = $CFG->wwwroot.'/file.php/'.$COURSE->id.'/';
$swf_coursepage = $CFG->wwwroot.'/course/view.php?id='.$COURSE->id;
 
// Build Javascript code for view.php print_header() function
$swf_header_js = '<script type="text/javascript" src="swfobject/swfobject.js"></script>
<script type="text/javascript">
var flashvars = {};
flashvars.gateway = "'.$CFG->wwwroot.'/lib/amfphp/gateway.php";
flashvars.swfid = "'.$swf->id.'";
flashvars.instance = "'.$swf->moduleid.'";
flashvars.interaction = "'.$swf->interaction.'";
flashvars.moodledata = "'.$swf_moodledata.'";
flashvars.coursepage = "'.$swf_coursepage.'";
flashvars.xmlurl = "'.$swf_moodledata.$swf->xmlurl.'?'.time().'";
flashvars.apikey = "'.$swf->apikey.'";
flashvars.flashvar1 = "'.$swf->flashvar1.'";
flashvars.flashvar2 = "'.$swf->flashvar2.'";
flashvars.flashvar3 = "'.$swf->flashvar3.'";
flashvars.username = "'.$swf->username.'";
var params = {};
params.play = "'.$swf->width.'";
params.loop = "'.$swf->loop.'";
params.menu = "'.$swf->menu.'";
params.quality = "'.$swf->quality.'";
params.scale = "'.$swf->scale.'";
params.salign = "'.$swf->salign.'";
params.wmode = "'.$swf->wmode.'";
params.bgcolor = "#'.$swf->bgcolor.'";
params.devicefont = "'.$swf->devicefont.'";
params.seamlesstabbing = "'.$swf->seamlesstabbing.'";
params.allowfullscreen = "'.$swf->allowfullscreen.'";
params.allowscriptaccess = "'.$swf->allowscriptaccess.'";
params.allownetworking = "'.$swf->allownetworking.'";
var attributes = {};
attributes.id = "contentid";
attributes.align = "middle";
swfobject.embedSWF("'.$swf_moodledata.$swf->swfurl.'?'.time().'", "myAlternativeContent", "'.$swf->width.'", "'.$swf->height.'", "'.$swf->version.'", "swfobject/expressInstall.swf", flashvars, params, attributes);
</script>';
 
return $swf_header_js;
}

/**
* Construct Javascript SWFObject embed code for <body> section of view.php
* Note: everything between the <div id="myAlternativeContent"></div> tags
* is overwritten by SWFObject. This embed code will only be used if SWFObject
* fails for some reason, e.g. Javascript isn't enabled. In any case, the module
* should function normally.
*
* @param $swf (mdl_swf DB record for current SWF module instance)
* @param $cm module instance data
* @return string
*/
function swf_print_body($swf) {
global $CFG;
global $COURSE;
global $USER;

$swf->username = $USER->username;
 
// Build URL to moodledata directory
$swf_moodledata = $CFG->wwwroot.'/file.php/'.$COURSE->id.'/';
$swf_coursepage = $CFG->wwwroot.'/course/view.php?id='.$COURSE->id;
 
// prevent using SWF file in browser cache by attaching time as query string
$swf_swfurl = $swf_moodledata.$swf->swfurl.'?'.time();
$swf_xmlurl = $swf_moodledata.$swf->xmlurl.'?'.time();
// e.g. http://yourmoodlesite.com/file.php/99/swf/flash_file.swf?123513670
 
// Build URL to AMFPHP gateway for Flash Remoting
$swf_gateway = $CFG->wwwroot.'/lib/amfphp/gateway.php';
// e.g. http://yourmoodlesite.com/lib/amfphp/gateway.php
//instance = "'.$cm->id.'
$swf_body = '<div align="center">
<div id="myAlternativeContent">
<div>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'.$swf->width.'" height="'.$swf->height.'" id="contentid" align="'.$swf->align.'">
<param name="movie" value="'.$swf_swfurl.'" />
<param name="play" value="'.$swf->play.'" />
<param name="loop" value="'.$swf->loopswf.'" />
<param name="menu" value="'.$swf->menu.'" />
<param name="quality" value="'.$swf->quality.'" />
<param name="scale" value="'.$swf->scale.'" />
<param name="salign" value="'.$swf->salign.'" />
<param name="wmode" value="'.$swf->wmode.'" />
<param name="bgcolor" value="#'.$swf->bgcolor.'" />
<param name="devicefont" value="'.$swf->devicefont.'" />
<param name="seamlesstabbing" value="'.$swf->seamlesstabbing.'" />
<param name="allowfullscreen" value="'.$swf->allowfullscreen.'" />
<param name="allowscriptaccess" value="'.$swf->allowscriptaccess.'" />
<param name="allownetworking" value="'.$swf->allownetworking.'" />
<param name="flashvars" value="gateway='.$swf_gateway.'&amp;swfid='.$swf->id.'&amp;interaction='.$swf->interaction.'&amp;instance='.$swf->moduleid.'&amp;moodledata='.$swf_moodledata.'&amp;coursepage='.$swf_coursepage.'&amp;swfurl='.$swf_swfurl.'&amp;xmlurl='.$swf_xmlurl.'&amp;apikey='.$swf->apikey.'&amp;flashvar1='.$swf->flashvar1.'&amp;flashvar2='.$swf->flashvar2.'&amp;flashvar3='.$swf->flashvar3.'&amp;username='.$swf->username.'" />
<!--[if !IE]>-->
<object type="application/x-shockwave-flash" data="'.$swf_swfurl.'" width="'.$swf->width.'" height="'.$swf->height.'" align="'.$swf->align.'">
<param name="play" value="'.$swf->play.'" />
<param name="loop" value="'.$swf->loop.'" />
<param name="menu" value="'.$swf->menu.'" />
<param name="quality" value="'.$swf->quality.'" />
<param name="scale" value="'.$swf->scale.'" />
<param name="salign" value="'.$swf->salign.'" />
<param name="wmode" value="'.$swf->wmode.'" />
<param name="bgcolor" value="#'.$swf->bgcolor.'" />
<param name="devicefont" value="'.$swf->devicefont.'" />
<param name="seamlesstabbing" value="'.$swf->seamlesstabbing.'" />
<param name="allowfullscreen" value="'.$swf->allowfullscreen.'" />
<param name="allowscriptaccess" value="'.$swf->allowscriptaccess.'" />
<param name="allownetworking" value="'.$swf->allownetworking.'" />
<param name="flashvars" value="gateway='.$swf_gateway.'&amp;swfid='.$swf->id.'&amp;interaction='.$swf->interaction.'&amp;instance='.$swf->moduleid.'&amp;moodledata='.$swf_moodledata.'&amp;coursepage='.$swf_coursepage.'&amp;swfurl='.$swf_swfurl.'&amp;xmlurl='.$swf_xmlurl.'&amp;apikey='.$swf->apikey.'&amp;flashvar1='.$swf->flashvar1.'&amp;flashvar2='.$swf->flashvar2.'&amp;flashvar3='.$swf->flashvar3.'&amp;username='.$swf->username.'" />
<!--<![endif]-->'.get_string('embederror','swf').'
<div align="center">
<p><strong>This activity requires <a href="http://www.adobe.com/products/flashplayer/">Flash Player '.$swf->version.'</a> to be installed.</strong></p>
<p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" border=0/>
</a>
</p>
</div>
<!--[if !IE]>-->
</object>
<!--<![endif]-->
</object>
</div>
</div>';
 
return $swf_body;
}

In reply to Matt Bury

Re: Flash module add-on: Getting flash games to send scores to Moodle

by boule bill -
Hi Matt,

Your trick works fine on Windows ! Thanks smile
But my flash game seems to be not working on Ubuntu (firefox 3.0.11) with your latest release and unfortunately I don't know where I can write this issue. (somewhere in http://code.google.com/p/moodle-swf/ ???)

Regards,

Gaël
In reply to boule bill

Re: Flash module add-on: Getting flash games to send scores to Moodle

by Matt Bury -
Picture of Plugin developers
Hi Gaël,

Yes, it's best to put any specific questions about the module in the tracker on the Google Code hosting site.

Re: the Ubuntu thing, it could be a simple ID string issue in the embed code. I think Flash requires an extra ID string that Windows and OSX don't. Could you post the issue in the tracker at:

http://code.google.com/p/moodle-swf/issues/list

...and write a clearer description of what is going wrong?

Thanks! smile