DayStudy.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace app\api\controller;
  3. use app\model\DayStudy as DayStudyModel;
  4. use app\model\DayStudySetting;
  5. use app\model\ExamEmpResult;
  6. use app\model\ExamQuestion;
  7. use app\model\TrainCourse;
  8. /**
  9. * 每日学练
  10. * Class DayStudy
  11. * @package app\api\controller
  12. */
  13. class DayStudy extends Base
  14. {
  15. /**
  16. * 设置详情
  17. */
  18. public function setting(){
  19. $root_id = $this->request->token['root_org'];
  20. $setting = DayStudySetting::where('root_id', '=', $root_id)->find();
  21. return json(['code'=> 0, 'msg'=> '请求成功', 'data'=> $setting]);
  22. }
  23. /**
  24. * 开始学习
  25. */
  26. public function study(){
  27. $employee_id = $this->request->token['employee_id'];
  28. $find = DayStudyModel::where([['employee_id', '=', $employee_id], ['studyday', '=', date('Y-m-d')]])->find();
  29. // 生成每日学练
  30. $root_id = $this->request->token['root_org'];
  31. $study_setting = DayStudySetting::where('root_id', '=', $root_id)->find();
  32. $setting = json_decode($study_setting['setting'], true);
  33. if (empty($find)) {
  34. $select_train = [];
  35. $select_question = [];
  36. if (!empty($setting)) {
  37. foreach ($setting as $k => $v) {
  38. $select_data_ids = [];
  39. if ($v['type'] == 'train') {
  40. // 课程
  41. $course_id = TrainCourse::where([['type_id', '=', $v['id']], ['delete_time', '=', 0]])->column('id');
  42. if ($v['percent'] == 0) {
  43. continue;
  44. } else if ($v['percent'] < count($course_id)) {
  45. $array_rand = array_rand($course_id, $v['percent']);
  46. if (is_array($array_rand)) {
  47. foreach ($array_rand as $v_arr_round) {
  48. $select_data_ids[] = $course_id[$v_arr_round];
  49. }
  50. } else {
  51. $select_data_ids[] = $course_id[$array_rand];
  52. }
  53. } else {
  54. // 拥有的课程数不足时
  55. $select_data_ids = $course_id;
  56. }
  57. $select_train = array_merge($select_data_ids, $select_train);
  58. }
  59. if ($v['type'] == 'question') {
  60. // 试题
  61. $question_id = ExamQuestion::where([['cate', '=', $v['id']], ['state', '=', 1]])->column('id');
  62. if ($v['percent'] == 0) {
  63. continue;
  64. } else if ($v['percent'] < count($question_id)) {
  65. $array_rand = array_rand($question_id, $v['percent']);
  66. $select_data_ids = [];
  67. if (is_array($array_rand)) {
  68. foreach ($array_rand as $v_arr_round) {
  69. $select_data_ids[] = $question_id[$v_arr_round];
  70. }
  71. } else {
  72. $select_data_ids[] = $question_id[$array_rand];
  73. }
  74. } else {
  75. // 拥有的课程数不足时
  76. $select_data_ids = $question_id;
  77. }
  78. $select_question = array_merge($select_data_ids, $select_question);
  79. }
  80. }
  81. }
  82. $content = []; //学习内容
  83. // 要求是课程在先
  84. foreach ($select_train as $k => $v) {
  85. // 课程
  86. $content[] = [
  87. 'type'=> 'train',
  88. 'id'=> $v
  89. ];
  90. }
  91. foreach ($select_question as $k => $v) {
  92. // 试题
  93. $content[] = [
  94. 'type'=> 'question',
  95. 'id'=> $v
  96. ];
  97. }
  98. foreach ($content as $k => $v) {
  99. $content[$k]['study'] = 0; //是否学习
  100. $content[$k]['answer'] = ''; // 试题答案
  101. $content[$k]['answer_check'] = ''; // 试题答案是否正确
  102. }
  103. $save_data['studyday'] = date('Y-m-d', time());
  104. $save_data['employee_id'] = $employee_id;
  105. $save_data['content'] = json_encode($content);
  106. $save_data['root_id'] = $root_id;
  107. $save_data['status'] = 0;
  108. $result = DayStudyModel::create($save_data);
  109. $find = DayStudyModel::find($result->id);
  110. }
  111. $train_count = 0;
  112. $train_type = [];
  113. $train_done = 0; //课程学习完成量
  114. $question_count = 0;
  115. $question_type = [];
  116. $question_done = 0; //试题完成量
  117. // 已经生成每日学练
  118. $content = json_decode($find['content'], true);
  119. foreach ($content as $k => $v) {
  120. $content[$k]['info'] = [];
  121. if ($v['type'] == 'train') {
  122. $train_course = TrainCourse::find($v['id']);
  123. if (!empty($train_course)) {
  124. $content[$k]['info'] = $train_course->toArray();
  125. $train_count ++;
  126. $train_type[] = $train_course['type'];
  127. } else {
  128. unset($content[$k]);
  129. continue;
  130. }
  131. if (isset($v['study']) && $v['study']){
  132. $train_done++;
  133. }
  134. }
  135. if ($v['type'] == 'question') {
  136. $examQuestion = ExamQuestion::find($v['id']);
  137. $question_count ++;
  138. $question_type[] = $examQuestion['type'];
  139. $examQuestion['ask'] = str_replace('\\','',$examQuestion['ask']);
  140. if ($examQuestion['type'] == '单选' || $examQuestion['type'] == '多选') {
  141. $examQuestion['types'] = 1;
  142. if (!empty($examQuestion['content'])) {
  143. $con = json_decode($examQuestion['content'],true);
  144. $op = [];
  145. if (!empty($con)) {
  146. foreach ($con as $v2) {
  147. if (!empty($v2['title'])) {
  148. $op[$v2['title']] = $v2['content'];
  149. }
  150. }
  151. }
  152. $examQuestion['content'] = $op;
  153. if (!empty($examQuestion['answer'])) {
  154. $examQuestion['answer'] = explode(',',$examQuestion['answer']);
  155. } else {
  156. $examQuestion['answer'] = [];
  157. }
  158. }
  159. }elseif ($examQuestion['type']=='判断') {
  160. $examQuestion['types'] = 2;
  161. }elseif ($examQuestion['type']=='简答') {
  162. $examQuestion['types'] = 3;
  163. }
  164. $content[$k]['info'] = $examQuestion->toArray();
  165. if (isset($v['answer_check']) && $v['answer_check']){
  166. $question_done++;
  167. }
  168. }
  169. }
  170. $find['content'] = array_values($content);
  171. $train_type_list = [
  172. 'video'=> '视频',
  173. 'image'=> '图文',
  174. 'audio'=> '音频'
  175. ];
  176. $train_type_name = '';
  177. foreach ($train_type_list as $k => $v) {
  178. if (in_array($k, $train_type)) {
  179. $train_type_name .= $v.'/';
  180. }
  181. }
  182. $question_type_name = implode('/', array_unique($question_type));
  183. $find['train_count'] = $train_count;
  184. $find['train_type'] = $train_type_name;
  185. $find['question_count'] = $question_count;
  186. $find['question_type'] = $question_type_name;
  187. $find['timelimit'] = $study_setting['timelimit'];
  188. $find['setting_starttime'] = $study_setting['starttime'];
  189. $find['setting_endtime'] = $study_setting['endtime'];
  190. $find['train_done_percent'] = $train_count > 0 ? round($train_done / $train_count *100) : 0;
  191. $find['question_done_percent'] = $question_count > 0 ? round($question_done / $question_count *100) : 0;
  192. $find['study_long'] = ceil($find['study_long']/60);
  193. return json(['code'=> 0, 'data'=> $find, 'msg'=> '获取成功']);
  194. }
  195. /**
  196. * 修改学习进度
  197. */
  198. public function update_study(){
  199. $param = $this->request->only(['type', 'id', 'answer', 'status', 'end']);
  200. $employee = $this->request->token['employee_id'];
  201. $study = DayStudyModel::where([['studyday', '=', date('Y-m-d', time())], ['employee_id', '=', $employee]])->find();
  202. $result = 0;
  203. if (!empty($param['end'])) {
  204. $study->endtime = date('Y-m-d H:i:s', time());
  205. $study->status = 1;
  206. $result = $study->save();
  207. }
  208. if (isset($param['status'])) {
  209. if ($param['type'] && $param['id']) {
  210. $content = json_decode($study->content, true);
  211. foreach ($content as $k => $v) {
  212. if ($v['type'] == $param['type'] && $v['id'] == $param['id']) {
  213. $content[$k]['status'] = 1;
  214. $content[$k]['study'] = $param['status'];
  215. $content[$k]['answer'] = $param['answer']??'';
  216. if ($param['type'] == 'question' && empty($param['answer'])){
  217. //return json(['code'=> 1, 'msg'=> '请填写答案']);
  218. $content[$k]['answer_check'] = 0;
  219. } elseif($param['type'] == 'question'){
  220. $examQuestion = ExamQuestion::find($param['id']);
  221. $examQuestion['ask'] = str_replace('\\','',$examQuestion['ask']);
  222. if ($examQuestion['type']=='单选' || $examQuestion['type']=='多选' || $examQuestion['type'] == '判断') {
  223. if ($param['answer'] == $examQuestion['answer']){
  224. $content[$k]['answer_check'] = 1;
  225. } else {
  226. $content[$k]['answer_check'] = 0;
  227. }
  228. } elseif ($examQuestion['type']=='简答') {
  229. $calldata = ExamEmpResult::autoPaperAnswerCheck($param['answer'], $examQuestion['answer'], 0);
  230. $keywordsarr = array_filter(explode('|',$examQuestion['answer']));
  231. if (count($keywordsarr) > 0){
  232. if ((count($keywordsarr) > 1 && $calldata['gotkeys'] > 1) || (count($keywordsarr) == 1 && $calldata['gotkeys'] == 1)) {
  233. $content[$k]['answer_check'] = 1;
  234. } else {
  235. $content[$k]['answer_check'] = 0;
  236. }
  237. } else {
  238. // 没有关键词
  239. $content[$k]['answer_check'] = 1;
  240. }
  241. }
  242. }
  243. }
  244. }
  245. $study->content = json_encode($content);
  246. $result = $study->save();
  247. }
  248. }
  249. if ($result !== false) {
  250. return json(['code'=> 0, 'msg'=> '操作成功']);
  251. } else {
  252. return json(['code'=> 1, 'msg'=> '操作失败']);
  253. }
  254. }
  255. /**
  256. * 调整学习时长
  257. */
  258. public function change_study_long(){
  259. $time = input('time', 0, 'intval');
  260. $employee_id = $this->request->token['employee_id'];
  261. $find = DayStudyModel::where([['employee_id', '=', $employee_id], ['studyday', '=', date('Y-m-d')]])->find();
  262. if (!empty($find)){
  263. $find->study_long = (int)$find['study_long'] + $time;
  264. $find->save();
  265. } else {
  266. DayStudyModel::where([['employee_id', '=', $employee_id], ['studyday', '=', date('Y-m-d')]])->save(['study_long'=> $time]);
  267. }
  268. return json(['code'=> self::success, 'msg'=> '']);
  269. }
  270. }