public function processAdmin() { global $CFG; $adminPath = iclicker_service::block_url('admin.php'); $this->results['adminPath'] = $adminPath; $this->results['status_url'] = iclicker_service::block_url('runner_status.php'); // admin check if (!iclicker_service::is_admin()) { throw new ClickerSecurityException("Current user is not an admin and cannot access the admin view"); } // get sorting params $pageNum = 1; $perPageNum = 20; // does not change if (optional_param('page', null, PARAM_ALPHANUM) != null) { $pageNum = required_param('page', PARAM_INT); if ($pageNum < 1) { $pageNum = 1; } } $sort = 'clicker_id'; if (optional_param('sort', null, PARAM_ALPHANUM) != null) { $sort = required_param('sort', PARAM_ALPHANUMEXT); } $this->results['sort'] = $sort; // get filtering params $search = optional_param('search', null, PARAM_ALPHANUMEXT); if (empty($search)) { $search = null; } $startDate = null; $startDateText = ''; if (optional_param('start_date', null, PARAM_ALPHANUMEXT) != null) { $startDate = required_param('start_date', PARAM_ALPHANUMEXT); $startDate = strtotime($startDate); if ($startDate) { $startDateText = date('Y-m-d', $startDate); } else { $startDate = null; } } $endDate = null; $endDateText = ''; if (optional_param('end_date', null, PARAM_ALPHANUMEXT) != null) { $endDate = required_param('end_date', PARAM_ALPHANUMEXT); $endDate = strtotime($endDate); if ($endDate) { // need to make this the end of the day $endDT = new DateTime('@' . $endDate); $endDT->setTimezone(new DateTimeZone(date_default_timezone_get())); $endDT->setTime(23, 59, 59); $endDate = $endDT->getTimestamp(); $endDateText = date('Y-m-d', $endDate); } else { $endDate = null; } } if ("POST" == $this->method) { if (optional_param('activate', null, PARAM_ALPHANUM) != null) { // First arrived at this page $activate = required_param('activate', PARAM_BOOL); if (optional_param('registrationId', null, PARAM_ALPHANUMEXT) == null) { $this->addMessage(self::KEY_ERROR, "reg.activate.registrationId.empty", null); } else { $reg_id = required_param('registrationId', PARAM_INT); // save a new clicker registration $cr = iclicker_service::set_registration_active($reg_id, $activate); if ($cr) { $args = new stdClass(); $args->cid = $cr->clicker_id; $args->user = iclicker_service::get_user_displayname($cr->owner_id); $this->addMessage(self::KEY_INFO, "admin.activate.success." . ($cr->activated ? 'true' : 'false'), $args); } } } else { if (optional_param('remove', null, PARAM_ALPHANUM) != null) { if (optional_param('registrationId', null, PARAM_ALPHANUMEXT) == null) { $this->addMessage(self::KEY_ERROR, "reg.activate.registrationId.empty", null); } else { $reg_id = required_param('registrationId', PARAM_INT); $cr = iclicker_service::get_registration_by_id($reg_id); if ($cr) { iclicker_service::remove_registration($reg_id); $args = new stdClass(); $args->cid = $cr->clicker_id; $args->rid = $reg_id; $args->user = iclicker_service::get_user_displayname($cr->owner_id); $this->addMessage(self::KEY_INFO, "admin.delete.success", $args); } } } else { if (optional_param('purge', null, PARAM_ALPHANUM) != null) { // actually do the purging $count = iclicker_service::purge_registrations($search, $startDate, $endDate); $this->addMessage(self::KEY_INFO, "admin.purge.success", $count); // reset all search params to defaults $search = null; $startDate = null; $endDate = null; $pageNum = 1; $perPageNum = 20; $sort = 'clicker_id'; } else { // invalid POST error_log('WARN: Invalid POST: does not contain remove, purge, or activate, nothing to do'); } } } } // put search and sort data into the page $this->results['search'] = $search; $this->results['startDate'] = $startDate; $this->results['startDateText'] = $startDateText; $this->results['endDate'] = $endDate; $this->results['endDateText'] = $endDateText; $this->results['page'] = $pageNum; $this->results['perPage'] = $perPageNum; $this->results['sort'] = $sort; // put config data into page $this->results['sso_enabled'] = iclicker_service::$block_iclicker_sso_enabled; $this->results['sso_shared_key'] = iclicker_service::$block_iclicker_sso_shared_key; $this->results['domainURL'] = iclicker_service::$domain_URL; $this->results['adminEmailAddress'] = empty($CFG->block_iclicker_notify_emails) ? '' : $CFG->block_iclicker_notify_emails; // put error data into page $this->results['recent_failures'] = iclicker_service::get_failures(); // handling the calcs for paging $first = ($pageNum - 1) * $perPageNum; $totalCount = iclicker_service::count_all_registrations($search, $startDate, $endDate); $pageCount = floor(($totalCount + $perPageNum - 1) / $perPageNum); $this->results['total_count'] = $totalCount; $this->results['page_count'] = $pageCount; $this->results['registrations'] = iclicker_service::get_all_registrations($first, $perPageNum, $sort, $search, $startDate, $endDate); $pagerHTML = ""; if ($totalCount > 0) { $timestamp = microtime(); for ($i = 0; $i < $pageCount; $i++) { $currentPage = $i + 1; $currentStart = $i * $perPageNum + 1; $currentEnd = $currentStart + $perPageNum - 1; if ($currentEnd > $totalCount) { $currentEnd = $totalCount; } $marker = '[' . $currentStart . '..' . $currentEnd . ']'; if ($currentPage == $pageNum) { // make it bold and not a link $pagerHTML .= '<span class="paging_current paging_item">' . $marker . '</span>' . "\n"; } else { // make it a link $pagingURL = $adminPath . '?page=' . $currentPage . '&sort=' . $sort; if (isset($search)) { $pagingURL .= '&search=' . urlencode($search); } if (!empty($startDateText)) { $pagingURL .= '&start_date=' . urlencode($startDateText); } if (!empty($endDateText)) { $pagingURL .= '&end_date=' . urlencode($endDateText); } $pagingURL .= '&nc=' . ($timestamp . $currentPage); $pagerHTML .= '<a class="paging_link paging_item" href="' . $pagingURL . '">' . $marker . '</a>' . "\n"; } } $this->results['pagerHTML'] = $pagerHTML; } }