Proyectos de Subversion LeadersLinked - Services

Rev

Rev 558 | Rev 564 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 558 Rev 563
Línea 1277... Línea 1277...
1277
        }
1277
        }
1278
    }
1278
    }
Línea 1279... Línea 1279...
1279
    
1279
    
1280
    public function getTopicAction()
1280
    public function getTopicAction()
-
 
1281
    {
1281
    {
1282
        // Handle GET request
1282
        $request = $this->getRequest();
1283
        $request = $this->getRequest();
-
 
1284
        if($request->isGet()) {
1283
        if($request->isGet()) {
1285
            // Get current user
1284
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1286
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1285
            $currentUser = $currentUserPlugin->getUser();
-
 
1286
            
1287
            $currentUser = $currentUserPlugin->getUser();
-
 
1288
 
1287
            
1289
            // Get topic ID from route
1288
            $id = $this->params()->fromRoute('id');
1290
            $id = $this->params()->fromRoute('id');
1289
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
1291
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
1290
            $topic = $topicMapper->fetchOneByUuid($id);
1292
            $topic = $topicMapper->fetchOneByUuid($id);
-
 
1293
 
1291
            
1294
            // Return error if topic not found
1292
            if(!$topic) {
1295
            if(!$topic) {
1293
                return new JsonModel([
1296
                return new JsonModel([
1294
                    'success' => false,
1297
                    'success' => false,
1295
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1298
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1296
                ]);
1299
                ]);
1297
            }
1300
            }
-
 
1301
 
1298
            
1302
            // Check if current user has access to the topic
1299
            $accessGrantedIds = $this->getAccessGranted();
-
 
1300
            
1303
            $accessGrantedIds = $this->getAccessGranted();
1301
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
1304
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
1302
                return new JsonModel([
1305
                return new JsonModel([
1303
                    'success' => false,
1306
                    'success' => false,
1304
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
1307
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
1305
                ]);
1308
                ]);
1306
            }
-
 
1307
            
1309
            }
1308
            
-
 
1309
            $data = [];
1310
 
1310
            $accessGrantedIds = $this->getAccessGranted();
-
 
1311
            
1311
            // Get user progress for this topic
-
 
1312
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
Línea -... Línea 1313...
-
 
1313
            $userProgress = $userProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $topic->id);
1312
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
1314
 
1313
 
1315
            $progress = 0;
1314
            $userProgress = $userProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $id);
1316
            $completed = false;
1315
            if($userProgress) {
1317
            if($userProgress) {
1316
                $progress = $userProgress->progress;
-
 
1317
                $completed = $userProgress->completed;
-
 
1318
            } else {
-
 
1319
                $progress = 0;
1318
                $progress = $userProgress->progress;
1320
                $completed = 0;
1319
                $completed = $userProgress->completed;
-
 
1320
            }
1321
            }
1321
 
1322
                
1322
            // Initialize storage service to get image paths
-
 
1323
            $storage = Storage::getInstance($this->config, $this->adapter);
-
 
1324
            $pathTopic = $storage->getPathMicrolearningTopic();
-
 
1325
 
1323
            $storage = Storage::getInstance($this->config, $this->adapter);
1326
            // Fetch associated capsules using the private helper method
-
 
1327
            $capsulesData = $this->_getCapsulesByTopic($topic, $storage);
1324
            $path = $storage->getPathMicrolearningTopic();
1328
 
1325
                
1329
            // Prepare data for JSON response
1326
            $data = [
1330
            $data = [
1327
                'uuid'          => $topic->uuid,
1331
                'uuid'          => $topic->uuid,
1328
                'name'          => $topic->name ? $topic->name : '',
1332
                'name'          => $topic->name ? $topic->name : '',
1329
                'description'   => $topic->description ? $topic->description : '',
1333
                'description'   => $topic->description ? $topic->description : '',
1330
                'image'         => $topic->image ? $storage->getGenericImage($path, $topic->uuid, $topic->image) : '',
1334
                'image'         => $topic->image ? $storage->getGenericImage($pathTopic, $topic->uuid, $topic->image) : '',
1331
                'progress'      => $progress,
1335
                'progress'      => $progress,
1332
                'completed'     => $completed,
1336
                'completed'     => $completed,
1333
                'order'         => $topic->order,
1337
                'order'         => $topic->order,
1334
                'added_on'      => $topic->added_on,
1338
                'added_on'      => $topic->added_on,
1335
                'updated_on'    => $topic->updated_on,
-
 
-
 
1339
                'updated_on'    => $topic->updated_on,
1336
                'link_capsules' => $this->url()->fromRoute('microlearning/capsules', ['topic_id' => $topic->uuid], ['force_canonical' => true]),
1340
                'total_capsules' => count($capsulesData),
1337
                    
-
 
1338
            ];
1341
                'capsules'      => $capsulesData,
1339
                
-
 
1340
                
1342
            ];
1341
                
1343
 
1342
            return new JsonModel([
1344
            return new JsonModel([
1343
                'success' => true,
1345
                'success' => true,
1344
                'data' => $data
1346
                'data' => $data
1345
            ]);
1347
            ]);
-
 
1348
 
1346
                
1349
        } else {
1347
        } else {
1350
            // Return error if not a GET request
1348
            return new JsonModel([
1351
            return new JsonModel([
1349
                'success' => false,
1352
                'success' => false,
1350
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1353
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1351
            ]);
1354
            ]);
-
 
1355
        }
-
 
1356
    }
-
 
1357
 
-
 
1358
    /**
-
 
1359
     * Fetches and formats capsules associated with a given topic.
-
 
1360
     *
-
 
1361
     * @param mixed $topic The topic object.
-
 
1362
     * @param Storage $storage The storage service instance.
-
 
1363
     * @return array An array of formatted capsule data.
-
 
1364
     */
-
 
1365
    private function _getCapsulesByTopic($topic, $storage)
-
 
1366
    {
-
 
1367
        $capsulesData = [];
-
 
1368
        // Get path for capsule images
-
 
1369
        $pathCapsule = $storage->getPathMicrolearningCapsule();
-
 
1370
        
-
 
1371
        // Initialize mappers
-
 
1372
        $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
-
 
1373
        $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
-
 
1374
        $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
-
 
1375
 
-
 
1376
        // Fetch all active capsules for the given topic ID
-
 
1377
        $capsules = $capsuleMapper->fetchAllByTopicId($topic->id, true);
-
 
1378
 
-
 
1379
        if(count($capsules) > 0) {
-
 
1380
            // Get current user
-
 
1381
            $currentUserPlugin = $this->plugin('currentUserPlugin');
-
 
1382
            $currentUser = $currentUserPlugin->getUser();
-
 
1383
 
-
 
1384
            // Iterate through each capsule to prepare its data
-
 
1385
            foreach($capsules as $capsule) {
-
 
1386
                // Get user progress for this capsule
-
 
1387
                $userCapsuleProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
-
 
1388
                $capsuleProgress = 0;
-
 
1389
                $capsuleCompleted = false;
-
 
1390
                if($userCapsuleProgress) {
-
 
1391
                    $capsuleProgress = $userCapsuleProgress->progress;
-
 
1392
                    $capsuleCompleted = $userCapsuleProgress->completed;
-
 
1393
                }
-
 
1394
 
-
 
1395
                // Get comment count and average rating for the capsule
-
 
1396
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->id);
-
 
1397
 
-
 
1398
                // Add formatted capsule data to the array
-
 
1399
                array_push($capsulesData, [
-
 
1400
                    'uuid'              => $capsule->uuid,
-
 
1401
                    'name'              => $capsule->name ? $capsule->name : '',
-
 
1402
                    'description'       => $capsule->description ? $capsule->description : '',
-
 
1403
                    'image'             => $capsule->image ? $storage->getGenericImage($pathCapsule, $capsule->uuid, $capsule->image)  : '',
-
 
1404
                    'link_comments'     => $this->url()->fromRoute('microlearning/capsules-comments', ['capsule_id' => $capsule->uuid], ['force_canonical' => true]),
-
 
1405
                    'link_comment_add'  => $this->url()->fromRoute('microlearning/capsules-comments/add', ['capsule_id' => $capsule->uuid],['force_canonical' => true]),
-
 
1406
                    'link_slides'       => $this->url()->fromRoute('microlearning/slides', ['topic_id' => $topic->uuid,  'capsule_id' => $capsule->uuid], ['force_canonical' => true]),
-
 
1407
                    'link_get'          => $this->url()->fromRoute('microlearning/get-capsule', ['id' => $capsule->uuid], ['force_canonical' => true]),
-
 
1408
                    'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
-
 
1409
                    'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
-
 
1410
                    'progress'          => $capsuleProgress,
-
 
1411
                    'completed'         => $capsuleCompleted,
-
 
1412
                    'order'             => $capsule->order,
-
 
1413
                    'added_on'          => $capsule->added_on,
-
 
1414
                    'updated_on'        => $capsule->updated_on,
-
 
1415
                ]);
-
 
1416
            }
-
 
1417
 
-
 
1418
            // Sort capsules by order, then by added_on date, then by name
-
 
1419
            usort($capsulesData, function($a, $b) {
-
 
1420
                $result =  $a['order'] <=> $b['order'];
-
 
1421
                if(0 == $result) {
-
 
1422
                    $result = strcasecmp($a['added_on'], $b['added_on']);
-
 
1423
                    if(0 == $result) {
-
 
1424
                        $result = strcasecmp($a['name'], $b['name']);
-
 
1425
                    }
-
 
1426
                }
-
 
1427
                // usort expects -1, 0, or 1. Adjusting the logic to fit this.
-
 
1428
                if($result < 0) { return -1; } else if($result > 0) { return 1; } else { return 0; }
-
 
1429
            });
-
 
1430
        }
Línea 1352... Línea 1431...
1352
        }
1431
        return $capsulesData;
1353
    }
1432
    }
1354
    
1433