Example #1
0
 function create_session_and_responses(&$question, &$state, $cmoptions, $attempt)
 {
     global $QTYPE_EXCLUDE_FROM_RANDOM;
     // Choose a random question from the category:
     // We need to make sure that no question is used more than once in the
     // quiz. Therfore the following need to be excluded:
     // 1. All questions that are explicitly assigned to the quiz
     // 2. All random questions
     // 3. All questions that are already chosen by an other random question
     // 4. Deleted questions
     if (!isset($cmoptions->questionsinuse)) {
         $cmoptions->questionsinuse = $attempt->layout;
     }
     if (!isset($this->catrandoms[$question->category][$question->questiontext])) {
         // Need to fetch random questions from category $question->category"
         // (Note: $this refers to the questiontype, not the question.)
         global $CFG;
         if ($question->questiontext == "1") {
             // recurse into subcategories
             $categorylist = question_categorylist($question->category);
         } else {
             $categorylist = $question->category;
         }
         if ($catrandoms = get_records_select('question', "category IN ({$categorylist})\n                         AND parent = '0'\n                         AND hidden = '0'\n                         AND id NOT IN ({$cmoptions->questionsinuse})\n                         AND qtype NOT IN ({$QTYPE_EXCLUDE_FROM_RANDOM})", '', 'id')) {
             $this->catrandoms[$question->category][$question->questiontext] = draw_rand_array($catrandoms, count($catrandoms));
             // from bug 1889
         } else {
             $this->catrandoms[$question->category][$question->questiontext] = array();
         }
     }
     while ($wrappedquestion = array_pop($this->catrandoms[$question->category][$question->questiontext])) {
         if (!ereg("(^|,){$wrappedquestion->id}(,|\$)", $cmoptions->questionsinuse)) {
             /// $randomquestion is not in use and will therefore be used
             /// as the randomquestion here...
             $wrappedquestion = get_record('question', 'id', $wrappedquestion->id);
             global $QTYPES;
             $QTYPES[$wrappedquestion->qtype]->get_question_options($wrappedquestion);
             $QTYPES[$wrappedquestion->qtype]->create_session_and_responses($wrappedquestion, $state, $cmoptions, $attempt);
             $wrappedquestion->name_prefix = $question->name_prefix;
             $wrappedquestion->maxgrade = $question->maxgrade;
             $cmoptions->questionsinuse .= ",{$wrappedquestion->id}";
             $state->options->question =& $wrappedquestion;
             return true;
         }
     }
     $question->questiontext = '<span class="notifyproblem">' . get_string('toomanyrandom', 'quiz') . '</span>';
     $question->qtype = 'description';
     $state->responses = array('' => '');
     return true;
 }
 * @package mod-flashcard
 * @category mod
 * @author Gustav Delius
 * @contributors Valery Fremaux
 */
// Security
if (!defined('MOODLE_INTERNAL')) {
    print_error('errorillegaldirectaccess', 'flashcard');
}
echo $out;
$subquestions = $DB->get_records('flashcard_deckdata', array('flashcardid' => $flashcard->id));
if (empty($subquestions)) {
    notice(get_string('nosubquestions', 'flashcard'));
    return;
}
$subquestions = draw_rand_array($subquestions, count($subquestions));
?>

<script language="javascript">
    //<![CDATA[
    currentitem = 0;
    maxitems = <?php 
echo count($subquestions);
?>
;
    remaining = maxitems;

    var qtype = "<?php 
echo $flashcard->questionsmediatype;
?>
";
Example #3
0
 function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options)
 {
     global $CFG, $OUTPUT;
     $context = $this->get_context_by_category_id($question->category);
     $subquestions = $state->options->subquestions;
     $correctanswers = $this->get_correct_responses($question, $state);
     $nameprefix = $question->name_prefix;
     $answers = array();
     // Answer choices formatted ready for output.
     $allanswers = array();
     // This and the next used to detect identical answers
     $answerids = array();
     // and adjust ids.
     $responses =& $state->responses;
     // Prepare a list of answers, removing duplicates.
     foreach ($subquestions as $subquestion) {
         foreach ($subquestion->options->answers as $ans) {
             $allanswers[$ans->id] = $ans->answer;
             if (!in_array($ans->answer, $answers)) {
                 $answers[$ans->id] = strip_tags(format_string($ans->answer, false));
                 $answerids[$ans->answer] = $ans->id;
             }
         }
     }
     // Fix up the ids of any responses that point the the eliminated duplicates.
     foreach ($responses as $subquestionid => $ignored) {
         if ($responses[$subquestionid]) {
             $responses[$subquestionid] = $answerids[$allanswers[$responses[$subquestionid]]];
         }
     }
     foreach ($correctanswers as $subquestionid => $ignored) {
         $correctanswers[$subquestionid] = $answerids[$allanswers[$correctanswers[$subquestionid]]];
     }
     // Shuffle the answers
     $answers = draw_rand_array($answers, count($answers));
     // Print formulation
     $questiontext = $this->format_text($question->questiontext, $question->questiontextformat, $cmoptions);
     // Print the input controls
     foreach ($subquestions as $key => $subquestion) {
         if ($subquestion->questiontext !== '' && !is_null($subquestion->questiontext)) {
             // Subquestion text:
             $a = new stdClass();
             $text = $this->format_subquestion_text($subquestion, $state, $context);
             $a->text = $this->format_text($text, $subquestion->questiontextformat, $cmoptions);
             // Drop-down list:
             $menuname = $nameprefix . $subquestion->id;
             $response = isset($state->responses[$subquestion->id]) ? $state->responses[$subquestion->id] : '0';
             $a->class = ' ';
             $a->feedbackimg = ' ';
             if ($options->readonly and $options->correct_responses) {
                 if (isset($correctanswers[$subquestion->id]) and $correctanswers[$subquestion->id] == $response) {
                     $correctresponse = 1;
                 } else {
                     $correctresponse = 0;
                 }
                 if ($options->feedback && $response) {
                     $a->class = question_get_feedback_class($correctresponse);
                     $a->feedbackimg = question_get_feedback_image($correctresponse);
                 }
             }
             $attributes = array();
             $attributes['disabled'] = $options->readonly ? 'disabled' : null;
             $a->control = html_writer::select($answers, $menuname, $response, array('' => 'choosedots'), $attributes);
             // Neither the editing interface or the database allow to provide
             // fedback for this question type.
             // However (as was pointed out in bug bug 3294) the randomsamatch
             // type which reuses this method can have feedback defined for
             // the wrapped shortanswer questions.
             //if ($options->feedback
             // && !empty($subquestion->options->answers[$responses[$key]]->feedback)) {
             //    print_comment($subquestion->options->answers[$responses[$key]]->feedback);
             //}
             $anss[] = $a;
         }
     }
     include "{$CFG->dirroot}/question/type/match/display.html";
 }
Example #4
0
 function create_session_and_responses(&$question, &$state, $cmoptions, $attempt)
 {
     // Choose a random shortanswer question from the category:
     // We need to make sure that no question is used more than once in the
     // quiz. Therfore the following need to be excluded:
     // 1. All questions that are explicitly assigned to the quiz
     // 2. All random questions
     // 3. All questions that are already chosen by an other random question
     global $QTYPES;
     if (!isset($cmoptions->questionsinuse)) {
         $cmoptions->questionsinuse = $cmoptions->questions;
     }
     if ($question->options->subcats) {
         // recurse into subcategories
         $categorylist = question_categorylist($question->category);
     } else {
         $categorylist = $question->category;
     }
     $saquestions = $this->get_sa_candidates($categorylist, $cmoptions->questionsinuse);
     $count = count($saquestions);
     $wanted = $question->options->choose;
     $errorstr = '';
     if ($count < $wanted && isteacherinanycourse()) {
         if ($count >= 2) {
             $errorstr = "Error: could not get enough Short-Answer questions!\n                 Got {$count} Short-Answer questions, but wanted {$wanted}.\n                 Reducing number to choose from to {$count}!";
             $wanted = $question->options->choose = $count;
         } else {
             $errorstr = "Error: could not get enough Short-Answer questions!\n                 This can happen if all available Short-Answer questions are already\n                 taken up by other Random questions or Random Short-Answer question.\n                 Another possible cause for this error is that Short-Answer\n                 questions were deleted after this Random Short-Answer question was\n                 created.";
         }
         notify($errorstr);
         $errorstr = '<span class="notifyproblem">' . $errorstr . '</span>';
     }
     if ($count < $wanted) {
         $question->questiontext = "{$errorstr}<br /><br />Insufficient selection options are\n             available for this question, therefore it is not available in  this\n             quiz. Please inform your teacher.";
         // Treat this as a description from this point on
         $question->qtype = DESCRIPTION;
         return true;
     }
     $saquestions = draw_rand_array($saquestions, $question->options->choose);
     // from bug 1889
     foreach ($saquestions as $key => $wrappedquestion) {
         if (!$QTYPES[$wrappedquestion->qtype]->get_question_options($wrappedquestion)) {
             return false;
         }
         // Now we overwrite the $question->options->answers field to only
         // *one* (the first) correct answer. This loop can be deleted to
         // take all answers into account (i.e. put them all into the
         // drop-down menu.
         $foundcorrect = false;
         foreach ($wrappedquestion->options->answers as $answer) {
             if ($foundcorrect || $answer->fraction != 1.0) {
                 unset($wrappedquestion->options->answers[$answer->id]);
             } else {
                 if (!$foundcorrect) {
                     $foundcorrect = true;
                 }
             }
         }
         if (!$QTYPES[$wrappedquestion->qtype]->create_session_and_responses($wrappedquestion, $state, $cmoptions, $attempt)) {
             return false;
         }
         $wrappedquestion->name_prefix = $question->name_prefix;
         $wrappedquestion->maxgrade = $question->maxgrade;
         $cmoptions->questionsinuse .= ",{$wrappedquestion->id}";
         $state->options->subquestions[$key] = clone $wrappedquestion;
     }
     // Shuffle the answers (Do this always because this is a random question type)
     $subquestionids = array_values(array_map(create_function('$val', 'return $val->id;'), $state->options->subquestions));
     $subquestionids = swapshuffle($subquestionids);
     // Create empty responses
     foreach ($subquestionids as $val) {
         $state->responses[$val] = '';
     }
     return true;
 }
Example #5
0
 public function create_session_and_responses(&$question, &$state, $cmoptions, $attempt)
 {
     // Choose a random shortanswer question from the category:
     // We need to make sure that no question is used more than once in the
     // quiz. Therfore the following need to be excluded:
     // 1. All questions that are explicitly assigned to the quiz
     // 2. All random questions
     // 3. All questions that are already chosen by an other random question.
     global $QTYPES, $OUTPUT, $USER;
     if (!isset($cmoptions->questionsinuse)) {
         $cmoptions->questionsinuse = $cmoptions->questions;
     }
     if ($question->options->subcats) {
         // Recurse into subcategories.
         $categorylist = question_categorylist($question->category);
     } else {
         $categorylist = array($question->category);
     }
     $saquestions = $this->get_sa_candidates($categorylist, $cmoptions->questionsinuse);
     $count = count($saquestions);
     $wanted = $question->options->choose;
     if ($count < $wanted) {
         $question->questiontext = "Insufficient selection options are\n                available for this question, therefore it is not available in  this\n                quiz. Please inform your teacher.";
         // Treat this as a description from this point on.
         $question->qtype = 'description';
         return true;
     }
     $saquestions = draw_rand_array($saquestions, $question->options->choose);
     // From bug 1889.
     foreach ($saquestions as $key => $wrappedquestion) {
         if (!$QTYPES[$wrappedquestion->qtype]->get_question_options($wrappedquestion)) {
             return false;
         }
         // Now we overwrite the $question->options->answers field to only
         // *one* (the first) correct answer. This loop can be deleted to
         // take all answers into account (i.e. put them all into the
         // drop-down menu.
         $foundcorrect = false;
         foreach ($wrappedquestion->options->answers as $answer) {
             if ($foundcorrect || $answer->fraction != 1.0) {
                 unset($wrappedquestion->options->answers[$answer->id]);
             } else {
                 if (!$foundcorrect) {
                     $foundcorrect = true;
                 }
             }
         }
         if (!$QTYPES[$wrappedquestion->qtype]->create_session_and_responses($wrappedquestion, $state, $cmoptions, $attempt)) {
             return false;
         }
         $wrappedquestion->name_prefix = $question->name_prefix;
         $wrappedquestion->maxgrade = $question->maxgrade;
         $cmoptions->questionsinuse .= ",{$wrappedquestion->id}";
         $state->options->subquestions[$key] = clone $wrappedquestion;
     }
     // Shuffle the answers (Do this always because this is a random question type).
     $subquestionids = array_values(array_map(create_function('$val', 'return $val->id;'), $state->options->subquestions));
     $subquestionids = swapshuffle($subquestionids);
     // Create empty responses.
     foreach ($subquestionids as $val) {
         $state->responses[$val] = '';
     }
     return true;
 }
 function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options)
 {
     global $CFG, $USER;
     $subquestions = $state->options->subquestions;
     $correctanswers = $this->get_correct_responses($question, $state);
     $nameprefix = $question->name_prefix;
     $answers = array();
     $allanswers = array();
     $answerids = array();
     $responses =& $state->responses;
     // Check browser version to see if YUI is supported properly.
     // This is similar to ajaxenabled() from lib/ajax/ajaxlib.php,
     // except it doesn't check the site-wide AJAX settings.
     $fallbackonly = false;
     $ie = check_browser_version('MSIE', 6.0);
     $ff = check_browser_version('Gecko', 20051106);
     $op = check_browser_version('Opera', 9.0);
     $sa = check_browser_version('Safari', 412);
     if (!$ie && !$ff && !$op && !$sa or !empty($USER->screenreader)) {
         $fallbackonly = true;
     }
     // Prepare a list of answers, removing duplicates.
     foreach ($subquestions as $subquestion) {
         foreach ($subquestion->options->answers as $ans) {
             $allanswers[$ans->id] = $this->format_text($ans->answer, $question->questiontextformat, $cmoptions);
             if (!in_array($allanswers[$ans->id], $answers)) {
                 $ans->answer = $allanswers[$ans->id];
                 $answers[$ans->id] = $ans->answer;
                 $answerids[$ans->answer] = $ans->id;
             }
         }
     }
     // Fix up the ids of any responses that point the the eliminated duplicates.
     foreach ($responses as $subquestionid => $ignored) {
         if ($responses[$subquestionid]) {
             $responses[$subquestionid] = $answerids[$allanswers[$responses[$subquestionid]]];
         }
     }
     foreach ($correctanswers as $subquestionid => $ignored) {
         $correctanswers[$subquestionid] = $answerids[$allanswers[$correctanswers[$subquestionid]]];
     }
     // Shuffle the answers
     $answers = draw_rand_array($answers, count($answers));
     // Print formulation
     $questiontext = $this->format_text($question->questiontext, $question->questiontextformat, $cmoptions);
     $image = get_question_image($question);
     // Javascript Array initialization of list ids
     $elems = array();
     foreach ($subquestions as $subquestion) {
         if ($subquestion->questiontext) {
             $elems[] = '"' . $subquestion->id . '"';
         }
     }
     $questionsarraystring = 'Array(' . implode(',', $elems) . ')';
     $elems = array();
     foreach ($answers as $key => $answer) {
         $elems[] = '"' . $key . '"';
     }
     $answersarraystring = 'Array(' . implode(',', $elems) . ')';
     $elems = array();
     foreach ($subquestions as $subquestion) {
         if ($subquestion->questiontext) {
             $elems[] = '"' . $responses[$subquestion->id] . '"';
         }
     }
     $responsesarraystring = 'Array(' . implode(',', $elems) . ')';
     // Print the input controls
     foreach ($subquestions as $key => $subquestion) {
         if ($subquestion->questiontext) {
             // Subquestion text:
             $a = new stdClass();
             $a->id = $subquestion->id;
             $a->text = $this->format_text($subquestion->questiontext, $question->questiontextformat, $cmoptions);
             // Drop-down list:
             $menuname = $nameprefix . $subquestion->id;
             $response = isset($state->responses[$subquestion->id]) ? $state->responses[$subquestion->id] : '0';
             $a->class = ' ';
             $a->feedbackimg = ' ';
             if ($options->readonly and $options->correct_responses) {
                 if (isset($correctanswers[$subquestion->id]) and $correctanswers[$subquestion->id] == $response) {
                     $correctresponse = 1;
                 } else {
                     $correctresponse = 0;
                 }
                 if ($options->feedback && $response) {
                     $a->class = question_get_feedback_class($correctresponse);
                     $a->feedbackimg = question_get_feedback_image($correctresponse);
                 }
             }
             if (preg_match('/<img/', $a->feedbackimg)) {
                 preg_match('/src="([^"]*)"/', $a->feedbackimg, $matches);
                 $a->feedbackimgsrc = $matches[1];
                 preg_match('/alt="([^"]*)"/', $a->feedbackimg, $matches);
                 $a->feedbackimgalt = $matches[1];
                 preg_match('/class="([^"]*)"/', $a->feedbackimg, $matches);
                 $a->feedbackimgclass = $matches[1];
             }
             $a->control = choose_from_menu($answers, $menuname, $response, 'choose', '', 0, true, $options->readonly);
             // Neither the editing interface or the database allow to provide
             // fedback for this question type.
             // However (as was pointed out in bug bug 3294) the randomsamatch
             // type which reuses this method can have feedback defined for
             // the wrapped shortanswer questions.
             //if ($options->feedback
             // && !empty($subquestion->options->answers[$responses[$key]]->feedback)) {
             //    print_comment($subquestion->options->answers[$responses[$key]]->feedback);
             //}
             $anss[] = $a;
         }
     }
     $dragstring = get_string('draganswerhere', 'qtype_ddmatch');
     include "{$CFG->dirroot}/question/type/ddmatch/display.html";
 }
Example #7
0
 function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options)
 {
     global $CFG;
     $subquestions = $state->options->subquestions;
     $correctanswers = $this->get_correct_responses($question, $state);
     $nameprefix = $question->name_prefix;
     $answers = array();
     $allanswers = array();
     $answerids = array();
     $responses =& $state->responses;
     // Prepare a list of answers, removing duplicates.
     foreach ($subquestions as $subquestion) {
         foreach ($subquestion->options->answers as $ans) {
             $allanswers[$ans->id] = $ans->answer;
             if (!in_array($ans->answer, $answers)) {
                 $answers[$ans->id] = $ans->answer;
                 $answerids[$ans->answer] = $ans->id;
             }
         }
     }
     // Fix up the ids of any responses that point the the eliminated duplicates.
     foreach ($responses as $subquestionid => $ignored) {
         if ($responses[$subquestionid]) {
             $responses[$subquestionid] = $answerids[$allanswers[$responses[$subquestionid]]];
         }
     }
     foreach ($correctanswers as $subquestionid => $ignored) {
         $correctanswers[$subquestionid] = $answerids[$allanswers[$correctanswers[$subquestionid]]];
     }
     // Shuffle the answers
     $answers = draw_rand_array($answers, count($answers));
     // Print formulation
     $questiontext = $this->format_text($question->questiontext, $question->questiontextformat, $cmoptions);
     $image = get_question_image($question);
     // Print the input controls
     foreach ($subquestions as $key => $subquestion) {
         if ($subquestion->questiontext != '') {
             // Subquestion text:
             $a = new stdClass();
             $a->text = $this->format_text($subquestion->questiontext, $question->questiontextformat, $cmoptions);
             // Drop-down list:
             $menuname = $nameprefix . $subquestion->id;
             $response = isset($state->responses[$subquestion->id]) ? $state->responses[$subquestion->id] : '0';
             $a->class = ' ';
             $a->feedbackimg = ' ';
             if ($options->readonly and $options->correct_responses) {
                 if (isset($correctanswers[$subquestion->id]) and $correctanswers[$subquestion->id] == $response) {
                     $correctresponse = 1;
                 } else {
                     $correctresponse = 0;
                 }
                 if ($options->feedback && $response) {
                     $a->class = question_get_feedback_class($correctresponse);
                     $a->feedbackimg = question_get_feedback_image($correctresponse);
                 }
             }
             $a->control = choose_from_menu($answers, $menuname, $response, 'choose', '', 0, true, $options->readonly);
             // Neither the editing interface or the database allow to provide
             // fedback for this question type.
             // However (as was pointed out in bug bug 3294) the randomsamatch
             // type which reuses this method can have feedback defined for
             // the wrapped shortanswer questions.
             //if ($options->feedback
             // && !empty($subquestion->options->answers[$responses[$key]]->feedback)) {
             //    print_comment($subquestion->options->answers[$responses[$key]]->feedback);
             //}
             $anss[] = $a;
         }
     }
     include "{$CFG->dirroot}/question/type/match/display.html";
 }
/**
 * Prints the question body. Since standard moodle question functions set up
 * variables and prints html in the same function, we cannot call these functions.
 * So, the most code of this function (specially the type-specific part) is copied
 * from the standard moodle question types.
 * **/
function wrsqz_print_question_formulation_and_controls($questionType, $dbType, &$question, &$state, &$cmoptions, &$options) {
	//COMMON VARIABLES:
  global $CFG, $QTYPES;
  //1. Question text
  $formatoptions = new stdClass;
  $formatoptions->noclean = true;
  $formatoptions->para = false;

  $questiontext = format_text($question->questiontext, $question->questiontextformat,
                              $formatoptions, $cmoptions->course);

  //2. Question image
  $image = get_question_image($question);

  //3. name & strings
  $inputname = $question->name_prefix;
  $stranswer = get_string("answer", "quiz").': ';

  //4. WIRIS CAS applet
  $wirisCASApplet = '';
  $wirisCASForComputationsEnabled = !empty($question->options->wiris->options['wirisCASForComputations']);
  if($wirisCASForComputationsEnabled){
    $wirisCASContent = '';
    if (!empty($state->responses['wirisCASHidden'])) {
      $wirisCASContent = htmlentities(stripslashes_safe($state->responses['wirisCASHidden']), ENT_QUOTES, 'UTF-8');
      unset($state->responses['wirisCASHidden']);
    }else if(!empty($question->options->wiris->options['hiddenInitialCASValue'])){
      $wirisCASContent = htmlentities(stripslashes_safe($question->options->wiris->options['hiddenInitialCASValue']), ENT_QUOTES, 'UTF-8');
    }
    $wirisCASApplet = wrsqz_wirisCASAppletHTML($inputname, $wirisCASContent, false, 630, 300);
    require_js(array('yui_yahoo', 'yui_dom-event'));
    require_js(array($CFG->wwwroot.'/wiris-quizzes/js/wiris-quizzes.js',$CFG->wwwroot.'/wiris-quizzes/js/constants.js.php'));
  }
  
  //ANSWER FIELDS & FEEDBACK: QUESTION-TYPE specific
  if($questionType == 'essay'){
    // Response
    if (isset($state->responses[''])) {
      $value = stripslashes_safe($state->responses['']);
    }else{
      $value = "";
    }
    //answer
    if($wirisCASForComputationsEnabled){
      $wirisCASForComputations = $question->options->wiris->options['wirisCASForComputations'];
    }else{
      $wirisCASForComputations = '0';
    }
    //replace answer by an applet
    if($wirisCASForComputations == '2'){
      $answer = $wirisCASApplet;
      $answer .= '<input type="hidden" name="'.$inputname.'" id="edit-'.$inputname.'"/>';
      $wirisCASApplet = '';
    }else{
      if (empty($options->readonly)) {
        static $htmleditorused = false;
        $usehtmleditor = can_use_html_editor() && !$htmleditorused;
        // the student needs to type in their answer so print out a text editor
        echo '<!-- yes we can: '.$usehtmleditor.'-->';
        $answer = print_textarea($usehtmleditor, 18, 80, 630, 400, $inputname, $value, $cmoptions->course, true);
      }else{
        //it is read only, so just format the students answer and output it
        $safeformatoptions = new stdClass;
        $safeformatoptions->para = false;
        $answer = format_text($value, FORMAT_MOODLE, $safeformatoptions, $cmoptions->course);
        $answer = '<div class="answerreview">' . $answer . '</div>';
      }
      if($wirisCASForComputationsEnabled){
        $wirisCASApplet = '<tr><td>'.$wirisCASApplet.'</td></tr>';
      }
    }
    
    // feedback handling
    $feedback = '';
    if ($options->feedback && !empty($answers)) {
      foreach ($answers as $answer) {
        $feedback = format_text($answer->feedback, '', $formatoptions, $cmoptions->course);
      }
    }
    
  }else if($questionType == 'match'){
    $subquestions   = $state->options->subquestions;
    $correctanswers = $QTYPES[$question->qtype]->get_correct_responses($question, $state);
    $answers        = array(); // Answer choices formatted ready for output.
    $allanswers     = array(); // This and the next used to detect identical answers
    $answerids      = array(); // and adjust ids.
    $responses      = &$state->responses;

    // Prepare a list of answers, removing duplicates.
    foreach ($subquestions as $subquestion) {
      foreach ($subquestion->options->answers as $ans) {
        $allanswers[$ans->id] = $ans->answer;
        if (!in_array($ans->answer, $answers)) {
          $answers[$ans->id] = strip_tags(format_string($ans->answer, false));
          $answerids[$ans->answer] = $ans->id;
        }
      }
    }
    // Fix up the ids of any responses that point the the eliminated duplicates.
    foreach ($responses as $subquestionid => $ignored) {
      if ($responses[$subquestionid]) {
        $responses[$subquestionid] = $answerids[$allanswers[$responses[$subquestionid]]];
      }
    }
    foreach ($correctanswers as $subquestionid => $ignored) {
      $correctanswers[$subquestionid] = $answerids[$allanswers[$correctanswers[$subquestionid]]];
    }
    // Shuffle the answers
    $answers = draw_rand_array($answers, count($answers));
    // Print the input controls
    foreach ($subquestions as $key => $subquestion) {
      if ($subquestion->questiontext !== '' && !is_null($subquestion->questiontext)) {
        // Subquestion text:
        $a = new stdClass;
        $a->text = format_text($subquestion->questiontext,
        $question->questiontextformat, $cmoptions);
        // Drop-down list:
        $menuname = $inputname.$subquestion->id;
        $response = isset($state->responses[$subquestion->id])
                    ? $state->responses[$subquestion->id] : '0';
        $a->class = ' ';
        $a->feedbackimg = ' ';

        if ($options->readonly and $options->correct_responses) {
          if (isset($correctanswers[$subquestion->id])
          and ($correctanswers[$subquestion->id] == $response)) {
            $correctresponse = 1;
          } else {
            $correctresponse = 0;
          }
          if ($options->feedback && $response) {
            $a->class = question_get_feedback_class($correctresponse);
            $a->feedbackimg = question_get_feedback_image($correctresponse);
          }
        }

        $a->control = choose_from_menu($answers, $menuname, $response, 'choose',
                                       '', 0, true, $options->readonly);
        $anss[] = $a;
      }
    }

  }else if($questionType == 'multianswer'){

  }else if($questionType == 'multichoice'){
  
    $answers = &$question->options->answers;
    $correctanswers = $QTYPES['multichoicewiris']->get_correct_responses($question, $state);
    $readonly = empty($options->readonly) ? '' : 'disabled="disabled"';
    $answerprompt = ($question->options->single) ? get_string('singleanswer', 'quiz') :
                    get_string('multipleanswers', 'quiz');
    // Print each answer in a separate row
    foreach ($state->options->order as $key => $aid) {
      $answer = &$answers[$aid];
      $checked = '';
      $chosen = false;

      if ($question->options->single) {
        $type = 'type="radio"';
        $name   = "name=\"{$question->name_prefix}\"";
        if (isset($state->responses['']) and $aid == $state->responses['']) {
          $checked = 'checked="checked"';
          $chosen = true;
        }
      } else {
        $type = ' type="checkbox" ';
        $name   = "name=\"{$question->name_prefix}{$aid}\"";
        if (isset($state->responses[$aid])) {
          $checked = 'checked="checked"';
          $chosen = true;
        }
      }

      $a = new stdClass;
      $a->id   = $question->name_prefix . $aid;
      $a->class = '';
      $a->feedbackimg = '';

      // Print the control
      $a->control = "<input $readonly id=\"$a->id\" $name $checked $type value=\"$aid\" />";

      if ($options->correct_responses && $answer->fraction > 0) {
        $a->class = question_get_feedback_class(1);
      }
      if (($options->feedback && $chosen) || $options->correct_responses) {
        if ($type == ' type="checkbox" ') {
          $a->feedbackimg = question_get_feedback_image($answer->fraction > 0 ? 1 : 0, $chosen && $options->feedback);
        } else {
          $a->feedbackimg = question_get_feedback_image($answer->fraction, $chosen && $options->feedback);
        }
      }

      // Print the answer text
      $a->text = $QTYPES['multichoicewiris']->number_in_style($key, $question->options->answernumbering) .
                 format_text($answer->answer, FORMAT_MOODLE, $formatoptions, $cmoptions->course);

      // Print feedback if feedback is on
      if (($options->feedback || $options->correct_responses) && $checked) {
        $a->feedback = format_text($answer->feedback, true, $formatoptions, $cmoptions->course);
      } else {
        $a->feedback = '';
      }

      $anss[] = clone($a);
    }

    $feedback = '';
      if ($options->feedback) {
        if ($state->raw_grade >= $question->maxgrade/1.01) {
          $feedback = $question->options->correctfeedback;
        } else if ($state->raw_grade > 0) {
          $feedback = $question->options->partiallycorrectfeedback;
        } else {
          $feedback = $question->options->incorrectfeedback;
        }
        $feedback = format_text($feedback, $question->questiontextformat,
                    $formatoptions, $cmoptions->course);
        }

  

  }else if($questionType == 'shortanswer'){
    $value = '';
    $readonly = (empty($options->readonly)) ? '' : 'readonly="readonly"';
    $feedback = '';
		$class = '';
		$feedbackimg = '';

    if (isset($state->responses['']) && $state->responses[''] != '') {
      $value = s($state->responses[''], true);
    }
    mb_parse_str($question->options->wiris->eqoption, $eqoptionArray);
		$wirisEditorEnabled = (isset($eqoptionArray['editor']) && $eqoptionArray['editor'] == 'true');
		if($wirisCASForComputationsEnabled && $wirisEditorEnabled){
      //copy button
      $wirisCASApplet .= '<input id="'.$inputname.'copy_button" name="'.$inputname.'copy_button" type="button" '.
                         'value="'.wrsqz_get_string('wrsqz_copyresponse')
                         .'" onclick="copyCASSessionToEditor('.$question->id.',\''.$inputname.'\')"/>';
      require_js('yui_connection');
    }
    //$wirisCASForComputationsEnabled = (isset($eqoptionArray['wirisCASForComputations']) && $eqoptionArray['wirisCASForComputations'] == 'true');
		$multipleAnswers = (isset($eqoptionArray['multipleAnswers']) && $eqoptionArray['multipleAnswers'] == 'true');

		if ($multipleAnswers) {
			$formulaGrammar = 'http://' . $CFG->wirisservicehost . ':' . $CFG->wirisserviceport . $CFG->wirisservicepath . '/test/grammars/grammar-assign.txt';
			$formulaGrammarTarget = 'math2';
		}else {
			$formulaGrammar = 'http://' . $CFG->wirisservicehost . ':' . $CFG->wirisserviceport . $CFG->wirisservicepath . '/test/grammars/grammar.txt';
			$formulaGrammarTarget = 'math';
		}
    $wirisEditorApplet = wrsqz_wirisEditorAppletHTML($inputname, $formulaGrammar, $formulaGrammarTarget, $value);
    require_js(array('yui_yahoo', 'yui_dom-event'));
    require_js(array($CFG->wwwroot.'/wiris-quizzes/js/wiris-quizzes.js', $CFG->wwwroot.'/wiris-quizzes/js/constants.js.php'));
    
    // Chose feedback according to current response
		if ($options->feedback) {
			$class = question_get_feedback_class(0);
			$feedbackimg = question_get_feedback_image(0);
			$i = 0;

			foreach ($question->options->answers as $answer) {
				if (wrsqz_testResponse($question, $state, $answer) || (!empty($state->grades) && $state->grades[$i] > 0.0)) {
					$class = question_get_feedback_class($answer->fraction);
					$feedbackimg = question_get_feedback_image($answer->fraction);

					if ($answer->feedback) {
						$feedback = format_text($answer->feedback, true, $formatoptions, $cmoptions->course);
					}
					break;
				}
				++$i;
			}
		}
  }else if($questionType == 'truefalse'){
    $readonly = $options->readonly ? ' disabled="disabled"' : '';
    $answers = &$question->options->answers;
    $trueanswer = &$answers[$question->options->trueanswer];
    $falseanswer = &$answers[$question->options->falseanswer];
    $correctanswer = ($trueanswer->fraction == 1) ? $trueanswer : $falseanswer;

    $trueclass = '';
    $falseclass = '';
    $truefeedbackimg = '';
    $falsefeedbackimg = '';

    // Work out which radio button to select (if any)
    if (isset($state->responses[''])) {
      $response = $state->responses[''];
    } else {
      $response = '';
    }
    $truechecked = ($response == $trueanswer->id) ? ' checked="checked"' : '';
    $falsechecked = ($response == $falseanswer->id) ? ' checked="checked"' : '';

    // Work out visual feedback for answer correctness.
    if ($options->feedback) {
      if ($truechecked) {
         $trueclass = question_get_feedback_class($trueanswer->fraction);
      } else if ($falsechecked) {
         $falseclass = question_get_feedback_class($falseanswer->fraction);
      }
    }
    if ($options->feedback || $options->correct_responses) {
      if (isset($answers[$response])) {
        $truefeedbackimg = question_get_feedback_image($trueanswer->fraction, !empty($truechecked) && $options->feedback);
        $falsefeedbackimg = question_get_feedback_image($falseanswer->fraction, !empty($falsechecked) && $options->feedback);
      }
    }

    $inputname = ' name="'.$question->name_prefix.'" ';
    $trueid    = $question->name_prefix.'true';
    $falseid   = $question->name_prefix.'false';

    $radiotrue = '<input type="radio"' . $truechecked . $readonly . $inputname
      . 'id="'.$trueid . '" value="' . $trueanswer->id . '" /><label for="'.$trueid . '">'
      . s($trueanswer->answer) . '</label>';
    $radiofalse = '<input type="radio"' . $falsechecked . $readonly . $inputname
      . 'id="'.$falseid . '" value="' . $falseanswer->id . '" /><label for="'.$falseid . '">'
      . s($falseanswer->answer) . '</label>';

    $feedback = '';
    if ($options->feedback and isset($answers[$response])) {
      //choose feedback depending on correctness of response
      if($answers[$response]->fraction == 1){
        $feedback = format_text($trueanswer->feedback, true, $formatoptions, $cmoptions->course);
      }else{
        $feedback = format_text($falseanswer->feedback, true, $formatoptions, $cmoptions->course);
      }
    }
  }
  //print body
  include("$CFG->dirroot/question/type/$question->qtype/display.html");

  //print buttons
  $QTYPES[$question->qtype]->print_question_submit_buttons($question, $state, $cmoptions, $options);
  echo '</div>'; //close <div class="block clearfix">
  if($questionType == 'essay' && empty($options->readonly) && !empty($usehtmleditor)){
    use_html_editor($inputname);
    $htmleditorused = true;
  }else if($questionType == 'truefalse'){
    echo '</div>'; //close <div class = "truefalse"/>
  }
}
Example #9
0
 /**
  * Choose and load the desired number of questions.
  * @return array of short answer questions.
  */
 public function load_questions()
 {
     if ($this->choose > count($this->availablequestions)) {
         throw new coding_exception('notenoughtshortanswerquestions');
     }
     $questionids = draw_rand_array($this->availablequestions, $this->choose);
     $questions = array();
     foreach ($questionids as $questionid) {
         $questions[] = question_bank::load_question($questionid);
     }
     return $questions;
 }
 $curr_goods = $goods;
 // add 4 from good_class one
 $good_ids = draw_rand_array(&$curr_goods[1], 4);
 // now go through each lvl. and add one good from it's class each time
 for ($level_count = 2; $level_count <= $level; $level_count++) {
     if ($level_count == 1 || $level_count == 2) {
         $good_class = 1;
     }
     if ($level_count == 3 || $level_count == 4 || $level_count == 5 || $level_count == 6) {
         $good_class = 2;
     }
     if ($level_count == 7 || $level_count == 8 || $level_count == 9) {
         $good_class = 3;
     }
     // get one good
     $good_ids[] = draw_rand_array(&$curr_goods[$good_class], 1);
 }
 // Make sure everything is good and random
 shuffle($good_ids);
 // We rig matters so that there is at least one good bought/sold
 // and the number sold doesn't hugely outweigh the number sold
 // since we can only fit 9 on the local/galaxy map without exploding the table
 $num_goods = count($good_ids);
 $range_low = 1 + floor($num_goods * 0.35);
 $range_high = floor($num_goods * 0.67);
 $num_sold = mt_rand($range_low, $range_high);
 $bought = $sold = 0;
 for ($i = 0; $i < $num_goods; ++$i) {
     if ($i < $num_sold) {
         $transaction = 'Sell';
     } else {