MDL-38214 Questions: Add MCS, MCVS, MCHS Cloze subquestion types
qtype_multianswer: fix codechecker issues. Signed-off-by: Pierre Pichet <[email protected]>
This commit is contained in:
@@ -44,11 +44,16 @@ class qtype_multianswer_edit_form extends question_edit_form {
|
||||
// from the questiontext in database.
|
||||
public $savedquestion;
|
||||
public $savedquestiondisplay;
|
||||
public $used_in_quiz = false;
|
||||
public $qtype_change = false;
|
||||
public $negative_diff = 0;
|
||||
public $nb_of_quiz = 0;
|
||||
public $nb_of_attempts = 0;
|
||||
/** @var bool this question is used in quiz */
|
||||
public $usedinquiz = false;
|
||||
/** @var bool the qtype has been changed */
|
||||
public $qtypechange = false;
|
||||
/** @var integer number of questions that have been deleted */
|
||||
public $negativediff = 0;
|
||||
/** @var integer number of quiz that used this question */
|
||||
public $nbofquiz = 0;
|
||||
/** @var integer number of attempts that used this question */
|
||||
public $nbofattempts = 0;
|
||||
public $confirm = 0;
|
||||
public $reload = false;
|
||||
/** @var qtype_numerical_answer_processor used when validating numerical answers. */
|
||||
@@ -60,14 +65,14 @@ class qtype_multianswer_edit_form extends question_edit_form {
|
||||
$this->regenerate = true;
|
||||
$this->reload = optional_param('reload', false, PARAM_BOOL);
|
||||
|
||||
$this->used_in_quiz = false;
|
||||
$this->usedinquiz = false;
|
||||
|
||||
if (isset($question->id) && $question->id != 0) {
|
||||
// TODO MDL-43779 should not have quiz-specific code here.
|
||||
$this->savedquestiondisplay = fullclone($question);
|
||||
$this->nb_of_quiz = $DB->count_records('quiz_slots', array('questionid' => $question->id));
|
||||
$this->used_in_quiz = $this->nb_of_quiz > 0;
|
||||
$this->nb_of_attempts = $DB->count_records_sql("
|
||||
$this->nbofquiz = $DB->count_records('quiz_slots', array('questionid' => $question->id));
|
||||
$this->usedinquiz = $this->nbofquiz > 0;
|
||||
$this->nbofattempts = $DB->count_records_sql("
|
||||
SELECT count(1)
|
||||
FROM {quiz_slots} slot
|
||||
JOIN {quiz_attempts} quiza ON quiza.quiz = slot.quizid
|
||||
@@ -151,14 +156,13 @@ class qtype_multianswer_edit_form extends question_edit_form {
|
||||
if (isset($this->savedquestiondisplay->options->questions[$sub]->qtype) &&
|
||||
$this->savedquestiondisplay->options->questions[$sub]->qtype !=
|
||||
$this->questiondisplay->options->questions[$sub]->qtype) {
|
||||
$this->qtype_change = true;
|
||||
$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',
|
||||
$mform->addElement('header', 'subhdr'.$sub, get_string('questionno', 'question',
|
||||
'{#'.$sub.'}').' '.question_bank::get_qtype_name(
|
||||
$this->questiondisplay->options->questions[$sub]->qtype).$storemess);
|
||||
|
||||
@@ -203,25 +207,25 @@ class qtype_multianswer_edit_form extends question_edit_form {
|
||||
}
|
||||
}
|
||||
|
||||
$this->negative_diff = $countsavedsubquestions - $countsubquestions;
|
||||
if (($this->negative_diff > 0) ||$this->qtype_change ||
|
||||
($this->used_in_quiz && $this->negative_diff != 0)) {
|
||||
$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->negative_diff > 0) {
|
||||
if ($this->negativediff > 0) {
|
||||
$mform->addElement('static', 'alert1', "<strong>".
|
||||
get_string('questiondeleted', 'qtype_multianswer')."</strong>",
|
||||
get_string('questionsless', 'qtype_multianswer', $this->negative_diff));
|
||||
get_string('questionsless', 'qtype_multianswer', $this->negativediff));
|
||||
}
|
||||
if ($this->qtype_change) {
|
||||
if ($this->qtypechange) {
|
||||
$mform->addElement('static', 'alert1', "<strong>".
|
||||
get_string('questiontypechanged', 'qtype_multianswer')."</strong>",
|
||||
get_string('questiontypechangedcomment', 'qtype_multianswer'));
|
||||
}
|
||||
}
|
||||
if ($this->used_in_quiz) {
|
||||
if ($this->negative_diff < 0) {
|
||||
if ($this->usedinquiz) {
|
||||
if ($this->negativediff < 0) {
|
||||
$diff = $countsubquestions - $countsavedsubquestions;
|
||||
$mform->addElement('static', 'alert1', "<strong>".
|
||||
get_string('questionsadded', 'qtype_multianswer')."</strong>",
|
||||
@@ -229,15 +233,15 @@ class qtype_multianswer_edit_form extends question_edit_form {
|
||||
"</strong>");
|
||||
}
|
||||
$a = new stdClass();
|
||||
$a->nb_of_quiz = $this->nb_of_quiz;
|
||||
$a->nb_of_attempts = $this->nb_of_attempts;
|
||||
$a->nbofquiz = $this->nbofquiz;
|
||||
$a->nbofattempts = $this->nbofattempts;
|
||||
$mform->addElement('header', 'additemhdr2',
|
||||
get_string('questionusedinquiz', 'qtype_multianswer', $a));
|
||||
$mform->addElement('static', 'alertas',
|
||||
get_string('youshouldnot', 'qtype_multianswer'));
|
||||
}
|
||||
if (($this->negative_diff > 0 || $this->used_in_quiz &&
|
||||
($this->negative_diff > 0 || $this->negative_diff < 0 || $this->qtype_change)) &&
|
||||
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'));
|
||||
@@ -255,7 +259,7 @@ class qtype_multianswer_edit_form extends question_edit_form {
|
||||
|
||||
public function set_data($question) {
|
||||
global $DB;
|
||||
$default_values = array();
|
||||
$defaultvalues = array();
|
||||
if (isset($question->id) and $question->id and $question->qtype &&
|
||||
$question->questiontext) {
|
||||
|
||||
@@ -282,7 +286,7 @@ class qtype_multianswer_edit_form extends question_edit_form {
|
||||
$separator = '';
|
||||
foreach ($wrapped->options->answers as $subanswer) {
|
||||
$parsableanswerdef .= $separator
|
||||
. '%' . round(100*$subanswer->fraction) . '%';
|
||||
. '%' . round(100 * $subanswer->fraction) . '%';
|
||||
if (is_array($subanswer->answer)) {
|
||||
$parsableanswerdef .= $subanswer->answer['text'];
|
||||
} else {
|
||||
@@ -329,39 +333,39 @@ class qtype_multianswer_edit_form extends question_edit_form {
|
||||
if ($subquestion->qtype == 'shortanswer') {
|
||||
switch ($subquestion->usecase) {
|
||||
case '1':
|
||||
$default_values[$prefix.'usecase'] =
|
||||
$defaultvalues[$prefix.'usecase'] =
|
||||
get_string('caseyes', 'qtype_shortanswer');
|
||||
break;
|
||||
case '0':
|
||||
default :
|
||||
$default_values[$prefix.'usecase'] =
|
||||
$defaultvalues[$prefix.'usecase'] =
|
||||
get_string('caseno', 'qtype_shortanswer');
|
||||
}
|
||||
}
|
||||
|
||||
if ($subquestion->qtype == 'multichoice') {
|
||||
$default_values[$prefix.'layout'] = $subquestion->layout;
|
||||
$defaultvalues[$prefix.'layout'] = $subquestion->layout;
|
||||
switch ($subquestion->layout) {
|
||||
case '0':
|
||||
$default_values[$prefix.'layout'] =
|
||||
$defaultvalues[$prefix.'layout'] =
|
||||
get_string('layoutselectinline', 'qtype_multianswer');
|
||||
break;
|
||||
case '1':
|
||||
$default_values[$prefix.'layout'] =
|
||||
$defaultvalues[$prefix.'layout'] =
|
||||
get_string('layoutvertical', 'qtype_multianswer');
|
||||
break;
|
||||
case '2':
|
||||
$default_values[$prefix.'layout'] =
|
||||
$defaultvalues[$prefix.'layout'] =
|
||||
get_string('layouthorizontal', 'qtype_multianswer');
|
||||
break;
|
||||
default:
|
||||
$default_values[$prefix.'layout'] =
|
||||
$defaultvalues[$prefix.'layout'] =
|
||||
get_string('layoutundefined', 'qtype_multianswer');
|
||||
}
|
||||
}
|
||||
foreach ($subquestion->answer as $key => $answer) {
|
||||
if ($subquestion->qtype == 'numerical' && $key == 0) {
|
||||
$default_values[$prefix.'tolerance['.$key.']'] =
|
||||
$defaultvalues[$prefix.'tolerance['.$key.']'] =
|
||||
$subquestion->tolerance[0];
|
||||
}
|
||||
if (is_array($answer)) {
|
||||
@@ -384,7 +388,7 @@ class qtype_multianswer_edit_form extends question_edit_form {
|
||||
}
|
||||
}
|
||||
|
||||
$default_values[$prefix.'answer['.$key.']'] =
|
||||
$defaultvalues[$prefix.'answer['.$key.']'] =
|
||||
htmlspecialchars($answer);
|
||||
}
|
||||
if ($answercount == 0) {
|
||||
@@ -402,11 +406,11 @@ class qtype_multianswer_edit_form extends question_edit_form {
|
||||
}
|
||||
foreach ($subquestion->feedback as $key => $answer) {
|
||||
|
||||
$default_values[$prefix.'feedback['.$key.']'] =
|
||||
$defaultvalues[$prefix.'feedback['.$key.']'] =
|
||||
htmlspecialchars ($answer['text']);
|
||||
}
|
||||
foreach ($subquestion->fraction as $key => $answer) {
|
||||
$default_values[$prefix.'fraction['.$key.']'] = $answer;
|
||||
$defaultvalues[$prefix.'fraction['.$key.']'] = $answer;
|
||||
}
|
||||
|
||||
$sub++;
|
||||
@@ -414,11 +418,11 @@ class qtype_multianswer_edit_form extends question_edit_form {
|
||||
}
|
||||
}
|
||||
}
|
||||
$default_values['alertas']= "<strong>".get_string('questioninquiz', 'qtype_multianswer').
|
||||
$defaultvalues['alertas'] = "<strong>".get_string('questioninquiz', 'qtype_multianswer').
|
||||
"</strong>";
|
||||
|
||||
if ($default_values != "") {
|
||||
$question = (object)((array)$question + $default_values);
|
||||
if ($defaultvalues != "") {
|
||||
$question = (object)((array)$question + $defaultvalues);
|
||||
}
|
||||
$question = $this->data_preprocessing_hints($question, true, true);
|
||||
parent::set_data($question);
|
||||
@@ -500,11 +504,11 @@ class qtype_multianswer_edit_form extends question_edit_form {
|
||||
}
|
||||
}
|
||||
|
||||
if (($this->negative_diff > 0 || $this->used_in_quiz &&
|
||||
($this->negative_diff > 0 || $this->negative_diff < 0 ||
|
||||
$this->qtype_change)) && !$this->confirm) {
|
||||
if (($this->negativediff > 0 || $this->usedinquiz &&
|
||||
($this->negativediff > 0 || $this->negativediff < 0 ||
|
||||
$this->qtypechange)) && !$this->confirm) {
|
||||
$errors['confirm'] =
|
||||
get_string('confirmsave', 'qtype_multianswer', $this->negative_diff);
|
||||
get_string('confirmsave', 'qtype_multianswer', $this->negativediff);
|
||||
}
|
||||
|
||||
return $errors;
|
||||
|
||||
@@ -316,7 +316,7 @@ function qtype_multianswer_extract_question($text) {
|
||||
$wrapped->generalfeedback['text'] = '';
|
||||
$wrapped->generalfeedback['format'] = FORMAT_HTML;
|
||||
$wrapped->generalfeedback['itemid'] = '';
|
||||
if (isset($answerregs[ANSWER_REGEX_NORM])&& $answerregs[ANSWER_REGEX_NORM]!== '') {
|
||||
if (isset($answerregs[ANSWER_REGEX_NORM]) && $answerregs[ANSWER_REGEX_NORM] !== '') {
|
||||
$wrapped->defaultmark = $answerregs[ANSWER_REGEX_NORM];
|
||||
} else {
|
||||
$wrapped->defaultmark = '1';
|
||||
|
||||
Reference in New Issue
Block a user