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

Flash module

From MoodleDocs
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Set-by-step guide to using the Flash module

This is a step-by-step guide for using the Flash module to record ‘score’ and ‘time taken’ in the Gradebook. It assumes that you have already created a Flash activity, and that there is already a variable within this activity that records the score.

Please note that the Flash code is ActionScript 2.0 and definitely works for Flash movies published for Flash Player 6. It may, however, be buggy or not work at all if published for later versions of Flash Player.

Step 1. Download and install the Flash module.
Step 2. Create a new directory in the 'mod/flash/movies' folder. Give it the same name as your Flash activity.
Step 3. Save or copy your flash activity in this directory (only the .swf file is required).
Step 4. In this same directory, create a Php file called "service.php". In this file, copy and paste the code given below.
Step 5. In Macromedia Flash, you need to add some code to communicate the time and score to Moodle. You can also insert answers given to questions or some other text which will show up on the results page. In the code below, just replace 'yourScoreVariable' with whatever variable you use for scoring, and do the same for yourAnswersVariable or if you have none, just leave it as it is. Finally, you need to have a 'finish' button which when clicked on will send the data to Moodle and take you to the results page. You need to call this button 'finishButton' (in the Properties panel). The following code can be placed in the first frame of your Flash activity.
 score = yourScoreVariable; // the final score, to test just insert any number
 answers = yourAnswersVariable; // optional text to go in results page, to test just insert any text "like this"

 startTime = getTimer(); // start timer

 _root.finishButton.onRelease = function() {
     endTime = getTimer() - startTime;
     moodleService.send_response(answers, endTime, score);
     moodleService.cleanUp(); // goes to results page
 }

 moodleService.init();
Step 6. Now add the activity to Moodle (Add an activity -> Flash Activity). Your Flash activity should be in the ‘movie’ list.
Step 7. In the settings, make sure that “Use a preloader?” is set to “Yes”. The preloader sets up the necessary code to make things work.

service.php code

Note: the code below is a slightly modified and stripped down version from Jamie's tutorial). For clarification about the code and to find out what else you can do with it, have a look at the tutorial.

<?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)   {
       //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 $message
    * @param integer $time time in milliseconds it took to answer
    * @param integer $score
    */
   function send_response($message, $time, $score){
           flash_save_response($this->fromcredentials['accessid'],$this->fromcredentials['courseid'], $this->fromcredentials['cmid'],1,
                        array('answer'=>$message, 'time'=>intval($time)/1000), $score);
           //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
   }
}
?>

See also