Пример #1
0
 public function batch_relate()
 {
     $ids = $this->input->post('ids');
     if (empty($ids) or !is_array($ids)) {
         message('请选择要关联的项目!');
         return;
     }
     // 检查被关联试题
     $relate_ques_id = intval($this->input->post('relate_ques_id'));
     $relate_ques_id && ($relate_ques = QuestionModel::get_question($relate_ques_id));
     if (empty($relate_ques)) {
         message('被关联试题不存在。');
         return;
     }
     $be_tested = QuestionModel::question_has_test_action($relate_ques_id);
     if ($be_tested) {
         message('被关联试题已经被考生考过 或者 正在被考, 无法操作');
     }
     // 如果被关联试题无分组,则:创建分组,并把该试题加入关联
     $group_id = $relate_ques['group_id'];
     if (empty($group_id)) {
         $this->db->insert('relate_group', array('group_name' => $relate_ques_id));
         $group_id = $this->db->insert_id();
         $this->db->update('question', array('group_id' => $group_id), array('ques_id' => $relate_ques_id));
     }
     $success = $fail = 0;
     foreach ($ids as $id) {
         $this->examine_permission($id);
         $num = QuestionModel::relate($id, $group_id);
         if ($num > 0) {
             $success += $num;
         } else {
             $fail++;
         }
     }
     $back_url = 'admin/relate_group/group/' . $group_id;
     message('批量操作完成,成功关联:' . $success . ' 个,失败:' . $fail . ' 个。', $back_url);
 }