Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 8 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
//
2
//  DataService.swift
3
//  twogetskills
4
//
5
//  Created by Efrain Yanez Recanatini on 2/27/22.
6
//
7
 
8
import Foundation
9
import SwiftyJSON
10
 
11
class DataService
12
{
13
    let appData = AppData.sharedInstance
14
 
15
    func completeCapsule(topicModel : TopicModel, capsuleModel : CapsuleModel)
16
    {
17
        let now = Date()
18
        let dateFormatter = DateFormatter()
19
        dateFormatter.dateFormat = Constants.FORMAT_DATETIME_SERVICE
20
        let dateOn = dateFormatter.string(from: now)
21
 
22
 
23
        let userUuid = appData.userUuid
24
        var json : [String: Any]
25
        var sync : SyncModel
26
        var userLog : UserLogModel
27
 
28
        let syncDao = SyncDao.sharedInstance
29
        let progressDao = ProgressDao.sharedInstance
30
        let userLogDao = UserLogDao.sharedInstance
31
 
32
        var progressCapsule = progressDao.selectByCapsuleUuidAndUserUuid(capsuleUuid: capsuleModel.uuid, userUuid: userUuid)
33
        if progressCapsule.id > 0 && progressCapsule.progress >= 100 {
34
            progressCapsule.completed = 1
35
            progressCapsule.updatedOn = dateOn
36
            progressDao.update(record: progressCapsule)
37
 
38
            json = progressCapsule.toJson()
39
            json[Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME] = Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_PROGRESS
40
 
41
            sync = SyncModel();
42
            sync.type = Constants.SYNC_ADAPTER_TYPE_SYNC
43
            if let theJSONData = try?  JSONSerialization.data(withJSONObject: json, options: .prettyPrinted),
44
               let data = String(data: theJSONData, encoding: String.Encoding.ascii) {
45
                    sync.data = data
46
                }
47
 
48
            syncDao.insert(record : sync);
49
 
50
            userLog = UserLogModel()
51
            userLog.userUuid = userUuid
52
            userLog.companyUuid = topicModel.companyUuid
53
            userLog.topicUuid = topicModel.uuid
54
            userLog.capsuleUuid = capsuleModel.uuid
55
            userLog.activity = Constants.USER_LOG_ACTIVITY_COMPLETED_CAPSULE
56
            userLog.addedOn = dateOn
57
            userLogDao.insert(record: userLog)
58
 
59
            json = userLog.toJson()
60
            json[Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME] = Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_USER_LOG
61
 
62
            sync = SyncModel();
63
            sync.type = Constants.SYNC_ADAPTER_TYPE_SYNC
64
            if let theJSONData = try?  JSONSerialization.data(withJSONObject: json, options: .prettyPrinted),
65
                let data = String(data: theJSONData, encoding: String.Encoding.ascii) {
66
                    sync.data = data
67
                }
68
 
69
            syncDao.insert(record : sync);
70
        }
71
 
72
 
73
    }
74
 
75
 
76
    func completeTopic(topicModel : TopicModel, capsuleModel : CapsuleModel)
77
    {
78
        let now = Date()
79
        let dateFormatter = DateFormatter()
80
        dateFormatter.dateFormat = Constants.FORMAT_DATETIME_SERVICE
81
        let dateOn = dateFormatter.string(from: now)
82
 
83
 
84
        let userUuid = appData.userUuid
85
        var json : [String: Any]
86
        var sync : SyncModel
87
        var userLog : UserLogModel
88
 
89
        let syncDao = SyncDao.sharedInstance
90
        let progressDao = ProgressDao.sharedInstance
91
        let userLogDao = UserLogDao.sharedInstance
92
 
93
        var progressCapsule = progressDao.selectByCapsuleUuidAndUserUuid(capsuleUuid: capsuleModel.uuid, userUuid: userUuid)
94
        if progressCapsule.id > 0 && progressCapsule.progress >= 100 {
95
            progressCapsule.completed = 1
96
            progressCapsule.updatedOn = dateOn
97
            progressDao.update(record: progressCapsule)
98
 
99
            json = progressCapsule.toJson()
100
            json[Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME] = Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_PROGRESS
101
 
102
            sync = SyncModel();
103
            sync.type = Constants.SYNC_ADAPTER_TYPE_SYNC
104
            if let theJSONData = try?  JSONSerialization.data(withJSONObject: json, options: .prettyPrinted),
105
               let data = String(data: theJSONData, encoding: String.Encoding.ascii) {
106
                    sync.data = data
107
                }
108
 
109
            syncDao.insert(record : sync);
110
 
111
            userLog = UserLogModel()
112
            userLog.userUuid = userUuid
113
            userLog.companyUuid = topicModel.companyUuid
114
            userLog.topicUuid = topicModel.uuid
115
            userLog.capsuleUuid = capsuleModel.uuid
116
            userLog.activity = Constants.USER_LOG_ACTIVITY_COMPLETED_CAPSULE
117
            userLog.addedOn = dateOn
118
            userLogDao.insert(record: userLog)
119
 
120
            json = userLog.toJson()
121
            json[Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME] = Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_USER_LOG
122
 
123
            sync = SyncModel();
124
            sync.type = Constants.SYNC_ADAPTER_TYPE_SYNC
125
            if let theJSONData = try?  JSONSerialization.data(withJSONObject: json, options: .prettyPrinted),
126
                let data = String(data: theJSONData, encoding: String.Encoding.ascii) {
127
                    sync.data = data
128
                }
129
 
130
            syncDao.insert(record : sync);
131
        }
132
 
133
        var progressTopic = progressDao.selectByTopicUuidAndUserUuid(topicUuid: topicModel.uuid, userUuid: userUuid)
134
        if progressTopic.id > 0 && progressTopic.progress >= 100 {
135
            progressTopic.completed = 1
136
            progressTopic.updatedOn = dateOn
137
            progressDao.update(record: progressTopic)
138
 
139
            json = progressCapsule.toJson()
140
            json[Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME] = Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_PROGRESS
141
 
142
            sync = SyncModel();
143
            sync.type = Constants.SYNC_ADAPTER_TYPE_SYNC
144
            if let theJSONData = try?  JSONSerialization.data(withJSONObject: json, options: .prettyPrinted),
145
               let data = String(data: theJSONData, encoding: String.Encoding.ascii) {
146
                    sync.data = data
147
                }
148
 
149
            syncDao.insert(record : sync);
150
 
151
            userLog = UserLogModel()
152
            userLog.userUuid = userUuid
153
            userLog.companyUuid = topicModel.companyUuid
154
            userLog.topicUuid = topicModel.uuid
155
            userLog.activity = Constants.USER_LOG_ACTIVITY_COMPLETED_TOPIC
156
            userLog.addedOn = dateOn
157
            userLogDao.insert(record: userLog)
158
 
159
            json = userLog.toJson()
160
            json[Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME] = Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_USER_LOG
161
 
162
            sync = SyncModel();
163
            sync.type = Constants.SYNC_ADAPTER_TYPE_SYNC
164
            if let theJSONData = try?  JSONSerialization.data(withJSONObject: json, options: .prettyPrinted),
165
                let data = String(data: theJSONData, encoding: String.Encoding.ascii) {
166
                    sync.data = data
167
                }
168
 
169
            syncDao.insert(record : sync);
170
        }
171
    }
172
 
173
    func incompleteSlide (slide: SlideModel)
174
    {
175
        print("Notification: \(Constants.NOTIFICATION_NAME_INCOMPLETED_SLIDE) ")
176
        NotificationCenter.default.post(name:Constants.NOTIFICATION_NAME_INCOMPLETED_SLIDE,
177
                                        object: nil, userInfo: ["slideUuid": slide.uuid])
178
    }
179
 
180
    func completeSlide( slide : SlideModel)
181
    {
182
        var changeProgress = false
183
        let now = Date()
184
        let dateFormatter = DateFormatter()
185
        dateFormatter.dateFormat = Constants.FORMAT_DATETIME_SERVICE
186
        let dateOn = dateFormatter.string(from: now)
187
 
188
        let slideDao = SlideDao.sharedInstance
189
 
190
        let capsuleDao = CapsuleDao.sharedInstance
191
        let capsule = capsuleDao.selectByUuid(uuid: slide.capsuleUuid)
192
 
193
 
194
        let topicDao = TopicDao.sharedInstance
195
        let topic = topicDao.selectByUuid(uuid: slide.topicUuid)
196
 
197
 
198
        let userUuid = appData.userUuid
199
        var json : [String: Any]
200
        var sync : SyncModel
201
        var userLog : UserLogModel
202
 
203
        let syncDao = SyncDao.sharedInstance
204
        let progressDao = ProgressDao.sharedInstance
205
        let userLogDao = UserLogDao.sharedInstance
206
 
207
 
208
        var progressSlide = progressDao.selectBySlideUuidAndUserUuid(slideUuid: slide.uuid, userUuid: userUuid)
209
        var progressCapsule = progressDao.selectByCapsuleUuidAndUserUuid(capsuleUuid: slide.capsuleUuid, userUuid: userUuid)
210
        var progressTopic = progressDao.selectByTopicUuidAndUserUuid(topicUuid: slide.topicUuid, userUuid: userUuid)
211
 
212
        if progressTopic.id == 0 {
213
            userLog = UserLogModel()
214
            userLog.userUuid = userUuid
215
            userLog.companyUuid = topic.companyUuid
216
            userLog.topicUuid = topic.uuid
217
            userLog.activity = Constants.USER_LOG_ACTIVITY_START_TOPIC
218
            userLog.addedOn = dateOn
219
            userLogDao.insert(record: userLog)
220
 
221
            json = userLog.toJson()
222
            json[Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME] = Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_USER_LOG
223
 
224
            sync = SyncModel();
225
            sync.type = Constants.SYNC_ADAPTER_TYPE_SYNC
226
            if let theJSONData = try?  JSONSerialization.data(withJSONObject: json, options: .prettyPrinted),
227
                let data = String(data: theJSONData, encoding: String.Encoding.ascii) {
228
                    sync.data = data
229
                }
230
 
231
            syncDao.insert(record : sync);
232
        }
233
 
234
        if progressCapsule.id == 0 {
235
            userLog = UserLogModel()
236
            userLog.userUuid = userUuid
237
            userLog.companyUuid = topic.companyUuid
238
            userLog.topicUuid = topic.uuid
239
            userLog.capsuleUuid = capsule.uuid
240
            userLog.activity = Constants.USER_LOG_ACTIVITY_START_CAPSULE
241
            userLog.addedOn = dateOn
242
            userLogDao.insert(record: userLog)
243
 
244
            json = userLog.toJson()
245
            json[Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME] = Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_USER_LOG
246
 
247
            sync = SyncModel();
248
            sync.type = Constants.SYNC_ADAPTER_TYPE_SYNC
249
            if let theJSONData = try?  JSONSerialization.data(withJSONObject: json, options: .prettyPrinted),
250
                let data = String(data: theJSONData, encoding: String.Encoding.ascii) {
251
                    sync.data = data
252
                }
253
 
254
            syncDao.insert(record : sync);
255
        }
256
 
257
        if progressSlide.id == 0 {
258
            progressSlide.userUuid = userUuid
259
            progressSlide.companyUuid = topic.companyUuid
260
            progressSlide.topicUuid = topic.uuid
261
            progressSlide.capsuleUuid = capsule.uuid
262
            progressSlide.slideUuid = slide.uuid
263
            progressSlide.viewSlides = 0
264
            progressSlide.totalSlides = 0
265
            progressSlide.progress = 0
266
            progressSlide.type = Constants.PROGRESS_TYPE_SLIDE
267
            progressSlide.returning = 0
268
            progressSlide.returningAfterCompleted = 0
269
            progressSlide.completed = 1
270
            progressSlide.addedOn = dateOn
271
            progressSlide.updatedOn = dateOn
272
            progressDao.insert(record: progressSlide)
273
 
274
            changeProgress = true
275
        } else {
276
 
277
            if progressCapsule.id > 0 && progressCapsule.completed == 1 {
278
 
279
                progressSlide.returningAfterCompleted = progressSlide.returningAfterCompleted + 1
280
 
281
                progressSlide.updatedOn = dateOn
282
                progressDao.update(record: progressSlide)
283
            }
284
        }
285
 
286
        json = progressSlide.toJson()
287
        json[Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME] = Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_PROGRESS
288
 
289
        sync = SyncModel();
290
        sync.type = Constants.SYNC_ADAPTER_TYPE_SYNC
291
        if let theJSONData = try?  JSONSerialization.data(withJSONObject: json, options: .prettyPrinted),
292
           let data = String(data: theJSONData, encoding: String.Encoding.ascii) {
293
                sync.data = data
294
            }
295
 
296
        syncDao.insert(record : sync);
297
 
298
        userLog = UserLogModel()
299
        userLog.userUuid = userUuid
300
        userLog.companyUuid = topic.companyUuid
301
        userLog.topicUuid = topic.uuid
302
        userLog.capsuleUuid = capsule.uuid
303
        userLog.slideUuid = slide.uuid
304
        userLog.activity = slide.type == Constants.SLIDE_TYPE_QUIZ ? Constants.USER_LOG_ACTIVITY_APPROVED_TEST :  Constants.USER_LOG_ACTIVITY_VIEW_SLIDE
305
        userLog.addedOn = dateOn
306
        userLogDao.insert(record: userLog)
307
 
308
 
309
        json = userLog.toJson()
310
        json[Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME] = Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_USER_LOG
311
 
312
        sync = SyncModel();
313
        sync.type = Constants.SYNC_ADAPTER_TYPE_SYNC
314
        if let theJSONData = try?  JSONSerialization.data(withJSONObject: json, options: .prettyPrinted),
315
            let data = String(data: theJSONData, encoding: String.Encoding.ascii) {
316
                sync.data = data
317
            }
318
 
319
        syncDao.insert(record : sync);
320
 
321
 
322
 
323
 
324
 
325
 
326
        if changeProgress {
327
 
328
            var totalSlides = slideDao.getCountByCapsuleUuid(capsuleUuid: slide.capsuleUuid)
329
            var totalViews = progressDao.getCountSlideCompletedByCapsuleUuidAndUserUuid(capsuleUuid: slide.capsuleUuid, userUuid: userUuid)
330
            var progress = totalSlides > 0 ? ((totalViews * 100) / totalSlides) : 0
331
 
332
 
333
            if progressCapsule.id == 0 {
334
 
335
                progressCapsule = ProgressModel()
336
                progressCapsule.userUuid = userUuid
337
                progressCapsule.companyUuid = topic.companyUuid
338
                progressCapsule.topicUuid = slide.topicUuid
339
                progressCapsule.capsuleUuid = slide.capsuleUuid
340
                progressCapsule.type = Constants.PROGRESS_TYPE_CAPSULE
341
 
342
                progressCapsule.addedOn = dateOn
343
                progressCapsule.updatedOn = dateOn
344
                progressCapsule.viewSlides = totalViews
345
                progressCapsule.totalSlides = totalSlides
346
                progressCapsule.progress = Double(progress)
347
 
348
                progressDao.insert(record: progressCapsule)
349
 
350
            } else {
351
                progressCapsule.updatedOn = dateOn
352
                progressCapsule.viewSlides = totalViews
353
                progressCapsule.totalSlides = totalSlides
354
                progressCapsule.progress = Double(progress)
355
 
356
 
357
                if progressCapsule.completed == 1 && slide.capsuleUuid != appData.capsuleUuidOld {
358
                    progressCapsule.returningAfterCompleted = progressCapsule.returningAfterCompleted + 1
359
                    progressDao.update(record: progressCapsule)
360
 
361
 
362
 
363
                    appData.capsuleUuidOld = slide.capsuleUuid
364
                    appData.save()
365
 
366
                } else {
367
                    progressDao.update(record: progressCapsule)
368
                }
369
            }
370
 
371
 
372
 
373
 
374
            json = progressSlide.toJson()
375
            json[Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME] = Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_PROGRESS
376
 
377
            sync = SyncModel();
378
            sync.type = Constants.SYNC_ADAPTER_TYPE_SYNC
379
            if let theJSONData = try?  JSONSerialization.data(withJSONObject: json, options: .prettyPrinted),
380
               let data = String(data: theJSONData, encoding: String.Encoding.ascii) {
381
                    sync.data = data
382
                }
383
 
384
            syncDao.insert(record : sync);
385
 
386
            totalSlides = slideDao.getCountByTopicUuid(topicUuid: slide.topicUuid)
387
            totalViews = progressDao.getCountSlideCompletedByTopicUuidAndUserUuid(topicUuid: slide.topicUuid, userUuid: userUuid)
388
            progress = totalSlides > 0 ? ((totalViews * 100) / totalSlides) : 0
389
 
390
 
391
            if progressTopic.id == 0 {
392
                progressTopic = ProgressModel()
393
                progressTopic.userUuid = userUuid
394
                progressTopic.companyUuid = topic.companyUuid
395
                progressTopic.topicUuid = slide.topicUuid
396
                progressTopic.type = Constants.PROGRESS_TYPE_TOPIC
397
 
398
                progressTopic.addedOn = dateOn
399
                progressTopic.updatedOn = dateOn
400
                progressTopic.viewSlides = totalViews
401
                progressTopic.totalSlides = totalSlides
402
                progressTopic.progress = Double(progress)
403
 
404
                progressDao.insert(record: progressTopic)
405
 
406
            } else {
407
 
408
                progressTopic.updatedOn = dateOn
409
                progressTopic.viewSlides = totalViews
410
                progressTopic.totalSlides = totalSlides
411
                progressTopic.progress = Double(progress)
412
                progressDao.update(record: progressTopic)
413
            }
414
 
415
 
416
 
417
            json = progressSlide.toJson()
418
            json[Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME] = Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_PROGRESS
419
 
420
            sync = SyncModel();
421
            sync.type = Constants.SYNC_ADAPTER_TYPE_SYNC
422
            if let theJSONData = try?  JSONSerialization.data(withJSONObject: json, options: .prettyPrinted),
423
               let data = String(data: theJSONData, encoding: String.Encoding.ascii) {
424
                   sync.data = data
425
                }
426
 
427
            syncDao.insert(record : sync);
428
        }
429
 
430
 
431
        print("Notification: \(Constants.NOTIFICATION_NAME_COMPLETED_SLIDE) ")
432
        NotificationCenter.default.post(name:Constants.NOTIFICATION_NAME_COMPLETED_SLIDE,
433
                                        object: nil, userInfo: ["slideUuid": slide.uuid])
434
 
435
        if (changeProgress) {
436
 
437
            print("Notification: \(Constants.NOTIFICATION_NAME_CHANGE_PERCENTAJE_COMPLETED_CAPSULE) ")
438
            NotificationCenter.default.post(name: Constants.NOTIFICATION_NAME_CHANGE_PERCENTAJE_COMPLETED_CAPSULE, object: nil, userInfo:["capsuleUuid": slide.capsuleUuid])
439
 
440
 
441
            print("Notification: \(Constants.NOTIFICATION_NAME_CHANGE_PERCENTAJE_COMPLETED_TOPIC) ")
442
            NotificationCenter.default.post(name: Constants.NOTIFICATION_NAME_CHANGE_PERCENTAJE_COMPLETED_TOPIC, object: nil, userInfo:["topicUuid": slide.topicUuid])
443
 
444
        }
445
 
446
    }
447
 
448
 
449
 
450
 
451
   public func syncFromServer(json : JSON?) -> Bool
452
    {
453
 
454
        let companyDao = CompanyDao.sharedInstance
455
        let topicDao = TopicDao.sharedInstance
456
        let capsuleDao = CapsuleDao.sharedInstance
457
        let slideDao = SlideDao.sharedInstance
458
        let quizDao = QuizDao.sharedInstance
459
        let questionDao = QuestionDao.sharedInstance
460
        let answerDao = AnswerDao.sharedInstance
461
        let userLogDao = UserLogDao.sharedInstance
462
        let progressDao = ProgressDao.sharedInstance
463
        let userExtendedDao = UserExtendedDao.sharedInstance
464
 
465
        answerDao.removeAll()
466
        questionDao.removeAll()
467
        quizDao.removeAll()
468
 
469
        slideDao.removeAll()
470
        capsuleDao.removeAll()
471
        topicDao.removeAll()
472
        companyDao.removeAll()
473
 
474
        userLogDao.removeAll()
475
        progressDao.removeAll()
476
        userExtendedDao.removeAll()
477
 
478
        var companyModel : CompanyModel
479
        let now = Date()
480
        let dateFormatter = DateFormatter()
481
        dateFormatter.dateFormat = Constants.FORMAT_DATETIME_SERVICE
482
 
483
 
484
 
485
        if(json?["data"]["max_date_changes"] != "") {
486
            let maxDateChanges = json?["data"]["max_date_changes"].string ?? ""
487
 
488
            print("maxDateChanges : \(maxDateChanges)")
489
 
490
            appData.maxDateChanges = maxDateChanges
491
        }
492
 
493
        if(json?["data"]["device"] != "") {
494
            let deviceAes = json?["data"]["device"]["aes"].string ?? ""
495
            let devicePassword = json?["data"]["device"]["password"].string ?? ""
496
 
497
            print("deviceAes: \(deviceAes)")
498
            print("devicePassword: \(devicePassword)")
499
            appData.aes = deviceAes
500
            appData.password = devicePassword
501
 
502
        }
503
        if(json?["data"]["user"] != "") {
504
            let userUuid = json?["data"]["user"]["uuid"].string ?? ""
505
            let userFirstName = json?["data"]["user"]["first_name"].string ?? ""
506
            let userLastName = json?["data"]["user"]["last_name"].string ?? ""
507
            let userEmail = json?["data"]["user"]["email"].string ?? ""
508
            let userImage = json?["data"]["user"]["image"].string ?? ""
509
 
510
 
511
            print("userUuid : \(userUuid)")
512
            print("userFirstName : \(userFirstName)")
513
            print("userLastName: \(userLastName)")
514
            print("userEmail : \(userEmail)")
515
            print("userImage : \(userImage)")
516
 
517
 
518
            appData.userUuid = userUuid
519
            appData.firstName = userFirstName
520
            appData.lastName =  userLastName
521
            appData.email = userEmail
522
            appData.image = userImage
523
 
524
 
525
 
526
        }
527
 
528
        appData.topicUuidActive = Constants.APP_DATA_EMPTY_ID
529
        appData.capsuleUuidActive = Constants.APP_DATA_EMPTY_ID
530
        appData.slideUuidActive = Constants.APP_DATA_EMPTY_ID
531
        appData.save()
532
 
533
 
534
        if json?["data"]["topics"] != "" {
535
            var topicModel : TopicModel
536
            var capsuleModel : CapsuleModel
537
            var slideModel : SlideModel
538
 
539
            for topic in json!["data"]["topics"]
540
            {
541
 
542
                companyModel = CompanyModel()
543
                companyModel.uuid = topic.1["company_uuid"].string ?? ""
544
                companyModel.name = topic.1["company_name"].string ?? ""
545
                companyModel.image = topic.1["company_image"].string ?? ""
546
 
547
                print("companyUuid : \(companyModel.uuid)")
548
                print("companyName : \(companyModel.name)")
549
                print("companyImage : \(companyModel.image)")
550
 
551
                let company = companyDao.selectByUuid(uuid:  companyModel.uuid)
552
                if(company.uuid.isEmpty) {
553
                    companyDao.insert(company: companyModel)
554
                } else {
555
                    companyDao.update(company: companyModel)
556
                }
557
 
558
                topicModel = TopicModel()
559
                topicModel.companyUuid = companyModel.uuid
560
                topicModel.uuid = topic.1["uuid"].string ?? ""
561
                topicModel.name  = topic.1["name"].string ?? ""
562
                topicModel.description = topic.1["description"].string ?? ""
563
                topicModel.image = topic.1["image"].string ?? ""
564
                topicModel.position = Int(topic.1["position"].string ?? "") ?? 1
565
                topicModel.addedOn = topic.1["added_on"].string ?? ""
566
                topicModel.updatedOn = topic.1["updated_on"].string ?? ""
567
 
568
 
569
                print("topiccompanyUuid: \(topicModel.companyUuid)")
570
                print("topicUuid: \(topicModel.uuid)")
571
                print("topicName: \(topicModel.name)")
572
                print("topicDescription: \(topicModel.description)")
573
                print("topicImage: \(topicModel.image)")
574
                print("topicPosition: \(topicModel.position)")
575
 
576
 
577
 
578
                // Insert Topics in DB
579
                topicDao.insert(topic: topicModel)
580
 
581
                for capsule in topic.1["capsules"]
582
                {
583
 
584
                    capsuleModel = CapsuleModel()
585
                    capsuleModel.topicUuid = topicModel.uuid
586
                    capsuleModel.uuid = capsule.1["uuid"].string ?? ""
587
                    capsuleModel.name = capsule.1["name"].string ?? ""
588
                    capsuleModel.description = capsule.1["description"].string ?? ""
589
                    capsuleModel.image = capsule.1["image"].string ?? ""
590
                    capsuleModel.position = Int(capsule.1["position"].string ?? "") ?? 1
591
                    capsuleModel.addedOn = capsule.1["added_on"].string ?? ""
592
                    capsuleModel.updatedOn = capsule.1["updated_on"].string ?? ""
593
 
594
                    print("capsuletopicUuid: \(capsuleModel.topicUuid)")
595
                    print("capsuleUuid: \(capsuleModel.uuid)")
596
                    print("capsuleName: \(capsuleModel.name)")
597
                    print("capsuleDescription: \(capsuleModel.description)")
598
                    print("capsuleImage: \(capsuleModel.image)")
599
                    print("capsulePosition: \(capsuleModel.position)")
600
 
601
 
602
                    // Insert Capsules in DB
603
                    capsuleDao.insert(capsule: capsuleModel)
604
 
605
                    for slide in capsule.1["slides"]
606
                    {
607
                        slideModel = SlideModel()
608
                        slideModel.topicUuid = capsuleModel.topicUuid
609
                        slideModel.capsuleUuid = capsuleModel.uuid
610
                        slideModel.uuid = slide.1["uuid"].string ?? ""
611
                        slideModel.quizUuid = slide.1["quiz_uuid"].string ?? ""
612
                        slideModel.name = slide.1["name"].string ?? ""
613
                        slideModel.description = slide.1["description"].string ?? ""
614
                        slideModel.position = Int(slide.1["position"].string ?? "") ?? 1
615
                        slideModel.background = slide.1["background"].string ?? ""
616
                        slideModel.file = slide.1["file"].string ?? ""
617
                        slideModel.type = slide.1["type"].string ?? ""
618
                        slideModel.addedOn = slide.1["added_on"].string ?? ""
619
                        slideModel.updatedOn = slide.1["updated_on"].string ?? ""
620
 
621
 
622
                        print("slidetopicUuid: \(slideModel.topicUuid)")
623
                        print("slidecapsuleUuid: \(slideModel.capsuleUuid)")
624
                        print("slideUuid: \(slideModel.uuid)")
625
                        print("slideQuizUuid: \(slideModel.quizUuid)")
626
                        print("slideName: \(slideModel.name)")
627
                        print("slideDescription: \(slideModel.description)")
628
                        print("slidePosition: \(slideModel.position)")
629
                        print("slideBackground: \(slideModel.background)")
630
                        print("slideFile: \(slideModel.file)")
631
                        print("slideTye: \(slideModel.type)")
632
 
633
                        // Insert Slides in DB
634
                        slideDao.insert(slide: slideModel)
635
                    }
636
                }
637
            }
638
        }
639
 
640
        if json?["data"]["quizzes"] != "" {
641
            var quizModel : QuizModel
642
            var questionModel : QuestionModel
643
            var answerModel  : AnswerModel
644
            for quiz in json!["data"]["quizzes"]
645
            {
646
 
647
 
648
                companyModel = CompanyModel()
649
                companyModel.uuid = quiz.1["company_uuid"].string ?? ""
650
                companyModel.name = quiz.1["company_name"].string ?? ""
651
                companyModel.image =  quiz.1["company_image"].string ?? ""
652
 
653
                print("companyUuid : \(companyModel.uuid)")
654
                print("companyName : \(companyModel.name)")
655
                print("companyImage : \(companyModel.image)")
656
 
657
                let company = companyDao.selectByUuid(uuid:  companyModel.uuid)
658
                if(company.uuid.isEmpty) {
659
                    companyDao.insert(company: companyModel)
660
                } else {
661
                    companyDao.update(company: companyModel)
662
                }
663
 
664
                quizModel = QuizModel()
665
                quizModel.companyUuid = company.uuid
666
                quizModel.uuid = quiz.1["uuid"].string ?? ""
667
                quizModel.name = quiz.1["name"].string ?? ""
668
                quizModel.text = quiz.1["text"].string ?? ""
669
                quizModel.failed = quiz.1["failed"].string ?? ""
670
                quizModel.points = Int(quiz.1["points"].string ?? "") ?? 0
671
                quizModel.minimumPointsRequired = Int(quiz.1["minimum_points_required"].string ?? "") ?? 0
672
                quizModel.maxTime = Int(quiz.1["max_time"].string ?? "") ?? 0
673
                quizModel.addedOn = quiz.1["added_on"].string ?? ""
674
                quizModel.updatedOn = quiz.1["updated_on"].string ?? ""
675
 
676
 
677
                print("quizcompanyUuid: \(quizModel.companyUuid)")
678
                print("quizUuid: \(quizModel.uuid)")
679
                print("quizName: \(quizModel.name)")
680
                print("quizText: \(quizModel.text)")
681
                print("quizFailed: \(quizModel.failed)")
682
                print("quizPoints: \(quizModel.points)")
683
                print("quizMinimumPointsRequired: \(quizModel.minimumPointsRequired)")
684
                print("quizMaxTime: \(quizModel.maxTime)")
685
 
686
 
687
                quizDao.insert(quiz: quizModel)
688
 
689
                for question in quiz.1["questions"]
690
                {
691
                    questionModel = QuestionModel()
692
                    questionModel.quizUuid = quizModel.uuid
693
                    questionModel.uuid = question.1["uuid"].string ?? ""
694
                    questionModel.text  = question.1["text"].string ?? ""
695
                    questionModel.type = question.1["type"].string ?? ""
696
                    questionModel.points  = Int(question.1["points"].string ?? "") ?? 0
697
                    questionModel.max_length = Int(question.1["maxlength"].string ?? "") ?? 0
698
                    questionModel.addedOn = question.1["added_on"].string ?? ""
699
                    questionModel.updatedOn = question.1["updated_on"].string ?? ""
700
 
701
                    print("questionQuizUuid: \(questionModel.quizUuid)")
702
                    print("questionId: \(questionModel.uuid)")
703
                    print("questionText: \(questionModel.text)")
704
                    print("questionType: \(questionModel.type)")
705
                    print("questionPoints: \(questionModel.points)")
706
                    print("questionMaxLength: \(questionModel.max_length)")
707
 
708
 
709
                    questionDao.insert(question: questionModel)
710
 
711
                    for answer in question.1["answers"]
712
                    {
713
                        answerModel = AnswerModel()
714
                        answerModel.questionUuid = questionModel.uuid
715
                        answerModel.uuid  = answer.1["uuid"].string ?? ""
716
                        answerModel.text = answer.1["text"].string ?? ""
717
                        answerModel.correct = Int(answer.1["correct"].string ?? "") ?? 0
718
                        answerModel.points = Int(answer.1["points"].string ?? "") ?? 0
719
                        answerModel.addedOn = answer.1["added_on"].string ?? ""
720
                        answerModel.updatedOn = answer.1["updated_on"].string ?? ""
721
 
722
                        print("answerQuestionUuid: \(answerModel.questionUuid)")
723
                        print("answerId: \(answerModel.uuid)")
724
                        print("answerText: \(answerModel.text)")
725
                        print("answerCorrect: \(answerModel.correct)")
726
 
727
 
728
 
729
                        answerDao.insert(answer: answerModel)
730
 
731
                    }
732
 
733
 
734
                }
735
 
736
            }
737
        }
738
 
739
        if json?["data"]["userlog"] != "" {
740
 
741
            var userLogModel : UserLogModel
742
            for userlog in json!["data"]["userlog"]
743
            {
744
                userLogModel = UserLogModel()
745
                userLogModel.companyUuid = userlog.1["company_uuid"].string ?? ""
746
                userLogModel.userUuid = userlog.1["user_uuid"].string ?? ""
747
                userLogModel.topicUuid = userlog.1["topic_uuid"].string ?? ""
748
                userLogModel.capsuleUuid = userlog.1["capsule_uuid"].string ?? ""
749
                userLogModel.slideUuid = userlog.1["slide_uuid"].string ?? ""
750
                userLogModel.activity = userlog.1["activity"].string ?? ""
751
                userLogModel.addedOn = userlog.1["added_on"].string ?? ""
752
 
753
                userLogDao.insert(record: userLogModel)
754
            }
755
        }
756
 
757
        if json?["data"]["extended"] != "" {
758
 
759
            var userExtendedModel : UserExtendedModel
760
            for userExtended in json!["data"]["extended"]
761
            {
762
                companyModel = CompanyModel()
763
                companyModel.uuid = userExtended.1["company_uuid"].string ?? ""
764
                companyModel.name = userExtended.1["company_name"].string ?? ""
765
                companyModel.image =  userExtended.1["company_image"].string ?? ""
766
 
767
                print("companyUuid : \(companyModel.uuid)")
768
                print("companyName : \(companyModel.name)")
769
                print("companyImage : \(companyModel.image)")
770
 
771
                let company = companyDao.selectByUuid(uuid: companyModel.uuid)
772
                if(company.uuid.isEmpty) {
773
                    companyDao.insert(company: companyModel)
774
                } else {
775
                    companyDao.update(company: companyModel)
776
                }
777
 
778
                for detail in userExtended.1["details"]
779
                {
780
                    userExtendedModel = UserExtendedModel()
781
                    userExtendedModel.companyUuid = companyModel.uuid
782
                    userExtendedModel.uuid = detail.1["uuid"].string ?? ""
783
                    userExtendedModel.label = detail.1["label"].string ?? ""
784
                    userExtendedModel.value = detail.1["value"].string ?? ""
785
 
786
                    userExtendedDao.insert(record: userExtendedModel)
787
                }
788
            }
789
        }
790
 
791
        if json?["data"]["progress"] != "" {
792
 
793
            var progressModel : ProgressModel
794
            for progress in json!["data"]["progress"] {
795
 
796
 
797
                let userUuid = progress.1["user_uuid"].string ?? ""
798
                let topicUuid = progress.1["topic_uuid"].string ?? ""
799
                let capsuleUuid = progress.1["capsule_uuid"].string ?? ""
800
                let slideUuid = progress.1["slide_uuid"].string ?? ""
801
                let type = progress.1["type"].string ?? ""
802
 
803
 
804
                switch type {
805
                    case Constants.PROGRESS_TYPE_TOPIC:
806
                        progressModel = progressDao.selectByTopicUuidAndUserUuid(topicUuid: topicUuid, userUuid: userUuid)
807
                        break
808
 
809
                    case Constants.PROGRESS_TYPE_CAPSULE:
810
                        progressModel = progressDao.selectByCapsuleUuidAndUserUuid(capsuleUuid: capsuleUuid, userUuid: userUuid)
811
                        break
812
 
813
                    case Constants.PROGRESS_TYPE_SLIDE:
814
                        progressModel = progressDao.selectBySlideUuidAndUserUuid(slideUuid: slideUuid, userUuid: userUuid)
815
                        break
816
                    default:
817
                        continue
818
                    }
819
 
820
 
821
                if progressModel.id == 0 {
822
                    progressModel = ProgressModel()
823
                    progressModel.userUuid = userUuid
824
                    progressModel.topicUuid = topicUuid
825
                    progressModel.capsuleUuid = capsuleUuid
826
                    progressModel.slideUuid = slideUuid
827
                    progressModel.type = type
828
                    progressModel.companyUuid = progress.1["company_uuid"].string ?? ""
829
                    progressModel.progress = Double(progress.1["progress"].string ?? "") ?? 0.0
830
                    progressModel.totalSlides = Int(progress.1["total_slides"].string ?? "") ?? 0
831
                    progressModel.viewSlides = Int(progress.1["view_slides"].string ?? "") ?? 0
832
                    progressModel.returning = Int(progress.1["returning"].string ?? "") ?? 0
833
                    progressModel.returningAfterCompleted = Int(progress.1["returning_after_completed"].string ?? "") ?? 0
834
                    progressModel.completed = Int(progress.1["completed"].string ?? "") ?? 0
835
                    progressModel.addedOn = progress.1["added_on"].string ?? ""
836
                    progressModel.updatedOn = progress.1["updated_on"].string ?? ""
837
 
838
                    progressDao.insert(record: progressModel)
839
                }
840
 
841
            }
842
        }
843
 
844
        /*
845
        let now = Date()
846
        let dateFormatter = DateFormatter()
847
        dateFormatter.dateFormat = Constants.FORMAT_DATETIME_SERVICE
848
        let dateOn = dateFormatter.string(from: now)
849
 
850
        userUuid = appData.string(forKey: Constants.APP_DATA_FIELD_USER_UUID) ?? ""
851
 
852
 
853
        var userLog = UserLogModel()
854
        userLog.userUuid = userUuid
855
        userLog.companyUuid = ""
856
        userLog.topicUuid = ""
857
        userLog.capsuleUuid = ""
858
        userLog.slideUuid = ""
859
        userLog.activity = Constants.USER_LOG_ACTIVITY_SIGNIN;
860
        userLog.addedOn =  dateOn
861
 
862
        userLogDao.insert(record : userLog);
863
 
864
        var json = userLog.toJson()
865
        json[Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME] = Constants.SYNC_ADAPTER_DATA_TYPE_USER_LOG
866
 
867
        var sync = SyncModel()
868
        sync.type = Constants.SYNC_ADAPTER_TYPE_SYNC
869
        if let theJSONData = try?  JSONSerialization.data(withJSONObject: json, options: .prettyPrinted),
870
            let data = String(data: theJSONData, encoding: String.Encoding.ascii) {
871
            sync.data = data
872
        }
873
 
874
        let syncDao = SyncDao.sharedInstance
875
        syncDao.insert(record : sync)
876
        */
877
 
878
        return true;
879
    }
880
 
881
 
882
}