paper.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. let configs_count = {}
  2. let config_dialog = 0
  3. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function ($, undefined, Backend, Table, Form) {
  4. //读取选中的条目
  5. $.jstree.core.prototype.get_all_checked = function (full) {
  6. var obj = this.get_selected(), i, j;
  7. for (i = 0, j = obj.length; i < j; i++) {
  8. obj = obj.concat(this.get_node(obj[i]).parents);
  9. }
  10. obj = $.grep(obj, function (v, i, a) {
  11. return v != '#';
  12. });
  13. obj = obj.filter(function (itm, i, a) {
  14. return i == a.indexOf(itm);
  15. });
  16. return full ? $.map(obj, $.proxy(function (i) {
  17. return this.get_node(i);
  18. }, this)) : obj;
  19. };
  20. var Controller = {
  21. index: function () {
  22. // 初始化表格参数配置
  23. Table.api.init({
  24. extend: {
  25. index_url: 'exam/paper/index' + location.search,
  26. add_url: 'exam/paper/add',
  27. edit_url: 'exam/paper/edit',
  28. del_url: 'exam/paper/del',
  29. multi_url: 'exam/paper/multi',
  30. import_url: 'exam/paper/import',
  31. table: 'exam_paper',
  32. }
  33. });
  34. var table = $("#table");
  35. //在普通搜索渲染后
  36. table.on('post-common-search.bs.table', function (event, table) {
  37. let form = $("form", table.$commonsearch);
  38. $("input[name='cate_id']", form).addClass("selectpage").data("source", "exam/cate/selectpage").data("params", {"custom[kind]": "QUESTION","isTree":true}).data("orderBy", "sort desc");
  39. // $("input[name='exam_type_id']", form).addClass("selectpage").data("source", "exam_type/index").data("orderBy", "sort desc");
  40. Form.events.cxselect(form);
  41. Form.events.selectpage(form);
  42. });
  43. //当内容渲染完成给编辑按钮添加`data-area`属性,点击列表编辑按钮时全屏
  44. table.on('post-body.bs.table', function (e, settings, json, xhr) {
  45. $(".btn-add").data("area", ["100%", "100%"]);
  46. $(".btn-editone").data("area", ["100%", "100%"]);
  47. });
  48. // 初始化表格
  49. table.bootstrapTable({
  50. url: $.fn.bootstrapTable.defaults.extend.index_url,
  51. pk: 'id',
  52. sortName: 'id',
  53. search: false,
  54. showExport: false,//隐藏导出
  55. showToggle: false,//隐藏浏览模式
  56. showColumns: false,//隐藏显示隐藏模式
  57. searchFormVisible: true,//默认显示搜索表单
  58. columns: [
  59. [
  60. {checkbox: true},
  61. {field: 'id', title: __('Id'), operate: false},
  62. {field: 'cate_id', title: __('Cate_id'), autocomplete: false, visible: false},
  63. {field: 'cate.name', title: __('Cate_id'), operate: false},
  64. {field: 'title', title: __('Title'), autocomplete: false, operate: 'LIKE'},
  65. {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
  66. // {field: 'configs', title: __('Configs'), operate: 'LIKE'},
  67. {field: 'quantity', title: __('Quantity'), operate: false},
  68. {field: 'total_score', title: __('Total_score'), operate: false},
  69. {field: 'pass_score', title: __('Pass_score'), operate: false},
  70. {field: 'limit_count', title: __('Limit_count'), operate: false},
  71. {
  72. field: 'mode',
  73. title: __('Mode'),
  74. searchList: {"RANDOM": __('Random'), "FIX": __('Fix')},
  75. formatter: Table.api.formatter.normal
  76. },
  77. {
  78. field: 'limit_time',
  79. title: __('Limit_time'),
  80. operate: false,
  81. autocomplete: false,
  82. formatter: function (value) {
  83. return Controller.api.formatDuring(value)
  84. }
  85. },
  86. {
  87. field: 'start_time',
  88. title: __('Start_time'),
  89. operate: 'RANGE',
  90. addclass: 'datetimerange',
  91. autocomplete: false,
  92. formatter: Table.api.formatter.datetime
  93. },
  94. {
  95. field: 'end_time',
  96. title: __('End_time'),
  97. operate: 'RANGE',
  98. addclass: 'datetimerange',
  99. autocomplete: false,
  100. formatter: Table.api.formatter.datetime
  101. },
  102. // {field: 'is_only_room', title: __('Is_only_room'), searchList: {"1":__('Yes'),"0":__('No')}, formatter: Table.api.formatter.toggle},
  103. {
  104. field: 'status',
  105. title: __('Status'),
  106. searchList: {"NORMAL": __('Normal'), "HIDDEN": __('Hidden')},
  107. formatter: Table.api.formatter.status
  108. },
  109. {
  110. field: 'createtime',
  111. title: __('Createtime'),
  112. operate: 'RANGE',
  113. addclass: 'datetimerange',
  114. autocomplete: false,
  115. formatter: Table.api.formatter.datetime
  116. },
  117. // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
  118. {
  119. field: 'operate',
  120. title: __('Operate'),
  121. table: table,
  122. buttons:[
  123. {
  124. name:'selectuser',
  125. text:'选择用户',
  126. title:'选择用户',
  127. icon:'fa fa-exclamation-circle',
  128. classname:'btn btn-xs btn-info btn-dialog btn-selectuser',
  129. url:'exam/paper/selectuser/id/{ids}?dialog=1',
  130. target:'_self',
  131. },
  132. {
  133. name:'grade',
  134. text:'成绩',
  135. title:'成绩',
  136. icon:'fa fa-exclamation-circle',
  137. classname:'btn btn-xs btn-info btn-dialog btn-grade',
  138. url:'exam/grade/index/paper_id/{ids}?dialog=1',
  139. target:'_self',
  140. },
  141. {
  142. name:'nograde',
  143. text:'缺考',
  144. title:'缺考',
  145. icon:'fa fa-exclamation-circle',
  146. classname:'btn btn-xs btn-info btn-dialog btn-nograde',
  147. url:'exam/paper/nograde/id/{ids}',
  148. target:'_self',
  149. }
  150. ],
  151. events: Table.api.events.operate,
  152. formatter: Table.api.formatter.operate
  153. }
  154. ]
  155. ],
  156. onLoadSuccess:function(){
  157. // 这里就是数据渲染结束后的回调函数
  158. $(".btn-selectuser").data("area", ['80%','80%']);
  159. $(".btn-grade").data("area", ['90%','90%']);
  160. $(".btn-nograde").data("area", ['90%','90%']);
  161. }
  162. });
  163. // 为表格绑定事件
  164. Table.api.bindevent(table);
  165. },
  166. recyclebin: function () {
  167. // 初始化表格参数配置
  168. Table.api.init({
  169. extend: {
  170. 'dragsort_url': ''
  171. }
  172. });
  173. var table = $("#table");
  174. // 初始化表格
  175. table.bootstrapTable({
  176. url: 'exam/paper/recyclebin' + location.search,
  177. pk: 'id',
  178. sortName: 'id',
  179. columns: [
  180. [
  181. {checkbox: true},
  182. {field: 'id', title: __('Id')},
  183. {field: 'title', title: __('Title'), align: 'left'},
  184. {
  185. field: 'deletetime',
  186. title: __('Deletetime'),
  187. operate: 'RANGE',
  188. addclass: 'datetimerange',
  189. formatter: Table.api.formatter.datetime
  190. },
  191. {
  192. field: 'operate',
  193. width: '130px',
  194. title: __('Operate'),
  195. table: table,
  196. events: Table.api.events.operate,
  197. buttons: [
  198. {
  199. name: 'Restore',
  200. text: __('Restore'),
  201. classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
  202. icon: 'fa fa-rotate-left',
  203. url: 'exam/paper/restore',
  204. refresh: true
  205. },
  206. /*{
  207. name: 'Destroy',
  208. text: __('Destroy'),
  209. classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
  210. icon: 'fa fa-times',
  211. url: 'exam/paper/destroy',
  212. refresh: true
  213. }*/
  214. ],
  215. formatter: Table.api.formatter.operate
  216. }
  217. ]
  218. ]
  219. });
  220. // 为表格绑定事件
  221. Table.api.bindevent(table);
  222. },
  223. add: function () {
  224. Controller.api.bindevent()
  225. Controller.api.bindConfigs()
  226. Controller.api.bindTime()
  227. },
  228. selectuser: function () {
  229. Controller.apisu.bindevent();
  230. },
  231. nograde: function () {
  232. Controller.api.bindevent()
  233. },
  234. edit: function () {
  235. Controller.api.bindevent()
  236. Controller.api.bindConfigs()
  237. Controller.api.getCountScore()
  238. Controller.api.renderCountScore()
  239. Controller.api.bindTime()
  240. },
  241. apisu: {
  242. bindevent: function () {
  243. Form.api.bindevent($("form[role=form]"), null, null, function () {
  244. if ($("#treeview").length > 0) {
  245. var r = $("#treeview").jstree("get_all_checked");
  246. $("input[name='row[user_rule]']").val(r.join(','));
  247. }
  248. return true;
  249. });
  250. //渲染权限节点树
  251. //销毁已有的节点树
  252. $("#treeview").jstree("destroy");
  253. Controller.apisu.rendertree(nodeData);
  254. //全选和展开
  255. $(document).on("click", "#checkall", function () {
  256. $("#treeview").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
  257. });
  258. $(document).on("click", "#expandall", function () {
  259. $("#treeview").jstree($(this).prop("checked") ? "open_all" : "close_all");
  260. });
  261. $("select[name='row[pid]']").trigger("change");
  262. },
  263. rendertree: function (content) {
  264. $("#treeview")
  265. .on('redraw.jstree', function (e) {
  266. $(".layer-footer").attr("domrefresh", Math.random());
  267. })
  268. .jstree({
  269. "themes": {"stripes": true},
  270. "checkbox": {
  271. "keep_selected_style": false,
  272. },
  273. "types": {
  274. "root": {
  275. "icon": "fa fa-folder-open",
  276. },
  277. "menu": {
  278. "icon": "fa fa-folder-open",
  279. },
  280. "file": {
  281. "icon": "fa fa-file-o",
  282. }
  283. },
  284. "plugins": ["checkbox", "types"],
  285. "core": {
  286. 'check_callback': true,
  287. "data": content
  288. }
  289. });
  290. },
  291. },
  292. api: {
  293. bindevent: function () {
  294. Form.api.bindevent($("form[role=form]"), function () {
  295. }, function () {
  296. }, function () {
  297. let mode = $('input[name="row[mode]"]:checked').val()
  298. console.log('submit mode', mode)
  299. if (mode === 'FIX') {
  300. $('#valid').click()
  301. return valid_result;
  302. } else {
  303. let configs = $('#c-configs').val()
  304. console.log('configs', configs)
  305. if (!configs) {
  306. Layer.alert('请配置试卷出题规则')
  307. return false
  308. }
  309. let quantity = $('.span_quantity').text()
  310. if (quantity !== $('#c-quantity').val()) {
  311. Layer.alert('试卷出题规则的选取题数与试卷题目数量不一致')
  312. return false
  313. }
  314. let total_score = $('.span_total_score').text()
  315. if (total_score !== $('#c-total_score').val()) {
  316. Layer.alert('试卷出题规则的总分与试卷总分不一致')
  317. return false
  318. }
  319. let limit_time_hour = $('#c-limit_time_hour').val()
  320. let limit_time_minute = $('#c-limit_time_minute').val()
  321. let limit_time = (limit_time_hour ? limit_time_hour : 0) * 3600 + (limit_time_minute ? limit_time_minute : 0) * 60
  322. $('#c-limit_time').val(limit_time)
  323. }
  324. return true
  325. });
  326. },
  327. // 选题模式设置
  328. bindConfigs: function () {
  329. // 选题模式弹窗
  330. $('.btn-configs').click(function () {
  331. let quantity = $('#c-quantity').val()
  332. let total_score = $('#c-total_score').val()
  333. if (!quantity || !total_score) {
  334. Layer.msg('请先设置题目数量和试卷总分')
  335. return false
  336. }
  337. config_dialog = Layer.open({
  338. type: 1,
  339. shade: false,
  340. title: '随机选题配置',
  341. area: ['600px', '600px'],
  342. zIndex: 1,
  343. content: $('#configsDialog'),
  344. cancel: function () {
  345. }
  346. })
  347. })
  348. // 选题库获取题数
  349. $(document).on("change", "#config-cate_id", function () {
  350. Controller.api.getCountScore()
  351. })
  352. // 选取题更改
  353. $('.input_count').change(function () {
  354. let type = $(this).data('type')
  355. let count = $(this).val()
  356. let score = $('.input_' + type + '_score').val()
  357. Controller.api.calcCountScore(type, count, score)
  358. })
  359. // 每题分数更改
  360. $('.input_score').change(function () {
  361. let type = $(this).data('type')
  362. let count = Controller.api.formatVal($('.input_' + type).val())
  363. let score = Controller.api.formatVal($(this).val())
  364. Controller.api.calcCountScore(type, count, score)
  365. })
  366. // 分难度更改
  367. $('.checkbox_difficulty').change(function () {
  368. let type = $(this).data('type')
  369. let value = $(this).is(':checked')
  370. let ul = $('.ul_' + type + '_difficulty')
  371. let input_count = Controller.api.formatVal($('.input_' + type))
  372. let input_score = Controller.api.formatVal($('.input_' + type + '_score'))
  373. let span_score = Controller.api.formatVal($('.span_' + type + '_score'))
  374. if (value) {
  375. ul.removeClass('hide').show()
  376. input_count.attr('disabled', 'disabled')
  377. input_score.attr('disabled', 'disabled')
  378. span_score.hide()
  379. // 触发计算
  380. $(ul.find('.input_count')[0]).trigger('change')
  381. } else {
  382. ul.addClass('hide').hide()
  383. input_count.removeAttr('disabled', 'disabled')
  384. input_score.removeAttr('disabled', 'disabled')
  385. span_score.show()
  386. }
  387. // $('.input_count').trigger('change')
  388. })
  389. // 保存选题配置 TODO 弃用
  390. $('#btnSaveConfig').click(function () {
  391. Controller.api.saveConfig()
  392. })
  393. },
  394. // 保存选题配置
  395. saveConfig() {
  396. let configs = {
  397. cate_ids: $('#config-cate_id').val(),
  398. all: {},
  399. judge: {
  400. difficulty: {}
  401. },
  402. single: {
  403. difficulty: {}
  404. },
  405. multi: {
  406. difficulty: {}
  407. },
  408. fill: {
  409. difficulty: {}
  410. },
  411. short: {
  412. difficulty: {}
  413. },
  414. material: {
  415. difficulty: {}
  416. },
  417. }
  418. // 总
  419. $('#divAll').find('span').each(function (index, ele) {
  420. let key = $(ele).attr('class').replace('span_', '')
  421. configs.all[key] = parseInt(Controller.api.formatVal($(ele).text()))
  422. })
  423. // 题型配置
  424. $('.input_kind_count').each(function (index, ele) {
  425. let type = $(ele).data('type')
  426. configs[type]['count'] = parseInt(Controller.api.formatVal($(ele).val()))
  427. configs[type]['score'] = parseInt(Controller.api.formatVal($(ele).next('span').find('.input_score').val()))
  428. configs[type]['total_score'] = configs[type]['count'] * configs[type]['score']
  429. configs[type]['use_difficulty'] = $(ele).parent().find('.checkbox_difficulty').is(':checked')
  430. })
  431. // 难度配置
  432. $('.ul_difficulty').find('.input_count').each(function (index, ele) {
  433. let type_key = $(ele).data('type').split('_')
  434. let type = type_key[0]
  435. let key = type_key[1]
  436. configs[type].difficulty[key] = {
  437. count: parseInt(Controller.api.formatVal($(ele).val())),
  438. score: parseInt(Controller.api.formatVal($(ele).next('.input_score').val()))
  439. }
  440. configs[type].difficulty[key]['total_score'] = configs[type].difficulty[key].count * configs[type].difficulty[key].score
  441. })
  442. console.log('configs', configs)
  443. $('#c-configs').val(JSON.stringify(configs))
  444. Layer.close(config_dialog)
  445. },
  446. // 根据题库设置题数、分数
  447. getCountScore() {
  448. let cate_ids = $('#config-cate_id').val()
  449. if (cate_ids) {
  450. configs_count = {}
  451. Fast.api.ajax({
  452. url: 'exam/question/getCount',
  453. type: 'post',
  454. data: {cate_ids: cate_ids}
  455. }, function (data, ret) {
  456. configs_count = data
  457. if (!configs_count) {
  458. return false
  459. }
  460. let quantity = $('#c-quantity').val()
  461. let total_score = $('#c-total_score').val()
  462. let sing_score = parseInt(total_score / quantity)
  463. for (let key of Object.keys(configs_count)) {
  464. let value = configs_count[key]
  465. $('.span_' + key + '_total').text(value)
  466. $('.input_' + key).attr('max', value)
  467. $('.input_' + key + '_score').attr('max', sing_score)//.val(sing_score)
  468. }
  469. return false
  470. });
  471. }
  472. },
  473. calcCountScore(type, count, score) {
  474. // 计算题型总分
  475. $('.span_' + type + '_total_score').text(count * score)
  476. // 计算分难度的总分
  477. if (type.indexOf('_') > 0) {
  478. let parent_type = type.split('_')[0]
  479. let ul = $('.ul_' + parent_type + '_difficulty')
  480. let parent_count_input = $('.input_' + parent_type)
  481. let parent_score_span = $('.span_' + parent_type + '_total_score')
  482. let sum_count = 0
  483. let sum_score = 0
  484. ul.find('.input_count').each(function (index, ele) {
  485. let single_score = $(ele).next('input').val()
  486. sum_count += parseInt($(ele).val())
  487. sum_score += parseInt($(ele).val()) * single_score
  488. })
  489. parent_count_input.val(sum_count)
  490. parent_score_span.text(sum_score)
  491. }
  492. // 总题数
  493. let kind_total_count = 0
  494. $('.input_kind_count').each(function (index, ele) {
  495. kind_total_count += parseInt($(ele).val())
  496. })
  497. $('.span_quantity').text(kind_total_count)
  498. // 计算总分
  499. let kind_total_score = 0
  500. $('.span_kind_total_score').each(function (index, ele) {
  501. kind_total_score += parseInt($(ele).text())
  502. })
  503. $('.span_total_score').text(kind_total_score)
  504. // 保存配置
  505. Controller.api.saveConfig()
  506. },
  507. // 根据配置设置题数渲染数据
  508. renderCountScore() {
  509. // 考试时间渲染
  510. const limit_time = $('#c-limit_time').val() ? $('#c-limit_time').val() : 0
  511. if (limit_time) {
  512. let hour = Math.floor(limit_time / 3600)
  513. let minute = Math.floor(limit_time / 60) % 60
  514. $('#c-limit_time_hour').val(hour)
  515. $('#c-limit_time_minute').val(minute)
  516. }
  517. let configs_val = $('#c-configs').val()
  518. console.log('configs_val', configs_val)
  519. const config_json = configs_val && typeof configs_val === 'string' ? JSON.parse(configs_val) : {}
  520. if (config_json && config_json.cate_ids) {
  521. for (const key in config_json) {
  522. if (key === 'all') {
  523. $('.span_quantity').text(config_json.all.quantity)
  524. $('.span_total_score').text(config_json.all.total_score)
  525. } else {
  526. const kind_config = config_json[key]
  527. $('.input_count[data-type="' + key + '"]').val(kind_config['count'])
  528. $('.input_score[data-type="' + key + '"]').val(kind_config['score'])
  529. $('.span_' + key + '_total_score').text(kind_config['total_score'])
  530. if (kind_config['use_difficulty'] === true) {
  531. $('.checkbox_' + key).click()
  532. const difficulty_config = kind_config['difficulty']
  533. for (const k in difficulty_config) {
  534. let difficulty_count = difficulty_config[k].count ? difficulty_config[k].count : 0
  535. let difficulty_score = difficulty_config[k].score ? difficulty_config[k].score : 0
  536. let difficulty_total_score = difficulty_count * difficulty_score
  537. $('.input_' + key + '_' + k).val(difficulty_count)
  538. $('.input_' + key + '_' + k + '_score').val(difficulty_score)
  539. $('.span_' + key + '_' + k + '_total_score').text(difficulty_total_score)
  540. }
  541. }
  542. }
  543. }
  544. // 触发计算
  545. // $($('.ul_judge_difficulty').find('.input_count')[0]).trigger('change')
  546. }
  547. },
  548. // 限定时间事件,59分转小时
  549. bindTime() {
  550. $('#c-limit_time_minute').change(function (ele) {
  551. let minute = $(this).val()
  552. if (minute >= 60) {
  553. $('#c-limit_time_minute').val(0)
  554. let hour_ctrl = $('#c-limit_time_hour')
  555. hour_ctrl.val(parseInt(hour_ctrl.val()) + 1)
  556. }
  557. })
  558. },
  559. // 绑定固定选题配置按钮事件
  560. bindFixButton() {
  561. $('.btn-fix-configs').click(function () {
  562. Fast.api.open('exam/question/select', '选择试题', {
  563. area: ['90%', '90%'],
  564. callback: function (data) {
  565. if (!data) {
  566. return
  567. }
  568. }
  569. })
  570. });
  571. },
  572. // 秒数转时分秒格式
  573. formatDuring (second) {
  574. var hours = parseInt((second % (60 * 60 * 24)) / (60 * 60));
  575. var minutes = parseInt((second % (60 * 60)) / (60));
  576. var seconds = (second % (60));
  577. return hours + "时 " + minutes + "分 " + seconds + "秒";
  578. },
  579. // 获取数字值
  580. formatVal (val) {
  581. return isNaN(val) ? 0 : val
  582. },
  583. }
  584. };
  585. return Controller;
  586. });