page.question.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. var question = function (entry) {
  2. entry = entry || {};
  3. var rightAnswerArray = new Array();
  4. var WordSerial = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
  5. this.test_question_Id = entry.test_question_Id || 0;
  6. this.base_question_type_id = entry.base_question_type_id;
  7. this.content = entry.content;
  8. this.question_file_id = entry.question_file_id;
  9. this.answers = entry.answers;
  10. this.note = entry.note || "";
  11. this.test_question_libary_id = entry.test_question_libary_id;
  12. this.score = entry.score;
  13. this.difficulty_degree = entry.difficulty_degree;
  14. this.is_vaild = entry.is_vaild;
  15. this.libaryName = entry.libaryName;
  16. this.question_provid_answer = entry.question_provid_answer;
  17. this.TypeName = entry.TypeName;
  18. this.getEntry = function () {
  19. this.base_question_type_id = $("[id$=QuestionType]").combobox("getValue");
  20. this.content = $('[id$=content]').val();
  21. this.question_file_id = $('#questionUpload').next('#iptSingleFile').val();
  22. if (this.question_file_id == "null" || this.question_file_id == "") this.question_file_id = 0;
  23. this.note = $('[id$=note]').val();
  24. this.test_question_libary_id = $("[id$=txt_libary]").val();
  25. this.score = $('[id$=score]').val();
  26. this.difficulty_degree = $("[id$=difficulty_degree]").combobox("getValue");
  27. this.is_vaild = $("input[id*='is_vaild']:checked").val();
  28. this.test_question_Id = $("[id$=test_question_Id]").val();
  29. var questionType = $("[id$=QuestionType]").combobox("getText");
  30. this.question_provid_answer = getQuestionOption(questionType);
  31. this.answers = rightAnswerArray.join("|");
  32. }
  33. this.setEntry = function () {
  34. $("[id$=QuestionType]").combobox("setValue", this.base_question_type_id);
  35. $("[id$=QuestionType]").combobox("setText", this.TypeName);
  36. $('[id$=content]').val(this.content);
  37. $('[id$=questionUpload]').next('#iptSingleFile').val(this.question_file_id);
  38. $('[id$=note]').val(this.note);
  39. $("[id$=txt_libary]").val(this.test_question_libary_id);
  40. $('[id$=txtScore]').val(this.score);
  41. $("[id$=difficulty_degree]").combobox("setValue", this.difficulty_degree);
  42. $("input[id*='is_vaild'][value=" + this.is_vaild + "]").attr("checked", true);
  43. $('[id$=lblLibaryName]').html(this.libaryName);
  44. rightAnswerArray = this.answers.split(',');
  45. showOptions(this.question_provid_answer);
  46. createImg(this.question_file_id, $('[id$=questionUpload]'));
  47. }
  48. this.validateEntry = function () {
  49. $(".errorInput").removeClass("errorInput");
  50. if (isNull(this.test_question_libary_id)) {
  51. alert("请选择题库");
  52. $('[id$=ddlLibary]').addClass("errorInput");
  53. return false;
  54. }
  55. if (isNull(this.base_question_type_id)) {
  56. alert("请选择试题类型");
  57. $('[id$=ddlQuestionType]').addClass("errorInput");
  58. return false;
  59. }
  60. if (isNull(this.content)) {
  61. alert("题干不能为空");
  62. $('[id$=txtContent]').addClass("errorInput");
  63. return false;
  64. }
  65. if (isNull(this.difficulty_degree)) {
  66. alert("请选择难度系数");
  67. $('[id$=ddlDifficulty]').addClass("errorInput");
  68. return false;
  69. }
  70. if (isNull(this.score)) {
  71. alert("缺省分数不能为空");
  72. $('[id$=txtScore]').addClass("errorInput");
  73. return false;
  74. }
  75. var questionType = $("[id$=QuestionType]").combobox("getText");
  76. if (questionType == "单选题" || questionType == "多选题") {
  77. if (this.question_provid_answer.length == 0) {
  78. alert(questionType + "必须有选项");
  79. return false;
  80. }
  81. if (questionType == "单选题" && rightAnswerArray.length != 1) {
  82. alert(questionType + "只能有一个正确答案");
  83. return false;
  84. }
  85. if (questionType == "多选题" && rightAnswerArray.length == 0) {
  86. alert(questionType + "必须设置正确答案");
  87. return false;
  88. }
  89. }
  90. if (!checkOptionIsNull(this.question_provid_answer)) {
  91. alert("选项内容不能为空");
  92. return false;
  93. }
  94. return true;
  95. }
  96. function checkOptionIsNull(providAnswer) {
  97. var isFlag = true;
  98. $(providAnswer).each(function (i, ans) {
  99. if (isNull(ans.answer_name)) {
  100. isFlag = false;
  101. return false;
  102. }
  103. });
  104. return isFlag;
  105. }
  106. function showOptions(providAnswer) {
  107. $('.trOption').find('div').hide();
  108. var questionType = $("[id$=QuestionType]").combobox("getText");
  109. if (questionType == "单选题") {
  110. return showSingleOption(providAnswer);
  111. } else if (questionType == "多选题") {
  112. return showMultipleOption(providAnswer);
  113. } else if (questionType == "填空题") {
  114. return showFillOption(providAnswer);
  115. } else if (questionType == "是非题") {
  116. return showJudgeOption(providAnswer);
  117. } else if (questionType == "简答题") {
  118. return showShortOption(providAnswer);
  119. }
  120. }
  121. function showSingleOption(providAnswer) {
  122. var reg1 = /^.{2}/;
  123. $(providAnswer).each(function (i, ans) {
  124. var tpl = $('#optionTpl').find('#trSingleTpl').clone();
  125. tpl.find("[name$=txtContext]").val(ans.answer_name.replace(reg1, ""));
  126. tpl.find("[id$=iptAnswerID]").val(ans.provid_answer_id);
  127. tpl.find('[id$=iptSingleFile]').val(ans.file_id);
  128. createImg(ans.file_id, tpl.find('[id$=singleUpload]'));
  129. var ckbRight;
  130. if (jQuery.inArray(ans.provid_answer_id.toString(), rightAnswerArray) > -1) {
  131. ckbRight = tpl.find("[id$=rdIsRight]");
  132. }
  133. tpl.insertBefore($('[id$=trSingle]').last());
  134. $(ckbRight).attr("checked", true); //兼容IE6啊
  135. });
  136. $('#divSingle').show();
  137. }
  138. function showMultipleOption(providAnswer) {
  139. var reg1 = /^.{2}/;
  140. $(providAnswer).each(function (i, ans) {
  141. var tpl = $('#optionTpl').find('#trMultipleTpl').clone();
  142. tpl.find("[name$=txtContext]").val(ans.answer_name.replace(reg1, ""));
  143. tpl.find("[id$=iptAnswerID]").val(ans.provid_answer_id);
  144. tpl.find('[id$=iptSingleFile]').val(ans.file_id);
  145. createImg(ans.file_id, tpl.find('[id$=singleUpload]'));
  146. var ckbRight;
  147. if (jQuery.inArray(ans.provid_answer_id.toString(), rightAnswerArray) > -1) {
  148. ckbRight = tpl.find("[id$=ckbIsRight]");
  149. }
  150. tpl.insertBefore($('[id$=trMultiple]').last());
  151. $(ckbRight).attr("checked", true); //兼容IE6啊
  152. });
  153. $('#divMultiple').show();
  154. }
  155. function showFillOption(providAnswer) {
  156. var reg1 = /^.{2}/;
  157. $(providAnswer).each(function (i, ans) {
  158. var tpl = $('#optionTpl').find('#trFillTpl').clone();
  159. tpl.find("[name$=txtContext]").val(ans.answer_name);
  160. tpl.find("[id$=iptAnswerID]").val(ans.provid_answer_id);
  161. tpl.find('[id$=iptSingleFile]').val(ans.file_id);
  162. createImg(ans.file_id, tpl.find('[id$=fillUpload]'));
  163. tpl.insertBefore($('[id$=trFill]').last());
  164. });
  165. $('#divFill').show();
  166. }
  167. function showJudgeOption(providAnswer) {
  168. $(providAnswer).each(function (i, ans) {
  169. if (jQuery.inArray(ans.provid_answer_id.toString(), rightAnswerArray) > -1) {
  170. $("input[name='rdJudgeOption'][value=" + ans.answer_name + "]").attr("checked", true);
  171. }
  172. });
  173. $('#divJudge').show();
  174. }
  175. function showShortOption(providAnswer) {
  176. $(providAnswer).each(function (i, ans) {
  177. $('#divShort').find("[name='txtShortAnswer']").val(ans.answer_name);
  178. });
  179. $('#divShort').show();
  180. }
  181. function getQuestionOption(questionType) {
  182. if (questionType == "单选题") {
  183. return SingleOption();
  184. } else if (questionType == "多选题") {
  185. return MultipleOption();
  186. } else if (questionType == "填空题") {
  187. return FillOption();
  188. } else if (questionType == "是非题") {
  189. return JudgeOption();
  190. } else if (questionType == "简答题") {
  191. return ShortOption();
  192. }
  193. }
  194. function SingleOption() {
  195. var questionOptions = new Array();
  196. $("#tbSingle").find("tr").filter(function () {
  197. return $(this).find('[name$=txtContext]').length > 0;
  198. }).each(function (i, j) {
  199. var item = $(j);
  200. var answer_name = item.find('[name$=txtContext]').val();
  201. if (answer_name != undefined && answer_name != "") {
  202. answer_name = WordSerial[i] + "、" + answer_name;
  203. }
  204. var isRight = item.find('#rdIsRight').attr('checked');
  205. var iptAnswerID = item.find("[id='iptAnswerID']").val() || 0;
  206. if (isRight) {
  207. rightAnswerArray.push(answer_name);
  208. }
  209. var file_id = item.find('#iptSingleFile').val() || null;
  210. questionOptions.push({ provid_answer_id: iptAnswerID, answer_name: answer_name, isRight: isRight, file_id: file_id });
  211. });
  212. return questionOptions;
  213. }
  214. function MultipleOption() {
  215. var questionOptions = new Array();
  216. $("#tbMultiple").find("tr").filter(function () {
  217. return $(this).find('[name$=txtContext]').length > 0;
  218. }).each(function (i, j) {
  219. var item = $(j);
  220. var answer_name = item.find('[name$=txtContext]').val();
  221. if (answer_name != undefined && answer_name != "") {
  222. answer_name = WordSerial[i] + "、" + answer_name;
  223. }
  224. var isRight = item.find('#ckbIsRight').attr('checked');
  225. var iptAnswerID = item.find("[id='iptAnswerID']").val() || 0;
  226. if (isRight) {
  227. rightAnswerArray.push(answer_name);
  228. }
  229. var file_id = item.find('#iptSingleFile').val() || null;
  230. questionOptions.push({ provid_answer_id: iptAnswerID, answer_name: answer_name, isRight: isRight, file_id: file_id });
  231. });
  232. return questionOptions;
  233. }
  234. function FillOption() {
  235. var questionOptions = new Array();
  236. $("#tbFill").find("tr").filter(function () { return $(this).find('[name$=txtContext]').length > 0; }).each(function (i, j) {
  237. var item = $(j);
  238. var answer_name = item.find('[name$=txtContext]').val();
  239. var isRight = item.find('#ckbIsRight').attr('checked');
  240. var file_id = item.find('#iptSingleFile').val() || null;
  241. var iptAnswerID = item.find("[id='iptAnswerID']").val() || 0;
  242. rightAnswerArray.push(answer_name);
  243. questionOptions.push({ provid_answer_id: iptAnswerID, answer_name: answer_name, isRight: true, file_id: file_id });
  244. });
  245. return questionOptions;
  246. }
  247. function JudgeOption() {
  248. var questionOptions = new Array();
  249. var rightAnswer = $('#divJudge').find("input[name='rdJudgeOption']:checked").val();
  250. questionOptions.push({ answer_name: "正确", isRight: rightAnswer == "正确" });
  251. questionOptions.push({ answer_name: "错误", isRight: rightAnswer == "错误" });
  252. rightAnswerArray.push(rightAnswer);
  253. return questionOptions;
  254. }
  255. function ShortOption() {
  256. var questionOptions = new Array();
  257. var answer = $('#divShort').find("[name='txtShortAnswer']").val();
  258. var iptAnswerID = $('#divShort').find("[id='iptAnswerID']").val() || 0;
  259. rightAnswerArray.push(answer);
  260. questionOptions.push({ provid_answer_id: iptAnswerID, answer_name: answer, isRight: true });
  261. return questionOptions;
  262. }
  263. }
  264. $(function () {
  265. if (typeof queLogic == "undefined")
  266. showQuestionOption();
  267. $('[id$=ddlQuestionType]').change(function () {
  268. showQuestionOption();
  269. })
  270. $('[id$=btnSave]').click(function () {
  271. var queLogic = new question();
  272. queLogic.getEntry();
  273. if (!queLogic.validateEntry()) {
  274. return false;
  275. }
  276. var inventoryJSONTextAgain = JSON.stringify(queLogic, null, 3);
  277. $('#question_provid_answer').val(JSON.stringify(queLogic.question_provid_answer))
  278. $('[id$=ipt_entry]').val(inventoryJSONTextAgain);
  279. $(document.forms[0]).submit();
  280. })
  281. $("textarea[maxlength]").bind('input propertychange', function () {
  282. var maxLength = $(this).attr('maxlength');
  283. if ($(this).val().length > maxLength) {
  284. $(this).val($(this).val().substring(0, maxLength));
  285. alert("限字数为300");
  286. }
  287. })
  288. });
  289. var uploadSender;
  290. function UploadFileBefore(sender){
  291. uploadSender=sender;
  292. UploadFile();
  293. }
  294. function GetUploadFileMessage(file_id, Url){
  295. $(uploadSender).parent().find('#iptSingleFile').val(file_id);
  296. $(uploadSender).parent().find('img').next("a").remove();
  297. $(uploadSender).parent().find('img').remove();
  298. createImg(file_id, uploadSender);
  299. $(uploadSender).parent().find('.singleUpload').hide();
  300. }
  301. function createImg(file_id,sender){
  302. if(isNull(file_id)||isNull(sender))
  303. return;
  304. $(sender).parent().find('.singleUpload').hide();
  305. $(sender).before("<div style='text-align:center'><img src='../../ajaxmethod/picture.ashx?q=" + file_id + "' width='100' height='100'/><br/><a style='cursor: pointer;' onclick='deleteImg(this)'>删除</a></div>");
  306. //$(sender).before("<img src='../../ajaxmethod/picture.ashx?q=" + file_id + "' style='margin:0 auto;text-align:center;' width='100' height='100'/><br/><a style='cursor: pointer;' onclick='deleteImg(this)'>删除</a>");
  307. }
  308. function deleteImg(center) {
  309. $(center).parent().find('#iptSingleFile').val("");
  310. $(center).parent().find('img').remove();
  311. $(center).parent().find('br').remove();
  312. $(center).parent().parent().find('.singleUpload').show();
  313. $(center).remove();
  314. }
  315. function isNull(value) {
  316. return value == undefined || value == null || value == "";
  317. }
  318. function AddSingleRowHandler(){
  319. AddSingleRow('trSingle');
  320. }
  321. function AddFillRowHandler(){
  322. AddSingleRow('trFill');
  323. }
  324. function AddMultipleRowHandler() {
  325. AddSingleRow('trMultiple');
  326. }
  327. function AddSingleRow(target){
  328. $('#optionTpl').find('#'+target+'Tpl').clone().insertBefore($('[id$='+target+']').last());
  329. }
  330. function delRow(sender){
  331. $(sender).parent().parent().remove();
  332. }
  333. function showQuestionOption(index) {
  334. if (index == undefined)
  335. return;
  336. $('.trOption').find('div').hide();
  337. var data = $("[id$=QuestionType]").combobox("getData");
  338. var selQuestionType = data[index-1].Name;
  339. if (selQuestionType == "单选题" ) {
  340. var optionCount = $('#divSingle').find('[id=trSingleTpl]').length;
  341. for (var i = 0; i < 4 - optionCount; i++) {
  342. AddSingleRow('trSingle');
  343. }
  344. $('#divSingle').show();
  345. $('#spAnswerTip').show();
  346. } else if (selQuestionType == "多选题") {
  347. var optionCount = $('#divMultiple').find('[id=trMultipleTpl]').length;
  348. for (var i = 0; i < 4 - optionCount; i++) {
  349. AddSingleRow('trMultiple');
  350. }
  351. $('#divMultiple').show();
  352. $('#spAnswerTip').show();
  353. } else if (selQuestionType == "填空题") {
  354. if ($('#divFill').find('#trFillTpl').length == 0)
  355. AddSingleRow('trFill');
  356. $('#divFill').show();
  357. $('#spAnswerTip').show();
  358. } else if (selQuestionType == "是非题") {
  359. $('#divJudge').show();
  360. $('#spAnswerTip').hide();
  361. } else if (selQuestionType == "简答题") {
  362. $('#divShort').show();
  363. $('#spAnswerTip').hide();
  364. }
  365. }