<?php

defined('BASEPATH') or exit('No direct script access allowed');

/*
 * This file is mainly for Mobile API Calls, so that it must extend MY_Controller => REST_Controller
 */
class Exercises extends MY_Controller {

    /**
     * Get All Data from this method.
     *
     * @return Response
     */
    public function __construct() {
        parent::__construct();
//        $this->load->database();
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
        $this->load->model('Exercise_model');
    }

    public function datatables_get() {
        $data = $this->Exercise_model->get(null, $tenant);
        $extra = array('draw' => 1, 'recordsFiltered' => count($data), 'recordsTotal' => count($data));
        $ret = array_merge($extra, array('data' => recast($data)));
        $this->response($ret, MY_Controller::HTTP_OK);
    }

    public function recordcount_get() {
        $data = $this->Exercise_model->get();
        $this->response(array('data' => count($data)), MY_Controller::HTTP_OK);
    }

    public function detail_get() {
        $exId = $this->input->get('exId');
        $data = $this->Exercise_model->detail($exId);
        $ret = array('data' => $data, 'success' => true);
        $this->response($ret, MY_Controller::HTTP_OK);
    }

    public function filter_get() {
        $unitname = $this->input->get('unitname');
        $coursename = $this->input->get('coursename');
        $employeename = $this->input->get('employeename');
        $data = $this->Exercise_model->filter($employeename, $unitname, $coursename);
        $exercises = array();
        foreach ($data as $k => $v) {
            $exercises[] = array(
                'id' => $v['id'],
                'status' => $v['status'],
                'title' => trim($v['title']),
                'exercise_date' => trim($v['exercise_date']),
                'unit_name' => trim($v['unit_name']),
                'course_name' => trim($v['course_name']),
                'chi_name' => trim($v['chi_name']),
            );
        }
        $ret = array('data' => array('exercise' => $exercises), 'success' => true);
        $this->response($ret, MY_Controller::HTTP_OK);
    }

    public function add_post() {
        $data = array(
            'course_id' => $this->input->post('course_id'),
            'title' => $this->input->post('title'),
            'date' => $this->input->post('date'),
            'tenant_no' => $this->input->post('tenant'),
            'questions' => explode(",", $this->input->post('questions')),
            'teacher' => $this->input->post('teacher'),
        );
        $exercisecode = $this->Exercise_model->add($data);
        $ret = array('data' => array('exercise' => $exercisecode), 'success' => true);
        $this->response($ret, MY_Controller::HTTP_OK);
    }

    public function marked_post() {
        $params = $this->input->post();
        
        $result = $this->Exercise_model->get_exercise_dates($params);
//         log_message('debug','result empty?'.empty($result));
        $ret = array('data' => array('marked' => $result), 'success' => true);
        $this->response($ret, MY_Controller::HTTP_OK);
    }

    public function daily_post() {
        $params = $this->input->post();

        $data = $this->Exercise_model->get_exercise_list($params);
        $ret = array('data' => array('exercise' => $data), 'success' => true);
        $this->response($ret, MY_Controller::HTTP_OK);
    }

    public function questions_post() {
        $data = array(
            'exercise_id' => $this->input->post('exercise_id'),
        );
        $data = $this->Exercise_model->get_question_list($data);
        $ret = array('data' => array('questions' => $data), 'success' => true);
        $this->response($ret, MY_Controller::HTTP_OK);
    }

    public function start_post() {
        $data = array(
            'exercise_id' => $this->input->post('exercise_id'),
            'student_id' => $this->input->post('student_id'),
            'questions' => json_decode($this->input->post('questions')),
        );
        $data = $this->Exercise_model->start_exercise($data);
        $ret = array('data' => array('progress' => $data), 'success' => true);
        $this->response($ret, MY_Controller::HTTP_OK);
    }

    public function answer_post() {
        $params = $this->input->post();

        $progressId = $params['progress_detail_id'];
        $yourAnswer = $params['your_answer'];
        $data = $this->Exercise_model->answer_exercise($progressId, $yourAnswer);
        $ret = array('data' => array('solution' => $data), 'success' => true);
        $this->response($ret, MY_Controller::HTTP_OK);
    }

    public function completed_post() {
        $data = array(
            'progress_id' => $this->input->post('progress_id'),
        );
        $data = $this->Exercise_model->complete_exercise($data);
        $ret = array('data' => array('solution' => $data), 'success' => true);
        $this->response($ret, MY_Controller::HTTP_OK);
    }

    public function history_post() {
        $data = array(
            'student_id' => $this->input->post('student_id'),
            'tenantno'=>$this->input->post('tenantno')
        );
        $data = $this->Exercise_model->exercise_history($data);
        $ret = array('data' => array('history' => $data), 'success' => true);
        $this->response($ret, MY_Controller::HTTP_OK);
    }

    public function historydetails_post() {
        $data = array(
            'progress_id' => $this->input->post('progress_id'),
            'tenantno'=>$this->input->post('tenantno')
        );
        $data = $this->Exercise_model->exercise_history_details($data);
        $ret = array('data' => array('historydetails' => $data), 'success' => true);
        $this->response($ret, MY_Controller::HTTP_OK);
    }

    public function scoreboard_post() {
        $data = array(
            'student_id' => $this->input->post('student_id'),
            'tenantno'=>$this->input->post('tenantno')
        );
        $data = $this->Exercise_model->scoreboard($data);
        $ret = array('data' => array('scoreboard' => $data), 'success' => true);
        $this->response($ret, MY_Controller::HTTP_OK);
    }

}
