socialforge/public/javascripts/homework.js

566 lines
23 KiB
JavaScript
Raw Normal View History

2016-01-20 16:03:28 +08:00
$(function(){
//提交作业
var bt=baidu.template;
bt.LEFT_DELIMITER='<!';
bt.RIGHT_DELIMITER='!>';
var tested = false;
var valid_form = function() {
var src = $('#program-src').val();
var title = $('#program-title').val();
if (!src) {
2016-05-17 14:12:42 +08:00
var htmlvalue = "</br><div style='width:550px;text-align:center'>请输入正确的代码!</div></br><div style='width:67px; margin:0 auto; text-align:center'><a href='javascript:void(0);' class='Blue-btn' onclick='hideModal()'>确定</a></div>";
pop_up_box(htmlvalue,580,30,50);
2016-01-20 16:03:28 +08:00
return false;
}
if (!title) {
2016-05-17 14:12:42 +08:00
var htmlvalue = "</br><div style='width:550px;text-align:center'>请输入标题!</div></br><div style='width:67px; margin:0 auto; text-align:center'><a href='javascript:void(0);' class='Blue-btn' onclick='hideModal()'>确定</a></div>";
pop_up_box(htmlvalue,580,30,50);
2016-01-20 16:03:28 +08:00
return false;
}
return true;
};
var test_program = function(cb){
$('#test-program-btn').hide();
2016-01-20 16:03:28 +08:00
var homework_id = $('#test-program-btn').attr('data-homework-id');
var student_work_id = $('#test-program-btn').attr('data-student-work-id');
var src = $('#program-src').val();
var title = $('#program-title').val();
var is_test = $('input[name=is_test]').val();
if(!valid_form()){
$('#test-program-btn').show();
2016-01-20 16:03:28 +08:00
return;
}
2016-03-25 14:43:51 +08:00
//先测试一次并返回测试集个数及结果再判断是否需要继续进行测试
2016-03-26 13:44:13 +08:00
var test_post = function(i, testid){
2016-03-25 14:43:51 +08:00
$.post(
'/student_work/program_test_ex',
2016-03-26 13:44:13 +08:00
{homework: homework_id, student_work_id: student_work_id,
src: src, title: title, is_test: is_test,tIndex:i,
testid: testid},
2016-03-25 14:43:51 +08:00
function(data,status){
if (data.status==-4 || data.status==-5 ){
//弹框
var root_path = getRootPath();
var forums_1_path = root_path + "/forums/1"
var htmlvalue = "</br><div style='width:430px;text-align:center;margin:0 auto' >您的程序引发了不知名异常,请在公共贴吧提交您的代码进行意见反馈,我们处理后会立即联系您,谢谢!</div><div style='width:550px;text-align:center'><a target='_Blank' href="+forums_1_path+">点我进入反馈页面</a></div>";
pop_up_box(htmlvalue,580,30,50);
$('#test-program-btn').show();
return;
}
else if (data.status==-3){
2016-05-17 14:12:42 +08:00
var htmlvalue = "</br><div style='width:550px;text-align:center'>对不起,服务器繁忙请稍后再试!</div></br><div style='width:67px; margin:0 auto; text-align:center'><a href='javascript:void(0);' class='Blue-btn' onclick='hideModal()'>确定</a></div>";
pop_up_box(htmlvalue,580,30,50);
$('#test-program-btn').show();
return;
}
2016-03-25 14:43:51 +08:00
var tSeq = data.tseq;
var tCount = data.tcount;
console.log("tSeq="+tSeq);
console.log("tCount="+tCount);
tested = true;
console.log(data);
if(data.index <=0){
data.index = $('.ProResultTop').length+1;
}
var html=bt('t:result-list',data);
$('.ProResult').prepend(html);
if (data.status==0 && is_test != 'true') {
if (typeof cb == 'function') {cb(data);$('#test-program-btn').show(); return;}
2016-05-17 14:12:42 +08:00
var htmlvalue = "</br><div style='width:550px;text-align:center'>答题正确,是否立刻提交?</div></br><div style='width:164px; margin:0 auto; text-align:center'><a href='javascript:void(0);' class='Blue-btn fl' onclick='submit_code()'>确定</a><a href='javascript:void(0);' class='Blue-btn fl' onclick='hideModal()'>取消</a></div><script>function submit_code(){$('.HomeWorkCon form').submit();hideModal();}</script>";
pop_up_box(htmlvalue,580,30,50);
$('#test-program-btn').show();
2016-03-25 14:43:51 +08:00
return;
}
//2 超时 -2 编译错误 测试结束
if (data.status == 2 || data.status == -2 || tSeq >= tCount ){
if (typeof cb == 'function') {cb(data);$('#test-program-btn').show(); return;}
$('#test-program-btn').show();
2016-03-25 14:43:51 +08:00
return;
}
2016-03-26 13:44:13 +08:00
test_post(i+1, data.testid);
2016-03-25 14:43:51 +08:00
}
2016-03-25 15:18:33 +08:00
).fail(function(xhr, status){
if(status == 'timeout'){
2016-05-17 14:12:42 +08:00
var htmlvalue = "</br><div style='width:550px;text-align:center'>您的答案超时了, 请检查代码是否存在死循环的错误!</div></br><div style='width:67px; margin:0 auto; text-align:center'><a href='javascript:void(0);' class='Blue-btn' onclick='hideModal()'>确定</a></div>";
pop_up_box(htmlvalue,580,30,50);
2016-03-25 15:18:33 +08:00
} else {
2016-05-17 14:12:42 +08:00
var htmlvalue = "</br><div style='width:550px;text-align:center'>对不起,服务器繁忙请稍后再试!</div></br><div style='width:67px; margin:0 auto; text-align:center'><a href='javascript:void(0);' class='Blue-btn' onclick='hideModal()'>确定</a></div>";
pop_up_box(htmlvalue,580,30,50);
2016-03-25 15:18:33 +08:00
}
$('#test-program-btn').show();
2016-03-25 17:07:06 +08:00
return;
2016-03-25 15:18:33 +08:00
});
2016-03-25 14:43:51 +08:00
};
2016-03-26 13:44:13 +08:00
test_post(1, 0);
2016-01-20 16:03:28 +08:00
};
$('#test-program-btn').on('click', test_program);
$('#commit-program-work-btn').on('click', function(){
if(!valid_form()){
return;
}
if($('.ProResult .ProResultTop').length<=0){
2016-05-17 14:12:42 +08:00
var htmlvalue = "</br><div style='width:550px;text-align:center'>测试后才能提交,是否立刻测试?</div></br><div style='width:164px; margin:0 auto; text-align:center'><a href='javascript:void(0);' id='code-test-button' class='Blue-btn fl'>确定</a><a href='javascript:void(0);' class='Blue-btn fl' onclick='hideModal()'>取消</a></div>";
pop_up_box(htmlvalue,580,30,50);
$("#code-test-button").on('click',function(){
2016-01-20 16:03:28 +08:00
test_program();
2016-05-17 14:12:42 +08:00
hideModal();
});
2016-01-20 16:03:28 +08:00
return;
}
if (!tested) {
test_program(function(data){
if (data.status!=0) {
2016-05-17 14:12:42 +08:00
var htmlvalue = "</br><div style='width:550px;text-align:center'>测试不通过,是否强制提交?</div></br><div style='width:164px; margin:0 auto; text-align:center'><a href='javascript:void(0);' class='Blue-btn fl' onclick='submit_code()'>确定</a><a href='javascript:void(0);' class='Blue-btn fl' onclick='hideModal()'>取消</a></div><script>function submit_code(){$('.HomeWorkCon form').submit();hideModal();}</script>";
pop_up_box(htmlvalue,580,30,50);
return;
2016-01-20 16:03:28 +08:00
};
$(".HomeWorkCon form").submit();
});
return;
}
$(".HomeWorkCon form").submit();
});
$('form.edit_student_work').on('keydown', '#program-src', function(){
tested = false;
});
//发布作业
$('#program-src').focus(function(){
$(this).css('height', '100px');
});
var datepickerOptions={dateFormat:'yy-mm-dd',firstDay:0,showWeek:true,showOtherMonths:true,selectOtherMonths:true};
$('input.date-input').datepicker(datepickerOptions);
$('a.pic_date').on('click', function(){
$(this).parent().prev().first().focus();
})
2016-01-29 18:17:27 +08:00
$("#NoticePopupBox").dialog({
modal: true,
autoOpen: false,
dialogClass: 'BluePopupBox',
minWidth: 500
});
$('#NoticePopupBox').parent().resizable("disable");
$('#NoticePopupBox').parent().removeClass("ui-state-disabled");
$('#NoticePopupBox').parent().css("border","3px solid #269ac9").css("padding","10px");
$('a#switch_type').live('click', function(){
if($("input[name=homework_type]").val() == "3"){
$("#NoticePopupBox" ).dialog("close");
$("input[name=homework_type]").val(1);
$('span.group_detail_info').text('');
$("#group_setting a").removeClass("fontBlue2");
$("#BluePopupBox").dialog("open");
$(".ui-dialog-titlebar").hide();
$("a.CloseBtn").on('click', function(){
$("#BluePopupBox" ).dialog("close");
});
var inputs = document.getElementsByName("program[input][]");
var outputs = document.getElementsByName("program[output][]");
if (inputs.length == outputs.length) {
for (var i=0; i<inputs.length; i++) {
autoTextarea2(inputs[i], outputs[i]);
autoTextarea2(outputs[i], inputs[i]);
}
}
$(inputs[0]).focus();
2016-01-29 18:17:27 +08:00
} else if($("input[name=homework_type]").val() == "2") {
$("#NoticePopupBox" ).dialog("close");
$("input[name=homework_type]").val(1);
$('span.program_detail_info').text("");
$("span.program_detail_info").removeClass("fontBlue2");
$("#program_setting a").removeClass("fontBlue2");
$("#GroupPopupBox").dialog("open");
$(".ui-dialog-titlebar").hide();
$("a.popClose").on('click', function(){
reset_group_attr();
2016-01-29 18:17:27 +08:00
$("#GroupPopupBox" ).dialog("close");
});
$("#cancel_group").on('click', function(){
reset_group_attr();
2016-01-29 18:17:27 +08:00
$("#GroupPopupBox" ).dialog("close");
});
$('#min_num').focus();
}
});
2016-01-20 16:03:28 +08:00
$("#BluePopupBox").dialog({
modal: true,
autoOpen: false,
dialogClass: 'BluePopupBox',
minWidth: 771
2016-01-20 16:03:28 +08:00
});
2016-01-20 17:55:20 +08:00
$('#BluePopupBox').parent().resizable("disable");
$('#BluePopupBox').parent().removeClass("ui-state-disabled");
$('#BluePopupBox').parent().css("border","3px solid #269ac9").css("padding","10px");
2016-01-22 22:17:52 +08:00
$('#BluePopupBox').parent().css("position","fixed");
2016-01-20 16:03:28 +08:00
$('a.ProBtn').live('click', function(){
2016-01-29 18:17:27 +08:00
if($("input[name=homework_type]").val() == "3"){
$("#NoticePopupBox").dialog("open");
$(".ui-dialog-titlebar").hide();
$("a.CloseBtn").on('click', function(){
$("#NoticePopupBox" ).dialog("close");
});
$("#cancel_switch").on('click', function(){
$("#NoticePopupBox" ).dialog("close");
});
$("#switch_homework_type_notice").text("您的作业类型将改为“编程作业”,并且您的分组设置将被清除。请问是否继续?");
2016-01-29 18:17:27 +08:00
}
else {
$("#BluePopupBox").dialog("open");
$(".ui-dialog-titlebar").hide();
$("a.CloseBtn").on('click', function(){
$("#BluePopupBox" ).dialog("close");
});
var inputs = document.getElementsByName("program[input][]");
var outputs = document.getElementsByName("program[output][]");
if (inputs.length == outputs.length) {
for (var i=0; i<inputs.length; i++) {
autoTextarea2(inputs[i], outputs[i]);
autoTextarea2(outputs[i], inputs[i]);
}
}
$(inputs[0]).focus();
2016-01-29 18:17:27 +08:00
}
2016-01-20 16:03:28 +08:00
});
var saveProgramAnswers = function() {
var test_numbers = 0;
var valid = true;
var input = null;
var output = null;
var input_groups = [];
$.each($('#BluePopupBox textarea.InputBox'), function(i, val){
if ($(val).val().length<=0) {
$(val)[0].focus();
valid =false;
return false;
}
if (test_numbers %2==0) {
input = $(val).val();
} else {
output = $(val).val();
input_groups.push({input: input, output: output});
}
test_numbers += 1;
});
var language = '';
if($('select.language_type').val() == 1){
language = 'C';
}else if($('select.language_type').val() == 2){
language = 'C++';
}else if($('select.language_type').val() == 3){
language = 'Python';
}else if($('select.language_type').val() == 4){
language = 'Java';
}
if (valid) {
$("input[name=homework_type]").val(2);
2016-01-29 18:17:27 +08:00
$('span.program_detail_info').text('('+language+'语言,'+test_numbers/2+'组测试)');
$("span.program_detail_info").addClass("fontBlue2");
$("#program_setting a").addClass("fontBlue2");
2016-01-20 16:03:28 +08:00
//保存js值
var data = {
language_type: $('select.language_type').val(),
input_groups: input_groups
};
//构建到form中
$('.program-input').remove();
var html=bt('t:program-input-list',data);
$("input[name=homework_type]").after(html);
}
return valid;
};
$("#BluePopupBox a.BlueCirBtn").live('click', function(){
if(saveProgramAnswers()){
if($( "#BluePopupBox" ).dialog( "isOpen" )){
$("#BluePopupBox").dialog( "close" );
}
}
});
$("#BluePopupBox").on('click', 'a.icon_add', function(){
var html = bt('t:test-answer-list', null);
$(this).parent('.mt10').after(html);
var inputs = document.getElementsByName("program[input][]");
var outputs = document.getElementsByName("program[output][]");
var inputs_labels = document.getElementsByName("inputs_label");
for(var j= 0; j<inputs_labels.length; j++){
$(inputs_labels[j]).html(j + 1 + "&nbsp;&nbsp;" );
}
2016-01-20 16:03:28 +08:00
if (inputs.length == outputs.length) {
for (var i=0; i<inputs.length; i++) {
autoTextarea2(inputs[i], outputs[i]);
autoTextarea2(outputs[i], inputs[i]);
}
}
$(inputs[inputs.length - 1]).focus();
2016-01-20 16:03:28 +08:00
});
$("#BluePopupBox").on('click', 'a.icon_remove', function(){
$(this).parent('.mt10').remove();
});
//分组作业
$("#GroupPopupBox").dialog({
modal: true,
autoOpen: false,
dialogClass: 'BluePopupBox',
minWidth: 290
});
2016-01-20 17:55:20 +08:00
$('#GroupPopupBox').parent().resizable("disable");
$('#GroupPopupBox').parent().removeClass("ui-state-disabled");
$('#GroupPopupBox').parent().css("border","3px solid #269ac9").css("padding","10px");
2016-01-20 16:03:28 +08:00
$('a.groupBtn').live('click', function(){
2016-01-29 18:17:27 +08:00
if($("input[name=homework_type]").val() == "2"){
$("#NoticePopupBox").dialog("open");
$(".ui-dialog-titlebar").hide();
$("a.CloseBtn").on('click', function(){
$("#NoticePopupBox" ).dialog("close");
});
$("#cancel_switch").on('click', function(){
$("#NoticePopupBox" ).dialog("close");
});
$("#switch_homework_type_notice").text("您的作业类型将改为“分组作业”,并且您的编程设置将被清除。请问是否继续?");
2016-01-29 18:17:27 +08:00
}
else {
$("#GroupPopupBox").dialog("open");
$(".ui-dialog-titlebar").hide();
$("a.popClose").on('click', function () {
reset_group_attr();
2016-01-29 18:17:27 +08:00
$("#GroupPopupBox").dialog("close");
});
$("#cancel_group").on('click', function () {
reset_group_attr();
2016-01-29 18:17:27 +08:00
$("#GroupPopupBox").dialog("close");
});
$('#min_num').focus();
}
2016-01-20 16:03:28 +08:00
});
var reset_group_attr = function() {
$("#min_num_notice").hide();
$("#min_max_num_notice").hide();
$("#max_num_notice").hide();
if($("input[name=min_num]").length > 0 && $("input[name=max_num]").length > 0) {
$("#min_num").val($("input[name=min_num]").val());
$("#max_num").val($("input[name=max_num]").val());
} else {
$("#min_num").val(2);
$("#max_num").val(10);
}
};
2016-01-20 16:03:28 +08:00
var saveGroupAttr = function() {
var valid = true;
var base_on_project = 0;
var min = $.trim($("#min_num").val());
var max = $.trim($("#max_num").val());
var regex = /^\d+$/;
if(!regex.test(min) || parseInt(min) <= 0) {
$("#min_num_notice").html("请输入正整数");
$("#max_num_notice").html("");
$("#min_max_num_notice").html("");
$("#min_num_notice").show();
2016-01-20 16:03:28 +08:00
$("#min_num").focus();
return false;
} else {
$("#min_num_notice").html("");
$("#min_num_notice").hide();
}
if(!regex.test(max) || parseInt(max) <= 0) {
$("#max_num_notice").html("请输入正整数");
$("#min_num_notice").html("");
$("#min_max_num_notice").html("");
$("#max_num_notice").show();
$("#max_num").focus();
return false;
} else {
$("#max_num_notice").html("");
$("#max_num_notice").hide();
2016-01-20 16:03:28 +08:00
}
if(parseInt(min) > parseInt(max)) {
$("#min_max_num_notice").html("最小人数不得大于最大人数");
$("#min_num_notice").html("");
$("#max_num_notice").html("");
$("#min_max_num_notice").show();
2016-01-20 16:03:28 +08:00
$("#max_num").focus();
return false;
} else {
$("#min_max_num_notice").html("");
$("#min_max_num_notice").hide();
2016-01-20 16:03:28 +08:00
}
if ($("#base_on_project").is(":checked")) {
base_on_project = 1;
}
if (valid) {
$("input[name=homework_type]").val(3);
$('span.group_detail_info').text('分组人数:'+min+'-'+max+' 人');
2016-01-29 18:17:27 +08:00
$("#group_setting a").addClass("fontBlue2");
2016-01-20 16:03:28 +08:00
//保存js值
var data = {
base_on_project: base_on_project,
min_num: min,
max_num: max
};
//构建到form中
$('.group-input').remove();
var html=bt('t:group-input-list',data);
$("input[name=homework_type]").after(html);
}
return valid;
};
2016-01-29 18:17:27 +08:00
$("#GroupPopupBox a.group_save_btn").live('click', function(){
2016-01-20 16:03:28 +08:00
if(saveGroupAttr()){
if($( "#GroupPopupBox" ).dialog( "isOpen" )){
$("#GroupPopupBox").dialog( "close" );
}
}
});
//代码编辑器
if (typeof CodeMirror != "undefined") {
var program_name = "text/x-csrc";
var language = $('#data-language').attr('data-language');
if (language == 1) {
program_name = 'text/x-csrc';
} else if(language==2){
program_name = 'text/x-c++src';
}else if(language==3){
program_name = 'text/x-cython';
} else if(language==4){
program_name = 'text/x-java';
}
if ($('#program-src').val().length<=0) {
var src = '';
if(language==4){
src = '\
2016-03-25 14:43:51 +08:00
//注意\n\
2016-03-25 15:24:31 +08:00
//1:该程序每次运行的时间必须小于200毫秒否则会超时,程序超时将不会测试剩余的测试集\n\
2016-03-25 15:38:14 +08:00
//2:该程序每次运行使用的内存不能超过1M否则会返回错误\n\
//3:该程序每次运行输出的结果最多显示100个字符(多余的不显示),每行末尾的所有空格用□表示\n\
2016-01-20 16:03:28 +08:00
import java.io.*;\n\
import java.util.*;\n\
\n\
//请一定不要修改类名\n\
class Main\n\
{\n\
public static void main (String args[])\n\
{\n\
//获取参数方式\n\
//Scanner in = new Scanner(System.in);\n\
//int a = in.nextInt();\n\
//int b = in.nextInt();\n\
\n\
//您的代码\n\
}\n\
}\n\
';
}
else if(language==1){
2016-03-25 14:43:51 +08:00
src = '\
//注意\n\
2016-03-25 15:24:31 +08:00
//1:该程序每次运行的时间必须小于200毫秒否则会超时,程序超时将不会测试剩余的测试集\n\
2016-03-25 15:38:14 +08:00
//2:该程序每次运行使用的内存不能超过1M否则会返回错误\n\
//3:该程序每次运行输出的结果最多显示100个字符(多余的不显示),每行末尾的所有空格用□表示\n\
2016-03-25 14:43:51 +08:00
#include <stdio.h>\n\
2016-01-20 16:03:28 +08:00
int main()\n\
{\n\
//获取参数方式 scanf\n\
//int x =0;\n\
//int y = 0;\n\
//scanf("%d", &x);\n\
\n\
//结果输出使用prinf\n\
//printf("%d",x);\n\
\n\
return 0;\n\
}\n\
';
} else if(language==2){
src = '\
2016-03-25 14:43:51 +08:00
//注意\n\
2016-03-25 15:24:31 +08:00
//1:该程序每次运行的时间必须小于200毫秒否则会超时,程序超时将不会测试剩余的测试集\n\
2016-03-25 15:38:14 +08:00
//2:该程序每次运行使用的内存不能超过1M否则会返回错误\n\
//3:该程序每次运行输出的结果最多显示100个字符(多余的不显示),每行末尾的所有空格用□表示\n\
2016-01-20 16:03:28 +08:00
#include <iostream>\n\
using namespace std;\n\
\n\
int main()\n\
{\n\
//获取参数方式 cin\n\
//int x =0;\n\
//cin >> x;\n\
\n\
//结果输出使用 cout\n\
//cout<<"1";\n\
\n\
return 0;\n\
}\n\
';
} else if(language==3){
src = '\
#-*-coding:utf-8-*-\n\
2016-03-25 14:43:51 +08:00
#注意\n\
2016-03-25 15:24:31 +08:00
#1:该程序每次运行的时间必须小于200毫秒否则会超时,程序超时将不会测试剩余的测试集\n\
2016-03-25 15:38:14 +08:00
#2:该程序每次运行使用的内存不能超过1M否则会返回错误\n\
2016-03-26 14:48:57 +08:00
#3:该程序每次运行输出的结果最多显示100个字符(多余的不显示),空格用表示\n\
2016-01-20 16:03:28 +08:00
import sys \n\
\n\
#获取参数方式使用raw_input\n\
#input = raw_input()\n\
#a, b = input.split()\n\
\n\
#结果输出使用 print \n\
#print (str(int(a)+int(b)))\n\
';
}
$('#program-src').val(src);
};
var editor = CodeMirror.fromTextArea(document.getElementById("program-src"), {
mode: {name: program_name,
version: 2,
singleLineStringErrors: false},
lineNumbers: true,
indentUnit: 2,
matchBrackets: true
});
editor.on('change',function(cMirror){
// get value right from instance
$('#program-src').val(cMirror.getValue());
});
}
2015-09-19 18:17:12 +08:00
});