From feda681f454991a76a9571f661f9dbf921729894 Mon Sep 17 00:00:00 2001 From: jkbockstael Date: Mon, 16 May 2011 15:41:28 +0200 Subject: [PATCH] Storage API : Added a stack-based data storage. --- main/install/db_main.sql | 12 ++ main/install/migrate-db-1.8.6.2-1.8.7-pre.sql | 16 +-- main/newscorm/js/storageapi.js | 102 +++++++++++++++ main/newscorm/storageapi.php | 118 ++++++++++++++++++ 4 files changed, 241 insertions(+), 7 deletions(-) diff --git a/main/install/db_main.sql b/main/install/db_main.sql index 04ee8eb7ba..b8f87538a8 100644 --- a/main/install/db_main.sql +++ b/main/install/db_main.sql @@ -2536,3 +2536,15 @@ CREATE TABLE stored_values ( ); ALTER TABLE stored_values ADD KEY (user_id, sco_id, course_id, sv_key); ALTER TABLE stored_values ADD UNIQUE (user_id, sco_id, course_id, sv_key); + +DROP TABLE IF EXISTS stored_value_stack; +CREATE TABLE stored_values_stack ( + user_id INT NOT NULL, + sco_id INT NOT NULL, + stack_order INT NOT NULL, + course_id CHAR(40) NOT NULL, + sv_key CHAR(64) NOT NULL, + sv_value TEXT NOT NULL +); +ALTER TABLE stored_values_stack ADD KEY (user_id, sco_id, course_id, sv_key, stack_order); +ALTER TABLE stored_values_stack ADD UNIQUE (user_id, sco_id, course_id, sv_key, stack_order); diff --git a/main/install/migrate-db-1.8.6.2-1.8.7-pre.sql b/main/install/migrate-db-1.8.6.2-1.8.7-pre.sql index 7ca231eea7..3aa52b7076 100644 --- a/main/install/migrate-db-1.8.6.2-1.8.7-pre.sql +++ b/main/install/migrate-db-1.8.6.2-1.8.7-pre.sql @@ -134,6 +134,15 @@ CREATE TABLE `event_type_message` (`id` int(11) NOT NULL AUTO_INCREMENT, `event_ CREATE TABLE `user_rel_event_type` (`id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `event_type_id` int(11) NOT NULL, PRIMARY KEY (`id`)); INSERT INTO `event_type` VALUES (1, 'course_deleted','courseDeletedTitle','courseDeletedComment'),(2,'course_created','courseCreatedTitle','courseCreatedComment'),(3,'user_deleted','userDeletedTitle','userDeletedComment'),(4,'user_created','userCreatedTitle','userCreatedComment'), (5, 'session_created','sessionCreatedTitle','sessionCreatedComment'), (6,'session_deleted','sessionDeletedTitle','sessionDeletedComment'), (7,'session_category_created','sessionCategoryCreatedTitle','sessionCategoryCreatedComment'),(8,'session_category_deleted','sessionCategoryDeletedTitle','sessionCategoryDeletedComment'),(9,'settings_changed','settingsChangedTitle','settingsChangedComment'),(10,'user_subscribed','userSubscribedTitle','userSubscribedComment'), (11,'user_unsubscribed','userUnsubscribedTitle','userUnsubscribedComment'); INSERT INTO `event_type_message` (`id`,`event_type_id`, `language_id`, `message`,`subject`) VALUES (1,4,10,'Bonjour, \r\n\r\nL\'utilisateur %username% (%firstname% %lastname%) a été créé.\r\nEmail : %mail%\r\n\r\nBien à vous.',''),(2,1,10,'Delete formation',''),(3,2,10,'Create formation',''),(4,3,10,'Bonjour, \r\n\r\nL\'utilisateur %username% (%firstname% %lastname%) a été supprimé.\r\n\r\nBien à vous.',''),(6,5,10,'Create session test',''),(7,6,10,'Delete session',''),(8,7,10,'Create category session',''),(9,8,10,'Delete category session',''),(10,9,10,'Change setting',''),(11,10,10,'Subscribe',''),(12,11,10,'Unsubscribe',''); +-- +-- Table structure for LP custom storage API +-- +CREATE TABLE stored_values (user_id INT NOT NULL, sco_id INT NOT NULL, course_id CHAR(40) NOT NULL, sv_key CHAR(64) NOT NULL, sv_value TEXT NOT NULL ); +ALTER TABLE stored_values ADD KEY (user_id, sco_id, course_id, sv_key); +ALTER TABLE stored_values ADD UNIQUE (user_id, sco_id, course_id, sv_key); +CREATE TABLE stored_values_stack (user_id INT NOT NULL, sco_id INT NOT NULL, stack_order INT NOT NULL, course_id CHAR(40) NOT NULL, sv_key CHAR(64) NOT NULL, sv_value TEXT NOT NULL ); +ALTER TABLE stored_values_stack ADD KEY (user_id, sco_id, course_id, sv_key, stack_order); +ALTER TABLE stored_values_stack ADD UNIQUE (user_id, sco_id, course_id, sv_key, stack_order); -- xxSTATSxx CREATE TABLE track_e_item_property(id int NOT NULL auto_increment PRIMARY KEY, course_id int NOT NULL, item_property_id int NOT NULL, title varchar(255), content text, progress int NOT NULL default 0, lastedit_date datetime NOT NULL default '0000-00-00 00:00:00', lastedit_user_id int NOT NULL, session_id int NOT NULL default 0); @@ -198,10 +207,3 @@ INSERT INTO course_setting(variable,value,category) VALUES ('email_alert_student ALTER TABLE lp ADD COLUMN hide_toc_frame TINYINT NOT NULL DEFAULT 0; alter table lp_item_view modify column suspend_data longtext; - --- --- Table structure for LP custom storage API --- -CREATE TABLE stored_values (user_id INT NOT NULL, sco_id INT NOT NULL, course_id CHAR(40) NOT NULL, sv_key CHAR(64) NOT NULL, sv_value TEXT NOT NULL ); -ALTER TABLE stored_values ADD KEY (user_id, sco_id, course_id, sv_key); -ALTER TABLE stored_values ADD UNIQUE (user_id, sco_id, course_id, sv_key); diff --git a/main/newscorm/js/storageapi.js b/main/newscorm/js/storageapi.js index bf8ebf694c..997f2cbc7f 100644 --- a/main/newscorm/js/storageapi.js +++ b/main/newscorm/js/storageapi.js @@ -1,5 +1,6 @@ // Storage API // JavaScript API +// CBlue SPRL, Jean-Karim Bockstael lms_storage_testCall = function(content) { alert(content); @@ -64,3 +65,104 @@ lms_storage_getAll = function() { }); return result; } + +lms_storage_stack_push = function(sv_key, sv_value) { + var result; + $.ajax({ + async: false, + type: "POST", + url: "storageapi.php", + data: { + action: "stackpush", + svvalue: sv_value, + svkey: sv_key, + svuser: sv_user, + svcourse: sv_course, + svsco: sv_sco + }, + success: function(data) { + result = (data != '0'); + } + }); + return result; +} + +lms_storage_stack_pop = function(sv_key) { + var result; + $.ajax({ + async: false, + type: "POST", + url: "storageapi.php", + data: { + action: "stackpop", + svkey: sv_key, + svuser: sv_user, + svcourse: sv_course, + svsco: sv_sco + }, + success: function(data) { + result = data; + } + }); + return result; +} + +lms_storage_stack_length = function(sv_key) { + var result; + $.ajax({ + async: false, + type: "POST", + url: "storageapi.php", + data: { + action: "stacklength", + svkey: sv_key, + svuser: sv_user, + svcourse: sv_course, + svsco: sv_sco + }, + success: function(data) { + result = data; + } + }); + return result; +} + +lms_storage_stack_clear = function(sv_key) { + var result; + $.ajax({ + async: false, + type: "POST", + url: "storageapi.php", + data: { + action: "stackclear", + svkey: sv_key, + svuser: sv_user, + svcourse: sv_course, + svsco: sv_sco + }, + success: function(data) { + result = data; + } + }); + return result; +} + +lms_storage_stack_getAll = function(sv_key) { + var result; + $.ajax({ + async: false, + type: "POST", + url: "storageapi.php", + data: { + action: "stackgetall", + svkey: sv_key, + svuser: sv_user, + svcourse: sv_course, + svsco: sv_sco + }, + success: function(data) { + result = eval(data); + } + }); + return result; +} diff --git a/main/newscorm/storageapi.php b/main/newscorm/storageapi.php index 1984a413d3..0a63a9fbb9 100644 --- a/main/newscorm/storageapi.php +++ b/main/newscorm/storageapi.php @@ -1,6 +1,7 @@ require_once('../inc/global.inc.php'); @@ -14,6 +15,21 @@ switch ($_REQUEST['action']) { case "getall": print storage_getall($_REQUEST['svuser'], $_REQUEST['svcourse'], $_REQUEST['svsco']); break; + case "stackpush": + print storage_stack_push($_REQUEST['svuser'], $_REQUEST['svcourse'], $_REQUEST['svsco'], $_REQUEST['svkey'], $_REQUEST['svvalue']); + break; + case "stackpop": + print storage_stack_pop($_REQUEST['svuser'], $_REQUEST['svcourse'], $_REQUEST['svsco'], $_REQUEST['svkey']); + break; + case "stacklength": + print storage_stack_length($_REQUEST['svuser'], $_REQUEST['svcourse'], $_REQUEST['svsco'], $_REQUEST['svkey']); + break; + case "stackclear": + print storage_stack_clear($_REQUEST['svuser'], $_REQUEST['svcourse'], $_REQUEST['svsco'], $_REQUEST['svkey']); + break; + case "stackgetall": + print storage_stack_getall($_REQUEST['svuser'], $_REQUEST['svcourse'], $_REQUEST['svsco'], $_REQUEST['svkey']); + break; default: // Do nothing } @@ -61,4 +77,106 @@ function storage_getall($sv_user, $sv_course, $sv_sco) { } return json_encode($data); } + +function storage_stack_push($sv_user, $sv_course, $sv_sco, $sv_key, $sv_value) { + $sv_value = mysql_real_escape_string($sv_value); + $mainDB = Database::get_main_database(); + Database::query("start transaction"); + $sqlorder = "select ifnull((select max(stack_order) + from $mainDB.stored_values_stack + where user_id= '$sv_user' + and sco_id='$sv_sco' + and course_id='$sv_course' + and sv_key='$sv_key' + ), 0) as stack_order"; + $resorder = Database::query($sqlorder); + $row = Database::fetch_assoc($resorder); + $stack_order = (1 + $row['stack_order']); + $sqlinsert = "insert into $mainDB.stored_values_stack + (user_id, sco_id, course_id, sv_key, stack_order, sv_value) + values + ('$sv_user', '$sv_sco', '$sv_course', '$sv_key', '$stack_order', '$sv_value')"; + $resinsert = Database::query($sqlinsert); + if ($resorder && $resinsert) { + Database::query("commit"); + return 1; + } + else { + Database::query("rollback"); + return 0; + } +} + +function storage_stack_pop($sv_user, $sv_course, $sv_sco, $sv_key) { + $mainDB = Database::get_main_database(); + Database::query("start transaction"); + $sqlselect = "select sv_value, stack_order + from $mainDB.stored_values_stack + where user_id= '$sv_user' + and sco_id='$sv_sco' + and course_id='$sv_course' + and sv_key='$sv_key' + order by stack_order desc + limit 1"; + $resselect = Database::query($sqlselect); + $rowselect = Database::fetch_assoc($resselect); + $stack_order = $rowselect['stack_order']; + $sqldelete = "delete + from $mainDB.stored_values_stack + where user_id= '$sv_user' + and sco_id='$sv_sco' + and course_id='$sv_course' + and sv_key='$sv_key' + and stack_order='$stack_order'"; + $resdelete = Database::query($sqldelete); + if ($resselect && $resdelete) { + Database::query("commit"); + return $rowselect['sv_value']; + } + else { + Database::query("rollback"); + return null; + } +} + +function storage_stack_length($sv_user, $sv_course, $sv_sco, $sv_key) { + $mainDB = Database::get_main_database(); + $sql = "select count(*) as length + from $mainDB.stored_values_stack + where user_id= '$sv_user' + and sco_id='$sv_sco' + and course_id='$sv_course' + and sv_key='$sv_key'"; + $res = Database::query($sql); + $row = Database::fetch_assoc($res); + return $row['length']; +} + +function storage_stack_clear($sv_user, $sv_course, $sv_sco, $sv_key) { + $mainDB = Database::get_main_database(); + $sql = "delete + from $mainDB.stored_values_stack + where user_id= '$sv_user' + and sco_id='$sv_sco' + and course_id='$sv_course' + and sv_key='$sv_key'"; + $res = Database::query($sql); + return mysql_num_rows($res); +} + +function storage_stack_getall($sv_user, $sv_course, $sv_sco, $sv_key) { + $mainDB = Database::get_main_database(); + $sql = "select stack_order as stack_order, sv_value as value + from $mainDB.stored_values_stack + where user_id= '$sv_user' + and sco_id='$sv_sco' + and course_id='$sv_course' + and sv_key='$sv_key'"; + $res = Database::query($sql); + $results = array(); + while ($row = Database::fetch_assoc($res)) { + $results[] = $row; + } + return json_encode($results); +} ?>