/** * Uses PHPExcel library to generate excel file of submissions */ private function _download_excel($view) { if (!in_array($view, array('all', 'final'))) { exit; } $now = shj_now_str(); // current time // Load PHPExcel library $this->load->library('phpexcel'); // Set document properties $this->phpexcel->getProperties()->setCreator('Sharif Judge')->setLastModifiedBy('Sharif Judge')->setTitle('Sharif Judge Users')->setSubject('Sharif Judge Users')->setDescription('List of Sharif Judge users (' . $now . ')'); // Name of the file sent to browser $output_filename = 'judge_' . $view . '_submissions'; // Set active sheet $this->phpexcel->setActiveSheetIndex(0); $sheet = $this->phpexcel->getActiveSheet(); // Add current time, username filter, and problem filter to document $sheet->fromArray(array('Time:', $now), null, 'A2', true); $sheet->fromArray(array('Username Filter:', $this->filter_user ? $this->filter_user : '******'), null, 'A3', true); $sheet->fromArray(array('Problem Filter:', $this->filter_problem ? $this->filter_problem : 'No filter'), null, 'A4', true); // Prepare header if ($this->user->level === 0) { $header = array('Final', 'Problem', 'Submit Time', 'Score', 'Final Score', 'Language', 'Status'); } else { $header = array('Final', 'Submit ID', 'Username', 'Name', 'Problem', 'Submit Time', 'Score', 'Final Score', 'Language', 'Status'); if ($view === 'final') { array_unshift($header, "#2"); array_unshift($header, "#1"); } } // Add header to document $sheet->fromArray($header, null, 'A6', true); $highest_column = $sheet->getHighestColumn(); // Set custom style for header $sheet->getStyle('A6:' . $highest_column . '6')->applyFromArray(array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => '173C45')), 'font' => array('bold' => true, 'color' => array('rgb' => 'FFFFFF')))); // Prepare data (in $rows array) if ($view === 'final') { $items = $this->submit_model->get_final_submissions($this->user->level, $this->user->username, NULL, $this->filter_user, $this->filter_problem); } else { $items = $this->submit_model->get_all_submissions($this->user->level, $this->user->username, NULL, $this->filter_user, $this->filter_problem); } $names = $this->user_model->get_names(); $i = 0; $j = 0; $un = ''; $rows = array(); foreach ($items as $item) { $i++; if ($item['username'] != $un) { $j++; } $un = $item['username']; $pi = $this->problems[$item['problem']]; $pre_score = ceil($item['pre_score'] * $pi['score'] / 10000); $checked = ''; if ($item['is_final']) { $checked = '*'; } $final_score = ceil($pre_score); if ($this->user->level === 0) { $row = array($checked, $item['problem'] . ' (' . $pi['name'] . ')', $item['time'], $pre_score, $final_score, filetype_to_language($item['file_type']), $item['status']); } else { $row = array($checked, $item['submit_id'], $item['username'], $names[$item['username']], $item['problem'] . ' (' . $pi['name'] . ')', $item['time'], $pre_score, $final_score, filetype_to_language($item['file_type']), $item['status']); if ($view === 'final') { array_unshift($row, $j); array_unshift($row, $i); } } array_push($rows, $row); } // Add rows to document $sheet->fromArray($rows, null, 'A7', true); // Add alternative colors to rows for ($i = 7; $i < count($rows) + 7; $i++) { $sheet->getStyle('A' . $i . ':' . $highest_column . $i)->applyFromArray(array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => $i % 2 ? 'F0F0F0' : 'FAFAFA')))); } // Set text align to center $sheet->getStyle($sheet->calculateWorksheetDimension())->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); // Making columns autosize for ($i = 2; $i < count($header); $i++) { $sheet->getColumnDimension(chr(65 + $i))->setAutoSize(true); } // Set Border $sheet->getStyle('A7:' . $highest_column . $sheet->getHighestRow())->applyFromArray(array('borders' => array('outline' => array('style' => PHPExcel_Style_Border::BORDER_THIN, 'color' => array('rgb' => '444444'))))); // Send the file to browser $ext = 'xlsx'; if (!class_exists('ZipArchive')) { // If class ZipArchive does not exist, export to excel5 instead of excel 2007 $ext = 'xls'; } header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="' . $output_filename . '.' . $ext . '"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($this->phpexcel, $ext === 'xlsx' ? 'Excel2007' : 'Excel5'); $objWriter->save('php://output'); }
/** * Saves submitted code and adds it to queue for judging */ private function _upload() { $now = shj_now(); foreach ($this->problems as $item) { if ($item['id'] == $this->input->post('problem')) { $this->problem = $item; break; } } $this->filetype = $this->_language_to_type(strtolower(trim($this->input->post('language')))); $this->ext = substr(strrchr($_FILES['userfile']['name'], '.'), 1); // uploaded file extension $this->file_name = basename($_FILES['userfile']['name'], ".{$this->ext}"); // uploaded file name without extension if ($this->queue_model->in_queue($this->user->username, $this->problem['id'])) { show_error('You have already submitted for this problem. Your last submission is still in queue.'); } if ($this->user->level == 0 && !$this->problem['open']) { show_error('Selected problem has been closed.'); } $filetypes = explode(",", $this->problem['allowed_languages']); foreach ($filetypes as &$filetype) { $filetype = $this->_language_to_type(strtolower(trim($filetype))); } if ($_FILES['userfile']['error'] == 4) { show_error('No file chosen.'); } if (!in_array($this->filetype, $filetypes)) { show_error('This file type is not allowed for this problem.'); } if (!$this->_match($this->filetype, $this->ext)) { show_error('This file type does not match your selected language.'); } if (!preg_match('/^[a-zA-Z0-9_\\-()]+$/', $this->file_name)) { show_error('Invalid characters in file name.'); } $user_dir = rtrim($this->problem_root, '/') . '/p' . $this->problem['id'] . '/user/'; if (!file_exists($user_dir)) { mkdir($user_dir, 0700); } $user_dir = rtrim($this->problem_root, '/') . '/p' . $this->problem['id'] . '/user/' . $this->user->username; if (!file_exists($user_dir)) { mkdir($user_dir, 0700); } $config['upload_path'] = $user_dir; $config['allowed_types'] = '*'; $config['max_size'] = $this->settings_model->get_setting('file_size_limit'); $config['file_name'] = $this->file_name . "-" . ($this->problem['total_submits'] + 1) . "." . $this->ext; $config['max_file_name'] = 20; $config['remove_spaces'] = TRUE; $this->upload->initialize($config); if ($this->upload->do_upload('userfile')) { $result = $this->upload->data(); $this->load->model('submit_model'); $submit_info = array('submit_id' => $this->problem_model->increase_total_submits($this->problem['id']), 'username' => $this->user->username, 'problem' => $this->problem['id'], 'file_name' => $result['raw_name'], 'main_file_name' => $this->file_name, 'file_type' => $this->filetype, 'pre_score' => 0, 'time' => shj_now_str()); $this->probleminfo_model->update_last_submissions($this->problem['id'], $submit_info['time']); $this->probleminfo_model->update_total_submissions($problem_id, 1); if ($this->problem['is_upload_only'] == 0) { $this->queue_model->add_to_queue($submit_info); process_the_queue(); } else { $this->submit_model->add_upload_only($submit_info); } return TRUE; } return FALSE; }
/** * Prints server time, used for server time synchronization */ public function index() { echo shj_now_str(); }
/** * Saves submitted code and adds it to queue for judging */ private function _upload() { $now = shj_now(); foreach ($this->problems as $item) { if ($item['id'] == $this->input->post('problem')) { $this->problem = $item; break; } } $this->filetype = $this->_language_to_type(strtolower(trim($this->input->post('language')))); $submitcode = $this->input->post("submitcode"); $this->ext = substr(strrchr($_FILES['userfile']['name'], '.'), 1); // uploaded file extension $this->file_name = basename($_FILES['userfile']['name'], ".{$this->ext}"); // uploaded file name without extension if ($this->queue_model->in_queue($this->user->username, $this->user->selected_assignment['id'], $this->problem['id'])) { show_error('You have already submitted for this problem. Your last submission is still in queue.'); } if ($this->user->level == 0 && !$this->user->selected_assignment['open']) { show_error('Selected assignment has been closed.'); } if ($now < strtotime($this->user->selected_assignment['start_time'])) { show_error('Selected assignment has not started.'); } if ($now > strtotime($this->user->selected_assignment['finish_time']) + $this->user->selected_assignment['extra_time']) { show_error('Selected assignment has finished.'); } if (!$this->assignment_model->is_participant($this->user->selected_assignment['participants'], $this->user->username)) { show_error('You are not registered for submitting.'); } $filetypes = explode(",", $this->problem['allowed_languages']); foreach ($filetypes as &$filetype) { $filetype = $this->_language_to_type(strtolower(trim($filetype))); } $user_dir = rtrim($this->assignment_root, '/') . '/assignment_' . $this->user->selected_assignment['id'] . '/p' . $this->problem['id'] . '/' . $this->user->username; if (!file_exists($user_dir)) { mkdir($user_dir, 0700); } $done = false; if ($submitcode == "" || $this->filetype == 'zip' || $this->filetype == 'pdf') { if ($_FILES['userfile']['error'] == 4) { show_error('No file chosen.'); } if (!in_array($this->filetype, $filetypes)) { show_error('This file type is not allowed for this problem.'); } if (!$this->_match($this->filetype, $this->ext)) { show_error('This file type does not match your selected language.'); } if (!preg_match('/^[a-zA-Z0-9_\\-()]+$/', $this->file_name)) { show_error('Invalid characters in file name.'); } $config['upload_path'] = $user_dir; $config['allowed_types'] = '*'; $config['max_size'] = $this->settings_model->get_setting('file_size_limit'); $config['file_name'] = $this->file_name . "-" . ($this->user->selected_assignment['total_submits'] + 1) . "." . $this->ext; $config['max_file_name'] = 20; $config['remove_spaces'] = TRUE; $this->upload->initialize($config); if ($this->upload->do_upload('userfile')) { $result = $this->upload->data(); $done = true; } } else { $this->load->helper('file'); $this->ext = $this->_retext($this->filetype); $this->file_name = "code"; $file_name = "code-" . ($this->user->selected_assignment['total_submits'] + 1) . "." . $this->ext; $upload_path = $user_dir; $max_size = $this->settings_model->get_setting('file_size_limit'); $fp = fopen($upload_path . "/" . $file_name, 'w'); //show_error($max_size); $wr = fwrite($fp, $submitcode, $max_size * 1024); if (!$wr) { show_error("Code is too large"); } fclose($fp); $result['raw_name'] = "code-" . ($this->user->selected_assignment['total_submits'] + 1); $done = true; } if ($done) { $this->load->model('submit_model'); $submit_info = array('submit_id' => $this->assignment_model->increase_total_submits($this->user->selected_assignment['id']), 'username' => $this->user->username, 'assignment' => $this->user->selected_assignment['id'], 'problem' => $this->problem['id'], 'file_name' => $result['raw_name'], 'main_file_name' => $this->file_name, 'file_type' => $this->filetype, 'coefficient' => $this->coefficient, 'pre_score' => 0, 'time' => shj_now_str()); if ($this->problem['is_upload_only'] == 0) { $this->queue_model->add_to_queue($submit_info); process_the_queue(); } else { $this->submit_model->add_upload_only($submit_info); } $val = $submit_info; return $val; } return FALSE; }
/** * Add a new notification */ public function add_notification($title, $text) { $now = shj_now_str(); $this->db->insert('notifications', array('title' => $title, 'text' => $text, 'time' => $now)); }
public function submitcmnt() { if (!$this->input->is_ajax_request()) { show_404(); } else { $values['title'] = $this->input->post('title'); $values['message'] = $this->input->post('text'); $values['assignment'] = $this->user->selected_assignment['id']; $values['problem'] = $this->input->post('problem'); $values['source'] = $this->user_model->username_to_user_id($this->user->username); if ($this->user->level == 0) { $values['private'] = 1; } else { $values['private'] = 0; } $values['time'] = shj_now_str(); $values['reply'] = 0; $this->query_model->add_query($values); $json_result = array('done' => 1); } $this->output->set_header('Content-Type: application/json; charset=utf-8'); echo json_encode($json_result); }
/** * Uses PHPExcel library to generate excel file of users list */ public function list_excel() { $now = shj_now_str(); // current time // Load PHPExcel library $this->load->library('phpexcel'); // Set document properties $this->phpexcel->getProperties()->setCreator('Sharif Judge')->setLastModifiedBy('Sharif Judge')->setTitle('Sharif Judge Users')->setSubject('Sharif Judge Users')->setDescription('List of Sharif Judge users (' . $now . ')'); // Name of the file sent to browser $output_filename = 'sharifjudge_users'; // Set active sheet $this->phpexcel->setActiveSheetIndex(0); $sheet = $this->phpexcel->getActiveSheet(); // Add current time to document $sheet->fromArray(array('Time:', $now), null, 'A1', true); // Add header to document $header = array('#', 'User ID', 'Username', 'Display Name', 'Email', 'Role', 'First Login', 'Last Login'); $sheet->fromArray($header, null, 'A3', true); $highest_column = $sheet->getHighestColumn(); // Set custom style for header $sheet->getStyle('A3:' . $highest_column . '3')->applyFromArray(array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => '173C45')), 'font' => array('bold' => true, 'color' => array('rgb' => 'FFFFFF')))); // Prepare user data (in $rows array) $users = $this->user_model->get_all_users(); $i = 0; $rows = array(); foreach ($users as $user) { array_push($rows, array(++$i, $user['id'], $user['username'], $user['display_name'], $user['email'], $user['role'], $user['first_login_time'] === NULL ? 'Never' : $user['first_login_time'], $user['last_login_time'] === NULL ? 'Never' : $user['last_login_time'])); } // Add rows to document and set a background color of #7BD1BE $sheet->fromArray($rows, null, 'A4', true); // Add alternative colors to rows for ($i = 4; $i < count($rows) + 4; $i++) { $sheet->getStyle('A' . $i . ':' . $highest_column . $i)->applyFromArray(array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => $i % 2 ? 'F0F0F0' : 'FAFAFA')))); } // Set text align to center $sheet->getStyle($sheet->calculateWorksheetDimension())->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); // Making columns autosize for ($i = 2; $i < count($header); $i++) { $sheet->getColumnDimension(chr(65 + $i))->setAutoSize(true); } // Set Border $sheet->getStyle('A4:' . $highest_column . $sheet->getHighestRow())->applyFromArray(array('borders' => array('outline' => array('style' => PHPExcel_Style_Border::BORDER_THIN, 'color' => array('rgb' => '444444'))))); // Send the file to browser // If class ZipArchive exists, export to excel2007, otherwise export to excel5 if (class_exists('ZipArchive')) { $ext = 'xlsx'; } else { $ext = 'xls'; } header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="' . $output_filename . '.' . $ext . '"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($this->phpexcel, $ext === 'xlsx' ? 'Excel2007' : 'Excel5'); $objWriter->save('php://output'); }
/** * Set Moss Time * * Updates "Moss Update Time" for given assignment * * @param $assignment_id */ public function set_moss_time($problem_id) { $now = shj_now_str(); $this->db->where('id', $problem_id)->update('assignments', array('moss_update' => $now)); }
/** * Uses PHPExcel library to generate excel file of submissions */ public function download_excel() { if ($this->user->level <= 2) { show_404(); } $now = shj_now_str(); // current time // Load PHPExcel library $this->load->library('phpexcel'); // Set document properties $this->phpexcel->getProperties()->setCreator('Sharif Judge')->setLastModifiedBy('Sharif Judge')->setTitle('Sharif Judge Users')->setSubject('Sharif Judge Users')->setDescription('List of Sharif Judge users (' . $now . ')'); // Name of the file sent to browser $output_filename = 'judge_scoreboard'; // Set active sheet $this->phpexcel->setActiveSheetIndex(0); $sheet = $this->phpexcel->getActiveSheet(); // Prepare header $header = array('#', 'Username', 'Display name', 'Total score', 'Total Submissions', 'Last Submissions', 'Problem name', 'Score', 'Last submissions', 'Count'); // Add header to document $sheet->fromArray($header, null, 'A1', true); $highest_column = $sheet->getHighestColumn(); // Set custom style for header $sheet->getStyle('A1:' . $highest_column . '1')->applyFromArray(array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => '173C45')), 'font' => array('bold' => true, 'color' => array('rgb' => 'FFFFFF')))); $items = $this->scoreboard_model->download_excel(); $scoreboard = $items['scoreboard']; $all_problems = $items['all_problems']; $problems = $items['problems']; $counts = $items['count']; $names = $items['names']; $i = 2; $num = 0; $rows = array(); foreach ($scoreboard['username'] as $username) { $row = array(++$num, $username, $items[$username], $scoreboard['total_score'][$num - 1], $scoreboard['total_submissions'][$num - 1], $scoreboard['last_submissions'][$num - 1]); $end = $i + $counts - 1; $sheet->mergeCells("A{$i}:A{$end}"); $sheet->mergeCells("B{$i}:B{$end}"); $sheet->mergeCells("C{$i}:C{$end}"); $sheet->mergeCells("D{$i}:D{$end}"); $sheet->mergeCells("E{$i}:E{$end}"); $sheet->mergeCells("F{$i}:F{$end}"); // Add rows to document $sheet->fromArray($row, null, 'A' . $i, true); $j = 0; foreach ($all_problems as $problem) { $end = $i + $j; $sheet->setCellValue('G' . $end, $problem['name']); if (isset($problems[$username][$problem['id']]['score'])) { $row = array($problems[$username][$problem['id']]['score'], $problems[$username][$problem['id']]['last_submissions'], $problems[$username][$problem['id']]['submis_count']); $sheet->fromArray($row, null, 'H' . $end, true); $sheet->getStyle('H' . $end . ':J' . $end)->applyFromArray(array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => $num % 2 ? '20C446' : 'E1E647')))); } else { $concat = "H{$end}:J{$end}"; $sheet->mergeCells($concat); $sheet->setCellValue('H' . $end, 'No Submissions'); $sheet->getStyle('H' . $end . ':H' . $end)->applyFromArray(array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => 'FD6864')), 'font' => array('bold' => true, 'color' => array('rgb' => 'FFFFFF')))); } $j++; } $i += $counts; } // Set text align to center $sheet->getStyle($sheet->calculateWorksheetDimension())->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER)->setVERTICAL(PHPExcel_Style_Alignment::VERTICAL_CENTER); // Making columns autosize for ($i = 2; $i < count($header); $i++) { $sheet->getColumnDimension(chr(65 + $i))->setAutoSize(true); } // Set Border $sheet->getStyle('A2:' . $highest_column . $sheet->getHighestRow())->applyFromArray(array('borders' => array('outline' => array('style' => PHPExcel_Style_Border::BORDER_THIN, 'color' => array('rgb' => '444444'))))); // Send the file to browser $ext = 'xlsx'; if (!class_exists('ZipArchive')) { // If class ZipArchive does not exist, export to excel5 instead of excel 2007 $ext = 'xls'; } header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="' . $output_filename . '.' . $ext . '"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($this->phpexcel, $ext === 'xlsx' ? 'Excel2007' : 'Excel5'); $objWriter->save('php://output'); }
/** * Update Login Time * * Updates First Login Time and Last Login Time for given username * */ public function update_login_time($username) { $now = shj_now_str(); $first_login = $this->db->select('first_login_time')->get_where('users', array('username' => $username))->row()->first_login_time; if ($first_login === NULL) { $this->db->where('username', $username)->update('users', array('first_login_time' => $now)); } $this->db->where('username', $username)->update('users', array('last_login_time' => $now)); }