4
\$\begingroup\$

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:

http://www.lms.com/?AICC_SID=C0000000M00000S&AICC_URL=https%3a%2f%2fwww%2elms%2ecom%2fplateau%2fPwsAicc

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]
\$\endgroup\$
2
  • \$\begingroup\$ first thing : Thank you trying to help Aicc users :) Do you have follow the Lars-Erik's advices ? I'm working on a scorm project and i could give you help to get a "jquery-Friendly" version. Anyway : thanks again ! \$\endgroup\$
    – user14271
    Commented Jun 19, 2012 at 14:54
  • \$\begingroup\$ Hello Niorgen, I did follow his advice and totally reconstructed the plugin. If you are interested in seeing what it does just follow this link ->forum.aicc.org/cgi-bin/yabb2/YaBB.pl?num=1330364068/0 \$\endgroup\$
    – Mike
    Commented Jun 23, 2012 at 16:44

1 Answer 1

2
\$\begingroup\$

I don't immediately see any efficiency issues with your code. It's simple and concise. But I do notice that you don't follow all the best practices for plugin authoring described here:

http://docs.jquery.com/Plugins/Authoring

I guess you'll do something else than alerting ten times in production? ;)

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for taking a look, yeah I see what you are saying. I actually have been following a jQuery Plugin tutorial I found via Google. After reading your link, I see what I need to do. That is exactly what I wanted someone to confirm, so I am going to mark this as answered because your link provides me with what changes I need. Thanks again :) \$\endgroup\$
    – Mike
    Commented Jan 27, 2012 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.