コード例 #1
0
 $ProbLevel = $_GET["ProbLevel"];
 $ProbMemo = $_GET["ProbMemo"];
 // ID check
 $str_query = "Select * from problems where ProblemId='{$ProbId}'";
 if ($result = mysqli_query($link, $str_query)) {
     $row_number = mysqli_num_rows($result);
     if ($row_number == 0) {
         echo ERR_PROB_NOT_EXIST;
         return;
     }
 }
 // get problem type
 $row = mysqli_fetch_assoc($result);
 $ProbStatus = $row['Status'];
 $ProbType = $row['ProblemType'];
 if (!is_correct_prob_desc_format($ProbDesc)) {
     echo ERR_PROB_DESC_FORMAT;
     return;
 }
 $selections = array($ProbSelA, $ProbSelB, $ProbSelC, $ProbSelD, $ProbSelE, $ProbSelF, $ProbSelG, $ProbSelH);
 if (!is_correct_prob_selection_format($selections, $ProbType)) {
     echo ERR_PROB_SELECTOR_FORMAT;
     return;
 }
 // Answer check, the number of answer depend on the type and selector
 if (!($ret = is_correct_prob_answer_format($ProbAnswer, $selections, $ProbType))) {
     echo $ret;
     echo ERR_PROB_ANSWER_FORMAT;
     return;
 }
 // Category check
コード例 #2
0
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);
        $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));
            }
            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);
            array_push($problems, $cur_problem);
        }
    }
    if ($file_status->status == UPLOAD_SUCCESS) {
        return write_into_database($problems);
    } else {
        return $file_status->status;
    }
}