Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
PKPToolsHandler.inc.php
1 <?php
2 
15 // Import the base ManagementHandler.
16 import('lib.pkp.pages.management.ManagementHandler');
17 
22  function PKPToolsHandler() {
23  parent::Handler();
24  $this->addRoleAssignment(
25  ROLE_ID_MANAGER,
26  array('tools')
27  );
28  }
29 
30 
31  //
32  // Public handler methods.
33  //
34  function setupTemplate($request) {
35  parent::setupTemplate($request);
36  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_MANAGER, LOCALE_COMPONENT_APP_SUBMISSION);
37  }
38 
44  function tools($args, $request) {
45  $path = array_shift($args);
46  switch ($path) {
47  case 'index':
48  $this->index($args, $request);
49  break;
50  case 'statistics':
51  $this->statisticcs($args, $request);
52  break;
53  case 'report':
54  $this->report($args, $request);
55  break;
56  case 'reportGenerator':
57  $this->reportGenerator($args, $request);
58  break;
59  case 'generateReport':
60  $this->generateReport($args, $request);
61  break;
62  case 'saveStatisticsSettings':
63  return $this->saveStatisticsSettings($args, $request);
64  default:
65  assert(false);
66  }
67  }
68 
74  function index($args, $request) {
75  $templateMgr = TemplateManager::getManager($request);
76  $this->setupTemplate($request);
77  $templateMgr->display('management/tools/index.tpl');
78  }
79 
85  function statisticcs($args, $request) {
86  $this->setupTemplate($request);
87  $context = $request->getContext();
88 
89  $templateMgr = TemplateManager::getManager($request);
90 
91  $application = Application::getApplication();
92  $templateMgr->assign('appSettings', $this->hasAppStatsSettings());
93  $templateMgr->assign('contextObjectName', __($application->getNameKey()));
94 
95  $reportPlugins = PluginRegistry::loadCategory('reports');
96  $templateMgr->assign_by_ref('reportPlugins', $reportPlugins);
97 
98  $templateMgr->assign('defaultMetricType', $context->getSetting('defaultMetricType'));
99  $availableMetricTypes = $context->getMetricTypes(true);
100  $templateMgr->assign('availableMetricTypes', $availableMetricTypes);
101  if (count($availableMetricTypes) > 1) {
102  $templateMgr->assign('showMetricTypeSelector', true);
103  }
104 
105  $templateMgr->display('management/tools/statistics.tpl');
106  }
107 
114  function report($args, $request) {
115  $this->setupTemplate($request);
116 
117  $pluginName = $request->getUserVar('pluginName');
118  $reportPlugins = PluginRegistry::loadCategory('reports');
119 
120  if ($pluginName == '' || !isset($reportPlugins[$pluginName])) {
121  Request::redirect(null, null, 'management', 'statistics');
122  }
123 
124  $plugin = $reportPlugins[$pluginName];
125  $plugin->display($args, $request);
126  }
127 
133  function reportGenerator($args, $request) {
134  $this->setupTemplate($request);
135 
136  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_SUBMISSION, LOCALE_COMPONENT_APP_EDITOR);
137 
138  $templateMgr = TemplateManager::getManager();
139  $templateMgr->display('management/tools/reportGenerator.tpl');
140  }
141 
142 
149  function generateReport($args, $request) {
150  $this->setupTemplate($request);
151  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_SUBMISSION);
152 
153  $router = $request->getRouter();
154  $context = $router->getContext($request);
155  import('classes.statistics.StatisticsHelper');
156  $statsHelper = new StatisticsHelper();
157 
158  $metricType = $request->getUserVar('metricType');
159  if (is_null($metricType)) {
160  $metricType = $context->getDefaultMetricType();
161  }
162 
163  // Generates only one metric type report at a time.
164  if (is_array($metricType)) $metricType = current($metricType);
165  if (!is_scalar($metricType)) $metricType = null;
166 
167  $reportPlugin = $statsHelper->getReportPluginByMetricType($metricType);
168  if (!$reportPlugin || is_null($metricType)) {
169  $request->redirect(null, null, 'tools', 'statistics');
170  }
171 
172  $columns = $request->getUserVar('columns');
173  $filters = unserialize($request->getUserVar('filters'));
174  if (!$filters) $filters = $request->getUserVar('filters');
175 
176  $orderBy = $request->getUserVar('orderBy');
177  if ($orderBy) {
178  $orderBy = unserialize($orderBy);
179  if (!$orderBy) $orderBy = $request->getUserVar('orderBy');
180  } else {
181  $orderBy = array();
182  }
183 
184  $metrics = $reportPlugin->getMetrics($metricType, $columns, $filters, $orderBy);
185 
186  $allColumnNames = $statsHelper->getColumnNames();
187  $columnOrder = array_keys($allColumnNames);
188  $columnNames = array();
189 
190  foreach ($columnOrder as $column) {
191  if (in_array($column, $columns)) {
192  $columnNames[$column] = $allColumnNames[$column];
193  }
194 
195  if ($column == STATISTICS_DIMENSION_ASSOC_TYPE && in_array(STATISTICS_DIMENSION_ASSOC_ID, $columns)) {
196  $columnNames['common.title'] = __('common.title');
197  }
198  }
199 
200  // Make sure the metric column will always be present.
201  if (!in_array(STATISTICS_METRIC, $columnNames)) $columnNames[STATISTICS_METRIC] = $allColumnNames[STATISTICS_METRIC];
202 
203  header('content-type: text/comma-separated-values');
204  header('content-disposition: attachment; filename=statistics-' . date('Ymd') . '.csv');
205  $fp = fopen('php://output', 'wt');
206  fputcsv($fp, array($reportPlugin->getDisplayName()));
207  fputcsv($fp, array($reportPlugin->getDescription()));
208  fputcsv($fp, array(__('common.metric') . ': ' . $metricType));
209  fputcsv($fp, array(__('manager.statistics.reports.reportUrl') . ': ' . $request->getCompleteUrl()));
210  fputcsv($fp, array(''));
211 
212  // Just for better displaying.
213  $columnNames = array_merge(array(''), $columnNames);
214 
215  fputcsv($fp, $columnNames);
216  foreach ($metrics as $record) {
217  $row = array();
218  foreach ($columnNames as $key => $name) {
219  if (empty($name)) {
220  // Column just for better displaying.
221  $row[] = '';
222  continue;
223  }
224 
225  // Give a chance for subclasses to set the row values.
226  if ($returner = $this->getReportRowValue($key, $record)) {
227  $row = $returner;
228  continue;
229  }
230 
231  switch ($key) {
232  case 'common.title':
233  $assocId = $record[STATISTICS_DIMENSION_ASSOC_ID];
234  $assocType = $record[STATISTICS_DIMENSION_ASSOC_TYPE];
235  $row[] = $this->getObjectTitle($assocId, $assocType);
236  break;
237  case STATISTICS_DIMENSION_ASSOC_TYPE:
238  $assocType = $record[STATISTICS_DIMENSION_ASSOC_TYPE];
239  $row[] = $statsHelper->getObjectTypeString($assocType);
240  break;
241  case STATISTICS_DIMENSION_CONTEXT_ID:
242  $assocId = $record[STATISTICS_DIMENSION_CONTEXT_ID];
243  $assocType = Application::getContextAssocType();
244  $row[] = $this->getObjectTitle($assocId, $assocType);
245  break;
246  case STATISTICS_DIMENSION_SUBMISSION_ID:
247  if (isset($record[STATISTICS_DIMENSION_SUBMISSION_ID])) {
248  $assocId = $record[STATISTICS_DIMENSION_SUBMISSION_ID];
249  $assocType = ASSOC_TYPE_SUBMISSION;
250  $row[] = $this->getObjectTitle($assocId, $assocType);
251  } else {
252  $row[] = '';
253  }
254  break;
255  case STATISTICS_DIMENSION_REGION:
256  if (isset($record[STATISTICS_DIMENSION_REGION]) && isset($record[STATISTICS_DIMENSION_COUNTRY])) {
257  $geoLocationTool = $statsHelper->getGeoLocationTool();
258  if ($geoLocationTool) {
259  $regions = $geoLocationTool->getRegions($record[STATISTICS_DIMENSION_COUNTRY]);
260  $regionId = $record[STATISTICS_DIMENSION_REGION];
261  if (strlen($regionId) == 1) $regionId = '0' . $regionId;
262  if (isset($regions[$regionId])) {
263  $row[] = $regions[$regionId];
264  break;
265  }
266  }
267  }
268  $row[] = '';
269  break;
270  case STATISTICS_DIMENSION_PKP_SECTION_ID:
271  $sectionId = null;
272  if (isset($record[STATISTICS_DIMENSION_PKP_SECTION_ID])) {
273  $sectionId = $record[STATISTICS_DIMENSION_PKP_SECTION_ID];
274  }
275  if ($sectionId) {
276  $row[] = $this->getObjectTitle($sectionId, ASSOC_TYPE_SECTION);
277  } else {
278  $row[] = '';
279  }
280  break;
281  case STATISTICS_DIMENSION_FILE_TYPE:
282  if ($record[$key]) {
283  $row[] = $statsHelper->getFileTypeString($record[$key]);
284  } else {
285  $row[] = '';
286  }
287  break;
288  default:
289  $row[] = $record[$key];
290  break;
291  }
292  }
293  fputcsv($fp, $row);
294  }
295  fclose($fp);
296  }
297 
302  function saveStatisticsSettings($args, $request) {
303  $router = $request->getRouter();
304  $context = $router->getContext($request);
305 
306  $defaultMetricType = $request->getUserVar('defaultMetricType');
307  $context->updateSetting('defaultMetricType', $defaultMetricType);
308 
309  $notificationManager = new NotificationManager();
310  $user = $request->getUser();
311  $notificationManager->createTrivialNotification($user->getId());
312 
313  ('classes.core.JSONMessage');
314  $json = new JSONMessage();
315  return $json->getString();
316  }
317 
318 
319  //
320  // Protected methods.
321  //
329  protected function getReportRowValue($key, $record) {
330  return null;
331  }
332 
340  protected function getObjectTitle($assocId, $assocType) {
341  switch ($assocType) {
343  $contextDao = Application::getContextDAO(); /* @var $contextDao ContextDAO */
344  $context = $contextDao->getById($assocId);
345  if (!$context) break;
346  return $context->getLocalizedName();
347  case ASSOC_TYPE_SUBMISSION:
348  $submissionDao = Application::getSubmissionDAO(); /* @var $submissionDao SubmissionDAO */
349  $submission = $submissionDao->getById($assocId, null, true);
350  if (!$submission) break;
351  return $submission->getLocalizedTitle();
352  case ASSOC_TYPE_SECTION:
353  $sectionDao = Application::getSectionDAO();
354  $section = $sectionDao->getById($assocId);
355  if (!$section) break;
356  return $section->getLocalizedTitle();
357  case ASSOC_TYPE_SUBMISSION_FILE:
358  $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO'); /* @var $submissionFileDao SubmissionFileDAO */
359  $submissionFile = $submissionFileDao->getLatestRevision($assocId);
360  if (!$submissionFile) break;
361  return $submissionFile->getFileLabel();
362  }
363 
364  return null;
365  }
366 
372  protected function hasAppStatsSettings() {
373  return false;
374  }
375 
376 }
377 
378 ?>
saveStatisticsSettings($args, $request)
static & getDAO($name, $dbconn=null)
static requireComponents()
static & loadCategory($category, $enabledOnly=false, $mainContextId=null)
redirect($context=null, $page=null, $op=null, $path=null, $params=null, $anchor=null)
addRoleAssignment($roleIds, $operations)
Class to represent a JSON (Javascript Object Notation) message.
static getSubmissionDAO()
statisticcs($args, $request)
static getContextAssocType()
tools($args, $request)
getReportRowValue($key, $record)
Statistics helper class.
Handle requests for Tool pages.
getObjectTitle($assocId, $assocType)
report($args, $request)
Base class for all management page handlers.
static getContextDAO()
reportGenerator($args, $request)
static getSectionDAO()
index($args, $request)
generateReport($args, $request)