Step 12 – Revisiting the API – Flushing the Buffers

On reflection, in Step 10 I may have been a little quick to say that I could ignore the LMSCommit() and LMSFinish() API calls. Even though I don’t have my own buffering system, I’m essentially taking advantage of AJAX to do this for me by using it in synchronous mode. So I do need to make sure that any AJAX calls are completed before the course finishes.

What I’m going to do is to modify the LMSCommit() and LMSFinish() calls in the api.html file by including calls to LMSGetValue() with an empty variable name. This means that LMSCommit() and LMSFinish() will not return their completion value (‘true’) to the calling program until the LMSGetValue(”) AJAX request has been completed.

function LMSCommit(dummyString) {
  LMSGetValue('');
  return "true";
}

function LMSFinish(dummyString) {
  LMSGetValue('');
  return "true";
}

And then I modify the getValue.php code to process the call when there’s no variable name provided (not strictly necessary, but it doesn’t hurt).

<?php 

/*

VS SCORM - getValue.php 
Rev 1.1 - Friday, July 03, 2009
Copyright (C) 2009, Addison Robson LLC

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, 
Boston, MA 02110-1301, USA.

*/

//  database login information
require "config.php";

// connect to the database
$link = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname,$link);

// read GET variable
$varname = trim($_REQUEST['varname']);

// if the varname variable is set ...
if ($varname) {

  // make safe for database
  $safevarname = mysql_escape_string($varname);
  $varvalue = "";

  // read data from the 'scormvars' table
  $result = mysql_query("select varValue from scormvars where (varName='$safevarname')",$link);
  list($varvalue) = mysql_fetch_row($result);

}

// return value to the calling program
print $varvalue;

?>

Now, I run my test course again to see what happens … and it works, so I certainly haven’t broken anything! Now to move on to the SCORM data model.

This entry was posted in Run Time Environment. Bookmark the permalink.