mirror of
https://github.com/moodle/moodle.git
synced 2025-08-03 16:13:28 +02:00
MDL-27413 qtype_multianswer more coding style cleanup.
This commit is contained in:
parent
df4be1222e
commit
12c6e0087e
4 changed files with 153 additions and 149 deletions
|
@ -1,5 +1,4 @@
|
|||
<?php
|
||||
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
|
@ -69,14 +68,12 @@ class qtype_multianswer_question extends question_graded_automatically {
|
|||
public function start_attempt(question_attempt_step $step) {
|
||||
foreach ($this->subquestions as $i => $subq) {
|
||||
$subq->start_attempt($this->get_substep($step, $i));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function apply_attempt_state(question_attempt_step $step) {
|
||||
foreach ($this->subquestions as $i => $subq) {
|
||||
$subq->apply_attempt_state($this->get_substep($step, $i));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -92,7 +92,8 @@ class qtype_multianswer extends question_type {
|
|||
array('question' => $question->id))) {
|
||||
$oldwrappedquestions = array();
|
||||
} else {
|
||||
$oldwrappedquestions = $DB->get_records_list('question', 'id', explode(',', $oldwrappedids), 'id ASC');
|
||||
$oldwrappedquestions = $DB->get_records_list('question', 'id',
|
||||
explode(',', $oldwrappedids), 'id ASC');
|
||||
}
|
||||
|
||||
$sequence = array();
|
||||
|
@ -100,7 +101,8 @@ class qtype_multianswer extends question_type {
|
|||
if (!empty($wrapped)) {
|
||||
// if we still have some old wrapped question ids, reuse the next of them
|
||||
|
||||
if (is_array($oldwrappedquestions) && $oldwrappedquestion = array_shift($oldwrappedquestions)) {
|
||||
if (is_array($oldwrappedquestions) &&
|
||||
$oldwrappedquestion = array_shift($oldwrappedquestions)) {
|
||||
$wrapped->id = $oldwrappedquestion->id;
|
||||
if ($oldwrappedquestion->qtype != $wrapped->qtype) {
|
||||
switch ($oldwrappedquestion->qtype) {
|
||||
|
@ -152,7 +154,8 @@ class qtype_multianswer extends question_type {
|
|||
$multianswer = new stdClass();
|
||||
$multianswer->question = $question->id;
|
||||
$multianswer->sequence = implode(',', $sequence);
|
||||
if ($oldid = $DB->get_field('question_multianswer', 'id', array('question' => $question->id))) {
|
||||
if ($oldid = $DB->get_field('question_multianswer', 'id',
|
||||
array('question' => $question->id))) {
|
||||
$multianswer->id = $oldid;
|
||||
$DB->update_record('question_multianswer', $multianswer);
|
||||
} else {
|
||||
|
@ -248,7 +251,8 @@ define('NUMERICAL_ABS_ERROR_MARGIN', 6);
|
|||
|
||||
// Remaining ANSWER regexes
|
||||
define('ANSWER_TYPE_DEF_REGEX',
|
||||
'(NUMERICAL|NM)|(MULTICHOICE|MC)|(MULTICHOICE_V|MCV)|(MULTICHOICE_H|MCH)|(SHORTANSWER|SA|MW)|(SHORTANSWER_C|SAC|MWC)');
|
||||
'(NUMERICAL|NM)|(MULTICHOICE|MC)|(MULTICHOICE_V|MCV)|(MULTICHOICE_H|MCH)|' .
|
||||
'(SHORTANSWER|SA|MW)|(SHORTANSWER_C|SAC|MWC)');
|
||||
define('ANSWER_START_REGEX',
|
||||
'\{([0-9]*):(' . ANSWER_TYPE_DEF_REGEX . '):');
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
<?php
|
||||
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
|
@ -39,7 +38,7 @@ require_once($CFG->dirroot . '/question/type/shortanswer/renderer.php');
|
|||
* @copyright 2010 Pierre Pichet
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class qtype_multianswer_renderer extends qtype_renderer {
|
||||
class qtype_multianswer_renderer extends qtype_renderer {
|
||||
|
||||
public function formulation_and_controls(question_attempt $qa,
|
||||
question_display_options $options) {
|
||||
|
@ -156,13 +155,15 @@ class qtype_multianswer_textfield_renderer extends qtype_renderer {
|
|||
|
||||
if ($options->rightanswer) {
|
||||
$correct = $subq->get_matching_answer($subq->get_correct_response());
|
||||
$feedback[] = get_string('correctansweris', 'qtype_shortanswer', s($correct->answer));
|
||||
$feedback[] = get_string('correctansweris', 'qtype_shortanswer',
|
||||
s($correct->answer));
|
||||
}
|
||||
|
||||
$subfraction = '';
|
||||
if ($options->marks >= question_display_options::MARK_AND_MAX && $subq->maxmark > 0) {
|
||||
$a = new stdClass();
|
||||
$a->mark = format_float($matchinganswer->fraction * $subq->maxmark, $options->markdp);
|
||||
$a->mark = format_float($matchinganswer->fraction * $subq->maxmark,
|
||||
$options->markdp);
|
||||
$a->max = format_float($subq->maxmark, $options->markdp);
|
||||
$feedback[] = get_string('markoutofmax', 'question', $a);
|
||||
}
|
||||
|
@ -182,11 +183,14 @@ class qtype_multianswer_textfield_renderer extends qtype_renderer {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* As multianswer have specific display requirements for multichoice display
|
||||
* a new class was defined although largely following the multichoice one
|
||||
*
|
||||
* @copyright 2010 Pierre Pichet
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
abstract class qtype_multianswer_multichoice_renderer_base extends qtype_renderer {
|
||||
abstract protected function get_input_type();
|
||||
|
||||
|
@ -202,8 +206,6 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
|
|||
|
||||
abstract protected function get_response(question_attempt $qa);
|
||||
|
||||
|
||||
|
||||
public function specific_feedback(question_attempt $qa) {
|
||||
return '';
|
||||
}
|
||||
|
@ -213,7 +215,7 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
|
|||
|
||||
$questiontot = $qa->get_question();
|
||||
$subquestion = $questiontot->subquestions[$qa->subquestionindex];
|
||||
$order = $subquestion->get_order($qa); //array_keys($question->answers); //
|
||||
$order = $subquestion->get_order($qa);
|
||||
$response = $this->get_response($qa);
|
||||
$inputattributes = array(
|
||||
'type' => $this->get_input_type(),
|
||||
|
@ -226,23 +228,22 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
|
|||
$feedbackimg = array();
|
||||
$feedback = array();
|
||||
$classes = array();
|
||||
$totfraction = 0 ;
|
||||
$nullresponse = true ;
|
||||
$totfraction = 0;
|
||||
$nullresponse = true;
|
||||
foreach ($order as $value => $ansid) {
|
||||
$ans = $subquestion->answers[$ansid];
|
||||
$inputattributes['name'] = $this->get_input_name($qa, $value);
|
||||
// echo "<p>name $value name".$inputattributes['name']." </p>";
|
||||
$inputattributes['value'] = $this->get_input_value($value);
|
||||
$inputattributes['id'] = $this->get_input_id($qa, $value);
|
||||
if ($subquestion->single) {
|
||||
$isselected = $this->is_choice_selected($response, $value);
|
||||
} else {
|
||||
$isselected = $this->is_choice_selected($response,$value) ; //$subquestion->field( $value));
|
||||
$isselected = $this->is_choice_selected($response, $value);
|
||||
}
|
||||
if ($isselected) {
|
||||
$inputattributes['checked'] = 'checked';
|
||||
$totfraction += $ans->fraction ;
|
||||
$nullresponse = false ;
|
||||
$totfraction += $ans->fraction;
|
||||
$nullresponse = false;
|
||||
} else {
|
||||
unset($inputattributes['checked']);
|
||||
}
|
||||
|
@ -266,17 +267,17 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
|
|||
$classes[] = $class;
|
||||
}
|
||||
|
||||
$result = '' ;
|
||||
$result = '';
|
||||
|
||||
$answername = 'answer' ;
|
||||
if ($subquestion->layout == 1 ){
|
||||
$answername = 'answer';
|
||||
if ($subquestion->layout == 1) {
|
||||
$result .= html_writer::start_tag('div', array('class' => 'ablock'));
|
||||
|
||||
$result .= html_writer::start_tag('table', array('class' => $answername));
|
||||
foreach ($radiobuttons as $key => $radio) {
|
||||
$result .= html_writer::start_tag('tr', array('class' => $answername));
|
||||
$result .= html_writer::start_tag('td', array('class' => $answername));
|
||||
$result .= html_writer::tag('span',$radio . $feedbackimg[$key] . $feedback[$key], array('class' => $classes[$key])) . "\n";
|
||||
$result .= html_writer::tag('span', $radio . $feedbackimg[$key] . $feedback[$key], array('class' => $classes[$key])) . "\n";
|
||||
$result .= html_writer::end_tag('td');
|
||||
$result .= html_writer::end_tag('tr');
|
||||
}
|
||||
|
@ -284,13 +285,13 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
|
|||
|
||||
$result .= html_writer::end_tag('div'); // ablock
|
||||
}
|
||||
if ($subquestion->layout == 2 ){
|
||||
if ($subquestion->layout == 2) {
|
||||
$result .= html_writer::start_tag('div', array('class' => 'ablock'));
|
||||
$result .= html_writer::start_tag('table', array('class' => $answername));
|
||||
$result .= html_writer::start_tag('tr', array('class' => $answername));
|
||||
foreach ($radiobuttons as $key => $radio) {
|
||||
$result .= html_writer::start_tag('td', array('class' => $answername));
|
||||
$result .= html_writer::tag('span',$radio . $feedbackimg[$key] . $feedback[$key]
|
||||
$result .= html_writer::tag('span', $radio . $feedbackimg[$key] . $feedback[$key]
|
||||
, array('class' => $classes[$key])) . "\n";
|
||||
$result .= html_writer::end_tag('td');
|
||||
}
|
||||
|
@ -298,23 +299,22 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
|
|||
$result .= html_writer::end_tag('table'); // answer
|
||||
|
||||
$result .= html_writer::end_tag('div'); // ablock
|
||||
|
||||
}
|
||||
if ($options->feedback ) {
|
||||
|
||||
if ($options->feedback) {
|
||||
$result .= html_writer::start_tag('div', array('class' => 'outcome'));
|
||||
|
||||
if ($options->correctness ) {
|
||||
if ( $nullresponse ){
|
||||
if ($options->correctness) {
|
||||
if ( $nullresponse) {
|
||||
$state = $qa->get_state();
|
||||
$state = question_state::$invalid;
|
||||
$result1 = $state->default_string();
|
||||
$result .= html_writer::nonempty_tag('div',$result1,
|
||||
$result .= html_writer::nonempty_tag('div', $result1,
|
||||
array('class' => 'validationerror'));
|
||||
$result1 = ($subquestion->single) ? get_string('singleanswer', 'quiz') : get_string('multipleanswers', 'quiz');
|
||||
$result .= html_writer::nonempty_tag('div', $result1,
|
||||
array('class' => 'validationerror'))
|
||||
;
|
||||
}else {
|
||||
array('class' => 'validationerror'));
|
||||
} else {
|
||||
$state = $qa->get_state();
|
||||
$state = question_state::graded_state_for_fraction($totfraction);
|
||||
$result1 = $state->default_string();
|
||||
|
@ -323,15 +323,13 @@ abstract class qtype_multianswer_multichoice_renderer_base extends qtype_render
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
if ($options->correctresponse ) {
|
||||
if ($options->correctresponse) {
|
||||
$result1 = $this->correct_response($qa);
|
||||
$result .= html_writer::nonempty_tag('div',$result1, array('class' => 'outcome'))
|
||||
;
|
||||
$result .= html_writer::nonempty_tag('div', $result1, array('class' => 'outcome'));
|
||||
}
|
||||
if ($options->marks ) {
|
||||
$subgrade= $totfraction * $subquestion->defaultmark ;
|
||||
$result .= $questiontot->mark_summary($options, $subquestion->defaultmark , $subgrade );
|
||||
if ($options->marks) {
|
||||
$subgrade= $totfraction * $subquestion->defaultmark;
|
||||
$result .= $questiontot->mark_summary($options, $subquestion->defaultmark , $subgrade);
|
||||
}
|
||||
|
||||
if ($qa->get_state() == question_state::$invalid) {
|
||||
|
@ -354,17 +352,20 @@ class qtype_multianswer_multichoice_single_renderer extends qtype_multianswer_mu
|
|||
}
|
||||
|
||||
protected function is_choice_selected($response, $value) {
|
||||
return $response == $value ;
|
||||
return $response == $value;
|
||||
}
|
||||
|
||||
protected function is_right(question_answer $ans) {
|
||||
return $ans->fraction > 0.9999999;
|
||||
}
|
||||
|
||||
protected function get_input_name(question_attempt $qa, $value) {
|
||||
$questiontot = $qa->get_question();
|
||||
$subquestion = $questiontot->subquestions[$qa->subquestionindex];
|
||||
$answername = $subquestion->fieldid.'answer';
|
||||
return $qa->get_qt_field_name($answername);
|
||||
}
|
||||
|
||||
protected function get_input_value($value) {
|
||||
return $value;
|
||||
}
|
||||
|
@ -382,6 +383,7 @@ class qtype_multianswer_multichoice_single_renderer extends qtype_multianswer_mu
|
|||
return $qa->get_last_qt_var($subquestion->fieldid.'answer', -1);
|
||||
|
||||
}
|
||||
|
||||
public function correct_response(question_attempt $qa) {
|
||||
$questiontot = $qa->get_question();
|
||||
$subquestion = $questiontot->subquestions[$qa->subquestionindex];
|
||||
|
@ -395,8 +397,9 @@ class qtype_multianswer_multichoice_single_renderer extends qtype_multianswer_mu
|
|||
|
||||
return '';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class qtype_multianswer_multichoice_single_inline_renderer extends qtype_multianswer_multichoice_single_renderer {
|
||||
protected function get_input_type() {
|
||||
return 'select';
|
||||
|
@ -408,12 +411,12 @@ class qtype_multianswer_multichoice_single_inline_renderer extends qtype_multian
|
|||
$subquestion = $questiontot->subquestions[$qa->subquestionindex];
|
||||
$answers = $subquestion->answers;
|
||||
$correctanswers = $subquestion->get_correct_response();
|
||||
foreach($correctanswers as $key=> $value){
|
||||
$correct = $value ;
|
||||
foreach ($correctanswers as $key => $value) {
|
||||
$correct = $value;
|
||||
}
|
||||
$order = $subquestion->get_order($qa);
|
||||
$response = $this->get_response($qa);
|
||||
$currentanswer = $response ;
|
||||
$currentanswer = $response;
|
||||
$answername = $subquestion->fieldid.'answer';
|
||||
$inputname = $qa->get_qt_field_name($answername);
|
||||
$inputattributes = array(
|
||||
|
@ -427,25 +430,25 @@ class qtype_multianswer_multichoice_single_inline_renderer extends qtype_multian
|
|||
}
|
||||
$choices = array();
|
||||
$popup = '';
|
||||
$feedback = '' ;
|
||||
$answer = '' ;
|
||||
$feedback = '';
|
||||
$answer = '';
|
||||
$classes = 'control';
|
||||
$feedbackimage = '';
|
||||
$fraction = 0 ;
|
||||
$chosen = 0 ;
|
||||
$fraction = 0;
|
||||
$chosen = 0;
|
||||
|
||||
foreach ($order as $value => $ansid) {
|
||||
$mcanswer = $subquestion->answers[$ansid];
|
||||
$choices[$value] = strip_tags($mcanswer->answer);
|
||||
$selected = '';
|
||||
$isselected = false ;
|
||||
if( $response != ''){
|
||||
$isselected = false;
|
||||
if ( $response != '') {
|
||||
$isselected = $this->is_choice_selected($response, $value);
|
||||
}
|
||||
if ($isselected) {
|
||||
$chosen = $value ;
|
||||
$answer = $mcanswer ;
|
||||
$fraction = $mcanswer->fraction ;
|
||||
$chosen = $value;
|
||||
$answer = $mcanswer;
|
||||
$fraction = $mcanswer->fraction;
|
||||
$selected = ' selected="selected"';
|
||||
}
|
||||
}
|
||||
|
@ -467,43 +470,42 @@ class qtype_multianswer_multichoice_single_inline_renderer extends qtype_multian
|
|||
// Correct response
|
||||
// marks
|
||||
$strfeedbackwrapped = 'Response Status';
|
||||
if ($options->feedback ) {
|
||||
if ($options->feedback) {
|
||||
$feedback = get_string('feedback', 'quiz').":".$feedback."<br />";
|
||||
|
||||
if ($options->correctness ) {
|
||||
if ( ! $answer ){
|
||||
if ($options->correctness) {
|
||||
if (!$answer) {
|
||||
$state = $qa->get_state();
|
||||
$state = question_state::$invalid;
|
||||
$strfeedbackwrapped .= ":<font color=red >".$state->default_string()."</font>" ;
|
||||
$strfeedbackwrapped .= ":<font color=red >".$state->default_string()."</font>";
|
||||
$feedback = "<font color=red >".get_string('singleanswer', 'quiz') ."</font><br />";
|
||||
}else {
|
||||
} else {
|
||||
$state = $qa->get_state();
|
||||
$state = question_state::graded_state_for_fraction($fraction);
|
||||
$strfeedbackwrapped .= ":".$state->default_string();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ($options->correctresponse ) {
|
||||
if ($options->correctresponse) {
|
||||
$feedback .= $this->correct_response($qa)."<br />";
|
||||
}
|
||||
if ($options->marks ) {
|
||||
$subgrade= $fraction * $subquestion->defaultmark ;
|
||||
$feedback .= $questiontot->mark_summary($options, $subquestion->defaultmark , $subgrade );
|
||||
if ($options->marks) {
|
||||
$subgrade= $fraction * $subquestion->defaultmark;
|
||||
$feedback .= $questiontot->mark_summary($options, $subquestion->defaultmark , $subgrade);
|
||||
}
|
||||
|
||||
$feedback .= '</div>';
|
||||
}
|
||||
|
||||
if ($options->feedback ) {
|
||||
if ($options->feedback) {
|
||||
// need to replace ' and " as they could break the popup string
|
||||
// as the text comes from database, slashes have been removed
|
||||
// addslashes will not work as it keeps the "
|
||||
// HTML ' for ' does not work
|
||||
$feedback = str_replace("'","\'",$feedback);
|
||||
$feedback = str_replace('"',"\'",$feedback);
|
||||
$strfeedbackwrapped = str_replace("'","\'",$strfeedbackwrapped);
|
||||
$strfeedbackwrapped = str_replace('"',"\'",$strfeedbackwrapped);
|
||||
$feedback = str_replace("'", "\'", $feedback);
|
||||
$feedback = str_replace('"', "\'", $feedback);
|
||||
$strfeedbackwrapped = str_replace("'", "\'", $strfeedbackwrapped);
|
||||
$strfeedbackwrapped = str_replace('"', "\'", $strfeedbackwrapped);
|
||||
|
||||
$popup = " onmouseover=\"return overlib('$feedback', STICKY, MOUSEOFF, CAPTION, '$strfeedbackwrapped', FGCOLOR, '#FFFFFF');\" ".
|
||||
" onmouseout=\"return nd();\" ";
|
||||
|
@ -513,13 +515,11 @@ class qtype_multianswer_multichoice_single_inline_renderer extends qtype_multian
|
|||
$result .= "<span $popup >";
|
||||
$result .= html_writer::start_tag('span', array('class' => $classes), '');
|
||||
|
||||
$result .=
|
||||
choose_from_menu($choices, $inputname, $chosen,
|
||||
' ', '', '', true, $options->readonly) . $feedbackimage ;
|
||||
$result .= choose_from_menu($choices, $inputname, $chosen,
|
||||
' ', '', '', true, $options->readonly) . $feedbackimage;
|
||||
$result .= html_writer::end_tag('span');
|
||||
$result .= html_writer::end_tag('span');
|
||||
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
@ -556,9 +556,9 @@ class qtype_multianswer_multichoice_multi_renderer extends qtype_multianswer_mul
|
|||
$responses = $qa->get_last_qt_data();
|
||||
$questiontot = $qa->get_question();
|
||||
$subresponses =$questiontot->decode_subquestion_responses($responses);
|
||||
if( isset($subresponses[$qa->subquestionindex])) {
|
||||
return $subresponses[$qa->subquestionindex] ;
|
||||
}else{
|
||||
if ( isset($subresponses[$qa->subquestionindex])) {
|
||||
return $subresponses[$qa->subquestionindex];
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -90,9 +90,12 @@ class qtype_multianswer_test_helper extends question_test_helper {
|
|||
$mc->layout = 1;
|
||||
|
||||
$mc->answers = array(
|
||||
13 => new question_answer(13, 'Bow-wow', 0, 'You seem to have a dog obsessions!', FORMAT_HTML),
|
||||
14 => new question_answer(14, 'Wiggly worm', 0, 'Now you are just being rediculous!', FORMAT_HTML),
|
||||
15 => new question_answer(15, 'Pussy-cat', 1, 'Well done!', FORMAT_HTML),
|
||||
13 => new question_answer(13, 'Bow-wow', 0,
|
||||
'You seem to have a dog obsessions!', FORMAT_HTML),
|
||||
14 => new question_answer(14, 'Wiggly worm', 0,
|
||||
'Now you are just being rediculous!', FORMAT_HTML),
|
||||
15 => new question_answer(15, 'Pussy-cat', 1,
|
||||
'Well done!', FORMAT_HTML),
|
||||
);
|
||||
$mc->qtype = question_bank::get_qtype('multichoice');
|
||||
$mc->maxmark = 1;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue