Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3. require_once APPPATH . 'presenters/App_presenter.php';
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  6.  
  7. class Quizz_manager extends ADMIN_Controller
  8. {
  9.  
  10.     public $models = array('cycle', 'question', 'group', 'referencial', 'quizz');
  11.  
  12.     public function __construct()
  13.     {
  14.         parent::__construct();
  15.         $this->table = 'cycle';
  16.         $this->data['cycle_id'] = $this->uri->segment(4);
  17.         $this->data['cycle'] = $this->cycle->get($this->data['cycle_id']);
  18.         if (isset($this->data['current_business_area']->id)) {
  19.             $this->data['cycles'] = $this->cycle->get_cycles($this->data['current_business_area']->id);
  20.         }
  21.  
  22.         $this->breadcrumb->add_crumb('Dashboard', site_url('dashboard/index'));
  23.         $this->breadcrumb->add_crumb($this->data['current_business_area']->name_show, site_url($this->data['business_area'] . '/index'));
  24.         $this->breadcrumb->add_crumb('Gestão de Questionários', site_url($this->data['business_area'] . '/quizz_manager/index'));
  25.         $this->acl = $this->config->item($this->router->class);
  26.     }
  27.  
  28.     /**
  29.      * Get all the quizzes and shows them to the user
  30.      */
  31.     public function index()
  32.     {
  33.         if (!$this->acl[$this->data['user_group']->id]['index']) {
  34.             $this->session->set_flashdata('error', 'Não tem permissões para efetuar esta operação!');
  35.             redirect($this->data['business_area']);
  36.         }
  37.         $this->get();
  38.     }
  39.  
  40.     /**
  41.      * View that returns the form that reflects the cycle
  42.      * @param  Int    $cycle_id
  43.      * @return View
  44.      */
  45.     public function view($cycle_id = null)
  46.     {
  47.         if (!$this->acl[$this->data['user_group']->id]['view']) {
  48.             $this->session->set_flashdata('error', 'Não tem permissões para efetuar esta operação!');
  49.             redirect($this->data['business_area']);
  50.         }
  51.  
  52.         if ($_POST) {
  53.             $this->question->update_order($_POST);
  54.             redirect($this->data['business_area'] . '/quizz_manager/view/' . $cycle_id);
  55.         }
  56.  
  57.         $this->get();
  58.         $this->data['parents'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id' => '0', 'cycle_id' => $cycle_id));
  59.         $this->data['childs'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id !=' => '0', 'cycle_id' => $cycle_id));
  60.         $this->data['questions'] = $this->question->by_filters(array('cycle_id' => $cycle_id));
  61.         $this->breadcrumb->add_crumb($this->data['cycle']->title, '#');
  62.     }
  63.  
  64.     /**
  65.      * Create copy cycle
  66.      * @param Int $cycle_id calls the model method that clone cycle
  67.      */
  68.     public function cycle_clone($cycle_id)
  69.     {
  70.         if (!$this->acl[$this->data['user_group']->id]['cycle_clone']) {
  71.             $this->session->set_flashdata('error', 'Não tem permissões para efetuar esta operação!');
  72.             redirect($this->data['business_area']);
  73.         }
  74.  
  75.         if ($this->group->cycle_clone($cycle_id)) {
  76.             $this->session->set_flashdata('success', 'O questionário foi copiado com sucesso');
  77.         } else {
  78.             $this->session->set_flashdata('error', 'Não foi possível copiar o questionário');
  79.         }
  80.         redirect($this->data['business_area'] . '/quizz_manager/index/?' . $_SERVER['QUERY_STRING']);
  81.     }
  82.  
  83.     /**
  84.      * get cycles to specif business area with ajax
  85.      */
  86.     public function ajax_cycles()
  87.     {
  88.         if (!$this->acl[$this->data['user_group']->id]['ajax_cycles']) {
  89.             $this->session->set_flashdata('error', 'Não tem permissões para efetuar esta operação!');
  90.             redirect($this->data['business_area']);
  91.         }
  92.  
  93.         $this->view = false;
  94.         $this->output->enable_profiler(false);
  95.         if ($this->input->is_ajax_request()) {
  96.             if (isset($_POST['business_area'])) {
  97.                 // get cycles with logged_user permissions
  98.                 $this->data['user_cycles'] = array_column($this->cycle->user_cycles($this->data['logged_user']->id), 'qz_cycle_id');
  99.                 if ($_POST['business_area'] === '6') {
  100.                     if (!empty($this->data['user_cycles'])) {
  101.                         $this->data['cycles'] = $this->cycle->avaiable_cycles($this->input->post('business_area'), $this->data['user_cycles']);
  102.                     } else {
  103.                         $this->data['cycles'] = array();
  104.                     }
  105.                 } else {
  106.                     $this->data['cycles'] = $this->cycle->avaiable_cycles($this->input->post('business_area'));
  107.                 }
  108.  
  109.                 if (!empty($this->data['cycles'])) {
  110.                     $result = '<option></option>';
  111.                     foreach ($this->data['cycles'] as $cycle) {
  112.                         $result .= '<option value="' . $cycle->id . '">' . $cycle->title . ' </option>';
  113.                     }
  114.                 } else {
  115.                     $result = '';
  116.                 }
  117.             }
  118.             echo $result;
  119.         } else {
  120.             show_404();
  121.         }
  122.     }
  123.  
  124.     /**
  125.      * View the quizzes specifc cycle_id
  126.      * @param  Int    $cycle_id
  127.      * @return View
  128.      */
  129.     public function quizzes($cycle_id = null)
  130.     {
  131.         if (is_null($cycle_id)) {
  132.             show_404();
  133.         }
  134.         $this->load->model('quizz_model', 'quizz_model');
  135.         $this->load->helper('calc_helper');
  136.         $this->data['cycle_id'] = $cycle_id;
  137.         $this->data['quizzes'] = $this->quizz_model->get_with_info($cycle_id);
  138.         $this->breadcrumb->add_crumb($this->data['cycle']->title, '#');
  139.     }
  140.  
  141.     /**
  142.      * View the quizz detail with anwswer
  143.      * @param  Int    $cycle_id
  144.      * @param  Int    $quizz_id
  145.      * @return View
  146.      */
  147.     public function detail($cycle_id = null, $quizz_id = null)
  148.     {
  149.         if (is_null($cycle_id) || is_null($quizz_id)) {
  150.             show_404();
  151.         }
  152.         if ($this->data['current_business_area']->id === '3') {
  153.             redirect($this->data['current_business_area']->name . '/quizz_manager/view_report/' . $cycle_id . '/' . $quizz_id);
  154.         }
  155.         $this->load->model('quizz_model', 'quizz');
  156.         $this->load->model('station_model', 'station');
  157.         $this->load->model('company_model', 'company');
  158.         $this->load->helper('calc_helper');
  159.         //header info
  160.         $this->data['quizz'] = $this->quizz->with_stations($quizz_id);
  161.         $this->data['quizz_station'] = $this->station->get($this->data['quizz']->station_id);
  162.         $this->data['quizz_company'] = $this->company->get($this->data['quizz_station']->company_id);
  163.         //quizz info
  164.         $this->data['parents'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id' => '0', 'cycle_id' => $cycle_id));
  165.         $this->data['childs'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id !=' => '0', 'cycle_id' => $cycle_id));
  166.         $this->data['questions'] = $this->question->with_results($quizz_id);
  167.  
  168.         $this->breadcrumb->add_crumb($this->data['cycle']->title, '#');
  169.     }
  170.  
  171.     /**
  172.      * View the quizz detail with anwswer
  173.      * @param  Int    $cycle_id
  174.      * @param  Int    $quizz_id
  175.      * @return View
  176.      */
  177.     public function view_report($cycle_id = null, $quizz_id = null)
  178.     {
  179.         if (is_null($cycle_id) || is_null($quizz_id)) {
  180.             show_404();
  181.         }
  182.         $this->layout = false;
  183.         $this->output->enable_profiler(false);
  184.  
  185.         $this->load->model('quizz_model', 'quizz');
  186.         $this->load->model('station_model', 'station');
  187.         $this->load->model('company_model', 'company');
  188.         $this->load->helper('calc_helper');
  189.         //header info
  190.         $this->data['quizz'] = $this->quizz->with_stations($quizz_id);
  191.         $this->data['quizz_station'] = $this->station->get($this->data['quizz']->station_id);
  192.         $this->data['station_manager'] = $this->user->get($this->data['quizz_station']->manager_id);
  193.         $this->data['station_supervisor'] = $this->user->get($this->data['quizz_station']->supervisor_id);
  194.         $this->data['network_dir'] = $this->user->get(905);
  195.         $this->data['quizz_manager'] = $this->user->get($this->data['quizz_station']->manager_id);
  196.         $this->data['quizz_supervisor'] = $this->user->get($this->data['quizz_station']->supervisor_id);
  197.         //quizz info
  198.         $this->data['parents'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id' => '0', 'cycle_id' => $cycle_id));
  199.         $this->data['count_parents'] = $this->group->order_by('position', 'ASC')->count_by(array('parent_id' => '0', 'cycle_id' => $cycle_id));
  200.         $this->data['childs'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id !=' => '0', 'cycle_id' => $cycle_id));
  201.         $this->data['questions'] = $this->question->with_results($quizz_id);
  202.         $this->data['obs'] = $this->quizz->get_obs_group($this->data['quizz']->id);
  203.         $this->breadcrumb->add_crumb($this->data['cycle']->title, '#');
  204.  
  205.         for ($page_parents = 0; $page_parents < $this->data['count_parents']; $page_parents++) {
  206.             foreach ($this->data['parents'] as $key => $parent) {
  207.                 if ($key === $page_parents) {
  208.                     $count_childs = $i = 0;
  209.                     $total[$key] = array();
  210.                     foreach ($this->data['childs'] as $key_child => $child) {
  211.                         if ($child->parent_id === $parent->id) {
  212.                             $count_childs++;
  213.                             $total[$key]['count_childs'] = $count_childs;
  214.                             $this->data['count_questions'] = count($this->question->with_results($quizz_id, $child->id));
  215.                             $total[$key]['childs'][$i] = array(
  216.                                 'parent_position' => $parent->position,
  217.                                 'parent_title' => $parent->title,
  218.                                 'parent_id' => $parent->id,
  219.                                 'child_position' => $child->position,
  220.                                 'child_title' => $child->title,
  221.                                 'child_id' => $child->id,
  222.                                 'count_questions' => $this->data['count_questions'],
  223.                                 'pages' => ceil($this->data['count_questions'] / 10),
  224.                                 'images' => $this->quizz->get_images($quizz_id, $child->id),
  225.                             );
  226.                             $i++;
  227.                         }
  228.                     }
  229.                 }
  230.             }
  231.         }
  232.         $this->data['total'] = $total;
  233.     }
  234.  
  235.     /**
  236.      * View cycle
  237.      * @param  [int]    $cycle_id
  238.      * @param  [int]    $user_id
  239.      * @param  [int]    $station_id
  240.      * @param  [string] $token
  241.      * @param  [int]    $parent
  242.      * @param  [int]    $child
  243.      * @return html
  244.      */
  245.     public function reply($cycle_id = null, $user_id = null, $station_id = null, $token = null, $parent = null, $child = null)
  246.     {
  247.         if (is_null($cycle_id) || is_null($user_id)) {
  248.             show_404();
  249.         }
  250.         $this->output->enable_profiler(false);
  251.         $this->load->model(
  252.             array('quizz_model' => 'quizz_model', 'answer_model' => 'answer')
  253.         );
  254.         $this->data['app_presenter'] = new App_presenter();
  255.         $this->layout = 'layouts/application';
  256.         $this->data['app_title'] = 'Prio Top Service';
  257.  
  258.         $this->data['is_qsa'] = '';
  259.         if ($this->data['current_business_area']->id === '3') {
  260.             $this->data['is_qsa'] = 'qsa';
  261.         }
  262.  
  263.         if (isset($station_id) && !empty($station_id) && empty($token)) {
  264.             $data = array(
  265.                 'quizz_type_id' => $this->data['cycle']->quizz_type_id,
  266.                 'station_id' => $station_id,
  267.                 'auditor_id' => $user_id,
  268.                 'email_at' => date('Y-m-d'),
  269.                 'sent' => '1',
  270.                 'start_time' => date('H:i:s'),
  271.                 'cycle_id' => $this->data['cycle']->id,
  272.             );
  273.             $new_quizz = $this->quizz_model->skip_validation()->insert($data);
  274.             $quizz = $this->quizz_model->get($new_quizz);
  275.             redirect(current_url() . '/' . $quizz->token);
  276.         }
  277.  
  278.         // Quizz base data
  279.         $this->data['current_auditor'] = $this->user->get($user_id);
  280.         $this->data['current_station'] = (!is_null($station_id)) ? $this->station->get($station_id) : array();
  281.         $this->data['cycles'] = $this->cycle->avaiable_cycles($this->data['current_business_area']->id);
  282.         $this->data['stations'] = $this->station->order_by(array('tipo' => 'DESC', 'cdg' => 'ASC', 'name' => 'ASC'))->get_many_by('supervisor_id !=', null);
  283.         $this->data['token'] = $token;
  284.         $this->data['first'] = (is_null($parent)) ? true : false;
  285.         if (!$this->data['first']) {
  286.             $this->data['previous_link'] = $this->group->previous_parent_and_child($parent, $child, $cycle_id);
  287.             if (!$this->data['previous_link']) {
  288.                 $this->data['first'] = true;
  289.             }
  290.         }
  291.  
  292.         // Quizz structure
  293.         if (isset($token) && !empty($token)) {
  294.             $this->data['quizz'] = $this->quizz_model->get_by('token', $token);
  295.             if (empty($parent)) {
  296.                 $parent_id = $this->quizz_model->last_answer($this->data['quizz']->id, $this->data['quizz']->cycle_id);
  297.                 $this->data['parent'] = $this->group->order_by('position', 'ASC')->get(
  298.                     array('parent_id' => '0', 'cycle_id' => $cycle_id, 'id' => $parent_id)
  299.                 );
  300.  
  301.             } else {
  302.                 $this->data['parent'] = $this->group->order_by('position', 'ASC')->get(
  303.                     array('parent_id' => '0', 'cycle_id' => $cycle_id, 'id' => $parent)
  304.                 );
  305.             }
  306.             if (empty($child)) {
  307.                 $this->data['child'] = $this->group->order_by('position', 'ASC')->get_by(
  308.                     array('parent_id' => $parent_id, 'cycle_id' => $cycle_id)
  309.                 );
  310.             } else {
  311.                 $this->data['child'] = $this->group->get($child);
  312.             }
  313.             $this->data['obs'] = $this->quizz->get_obs_group($this->data['quizz']->id, $this->data['child']->id);
  314.             $this->data['questions'] = $this->question->order_by(array('order' => 'ASC', 'id' => 'ASC'))->get_many_by(array('question_group_id' => $this->data['child']->id));
  315.             $this->data['answers'] = $this->answer->for_questions($this->data['questions'], $this->data['quizz']->id);
  316.         }
  317.         $this->data['images_child'] = $this->quizz->get_images($this->data['quizz']->id, $child);
  318.         $this->data['images'] = $this->quizz->get_images($this->data['quizz']->id, $child, true);
  319.  
  320.         $this->data['all_parents'] = $this->group->order_by('position', 'ASC')->get_many_by(
  321.             array('parent_id' => '0', 'cycle_id' => $cycle_id)
  322.         );
  323.         $this->data['all_childs'] = $this->group->with_answers(
  324.             array(4, $cycle_id, $token, $this->data['quizz']->id, $cycle_id)
  325.         );
  326.     }
  327.  
  328.     /**
  329.      * save state
  330.      * Function that can only be access via POST to save the current answers I'm givin to the quest
  331.      * @return redirect (either to the next step, success page or the same one in case its an error)
  332.      */
  333.     public function save_state()
  334.     {
  335.         if (!$_POST) {
  336.             show_404();
  337.         }
  338.         $is_ajax = false;
  339.         if ($this->input->is_ajax_request()) {
  340.             $this->output->enable_profiler(false);
  341.             $is_ajax = true;
  342.         }
  343.         $this->view = false;
  344.         $this->load->model('quizz_model', 'quizz_model');
  345.         $this->load->model('answer_model', 'answer');
  346.         $this->load->model('pac_model', 'pac');
  347.         $this->load->model('cycle_model', 'cycle');
  348.         $quizz = $this->quizz_model->get_by('token', $this->input->post('token'));
  349.         $cycle = $this->cycle->get_by('id', $quizz->cycle_id);
  350.         if (!empty($_POST['obs_group'])) {
  351.             $this->quizz_model->update_obs_group($quizz->id, $_POST['current_child'], $this->input->post('obs_group'));
  352.             unset($_POST['obs_group']);
  353.         }
  354.         if (!empty($_POST['obs'])) {
  355.             $this->quizz_model->update_obs($this->input->post('token'), $this->input->post('obs'));
  356.             unset($_POST['obs']);
  357.         }
  358.         if (empty($_POST['answer'])) {
  359.             if (!$is_ajax) {
  360.                 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');
  361.             } else {
  362.                 return false;
  363.             }
  364.         }
  365.         $questions = array_keys($_POST['answer']);
  366.         $this->answer->upload_images($_FILES, $quizz->id, $this->input->post('current_child')); // insert new structure
  367.         $this->answer->delete_answers($quizz->id, $questions); // Delete current structure before insert
  368.         $this->answer->insert_answers($quizz->id, $this->input->post('answer')); // insert new structure
  369.  
  370.         $this->quizz_model->update_status($this->input->post('token'), $this->input->post('parent_id'), $this->input->post('current_child'));
  371.         $parent_childs = $this->group->order_by('position', 'ASC')->get_many_by(
  372.             array('parent_id' => $_POST['parent_id'], 'cycle_id' => $this->input->post('cycle_id'))
  373.         );
  374.         $next_child = null;
  375.         foreach ($parent_childs as $key => $child) {
  376.             if ($_POST['current_child'] === $child->id) {
  377.                 $next_child = (isset($parent_childs[$key + 1])) ? $parent_childs[$key + 1] : null;
  378.                 break;
  379.             }
  380.         }
  381.         $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')));
  382.         if (empty($all_unanswered_childs)) {
  383.             $this->quizz_model->calc_result($this->input->post('token'));
  384.             if (!$is_ajax) {
  385.                 redirect($this->data['business_area'] . '/quizz_manager/obs/' . $_POST['token'] . '/' . $_POST['parent_id'] . '/' . $_POST['current_child']);
  386.             } else {
  387.                 return true;
  388.             }
  389.         }
  390.         if (!empty($next_child)) {
  391.             if (!$is_ajax) {
  392.                 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);
  393.             } else {
  394.                 return true;
  395.             }
  396.         } else {
  397.             $parents = $this->group->order_by('position', 'ASC')->get_many_by(
  398.                 array('parent_id' => '0', 'cycle_id' => $this->input->post('cycle_id'))
  399.             );
  400.             $next_parent = null;
  401.             foreach ($parents as $k => $parent) {
  402.                 if ($_POST['parent_id'] === $parent->id) {
  403.                     $next_parent = (isset($parents[$k + 1])) ? $parents[$k + 1] : null;
  404.                     break;
  405.                 }
  406.             }
  407.             if (!empty($next_parent)) {
  408.                 $parent_first_child = $this->group->order_by('position', 'ASC')->get_by(
  409.                     array('parent_id' => $next_parent->id, 'cycle_id' => $this->input->post('cycle_id'))
  410.                 );
  411.                 $next_child = $parent_first_child->id;
  412.                 if (!empty($next_child)) {
  413.                     if (!$is_ajax) {
  414.                         redirect($this->data['business_area'] . '/quizz_manager/reply/' . $_POST['cycle_id'] . '/' . $quizz->auditor_id . '/' . $_POST['station_id'] . '/' . $_POST['token'] . '/' . $next_parent->id . '/' . $next_child);
  415.                     } else {
  416.                         return true;
  417.                     }
  418.                 }
  419.             } else {
  420.                 // in case you're not answering in order and its the last one redirect to the same one
  421.                 if (!$is_ajax) {
  422.                     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']);
  423.                 } else {
  424.                     return true;
  425.                 }
  426.             }
  427.         }
  428.     }
  429.  
  430.     /**
  431.      * Add one final comment to the quizz
  432.      * @param  string   $token  The quizz token
  433.      * @param  int      $parent The parent (not really being used mostly to keep harmony in the urls)
  434.      * @param  int      $child  The child (not really being used mostly to keep harmony in the urls)
  435.      * @return redirect on post
  436.      */
  437.     public function obs($token = null, $parent = null, $child = null)
  438.     {
  439.         $this->layout = 'layouts/application';
  440.         $this->data['app_title'] = 'Prio Top Service';
  441.         $this->load->model(
  442.             array('quizz_model' => 'quizz_model', 'answer_model' => 'answer')
  443.         );
  444.         $this->output->enable_profiler(false);
  445.         // Quizz structure
  446.         if (isset($token) && !empty($token)) {
  447.             $this->data['quizz'] = $this->quizz_model->get_by('token', $token);
  448.             if (empty($parent)) {
  449.                 $parent_id = $this->quizz_model->last_answer($this->data['quizz']->id, $this->data['quizz']->cycle_id);
  450.                 $this->data['parent'] = $this->group->order_by('position', 'ASC')->get(
  451.                     array('parent_id' => '0', 'cycle_id' => $this->data['quizz']->cycle_id, 'id' => $parent_id)
  452.                 );
  453.  
  454.             } else {
  455.                 $this->data['parent'] = $this->group->order_by('position', 'ASC')->get(
  456.                     array('parent_id' => '0', 'cycle_id' => $this->data['quizz']->cycle_id, 'id' => $parent)
  457.                 );
  458.             }
  459.             if (empty($child)) {
  460.                 $this->data['child'] = $this->group->order_by('position', 'ASC')->get_by(
  461.                     array('parent_id' => $parent_id, 'cycle_id' => $this->data['quizz']->cycle_id)
  462.                 );
  463.             } else {
  464.                 $this->data['child'] = $this->group->get($child);
  465.             }
  466.             $this->data['questions'] = $this->question->get_many_by(array('question_group_id' => $this->data['child']->id));
  467.             $this->data['answers'] = $this->answer->for_questions($this->data['questions'], $this->data['quizz']->id);
  468.         }
  469.         $this->data['all_parents'] = $this->group->order_by('position', 'ASC')->get_many_by(
  470.             array('parent_id' => '0', 'cycle_id' => $this->data['quizz']->cycle_id)
  471.         );
  472.         $this->data['all_childs'] = $this->group->with_answers(
  473.             array(4, $this->data['quizz']->cycle_id, $token, $this->data['quizz']->id, $this->data['quizz']->cycle_id)
  474.         );
  475.  
  476.         $this->data['quizz'] = $this->quizz_model->get_by('token', $token);
  477.         $this->data['current_auditor'] = $this->user->get($this->data['quizz']->auditor_id);
  478.         $this->data['current_station'] = (!is_null($this->data['quizz']->station_id)) ? $this->station->get($this->data['quizz']->station_id) : array();
  479.         $this->data['stations'] = $this->station->order_by(array('tipo' => 'DESC', 'cdg' => 'ASC', 'name' => 'ASC'))->get_many_by('supervisor_id !=', null);
  480.         if ($_POST) {
  481.             if (!empty($_POST['obs'])) {
  482.                 $this->quizz_model->update_obs($token, $this->input->post('obs'));
  483.             }
  484.             redirect($this->data['business_area'] . '/quizz_manager/confirm/' . $token . '/' . $parent . '/' . $child);
  485.         }
  486.     }
  487.  
  488.     /**
  489.      * Add options before closed quizz
  490.      * @param string $token  The quizz token
  491.      * @param int    $parent The parent (not really being used mostly to keep harmony in the urls)
  492.      * @param int    $child  The child (not really being used mostly to keep harmony in the urls)
  493.      */
  494.     public function confirm($token = null, $parent = null, $child = null)
  495.     {
  496.         if (is_null($token) || is_null($parent) || is_null($child)) {
  497.             show_404();
  498.         }
  499.  
  500.         $this->layout = 'layouts/application';
  501.         $this->data['app_title'] = 'Prio Top Service';
  502.         $this->load->model(
  503.             array('quizz_model' => 'quizz_model', 'answer_model' => 'answer')
  504.         );
  505.  
  506.         $this->data['token'] = $token;
  507.         $this->data['parent_id'] = $parent;
  508.         $this->data['child_id'] = $child;
  509.         $this->data['quizz'] = $this->quizz_model->get_by('token', $token);
  510.         $this->data['current_auditor'] = $this->user->get($this->data['quizz']->auditor_id);
  511.         $this->data['current_station'] = (!is_null($this->data['quizz']->station_id)) ? $this->station->get($this->data['quizz']->station_id) : array();
  512.         $this->data['stations'] = $this->station->order_by(array('tipo' => 'DESC', 'cdg' => 'ASC', 'name' => 'ASC'))->get_many_by('supervisor_id !=', null);
  513.     }
  514.  
  515.     /**
  516.      * View with quizz report detail
  517.      * @param int    $cycle_id The cycle id
  518.      * @param int    $quizz_id The quizz id
  519.      * @param string $token    The quizz token
  520.      */
  521.     public function view_detail($cycle_id = null, $quizz_id = null, $token = null)
  522.     {
  523.         if (is_null($token) || is_null($cycle_id) || is_null($quizz_id)) {
  524.             show_404();
  525.         }
  526.  
  527.         $this->layout = 'layouts/application';
  528.         $this->data['app_title'] = 'Prio Top Service';
  529.         $this->load->model(
  530.             array('quizz_model' => 'quizz', 'answer_model' => 'answer', 'station_model' => 'station', 'company_model' => 'company')
  531.         );
  532.         $this->load->helper('calc_helper');
  533.  
  534.         $this->data['token'] = $token;
  535.         $this->data['quizz'] = $this->quizz->with_stations($quizz_id);
  536.         $this->data['quizz_station'] = $this->station->get($this->data['quizz']->station_id);
  537.         $this->data['quizz_company'] = $this->company->get($this->data['quizz_station']->company_id);
  538.         $this->data['stations'] = $this->station->order_by(array('tipo' => 'DESC', 'cdg' => 'ASC', 'name' => 'ASC'))->get_many_by('supervisor_id !=', null);
  539.         $this->data['parents'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id' => '0', 'cycle_id' => $cycle_id));
  540.         $this->data['childs'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id !=' => '0', 'cycle_id' => $cycle_id));
  541.         $this->data['questions'] = $this->question->with_results($quizz_id);
  542.     }
  543.  
  544.     /**
  545.      * Success page after respond quizz!
  546.      */
  547.     public function success($token = null, $parent = null, $child = null)
  548.     {
  549.         if (is_null($token) || is_null($parent) || is_null($child)) {
  550.             show_404();
  551.         }
  552.         $this->data['app_presenter'] = new App_presenter();
  553.         $this->layout = 'layouts/application';
  554.         $this->data['app_title'] = 'Prio Top Service';
  555.  
  556.         $this->load->model(
  557.             array('quizz_model' => 'quizz_model', 'answer_model' => 'answer', 'pac_model' => 'pac')
  558.         );
  559.  
  560.         $this->quizz_model->change_status($token, 1);
  561.  
  562.         $this->data['quizz'] = $this->quizz_model->get_by('token', $token);
  563.  
  564.         // Quizz database
  565.         $this->data['current_auditor'] = $this->user->get($this->data['quizz']->auditor_id);
  566.         $this->data['current_station'] = (!is_null($this->data['quizz']->station_id)) ? $this->station->get($this->data['quizz']->station_id) : array();
  567.         $this->data['cycles'] = $this->cycle->avaiable_cycles($this->data['current_business_area']->id);
  568.         $this->data['stations'] = $this->station->order_by(array('tipo' => 'DESC', 'cdg' => 'ASC', 'name' => 'ASC'))->get_many_by('supervisor_id !=', null);
  569.         // Quizz structure
  570.         $this->data['quizz'] = $this->quizz_model->get_by('token', $token);
  571.         $this->data['parent'] = $this->group->order_by('position', 'ASC')->get(
  572.             array('parent_id' => '0', 'cycle_id' => $this->data['quizz']->cycle_id, 'id' => $parent)
  573.         );
  574.         $this->data['child'] = $this->group->get($child);
  575.         $this->data['questions'] = $this->question->get_many_by(array('question_group_id' => $this->data['child']->id));
  576.         $this->data['answers'] = array();
  577.  
  578.         // Create and send PACS - We need to check if there's any pacs related to these quizz, if so skip this step
  579.         $cycle = $this->cycle->get($this->data['quizz']->cycle_id);
  580.         if ($this->pac->count_by(array('quizz_id' => $this->data['quizz']->id)) < 1) {
  581.             if ($cycle->business_area_id !== '4') {
  582.                 $anwsers_non_conformities = $this->quizz_model->get_non_conformities($this->data['quizz']->token);
  583.                 $anwsers_om = $this->quizz_model->get_om($this->data['quizz']->token);
  584.                 $anwswer_to_pac = array_merge($anwsers_non_conformities, $anwsers_om);
  585.                 if (!empty($anwswer_to_pac)) {
  586.                     $this->pac->send_notification_nc($anwswer_to_pac, $this->data['quizz']->station_id);
  587.                 }
  588.             }
  589.         }
  590.     }
  591.  
  592.     /**
  593.      *  Get and Set users Pesmirssions to specific cycle_id
  594.      * @param int $cycle_id The cycle_id
  595.      */
  596.     public function users($cycle_id)
  597.     {
  598.         if ($_POST) {
  599.             if ($this->cycle->set_users($this->input->post())) {
  600.                 $this->session->set_flashdata('success', 'As permissões foram editadas com sucesso');
  601.             } else {
  602.                 $this->session->set_flashdata('error', 'Não foi possível editar as permissões');
  603.             }
  604.             redirect($this->data['business_area'] . '/quizz_manager/index');
  605.         }
  606.  
  607.         $this->data['cycle'] = $this->cycle->get($cycle_id);
  608.         $this->data['groups'] = $this->ion_auth->groups()->result();
  609.         $this->data['users'] = $this->user->with_cycle_permissions($cycle_id);
  610.         $this->breadcrumb->add_crumb($this->data['cycle']->title, '#');
  611.         $this->breadcrumb->add_crumb('Gestão de utilizadores', '#');
  612.     }
  613.  
  614.     /**
  615.      * Export anwsers to excel by cycle
  616.      * @param  Int    $cycle_id
  617.      * @return file
  618.      */
  619.     public function export($cycle_id = null)
  620.     {
  621.         if (is_null($cycle_id)) {
  622.             show_404();
  623.         }
  624.  
  625.         //quizz_struture
  626.         $quizz_struture = $this->get_struture($cycle_id);
  627.         //quizz_content
  628.         $content = $this->get_content_quizzes($cycle_id, $quizz_struture);
  629.  
  630.         //exportxls
  631.         $this->view = false;
  632.         $this->output->enable_profiler(false);
  633.  
  634.         $this->config->load('settings');
  635.         $abc = $this->config->item('xlx_columns');
  636.         $cell = 2;
  637.         $how_many_cell_resets = 0;
  638.  
  639.         $spreadsheet = new Spreadsheet();
  640.         $sheet = $spreadsheet->getActiveSheet();
  641.         $sheet->setCellValue('A1', 'Posto');
  642.  
  643.         foreach ($quizz_struture as $key => $question) {
  644.             $letter = ($how_many_cell_resets > 0) ? $abc[$how_many_cell_resets] . $abc[$cell] : $abc[$cell];
  645.             if ($cell === 26) {
  646.                 $cell = 0;
  647.                 $how_many_cell_resets++;
  648.             }
  649.             $sheet->setCellValue($letter . '1', $question);
  650.             $cell++;
  651.         }
  652.  
  653.         $cell = 1;
  654.         $row = 2;
  655.         $how_many_cell_resets = 0;
  656.         foreach ($content as $station => $values) {
  657.  
  658.             $keys_diff[$station] = array_diff_key($quizz_struture, $values['questions']);
  659.  
  660.             //verificar questoes vazias
  661.             if (!empty($keys_diff[$station])) {
  662.                 foreach ($keys_diff[$station] as $key_question => $value) {
  663.                     $keys_diff[$station][$key_question] = '---';
  664.                 }
  665.  
  666.                 $values['questions'] = array_replace($values['questions'], $keys_diff[$station]);
  667.                 $values['questions'] = ksort($values['questions'], SORT_NUMERIC);
  668.             }
  669.             echo "<pre>";
  670.             print_r($values['questions']);
  671.             echo "</pre>";
  672.             if (count($values['questions']) === count($quizz_struture)) {
  673.                 // $values['questions'] = arsort($values['questions']);
  674.  
  675.                 // if ($cell === 1 && $how_many_cell_resets === 0) {
  676.                 //     $sheet->setCellValue('A' . $row, $values['station_name']);
  677.                 //     $cell++;
  678.                 // }
  679.                 // foreach ($values['questions'] as $id => $answer) {
  680.                 //     $letter = ($how_many_cell_resets > 0) ? $abc[$how_many_cell_resets] . $abc[$cell] : $abc[$cell];
  681.  
  682.                 //     if ($cell === 26) {
  683.                 //         $cell = 0;
  684.                 //         $how_many_cell_resets++;
  685.                 //     }
  686.  
  687.                 //     $sheet->setCellValue($letter . $row, $answer);
  688.                 //     $cell++;
  689.                 // }
  690.                 // $row++;
  691.                 // $cell = 1;
  692.                 // $how_many_cell_resets = 0;
  693.             }
  694.         }
  695.         die();
  696.         $writer = new Xlsx($spreadsheet);
  697.         ob_get_clean();
  698.         header('Content-type: application/vnd.ms-excel');
  699.         header('Content-Disposition: attachment; filename="' . md5(date('Y-m-d')) . '.xlsx"');
  700.         $writer->save('php://output');
  701.     }
  702.  
  703.     /**
  704.      * Get struture questions by cycle to create cells
  705.      * @param  Int   $cycle_id
  706.      * @return array $quizz_struture
  707.      */
  708.     protected function get_struture($cycle_id = null)
  709.     {
  710.         if (is_null($cycle_id)) {
  711.             show_404();
  712.         }
  713.  
  714.         //quizz_struture
  715.         $this->data['parents'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id' => '0', 'cycle_id' => $cycle_id));
  716.         $quizz_struture = array();
  717.  
  718.         if (!empty($this->data['parents'])) {
  719.             foreach ($this->data['parents'] as $parent) {
  720.                 $this->data['childs'] = $this->group->order_by('position', 'ASC')->get_many_by(array('parent_id' => $parent->id, 'cycle_id' => $cycle_id));
  721.                 foreach ($this->data['childs'] as $child) {
  722.                     $this->data['questions'] = $this->question->get_questions(null, $parent->id, $child->id, 3);
  723.                     foreach ($this->data['questions'] as $key => $question) {
  724.                         $quizz_struture[$question->id] = $parent->position . '.' . $child->position . '.' . $question->order;
  725.                     }
  726.                 }
  727.             }
  728.         }
  729.  
  730.         return $quizz_struture;
  731.     }
  732.  
  733.     /**
  734.      * Get content with station name and answers by cycle_id
  735.      * @param    Int $cycle_id
  736.      * @return
  737.      */
  738.     protected function get_content_quizzes($cycle_id = null, $quizz_struture = array())
  739.     {
  740.         if (is_null($cycle_id) && !empty($quizz_struture)) {
  741.             show_404();
  742.         }
  743.  
  744.         $this->load->helper('calc_helper');
  745.         $this->data['quizzes'] = $this->quizz->get_with_info($cycle_id, null, array('qz_quizzes.status' => 1));
  746.  
  747.         foreach ($this->data['quizzes'] as $key => $quizz) {
  748.             $this->data['quizzes_export'][$key] = array();
  749.             $this->data['quizzes_export'][$key]['station_name'] = $quizz->station_name;
  750.             $this->data['quizzes_export'][$key]['final_score'] = convertToPercent($quizz->final_score, $quizz->max_score) . '%';
  751.             $this->data['quizzes_export'][$key]['questions'] = array();
  752.  
  753.             $questions = $this->question->with_results($quizz->id);
  754.  
  755.             foreach ($quizz_struture as $id_question => $question) {
  756.                 foreach ($questions as $key_question => $question_station) {
  757.                         $this->data['quizzes_export'][$key]['questions'][$id_question] = '--';
  758.                         if ($question_station->question_id === $id_question) {
  759.                             if ($question_station->conforme === '1') {
  760.                                 $this->data['quizzes_export'][$key]['questions'][$id_question] = 'C';
  761.                             } elseif ($question_station->naoconforme === '1') {
  762.                                 $this->data['quizzes_export'][$key]['questions'][$id_question] = 'NC';
  763.                             } else {
  764.                                 $this->data['quizzes_export'][$key]['questions'][$id_question] = 'NA';
  765.                             }
  766.                         }
  767.                    
  768.  
  769.                 }
  770.             }
  771.         }
  772.  
  773.         return $this->data['quizzes_export'];
  774.     }
  775. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement