It turns out that students can actually start an exam even when the exam date is still to the future. Normally, when a student clicks on the 'start exam' button when it's not time, there is usually a warning message but for some reason, it doesn't work again.
Students should be restricted from being able to start an exam when the date of the exam has not been reached
View
<button type="button" class="btn btn-info questions" data-recordid="<?php echo $exam->id; ?>" data-loading-text="<i class='fa fa-spinner fa-spin'></i> Please wait..."><i class="fa fa-bullhorn"></i> <?php echo $this->lang->line('start') . " " . $this->lang->line('exam') ?></button>
Teacher Controller
public function add() {
$this->form_validation->set_rules('exam', $this->lang->line('exam'), 'trim|required|xss_clean');
$this->form_validation->set_rules('attempt', $this->lang->line('attempt'), 'trim|required|xss_clean');
$this->form_validation->set_rules('exam_from', $this->lang->line('exam') . " " . $this->lang->line('from'), 'trim|required|xss_clean');
$this->form_validation->set_rules('exam_to', $this->lang->line('exam') . " " . $this->lang->line('to'), 'trim|required|xss_clean');
$this->form_validation->set_rules('duration', $this->lang->line('duration'), 'trim|required|xss_clean');
$this->form_validation->set_rules('description', $this->lang->line('description'), 'trim|required|xss_clean');
$this->form_validation->set_rules('passing_percentage', $this->lang->line('percentage'), 'trim|required|xss_clean');
if ($this->form_validation->run() == false) {
$msg = array(
'exam' => form_error('exam'),
'attempt' => form_error('attempt'),
'exam_from' => form_error('exam_from'),
'duration' => form_error('duration'),
'exam_to' => form_error('exam_to'),
'description' => form_error('description'),
'passing_percentage' => form_error('passing_percentage'),
);
$array = array('status' => 0, 'error' => $msg, 'message' => '');
} else {
$is_active = 0;
$publish_result = 0;
if (isset($_POST['is_active'])) {
$is_active = 1;
}
if (isset($_POST['publish_result'])) {
$publish_result = 1;
}
$idd = $this->session->userdata('admin');
$insert_data = array(
'exam' => $this->input->post('exam'),
'attempt' => $this->input->post('attempt'),
'exam_from' => date('Y-m-d', $this->customlib->datetostrtotime($this->input->post('exam_from'))),
'exam_to' => date('Y-m-d', $this->customlib->datetostrtotime($this->input->post('exam_to'))),
'duration' => $this->input->post('duration'),
'description' => $this->input->post('description'),
'session_id' => $this->setting_model->getCurrentSession(),
'is_active' => $is_active,
'publish_result' => $publish_result,
'passing_percentage' => $this->input->post('passing_percentage'),
'teacher_id' => $idd['id'],
);
$id = $this->input->post('recordid');
if ($id != 0) {
$insert_data['id'] = $id;
}
$this->onlineexam_model->add($insert_data);
$array = array('status' => 1, 'error' => '', 'message' => $this->lang->line('success_message'));
}
echo json_encode($array);
}
user controller
public function startexam____($id) {
$data = array();
$this->session->set_userdata('top_menu', 'Hostel');
$this->session->set_userdata('sub_menu', 'hostel/index');
$questionOpt = $this->customlib->getQuesOption();
$data['questionOpt'] = $questionOpt;
$onlineexam_question = $this->onlineexam_model->getExamQuestions($id);
$data['examquestion'] = $onlineexam_question;
$this->load->view('layout/student/header');
$this->load->view('user/onlineexam/startexam', $data);
$this->load->view('layout/student/footer');
}
public function getExamForm() {
$data = array();
$question_status = 0;
$recordid = $this->input->post('recordid');
$exam = $this->onlineexam_model->get($recordid);
$data['questions'] = $this->onlineexam_model->getExamQuestions($recordid);
$student_current_class = $this->customlib->getStudentCurrentClsSection();
$student_session_id = $student_current_class->student_session_id;
$onlineexam_student = $this->onlineexam_model->examstudentsID($student_session_id, $exam->id);
$data['onlineexam_student_id'] = $onlineexam_student;
$getStudentAttemts = $this->onlineexam_model->getStudentAttemts($onlineexam_student->id);
$data['question_status'] = 0;
if (strtotime(date('Y-m-d H:i:s')) >= strtotime(date($exam->exam_to . ' 23:59:59'))) {
$question_status = 1;
$data['question_status'] = 1;
} else if ($exam->attempt > $getStudentAttemts) {
$this->onlineexam_model->addStudentAttemts(array('onlineexam_student_id' => $onlineexam_student->id));
} else {
$question_status = 1;
$data['question_status'] = 1;
}
$questionOpt = $this->customlib->getQuesOption();
$data['questionOpt'] = $questionOpt;
$pag_content = $this->load->view('user/onlineexam/_searchQuestionByExamID', $data, true);
echo json_encode(array('status' => 0, 'exam' => $exam, 'page' => $pag_content, 'question_status' => $question_status));
}