Skip to content

Commit

Permalink
Merge pull request #60 from brianjmiller/master
Browse files Browse the repository at this point in the history
Handle 'category' as part of contextActivities
  • Loading branch information
bscSCORM committed Feb 13, 2014
2 parents 5641414 + f12d55a commit 2c6897e
Show file tree
Hide file tree
Showing 9 changed files with 516 additions and 36 deletions.
2 changes: 1 addition & 1 deletion build/tincan-min.js

Large diffs are not rendered by default.

96 changes: 86 additions & 10 deletions build/tincan-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -1715,13 +1715,41 @@ TinCan client library
*/
saveStatement: function (stmt, cfg) {
this.log("saveStatement");
var requestCfg;
var requestCfg,
versionedStatement;

cfg = cfg || {};

try {
versionedStatement = stmt.asVersion( this.version );
}
catch (ex) {
if (this.allowFail) {
this.log("[warning] statement could not be serialized in version (" + this.version + "): " + ex);
if (typeof cfg.callback !== "undefined") {
cfg.callback(null, null);
return;
}
return {
err: null,
xhr: null
};
}

this.log("[error] statement could not be serialized in version (" + this.version + "): " + ex);
if (typeof cfg.callback !== "undefined") {
cfg.callback(ex, null);
return;
}
return {
err: ex,
xhr: null
};
}

requestCfg = {
url: "statements",
data: JSON.stringify(stmt.asVersion( this.version )),
data: JSON.stringify(versionedStatement),
headers: {
"Content-Type": "application/json"
}
Expand Down Expand Up @@ -1856,6 +1884,7 @@ TinCan client library
saveStatements: function (stmts, cfg) {
this.log("saveStatements");
var requestCfg,
versionedStatement,
versionedStatements = [],
i
;
Expand All @@ -1864,15 +1893,43 @@ TinCan client library

if (stmts.length === 0) {
if (typeof cfg.callback !== "undefined") {
cfg.callback.apply(this, ["no statements"]);
cfg.callback(new Error("no statements"), null);
return;
}
return;
return {
err: new Error("no statements"),
xhr: null
};
}

for (i = 0; i < stmts.length; i += 1) {
versionedStatements.push(
stmts[i].asVersion( this.version )
);
try {
versionedStatement = stmts[i].asVersion( this.version );
}
catch (ex) {
if (this.allowFail) {
this.log("[warning] statement could not be serialized in version (" + this.version + "): " + ex);
if (typeof cfg.callback !== "undefined") {
cfg.callback(null, null);
return;
}
return {
err: null,
xhr: null
};
}

this.log("[error] statement could not be serialized in version (" + this.version + "): " + ex);
if (typeof cfg.callback !== "undefined") {
cfg.callback(ex, null);
return;
}
return {
err: ex,
xhr: null
};
}
versionedStatements.push(versionedStatement);
}

requestCfg = {
Expand Down Expand Up @@ -4565,6 +4622,12 @@ TinCan client library
var ContextActivities = TinCan.ContextActivities = function (cfg) {
this.log("constructor");

/**
@property category
@type Array
*/
this.category = null;

/**
@property parent
@type Array
Expand Down Expand Up @@ -4606,6 +4669,7 @@ TinCan client library
var i,
j,
objProps = [
"category",
"parent",
"grouping",
"other"
Expand Down Expand Up @@ -4635,11 +4699,11 @@ TinCan client library

/**
@method add
@param String key Property to add value to one of "parent", "grouping", "other"
@param String key Property to add value to one of "category", "parent", "grouping", "other"
@return Number index where the value was added
*/
add: function (key, val) {
if (key !== "parent" && key !== "grouping" && key !== "other") {
if (key !== "category" && key !== "parent" && key !== "grouping" && key !== "other") {
return;
}

Expand Down Expand Up @@ -4680,7 +4744,7 @@ TinCan client library
if (version === "0.9" || version === "0.95") {
if (this[optionalObjProps[i]].length > 1) {
// TODO: exception?
this.log("[WARNING] version does not support multiple values in: " + optionalObjProps[i]);
this.log("[warning] version does not support multiple values in: " + optionalObjProps[i]);
}

result[optionalObjProps[i]] = this[optionalObjProps[i]][0].asVersion(version);
Expand All @@ -4695,6 +4759,18 @@ TinCan client library
}
}
}
if (this.category !== null && this.category.length > 0) {
if (version === "0.9" || version === "0.95") {
this.log("[error] version does not support the 'category' property: " + version);
throw new Error(version + " does not support the 'category' property");
}
else {
result.category = [];
for (i = 0; i < this.category.length; i += 1) {
result.category.push(this.category[i].asVersion(version));
}
}
}

return result;
}
Expand Down
96 changes: 86 additions & 10 deletions build/tincan.js
Original file line number Diff line number Diff line change
Expand Up @@ -1715,13 +1715,41 @@ TinCan client library
*/
saveStatement: function (stmt, cfg) {
this.log("saveStatement");
var requestCfg;
var requestCfg,
versionedStatement;

cfg = cfg || {};

try {
versionedStatement = stmt.asVersion( this.version );
}
catch (ex) {
if (this.allowFail) {
this.log("[warning] statement could not be serialized in version (" + this.version + "): " + ex);
if (typeof cfg.callback !== "undefined") {
cfg.callback(null, null);
return;
}
return {
err: null,
xhr: null
};
}

this.log("[error] statement could not be serialized in version (" + this.version + "): " + ex);
if (typeof cfg.callback !== "undefined") {
cfg.callback(ex, null);
return;
}
return {
err: ex,
xhr: null
};
}

requestCfg = {
url: "statements",
data: JSON.stringify(stmt.asVersion( this.version )),
data: JSON.stringify(versionedStatement),
headers: {
"Content-Type": "application/json"
}
Expand Down Expand Up @@ -1856,6 +1884,7 @@ TinCan client library
saveStatements: function (stmts, cfg) {
this.log("saveStatements");
var requestCfg,
versionedStatement,
versionedStatements = [],
i
;
Expand All @@ -1864,15 +1893,43 @@ TinCan client library

if (stmts.length === 0) {
if (typeof cfg.callback !== "undefined") {
cfg.callback.apply(this, ["no statements"]);
cfg.callback(new Error("no statements"), null);
return;
}
return;
return {
err: new Error("no statements"),
xhr: null
};
}

for (i = 0; i < stmts.length; i += 1) {
versionedStatements.push(
stmts[i].asVersion( this.version )
);
try {
versionedStatement = stmts[i].asVersion( this.version );
}
catch (ex) {
if (this.allowFail) {
this.log("[warning] statement could not be serialized in version (" + this.version + "): " + ex);
if (typeof cfg.callback !== "undefined") {
cfg.callback(null, null);
return;
}
return {
err: null,
xhr: null
};
}

this.log("[error] statement could not be serialized in version (" + this.version + "): " + ex);
if (typeof cfg.callback !== "undefined") {
cfg.callback(ex, null);
return;
}
return {
err: ex,
xhr: null
};
}
versionedStatements.push(versionedStatement);
}

requestCfg = {
Expand Down Expand Up @@ -4565,6 +4622,12 @@ TinCan client library
var ContextActivities = TinCan.ContextActivities = function (cfg) {
this.log("constructor");

/**
@property category
@type Array
*/
this.category = null;

/**
@property parent
@type Array
Expand Down Expand Up @@ -4606,6 +4669,7 @@ TinCan client library
var i,
j,
objProps = [
"category",
"parent",
"grouping",
"other"
Expand Down Expand Up @@ -4635,11 +4699,11 @@ TinCan client library

/**
@method add
@param String key Property to add value to one of "parent", "grouping", "other"
@param String key Property to add value to one of "category", "parent", "grouping", "other"
@return Number index where the value was added
*/
add: function (key, val) {
if (key !== "parent" && key !== "grouping" && key !== "other") {
if (key !== "category" && key !== "parent" && key !== "grouping" && key !== "other") {
return;
}

Expand Down Expand Up @@ -4680,7 +4744,7 @@ TinCan client library
if (version === "0.9" || version === "0.95") {
if (this[optionalObjProps[i]].length > 1) {
// TODO: exception?
this.log("[WARNING] version does not support multiple values in: " + optionalObjProps[i]);
this.log("[warning] version does not support multiple values in: " + optionalObjProps[i]);
}

result[optionalObjProps[i]] = this[optionalObjProps[i]][0].asVersion(version);
Expand All @@ -4695,6 +4759,18 @@ TinCan client library
}
}
}
if (this.category !== null && this.category.length > 0) {
if (version === "0.9" || version === "0.95") {
this.log("[error] version does not support the 'category' property: " + version);
throw new Error(version + " does not support the 'category' property");
}
else {
result.category = [];
for (i = 0; i < this.category.length; i += 1) {
result.category.push(this.category[i].asVersion(version));
}
}
}

return result;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tincanjs",
"description": "Tin Can API Library",
"version": "0.20.1",
"version": "0.22.0",
"private": false,
"main": "build/tincan-node.js",
"directories": {
Expand Down
25 changes: 22 additions & 3 deletions src/ContextActivities.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ TinCan client library
var ContextActivities = TinCan.ContextActivities = function (cfg) {
this.log("constructor");

/**
@property category
@type Array
*/
this.category = null;

/**
@property parent
@type Array
Expand Down Expand Up @@ -71,6 +77,7 @@ TinCan client library
var i,
j,
objProps = [
"category",
"parent",
"grouping",
"other"
Expand Down Expand Up @@ -100,11 +107,11 @@ TinCan client library

/**
@method add
@param String key Property to add value to one of "parent", "grouping", "other"
@param String key Property to add value to one of "category", "parent", "grouping", "other"
@return Number index where the value was added
*/
add: function (key, val) {
if (key !== "parent" && key !== "grouping" && key !== "other") {
if (key !== "category" && key !== "parent" && key !== "grouping" && key !== "other") {
return;
}

Expand Down Expand Up @@ -145,7 +152,7 @@ TinCan client library
if (version === "0.9" || version === "0.95") {
if (this[optionalObjProps[i]].length > 1) {
// TODO: exception?
this.log("[WARNING] version does not support multiple values in: " + optionalObjProps[i]);
this.log("[warning] version does not support multiple values in: " + optionalObjProps[i]);
}

result[optionalObjProps[i]] = this[optionalObjProps[i]][0].asVersion(version);
Expand All @@ -160,6 +167,18 @@ TinCan client library
}
}
}
if (this.category !== null && this.category.length > 0) {
if (version === "0.9" || version === "0.95") {
this.log("[error] version does not support the 'category' property: " + version);
throw new Error(version + " does not support the 'category' property");
}
else {
result.category = [];
for (i = 0; i < this.category.length; i += 1) {
result.category.push(this.category[i].asVersion(version));
}
}
}

return result;
}
Expand Down
Loading

0 comments on commit 2c6897e

Please sign in to comment.