/** * @description 添加期次科目试卷 * @author * @final * @param int $exam_id 考试期次id * @param array $ids 试卷id */ public function insert() { if (!$this->check_power('exam_manage')) { return; } $exam_id = (int) $this->input->post('exam_id'); $exam_id && ($exam = ExamModel::get_exam($exam_id, 'exam_id,exam_pid,grade_id,class_id')); if (empty($exam)) { message('考试期次科目不存在', 'admin/exam/index'); return; } $ids = $this->input->post('ids'); if (empty($ids) or !is_array($ids)) { message('请至少选择一项'); return; } //检查该学科考试状态 $has_tested = ExamPlaceSubjectModel::exam_subject_has_test_action($exam_id); if ($has_tested) { message('该期次科目已经被考生考过 或者 正在被考中,因此无法操作'); } $ids = my_intval($ids); /* ------------------------------- */ /* 补全外部新试卷信息 */ foreach ($ids as $paper_id) { /* 查询试卷信息 */ $paper = PaperModel::get_paper_by_id($paper_id, 'paper_id,admin_id,question_sort'); /* 判定是否为外部试卷 */ if ($paper['admin_id'] > 0) { $questions = json_decode($paper['question_sort'], true); $qtype_ques_num = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0); $question_difficulty = array(); $this->db->trans_start(); $sql = "DELETE FROM rd_exam_question WHERE paper_id={$paper_id} \n AND exam_id={$exam_id}"; Fn::db()->query($sql); if (count($questions) > 0) { foreach ($questions as $ques_id) { /* 补全exam_question信息 */ $data = array(); $data['paper_id'] = $paper_id; $data['exam_id'] = $exam_id; $data['ques_id'] = $ques_id; $this->db->insert('exam_question', $data); $sql = "select q.ques_id,q.type,rc.difficulty from\n {pre}question q left join {pre}relate_class rc on q.ques_id=rc.ques_id\n where q.ques_id={$ques_id} and rc.grade_id={$exam['grade_id']} and rc.class_id={$exam['class_id']}"; $question = $this->db->query($sql)->row_array(); if (empty($question)) { $this->db->trans_rollback(); message('当前试卷中存在不属于当前考试期次年级的试题!请检查试题!'); exit; } /* 各个类型试题数量 */ $qtype_ques_num[$question['type']]++; /* 试题难易度 */ $question_difficulty[] = $question['difficulty']; } } /* 补全exam_pager信息 */ $data = array(); $data['exam_id'] = $exam_id; $data['qtype_ques_num'] = implode(',', $qtype_ques_num); $data['difficulty'] = array_sum($question_difficulty) / count($question_difficulty); PaperModel::update_paper($paper_id, $data); $this->db->trans_complete(); } } /* -------------------------------------- */ $inserts = array(); $sql = "SELECT p.paper_id,e.subject_id FROM {pre}exam_paper p,{pre}exam e\n WHERE p.exam_id=e.exam_id AND p.paper_id IN (" . my_implode($ids) . ")\n AND p.is_delete=0 AND p.exam_id={$exam_id}"; $query = $this->db->query($sql); foreach ($query->result_array() as $row) { $inserts[] = array('exam_pid' => $exam['exam_pid'], 'exam_id' => $exam['exam_id'], 'paper_id' => $row['paper_id'], 'subject_id' => $row['subject_id']); } $res = 0; if ($inserts) { // 关闭错误信息,防止 unique index 冲突出错 $this->db->db_debug = false; $this->db->insert_batch('exam_subject_paper', $inserts); $res = $this->db->affected_rows(); } $back_url = 'admin/subject_paper/add/' . $exam_id; if ($res < 1) { message('试卷添加失败', $back_url); } else { message('试卷添加成功', $back_url); } }
/** * 更新试卷试题 * @param array $paper_id 试卷ID * @return boolen 成功返回true, 失败返回false */ public static function update_paper_question($paper_id) { /* 更新外部新试卷信息 */ $paper_id = (int) $paper_id; if (!$paper_id) { return false; } /* 查询试卷信息 */ $paper = self::get_paper_by_id($paper_id, 'exam_id,paper_id,admin_id,question_sort'); if (!$paper) { return false; } $exam = ExamModel::get_exam($paper['exam_id'], 'exam_id,exam_pid,grade_id,class_id'); if (!$exam) { return false; } $db = Fn::db(); /* 判定是否为外部试卷 */ if ($paper['admin_id'] > 0) { $questions = json_decode($paper['question_sort'], true); $question_difficulty = array(); $qtype_ques_num = array_fill(0, count(C('qtype')), '0'); if (!$db->beginTransaction()) { return false; } /* 清除exam_question原有信息 */ $db->delete('rd_exam_question', 'exam_id =? AND paper_id = ?', array($paper['exam_id'], $paper_id)); if (count($questions) > 0) { foreach ($questions as $ques_id) { $sql = "SELECT q.ques_id,q.type,rc.difficulty FROM\n rd_question q \n LEFT JOIN rd_relate_class rc ON q.ques_id=rc.ques_id\n WHERE q.ques_id={$ques_id} AND rc.grade_id={$exam['grade_id']} \n AND rc.class_id={$exam['class_id']}"; $question = $db->fetchRow($sql); if (empty($question)) { $db->rollBack(); throw new Exception('当前试卷中存在不属于当前考试期次年级的试题!请检查试题!'); } /* 补全exam_question信息 */ $data = array(); $data['paper_id'] = $paper_id; $data['exam_id'] = $paper['exam_id']; $data['ques_id'] = $ques_id; $db->insert('rd_exam_question', $data); /* 试题难易度 */ $question_difficulty[] = $question['difficulty']; /* 各个类型试题数量 */ $qtype_ques_num[$question['type']]++; } } /* 补全exam_pager信息 */ $data = array(); $data['exam_id'] = $paper['exam_id']; $data['difficulty'] = array_sum($question_difficulty) / count($question_difficulty); $data['qtype_ques_num'] = implode(',', $qtype_ques_num); PaperModel::update_paper($paper_id, $data); $flag = $db->commit(); if (!$flag) { $db->rollBack(); } //如果是mini测试卷,则更新试卷统计 if (ExamModel::is_mini_test($exam['exam_pid'])) { $sql = "SELECT exam_pid FROM rd_exam_subject_paper \n WHERE paper_id = {$paper_id} AND exam_id = " . $exam['exam_id']; if (Fn::db()->fetchOne($sql)) { SummaryModel::summary_paper($exam['exam_pid'], 0, $paper_id, true); } } return $flag; } }
/** * 添加试题 * * @return void **/ public function update_question() { $post = $this->input->post(); $paper_id = (int) $post['paper_id']; /* exam_paper 试卷对应排序 */ /* 限定用户只能更新跟自己相关的数据 */ $admin_id = $this->session->userdata('admin_id'); if (!$admin_id) { message('获取管理员数据失败,请从新登陆后重试!'); } /* 获取试卷信息 */ $paper = PaperModel::get_paper_by_id($paper_id); if (!$paper) { message('获取试卷数据失败,请重试!'); } /* 用户可以编辑自己的试卷 管理员可以查看所有试卷 */ if ($paper['admin_id'] != $admin_id && !$this->session->userdata('is_super')) { message('没有当前试卷编辑权限!'); } if (!$this->is_super_user() && ExamPlaceSubjectModel::exam_subject_has_test_action($paper['exam_id'])) { message('当前试卷已进行考试,不可以修改更新!'); } /* 计算排序 */ $questions = $this->sort_question($post['ques_ids'], $post['sort']); $question_score = $this->sort_question($post['score'], $post['sort']); if (!$questions) { $questions = array(); } /* 更新试卷信息 */ $data = array(); $data['question_sort'] = json_encode($questions); $data['question_score'] = json_encode($post['score']); $qtype_ques_num = array_fill(0, count(C('q_type')), '0'); $this->db->trans_start(); $this->db->delete('exam_question', array('paper_id' => $paper_id)); $question_difficulty = array(); if (count($questions) > 0) { foreach ($questions as $ques_id) { $sql = "select q.ques_id,q.type,AVG(rc.difficulty) as difficulty from\n {pre}question q left join {pre}relate_class rc on q.ques_id=rc.ques_id\n where q.ques_id={$ques_id} group by q.ques_id"; $question = $this->db->query($sql)->row_array(); if (empty($question)) { $this->db->trans_rollback(); message('当前试卷中存在不属于当前考试期次年级的试题!请检查试题!'); exit; } $data1 = array(); $data1['paper_id'] = $paper_id; $data1['exam_id'] = $paper['exam_id']; $data1['ques_id'] = $ques_id; $this->db->insert('exam_question', $data1); /* 各个类型试题数量 */ $qtype_ques_num[$question['type']]++; /* 试题难易度 */ $question_difficulty[] = $question['difficulty']; } } /* 更新试题数量 */ $data['ques_num'] = count($questions); $data['qtype_ques_num'] = implode(',', $qtype_ques_num); $data['difficulty'] = array_sum($question_difficulty) / count($question_difficulty); PaperModel::update_paper($paper_id, $data); $rst = $this->db->trans_commit(); if (!$rst) { message('更新试卷信息失败!请重试!'); } else { $sql = "SELECT exam_pid FROM rd_exam_subject_paper \n WHERE paper_id = {$paper_id}"; $exam_pid = Fn::db()->fetchOne($sql); if ($exam_pid && ExamModel::is_mini_test($exam_pid)) { PaperModel::update_paper_question($paper_id); } message('更新试卷信息成功!', site_url('admin/paper_diy/question_manage/' . $paper_id)); } }