Advertisement
Guest User

Quiz.php

a guest
Dec 12th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.73 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Quiz extends CI_Controller {
  5.  
  6.     public function __construct()
  7.     {
  8.         parent::__construct();
  9.  
  10.         // cek login
  11.         if ($this->session->userdata('level') != 2)
  12.         {
  13.             $this->session->set_flashdata('info','Maaf, hanya guru yang berhak mengakses halaman ini');
  14.             redirect('main/teacher_login');
  15.         }
  16.  
  17.         // Set template and directori content
  18.         $this->template->set_template('backend/template/teacher-template');
  19.         $this->template->set_directory_content('backend/teacher');
  20.     }
  21.  
  22.     public function index()
  23.     {
  24.         $data['quiz']   = $this->quiz_model->get_quiz_name_by_teacher_id($this->session->userdata['ID'], $status = 1);
  25.         $this->template->view('quiz_name', $data);
  26.     }
  27.  
  28.     public function questions_list($id = NULL)
  29.     {
  30.         if (!$id) redirect('teacher/quiz');
  31.  
  32.         $quiz_model = $this->quiz_model;
  33.  
  34.         if ($quiz_model->get_quiz_name_by_id(decode($id)))
  35.         {
  36.             $data['ID']     = $this->uri->segment(4);
  37.             $data['quiz']   = $this->quiz_model->get_quiz_by_quiz_name_id(decode($id));
  38.             $data['quiz_name'] = $this->quiz_model->get_quiz_name_by_id(decode($id));
  39.             $this->template->view('questions_list', $data);
  40.         }
  41.         else
  42.         {
  43.             $this->session->set_flashdata('failed', 'Maaf, berkas ujian tidak ditemukan');
  44.             redirect('teacher/quiz');
  45.         }
  46.     }
  47.  
  48.     public function question_management_guide_hide($id = NULL)
  49.     {
  50.         if (!$id) redirect('teacher/quiz');
  51.         $this->session->set_userdata('question_management_guide', FALSE);
  52.         redirect('teacher/quiz/questions_list/'.$id);
  53.     }
  54.  
  55.     public function question_management_guide_show($id = NULL)
  56.     {
  57.         if (!$id) redirect('teacher/quiz');
  58.         $this->session->set_userdata('question_management_guide', TRUE);
  59.         redirect('teacher/quiz/questions_list/'.$id);
  60.     }
  61.  
  62.     public function create_quiz_name()
  63.     {
  64.         if (($this->input->post()) && (!empty($this->input->post())))
  65.         {
  66.             $validation = $this->form_validation;
  67.             $validation->set_rules($this->quiz_model->quiz_name_rules());
  68.  
  69.             if ($validation->run() == TRUE)
  70.             {
  71.                     // Create new quiz name
  72.                 $this->quiz_model->create_quiz_name($this->session->userdata('ID'));
  73.                 $this->session->set_flashdata('success', 'Selamat, quiz berhasil ditambah!');
  74.             }
  75.             else
  76.             {
  77.                 $this->session->set_flashdata('failed', 'Maaf,quiz gagal ditambah!');
  78.             }
  79.         }
  80.         else
  81.         {
  82.             $this->session->set_flashdata('failed', 'Maaf, quiz gagal ditambah!');
  83.         }
  84.         redirect('teacher/quiz');
  85.     }
  86.  
  87.     public function update_quiz_name()
  88.     {
  89.         if (($this->input->post()) && (!empty($this->input->post())))
  90.         {
  91.             $validation = $this->form_validation;
  92.             $validation->set_rules($this->quiz_model->quiz_name_rules());
  93.  
  94.             if ($validation->run() == TRUE)
  95.             {
  96.                     // Create new quiz name
  97.                 $this->quiz_model->update_quiz_name();
  98.                 $this->session->set_flashdata('success', 'Selamat, quiz berhasil dirubah!');
  99.             }
  100.             else
  101.             {
  102.                 $this->session->set_flashdata('failed', 'Maaf,quiz gagal dirubah!');
  103.             }
  104.         }
  105.         else
  106.         {
  107.             $this->session->set_flashdata('failed', 'Maaf, quiz tidak ditemukan!');
  108.         }
  109.         redirect('teacher/quiz');
  110.     }
  111.  
  112.     public function make_an_archive($id = NULL)
  113.     {
  114.         if (!$id) redirect('teacher/quiz');
  115.  
  116.         $quiz_model = $this->quiz_model;
  117.        
  118.         if ($quiz_model->get_quiz_name_by_id(decode($id)))
  119.         {
  120.             $quiz_model->make_an_archive(decode($id));
  121.             $this->session->set_flashdata('success', 'Soal berhasil dijadikan arsip');
  122.         }
  123.         else
  124.         {
  125.             $this->session->set_flashdata('failed', 'Soal tidak ditemukan');
  126.         }
  127.         redirect('teacher/quiz');
  128.     }
  129.  
  130.     public function create_quiz($id = NULL)
  131.     {
  132.         if (!$id) redirect('teacher/quiz');
  133.  
  134.         if ($this->quiz_model->get_quiz_name_by_id(decode($id)))
  135.         {
  136.             if ($this->input->post())
  137.             {
  138.                 $validation = $this->form_validation;
  139.                 $validation->set_rules($this->quiz_model->quiz_rules());
  140.  
  141.                 if ($validation->run() === FALSE)
  142.                 {
  143.                    
  144.                     $data['ID']     = $this->uri->segment(4);
  145.                     $this->template->view('create_quiz', $data);
  146.                 }
  147.                 else
  148.                 {
  149.                         // Create new quiz
  150.                     $this->quiz_model->create_quiz(decode($id));
  151.                     $this->session->set_flashdata('success', 'Selamat, quiz berhasil ditambah!');
  152.                     redirect('teacher/quiz/questions_list/'.$id);
  153.                 }
  154.             }
  155.             else
  156.             {
  157.                 $data['ID']     = $this->uri->segment(4);
  158.                 $this->template->view('create_quiz', $data);
  159.             }
  160.         }
  161.         else
  162.         {
  163.             $this->session->set_flashdata('failed', 'Judul soal tidak ditemukan');
  164.             redirect('teacher/quiz');
  165.         }
  166.     }
  167.  
  168.     function tinymce_upload() {
  169.         $this->load->library('upload');
  170.         $this->load->library('image_lib');
  171.         // $nmfile                  = "post_".time();
  172.         //path folder
  173.         $config['upload_path']      = './aila_cbt/images/trash';
  174.         //type yang dapat diakses bisa anda sesuaikan
  175.         $config['allowed_types']    = 'gif|jpg|png|jpeg|bmp';
  176.         //maksimum besar file 1 MB
  177.         $config['max_size']         = '5000';
  178.         //lebar maksimum 1000 px
  179.         $config['max_width']        = '5000';
  180.         //tinggi maksimu 1000 px
  181.         $config['max_height']       = '5000';
  182.         //nama yang terupload nantinya
  183.         // $config['file_name']         = $nmfile;
  184.         //inisialis konfigurasi
  185.  
  186.         $this->upload->initialize($config);
  187.  
  188.  
  189.         if ( ! $this->upload->do_upload('file')) {
  190.             $this->output->set_header('HTTP/1.0 500 Server Error');
  191.             exit;
  192.         } else {
  193.             $gbr = $this->upload->data();
  194.             $config1['image_library']   = 'gd2';
  195.                 // folder tempat menyimpan gambar asal
  196.             $config1['source_image']    = $this->upload->upload_path.$this->upload->file_name;
  197.                 // folder tempat menyimpan hasil resize
  198.             $config1['new_image']       = './aila_cbt/images/quiz/';
  199.                 //
  200.             $config1['maintain_ratio']  = TRUE;
  201.                 //lebar setelah resize menjadi 700 px
  202.             $config1['width']           = 1000;
  203.                 //lebar setelah resize menjadi 500 px
  204.             $config1['height']          = 800;
  205.                 //
  206.             $this->image_lib->initialize($config1);
  207.                 //lakukan resize gambar
  208.             $this->image_lib->resize();
  209.  
  210.             $resize = $this->image_lib->clear();
  211.  
  212.                    //hapus gambar setelah selesai di resize
  213.             unlink($this->upload->upload_path.$this->upload->file_name);
  214.  
  215.             $file = $this->upload->data();
  216.             $this->output
  217.             ->set_content_type('application/json', 'utf-8')
  218.             ->set_output(json_encode(['location' => '../aila_cbt/images/quiz/'.$file['file_name']]))
  219.             ->_display();
  220.             exit;
  221.         }
  222.     }
  223.  
  224.     public function import($ID)
  225.     {
  226.         $quiz_name_ID = decode($ID);
  227.  
  228.         $this->load->library(array('PHPExcel','PHPExcel/IOFactory'));
  229.  
  230.         $fileName = time().$_FILES['file']['name'];
  231.  
  232.         $config['upload_path'] = '././aila_cbt/xls_file/';
  233.         $config['file_name'] = $fileName;
  234.         $config['allowed_types'] = 'xls|xlsx|csv';
  235.         $config['max_size'] = 10000;
  236.  
  237.         $this->load->library('upload');
  238.         $this->upload->initialize($config);
  239.  
  240.         if(! $this->upload->do_upload('file') )
  241.             $this->upload->display_errors();
  242.  
  243.         $media = $this->upload->data('file');
  244.         $inputFileName = $this->upload->data('full_path');
  245.  
  246.         try {
  247.             $inputFileType = IOFactory::identify($inputFileName);
  248.             $objReader = IOFactory::createReader($inputFileType);
  249.             $objPHPExcel = $objReader->load($inputFileName);
  250.         } catch(Exception $e) {
  251.             die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
  252.         }
  253.  
  254.         $sheet = $objPHPExcel->getSheet(0);
  255.         $highestRow = $sheet->getHighestRow();
  256.         $highestColumn = $sheet->getHighestColumn();
  257.  
  258. for ($row = 2; $row <= $highestRow; $row++) {                  //  Read a row of data into an array                
  259.     $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
  260.         NULL,
  261.         TRUE,
  262.         FALSE);
  263. //Sesuaikan sama nama kolom tabel di database              
  264.     $data = array(
  265.         "question"      => $rowData[0][0],
  266.         "quiz_type"     => $rowData[0][1],
  267.         "answer_1"      => $rowData[0][2],
  268.         "answer_2"      => $rowData[0][3],
  269.         "answer_3"      => $rowData[0][4],
  270.         "answer_4"      => $rowData[0][5],
  271.         "answer_5"      => $rowData[0][6],
  272.         "answer_key"    => $rowData[0][7],
  273.         "weight"        => $rowData[0][8],
  274.         "quiz_name_ID"  =>$quiz_name_ID
  275.  
  276.     );
  277.  
  278. // Cek apakah Quiz sudah ada atau belum, kalau ada maka lakukan proses update kalau belum ada maka lakukan proses insert
  279.     $where      = array(
  280.         'question' => $rowData[0][0],
  281.     );
  282.  
  283.     $cek_quiz = $this->quiz_model->get_question($rowData[0][0]);
  284.     if ($cek_quiz >=1 ) {
  285.         $this->quiz_model->update_data($where,$data);
  286.     }
  287.     else
  288.     {
  289.     //sesuaikan nama dengan nama tabel
  290.         $insert = $this->quiz_model->create_data($data);
  291.     }
  292. }
  293.  
  294. // Delete all file trash
  295. delete_files('././aila_cbt/xls_file/');
  296.  
  297. $this->session->set_flashdata('success','<b>'. ($highestRow - 1).' Data berhasil ditambahkan / dirubah.</b> <br/>');
  298. redirect('teacher/quiz/questions_list/'.$ID);
  299. }
  300.  
  301. public function detail_quiz($id = NULL)
  302. {
  303.     if (!$id) redirect('teacher/quiz');
  304.  
  305.     if ($this->quiz_model->get_quiz_by_id(decode($id)))
  306.     {
  307.         $quiz = $this->quiz_model->get_quiz_by_id(decode($id));
  308.  
  309.         $data['ID']     = $quiz->quiz_name_ID;
  310.         $data['quiz']   = $quiz;
  311.         $this->template->view('detail_quiz', $data);
  312.     }
  313.     else
  314.     {
  315.         $this->session->set_flashdata('failed', 'Maaf, soal tidak ditemukan');
  316.         redirect('teacher/quiz');
  317.     }
  318. }
  319.  
  320. public function update_quiz($id = NULL)
  321. {
  322.     if (!$id) redirect('teacher/quiz');
  323.  
  324.     if ($this->quiz_model->get_quiz_by_id(decode($id)))
  325.     {
  326.         $quiz = $this->quiz_model->get_quiz_by_id(decode($id));
  327.         $quiz_name_ID = $quiz->quiz_name_ID;
  328.  
  329.         if ($this->input->post())
  330.         {
  331.             $validation = $this->form_validation;
  332.             $validation->set_rules($this->quiz_model->quiz_rules());
  333.  
  334.             if ($validation->run() === FALSE)
  335.             {
  336.  
  337.                 $data['ID']     = $this->uri->segment(4);
  338.                 $data['quiz']   = $quiz;
  339.                 $this->template->view('update_quiz', $data);
  340.             }
  341.             else
  342.             {
  343.                
  344.                         // Update quiz
  345.                 $this->quiz_model->update_quiz();
  346.                 $this->session->set_flashdata('success', 'Selamat, soal berhasil dirubah!');
  347.                 redirect('teacher/quiz/questions_list/'.encode($quiz_name_ID));
  348.             }
  349.         }
  350.         else
  351.         {
  352.             $data['ID']     = $this->uri->segment(4);
  353.             $data['quiz']   = $quiz;
  354.             $this->template->view('update_quiz', $data);
  355.         }
  356.     }
  357.     else
  358.     {
  359.         $this->session->set_flashdata('failed', 'Maaf, soal tidak ditemukan');
  360.         redirect('teacher/quiz');
  361.     }
  362. }
  363.  
  364.  
  365. public function disable_quiz($id = NULL)
  366. {
  367.     if (!$id) redirect('teacher/quiz');
  368.  
  369.     if ($this->quiz_model->get_quiz_by_id(decode($id)))
  370.     {
  371.         $quiz = $this->quiz_model->get_quiz_by_id(decode($id));
  372.         $quiz_name_ID = $quiz->quiz_name_ID;
  373.  
  374.         $this->quiz_model->disable_quiz(decode($id));
  375.  
  376.         $this->session->set_flashdata('success', 'Selamat, soal berhasil dinonaktifkan!');
  377.         redirect('teacher/quiz/questions_list/'.encode($quiz_name_ID));
  378.     }
  379.     else
  380.     {
  381.         $this->session->set_flashdata('failed', 'Maaf, soal tidak ditemukan');
  382.         redirect('teacher/quiz');
  383.     }
  384. }
  385.  
  386. public function enable_quiz($id = NULL)
  387. {
  388.     if (!$id) redirect('teacher/quiz');
  389.  
  390.     if ($this->quiz_model->get_quiz_by_id(decode($id)))
  391.     {
  392.         $quiz = $this->quiz_model->get_quiz_by_id(decode($id));
  393.         $quiz_name_ID = $quiz->quiz_name_ID;
  394.  
  395.         $this->quiz_model->enable_quiz(decode($id));
  396.  
  397.         $this->session->set_flashdata('success', 'Selamat, soal berhasil diaktifkan!');
  398.         redirect('teacher/quiz/questions_list/'.encode($quiz_name_ID));
  399.     }
  400.     else
  401.     {
  402.         $this->session->set_flashdata('failed', 'Maaf, soal tidak ditemukan');
  403.         redirect('teacher/quiz');
  404.     }
  405. }
  406.  
  407.  
  408. public function delete_quiz($id = NULL)
  409. {
  410.     if (!$id) redirect('teacher/quiz');
  411.  
  412.     if ($this->quiz_model->get_quiz_by_id(decode($id)))
  413.     {
  414.         $quiz = $this->quiz_model->get_quiz_by_id(decode($id));
  415.         $quiz_name_ID = $quiz->quiz_name_ID;
  416.  
  417.         $this->quiz_model->delete_quiz($quiz->ID);
  418.  
  419.         $this->session->set_flashdata('success', 'Selamat, soal berhasil dihapus!');
  420.         redirect('teacher/quiz/questions_list/'.encode($quiz_name_ID));
  421.     }
  422.     else
  423.     {
  424.         $this->session->set_flashdata('failed', 'Maaf, soal tidak ditemukan');
  425.         redirect('teacher/quiz');
  426.     }
  427. }
  428.  
  429. public function review($encoded = NULL)
  430. {
  431.     if (!$encoded) redirect('admin/classroom');
  432.  
  433.     $decode     = decode($encoded);
  434.     $array_data = explode('/', $decode);
  435.  
  436.     $classroom_ID   = $array_data[0];
  437.     $student_ID     = $array_data[1];
  438.  
  439.     if (empty($classroom_ID) or empty($student_ID)) redirect('teacher/classroom');
  440.     $data['encoded']    = $encoded;
  441.     $data['student']    = $this->student_model->get_student_by_id($student_ID);
  442.     $data['classroom']  = $this->classroom_model->get_classroom_by_id($classroom_ID);
  443.     $data['multiple_choice']        = $this->quiz_model->get_multiple_choice_review($classroom_ID, $student_ID);
  444.     $data['essay']      = $this->quiz_model->get_essay_review($classroom_ID, $student_ID);
  445.     $data['total_mc_quiz'] = $this->quiz_model->count_mutiple_choice_by_cc($data['classroom']->code);
  446.     $data['total_essay_quiz'] = $this->quiz_model->count_essay_by_cc($data['classroom']->code);
  447.     $data['total_quiz'] = $data['total_mc_quiz'] + $data['total_essay_quiz'];
  448.     $data['correct_answer'] = $this->quiz_model->get_correct_answer_total($classroom_ID, $student_ID);
  449.     $data['quiz_name']  = $this->quiz_model->get_quiz_name_by_id($data['classroom']->quiz_name_ID);
  450.  
  451.     $this->template->view('review', $data);
  452. }
  453.  
  454. public function set_essay_score($encoded='')
  455. {
  456.     if (!$encoded) redirect('teacher/classroom');
  457.  
  458.     $decode     = decode($encoded);
  459.     $array_data = explode('/', $decode);
  460.  
  461.     $classroom_ID   = $array_data[0];
  462.     $student_ID     = $array_data[1];
  463.  
  464.     if (empty($classroom_ID) or empty($student_ID)) redirect('teacher/classroom');
  465.  
  466.     if ($this->input->post())
  467.     {
  468.         $id = $this->input->post('ID');
  469.         $answer_score = $this->input->post('answer_score');
  470.         $data = [];
  471.         foreach ($answer_score as $key => $value) {
  472.             $data[] = array(
  473.                 'ID' => $id[$key],
  474.                 'answer_score' => $answer_score[$key]
  475.             );
  476.         }
  477.  
  478.         $this->db->update_batch('quiz_answer',$data, 'ID');
  479.         $data['encoded'] = $encoded;
  480.         $data['essay'] = $this->quiz_model->get_essay_answer($classroom_ID, $student_ID)->result();
  481.  
  482.         $this->template->view('set_essay_score', $data);
  483.     }
  484.     else
  485.     {
  486.         $data['encoded'] = $encoded;
  487.         $data['essay'] = $this->quiz_model->get_essay_answer($classroom_ID, $student_ID)->result();
  488.  
  489.         $this->template->view('set_essay_score', $data);
  490.     }
  491.  
  492. }
  493.  
  494. public function set_all_essay_score($code='', $offset = '')
  495. {
  496.     if (!$code) redirect('teacher/classroom');
  497.  
  498.     $classroom = $this->classroom_model->get_classroom_by_code($code);
  499.     if ($classroom)
  500.     {
  501.         if ($this->input->post())
  502.         {
  503.             $id = $this->input->post('ID');
  504.             $answer_score = $this->input->post('answer_score');
  505.             $data = [];
  506.             foreach ($answer_score as $key => $value) {
  507.                 $data[] = array(
  508.                     'ID' => $id[$key],
  509.                     'answer_score' => $answer_score[$key]
  510.                 );
  511.             }
  512.  
  513.             $this->db->update_batch('quiz_answer',$data, 'ID');
  514.             redirect($this->input->post('current_url'));
  515.  
  516.         }
  517.  
  518.         $data['code'] = $code;
  519.         $data['classroom']  = $classroom;
  520.  
  521.         // count total student
  522.         $student_total = $this->quiz_model->get_essay_answer_student($classroom->ID);
  523.  
  524.         if (!$student_total) {
  525.             $this->session->set_flashdata('failed', 'Maaf, tidak ditemukan jawaban essai pada kelas ini');
  526.             redirect('teacher/classroom/check_code/'.$code);
  527.         }
  528.  
  529.         // get student id
  530.         $student_ID = $this->quiz_model->get_essay_by_paggination('1', $offset,$classroom->ID)->row()->ID;
  531.  
  532.         // get essay answer
  533.         $data['essay'] = $this->quiz_model->get_essay_answer($classroom->ID, $student_ID)->result();
  534.  
  535.         // parsing student data
  536.         $data['student']    = $this->student_model->get_student_by_id($student_ID);
  537.  
  538.         // config pagination
  539.         $url        = 'teacher/quiz/set_all_essay_score/'.$code;
  540.         $total_rows = $student_total;
  541.         $per_page   = 1;
  542.  
  543.         // set and parsing pagination
  544.         $data['page'] =  pagination($url, $total_rows, $per_page);
  545.  
  546.         $this->template->view('set_all_essay_score', $data);
  547.  
  548.     }
  549.     else
  550.     {
  551.         $this->session->set_flashdata('failed', 'Maaf, kode kelas tidak ditemukan!');
  552.         redirect('teacher/classroom');
  553.     }
  554.  
  555. }
  556.  
  557.  
  558.  
  559. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement