public function question_count() { $result = array('error' => '', 'count' => array(), 'group_count' => array()); $type = intval($this->input->post('type')); $subject_id = intval($this->input->post('subject_id')); $grade_id = intval($this->input->post('grade_id')); $class_id = intval($this->input->post('class_id')); $subject_type = intval($this->input->post('subject_type')); $is_original = intval($this->input->post('is_original')); if (empty($subject_id) || empty($grade_id) || empty($class_id)) { $result['error'] = '请选择学科、年级、类型'; die(json_encode($result)); } if ($grade_id < 11 || $subject_id > 3 || !in_array($class_id, array(2, 3))) { $subject_type = '-1'; } // 范围知识点 $knowledge_ids = trim($this->input->post('knowledge_ids')); $knowledge_ids = explode(',', $knowledge_ids); $knowledge_id_arr = array(); foreach ($knowledge_ids as $id) { $id = intval($id); $id && array_push($knowledge_id_arr, $id); } if ($type > 0 && empty($knowledge_id_arr)) { $result['error'] = '请选择知识点范围'; die(json_encode($result)); } // 重点知识点 $rule_knowledge = intval($this->input->post('rule_knowledge')); $children_ids = array(); if ($type == 2) { if ($rule_knowledge) { $pid = KnowledgeModel::get_knowledge($rule_knowledge, 'pid'); if ($pid === false) { $rule_knowledge = 0; } else { if ($pid == 0) { // 一级知识点 $children = KnowledgeModel::get_knowledge_list(0, $rule_knowledge, 0); foreach ($knowledge_id_arr as $kid) { if (isset($children[$kid])) { $children_ids[] = $kid; } } } else { // 二级知识点 if (!in_array($rule_knowledge, $knowledge_id_arr)) { $rule_knowledge = 0; } } } } if (empty($rule_knowledge)) { $result['error'] = '请选择重点知识点'; die(json_encode($result)); } } $where = array(); $where[] = "q.is_delete<>1 AND q.parent_id=0"; $where[] = "q.subject_id={$subject_id}"; $where[] = "q.start_grade<={$grade_id} AND q.end_grade>={$grade_id}"; $where[] = "q.ques_id IN (SELECT distinct ques_id FROM {pre}relate_class \n\t\t WHERE grade_id={$grade_id} AND class_id={$class_id} AND subject_type={$subject_type})"; if ($is_original > 0) { $where[] = "q.is_original={$is_original}"; } //认知过程 $know_process = intval($this->input->post('know_process')); //die(json_encode( $know_process)); $know_process_sql = !$know_process ? '' : "and know_process={$know_process}"; if ($type) { // 范围知识点 $knowledge_id_arr // $except_ids = array(); // $query = $this->db->select('id')->get_where('knowledge', array('subject_id'=>$subject_id,'pid >'=>'0')); // foreach ($query->result_array() as $row) // { // if ( !in_array($row['id'], $knowledge_id_arr)) // $except_ids[] = $row['id']; // } if ($type == 2) { if ($children_ids) { $where[] = "q.ques_id IN (SELECT ques_id FROM {pre}relate_knowledge \n where knowledge_id IN(" . my_implode($children_ids) . ") {$know_process_sql} and is_child=0)"; } else { $where[] = "q.ques_id IN (SELECT ques_id FROM {pre}relate_knowledge \n where knowledge_id={$rule_knowledge} {$know_process_sql} and is_child=0)"; } } else { $where[] = "q.ques_id IN (SELECT ques_id FROM {pre}relate_knowledge \n where knowledge_id IN(" . implode(',', $knowledge_id_arr) . ") {$know_process_sql} and is_child=0)"; } // if ($except_ids) // { // $where[] = "q.ques_id NOT IN (SELECT ques_id FROM {pre}relate_knowledge // where knowledge_id IN(".implode(',', $except_ids).") {$know_process_sql} and is_child=0)"; // } } if ($type < 2) { // 总数量 $sql = "SELECT COUNT(ques_id) nums FROM {pre}question q WHERE " . implode(' AND ', $where); $query = $this->db->query($sql); $row = $query->row_array(); $result['count'][0] = $row['nums']; //--------------------------------------------// // 统计关联试题数 // = 无分组试题数 + 分组数量 //--------------------------------------------// $result['group_count'][0] = 0; // 独立试题数(无分组) $sql2 = "SELECT COUNT(q.ques_id) nums FROM {pre}question q \n WHERE " . implode(' AND ', $where) . " AND q.group_id=0"; $query = $this->db->query($sql2); foreach ($query->result_array() as $row) { $result['group_count'][0] = $row['nums']; } // 不同分组数 $sql2 = "SELECT COUNT(distinct q.group_id) nums FROM {pre}question q \n WHERE " . implode(' AND ', $where) . " AND q.group_id>0"; $query = $this->db->query($sql2); foreach ($query->result_array() as $row) { $result['group_count'][0] += $row['nums']; } die(json_encode($result)); } // 重点知识点统计:按题型、难易度区间分组 $areas = C('difficulty_area'); if ($subject_id == 3) { $result['count'] = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); $result['group_count'] = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); } else { $result['count'] = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); $result['group_count'] = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); } foreach ($areas as $k => $area) { $new_where = $where; $new_where[] = "qd.difficulty between {$area['0']} AND {$area['1']}"; $sql = "SELECT q.type, COUNT(q.ques_id) nums FROM {pre}question q \n LEFT JOIN {pre}relate_class qd ON q.ques_id=qd.ques_id AND qd.grade_id={$grade_id} AND qd.class_id={$class_id}\n WHERE " . implode(' AND ', $new_where) . " GROUP BY q.type"; //echo $sql ;die; $query = $this->db->query($sql); foreach ($query->result_array() as $row) { if ($row['type'] > 9) { continue; } $result['count'][$row['type'] * 3 + $k] = $row['nums']; } // 独立试题数 $sql2 = "SELECT q.type,COUNT(q.ques_id) nums FROM {pre}question q \n LEFT JOIN {pre}relate_class qd ON q.ques_id=qd.ques_id AND qd.grade_id={$grade_id} AND qd.class_id={$class_id}\n WHERE " . implode(' AND ', $new_where) . " AND q.group_id=0 GROUP BY q.type"; $query = $this->db->query($sql2); foreach ($query->result_array() as $row) { if ($row['type'] > 9) { continue; } $result['group_count'][$row['type'] * 3 + $k] = $row['nums']; } // 分组试题数 $sql2 = "SELECT q.type, COUNT(distinct q.group_id) nums FROM {pre}question q \n LEFT JOIN {pre}relate_class qd ON q.ques_id=qd.ques_id AND qd.grade_id={$grade_id} AND qd.class_id={$class_id}\n WHERE " . implode(' AND ', $new_where) . " AND q.group_id>0 GROUP BY q.type"; $query = $this->db->query($sql2); foreach ($query->result_array() as $row) { if ($row['type'] > 9) { continue; } $result['group_count'][$row['type'] * 3 + $k] += $row['nums']; } } // echo $this->db->last_query();die; die(json_encode($result)); }
private function _delete($id) { $id = intval($id); $item = KnowledgeModel::get_knowledge($id); if (empty($item)) { // 知识点不存在 return -1; } $query = $this->db->select('count(*) num')->get_where('knowledge', array('pid' => $id)); $row = $query->row_array(); if ($row['num']) { //该知识点还存在下级分类; return -2; } $query = $this->db->select('count(*) num')->get_where('relate_knowledge', array('knowledge_id' => $id, 'is_child' => 0)); $row = $query->row_array(); if ($row['num']) { //该知识点还存在关联试题; return -3; } /* if ( ! QuestionModel::check_subject_power($item['subject_id'], 'w', false)) { //没有权限 return -4; } */ try { $this->db->delete('knowledge', array('id' => $id)); admin_log('delete', 'knowledge', $id); } catch (Exception $e) { return FALSE; } return TRUE; }
/** * 尝试加入一个试题到重点知识点 * * @param * array 试题信息 * @param * boolean 在试题不符合任何重点知识点规则的时候,是否强制加入 * @return boolean */ private static function reset_rule_ques_num(ExamPaperModelPrivateData &$this_data, $question, $force = FALSE) { $added = $force; // 修改待添加的重点知识点试题数量 $pids = array(); $offset = $question['type'] * 3 + ($question['difficulty'] <= 30 ? 0 : ($question['difficulty'] <= 60 ? 1 : 2)); if (!is_array($question['knowledge'])) { $question['knowledge'] = explode(',', trim($question['knowledge'], ',')); } foreach ($question['knowledge'] as $kid) { foreach ($this_data->sub_rules as &$val) { if ($val['knowledge_id'] == $kid && $val['nums'][$offset] > 0) { $val['nums'][$offset]--; $added = TRUE; } } // 如果存在一级重点知识点 $pid = KnowledgeModel::get_knowledge($kid, 'pid'); if (!isset($pids[$pid])) { foreach ($this_data->sub_rules as &$val) { if ($val['knowledge_id'] == $pid && $val['nums'][$offset] > 0) { $val['nums'][$offset]--; $pids[$pid] = $pid; $added = TRUE; } } } } if ($added) { self::insert_question($this_data, $question); } return $added; }
/** * 生成试卷 * * @param int $rule_id 组题规则id * @param int $exam_id 考试期次id * @param string $method 试卷生成方式 * @return void */ public function gen($exam_id = 0, $rule_id = 0, $method = 'new') { if ($rule_id = intval($rule_id)) { $query = $this->db->get_where('exam_rule', array('rule_id' => $rule_id)); $rule = $query->row_array(); } if (empty($rule)) { message('规则不存在'); return; } /* * 检查提交数据是否有效 */ $exam_id = intval($exam_id); $method = $method == 'new' ? 'generate' : 'generate_rand'; if ($exam_id) { $exam = ExamModel::get_exam($exam_id); } if (empty($exam) || $exam['status']) { die('请选择考试期次.'); } else { if ($exam['subject_id'] != $rule['subject_id'] || $exam['grade_id'] != $rule['grade_id'] || $exam['class_id'] != $rule['class_id']) { die('组题规则和考试期次不匹配(学科、年级或类型)'); } } $rule['knowledge_id_arr'] = explode(',', $rule['knowledge_ids']); $rule['ques_num'] = explode(',', $rule['ques_num']); if (count($rule['ques_num']) < 4) { for ($i = count($rule['ques_num']); $i < 4; $i++) { $rule['ques_num'][$i] = 0; } } /* 读取重点知识点 */ $sub_rules = array(); $query = $this->db->get_where('exam_rule_knowledge', array('rule_id' => $rule_id)); foreach ($query->result_array() as $row) { $row['nums'] = explode(',', $row['setting']); $row['pid'] = KnowledgeModel::get_knowledge($row['knowledge_id'], 'pid'); // 一级知识点规则 if ($row['pid'] == 0) { $children = KnowledgeModel::get_knowledge_list(0, $row['knowledge_id'], 0); $children_ids = array_keys($children); $row['children'] = array_intersect($children_ids, $rule['knowledge_id_arr']); } $sub_rules[$row['knowledge_id']] = $row; } $res = ExamPaperModel::$method($exam, $rule, $sub_rules); if ($res['success'] == true) { die('试卷生成成功'); } else { die($res['msg']); } }
/** * 试卷试题列表未加入期次 * * @param int $paper_id 试卷id * @param int $qtype 题型 * @param int $method_tactic_id 方法策略 * @param int $kid 知识点 * @return void */ public function index3($paper_id = 0) { if (!$this->check_power('exam_list,exam_manage,paper_diy')) { return; } $paper_id = intval($paper_id); $paper = ExamPaperModel::get_paper($paper_id); if (!empty($paper['question_sort'])) { $paper = ExamPaperModel::detail_sg($paper_id, 1, 1); } $paper['ques_ids'] = json_decode($paper['question_sort'], true); $ques_ids_str = implode(',', $paper['ques_ids']); if (empty($paper)) { message('试卷不存在'); return; } /* * 考试期次 */ $exam = ExamModel::get_exam($paper['exam_id'], 'exam_pid,grade_id,class_id'); $search = $where = $param = array(); if (!empty($paper['question_sort'])) { $where[] = "1=1"; } $qtypes = C('qtype'); $qtype = $this->input->get('qtype'); if (strlen($qtype)) { $qtype = abs(intval($qtype)); $where['qtype'] = "q.type={$qtype}"; $param[] = "qtype={$qtype}"; } $search['qtype'] =& $qtype; /** is_original */ $is_original = $this->input->get('is_original'); if (in_array($is_original, array(1, 2))) { $where['is_original'] = "q.is_original={$is_original}"; $param[] = "is_original={$is_original}"; } $search['is_original'] =& $is_original; //方法逻辑 $method_tactic_id = intval($this->input->get('method_tactic_id')); if ($method_tactic_id) { $where['method_tactic_id'] = "q.ques_id IN (SELECT\n DISTINCT rmt.ques_id FROM {pre}relate_method_tactic rmt\n WHERE rmt.ques_id in({$ques_ids_str})\n AND rmt.method_tactic_id={$method_tactic_id})"; $param[] = "method_tactic_id={$method_tactic_id}"; } $search['method_tactic_id'] =& $method_tactic_id; //知识点 if ($kid = intval($this->input->get('kid'))) { $pkid = KnowledgeModel::get_knowledge($kid, 'pid'); if ($pkid !== false) { if ($pkid == 0) { // 一级知识点 $k_children = KnowledgeModel::get_knowledge_list(0, $kid, 0); $where['knowledge'] = "q.ques_id IN (\n SELECT DISTINCT rk.ques_id FROM {pre}relate_knowledge rk\n WHERE rk.ques_id in({$ques_ids_str}) AND rk.knowledge_id\n IN (" . my_implode(array_keys($k_children)) . ") AND rk.is_child=0)"; } else { // 二级知识点 if (!empty($paper['question_sort'])) { $where['knowledge'] = "q.ques_id IN (SELECT DISTINCT rk.ques_id FROM {pre}relate_knowledge\n rk WHERE rk.ques_id in({$ques_ids_str}) AND rk.knowledge_id={$kid} AND rk.is_child=0)"; } } $param[] = "kid={$kid}"; } } $search['kid'] =& $kid; /* 信息提取方式 */ if ($gid = intval($this->input->get('group_type_id'))) { $pgid = GroupTypeModel::get_group_type($gid, 'pid'); if ($pgid !== false) { if ($pgid == 0) { $g_children = GroupTypeModel::get_group_type_list($gid, 0); if (!empty($paper['question_sort'])) { $where['group_type'] = "q.ques_id IN ( SELECT DISTINCT\n rk.ques_id FROM {pre}relate_group_type rk WHERE\n rk.ques_id in({$ques_ids_str}) AND rk.group_type_id\n IN (" . my_implode(array_keys($g_children)) . ") AND rk.is_child=0)"; } } else { if (!empty($paper['question_sort'])) { $where['group_type'] = "q.ques_id IN (\n SELECT DISTINCT rk.ques_id FROM {pre}relate_group_type rk\n WHERE rk.ques_id in({$ques_ids_str})\n AND rk.group_type_id={$gid} AND rk.is_child=0)"; } } $param[] = "group_type_id={$gid}"; } } $search['group_type_id'] =& $gid; if (!empty($paper['question_sort'])) { $total = count($paper['question_sort']); } $size = 150; $page = isset($_GET['page']) && intval($_GET['page']) > 1 ? intval($_GET['page']) : 1; $offset = ($page - 1) * $size; $list = array(); if ($total) { $sql = "SELECT q.ques_id,q.title,q.type,q.group_id,q.addtime,\n (SELECT AVG(rc.difficulty) FROM {pre}relate_class rc where rc.ques_id=q.ques_id) AS difficulty\n FROM {pre}question q WHERE q.ques_id in({$ques_ids_str})\n AND " . implode(' AND ', $where) . " LIMIT {$offset},{$size}"; $res = $this->db->query($sql); foreach ($res->result_array() as $row) { $row['qtype'] = isset($qtypes[$row['type']]) ? $qtypes[$row['type']] : ''; $row['addtime'] = date('Y-m-d H:i', $row['addtime']); $row['has_edit_power'] = QuestionModel::check_question_power($row['ques_id'], 'w', false); $row['difficulty'] = round($row['difficulty'], 2); $list[] = $row; } } // 分页 $purl = site_url('admin/exam_question/index3/' . $paper_id . '/') . ($param ? '?' . implode('&', $param) : ''); $data['pagination'] = multipage($total, $size, $page, $purl); // 按题型分组统计数量 $qtype_nums = array(0, 0, 0, 0); foreach (explode(',', $paper['qtype_ques_num']) as $k => $v) { $qtype_nums[$k] = $v; } //判断该试题已经被考试过 或 正在被考 $paper['be_tested'] = false; if (!ExamModel::is_mini_test($exam['exam_pid'])) { $paper['be_tested'] = QuestionModel::paper_has_test_action($paper_id); } $data['search'] = $search; $data['qtypes'] = $qtypes; $data['paper'] = $paper; $data['list'] = $list; $data['qtype_nums'] = $qtype_nums; $data['priv_manage'] = $this->check_power('exam_manage', FALSE); /* 是否有权限查看试卷列表 */ $is_exam_paper_list = $this->check_power('exam_list', false); $data['is_exam_paper_list'] = $is_exam_paper_list; // 模版 $this->load->view('exam_question/index3', $data); }