paper.js 28 KB

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