public function where($campos)
 {
     $sqlWhere = "";
     if (is_array($campos)) {
         foreach ($campos as $field) {
             if ($sqlWhere != "") {
                 $sqlWhere .= " AND ";
             }
             $sqlWhere .= $field['campo'] . ' ' . $field['operador'] . ' \'' . $field['valor'] . '\'';
         }
     }
     if ($sqlWhere != "") {
         $sqlWhere = " WHERE {$sqlWhere}";
     }
     $resultados = array();
     $consulta = $this->db->query("SELECT * FROM cursos_preguntas {$sqlWhere}");
     if ($consulta->num_rows() == 0) {
         return;
     } else {
         foreach ($consulta->result() as $fila) {
             $preg = new Md_pregunta();
             $resultados[] = $preg->find($fila->id);
         }
     }
     return $resultados;
 }
 public function curso_pregunta_importar_do()
 {
     $idCurso = $this->input->post("idCurso");
     $this->db->simple_query("DELETE FROM cursos_preguntas WHERE curso_id='{$idCurso}'");
     $this->load->model('md_pregunta');
     $config['upload_path'] = './uploads/';
     $config['allowed_types'] = 'xls|xlsx';
     $config['encrypt_name'] = true;
     $this->load->library('upload', $config);
     $archivo = "";
     if ($this->upload->do_upload()) {
         $data = array('upload_data' => $this->upload->data());
         $archivo = $data['upload_data']['file_name'];
     }
     if ($archivo != "") {
         $this->load->library('excel');
         $objPHPExcel = PHPExcel_IOFactory::load('./uploads/' . $archivo);
         for ($i = 0; $i < $objPHPExcel->getSheetCount() - 1; $i++) {
             $objPHPExcel->setActiveSheetIndex($i);
             $header = "";
             $arr_data = "";
             $cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
             foreach ($cell_collection as $cell) {
                 $column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
                 $row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
                 $data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
                 //header will/should be in row 1 only. of course this can be modified to suit your need.
                 if ($row == 1) {
                     $header[$row][$column] = $data_value;
                 } else {
                     $arr_data[$row][$column] = $data_value;
                 }
             }
             foreach ($arr_data as $fila) {
                 $pregunta = new Md_pregunta();
                 $pregunta->setCursoId($idCurso);
                 $pregunta->setPregunta($fila['B']);
                 $pregunta->setGrupo($objPHPExcel->getActiveSheet()->getTitle());
                 $pregunta->setRespuesta($fila['C']);
                 $pregunta->setUbicacion($fila['D']);
                 $pregunta->setRespuestaFalsa1($fila['F']);
                 $pregunta->setRespuestaFalsa2($fila['G']);
                 if (count($header[1]) == 10) {
                     $pregunta->setRespuestaFalsa3($fila['H']);
                 }
                 $pregunta->save();
             }
         }
     }
     redirect("admin/curso_preguntas/{$idCurso}");
 }