扩展Moodle的Qformat插件

时间:2018-12-25 08:19:38

标签: php wordpress laravel moodle

我想扩展此插件,以支持超过4个问题选项和2个以上答案。有人可以为我提供通用插件或扩展该插件的代码吗?还从下面的插件文件粘贴代码-

https://moodle.org/plugins/qformat_csv

public function provide_import() {
    return true;
}

public function provide_export() {
    return true;
}

/**
 * @return string the file extension (including .) that is normally used for
 * files handled by this plugin.
 */
public function export_file_extension() {
    return '.csv';
}

public function readquestions($lines) {
    global $CFG;
    require_once($CFG->libdir . '/csvlib.class.php');
    question_bank::get_qtype('multianswer'); // Ensure the multianswer code is loaded.
    $questions = array();
    $question = $this->defaultquestion();
    $headers = explode(',', $lines[0]);
    $answertwo = 0;
    foreach ($headers as $key => $value) {
        if (trim($value) == "Answer 2") {
            $answertwo = $key;
        }
    }
    // Get All the Header Values from the CSV file.
    for ($rownum = 1; $rownum < count($lines); $rownum++) {
        $rowdata = str_getcsv($lines[$rownum], ",", '"'); // Ignore the commas(,) within the double quotes (").
        $columncount = count($rowdata);
        $headerscount = count($headers);
        if ($columncount != $headerscount || $columncount != 7  || $headerscount != 7) {
            if ($columncount > $headerscount ) {
                // There are more than 7 values or there will be extra comma making them more then 7 values.
                    echo get_string('commma_error', 'qformat_csv', $rownum);
                return 0;
            } else if ($columncount < $headerscount) {
                // Entire question with options and answer is not in one line, new line found.
                    echo get_string('newline_error', 'qformat_csv', $rownum);
                return 0;
            } else {
                // There are more than 7 values or there will be extra comma making them more then 7 values.
                    echo get_string('csv_file_error', 'qformat_csv', $rownum);
                return 0;
            }
        }
        for ($linedata = 0; $linedata < count($rowdata); $linedata++) {
            if ($answertwo != 0 && !empty(trim($rowdata[$answertwo]))) {
                $fraction = 0.5;
                $question->single = 0;
            } else {
                $fraction = 1;
                $question->single = 1;
            }

            $question->qtype = 'multichoice';
            $question->name = $this->create_default_question_name($rownum, get_string('questionname', 'question'));
            if ($headers[$linedata] == 'questiontext') {
                  $question->questiontext = htmlspecialchars(trim($rowdata[$linedata]), ENT_NOQUOTES);
            } else if ($headers[$linedata] == 'generalfeedback') {
                // If extra column is provide with header 'generalfeedback' then that feedback will get applied.
                $question->generalfeedback = $rowdata[$linedata];
                $question->generalfeedbackformat = FORMAT_HTML;
            } else if ($headers[$linedata] == 'defaultgrade') {
                $question->defaultgrade = $this->text_field($rowdata[$linedata]);
            } else if ($headers[$linedata] == 'penalty') {
                $question->penalty = $rowdata[$linedata];
            } else if ($headers[$linedata] == 'hidden') {
                $question->hidden = $rowdata[$linedata];
            } else if ($headers[$linedata] == 'answernumbering') {
                $question->answernumbering = $rowdata[$linedata];
            } else if ($headers[$linedata] == 'correctfeedback') {
                $question->correctfeedback = $this->text_field($rowdata[$linedata]);
            } else if ($headers[$linedata] == 'partiallycorrectfeedback') {
                $question->partiallycorrectfeedback = $this->text_field($rowdata[$linedata]);
            } else if ($headers[$linedata] == 'incorrectfeedback') {
                $question->incorrectfeedback = $this->text_field($rowdata[$linedata]);
            } else if ($headers[$linedata] == 'A') {
                $correctans1 = $linedata + 4;
                $correctans2 = $linedata + 5;
                $question->answer[] = $this->text_field($rowdata[$linedata]);
                if (trim($rowdata[$correctans1]) == 'A' || trim($rowdata[$correctans2]) == 'A') {
                    $question->fraction[]  = $fraction;
                } else {
                    $question->fraction[] = 0;
                }
                                                $question->feedback[] = $this->text_field('');
            } else if ($headers[$linedata] == 'B') {
                $correctans1 = $linedata + 3;
                $correctans2 = $linedata + 4;
                $question->answer[] = $this->text_field($rowdata[$linedata]);
                if (trim($rowdata[$correctans1]) == 'B' || trim($rowdata[$correctans2]) == 'B') {
                    $question->fraction[]  = $fraction;
                } else {
                    $question->fraction[] = 0;
                }
                $question->feedback[] = $this->text_field('');
            } else if ($headers[$linedata] == 'C') {
                $correctans1 = $linedata + 2;
                $correctans2 = $linedata + 3;
                $question->answer[] = $this->text_field($rowdata[$linedata]);
                if (trim($rowdata[$correctans1]) == 'C' || trim($rowdata[$correctans2]) == 'C') {
                    $question->fraction[]  = $fraction;
                } else {
                    $question->fraction[] = 0;
                }
                $question->feedback[] = $this->text_field('');
            } else if ($headers[$linedata] == 'D') {
                $correctans1 = $linedata + 1;
                $correctans2 = $linedata + 2;
                $question->answer[] = $this->text_field($rowdata[$linedata]);
                if (trim($rowdata[$correctans1]) == 'D' || trim($rowdata[$correctans2]) == 'D') {
                    $question->fraction[]  = $fraction;
                } else {
                    $question->fraction[] = 0;
                }
                $question->feedback[] = $this->text_field('');
            }
        }
                $questions[] = $question;
                // Clear array for next question set.
                $question = $this->defaultquestion();
    }
     return $questions;
}
protected function text_field($text) {
    return array(
        'text' => htmlspecialchars(trim($text), ENT_NOQUOTES),
        'format' => FORMAT_HTML,
        'files' => array(),
    );
}

public function readquestion($lines) {
    // This is no longer needed but might still be called by default.php.
    return;
}

public function writequestion($question) {
    global $OUTPUT;
    // Output depends on question type.
    if ($globals['header']) {
        if ($question->qtype == 'category') {// If Write "category to file" i selected then no need to add "\n" in header.
            $expout = "questiontext,A,B,C,D,Answer 1,Answer 2";
        } else {
            $expout .= "questiontext,A,B,C,D,Answer 1,Answer 2\n";
        }
        $globals['header'] = false;
    }
      $answercount = 0;
      $rightanswercount = 0;
    switch($question->qtype) {
        case 'multichoice':
            if (count($question->options->answers) != 4 ) {
                break;
            }
            $expout .= '"'.$question->questiontext.'"'.',';
            foreach ($question->options->answers as $answer) {
                $answercount++;
                if ($answer->fraction == 1 && $question->options->single) {
                    switch ($answercount) {
                        case 1:
                            $rightanswer = 'A'.',';
                            break;
                        case 2:
                            $rightanswer = 'B'.',';
                            break;
                        case 3:
                            $rightanswer = 'C'.',';
                            break;
                        case 4:
                            $rightanswer = 'D'.',';
                            break;
                        default:
                            $rightanswer = '';
                            break;
                    }
                } else if ($answer->fraction == 0.5 && !$question->options->single) {
                    $rightanswercount ++;
                    $comma = "";
                    if ( $rightanswercount <= 1 ) {
                        $comma = ","; // Add comma  to first answer i.e. to 'Answer 1'.
                    }
                    switch ($answercount) {
                        case 1:
                            $rightanswer .= 'A'.$comma;
                            break;
                        case 2:
                            $rightanswer .= 'B'.$comma;
                            break;
                        case 3:
                            $rightanswer .= 'C'.$comma;
                            break;
                        case 4:
                            $rightanswer .= 'D'.$comma;
                            break;
                        default:
                            $rightanswer = '';
                            break;
                    }

                }
                $expout .= '"'.$answer->answer.'"'.',';
            }
            $expout .= $rightanswer;
        break;
    }

    return $expout;
}

}

0 个答案:

没有答案