PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/learn-portlet/src/main/webapp/js2.0/lesson-designer1/entities.js

https://github.com/ViLPy/JSCORM
JavaScript | 417 lines | 354 code | 36 blank | 27 comment | 14 complexity | 7957440875a0a0c40d7abd8da76414a6 MD5 | raw file
  1. /**
  2. * Created by igorborisov on 12.03.15.
  3. */
  4. lessonDesigner.module("Entities", function(Entities, lessonDesigner, Backbone, Marionette, $, _) {
  5. var apiUrl = path.api.quiz;
  6. var lessonService = new Backbone.Service({
  7. url: path.root,
  8. sync: {
  9. 'create': {
  10. 'path': apiUrl,
  11. 'data': function(model){
  12. return {
  13. action: 'ADD',
  14. title: model.get('title') || 'New lesson',
  15. description: model.get('description') || 'Lesson description',
  16. logo: model.get('logo') || '',
  17. maxDuration: model.get('maxDuration') || '',
  18. courseId: Utils.getCourseId()
  19. };
  20. },
  21. 'method': 'post'
  22. },
  23. 'update': {
  24. 'path': apiUrl,
  25. 'data': function(model){
  26. return {
  27. action: 'UPDATE',
  28. id: model.get('id'),
  29. title: model.get('title') || 'New lesson',
  30. description: model.get('description') || 'Lesson description',
  31. maxDuration: model.get('maxDuration') || '',
  32. courseId: Utils.getCourseId()
  33. };
  34. },
  35. 'method': 'post'
  36. },
  37. 'delete': {
  38. 'path': apiUrl,
  39. 'data': function(model){
  40. return {
  41. action: 'DELETE',
  42. id: model.get('id'),
  43. courseId: Utils.getCourseId()
  44. };
  45. },
  46. 'method': 'post'
  47. }
  48. },
  49. targets: {
  50. 'publish': {
  51. 'path': apiUrl,
  52. 'data': function (model) {
  53. return {
  54. action: 'PUBLISH',
  55. id: model.get('id'),
  56. courseId: Utils.getCourseId()
  57. };
  58. },
  59. 'method': 'post'
  60. },
  61. 'convert': {
  62. 'path': apiUrl,
  63. 'data': function (model) {
  64. return {
  65. action: 'CONVERT',
  66. id: model.get('id'),
  67. courseId: Utils.getCourseId()
  68. };
  69. },
  70. 'method': 'post'
  71. },
  72. 'clone': {
  73. 'path': apiUrl,
  74. 'data': function(model){
  75. return {
  76. action: 'CLONE',
  77. id: model.get('id'),
  78. courseId: Utils.getCourseId()
  79. };
  80. },
  81. 'method': 'post'
  82. },
  83. 'updateLogo': {
  84. 'path': apiUrl,
  85. 'data' : function (model) {
  86. var params = {
  87. action: 'UPDATELOGO',
  88. id: model.get('id'),
  89. logo: model.get('logo'),
  90. courseId: Utils.getCourseId()
  91. };
  92. return params;
  93. },
  94. 'method': 'post'
  95. }
  96. }
  97. });
  98. Entities.LessonModel = Backbone.Model.extend({
  99. defaults: {
  100. title: '',
  101. description: '',
  102. logo: ''
  103. }
  104. }).extend(lessonService);
  105. var lessonCollectionService = new Backbone.Service({
  106. url: path.root,
  107. sync: {
  108. 'read': {
  109. 'path': apiUrl,
  110. 'data': function(collection, options){
  111. //TODO complete parameters
  112. var filter = options.filter || {
  113. sort : 'nameAsc',
  114. searchtext: ''
  115. };
  116. var sortBy = 'TITLE'; //TITLE, CREATIONDATE, DESCRIPTION
  117. var sortAscDirection = true;
  118. switch (filter.sort) {
  119. case 'dateAsc':
  120. sortBy = 'CREATIONDATE'; sortAscDirection = true;
  121. break;
  122. case 'dateDesc':
  123. sortBy = 'CREATIONDATE'; sortAscDirection = false;
  124. break;
  125. case 'nameAsc':
  126. sortBy = 'TITLE'; sortAscDirection = true;
  127. break;
  128. case 'nameDesc':
  129. sortBy = 'TITLE'; sortAscDirection = false;
  130. break;
  131. }
  132. var params = {
  133. action: "GETALL",
  134. courseId: Utils.getCourseId(),
  135. filter: filter.searchtext || '',
  136. sortAscDirection: sortAscDirection,
  137. sortBy:sortBy,
  138. page: options.currentPage,
  139. count: options.itemsOnPage
  140. };
  141. return params;
  142. },
  143. 'method': 'get'
  144. }
  145. }
  146. });
  147. Entities.LessonCollection = Backbone.Collection.extend({
  148. model: Entities.LessonModel,
  149. parse: function(response){
  150. this.trigger('lessonCollection:updated', { total: response.total, currentPage: response.currentPage });
  151. _.forEach(response.records,function(record) {
  152. record.description = record.description;
  153. });
  154. return response.records;
  155. }
  156. }).extend(lessonCollectionService);
  157. Entities.Filter = Backbone.Model.extend({
  158. defaults: {
  159. searchtext: '',
  160. sort: 'nameAsc'
  161. },
  162. initialize: function(){}
  163. });
  164. var lessonContentModelService = new Backbone.Service({
  165. url: path.root,
  166. targets: {
  167. 'move': {
  168. 'path': path.api.quiz + '?action=MOVEELEMENT',
  169. 'data': function (model, options) {
  170. return {
  171. 'lessonId': model.get('lessonId'),
  172. 'id': model.id,
  173. 'categoryId': options.parentID,
  174. 'index': options.index
  175. }
  176. },
  177. 'method': 'post'
  178. }
  179. },
  180. sync: {
  181. 'create': {
  182. 'path': function (model, options) {
  183. if (options.bankQuestionIds) {
  184. return path.api.quiz + '?action=ADD' + model.get('contentType').toUpperCase() + "&" + options.bankQuestionIds +
  185. '&courseId=' + Utils.getCourseId();
  186. }
  187. else return path.api.quiz + '?action=ADD' + model.get('contentType').toUpperCase() +
  188. '&courseId=' + Utils.getCourseId();
  189. },
  190. 'method': 'post'
  191. },
  192. 'update': {
  193. 'path': function (model) {
  194. return path.api.quiz + '?action=UPDATE' + model.get('contentType').toUpperCase() +
  195. '&courseId=' + Utils.getCourseId();
  196. },
  197. 'method': 'post'
  198. },
  199. 'delete': {
  200. 'path': function (model) {
  201. var type = 'CATEGORY';
  202. if (model.get('contentType').indexOf('question') === 0) type = 'QUESTION';
  203. return path.api.quiz + '?action=DELETE' + type +
  204. '&courseId=' + Utils.getCourseId();
  205. },
  206. 'data': function(model) {
  207. return {lessonId: model.get('lessonId'), id: model.id};
  208. },
  209. 'method': 'post'
  210. }
  211. }
  212. });
  213. var LessonContentModel = Backbone.Model.extend({
  214. /* question
  215. ==============
  216. choiceQuestion,
  217. shortAnswerQuestion,
  218. numericQuestion,
  219. positioningQuestion,
  220. matchingQuestion,
  221. essayQuestion,
  222. categorizationQuestion
  223. plainText
  224. ==============
  225. welcomePage,
  226. finalPage,
  227. category,
  228. liferayArticle,
  229. picture,
  230. video,
  231. externalResource
  232. */
  233. defaults: {
  234. collapsed: true
  235. }
  236. }).extend(lessonContentModelService);
  237. //Questions
  238. //TODO Entities.LessonContent. ...
  239. Entities.QuestionModel = LessonContentModel.extend({
  240. defaults: {
  241. contentType: 'question',
  242. collapsed: true
  243. }
  244. });
  245. Entities.QuestionPlainTextModel = LessonContentModel.extend({
  246. defaults: {
  247. contentType: 'questionPlainText',
  248. collapsed: true
  249. }
  250. });
  251. Entities.CategoryModel = LessonContentModel.extend({
  252. defaults: {
  253. title: 'new category',
  254. description: '',
  255. contentType: 'category',
  256. collapsed: true,
  257. selected: false
  258. }
  259. });
  260. Entities.ExternalResourceModel = LessonContentModel.extend({
  261. defaults: {
  262. contentType: 'questionExternalResource',
  263. title: '',
  264. url: ''
  265. }
  266. });
  267. Entities.RevealJSModel = LessonContentModel.extend({
  268. defaults: {
  269. contentType: 'questionRevealJS',
  270. title: '',
  271. text: ''
  272. }
  273. });
  274. Entities.PDFModel = LessonContentModel.extend({
  275. defaults: {
  276. contentType: 'questionPDF',
  277. title: '',
  278. text: ''
  279. }
  280. });
  281. Entities.PPTXModel = LessonContentModel.extend({
  282. defaults: {
  283. contentType: 'questionPPTX',
  284. title: '',
  285. text: ''
  286. }
  287. });
  288. Entities.VideoModel = LessonContentModel.extend({
  289. defaults: {
  290. contentType: 'questionVideo',
  291. title: '',
  292. url: '',
  293. selected: false
  294. }
  295. });
  296. var lessonContentService = new Backbone.Service({
  297. url: path.root,
  298. sync: {
  299. 'read': {
  300. 'path': function (model, options) {
  301. return path.api.quiz + '?action=GETCONTENT' +
  302. '&id=' + options.id +
  303. '&courseId=' + Utils.getCourseId();
  304. },
  305. 'method': 'get'
  306. }
  307. }
  308. });
  309. Entities.LessonContentCollection = Backbone.Collection.extend({
  310. model: function (attrs, options) {
  311. switch (attrs.contentType) {
  312. case 'category':
  313. return new Entities.CategoryModel(attrs, options);
  314. case 'questionExternalResource':
  315. return new Entities.ExternalResourceModel(attrs, options);
  316. case 'questionPlainText':
  317. return new Entities.QuestionPlainTextModel(attrs, options);
  318. case 'questionRevealJS':
  319. return new Entities.RevealJSModel(attrs, options);
  320. case 'questionPDF':
  321. return new Entities.PDFModel(attrs, options);
  322. case 'questionPPTX':
  323. return new Entities.PPTXModel(attrs, options);
  324. case 'question':
  325. return new Entities.QuestionModel(attrs, options);
  326. case 'questionVideo':
  327. return new Entities.VideoModel(attrs, options);
  328. case 'questionVideoDL':
  329. return new Entities.VideoModel(attrs, options);
  330. default:
  331. throw Error('unsupport question type: ' + attrs.contentType);
  332. }
  333. },
  334. comparator: 'arrangementIndex'
  335. }).extend(lessonContentService);
  336. var articleCollectionService = new Backbone.Service({
  337. url: path.root,
  338. sync: {
  339. 'read': {
  340. 'path': function () {
  341. return '/services/liferay/article?companyID=' + GLOBAL_companyID;
  342. },
  343. 'method': 'get'
  344. }
  345. }
  346. });
  347. var articleService = new Backbone.Service({
  348. url: function () {
  349. return contentManagerActionUrl;
  350. },
  351. targets: {
  352. 'getContent': {
  353. 'path': function (model) {
  354. return '&companyID=' + GLOBAL_companyID
  355. + '&articleID=' + model.get('articleID')
  356. + '&language=' + model.get('localeId')
  357. + '&groupID=' + model.get('groupID');
  358. },
  359. 'method': 'get'
  360. }
  361. }
  362. });
  363. Entities.LiferayArticleModel = Backbone.Model.extend({
  364. parse: function (response) {
  365. var model = response;
  366. model.availableLocales = _.map(model.availableLocales, function (value, key) {
  367. value.id = key;
  368. return value;
  369. });
  370. model.localeId = model.availableLocales[0].id;
  371. model.title = model.titles[model.localeId];
  372. model.selected = false;
  373. return model;
  374. }
  375. }).extend(articleService);
  376. Entities.LiferayArticleCollection = Backbone.Collection.extend({
  377. model: Entities.LiferayArticleModel
  378. }).extend(articleCollectionService);
  379. });