You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
203 lines
6.6 KiB
203 lines
6.6 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Niucloud-admin 企业快速开发的多应用管理平台
|
|
// +----------------------------------------------------------------------
|
|
// | 官方网址:https://www.niucloud.com
|
|
// +----------------------------------------------------------------------
|
|
// | niucloud团队 版权所有 开源版本可自由商用
|
|
// +----------------------------------------------------------------------
|
|
// | Author: Niucloud Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\service\admin\exam_questions;
|
|
|
|
use app\model\exam_questions\ExamQuestions;
|
|
|
|
use core\base\BaseAdminService;
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
|
|
|
|
/**
|
|
* 试题服务层
|
|
* Class ExamQuestionsService
|
|
* @package app\service\admin\exam_questions
|
|
*/
|
|
class ExamQuestionsService extends BaseAdminService
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->model = new ExamQuestions();
|
|
}
|
|
|
|
/**
|
|
* 获取试题列表
|
|
* @param array $where
|
|
* @return array
|
|
*/
|
|
public function getPage(array $where = [])
|
|
{
|
|
$field = 'id,title,question_type,question_content_type,correct_answer,question_content,option_json,created_at,updated_at';
|
|
$order = 'id desc';
|
|
|
|
$search_model = $this->model->withSearch(["title","question_type","created_at"], $where)->field($field)->order($order);
|
|
$list = $this->pageQuery($search_model);
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* 获取试题信息
|
|
* @param int $id
|
|
* @return array
|
|
*/
|
|
public function getInfo(int $id)
|
|
{
|
|
$field = 'id,title,question_type,question_content_type,correct_answer,question_content,option_json,created_at,updated_at';
|
|
|
|
$info = $this->model->field($field)->where([['id', "=", $id]])->findOrEmpty()->toArray();
|
|
return $info;
|
|
}
|
|
|
|
/**
|
|
* 添加试题
|
|
* @param array $data
|
|
* @return mixed
|
|
*/
|
|
public function add(array $data)
|
|
{
|
|
$res = $this->model->create($data);
|
|
return $res->id;
|
|
|
|
}
|
|
|
|
/**
|
|
* 试题编辑
|
|
* @param int $id
|
|
* @param array $data
|
|
* @return bool
|
|
*/
|
|
public function edit(int $id, array $data)
|
|
{
|
|
|
|
$this->model->where([['id', '=', $id]])->update($data);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 删除试题
|
|
* @param int $id
|
|
* @return bool
|
|
*/
|
|
public function del(int $id)
|
|
{
|
|
$model = $this->model->where([['id', '=', $id]])->find();
|
|
$res = $model->delete();
|
|
return $res;
|
|
}
|
|
|
|
|
|
public function random_questions_list(array $data){
|
|
|
|
$singleChoiceIds = $data['single_choice_count'] > 0 ? $this->model
|
|
->where([['question_type', '=', 'single_choice']])
|
|
->orderRaw('RAND()')
|
|
->limit($data['single_choice_count'])
|
|
->column("id") : [];
|
|
|
|
$multipleChoiceIds = $data['multiple_choice_count'] > 0 ? $this->model
|
|
->where([['question_type', '=', 'multiple_choice']])
|
|
->orderRaw('RAND()')
|
|
->limit($data['multiple_choice_count'])
|
|
->column("id") : [];
|
|
|
|
$trueFalseIds = $data['true_false_count'] > 0 ? $this->model
|
|
->where([['question_type', '=', 'true_false']])
|
|
->orderRaw('RAND()')
|
|
->limit($data['true_false_count'])
|
|
->column("id") : [];
|
|
|
|
$questions_ids = array_merge($singleChoiceIds, $multipleChoiceIds, $trueFalseIds);
|
|
|
|
return success("随机成功", ['questions_ids' => implode(',', $questions_ids)]);
|
|
}
|
|
|
|
public function to_lead_into(array $data)
|
|
{
|
|
$filePath = public_path() . $data['url'];
|
|
|
|
try {
|
|
$spreadsheet = IOFactory::load($filePath);
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
$highestRow = $worksheet->getHighestRow();
|
|
|
|
$list = [];
|
|
// 遍历每一行
|
|
foreach ($worksheet->getRowIterator() as $rowIndex => $row) {
|
|
if ($rowIndex == 1) {
|
|
// 跳过第一行(表头)和最后一行
|
|
continue;
|
|
}
|
|
|
|
$cellIterator = $row->getCellIterator();
|
|
$cellIterator->setIterateOnlyExistingCells(false);
|
|
|
|
$data = [];
|
|
foreach ($cellIterator as $cell) {
|
|
$value = $cell->getValue(); // 获取每个单元格的值
|
|
|
|
if ($value instanceof \PhpOffice\PhpSpreadsheet\RichText\RichText) {
|
|
$value = $value->getPlainText();
|
|
}
|
|
$data[] = $value;
|
|
}
|
|
$list[] = $data;
|
|
}
|
|
$inserAll = [];
|
|
$question_type_arr = ['单选' => 'single_choice','多选'=>'multiple_choice','判断' => 'true_false'];
|
|
foreach ($list as $key => $item) {
|
|
$option_json = [
|
|
[
|
|
'option_content_type' => $item[4],
|
|
'option_content' => $item[5],
|
|
'option' => 'A',
|
|
'correct_answer' => str_contains($item[12], 'A')
|
|
],
|
|
[
|
|
'option_content_type' => $item[6],
|
|
'option_content' => $item[7],
|
|
'option' => 'B',
|
|
'correct_answer' => str_contains($item[12], 'B')
|
|
],
|
|
[
|
|
'option_content_type' => $item[8],
|
|
'option_content' => $item[9],
|
|
'option' => 'C',
|
|
'correct_answer' => str_contains($item[12], 'C')
|
|
],
|
|
[
|
|
'option_content_type' => $item[10],
|
|
'option_content' => $item[11],
|
|
'option' => 'D',
|
|
'correct_answer' => str_contains($item[12], 'D')
|
|
]
|
|
];
|
|
|
|
$inserAll[] = [
|
|
'title' => $item[0],
|
|
'question_type' =>$question_type_arr[$item[1]],
|
|
'question_content_type' => $item[2],
|
|
'question_content' => $item[3],
|
|
'option_json' => json_encode($option_json, JSON_UNESCAPED_UNICODE),
|
|
'correct_answer' => $item[12]
|
|
];
|
|
}
|
|
$this->model->insertAll($inserAll);
|
|
return success("导入成功");
|
|
|
|
} catch (\PhpOffice\PhpSpreadsheet\Reader\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|