Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev 576 Rev 578
Línea 1351... Línea 1351...
1351
     * @param Storage $storage The storage service instance.
1351
     * @param Storage $storage The storage service instance.
1352
     * @return array An array of formatted capsule data.
1352
     * @return array An array of formatted capsule data.
1353
     */
1353
     */
1354
    private function _getCapsulesByTopic($topic, $storage)
1354
    private function _getCapsulesByTopic($topic, $storage)
1355
    {
1355
    {
1356
        $capsulesData = [];
1356
        $data = [];
1357
        
-
 
1358
        // Get path for capsule images
-
 
1359
        $pathCapsule = $storage->getPathMicrolearningCapsule();
-
 
1360
        
-
 
1361
        // Initialize mappers
-
 
1362
        $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
-
 
1363
        $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
-
 
1364
        $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
-
 
1365
        $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter); // Added mapper
-
 
1366
 
-
 
1367
        // Fetch access granted capsule IDs for the current user
-
 
1368
        $accessGrantedIds = $this->getAccessGranted();
1357
        $accessGrantedIds = $this->getAccessGranted();
Línea -... Línea 1358...
-
 
1358
 
-
 
1359
        if (!$topic) {
-
 
1360
            return $data;
-
 
1361
        }
1369
 
1362
 
-
 
1363
        if (!in_array($topic->id, $accessGrantedIds->topics)) {
-
 
1364
            return $data;
-
 
1365
        }
1370
        // Fetch active topic-capsule relations for the given topic
1366
 
1371
        // Assuming the 'true' in the original call implied active relations/capsules.
1367
        $currentUserPlugin = $this->plugin('currentUserPlugin');
-
 
1368
        $currentUser = $currentUserPlugin->getUser();
-
 
1369
 
1372
        // We fetch active relations first.
1370
        $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
Línea 1373... Línea 1371...
1373
        $topicCapsuleRelations = $topicCapsuleMapper->fetchAllActiveByTopicId($topic->id);
1371
        $topicCapsuleRelations = $topicCapsuleMapper->fetchAllActiveByTopicId($topic->id);
1374
 
-
 
-
 
1372
 
1375
        $capsuleIdsToFetch = [];
1373
        $capsuleIdsToFetch = [];
1376
        if (!empty($topicCapsuleRelations)) {
-
 
1377
            foreach ($topicCapsuleRelations as $relation) {
1374
 
1378
                // Ensure the user has access to this specific capsule ID from the relation
1375
        foreach ($topicCapsuleRelations as $relation) {
1379
                if (in_array($relation->capsule_id, $accessGrantedIds->capsules)) {
-
 
1380
                    $capsuleIdsToFetch[] = $relation->capsule_id;
1376
            if (in_array($relation->capsule_id, $accessGrantedIds->capsules)) {
1381
                }
1377
                $capsuleIdsToFetch[] = $relation->capsule_id;
1382
            }
1378
            }
1383
        }
-
 
1384
        
1379
        }
1385
        $capsules = []; 
-
 
1386
        if (!empty($capsuleIdsToFetch)) {
1380
 
1387
            // Fetch capsule details for the accessible and related capsule IDs
-
 
1388
            // Removed array_unique to match capsulesAction more closely
1381
        if (empty($capsuleIdsToFetch)) {
Línea 1389... Línea -...
1389
            $capsules = $capsuleMapper->fetchAllByIds($capsuleIdsToFetch); 
-
 
1390
        }
-
 
1391
 
-
 
1392
        if(count($capsules) > 0) {
-
 
1393
            // Get current user
-
 
1394
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1382
            return $data;
1395
            $currentUser = $currentUserPlugin->getUser();
-
 
1396
 
-
 
1397
            // Iterate through each capsule to prepare its data
-
 
1398
            foreach($capsules as $capsule) {
-
 
1399
                // Get user progress for this capsule
-
 
1400
                $userCapsuleProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
-
 
1401
                $capsuleProgress = 0;
-
 
1402
                $capsuleCompleted = false;
1383
        }
1403
                if($userCapsuleProgress) {
-
 
1404
                    $capsuleProgress = $userCapsuleProgress->progress;
-
 
1405
                    $capsuleCompleted = $userCapsuleProgress->completed;
-
 
1406
                }
-
 
1407
 
-
 
1408
                // Get comment count and average rating for the capsule
-
 
1409
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->id);
-
 
1410
 
-
 
1411
                // Add formatted capsule data to the array, ensuring links are present as in capsulesAction
-
 
1412
                array_push($capsulesData, [
-
 
1413
                    'uuid'              => $capsule->uuid,
-
 
1414
                    'name'              => $capsule->name ? $capsule->name : '',
-
 
1415
                    'description'       => $capsule->description ? $capsule->description : '',
-
 
1416
                    'image'             => $capsule->image ? $storage->getGenericImage($pathCapsule, $capsule->uuid, $capsule->image)  : '',
-
 
1417
                    'link_comments'     => $this->url()->fromRoute('microlearning/capsules-comments', ['capsule_id' => $capsule->uuid], ['force_canonical' => true]),
-
 
1418
                    'link_comment_add'  => $this->url()->fromRoute('microlearning/capsules-comments/add', ['capsule_id' => $capsule->uuid],['force_canonical' => true]),
-
 
1419
                    'link_slides'       => $this->url()->fromRoute('microlearning/slides', ['topic_id' => $topic->uuid,  'capsule_id' => $capsule->uuid], ['force_canonical' => true]),
-
 
1420
                    'link_get'          => $this->url()->fromRoute('microlearning/get-capsule', ['id' => $capsule->uuid], ['force_canonical' => true]),
-
 
1421
                    'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
-
 
1422
                    'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
-
 
1423
                    'progress'          => $capsuleProgress,
-
 
1424
                    'completed'         => $capsuleCompleted,
-
 
1425
                    'order'             => $capsule->order,
-
 
1426
                    'added_on'          => $capsule->added_on,
-
 
Línea 1427... Línea 1384...
1427
                    'updated_on'        => $capsule->updated_on,
1384
 
-
 
1385
        $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
-
 
1386
        $capsules = $capsuleMapper->fetchAllByIds($capsuleIdsToFetch);
-
 
1387
 
-
 
1388
        $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
1428
                ]);
1389
        $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
1429
            }
1390
        
1430
 
1391
        $path = $storage->getPathMicrolearningCapsule();
-
 
1392
 
-
 
1393
        foreach ($capsules as $capsule) {
-
 
1394
            if (!in_array($capsule->id, $accessGrantedIds->capsules)) {
-
 
1395
                continue;
-
 
1396
            }
-
 
1397
 
-
 
1398
            $userProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
-
 
1399
            $progress = $userProgress->progress ?? 0;
1431
            // Sort capsules by order, then by added_on date, then by name
1400
            $completed = $userProgress->completed ?? 0;
-
 
1401
            $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->id);
-
 
1402
            $image = $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image) : '';
1432
            usort($capsulesData, function($a, $b) {
1403
            $total_comments = strval($dataCountAndRatingAverage['total_comments']);
1433
                $result =  $a['order'] <=> $b['order'];
1404
            $total_rating = strval($dataCountAndRatingAverage['total_rating']);
-
 
1405
 
1434
                if(0 == $result) {
1406
            array_push($data, [
-
 
1407
                'uuid'              => $capsule->uuid,
-
 
1408
                'name'              => $capsule->name ? $capsule->name : '',
1435
                    $result = strcasecmp($a['added_on'], $b['added_on']);
1409
                'description'       => $capsule->description ? $capsule->description : '',
1436
                    if(0 == $result) {
1410
                'image'             => $image,
1437
                        $result = strcasecmp($a['name'], $b['name']);
1411
                'total_comments'    => $total_comments,
-
 
1412
                'total_rating'      => $total_rating,
-
 
1413
                'progress'          => $progress,
1438
                    }
1414
                'completed'         => $completed,
1439
                }
1415
                'order'             => $capsule->order,
-
 
1416
                'added_on'          => $capsule->added_on,
1440
                // usort expects -1, 0, or 1. Adjusting the logic to fit this.
1417
                'updated_on'        => $capsule->updated_on,
1441
                if($result < 0) { return -1; } else if($result > 0) { return 1; } else { return 0; }
1418
            ]);
Línea 1442... Línea 1419...
1442
            });
1419
        }
1443
        }
1420
 
1444
        return $capsulesData;
1421
        return $data;
1445
    }
1422
    }
-
 
1423
    
1446
    
1424
    
1447
    
1425
    public function capsulesAction()
1448
    public function capsulesAction()
-
 
Línea 1449... Línea -...
1449
    {
-
 
1450
        $request = $this->getRequest();
1426
    {
1451
        if($request->isGet()) {
1427
        $request = $this->getRequest();
1452
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1428
        if($request->isGet()) {
Línea 1453... Línea 1429...
1453
            $currentUser = $currentUserPlugin->getUser();
1429
            // currentUserPlugin and currentUser are fetched within _getCapsulesByTopic if needed directly by it
1454
            
1430
            // $currentUserPlugin = $this->plugin('currentUserPlugin');
1455
            
1431
            // $currentUser = $currentUserPlugin->getUser();
1456
            
1432
            
1457
            $topic_id_param = $this->params()->fromRoute('topic_id'); // Renombrado para claridad
1433
            $topic_id_param = $this->params()->fromRoute('topic_id'); 
1458
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
1434
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
Línea 1459... Línea 1435...
1459
            $topic = $topicMapper->fetchOneByUuid($topic_id_param);
1435
            $topic = $topicMapper->fetchOneByUuid($topic_id_param); // $topic is the topic object
Línea 1460... Línea 1436...
1460
            
1436
            
1461
            if(!$topic) {
1437
            if(!$topic) {
1462
                return new JsonModel([
1438
                return new JsonModel([
1463
                    'success' => false,
1439
                    'success' => false,
1464
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1440
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1465
                ]);
1441
                ]);
Línea 1466... Línea -...
1466
            }
-
 
1467
            
-
 
1468
            $accessGrantedIds = $this->getAccessGranted();
-
 
1469
            
-
 
1470
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
-
 
1471
                return new JsonModel([
-
 
1472
                    'success' => false,
-
 
1473
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
-
 
1474
                ]);
-
 
1475
            }
-
 
1476
            
-
 
1477
            
-
 
1478
            
-
 
1479
            $data = [];
-
 
1480
          
-
 
1481
            // Obtener la relación Tópico-Cápsula
-
 
1482
            $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
-
 
1483
            $topicCapsuleRelations = $topicCapsuleMapper->fetchAllActiveByTopicId($topic->id);
-
 
1484
            
-
 
1485
            $capsuleIdsToFetch = [];
-
 
1486
            $capsuleRelationData = []; // Para guardar datos específicos de la relación si es necesario
-
 
1487
            foreach ($topicCapsuleRelations as $relation) {
-
 
1488
                if (in_array($relation->capsule_id, $accessGrantedIds->capsules)) {
-
 
1489
                    $capsuleIdsToFetch[] = $relation->capsule_id;
1442
            }
1490
                    $capsuleRelationData[$relation->capsule_id] = [
-
 
1491
                        // Aquí puedes guardar datos de $relation si los necesitas más tarde
-
 
1492
                        // por ejemplo: 'publish_on' => $relation->publish_on
-
 
1493
                    ];
-
 
1494
                }
-
 
1495
            }
-
 
1496
            
-
 
1497
            if (empty($capsuleIdsToFetch)) {
-
 
1498
                return new JsonModel([
-
 
1499
                    'success' => true,
-
 
1500
                    'data' => [] // No hay cápsulas accesibles para este tópico
1443
            
1501
                ]);
1444
            $accessGrantedIds = $this->getAccessGranted(); // Needed for the access check here
Línea 1502... Línea -...
1502
            }
-
 
1503
            
-
 
1504
            // Obtener los detalles de las cápsulas
-
 
1505
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
-
 
1506
            $capsules = $capsuleMapper->fetchAllByIds($capsuleIdsToFetch);
-
 
1507
            
-
 
1508
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
-
 
1509
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
1445
            
1510
     
-
 
1511
            $storage = Storage::getInstance($this->config, $this->adapter);
-
 
1512
            $path = $storage->getPathMicrolearningCapsule();
-
 
1513
            
-
 
1514
            // Construir la respuesta combinando datos de Cápsula y Progreso
-
 
1515
            foreach($capsules as $capsule) // Iterar sobre los objetos Capsule obtenidos
-
 
1516
            {
-
 
1517
                // $capsule ya no tiene topic_id directamente.
-
 
1518
                // El contexto del tópico viene de $topic (obtenido al inicio).
-
 
1519
                
-
 
1520
                // Verificar acceso de nuevo (doble chequeo, podría optimizarse si getAccessGranted es fiable)
-
 
1521
                if(!in_array($capsule->id, $accessGrantedIds->capsules)) {
-
 
1522
                     continue;
-
 
1523
                }
-
 
1524
                
-
 
1525
                $userProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
-
 
1526
                if($userProgress) {
-
 
1527
                    $progress = $userProgress->progress;
-
 
1528
                    $completed = $userProgress->completed;
1446
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
1529
                } else {
-
 
1530
                    $progress = 0;
-
 
1531
                    $completed = 0;
-
 
1532
                }
-
 
1533
                
-
 
1534
                // Pasar company_id y capsule_id de la cápsula
-
 
1535
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id,  $capsule->id);
-
 
1536
                
-
 
1537
                
-
 
1538
                
-
 
1539
                array_push($data, [
-
 
1540
                    'uuid'              => $capsule->uuid,
-
 
1541
                    'name'              => $capsule->name ? $capsule->name : '',
-
 
1542
                    'description'       => $capsule->description ? $capsule->description : '',
-
 
1543
                    'image'             => $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image)  : '',
-
 
1544
                    // Usar $topic->uuid y $capsule->uuid para los enlaces
-
 
1545
                    'link_comments'     => $this->url()->fromRoute('microlearning/capsules-comments', ['capsule_id' => $capsule->uuid], ['force_canonical' => true]),
-
 
1546
                    'link_comment_add'  => $this->url()->fromRoute('microlearning/capsules-comments/add', ['capsule_id' => $capsule->uuid],['force_canonical' => true]),
-
 
1547
                    'link_slides'       => $this->url()->fromRoute('microlearning/slides', ['topic_id' => $topic->uuid,  'capsule_id' => $capsule->uuid], ['force_canonical' => true]),
-
 
1548
                    'link_get'          => $this->url()->fromRoute('microlearning/get-capsule', ['id' => $capsule->uuid], ['force_canonical' => true]), 
-
 
1549
                    'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
-
 
Línea 1550... Línea -...
1550
                    'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
-
 
1551
                    'progress'          => $progress,
-
 
1552
                    'completed'         => $completed,
-
 
1553
                    'order'             => $capsule->order, // Asumiendo que el orden viene de la cápsula
-
 
1554
                    'added_on'          => $capsule->added_on, // Fecha de la cápsula
-
 
1555
                    'updated_on'        => $capsule->updated_on, // Fecha de la cápsula
-
 
1556
                    // Puedes agregar datos de $capsuleRelationData[$capsule->id] si es necesario
-
 
1557
                ]);
-
 
1558
                
-
 
1559
                
-
 
1560
 
-
 
1561
            }
-
 
1562
            
-
 
1563
            usort($data, function($a, $b) {
-
 
1564
                
-
 
1565
                $result =  $a['order'] <=> $b['order'];
-
 
1566
                if(0 == $result) {
-
 
1567
                    $result = strcasecmp($a['added_on'], $b['added_on']);
-
 
1568
                    if(0 == $result) {
-
 
1569
                        $result = strcasecmp($a['name'], $b['name']);
-
 
1570
                    }
-
 
1571
                }
1447
                return new JsonModel([
1572
                
1448
                    'success' => false,
1573
                if($result < 0) {
1449
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
1574
                    return 1;
1450
                ]);
Línea 1575... Línea 1451...
1575
                } else if($result > 0) {
1451
            }
1576
                    return -1;
1452
            
1577
                } else  {
1453
            // Initialize storage and call the refactored private method
1578
                    return  0;
1454
            $storage = Storage::getInstance($this->config, $this->adapter);