/** * 批量删除试卷中的试题 * * @param int $paper_id 试卷id * @param int $ids 试卷中的试题id * @return void */ public function batch_delete() { if (!$this->check_power('exam_manage')) { return; } $paper_id = (int) $this->input->post('paper_id'); $ids = $this->input->post('ids'); if (empty($ids) or !is_array($ids)) { message('请选择要操作的项目!'); return; } $paper = PaperModel::get_paper_by_id($paper_id); if (!$paper) { message('试卷不存在'); return; } $exam_pid = ExamModel::get_exam($paper['exam_id'], 'exam_pid'); $is_mini_test = ExamModel::is_mini_test($exam_pid); //判断该试卷已经被考试过 或 正在被考 $count_fail = 0; $count_success = 0; $tmp_ids = array(); foreach ($ids as $ques_id) { if (!$is_mini_test) { $be_tested = QuestionModel::paper_question_has_been_tested($paper_id, $ques_id); if ($be_tested) { $count_fail++; continue; } } $tmp_ids[] = $ques_id; $count_success++; } $back_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'admin/exam_question/index/' . $paper_id; $this->db->where_in('id', $tmp_ids)->delete('exam_question'); ExamPaperModel::renew($paper_id); if ($is_mini_test && $count_success) { SummaryModel::summary_paper($exam_pid, 0, $paper_id, true); } message("批量操作完成(成功 {$count_success}个,失败 {$count_fail} 个)", $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; } }
/** * 更新试卷信息 * * @param int $exam_id 考试期次id * @param int $paper_id 试卷id * @return void */ public function update_question_info($exam_id, $paper_id) { if (!$this->check_power('exam_manage')) { exit; } $exam_id = (int) $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'); exit; } $is_mini_test = ExamModel::is_mini_test($exam['exam_pid']); //检查该学科考试状态 $has_tested = false; if (!$is_mini_test) { $has_tested = ExamPlaceSubjectModel::exam_subject_has_test_action($exam_id); } if ($has_tested) { message('该期次科目已经被考生考过 或者 正在被考中,因此无法操作'); exit; } /* 更新外部新试卷信息 */ $paper_id = (int) $paper_id; /* 查询试卷信息 */ $paper = PaperModel::get_paper_by_id($paper_id, 'exam_id,paper_id,admin_id,question_sort'); if (empty($paper) or $exam_id != $paper['exam_id']) { message('试卷不在当前考试期次中!'); exit; } /* 判定是否为外部试卷 */ if ($paper['admin_id'] > 0) { $rst = PaperModel::update_paper_question($paper_id); if (!$rst) { message('更新试卷失败'); } else { message('更新试卷成功', 'javascript'); } } else { message('当前试卷不属于手工组卷,取消操作!'); } }
/** * 一键更新试题缓存 * * @author TCG * @param int $exam_id 考试期次科目id * @return mixed 返回成功及失败提示 */ public function update_questions_cache($exam_id) { $exam_id = (int) $exam_id; $exam = Fn::db()->fetchRow("SELECT exam_id, exam_pid, subject_id, grade_id, class_id, total_score, qtype_score FROM rd_exam WHERE exam_id=?", $exam_id); if (empty($exam)) { return false; } $is_mini_test = ExamModel::is_mini_test($exam['exam_pid']); /** 载入缓存功能 */ if ($is_mini_test) { $this->load->driver('cache'); /** 缓存时间 单位second 默认缓存30天 */ $cache_time = 24 * 3600 * 20; } else { $cache = Fn::factory('File'); } /** 写入缓存 */ $is_mini_test && $this->cache->file->save('get_paper_question_detail_p_exam_' . $exam_id, $exam, $cache_time); $sql = "SELECT paper_id FROM rd_exam_paper WHERE exam_id={$exam_id}"; $rows = Fn::db()->fetchAll($sql); foreach ($rows as $row) { /** 写入缓存 */ $is_mini_test && $this->cache->file->save('get_paper_question_detail_p_exam_id_' . $row['paper_id'], $exam_id, $cache_time); $question_sort = Fn::db()->fetchOne('SELECT question_sort FROM rd_exam_paper WHERE paper_id = ?', array($row[paper_id])); $question_sort = json_decode($question_sort, true); $sql = <<<EOT SELECT q.ques_id,q.type,q.title,q.score_factor,rc.difficulty FROM rd_exam_question eq LEFT JOIN rd_question q ON eq.ques_id=q.ques_id LEFT JOIN rd_relate_class rc ON rc.ques_id = q.ques_id AND rc.grade_id = ? AND rc.class_id = ? WHERE eq.paper_id = ? ORDER BY rc.difficulty DESC,q.ques_id ASC EOT; if ($question_sort) { $res = Fn::db()->fetchAssoc($sql, array($exam['grade_id'], $exam['class_id'], $row['paper_id'])); $sort = explode(',', $question_sort); $result = array(); foreach ($question_sort as $ques_id) { $result[] = $res[$ques_id]; unset($res[$ques_id]); } } else { $result = Fn::db()->fetchAll($sql, array($exam[grade_id], $exam[class_id], $row[paper_id])); } $key = 'get_paper_question_detail_p_' . $exam[grade_id] . '_' . $exam[class_id] . '_' . $row[paper_id]; !$is_mini_test && $cache->save('/zmexam/' . $key, $result); /** 写入缓存 */ $is_mini_test && $this->cache->file->save($key, $result, $cache_time); } //$domain=C('memcache_pre'); /** 获取考试期次所有试题 */ $sql = "SELECT ques_id FROM {pre}exam_question WHERE exam_id={$exam_id}"; $query = $this->db->query($sql); $rows = $query->result_array(); /** 生成缓存 (执行时间可能过长) */ if (count($rows) > 0 && !empty($rows)) { $questions = array(); /** 获取试题数据 */ foreach ($rows as $key => $value) { $question = QuestionModel::get_question($value['ques_id'], 'ques_id, type, title, picture, parent_id,subject_id'); if (!$question) { continue; } /** 题组 */ if (in_array($question['type'], array(0, 4, 5, 6, 8))) { $pid = $question['ques_id']; $sql = "SELECT ques_id,type,title,picture,parent_id,subject_id FROM {pre}question WHERE parent_id={$pid}"; $query = $this->db->query($sql); $group_rows = $query->result_array(); /** 写入题组子题 */ $question['children'] = $group_rows; /** 子题加入缓存列表 */ $questions = array_merge($questions, $group_rows); } $questions[] = $question; } foreach ($questions as $key => &$question) { /** 样式修正 */ $question['title'] && $this->_format_question_content($question['ques_id'], $question['title'], in_array($question['type'], array(3, 9))); /** 获取试题选项数据(选择题) */ if (in_array($question['type'], array(1, 2, 7, 14))) { $question['options'] = QuestionModel::get_options($question['ques_id']); } /** 获取题组子题选项 */ if (in_array($question['type'], array(0, 4, 5, 6, 8)) && !empty($question['children'])) { foreach ($question['children'] as $k => &$v) { $v['title'] && $this->_format_question_content($v['ques_id'], $v['title'], $v['type'] == 3); $question['children'][$k]['options'] = QuestionModel::get_options($v['ques_id']); } } !$is_mini_test && $cache->save('/zmexam/question_' . $question['ques_id'], $question); /** 写入缓存 */ $is_mini_test && $this->cache->file->save($question['ques_id'], $question, $cache_time); } /** 成功提示页面 */ message('生成成功.', 'admin/exam/index'); } else { /** 失败提示页面 */ message('生成失败!!!请尝试从新输入!', 'admin/exam/index'); } /** $this->output->enable_profiler(TRUE); */ }
/** * 添加试题 * * @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)); } }