Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev 579 Rev 580
Línea 1299... Línea 1299...
1299
 
1299
 
1300
            // Get user progress for this topic
1300
            // Get user progress for this topic
1301
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
1301
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
Línea 1302... Línea -...
1302
            $userProgress = $userProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $topic->id);
-
 
1303
 
-
 
1304
            $progress = 0;
-
 
1305
            $completed = false;
1302
            $userProgress = $userProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $topic->id);
1306
            if($userProgress) {
1303
 
1307
                $progress = $userProgress->progress;
-
 
Línea 1308... Línea 1304...
1308
                $completed = $userProgress->completed;
1304
            $progress = $userProgress->progress ?? 0;
1309
            }
1305
            $completed = $userProgress->completed ?? 0;
1310
 
1306
 
Línea 1343... Línea 1339...
1343
            ]);
1339
            ]);
1344
        }
1340
        }
1345
    }
1341
    }
Línea 1346... Línea 1342...
1346
 
1342
 
1347
    /**
-
 
1348
     * Fetches and formats capsules associated with a given topic.
-
 
1349
     *
1343
    /**
1350
     * @param mixed $topic The topic object.
1344
     * @param MicrolearningTopic $topic The topic object.
1351
     * @param Storage $storage The storage service instance.
1345
     * @param Storage $storage The storage object.
1352
     * @return array An array of formatted capsule data.
1346
     * @return array The capsules data.
1353
     */
1347
     */
1354
    private function _getCapsulesByTopic($topic, $storage)
1348
    private function _getCapsulesByTopic($topic, $storage)
1355
    {
1349
    {
1356
        $data = [];
1350
        $data = [];
Línea 1402... Línea 1396...
1402
            $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->id);
1396
            $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->id);
1403
            $image = $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image) : '';
1397
            $image = $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image) : '';
1404
            $total_comments = strval($dataCountAndRatingAverage['total_comments']);
1398
            $total_comments = strval($dataCountAndRatingAverage['total_comments']);
1405
            $total_rating = strval($dataCountAndRatingAverage['total_rating']);
1399
            $total_rating = strval($dataCountAndRatingAverage['total_rating']);
Línea 1406... Línea 1400...
1406
 
1400
 
1407
            array_push($data, [
1401
            /* array_push($data, [
1408
                'uuid'              => $capsule->uuid,
1402
                'uuid'              => $capsule->uuid,
1409
                'name'              => $capsule->name ? $capsule->name : '',
1403
                'name'              => $capsule->name ? $capsule->name : '',
1410
                'description'       => $capsule->description ? $capsule->description : '',
1404
                'description'       => $capsule->description ? $capsule->description : '',
1411
                'image'             => $image,
1405
                'image'             => $image,
Línea 1414... Línea 1408...
1414
                'progress'          => $progress,
1408
                'progress'          => $progress,
1415
                'completed'         => $completed,
1409
                'completed'         => $completed,
1416
                'order'             => $capsule->order,
1410
                'order'             => $capsule->order,
1417
                'added_on'          => $capsule->added_on,
1411
                'added_on'          => $capsule->added_on,
1418
                'updated_on'        => $capsule->updated_on,
1412
                'updated_on'        => $capsule->updated_on,
1419
            ]);
1413
            ]); */
-
 
1414
 
-
 
1415
            
1420
        }
1416
        }
Línea -... Línea 1417...
-
 
1417
 
-
 
1418
        return [
-
 
1419
            'relations' => $topicCapsuleRelations,
-
 
1420
            'capsules' => $capsules,
-
 
1421
            'topic' => $topic,
-
 
1422
        ];
1421
 
1423
 
1422
        return $data;
1424
        //return $data;
Línea 1423... Línea -...
1423
    }
-
 
1424
    
1425
    }
1425
    
1426
    
1426
    public function capsulesAction()
1427
    public function capsulesAction()
1427
    {
1428
    {
1428
        $request = $this->getRequest();
-
 
1429
        if($request->isGet()) {
1429
        $request = $this->getRequest();
1430
            // currentUserPlugin and currentUser are fetched within _getCapsulesByTopic if needed directly by it
1430
        if($request->isGet()) {
Línea 1431... Línea 1431...
1431
            // $currentUserPlugin = $this->plugin('currentUserPlugin');
1431
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1432
            // $currentUser = $currentUserPlugin->getUser();
1432
            $currentUser = $currentUserPlugin->getUser();
1433
            
1433
            
Línea 1434... Línea 1434...
1434
            $topic_id_param = $this->params()->fromRoute('topic_id'); 
1434
            $topic_id_param = $this->params()->fromRoute('topic_id'); // Renombrado para claridad
1435
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
1435
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
1436
            $topic = $topicMapper->fetchOneByUuid($topic_id_param); // $topic is the topic object
1436
            $topic = $topicMapper->fetchOneByUuid($topic_id_param);
1437
            
1437
            
1438
            if(!$topic) {
1438
            if(!$topic) {
1439
                return new JsonModel([
1439
                return new JsonModel([
Línea 1440... Línea 1440...
1440
                    'success' => false,
1440
                    'success' => false,
Línea 1441... Línea 1441...
1441
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1441
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1442
                ]);
1442
                ]);
1443
            }
1443
            }
1444
            
1444
            
1445
            $accessGrantedIds = $this->getAccessGranted(); // Needed for the access check here
1445
            $accessGrantedIds = $this->getAccessGranted();
1446
            
1446
            
Línea -... Línea 1447...
-
 
1447
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
-
 
1448
                return new JsonModel([
-
 
1449
                    'success' => false,
-
 
1450
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
-
 
1451
                ]);
-
 
1452
            }
-
 
1453
            
-
 
1454
            
-
 
1455
            
-
 
1456
            $data = [];
-
 
1457
          
-
 
1458
            // Obtener la relación Tópico-Cápsula
-
 
1459
            $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
-
 
1460
            $topicCapsuleRelations = $topicCapsuleMapper->fetchAllActiveByTopicId($topic->id);
-
 
1461
            
-
 
1462
            $capsuleIdsToFetch = [];
-
 
1463
            $capsuleRelationData = []; // Para guardar datos específicos de la relación si es necesario
-
 
1464
            foreach ($topicCapsuleRelations as $relation) {
-
 
1465
                if (in_array($relation->capsule_id, $accessGrantedIds->capsules)) {
-
 
1466
                    $capsuleIdsToFetch[] = $relation->capsule_id;
-
 
1467
                    $capsuleRelationData[$relation->capsule_id] = [
-
 
1468
                        // Aquí puedes guardar datos de $relation si los necesitas más tarde
-
 
1469
                        // por ejemplo: 'publish_on' => $relation->publish_on
1447
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
1470
                    ];
-
 
1471
                }
-
 
1472
            }
-
 
1473
            
-
 
1474
            if (empty($capsuleIdsToFetch)) {
-
 
1475
                return new JsonModel([
-
 
1476
                    'success' => true,
-
 
1477
                    'data' => [] // No hay cápsulas accesibles para este tópico
-
 
1478
                ]);
-
 
1479
            }
-
 
1480
            
1448
                return new JsonModel([
1481
            // Obtener los detalles de las cápsulas
1449
                    'success' => false,
1482
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
Línea -... Línea 1483...
-
 
1483
            $capsules = $capsuleMapper->fetchAllByIds($capsuleIdsToFetch);
-
 
1484
            
-
 
1485
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
-
 
1486
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
-
 
1487
     
-
 
1488
            $storage = Storage::getInstance($this->config, $this->adapter);
-
 
1489
            $path = $storage->getPathMicrolearningCapsule();
1450
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
1490
            
-
 
1491
            // Construir la respuesta combinando datos de Cápsula y Progreso
-
 
1492
            foreach($capsules as $capsule) // Iterar sobre los objetos Capsule obtenidos
-
 
1493
            {
-
 
1494
                // $capsule ya no tiene topic_id directamente.
-
 
1495
                // El contexto del tópico viene de $topic (obtenido al inicio).
-
 
1496
                
-
 
1497
                // Verificar acceso de nuevo (doble chequeo, podría optimizarse si getAccessGranted es fiable)
-
 
1498
                if(!in_array($capsule->id, $accessGrantedIds->capsules)) {
-
 
1499
                     continue;
-
 
1500
                }
-
 
1501
                
-
 
1502
                $userProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
-
 
1503
                if($userProgress) {
-
 
1504
                    $progress = $userProgress->progress;
-
 
1505
                    $completed = $userProgress->completed;
-
 
1506
                } else {
-
 
1507
                    $progress = 0;
-
 
1508
                    $completed = 0;
1451
                ]);
1509
                }
-
 
1510
                
-
 
1511
                // Pasar company_id y capsule_id de la cápsula
-
 
1512
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id,  $capsule->id);
-
 
1513
                
-
 
1514
                
-
 
1515
                
-
 
1516
                array_push($data, [
-
 
1517
                    'uuid'              => $capsule->uuid,
-
 
1518
                    'name'              => $capsule->name ? $capsule->name : '',
-
 
1519
                    'description'       => $capsule->description ? $capsule->description : '',
-
 
1520
                    'image'             => $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image)  : '',
-
 
1521
                    // Usar $topic->uuid y $capsule->uuid para los enlaces
-
 
1522
                    'link_comments'     => $this->url()->fromRoute('microlearning/capsules-comments', ['capsule_id' => $capsule->uuid], ['force_canonical' => true]),
-
 
1523
                    'link_comment_add'  => $this->url()->fromRoute('microlearning/capsules-comments/add', ['capsule_id' => $capsule->uuid],['force_canonical' => true]),
-
 
1524
                    'link_slides'       => $this->url()->fromRoute('microlearning/slides', ['topic_id' => $topic->uuid,  'capsule_id' => $capsule->uuid], ['force_canonical' => true]),
-
 
1525
                    'link_get'          => $this->url()->fromRoute('microlearning/get-capsule', ['id' => $capsule->uuid], ['force_canonical' => true]), 
-
 
1526
                    'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
-
 
1527
                    'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
-
 
1528
                    'progress'          => $progress,
-
 
1529
                    'completed'         => $completed,
-
 
1530
                    'order'             => $capsule->order, // Asumiendo que el orden viene de la cápsula
Línea -... Línea 1531...
-
 
1531
                    'added_on'          => $capsule->added_on, // Fecha de la cápsula
-
 
1532
                    'updated_on'        => $capsule->updated_on, // Fecha de la cápsula
-
 
1533
                    // Puedes agregar datos de $capsuleRelationData[$capsule->id] si es necesario
-
 
1534
                ]);
-
 
1535
                
-
 
1536
                
-
 
1537
 
-
 
1538
            }
-
 
1539
            
-
 
1540
            usort($data, function($a, $b) {
-
 
1541
                
-
 
1542
                $result =  $a['order'] <=> $b['order'];
-
 
1543
                if(0 == $result) {
-
 
1544
                    $result = strcasecmp($a['added_on'], $b['added_on']);
-
 
1545
                    if(0 == $result) {
-
 
1546
                        $result = strcasecmp($a['name'], $b['name']);
-
 
1547
                    }
-
 
1548
                }
-
 
1549
                
-
 
1550
                if($result < 0) {
-
 
1551
                    return 1;
1452
            }
1552
                } else if($result > 0) {
1453
            
1553
                    return -1;
1454
            // Initialize storage and call the refactored private method
1554
                } else  {
1455
            $storage = Storage::getInstance($this->config, $this->adapter);
1555
                    return  0;
Línea 1456... Línea 1556...
1456
            $data = $this->_getCapsulesByTopic($topic, $storage); // $topic is the object
1556
                }
1457
            
1557
            });
1458
            // The extensive logic for fetching, processing, and sorting capsules
1558
                
1459
            // has been removed from here and is now in _getCapsulesByTopic.
1559