paper.js 23 KB

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