Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- defined('BASEPATH') or exit('No direct script access allowed');
- require_once APPPATH . 'presenters/App_presenter.php';
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
- class Quizz_manager extends ADMIN_Controller
- {
- public $models = array('cycle', 'question', 'group', 'referencial', 'quizz');
- public function __construct()
- {
- parent::__construct();
- $this->table = 'cycle';
- $this->data['cycle_id'] = $this->uri->segment(4);
- $this->data['cycle'] = $this->cycle->get($this->data['cycle_id']);
- if (isset($this->data['current_business_area']->id)) {
- $this->data['cycles'] = $this->cycle->get_cycles($this->data['current_business_area']->id);
- }
- $this->breadcrumb->add_crumb('Dashboard', site_url('dashboard/index'));
- $this->breadcrumb->add_crumb($this->data['current_business_area']->name_show, site_url($this->data['business_area'] . '/index'));
- $this->breadcrumb->add_crumb('Gestão de Questionários', site_url($this->data['business_area'] . '/quizz_manager/index'));
- $this->acl = $this->config->item($this->router->class);
- }
- /**
- * Get all the quizzes and shows them to the user
- */
- public function index()
- {
- if (!$this->acl[$this->data['user_group']->id]['index']) {
- $this->session->set_flashdata('error', 'Não tem permissões para efetuar esta operação!');
- redirect($this->data['business_area']);
- }
- $this->get();
- }
- /**
- * View that returns the form that reflects the cycle
- * @param Int $cycle_id
- * @return View
- */
- public function view($cycle_id = null)
- {
- if (!$this->acl[$this->data['user_group']->id]['view']) {
- $this->session->set_flashdata('error', 'Não tem permissões para efetuar esta operação!');
- redirect($this->data['business_area']);
- }
- if ($_POST) {
- $this->question->update_order($_POST);
- redirect($this->data['business_area'] . '/quizz_manager/view/' . $cycle_id);
- }
- $this->get();
- $this->data['parents'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id' => '0', 'cycle_id' => $cycle_id));
- $this->data['childs'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id !=' => '0', 'cycle_id' => $cycle_id));
- $this->data['questions'] = $this->question->by_filters(array('cycle_id' => $cycle_id));
- $this->breadcrumb->add_crumb($this->data['cycle']->title, '#');
- }
- /**
- * Create copy cycle
- * @param Int $cycle_id calls the model method that clone cycle
- */
- public function cycle_clone($cycle_id)
- {
- if (!$this->acl[$this->data['user_group']->id]['cycle_clone']) {
- $this->session->set_flashdata('error', 'Não tem permissões para efetuar esta operação!');
- redirect($this->data['business_area']);
- }
- if ($this->group->cycle_clone($cycle_id)) {
- $this->session->set_flashdata('success', 'O questionário foi copiado com sucesso');
- } else {
- $this->session->set_flashdata('error', 'Não foi possível copiar o questionário');
- }
- redirect($this->data['business_area'] . '/quizz_manager/index/?' . $_SERVER['QUERY_STRING']);
- }
- /**
- * get cycles to specif business area with ajax
- */
- public function ajax_cycles()
- {
- if (!$this->acl[$this->data['user_group']->id]['ajax_cycles']) {
- $this->session->set_flashdata('error', 'Não tem permissões para efetuar esta operação!');
- redirect($this->data['business_area']);
- }
- $this->view = false;
- $this->output->enable_profiler(false);
- if ($this->input->is_ajax_request()) {
- if (isset($_POST['business_area'])) {
- // get cycles with logged_user permissions
- $this->data['user_cycles'] = array_column($this->cycle->user_cycles($this->data['logged_user']->id), 'qz_cycle_id');
- if ($_POST['business_area'] === '6') {
- if (!empty($this->data['user_cycles'])) {
- $this->data['cycles'] = $this->cycle->avaiable_cycles($this->input->post('business_area'), $this->data['user_cycles']);
- } else {
- $this->data['cycles'] = array();
- }
- } else {
- $this->data['cycles'] = $this->cycle->avaiable_cycles($this->input->post('business_area'));
- }
- if (!empty($this->data['cycles'])) {
- $result = '<option></option>';
- foreach ($this->data['cycles'] as $cycle) {
- $result .= '<option value="' . $cycle->id . '">' . $cycle->title . ' </option>';
- }
- } else {
- $result = '';
- }
- }
- echo $result;
- } else {
- show_404();
- }
- }
- /**
- * View the quizzes specifc cycle_id
- * @param Int $cycle_id
- * @return View
- */
- public function quizzes($cycle_id = null)
- {
- if (is_null($cycle_id)) {
- show_404();
- }
- $this->load->model('quizz_model', 'quizz_model');
- $this->load->helper('calc_helper');
- $this->data['cycle_id'] = $cycle_id;
- $this->data['quizzes'] = $this->quizz_model->get_with_info($cycle_id);
- $this->breadcrumb->add_crumb($this->data['cycle']->title, '#');
- }
- /**
- * View the quizz detail with anwswer
- * @param Int $cycle_id
- * @param Int $quizz_id
- * @return View
- */
- public function detail($cycle_id = null, $quizz_id = null)
- {
- if (is_null($cycle_id) || is_null($quizz_id)) {
- show_404();
- }
- if ($this->data['current_business_area']->id === '3') {
- redirect($this->data['current_business_area']->name . '/quizz_manager/view_report/' . $cycle_id . '/' . $quizz_id);
- }
- $this->load->model('quizz_model', 'quizz');
- $this->load->model('station_model', 'station');
- $this->load->model('company_model', 'company');
- $this->load->helper('calc_helper');
- //header info
- $this->data['quizz'] = $this->quizz->with_stations($quizz_id);
- $this->data['quizz_station'] = $this->station->get($this->data['quizz']->station_id);
- $this->data['quizz_company'] = $this->company->get($this->data['quizz_station']->company_id);
- //quizz info
- $this->data['parents'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id' => '0', 'cycle_id' => $cycle_id));
- $this->data['childs'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id !=' => '0', 'cycle_id' => $cycle_id));
- $this->data['questions'] = $this->question->with_results($quizz_id);
- $this->breadcrumb->add_crumb($this->data['cycle']->title, '#');
- }
- /**
- * View the quizz detail with anwswer
- * @param Int $cycle_id
- * @param Int $quizz_id
- * @return View
- */
- public function view_report($cycle_id = null, $quizz_id = null)
- {
- if (is_null($cycle_id) || is_null($quizz_id)) {
- show_404();
- }
- $this->layout = false;
- $this->output->enable_profiler(false);
- $this->load->model('quizz_model', 'quizz');
- $this->load->model('station_model', 'station');
- $this->load->model('company_model', 'company');
- $this->load->helper('calc_helper');
- //header info
- $this->data['quizz'] = $this->quizz->with_stations($quizz_id);
- $this->data['quizz_station'] = $this->station->get($this->data['quizz']->station_id);
- $this->data['station_manager'] = $this->user->get($this->data['quizz_station']->manager_id);
- $this->data['station_supervisor'] = $this->user->get($this->data['quizz_station']->supervisor_id);
- $this->data['network_dir'] = $this->user->get(905);
- $this->data['quizz_manager'] = $this->user->get($this->data['quizz_station']->manager_id);
- $this->data['quizz_supervisor'] = $this->user->get($this->data['quizz_station']->supervisor_id);
- //quizz info
- $this->data['parents'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id' => '0', 'cycle_id' => $cycle_id));
- $this->data['count_parents'] = $this->group->order_by('position', 'ASC')->count_by(array('parent_id' => '0', 'cycle_id' => $cycle_id));
- $this->data['childs'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id !=' => '0', 'cycle_id' => $cycle_id));
- $this->data['questions'] = $this->question->with_results($quizz_id);
- $this->data['obs'] = $this->quizz->get_obs_group($this->data['quizz']->id);
- $this->breadcrumb->add_crumb($this->data['cycle']->title, '#');
- for ($page_parents = 0; $page_parents < $this->data['count_parents']; $page_parents++) {
- foreach ($this->data['parents'] as $key => $parent) {
- if ($key === $page_parents) {
- $count_childs = $i = 0;
- $total[$key] = array();
- foreach ($this->data['childs'] as $key_child => $child) {
- if ($child->parent_id === $parent->id) {
- $count_childs++;
- $total[$key]['count_childs'] = $count_childs;
- $this->data['count_questions'] = count($this->question->with_results($quizz_id, $child->id));
- $total[$key]['childs'][$i] = array(
- 'parent_position' => $parent->position,
- 'parent_title' => $parent->title,
- 'parent_id' => $parent->id,
- 'child_position' => $child->position,
- 'child_title' => $child->title,
- 'child_id' => $child->id,
- 'count_questions' => $this->data['count_questions'],
- 'pages' => ceil($this->data['count_questions'] / 10),
- 'images' => $this->quizz->get_images($quizz_id, $child->id),
- );
- $i++;
- }
- }
- }
- }
- }
- $this->data['total'] = $total;
- }
- /**
- * View cycle
- * @param [int] $cycle_id
- * @param [int] $user_id
- * @param [int] $station_id
- * @param [string] $token
- * @param [int] $parent
- * @param [int] $child
- * @return html
- */
- public function reply($cycle_id = null, $user_id = null, $station_id = null, $token = null, $parent = null, $child = null)
- {
- if (is_null($cycle_id) || is_null($user_id)) {
- show_404();
- }
- $this->output->enable_profiler(false);
- $this->load->model(
- array('quizz_model' => 'quizz_model', 'answer_model' => 'answer')
- );
- $this->data['app_presenter'] = new App_presenter();
- $this->layout = 'layouts/application';
- $this->data['app_title'] = 'Prio Top Service';
- $this->data['is_qsa'] = '';
- if ($this->data['current_business_area']->id === '3') {
- $this->data['is_qsa'] = 'qsa';
- }
- if (isset($station_id) && !empty($station_id) && empty($token)) {
- $data = array(
- 'quizz_type_id' => $this->data['cycle']->quizz_type_id,
- 'station_id' => $station_id,
- 'auditor_id' => $user_id,
- 'email_at' => date('Y-m-d'),
- 'sent' => '1',
- 'start_time' => date('H:i:s'),
- 'cycle_id' => $this->data['cycle']->id,
- );
- $new_quizz = $this->quizz_model->skip_validation()->insert($data);
- $quizz = $this->quizz_model->get($new_quizz);
- redirect(current_url() . '/' . $quizz->token);
- }
- // Quizz base data
- $this->data['current_auditor'] = $this->user->get($user_id);
- $this->data['current_station'] = (!is_null($station_id)) ? $this->station->get($station_id) : array();
- $this->data['cycles'] = $this->cycle->avaiable_cycles($this->data['current_business_area']->id);
- $this->data['stations'] = $this->station->order_by(array('tipo' => 'DESC', 'cdg' => 'ASC', 'name' => 'ASC'))->get_many_by('supervisor_id !=', null);
- $this->data['token'] = $token;
- $this->data['first'] = (is_null($parent)) ? true : false;
- if (!$this->data['first']) {
- $this->data['previous_link'] = $this->group->previous_parent_and_child($parent, $child, $cycle_id);
- if (!$this->data['previous_link']) {
- $this->data['first'] = true;
- }
- }
- // Quizz structure
- if (isset($token) && !empty($token)) {
- $this->data['quizz'] = $this->quizz_model->get_by('token', $token);
- if (empty($parent)) {
- $parent_id = $this->quizz_model->last_answer($this->data['quizz']->id, $this->data['quizz']->cycle_id);
- $this->data['parent'] = $this->group->order_by('position', 'ASC')->get(
- array('parent_id' => '0', 'cycle_id' => $cycle_id, 'id' => $parent_id)
- );
- } else {
- $this->data['parent'] = $this->group->order_by('position', 'ASC')->get(
- array('parent_id' => '0', 'cycle_id' => $cycle_id, 'id' => $parent)
- );
- }
- if (empty($child)) {
- $this->data['child'] = $this->group->order_by('position', 'ASC')->get_by(
- array('parent_id' => $parent_id, 'cycle_id' => $cycle_id)
- );
- } else {
- $this->data['child'] = $this->group->get($child);
- }
- $this->data['obs'] = $this->quizz->get_obs_group($this->data['quizz']->id, $this->data['child']->id);
- $this->data['questions'] = $this->question->order_by(array('order' => 'ASC', 'id' => 'ASC'))->get_many_by(array('question_group_id' => $this->data['child']->id));
- $this->data['answers'] = $this->answer->for_questions($this->data['questions'], $this->data['quizz']->id);
- }
- $this->data['images_child'] = $this->quizz->get_images($this->data['quizz']->id, $child);
- $this->data['images'] = $this->quizz->get_images($this->data['quizz']->id, $child, true);
- $this->data['all_parents'] = $this->group->order_by('position', 'ASC')->get_many_by(
- array('parent_id' => '0', 'cycle_id' => $cycle_id)
- );
- $this->data['all_childs'] = $this->group->with_answers(
- array(4, $cycle_id, $token, $this->data['quizz']->id, $cycle_id)
- );
- }
- /**
- * save state
- * Function that can only be access via POST to save the current answers I'm givin to the quest
- * @return redirect (either to the next step, success page or the same one in case its an error)
- */
- public function save_state()
- {
- if (!$_POST) {
- show_404();
- }
- $is_ajax = false;
- if ($this->input->is_ajax_request()) {
- $this->output->enable_profiler(false);
- $is_ajax = true;
- }
- $this->view = false;
- $this->load->model('quizz_model', 'quizz_model');
- $this->load->model('answer_model', 'answer');
- $this->load->model('pac_model', 'pac');
- $this->load->model('cycle_model', 'cycle');
- $quizz = $this->quizz_model->get_by('token', $this->input->post('token'));
- $cycle = $this->cycle->get_by('id', $quizz->cycle_id);
- if (!empty($_POST['obs_group'])) {
- $this->quizz_model->update_obs_group($quizz->id, $_POST['current_child'], $this->input->post('obs_group'));
- unset($_POST['obs_group']);
- }
- if (!empty($_POST['obs'])) {
- $this->quizz_model->update_obs($this->input->post('token'), $this->input->post('obs'));
- unset($_POST['obs']);
- }
- if (empty($_POST['answer'])) {
- if (!$is_ajax) {
- redirect($this->data['business_area'] . '/quizz_manager/reply/' . $_POST['cycle_id'] . '/' . $quizz->auditor_id . '/' . $_POST['station_id'] . '/' . $_POST['token'] . '/' . $_POST['parent_id'] . '/' . $_POST['current_child'] . '?error=1');
- } else {
- return false;
- }
- }
- $questions = array_keys($_POST['answer']);
- $this->answer->upload_images($_FILES, $quizz->id, $this->input->post('current_child')); // insert new structure
- $this->answer->delete_answers($quizz->id, $questions); // Delete current structure before insert
- $this->answer->insert_answers($quizz->id, $this->input->post('answer')); // insert new structure
- $this->quizz_model->update_status($this->input->post('token'), $this->input->post('parent_id'), $this->input->post('current_child'));
- $parent_childs = $this->group->order_by('position', 'ASC')->get_many_by(
- array('parent_id' => $_POST['parent_id'], 'cycle_id' => $this->input->post('cycle_id'))
- );
- $next_child = null;
- foreach ($parent_childs as $key => $child) {
- if ($_POST['current_child'] === $child->id) {
- $next_child = (isset($parent_childs[$key + 1])) ? $parent_childs[$key + 1] : null;
- break;
- }
- }
- $all_unanswered_childs = $this->group->with_no_answers(array(4, $this->input->post('cycle_id'), $this->input->post('token'), $quizz->id, $this->input->post('cycle_id')));
- if (empty($all_unanswered_childs)) {
- $this->quizz_model->calc_result($this->input->post('token'));
- if (!$is_ajax) {
- redirect($this->data['business_area'] . '/quizz_manager/obs/' . $_POST['token'] . '/' . $_POST['parent_id'] . '/' . $_POST['current_child']);
- } else {
- return true;
- }
- }
- if (!empty($next_child)) {
- if (!$is_ajax) {
- redirect($this->data['business_area'] . '/quizz_manager/reply/' . $_POST['cycle_id'] . '/' . $quizz->auditor_id . '/' . $_POST['station_id'] . '/' . $_POST['token'] . '/' . $_POST['parent_id'] . '/' . $next_child->id);
- } else {
- return true;
- }
- } else {
- $parents = $this->group->order_by('position', 'ASC')->get_many_by(
- array('parent_id' => '0', 'cycle_id' => $this->input->post('cycle_id'))
- );
- $next_parent = null;
- foreach ($parents as $k => $parent) {
- if ($_POST['parent_id'] === $parent->id) {
- $next_parent = (isset($parents[$k + 1])) ? $parents[$k + 1] : null;
- break;
- }
- }
- if (!empty($next_parent)) {
- $parent_first_child = $this->group->order_by('position', 'ASC')->get_by(
- array('parent_id' => $next_parent->id, 'cycle_id' => $this->input->post('cycle_id'))
- );
- $next_child = $parent_first_child->id;
- if (!empty($next_child)) {
- if (!$is_ajax) {
- redirect($this->data['business_area'] . '/quizz_manager/reply/' . $_POST['cycle_id'] . '/' . $quizz->auditor_id . '/' . $_POST['station_id'] . '/' . $_POST['token'] . '/' . $next_parent->id . '/' . $next_child);
- } else {
- return true;
- }
- }
- } else {
- // in case you're not answering in order and its the last one redirect to the same one
- if (!$is_ajax) {
- redirect($this->data['business_area'] . '/quizz_manager/reply/' . $_POST['cycle_id'] . '/' . $quizz->auditor_id . '/' . $_POST['station_id'] . '/' . $_POST['token'] . '/' . $_POST['parent_id'] . '/' . $_POST['current_child']);
- } else {
- return true;
- }
- }
- }
- }
- /**
- * Add one final comment to the quizz
- * @param string $token The quizz token
- * @param int $parent The parent (not really being used mostly to keep harmony in the urls)
- * @param int $child The child (not really being used mostly to keep harmony in the urls)
- * @return redirect on post
- */
- public function obs($token = null, $parent = null, $child = null)
- {
- $this->layout = 'layouts/application';
- $this->data['app_title'] = 'Prio Top Service';
- $this->load->model(
- array('quizz_model' => 'quizz_model', 'answer_model' => 'answer')
- );
- $this->output->enable_profiler(false);
- // Quizz structure
- if (isset($token) && !empty($token)) {
- $this->data['quizz'] = $this->quizz_model->get_by('token', $token);
- if (empty($parent)) {
- $parent_id = $this->quizz_model->last_answer($this->data['quizz']->id, $this->data['quizz']->cycle_id);
- $this->data['parent'] = $this->group->order_by('position', 'ASC')->get(
- array('parent_id' => '0', 'cycle_id' => $this->data['quizz']->cycle_id, 'id' => $parent_id)
- );
- } else {
- $this->data['parent'] = $this->group->order_by('position', 'ASC')->get(
- array('parent_id' => '0', 'cycle_id' => $this->data['quizz']->cycle_id, 'id' => $parent)
- );
- }
- if (empty($child)) {
- $this->data['child'] = $this->group->order_by('position', 'ASC')->get_by(
- array('parent_id' => $parent_id, 'cycle_id' => $this->data['quizz']->cycle_id)
- );
- } else {
- $this->data['child'] = $this->group->get($child);
- }
- $this->data['questions'] = $this->question->get_many_by(array('question_group_id' => $this->data['child']->id));
- $this->data['answers'] = $this->answer->for_questions($this->data['questions'], $this->data['quizz']->id);
- }
- $this->data['all_parents'] = $this->group->order_by('position', 'ASC')->get_many_by(
- array('parent_id' => '0', 'cycle_id' => $this->data['quizz']->cycle_id)
- );
- $this->data['all_childs'] = $this->group->with_answers(
- array(4, $this->data['quizz']->cycle_id, $token, $this->data['quizz']->id, $this->data['quizz']->cycle_id)
- );
- $this->data['quizz'] = $this->quizz_model->get_by('token', $token);
- $this->data['current_auditor'] = $this->user->get($this->data['quizz']->auditor_id);
- $this->data['current_station'] = (!is_null($this->data['quizz']->station_id)) ? $this->station->get($this->data['quizz']->station_id) : array();
- $this->data['stations'] = $this->station->order_by(array('tipo' => 'DESC', 'cdg' => 'ASC', 'name' => 'ASC'))->get_many_by('supervisor_id !=', null);
- if ($_POST) {
- if (!empty($_POST['obs'])) {
- $this->quizz_model->update_obs($token, $this->input->post('obs'));
- }
- redirect($this->data['business_area'] . '/quizz_manager/confirm/' . $token . '/' . $parent . '/' . $child);
- }
- }
- /**
- * Add options before closed quizz
- * @param string $token The quizz token
- * @param int $parent The parent (not really being used mostly to keep harmony in the urls)
- * @param int $child The child (not really being used mostly to keep harmony in the urls)
- */
- public function confirm($token = null, $parent = null, $child = null)
- {
- if (is_null($token) || is_null($parent) || is_null($child)) {
- show_404();
- }
- $this->layout = 'layouts/application';
- $this->data['app_title'] = 'Prio Top Service';
- $this->load->model(
- array('quizz_model' => 'quizz_model', 'answer_model' => 'answer')
- );
- $this->data['token'] = $token;
- $this->data['parent_id'] = $parent;
- $this->data['child_id'] = $child;
- $this->data['quizz'] = $this->quizz_model->get_by('token', $token);
- $this->data['current_auditor'] = $this->user->get($this->data['quizz']->auditor_id);
- $this->data['current_station'] = (!is_null($this->data['quizz']->station_id)) ? $this->station->get($this->data['quizz']->station_id) : array();
- $this->data['stations'] = $this->station->order_by(array('tipo' => 'DESC', 'cdg' => 'ASC', 'name' => 'ASC'))->get_many_by('supervisor_id !=', null);
- }
- /**
- * View with quizz report detail
- * @param int $cycle_id The cycle id
- * @param int $quizz_id The quizz id
- * @param string $token The quizz token
- */
- public function view_detail($cycle_id = null, $quizz_id = null, $token = null)
- {
- if (is_null($token) || is_null($cycle_id) || is_null($quizz_id)) {
- show_404();
- }
- $this->layout = 'layouts/application';
- $this->data['app_title'] = 'Prio Top Service';
- $this->load->model(
- array('quizz_model' => 'quizz', 'answer_model' => 'answer', 'station_model' => 'station', 'company_model' => 'company')
- );
- $this->load->helper('calc_helper');
- $this->data['token'] = $token;
- $this->data['quizz'] = $this->quizz->with_stations($quizz_id);
- $this->data['quizz_station'] = $this->station->get($this->data['quizz']->station_id);
- $this->data['quizz_company'] = $this->company->get($this->data['quizz_station']->company_id);
- $this->data['stations'] = $this->station->order_by(array('tipo' => 'DESC', 'cdg' => 'ASC', 'name' => 'ASC'))->get_many_by('supervisor_id !=', null);
- $this->data['parents'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id' => '0', 'cycle_id' => $cycle_id));
- $this->data['childs'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id !=' => '0', 'cycle_id' => $cycle_id));
- $this->data['questions'] = $this->question->with_results($quizz_id);
- }
- /**
- * Success page after respond quizz!
- */
- public function success($token = null, $parent = null, $child = null)
- {
- if (is_null($token) || is_null($parent) || is_null($child)) {
- show_404();
- }
- $this->data['app_presenter'] = new App_presenter();
- $this->layout = 'layouts/application';
- $this->data['app_title'] = 'Prio Top Service';
- $this->load->model(
- array('quizz_model' => 'quizz_model', 'answer_model' => 'answer', 'pac_model' => 'pac')
- );
- $this->quizz_model->change_status($token, 1);
- $this->data['quizz'] = $this->quizz_model->get_by('token', $token);
- // Quizz database
- $this->data['current_auditor'] = $this->user->get($this->data['quizz']->auditor_id);
- $this->data['current_station'] = (!is_null($this->data['quizz']->station_id)) ? $this->station->get($this->data['quizz']->station_id) : array();
- $this->data['cycles'] = $this->cycle->avaiable_cycles($this->data['current_business_area']->id);
- $this->data['stations'] = $this->station->order_by(array('tipo' => 'DESC', 'cdg' => 'ASC', 'name' => 'ASC'))->get_many_by('supervisor_id !=', null);
- // Quizz structure
- $this->data['quizz'] = $this->quizz_model->get_by('token', $token);
- $this->data['parent'] = $this->group->order_by('position', 'ASC')->get(
- array('parent_id' => '0', 'cycle_id' => $this->data['quizz']->cycle_id, 'id' => $parent)
- );
- $this->data['child'] = $this->group->get($child);
- $this->data['questions'] = $this->question->get_many_by(array('question_group_id' => $this->data['child']->id));
- $this->data['answers'] = array();
- // Create and send PACS - We need to check if there's any pacs related to these quizz, if so skip this step
- $cycle = $this->cycle->get($this->data['quizz']->cycle_id);
- if ($this->pac->count_by(array('quizz_id' => $this->data['quizz']->id)) < 1) {
- if ($cycle->business_area_id !== '4') {
- $anwsers_non_conformities = $this->quizz_model->get_non_conformities($this->data['quizz']->token);
- $anwsers_om = $this->quizz_model->get_om($this->data['quizz']->token);
- $anwswer_to_pac = array_merge($anwsers_non_conformities, $anwsers_om);
- if (!empty($anwswer_to_pac)) {
- $this->pac->send_notification_nc($anwswer_to_pac, $this->data['quizz']->station_id);
- }
- }
- }
- }
- /**
- * Get and Set users Pesmirssions to specific cycle_id
- * @param int $cycle_id The cycle_id
- */
- public function users($cycle_id)
- {
- if ($_POST) {
- if ($this->cycle->set_users($this->input->post())) {
- $this->session->set_flashdata('success', 'As permissões foram editadas com sucesso');
- } else {
- $this->session->set_flashdata('error', 'Não foi possível editar as permissões');
- }
- redirect($this->data['business_area'] . '/quizz_manager/index');
- }
- $this->data['cycle'] = $this->cycle->get($cycle_id);
- $this->data['groups'] = $this->ion_auth->groups()->result();
- $this->data['users'] = $this->user->with_cycle_permissions($cycle_id);
- $this->breadcrumb->add_crumb($this->data['cycle']->title, '#');
- $this->breadcrumb->add_crumb('Gestão de utilizadores', '#');
- }
- /**
- * Export anwsers to excel by cycle
- * @param Int $cycle_id
- * @return file
- */
- public function export($cycle_id = null)
- {
- if (is_null($cycle_id)) {
- show_404();
- }
- //quizz_struture
- $quizz_struture = $this->get_struture($cycle_id);
- //quizz_content
- $content = $this->get_content_quizzes($cycle_id, $quizz_struture);
- //exportxls
- $this->view = false;
- $this->output->enable_profiler(false);
- $this->config->load('settings');
- $abc = $this->config->item('xlx_columns');
- $cell = 2;
- $how_many_cell_resets = 0;
- $spreadsheet = new Spreadsheet();
- $sheet = $spreadsheet->getActiveSheet();
- $sheet->setCellValue('A1', 'Posto');
- foreach ($quizz_struture as $key => $question) {
- $letter = ($how_many_cell_resets > 0) ? $abc[$how_many_cell_resets] . $abc[$cell] : $abc[$cell];
- if ($cell === 26) {
- $cell = 0;
- $how_many_cell_resets++;
- }
- $sheet->setCellValue($letter . '1', $question);
- $cell++;
- }
- $cell = 1;
- $row = 2;
- $how_many_cell_resets = 0;
- foreach ($content as $station => $values) {
- $keys_diff[$station] = array_diff_key($quizz_struture, $values['questions']);
- //verificar questoes vazias
- if (!empty($keys_diff[$station])) {
- foreach ($keys_diff[$station] as $key_question => $value) {
- $keys_diff[$station][$key_question] = '---';
- }
- $values['questions'] = array_replace($values['questions'], $keys_diff[$station]);
- $values['questions'] = ksort($values['questions'], SORT_NUMERIC);
- }
- echo "<pre>";
- print_r($values['questions']);
- echo "</pre>";
- if (count($values['questions']) === count($quizz_struture)) {
- // $values['questions'] = arsort($values['questions']);
- // if ($cell === 1 && $how_many_cell_resets === 0) {
- // $sheet->setCellValue('A' . $row, $values['station_name']);
- // $cell++;
- // }
- // foreach ($values['questions'] as $id => $answer) {
- // $letter = ($how_many_cell_resets > 0) ? $abc[$how_many_cell_resets] . $abc[$cell] : $abc[$cell];
- // if ($cell === 26) {
- // $cell = 0;
- // $how_many_cell_resets++;
- // }
- // $sheet->setCellValue($letter . $row, $answer);
- // $cell++;
- // }
- // $row++;
- // $cell = 1;
- // $how_many_cell_resets = 0;
- }
- }
- die();
- $writer = new Xlsx($spreadsheet);
- ob_get_clean();
- header('Content-type: application/vnd.ms-excel');
- header('Content-Disposition: attachment; filename="' . md5(date('Y-m-d')) . '.xlsx"');
- $writer->save('php://output');
- }
- /**
- * Get struture questions by cycle to create cells
- * @param Int $cycle_id
- * @return array $quizz_struture
- */
- protected function get_struture($cycle_id = null)
- {
- if (is_null($cycle_id)) {
- show_404();
- }
- //quizz_struture
- $this->data['parents'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id' => '0', 'cycle_id' => $cycle_id));
- $quizz_struture = array();
- if (!empty($this->data['parents'])) {
- foreach ($this->data['parents'] as $parent) {
- $this->data['childs'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id' => $parent->id, 'cycle_id' => $cycle_id));
- foreach ($this->data['childs'] as $child) {
- $this->data['questions'] = $this->question->get_questions(null, $parent->id, $child->id, 3);
- foreach ($this->data['questions'] as $key => $question) {
- $quizz_struture[$question->id] = $parent->position . '.' . $child->position . '.' . $question->order;
- }
- }
- }
- }
- return $quizz_struture;
- }
- /**
- * Get content with station name and answers by cycle_id
- * @param Int $cycle_id
- * @return
- */
- protected function get_content_quizzes($cycle_id = null, $quizz_struture = array())
- {
- if (is_null($cycle_id) && !empty($quizz_struture)) {
- show_404();
- }
- $this->load->helper('calc_helper');
- $this->data['quizzes'] = $this->quizz->get_with_info($cycle_id, null, array('qz_quizzes.status' => 1));
- foreach ($this->data['quizzes'] as $key => $quizz) {
- $this->data['quizzes_export'][$key] = array();
- $this->data['quizzes_export'][$key]['station_name'] = $quizz->station_name;
- $this->data['quizzes_export'][$key]['final_score'] = convertToPercent($quizz->final_score, $quizz->max_score) . '%';
- $this->data['quizzes_export'][$key]['questions'] = array();
- $questions = $this->question->with_results($quizz->id);
- foreach ($quizz_struture as $id_question => $question) {
- foreach ($questions as $key_question => $question_station) {
- $this->data['quizzes_export'][$key]['questions'][$id_question] = '--';
- if ($question_station->question_id === $id_question) {
- if ($question_station->conforme === '1') {
- $this->data['quizzes_export'][$key]['questions'][$id_question] = 'C';
- } elseif ($question_station->naoconforme === '1') {
- $this->data['quizzes_export'][$key]['questions'][$id_question] = 'NC';
- } else {
- $this->data['quizzes_export'][$key]['questions'][$id_question] = 'NA';
- }
- }
- }
- }
- }
- return $this->data['quizzes_export'];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement