paper.js 28 KB

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