Skip to content

Commit

Permalink
MDL-38214 Questions: Add MCS, MCVS, MCHS Cloze subquestion types
Browse files Browse the repository at this point in the history
qtype_multianswer: fix codechecker issues.

Signed-off-by: Pierre Pichet <[email protected]>
  • Loading branch information
ppichet committed Oct 8, 2015
1 parent 74fad2c commit 48b5b28
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 45 deletions.
92 changes: 48 additions & 44 deletions question/type/multianswer/edit_multianswer_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -60,14 +65,14 @@ public function __construct($submiturl, $question, $category, $contexts, $formed
$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
Expand Down Expand Up @@ -151,14 +156,13 @@ protected function definition_inner($mform) {
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.'}').'&nbsp;'.question_bank::get_qtype_name(
$this->questiondisplay->options->questions[$sub]->qtype).$storemess);

Expand Down Expand Up @@ -203,41 +207,41 @@ protected function definition_inner($mform) {
}
}

$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>",
"<strong>".get_string('questionsmore', 'qtype_multianswer', $diff).
"</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'));
Expand All @@ -255,7 +259,7 @@ protected function definition_inner($mform) {

public function set_data($question) {
global $DB;
$default_values = array();
$defaultvalues = array();
if (isset($question->id) and $question->id and $question->qtype &&
$question->questiontext) {

Expand All @@ -282,7 +286,7 @@ public function set_data($question) {
$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 {
Expand Down Expand Up @@ -329,39 +333,39 @@ public function set_data($question) {
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)) {
Expand All @@ -384,7 +388,7 @@ public function set_data($question) {
}
}

$default_values[$prefix.'answer['.$key.']'] =
$defaultvalues[$prefix.'answer['.$key.']'] =
htmlspecialchars($answer);
}
if ($answercount == 0) {
Expand All @@ -402,23 +406,23 @@ public function set_data($question) {
}
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++;
}
}
}
}
$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);
Expand Down Expand Up @@ -500,11 +504,11 @@ public function validation($data, $files) {
}
}

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;
Expand Down
2 changes: 1 addition & 1 deletion question/type/multianswer/questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 48b5b28

Please sign in to comment.