Get course id and userid in the Scorm localStorage along with the response

Get course id and userid in the Scorm localStorage along with the response

by Amir Mustafa -
Number of replies: 2

Hello Developers,

Scorm saves the response taken in the user's system in the localStorge

I need to save the course id and user id along with the response collected in the localStorage. As we have to save the data in the database.

I am in moodle end. I need to receive data from the localStorage (javascript).

Thank you for your response.

Attachment b.png
Average of ratings: -
In reply to Amir Mustafa

Re: Get course id and userid in the Scorm localStorage along with the response

by Amir Mustafa -
________________________________________________________________________________________________________________________________________

Hello Friends,

I managed to solve my issue. Here is the solutions for the developers who may need similar issue in future. The purpose of this task was if a user logins from system 1 and fills the i/p data SCORM by default saves the data in localStorage.

Now suppose if the user logins the same from system 2. How to get those data as it is not saved in the same system 

________________________________________________________________________________________________________________________________________

Logic:

When user is in system 1:

He fills the data from the system 1 (first time). Along with the localStotage(default case). We will save the data in the database (scorm_tracker). 

When user is in system 2:

Data is updated from database to localStorage of system 2.

________________________________________________________________________________________________________________________________________

Video:

Video 1 (System 1) -- Setting up data here from localStorage to DB
Link - https://www.useloom.com/share/a612cf6cff2046b5b065e5aa8c369b19

Video 2 (System 2) - Getting data in localStorage here from DB
Link - https://www.useloom.com/share/eeec39cc3c4e4d18a9e60a04e0b85d95

________________________________________________________________________________________________________________________________________

SOLUTION:

1st: Its was the core change (in  our case course changes was allowed from client's end)

Pages - 

moodle/mod/scorm/player.php (core page - page where ajax is trigerred)

moodle/mod/scorm/scorm_ajax.php (new page - handeling database)

scorm_tracker - a custom table

________________________________________________________________________________________________________________________________________

player.php - added following lines of code: Basically what we did here get data from db to localStorage

/* =========================== CODE FOR GETTING LOCALSTORAGE: START =========================== */
// added getting SCORM content - amir 01.03.2019
echo "<br><br><br><br><div id='data'><h1></h1></div>";
global $DB, $USER;
if(isset($id)) {
$get_course_sql = $DB->get_record_sql("SELECT * FROM {course_modules} WHERE id = $id");
$courseid = $get_course_sql->course;

// get SCORM id from the sco id
$get_course_sql = $DB->get_record_sql("SELECT * FROM {scorm_scoes} WHERE id = $scoid");
$scormid = $get_course_sql->scorm;

// user id of the student
$userid = $USER->id;

// getting prev records from the db
$check_prev_record = array_values( $DB->get_records_sql("SELECT * FROM {scorm_tracker}
WHERE userid = $userid AND courseid = $courseid AND scormid = $scormid"));

$localStorageArray = Array();
$localStorageArray['title'] = $check_prev_record[0]->section;
$i = 0;
foreach($check_prev_record as $cpr) {
$localStorageArray['entries'][$i]['section'] = $cpr->section;
$localStorageArray['entries'][$i]['prompt'] = $cpr->prompt;
$localStorageArray['entries'][$i]['response'] = $cpr->response;
$localStorageArray['entries'][$i]['isTakeAction'] = $cpr->isTakeAction == 0 ? false: $cpr->isTakeAction;
$i++;
}
$localStorageArray['introtitle'] = "";
$localStorageArray['introtext'] = "";

$localStorageJSON = json_encode($localStorageArray);
/* echo "<pre>";
print_r($localStorageJSON); */
}
/* =========================== CODE FOR GETTING LOCALSTORAGE: END =========================== */

JavaScript (in the same page at the bottom):

<!-- ========================= JavaScript files for localStorage file: Start ========================= -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
    let scormKey = 'LearningJournal_http%3A%2F%2Flocalhost%2Fmoodle_mrbs%2Fpluginfile.php%2F45%2Fmod_scorm%2Fcontent%2F1%2Fscormcontent';
let scormid = '<?php echo $scormid ?>';
let courseid = '<?php echo $courseid ?>';
let userid = '<?php echo $userid ?>';
let localStorageJSON = '<?php echo $localStorageJSON ?>' || '';
// alert(localStorageJSON);
// looping through the localStorage
let count = 0;
$.each(localStorage, function(key, value){
if (key.match(/LearningJournal/)) {
$.ajax({
url: 'scorm_ajax.php',
type: 'post',
data: {
value: value,
courseid: courseid,
userid: userid,
scormid: scormid
},
success: function (res) {
// alert(res);
$("#data").html(res);
}
});
count++;
}
});

// Update case - we will update the data in localStorage only when we have data in database
if(count == 1) {
// alert('update case = ' + localStorageJSON);
localStorage.setItem(scormKey, localStorageJSON);
}
</script>
<!-- ========================= JavaScript files for localStorage file: End ========================= -->

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
scorm_ajax.php (I have attached this file)
Here we have added the data from localStorage (first time data written in i/p) to database 


Cheers !! smile smile