function write_into_database($problems) { foreach ($problems as $problem) { if (is_problem_exist($problem)) { $ret = update_old_problem($problem); } else { $ret = insert_new_problem($problem); } if ($ret != SUCCESS) { return $ret; } } return SUCCESS; }
function read_excel_and_insert_into_database($target_file) { // return {"status":, error:[{"line":"1", "message":"xxx error"},{"line":"", "message":""}, ...]} $problems = array(); global $file_status; // load file try { $input_file_type = PHPExcel_IOFactory::identify($target_file); $reader = PHPExcel_IOFactory::createReader($input_file_type); $excel = $reader->load($target_file); } catch (Exception $e) { $file_status->status = ERR_FILE_LOAD; array_push($file_status->errors, array("sheet" => 0, "lines" => 0, "message" => $e->getMessage())); return $file_status->status; } // parse file $sheet_count = $excel->getSheetCount(); for ($cur_sheet = 0; $cur_sheet < $sheet_count; $cur_sheet++) { $sheet = $excel->getSheet($cur_sheet); $sheet_title = $sheet->getTitle(); //print_r($sheet_title); if ($sheet_title == "上传题库说明") { continue; } // if sheet name is xxxx, skip it $highest_row = $sheet->getHighestRow(); $highest_col = count($file_status->upload_problem_syntax); $tmp = array(); for ($col = 0; $col <= $highest_col; $col++) { array_push($tmp, trim($sheet->getCellByColumnAndRow($col, 1)->getValue())); } if (!is_valid_syntax_import_file($tmp)) { $file_status->status = ERR_FILE_LOAD; array_push($file_status->errors, array("sheet" => $cur_sheet, "lines" => 0, "message" => MSG_ERR_FILE_CONTENT_SYNTAX)); return $file_status->status; } for ($row = 2; $row <= $highest_row; $row++) { $tmp = array(); $functions = array(); for ($col = 0; $col <= $highest_col; $col++) { array_push($tmp, trim($sheet->getCellByColumnAndRow($col, $row)->getValue())); } if (is_empty_row($tmp)) { continue; } $cur_problem = new UploadProblem($tmp); if (!is_correct_prob_type_format($cur_problem->type)) { $file_status->status = ERR_FILE_LOAD; array_push($file_status->errors, array("sheet" => $cur_sheet, "lines" => $row, "message" => MSG_ERR_PROB_TYPE_FORMAT)); } if (!is_correct_prob_desc_format($cur_problem->desc)) { $file_status->status = ERR_FILE_LOAD; array_push($file_status->errors, array("sheet" => $cur_sheet, "lines" => $row, "message" => MSG_ERR_PROB_DESC_FORMAT)); } if (!is_correct_prob_level_format($cur_problem->level)) { $file_status->status = ERR_FILE_LOAD; array_push($file_status->errors, array("sheet" => $cur_sheet, "lines" => $row, "message" => MSG_ERR_PROB_LEVEL_FORMAT)); } if (!is_correct_prob_answer_format($cur_problem->answer, $cur_problem->selections, $cur_problem->type)) { $file_status->status = ERR_FILE_LOAD; array_push($file_status->errors, array("sheet" => $cur_sheet, "lines" => $row, "message" => MSG_ERR_PROB_ANSWER_FORMAT)); } if (!is_correct_prob_selection_format($cur_problem->selections, $cur_problem->type)) { $file_status->status = ERR_FILE_LOAD; array_push($file_status->errors, array("sheet" => $cur_sheet, "lines" => $row, "message" => MSG_ERR_PROB_SELECTOR_FORMAT)); } // if this sheet is OBL, insert OBL function name to $cur_problem->category_product if ($sheet_title == "OBL") { print_r("OBL"); array_push($cur_problem->category_product, "OBL"); } foreach ($cur_problem->category_product as $product_name) { $func_id = get_function_id_from_database($product_name); if ($func_id == ERR_PROB_FUNC_NOT_EXIST) { $file_status->status = ERR_FILE_LOAD; array_push($file_status->errors, array("sheet" => $cur_sheet, "lines" => $row, "message" => "{$product_name} 不存在")); } else { array_push($functions, $func_id); } } foreach ($cur_problem->category_adaptaion as $adaptation_name) { $func_id = get_function_id_from_database($adaptation_name); if ($func_id == ERR_PROB_FUNC_NOT_EXIST) { $file_status->status = ERR_FILE_LOAD; array_push($file_status->errors, array("sheet" => $cur_sheet, "lines" => $row, "message" => "{$adaptation_name} 不存在")); } else { array_push($functions, $func_id); } } foreach ($cur_problem->problem_category as $category_name) { $func_id = get_function_id_from_database($category_name); if ($func_id == ERR_PROB_FUNC_NOT_EXIST) { $file_status->status = ERR_FILE_LOAD; array_push($file_status->errors, array("sheet" => $cur_sheet, "lines" => $row, "message" => "{$category_name} 不存在")); } else { array_push($functions, $func_id); } } if (is_no_any_functions($functions)) { $file_status->status = ERR_FILE_LOAD; array_push($file_status->errors, array("sheet" => $cur_sheet, "lines" => $row, "message" => "至少需要有一个分类")); } $cur_problem->functions_str = output_category_str_from_func_array($functions); if (is_problem_exist($cur_problem)) { array_push($file_status->errors, array("sheet" => $cur_sheet, "lines" => $row, "ProblemDesc" => $cur_problem->desc, "message" => "此考题系统中已存在!")); } else { array_push($problems, $cur_problem); } } } if ($file_status->status == UPLOAD_SUCCESS) { return write_into_database($problems); } else { return $file_status->status; } }