/** * View entries * * @param int $uid * @return string */ protected function _view($uid = 0) { // Build the final HTML $view = $this->view('default', 'stats'); // Get pub stats for each publication $pubLog = new \Components\Publications\Tables\Log($this->_database); $view->pubstats = $this->_stats ? $this->_stats : $pubLog->getAuthorStats($uid, 0, false); // Get date of first log $view->firstlog = $pubLog->getFirstLogDate(); $view->totals = $pubLog->getTotals($uid); // Output HTML $view->option = $this->_option; $view->database = $this->_database; $view->uid = $uid; $view->pubconfig = Component::params('com_publications'); if ($this->getError()) { $view->setError($this->getError()); } return $view->loadTemplate(); }
/** * Send emails to authors with the monthly stats * * @param object $job \Components\Cron\Models\Job * @return boolean */ public function sendAuthorStats(\Components\Cron\Models\Job $job) { $database = App::get('db'); $pconfig = Component::params('com_publications'); // Get some params $limit = $pconfig->get('limitStats', 5); $image = $pconfig->get('email_image', ''); Lang::load('com_publications', PATH_CORE) || Lang::load('com_publications', PATH_CORE . DS . 'components' . DS . 'com_publications' . DS . 'site'); // Is logging enabled? if (is_file(PATH_CORE . DS . 'components' . DS . 'com_publications' . DS . 'tables' . DS . 'logs.php')) { require_once PATH_CORE . DS . 'components' . DS . 'com_publications' . DS . 'tables' . DS . 'logs.php'; } else { $this->setError('Publication logs not present on this hub, cannot email stats to authors'); return false; } // Helpers require_once PATH_CORE . DS . 'components' . DS . 'com_members' . DS . 'helpers' . DS . 'imghandler.php'; require_once PATH_CORE . DS . 'components' . DS . 'com_publications' . DS . 'helpers' . DS . 'html.php'; // Get all registered authors who subscribed to email $query = "SELECT A.user_id, P.picture "; $query .= " FROM #__publication_authors as A "; $query .= " JOIN #__xprofiles as P ON A.user_id = P.uidNumber "; $query .= " WHERE P.mailPreferenceOption != 0 "; // either 1 (set to YES) or -1 (never set) $query .= " AND A.user_id > 0 AND A.status=1 "; // If we need to restrict to selected authors $params = $job->get('params'); if (is_object($params) && $params->get('userids')) { $apu = explode(',', $params->get('userids')); $apu = array_map('trim', $apu); $query .= " AND A.user_id IN ("; $tquery = ''; foreach ($apu as $a) { $tquery .= "'" . $a . "',"; } $tquery = substr($tquery, 0, strlen($tquery) - 1); $query .= $tquery . ") "; } $query .= " GROUP BY A.user_id "; $database->setQuery($query); if (!($authors = $database->loadObjectList())) { return true; } // Set email config $from = array('name' => Config::get('fromname') . ' ' . Lang::txt('Publications'), 'email' => Config::get('mailfrom'), 'multipart' => md5(date('U'))); $subject = Lang::txt('Monthly Publication Usage Report'); $i = 0; foreach ($authors as $author) { // Get the user's account $user = User::getInstance($author->user_id); if (!$user->get('id')) { // Skip if not registered continue; } // Get pub stats for each author $pubLog = new \Components\Publications\Tables\Log($database); $pubstats = $pubLog->getAuthorStats($author->user_id); if (!$pubstats || !count($pubstats)) { // Nothing to send continue; } // Plain text $eview = new \Hubzero\Plugin\View(array('folder' => 'cron', 'element' => 'publications', 'name' => 'emails', 'layout' => 'stats_plain')); $eview->option = 'com_publications'; $eview->controller = 'publications'; $eview->user = $user; $eview->pubstats = $pubstats; $eview->limit = $limit; $eview->image = $image; $eview->config = $pconfig; $eview->totals = $pubLog->getTotals($author->user_id, 'author'); $plain = $eview->loadTemplate(); $plain = str_replace("\n", "\r\n", $plain); // HTML $eview->setLayout('stats_html'); $html = $eview->loadTemplate(); $html = str_replace("\n", "\r\n", $html); // Build message $message = new \Hubzero\Mail\Message(); $message->setSubject($subject)->addFrom($from['email'], $from['name'])->addTo($user->get('email'), $user->get('name'))->addHeader('X-Component', 'com_publications')->addHeader('X-Component-Object', 'publications'); $message->addPart($plain, 'text/plain'); $message->addPart($html, 'text/html'); // Send mail if (!$message->send()) { $this->setError(Lang::txt('PLG_CRON_PUBLICATIONS_ERROR_FAILED_TO_MAIL', $user->get('email'))); } $mailed[] = $user->get('email'); } return true; }
/** * Publication stats * * @return string */ protected function _stats() { // Incoming $pid = $this->_pid ? $this->_pid : Request::getInt('pid', 0); $version = Request::getVar('version', 'default'); require_once PATH_CORE . DS . 'components' . DS . 'com_publications' . DS . 'tables' . DS . 'logs.php'; $view = new \Hubzero\Plugin\View(array('folder' => 'projects', 'element' => 'publications', 'name' => 'stats')); // Get pub stats for each publication $pubLog = new \Components\Publications\Tables\Log($this->_database); $view->pubstats = $pubLog->getPubStats($this->model->get('id'), $pid); // Get date of first log $view->firstlog = $pubLog->getFirstLogDate(); // Test $view->totals = $pubLog->getTotals($this->model->get('id'), 'project'); // Output HTML $view->option = $this->_option; $view->database = $this->_database; $view->project = $this->model; $view->uid = $this->_uid; $view->pid = $pid; $view->pub = new \Components\Publications\Models\Publication($pid, $version); $view->task = $this->_task; $view->config = $this->model->config(); $view->pubconfig = $this->_pubconfig; $view->version = $version; $view->title = $this->_area['title']; // Get messages and errors $view->msg = $this->_msg; if ($this->getError()) { $view->setError($this->getError()); } return $view->loadTemplate(); }
/** * Generate report * * @return void */ public function generateTask() { // Incoming $data = Request::getVar('data', array(), 'post', 'array'); $from = Request::getVar('fromdate', Date::of('-1 month')->toLocal('Y-m')); $to = Request::getVar('todate', Date::of('now')->toLocal('Y-m')); $filter = Request::getVar('searchterm', ''); if (empty($data)) { $this->setError(Lang::txt('Please pick at least one information field to report')); } $date_regex = '/^(19|20)\\d\\d[\\-\\/.](0[1-9]|1[012])$/'; if (!preg_match($date_regex, $from) || !preg_match($date_regex, $to)) { $this->setError(Lang::txt('Please use yyyy-mm format for the date')); } if (strtotime($from) > strtotime($to)) { $this->setError(Lang::txt('The start date of report should be earlier than the end date')); } // Project table class $obj = $this->model->table(); // Check authorization if (!$this->model->reviewerAccess('admin') && !$this->model->reviewerAccess('reports')) { if (User::isGuest()) { $this->_msg = Lang::txt('COM_PUBLICATIONS_REPORTS_LOGIN'); $this->_login(); return; } throw new Exception(Lang::txt('COM_PUBLICATIONS_REPORTS_ERROR_UNAUTHORIZED'), 403); return; } // Get stats if (!$this->getError()) { require_once PATH_CORE . DS . 'components' . DS . 'com_publications' . DS . 'tables' . DS . 'logs.php'; $objLog = new \Components\Publications\Tables\Log($this->database); // Get all test projects $exclude = $obj->getProjectsByTag('test', true, 'id'); $stats = $objLog->getCustomStats($from, $to, $exclude, $filter); $filename = 'from_' . $from . '_to_' . $to . '_report.csv'; if ($stats) { // Output to CSV header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename=' . $filename); $output = fopen('php://output', 'w'); // output the column headings fputcsv($output, $data); foreach ($stats as $record) { $sorted = array(); foreach ($data as $field) { $input = $record->{$field}; if ($field == 'doi') { $input = $input ? 'http://dx.doi.org/' . $input : 'N/A'; } $sorted[] = $input; } fputcsv($output, $sorted); } fclose($output); } else { $this->setError(Lang::txt('Nothing to report for selected date range and/or search term')); } } // Redirect on error if ($this->getError()) { App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=reports&task=custom' . '&searchterm=' . $filter), $this->getError(), 'error'); } return; }