Step 9 – Processing Requests on the Server

My last couple of posts talked about setting up the SCORM RTE API to submit requests to the LMS server using AJAX. Now I have to set up something on the server end to receive and process those requests. Remember that I’m not talking about a real LMS server here – just something to mimic the most primitive behavior of a SCORM-compliant LMS.

The two server-side functions that I need to put in place are ‘getValue.php’ and ‘setValue.php’ which are called by the LMSGetValue() and LMSSetValue() API functions respectively. These are going to access the database table that I set up earlier, so I’m going to start by creating a PHP file called ‘config.php’ which will contain my database login information.

<?php 

$dbname = "my database name";
$dbhost = "my database host";
$dbuser = "my database username";
$dbpass = "my database password";

?>

Note that I’m using PHP for the server-side functions, but it shouldn’t be too difficult to reproduce it using another language. Now I’m going to create the ‘getValue.php’ code. Nothing very special here – it simply reads the variable name contained in the GET data, reads a value from the database (if set), and returns the value to the calling program.

<?php 

/*

VS SCORM - getValue.php 
Rev 1.0 - Wednesday, June 10, 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 = $_REQUEST['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;

?>

‘setValue.php’ is slightly more complicated. It reads the variable name from the GET data (as above) and the variable value from the POST data. It then deletes any existing data from the database before inserting a new record for that variable. Finally, it returns the value “true” to the calling program to indicate that it’s worked.

<?php 

/*

VS SCORM - setValue.php 
Rev 1.0 - Wednesday, June 10, 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 and POST variables
$varname = $_REQUEST['varname'];
$varvalue = $_REQUEST['varvalue'];

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

// save data to the 'scormvars' table
mysql_query("delete from scormvars where (varName='$safevarname')",$link);
mysql_query("insert into scormvars (varName,varValue) values ('$safevarname','$safevarvalue')",$link);

// return value to the calling program
print "true";

?>

Nothing too complex there. Next step – review the other API functions to see if I need to do anything with them.

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

1 Response to Step 9 – Processing Requests on the Server

  1. Pingback: Desarrollando un LMS(con soporte de SCORM) « Jorge Dieguez Blog

Leave a Reply

Your email address will not be published. Required fields are marked *