123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- namespace app\api\controller;
- use app\model\DayStudy as DayStudyModel;
- use app\model\DayStudySetting;
- use app\model\ExamEmpResult;
- use app\model\ExamQuestion;
- use app\model\TrainCourse;
- /**
- * 每日学练
- * Class DayStudy
- * @package app\api\controller
- */
- class DayStudy extends Base
- {
- /**
- * 设置详情
- */
- public function setting(){
- $root_id = $this->request->token['root_org'];
- $setting = DayStudySetting::where('root_id', '=', $root_id)->find();
- return json(['code'=> 0, 'msg'=> '请求成功', 'data'=> $setting]);
- }
- /**
- * 开始学习
- */
- public function study(){
- $employee_id = $this->request->token['employee_id'];
- $find = DayStudyModel::where([['employee_id', '=', $employee_id], ['studyday', '=', date('Y-m-d')]])->find();
- // 生成每日学练
- $root_id = $this->request->token['root_org'];
- $study_setting = DayStudySetting::where('root_id', '=', $root_id)->find();
- $setting = json_decode($study_setting['setting'], true);
- if (empty($find)) {
- $select_train = [];
- $select_question = [];
- if (!empty($setting)) {
- foreach ($setting as $k => $v) {
- $select_data_ids = [];
- if ($v['type'] == 'train') {
- // 课程
- $course_id = TrainCourse::where([['type_id', '=', $v['id']], ['delete_time', '=', 0]])->column('id');
- if ($v['percent'] == 0) {
- continue;
- } else if ($v['percent'] < count($course_id)) {
- $array_rand = array_rand($course_id, $v['percent']);
- if (is_array($array_rand)) {
- foreach ($array_rand as $v_arr_round) {
- $select_data_ids[] = $course_id[$v_arr_round];
- }
- } else {
- $select_data_ids[] = $course_id[$array_rand];
- }
- } else {
- // 拥有的课程数不足时
- $select_data_ids = $course_id;
- }
- $select_train = array_merge($select_data_ids, $select_train);
- }
- if ($v['type'] == 'question') {
- // 试题
- $question_id = ExamQuestion::where([['cate', '=', $v['id']], ['state', '=', 1]])->column('id');
- if ($v['percent'] == 0) {
- continue;
- } else if ($v['percent'] < count($question_id)) {
- $array_rand = array_rand($question_id, $v['percent']);
- $select_data_ids = [];
- if (is_array($array_rand)) {
- foreach ($array_rand as $v_arr_round) {
- $select_data_ids[] = $question_id[$v_arr_round];
- }
- } else {
- $select_data_ids[] = $question_id[$array_rand];
- }
- } else {
- // 拥有的课程数不足时
- $select_data_ids = $question_id;
- }
- $select_question = array_merge($select_data_ids, $select_question);
- }
- }
- }
- $content = []; //学习内容
- // 要求是课程在先
- foreach ($select_train as $k => $v) {
- // 课程
- $content[] = [
- 'type'=> 'train',
- 'id'=> $v
- ];
- }
- foreach ($select_question as $k => $v) {
- // 试题
- $content[] = [
- 'type'=> 'question',
- 'id'=> $v
- ];
- }
- foreach ($content as $k => $v) {
- $content[$k]['study'] = 0; //是否学习
- $content[$k]['answer'] = ''; // 试题答案
- $content[$k]['answer_check'] = ''; // 试题答案是否正确
- }
- $save_data['studyday'] = date('Y-m-d', time());
- $save_data['employee_id'] = $employee_id;
- $save_data['content'] = json_encode($content);
- $save_data['root_id'] = $root_id;
- $save_data['status'] = 0;
- $result = DayStudyModel::create($save_data);
- $find = DayStudyModel::find($result->id);
- }
- $train_count = 0;
- $train_type = [];
- $train_done = 0; //课程学习完成量
- $question_count = 0;
- $question_type = [];
- $question_done = 0; //试题完成量
- // 已经生成每日学练
- $content = json_decode($find['content'], true);
- foreach ($content as $k => $v) {
- $content[$k]['info'] = [];
- if ($v['type'] == 'train') {
- $train_course = TrainCourse::find($v['id']);
- if (!empty($train_course)) {
- $content[$k]['info'] = $train_course->toArray();
- $train_count ++;
- $train_type[] = $train_course['type'];
- } else {
- unset($content[$k]);
- continue;
- }
- if (isset($v['study']) && $v['study']){
- $train_done++;
- }
- }
- if ($v['type'] == 'question') {
- $examQuestion = ExamQuestion::find($v['id']);
- $question_count ++;
- $question_type[] = $examQuestion['type'];
- $examQuestion['ask'] = str_replace('\\','',$examQuestion['ask']);
- if ($examQuestion['type'] == '单选' || $examQuestion['type'] == '多选') {
- $examQuestion['types'] = 1;
- if (!empty($examQuestion['content'])) {
- $con = json_decode($examQuestion['content'],true);
- $op = [];
- if (!empty($con)) {
- foreach ($con as $v2) {
- if (!empty($v2['title'])) {
- $op[$v2['title']] = $v2['content'];
- }
- }
- }
- $examQuestion['content'] = $op;
- if (!empty($examQuestion['answer'])) {
- $examQuestion['answer'] = explode(',',$examQuestion['answer']);
- } else {
- $examQuestion['answer'] = [];
- }
- }
- }elseif ($examQuestion['type']=='判断') {
- $examQuestion['types'] = 2;
- }elseif ($examQuestion['type']=='简答') {
- $examQuestion['types'] = 3;
- }
- $content[$k]['info'] = $examQuestion->toArray();
- if (isset($v['answer_check']) && $v['answer_check']){
- $question_done++;
- }
- }
- }
- $find['content'] = array_values($content);
- $train_type_list = [
- 'video'=> '视频',
- 'image'=> '图文',
- 'audio'=> '音频'
- ];
- $train_type_name = '';
- foreach ($train_type_list as $k => $v) {
- if (in_array($k, $train_type)) {
- $train_type_name .= $v.'/';
- }
- }
- $question_type_name = implode('/', array_unique($question_type));
- $find['train_count'] = $train_count;
- $find['train_type'] = $train_type_name;
- $find['question_count'] = $question_count;
- $find['question_type'] = $question_type_name;
- $find['timelimit'] = $study_setting['timelimit'];
- $find['setting_starttime'] = $study_setting['starttime'];
- $find['setting_endtime'] = $study_setting['endtime'];
- $find['train_done_percent'] = $train_count > 0 ? round($train_done / $train_count *100) : 0;
- $find['question_done_percent'] = $question_count > 0 ? round($question_done / $question_count *100) : 0;
- $find['study_long'] = ceil($find['study_long']/60);
- return json(['code'=> 0, 'data'=> $find, 'msg'=> '获取成功']);
- }
- /**
- * 修改学习进度
- */
- public function update_study(){
- $param = $this->request->only(['type', 'id', 'answer', 'status', 'end']);
- $employee = $this->request->token['employee_id'];
- $study = DayStudyModel::where([['studyday', '=', date('Y-m-d', time())], ['employee_id', '=', $employee]])->find();
- $result = 0;
- if (!empty($param['end'])) {
- $study->endtime = date('Y-m-d H:i:s', time());
- $study->status = 1;
- $result = $study->save();
- }
- if (isset($param['status'])) {
- if ($param['type'] && $param['id']) {
- $content = json_decode($study->content, true);
- foreach ($content as $k => $v) {
- if ($v['type'] == $param['type'] && $v['id'] == $param['id']) {
- $content[$k]['status'] = 1;
- $content[$k]['study'] = $param['status'];
- $content[$k]['answer'] = $param['answer']??'';
- if ($param['type'] == 'question' && empty($param['answer'])){
- //return json(['code'=> 1, 'msg'=> '请填写答案']);
- $content[$k]['answer_check'] = 0;
- } elseif($param['type'] == 'question'){
- $examQuestion = ExamQuestion::find($param['id']);
- $examQuestion['ask'] = str_replace('\\','',$examQuestion['ask']);
- if ($examQuestion['type']=='单选' || $examQuestion['type']=='多选' || $examQuestion['type'] == '判断') {
- if ($param['answer'] == $examQuestion['answer']){
- $content[$k]['answer_check'] = 1;
- } else {
- $content[$k]['answer_check'] = 0;
- }
- } elseif ($examQuestion['type']=='简答') {
- $calldata = ExamEmpResult::autoPaperAnswerCheck($param['answer'], $examQuestion['answer'], 0);
- $keywordsarr = array_filter(explode('|',$examQuestion['answer']));
- if (count($keywordsarr) > 0){
- if ((count($keywordsarr) > 1 && $calldata['gotkeys'] > 1) || (count($keywordsarr) == 1 && $calldata['gotkeys'] == 1)) {
- $content[$k]['answer_check'] = 1;
- } else {
- $content[$k]['answer_check'] = 0;
- }
- } else {
- // 没有关键词
- $content[$k]['answer_check'] = 1;
- }
- }
- }
- }
- }
- $study->content = json_encode($content);
- $result = $study->save();
- }
- }
- if ($result !== false) {
- return json(['code'=> 0, 'msg'=> '操作成功']);
- } else {
- return json(['code'=> 1, 'msg'=> '操作失败']);
- }
- }
- /**
- * 调整学习时长
- */
- public function change_study_long(){
- $time = input('time', 0, 'intval');
- $employee_id = $this->request->token['employee_id'];
- $find = DayStudyModel::where([['employee_id', '=', $employee_id], ['studyday', '=', date('Y-m-d')]])->find();
- if (!empty($find)){
- $find->study_long = (int)$find['study_long'] + $time;
- $find->save();
- } else {
- DayStudyModel::where([['employee_id', '=', $employee_id], ['studyday', '=', date('Y-m-d')]])->save(['study_long'=> $time]);
- }
- return json(['code'=> self::success, 'msg'=> '']);
- }
- }
|