Rev 602 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?phpdeclare(strict_types=1);namespace LeadersLinked\Controller;use Laminas\Db\Adapter\AdapterInterface;use Laminas\Mvc\Controller\AbstractActionController;use Laminas\Log\LoggerInterface;use Laminas\View\Model\ViewModel;use Laminas\View\Model\JsonModel;use LeadersLinked\Mapper\QueryMapper;use LeadersLinked\Mapper\CompanyMapper;use LeadersLinked\Mapper\MicrolearningTopicMapper;use LeadersLinked\Mapper\MicrolearningCapsuleMapper;use LeadersLinked\Model\MicrolearningCapsule;use LeadersLinked\Model\MicrolearningCapsuleUser;use LeadersLinked\Mapper\MicrolearningCapsuleUserMapper;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\Mapper\MicrolearningTopicCapsuleMapper;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 == 'capsules') {$microlearningCapsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);$path = $storage->getPathMicrolearningCapsule();$queryMapper = QueryMapper::getInstance($this->adapter);$select = $queryMapper->getSql()->select();$select->columns(['id', 'uuid', 'name', 'status', 'image', 'privacy', 'type', 'marketplace']);$select->from(['c' => MicrolearningCapsuleMapper::_TABLE]);$select->join(['t' => MicrolearningTopicMapper::_TABLE], ' c.topic_id = t.id ', ['topic_uuid' => 'uuid']);$select->join(['o' => CompanyMapper::_TABLE], ' c.company_id = o.id ', ['company_uuid' => 'uuid']);$select->where->equalTo('c.privacy', MicrolearningCapsule::PRIVACY_PUBLIC);$select->where->equalTo('c.type', MicrolearningCapsule::TYPE_FREE);$select->where->equalTo('c.status', MicrolearningCapsule::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){$capsuleUser = $microlearningCapsuleUserMapper->fetchOneByUserIdAndCapsuleId($currentUser->id, $record['id']);$params = ['company_id' => $record['company_uuid'],'topic_id' => $record['topic_uuid'],'capsule_id' => $record['uuid']];$item = ['name' => $record['name'],'image' => $storage->getGenericImage($path, $record['uuid'], $record['marketplace']),'status' => $capsuleUser ? 'LABEL_ENROLLED' : 'LABEL_AVAILABLE','link_enroll' => $capsuleUser ? '' : $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_id = $this->params()->fromRoute('company_id');$topic_id = $this->params()->fromRoute('topic_id');$capsule_id = $this->params()->fromRoute('capsule_id');$companyMapper = CompanyMapper::getInstance($this->adapter);$company = $companyMapper->fetchOneByUuid($company_id);if(!$company) {return new JsonModel(['success' => false,'data' => 'ERROR_COMPANY_NOT_FOUND']);}$topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);$topic = $topicMapper->fetchOneByUuid($topic_id);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']);}$capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);$capsule = $capsuleMapper->fetchOneByUuid($capsule_id);if(!$capsule) {return new JsonModel(['success' => false,'data' => 'ERROR_CAPSULE_NOT_FOUND']);}$topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);$topicCapsule = $topicCapsuleMapper->fetchOneByTopicIdAndCapsuleId($topic->id, $capsule->id);if(!$topicCapsule) {return new JsonModel(['success' => false,'data' => 'ERROR_UNAUTHORIZED']);}if($capsule->status != MicrolearningCapsule::STATUS_ACTIVE|| $capsule->privacy != MicrolearningCapsule::PRIVACY_PUBLIC|| $capsule->type != MicrolearningCapsule::TYPE_FREE) {return new JsonModel(['success' => false,'data' => 'ERROR_UNAUTHORIZED']);}$capsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);$capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($currentUser->id, $capsule->id);if($capsuleUser) {return new JsonModel(['success' => false,'data' => 'ERROR_UNAUTHORIZED']);}$capsuleUser = new MicrolearningCapsuleUser();$capsuleUser->company_id = $company->id;$capsuleUser->topic_id = $topic->id;$capsuleUser->capsule_id = $capsule->id;$capsuleUser->user_id = $currentUser->id;$capsuleUser->access = MicrolearningCapsuleUser::ACCESS_UNLIMITED;if($capsuleUserMapper->insert($capsuleUser)) {$capsuleUser = $capsuleUserMapper->fetchOne($capsule->id);if($capsuleUser) {$microlearningUserMapper = MicrolearningUserMapper::getInstance($this->adapter);$microlearningUser = $microlearningUserMapper->fetchOneByUserIdAndCompanyId($capsuleUser->user_id, $capsuleUser->company_id);if($microlearningUser) {$microlearningUser->updated_on = $capsuleUser->updated_on;$microlearningUserMapper->update($microlearningUser);} else {$microlearningUser = new MicrolearningUser();$microlearningUser->company_id = $capsuleUser->company_id;$microlearningUser->user_id = $capsuleUser->user_id;$microlearningUser->added_on = $capsuleUser->added_on;$microlearningUser->updated_on = $capsuleUser->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]);}}