Flash module add-on: Making interactive learning activities with Adobe Flash and the Flash Activity Module - a step by step tutorial part 2

Flash module add-on: Making interactive learning activities with Adobe Flash and the Flash Activity Module - a step by step tutorial part 2

by Jamie Pratt -
Number of replies: 7

I've created a new file in the mod/flash download that is a library of useful functions to call from service.php which I hope will be added to over time. This example uses this library. You will need to get the latest version of the Flash module to use this or get the library file from here and add it to flash/movies/


Find attached the files for this movie.

Sending answers and grades to Moodle

 Next we'll learn how to send answers back to Moodle and store grades for an activity.

Attached is a demo movie that does this and the code is below.

There is only one frame to the Flash movie and it has the following objects on it :

  • a dynamic textfield called 'textfield1'
  • an input textfield called 'response'
  • a dynamic textfield called 'feedback' with nothing in it.
  • a movie clip called "sendbutton" with a textfield inside it called "buttonlabel"
The following code is on the first frame of the movie :


//this function responds to the return call from Moodle after init is called.
//Moodle passes data back to this function as properties of moodleService
//in this case their is one propery moodleService.question
moodleService.onInit=function(){
    _root.textfield1.text = moodleService.question;
    //start timer after we have displayed question so 
    //that we can tell the time difference later
    startTime = getTimer();
}
//this function is triggered by pressing the sendbutton 
_root.sendbutton.onRelease = function() {
    //we send the answer off to the server and it is checked there
    //we also send the time it took to answer the question
    moodleService.send_response(_root.response.text, ((getTimer()-startTime)));
    //we want to stop the user editing his response after the question has 
    //been sent
    _root.response.type='dynamic';//stop editing
    //we will stop displaying this button for now
    this._visible=false;
    
}
moodleService.send_response_onResult=function(feedback){
    _root.feedback.text = feedback;
    //redisplay the send button for a different purpose
    _root.sendbutton._visible=true;
    //now the send button is used to finish the movie
    _root.sendbutton.buttonlabel.text='Finish';
    //when a movie is finished then you should cleanup your flash session 
    //with this function when possible, this will also send the user back to the
    //results page for this activity:
    _root.sendbutton.onRelease=moodleService.cleanUp;
}
//here we now call init
//(note nothing else has been called up to now on the first frame, we've just been setting up
//up some functions to handle returned results later
moodleService.init();
//init calls the service class constrcutor service()
//then when that returns moodleService.onInit() is called


On the server side the code looks like this :

<?php
global $CFG;
require("$CFG->dirroot/mod/flash/movies/library.php");
class service  {
    var $fromcredentials;
    /**
     * This function is called when you call init() from Flash
     *
     * @param array $this->fromcredentials is an array of info about this activity passed from Moodle
     */
    function service($fromcredentials)   {
        //this will allow you to access the first name of the user
        global $USER;
        $this->question=$USER->firstname.", isn't this great?";
        //save array passed from Moodle
        //this array includes the course id and cmid etc. This is not from the flash movie.
        //you may pass parameter to this function call from the flash movie using 
        //moodleService.init(param1, param2) you can only pass upto 2 params
        //when you pass params for example 2 then you would write this function as :
        // function service($param1, $param2, $fromcredentials) 
        $this->fromcredentials=$fromcredentials;
    }
    /**
     * Used to receive a response from Flash to PHP. When this function returns and data is received by Flash 
     * then onResult_send_response() is called in Flash with one parameter - the returned value
     *
     * @param string $response
     * @param integer $time time in milliseconds it took to answer
     */
    function send_response($response, $time){
        if ($response=="yes"){
            flash_save_response($this->fromcredentials['accessid'],$this->fromcredentials['courseid'], $this->fromcredentials['cmid'],1,
                         array('answer'=>$response, 'time'=>intval($time)/1000), 100);
            //the answer was correct we'll return text feedback.
            //if you want to return more than one param then you can use an array or object to return them in
            //nested arrays and objects work fine for returning complex object / array structures
            return "Correct";
                         
        } else {
            flash_save_response($this->fromcredentials['accessid'],$this->fromcredentials['courseid'], $this->fromcredentials['cmid'],1,
                         array('answer'=>$response, 'time'=>intval($time)/1000), 0);
            return "Wrong";
                          
            
        }
        
        
    }
}
   
?>
 I hope this example is fairly self explanatory. Try to follow through what is happening.


  1. the Flash movie is displayed and the actionscript on the first frame does nothing else initially except to call moodleService.init(). This is a call to the special 'constructor' function the service class 'service()' No parameters are passed from Flash to the function call.
  2. The service() function in php is triggered. This fetches the user name as before and constructs the question text including the username. Notice their is an extra param that comes from Moodle to this function that didn't get passed from Flash, we've called it '$fromcredentials' this is an array that tells us alot about the instance of the Flash activity running.
  3. When the service function finishes then the code on the first frame of the Flash movie onInit() is triggered. At this point the properties set in the service function have been set and flash can access the data that has been passed from php. We use the onInit function to write the question to the text field.
  4. When the sendbutton is pressed we then hide the sendbutton so it can't be pressed again. We make the input field noneditable so that the answer cannot be changed by the user again. We also send off the answer to the server to mark the answer. We call moodleService.send_response() to send the resonse to the server. When this function returns it the returned value is then passed back to the flash movie as a parameter we'll call $feedback to function moodleService.onResult_send_response($feedback)
  5. Then onResult_send_response displays the feedback from php and displays again the sendbutton this time with a label 'Finish'
  6. We call moodleService.cleanUp(); when the finish button is pressed to clean up after this instance of the FLash module activity that has been done and to send the user to the results page.
Average of ratings: -
In reply to Jamie Pratt

Re: Flash module add-on: Making interactive learning activities with Adobe Flash and the Flash Activity Module - a step by step tutorial part 2

by kathy hooper -
Hi Jamie
thanks to the tutorials - I have been trying half pie to sort out flash and moodle for some time, so this is great. The first tutorial went okay but in the second, I am unsure of the library that needs to be added. What is it actually called. Even though I have just updated my flash module from sourceforge, the movie folder contains no library.
cheers
kathy
In reply to kathy hooper

Re: Flash module add-on: Making interactive learning activities with Adobe Flash and the Flash Activity Module - a step by step tutorial part 2

by Jamie Pratt -
oops. It seems I didn't commit library.php to the cvs. It is in there now and should be part of the downloadable zip package in a day.

Jamie
In reply to Jamie Pratt

Re: Flash module add-on: Making interactive learning activities with Adobe Flash and the Flash Activity Module - a step by step tutorial part 2

by kathy hooper -
thanks - its working now
kathy
In reply to Jamie Pratt

Re: Flash module add-on: Making interactive learning activities with Adobe Flash and the Flash Activity Module - a step by step tutorial part 2

by Maxim Kovalenko -

If I am not mistaken, this is AS2. Any chance for getting an AS3 version of this?

Moodle+Flash combination is really handy in delivering e-learning for low-budget situations, where still some visual appeal is required.

In reply to Maxim Kovalenko

Re: Flash module add-on: Making interactive learning activities with Adobe Flash and the Flash Activity Module - a step by step tutorial part 2

by Matt Bury -
Picture of Plugin developers

Hi Maxim,

If yo check the dates on this thread you'll see that they're dated 2006. The FAM wasn't finised and, as far as I know, is no longer supported.

It's successor is the SWF Activity Module: http://code.google.com/p/swf-activity-module/ which is backwardly and forwardly compatible with all versions of Flash and the accompanying service library is compatible with AS 3.0 (Flash Player 9+).

I recommend holding off installing it on a production server for the next week or so because an update is due very soon. This should give you enough time to try it out on your testing server.

I hope this helps! smile

In reply to Matt Bury

Re: Flash module add-on: Making interactive learning activities with Adobe Flash and the Flash Activity Module - a step by step tutorial part 2

by Maxim Kovalenko -

 

Thanks Matt, appreciate the reference. This page - http://docs.moodle.org/en/Flash_module - got me confused. Funnily, Google finds just it, and not the recent version.

In reply to Maxim Kovalenko

Re: Flash module add-on: Making interactive learning activities with Adobe Flash and the Flash Activity Module - a step by step tutorial part 2

by Matt Bury -
Picture of Plugin developers

You're welcome! smile