Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  /**
  17   * @package    backup-convert
  18   * @copyright  2012 Darko Miletic <dmiletic@moodlerooms.com>
  19   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20   */
  21  
  22  defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
  23  
  24  require_once ('cc_asssesment.php');
  25  
  26  class cc_assesment_question_truefalse extends cc_assesment_question_proc_base {
  27      public function __construct($quiz, $questions, $manifest, $section, $question_node, $rootpath, $contextid, $outdir) {
  28          parent::__construct($quiz, $questions, $manifest, $section, $question_node, $rootpath, $contextid, $outdir);
  29          $this->qtype = cc_qti_profiletype::true_false;
  30  
  31          // Determine the correct answer by finding out which answer has the non zero fraction...
  32          // This is because a true / false question type can have 'false' as the correct answer.
  33          $answers = $this->questions->nodeList('plugin_qtype_truefalse_question/answers/answer', $this->question_node);
  34          foreach ($answers as $answer) {
  35              $fraction = $this->questions->nodeValue('fraction', $answer);
  36  
  37              if ($fraction != 0) {
  38                  $this->correct_answer_node_id = (int)$this->questions->nodeValue('@id', $answer);
  39              }
  40          }
  41  
  42          $maximum_quiz_grade = (int)$this->quiz->nodeValue('/activity/quiz/grade');
  43          $this->total_grade_value = ($maximum_quiz_grade + 1).'.0000000';
  44      }
  45  
  46      public function on_generate_answers() {
  47          // Add responses holder.
  48          $qresponse_lid = new cc_response_lidtype();
  49          $this->qresponse_lid = $qresponse_lid;
  50          $this->qpresentation->set_response_lid($qresponse_lid);
  51          $qresponse_choice = new cc_assesment_render_choicetype();
  52          $qresponse_lid->set_render_choice($qresponse_choice);
  53          // Mark that question has only one correct answer -
  54          // which applies for multiple choice and yes/no questions.
  55          $qresponse_lid->set_rcardinality(cc_qti_values::Single);
  56          // Are we to shuffle the responses?
  57          $shuffle_answers = (int)$this->quiz->nodeValue('/activity/quiz/shuffleanswers') > 0;
  58          $qresponse_choice->enable_shuffle($shuffle_answers);
  59          $answerlist = array();
  60          $qa_responses = $this->questions->nodeList('plugin_qtype_truefalse_question/answers/answer', $this->question_node);
  61          foreach ($qa_responses as $node) {
  62              $answer_content = $this->questions->nodeValue('answertext', $node);
  63              $id = ((int)$this->questions->nodeValue('@id', $node) == $this->correct_answer_node_id);
  64              $qresponse_label = cc_assesment_helper::add_answer( $qresponse_choice,
  65                                                                  $answer_content,
  66                                                                  cc_qti_values::htmltype);
  67              $answer_ident = strtolower(trim($answer_content));
  68              $qresponse_label->set_ident($answer_ident);
  69              $feedback_ident = ($id) ? 'correct_fb' : 'incorrect_fb';
  70              if (empty($this->correct_answer_ident) && $id) {
  71                  $this->correct_answer_ident = $answer_ident;
  72              }
  73              // Add answer specific feedback if not empty.
  74              $content = $this->questions->nodeValue('feedback', $node);
  75              if (!empty($content)) {
  76                  $result = cc_helpers::process_linked_files( $content,
  77                                                              $this->manifest,
  78                                                              $this->rootpath,
  79                                                              $this->contextid,
  80                                                              $this->outdir);
  81  
  82  
  83                  cc_assesment_helper::add_feedback( $this->qitem,
  84                                                      $result[0],
  85                                                      cc_qti_values::htmltype,
  86                                                      $feedback_ident);
  87  
  88                  pkg_resource_dependencies::instance()->add($result[1]);
  89  
  90                  $answerlist[$answer_ident] = $feedback_ident;
  91              }
  92          }
  93  
  94          $this->answerlist = $answerlist;
  95  
  96      }
  97  
  98      public function on_generate_response_processing() {
  99          parent::on_generate_response_processing();
 100  
 101          // Response conditions.
 102          // General unconditional feedback must be added as a first respcondition
 103          // without any condition and just displayfeedback (if exists).
 104          if (!empty($this->general_feedback)) {
 105              $qrespcondition = new cc_assesment_respconditiontype();
 106              $qrespcondition->set_title('General feedback');
 107              $this->qresprocessing->add_respcondition($qrespcondition);
 108              $qrespcondition->enable_continue();
 109              // Define the condition for success.
 110              $qconditionvar = new cc_assignment_conditionvar();
 111              $qrespcondition->set_conditionvar($qconditionvar);
 112              $qother = new cc_assignment_conditionvar_othertype();
 113              $qconditionvar->set_other($qother);
 114              $qdisplayfeedback = new cc_assignment_displayfeedbacktype();
 115              $qrespcondition->add_displayfeedback($qdisplayfeedback);
 116              $qdisplayfeedback->set_feedbacktype(cc_qti_values::Response);
 117              $qdisplayfeedback->set_linkrefid('general_fb');
 118          }
 119  
 120          // Success condition.
 121          // For all question types outside of the Essay question, scoring is done in a
 122          // single <respcondition> with a continue flag set to No. The outcome is always
 123          // a variable named SCORE which value must be set to 100 in case of correct answer.
 124          // Partial scores (not 0 or 100) are not supported.
 125          $qrespcondition = new cc_assesment_respconditiontype();
 126          $qrespcondition->set_title('Correct');
 127          $this->qresprocessing->add_respcondition($qrespcondition);
 128          $qrespcondition->enable_continue(false);
 129          $qsetvar = new cc_assignment_setvartype(100);
 130          $qrespcondition->add_setvar($qsetvar);
 131          // Define the condition for success.
 132          $qconditionvar = new cc_assignment_conditionvar();
 133          $qrespcondition->set_conditionvar($qconditionvar);
 134          // TODO: recheck this.
 135          $qvarequal = new cc_assignment_conditionvar_varequaltype($this->correct_answer_ident);
 136          $qconditionvar->set_varequal($qvarequal);
 137          $qvarequal->set_respident($this->qresponse_lid->get_ident());
 138  
 139          if (array_key_exists($this->correct_answer_ident, $this->answerlist)) {
 140              $qdisplayfeedback = new cc_assignment_displayfeedbacktype();
 141              $qrespcondition->add_displayfeedback($qdisplayfeedback);
 142              $qdisplayfeedback->set_feedbacktype(cc_qti_values::Response);
 143              $qdisplayfeedback->set_linkrefid($this->answerlist[$this->correct_answer_ident]);
 144          }
 145  
 146          foreach ($this->correct_feedbacks as $ident) {
 147              $qdisplayfeedback = new cc_assignment_displayfeedbacktype();
 148              $qrespcondition->add_displayfeedback($qdisplayfeedback);
 149              $qdisplayfeedback->set_feedbacktype(cc_qti_values::Response);
 150              $qdisplayfeedback->set_linkrefid($ident);
 151          }
 152  
 153          // Rest of the conditions.
 154          foreach ($this->answerlist as $ident => $refid) {
 155              if ($ident == $this->correct_answer_ident) {
 156                  continue;
 157              }
 158  
 159              $qrespcondition = new cc_assesment_respconditiontype();
 160              $this->qresprocessing->add_respcondition($qrespcondition);
 161              $qsetvar = new cc_assignment_setvartype(0);
 162              $qrespcondition->add_setvar($qsetvar);
 163              // Define the condition for fail.
 164              $qconditionvar = new cc_assignment_conditionvar();
 165              $qrespcondition->set_conditionvar($qconditionvar);
 166              $qvarequal = new cc_assignment_conditionvar_varequaltype($ident);
 167              $qconditionvar->set_varequal($qvarequal);
 168              $qvarequal->set_respident($this->qresponse_lid->get_ident());
 169  
 170              $qdisplayfeedback = new cc_assignment_displayfeedbacktype();
 171              $qrespcondition->add_displayfeedback($qdisplayfeedback);
 172              $qdisplayfeedback->set_feedbacktype(cc_qti_values::Response);
 173              $qdisplayfeedback->set_linkrefid($refid);
 174  
 175              foreach ($this->incorrect_feedbacks as $ident) {
 176                  $qdisplayfeedback = new cc_assignment_displayfeedbacktype();
 177                  $qrespcondition->add_displayfeedback($qdisplayfeedback);
 178                  $qdisplayfeedback->set_feedbacktype(cc_qti_values::Response);
 179                  $qdisplayfeedback->set_linkrefid($ident);
 180              }
 181          }
 182      }
 183  }