PageRenderTime 39ms CodeModel.GetById 26ms app.highlight 10ms RepoModel.GetById 0ms app.codeStats 0ms

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