paper.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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.apisu.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. apisu: {
  241. bindevent: function () {
  242. Form.api.bindevent($("form[role=form]"), null, null, function () {
  243. if ($("#treeview").length > 0) {
  244. var r = $("#treeview").jstree("get_all_checked");
  245. $("input[name='row[user_rule]']").val(r.join(','));
  246. }
  247. return true;
  248. });
  249. //渲染权限节点树
  250. //销毁已有的节点树
  251. $("#treeview").jstree("destroy");
  252. Controller.apisu.rendertree(nodeData);
  253. //全选和展开
  254. $(document).on("click", "#checkall", function () {
  255. $("#treeview").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
  256. });
  257. $(document).on("click", "#expandall", function () {
  258. $("#treeview").jstree($(this).prop("checked") ? "open_all" : "close_all");
  259. });
  260. $("select[name='row[pid]']").trigger("change");
  261. },
  262. rendertree: function (content) {
  263. $("#treeview")
  264. .on('redraw.jstree', function (e) {
  265. $(".layer-footer").attr("domrefresh", Math.random());
  266. })
  267. .jstree({
  268. "themes": {"stripes": true},
  269. "checkbox": {
  270. "keep_selected_style": false,
  271. },
  272. "types": {
  273. "root": {
  274. "icon": "fa fa-folder-open",
  275. },
  276. "menu": {
  277. "icon": "fa fa-folder-open",
  278. },
  279. "file": {
  280. "icon": "fa fa-file-o",
  281. }
  282. },
  283. "plugins": ["checkbox", "types"],
  284. "core": {
  285. 'check_callback': true,
  286. "data": content
  287. }
  288. });
  289. },
  290. },
  291. api: {
  292. bindevent: function () {
  293. Form.api.bindevent($("form[role=form]"), function () {
  294. }, function () {
  295. }, function () {
  296. let mode = $('input[name="row[mode]"]:checked').val()
  297. console.log('submit mode', mode)
  298. if (mode === 'FIX') {
  299. $('#valid').click()
  300. return valid_result;
  301. } else {
  302. let configs = $('#c-configs').val()
  303. console.log('configs', configs)
  304. if (!configs) {
  305. Layer.alert('请配置试卷出题规则')
  306. return false
  307. }
  308. let quantity = $('.span_quantity').text()
  309. if (quantity !== $('#c-quantity').val()) {
  310. Layer.alert('试卷出题规则的选取题数与试卷题目数量不一致')
  311. return false
  312. }
  313. let total_score = $('.span_total_score').text()
  314. if (total_score !== $('#c-total_score').val()) {
  315. Layer.alert('试卷出题规则的总分与试卷总分不一致')
  316. return false
  317. }
  318. let limit_time_hour = $('#c-limit_time_hour').val()
  319. let limit_time_minute = $('#c-limit_time_minute').val()
  320. let limit_time = (limit_time_hour ? limit_time_hour : 0) * 3600 + (limit_time_minute ? limit_time_minute : 0) * 60
  321. $('#c-limit_time').val(limit_time)
  322. }
  323. return true
  324. });
  325. },
  326. // 选题模式设置
  327. bindConfigs: function () {
  328. // 选题模式弹窗
  329. $('.btn-configs').click(function () {
  330. let quantity = $('#c-quantity').val()
  331. let total_score = $('#c-total_score').val()
  332. if (!quantity || !total_score) {
  333. Layer.msg('请先设置题目数量和试卷总分')
  334. return false
  335. }
  336. config_dialog = Layer.open({
  337. type: 1,
  338. shade: false,
  339. title: '随机选题配置',
  340. area: ['600px', '600px'],
  341. zIndex: 1,
  342. content: $('#configsDialog'),
  343. cancel: function () {
  344. }
  345. })
  346. })
  347. // 选题库获取题数
  348. $(document).on("change", "#config-cate_id", function () {
  349. Controller.api.getCountScore()
  350. })
  351. // 选取题更改
  352. $('.input_count').change(function () {
  353. let type = $(this).data('type')
  354. let count = $(this).val()
  355. let score = $('.input_' + type + '_score').val()
  356. Controller.api.calcCountScore(type, count, score)
  357. })
  358. // 每题分数更改
  359. $('.input_score').change(function () {
  360. let type = $(this).data('type')
  361. let count = Controller.api.formatVal($('.input_' + type).val())
  362. let score = Controller.api.formatVal($(this).val())
  363. Controller.api.calcCountScore(type, count, score)
  364. })
  365. // 分难度更改
  366. $('.checkbox_difficulty').change(function () {
  367. let type = $(this).data('type')
  368. let value = $(this).is(':checked')
  369. let ul = $('.ul_' + type + '_difficulty')
  370. let input_count = Controller.api.formatVal($('.input_' + type))
  371. let input_score = Controller.api.formatVal($('.input_' + type + '_score'))
  372. let span_score = Controller.api.formatVal($('.span_' + type + '_score'))
  373. if (value) {
  374. ul.removeClass('hide').show()
  375. input_count.attr('disabled', 'disabled')
  376. input_score.attr('disabled', 'disabled')
  377. span_score.hide()
  378. // 触发计算
  379. $(ul.find('.input_count')[0]).trigger('change')
  380. } else {
  381. ul.addClass('hide').hide()
  382. input_count.removeAttr('disabled', 'disabled')
  383. input_score.removeAttr('disabled', 'disabled')
  384. span_score.show()
  385. }
  386. // $('.input_count').trigger('change')
  387. })
  388. // 保存选题配置 TODO 弃用
  389. $('#btnSaveConfig').click(function () {
  390. Controller.api.saveConfig()
  391. })
  392. },
  393. // 保存选题配置
  394. saveConfig() {
  395. let configs = {
  396. cate_ids: $('#config-cate_id').val(),
  397. all: {},
  398. judge: {
  399. difficulty: {}
  400. },
  401. single: {
  402. difficulty: {}
  403. },
  404. multi: {
  405. difficulty: {}
  406. },
  407. fill: {
  408. difficulty: {}
  409. },
  410. short: {
  411. difficulty: {}
  412. },
  413. material: {
  414. difficulty: {}
  415. },
  416. }
  417. // 总
  418. $('#divAll').find('span').each(function (index, ele) {
  419. let key = $(ele).attr('class').replace('span_', '')
  420. configs.all[key] = parseInt(Controller.api.formatVal($(ele).text()))
  421. })
  422. // 题型配置
  423. $('.input_kind_count').each(function (index, ele) {
  424. let type = $(ele).data('type')
  425. configs[type]['count'] = parseInt(Controller.api.formatVal($(ele).val()))
  426. configs[type]['score'] = parseInt(Controller.api.formatVal($(ele).next('span').find('.input_score').val()))
  427. configs[type]['total_score'] = configs[type]['count'] * configs[type]['score']
  428. configs[type]['use_difficulty'] = $(ele).parent().find('.checkbox_difficulty').is(':checked')
  429. })
  430. // 难度配置
  431. $('.ul_difficulty').find('.input_count').each(function (index, ele) {
  432. let type_key = $(ele).data('type').split('_')
  433. let type = type_key[0]
  434. let key = type_key[1]
  435. configs[type].difficulty[key] = {
  436. count: parseInt(Controller.api.formatVal($(ele).val())),
  437. score: parseInt(Controller.api.formatVal($(ele).next('.input_score').val()))
  438. }
  439. configs[type].difficulty[key]['total_score'] = configs[type].difficulty[key].count * configs[type].difficulty[key].score
  440. })
  441. console.log('configs', configs)
  442. $('#c-configs').val(JSON.stringify(configs))
  443. Layer.close(config_dialog)
  444. },
  445. // 根据题库设置题数、分数
  446. getCountScore() {
  447. let cate_ids = $('#config-cate_id').val()
  448. if (cate_ids) {
  449. configs_count = {}
  450. Fast.api.ajax({
  451. url: 'exam/question/getCount',
  452. type: 'post',
  453. data: {cate_ids: cate_ids}
  454. }, function (data, ret) {
  455. configs_count = data
  456. if (!configs_count) {
  457. return false
  458. }
  459. let quantity = $('#c-quantity').val()
  460. let total_score = $('#c-total_score').val()
  461. let sing_score = parseInt(total_score / quantity)
  462. for (let key of Object.keys(configs_count)) {
  463. let value = configs_count[key]
  464. $('.span_' + key + '_total').text(value)
  465. $('.input_' + key).attr('max', value)
  466. $('.input_' + key + '_score').attr('max', sing_score)//.val(sing_score)
  467. }
  468. return false
  469. });
  470. }
  471. },
  472. calcCountScore(type, count, score) {
  473. // 计算题型总分
  474. $('.span_' + type + '_total_score').text(count * score)
  475. // 计算分难度的总分
  476. if (type.indexOf('_') > 0) {
  477. let parent_type = type.split('_')[0]
  478. let ul = $('.ul_' + parent_type + '_difficulty')
  479. let parent_count_input = $('.input_' + parent_type)
  480. let parent_score_span = $('.span_' + parent_type + '_total_score')
  481. let sum_count = 0
  482. let sum_score = 0
  483. ul.find('.input_count').each(function (index, ele) {
  484. let single_score = $(ele).next('input').val()
  485. sum_count += parseInt($(ele).val())
  486. sum_score += parseInt($(ele).val()) * single_score
  487. })
  488. parent_count_input.val(sum_count)
  489. parent_score_span.text(sum_score)
  490. }
  491. // 总题数
  492. let kind_total_count = 0
  493. $('.input_kind_count').each(function (index, ele) {
  494. kind_total_count += parseInt($(ele).val())
  495. })
  496. $('.span_quantity').text(kind_total_count)
  497. // 计算总分
  498. let kind_total_score = 0
  499. $('.span_kind_total_score').each(function (index, ele) {
  500. kind_total_score += parseInt($(ele).text())
  501. })
  502. $('.span_total_score').text(kind_total_score)
  503. // 保存配置
  504. Controller.api.saveConfig()
  505. },
  506. // 根据配置设置题数渲染数据
  507. renderCountScore() {
  508. // 考试时间渲染
  509. const limit_time = $('#c-limit_time').val() ? $('#c-limit_time').val() : 0
  510. if (limit_time) {
  511. let hour = Math.floor(limit_time / 3600)
  512. let minute = Math.floor(limit_time / 60) % 60
  513. $('#c-limit_time_hour').val(hour)
  514. $('#c-limit_time_minute').val(minute)
  515. }
  516. let configs_val = $('#c-configs').val()
  517. console.log('configs_val', configs_val)
  518. const config_json = configs_val && typeof configs_val === 'string' ? JSON.parse(configs_val) : {}
  519. if (config_json && config_json.cate_ids) {
  520. for (const key in config_json) {
  521. if (key === 'all') {
  522. $('.span_quantity').text(config_json.all.quantity)
  523. $('.span_total_score').text(config_json.all.total_score)
  524. } else {
  525. const kind_config = config_json[key]
  526. $('.input_count[data-type="' + key + '"]').val(kind_config['count'])
  527. $('.input_score[data-type="' + key + '"]').val(kind_config['score'])
  528. $('.span_' + key + '_total_score').text(kind_config['total_score'])
  529. if (kind_config['use_difficulty'] === true) {
  530. $('.checkbox_' + key).click()
  531. const difficulty_config = kind_config['difficulty']
  532. for (const k in difficulty_config) {
  533. let difficulty_count = difficulty_config[k].count ? difficulty_config[k].count : 0
  534. let difficulty_score = difficulty_config[k].score ? difficulty_config[k].score : 0
  535. let difficulty_total_score = difficulty_count * difficulty_score
  536. $('.input_' + key + '_' + k).val(difficulty_count)
  537. $('.input_' + key + '_' + k + '_score').val(difficulty_score)
  538. $('.span_' + key + '_' + k + '_total_score').text(difficulty_total_score)
  539. }
  540. }
  541. }
  542. }
  543. // 触发计算
  544. // $($('.ul_judge_difficulty').find('.input_count')[0]).trigger('change')
  545. }
  546. },
  547. // 限定时间事件,59分转小时
  548. bindTime() {
  549. $('#c-limit_time_minute').change(function (ele) {
  550. let minute = $(this).val()
  551. if (minute >= 60) {
  552. $('#c-limit_time_minute').val(0)
  553. let hour_ctrl = $('#c-limit_time_hour')
  554. hour_ctrl.val(parseInt(hour_ctrl.val()) + 1)
  555. }
  556. })
  557. },
  558. // 绑定固定选题配置按钮事件
  559. bindFixButton() {
  560. $('.btn-fix-configs').click(function () {
  561. Fast.api.open('exam/question/select', '选择试题', {
  562. area: ['90%', '90%'],
  563. callback: function (data) {
  564. if (!data) {
  565. return
  566. }
  567. }
  568. })
  569. });
  570. },
  571. // 秒数转时分秒格式
  572. formatDuring (second) {
  573. var hours = parseInt((second % (60 * 60 * 24)) / (60 * 60));
  574. var minutes = parseInt((second % (60 * 60)) / (60));
  575. var seconds = (second % (60));
  576. return hours + "时 " + minutes + "分 " + seconds + "秒";
  577. },
  578. // 获取数字值
  579. formatVal (val) {
  580. return isNaN(val) ? 0 : val
  581. },
  582. }
  583. };
  584. return Controller;
  585. });