Exemplo n.º 1
0
 /**
  * 数据库备份
  */
 public function backup()
 {
     set_time_limit(0);
     $data = array();
     if ($_POST) {
         $backup_dir = BASEPATH . '../cache/backup/';
         if (!is_dir($backup_dir)) {
             mkdir($backup_dir, '0777', true);
         }
         include dirname(__FILE__) . '/../../config/config.db.php';
         $cfg = $db[$db['active_group']];
         $is_export_student = intval($this->input->post('is_export_student'));
         $sql_file = DbmanageModel::backupTables($cfg['database'], $backup_dir, array('*'), $is_export_student);
         if (file_exists($backup_dir . $sql_file)) {
             require_once APPPATH . 'libraries/Pclzip.php';
             $save_file = $backup_dir . "/zmte_database.zip";
             if (is_file($save_file)) {
                 @unlink($save_file);
             }
             $archive = new PclZip($save_file);
             //将文件进行压缩
             $archive->create($backup_dir . $sql_file, PCLZIP_OPT_REMOVE_ALL_PATH);
             @unlink($backup_dir . $sql_file);
             Func::dumpFile('application/zip', $save_file, 'zmte_database_' . date('YmdHis') . '.zip');
             @unlink($save_file);
             redirect('/admin/dbmanage/backup');
         } else {
             message('数据库备份失败,请稍后重试!');
         }
     } else {
         $this->load->view('dbmanage/backup', $data);
     }
 }
Exemplo n.º 2
0
    /**
     * 导入机构和校区记录(从excel文件中),
     * 注意: 目前$ti_id参数未启用
     *
     * @param   int     $ti_id = NULL   默认将校区全导入到该机构ID下
     */
    public function import_titc_excel($ti_id = NULL)
    {
        if ($_GET['dl'] == '1') {
            Func::dumpFile('application/vnd.ms-excel', 'file/import_training_campus_template.xls', '培训机构及校区导入模板.xls');
            exit;
        }
        $data = array();
        while (isset($_FILES['file'])) {
            $param = $_POST;
            $err_map = array(UPLOAD_ERR_OK => '没有错误发生,文件上传成功', UPLOAD_ERR_INI_SIZE => '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值', UPLOAD_ERR_FORM_SIZE => '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值', UPLOAD_ERR_PARTIAL => '文件只有部分被上传', UPLOAD_ERR_NO_FILE => '没有文件被上传', UPLOAD_ERR_NO_TMP_DIR => '找不到临时文件夹', UPLOAD_ERR_CANT_WRITE => '文件写入失败');
            if ($_FILES['file']['error'] !== 0) {
                $data['error'] = $err_map[$_FILES['file']['error']];
                break;
            }
            if (strpos($_FILES['file']['type'], 'excel') === false) {
                $mime = mime_content_type($_FILES['file']['tmp_name']);
                if (!in_array($mime, array('application/vnd.ms-excel', 'application/vnd.ms-office', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'))) {
                    $data['error'] = "您上传的不是Excel文件({$mime})";
                    break;
                }
            }
            // 开始处理excel
            $excel = PHPExcel_IOFactory::load($_FILES['file']['tmp_name']);
            $sheet = $excel->getSheet(0);
            $row_num = $sheet->getHighestRow();
            $col_num = $sheet->getHighestColumn();
            $title = array('机构名称', '机构类型', '机构省份', '机构城市', '机构区县', '机构地址', '优先级', '学员人数/年', '机构网址', '校区名称', '校区省份', '校区城市', '校区区县', '校区地址', '联系人', '联系电话');
            $col_num = base_convert($col_num, 36, 10);
            if ($col_num < count($title)) {
                $data['error'] = 'Excel列数验证未通过';
                break;
            }
            $col_num = count($title);
            $col_char = array();
            for ($j = 0; $j < $col_num; $j++) {
                $col_char[$j] = strtoupper(base_convert(10 + $j, 10, 36));
                if ($title[$j] !== trim($sheet->getCell($col_char[$j] . '1')->getValue())) {
                    $data['error'] = $col_char[$j] . '列标题不符';
                    break;
                }
            }
            if (isset($data['error'])) {
                break;
            }
            $rows = array();
            for ($i = 2; $i <= $row_num; $i++) {
                $rows[$i - 2] = array();
                for ($j = 0; $j < $col_num; $j++) {
                    $rows[$i - 2][$j] = trim($sheet->getCell($col_char[$j] . $i)->getValue());
                }
                if ($rows[$i - 2][0] == '') {
                    unset($rows[$i - 2]);
                    break;
                }
            }
            unset($sheet);
            unset($excel);
            if (empty($rows)) {
                $data['error'] = 'Excel文件工作表中没有任何要导入的记录';
                break;
            }
            $tit_map = array_flip(array_map("trim", TrainingInstitutionModel::trainingInstitutionTypePairs()));
            $tipt_map = array_flip(array_map("trim", TrainingInstitutionModel::trainingInstitutionPriTypePairs()));
            $sql = <<<EOT
SELECT * FROM rd_region WHERE region_id > 1 ORDER BY region_type
EOT;
            $region_list = Fn::db()->fetchAll($sql);
            // 以region_id为键以行记录为值,同时加个children,以保存直接子项
            $region_map = array();
            // 以名字加'_'连接起来为键,以region_id为值
            $regionname_map = array();
            foreach ($region_list as $v) {
                $region_map[$v['region_id']] = $v;
                $region_map[$v['region_id']]['children'] = array();
                if ($v['region_type'] == 1) {
                    $regionname_map[trim($v['region_name'])] = $v['region_id'];
                } else {
                    if ($v['region_type'] == 2) {
                        $region_map[$v['parent_id']]['children'][] = $v['region_id'];
                        $regionname_map[trim($region_map[$v['parent_id']]['region_name']) . '_' . trim($v['region_name'])] = $v['region_id'];
                    } else {
                        if ($v['region_type'] == 3) {
                            $region_map[$v['parent_id']]['children'][] = $v['region_id'];
                            $regionname_map[trim($region_map[$region_map[$v['parent_id']]['parent_id']]['region_name']) . '_' . trim($region_map[$v['parent_id']]['region_name']) . '_' . trim($v['region_name'])] = $v['region_id'];
                        }
                    }
                }
            }
            unset($region_list);
            $row_num = count($rows);
            $ti_arr = array();
            //ti_arr[ti_name] => ti_list[i]
            $tc_arr = array();
            //tc_arr[ti_name][tc_name] => tc_list[i]
            $ti_list = array();
            $tc_list = array();
            foreach ($rows as $k => $row) {
                // 机构名称
                if (mb_strlen($row[0], 'UTF-8') > 60) {
                    $data['error'] = $col_char[0] . ($k + 2) . ' - "机构名称"内容太长了,不可超过60个字符';
                    break;
                }
                $row['ti_name'] = $row[0];
                // 机构类型
                if ($row[1] == '') {
                    $data['error'] = $col_char[1] . ($k + 2) . ' - "机构类型"内容不可为空';
                    break;
                }
                // 机构类型
                if (!isset($tit_map[$row[1]])) {
                    $data['error'] = $col_char[1] . ($k + 2) . ' - "机构类型"不正确';
                    break;
                }
                $row['ti_typeid'] = $tit_map[$row[1]];
                if ($row[2] == '') {
                    if ($param['ti_provid_required']) {
                        $data['error'] = $col_char[2] . ($k + 2) . ' - "机构省份"不可为空';
                        break;
                    } else {
                        $row['ti_provid'] = 0;
                    }
                } else {
                    if (!isset($regionname_map[$row[2]])) {
                        $data['error'] = $col_char[2] . ($k + 2) . ' - "机构省份"不存在';
                        break;
                    }
                    $row['ti_provid'] = $regionname_map[$row[2]];
                }
                $row['ti_cityid'] = 0;
                $row['ti_areaid'] = 0;
                if (!empty($region_map[$regionname_map[$row[2]]]['children'])) {
                    // 验证市
                    if ($row[3] == '') {
                        if ($param['ti_cityid_required']) {
                            $data['error'] = $col_char[3] . ($k + 2) . ' - "机构城市"不可为空';
                            break;
                        } else {
                            $row['ti_cityid'] = 0;
                        }
                    } else {
                        if (!isset($regionname_map[$row[2] . '_' . $row[3]])) {
                            $data['error'] = $col_char[3] . ($k + 2) . ' - "机构城市"不存在';
                            break;
                        }
                        $row['ti_cityid'] = $regionname_map[$row[2] . '_' . $row[3]];
                    }
                    // 验证区县
                    if ($row[4] == '') {
                        if ($param['ti_areaid_required']) {
                            $data['error'] = $col_char[4] . ($k + 2) . ' - "机构区县"不可为空';
                            break;
                        } else {
                            $row['ti_areaid'] = 0;
                        }
                    } else {
                        if (!isset($regionname_map[$row[2] . '_' . $row[3] . '_' . $row[4]])) {
                            $data['error'] = $col_char[4] . ($k + 2) . ' - "机构区县"不存在';
                            break;
                        }
                        $row['ti_areaid'] = $regionname_map[$row[2] . '_' . $row[3] . '_' . $row[4]];
                    }
                }
                // 机构地址
                if ($row[5] == '') {
                    if ($param['ti_addr_required']) {
                        $data['error'] = $col_char[5] . ($k + 2) . ' - "机构地址"不可为空';
                        break;
                    }
                }
                if (mb_strlen($row[5], 'UTF-8') > 255) {
                    $data['error'] = $col_char[5] . ($k + 2) . ' - "机构地址"内容太长了';
                    break;
                }
                $row['ti_addr'] = $row[5];
                // 优先级
                if ($row[6] == '') {
                    if ($param['ti_priid_required']) {
                        $data['error'] = $col_char[6] . ($k + 2) . ' - "优先级"不能为空';
                        break;
                    } else {
                        $row[6] = '一般';
                    }
                }
                if (!isset($tipt_map[$row[6]])) {
                    $data['error'] = $col_char[6] . ($k + 2) . ' - "优先级"不正确';
                    break;
                }
                $row['ti_priid'] = $tipt_map[$row[6]];
                if ($row[7] == '') {
                    if ($param['ti_stumax_required']) {
                        $data['error'] = $col_char[6] . ($k + 2) . ' - "学员人数/年"不能为空';
                        break;
                    } else {
                        $row[7] = '0';
                    }
                }
                // 学员人数/年
                if (!Validate::isInt($row[7]) || $row[7] < 0) {
                    $data['error'] = $col_char[7] . ($k + 2) . ' - "学员人数/年"必须为正整数';
                    break;
                }
                $row['ti_stumax'] = $row[7];
                // 机构网址
                if (mb_strlen($row[8], 'UTF-8') > 512) {
                    $data['error'] = $col_char[8] . ($k + 2) . ' - "机构网址"内容太长了';
                    break;
                }
                $row['ti_url'] = $row[8];
                // 校区名称
                if ($row[9] == '') {
                    $data['error'] = $col_char[9] . ($k + 2) . ' - "校区名称"不可为空';
                    break;
                }
                if (mb_strlen($row[9], 'UTF-8') > 60) {
                    $data['error'] = $col_char[9] . ($k + 2) . ' - "校区名称"太长了';
                    break;
                }
                $row['tc_name'] = $row[9];
                if ($row[10] == '') {
                    if ($param['tc_provid_required']) {
                        $data['error'] = $col_char[10] . ($k + 2) . ' - "校区省份"不可为空';
                        break;
                    } else {
                        $row['tc_provid'] = 0;
                    }
                } else {
                    if (!isset($regionname_map[$row[10]])) {
                        $data['error'] = $col_char[10] . ($k + 2) . ' - "校区省份"不存在';
                        break;
                    }
                    $row['tc_provid'] = $regionname_map[$row[10]];
                }
                $row['tc_cityid'] = 0;
                $row['tc_areaid'] = 0;
                if (!empty($region_map[$regionname_map[$row[10]]]['children'])) {
                    // 验证市
                    if ($row[11] == '') {
                        if ($param['tc_cityid_required']) {
                            $data['error'] = $col_char[11] . ($k + 2) . ' - "校区城市"不可为空';
                            break;
                        } else {
                            $row['tc_cityid'] = 0;
                        }
                    } else {
                        if (!isset($regionname_map[$row[10] . '_' . $row[11]])) {
                            $data['error'] = $col_char[11] . ($k + 2) . ' - "校区城市"不存在';
                            break;
                        }
                        $row['tc_cityid'] = $regionname_map[$row[10] . '_' . $row[11]];
                    }
                    // 验证区县
                    if ($row[12] == '') {
                        if ($param['tc_areaid_required']) {
                            $data['error'] = $col_char[12] . ($k + 2) . ' - "校区区县"不可为空';
                            break;
                        } else {
                            $row[12] = 0;
                        }
                    } else {
                        if (!isset($regionname_map[$row[10] . '_' . $row[11] . '_' . $row[12]])) {
                            $data['error'] = $col_char[12] . ($k + 2) . ' - "校区区县"不存在';
                            break;
                        }
                        $row['tc_areaid'] = $regionname_map[$row[10] . '_' . $row[11] . '_' . $row[12]];
                    }
                }
                // 校区地址
                if ($row[13] == '') {
                    if ($param['tc_ctcaddr_required']) {
                        $data['error'] = $col_char[13] . ($k + 2) . ' - "校区地址"不可为空';
                        break;
                    }
                }
                if (mb_strlen($row[13], 'UTF-8') > 255) {
                    $data['error'] = $col_char[13] . ($k + 2) . ' - "校区地址"太长了';
                    break;
                }
                $row['tc_ctcaddr'] = $row[13];
                // 联系人
                if (mb_strlen($row[14], 'UTF-8') > 60) {
                    $data['error'] = $col_char[14] . ($k + 2) . ' - "联系人"太长了';
                    break;
                }
                $row['tc_ctcperson'] = $row[14];
                // 联系电话
                if ($row['15'] == '') {
                    if ($param['tc_ctcphone_required']) {
                        $data['error'] = $col_char[15] . ($k + 2) . ' - "联系电话"不能为空';
                        break;
                    }
                }
                if (mb_strlen($row[15], 'UTF-8') > 120) {
                    $data['error'] = $col_char[15] . ($k + 2) . ' - "联系电话"太长了';
                    break;
                }
                $row['tc_ctcphone'] = $row[15];
                if (!isset($ti_arr[$row[0]])) {
                    $ti_arr[$row[0]] = count($ti_list);
                    $ti_list[] = array('index' => $k + 2, 'ti_id' => 0, 'ti_name' => $row[0], 'ti_typeid' => $row['ti_typeid'], 'ti_provid' => $row['ti_provid'], 'ti_cityid' => $row['ti_cityid'], 'ti_areaid' => $row['ti_areaid'], 'ti_addr' => $row[5], 'ti_priid' => $row['ti_priid'], 'ti_stumax' => $row[7], 'ti_url' => $row[8]);
                }
                if (!isset($tc_arr[$row[0]])) {
                    $tc_arr[$row[0]] = array();
                }
                if (isset($tc_arr[$row[0]][$row[9]])) {
                    $data['error'] = $col_char[9] . ($k + 2) . ' - "校区名称"同一机构内有重复';
                    break;
                }
                $tc_arr[$row[0]][$row[9]] = count($tc_list);
                $tc_list[] = array('index' => $k + 2, 'ti_name' => $row[0], 'tc_name' => $row[9], 'tc_provid' => $row['tc_provid'], 'tc_cityid' => $row['tc_cityid'], 'tc_areaid' => $row['tc_areaid'], 'tc_ctcaddr' => $row[13], 'tc_ctcperson' => $row[14], 'tc_ctcphone' => $row[15]);
            }
            if (isset($data['error'])) {
                break;
            }
            unset($region_map);
            unset($regionname_map);
            unset($tit_map);
            unset($tipt_map);
            unset($rows);
            // 这里开始导入
            try {
                $db = Fn::db();
                if (!$db->beginTransaction()) {
                    throw new Exception('开始导入事务处理失败');
                }
                $time = time();
                $adduid = Fn::sess()->userdata('admin_id');
                // 导入机构
                $sql1 = <<<EOT
SELECT ti_id FROM t_training_institution WHERE ti_name = ?
EOT;
                $sql2 = <<<EOT
SELECT tc_id FROM t_training_campus WHERE tc_tiid = ? AND tc_name = ?
EOT;
                $sql3 = <<<EOT
UPDATE t_training_institution SET ti_campusnum = ti_campusnum + 1 
WHERE ti_id = 
EOT;
                $tc_update = 0;
                $tc_insert = 0;
                $ti_update = 0;
                $ti_insert = 0;
                foreach ($ti_list as $k => $row) {
                    $ti_id = $db->fetchOne($sql1, array($row['ti_name']));
                    if ($ti_id) {
                        $ti_list[$k]['ti_id'] = $ti_id;
                        // update
                        $row2 = array();
                        if ($param['same_tiname_update_ti_typeid']) {
                            $row2['ti_typeid'] = $row['ti_typeid'];
                        }
                        if ($param['same_tiname_update_ti_region'] && intval($row['ti_provid']) != 0) {
                            $row2['ti_provid'] = $row['ti_provid'];
                            $row2['ti_cityid'] = $row['ti_provid'];
                            $row2['ti_areaid'] = $row['ti_areaid'];
                        }
                        if ($param['same_tiname_update_ti_addr'] && $row['ti_addr'] != '') {
                            $row2['ti_addr'] = $row['ti_addr'];
                        }
                        if ($param['same_tiname_update_ti_priid']) {
                            $row2['ti_priid'] = $row['ti_priid'];
                        }
                        if ($param['same_tiname_update_ti_stumax'] && intval($row['ti_stumax']) != '0') {
                            $row2['ti_stumax'] = $row['ti_stumax'];
                        }
                        if ($param['same_tiname_update_ti_url'] && $row['ti_url'] != '') {
                            $row2['ti_url'] = $row['ti_url'];
                        }
                        if ($row2) {
                            $db->update('t_training_institution', $row2, 'ti_id = ' . $ti_id);
                            $ti_update++;
                        }
                    } else {
                        // insert
                        unset($row['index']);
                        unset($row['ti_id']);
                        $row['ti_flag'] = $time;
                        if ($row['ti_addr'] == '') {
                            $row['ti_addr'] = NULL;
                        }
                        if ($row['ti_url'] == '') {
                            $row['ti_url'] = NULL;
                        }
                        $row['ti_addtime'] = date('Y-m-d H:i:s', $time);
                        $row['ti_adduid'] = $adduid;
                        $db->insert('t_training_institution', $row);
                        $ti_list[$k]['ti_id'] = $db->lastInsertId('t_training_institution', 'ti_id');
                        $ti_insert++;
                    }
                }
                // 导入校区
                foreach ($tc_list as $k => $row) {
                    $ti_id = $ti_list[$ti_arr[$row['ti_name']]]['ti_id'];
                    $tc_list[$k]['ti_id'] = $ti_id;
                    $tc_id = $db->fetchOne($sql2, array($ti_id, $row['tc_name']));
                    if ($tc_id) {
                        // update
                        $tc_list[$k]['tc_id'] = $tc_id;
                        $row2 = array();
                        if ($param['same_tcname_update_tc_region'] && intval($row['tc_provid']) != 0) {
                            $row2['tc_provid'] = $row['tc_provid'];
                            $row2['tc_cityid'] = $row['tc_provid'];
                            $row2['tc_areaid'] = $row['tc_areaid'];
                        }
                        if ($param['same_tcname_update_tc_ctcaddr'] && $row['tc_ctcaddr'] != '') {
                            $row2['tc_ctcaddr'] = $row['tc_ctcaddr'];
                        }
                        if ($param['same_tcname_update_tc_ctcperson'] && $row['tc_ctcperson'] != '') {
                            $row2['tc_ctcperson'] = $row['tc_ctcperson'];
                        }
                        if ($param['same_tcname_update_tc_ctcphone'] && $row['tc_ctcphone'] != '') {
                            $row2['tc_ctcphone'] = $row['tc_ctcphone'];
                        }
                        if ($row2) {
                            $db->update('t_training_campus', $row2, 'tc_id = ' . $tc_id);
                            $tc_update++;
                        }
                    } else {
                        // insert
                        unset($row['index']);
                        unset($row['ti_name']);
                        $row['tc_tiid'] = $ti_id;
                        $row['tc_flag'] = $time;
                        $row['tc_environ'] = 3;
                        $row['tc_addtime'] = date('Y-m-d H:i:s', $time);
                        $row['tc_adduid'] = $adduid;
                        if ($row['tc_ctcperson'] == '') {
                            $row['tc_ctcperson'] = NULL;
                        }
                        $db->insert('t_training_campus', $row);
                        $tc_list[$k]['tc_id'] = $db->lastInsertId('t_training_campus', 'tc_id');
                        $db->exec($sql3 . $ti_id);
                        $tc_insert++;
                    }
                }
                if ($db->commit()) {
                    $data['success'] = <<<EOT
导入Excel文件({$_FILES['file']['name']})成功,共更新{$ti_update}条机构记录, 插入{$ti_insert}条机构记录, 更新{$tc_update}条校区记录, 插入{$tc_insert}条校区记录
EOT;
                    admin_log('import', '', "培训机构和校区 " . $data['success']);
                } else {
                    $err = $db->errorInfo()[2];
                    $db->rollBack();
                    throw new Exception($err);
                }
            } catch (Exception $e) {
                $data['error'] = $e->getMessage();
            }
            break;
        }
        if (!isset($_FILES['file'])) {
            $param = array('ti_provid_required' => 1, 'ti_cityid_required' => 2, 'ti_areaid_required' => 3, 'ti_addr_required' => 1, 'ti_priid_required' => 1, 'ti_stumax_required' => 1, 'tc_provid_required' => 1, 'tc_cityid_required' => 2, 'tc_areaid_required' => 3, 'tc_ctcaddr_required' => 1, 'tc_ctcphone_required' => 1, 'same_tiname_update_ti_typeid' => 1, 'same_tiname_update_ti_region' => 1, 'same_tiname_update_ti_addr' => 1, 'same_tiname_update_ti_priid' => 1, 'same_tiname_update_ti_stumax' => 1, 'same_tiname_update_ti_url' => 1, 'same_tcname_update_tc_region' => 1, 'same_tcname_update_tc_ctcaddr' => 1, 'same_tcname_update_tc_ctcperson' => 1, 'same_tcname_update_tc_ctcphone' => 1);
            // 同名校区更新联系电话
        }
        $data['param'] = $param;
        $this->load->view('traininginstitution/import_titc_excel', $data);
    }
Exemplo n.º 3
0
 /**
  * 下载班级报告
  */
 public function down_teacher_report($rule_id = 0)
 {
     $rule_id = intval($rule_id);
     $rule_id && ($rule = EvaluateRuleModel::get_evaluate_rule($rule_id));
     if (empty($rule)) {
         message('不存在该评估规则');
     }
     $save_file = realpath(dirname(APPPATH)) . "/cache/down_teacher_report/" . $rule_id . '.zip';
     if (!file_exists($save_file)) {
         $teacher_list = TeacherStudentModel::examTeacherList($rule['exam_pid']);
         if (!$teacher_list) {
             message('当前评估规则没有关联教师,无法下载教师报告!');
         }
         $dir_name = realpath(dirname(APPPATH)) . "/cache/down_teacher_report/" . $rule_id;
         if (!is_dir($dir_name)) {
             @mkdir($dir_name, 0777, true);
         }
         $pdf_dir = C('html2pdf_path') . '/zeming/report/';
         $pdf_ready = false;
         foreach ($teacher_list as $ct_id => $item) {
             $dir = $pdf_dir . "{$rule_id}/teacher_{$ct_id}";
             if (!is_dir($dir)) {
                 continue;
             }
             $pdf_ready = true;
             $teacher_name = $item['ct_name'];
             $f = @dir($dir);
             if ($f) {
                 while (false !== ($entry = $f->read())) {
                     if ($entry != '.' && $entry != '..') {
                         @copy($dir . '/' . $entry, $dir_name . '/' . $teacher_name . '_' . $entry);
                     }
                 }
             }
         }
         if ($pdf_ready) {
             require_once APPPATH . 'libraries/Pclzip.php';
             $archive = new PclZip($save_file);
             //将文件进行压缩
             $archive->create($dir_name, PCLZIP_OPT_REMOVE_PATH, realpath(dirname(APPPATH)) . "/cache/down_teacher_report");
             $this->rm_dir($dir_name);
         } else {
             message('教师报告PDF文件还未生成,无法下载!');
         }
     }
     if (file_exists($save_file)) {
         $exam_name = ExamModel::get_exam($rule['exam_pid'], 'exam_name');
         $subject_name = '';
         if ($rule['subject_id'] > 0) {
             $subject_name = $this->_subject_name($rule['exam_pid'], $rule['subject_id']);
         }
         $filename = $exam_name . $subject_name . '教师报告';
         Func::dumpFile('application/zip', $save_file, $filename . '.zip');
     }
 }
Exemplo n.º 4
0
    /**
     * 导入
     */
    public function import($exam_pid = 0)
    {
        if ($_GET['dl'] == '1') {
            Func::dumpFile('application/vnd.ms-excel', 'file/import_teacher_stundent_template.xlsx', '师生关联模板.xlsx');
            exit;
        }
        if (!$exam_pid) {
            message('参数错误');
        }
        $data = array();
        while (isset($_FILES['file'])) {
            $param = $_POST;
            $col_char = array();
            $rows = Excel::readSimpleUploadFile2($_FILES['file']);
            if (!is_array($rows)) {
                $data['error'] = $rows;
                break;
            }
            $subject_map = array_flip(C('subject'));
            $db = Fn::db();
            $exam_ticket_maprule = ExamModel::get_exam($exam_pid, 'exam_ticket_maprule');
            $sql = "SELECT subject_id, exam_id FROM rd_exam\n                    WHERE exam_pid = {$exam_pid}";
            $subject_exam = $db->fetchPairs($sql);
            if (!is_array($subject_exam)) {
                $data['error'] = '考试期次没有考试学科';
                break;
            }
            $exam_subjectid = array_keys($subject_exam);
            $list = array();
            $subject_key = array();
            foreach ($rows as $k => $row) {
                if ($k == 0) {
                    for ($i = 2; $i <= count($row); $i++) {
                        $subject_id = $subject_map[str_replace("'", "", trim($row[$i]))];
                        if ($subject_id && in_array($subject_id, $exam_subjectid)) {
                            $subject_key[$i] = $subject_id;
                        }
                    }
                } else {
                    $student = array();
                    for ($i = 1; $i <= count($row); $i++) {
                        if ($i == 1) {
                            $exam_ticket = trim($row[$i]);
                            if (!$exam_ticket) {
                                break;
                            }
                            $exam_ticket = exam_ticket_maprule_encode($exam_ticket, $exam_ticket_maprule);
                            $sql = "SELECT uid, school_id FROM rd_student\n                                    WHERE exam_ticket = '{$exam_ticket}'";
                            $student = $db->fetchRow($sql);
                            if (!$student) {
                                break;
                            }
                            $list[$student['uid']]['uid'] = $student['uid'];
                        } else {
                            $ct_name = str_replace("'", "", trim($row[$i]));
                            if (!$ct_name) {
                                continue;
                            }
                            $sql = "SELECT ct_id FROM t_cteacher \n                                    LEFT JOIN t_cteacher_school ON scht_ctid = ct_id\n                                    WHERE scht_schid = {$student['school_id']} \n                                    AND ct_name = '{$ct_name}'";
                            $ct_id = $db->fetchOne($sql);
                            if (!$ct_id) {
                                continue;
                            }
                            $list[$student['uid']]['teacher'][$subject_key[$i]] = $ct_id;
                        }
                    }
                }
            }
            try {
                if (!$db->beginTransaction()) {
                    throw new Exception('开始导入事务处理失败');
                }
                $insert = 0;
                // 导入教师
                foreach ($list as $uid => $row) {
                    foreach ($row['teacher'] as $subject_id => $ct_id) {
                        $bind = array('tstu_ctid' => $ct_id, 'tstu_stuid' => $uid, 'tstu_exampid' => $exam_pid, 'tstu_examid' => $subject_exam[$subject_id], 'tstu_subjectid' => $subject_id);
                        TeacherStudentModel::addTeacherStudent($bind);
                        $insert++;
                    }
                }
                if ($db->commit()) {
                    $data['success'] = <<<EOT
导入Excel文件({$_FILES['file']['name']})成功,共插入{$insert}条师生记录
EOT;
                    admin_log('import', 'teacher_student', $data['success']);
                } else {
                    $err = $db->errorInfo()[2];
                    $db->rollBack();
                    throw new Exception($err);
                }
            } catch (Exception $e) {
                $data['error'] = $e->getMessage();
            }
            break;
        }
        $data['exam_pid'] = $exam_pid;
        $data['param'] = $param;
        $this->load->view('teacher_student/import', $data);
    }
Exemplo n.º 5
0
    /**
     * 导入课程记录(从excel文件中),
     * 注意: 目前$cors_id参数未启用
     *
     * @param   int     $cors_id = NULL   默认将课程全导入到该课程ID下
     */
    public function import_cors_excel($cors_id = NULL)
    {
        if ($_GET['dl'] == '1') {
            Func::dumpFile('application/vnd.ms-excel', 'file/import_course_template.xls', '培训课程导入模板.xls');
            exit;
        }
        $data = array();
        while (isset($_FILES['file'])) {
            $param = $_POST;
            $err_map = array(UPLOAD_ERR_OK => '没有错误发生,文件上传成功', UPLOAD_ERR_INI_SIZE => '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值', UPLOAD_ERR_FORM_SIZE => '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值', UPLOAD_ERR_PARTIAL => '文件只有部分被上传', UPLOAD_ERR_NO_FILE => '没有文件被上传', UPLOAD_ERR_NO_TMP_DIR => '找不到临时文件夹', UPLOAD_ERR_CANT_WRITE => '文件写入失败');
            if ($_FILES['file']['error'] !== 0) {
                $data['error'] = $err_map[$_FILES['file']['error']];
                break;
            }
            if (strpos($_FILES['file']['type'], 'excel') === false) {
                $mime = mime_content_type($_FILES['file']['tmp_name']);
                if (!in_array($mime, array('application/vnd.ms-excel', 'application/vnd.ms-office', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'))) {
                    $data['error'] = "您上传的不是Excel文件({$mime})";
                    break;
                }
            }
            // 开始处理excel
            $excel = PHPExcel_IOFactory::load($_FILES['file']['tmp_name']);
            $sheet = $excel->getSheet(0);
            $row_num = $sheet->getHighestRow();
            $col_num = $sheet->getHighestColumn();
            $title = array('课程名称', '来源机构', '授课模式', '年级', '学科', '类型', '授课教师', '教师来源', '课程时间', '课程周期', '上课校区', '上课省份', '上课城市', '上课区县', '上课地址', '收费', '上课课时', '联系人', '联系电话', '课程简介', '网址');
            $col_num = base_convert($col_num, 36, 10);
            if ($col_num < count($title)) {
                $data['error'] = 'Excel列数验证未通过';
                break;
            }
            $col_num = count($title);
            $col_char = array();
            for ($j = 0; $j < $col_num; $j++) {
                $col_char[$j] = strtoupper(base_convert(10 + $j, 10, 36));
                if ($title[$j] !== trim($sheet->getCell($col_char[$j] . '1')->getValue())) {
                    $data['error'] = $col_char[$j] . '列标题不符';
                    break;
                }
            }
            if (isset($data['error'])) {
                break;
            }
            $rows = array();
            for ($i = 2; $i <= $row_num; $i++) {
                $rows[$i - 2] = array();
                for ($j = 0; $j < $col_num; $j++) {
                    $rows[$i - 2][$j] = trim($sheet->getCell($col_char[$j] . $i)->getValue());
                }
                if ($rows[$i - 2][0] == '') {
                    unset($rows[$i - 2]);
                    break;
                }
            }
            unset($sheet);
            unset($excel);
            if (empty($rows)) {
                $data['error'] = 'Excel文件工作表中没有任何要导入的记录';
                break;
            }
            $cm_map = array_flip(array_map("trim", CourseModel::courseModePairs()));
            $grade_map = array_flip(C('grades'));
            $subject_map = array_flip(C('subject'));
            $ctf_map = array_flip(array_map("trim", CourseModel::courseTeachfromPairs()));
            $sql = <<<EOT
SELECT class_id, class_name FROM rd_question_class
EOT;
            $class_map = Fn::db()->fetchPairs($sql);
            if (!is_array($class_map)) {
                $class_map = array();
            } else {
                $class_map = array_flip(array_map("trim", $class_map));
            }
            $sql = <<<EOT
SELECT * FROM rd_region WHERE region_id > 1 ORDER BY region_type
EOT;
            $region_list = Fn::db()->fetchAll($sql);
            // 以region_id为键以行记录为值,同时加个children,以保存直接子项
            $region_map = array();
            // 以名字加'_'连接起来为键,以region_id为值
            $regionname_map = array();
            foreach ($region_list as $v) {
                $region_map[$v['region_id']] = $v;
                $region_map[$v['region_id']]['children'] = array();
                if ($v['region_type'] == 1) {
                    $regionname_map[trim($v['region_name'])] = $v['region_id'];
                } else {
                    if ($v['region_type'] == 2) {
                        $region_map[$v['parent_id']]['children'][] = $v['region_id'];
                        $regionname_map[trim($region_map[$v['parent_id']]['region_name']) . '_' . trim($v['region_name'])] = $v['region_id'];
                    } else {
                        if ($v['region_type'] == 3) {
                            $region_map[$v['parent_id']]['children'][] = $v['region_id'];
                            $regionname_map[trim($region_map[$region_map[$v['parent_id']]['parent_id']]['region_name']) . '_' . trim($region_map[$v['parent_id']]['region_name']) . '_' . trim($v['region_name'])] = $v['region_id'];
                        }
                    }
                }
            }
            unset($region_list);
            $row_num = count($rows);
            $cors_arr = array();
            //cors_arr[cors_name] => cors_list[i]
            $cc_arr = array();
            //cc_arr[ti_name][tc_name] => cc_list[i]
            $cors_list = array();
            $cc_list = array();
            foreach ($rows as $k => $row) {
                // 课程名称
                if ($row[0] == '') {
                    $data['error'] == $col_char[0] . ($k + 2) . ' - "课程名称"不可为空';
                    break;
                }
                if (mb_strlen($row[0], 'UTF-8') > 100) {
                    $data['error'] = $col_char[0] . ($k + 2) . ' - "课程名称"内容太长了,不可超过100个字符';
                    break;
                }
                // 来源机构
                if ($row[1] == '') {
                    $data['error'] = $col_char[1] . ($k + 2) . ' - "来源机构"不可为空';
                    break;
                }
                if (mb_strlen($row[1], 'UTF-8') > 60) {
                    $data['error'] = $col_char[1] . ($k + 2) . ' - "来源机构"内容太长了,不可超过60个字符';
                    break;
                }
                // 授课模式
                if ($row[2] == '') {
                    $data['error'] = $col_char[2] . ($k + 2) . ' - "授课模式"不能为空';
                    break;
                }
                if (!isset($cm_map[$row[2]])) {
                    $data['error'] = $col_char[2] . ($k + 2) . ' - "授课模式"不正确';
                    break;
                }
                $row['cors_cmid'] = $cm_map[$row[2]];
                ////////////
                //   年级3 学科4 类型5 授课教师6 教师来源7 课程时间8
                //   课程周期9 上课校区10
                //////////////////
                // 年级
                if ($row[3] == '') {
                    $data['error'] = $col_char[3] . ($k + 2) . ' - "年级"不能为空';
                    break;
                }
                $row['cors_gradeid'] = array();
                $arr = explode(',', $row[3]);
                foreach ($arr as $v) {
                    $v = trim($v);
                    if (isset($grade_map[$v])) {
                        $row['cors_gradeid'][] = $grade_map[$v];
                    }
                }
                if (count($arr) != count($row['cors_gradeid'])) {
                    $data['error'] = $col_char[3] . ($k + 2) . ' - "年级"里有不正确的选项';
                    break;
                }
                if (empty($row['cors_gradeid'])) {
                    $data['error'] = $col_char[3] . ($k + 2) . ' - "年级"不能为空';
                    break;
                }
                $row['cors_gradeid'] = array_unique($row['cors_gradeid']);
                if ($row['cors_cmid'] != 1 && count($row['cors_gradeid']) > 1) {
                    $data['error'] = $col_char[3] . ($k + 2) . ' - 一对一课程"年级"只能有一个';
                    break;
                }
                // 学科
                if ($row[4] == '') {
                    $data['error'] = $col_char[4] . ($k + 2) . ' - "学科"不能为空';
                    break;
                }
                $row['cors_subjectid'] = array();
                $arr = explode(',', $row[4]);
                foreach ($arr as $v) {
                    $v = trim($v);
                    if (isset($subject_map[$v])) {
                        $row['cors_subjectid'][] = $subject_map[$v];
                    }
                }
                if (count($arr) != count($row['cors_subjectid'])) {
                    $data['error'] = $col_char[4] . ($k + 2) . ' - "学科"里有不正确的选项空';
                    break;
                }
                if (empty($row['cors_subjectid'])) {
                    $data['error'] = $col_char[4] . ($k + 2) . ' - "学科"不能为空';
                    break;
                }
                $row['cors_subjectid'] = array_unique($row['cors_subjectid']);
                if ($row['cors_cmid'] != 1 && count($row['cors_subjectid']) > 1) {
                    $data['error'] = $col_char[3] . ($k + 2) . ' - 一对一课程"学科"只能有一个';
                    break;
                }
                // 类型
                if ($row[5] == '') {
                    if ($param['cors_classid_required']) {
                        $data['error'] = $col_char[5] . ($k + 2) . ' - "类型"不能为空';
                        break;
                    }
                    $row['cors_classid'] = array();
                } else {
                    $row['cors_classid'] = array();
                    $arr = explode(',', $row[5]);
                    foreach ($arr as $v) {
                        $v = trim($v);
                        if (isset($class_map[$v])) {
                            $row['cors_classid'][] = $class_map[$v];
                        }
                    }
                    if (count($arr) != count($row['cors_classid'])) {
                        $data['error'] = $col_char[5] . ($k + 2) . ' - "类型"里有不正确的选项空';
                        break;
                    }
                    if (empty($row['cors_classid'])) {
                        if ($param['cors_classid_required']) {
                            $data['error'] = $col_char[5] . ($k + 2) . ' - "类型"不能为空';
                            break;
                        }
                    }
                    $row['cors_classid'] = array_unique($row['cors_classid']);
                }
                // TODO 需要验证
                $row['cc_teachers'] = $row[6];
                // 教师来源
                if ($row[7] == '') {
                    $data['error'] = $col_char[7] . ($k + 2) . ' - "教师来源"不能为空';
                    break;
                }
                if (!isset($ctf_map[$row[7]])) {
                    $data['error'] = $col_char[7] . ($k + 2) . ' - "教师来源"不正确';
                    break;
                }
                $row['cc_ctfid'] = $ctf_map[$row[7]];
                // 课程时间
                if ($row[8] == '') {
                    if ($param['cc_classtime_required']) {
                        $data['error'] = $col_char[8] . ($k + 2) . ' - "课程时间"不能为空';
                        break;
                    }
                }
                if (mb_strlen($row[8], 'UTF-8') > 255) {
                    $data['error'] = $col_char[8] . ($k + 2) . ' - "课程时间"内容太长了,不可超过255个字符';
                    break;
                }
                // 课程周期
                if ($row[9] == '') {
                    // 任意时间开课
                    $row['cc_startanytime'] = 1;
                    $row['cc_begindate'] = NULL;
                    $row['cc_enddate'] = NULL;
                } else {
                    $row['cc_startanytime'] = 0;
                    if (strlen($row[9]) < 21) {
                        $data['error'] = $col_char[9] . ($k + 2) . ' - "课程周期"格式不正确,应为"2015-02-04x2015-08-01",其中x可为一个或多个字符串';
                        break;
                    }
                    $d1 = substr($row[9], 0, 10);
                    $d2 = substr($row[9], -10, 10);
                    if (!Validate::isDate($d1) || !Validate::isDate($d2)) {
                        $data['error'] = $col_char[9] . ($k + 2) . ' - "课程周期"格式不正确,应为"2015-02-04x2015-08-01",其中x可为一个或多个字符串';
                        break;
                    }
                    if ($d1 > $d2) {
                        $data['error'] = $col_char[9] . ($k + 2) . ' - "课程周期"开始日期不能大于结束日期';
                        break;
                    }
                    $row['cc_begindate'] = $d1;
                    $row['cc_enddate'] = $d2;
                }
                // 上课校区
                if ($row['cors_cmid'] == 1) {
                    // 一对一
                    // 上课校区
                    if ($row[10] == '') {
                        $row['tc_name'] = NULL;
                    } else {
                        if (mb_strlen($row[10], 'UTF-8') > 60) {
                            $data['error'] = $col_char[10] . ($k + 2) . ' - "上课校区"太长了';
                            break;
                        }
                        $row['tc_name'] = $row[10];
                    }
                } else {
                    // 上课校区
                    if ($row[10] == '') {
                        $data['error'] = $col_char[10] . ($k + 2) . ' - "上课校区"不可为空';
                        break;
                    }
                    if (mb_strlen($row[10], 'UTF-8') > 60) {
                        $data['error'] = $col_char[10] . ($k + 2) . ' - "上课校区"太长了';
                        break;
                    }
                    $row['tc_name'] = $row[10];
                }
                // 上课省份
                if ($row[11] == '') {
                    if ($param['cc_provid_required']) {
                        $data['error'] = $col_char[11] . ($k + 2) . ' - "上课省份"不可为空';
                        break;
                    } else {
                        $row['cc_provid'] = 0;
                    }
                } else {
                    if (!isset($regionname_map[$row[11]])) {
                        $data['error'] = $col_char[11] . ($k + 2) . ' - "上课省份"不存在';
                        break;
                    }
                    $row['cc_provid'] = $regionname_map[$row[11]];
                }
                $row['cc_cityid'] = 0;
                $row['cc_areaid'] = 0;
                if (!empty($region_map[$regionname_map[$row[11]]]['children'])) {
                    // 验证市
                    if ($row[12] == '') {
                        if ($param['cc_cityid_required']) {
                            $data['error'] = $col_char[12] . ($k + 2) . ' - "上课城市"不可为空';
                            break;
                        } else {
                            $row['cc_cityid'] = 0;
                        }
                    } else {
                        if (!isset($regionname_map[$row[11] . '_' . $row[12]])) {
                            $data['error'] = $col_char[12] . ($k + 2) . ' - "上课城市"不存在';
                            break;
                        }
                        $row['cc_cityid'] = $regionname_map[$row[11] . '_' . $row[12]];
                    }
                    // 验证区县
                    if ($row[13] == '') {
                        if ($param['cc_areaid_required']) {
                            $data['error'] = $col_char[13] . ($k + 2) . ' - "上课区县"不可为空';
                            break;
                        } else {
                            $row['cc_areaid'] = 0;
                        }
                    } else {
                        if (!isset($regionname_map[$row[11] . '_' . $row[12] . '_' . $row[13]])) {
                            $data['error'] = $col_char[13] . ($k + 2) . ' - "上课区县"不存在';
                            break;
                        }
                        $row['cc_areaid'] = $regionname_map[$row[11] . '_' . $row[12] . '_' . $row[13]];
                    }
                }
                // 上课地址
                if ($row[14] == '') {
                    if ($param['cc_addr_required']) {
                        $data['error'] = $col_char[14] . ($k + 2) . ' - "上课地址"不可为空';
                        break;
                    }
                }
                if (mb_strlen($row[14], 'UTF-8') > 255) {
                    $data['error'] = $col_char[14] . ($k + 2) . ' - "上课地址"内容太长了';
                    break;
                }
                // 收费
                if ($row[15] == '') {
                    if ($param['cc_price_required']) {
                        $data['error'] = $col_char[15] . ($k + 2) . ' - "收费"不能为空';
                        break;
                    } else {
                        $row[15] = '0.00';
                    }
                }
                if (!is_numeric($row[15]) || $row[15] < 0) {
                    $data['error'] = $col_char[15] . ($k + 2) . ' - "收费"必须为非负数';
                    break;
                }
                $row[15] = bcadd($row[15], '0.00', 2);
                // 上课课时
                if ($row[16] == '') {
                    if ($param['cc_hours_required']) {
                        $data['error'] = $col_char[16] . ($k + 2) . ' - "上课课时"不能为空';
                        break;
                    } else {
                        $row[16] = '0';
                    }
                }
                if (!Validate::isInt($row[16]) || $row[16] < 0) {
                    $data['error'] = $col_char[16] . ($k + 2) . ' - "上课课时"必须为非负整数';
                    break;
                }
                // 联系人
                if ($row[17] == '') {
                    if ($param['cc_ctcperson_required']) {
                        $data['error'] = $col_char[17] . ($k + 2) . ' - "联系人"不可为空';
                        break;
                    }
                }
                if (mb_strlen($row[17], 'UTF-8') > 60) {
                    $data['error'] = $col_char[17] . ($k + 2) . ' - "联系人"太长了';
                    break;
                }
                // 联系电话
                if ($row['18'] == '') {
                    if ($param['cc_ctcphone_required']) {
                        $data['error'] = $col_char[18] . ($k + 2) . ' - "联系电话"不能为空';
                        break;
                    }
                }
                if (mb_strlen($row[18], 'UTF-8') > 120) {
                    $data['error'] = $col_char[18] . ($k + 2) . ' - "联系电话"太长了';
                    break;
                }
                // 课程简介
                if ($row[19] == '') {
                    if ($param['cors_memo_required']) {
                        $data['error'] = $col_char[19] . ($k + 2) . ' - "课程简介"不能为空';
                        break;
                    }
                }
                if (mb_strlen($row[19], 'UTF-8') > 65535) {
                    $data['error'] = $col_char[19] . ($k + 2) . ' - "课程简介"太长了';
                    break;
                }
                // 网址
                if ($row[20] == '') {
                    if ($param['cors_url_required']) {
                        $data['error'] = $col_char[20] . ($k + 2) . ' - "网址"不能为空';
                        break;
                    }
                }
                if (mb_strlen($row[20], 'UTF-8') > 512) {
                    $data['error'] = $col_char[20] . ($k + 2) . ' - "网址"内容太长了';
                    break;
                }
                $key = <<<EOT
{$row[0]}/{$row[1]}/{$row[2]}/{$row[3]}/{$row[4]}
EOT;
                if (!isset($cors_arr[$key])) {
                    $cors_arr[$key] = count($cors_list);
                    $cors_list[] = array('key' => $key, 'index' => $k + 2, 'cors_id' => 0, 'cors_name' => $row[0], 'ti_name' => $row[1], 'cors_cmid' => $row['cors_cmid'], 'cors_gradeid' => $row['cors_gradeid'], 'cors_subjectid' => $row['cors_subjectid'], 'cors_classid' => $row['cors_classid'], 'cors_memo' => $row[19], 'cors_url' => $row[20]);
                }
                if (!isset($cc_arr[$key])) {
                    $cc_arr[$key] = array();
                }
                $cc_arr[$key][] = count($cc_list);
                $cc_list[] = array('key' => $key, 'index' => $k + 2, 'tc_name' => $row['tc_name'], 'cc_teachers' => $row['cc_teachers'], 'cc_ctfid' => $row['cc_ctfid'], 'cc_classtime' => $row[8], 'cc_startanytime' => $row['cc_startanytime'], 'cc_begindate' => $row['cc_begindate'], 'cc_enddate' => $row['cc_enddate'], 'cc_provid' => $row['cc_provid'], 'cc_cityid' => $row['cc_cityid'], 'cc_areaid' => $row['cc_areaid'], 'cc_price' => $row[15], 'cc_hours' => $row[16], 'cc_addr' => $row[14], 'cc_ctcperson' => $row[17], 'cc_ctcphone' => $row[18]);
            }
            if (isset($data['error'])) {
                break;
            }
            unset($region_map);
            unset($regionname_map);
            unset($cm_map);
            unset($grade_map);
            unset($subject_map);
            unset($ctf_map);
            unset($class_map);
            unset($rows);
            // 这里开始导入
            $db = Fn::db();
            // 所属机构
            $sql1 = <<<EOT
SELECT ti_id FROM t_training_institution WHERE ti_name = ?
EOT;
            $sql2 = <<<EOT
SELECT tc_tiid, tc_id, tc_name FROM t_training_campus WHERE tc_tiid IN 
EOT;
            $sql3 = <<<EOT
UPDATE t_training_institution SET ti_campusnum = ti_campusnum + 1 
WHERE ti_id = 
EOT;
            $sql4 = <<<EOT
SELECT ct_id FROM t_cteacher WHERE ct_name = ? AND ct_contact IS NULL
EOT;
            $ti_id_arr = array();
            foreach ($cors_list as $k => $row) {
                $ti_id = $db->fetchOne($sql1, array($row['ti_name']));
                if ($ti_id) {
                    $ti_id_arr[] = $ti_id;
                    $cors_list[$k]['cors_tiid'] = $ti_id;
                } else {
                    $data['error'] = $col_char[1] . $row['index'] . ' - "来源机构"不存在';
                    break;
                }
            }
            if (isset($data['error'])) {
                break;
            }
            $ti_id_arr = array_unique($ti_id_arr);
            $tclist = $db->fetchAll($sql2 . '(' . implode(',', $ti_id_arr) . ')');
            $ti_tc_map = array();
            foreach ($tclist as $v) {
                if (!isset($ti_tc_map[$v['tc_tiid']])) {
                    $ti_tc_map[$v['tc_tiid']] = array();
                }
                $ti_tc_map[$v['tc_tiid']][trim($v['tc_name'])] = $v['tc_id'];
            }
            unset($tclist);
            foreach ($cc_list as $k => $row) {
                $ti_id = $cors_list[$cors_arr[$row['key']]]['cors_tiid'];
                $cc_list[$k]['ti_id'] = $ti_id;
                if (is_null($row['tc_name'])) {
                    $cc_list[$k]['cc_tcid'] = NULL;
                } else {
                    if (isset($ti_tc_map[$ti_id])) {
                        if (isset($ti_tc_map[$ti_id][$row['tc_name']])) {
                            $cc_list[$k]['cc_tcid'] = $ti_tc_map[$ti_id][$row['tc_name']];
                        } else {
                            // 不存在
                            if ($param['non_exist_tcname_action'] == '0') {
                                $data['error'] = $col_char[10] . $row['index'] . ' - "上课校区"不存在';
                                break;
                            }
                            $cc_list[$k]['cc_tcid'] = 0;
                        }
                    } else {
                        //  不存在
                        if ($param['non_exist_tcname_action'] == '0') {
                            $data['error'] = $col_char[10] . $row['index'] . ' - "上课校区"不存在';
                            break;
                        }
                        $cc_list[$k]['cc_tcid'] = 0;
                    }
                }
            }
            if (isset($data['error'])) {
                break;
            }
            /* 不需要验证一对一课程是否只能有一个校区了
               foreach ($cors_arr as $key => $index)
               {
                   if ($cors_list[$index]['cors_cmid'] == 1)
                   {
                       if (count($cc_arr[$key]) > 1)
                       {
                           $data['error'] = $col_char[10] 
                               . $cc_list[$cc_arr[$key][1]]['index']
                               . ' - 该行为一对一课程,只能有一条课程校区';
                           break;
                       }
                   }
               }
               if (isset($data['error']))
               {
                   break;
               }
                */
            try {
                $time = time();
                $adduid = Fn::sess()->userdata('admin_id');
                if (!$db->beginTransaction()) {
                    throw new Exception('开始导入事务处理失败');
                }
                $cors_insert = 0;
                $tc_insert = 0;
                $cc_insert = 0;
                $ct_insert = 0;
                // 导入课程
                foreach ($cors_list as $k => $row) {
                    // insert
                    $row2 = array();
                    $row2['cors_cmid'] = $row['cors_cmid'];
                    $row2['cors_name'] = $row['cors_name'];
                    $row2['cors_flag'] = $time;
                    $row2['cors_tiid'] = $row['cors_tiid'];
                    if ($row['cors_cmid'] == 1) {
                        $row2['cors_stunumtype'] = 1;
                    } else {
                        $row2['cors_stunumtype'] = 2;
                    }
                    $row2['cors_url'] = $row['cors_url'];
                    $row2['cors_memo'] = $row['cors_memo'];
                    $row2['cors_addtime'] = date('Y-m-d H:i:s', $time);
                    $row2['cors_adduid'] = $adduid;
                    $row2['cors_lastmodify'] = date('Y-m-d H:i:s', $time);
                    $db->insert('t_course', $row2);
                    $cors_list[$k]['cors_id'] = $cors_id = $db->lastInsertId('t_course', 'cors_id');
                    if (empty($row['cors_gradeid'])) {
                        $db->insert('t_course_gradeid', array('cg_corsid' => $cors_id, 'cg_gradeid' => 0));
                    } else {
                        foreach ($row['cors_gradeid'] as $v) {
                            $db->insert('t_course_gradeid', array('cg_corsid' => $cors_id, 'cg_gradeid' => $v));
                        }
                    }
                    if (empty($row['cors_subjectid'])) {
                        $db->insert('t_course_subjectid', array('cs_corsid' => $cors_id, 'cs_subjectid' => 0));
                    } else {
                        foreach ($row['cors_subjectid'] as $v) {
                            $db->insert('t_course_subjectid', array('cs_corsid' => $cors_id, 'cs_subjectid' => $v));
                        }
                    }
                    if (!empty($row['cors_classid'])) {
                        foreach ($row['cors_classid'] as $v) {
                            $db->insert('t_course_classid', array('cci_corsid' => $cors_id, 'cci_classid' => $v));
                        }
                    }
                    if ($row['cors_cmid'] != 1) {
                        $db->insert('t_course_knowledge', array('ck_corsid' => $cors_id, 'ck_kid' => 0, 'ck_knprocid' => 0));
                    }
                    $cors_insert++;
                }
                // 导入校区
                foreach ($cc_list as $k => $row) {
                    $key = $row['key'];
                    $row['cc_corsid'] = $cors_id = $cors_list[$cors_arr[$key]]['cors_id'];
                    $ti_id = $row['ti_id'];
                    if (is_null($row['cc_tcid'])) {
                        // 不要加
                    } else {
                        if ($row['cc_tcid'] == 0) {
                            if (isset($ti_tc_map[$ti_id][$row['tc_name']])) {
                                $row['cc_tcid'] = $ti_tc_map[$ti_id][$row['tc_name']];
                            }
                        }
                        if ($row['cc_tcid'] == 0) {
                            // 自动增加
                            $row2 = array();
                            $row2['tc_name'] = $row['tc_name'];
                            $row2['tc_tiid'] = $ti_id;
                            $row2['tc_provid'] = $row['cc_provid'];
                            $row2['tc_cityid'] = $row['cc_cityid'];
                            $row2['tc_areaid'] = $row['cc_areaid'];
                            $row2['tc_flag'] = $time;
                            $row2['tc_ctcaddr'] = $row['cc_addr'];
                            $row2['tc_ctcperson'] = $row['cc_ctcperson'];
                            $row2['tc_ctcphone'] = $row['cc_ctcphone'];
                            $row2['tc_environ'] = 3;
                            $row2['tc_addtime'] = date('Y-m-d H:i:s', $time);
                            $row2['tc_adduid'] = $adduid;
                            $db->insert('t_training_campus', $row2);
                            $ti_tc_map[$ti_id][$row['tc_name']] = $row['cc_tcid'] = $db->lastInsertId('t_training_campus', 'tc_id');
                            $tc_insert++;
                            $db->exec($sql3 . $ti_id);
                        }
                    }
                    $cc_teachers = $row['cc_teachers'];
                    unset($row['cc_teachers']);
                    unset($row['key']);
                    unset($row['index']);
                    unset($row['tc_name']);
                    unset($row['ti_id']);
                    $db->insert('t_course_campus', $row);
                    $cc_id = $db->lastInsertId('t_course_campus', 'cc_id');
                    $cc_insert++;
                    if ($cc_teachers == '') {
                        continue;
                    }
                    $cc_teachers_arr = explode(',', $cc_teachers);
                    $cc_teachers_arr = array_unique($cc_teachers_arr);
                    foreach ($cc_teachers_arr as $ctname) {
                        if ($ctname == '') {
                            continue;
                        }
                        $ct_id = $db->fetchOne($sql4, array($ctname));
                        if (!$ct_id) {
                            $db->insert('t_cteacher', array('ct_name' => $ctname, 'ct_flag' => $time));
                            $ct_id = $db->lastInsertId('t_cteacher', 'ct_id');
                            $ct_insert++;
                            $row3 = $cors_list[$cors_arr[$key]];
                            if (empty($row3['cors_gradeid'])) {
                                $db->insert('t_cteacher_gradeid', array('ctg_ctid' => $ct_id, 'ctg_gradeid' => 0));
                            } else {
                                foreach ($row3['cors_gradeid'] as $v) {
                                    $db->insert('t_cteacher_gradeid', array('ctg_ctid' => $ct_id, 'ctg_gradeid' => $v));
                                }
                            }
                            if (empty($row3['cors_subjectid'])) {
                                $db->insert('t_cteacher_subjectid', array('cts_ctid' => $ct_id, 'cts_subjectid' => 0));
                            } else {
                                foreach ($row3['cors_subjectid'] as $v) {
                                    $db->insert('t_cteacher_subjectid', array('cts_ctid' => $ct_id, 'cts_subjectid' => $v));
                                }
                            }
                        }
                        $db->insert('t_course_campus_teacher', array('cct_ccid' => $cc_id, 'cct_ctid' => $ct_id));
                    }
                }
                if ($db->commit()) {
                    $data['success'] = <<<EOT
导入Excel文件({$_FILES['file']['name']})成功,共插入{$cors_insert}条课程记录, 插入{$tc_insert}条机构校区记录, 插入{$cc_insert}条课程校区记录, 插入{$ct_insert}条教师记录
EOT;
                    admin_log('add', 'course', $data['success']);
                } else {
                    $err = $db->errorInfo()[2];
                    $db->rollBack();
                    throw new Exception($err);
                }
            } catch (Exception $e) {
                $data['error'] = $e->getMessage();
            }
            break;
        }
        if (!isset($_FILES['file'])) {
            $param = array('cors_classid_required' => 1, 'cc_classtime_required' => 1, 'cc_provid_required' => 1, 'cc_cityid_required' => 2, 'cc_areaid_required' => 3, 'cc_addr_required' => 1, 'cc_price_required' => 1, 'cc_hours_required' => 1, 'cc_ctcperson_required' => 1, 'cc_ctcphone_required' => 1, 'cors_memo_required' => 1, 'cors_url_required' => 1, 'non_exist_tcname_action' => 0);
        }
        $data['param'] = $param;
        $this->load->view('course/import_cors_excel', $data);
    }
Exemplo n.º 6
0
    /**
     * 导入教师记录(从excel文件中),
     */
    public function importteacher($sch_id = 0)
    {
        if ($_GET['dl'] == '1') {
            Func::dumpFile('application/vnd.ms-excel', 'file/import_school_teacher_template.xlsx', '教师导入模板.xlsx');
            exit;
        }
        if (!$sch_id || !SchoolModel::schoolInfo($sch_id)) {
            message('学校不存在,无法导入教师!');
        }
        $data = array();
        $data['sch_id'] = $sch_id;
        while (isset($_FILES['file'])) {
            $param = $_POST;
            $title = array('姓名', '年级', '学科', '简介');
            $col_char = array();
            $rows = Excel::readSimpleUploadFile($_FILES['file'], $title, $col_char);
            if (!is_array($rows)) {
                $data['error'] = $rows;
                break;
            }
            $grade_map = array_flip(C('grades'));
            $subject_map = array_flip(C('subject'));
            $ct_list = array();
            foreach ($rows as $k => $row) {
                //////////////////////////////
                //   姓名0 年级1 学科2  简介3
                //////////////////////////////
                // 姓名
                if ($row[0] == '') {
                    $data['error'] == $col_char[0] . ($k + 2) . ' - "姓名"不可为空';
                    break;
                }
                if (mb_strlen($row[0], 'UTF-8') > 30) {
                    $data['error'] = $col_char[0] . ($k + 2) . ' - "姓名"内容太长了,不可超过30个字符';
                    break;
                }
                // 年级
                if ($row[1] == '') {
                    $data['error'] = $col_char[1] . ($k + 2) . ' - "年级"不能为空';
                    break;
                }
                $row[1] = str_replace(array(',', ' ', ' ', '、', "\r\n", "\r", "\n"), ',', $row[1]);
                $row['ctg_gradeid'] = array();
                $arr = explode(',', $row[1]);
                foreach ($arr as $v) {
                    $v = trim($v);
                    if ($v == '') {
                        continue;
                    }
                    if (isset($grade_map[$v])) {
                        $row['ctg_gradeid'][] = $grade_map[$v];
                    } else {
                        $data['error'] = $col_char[1] . ($k + 2) . ' - "年级"里有不正确的选项';
                        break;
                    }
                }
                if (isset($data['error'])) {
                    break;
                }
                if (empty($row['ctg_gradeid'])) {
                    $data['error'] = $col_char[1] . ($k + 2) . ' - "年级"不能为空';
                    break;
                }
                $row['ctg_gradeid'] = array_unique($row['ctg_gradeid']);
                // 学科
                if ($row[2] == '') {
                    $data['error'] = $col_char[2] . ($k + 2) . ' - "学科"不能为空';
                    break;
                }
                $row[2] = str_replace(array(',', ' ', ' ', '、', "\r\n", "\r", "\n"), ',', $row[2]);
                $row['cts_subjectid'] = array();
                $arr = explode(',', $row[2]);
                foreach ($arr as $v) {
                    $v = trim($v);
                    if ($v == '') {
                        continue;
                    }
                    if (isset($subject_map[$v])) {
                        $row['cts_subjectid'][] = $subject_map[$v];
                    } else {
                        $data['error'] = $col_char[2] . ($k + 2) . ' - "学科"里有不正确的选项空';
                        break;
                    }
                }
                if (isset($data['error'])) {
                    break;
                }
                if (empty($row['cts_subjectid'])) {
                    $data['error'] = $col_char[2] . ($k + 2) . ' - "学科"不能为空';
                    break;
                }
                $row['cts_subjectid'] = array_unique($row['cts_subjectid']);
                // 简介
                if ($row[3] == '') {
                    $row[3] = NULL;
                }
                $ct_list[] = array('index' => $k + 2, 'ct_name' => $row[0], 'ct_memo' => $row[3], 'ctg_gradeid' => $row['ctg_gradeid'], 'cts_subjectid' => $row['cts_subjectid']);
            }
            if (isset($data['error'])) {
                break;
            }
            unset($grade_map);
            unset($subject_map);
            unset($rows);
            // 这里开始导入
            $db = Fn::db();
            try {
                $time = time();
                $adduid = Fn::sess()->userdata('admin_id');
                if (!$db->beginTransaction()) {
                    throw new Exception('开始导入事务处理失败');
                }
                $ct_insert = 0;
                // 导入教师
                foreach ($ct_list as $k => $row) {
                    // insert
                    $db->insert('t_cteacher', array('ct_name' => $row['ct_name'], 'ct_memo' => $row['ct_memo'], 'ct_flag' => $param['ct_flag']));
                    $ct_id = $db->lastInsertId('t_cteacher', 'ct_id');
                    $bind = array('scht_schid' => $sch_id, 'scht_ctid' => $ct_id);
                    $db->insert('t_cteacher_school', $bind);
                    foreach ($row['ctg_gradeid'] as $v) {
                        $db->insert('t_cteacher_gradeid', array('ctg_ctid' => $ct_id, 'ctg_gradeid' => $v));
                    }
                    foreach ($row['cts_subjectid'] as $v) {
                        $db->insert('t_cteacher_subjectid', array('cts_ctid' => $ct_id, 'cts_subjectid' => $v));
                    }
                    $ct_insert++;
                }
                if ($db->commit()) {
                    $data['success'] = <<<EOT
导入Excel文件({$_FILES['file']['name']})成功,共插入{$ct_insert}条教师记录
EOT;
                    admin_log('add', 'cteacher', $data['success']);
                } else {
                    $err = $db->errorInfo()[2];
                    $db->rollBack();
                    throw new Exception($err);
                }
            } catch (Exception $e) {
                $data['error'] = $e->getMessage();
            }
            break;
        }
        if (!isset($_FILES['file'])) {
            $param = array('ct_flag' => time());
        }
        $data['param'] = $param;
        $this->load->view('school/importteacher', $data);
    }
Exemplo n.º 7
0
 /**
  * @description  批量导入管理员
  * @author
  * @final
  */
 public function import()
 {
     if (!$this->check_power('import_cpuser')) {
         return;
     }
     if ($_GET['dl'] == '1') {
         Func::dumpFile('application/vnd.ms-excel', 'file/import_adminuser_template.xlsx', '后台管理员导入模板.xlsx');
         exit;
     }
     $this->load->view('cpuser/import');
 }