123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace App\Admin\Controllers;
- use App\Article;
- use App\Articletype;
- use App\Exam;
- use App\Examarg;
- use App\Http\Controllers\Controller;
- use App\Question;
- use App\School;
- use App\Sclass;
- use App\User;
- use Encore\Admin\Widgets\InfoBox;
- use DB;
- use Illuminate\Http\Request;
- use function EasyWeChat\Kernel\data_to_array;
- use Encore\Admin\Controllers\Dashboard;
- use Encore\Admin\Facades\Admin;
- use Encore\Admin\Layout\Column;
- use Encore\Admin\Layout\Content;
- use Encore\Admin\Layout\Row;
- use Illuminate\Support\Facades\Storage;
- use Maatwebsite\Excel\Facades\Excel;
- class HomeController extends Controller
- {
- public function index(Request $request)
- {
- return Admin::content(function (Content $content) use ($request) {
- $content->header('统计');
- $content->description('微考试信息');
- $start_time = $request->get('start_time');
- $stop_time = $request->get('stop_time');
- $content->row(function ($row) use ($request) {
- $school_num = School::count();
- $sclass_num = Sclass::count();
- $student_num = User::count();
- $question_num = Question::count();
- $art_count = Article::count();
- $exam_count = Examarg::count();
- //$row->column(2, new InfoBox('总项目数', 'users', 'aqua', '/demo/users', Project::filter($request)->count('*')));
- $row->column(2, new InfoBox('学校数量', 'bank', 'green', '/admin/school', $school_num));
- $row->column(2, new InfoBox('班级数量', 'asl-interpreting', 'red', '/admin/sclass', $sclass_num));
- $row->column(2, new InfoBox('学生数量', 'graduation-cap', 'yellow', '/admin/users', $student_num));
- $row->column(2, new InfoBox('试卷数', 'twitch', 'purple', '/admin/examarg', $exam_count));
- $row->column(2, new InfoBox('题目数量', 'file-text', 'purple', '/admin/question', $question_num));
- $row->column(2, new InfoBox('文章总数', 'bars', 'purple', '/admin/article', $art_count));
- });
- $bills = (User::get([DB::raw('DATE_FORMAT(created_at,"%Y-%m") as ym'), DB::raw('DATE_FORMAT(created_at,"%Y") as year'), DB::raw('DATE_FORMAT(created_at,"%m") as month')]));
- $content->row(function ($row) use ($bills) {
- $studentCount = collect($bills)->groupBy('ym')->map(function ($v, $k) {
- // Salary::where('time', 'like', "{$k}%")->where('ac', 'F')->sum('money')
- return (collect($v)->count() + 0);
- })->toArray();
- $keys = array_keys(array_flip(array_keys($studentCount)));
- $row->column(12, view('admin.charts.line', [
- 'title' => '每月学生增加数',
- 'come' => ($this->getValue($studentCount, $keys)),
- 'keys' => $keys
- ]));
- });
- $school = (School::get([DB::raw('DATE_FORMAT(created_at,"%Y-%m") as ym'), DB::raw('DATE_FORMAT(created_at,"%Y") as year'), DB::raw('DATE_FORMAT(created_at,"%m") as month'), DB::raw('schools.*')]));
- $sclass = (Sclass::get([DB::raw('DATE_FORMAT(created_at,"%Y-%m") as ym'), DB::raw('DATE_FORMAT(created_at,"%Y") as year'), DB::raw('DATE_FORMAT(created_at,"%m") as month')]));
- $article = (Article::get([DB::raw('DATE_FORMAT(created_at,"%Y-%m") as ym'), DB::raw('DATE_FORMAT(created_at,"%Y") as year'), DB::raw('DATE_FORMAT(created_at,"%m") as month'), DB::raw('articles.*')]));
- $content->row(function ($row) use ($school, $sclass, $article) {
- $scloo_num = collect($school)->groupBy('ym')->map(function ($v, $k) {
- return (collect($v)->count() + 0);
- })->toArray();
- $sclass_num = collect($sclass)->groupBy('ym')->map(function ($v, $k) {
- // Salary::where('time', 'like', "{$k}%")->where('ac', 'F')->sum('money')
- return (collect($v)->count() + 0);
- })->toArray();
- $article_num = collect($article)->groupBy('ym')->map(function ($v, $k) {
- // Salary::where('time', 'like', "{$k}%")->where('ac', 'F')->sum('money')
- return (collect($v)->count() + 0);
- })->toArray();
- $keys = array_keys(array_flip(array_keys($article_num)));
- $row->column(12, view('admin.charts.sline', [
- 'title' => '每月学校班级等增加数',
- 'school' => ($this->getValue($scloo_num, $keys)),
- "sclass" => ($this->getValue($sclass_num, $keys)),
- "article" => ($this->getValue($article_num, $keys)),
- 'keys' => $keys,
- ]));
- });
- });
- }
- public function getValue($arr, $keys)
- {
- return collect($keys)->map(function ($v) use ($arr) {
- return isset($arr[$v]) ? $arr[$v] : 0;
- })->values();
- }
- public function getSchoolApi(Request $request)
- {
- $q = $request->get('q');
- return School::where('name', 'like', "%$q%")->paginate(null, ['id', 'name as text']);
- }
- public function getClassApi(Request $request)
- {
- $q = $request->get('q');
- return Sclass::where('school_id', $q)->select('id', 'name as text')->get();
- }
- public function getArticleTypeApi(Request $request)
- {
- return Articletype::select('id', 'name as text')->get();
- }
- public function getExams()
- {
- return Exam::select('id', 'name as text')->get();
- }
- public function getSchoolSclass()
- {
- $allsclass = Sclass::get();
- $selectdata = [];
- foreach ($allsclass as $key => $value) {
- $selectdata[] = array(
- 'id' => $value['id'],
- "text" => $value['school']['name'] . '-' . $value['name']
- );
- }
- return $selectdata;
- }
- protected function upload(Request $request)
- {
- //图片存储方法。如果为图片存起来。如果不是返回''
- $fileCharater = $request->file('file');
- if (!$fileCharater) {
- $msg = '请选择上传文件';
- return view('admin.tools.excelskip', compact('msg'));
- }
- if ($fileCharater->isValid()) { //括号里面的是必须加的哦
- //如果括号里面的不加上的话,下面的方法也无法调用的
- $originalName = $fileCharater->getClientOriginalName(); // 文件原名
- $ext = $fileCharater->getClientOriginalExtension(); // 扩展名
- $realPath = $fileCharater->getRealPath(); //临时文件的绝对路径
- $type = $fileCharater->getClientMimeType(); // image/jpeg
- if (strpos($type, 'vnd.openxmlformats-officedocument.spreadsheetml.sheet') === false) {
- return '非法格式';
- }
- //// // 上传文件
- $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . 'xlsx';
- // 使用我们新建的uploads本地存储空间(目录)
- //这里的uploads是配置文件的名称
- $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
- if ($bool) {
- $filePath = 'storage/app/public/uploads/' . $filename;
- $data = [];
- Excel::load($filePath, function ($reader) use (&$data) {
- $reader = $reader->getSheet(0);
- $data = $reader->toArray();
- });
- array_splice($data, 0, 2);
- $questions = [];
- foreach ($data as $key => $row) {
- $question['name'] = $row[1];
- $question['category'] = array_search($row[2], Question::$categoryMap);
- $question['type'] = $row[3];
- $question['answers'] = [];
- for ($i = 0; $i < strlen($row[4]); $i++) {
- $question['answers'][] = strtoupper(substr($row[4], $i, 1));
- }
- $question['options'] = [];
- if ($row[5] !== null) $question['options'][] = ['name' => 'A', 'val' => $row[5]];
- if ($row[6] !== null) $question['options'][] = ['name' => 'B', 'val' => $row[6]];
- if ($row[7] !== null) $question['options'][] = ['name' => 'C', 'val' => $row[7]];
- if ($row[8] !== null) $question['options'][] = ['name' => 'D', 'val' => $row[8]];
- if ($row[9] !== null) $question['options'][] = ['name' => 'E', 'val' => $row[9]];
- if ($row[10] !== null) $question['options'][] = ['name' => 'F', 'val' => $row[10]];
- if ($row[11] !== null) $question['options'][] = ['name' => 'G', 'val' => $row[11]];
- if ($question['name'] && $question['type'] && count($question['answers']) && count($question['options'])) {
- $model = new Question($question);
- $model->save();
- }
- }
- $msg = "上传成功";
- return view('admin.tools.excelskip', compact('msg'));
- }
- return "文件错误只能使用模板上传";
- }
- }
- // protected function upload(Request $request)
- // {
- // //图片存储方法。如果为图片存起来。如果不是返回''
- // $fileCharater = $request->file('file');
- //
- // if ($fileCharater->isValid()) { //括号里面的是必须加的哦
- // //如果括号里面的不加上的话,下面的方法也无法调用的
- //
- // $originalName = $fileCharater->getClientOriginalName(); // 文件原名
- // $ext = $fileCharater->getClientOriginalExtension(); // 扩展名
- // $realPath = $fileCharater->getRealPath(); //临时文件的绝对路径
- // $type = $fileCharater->getClientMimeType(); // image/jpeg
- //
- //// if (strpos($type, 'octet-stream') === false) {
- //// return '非法格式';
- //// }
- //
- // $filePath = 'storage/app/public/uploads/2019-02-18-09-57-23-5c6a8183ebc40.xlsx';
- //// $filePath = 'storage/exports/'.iconv('UTF-8', 'GBK', '学生成绩').'.xls';
- //
- // Excel::load($filePath, function($reader) {
- // $data = $reader->get();
- // return $data->toArray();
- //// foreach($qdatas as $qdata => $value){
- ////
- //// }
- //
- // });
- //
- //// // 上传文件
- //// $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . 'xls';
- //// // 使用我们新建的uploads本地存储空间(目录)
- //// //这里的uploads是配置文件的名称
- //// $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
- //// if ($bool) {
- //// return '/storage/uploads/' . $filename;
- //// }
- // }
- // return 'error';
- // }
- }
|