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"
//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
<?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";
}
}
}
?>
- 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.
- 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.
- 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.
- 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)
- Then onResult_send_response displays the feedback from php and displays again the sendbutton this time with a label 'Finish'
- 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.