123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?php
- namespace app\sys\controller;
- use app\model\DayStudySetting;
- use app\model\Setting;
- use app\model\TrainCourse;
- use app\model\TrainType;
- use think\facade\View;
- use app\model\ExamQuestion;
- class DayStudy
- {
- /**
- * 设置页
- * @return string
- */
- public function index(){
- $root_id = request()->employee->root_id;
- $info = DayStudySetting::where('root_id', '=', $root_id)->find();
- if (!empty($info)) {
- $setting = json_decode($info['setting'], true);
- $train_type = [];
- $question_cate = [];
- if (!empty($setting)) {
- foreach ($setting as $k => $v){
- if ($v['type'] == 'train') {
- $train_type[] = $v['id'];
- }
- if ($v['type'] == 'question') {
- $question_cate[] = $v['id'];
- }
- }
- }
- // 已选课件场景
- $train_type_select = TrainType::where([['id', 'in', $train_type]])->column('type');
- $info['train_type_name'] = implode('/', $train_type_select);
- // 已选课件类型
- $train_class_type = TrainCourse::where([['type_id', 'in', $train_type]])->group('type')->column('type');
- $type_list = [
- 'video'=> '视频',
- 'image'=> '图文',
- 'audio'=> '音频'
- ];
- $type_name = '';
- foreach ($type_list as $kk => $vv) {
- if (in_array($kk, $train_class_type)) {
- $type_name .= $vv.'/';
- }
- }
- $info['train_class_type'] = trim($type_name, '/');
- // 已选试题分类
- $info['question_cate_name'] = implode('/', $question_cate);
- // 已选试题类型
- $question_type = ExamQuestion::where([['cate', 'in', $question_cate]])->group('type')->column('type');
- $info['question_type'] = implode('/', $question_type);
- $info['train_type_count'] = count($train_type_select);
- $info['train_count'] = TrainCourse::where([['type_id', 'in', $train_type]])->count();
- $info['question_cate_count'] = count($question_cate);
- $info['question_count'] = ExamQuestion::where([['cate', 'in', $question_cate]])->count();
- $info['total_count'] = $info['train_count'] + $info['question_count'];
- } else {
- $info['status'] = 0;
- }
- View::assign('data', $info);
- return View::fetch();
- }
- /**
- * 设置每日学练
- * @return string
- */
- public function setting(){
- if (request()->isPost()) {
- $param = request()->only(['status', 'amount', 'starttime', 'endtime', 'setting', 'timelimit']);
- $data = [];
- if(isset($param['status'])){
- $data['status'] = $param['status'];
- }
- if(isset($param['amount'])){
- $data['amount'] = $param['amount'];
- }
- if(isset($param['starttime'])){
- $data['starttime'] = $param['starttime'];
- }
- if(isset($param['endtime'])){
- $data['endtime'] = $param['endtime'];
- }
- if(isset($param['setting'])){
- $all_amount = array_sum(array_column($param['setting'], 'percent'));
- $root_id = request()->employee->root_id;
- $find = DayStudySetting::where('root_id', '=', $root_id)->find();
- if (empty($find) || $all_amount > $find['amount']){
- return json(['code'=> 1, 'msg'=> '随机出题总量不能大于每日学习量']);
- }
- $data['setting'] = json_encode($param['setting']);
- }
- if(isset($param['timelimit'])){
- $data['timelimit'] = $param['timelimit'];
- }
- $root_id = request()->employee->root_id;
- $find = DayStudySetting::where('root_id', '=', $root_id)->find();
- if (empty($find)) {
- $data['root_id'] = request()->employee->root_id;
- $result = DayStudySetting::create($data);
- } else {
- $result = $find->save($data);
- }
- if ($result !== false) {
- return json(['code'=> 0, 'msg'=> '保存成功']);
- }
- } else {
- $root_id = request()->employee->root_id;
- $find = DayStudySetting::where('root_id', '=', $root_id)->find();
- View::assign('data', $find);
- return View::fetch();
- }
- }
- /**
- * 设置学练内容详情
- */
- public function setting_detail(){
- $root_id = request()->employee->root_id;
- $find = DayStudySetting::where('root_id', '=', $root_id)->find();
- $page = input('page', 1, 'intval');
- $type = input('type', '', 'trim');
- if (empty($find)) {
- return json(['code'=> 0, 'data'=> []]);
- }
- $setting = json_decode($find['setting'], true);
- $list = [];
- if (!empty($setting)) {
- foreach ($setting as $k => $v) {
- if ($v['type'] == 'train') {
- if ($type == '' || $type == 'train') {
- $setting[$k]['type_name'] = '课件';
- $setting[$k]['name'] = TrainType::where('id', '=', $v['id'])->value('type');
- $train_type = TrainCourse::where('type_id', '=', $v['id'])->group('type')->column('type');
- $type_list = [
- 'video'=> '视频',
- 'image'=> '图文',
- 'audio'=> '音频'
- ];
- $type_name = '';
- foreach ($type_list as $kk => $vv) {
- if (in_array($kk, $train_type)) {
- $type_name .= $vv.'/';
- }
- }
- $setting[$k]['child_type'] = trim($type_name, '/');
- $setting[$k]['child_count'] = TrainCourse::where([['type_id', '=', $v['id']], ['delete_time', '=', 0]])->count();
- $list[] = $setting[$k];
- }
- } elseif($v['type'] == 'question') {
- if ($type == 'question' || $type == '') {
- $setting[$k]['type_name'] = '试题';
- $setting[$k]['name'] = $v['id'];
- $question_type = ExamQuestion::where('cate', '=', $v['id'])->group('type')->column('type');
- $setting[$k]['child_type'] = implode('/', $question_type);
- $setting[$k]['child_count'] = ExamQuestion::where([['cate', '=', $v['id']], ['state', '=', 1]])->count();
- $list[] = $setting[$k];
- }
- }
- }
- } else {
- $list = [];
- }
- $start = ($page-1) * 10;
- $return_list = array_slice($list, $start, 10);
- return json(['code'=> 0, 'data'=> $return_list, 'count'=> count($list)]);
- }
- /**
- * 获取课程场景
- */
- public function getTrainType(){
- $root_id = request()->employee->root_id;
- $find = DayStudySetting::where('root_id', '=', $root_id)->find();
- if (empty($find)) {
- $setting = [];
- } else {
- $setting = json_decode($find['setting'], true);
- }
- $list = TrainType::where('root_id', '=', $root_id)->select()->each(function ($item) use ($setting){
- $train_type = TrainCourse::where([['type_id', '=', $item['id']], ['delete_time', '=', 0]])->group('type')->column('type');
- $type_list = [
- 'video'=> '视频',
- 'image'=> '图文',
- 'audio'=> '音频'
- ];
- $type_name = '';
- foreach ($type_list as $k => $v) {
- if (in_array($k, $train_type)) {
- $type_name .= $v.'/';
- }
- }
- $item['child_type'] = trim($type_name, '/');
- $item['child_count'] = TrainCourse::where([['type_id', '=', $item['id']], ['delete_time', '=', 0]])->count();
- $item['percent'] = 0;
- $item['selected'] = 0;
- if (!empty($setting)) {
- foreach ($setting as $kk => $vv) {
- if ($vv['type'] == 'train' && $vv['id'] == $item['id']) {
- $item['selected'] = 1;
- $item['percent'] = $vv['percent'];
- }
- }
- }
- });
- return json(['code'=> 0, 'data'=> $list->toArray()]);
- }
- /**
- * 获取试题分类
- */
- public function getQuestionCate(){
- $root_id = request()->employee->root_id;
- $cate = Setting::where([['name', '=', 'questioncate'],['root_id', '=', $root_id], ['state', '=', 1]])->find();
- $find = DayStudySetting::where('root_id', '=', $root_id)->find();
- if (empty($find)) {
- $setting = [];
- } else {
- $setting = json_decode($find['setting'], true);
- }
- $list = json_decode($cate['content'], true);
- $data = [];
- foreach ($list as $k => $v) {
- $question_type = ExamQuestion::where([['cate', '=', $v], ['state', '=', 1]])->group('type')->column('type');
- $data[$k]['child_type'] = implode('/', $question_type);
- $data[$k]['child_count'] = ExamQuestion::where([['cate', '=', $v], ['state', '=', 1]])->count();
- $data[$k]['percent'] = 0;
- $data[$k]['id'] = $v;
- $data[$k]['name'] = $v;
- $data[$k]['selected'] = 0; //是否已选
- if (!empty($setting)) {
- foreach($setting as $kk => $vv){
- if ($vv['type'] == 'question' && $vv['id'] == $v){
- $data[$k]['selected'] = 1;
- $data[$k]['percent'] = $vv['percent'];
- }
- }
- }
- }
- return json(['code'=> 0, 'data'=> $data]);
- }
- }
|