//                                                                       //
///////////////////////////////////////////////////////////////////////////
/**
 * Displays details of one task
 *
 * @package   local_onlinejudge
 * @copyright 2011 Sun Zhigang (http://sunner.cn)
 * @author    Sun Zhigang
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require_once dirname(__FILE__) . '/../../config.php';
require_once $CFG->dirroot . '/local/onlinejudge/judgelib.php';
require_login(SITEID, false);
$taskid = required_param('task', PARAM_INT);
$ajax = optional_param('ajax', 0, PARAM_BOOL);
$task = onlinejudge_get_task($taskid);
if (empty($task)) {
    print_error('invalidtaskid', 'local_onlinejudge', '', $taskid);
}
$context = get_context_instance(CONTEXT_MODULE, $task->cmid);
$PAGE->set_url('/mod/assignment/type/onlinejudge/details.php');
$PAGE->set_pagelayout('popup');
$PAGE->set_context($context);
$PAGE->set_title(get_string('details', 'local_onlinejudge'));
$PAGE->set_heading(get_string('details', 'local_onlinejudge'));
$PAGE->set_course($COURSE);
$PAGE->navbar->add(get_string('details', 'local_onlinejudge'));
if ($ajax) {
    @header('Content-Type: text/plain; charset=utf-8');
} else {
    echo $OUTPUT->header();
 /**
  * return all results of the submission
  *
  * it will update the grade if necessary
  * @param object submission
  * @return object
  */
 function get_onlinejudge_result($submission)
 {
     global $DB;
     if (empty($submission)) {
         return null;
     }
     $sql = 'SELECT s.*, t.feedback, t.subgrade
             FROM {assignment_oj_submissions} s LEFT JOIN {assignment_oj_testcases} t
             ON s.testcase = t.id
             WHERE s.submission = ? AND s.latest = 1
             ORDER BY t.sortorder ASC';
     $onlinejudges = $DB->get_records_sql($sql, array($submission->id));
     $cases = array();
     $result->judgetime = 0;
     foreach ($onlinejudges as $oj) {
         if ($task = onlinejudge_get_task($oj->task)) {
             $task->testcase = $oj->testcase;
             $task->feedback = $oj->feedback;
             $task->grade = $this->grade_marker($task->status, $oj->subgrade);
             if ($task->judgetime > $result->judgetime) {
                 $result->judgetime = $task->judgetime;
             }
             $cases[] = $task;
         } else {
             $cases[] = null;
         }
     }
     $result->testcases = $cases;
     $result->status = onlinejudge_get_overall_status($cases);
     return $result;
 }