/**
  * Get report generator form object.
  * @return ReportGeneratorForm
  */
 function &_getReportGeneratorForm($request)
 {
     $router = $request->getRouter();
     $context = $router->getContext($request);
     $metricType = $request->getUserVar('metricType');
     if (!$metricType) {
         $metricType = $context->getDefaultMetricType();
     }
     $statsHelper = new StatisticsHelper();
     $reportPlugin = $statsHelper->getReportPluginByMetricType($metricType);
     if (!is_scalar($metricType) || !$reportPlugin) {
         fatalError('Invalid metric type.');
     }
     $columns = $reportPlugin->getColumns($metricType);
     $columns = array_flip(array_intersect(array_flip($statsHelper->getColumnNames()), $columns));
     $optionalColumns = $reportPlugin->getOptionalColumns($metricType);
     $optionalColumns = array_flip(array_intersect(array_flip($statsHelper->getColumnNames()), $optionalColumns));
     $objects = $reportPlugin->getObjectTypes($metricType);
     $objects = array_flip(array_intersect(array_flip($statsHelper->getObjectTypeString()), $objects));
     $defaultReportTemplates = $reportPlugin->getDefaultReportTemplates($metricType);
     // If the report plugin doesn't works with the file type column,
     // don't load file types.
     if (isset($columns[STATISTICS_DIMENSION_FILE_TYPE])) {
         $fileTypes = $statsHelper->getFileTypeString();
     } else {
         $fileTypes = null;
     }
     // Metric type will be presented in header, remove if any.
     if (isset($columns[STATISTICS_DIMENSION_METRIC_TYPE])) {
         unset($columns[STATISTICS_DIMENSION_METRIC_TYPE]);
     }
     $reportTemplate = $request->getUserVar('reportTemplate');
     import('controllers.statistics.form.ReportGeneratorForm');
     $reportGeneratorForm = new ReportGeneratorForm($columns, $optionalColumns, $objects, $fileTypes, $metricType, $defaultReportTemplates, $reportTemplate);
     return $reportGeneratorForm;
 }
Ejemplo n.º 2
0
 /**
  * Generate statistics reports from passed
  * request arguments.
  * @param $args array
  * @param $request PKPRequest
  */
 function generateReport($args, $request)
 {
     $this->setupTemplate($request);
     AppLocale::requireComponents(LOCALE_COMPONENT_PKP_SUBMISSION);
     $router = $request->getRouter();
     $context = $router->getContext($request);
     import('classes.statistics.StatisticsHelper');
     $statsHelper = new StatisticsHelper();
     $metricType = $request->getUserVar('metricType');
     if (is_null($metricType)) {
         $metricType = $context->getDefaultMetricType();
     }
     // Generates only one metric type report at a time.
     if (is_array($metricType)) {
         $metricType = current($metricType);
     }
     if (!is_scalar($metricType)) {
         $metricType = null;
     }
     $reportPlugin = $statsHelper->getReportPluginByMetricType($metricType);
     if (!$reportPlugin || is_null($metricType)) {
         $request->redirect(null, null, 'tools', 'statistics');
     }
     $columns = $request->getUserVar('columns');
     $filters = unserialize($request->getUserVar('filters'));
     if (!$filters) {
         $filters = $request->getUserVar('filters');
     }
     $orderBy = $request->getUserVar('orderBy');
     if ($orderBy) {
         $orderBy = unserialize($orderBy);
         if (!$orderBy) {
             $orderBy = $request->getUserVar('orderBy');
         }
     } else {
         $orderBy = array();
     }
     $metrics = $reportPlugin->getMetrics($metricType, $columns, $filters, $orderBy);
     $allColumnNames = $statsHelper->getColumnNames();
     $columnOrder = array_keys($allColumnNames);
     $columnNames = array();
     foreach ($columnOrder as $column) {
         if (in_array($column, $columns)) {
             $columnNames[$column] = $allColumnNames[$column];
         }
         if ($column == STATISTICS_DIMENSION_ASSOC_TYPE && in_array(STATISTICS_DIMENSION_ASSOC_ID, $columns)) {
             $columnNames['common.title'] = __('common.title');
         }
     }
     // Make sure the metric column will always be present.
     if (!in_array(STATISTICS_METRIC, $columnNames)) {
         $columnNames[STATISTICS_METRIC] = $allColumnNames[STATISTICS_METRIC];
     }
     header('content-type: text/comma-separated-values');
     header('content-disposition: attachment; filename=statistics-' . date('Ymd') . '.csv');
     $fp = fopen('php://output', 'wt');
     fputcsv($fp, array($reportPlugin->getDisplayName()));
     fputcsv($fp, array($reportPlugin->getDescription()));
     fputcsv($fp, array(__('common.metric') . ': ' . $metricType));
     fputcsv($fp, array(__('manager.statistics.reports.reportUrl') . ': ' . $request->getCompleteUrl()));
     fputcsv($fp, array(''));
     // Just for better displaying.
     $columnNames = array_merge(array(''), $columnNames);
     fputcsv($fp, $columnNames);
     foreach ($metrics as $record) {
         $row = array();
         foreach ($columnNames as $key => $name) {
             if (empty($name)) {
                 // Column just for better displaying.
                 $row[] = '';
                 continue;
             }
             // Give a chance for subclasses to set the row values.
             if ($returner = $this->getReportRowValue($key, $record)) {
                 $row[] = $returner;
                 continue;
             }
             switch ($key) {
                 case 'common.title':
                     $assocId = $record[STATISTICS_DIMENSION_ASSOC_ID];
                     $assocType = $record[STATISTICS_DIMENSION_ASSOC_TYPE];
                     $row[] = $this->getObjectTitle($assocId, $assocType);
                     break;
                 case STATISTICS_DIMENSION_ASSOC_TYPE:
                     $assocType = $record[STATISTICS_DIMENSION_ASSOC_TYPE];
                     $row[] = $statsHelper->getObjectTypeString($assocType);
                     break;
                 case STATISTICS_DIMENSION_CONTEXT_ID:
                     $assocId = $record[STATISTICS_DIMENSION_CONTEXT_ID];
                     $assocType = Application::getContextAssocType();
                     $row[] = $this->getObjectTitle($assocId, $assocType);
                     break;
                 case STATISTICS_DIMENSION_SUBMISSION_ID:
                     if (isset($record[STATISTICS_DIMENSION_SUBMISSION_ID])) {
                         $assocId = $record[STATISTICS_DIMENSION_SUBMISSION_ID];
                         $assocType = ASSOC_TYPE_SUBMISSION;
                         $row[] = $this->getObjectTitle($assocId, $assocType);
                     } else {
                         $row[] = '';
                     }
                     break;
                 case STATISTICS_DIMENSION_REGION:
                     if (isset($record[STATISTICS_DIMENSION_REGION]) && isset($record[STATISTICS_DIMENSION_COUNTRY])) {
                         $geoLocationTool = $statsHelper->getGeoLocationTool();
                         if ($geoLocationTool) {
                             $regions = $geoLocationTool->getRegions($record[STATISTICS_DIMENSION_COUNTRY]);
                             $regionId = $record[STATISTICS_DIMENSION_REGION];
                             if (strlen($regionId) == 1) {
                                 $regionId = '0' . $regionId;
                             }
                             if (isset($regions[$regionId])) {
                                 $row[] = $regions[$regionId];
                                 break;
                             }
                         }
                     }
                     $row[] = '';
                     break;
                 case STATISTICS_DIMENSION_PKP_SECTION_ID:
                     $sectionId = null;
                     if (isset($record[STATISTICS_DIMENSION_PKP_SECTION_ID])) {
                         $sectionId = $record[STATISTICS_DIMENSION_PKP_SECTION_ID];
                     }
                     if ($sectionId) {
                         $row[] = $this->getObjectTitle($sectionId, ASSOC_TYPE_SECTION);
                     } else {
                         $row[] = '';
                     }
                     break;
                 case STATISTICS_DIMENSION_FILE_TYPE:
                     if ($record[$key]) {
                         $row[] = $statsHelper->getFileTypeString($record[$key]);
                     } else {
                         $row[] = '';
                     }
                     break;
                 default:
                     $row[] = $record[$key];
                     break;
             }
         }
         fputcsv($fp, $row);
     }
     fclose($fp);
 }