Example #1
0
 protected function display_content($question, $rowclasses)
 {
     $text = question_rewrite_question_preview_urls($question->questiontext, $question->id, $question->contextid, 'question', 'questiontext', $question->id, $question->contextid, 'core_question');
     $text = format_text($text, $question->questiontextformat, $this->formatoptions);
     if ($text == '') {
         $text = ' ';
     }
     echo $text;
 }
Example #2
0
 protected function writequestion($question)
 {
     global $OUTPUT;
     // Turns question into string.
     // Question reflects database fields for general question and specific to type.
     // If a category switch, just ignore.
     if ($question->qtype == 'category') {
         return '';
     }
     // Initial string.
     $expout = "";
     $id = $question->id;
     // Add comment and div tags.
     $expout .= "<!-- question: {$id}  name: {$question->name} -->\n";
     $expout .= "<div class=\"question\">\n";
     // Add header.
     $expout .= "<h3>{$question->name}</h3>\n";
     // Format and add the question text.
     $text = question_rewrite_question_preview_urls($question->questiontext, $question->id, $question->contextid, 'question', 'questiontext', $question->id, $question->contextid, 'qformat_xhtml');
     $expout .= '<p class="questiontext">' . format_text($text, $question->questiontextformat, array('noclean' => true)) . "</p>\n";
     // Selection depends on question type.
     switch ($question->qtype) {
         case 'truefalse':
             $sttrue = get_string('true', 'qtype_truefalse');
             $stfalse = get_string('false', 'qtype_truefalse');
             $expout .= "<ul class=\"truefalse\">\n";
             $expout .= "  <li><input name=\"quest_{$id}\" type=\"radio\" value=\"{$sttrue}\" />{$sttrue}</li>\n";
             $expout .= "  <li><input name=\"quest_{$id}\" type=\"radio\" value=\"{$stfalse}\" />{$stfalse}</li>\n";
             $expout .= "</ul>\n";
             break;
         case 'multichoice':
             $expout .= "<ul class=\"multichoice\">\n";
             foreach ($question->options->answers as $answer) {
                 $answertext = $this->repchar($answer->answer);
                 if ($question->options->single) {
                     $expout .= "  <li><input name=\"quest_{$id}\" type=\"radio\" value=\"" . s($answertext) . "\" />{$answertext}</li>\n";
                 } else {
                     $expout .= "  <li><input name=\"quest_{$id}\" type=\"checkbox\" value=\"" . s($answertext) . "\" />{$answertext}</li>\n";
                 }
             }
             $expout .= "</ul>\n";
             break;
         case 'shortanswer':
             $expout .= html_writer::start_tag('ul', array('class' => 'shortanswer'));
             $expout .= html_writer::start_tag('li');
             $expout .= html_writer::label(get_string('answer'), 'quest_' . $id, false, array('class' => 'accesshide'));
             $expout .= html_writer::empty_tag('input', array('id' => "quest_{$id}", 'name' => "quest_{$id}", 'type' => 'text'));
             $expout .= html_writer::end_tag('li');
             $expout .= html_writer::end_tag('ul');
             break;
         case 'numerical':
             $expout .= html_writer::start_tag('ul', array('class' => 'numerical'));
             $expout .= html_writer::start_tag('li');
             $expout .= html_writer::label(get_string('answer'), 'quest_' . $id, false, array('class' => 'accesshide'));
             $expout .= html_writer::empty_tag('input', array('id' => "quest_{$id}", 'name' => "quest_{$id}", 'type' => 'text'));
             $expout .= html_writer::end_tag('li');
             $expout .= html_writer::end_tag('ul');
             break;
         case 'match':
             $expout .= html_writer::start_tag('ul', array('class' => 'match'));
             // Build answer list.
             $answerlist = array();
             foreach ($question->options->subquestions as $subquestion) {
                 $answerlist[] = $this->repchar($subquestion->answertext);
             }
             shuffle($answerlist);
             // Random display order.
             // Build select options.
             $selectoptions = array();
             foreach ($answerlist as $ans) {
                 $selectoptions[s($ans)] = s($ans);
             }
             // Display.
             $option = 0;
             foreach ($question->options->subquestions as $subquestion) {
                 // Build drop down for answers.
                 $questiontext = $this->repchar($subquestion->questiontext);
                 if ($questiontext != '') {
                     $dropdown = html_writer::label(get_string('answer', 'qtype_match', $option + 1), 'quest_' . $id . '_' . $option, false, array('class' => 'accesshide'));
                     $dropdown .= html_writer::select($selectoptions, "quest_{$id}_{$option}", '', false, array('id' => "quest_{$id}_{$option}"));
                     $expout .= html_writer::tag('li', $questiontext);
                     $expout .= $dropdown;
                     $option++;
                 }
             }
             $expout .= html_writer::end_tag('ul');
             break;
         case 'description':
             break;
         case 'multianswer':
         default:
             $expout .= "<!-- export of {$question->qtype} type is not supported  -->\n";
     }
     // Close off div.
     $expout .= "</div>\n\n\n";
     return $expout;
 }
/**
 * Rewrite the PLUGINFILE urls in the questiontext, when viewing the question
 * text outside an attempt (for example, in the question bank listing or in the
 * quiz statistics report).
 *
 * @param string $questiontext the question text.
 * @param int $contextid the context the text is being displayed in.
 * @param string $component component
 * @param array $questionid the question id
 * @param array $options e.g. forcedownload. Passed to file_rewrite_pluginfile_urls.
 * @return string $questiontext with URLs rewritten.
 * @deprecated since Moodle 2.6
 */
function question_rewrite_questiontext_preview_urls($questiontext, $contextid, $component, $questionid, $options = null)
{
    global $DB;
    debugging('question_rewrite_questiontext_preview_urls has been deprecated. ' . 'Please use question_rewrite_question_preview_urls instead', DEBUG_DEVELOPER);
    $questioncontextid = $DB->get_field_sql('
            SELECT qc.contextid
              FROM {question} q
              JOIN {question_categories} qc ON qc.id = q.category
             WHERE q.id = :id', array('id' => $questionid), MUST_EXIST);
    return question_rewrite_question_preview_urls($questiontext, $questionid, $questioncontextid, 'question', 'questiontext', $questionid, $contextid, $component, $options);
}
Example #4
0
 /**
  * Output question text in a box with urls appropriate for a preview of the question.
  *
  * @param object $question question data.
  * @return string HTML of question text, ready for display.
  */
 protected function render_question_text($question)
 {
     global $OUTPUT;
     $text = question_rewrite_question_preview_urls($question->questiontext, $question->id, $question->contextid, 'question', 'questiontext', $question->id, $this->context->id, 'quiz_statistics');
     return $OUTPUT->box(format_text($text, $question->questiontextformat, array('noclean' => true, 'para' => false, 'overflowdiv' => true)), 'questiontext boxaligncenter generalbox boxwidthnormal mdl-align');
 }
/**
 * Prints a preview for a question in an offlinequiz to Stdout.
 *
 * @param object $question
 * @param array $choiceorder
 * @param int $number
 * @param object $context
 */
function offlinequiz_print_question_preview($question, $choiceorder, $number, $context, $page)
{
    global $CFG, $DB;
    require_once $CFG->dirroot . '/filter/mathjaxloader/filter.php';
    $letterstr = 'abcdefghijklmnopqrstuvwxyz';
    echo '<div id="q' . $question->id . '" class="preview">
            <div class="question">
              <span class="number">';
    if ($question->qtype != 'description') {
        echo $number . ')&nbsp;&nbsp;';
    }
    echo '    </span>';
    $text = question_rewrite_question_preview_urls($question->questiontext, $question->id, $question->contextid, 'question', 'questiontext', $question->id, $context->id, 'offlinequiz');
    // Remove leading paragraph tags because the cause a line break after the question number.
    $text = preg_replace('!^<p>!i', '', $text);
    // Filter only for tex formulas.
    $texfilter = null;
    $mathjaxfilter = null;
    $filters = filter_get_active_in_context($context);
    if (array_key_exists('mathjaxloader', $filters)) {
        $mathjaxfilter = new filter_mathjaxloader($context, array());
        $mathjaxfilter->setup($page, $context);
    }
    if (array_key_exists('tex', $filters)) {
        $texfilter = new filter_tex($context, array());
    }
    if ($mathjaxfilter) {
        $text = $mathjaxfilter->filter($text);
        if ($question->qtype != 'description') {
            foreach ($choiceorder as $key => $answer) {
                $question->options->answers[$answer]->answer = $mathjaxfilter->filter($question->options->answers[$answer]->answer);
            }
        }
    } else {
        if ($texfilter) {
            $text = $texfilter->filter($text);
            if ($question->qtype != 'description') {
                foreach ($choiceorder as $key => $answer) {
                    $question->options->answers[$answer]->answer = $texfilter->filter($question->options->answers[$answer]->answer);
                }
            }
        }
    }
    echo $text;
    echo '  </div>';
    if ($question->qtype != 'description') {
        echo '  <div class="grade">';
        echo '(' . get_string('marks', 'quiz') . ': ' . ($question->maxmark + 0) . ')';
        echo '  </div>';
        foreach ($choiceorder as $key => $answer) {
            $answertext = $question->options->answers[$answer]->answer;
            // Remove all HTML comments (typically from MS Office).
            $answertext = preg_replace("/<!--.*?--\\s*>/ms", "", $answertext);
            // Remove all paragraph tags because they mess up the layout.
            $answertext = preg_replace("/<p[^>]*>/ms", "", $answertext);
            $answertext = preg_replace("/<\\/p[^>]*>/ms", "", $answertext);
            echo "<div class=\"answer\">{$letterstr[$key]})&nbsp;&nbsp;";
            echo $answertext;
            echo "</div>";
        }
    }
    echo "</div>";
}
 protected function writequestion($question)
 {
     global $OUTPUT;
     // Turns question into string.
     // Question reflects database fields for general question and specific to type.
     // If a category switch, just ignore.
     if ($question->qtype == 'category') {
         return '';
     }
     // Initial string.
     $expout = "";
     $id = $question->id;
     $ishidden = '';
     if ($question->hidden) {
         $ishidden = 'hiddenquestion';
     }
     // Add comment and div tags.
     $expout .= "<!-- question: {$id}  name: {$question->name} -->\n";
     $expout .= "<div class=\"question {$ishidden}\">\n";
     // Add header.
     $expout .= "<h3>{$question->name}</h3>\n";
     $defaultmark = preg_replace('/[0|\\.]*$/', '', 'Default value: ' . $question->defaultmark);
     $expout .= '<p class="">' . $defaultmark . '</p>';
     // Format and add the question text.
     $text = question_rewrite_question_preview_urls($question->questiontext, $question->id, $question->contextid, 'question', 'questiontext', $question->id, $question->contextid, 'qformat_xhtml');
     $expout .= '<p class="questiontext">' . format_text($text, $question->questiontextformat, array('noclean' => true)) . "</p>\n";
     // Selection depends on question type.
     switch ($question->qtype) {
         case TRUEFALSE:
         case 'truefalse':
             $expout .= "<ul class=\"truefalse\">\n";
             foreach ($question->options->answers as $answer) {
                 $ans_text = $this->repchar($answer->answer);
                 if ($answer->fraction == 1) {
                     $correctnessclass = 'correctness_correct';
                 } elseif ($answer->fraction > 0) {
                     $correctnessclass = 'correctness_partial';
                 } else {
                     $correctnessclass = 'correctness_incorrect';
                 }
                 $expout .= "  <li class=\"{$correctnessclass}\">{$ans_text}</li>\n";
             }
             $expout .= "</ul>\n";
             break;
         case MULTICHOICE:
         case 'multichoice':
             $expout .= "<ul class=\"multichoice\">\n";
             foreach ($question->options->answers as $answer) {
                 $ans_text = $this->repchar($answer->answer);
                 if ($answer->fraction == 1) {
                     $correctnessclass = 'correctness_correct';
                 } elseif ($answer->fraction > 0) {
                     $correctnessclass = 'correctness_partial';
                 } else {
                     $correctnessclass = 'correctness_incorrect';
                 }
                 $expout .= "  <li class=\"{$correctnessclass}\">{$ans_text}</li>\n";
             }
             $expout .= "</ul>\n";
             break;
         case SHORTANSWER:
         case 'shortanswer':
             $expout .= "<ul class=\"shortanswer\">\n";
             $expout .= "  <li><input name=\"quest_{$id}\" type=\"text\" /></li>\n";
             $expout .= "</ul>\n";
             break;
         case NUMERICAL:
         case 'numerical':
             $expout .= "<ul class=\"numerical\">\n";
             $expout .= "  <li><input name=\"quest_{$id}\" type=\"text\" /></li>\n";
             $expout .= "</ul>\n";
             break;
         case MATCH:
         case 'match':
             $expout .= "<ul class=\"match\">\n";
             // Build answer list.
             $ans_list = array();
             foreach ($question->options->subquestions as $subquestion) {
                 $ans_list[] = $this->repchar($subquestion->answertext);
             }
             shuffle($ans_list);
             // Random display order.
             // Build drop down for answers.
             $dropdown = "<select name=\"quest_{$id}\">\n";
             foreach ($ans_list as $ans) {
                 $dropdown .= "<option value=\"" . s($ans) . "\">" . s($ans) . "</option>\n";
             }
             $dropdown .= "</select>\n";
             // Finally display.
             foreach ($question->options->subquestions as $subquestion) {
                 $quest_text = $this->repchar($subquestion->questiontext);
                 $expout .= "  <li>{$quest_text}</li>\n";
                 $expout .= $dropdown;
             }
             $expout .= "</ul>\n";
             break;
         case DESCRIPTION:
         case 'description':
             break;
         case MULTIANSWER:
         case 'multianswer':
             $expout .= "<!-- CLOZE type is not supported  -->\n";
             break;
         default:
             echo $OUTPUT->notification("No handler for qtype {$question->qtype}");
     }
     // Close off div.
     $expout .= "</div>\n\n\n";
     return $expout;
 }
 /**
  * @param object $question question data.
  * @return string HTML of question text, ready for display.
  */
 protected function render_question_text_plain($question, $showimages = true)
 {
     global $OUTPUT;
     if ($showimages) {
         $text = question_rewrite_question_preview_urls($question->questiontext, $question->id, $question->contextid, 'question', 'questiontext', $question->id, $this->context->id, 'quiz_statistics');
     } else {
         $text = $question->questiontext;
     }
     $questiontext = question_utils::to_plain_text($text, $question->questiontextformat, array('noclean' => true, 'para' => false, 'overflowdiv' => true));
     return '&nbsp;&nbsp;&nbsp;' . $questiontext;
 }