1
0

DayStudy.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace app\sys\controller;
  3. use app\model\DayStudySetting;
  4. use app\model\Setting;
  5. use app\model\TrainCourse;
  6. use app\model\TrainType;
  7. use think\facade\View;
  8. use app\model\ExamQuestion;
  9. class DayStudy
  10. {
  11. /**
  12. * 设置页
  13. * @return string
  14. */
  15. public function index(){
  16. $root_id = request()->employee->root_id;
  17. $info = DayStudySetting::where('root_id', '=', $root_id)->find();
  18. if (!empty($info)) {
  19. $setting = json_decode($info['setting'], true);
  20. $train_type = [];
  21. $question_cate = [];
  22. if (!empty($setting)) {
  23. foreach ($setting as $k => $v){
  24. if ($v['type'] == 'train') {
  25. $train_type[] = $v['id'];
  26. }
  27. if ($v['type'] == 'question') {
  28. $question_cate[] = $v['id'];
  29. }
  30. }
  31. }
  32. // 已选课件场景
  33. $train_type_select = TrainType::where([['id', 'in', $train_type]])->column('type');
  34. $info['train_type_name'] = implode('/', $train_type_select);
  35. // 已选课件类型
  36. $train_class_type = TrainCourse::where([['type_id', 'in', $train_type]])->group('type')->column('type');
  37. $type_list = [
  38. 'video'=> '视频',
  39. 'image'=> '图文',
  40. 'audio'=> '音频'
  41. ];
  42. $type_name = '';
  43. foreach ($type_list as $kk => $vv) {
  44. if (in_array($kk, $train_class_type)) {
  45. $type_name .= $vv.'/';
  46. }
  47. }
  48. $info['train_class_type'] = trim($type_name, '/');
  49. // 已选试题分类
  50. $info['question_cate_name'] = implode('/', $question_cate);
  51. // 已选试题类型
  52. $question_type = ExamQuestion::where([['cate', 'in', $question_cate]])->group('type')->column('type');
  53. $info['question_type'] = implode('/', $question_type);
  54. $info['train_type_count'] = count($train_type_select);
  55. $info['train_count'] = TrainCourse::where([['type_id', 'in', $train_type]])->count();
  56. $info['question_cate_count'] = count($question_cate);
  57. $info['question_count'] = ExamQuestion::where([['cate', 'in', $question_cate]])->count();
  58. $info['total_count'] = $info['train_count'] + $info['question_count'];
  59. } else {
  60. $info['status'] = 0;
  61. }
  62. View::assign('data', $info);
  63. return View::fetch();
  64. }
  65. /**
  66. * 设置每日学练
  67. * @return string
  68. */
  69. public function setting(){
  70. if (request()->isPost()) {
  71. $param = request()->only(['status', 'amount', 'starttime', 'endtime', 'setting', 'timelimit']);
  72. $data = [];
  73. if(isset($param['status'])){
  74. $data['status'] = $param['status'];
  75. }
  76. if(isset($param['amount'])){
  77. $data['amount'] = $param['amount'];
  78. }
  79. if(isset($param['starttime'])){
  80. $data['starttime'] = $param['starttime'];
  81. }
  82. if(isset($param['endtime'])){
  83. $data['endtime'] = $param['endtime'];
  84. }
  85. if(isset($param['setting'])){
  86. $all_amount = array_sum(array_column($param['setting'], 'percent'));
  87. $root_id = request()->employee->root_id;
  88. $find = DayStudySetting::where('root_id', '=', $root_id)->find();
  89. if (empty($find) || $all_amount > $find['amount']){
  90. return json(['code'=> 1, 'msg'=> '随机出题总量不能大于每日学习量']);
  91. }
  92. $data['setting'] = json_encode($param['setting']);
  93. }
  94. if(isset($param['timelimit'])){
  95. $data['timelimit'] = $param['timelimit'];
  96. }
  97. $root_id = request()->employee->root_id;
  98. $find = DayStudySetting::where('root_id', '=', $root_id)->find();
  99. if (empty($find)) {
  100. $data['root_id'] = request()->employee->root_id;
  101. $result = DayStudySetting::create($data);
  102. } else {
  103. $result = $find->save($data);
  104. }
  105. if ($result !== false) {
  106. return json(['code'=> 0, 'msg'=> '保存成功']);
  107. }
  108. } else {
  109. $root_id = request()->employee->root_id;
  110. $find = DayStudySetting::where('root_id', '=', $root_id)->find();
  111. View::assign('data', $find);
  112. return View::fetch();
  113. }
  114. }
  115. /**
  116. * 设置学练内容详情
  117. */
  118. public function setting_detail(){
  119. $root_id = request()->employee->root_id;
  120. $find = DayStudySetting::where('root_id', '=', $root_id)->find();
  121. $page = input('page', 1, 'intval');
  122. $type = input('type', '', 'trim');
  123. if (empty($find)) {
  124. return json(['code'=> 0, 'data'=> []]);
  125. }
  126. $setting = json_decode($find['setting'], true);
  127. $list = [];
  128. if (!empty($setting)) {
  129. foreach ($setting as $k => $v) {
  130. if ($v['type'] == 'train') {
  131. if ($type == '' || $type == 'train') {
  132. $setting[$k]['type_name'] = '课件';
  133. $setting[$k]['name'] = TrainType::where('id', '=', $v['id'])->value('type');
  134. $train_type = TrainCourse::where('type_id', '=', $v['id'])->group('type')->column('type');
  135. $type_list = [
  136. 'video'=> '视频',
  137. 'image'=> '图文',
  138. 'audio'=> '音频'
  139. ];
  140. $type_name = '';
  141. foreach ($type_list as $kk => $vv) {
  142. if (in_array($kk, $train_type)) {
  143. $type_name .= $vv.'/';
  144. }
  145. }
  146. $setting[$k]['child_type'] = trim($type_name, '/');
  147. $setting[$k]['child_count'] = TrainCourse::where([['type_id', '=', $v['id']], ['delete_time', '=', 0]])->count();
  148. $list[] = $setting[$k];
  149. }
  150. } elseif($v['type'] == 'question') {
  151. if ($type == 'question' || $type == '') {
  152. $setting[$k]['type_name'] = '试题';
  153. $setting[$k]['name'] = $v['id'];
  154. $question_type = ExamQuestion::where('cate', '=', $v['id'])->group('type')->column('type');
  155. $setting[$k]['child_type'] = implode('/', $question_type);
  156. $setting[$k]['child_count'] = ExamQuestion::where([['cate', '=', $v['id']], ['state', '=', 1]])->count();
  157. $list[] = $setting[$k];
  158. }
  159. }
  160. }
  161. } else {
  162. $list = [];
  163. }
  164. $start = ($page-1) * 10;
  165. $return_list = array_slice($list, $start, 10);
  166. return json(['code'=> 0, 'data'=> $return_list, 'count'=> count($list)]);
  167. }
  168. /**
  169. * 获取课程场景
  170. */
  171. public function getTrainType(){
  172. $root_id = request()->employee->root_id;
  173. $find = DayStudySetting::where('root_id', '=', $root_id)->find();
  174. if (empty($find)) {
  175. $setting = [];
  176. } else {
  177. $setting = json_decode($find['setting'], true);
  178. }
  179. $list = TrainType::where('root_id', '=', $root_id)->select()->each(function ($item) use ($setting){
  180. $train_type = TrainCourse::where([['type_id', '=', $item['id']], ['delete_time', '=', 0]])->group('type')->column('type');
  181. $type_list = [
  182. 'video'=> '视频',
  183. 'image'=> '图文',
  184. 'audio'=> '音频'
  185. ];
  186. $type_name = '';
  187. foreach ($type_list as $k => $v) {
  188. if (in_array($k, $train_type)) {
  189. $type_name .= $v.'/';
  190. }
  191. }
  192. $item['child_type'] = trim($type_name, '/');
  193. $item['child_count'] = TrainCourse::where([['type_id', '=', $item['id']], ['delete_time', '=', 0]])->count();
  194. $item['percent'] = 0;
  195. $item['selected'] = 0;
  196. if (!empty($setting)) {
  197. foreach ($setting as $kk => $vv) {
  198. if ($vv['type'] == 'train' && $vv['id'] == $item['id']) {
  199. $item['selected'] = 1;
  200. $item['percent'] = $vv['percent'];
  201. }
  202. }
  203. }
  204. });
  205. return json(['code'=> 0, 'data'=> $list->toArray()]);
  206. }
  207. /**
  208. * 获取试题分类
  209. */
  210. public function getQuestionCate(){
  211. $root_id = request()->employee->root_id;
  212. $cate = Setting::where([['name', '=', 'questioncate'],['root_id', '=', $root_id], ['state', '=', 1]])->find();
  213. $find = DayStudySetting::where('root_id', '=', $root_id)->find();
  214. if (empty($find)) {
  215. $setting = [];
  216. } else {
  217. $setting = json_decode($find['setting'], true);
  218. }
  219. $list = json_decode($cate['content'], true);
  220. $data = [];
  221. foreach ($list as $k => $v) {
  222. $question_type = ExamQuestion::where([['cate', '=', $v], ['state', '=', 1]])->group('type')->column('type');
  223. $data[$k]['child_type'] = implode('/', $question_type);
  224. $data[$k]['child_count'] = ExamQuestion::where([['cate', '=', $v], ['state', '=', 1]])->count();
  225. $data[$k]['percent'] = 0;
  226. $data[$k]['id'] = $v;
  227. $data[$k]['name'] = $v;
  228. $data[$k]['selected'] = 0; //是否已选
  229. if (!empty($setting)) {
  230. foreach($setting as $kk => $vv){
  231. if ($vv['type'] == 'question' && $vv['id'] == $v){
  232. $data[$k]['selected'] = 1;
  233. $data[$k]['percent'] = $vv['percent'];
  234. }
  235. }
  236. }
  237. }
  238. return json(['code'=> 0, 'data'=> $data]);
  239. }
  240. }