*/ class DeliveriesMonitorDatatable implements DatatablePayload, ServiceLocatorAwareInterface { use ServiceLocatorAwareTrait; /** * @var DatatableRequest */ protected $request; /** * @var DatatableRequest */ protected $datatableRequest; /** * @var \core_kernel_classes_Resource */ protected $delivery; /** * DeliveriesMonitorDatatable constructor. * @param \core_kernel_classes_Resource $delivery * @param \Request $request */ public function __construct(\core_kernel_classes_Resource $delivery = null, $request) { $this->datatableRequest = DatatableRequest::fromGlobals(); $this->request = $request; $this->delivery = $delivery; } public function getPayload() { $context = $this->request->hasParameter('context') ? $this->request->getParameter('context') : null; $filters = []; foreach ($this->datatableRequest->getFilters() as $filterKey => $filterValue) { if ($filterKey === 'start_time') { $times = explode(' - ', $filterValue); $filters[] = ['start_time' => '>' . $times[0]]; if (isset($times[1])) { $filters[] = ['start_time' => '<' . $times[1]]; } } elseif ($filterKey === 'tag') { $filters[] = [$filterKey => $filterValue]; } else { $filters[] = [$filterKey => 'LIKE %'.$filterValue.'%']; } } $options = []; $options['filters'] = $filters; $orderCol = DeliveryHelper::adjustColumnName($this->datatableRequest->getSortBy()); if ($orderCol) { $options['order'] = join(' ', [ $orderCol, $this->datatableRequest->getSortOrder(), $this->datatableRequest->getSortType() ]); } $options['limit'] = $this->datatableRequest->getRows(); $options['offset'] = ($this->datatableRequest->getPage() - 1) * $this->datatableRequest->getRows(); $service = $this->getServiceLocator()->get(ProctorService::SERVICE_ID); $proctor = \common_session_SessionManager::getSession()->getUser(); $executions = $service->getProctorableDeliveryExecutions($proctor, $this->delivery, $context, $options); $total = $service->countProctorableDeliveryExecutions($proctor, $this->delivery, $context, $options); $totalPages = ceil($total / $this->datatableRequest->getRows()); return $this->doPostProcessing($executions, $total, $totalPages); } /** * @param array $executionsData * @param integer $amount * @param integer $pages * @return array * @throws \common_ext_ExtensionException */ protected function doPostProcessing(array $executionsData, $amount, $pages) { return [ 'success' => true, 'amount' => $amount, 'total' => $pages, 'page' => $this->datatableRequest->getPage(), 'rows' => $this->datatableRequest->getRows(), 'data' => $this->getDeliveryHelperService()->adjustDeliveryExecutions($executionsData), ]; } /** * @return array */ public function jsonSerialize() { return $this->getPayload(); } /** * @return DeliveryExecutionList */ private function getDeliveryHelperService() { return $this->getServiceLocator()->get(DeliveryExecutionList::class); } }