Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev 581 Rev 584
Línea 1346... Línea 1346...
1346
     * @return array The capsules data.
1346
     * @return array The capsules data.
1347
     */
1347
     */
1348
    private function _getCapsulesByTopic($topic, $storage)
1348
    private function _getCapsulesByTopic($topic, $storage)
1349
    {
1349
    {
1350
        $data = [];
1350
        $data = [];
1351
        // $accessGrantedIds = $this->getAccessGranted();
1351
        $accessGrantedIds = $this->getAccessGranted();
Línea 1352... Línea 1352...
1352
 
1352
 
1353
        if (!$topic) {
1353
        if (!$topic) {
1354
            return $data;
1354
            return $data;
Línea 1355... Línea 1355...
1355
        }
1355
        }
1356
 
1356
 
1357
        /* if (!in_array($topic->id, $accessGrantedIds->topics)) {
1357
        if (!in_array($topic->id, $accessGrantedIds->topics)) {
Línea 1358... Línea 1358...
1358
            return $data;
1358
            return $data;
1359
        } */
1359
        }
Línea 1360... Línea 1360...
1360
 
1360
 
Línea 1368... Línea 1368...
1368
        $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
1368
        $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
Línea 1369... Línea 1369...
1369
 
1369
 
Línea 1370... Línea 1370...
1370
        $path = $storage->getPathMicrolearningCapsule();
1370
        $path = $storage->getPathMicrolearningCapsule();
1371
 
1371
 
1372
        foreach ($topicCapsuleRelations as $relation) {
-
 
1373
            /* if (in_array($relation->capsule_id, $accessGrantedIds->capsules)) {
-
 
1374
                $capsuleIdsToFetch[] = $relation->capsule_id;
-
 
1375
            } */
-
 
1376
 
1372
        foreach ($topicCapsuleRelations as $relation) {
1377
            /* if (!in_array($capsule->id, $accessGrantedIds->capsules)) {
1373
            if(!in_array($relation->capsule_id, $accessGrantedIds->capsules)) {
-
 
1374
                continue;
1378
                continue;
1375
            }
1379
            } */
1376
 
Línea 1380... Línea 1377...
1380
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
1377
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
1381
            $capsule = $capsuleMapper->fetchOne($relation->capsule_id);
-
 
1382
 
-
 
1383
            if(!$capsule) {
-
 
1384
                array_push($data, [
-
 
1385
                    'uuid'              => $relation->capsule_id,
-
 
1386
                    'name'              => 'Cápsula no encontrada',
-
 
1387
                    'description'       => 'Cápsula no encontrada',
-
 
1388
                    'image'             => '',
1378
            $capsule = $capsuleMapper->fetchOne($relation->capsule_id);
1389
                    'total_comments'    => 0,
1379
 
Línea 1390... Línea 1380...
1390
                ]);
1380
            if(!$capsule) {
1391
                continue;
1381
                continue;
Línea 1421... Línea 1411...
1421
    {
1411
    {
1422
        $request = $this->getRequest();
1412
        $request = $this->getRequest();
1423
        if($request->isGet()) {
1413
        if($request->isGet()) {
1424
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1414
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1425
            $currentUser = $currentUserPlugin->getUser();
1415
            $currentUser = $currentUserPlugin->getUser();
1426
            
1416
 
1427
            $topic_id_param = $this->params()->fromRoute('topic_id'); // Renombrado para claridad
1417
            $topic_id_param = $this->params()->fromRoute('topic_id');
1428
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
1418
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
1429
            $topic = $topicMapper->fetchOneByUuid($topic_id_param);
1419
            $topic = $topicMapper->fetchOneByUuid($topic_id_param);
1430
            
1420
 
1431
            if(!$topic) {
1421
            if(!$topic) {
1432
                return new JsonModel([
1422
                return new JsonModel([
1433
                    'success' => false,
1423
                    'success' => false,
1434
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1424
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1435
                ]);
1425
                ]);
1436
            }
1426
            }
1437
            
1427
 
1438
            $accessGrantedIds = $this->getAccessGranted();
1428
            $accessGrantedIds = $this->getAccessGranted();
1439
            
1429
 
1440
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
1430
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
1441
                return new JsonModel([
1431
                return new JsonModel([
1442
                    'success' => false,
1432
                    'success' => false,
1443
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
1433
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
1444
                ]);
1434
                ]);
1445
            }
1435
            }
1446
            
-
 
1447
            
1436
 
1448
            
-
 
1449
            $data = [];
-
 
1450
          
-
 
1451
            // Obtener la relación Tópico-Cápsula
-
 
1452
            $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
-
 
1453
            $topicCapsuleRelations = $topicCapsuleMapper->fetchAllActiveByTopicId($topic->id);
-
 
1454
            
-
 
1455
            $capsuleIdsToFetch = [];
-
 
1456
            $capsuleRelationData = []; // Para guardar datos específicos de la relación si es necesario
-
 
1457
            foreach ($topicCapsuleRelations as $relation) {
-
 
1458
                if (in_array($relation->capsule_id, $accessGrantedIds->capsules)) {
-
 
1459
                    $capsuleIdsToFetch[] = $relation->capsule_id;
-
 
1460
                    $capsuleRelationData[$relation->capsule_id] = [
-
 
1461
                        // Aquí puedes guardar datos de $relation si los necesitas más tarde
-
 
1462
                        // por ejemplo: 'publish_on' => $relation->publish_on
-
 
1463
                    ];
-
 
1464
                }
-
 
1465
            }
-
 
1466
            
-
 
1467
            if (empty($capsuleIdsToFetch)) {
-
 
1468
                return new JsonModel([
-
 
1469
                    'success' => true,
-
 
1470
                    'data' => [] // No hay cápsulas accesibles para este tópico
-
 
1471
                ]);
-
 
1472
            }
-
 
1473
            
-
 
1474
            // Obtener los detalles de las cápsulas
-
 
1475
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
-
 
1476
            $capsules = $capsuleMapper->fetchAllByIds($capsuleIdsToFetch);
-
 
1477
            
-
 
1478
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
-
 
1479
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
-
 
1480
     
-
 
1481
            $storage = Storage::getInstance($this->config, $this->adapter);
1437
            $storage = Storage::getInstance($this->config, $this->adapter);
1482
            $path = $storage->getPathMicrolearningCapsule();
-
 
1483
            
-
 
1484
            // Construir la respuesta combinando datos de Cápsula y Progreso
1438
            // Use the _getCapsulesByTopic method to fetch capsules
1485
            foreach($capsules as $capsule) // Iterar sobre los objetos Capsule obtenidos
-
 
1486
            {
-
 
1487
                // $capsule ya no tiene topic_id directamente.
1439
            $data = $this->_getCapsulesByTopic($topic, $storage);
1488
                // El contexto del tópico viene de $topic (obtenido al inicio).
-
 
1489
                
1440
 
1490
                // Verificar acceso de nuevo (doble chequeo, podría optimizarse si getAccessGranted es fiable)
1441
            // The _getCapsulesByTopic method already returns capsules with the required data structure.
1491
                if(!in_array($capsule->id, $accessGrantedIds->capsules)) {
-
 
1492
                     continue;
1442
            // We just need to sort them.
1493
                }
-
 
1494
                
-
 
1495
                $userProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
-
 
1496
                if($userProgress) {
-
 
1497
                    $progress = $userProgress->progress;
-
 
1498
                    $completed = $userProgress->completed;
-
 
1499
                } else {
-
 
1500
                    $progress = 0;
-
 
1501
                    $completed = 0;
-
 
1502
                }
-
 
1503
                
-
 
1504
                // Pasar company_id y capsule_id de la cápsula
-
 
1505
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id,  $capsule->id);
-
 
1506
                
-
 
1507
                
-
 
1508
                
-
 
1509
                array_push($data, [
-
 
1510
                    'uuid'              => $capsule->uuid,
-
 
1511
                    'name'              => $capsule->name ? $capsule->name : '',
-
 
1512
                    'description'       => $capsule->description ? $capsule->description : '',
-
 
1513
                    'image'             => $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image)  : '',
-
 
1514
                    // Usar $topic->uuid y $capsule->uuid para los enlaces
-
 
1515
                    'link_comments'     => $this->url()->fromRoute('microlearning/capsules-comments', ['capsule_id' => $capsule->uuid], ['force_canonical' => true]),
-
 
1516
                    'link_comment_add'  => $this->url()->fromRoute('microlearning/capsules-comments/add', ['capsule_id' => $capsule->uuid],['force_canonical' => true]),
-
 
1517
                    'link_slides'       => $this->url()->fromRoute('microlearning/slides', ['topic_id' => $topic->uuid,  'capsule_id' => $capsule->uuid], ['force_canonical' => true]),
-
 
1518
                    'link_get'          => $this->url()->fromRoute('microlearning/get-capsule', ['id' => $capsule->uuid], ['force_canonical' => true]), 
-
 
1519
                    'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
-
 
1520
                    'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
-
 
1521
                    'progress'          => $progress,
-
 
1522
                    'completed'         => $completed,
-
 
1523
                    'order'             => $capsule->order, // Asumiendo que el orden viene de la cápsula
-
 
1524
                    'added_on'          => $capsule->added_on, // Fecha de la cápsula
-
 
1525
                    'updated_on'        => $capsule->updated_on, // Fecha de la cápsula
-
 
1526
                    // Puedes agregar datos de $capsuleRelationData[$capsule->id] si es necesario
-
 
1527
                ]);
-
 
1528
                
-
 
1529
                
-
 
Línea 1530... Línea -...
1530
 
-
 
1531
            }
-
 
1532
            
1443
 
1533
            usort($data, function($a, $b) {
1444
            usort($data, function($a, $b) {
1534
                
1445
 
1535
                $result =  $a['order'] <=> $b['order'];
1446
                $result =  $a['order'] <=> $b['order'];
1536
                if(0 == $result) {
1447
                if(0 == $result) {
1537
                    $result = strcasecmp($a['added_on'], $b['added_on']);
1448
                    $result = strcasecmp($a['added_on'], $b['added_on']);
1538
                    if(0 == $result) {
1449
                    if(0 == $result) {
1539
                        $result = strcasecmp($a['name'], $b['name']);
1450
                        $result = strcasecmp($a['name'], $b['name']);
1540
                    }
1451
                    }
1541
                }
1452
                }
1542
                
1453
 
1543
                if($result < 0) {
1454
                if($result < 0) {
1544
                    return 1;
1455
                    return 1;
1545
                } else if($result > 0) {
1456
                } else if($result > 0) {
1546
                    return -1;
1457
                    return -1;
1547
                } else  {
1458
                } else  {
1548
                    return  0;
1459
                    return  0;
1549
                }
1460
                }
1550
            });
-
 
1551
                
1461
            });
1552
                
-
 
1553
                
1462
 
1554
                return new JsonModel([
1463
            return new JsonModel([
1555
                    'success' => true,
1464
                'success' => true,
1556
                    'data' => $data
1465
                'data' => $data
1557
                ]);
1466
            ]);
1558
                
1467
 
1559
        } else {
1468
        } else {
1560
            return new JsonModel([
1469
            return new JsonModel([
1561
                'success' => false,
1470
                'success' => false,
1562
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1471
                'data' => 'ERROR_METHOD_NOT_ALLOWED'