Exam.php 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352
  1. <?php
  2. namespace app\sys\controller;
  3. use app\logics\EmployeeLogic;
  4. use app\logics\OrgLogic;
  5. use app\model\ExamEmpResult;
  6. use app\model\ExamPaper;
  7. use app\model\ExamPaperQuestion;
  8. use PhpOffice\PhpSpreadsheet\IOFactory;
  9. use think\facade\View;
  10. use think\facade\Request;
  11. use app\model\ExamQuestion;
  12. use app\model\Setting;
  13. use app\model\Employee;
  14. use app\model\Org;
  15. use app\event\Msg;
  16. use think\facade\Db;
  17. use OSS\Core\OssException;
  18. use OSS\OssClient;
  19. use think\facade\Log;
  20. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  21. class Exam
  22. {
  23. /**
  24. * 列表
  25. */
  26. public function questionlist()
  27. {
  28. if (!Request::isAjax()) {
  29. $where = [
  30. 'root_id' => request()->employee->root_id,
  31. 'name' => 'questioncate'
  32. ];
  33. $setting = Setting::where($where)->find();
  34. $list = !empty($setting->content) ? json_decode($setting->content, true) : [];
  35. View::assign('cate', $list);
  36. return View::fetch();
  37. }
  38. $param = Request::only(['page', 'limit', 'cate', 'keyword','type'=>'']);
  39. $root_id = request()->employee->root_id;
  40. $where[] = ['state', '=', 1];
  41. $where[] = ['root_id', '=', $root_id];
  42. if (!empty($param['keyword'])) {
  43. $where[] = ['ask', 'like', '%' . $param['keyword'] . '%'];
  44. }
  45. if (!empty($param['cate'])) {
  46. $where[] = ['cate', '=', $param['cate']];
  47. }
  48. if (!empty($param['type'])) {
  49. $where[] = ['type', '=', $param['type']];
  50. }
  51. $list = ExamQuestion::where($where)->page($param['page'], $param['limit'])->order('addtime desc')->select();
  52. foreach ($list as $k => $v) {
  53. }
  54. $count = ExamQuestion::where($where)->count();
  55. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  56. }
  57. /*
  58. * 分类管理
  59. */
  60. function catelist()
  61. {
  62. return View::fetch();
  63. }
  64. function catealllist()
  65. {
  66. $page = input('page', 1);
  67. $limit = input('limit', 10);
  68. $where = [
  69. 'root_id' => request()->employee->root_id,
  70. 'name' => 'questioncate'
  71. ];
  72. $setting = Setting::where($where)->find();
  73. $for = ($setting) ? json_decode($setting->content) : [];
  74. $list = [];
  75. //试题
  76. $questionCount = ExamQuestion::where(['root_id' => request()->employee->root_id, 'state' => 1])->group('cate')->column('count(id) as count', 'cate');
  77. $questionMaxTime = ExamQuestion::where(['root_id' => request()->employee->root_id, 'state' => 1])->group('cate')->column('max(addtime)', 'cate');
  78. foreach ($for as $k => $v) {
  79. $cate = [
  80. 'name' => $v,
  81. 'value' => $k,
  82. 'questionCount' => isset($questionCount[$v]) ? $questionCount[$v] : 0,
  83. 'addtimeMax' => isset($questionMaxTime[$v]) ? $questionMaxTime[$v] : '',
  84. 'show' => $v == '其它' ? 1 : 0
  85. ];
  86. $list[] = $cate;
  87. }
  88. $count = count($list);
  89. $list = array_slice($list, $limit * ($page - 1), $limit);
  90. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  91. }
  92. /*
  93. * 分类删除
  94. */
  95. public function catedel()
  96. {
  97. $name = input('name');
  98. $where = ['root_id' => request()->employee->root_id, 'name' => 'questioncate'];
  99. $setting = Setting::where($where)->find();
  100. $content = json_decode($setting->content, true);
  101. $key = array_search($name, $content);
  102. unset($content[$key]);
  103. $content[] = '其它';
  104. $content = array_unique($content);
  105. Setting::where($where)->update(['content' => json_encode($content)]);
  106. ExamQuestion::where(['root_id' => request()->employee->root_id, 'cate' => $name])->update(['cate' => '其它']);
  107. return json(['code' => 0, 'msg' => '删除成功']);
  108. }
  109. function cateedit()
  110. {
  111. $name = input('name', '');
  112. $yname = input('yname', '');
  113. if ($name == $yname) return json(['code' => 1, 'msg' => '请修改分类名称']);
  114. $w[] = ['root_id', '=', request()->employee->root_id];
  115. $w[] = ['name', '=', 'questioncate'];
  116. $setting = Setting::where($w)->find();
  117. if ($setting) {
  118. $for = ($setting) ? json_decode($setting->content, true) : [];
  119. if (in_array($name, $for)) return json(['code' => 1, 'msg' => '分类已存在']);
  120. $key = array_search($yname, $for);
  121. $for[$key] = $name;
  122. // if ($key!==false) unset($for[$key]);
  123. // array_unshift($for,$name);
  124. Setting::where($w)->update(['content' => json_encode($for)]);
  125. $cw[] = ['cate', '=', $yname];
  126. $cw[] = ['root_id', '=', request()->employee->root_id];
  127. ExamQuestion::where($cw)->update(['cate' => $name]);
  128. } else {
  129. $save['root_id'] = request()->employee->root_id;
  130. $save['name'] = 'questioncate';
  131. $save['content'] = json_encode([$name]);
  132. Setting::insert($save);
  133. }
  134. return json(['code' => 0, 'data' => '', 'msg' => '编辑成功']);
  135. }
  136. function cateadd()
  137. {
  138. $name = input('name', '');
  139. $w[] = ['root_id', '=', request()->employee->root_id];
  140. $w[] = ['name', '=', 'questioncate'];
  141. $setting = Setting::where($w)->findOrEmpty();
  142. if (!$setting->isEmpty()) {
  143. $for = ($setting->content) ? json_decode($setting->content, true) : [];
  144. if (in_array($name, $for)) return json(['code' => 1, 'msg' => '分类已存在']);
  145. array_unshift($for, $name);
  146. Setting::where($w)->update(['content' => json_encode($for)]);
  147. } else {
  148. $save['root_id'] = request()->employee->root_id;
  149. $save['name'] = 'questioncate';
  150. $save['content'] = json_encode([$name]);
  151. Setting::insert($save);
  152. }
  153. return json(['code' => 0, 'data' => '', 'msg' => '编辑成功']);
  154. }
  155. /**
  156. * 试题列表
  157. */
  158. public function questionadd()
  159. {
  160. $where = [
  161. 'root_id' => request()->employee->root_id,
  162. 'name' => 'questioncate'
  163. ];
  164. $setting = Setting::where($where)->find();
  165. $list = !empty($setting->content) ? json_decode($setting->content, true) : [];
  166. View::assign('cate', $list);
  167. return View::fetch();
  168. }
  169. /**
  170. * 添加试题
  171. */
  172. public function questionsave()
  173. {
  174. $root_id = request()->employee->root_id;
  175. // $param = Request::param();
  176. $type = input('type');
  177. $save['ask'] = $ask = input('ask');
  178. $ask = str_replace('&nbsp;', '', $ask);
  179. if (trim($ask) == '') {
  180. return json(['code' => 1, 'msg' => '题干内容不能为空']);
  181. }
  182. $imgs = input('imgs/a');
  183. $save['ask'] = str_replace('"', '\\"', $save['ask']);
  184. $save['type'] = $type;
  185. $save['cate'] = input('cate');
  186. $save['imgs'] = empty($imgs) ? '' : implode(',', $imgs);
  187. if ($type == '单选') {
  188. $radio = input('checkbox0', '');
  189. if (!$radio) return json(['code' => 1, 'msg' => '请选择正确答案']);
  190. $save['type'] = '单选';
  191. $save['answer'] = $radio;
  192. $answer = input('answer0/a');
  193. $option = input('option0/a');
  194. $op = [];
  195. foreach ($option as $k => $v) {
  196. $o = [];
  197. if (isset($answer[$k]) && $answer[$k]) {
  198. $o['title'] = $v;
  199. $o['content'] = $answer[$k];
  200. $o['ansower'] = $radio == $v ? 1 : 0;
  201. }
  202. $op[] = $o;
  203. }
  204. $save['content'] = json_encode($op);
  205. } elseif ($type == '多选') { //选择
  206. $checkbox = input('checkbox1/a');
  207. // var_dump($checkbox);die;
  208. if (!$checkbox) return json(['code' => 1, 'msg' => '请勾选答案']);
  209. $answer = input('answer1/a');
  210. $save['type'] = '多选';
  211. $option = input('option1/a');
  212. $op = [];
  213. foreach ($option as $k => $v) {
  214. $o = [];
  215. if (isset($answer[$k]) && $answer[$k]) {
  216. $o['title'] = $v;
  217. $o['content'] = $answer[$k];
  218. $o['ansower'] = isset($checkbox[$k]) ? 1 : 0;
  219. }
  220. $op[] = $o;
  221. }
  222. $save['content'] = json_encode($op);
  223. $save['answer'] = implode(',', $checkbox);
  224. } elseif ($type == '判断') {
  225. $save['content'] = $save['ask'];
  226. $save['answer'] = input('answer2') == 1 ? '正确' : '错误';
  227. } elseif ($type == '简答') {
  228. // if (!input('content3', '')) return json(['code' => 1, 'msg' => '请输入参考答案']);
  229. if (!input('answer3', '')) return json(['code' => 1, 'msg' => '请输入答案关键词']);
  230. $save['content'] = input('content3', '');
  231. $save['answer'] = input('answer3');
  232. }
  233. $save['root_id'] = request()->employee->root_id;
  234. $quesObj = new ExamQuestion();
  235. if ($quesObj->save($save)) {
  236. return json(['code' => 0, 'msg' => '添加成功']);
  237. } else {
  238. return json(['code' => 1, 'msg' => '添加失败']);
  239. }
  240. }
  241. /**
  242. * 试题编辑
  243. */
  244. public function questionedit()
  245. {
  246. $root_id = request()->employee->root_id;
  247. if (!Request::isAjax()) {
  248. $where[] = ['root_id', '=', $root_id];
  249. $where[] = ['name', '=', 'questioncate'];
  250. $questioncateobj = Setting::where($where)->find();
  251. $cate = ($questioncateobj && $questioncateobj->content) ? json_decode($questioncateobj->content, true) : [];
  252. View::assign('cate', $cate);
  253. $questionid = Request::param('qid');
  254. $question = ExamQuestion::find($questionid);
  255. // var_dump($question);die;
  256. if ($question['type'] == "单选") {
  257. $question['type'] = 0;
  258. $question['content'] = json_decode($question['content'], true);
  259. $question['answer'] = [$question['answer']];
  260. } elseif ($question['type'] == '多选') {
  261. $question['type'] = 1;
  262. $question['content'] = json_decode($question['content'], true);
  263. $question['answer'] = explode(',', $question['answer']);
  264. } elseif ($question['type'] == '判断') {
  265. $question['type'] = 2;
  266. $question['answer'] = $question['answer'] == '正确' ? 1 : 2;
  267. } elseif ($question['type'] == '简答') {
  268. $question['type'] = 3;
  269. }
  270. $question['vo_imgs'] = array_filter(explode(',', $question->getData('imgs')));
  271. View::assign('data', $question);
  272. $url = config('app.ali_oss_bindurl');
  273. View::assign('url', 'https://' . $url . '/');
  274. return View::fetch();
  275. }
  276. $param = Request::param();
  277. $info = ExamQuestion::find($param['qid'])->toArray();
  278. $save['ask'] = $param['ask'];
  279. $save['ask'] = str_replace('"', '\\"', $save['ask']);
  280. $save['cate'] = $param['cate'];
  281. $data = Request::only(['imgs', 'article_image_exist']);
  282. $data['imgs'] = isset($data['imgs']) ? $data['imgs'] : [];
  283. $data['article_image_exist'] = isset($data['article_image_exist']) ? $data['article_image_exist'] : [];
  284. $data['imgs'] = array_filter(array_merge($data['article_image_exist'], $data['imgs']));
  285. $save['imgs'] = empty($data['imgs']) ? '' : implode(',', $data['imgs']);
  286. //选择
  287. if ($info['type'] == '单选') {
  288. $checkbox = input('checkbox0', '');
  289. if (!$checkbox) return json(['code' => 1, 'msg' => '请选择正确答案']);
  290. $save['answer'] = $checkbox;
  291. $answer = input('answer0/a');
  292. $option = input('option0/a');
  293. $op = [];
  294. foreach ($option as $k => $v) {
  295. $o = [];
  296. if (isset($answer[$k]) && $answer[$k]) {
  297. $o['title'] = $v;
  298. $o['content'] = $answer[$k];
  299. $o['ansower'] = $v == $checkbox ? 1 : 0;
  300. }
  301. $op[] = $o;
  302. }
  303. $save['content'] = json_encode($op);
  304. $save['type'] = '单选';
  305. } elseif ($info['type'] == 1 || $info['type'] == '多选') {
  306. $checkbox = input('checkbox1/a');
  307. // var_dump($checkbox);die;
  308. if ($checkbox == null) return json(['code' => 1, 'msg' => '请勾选答案']);
  309. $answer = input('answer1/a');
  310. $option = input('option1/a');
  311. $op = [];
  312. foreach ($option as $k => $v) {
  313. $o = [];
  314. if (isset($answer[$k]) && $answer[$k]) {
  315. $o['title'] = $v;
  316. $o['content'] = $answer[$k];
  317. $o['ansower'] = isset($checkbox[$k]) ? 1 : 0;
  318. }
  319. $op[] = $o;
  320. }
  321. $save['content'] = json_encode($op);
  322. $save['answer'] = implode(',', $checkbox);
  323. $save['type'] = '多选';
  324. } elseif ($info['type'] == 2 || $info['type'] == '判断') { //判断
  325. $save['content'] = $save['ask'];
  326. $save['answer'] = input('answer2') == 1 ? '正确' : '错误';
  327. } elseif ($info['type'] == '简答' || $info['type'] == 3) { //简单
  328. // if (!input('content3', '')) return json(['code' => 1, 'msg' => '请输入参考答案']);
  329. if (!input('answer3', '')) return json(['code' => 1, 'msg' => '请输入答案关键词']);
  330. $save['content'] = input('content3', '');
  331. $save['answer'] = input('answer3');
  332. }
  333. $w[] = ['root_id', '=', $root_id];
  334. $w[] = ['id', '=', $param['qid']];
  335. ExamQuestion::where($w)->update($save);
  336. return json(['code' => 0, 'msg' => '编辑成功']);
  337. // if (ExamQuestion::where($w)->update($save)) {
  338. // return json(['code' => 0, 'msg' => '编辑成功']);
  339. // } else {
  340. // return json(['code' => 1, 'msg' => '编辑失败']);
  341. // }
  342. }
  343. /**
  344. * 试题删除
  345. */
  346. public function questionpredel()
  347. {
  348. $qid = Request::param('qid');
  349. $root_id = request()->employee->root_id;
  350. $w[] = ['root_id', '=', $root_id];
  351. $w[] = ['question_id', '=', $qid];
  352. $records_num = ExamPaperQuestion::where($w)->count();
  353. if ($records_num > 0) {
  354. return json(['code' => 0, 'msg' => '此试题已被试卷试用,确定要删除么?']);
  355. } else {
  356. return json(['code' => 0, 'msg' => '确定要删除试题么?']);
  357. }
  358. }
  359. public function questiondel()
  360. {
  361. $qid = Request::param('qid');
  362. $root_id = request()->employee->root_id;
  363. $w[] = ['root_id', '=', $root_id];
  364. $w[] = ['question_id', '=', $qid];
  365. $records_num = ExamPaperQuestion::where($w)->count();
  366. // if($records_num > 0){
  367. // ExamPaperQuestion::where($w)->delete();
  368. // }
  369. $w1[] = ['root_id', '=', $root_id];
  370. $w1[] = ['id', '=', $qid];
  371. if (ExamQuestion::where($w1)->update(['state' => 0])) {
  372. return json(['code' => 0, 'msg' => '删除成功']);
  373. } else {
  374. return json(['code' => 1, 'msg' => '删除失败']);
  375. }
  376. }
  377. public function addquestioncate()
  378. {
  379. $param = Request::only(['cate']);
  380. $root_id = request()->employee->root_id;
  381. $w[] = ['root_id', '=', $root_id];
  382. $w[] = ['name', '=', 'questioncate'];
  383. $cate = Setting::where($w)->find();
  384. if (!$cate) {
  385. $save['root_id'] = $root_id;
  386. $save['content'] = json_encode([$param['cate']]);
  387. $save['name'] = 'questioncate';
  388. Setting::insert($save);
  389. // Setting::where()->save(['content' => json_encode([$param['cate']])]);
  390. } else {
  391. $catearr = json_decode($cate['content'], true);
  392. array_push($catearr, $param['cate']);
  393. $catearr = array_unique($catearr);
  394. Setting::where('id', $cate['id'])->save(['content' => json_encode($catearr)]);
  395. }
  396. return json(['code' => 0, 'msg' => '添加成功']);
  397. }
  398. public function check_excel($title, $type)
  399. {
  400. $res = true;
  401. foreach ($type as $k => $v) {
  402. if ($v != $title[$k]) {
  403. $res = $v;
  404. break;
  405. }
  406. }
  407. return $res;
  408. }
  409. /**
  410. * 测试图片上传
  411. */
  412. public function extract_img($sheet)
  413. {
  414. // $filename = $_FILES['file']['tmp_name'];
  415. // $objReader = IOFactory::createReader('Xlsx');
  416. // $objPHPExcel = $objReader->load($filename);
  417. // $sheet = $objPHPExcel->getSheet(0); //excel中的第一张sheet
  418. $data = $sheet->toArray();
  419. $imageFilePath='./images/'.date('Y-m-d').'/';//图片在本地存储的路径
  420. if (!file_exists( $imageFilePath )) {
  421. mkdir("$imageFilePath", 0777, true);
  422. }
  423. $text = [];
  424. //处理图片
  425. try {
  426. foreach ($sheet->getDrawingCollection() as $drawing) {
  427. list($startColumn, $startRow) = Coordinate::coordinateFromString($drawing->getCoordinates());
  428. //$imageFileName = $drawing->getCoordinates() . mt_rand(1000, 9999);
  429. $imageFileName = $drawing->getCoordinates() . uniqid();
  430. switch ($drawing->getExtension()){
  431. case 'jpg':
  432. case 'jpeg':
  433. $imageFileName .= '.jpg';
  434. $source = imagecreatefromjpeg($drawing->getPath());
  435. imagejpeg($source, $imageFilePath . $imageFileName);
  436. break;
  437. case 'gif':
  438. $imageFileName .= '.gif';
  439. $source = imagecreatefromgif($drawing->getPath());
  440. imagegif($source, $imageFilePath . $imageFileName);
  441. break;
  442. case 'png':
  443. $imageFileName .= '.png';
  444. $source = imagecreatefrompng($drawing->getPath());
  445. imagepng($source, $imageFilePath . $imageFileName);
  446. break;
  447. }
  448. $startColumn = $this->ABC2decimal($startColumn);
  449. $url = 'exam/'. $imageFileName;
  450. $imgUrlOss[] = ['tmp_name' => $imageFilePath . $imageFileName, 'name' => $url];
  451. if($data[$startRow-1][$startColumn] && !isset($text[$startRow-1][$startColumn])) $text[$startRow-1][$startColumn] = $data[$startRow-1][$startColumn];
  452. $data[$startRow-1][$startColumn] .= '<p><img src="https://' .config('app.ali_oss_bindurl').'/'.$url.'"/></p>';
  453. }
  454. if(!empty($text)){
  455. foreach($text as $key=>$val){
  456. foreach($val as $k=>$v){
  457. $data[$key][$k] = '<p>'.$v.'</p>'.str_replace($v,'',$data[$key][$k]);
  458. }
  459. }
  460. }
  461. if(!empty($imgUrlOss)) $this->upossimg($imgUrlOss);
  462. }catch (\Exception $e) {
  463. throw $e;
  464. var_dump($e->getMessage());
  465. }
  466. return $data;
  467. }
  468. public function ABC2decimal($abc){
  469. $ten = 0;
  470. $len = strlen($abc);
  471. for($i=1;$i<=$len;$i++){
  472. $char = substr($abc,0-$i,1);//反向获取单个字符
  473. $int = ord($char);
  474. $ten += ($int-65)*pow(26,$i-1);
  475. }
  476. return $ten;
  477. }
  478. /**
  479. * 将图片上传oss保存
  480. */
  481. public function upossimg($imgUrlOss)
  482. {
  483. // 将上传到图片保存
  484. if (!empty($imgUrlOss)) {
  485. $accessKeyId = config('app.ali_oss_access_key_id');
  486. $accessKeySecret = config('app.ali_oss_access_key_secret');
  487. $endpoint = config('app.ali_oss_end_point');
  488. $bucket = config('app.ali_oss_bucket');
  489. $oss = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  490. try {
  491. foreach ($imgUrlOss as $key => $v) {
  492. $oss->uploadFile($bucket, $v['name'], $v['tmp_name']);
  493. unlink($v['tmp_name']);
  494. }
  495. } catch (OssException $e) {
  496. Log::record('文件上传失败,error:' . $e->getErrorCode(), 'error');
  497. //return json(['code' => 1, 'msg' => $e->getErrorMessage()]);
  498. }
  499. }
  500. }
  501. ///
  502. // 试题导入importing questions
  503. ///
  504. public function importing()
  505. {
  506. $root_id = request()->employee->root_id;
  507. $request = request();
  508. if (!$request->isAjax()) {
  509. return View::fetch();
  510. }
  511. $title0 = ['分类', '题干(必填)', '选项 A', '选项 B', '选项 C', '选项 D', '选项E', '选项F', '选项G', '选项H', '正确答案(必填)'];
  512. $title1 = ['分类', '题干(必填)', '选项 A', '选项 B', '选项 C', '选项 D', '选项E', '选项F', '选项G', '选项H', '正确答案(必填)'];
  513. $title2 = ['分类', '题干(必填)', '正确答案(必填)'];
  514. $title3 = ['分类', '题干(必填)', '参考答案(必填)', '答案关键字(必填)'];
  515. $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
  516. $filename = $_FILES['file']['tmp_name'];
  517. if ($fileExtendName == 'xlsx') {
  518. $objReader = IOFactory::createReader('Xlsx');
  519. } else {
  520. $objReader = IOFactory::createReader('Xls');
  521. }
  522. $objPHPExcel = $objReader->load($filename); //$filename可以是上传的表格,或者是指定的表格
  523. $cates = $saves = [];
  524. $error = 0;
  525. $remark = [];
  526. //单选题
  527. $sheet = $objPHPExcel->getSheet(0); //excel中的第一张sheet
  528. $data = $this->extract_img($sheet);
  529. //var_dump($data);exit;
  530. //$data = $sheet->toArray();
  531. if (count($data) > 1) {
  532. $check = $this->check_excel($data[0], $title0);
  533. if ($check !== true) return json(['code' => 1, 'data' => $check, 'msg' => '模板标题异常,请使用官方提供的模板']);
  534. unset($data[0]);
  535. $arr = [2 => 'A', 3 => 'B', 4 => 'C', 5 => 'D', 6 => 'E', 7 => 'F', 8 => 'G', 9 => 'H'];
  536. foreach ($data as $k => $v) {
  537. if(empty(array_filter(array_slice($v,0,11)))) continue;
  538. //题目,答案必传才能保存
  539. if ($v[1] && $v[10] && $v[0]) {
  540. //如果存在选项才能保存
  541. $options = [];
  542. $answer = explode(',', trim($v[10]));
  543. foreach ($arr as $k2 => $v2) {
  544. $option = [];
  545. if (!empty($v[$k2])) {
  546. $option['title'] = $v2;
  547. $option['content'] = $v[$k2];
  548. $option['ansower'] = in_array($v2, $answer) ? 1 : 0;
  549. // $option[$v2] = $v[$k2];
  550. $options[] = $option;
  551. } else {
  552. break; //选项不能中断
  553. }
  554. }
  555. if (count($options) > 0) {
  556. $cates[] = trim($v[0]); //分类
  557. $save = [];
  558. $save['cate'] = $v[0];
  559. $save['ask'] = $v[1];
  560. $save['content'] = json_encode($options);
  561. $save['answer'] = trim($v[10]);
  562. $save['type'] = '单选';
  563. $save['root_id'] = $root_id;
  564. $saves[] = $save;
  565. }
  566. } else {
  567. $error += 1;
  568. }
  569. }
  570. if($error) $remark[] = '单选题有'.$error.'道试题上传格式错误<br/>';
  571. }
  572. //多选题
  573. $sheet = $objPHPExcel->getSheet(1); //excel中的第二张sheet
  574. $data = $this->extract_img($sheet);
  575. //$data = $sheet->toArray();
  576. $error = 0;
  577. if (count($data) > 1) {
  578. $check = $this->check_excel($data[0], $title1);
  579. if ($check !== true) return json(['code' => 1, 'data' => $check, 'msg' => '模板标题异常,请使用官方提供的模板']);
  580. unset($data[0]);
  581. $arr = [2 => 'A', 3 => 'B', 4 => 'C', 5 => 'D', 6 => 'E', 7 => 'F', 8 => 'G', 9 => 'H'];
  582. foreach ($data as $k => $v) {
  583. if(empty(array_filter(array_slice($v,0,11)))) continue;
  584. //题目,答案必传才能保存
  585. if ($v[1] && $v[10] && $v[0]) {
  586. //如果存在选项才能保存
  587. $options = [];
  588. $answer = explode(',', trim($v[10]));
  589. foreach ($arr as $k2 => $v2) {
  590. $option = [];
  591. if (!empty($v[$k2])) {
  592. $option['title'] = $v2;
  593. $option['content'] = $v[$k2];
  594. $option['ansower'] = in_array($v2, $answer) ? 1 : 0;
  595. // $option[$v2] = $v[$k2];
  596. $options[] = $option;
  597. } else {
  598. break; //选项不能中断
  599. }
  600. }
  601. if (count($options) > 0) {
  602. $cates[] = trim($v[0]); //分类
  603. $save = [];
  604. $save['cate'] = $v[0];
  605. $save['ask'] = $v[1];
  606. $save['content'] = json_encode($options);
  607. $save['answer'] = trim($v[10]);
  608. $save['type'] = '多选';
  609. $save['root_id'] = $root_id;
  610. $saves[] = $save;
  611. }
  612. } else {
  613. $error += 1;
  614. }
  615. }
  616. if($error) $remark[] = '多选题有'.$error.'道试题上传格式错误<br/>';
  617. }
  618. // var_dump($saves);die;
  619. //判断题
  620. $sheet = $objPHPExcel->getSheet(2); //第二章sheet
  621. $data = $sheet->toArray();
  622. $error = 0;
  623. if (count($data) > 1) {
  624. $check = $this->check_excel($data[0], $title2);
  625. if ($check !== true) return json(['code' => 1, 'data' => $check, 'msg' => '模板标题异常,请使用官方提供的模板']);
  626. unset($data[0]);
  627. foreach ($data as $k => $v) {
  628. if(empty(array_filter(array_slice($v,0,3)))) continue;
  629. $save = [];
  630. if ($v[1] && $v[2] && $v[0]) {
  631. $cates[] = trim($v[0]); //分类
  632. $save = [];
  633. $save['cate'] = $v[0];
  634. $save['ask'] = $save['content'] = $v[1];
  635. $save['answer'] = trim($v[2]);
  636. $save['type'] = '判断';
  637. $save['root_id'] = $root_id;
  638. $saves[] = $save;
  639. } else {
  640. $error += 1;
  641. }
  642. }
  643. if($error) $remark[] = '判断题有'.$error.'道试题上传格式错误<br/>';
  644. }
  645. //问答题
  646. $sheet = $objPHPExcel->getSheet(3); //第三章sheet
  647. $data = $sheet->toArray();
  648. $error = 0;
  649. if (count($data) > 1) {
  650. // var_dump($data[0]);die;
  651. $check = $this->check_excel($data[0], $title3);
  652. if ($check !== true) return json(['code' => 1, 'data' => $check, 'msg' => '模板标题异常,请使用官方提供的模板']);
  653. unset($data[0]);
  654. foreach ($data as $k => $v) {
  655. if(empty(array_filter(array_slice($v,0,4)))) continue;
  656. $save = [];
  657. if ($v[1] && $v[2] && $v[0]) {
  658. $cates[] = trim($v[0]); //分类
  659. $save = [];
  660. $save['cate'] = $v[0];
  661. $save['ask'] = $v[1];
  662. $save['answer'] = trim($v[3]);
  663. $save['content'] = trim($v[2]);
  664. $save['type'] = '简答';
  665. $save['root_id'] = $root_id;
  666. $saves[] = $save;
  667. } else {
  668. $error += 1;
  669. }
  670. }
  671. if($error) $remark[] = '问答题有'.$error.'道试题上传格式错误<br/>';
  672. }
  673. $cates = array_unique(array_filter($cates));
  674. //保存分类
  675. $cw[] = ['root_id', '=', request()->employee->root_id];
  676. $cw[] = ['name', '=', 'questioncate'];
  677. $ques_cate = Setting::where($cw)->find();
  678. if (!$ques_cate) {
  679. Setting::insert(['content' => json_encode($cates), 'name' => 'questioncate', 'root_id' => request()->employee->root_id]);
  680. } else {
  681. $catearr = json_decode($ques_cate['content'], true);
  682. $cates = array_unique(array_merge($catearr, $cates));
  683. Setting::where($cw)->save(['content' => json_encode($cates)]);
  684. }
  685. //保存问题
  686. (new ExamQuestion())->saveAll($saves);
  687. if ($remark) {
  688. return json(["code" => 1, 'msg' => implode('',$remark)]);
  689. }else{
  690. return json(["code" => 0, 'msg' => '导入成功']);
  691. }
  692. }
  693. /**
  694. * paper列表
  695. */
  696. public function paperlist()
  697. {
  698. if (!Request::isAjax()) {
  699. return View::fetch();
  700. }
  701. $root_id = request()->employee->root_id;
  702. $param = Request::only(['page', 'limit', 'keyword']);
  703. // $where[] = ['state','=', 1];
  704. $where[] = ['root_id', '=', $root_id];
  705. if (!empty($param['keyword'])) {
  706. $where[] = ['name', 'like', '%' . $param['keyword'] . '%'];
  707. }
  708. $list = ExamPaper::with('result')->where($where)->page($param['page'], $param['limit'])->order('addtime desc')->select()->toArray();
  709. foreach ($list as &$v) {
  710. $time = date('Y-m-d H:i:s');
  711. if ($time >= $v['starttime'] && $time <= $v['endtime'] && $v['result']) {
  712. $v['done'] = 1;
  713. } else {
  714. $v['done'] = 0;
  715. }
  716. $v['attendee_num'] = $v['assessment'] ? count(explode(',', $v['assessment'])) : 0;
  717. }
  718. $count = ExamPaper::where($where)->count();
  719. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  720. }
  721. /**
  722. * 阅卷批改,试卷列表
  723. */
  724. public function checkpaperlist()
  725. {
  726. if (!Request::isAjax()) {
  727. return View::fetch();
  728. }
  729. $root_id = request()->employee->root_id;
  730. $param = Request::only(['page', 'limit', 'keyword']);
  731. // $where[] = ['state','=', 1];
  732. $where[] = ['root_id', '=', $root_id];
  733. if (!empty($param['keyword'])) {
  734. $where[] = ['name', 'like', '%' . $param['keyword'] . '%'];
  735. }
  736. //只查询人工审核的试卷列表
  737. // $where[] = ['checkway','=','employee'];
  738. $list = ExamPaper::withCount(['result'=>function($query){
  739. $query->where([['approve_status', '>=', 0]]); // 考试中的不在统计范围内
  740. }])->where($where)->page($param['page'], $param['limit'])->order('addtime desc')->select()->toArray();
  741. foreach ($list as &$v) {
  742. $time = date('Y-m-d H:i:s');
  743. if ($time >= $v['starttime'] && $time <= $v['endtime'] && $v['result_count']) {
  744. $v['done'] = 1;
  745. } else {
  746. $v['done'] = 0;
  747. }
  748. }
  749. $count = ExamPaper::where($where)->count();
  750. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  751. }
  752. // (更新操作逻辑,此接口不在使用)
  753. // public function paperadd()
  754. // {
  755. // $root_id = request()->employee->root_id;
  756. // //退回上一步处理
  757. // $paperid = input('paperid', 0);
  758. // $data['paperid'] = $paperid ?: 0;
  759. // if ($paperid) {
  760. // $info = ExamPaper::where('id', $paperid)->find()->toArray();
  761. // $data['info'] = $info;
  762. // view::assign('data', $data);
  763. // return View::fetch();
  764. // } else {
  765. // $data['info'] = [];
  766. // }
  767. // view::assign('data', $data);
  768. // if (!Request::isAjax()) {
  769. // return View::fetch();
  770. // }
  771. // if (!input('name', '')) return json(['code' => 1, 'msg' => '请填写考卷名称']);
  772. // $quesObj = new ExamPaper;
  773. // $quesObj->name = input('name');
  774. // $quesObj->for_newbie = input('for_newbie', 0);
  775. // $quesObj->desc = input('desc');
  776. // $quesObj->root_id = $root_id;
  777. // $quesObj->state = 0;
  778. // $quesObj->approve_employee_ids = '';
  779. // $quesObj->assessment = '';
  780. // $quesObj->save();
  781. // $id = $quesObj->id;
  782. // if ($id) {
  783. // return json(['code' => 0, 'data' => $id, 'msg' => '添加成功']);
  784. // } else {
  785. // return json(['code' => 1, 'msg' => '添加失败']);
  786. // }
  787. // }
  788. //退回后再次点击下一步
  789. public function paperaddt()
  790. {
  791. $root_id = request()->employee->root_id;
  792. $paperid = input('paperid', 0);
  793. $name = input('name', '');
  794. if ($paperid && $name) {
  795. $u['desc'] = input('desc', '');
  796. $u['name'] = $name;
  797. ExamPaper::where('id', $paperid)->update($u);
  798. return json(['code' => 0, 'data' => $paperid, 'msg' => '添加成功']);
  799. } else {
  800. return json(['code' => 1, 'msg' => '参数错误']);
  801. }
  802. }
  803. public function paperaddquestion()
  804. {
  805. $root_id = request()->employee->root_id;
  806. if (!Request::isAjax()) {
  807. View::assign('paperid', Request::param('paperid'));
  808. //查询已经保存的题目
  809. $cw[] = ['root_id', '=', $root_id];
  810. $cw[] = ['paper_id', '=', Request::param('paperid')];
  811. $ids = ExamPaperQuestion::where($cw)->column('question_id');
  812. $wherec[] = ['id', 'in', $ids];
  813. $wherec[] = ['root_id', '=', $root_id];
  814. $wherec[] = ['state', '=', 1];
  815. $data['data2'] = ExamQuestion::where($wherec)->field('id,type,ask,cate')->order('type', 'desc')->select()->toArray();
  816. //查询剩余的题目
  817. $where[] = ['root_id', '=', $root_id];
  818. $where[] = ['id', 'not in', $ids];
  819. $where[] = ['state', '=', 1];
  820. $questList = ExamQuestion::where($where)->field('id,type,ask,cate')->order('type', 'desc')->select()->toArray();
  821. $data['count'] = count($data['data2']); //选中题目的数量
  822. $data['data2'] = json_encode($data['data2']);
  823. //分类
  824. $cate_w[] = ['root_id', '=', $root_id];
  825. $cate_w[] = ['name', '=', 'questioncate'];
  826. $cate = Setting::where($cate_w)->value('content');
  827. $data['cate'] = $cate ? json_decode($cate, true) : [];
  828. View::assign('data', $data);
  829. // var_dump(json_encode($questList));die;
  830. View::assign('questListstr', strip_tags(json_encode($questList)));
  831. return View::fetch();
  832. }
  833. $param = Request::param();
  834. //var_dump($param);
  835. $idarr = array_unique(explode(',', $param['idstr']));
  836. $old_where[] = ['root_id', '=', $root_id];
  837. $old_where[] = ['paper_id', '=', Request::param('paperid')];
  838. $old_questions = ExamPaperQuestion::with('questions')->where($old_where)->field('question_id,score,id')->select()->toArray();
  839. $types = ['type0' => '单选', 'type1' => '多选', 'type2' => '判断', 'type3' => '简答'];
  840. $score = [];
  841. foreach ($old_questions as $k => $v) {
  842. foreach ($types as $kk => $vv) {
  843. if ($v['questions']['type'] == $vv) {
  844. $score[$kk] = $v['score'];
  845. }
  846. }
  847. }
  848. $dw[] = ['root_id', '=', $root_id];
  849. $dw[] = ['paper_id', '=', Request::param('paperid')];
  850. $del = ExamPaperQuestion::where($dw)->delete();
  851. $new_questions = ExamQuestion::where([['id', 'in', $idarr]])->select();
  852. $save_data = [];
  853. foreach ($new_questions as $k => $v) {
  854. $once_data['paper_id'] = $param['paperid'];
  855. $once_data['question_id'] = $v['id'];
  856. $once_data['root_id'] = $root_id;
  857. foreach ($types as $kk => $vv) {
  858. if ($v['type'] == $vv) {
  859. $once_data['score'] = $score[$kk] ?? '';
  860. }
  861. }
  862. $save_data[] = $once_data;
  863. }
  864. (new ExamPaperQuestion())->saveAll($save_data);
  865. /*$dw[] = ['root_id','=',$root_id];
  866. $dw[] = ['paper_id','=',Request::param('paperid')];
  867. $del = ExamPaperQuestion::where($dw)->delete();
  868. foreach($idarr as $qid){
  869. $pivot = new ExamPaperQuestion();
  870. $pivot->save(['paper_id'=>$param['paperid'],'question_id' => $qid,'root_id'=>$root_id]);
  871. }*/
  872. return json(['code' => 0, 'paperid' => $param['paperid'], 'msg' => '添加成功']);
  873. }
  874. //搜索题目
  875. public function searchquestion()
  876. {
  877. $root_id = request()->employee->root_id;
  878. $type = input('type', '');
  879. $keyword = input('keyword', '');
  880. $data2 = input('data2', '');
  881. $cate = input('cate', '');
  882. $w[] = ['root_id', '=', $root_id];
  883. if ($type) {
  884. $w[] = ['type', 'in', array_filter(explode(',', $type))];
  885. }
  886. if ($keyword) {
  887. $w[] = ['ask', 'like', '%' . $keyword . '%'];
  888. }
  889. if ($data2) {
  890. $w[] = ['id', 'not in', array_column($data2, 'id')];
  891. }
  892. if ($cate) {
  893. $w[] = ['cate', '=', $cate];
  894. }
  895. $w[] = ['state', '=', 1];
  896. $questList = ExamQuestion::where($w)->field('id,type,ask,cate')->order('type', 'desc')->select()->toArray();
  897. $data['data2'] = $data2 ?: [];
  898. $data['data1'] = $questList;
  899. $data['count'] = ($data2) ? count($data2) : 0;
  900. return json(['code' => 0, 'data' => $data, 'msg' => '添加成功']);
  901. }
  902. // 设置分值
  903. // public function paperscore()
  904. // {
  905. // $root_id = request()->employee->root_id;
  906. // $typeEn = ['单选' => 'single', '多选' => 'multi', '判断' => 'judge', '简答' => 'answer'];
  907. // if (!Request::isAjax()) {
  908. // $paperid = Request::param('paperid');
  909. // View::assign('paperid', $paperid);
  910. // $w[] = ['root_id', '=', $root_id];
  911. // $w[] = ['paper_id', '=', $paperid];
  912. // // $idsarr = ExamPaperQuestion::where($w)->column('question_id');
  913. // // $types = ExamQuestion::where([['id','in',$idsarr]])->column('type');
  914. // $questions = ExamPaperQuestion::with('questions')->where($w)->field('question_id,score,id')->select()->toArray();
  915. // $types = ['type0' => '单选', 'type1' => '多选', 'type2' => '判断', 'type3' => '简答'];
  916. // $sum = array_sum(array_column($questions, 'score'));
  917. // foreach ($types as $k => $v) {
  918. // $data[$k]['count'] = 0; //分类下题目数量
  919. // $data[$k]['score_sum'] = 0; //合计分值
  920. // $data[$k]['grawth'] = 0; //分值占比
  921. // $data[$k]['score'] = ''; //每题分值
  922. // $data[$k]['questions'] = [];
  923. // foreach ($questions as $k2 => $v2) {
  924. // $v2['questions']['score'] = $v2['score'] ? $v2['score'] . '分' : '';
  925. // $v2['questions']['del_id'] = $v2['id'];
  926. // $v2['questions']['ask'] = strip_tags($v2['questions']['ask']);
  927. // if ($v2['questions']['type'] == '简答') {
  928. // $v2['questions']['answer_j'] = $v2['questions']['answer'];
  929. // $v2['questions']['answer'] = $v2['questions']['content'];
  930. // }
  931. // if ($v2['questions']['type'] == $v) {
  932. // $data[$k]['count'] += 1;
  933. // $data[$k]['score_sum'] = $data[$k]['score_sum'] + $v2['score'];
  934. // $data[$k]['score'] = $v2['score'] ?: '';
  935. // if ($k == 'type0' || $k == 'type1') {
  936. // $v2['questions']['content'] = json_decode($v2['questions']['content'], true);
  937. // }
  938. // $data[$k]['questions'][] = $v2['questions'];
  939. // }
  940. // }
  941. // $data[$k]['grawth'] = !$sum ? '0%' : round($data[$k]['score_sum'] / $sum * 100, 2) . '%';
  942. // }
  943. // $w1[] = ['root_id', '=', $root_id];
  944. // $w1[] = ['id', '=', $paperid];
  945. // $paperInfo = ExamPaper::where($w1)->field('target_group,base_score,starttime,endtime,checkway,desc,state,approve_employee_ids,scope_org,duringtime')->find()->toArray();
  946. // $data['base_score'] = $paperInfo['base_score'] ?: 0;
  947. // View::assign('data', $data);
  948. // return View::fetch();
  949. // }
  950. // $param = Request::param();
  951. // $paperid = $param['paperid'];
  952. // $w[] = ['root_id', '=', $root_id];
  953. // $w[] = ['paper_id', '=', $paperid];
  954. // $idsarr = ExamPaperQuestion::where($w)->column('question_id');
  955. // $questions = ExamQuestion::where([['id', 'in', $idsarr]])->select();
  956. // $totalscore = 0;
  957. // foreach ($questions as $item) {
  958. // ExamPaperQuestion::where(['paper_id' => $paperid, 'question_id' => $item->id, 'root_id' => $root_id])->update(['score' => $param[$typeEn[$item->type]]]);
  959. // $totalscore += $param[$typeEn[$item->type]];
  960. // }
  961. // $w1[] = ['root_id', '=', $root_id];
  962. // $w1[] = ['id', '=', $paperid];
  963. // ExamPaper::where($w1)->update(['total_score' => $totalscore, 'base_score' => $param['base_score']]);
  964. // return json(['code' => 0, 'paperid' => $paperid, 'msg' => '设置成功']);
  965. // }
  966. /**
  967. * 设置分值
  968. */
  969. public function paperscore()
  970. {
  971. $root_id = request()->employee->root_id;
  972. $paper_id = request()->param('paper_id',0);
  973. $question_ids = request()->param('question_ids');
  974. $questions = ExamQuestion::where([['root_id', '=', $root_id], ['id', 'in', explode(',',$question_ids)]])->select()->toArray();
  975. $types = ['type0' => '单选', 'type1' => '多选', 'type2' => '判断', 'type3' => '简答'];
  976. $sum = 0;
  977. $data = [];
  978. foreach ($types as $k => $v) {
  979. $data[$k]['count'] = 0; //分类下题目数量
  980. $data[$k]['score_sum'] = 0; //合计分值
  981. $data[$k]['grawth'] = 0; //分值占比
  982. $data[$k]['score'] = ''; //每题分值
  983. $data[$k]['questions'] = [];
  984. foreach ($questions as $k2 => $v2) {
  985. $v2['score'] = '';
  986. $v2['del_id'] = $v2['id'];
  987. $v2['ask'] = strip_tags($v2['ask']);
  988. if ($v2['type'] == '简答') {
  989. $v2['answer_j'] = $v2['answer'];
  990. $v2['answer'] = $v2['content'];
  991. }
  992. if ($v2['type'] == $v) {
  993. $data[$k]['count'] += 1;
  994. $data[$k]['score_sum'] = $data[$k]['score_sum'];
  995. $data[$k]['score'] = $v2['score'] ?: '';
  996. if ($k == 'type0' || $k == 'type1') {
  997. $v2['content'] = json_decode($v2['content'], true);
  998. }
  999. $data[$k]['questions'][] = $v2;
  1000. }
  1001. }
  1002. $data[$k]['grawth'] = !$sum ? '0%' : round($data[$k]['score_sum'] / $sum * 100, 2) . '%';
  1003. }
  1004. //及格分数
  1005. $paper = ExamPaper::where(['root_id' => $root_id, 'id' => $paper_id])->field('total_score,base_score')->find();
  1006. $data['base_score'] = $paper && $paper['base_score'] ? $paper['base_score'] : 0;
  1007. $data['total_score'] = $paper && $paper['total_score'] ? $paper['total_score'] : 0;
  1008. return json(['code' => 0, 'data' => $data, 'msg' => '获取成功']);
  1009. }
  1010. //删除题目
  1011. public function delquestion()
  1012. {
  1013. $id = input('id', 0);
  1014. if ($id) {
  1015. $root_id = request()->employee->root_id;
  1016. $w[] = ['root_id', '=', $root_id];
  1017. $w[] = ['id', '=', $id];
  1018. $info = ExamPaperQuestion::with('questions')->where($w)->find()->toArray();
  1019. $data['type'] = $info['questions']['type'];
  1020. $cw[] = ['exam_paper_question.root_id', '=', $root_id];
  1021. $cw[] = ['exam_paper_question.paper_id', '=', $info['paper_id']];
  1022. $cw[] = ['questions.type', '=', input('type')];
  1023. $cw[] = ['questions.root_id', '=', $root_id];
  1024. $cw[] = ['exam_paper_question.id', '<>', $id];
  1025. $data['count'] = ExamPaperQuestion::withJoin('questions', 'inner')->where($cw)->count();
  1026. $res = ExamPaperQuestion::where($w)->delete();
  1027. if ($res) {
  1028. return json(['code' => 0, 'data' => $data, 'msg' => '删除成功']);
  1029. } else {
  1030. return json(['code' => 1, 'data' => '删除失败', 'msg' => '删除失败']);
  1031. }
  1032. }
  1033. return json(['code' => 1, 'data' => '删除失败', 'msg' => '删除失败']);
  1034. }
  1035. public function paperpublish()
  1036. {
  1037. $root_id = request()->employee->root_id;
  1038. $request = request();
  1039. if (!$request->isAjax()) {
  1040. $org = OrgLogic::struc(request()->employee->root_id);
  1041. View::assign('org', $org);
  1042. View::assign('orgids', json_encode($request->org));
  1043. View::assign('empid', $request->employee->id);
  1044. // View::assign('manager', $request->employee->is_manager);
  1045. $paperid = $request->param('paperid');
  1046. View::assign('paperid', $paperid);
  1047. //及格分数设置
  1048. $w[] = ['root_id', '=', $root_id];
  1049. $w[] = ['paper_id', '=', $paperid];
  1050. $sum = ExamPaperQuestion::where($w)->sum('score');
  1051. $sum = $sum ? $sum * 0.6 : 0;
  1052. $w1[] = ['root_id', '=', $root_id];
  1053. $w1[] = ['id', '=', $paperid];
  1054. $paperInfo = ExamPaper::where($w1)->field('target_group,base_score,starttime,endtime,checkway,desc,state,approve_employee_ids,scope_org,duringtime')->find()->toArray();
  1055. $data['fenshu'] = $sum;
  1056. $data['starttime'] = date('Y-m-d H:i:s');
  1057. $data['endtime'] = date('Y-m-d H:i:s', strtotime('+2 day'));
  1058. $data['base_score'] = $paperInfo['base_score'];
  1059. $data['total_score'] = input('total_score', 0);
  1060. View::assign('data', $data);
  1061. return View::fetch();
  1062. }
  1063. $param = $request->post();
  1064. $paperid = $param['paperid'];
  1065. $w[] = ['root_id', '=', $root_id];
  1066. $w[] = ['paper_id', '=', $paperid];
  1067. $sum = ExamPaperQuestion::where($w)->sum('score');
  1068. if ($param['base_score'] <= 0 || $param['base_score'] > $sum) {
  1069. return json(['code' => 1, 'msg' => '及格分设置错误']);
  1070. }
  1071. unset($param['paperid']);
  1072. //
  1073. $org_id = $param['scope_org'] = 0;
  1074. $select = input('select', '');
  1075. $selects = explode(',', $select);
  1076. // $attendee_num = EmployeeLogic::count($org_id);
  1077. $param['attendee_num'] = count($selects);
  1078. $param['assessment'] = $select; //考试人员
  1079. $seconds = strtotime($param['endtime']) - strtotime($param['starttime']);
  1080. // $param['duringtime'] = ceil($seconds/60);
  1081. $param['duringtime'] = $param['duringtime'];
  1082. unset($param['desc']);
  1083. $a = ExamPaper::update($param, ['id' => $paperid, 'root_id' => $root_id]);
  1084. if (is_object($a)) {
  1085. //消息通知
  1086. $info = ExamPaper::find($paperid);
  1087. if ($info['state'] == 1){
  1088. foreach($selects as $eid) {
  1089. event(new Msg($eid, '有新的试卷指派给您!', 'assignPaper', $paperid));
  1090. }
  1091. }
  1092. return json(['code' => 0, 'paperid' => $paperid, 'msg' => '考卷发布成功']);
  1093. } else {
  1094. return json(['code' => 1, 'msg' => '考卷发布失败']);
  1095. }
  1096. }
  1097. public function paperpreview()
  1098. {
  1099. $root_id = request()->employee->root_id;
  1100. $request = request();
  1101. if (!$request->isAjax()) {
  1102. //$org = OrgLogic::struc(request()->employee->root_id);
  1103. //View::assign('org', $org);
  1104. //View::assign('orgids', json_encode($request->org));
  1105. //View::assign('empid', $request->employee->id);
  1106. //View::assign('manager', $request->employee->is_manager);
  1107. $paperid = $request->param('paperid');
  1108. View::assign('paperid', $paperid);
  1109. $paperInfo = ExamPaper::with('questions')->find($paperid)->toArray();
  1110. // var_dump($paperInfo);die;
  1111. foreach ($paperInfo['questions'] as &$v) {
  1112. if ($v['type'] == '单选' || $v['type'] == '多选') {
  1113. $v['types'] = 1;
  1114. $con = json_decode($v['content'], true);
  1115. $op = [];
  1116. foreach ($con as $v2) {
  1117. $op[$v2['title']] = $v2['content'];
  1118. }
  1119. $v['content'] = $op;
  1120. // $v['answer'] = explode(',',$v['answer']);
  1121. } elseif ($v['type'] == '判断') {
  1122. $v['types'] = 2;
  1123. } elseif ($v['type'] == '简答') {
  1124. $v['types'] = 3;
  1125. //简单题表字段 内容存的是参考答案 答案字段存的是关键词
  1126. $answer = $v['answer'];
  1127. $content = $v['content'];
  1128. $v['answer'] = $v['content'];
  1129. $v['content'] = '';
  1130. }
  1131. }
  1132. View::assign('info', $paperInfo);
  1133. return View::fetch();
  1134. }
  1135. }
  1136. // public function paperedit()
  1137. // {
  1138. // if (!Request::isAjax()) {
  1139. // $paperid = Request::param('paperid');
  1140. // View::assign('paperid', $paperid);
  1141. // $paperfirstinfo = ExamPaper::field('name,for_newbie,desc')->find($paperid);
  1142. // View::assign('datastr', json_encode($paperfirstinfo));
  1143. // return View::fetch();
  1144. // }
  1145. // $root_id = request()->employee->root_id;
  1146. // $param = Request::param();
  1147. // $newdata = [
  1148. // 'name' => $param['name'],
  1149. // 'for_newbie' => (int)$param['for_newbie'],
  1150. // 'desc' => $param['desc']
  1151. // ];
  1152. // $w[] = ['root_id', '=', $root_id];
  1153. // $w[] = ['id', '=', $param['paperid']];
  1154. // ExamPaper::where($w)->update($newdata);
  1155. // return json(['code' => 0, 'id' => $param['paperid'], 'msg' => '基本信息修改成功']);
  1156. // }
  1157. public function papereditquestion()
  1158. {
  1159. $root_id = request()->employee->root_id;
  1160. if (!Request::isAjax()) {
  1161. $paperid = Request::param('paperid');
  1162. View::assign('paperid', $paperid);
  1163. //查询已经保存的题目
  1164. $cw[] = ['root_id', '=', $root_id];
  1165. $cw[] = ['paper_id', '=', Request::param('paperid')];
  1166. $ids = ExamPaperQuestion::where($cw)->column('question_id');
  1167. $wherec[] = ['id', 'in', $ids];
  1168. $wherec[] = ['root_id', '=', $root_id];
  1169. $wherec[] = ['state', '=', 1];
  1170. $data['data2'] = ExamQuestion::where($wherec)->field('id,type,ask,cate')->order('type', 'desc')->select()->toArray();
  1171. //查询剩余的题目
  1172. $where[] = ['root_id', '=', $root_id];
  1173. $where[] = ['id', 'not in', $ids];
  1174. $where[] = ['state', '=', 1];
  1175. $questList = ExamQuestion::where($where)->field('id,type,ask,cate')->order('type', 'desc')->select()->toArray();
  1176. $data['count'] = count($data['data2']); //选中题目的数量
  1177. $data['data2'] = json_encode($data['data2']);
  1178. //分类
  1179. $cate_w[] = ['root_id', '=', $root_id];
  1180. $cate_w[] = ['name', '=', 'questioncate'];
  1181. $cate = Setting::where($cate_w)->value('content');
  1182. $data['cate'] = $cate ? json_decode($cate, true) : [];
  1183. View::assign('data', $data);
  1184. View::assign('questListstr', strip_tags(json_encode($questList)));
  1185. View::assign('choosedquestListstr', strip_tags($data['data2']));
  1186. return View::fetch();
  1187. }
  1188. $param = Request::param();
  1189. $idarr = array_unique(explode(',', $param['idstr']));
  1190. $old_where[] = ['root_id', '=', $root_id];
  1191. $old_where[] = ['paper_id', '=', Request::param('paperid')];
  1192. $old_questions = ExamPaperQuestion::with('questions')->where($old_where)->field('question_id,score,id')->select()->toArray();
  1193. $types = ['type0' => '单选', 'type1' => '多选', 'type2' => '判断', 'type3' => '简答'];
  1194. $score = [];
  1195. foreach ($old_questions as $k => $v) {
  1196. foreach ($types as $kk => $vv) {
  1197. if ($v['questions']['type'] == $vv) {
  1198. $score[$kk] = $v['score'];
  1199. }
  1200. }
  1201. }
  1202. $dw[] = ['root_id', '=', $root_id];
  1203. $dw[] = ['paper_id', '=', Request::param('paperid')];
  1204. $del = ExamPaperQuestion::where($dw)->delete();
  1205. $new_questions = ExamQuestion::where([['id', 'in', $idarr]])->select();
  1206. $save_data = [];
  1207. foreach ($new_questions as $k => $v) {
  1208. $once_data['paper_id'] = $param['paperid'];
  1209. $once_data['question_id'] = $v['id'];
  1210. $once_data['root_id'] = $root_id;
  1211. foreach ($types as $kk => $vv) {
  1212. if ($v['type'] == $vv) {
  1213. $once_data['score'] = $score[$kk] ?? '';
  1214. }
  1215. }
  1216. $save_data[] = $once_data;
  1217. }
  1218. (new ExamPaperQuestion())->saveAll($save_data);
  1219. /*foreach($idarr as $qid){
  1220. $pivot = new ExamPaperQuestion();
  1221. $pivot->save(['paper_id'=>$param['paperid'],'question_id' => $qid,'root_id'=>$root_id]);
  1222. }*/
  1223. // $idarr = explode(',',$param['idstr']);
  1224. // foreach($idarr as $qid){
  1225. // if(!ExamPaperQuestion::where(['paper_id'=>$param['paperid'],'question_id' => $qid])->find()){
  1226. // $pivot = new ExamPaperQuestion();
  1227. // $pivot->save(['paper_id'=>$param['paperid'],'question_id' => $qid,'root_id'=>$root_id]);
  1228. // }
  1229. // }
  1230. // $qidArr = ExamPaperQuestion::where('paper_id',$param['paperid'])->column('question_id');
  1231. // foreach($qidArr as $oldqid){
  1232. // if(!in_array($oldqid, $idarr)){
  1233. // ExamPaperQuestion::where(['paper_id'=>$param['paperid'],'question_id' => $oldqid])->delete();
  1234. // }
  1235. // }
  1236. return json(['code' => 0, 'paperid' => $param['paperid'], 'msg' => '修改成功']);
  1237. }
  1238. // public function papereditscore()
  1239. // {
  1240. // $root_id = request()->employee->root_id;
  1241. // $typeEn = ['单选' => 'single', '多选' => 'multi', '判断' => 'judge', '简答' => 'answer'];
  1242. // if (!Request::isAjax()) {
  1243. // $paperid = Request::param('paperid');
  1244. // View::assign('paperid', $paperid);
  1245. // $w[] = ['root_id', '=', $root_id];
  1246. // $w[] = ['paper_id', '=', $paperid];
  1247. // // $idsarr = ExamPaperQuestion::where($w)->column('question_id');
  1248. // // $types = ExamQuestion::where([['id','in',$idsarr]])->column('type');
  1249. // $questions = ExamPaperQuestion::with('questions')->where($w)->field('question_id,score,id')->select()->toArray();
  1250. // $types = ['type0' => '单选', 'type1' => '多选', 'type2' => '判断', 'type3' => '简答'];
  1251. // $sum = array_sum(array_column($questions, 'score'));
  1252. // foreach ($types as $k => $v) {
  1253. // $data[$k]['count'] = 0; //分类下题目数量
  1254. // $data[$k]['score_sum'] = 0; //合计分值
  1255. // $data[$k]['grawth'] = 0; //分值占比
  1256. // $data[$k]['score'] = ''; //每题分值
  1257. // $data[$k]['questions'] = [];
  1258. // foreach ($questions as $k2 => $v2) {
  1259. // $v2['questions']['score'] = $v2['score'] ? $v2['score'] . '分' : '';
  1260. // $v2['questions']['del_id'] = $v2['id'];
  1261. // $v2['questions']['ask'] = strip_tags($v2['questions']['ask']);
  1262. // if ($v2['questions']['type'] == '简答') {
  1263. // $v2['questions']['answer_j'] = $v2['questions']['answer'];
  1264. // $v2['questions']['answer'] = $v2['questions']['content'];
  1265. // }
  1266. // if ($v2['questions']['type'] == $v) {
  1267. // $data[$k]['count'] += 1;
  1268. // $data[$k]['score_sum'] = $data[$k]['score_sum'] + $v2['score'];
  1269. // $data[$k]['score'] = $v2['score'] ?: '';
  1270. // if ($k == 'type0' || $k == 'type1') {
  1271. // $v2['questions']['content'] = json_decode($v2['questions']['content'], true);
  1272. // }
  1273. // $data[$k]['questions'][] = $v2['questions'];
  1274. // }
  1275. // }
  1276. // $data[$k]['grawth'] = !$sum ? '0%' : round($data[$k]['score_sum'] / $sum * 100, 2) . '%';
  1277. // }
  1278. // $w1[] = ['root_id', '=', $root_id];
  1279. // $w1[] = ['id', '=', $paperid];
  1280. // $paperInfo = ExamPaper::where($w1)->field('target_group,base_score,starttime,endtime,checkway,desc,state,approve_employee_ids,scope_org,duringtime')->find()->toArray();
  1281. // $data['base_score'] = $paperInfo['base_score'] ?: 0;
  1282. // View::assign('data', $data);
  1283. // return View::fetch();
  1284. // }
  1285. // $param = Request::param();
  1286. // $paperid = $param['paperid'];
  1287. // $w[] = ['root_id', '=', $root_id];
  1288. // $w[] = ['paper_id', '=', $paperid];
  1289. // $idsarr = ExamPaperQuestion::where($w)->column('question_id');
  1290. // $questions = ExamQuestion::where([['id', 'in', $idsarr]])->select();
  1291. // $totalscore = 0;
  1292. // foreach ($questions as $item) {
  1293. // ExamPaperQuestion::where(['paper_id' => $paperid, 'question_id' => $item->id, 'root_id' => $root_id])->update(['score' => $param[$typeEn[$item->type]]]);
  1294. // $totalscore += $param[$typeEn[$item->type]];
  1295. // }
  1296. // $w1[] = ['root_id', '=', $root_id];
  1297. // $w1[] = ['id', '=', $paperid];
  1298. // ExamPaper::where($w1)->update(['total_score' => $totalscore, 'base_score' => $param['base_score']]);
  1299. // return json(['code' => 0, 'paperid' => $paperid, 'msg' => '设置成功']);
  1300. // }
  1301. /**
  1302. * 编辑时获取设置分值
  1303. */
  1304. public function papereditscore()
  1305. {
  1306. $root_id = request()->employee->root_id;
  1307. $paperid = Request::param('paper_id');
  1308. $questions = ExamPaperQuestion::with('questions')->where([['root_id', '=', $root_id], ['paper_id', '=', $paperid]])->field('question_id,score,id')->select()->toArray();
  1309. $types = ['type0' => '单选', 'type1' => '多选', 'type2' => '判断', 'type3' => '简答'];
  1310. $sum = array_sum(array_column($questions, 'score'));
  1311. foreach ($types as $k => $v) {
  1312. $data[$k]['count'] = 0; //分类下题目数量
  1313. $data[$k]['score_sum'] = 0; //合计分值
  1314. $data[$k]['grawth'] = 0; //分值占比
  1315. $data[$k]['score'] = 0; //每题分值
  1316. $data[$k]['questions'] = [];
  1317. foreach ($questions as $k2 => $v2) {
  1318. $v2['questions']['score'] = $v2['score'];
  1319. $v2['questions']['del_id'] = $v2['id'];
  1320. $v2['questions']['ask'] = strip_tags($v2['questions']['ask']);
  1321. if ($v2['questions']['type'] == '简答') {
  1322. $v2['questions']['answer_j'] = $v2['questions']['answer'];
  1323. $v2['questions']['answer'] = $v2['questions']['content'];
  1324. }
  1325. if ($v2['questions']['type'] == $v) {
  1326. $data[$k]['count'] += 1;
  1327. $data[$k]['score_sum'] = $data[$k]['score_sum'] + $v2['score'];
  1328. $data[$k]['score'] = $v2['score'] ?: '';
  1329. if ($k == 'type0' || $k == 'type1') {
  1330. $v2['questions']['content'] = json_decode($v2['questions']['content'], true);
  1331. }
  1332. $data[$k]['questions'][] = $v2['questions'];
  1333. }
  1334. }
  1335. $data[$k]['grawth'] = !$sum ? '0%' : round($data[$k]['score_sum'] / $sum * 100, 2) . '%';
  1336. }
  1337. $paperInfo = ExamPaper::where([['root_id', '=', $root_id], ['id', '=', $paperid]])->field('target_group,total_score,base_score,starttime,endtime,checkway,desc,state,approve_employee_ids,scope_org,duringtime')->find();
  1338. $data['base_score'] = $paperInfo && $paperInfo['base_score'] ? $paperInfo['base_score'] : 0;
  1339. $data['total_score'] = $paperInfo && $paperInfo['total_score'] ? $paperInfo['total_score'] : 0;
  1340. return json(['code' => 0, 'data' => $data, 'msg' => '获取成功']);
  1341. }
  1342. public function papereditpublish()
  1343. {
  1344. $root_id = request()->employee->root_id;
  1345. $request = request();
  1346. if (!$request->isAjax()) {
  1347. $org = OrgLogic::struc(request()->employee->root_id);
  1348. View::assign('org', $org);
  1349. View::assign('orgids', json_encode($request->org));
  1350. View::assign('empid', $request->employee->id);
  1351. // View::assign('manager', $request->employee->is_manager);
  1352. $paperid = $request->param('paperid');
  1353. View::assign('paperid', $paperid);
  1354. $w1[] = ['root_id', '=', $root_id];
  1355. $w1[] = ['id', '=', $paperid];
  1356. $paperInfo = ExamPaper::where($w1)->field('target_group,base_score,starttime,endtime,checkway,desc,state,approve_employee_ids,scope_org,duringtime,assessment')->find()->toArray();
  1357. $paperInfo['state'] = (string)$paperInfo['state'];
  1358. $data['duringtime'] = $paperInfo['duringtime'] = $paperInfo['duringtime'] ?: 45;
  1359. View::assign('paperinfostr', json_encode($paperInfo));
  1360. $data['base_score'] = $paperInfo['base_score'];
  1361. $data['total_score'] = input('total_score', 0);
  1362. $data['assessment'] = $paperInfo['assessment'];
  1363. View::assign('data', $data);
  1364. return View::fetch();
  1365. }
  1366. $param = input('post.');
  1367. //exit;
  1368. $paperid = $param['paperid'];
  1369. $w[] = ['root_id', '=', $root_id];
  1370. $w[] = ['paper_id', '=', $paperid];
  1371. $sum = ExamPaperQuestion::where($w)->sum('score');
  1372. if ($param['base_score'] <= 0 || $param['base_score'] > $sum) {
  1373. return json(['code' => 1, 'msg' => '及格分设置错误']);
  1374. }
  1375. unset($param['paperid']);
  1376. //
  1377. $org_id = $param['scope_org'] = 0;
  1378. // $attendee_num = EmployeeLogic::count($org_id);
  1379. $select = input('select', '');
  1380. $param['attendee_num'] = count(explode(',', $select));
  1381. $param['assessment'] = $select;
  1382. unset($param['select']);
  1383. //
  1384. $seconds = strtotime($param['endtime']) - strtotime($param['starttime']);
  1385. // $param['duringtime'] = ceil($seconds/60);
  1386. $w1[] = ['root_id', '=', $root_id];
  1387. $w1[] = ['id', '=', $paperid];
  1388. $info = ExamPaper::where($w1)->find();
  1389. ExamPaper::where($w1)->update($param);
  1390. if ($info['state'] == 1){
  1391. $old_select = array_filter(explode(',', $info['assessment']));
  1392. $new_select = array_filter(explode(',', $select));
  1393. $diff_select = array_diff($new_select, $old_select);
  1394. foreach($diff_select as $eid) {
  1395. event(new Msg($eid, '有新的试卷指派给您!', 'assignPaper', $paperid));
  1396. }
  1397. }
  1398. return json(['code' => 0, 'paperid' => $paperid, 'msg' => '考卷发布成功']);
  1399. }
  1400. ///批卷部分
  1401. public function checklist()
  1402. {
  1403. $root_id = request()->employee->root_id;
  1404. $request = request();
  1405. $paperid = input('paperid');
  1406. if (!$request->isAjax()) {
  1407. $org = OrgLogic::struc(request()->employee->root_id);
  1408. View::assign('org', $org);
  1409. View::assign('orgids', json_encode($request->org));
  1410. View::assign('empid', $request->employee->id);
  1411. // View::assign('manager', $request->employee->is_manager);
  1412. $pw[] = ['root_id', '=', $root_id];
  1413. $paperlist = ExamPaper::where($pw)->field('id,name')->select()->toArray();
  1414. $w[] = ['state', '=', '在职'];
  1415. $w[] = ['root_id', '=', request()->employee->root_id];
  1416. $employee = Employee::where($w)->field('id,name')->select()->toArray();
  1417. $data['employee'] = $employee;
  1418. View::assign('data', $data);
  1419. view::assign('paperid', $paperid);
  1420. View::assign('paperlist', $paperlist);
  1421. return View::fetch();
  1422. }
  1423. $param = Request::param();
  1424. $where[] = ['state', '=', 1];
  1425. $where[] = ['root_id', '=', $root_id];
  1426. if (isset($param['paper_id']) && $param['paper_id']) {
  1427. $where[] = ['paper_id', '=', $param['paper_id']];
  1428. }
  1429. if (isset($param['eid']) && $param['eid']) {
  1430. $where[] = ['employee_id', '=', $param['eid']];
  1431. }
  1432. if (isset($param['approve_status']) && $param['approve_status'] !== '') {
  1433. $where[] = ['approve_status', '=', $param['approve_status']];
  1434. }
  1435. $where[] = ['from', 'in', [0, 1]];
  1436. $list = ExamEmpResult::where($where)->with(['paper', 'employee'])->page($param['page'], $param['limit'])->order('addtime desc')->select();
  1437. $count = ExamEmpResult::where($where)->count();
  1438. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  1439. }
  1440. public function checkdetail()
  1441. {
  1442. $root_id = request()->employee->root_id;
  1443. $request = request();
  1444. if (!$request->isAjax()) {
  1445. $resultid = $request->param('resultid');
  1446. $w[] = ['id', '=', $resultid];
  1447. // $w[] = ['from','=',0];
  1448. $empresult = ExamEmpResult::with(['employee', 'employees'])->where($w)->find();
  1449. if(empty($empresult->p_name)){
  1450. $empresult->p_name = Employee::where([['root_id','=',$root_id],['state','=','在职'],['id','=',$empresult->aprove_employee_id]])->value('opt_name');
  1451. }
  1452. View::assign('result_checked', $empresult->checked);
  1453. View::assign('employee_name', $empresult->emp_name);
  1454. View::assign('final_score', $empresult->final_score);
  1455. View::assign('time_spend', $empresult->time_spend);
  1456. View::assign('resultid', $resultid);
  1457. View::assign('result', $empresult);
  1458. //批改详情
  1459. $correct = $empresult->data ? json_decode($empresult->data,true) : [];
  1460. $emp_answer_arr = [];
  1461. $emp_answer_json = $empresult->answer;
  1462. if ($emp_answer_json) {
  1463. $emp_answer_arr = json_decode($emp_answer_json, true);
  1464. }
  1465. $paperInfo = ExamPaper::with('questions')->find($empresult->paper_id)->toArray();
  1466. $paperInfo['got_total_score'] = 0;
  1467. foreach ($paperInfo['questions'] as &$item) {
  1468. $item['ask'] = strip_tags($item['ask']);
  1469. //$item['content'] = strip_tags($item['content']);
  1470. $item['content'] = $item['content'];
  1471. $item['emp_answer'] = isset($emp_answer_arr[$item['id']]) ? $emp_answer_arr[$item['id']] : ''; //填写的答案
  1472. if ($item['type'] == '单选' || $item['type'] == '多选' || $item['type'] == '判断') {
  1473. if (isset($emp_answer_arr[$item['id']]) && $emp_answer_arr[$item['id']]) {
  1474. $item['got_score'] = ($emp_answer_arr[$item['id']] == $item['answer']) ? $item['pivot']['score'] : 0;
  1475. } else {
  1476. $item['got_score'] = 0;
  1477. }
  1478. } elseif ($item['type'] == '简答') {
  1479. if (isset($emp_answer_arr[$item['id']]) && $emp_answer_arr[$item['id']]) {
  1480. $calldata = ExamEmpResult::autoPaperAnswerCheck($emp_answer_arr[$item['id']], $item['answer'], $item['pivot']['score']);
  1481. $item['got_score'] = $calldata['got_score'];
  1482. } else {
  1483. $item['got_score'] = 0;
  1484. }
  1485. }
  1486. $paperInfo['got_total_score'] += $item['got_score']; //得分总和
  1487. if ($item['type'] == '单选' || $item['type'] == '多选') {
  1488. $item['types'] = 1;
  1489. $con = json_decode($item['content'], true);
  1490. $op = [];
  1491. foreach ($con as $v2) {
  1492. $op[$v2['title']] = $v2['content'];
  1493. }
  1494. $item['content'] = $op;
  1495. // $v['answer'] = explode(',',$v['answer']);
  1496. } elseif ($item['type'] == '判断') {
  1497. $item['types'] = 2;
  1498. } elseif ($item['type'] == '简答') {
  1499. $item['types'] = 3;
  1500. }
  1501. //考生得分
  1502. $item['correct_score'] = 0;
  1503. if (isset($correct[$item['id']])) {
  1504. $item['correct_score'] = is_array($correct[$item['id']]) ? $correct[$item['id']]['score'] : $correct[$item['id']];
  1505. }
  1506. }
  1507. View::assign('info', $paperInfo);
  1508. return View::fetch();
  1509. }
  1510. }
  1511. /**
  1512. * 试卷的试题列表
  1513. */
  1514. public function correct_papers_question_list()
  1515. {
  1516. $root_id = request()->employee->root_id;
  1517. $param = request()->only(['paper_id' => 0, 'employee_id','resultid'=>0]);
  1518. //是否参加考试
  1519. $w[] = ['paper_id', '=', $param['paper_id']];
  1520. $w[] = ['employee_id', '=', $param['employee_id']];
  1521. $w[] = ['root_id', '=', $root_id];
  1522. $w[] = ['from', 'in', [0, 1]];
  1523. $empresult = ExamEmpResult::where($w)->with(['employee'])->find();
  1524. if (!$empresult) return json(['code' => 1, 'msg' => '没有参加考试']);
  1525. //批改详情
  1526. $correct = $empresult->data ? json_decode($empresult->data, true) : [];
  1527. //试卷题目列表
  1528. $emp_answer_arr = [];
  1529. $emp_answer_json = $empresult->answer;
  1530. if ($emp_answer_json) {
  1531. $emp_answer_arr = json_decode($emp_answer_json, true);
  1532. }
  1533. $paperInfo = ExamPaper::with('questions')->find($param['paper_id'])->toArray();
  1534. $paperInfo['got_total_score'] = 0; //
  1535. $options = [];
  1536. $is_answer = 0;
  1537. foreach ($paperInfo['questions'] as &$item) {
  1538. $item['ask'] = str_replace('\\', '', $item['ask']);
  1539. $item['emp_answer'] = isset($emp_answer_arr[$item['id']]) ? $emp_answer_arr[$item['id']] : ''; //填写的答案
  1540. //得分
  1541. if ($item['type'] == '单选' || $item['type'] == '多选') {
  1542. if (isset($emp_answer_arr[$item['id']]) && $emp_answer_arr[$item['id']]) {
  1543. $item['got_score'] = ($emp_answer_arr[$item['id']] == $item['answer']) ? $item['pivot']['score'] : 0;
  1544. $item['c_type'] = $item['got_score'] > 0 ? 1 : 0; //1正确,0错误
  1545. } else {
  1546. $item['got_score'] = 0;
  1547. }
  1548. $con = json_decode($item['content'], true);
  1549. $op = [];
  1550. foreach ($con as $v2) {
  1551. $op[$v2['title']] = $v2['content'];
  1552. }
  1553. $item['content'] = $op;
  1554. $item['types'] = 1;
  1555. //
  1556. } elseif ($item['type'] == '判断') {
  1557. if (isset($emp_answer_arr[$item['id']]) && $emp_answer_arr[$item['id']]) {
  1558. $item['got_score'] = ($emp_answer_arr[$item['id']] == $item['answer']) ? $item['pivot']['score'] : 0;
  1559. $item['c_type'] = $item['got_score'] > 0 ? 1 : 0; //1正确,0错误
  1560. } else {
  1561. $item['got_score'] = 0;
  1562. }
  1563. $item['types'] = 2;
  1564. } elseif ($item['type'] == '简答') {
  1565. if (isset($emp_answer_arr[$item['id']]) && $emp_answer_arr[$item['id']]) {
  1566. $calldata = ExamEmpResult::autoPaperAnswerCheck($emp_answer_arr[$item['id']], $item['answer'], $item['pivot']['score']);
  1567. $item['got_score'] = $calldata['got_score'];
  1568. } else {
  1569. $item['got_score'] = 0;
  1570. }
  1571. $item['c_type'] = $item['got_score'] > 0 ? 1 : 0; //1正确,0错误
  1572. //简单题表字段 内容存的是参考答案 答案字段存的是关键词
  1573. $answer = $item['answer'];
  1574. $content = $item['content'];
  1575. $item['answer'] = $item['content'];
  1576. $item['content'] = $item['ask'];
  1577. $item['types'] = 3;
  1578. $is_answer = 1;
  1579. }
  1580. $paperInfo['got_total_score'] += $item['got_score']; //得分总和
  1581. //是否批改过
  1582. $item['is_correct'] = isset($correct[$item['id']]) ? 1 : 0;
  1583. //批改分数
  1584. $item['correct_score'] = 0;
  1585. if (isset($correct[$item['id']])) {
  1586. $item['correct_score'] = is_array($correct[$item['id']]) ? $correct[$item['id']]['score'] : $correct[$item['id']];
  1587. }
  1588. $options[] = $item;
  1589. }
  1590. View::assign('is_answer',$is_answer);
  1591. View::assign('options',$options);
  1592. View::assign('resultid',$param['resultid']);
  1593. View::assign('employee_id',$param['employee_id']);
  1594. View::assign('paperid',$param['paper_id']);
  1595. return View::fetch();
  1596. //return json(['code' => 0, 'data' => $options, 'msg' => '获取成功']);
  1597. }
  1598. /**
  1599. * 批改试卷
  1600. */
  1601. public function correct_papers()
  1602. {
  1603. $root_id = request()->employee->root_id;
  1604. $param = request()->only(['paperid' => 0, 'employee_id','answer'=>'','result_id']);
  1605. //权限验证
  1606. //$paper = ExamPaper::with('questions')->where('id', $param['paperid'])->whereRaw("FIND_IN_SET(" . request()->employee->id. " , approve_employee_ids)")->find();
  1607. //if (!$paper) return json(['code' => 1, 'msg' => '无权限批改']);
  1608. $paper = ExamPaper::with('questions')->where('id', $param['paperid'])->find();
  1609. if ($paper->checkway == 'sys') return json(['code' => 1, 'msg' => '该试卷由系统批改']);
  1610. //是否参加考试
  1611. $w[] = ['paper_id', '=', $param['paperid']];
  1612. $w[] = ['employee_id', '=', $param['employee_id']];
  1613. $w[] = ['root_id', '=', $root_id];
  1614. $w[] = ['id', '=', $param['result_id']];
  1615. $w[] = ['approve_status', 'in', [0, 1]]; // 只查询考试完毕,未批改的试卷
  1616. $empresult = ExamEmpResult::where($w)->with(['employee'])->find();
  1617. if (!$empresult) return json(['code' => 1, 'msg' => '未考试或已批改']);
  1618. if ($empresult['state'] == 0) return json(['code' => 1, 'msg' => '答题失效,无法批改']);
  1619. if ($empresult['approve_status'] == 0 && $empresult->aprove_employee_id != 0 && request()->employee->id != $empresult->aprove_employee_id) {
  1620. return json(['code' => 1, 'msg' => '试卷已在批改中,请选择其他试卷进行批改']);
  1621. }
  1622. // 题目卷面分(人工批改)
  1623. $answer = json_decode($param['answer'], true);
  1624. $aids = array_column($answer,'id');
  1625. $ascore = array_column($answer,'mannal');
  1626. $json = array_combine($aids,$ascore);
  1627. //考生填写的答案
  1628. $jsona = $empresult->answer;
  1629. $jsona = json_decode($jsona, true);
  1630. // 分数结果
  1631. $data = [];
  1632. $score = 0;
  1633. foreach ($paper->questions as $question) {
  1634. $questionScore = 0;
  1635. // 系统打分
  1636. if (isset($jsona[$question->id]))
  1637. $questionScore = $this->fraction($jsona[$question->id], $question->answer, $question->type, (int)$question->pivot->score);
  1638. if (isset($json[$question->id])) { // 人工打分
  1639. $questionScore = $json[$question->id];
  1640. $data[$question->id] = [
  1641. 'id' => $question->id,
  1642. 'score' => $questionScore,
  1643. 'time' => date('Y-m-d H:i:s'),
  1644. ];
  1645. }
  1646. $score += $questionScore;
  1647. }
  1648. $u['answer'] = json_encode($jsona);
  1649. $u['final_score'] = $score;
  1650. //确认批改完成
  1651. $u['approve_status'] = 2;
  1652. $u['data'] = json_encode($data);
  1653. $u['aprove_employee_id'] = request()->employee->id;
  1654. $u['aprove_time'] = date('Y-m-d H:i:s');
  1655. ExamEmpResult::where($w)->update($u);
  1656. //发送消息通知
  1657. event(new Msg($param['employee_id'], '您提交的试卷“' . $paper['name'] . '”' . '已批改', 'correctionPaper', $paper['id']));
  1658. //下一张,待批改的试卷
  1659. // $w = [];
  1660. // $root_id = $token['root_org'];
  1661. // $w[] = ['exam_emp_result.root_id', '=', $root_id];
  1662. // $w[] = ['paper.root_id', '=', $root_id];
  1663. // $w[] = ['exam_emp_result.state', '=', 1];
  1664. // $w[] = ['exam_emp_result.approve_status', '<>', 2];
  1665. // $w[] = ['paper.id', '=', $paperid];
  1666. // $id = ExamEmpResult::withJoin('paper')->where($w)->whereRaw("FIND_IN_SET(" . $token['employee_id'] . " , approve_employee_ids)")->order('exam_emp_result.id asc')->find();
  1667. // if ($id) {
  1668. // $id = $id->toArray();
  1669. // } else {
  1670. // $id = [];
  1671. // }
  1672. return json(['code' => 0, 'data' =>'', 'msg' => '批改成功']);
  1673. //return View::fetch();
  1674. }
  1675. //计算分数
  1676. public function fraction($emp_answer, $answer, $type, $score = 0, $requestid = 0, $questionsid = 0)
  1677. {
  1678. $emp_score = 0;
  1679. if (!$emp_answer) return $emp_score;
  1680. if ($type == '判断') {
  1681. if (trim($answer) == trim($emp_answer)) {
  1682. $emp_score = $score;
  1683. }
  1684. } elseif ($type == '单选') {
  1685. if ($answer == $emp_answer) {
  1686. $emp_score = $score;
  1687. }
  1688. } elseif ($type == '多选') {
  1689. $answer = explode(',', $answer);
  1690. $emp_answer = explode(',', $emp_answer);
  1691. sort($answer);
  1692. sort($emp_answer);
  1693. if (implode(',', $answer) == implode(',', $emp_answer)) {
  1694. $emp_score = $score;
  1695. } else {
  1696. $danf = $score / count($answer);
  1697. $i = 0;
  1698. foreach ($emp_answer as $k => $v) {
  1699. if (in_array($v, $answer)) {
  1700. $i += 1;
  1701. }
  1702. }
  1703. $emp_score = $i * $danf;
  1704. }
  1705. } elseif ($type == '简答' || $type == '问答') {
  1706. $emp_score = $answer = ExamEmpResult::autoPaperAnswerCheck($emp_answer, $answer, $score);
  1707. $emp_score = $emp_score['got_score'];
  1708. $emp_answer = $answer['emp_answer'];
  1709. if ($requestid && $questionsid) {
  1710. $tw[] = ['id', '=', $requestid];
  1711. $tw[] = ['from', '=', 0];
  1712. $json = ExamEmpResult::where($tw)->value('answer');
  1713. $json = json_decode($json, true);
  1714. if (isset($json[$questionsid])) {
  1715. $json[$questionsid] = $emp_answer;
  1716. ExamEmpResult::where($tw)->update(['answer' => json_encode($json)]);
  1717. }
  1718. }
  1719. }
  1720. return $emp_score;
  1721. }
  1722. /**
  1723. * 修改视图
  1724. */
  1725. public function makecheck()
  1726. {
  1727. $root_id = request()->employee->root_id;
  1728. $request = request();
  1729. $param = $request->post();
  1730. $updatedata = [
  1731. 'check_result' => $param['data'],
  1732. 'final_score' => $param['finalscore'],
  1733. 'checked' => 1
  1734. ];
  1735. $w1[] = ['root_id', '=', $root_id];
  1736. $w1[] = ['id', '=', $param['resultid']];
  1737. $w1[] = ['from', '=', 0];
  1738. if (ExamEmpResult::where($w1)->update($updatedata)) {
  1739. return json(['code' => 0, 'msg' => '审核成功']);
  1740. } else {
  1741. return json(['code' => 1, 'msg' => '审核失败']);
  1742. }
  1743. }
  1744. public function tree($data, $pid = 0, $persons)
  1745. {
  1746. $new_arr = [];
  1747. foreach ($data as $k => $v) {
  1748. if ($v['pid'] == $pid) {
  1749. $persions = isset($persons[$v['id']]) ? $persons[$v['id']] : [];
  1750. $children = $this->tree($data, $v['id'], $persons);
  1751. $v['children'] = array_merge_recursive($children, $persions);
  1752. if (empty($v['children'])) $v['disabled'] = true;
  1753. $new_arr[] = $v;
  1754. }
  1755. }
  1756. return $new_arr;
  1757. }
  1758. /*
  1759. * 审核人员
  1760. */
  1761. public function get_person()
  1762. {
  1763. $ids = input('id', '');
  1764. $root_id = request()->employee->root_id;
  1765. $type = input('type', 0);
  1766. //树形
  1767. if ($type == 1) {
  1768. //所有员工
  1769. $w1[] = ['root_id', '=', $root_id];
  1770. $w1[] = ['state', '=', '在职'];
  1771. $w1[] = ['uid', '<>', 0];
  1772. $w1[] = ['org_id', '>', 0];
  1773. $employee = Employee::where($w1)->field('id value,name,org_id')->select()->toArray();
  1774. if ($ids) {
  1775. $ids = explode(',', $ids);
  1776. foreach ($employee as $k => &$v) {
  1777. $v['selected'] = in_array($v['value'], $ids);
  1778. }
  1779. } else {
  1780. foreach ($employee as $k => &$v) {
  1781. $v['selected'] = false;
  1782. }
  1783. }
  1784. $persons = [];
  1785. foreach ($employee as $k => $v1) {
  1786. $persons[$v1['org_id']][] = $v1;
  1787. }
  1788. $where = [
  1789. ['path', 'like', $root_id . '-%'],
  1790. ['status', '=', 1]
  1791. ];
  1792. $allnodes = Org::where($where)->field('id value,id,pid,name')->order('level asc, id asc')->select()->toArray();
  1793. $tree = $this->tree($allnodes, 0, $persons);
  1794. return json($tree);
  1795. }
  1796. $w[] = ['root_id', '=', $root_id];
  1797. $w[] = ['state', '=', '在职'];
  1798. $w[] = ['role', '=', '领导'];
  1799. $w[] = ['uid', '<>', 0];
  1800. $employee = Employee::where($w)->field('id value,name')->select()->toArray();
  1801. if ($ids) {
  1802. $ids = explode(',', $ids);
  1803. foreach ($employee as $k => &$v) {
  1804. $v['selected'] = in_array($v['value'], $ids);
  1805. }
  1806. } else {
  1807. foreach ($employee as $k => &$v) {
  1808. $v['selected'] = false;
  1809. }
  1810. }
  1811. return json($employee);
  1812. }
  1813. /*
  1814. * 发布
  1815. */
  1816. public function show()
  1817. {
  1818. $id = input('id', '');
  1819. $info = $paper = ExamPaper::where('id', $id)->find();
  1820. if ($info->state == 0) {
  1821. ExamPaper::where('id', $id)->update(['state' => 1]);
  1822. $selects = explode(',', $info['assessment']);
  1823. foreach($selects as $eid) {
  1824. event(new Msg($eid, '有新的试卷指派给您!', 'assignPaper', $id));
  1825. }
  1826. return json(['code' => 0, 'msg' => '发布成功']);
  1827. }
  1828. $root_id = request()->employee->root_id;
  1829. $w = [];
  1830. $w[] = ['exam_emp_result.root_id', '=', $root_id];
  1831. $w[] = ['paper.root_id', '=', $root_id];
  1832. // $w[] = ['exam_emp_result.state','=',1];
  1833. // $w[] = ['exam_emp_result.approve_status','<>',2];
  1834. $w[] = ['paper.id', '=', $id];
  1835. $info = ExamEmpResult::where('paper_id', $id)->find();
  1836. $time = date('Y-m-d H:i:s');
  1837. if ($info && $paper['starttime'] <= $time && $paper['endtime'] >= $time) {
  1838. return json(['code' => 1, 'msg' => '试卷正在考试中无法修改']);
  1839. }
  1840. $res = ExamPaper::where('id', $id)->update(['state' => 0]);
  1841. return json(['code' => 0, 'msg' => '取消成功']);
  1842. }
  1843. public function statistics()
  1844. {
  1845. $paperid = input('paperid', 0);
  1846. $root_id = request()->employee->root_id;
  1847. if (!Request::isAjax() && input('ajax', 0) != 1) {
  1848. View::assign('paperid', $paperid);
  1849. $info = ExamPaper::where('id', $paperid)->find()->toArray();
  1850. $data['count'] = $info['assessment'] ? count(explode(',', $info['assessment'])) : 0; //应考总数
  1851. $tw[] = ['paper_id', '=', $paperid];
  1852. $tw[] = ['from', '=', 0];
  1853. $data['result_count'] = ExamEmpResult::where($tw)->count(); //实际考试数量
  1854. $data['que_count'] = $data['count'] - $data['result_count']; //缺考人数
  1855. $data['que_count'] = $data['que_count'] >= 0 ? $data['que_count'] : 0;
  1856. $w[] = ['paper_id', '=', $paperid];
  1857. $w[] = ['root_id', '=', $root_id];
  1858. $w[] = ['approve_status', '=', 2];
  1859. $w[] = ['from', '=', 0];
  1860. $result = ExamEmpResult::where($w)->field('employee_id,paper_id,final_score,state,addtime')->order('final_score desc')->select()->toArray();
  1861. $data['jg_count'] = $data['bjg_count'] = $data['avg'] = $data['top_score'] = $data['down_score'] = 0;
  1862. $data['grawth'] = '0%';
  1863. if ($result) {
  1864. $data['down_score'] = 999999999999;
  1865. foreach ($result as $k => $v) {
  1866. if ($v['final_score'] >= $info['base_score']) {
  1867. $data['jg_count'] += 1;
  1868. } else {
  1869. $data['bjg_count'] += 1;
  1870. }
  1871. if ($v['final_score'] > $data['top_score']) {
  1872. $data['top_score'] = $v['final_score'];
  1873. }
  1874. if ($v['final_score'] < $data['down_score']) {
  1875. $data['down_score'] = $v['final_score'];
  1876. }
  1877. }
  1878. $data['avg'] = round(array_sum(array_column($result, 'final_score')) / count($result), 2);
  1879. $data['grawth'] = round($data['jg_count'] / ($data['jg_count'] + $data['bjg_count']) * 100, 2) . '%';
  1880. }
  1881. View::assign('data', $data);
  1882. $org = Org::where([['path', 'like', request()->employee->root_id . '-%']])->field('id,name')->select()->toArray();
  1883. View::assign('org', $org);
  1884. return View::fetch();
  1885. }
  1886. $type = input('type', 0);
  1887. $page = input('page', 0);
  1888. $limit = input('limit', 0);
  1889. $org = input('org', 0);
  1890. $ew[] = ['id', '=', $paperid];
  1891. $ew[] = ['root_id', '=', $root_id];
  1892. $info = ExamPaper::where($ew)->find();
  1893. if (!$info) return json(['code' => 1, 'data' => '未查询到该试卷', 'msg' => '未查询到该试卷']);
  1894. // if (empty($info->assessment)) return json(['code' => 0,'data'=>[],'count'=>0]);
  1895. if ($org) {
  1896. $w[] = ['org_id', '=', $org];
  1897. }
  1898. $w[] = ['id', 'in', explode(',', $info->assessment)];
  1899. $w[] = ['root_id', '=', $root_id];
  1900. $res = Employee::with(['examResult' => function ($query) use ($paperid) {
  1901. $query->where([['paper_id', '=', $paperid]])->bind(['final_score', 'addtime', 'approve_status']);
  1902. // ->visible(['approve_status','final_score','addtime']);
  1903. }])->where($w)->field('id,name')->select();
  1904. $complete = $nocomplete = [];
  1905. foreach ($res as $k => $v) {
  1906. if ($v['approve_status'] == 2) {
  1907. $complete[] = $v;
  1908. } else {
  1909. $v['addtime'] = '-------';
  1910. $v['final_score'] = 0;
  1911. $nocomplete[] = $v;
  1912. }
  1913. }
  1914. array_multisort(array_column($complete, 'final_score'), SORT_DESC, $complete);
  1915. array_multisort(array_column($nocomplete, 'id'), SORT_DESC, $nocomplete);
  1916. $res = array_merge($complete, $nocomplete);
  1917. foreach ($res as $k2 => $v2) {
  1918. $res[$k2]['ranking'] = $k2 + 1;
  1919. if ($type == 1 && $v2['final_score'] < $info->base_score) unset($res[$k2]);
  1920. if ($type == 2 && $v2['final_score'] >= $info->base_score) unset($res[$k2]);
  1921. }
  1922. $count = count($res);
  1923. $res = array_slice($res, ($page - 1) * $limit, $limit);
  1924. return json(['code' => 0, 'data' => $res, 'count' => $count, 'msg' => '']);
  1925. }
  1926. /**
  1927. * 获取试题列表
  1928. */
  1929. public function getExamQuestion(){
  1930. $root_id = request()->employee->root_id;
  1931. $questList = ExamQuestion::where([['root_id', '=', $root_id], ['state', '=', 1]])->field('id,type,ask,cate')->order('type', 'desc')->select()->toArray();
  1932. return json(['code'=> 0, 'data'=> $questList, 'msg'=> '获取成功']);
  1933. }
  1934. /**
  1935. * 创建试卷
  1936. */
  1937. public function paperadd()
  1938. {
  1939. $root_id = request()->employee->root_id;
  1940. if (!Request::isAjax()) {
  1941. $data = [];
  1942. //查询题目
  1943. // $questList = ExamQuestion::where([['root_id', '=', $root_id], ['state', '=', 1]])->field('id,type,ask,cate')->order('type', 'desc')->select()->toArray();
  1944. $data['data2'] = json_encode([]);
  1945. //分类
  1946. $cate = Setting::where([['root_id', '=', $root_id], ['name', '=', 'questioncate']])->value('content');
  1947. $data['cate'] = $cate ? json_decode($cate, true) : [];
  1948. // View::assign('questListstr', strip_tags(json_encode($questList)));
  1949. //发布考试
  1950. $org = OrgLogic::struc($root_id);
  1951. View::assign('org', $org);
  1952. View::assign('orgids', json_encode(request()->org));
  1953. View::assign('empid', request()->employee->id);
  1954. $data['starttime'] = date('Y-m-d H:i:s');
  1955. $data['endtime'] = date('Y-m-d H:i:s', strtotime('+2 day'));
  1956. $data['total_score'] = input('total_score', 0);
  1957. View::assign('data', $data);
  1958. return View::fetch();
  1959. }
  1960. $param = request()->only(['name', 'for_newbie', 'desc', 'idstr', 'total_score', 'base_score', 'single' => '', 'multi' => '', 'judge' => '', 'answer' => '', 'select', 'target_group', 'duringtime', 'starttime', 'endtime', 'checkway', 'state', 'approve_employee_ids', 'show_real_name']);
  1961. if (!trim($param['name'])) return json(['code' => 1, 'msg' => '请填写考卷名称']);
  1962. $type_questions = ExamQuestion::where([['id', 'in', $param['idstr']], ['root_id', '=', $root_id]])->column('type');
  1963. if($param['checkway'] == 'employee' && !in_array('简答',$type_questions)){
  1964. return json(['code' => 1, 'msg' => '人工审核时,必须选择简答题']);
  1965. }
  1966. $examPaperAdd = [
  1967. 'name' => $param['name'],
  1968. 'for_newbie' => $param['for_newbie'],
  1969. 'desc' => $param['desc'],
  1970. 'root_id' => $root_id,
  1971. 'state' => 0,
  1972. 'approve_employee_ids' => '',
  1973. 'assessment' => ''
  1974. ];
  1975. Db::startTrans();
  1976. try {
  1977. //试卷
  1978. $new_paper_id = ExamPaper::insertGetId($examPaperAdd);
  1979. //2023-01-30 修改逻辑 每道题可以单独设置分数 fraction=1@1,2@2
  1980. $scores = request()->only(['fraction'=>'']);
  1981. if(empty($scores['fraction'])) return json(['code' => 1, 'msg' => '请完善题目分数']);
  1982. $scores = explode(',',$scores['fraction']);
  1983. $questionIds = [];
  1984. foreach ($scores as $key => $val) {
  1985. $ls = explode('@',$val);
  1986. if(!isset($ls[1]) || $ls[1]=='0') return json(['code' => 1, 'msg' => '题目分数不能为0分']);
  1987. $questionIds[$ls[0]] = $ls[1];
  1988. }
  1989. $param['idstr'] = implode(',',array_keys($questionIds));
  1990. $totalscore = array_sum(array_values($questionIds));
  1991. //试题
  1992. $idarr = array_unique(explode(',', $param['idstr']));
  1993. $new_questions = ExamQuestion::where([['id', 'in', $idarr], ['root_id', '=', $root_id]])->select();
  1994. $save_data = [];
  1995. foreach ($new_questions as $k => $v) {
  1996. $once_data['paper_id'] = $new_paper_id;
  1997. $once_data['question_id'] = $v['id'];
  1998. $once_data['root_id'] = $root_id;
  1999. $once_data['score'] = $questionIds[$v['id']];
  2000. $save_data[] = $once_data;
  2001. }
  2002. (new ExamPaperQuestion())->saveAll($save_data);
  2003. //设置分值
  2004. // $typeEn = ['单选' => 'single', '多选' => 'multi', '判断' => 'judge', '简答' => 'answer'];
  2005. // $questions = ExamQuestion::where([['id', 'in', $idarr], ['root_id', '=', $root_id]])->select();
  2006. // $totalscore = 0;
  2007. // foreach ($questions as $item) {
  2008. // if (isset($param[$typeEn[$item->type]]) && !empty($param[$typeEn[$item->type]])) {
  2009. // ExamPaperQuestion::where(['paper_id' => $new_paper_id, 'question_id' => $item->id, 'root_id' => $root_id])->update(['score' => $param[$typeEn[$item->type]]]);
  2010. // $totalscore += $param[$typeEn[$item->type]];
  2011. // }
  2012. // }
  2013. ExamPaper::where([['root_id', '=', $root_id], ['id', '=', $new_paper_id]])->update(['total_score' => $totalscore, 'base_score' => $param['base_score']]);
  2014. //保存试卷
  2015. if ($param['base_score'] <= 0 || $param['base_score'] > $totalscore) {
  2016. return json(['code' => 1, 'msg' => '及格分设置错误']);
  2017. }
  2018. $selects = explode(',', $param['select']);
  2019. ExamPaper::where(['id' => $new_paper_id, 'root_id' => $root_id])->update([
  2020. 'assessment' => $param['select'], //考试人员
  2021. 'attendee_num' => count($selects),
  2022. 'duringtime' => $param['duringtime'],
  2023. 'starttime' => $param['starttime'],
  2024. 'endtime' => $param['endtime'],
  2025. 'target_group' => $param['target_group'],
  2026. 'base_score' => $param['base_score'],
  2027. 'state' => $param['state'],
  2028. 'checkway' => $param['checkway'],
  2029. 'approve_employee_ids' => $param['approve_employee_ids'],
  2030. 'show_real_name' => $param['show_real_name']
  2031. ]);
  2032. //消息通知
  2033. $state = ExamPaper::where('id', $new_paper_id)->value('state');
  2034. if ($state == 1){
  2035. foreach($selects as $eid) {
  2036. event(new Msg($eid, '有新的试卷指派给您!', 'assignPaper', $new_paper_id));
  2037. }
  2038. }
  2039. Db::commit();
  2040. return json(['code' => 0, 'msg' => '考卷发布成功']);
  2041. } catch (\Exception $e) {
  2042. Db::rollback();
  2043. return json(['code' => 1, 'msg' => '考卷发布失败']);
  2044. }
  2045. }
  2046. /**
  2047. * 试卷编辑
  2048. */
  2049. public function paperedit()
  2050. {
  2051. $root_id = request()->employee->root_id;
  2052. if (!Request::isAjax()) {
  2053. $paperid = Request::param('paperid');
  2054. View::assign('paperid', $paperid);
  2055. $data = ExamPaper::find($paperid);
  2056. $data['duringtime'] = $data['duringtime'] ?: 45;
  2057. //分类
  2058. $cate = Setting::where([['root_id', '=', $root_id], ['name', '=', 'questioncate']])->value('content');
  2059. $data['cate'] = $cate ? json_decode($cate, true) : [];
  2060. //左侧试题列表
  2061. $ids = ExamPaperQuestion::where([['root_id', '=', $root_id], ['paper_id', '=', $paperid]])->column('question_id');
  2062. $questList = ExamQuestion::where([['root_id', '=', $root_id], ['id', 'not in', $ids], ['state', '=', 1]])->field('id,type,ask,cate')->order('type', 'desc')->select()->toArray();
  2063. View::assign('questListstr', strip_tags(json_encode($questList)));
  2064. //右侧试题列表
  2065. $choosedquestListstr = ExamQuestion::where([['root_id', '=', $root_id], ['id', 'in', $ids], ['state', '=', 1]])->field('id,type,ask,cate')->order('type', 'desc')->select()->toArray();
  2066. View::assign('choosedquestListstr', strip_tags(json_encode($choosedquestListstr)));
  2067. //选择题目数量
  2068. $data['count'] = count($choosedquestListstr);
  2069. //考试人员
  2070. $org = OrgLogic::struc(request()->employee->root_id);
  2071. View::assign('org', $org);
  2072. View::assign('orgids', json_encode(request()->org));
  2073. View::assign('data', $data);
  2074. return View::fetch();
  2075. }
  2076. $param = Request::param();
  2077. $param = request()->only(['paper_id', 'name', 'for_newbie', 'desc', 'idstr', 'total_score', 'base_score', 'single' => '', 'multi' => '', 'judge' => '', 'answer' => '', 'select', 'target_group', 'duringtime', 'starttime', 'endtime', 'checkway', 'state', 'approve_employee_ids', 'show_real_name']);
  2078. if (!trim($param['name'])) return json(['code' => 1, 'msg' => '请填写考卷名称']);
  2079. $examPaperEdit = [
  2080. 'name' => $param['name'],
  2081. 'for_newbie' => $param['for_newbie'],
  2082. 'desc' => $param['desc'],
  2083. ];
  2084. Db::startTrans();
  2085. try {
  2086. //试卷
  2087. ExamPaper::where(['id'=>$param['paper_id'], 'root_id'=>$root_id])->update($examPaperEdit);
  2088. //试题
  2089. ExamPaperQuestion::where([['root_id', '=', $root_id], ['paper_id', '=', $param['paper_id']]])->delete();
  2090. $idarr = array_unique(explode(',', $param['idstr']));
  2091. //2023-01-30 修改逻辑 每道题可以单独设置分数 fraction=1@1,2@2
  2092. $scores = request()->only(['fraction'=>'']);
  2093. if(empty($scores['fraction'])) return json(['code' => 1, 'msg' => '请完善题目分数']);
  2094. $scores = explode(',',$scores['fraction']);
  2095. $questionIds = [];
  2096. foreach ($scores as $key => $val) {
  2097. $ls = explode('@',$val);
  2098. if(!isset($ls[1]) || $ls[1]=='0') return json(['code' => 1, 'msg' => '题目分数不能为0分']);
  2099. $questionIds[$ls[0]] = $ls[1];
  2100. }
  2101. $idarr = array_keys($questionIds);
  2102. $totalscore = array_sum(array_values($questionIds));
  2103. $new_questions = ExamQuestion::where([['id', 'in', $idarr], ['root_id', '=', $root_id]])->select();
  2104. $save_data = [];
  2105. foreach ($new_questions as $k => $v) {
  2106. $once_data['paper_id'] = $param['paper_id'];
  2107. $once_data['question_id'] = $v['id'];
  2108. $once_data['root_id'] = $root_id;
  2109. $once_data['score'] = $questionIds[$v['id']];
  2110. $save_data[] = $once_data;
  2111. }
  2112. (new ExamPaperQuestion())->saveAll($save_data);
  2113. //设置分值
  2114. // $typeEn = ['单选' => 'single', '多选' => 'multi', '判断' => 'judge', '简答' => 'answer'];
  2115. // $questions = ExamQuestion::where([['id', 'in', $idarr], ['root_id', '=', $root_id]])->select();
  2116. // $totalscore = 0;
  2117. // foreach ($questions as $item) {
  2118. // if (isset($param[$typeEn[$item->type]]) && !empty($param[$typeEn[$item->type]])) {
  2119. // ExamPaperQuestion::where(['paper_id' => $param['paper_id'], 'question_id' => $item->id, 'root_id' => $root_id])->update(['score' => $param[$typeEn[$item->type]]]);
  2120. // $totalscore += $param[$typeEn[$item->type]];
  2121. // }
  2122. // }
  2123. ExamPaper::where([['root_id', '=', $root_id], ['id', '=', $param['paper_id']]])->update(['total_score' => $totalscore, 'base_score' => $param['base_score']]);
  2124. //保存试卷
  2125. if ($param['base_score'] <= 0 || $param['base_score'] > $totalscore) {
  2126. return json(['code' => 1, 'msg' => '及格分设置错误']);
  2127. }
  2128. //原有指派人员
  2129. $exmaPaperAssessment = ExamPaper::where(['id' => $param['paper_id'], 'root_id' => $root_id])->value('assessment');
  2130. $selects = explode(',', $param['select']);
  2131. ExamPaper::where(['id' => $param['paper_id'], 'root_id' => $root_id])->update([
  2132. 'assessment' => $param['select'], //考试人员
  2133. 'attendee_num' => count($selects),
  2134. 'duringtime' => $param['duringtime'],
  2135. 'starttime' => $param['starttime'],
  2136. 'endtime' => $param['endtime'],
  2137. 'target_group' => $param['target_group'],
  2138. 'base_score' => $param['base_score'],
  2139. 'state' => $param['state'],
  2140. 'checkway' => $param['checkway'],
  2141. 'approve_employee_ids' => $param['approve_employee_ids'],
  2142. 'show_real_name' => $param['show_real_name']
  2143. ]);
  2144. //消息通知
  2145. if ($param['state'] == 1){
  2146. $old_select = array_filter(explode(',', $exmaPaperAssessment));
  2147. $new_select = array_filter(explode(',', $param['select']));
  2148. $diff_select = array_diff($new_select, $old_select);
  2149. foreach($diff_select as $eid) {
  2150. event(new Msg($eid, '有新的试卷指派给您!', 'assignPaper', $param['paper_id']));
  2151. }
  2152. }
  2153. Db::commit();
  2154. return json(['code' => 0, 'msg' => '考卷发布成功']);
  2155. } catch (\Exception $e) {
  2156. Db::rollback();
  2157. return json(['code' => 1, 'msg' => '考卷发布失败']);
  2158. }
  2159. }
  2160. }