/learn-portlet/src/main/webapp/js2.0/curriculum/models/CertificateModel.js

https://github.com/ViLPy/JSCORM · JavaScript · 241 lines · 227 code · 12 blank · 2 comment · 0 complexity · 6d25453deee3765b47d3541d0b500976 MD5 · raw file

  1. CertificateService = new Backbone.Service({ url: '/',
  2. sync: {
  3. 'create': {
  4. 'path': path.api.certificates,
  5. 'data': {
  6. action: 'ADD',
  7. courseId: Utils.getCourseId(),
  8. companyID: jQuery('#curriculumCompanyID').val()
  9. },
  10. 'method': 'post'
  11. },
  12. 'update': {
  13. 'path':function (model) {
  14. return path.api.certificates + model.id
  15. },
  16. 'data': function (model) {
  17. parameters = {
  18. action: 'UPDATE',
  19. courseId: Utils.getCourseId()
  20. }
  21. _.extend(parameters, model.toJSON())
  22. return parameters
  23. },
  24. 'method': 'post'
  25. },
  26. 'read': {
  27. 'path': function (model) {
  28. return path.api.certificates + model.id;
  29. },
  30. 'data': function (model) {
  31. return {
  32. action: 'GETBYID',
  33. courseId: Utils.getCourseId()
  34. }
  35. },
  36. 'method': 'get'
  37. },
  38. 'delete': {
  39. 'path': function (model) {
  40. return path.api.certificates + model.id
  41. },
  42. 'data': function (model) {
  43. return {
  44. action:'DELETE',
  45. courseId: Utils.getCourseId()
  46. }
  47. },
  48. 'method': 'post'
  49. }
  50. },
  51. targets: {
  52. 'clone': {
  53. 'path': function (model) {
  54. return path.api.certificates + model.id
  55. },
  56. 'data': function (model) {
  57. return {
  58. action: 'CLONE',
  59. courseId: Utils.getCourseId()
  60. }
  61. },
  62. method: 'post'
  63. },
  64. 'publish': {
  65. 'path': function (model) {
  66. return path.api.certificates + model.id
  67. },
  68. 'data': function (model) {
  69. return {
  70. action:'PUBLISH',
  71. courseId: Utils.getCourseId()
  72. }
  73. },
  74. method: 'post'
  75. },
  76. 'unpublish': {
  77. 'path': function (model) {
  78. return path.api.certificates + model.id
  79. },
  80. 'data': function (model) {
  81. return {
  82. action: 'UNPUBLISH',
  83. courseId: Utils.getCourseId()
  84. }
  85. },
  86. method: 'post'
  87. },
  88. 'join' : {
  89. 'path': function (model) {
  90. return path.api.certificates + model.id
  91. },
  92. 'data': function (model) {
  93. return {
  94. action: 'ADDUSER',
  95. userID: jQuery('#curriculumStudentID').val(),
  96. courseId: Utils.getCourseId()
  97. }
  98. },
  99. method: 'post'
  100. },
  101. 'leave' : {
  102. 'path': function (model) {
  103. return path.api.certificates + model.id
  104. },
  105. 'data': function (model) {
  106. return {
  107. action: 'DELETEUSER',
  108. userID: jQuery('#curriculumStudentID').val(),
  109. courseId: Utils.getCourseId()
  110. }
  111. },
  112. method: 'post'
  113. }
  114. }
  115. });
  116. var CertificateModel = Backbone.Model.extend({
  117. defaults: {
  118. title: '',
  119. description: '',
  120. sitesCount: 0,
  121. usersCount: 0,
  122. isPublished: false,
  123. isPermanent: true,
  124. validPeriod: {value: 0, valueType: 'UNLIMITED'},
  125. isOpenBadgesIntegration: false
  126. }
  127. }).extend(CertificateService);
  128. CertificateCollectionService = new Backbone.Service({ url: path.root,
  129. sync: {
  130. 'read': {
  131. 'path': path.api.certificates,
  132. 'data': function (e, options) {
  133. var order = options.order;
  134. var sortBy = order.split(':')[0];
  135. var asc = order.split(':')[1];
  136. return {
  137. action: 'GETALL',
  138. companyID: jQuery('#curriculumCompanyID').val(),
  139. page: options.currentPage,
  140. count: options.itemsOnPage,
  141. filter: options.filter,
  142. scope: options.scope,
  143. sortBy: sortBy,
  144. sortAscDirection: asc,
  145. resultAs: "short",
  146. courseId: Utils.getCourseId()
  147. }
  148. },
  149. method: 'get'
  150. }
  151. }
  152. });
  153. var CertificateCollection = Backbone.Collection.extend({
  154. model: CertificateModel,
  155. parse: function (response) {
  156. this.trigger('certificateCollection:updated', { total: response.total, currentPage: response.currentPage });
  157. return response.records;
  158. }
  159. }).extend(CertificateCollectionService);
  160. // my collection
  161. MyCertificateCollectionService = new Backbone.Service({ url: path.root,
  162. sync: {
  163. 'read': {
  164. 'path': function (e, options) {
  165. return path.api.users + jQuery('#curriculumStudentID').val() + '/certificates'
  166. },
  167. 'data': function (e, options) {
  168. var order = options.order;
  169. var sortBy = order.split(':')[0];
  170. var asc = order.split(':')[1];
  171. return {
  172. companyID: jQuery('#curriculumUserCompanyID').val(),
  173. page: options.currentPage,
  174. count: options.itemsOnPage,
  175. filter: options.filter,
  176. scope: options.scope,
  177. sortBy: sortBy,
  178. sortAscDirection: asc,
  179. resultAs: "short",
  180. isOnlyPublished: true,
  181. courseId: Utils.getCourseId()
  182. }
  183. },
  184. method: 'get'
  185. }
  186. }});
  187. var MyCertificateCollection = Backbone.Collection.extend({
  188. model: CertificateModel,
  189. parse: function (response) {
  190. this.trigger('certificateCollection:updated', { total: response.total, currentPage: response.currentPage });
  191. return response.records;
  192. }
  193. }).extend(MyCertificateCollectionService);
  194. // available certificates
  195. AvailableCertificateCollectionService = new Backbone.Service({ url: path.root,
  196. sync: {
  197. 'read': {
  198. 'path': function (e, options) {
  199. return path.api.users + jQuery('#curriculumStudentID').val() + '/certificates' +
  200. ''
  201. },
  202. 'data': function (e, options) {
  203. var order = options.order;
  204. var sortBy = order.split(':')[0];
  205. var asc = order.split(':')[1];
  206. return {
  207. companyID: jQuery('#curriculumUserCompanyID').val(),
  208. page: options.currentPage,
  209. count: options.itemsOnPage,
  210. filter: options.filter,
  211. sortBy: sortBy,
  212. sortAscDirection: asc,
  213. resultAs: "short",
  214. available:true,
  215. isOnlyPublished: true,
  216. scope: options.scope,
  217. courseId: Utils.getCourseId()
  218. }
  219. },
  220. method: 'get'
  221. }}
  222. });
  223. var AvailableCertificateCollection = Backbone.Collection.extend({
  224. model: CertificateModel,
  225. parse: function (response) {
  226. this.trigger('certificateCollection:updated', { total: response.total, currentPage: response.currentPage });
  227. return response.records;
  228. }
  229. }).extend(AvailableCertificateCollectionService);