/**
  * The question type name.
  * @param object $question containst the data to display.
  * @return string contents of this table cell.
  */
 protected function col_qtype($question)
 {
     if (property_exists($question, 'qtype') && $question->qtype) {
         return question_bank::get_qtype_name($question->qtype);
     } else {
         return '';
     }
 }
Пример #2
0
/**
 * Print a given single question in quiz for the edit tab of edit.php.
 * Meant to be used from quiz_print_question_list()
 *
 * @param object $question A question object from the database questions table
 * @param object $returnurl The url to get back to this page, for example after editing.
 * @param object $quiz The quiz in the context of which the question is being displayed
 */
function quiz_print_singlequestion($question, $returnurl, $quiz) {
    echo '<div class="singlequestion ' . $question->qtype . '">';
    echo quiz_question_edit_button($quiz->cmid, $question, $returnurl,
            quiz_question_tostring($question) . ' ');
    echo '<span class="questiontype">';
    echo print_question_icon($question);
    echo ' ' . question_bank::get_qtype_name($question->qtype) . '</span>';
    echo '<span class="questionpreview">' .
            quiz_question_preview_button($quiz, $question, true) . '</span>';
    echo "</div>\n";
}
Пример #3
0
 /**
  * The question type name.
  *
  * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
  * @return string contents of this table cell.
  */
 protected function col_qtype($questionstat)
 {
     return question_bank::get_qtype_name($questionstat->question->qtype);
 }
Пример #4
0
    public function validation($data, $files) {
        $errors = parent::validation($data, $files);

        $questiondisplay = qtype_multianswer_extract_question($data['questiontext']);

        if (isset($questiondisplay->options->questions)) {
            $subquestions = fullclone($questiondisplay->options->questions);
            if (count($subquestions)) {
                $sub = 1;
                foreach ($subquestions as $subquestion) {
                    $prefix = 'sub_'.$sub.'_';
                    $answercount = 0;
                    $maxgrade = false;
                    $maxfraction = -1;
                    if (isset($this->savedquestiondisplay->options->questions[$sub]->qtype) &&
                            $this->savedquestiondisplay->options->questions[$sub]->qtype !=
                                    $questiondisplay->options->questions[$sub]->qtype) {
                        $storemess = " STORED QTYPE ".question_bank::get_qtype_name(
                                $this->savedquestiondisplay->options->questions[$sub]->qtype);
                    }
                    foreach ($subquestion->answer as $key => $answer) {
                        if (is_array($answer)) {
                            $answer = $answer['text'];
                        }
                        $trimmedanswer = trim($answer);
                        if ($trimmedanswer !== '') {
                            $answercount++;
                            if ($subquestion->qtype == 'numerical' &&
                                    !(is_numeric($trimmedanswer) || $trimmedanswer == '*')) {
                                $errors[$prefix.'answer['.$key.']'] =
                                        get_string('answermustbenumberorstar', 'qtype_numerical');
                            }
                            if ($subquestion->fraction[$key] == 1) {
                                $maxgrade = true;
                            }
                            if ($subquestion->fraction[$key] > $maxfraction) {
                                $maxfraction = $subquestion->fraction[$key];
                            }
                        }
                    }
                    if ($answercount == 0) {
                        if ($subquestion->qtype == 'multichoice') {
                            $errors[$prefix.'answer[0]'] =
                                    get_string('notenoughanswers', 'qtype_multichoice', 2);
                        } else {
                            $errors[$prefix.'answer[0]'] =
                                    get_string('notenoughanswers', 'question', 1);
                        }
                    }
                    if ($maxgrade == false) {
                        $errors[$prefix.'fraction[0]'] =
                                get_string('fractionsnomax', 'question');
                    }
                    $sub++;
                }
            } else {
                $errors['questiontext'] = get_string('questionsmissing', 'qtype_multianswer');
            }
        }

        if (($this->negative_diff > 0 || $this->used_in_quiz &&
                ($this->negative_diff > 0 || $this->negative_diff < 0 ||
                        $this->qtype_change))&& $this->confirm == 0) {
            $errors['confirm'] =
                    get_string('confirmsave', 'qtype_multianswer', $this->negative_diff);
        }

        return $errors;
    }
Пример #5
0
 protected function definition_inner($mform)
 {
     $mform->addElement('hidden', 'reload', 1);
     $mform->setType('reload', PARAM_INT);
     // Remove meaningless defaultmark field.
     $mform->removeElement('defaultmark');
     $this->confirm = optional_param('confirm', false, PARAM_BOOL);
     // Display the questions from questiontext.
     if ($questiontext = optional_param_array('questiontext', false, PARAM_RAW)) {
         $this->questiondisplay = fullclone(qtype_multianswer_extract_question($questiontext));
     } else {
         if (!$this->reload && !empty($this->savedquestiondisplay->id)) {
             // Use database data as this is first pass
             // question->id == 0 so no stored datasets.
             $this->questiondisplay = fullclone($this->savedquestiondisplay);
             foreach ($this->questiondisplay->options->questions as $subquestion) {
                 if (!empty($subquestion)) {
                     $subquestion->answer = array('');
                     foreach ($subquestion->options->answers as $ans) {
                         $subquestion->answer[] = $ans->answer;
                     }
                 }
             }
         } else {
             $this->questiondisplay = "";
         }
     }
     if (isset($this->savedquestiondisplay->options->questions) && is_array($this->savedquestiondisplay->options->questions)) {
         $countsavedsubquestions = 0;
         foreach ($this->savedquestiondisplay->options->questions as $subquestion) {
             if (!empty($subquestion)) {
                 $countsavedsubquestions++;
             }
         }
     } else {
         $countsavedsubquestions = 0;
     }
     if ($this->reload) {
         if (isset($this->questiondisplay->options->questions) && is_array($this->questiondisplay->options->questions)) {
             $countsubquestions = 0;
             foreach ($this->questiondisplay->options->questions as $subquestion) {
                 if (!empty($subquestion)) {
                     $countsubquestions++;
                 }
             }
         } else {
             $countsubquestions = 0;
         }
     } else {
         $countsubquestions = $countsavedsubquestions;
     }
     $mform->addElement('submit', 'analyzequestion', get_string('decodeverifyquestiontext', 'qtype_multianswer'));
     $mform->registerNoSubmitButton('analyzequestion');
     if ($this->reload) {
         for ($sub = 1; $sub <= $countsubquestions; $sub++) {
             if (isset($this->questiondisplay->options->questions[$sub]->qtype)) {
                 $this->editas[$sub] = $this->questiondisplay->options->questions[$sub]->qtype;
             } else {
                 $this->editas[$sub] = optional_param('sub_' . $sub . '_qtype', 'unknown type', PARAM_PLUGIN);
             }
             $storemess = '';
             if (isset($this->savedquestiondisplay->options->questions[$sub]->qtype) && $this->savedquestiondisplay->options->questions[$sub]->qtype != $this->questiondisplay->options->questions[$sub]->qtype) {
                 $this->qtypechange = true;
                 $storemess = ' ' . html_writer::tag('span', get_string('storedqtype', 'qtype_multianswer', question_bank::get_qtype_name($this->savedquestiondisplay->options->questions[$sub]->qtype)), array('class' => 'error'));
             }
             $mform->addElement('header', 'subhdr' . $sub, get_string('questionno', 'question', '{#' . $sub . '}') . '&nbsp;' . question_bank::get_qtype_name($this->questiondisplay->options->questions[$sub]->qtype) . $storemess);
             $mform->addElement('static', 'sub_' . $sub . '_questiontext', get_string('questiondefinition', 'qtype_multianswer'));
             if (isset($this->questiondisplay->options->questions[$sub]->questiontext)) {
                 $mform->setDefault('sub_' . $sub . '_questiontext', $this->questiondisplay->options->questions[$sub]->questiontext['text']);
             }
             $mform->addElement('static', 'sub_' . $sub . '_defaultmark', get_string('defaultmark', 'question'));
             $mform->setDefault('sub_' . $sub . '_defaultmark', $this->questiondisplay->options->questions[$sub]->defaultmark);
             if ($this->questiondisplay->options->questions[$sub]->qtype == 'shortanswer') {
                 $mform->addElement('static', 'sub_' . $sub . '_usecase', get_string('casesensitive', 'qtype_shortanswer'));
             }
             if ($this->questiondisplay->options->questions[$sub]->qtype == 'multichoice') {
                 $mform->addElement('static', 'sub_' . $sub . '_layout', get_string('layout', 'qtype_multianswer'));
                 $mform->addElement('static', 'sub_' . $sub . '_shuffleanswers', get_string('shuffleanswers', 'qtype_multichoice'));
             }
             foreach ($this->questiondisplay->options->questions[$sub]->answer as $key => $ans) {
                 $mform->addElement('static', 'sub_' . $sub . '_answer[' . $key . ']', get_string('answer', 'question'));
                 if ($this->questiondisplay->options->questions[$sub]->qtype == 'numerical' && $key == 0) {
                     $mform->addElement('static', 'sub_' . $sub . '_tolerance[' . $key . ']', get_string('acceptederror', 'qtype_numerical'));
                 }
                 $mform->addElement('static', 'sub_' . $sub . '_fraction[' . $key . ']', get_string('grade'));
                 $mform->addElement('static', 'sub_' . $sub . '_feedback[' . $key . ']', get_string('feedback', 'question'));
             }
         }
         $this->negativediff = $countsavedsubquestions - $countsubquestions;
         if ($this->negativediff > 0 || $this->qtypechange || $this->usedinquiz && $this->negativediff != 0) {
             $mform->addElement('header', 'additemhdr', get_string('warningquestionmodified', 'qtype_multianswer'));
         }
         if ($this->negativediff > 0) {
             $mform->addElement('static', 'alert1', "<strong>" . get_string('questiondeleted', 'qtype_multianswer') . "</strong>", get_string('questionsless', 'qtype_multianswer', $this->negativediff));
         }
         if ($this->qtypechange) {
             $mform->addElement('static', 'alert1', "<strong>" . get_string('questiontypechanged', 'qtype_multianswer') . "</strong>", get_string('questiontypechangedcomment', 'qtype_multianswer'));
         }
     }
     if ($this->usedinquiz) {
         if ($this->negativediff < 0) {
             $diff = $countsubquestions - $countsavedsubquestions;
             $mform->addElement('static', 'alert1', "<strong>" . get_string('questionsadded', 'qtype_multianswer') . "</strong>", "<strong>" . get_string('questionsmore', 'qtype_multianswer', $diff) . "</strong>");
         }
         $a = new stdClass();
         $a->nb_of_quiz = $this->nbofquiz;
         $a->nb_of_attempts = $this->nbofattempts;
         $mform->addElement('header', 'additemhdr2', get_string('questionusedinquiz', 'qtype_multianswer', $a));
         $mform->addElement('static', 'alertas', get_string('youshouldnot', 'qtype_multianswer'));
     }
     if (($this->negativediff > 0 || $this->usedinquiz && ($this->negativediff > 0 || $this->negativediff < 0 || $this->qtypechange)) && $this->reload) {
         $mform->addElement('header', 'additemhdr', get_string('questionsaveasedited', 'qtype_multianswer'));
         $mform->addElement('checkbox', 'confirm', '', get_string('confirmquestionsaveasedited', 'qtype_multianswer'));
         $mform->setDefault('confirm', 0);
     } else {
         $mform->addElement('hidden', 'confirm', 0);
         $mform->setType('confirm', PARAM_BOOL);
     }
     $this->add_interactive_settings(true, true);
 }
Пример #6
0
 public function writequestion($question)
 {
     global $OUTPUT;
     // Start with a comment
     $expout = "// question: {$question->id}  name: {$question->name}\n";
     // output depends on question type
     switch ($question->qtype) {
         case 'category':
             // not a real question, used to insert category switch
             $expout .= "\$CATEGORY: {$question->category}\n";
             break;
         case DESCRIPTION:
             $expout .= $this->write_name($question->name);
             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
             break;
         case ESSAY:
             $expout .= $this->write_name($question->name);
             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
             $expout .= "{}\n";
             break;
         case TRUEFALSE:
             $trueanswer = $question->options->answers[$question->options->trueanswer];
             $falseanswer = $question->options->answers[$question->options->falseanswer];
             if ($trueanswer->fraction == 1) {
                 $answertext = 'TRUE';
                 $rightfeedback = $this->write_questiontext($trueanswer->feedback, $trueanswer->feedbackformat, $question->questiontextformat);
                 $wrongfeedback = $this->write_questiontext($falseanswer->feedback, $falseanswer->feedbackformat, $question->questiontextformat);
             } else {
                 $answertext = 'FALSE';
                 $rightfeedback = $this->write_questiontext($falseanswer->feedback, $falseanswer->feedbackformat, $question->questiontextformat);
                 $wrongfeedback = $this->write_questiontext($trueanswer->feedback, $trueanswer->feedbackformat, $question->questiontextformat);
             }
             $expout .= $this->write_name($question->name);
             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
             $expout .= '{' . $this->repchar($answertext);
             if ($wrongfeedback) {
                 $expout .= '#' . $wrongfeedback;
             } else {
                 if ($rightfeedback) {
                     $expout .= '#';
                 }
             }
             if ($rightfeedback) {
                 $expout .= '#' . $rightfeedback;
             }
             $expout .= "}\n";
             break;
         case MULTICHOICE:
             $expout .= $this->write_name($question->name);
             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
             $expout .= "{\n";
             foreach ($question->options->answers as $answer) {
                 if ($answer->fraction == 1) {
                     $answertext = '=';
                 } else {
                     if ($answer->fraction == 0) {
                         $answertext = '~';
                     } else {
                         $weight = $answer->fraction * 100;
                         $answertext = '~%' . $weight . '%';
                     }
                 }
                 $expout .= "\t" . $answertext . $this->write_questiontext($answer->answer, $answer->answerformat, $question->questiontextformat);
                 if ($answer->feedback != '') {
                     $expout .= '#' . $this->write_questiontext($answer->feedback, $answer->feedbackformat, $question->questiontextformat);
                 }
                 $expout .= "\n";
             }
             $expout .= "}\n";
             break;
         case SHORTANSWER:
             $expout .= $this->write_name($question->name);
             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
             $expout .= "{\n";
             foreach ($question->options->answers as $answer) {
                 $weight = 100 * $answer->fraction;
                 $expout .= "\t=%" . $weight . '%' . $this->repchar($answer->answer) . '#' . $this->write_questiontext($answer->feedback, $answer->feedbackformat, $question->questiontextformat) . "\n";
             }
             $expout .= "}\n";
             break;
         case NUMERICAL:
             $expout .= $this->write_name($question->name);
             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
             $expout .= "{#\n";
             foreach ($question->options->answers as $answer) {
                 if ($answer->answer != '' && $answer->answer != '*') {
                     $weight = 100 * $answer->fraction;
                     $expout .= "\t=%" . $weight . '%' . $answer->answer . ':' . (double) $answer->tolerance . '#' . $this->write_questiontext($answer->feedback, $answer->feedbackformat, $question->questiontextformat) . "\n";
                 } else {
                     $expout .= "\t~#" . $this->write_questiontext($answer->feedback, $answer->feedbackformat, $question->questiontextformat) . "\n";
                 }
             }
             $expout .= "}\n";
             break;
         case MATCH:
             $expout .= $this->write_name($question->name);
             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
             $expout .= "{\n";
             foreach ($question->options->subquestions as $subquestion) {
                 $expout .= "\t=" . $this->write_questiontext($subquestion->questiontext, $subquestion->questiontextformat, $question->questiontextformat) . ' -> ' . $this->repchar($subquestion->answertext) . "\n";
             }
             $expout .= "}\n";
             break;
         default:
             // Check for plugins
             if ($out = $this->try_exporting_using_qtypes($question->qtype, $question)) {
                 $expout .= $out;
             } else {
                 $expout .= "Question type {$question->qtype} is not supported\n";
                 echo $OUTPUT->notification(get_string('nohandler', 'qformat_gift', question_bank::get_qtype_name($question->qtype)));
             }
     }
     // Add empty line to delimit questions
     $expout .= "\n";
     return $expout;
 }
Пример #7
0
    public function writequestion($question) {
        global $OUTPUT;

        // Start with a comment
        $expout = "// question: $question->id  name: $question->name\n";

        // output depends on question type
        switch($question->qtype) {

        case 'category':
            // not a real question, used to insert category switch
            $expout .= "\$CATEGORY: $question->category\n";
            break;

        case 'description':
            $expout .= $this->write_name($question->name);
            $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
            break;

        case 'essay':
            $expout .= $this->write_name($question->name);
            $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
            $expout .= "{";
            $expout .= $this->write_general_feedback($question, '');
            $expout .= "}\n";
            break;

        case 'truefalse':
            $trueanswer = $question->options->answers[$question->options->trueanswer];
            $falseanswer = $question->options->answers[$question->options->falseanswer];
            if ($trueanswer->fraction == 1) {
                $answertext = 'TRUE';
                $rightfeedback = $this->write_questiontext($trueanswer->feedback,
                        $trueanswer->feedbackformat, $question->questiontextformat);
                $wrongfeedback = $this->write_questiontext($falseanswer->feedback,
                        $falseanswer->feedbackformat, $question->questiontextformat);
            } else {
                $answertext = 'FALSE';
                $rightfeedback = $this->write_questiontext($falseanswer->feedback,
                        $falseanswer->feedbackformat, $question->questiontextformat);
                $wrongfeedback = $this->write_questiontext($trueanswer->feedback,
                        $trueanswer->feedbackformat, $question->questiontextformat);
            }

            $expout .= $this->write_name($question->name);
            $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
            $expout .= '{' . $this->repchar($answertext);
            if ($wrongfeedback) {
                $expout .= '#' . $wrongfeedback;
            } else if ($rightfeedback) {
                $expout .= '#';
            }
            if ($rightfeedback) {
                $expout .= '#' . $rightfeedback;
            }
            $expout .= $this->write_general_feedback($question, '');
            $expout .= "}\n";
            break;

        case 'multichoice':
            $expout .= $this->write_name($question->name);
            $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
            $expout .= "{\n";
            foreach($question->options->answers as $answer) {
                if ($answer->fraction == 1) {
                    $answertext = '=';
                } else if ($answer->fraction == 0) {
                    $answertext = '~';
                } else {
                    $weight = $answer->fraction * 100;
                    $answertext = '~%' . $weight . '%';
                }
                $expout .= "\t" . $answertext . $this->write_questiontext($answer->answer,
                            $answer->answerformat, $question->questiontextformat);
                if ($answer->feedback != '') {
                    $expout .= '#' . $this->write_questiontext($answer->feedback,
                            $answer->feedbackformat, $question->questiontextformat);
                }
                $expout .= "\n";
            }
            $expout .= $this->write_general_feedback($question);
            $expout .= "}\n";
            break;

        case 'shortanswer':
            $expout .= $this->write_name($question->name);
            $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
            $expout .= "{\n";
            foreach($question->options->answers as $answer) {
                $weight = 100 * $answer->fraction;
                $expout .= "\t=%" . $weight . '%' . $this->repchar($answer->answer) .
                        '#' . $this->write_questiontext($answer->feedback,
                            $answer->feedbackformat, $question->questiontextformat) . "\n";
            }
            $expout .= $this->write_general_feedback($question);
            $expout .= "}\n";
            break;

        case 'numerical':
            $expout .= $this->write_name($question->name);
            $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
            $expout .= "{#\n";
            foreach ($question->options->answers as $answer) {
                if ($answer->answer != '' && $answer->answer != '*') {
                    $weight = 100 * $answer->fraction;
                    $expout .= "\t=%" . $weight . '%' . $answer->answer . ':' .
                            (float)$answer->tolerance . '#' . $this->write_questiontext($answer->feedback,
                            $answer->feedbackformat, $question->questiontextformat) . "\n";
                } else {
                    $expout .= "\t~#" . $this->write_questiontext($answer->feedback,
                            $answer->feedbackformat, $question->questiontextformat) . "\n";
                }
            }
            $expout .= $this->write_general_feedback($question);
            $expout .= "}\n";
            break;

        case 'match':
            $expout .= $this->write_name($question->name);
            $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
            $expout .= "{\n";
            foreach($question->options->subquestions as $subquestion) {
                $expout .= "\t=" . $this->write_questiontext($subquestion->questiontext,
                        $subquestion->questiontextformat, $question->questiontextformat) .
                        ' -> ' . $this->repchar($subquestion->answertext) . "\n";
            }
            $expout .= $this->write_general_feedback($question);
            $expout .= "}\n";
            break;

        default:
            // Check for plugins
            if ($out = $this->try_exporting_using_qtypes($question->qtype, $question)) {
                $expout .= $out;
            } else {
                $expout .= "Question type $question->qtype is not supported\n";
                echo $OUTPUT->notification(get_string('nohandler', 'qformat_gift',
                        question_bank::get_qtype_name($question->qtype)));
            }
        }

        // Add empty line to delimit questions
        $expout .= "\n";
        return $expout;
    }