This is my first attempt at a jQuery plugin. If someone could help me make sure that the code is clean, efficient, and formated correctly I would really appreciate it.
Background
So the reason I wrote this plugin is because I cannot find an easy to use code that has already been written. Plus, many people who work with a Learning Management System could utilize this without any effort on their part. All the communication with the LMS happens within the plugin, so they don't have to really learn the process of AICC communication. The only requirement to use this plugin is, the developer will have to know what AICC variables the LMS uses. These variables are set in stone by LMS and will not change per user ... everyone will have to use the same variables.
LMS Process
Step 1. User starts the course (a $.get()
method is used to see if any information was previously stored. If so, load the information.)
Step 2. As the user traverses the course you post information to the LMS and store the pages already viewed so you can have them start where they left off if they leave.(AKA Bookmarking)
Step 3. After the user finishes the course you $.post()
the information to the LMS and mark the course as passed.
So the process is pretty straight forward, the only thing is the callback from the server is in straight text and that cannot be changed, so you are stuck using the format the LMS returns.
What I am looking for
What I would like is for everyone to look at the plug and see if its the best it could be and if not offer some type off feedback. This plugin works very good, but is it efficient?
Here is my code, I will show the plugin code first then the HTML
jquery.aicc.js
(function($)
{
$.extend(
{
aicc:function(p)
{
var _sid = getUrlVar('AICC_SID');
var _url = getUrlVar('AICC_URL');
var _action =(p['action']=='set')?setAICC(p['score'],p['time'],p['credit'],p['location'],p['status']):(p['action']=='get')?getAICC():false;
function getUrlVar(urlVar)
{
var match = RegExp('[?&]' + urlVar + '=([^&]*)').exec(window.location.search);
return unescape(match && decodeURIComponent(match[1].replace(/\+/g, ' ')));
}
function setAICC(s,t,c,l,ls,c)
{
$.post(_url,{command:"PutParam",version:"2.2",session_id:_sid, aicc_data:"[CORE]\nlesson_location="+l+"\ncredit="+c+"\nscore="+s+"\ntime="+t+"\nlesson_status="+ls},function(r)
{
p['response'].call(this,json(r));
})
.error(function(a,b,c){p['response'].call(this,c);});
}
function getAICC()
{
$.get(_url,{command:"GetParam",version:"2.2",session_id:_sid},function(r)
{
p['response'].call(this,json(r));
})
.error(function(a,b,c){p['response'].call(this,c);});
}
function json(str)
{
var obj = {};
str.replace(/([^=]+)=(.*)\n/g,function(_,name,value){obj[name]=value;});
return obj;
}
}
});
})(jQuery);
index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "https://www.w3.org/TR/html4/transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>jQuery Plugin Testing Portal</title>
<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script language="javascript" src="library/jquery.aicc.js"></script>
<script type="text/javascript">
$(function()
{
/* sets aicc information */
$('#setBtn').click(function()
{
$.aicc(
{
action:'set',
score:'83',
time:'00:00:00',
credit:'C',
location:'page_1',
status:'Incomplete',
response:function(r)
{
alert(r['ERROR']);
alert(r['ERROR_TEXT']);
alert(r['VERSION']);
}
});
});
/* gets ALL aicc information (returns in JSON format */
$('#getBtn').click(function()
{
$.aicc(
{
action:'get',
response:function(r)
{
alert(r['ERROR']);
alert(r['ERROR_TEXT']);
alert(r['VERSION']);
alert(r['AICC_DATA']);
alert(r['STUDENT_ID']);
alert(r['STUDENT_NAME']);
alert(r['SCORE']);
alert(r['TIME']);
alert(r['CREDIT']);
alert(r['LESSON_LOCATION']);
alert(r['LESSON_STATUS']);
}
});
});
});
</script>
</head>
<body>
<button id="setBtn">Set AICC</button>
<button id="getBtn">Get AICC</button>
</body>
</html>
Note: When the user is logged into the LMS and is taking the course, there are variables in the URL that the plugin grabs:
Here is the callback from the LMS on the $.post()
and $.get()
. (This is all text with each line ending with \n
.)
ERROR=0
ERROR_TEXT=Successful
VERSION=2.2
AICC_DATA=[CORE]
STUDENT_ID=0123456
STUDENT_NAME=Doe, John R
SCORE=83
TIME=02:35:37
CREDIT=C
LESSON_LOCATION=page_1
LESSON_STATUS=INCOMPLETE
[Core_Lesson]
[Objectives_Status]