Rev 625 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?phpdeclare(strict_types=1);namespace LeadersLinked\Controller;use Laminas\Mvc\Controller\AbstractActionController;use Laminas\View\Model\JsonModel;use LeadersLinked\Mapper\QueryMapper;use LeadersLinked\Mapper\CompanyMapper;use LeadersLinked\Mapper\MicrolearningTopicMapper;use LeadersLinked\Model\MicrolearningTopic;use LeadersLinked\Mapper\MicrolearningUserMapper;use LeadersLinked\Model\MicrolearningUser;use LeadersLinked\Mapper\EngagementRewardMapper;use LeadersLinked\Model\EngagementReward;use LeadersLinked\Library\Functions;use LeadersLinked\Library\Storage;use LeadersLinked\Model\MicrolearningTopicUser;class MarketPlaceController extends AbstractActionController{/**** @var \Laminas\Db\Adapter\AdapterInterface*/private $adapter;/**** @var \LeadersLinked\Cache\CacheInterface*/private $cache;/**** @var \Laminas\Log\LoggerInterface*/private $logger;/**** @var array*/private $config;/**** @var \Laminas\Mvc\I18n\Translator*/private $translator;/**** @param \Laminas\Db\Adapter\AdapterInterface $adapter* @param \LeadersLinked\Cache\CacheInterface $cache* @param \Laminas\Log\LoggerInterface LoggerInterface $logger* @param array $config* @param \Laminas\Mvc\I18n\Translator $translator*/public function __construct($adapter, $cache, $logger, $config, $translator){$this->adapter = $adapter;$this->cache = $cache;$this->logger = $logger;$this->config = $config;$this->translator = $translator;}/**** Generación del listado de perfiles* {@inheritDoc}* @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()*/public function indexAction(){$request = $this->getRequest();if(!$request->isGet()) {return new JsonModel(['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED']);}$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$search = Functions::sanitizeFilterString($this->params()->fromQuery('search', ''));$entity = Functions::sanitizeFilterString($this->params()->fromQuery('entity', 'capsules'));$storage = Storage::getInstance($this->config, $this->adapter);if($entity == 'rewards') {$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$currentNetworkPlugin = $this->plugin('currentNetworkPlugin');$currentNetwork = $currentNetworkPlugin->getNetwork();$companyMapper = CompanyMapper::getInstance($this->adapter);$company = $companyMapper->fetchDefaultForNetworkByNetworkId($currentNetwork->id);$queryMapper = QueryMapper::getInstance($this->adapter);$select = $queryMapper->getSql()->select( EngagementRewardMapper::_TABLE);$select->columns(['uuid', 'name', 'image', 'points']);$select->where->equalTo('company_id', $company->id);$select->where->equalTo('status', EngagementReward::STATUS_ACTIVE);if($search) {$select->where->like('c.name', '%' . $search . '%');}$select->order(['name ASC']);$path = $storage->getPathEngagementReward();$records = $queryMapper->fetchAll($select);$items = [];foreach($records as $record){$item = ['name' => $record['name'],'image' => $storage->getGenericImage($path, $record['uuid'], $record['image']),'points' => $record['points'],'link_claim' => $this->url()->fromRoute('marketplace/claim', ['id' => $record['uuid']])];array_push($items, $item);}}if($entity == 'topics') {$microlearningTopicUserMapper = MicrolearningTopicUserMapper::getInstance($this->adapter);$path = $storage->getPathMicrolearningTopic();// Obtener las cápsulas públicas y gratuitas$accessGrantedIds = new MicrolearningUserAccessGrantedIds();$topicUserRecords = $microlearningTopicUserMapper->fetchAllActiveByUserId($currentUser->id);// Obtener las cápsulas públicas y gratuitasforeach($topicUserRecords as $topicUserRecord) {if(!in_array($topicUserRecord->company_id, $accessGrantedIds->companies)) {array_push($accessGrantedIds->companies, $topicUserRecord->company_id);}if(!in_array($topicUserRecord->topic_id, $accessGrantedIds->topics)) {array_push($accessGrantedIds->topics, $topicUserRecord->topic_id);}if(!in_array($topicUserRecord->topic_id, $accessGrantedIds->topics)) {array_push($accessGrantedIds->topics, $topicUserRecord->topic_id);}}$queryMapper = QueryMapper::getInstance($this->adapter);$select = $queryMapper->getSql()->select();$select->columns(['id', 'uuid', 'name', 'status', 'image', 'privacy', 'type', 'marketplace']);$select->from(['t' => MicrolearningTopicMapper::_TABLE]);$select->join(['o' => CompanyMapper::_TABLE], ' c.company_id = o.id ', ['company_uuid' => 'uuid']);$select->where->equalTo('c.privacy', MicrolearningTopic::PRIVACY_PUBLIC);$select->where->equalTo('c.type', MicrolearningTopic::TYPE_FREE);$select->where->equalTo('c.status', MicrolearningTopic::STATUS_ACTIVE);if($search) {$select->where->like('c.name', '%' . $search . '%');}$select->order(['name ASC']);//echo $select->getSqlString($this->adapter->platform); exit;$records = $queryMapper->fetchAll($select);$items = [];foreach($records as $record){$topicUser = $microlearningTopicUserMapper->fetchOneByUserIdAndTopicId($currentUser->id, $record['id']);$params = ['company_uuid' => $record['company_uuid'],'topic_uuid' => $record['uuid']];$item = ['name' => $record['name'],'image' => $storage->getGenericImage($path, $record['uuid'], $record['marketplace']),'status' => $topicUser ? 'LABEL_ENROLLED' : 'LABEL_AVAILABLE','link_enroll' => $topicUser ? '' : $this->url()->fromRoute('marketplace/enroll', $params),];array_push($items, $item);}}$response = ['success' => true,'data' => $items];return new JsonModel($response);}public function enrollAction(){$request = $this->getRequest();if($request->isPost()) {$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$company_uuid = $this->params()->fromRoute('company_uuid');$topic_uuid = $this->params()->fromRoute('topic_uuid');$companyMapper = CompanyMapper::getInstance($this->adapter);$company = $companyMapper->fetchOneByUuid($company_uuid);if(!$company) {$this->logger->err('ERROR_COMPANY_NOT_FOUND', ['company_uuid' => $company_uuid]);return new JsonModel(['success' => false,'data' => 'ERROR_COMPANY_NOT_FOUND']);}$topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);$topic = $topicMapper->fetchOneByUuid($topic_uuid);if(!$topic) {return new JsonModel(['success' => false,'data' => 'ERROR_TOPIC_NOT_FOUND']);}if($topic->company_id != $company->id) {return new JsonModel(['success' => false,'data' => 'ERROR_UNAUTHORIZED']);}if($topic->status != MicrolearningTopic::STATUS_ACTIVE || $topic->privacy != MicrolearningTopic::PRIVACY_PUBLIC || $topic->type != MicrolearningTopic::TYPE_FREE) {$this->logger->err('ERROR_UNAUTHORIZED', ['company_id' => $company->id, 'topic_id' => $topic->id]);return new JsonModel(['success' => false,'data' => 'ERROR_UNAUTHORIZED']);}$topicUserMapper = MicrolearningTopicUserMapper::getInstance($this->adapter);$topicUser = $topicUserMapper->fetchOneByUserIdAndTopicId($currentUser->id, $topic->id);if($topicUser) {return new JsonModel(['success' => false,'data' => 'ERROR_UNAUTHORIZED']);}$topicUser = new MicrolearningTopicUser();$topicUser->company_id = $company->id;$topicUser->topic_id = $topic->id;$topicUser->user_id = $currentUser->id;$topicUser->access = MicrolearningTopicUser::ACCESS_UNLIMITED;if($topicUserMapper->insert($topicUser)) {$topicUser = $topicUserMapper->fetchOne($topic->id);if($topicUser) {$microlearningUserMapper = MicrolearningUserMapper::getInstance($this->adapter);$microlearningUser = $microlearningUserMapper->fetchOneByUserIdAndCompanyId($topicUser->user_id, $topicUser->company_id);if($microlearningUser) {$microlearningUser->updated_on = $topicUser->updated_on;$microlearningUserMapper->update($microlearningUser);} else {$microlearningUser = new MicrolearningUser();$microlearningUser->company_id = $topicUser->company_id;$microlearningUser->user_id = $topicUser->user_id;$microlearningUser->added_on = $topicUser->added_on;$microlearningUser->updated_on = $topicUser->updated_on;$microlearningUserMapper->insert($microlearningUser);}}return new JsonModel(['success' => true,'data' => 'LABEL_YOU_HAVE_BEEN_SUCCESSFULLY_ENROLLED']);} else {return new JsonModel(['success' => false,'data' => 'ERROR_UNAUTHORIZED']);}} else {return new JsonModel(['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED']);}}public function claimAction(){$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();return new JsonModel(['success' => true,'data' => 'Por definirse']);}public function getCategoriesAction(){$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$acl = $this->getEvent()->getViewModel()->getVariable('acl');$allowDailyPuse = $acl->isAllowed($currentUser->usertype_id, 'daily-pulse');$data = ['capsules' => 'LABEL_CAPSULES'];if( $allowDailyPuse) {$data['rewards'] = 'LABEL_REWARDS';}return new JsonModel(['success' => true,'data' => $data]);}}