socialforge/public/javascripts/homework.js

365 lines
10 KiB
JavaScript
Raw Normal View History

2015-08-28 12:04:49 +08:00
$(function(){
2015-08-29 18:21:01 +08:00
//提交作业
2015-08-29 11:59:52 +08:00
var bt=baidu.template;
bt.LEFT_DELIMITER='<!';
bt.RIGHT_DELIMITER='!>';
2015-09-07 18:29:25 +08:00
2015-08-29 11:59:52 +08:00
var tested = false;
2015-08-28 15:08:07 +08:00
var valid_form = function() {
var src = $('#program-src').val();
var title = $('#program-title').val();
if (!src) {
alert('请输入正确的代码');
return false;
}
if (!title) {
alert('请输入标题');
return false;
}
return true;
};
2015-08-28 15:08:07 +08:00
2015-08-29 11:59:52 +08:00
var test_program = function(cb){
var homework_id = $('#test-program-btn').attr('data-homework-id');
var student_work_id = $('#test-program-btn').attr('data-student-work-id');
2015-08-28 12:04:49 +08:00
var src = $('#program-src').val();
2015-08-28 15:08:07 +08:00
var title = $('#program-title').val();
2015-09-02 21:38:13 +08:00
var is_test = $('input[name=is_test]').val();
2015-08-28 12:04:49 +08:00
2015-08-28 15:08:07 +08:00
if(!valid_form()){
2015-08-28 12:04:49 +08:00
return;
}
$.post(
'/student_work/program_test',
2015-09-02 21:38:13 +08:00
{homework: homework_id, student_work_id: student_work_id, src: src, title: title, is_test: is_test},
2015-08-28 12:04:49 +08:00
function(data,status){
2015-08-29 11:59:52 +08:00
tested = true;
console.log(data);
2015-09-02 22:09:23 +08:00
if(data.index <=0){
data.index = $('.ProResultTop').length+1;
}
2015-08-29 11:59:52 +08:00
2015-09-06 22:59:27 +08:00
if (typeof cb == 'function') {cb(data); return;}
2015-08-29 11:59:52 +08:00
2015-08-29 18:21:01 +08:00
2015-08-29 11:59:52 +08:00
var html=bt('t:result-list',data);
$('.ProResult').prepend(html);
2015-08-29 18:21:01 +08:00
2015-09-02 22:09:23 +08:00
if (data.status==0 && is_test != 'true') {
2015-08-29 11:59:52 +08:00
var r=confirm("答题正确,是否立刻提交?");
if (r) {
$(".HomeWorkCon form").submit();
}
2015-08-28 12:04:49 +08:00
}
}
);
2015-08-29 11:59:52 +08:00
};
2015-09-11 14:59:30 +08:00
$('#test-program-btn').on('click', test_program);
2015-08-28 15:08:07 +08:00
2015-09-11 14:59:30 +08:00
$('#commit-program-work-btn').on('click', function(){
2015-08-28 15:08:07 +08:00
if(!valid_form()){
return;
}
2015-08-29 11:59:52 +08:00
if($('.ProResult .ProResultTop').length<=0){
var r=confirm("测试后才能提交,是否立刻测试?");
if (r) {
test_program();
}
return;
}
if (!tested) {
2015-09-06 22:59:27 +08:00
test_program(function(data){
if (data.status!=0) {
var r=confirm("测试不通过,是否强制提交?");
if (!r) {
return;
}
};
2015-08-29 11:59:52 +08:00
$(".HomeWorkCon form").submit();
});
return;
}
2015-08-28 15:08:07 +08:00
$(".HomeWorkCon form").submit();
});
2015-09-11 14:59:30 +08:00
$('form.edit_student_work').on('keydown', '#program-src', function(){
2015-08-29 11:59:52 +08:00
tested = false;
2015-08-29 18:21:01 +08:00
});
//发布作业
2015-09-07 18:29:25 +08:00
2015-09-19 18:17:12 +08:00
$('#program-src').focus(function(){
$(this).css('height', '100px');
});
2015-09-08 14:48:02 +08:00
2015-08-29 18:21:01 +08:00
var datepickerOptions={dateFormat:'yy-mm-dd',firstDay:0,showWeek:true,showOtherMonths:true,selectOtherMonths:true};
$('input.date-input').datepicker(datepickerOptions);
2015-09-11 14:59:30 +08:00
$('a.pic_date').on('click', function(){
2015-08-29 18:21:01 +08:00
$(this).parent().prev().first().focus();
2015-08-29 11:59:52 +08:00
})
2015-08-29 18:21:01 +08:00
2015-09-11 14:59:30 +08:00
$("#BluePopupBox").dialog({
2015-09-07 23:09:19 +08:00
modal: true,
2015-09-11 14:59:30 +08:00
autoOpen: false,
2015-09-07 23:09:19 +08:00
dialogClass: 'BluePopupBox',
minWidth: 753
2015-09-11 14:59:30 +08:00
});
2015-09-07 23:09:19 +08:00
$('a.ProBtn').live('click', function(){
2015-09-11 14:59:30 +08:00
$("#BluePopupBox").dialog("open");
$(".ui-dialog-titlebar").hide();
$("a.CloseBtn").on('click', function(){
2015-09-07 23:09:19 +08:00
$("#BluePopupBox" ).dialog("close");
});
$('#textarea_input_test').focus();
2015-09-11 14:59:30 +08:00
});
2015-09-07 23:09:19 +08:00
2015-09-11 15:03:20 +08:00
var saveProgramAnswers = function() {
2015-09-11 14:59:30 +08:00
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;
}
2015-09-11 14:59:30 +08:00
if (test_numbers %2==0) {
input = $(val).val();
} else {
output = $(val).val();
input_groups.push({input: input, output: output});
}
test_numbers += 1;
2015-09-07 23:09:19 +08:00
});
2015-09-11 14:59:30 +08:00
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';
2015-09-24 15:09:15 +08:00
}else if($('select.language_type').val() == 4){
language = 'Java';
}
2015-09-11 14:59:30 +08:00
if (valid) {
$("input[name=homework_type]").val(2);
$('span.program_detail_info').text('('+language+''+test_numbers/2+'组测试)');
//保存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);
2015-09-11 15:03:20 +08:00
}
return valid;
2015-12-02 11:14:46 +08:00
};
2015-09-11 15:03:20 +08:00
$("#BluePopupBox a.BlueCirBtn").live('click', function(){
2015-09-11 15:03:20 +08:00
if(saveProgramAnswers()){
2015-09-11 14:59:30 +08:00
if($( "#BluePopupBox" ).dialog( "isOpen" )){
$("#BluePopupBox").dialog( "close" );
}
}
2015-08-29 18:21:01 +08:00
});
2015-09-11 14:59:30 +08:00
$("#BluePopupBox").on('click', 'a.icon_add', function(){
2015-08-29 18:21:01 +08:00
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][]");
if (inputs.length == outputs.length) {
for (var i=0; i<inputs.length; i++) {
autoTextarea2(inputs[i], outputs[i]);
autoTextarea2(outputs[i], inputs[i]);
}
}
2015-09-19 18:17:12 +08:00
2015-08-29 18:21:01 +08:00
});
2015-09-11 14:59:30 +08:00
$("#BluePopupBox").on('click', 'a.icon_remove', function(){
2015-08-29 18:21:01 +08:00
$(this).parent('.mt10').remove();
});
2015-09-19 18:17:12 +08:00
2015-12-02 11:14:46 +08:00
//分组作业
$("#GroupPopupBox").dialog({
modal: true,
autoOpen: false,
dialogClass: 'BluePopupBox',
minWidth: 290
});
$('a.groupBtn').live('click', function(){
$("#GroupPopupBox").dialog("open");
$(".ui-dialog-titlebar").hide();
$("a.popClose").on('click', function(){
$("#GroupPopupBox" ).dialog("close");
});
$("#cancel_group").on('click', function(){
$("#GroupPopupBox" ).dialog("close");
});
$('#min_num').focus();
});
var saveGroupAttr = function() {
var valid = true;
var base_on_project = 0;
var min = $.trim($("#min_num").val());
var max = $.trim($("#max_num").val());
if(min.length <= 0) {
$("#min_num").focus();
valid = false;
return false;
}
if(max.length <= 0) {
$("#max_num").focus();
valid = false;
return false;
}
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+' 人');
//保存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;
};
$("#GroupPopupBox a.group_btn").live('click', function(){
if(saveGroupAttr()){
if($( "#GroupPopupBox" ).dialog( "isOpen" )){
$("#GroupPopupBox").dialog( "close" );
}
}
});
2015-09-19 18:17:12 +08:00
//代码编辑器
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';
2015-09-24 15:09:15 +08:00
} else if(language==4){
program_name = 'text/x-java';
2015-09-19 18:17:12 +08:00
}
2015-09-24 15:09:15 +08:00
if ($('#program-src').val().length<=0) {
2015-09-24 15:26:54 +08:00
var src = '';
if(language==4){
src = '\
2015-09-24 15:09:15 +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\
2015-09-24 15:26:54 +08:00
';
}
else if(language==1){
src = '#include <stdio.h>\n\
\n\
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 = '\
#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 = '\
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);
2015-09-24 15:09:15 +08:00
};
2015-09-19 18:17:12 +08:00
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());
});
}
});