Advertisement
Guest User

Untitled

a guest
Aug 21st, 2013
1,666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.26 KB | None | 0 0
  1. /****************************************************************************
  2. SCORM_2004_APIwrapper.js
  3. © 2000, 2011 Advanced Distributed Learning (ADL). Some Rights Reserved.
  4. *****************************************************************************
  5.  
  6. Advanced Distributed Learning ("ADL") grants you ("Licensee") a non-exclusive,
  7. royalty free, license to use and redistribute this software in source and binary
  8. code form, provided that i) this copyright notice and license appear on all
  9. copies of the software; and ii) Licensee does not utilize the software in a
  10. manner which is disparaging to ADL.
  11.  
  12. This software is provided "AS IS," without a warranty of any kind.
  13. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  14. ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15. NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ADL AND ITS LICENSORS SHALL NOT BE LIABLE
  16. FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17. DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL ADL OR ITS LICENSORS
  18. BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
  19. CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE
  20. THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF
  21. ADL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  22.  
  23. *****************************************************************************
  24. *SCORM_2004_APIwrapper.js code is licensed under the Creative Commons
  25. Attribution-ShareAlike 3.0 Unported License.
  26.  
  27. To view a copy of this license:
  28.  
  29. - Visit http://creativecommons.org/licenses/by-sa/3.0/
  30. - Or send a letter to
  31. Creative Commons, 444 Castro Street, Suite 900, Mountain View,
  32. California, 94041, USA.
  33.  
  34. The following is a summary of the full license which is available at:
  35.  
  36. - http://creativecommons.org/licenses/by-sa/3.0/legalcode
  37.  
  38. *****************************************************************************
  39.  
  40. Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)
  41.  
  42. You are free to:
  43.  
  44. - Share : to copy, distribute and transmit the work
  45. - Remix : to adapt the work
  46.  
  47. Under the following conditions:
  48.  
  49. - Attribution: You must attribute the work in the manner specified by
  50. the author or licensor (but not in any way that suggests that they
  51. endorse you or your use of the work).
  52.  
  53. - Share Alike: If you alter, transform, or build upon this work, you
  54. may distribute the resulting work only under the same or similar
  55. license to this one.
  56.  
  57. With the understanding that:
  58.  
  59. - Waiver: Any of the above conditions can be waived if you get permission
  60. from the copyright holder.
  61.  
  62. - Public Domain: Where the work or any of its elements is in the public
  63. domain under applicable law, that status is in no way affected by the license.
  64.  
  65. - Other Rights: In no way are any of the following rights affected by the license:
  66.  
  67. * Your fair dealing or fair use rights, or other applicable copyright
  68. exceptions and limitations;
  69.  
  70. * The author's moral rights;
  71.  
  72. * Rights other persons may have either in the work itself or in how the
  73. work is used, such as publicity or privacy rights.
  74.  
  75. - Notice: For any reuse or distribution, you must make clear to others the
  76. license terms of this work.
  77.  
  78. ****************************************************************************/
  79. /*******************************************************************************
  80. ** Usage: Executable course content can call the API Wrapper
  81. ** functions as follows:
  82. **
  83. ** javascript:
  84. ** var result = doInitialize();
  85. ** if (result != true)
  86. ** {
  87. ** // handle error
  88. ** }
  89. **
  90. ** authorware:
  91. ** result := ReadURL("javascript:doInitialize()", 100)
  92. **
  93. ** director:
  94. ** result = externalEvent("javascript:doInitialize()")
  95. **
  96. **
  97. *******************************************************************************/
  98.  
  99. var debug = true; // set this to false to turn debugging off
  100.  
  101. var output = window.console; // output can be set to any object that has a log(string) function
  102. // such as: var output = { log: function(str){alert(str);} };
  103.  
  104. // Define exception/error codes
  105. var _NoError = {"code":"0","string":"No Error","diagnostic":"No Error"};;
  106. var _GeneralException = {"code":"101","string":"General Exception","diagnostic":"General Exception"};
  107. var _AlreadyInitialized = {"code":"103","string":"Already Initialized","diagnostic":"Already Initialized"};
  108.  
  109. var initialized = false;
  110.  
  111. // local variable definitions
  112. var apiHandle = null;
  113.  
  114. /*******************************************************************************
  115. **
  116. ** Function: doInitialize()
  117. ** Inputs: None
  118. ** Return: true if the initialization was successful, or
  119. ** false if the initialization failed.
  120. **
  121. ** Description:
  122. ** Initialize communication with LMS by calling the Initialize
  123. ** function which will be implemented by the LMS.
  124. **
  125. *******************************************************************************/
  126. function doInitialize()
  127. {
  128. if (initialized) return "true";
  129.  
  130. var api = getAPIHandle();
  131. if (api == null)
  132. {
  133. message("Unable to locate the LMS's API Implementation.\nInitialize was not successful.");
  134. return "false";
  135. }
  136.  
  137. var result = api.Initialize("");
  138.  
  139. if (result.toString() != "true")
  140. {
  141. var err = ErrorHandler();
  142. message("Initialize failed with error code: " + err.code);
  143. }
  144. else
  145. {
  146. initialized = true;
  147. }
  148.  
  149. return result.toString();
  150. }
  151.  
  152. /*******************************************************************************
  153. **
  154. ** Function doTerminate()
  155. ** Inputs: None
  156. ** Return: true if successful
  157. ** false if failed.
  158. **
  159. ** Description:
  160. ** Close communication with LMS by calling the Terminate
  161. ** function which will be implemented by the LMS
  162. **
  163. *******************************************************************************/
  164. function doTerminate()
  165. {
  166. if (! initialized) return "true";
  167.  
  168. var api = getAPIHandle();
  169. if (api == null)
  170. {
  171. message("Unable to locate the LMS's API Implementation.\nTerminate was not successful.");
  172. return "false";
  173. }
  174. else
  175. {
  176. // call the Terminate function that should be implemented by the API
  177. var result = api.Terminate("");
  178. if (result.toString() != "true")
  179. {
  180. var err = ErrorHandler();
  181. message("Terminate failed with error code: " + err.code);
  182. }
  183. }
  184.  
  185. initialized = false;
  186.  
  187. return result.toString();
  188. }
  189.  
  190. /*******************************************************************************
  191. **
  192. ** Function doGetValue(name)
  193. ** Inputs: name - string representing the cmi data model defined category or
  194. ** element (e.g. cmi.learner_id)
  195. ** Return: The value presently assigned by the LMS to the cmi data model
  196. ** element defined by the element or category identified by the name
  197. ** input value.
  198. **
  199. ** Description:
  200. ** Wraps the call to the GetValue method
  201. **
  202. *******************************************************************************/
  203. function doGetValue(name)
  204. {
  205. var api = getAPIHandle();
  206. var result = "";
  207. if (api == null)
  208. {
  209. message("Unable to locate the LMS's API Implementation.\nGetValue was not successful.");
  210. }
  211. else if (!initialized && ! doInitialize())
  212. {
  213. var err = ErrorHandler();
  214. message("GetValue failed - Could not initialize communication with the LMS - error code: " + err.code);
  215. }
  216. else
  217. {
  218. result = api.GetValue(name);
  219.  
  220. var error = ErrorHandler();
  221. if (error.code != _NoError.code)
  222. {
  223. // an error was encountered so display the error description
  224. message("GetValue("+name+") failed. \n"+ error.code + ": " + error.string);
  225. result = "";
  226. }
  227. }
  228. return result.toString();
  229. }
  230.  
  231. /*******************************************************************************
  232. **
  233. ** Function doSetValue(name, value)
  234. ** Inputs: name -string representing the data model defined category or element
  235. ** value -the value that the named element or category will be assigned
  236. ** Return: true if successful
  237. ** false if failed.
  238. **
  239. ** Description:
  240. ** Wraps the call to the SetValue function
  241. **
  242. *******************************************************************************/
  243. function doSetValue(name, value)
  244. {
  245. var api = getAPIHandle();
  246. var result = "false";
  247. if (api == null)
  248. {
  249. message("Unable to locate the LMS's API Implementation.\nSetValue was not successful.");
  250. }
  251. else if (!initialized && !doInitialize())
  252. {
  253. var error = ErrorHandler();
  254. message("SetValue failed - Could not initialize communication with the LMS - error code: " + error.code);
  255. }
  256. else
  257. {
  258. result = api.SetValue(name, value);
  259. if (result.toString() != "true")
  260. {
  261. var err = ErrorHandler();
  262. message("SetValue("+name+", "+value+") failed. \n"+ err.code + ": " + err.string);
  263. }
  264. }
  265.  
  266. return result.toString();
  267. }
  268.  
  269. /*******************************************************************************
  270. **
  271. ** Function doCommit()
  272. ** Inputs: None
  273. ** Return: true if successful
  274. ** false if failed
  275. **
  276. ** Description:
  277. ** Commits the data to the LMS.
  278. **
  279. *******************************************************************************/
  280. function doCommit()
  281. {
  282. var api = getAPIHandle();
  283. var result = "false";
  284. if (api == null)
  285. {
  286. message("Unable to locate the LMS's API Implementation.\nCommit was not successful.");
  287. }
  288. else if (!initialized && ! doInitialize())
  289. {
  290. var error = ErrorHandler();
  291. message("Commit failed - Could not initialize communication with the LMS - error code: " + error.code);
  292. }
  293. else
  294. {
  295. result = api.Commit("");
  296. if (result != "true")
  297. {
  298. var err = ErrorHandler();
  299. message("Commit failed - error code: " + err.code);
  300. }
  301. }
  302.  
  303. return result.toString();
  304. }
  305.  
  306. /*******************************************************************************
  307. **
  308. ** Function doGetLastError()
  309. ** Inputs: None
  310. ** Return: The error code that was set by the last LMS function call
  311. **
  312. ** Description:
  313. ** Call the GetLastError function
  314. **
  315. *******************************************************************************/
  316. function doGetLastError()
  317. {
  318. var api = getAPIHandle();
  319. if (api == null)
  320. {
  321. message("Unable to locate the LMS's API Implementation.\nGetLastError was not successful.");
  322. //since we can't get the error code from the LMS, return a general error
  323. return _GeneralException.code;
  324. }
  325.  
  326. return api.GetLastError().toString();
  327. }
  328.  
  329. /*******************************************************************************
  330. **
  331. ** Function doGetErrorString(errorCode)
  332. ** Inputs: errorCode - Error Code
  333. ** Return: The textual description that corresponds to the input error code
  334. **
  335. ** Description:
  336. ** Call the GetErrorString function
  337. **
  338. ********************************************************************************/
  339. function doGetErrorString(errorCode)
  340. {
  341. var api = getAPIHandle();
  342. if (api == null)
  343. {
  344. message("Unable to locate the LMS's API Implementation.\nGetErrorString was not successful.");
  345. return _GeneralException.string;
  346. }
  347.  
  348. return api.GetErrorString(errorCode).toString();
  349. }
  350.  
  351. /*******************************************************************************
  352. **
  353. ** Function doGetDiagnostic(errorCode)
  354. ** Inputs: errorCode - Error Code(integer format), or null
  355. ** Return: The vendor specific textual description that corresponds to the
  356. ** input error code
  357. **
  358. ** Description:
  359. ** Call the LMSGetDiagnostic function
  360. **
  361. *******************************************************************************/
  362. function doGetDiagnostic(errorCode)
  363. {
  364. var api = getAPIHandle();
  365. if (api == null)
  366. {
  367. message("Unable to locate the LMS's API Implementation.\nGetDiagnostic was not successful.");
  368. return "Unable to locate the LMS's API Implementation. GetDiagnostic was not successful.";
  369. }
  370.  
  371. return api.GetDiagnostic(errorCode).toString();
  372. }
  373.  
  374. /*******************************************************************************
  375. **
  376. ** Function ErrorHandler()
  377. ** Inputs: None
  378. ** Return: The current error
  379. **
  380. ** Description:
  381. ** Determines if an error was encountered by the previous API call
  382. ** and if so, returns the error.
  383. **
  384. ** Usage:
  385. ** var last_error = ErrorHandler();
  386. ** if (last_error.code != _NoError.code)
  387. ** {
  388. ** message("Encountered an error. Code: " + last_error.code +
  389. ** "\nMessage: " + last_error.string +
  390. ** "\nDiagnostics: " + last_error.diagnostic);
  391. ** }
  392. *******************************************************************************/
  393. function ErrorHandler()
  394. {
  395. var error = {"code":_NoError.code, "string":_NoError.string, "diagnostic":_NoError.diagnostic};
  396. var api = getAPIHandle();
  397. if (api == null)
  398. {
  399. message("Unable to locate the LMS's API Implementation.\nCannot determine LMS error code.");
  400. error.code = _GeneralException.code;
  401. error.string = _GeneralException.string;
  402. error.diagnostic = "Unable to locate the LMS's API Implementation. Cannot determine LMS error code.";
  403. return error;
  404. }
  405.  
  406. // check for errors caused by or from the LMS
  407. error.code = api.GetLastError().toString();
  408. if (error.code != _NoError.code)
  409. {
  410. // an error was encountered so display the error description
  411. error.string = api.GetErrorString(error.code);
  412. error.diagnostic = api.GetDiagnostic("");
  413. }
  414.  
  415. return error;
  416. }
  417.  
  418. /******************************************************************************
  419. **
  420. ** Function getAPIHandle()
  421. ** Inputs: None
  422. ** Return: value contained by APIHandle
  423. **
  424. ** Description:
  425. ** Returns the handle to API object if it was previously set,
  426. ** otherwise it returns null
  427. **
  428. *******************************************************************************/
  429. function getAPIHandle()
  430. {
  431. if (apiHandle == null)
  432. {
  433. apiHandle = getAPI();
  434. }
  435.  
  436. return apiHandle;
  437. }
  438.  
  439.  
  440. /*******************************************************************************
  441. **
  442. ** Function findAPI(win)
  443. ** Inputs: win - a Window Object
  444. ** Return: If an API object is found, it's returned, otherwise null is returned
  445. **
  446. ** Description:
  447. ** This function looks for an object named API_1484_11 in parent and opener
  448. ** windows
  449. **
  450. *******************************************************************************/
  451. function findAPI(win)
  452. {
  453. var findAPITries = 0;
  454. while ((win.API_1484_11 == null) && (win.parent != null) && (win.parent != win))
  455. {
  456. findAPITries++;
  457.  
  458. if (findAPITries > 500)
  459. {
  460. message("Error finding API -- too deeply nested.");
  461. return null;
  462. }
  463.  
  464. win = win.parent;
  465.  
  466. }
  467. return win.API_1484_11;
  468. }
  469.  
  470. /*******************************************************************************
  471. **
  472. ** Function getAPI()
  473. ** Inputs: none
  474. ** Return: If an API object is found, it's returned, otherwise null is returned
  475. **
  476. ** Description:
  477. ** This function looks for an object named API_1484_11, first in the current window's
  478. ** frame hierarchy and then, if necessary, in the current window's opener window
  479. ** hierarchy (if there is an opener window).
  480. **
  481. *******************************************************************************/
  482. function getAPI()
  483. {
  484. var theAPI = findAPI(window);
  485. if ((theAPI == null) && (window.opener != null) && (typeof(window.opener) != "undefined"))
  486. {
  487. theAPI = findAPI(window.opener);
  488. }
  489. if (theAPI == null)
  490. {
  491. message("Unable to find an API adapter");
  492. }
  493. return theAPI
  494. }
  495.  
  496. /*******************************************************************************
  497. **
  498. ** Function findObjective(objId)
  499. ** Inputs: objId - the id of the objective
  500. ** Return: the index where this objective is located
  501. **
  502. ** Description:
  503. ** This function looks for the objective within the objective array and returns
  504. ** the index where it was found or it will create the objective for you and return
  505. ** the new index.
  506. **
  507. *******************************************************************************/
  508. function findObjective(objId)
  509. {
  510. var num = doGetValue("cmi.objectives._count");
  511. var objIndex = -1;
  512.  
  513. for (var i=0; i < num; ++i) {
  514. if (doGetValue("cmi.objectives." + i + ".id") == objId) {
  515. objIndex = i;
  516. break;
  517. }
  518. }
  519.  
  520. if (objIndex == -1) {
  521. message("Objective " + objId + " not found.");
  522. objIndex = num;
  523. message("Creating new objective at index " + objIndex);
  524. doSetValue("cmi.objectives." + objIndex + ".id", objId);
  525. }
  526. return objIndex;
  527. }
  528.  
  529. /*******************************************************************************
  530. ** NOTE: This is a SCORM 2004 4th Edition feature.
  531. *
  532. ** Function findDataStore(id)
  533. ** Inputs: id - the id of the data store
  534. ** Return: the index where this data store is located or -1 if the id wasn't found
  535. **
  536. ** Description:
  537. ** This function looks for the data store within the data array and returns
  538. ** the index where it was found or returns -1 to indicate the id wasn't found
  539. ** in the collection.
  540. **
  541. ** Usage:
  542. ** var dsIndex = findDataStore("myds");
  543. ** if (dsIndex > -1)
  544. ** {
  545. ** doSetValue("adl.data." + dsIndex + ".store", "save this info...");
  546. ** }
  547. ** else
  548. ** {
  549. ** var appending_data = doGetValue("cmi.suspend_data");
  550. ** doSetValue("cmi.suspend_data", appending_data + "myds:save this info");
  551. ** }
  552. *******************************************************************************/
  553. function findDataStore(id)
  554. {
  555. var num = doGetValue("adl.data._count");
  556. var index = -1;
  557.  
  558. // if the get value was not null and is a number
  559. // in other words, we got an index in the adl.data array
  560. if (num != null && ! isNaN(num))
  561. {
  562. for (var i=0; i < num; ++i)
  563. {
  564. if (doGetValue("adl.data." + i + ".id") == id)
  565. {
  566. index = i;
  567. break;
  568. }
  569. }
  570.  
  571. if (index == -1)
  572. {
  573. message("Data store " + id + " not found.");
  574. }
  575. }
  576.  
  577. return index;
  578. }
  579.  
  580. /*******************************************************************************
  581. **
  582. ** Function message(str)
  583. ** Inputs: String - message you want to send to the designated output
  584. ** Return: none
  585. ** Depends on: boolean debug to indicate if output is wanted
  586. ** object output to handle the messages. must implement a function
  587. ** log(string)
  588. **
  589. ** Description:
  590. ** This function outputs messages to a specified output. You can define your own
  591. ** output object. It will just need to implement a log(string) function. This
  592. ** interface was used so that the output could be assigned the window.console object.
  593. *******************************************************************************/
  594. function message(str)
  595. {
  596. if(debug)
  597. {
  598. output.log(str);
  599. }
  600. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement