Skip to content

Commit

Permalink
MDL-53557 mod_feedback: partial boundaries in numeric items
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Mar 27, 2016
1 parent 0114ca3 commit 17e1668
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 66 deletions.
2 changes: 1 addition & 1 deletion mod/feedback/item/info/lib.php
Expand Up @@ -142,7 +142,7 @@ public function get_printval($item, $value) {
if (!isset($value->value)) {
return '';
}
return userdate($value->value);
return $item->presentation == 1 ? userdate($value->value) : $value->value;
}

public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
Expand Down
110 changes: 60 additions & 50 deletions mod/feedback/item/numeric/lib.php
Expand Up @@ -19,21 +19,12 @@

class feedback_item_numeric extends feedback_item_base {
protected $type = "numeric";
public $sep_dec, $sep_thous;
private $commonparams;
private $item_form;
private $item;

public function init() {
$this->sep_dec = get_string('separator_decimal', 'feedback');
if (substr($this->sep_dec, 0, 2) == '[[') {
$this->sep_dec = FEEDBACK_DECIMAL;
}

$this->sep_thous = get_string('separator_thousand', 'feedback');
if (substr($this->sep_thous, 0, 2) == '[[') {
$this->sep_thous = FEEDBACK_THOUSAND;
}
}

public function build_editform($item, $feedback, $cm) {
Expand All @@ -58,17 +49,13 @@ public function build_editform($item, $feedback, $cm) {

$range_from_to = explode('|', $item->presentation);
if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
$range_from = str_replace(FEEDBACK_DECIMAL,
$this->sep_dec,
floatval($range_from_to[0]));
$range_from = $this->format_float($range_from_to[0]);
} else {
$range_from = '-';
}

if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
$range_to = str_replace(FEEDBACK_DECIMAL,
$this->sep_dec,
floatval($range_from_to[1]));
$range_to = $this->format_float($range_from_to[1]);
} else {
$range_to = '-';
}
Expand Down Expand Up @@ -152,7 +139,7 @@ public function get_analysed($item, $groupid = false, $courseid = false) {
$counter++;
}
}
$avg = $counter > 0 ? $avg / $counter : 0;
$avg = $counter > 0 ? $avg / $counter : null;
$analysed->data = $data;
$analysed->avg = $avg;
}
Expand All @@ -178,14 +165,14 @@ public function print_analysed($item, $itemnr = '', $groupid = false, $courseid

foreach ($values->data as $value) {
echo '<tr><td colspan="2" valign="top" align="left">';
echo '-&nbsp;&nbsp;'.number_format($value, 2, $this->sep_dec, $this->sep_thous);
echo '-&nbsp;&nbsp;'.$this->format_float($value);
echo '</td></tr>';
}

if (isset($values->avg)) {
$avg = number_format($values->avg, 2, $this->sep_dec, $this->sep_thous);
$avg = format_float($values->avg, 2);
} else {
$avg = number_format(0, 2, $this->sep_dec, $this->sep_thous);
$avg = '-';
}
echo '<tr><td align="left" colspan="2"><b>';
echo get_string('average', 'feedback').': '.$avg;
Expand All @@ -204,16 +191,23 @@ public function excelprint_item(&$worksheet, $row_offset,
$data = $analysed_item->data;
if (is_array($data)) {

//mittelwert anzeigen
// Export average.
$worksheet->write_string($row_offset,
2,
get_string('average', 'feedback'),
$xls_formats->value_bold);

$worksheet->write_number($row_offset + 1,
2,
$analysed_item->avg,
$xls_formats->value_bold);
if (isset($analysed_item->avg)) {
$worksheet->write_number($row_offset + 1,
2,
$analysed_item->avg,
$xls_formats->value_bold);
} else {
$worksheet->write_string($row_offset + 1,
2,
'',
$xls_formats->value_bold);
}
$row_offset++;
}
$row_offset++;
Expand Down Expand Up @@ -241,14 +235,14 @@ public function print_item_preview($item) {
if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
$range_from = floatval($range_from_to[0]);
} else {
$range_from = 0;
$range_from = '-';
}

//get the max-value
if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
$range_to = floatval($range_from_to[1]);
} else {
$range_to = 0;
$range_to = '-';
}

$requiredmark = ($item->required == 1) ? $strrequiredmark : '';
Expand All @@ -270,17 +264,17 @@ public function print_item_preview($item) {
switch(true) {
case ($range_from === '-' AND is_numeric($range_to)):
echo ' ('.get_string('maximal', 'feedback').
': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
': '.$this->format_float($range_to).')';
break;
case (is_numeric($range_from) AND $range_to === '-'):
echo ' ('.get_string('minimal', 'feedback').
': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
': '.$this->format_float($range_from).')';
break;
case ($range_from === '-' AND $range_to === '-'):
break;
default:
echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).
' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
echo ' ('.$this->format_float($range_from).
' - '.$this->format_float($range_to).')';
break;
}
echo '</span>';
Expand All @@ -301,6 +295,22 @@ public function print_item_preview($item) {
echo '</div>';
}

/**
* Prints the float nicely in the localized format
*
* Similar to format_float() but automatically calculates the number of decimal places
*
* @param float $value The float to print
* @return string
*/
protected function format_float($value) {
if (!is_numeric($value)) {
return null;
}
$decimal = is_int($value) ? 0 : strcspn(strrev($value), '.');
return format_float($value, $decimal);
}

/**
* print the item at the complete-page of feedback
*
Expand All @@ -323,14 +333,14 @@ public function print_item_complete($item, $value = '', $highlightrequire = fals
if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
$range_from = floatval($range_from_to[0]);
} else {
$range_from = 0;
$range_from = '-';
}

//get the max-value
if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
$range_to = floatval($range_from_to[1]);
} else {
$range_to = 0;
$range_to = '-';
}

$requiredmark = ($item->required == 1) ? $strrequiredmark : '';
Expand All @@ -344,17 +354,17 @@ public function print_item_complete($item, $value = '', $highlightrequire = fals
switch(true) {
case ($range_from === '-' AND is_numeric($range_to)):
echo ' ('.get_string('maximal', 'feedback').
': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
': '.$this->format_float($range_to).')';
break;
case (is_numeric($range_from) AND $range_to === '-'):
echo ' ('.get_string('minimal', 'feedback').
': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
': '.$this->format_float($range_from).')';
break;
case ($range_from === '-' AND $range_to === '-'):
break;
default:
echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).
' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
echo ' ('.$this->format_float($range_from).
' - '.$this->format_float($range_to).')';
break;
}
echo '</span>';
Expand Down Expand Up @@ -399,13 +409,13 @@ public function print_item_show_value($item, $value = '') {
if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
$range_from = floatval($range_from_to[0]);
} else {
$range_from = 0;
$range_from = '-';
}
//get the max-value
if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
$range_to = floatval($range_from_to[1]);
} else {
$range_to = 0;
$range_to = '-';
}
$requiredmark = ($item->required == 1) ? $strrequiredmark : '';

Expand All @@ -416,17 +426,17 @@ public function print_item_show_value($item, $value = '') {
switch(true) {
case ($range_from === '-' AND is_numeric($range_to)):
echo ' ('.get_string('maximal', 'feedback').
': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
': '.$this->format_float($range_to).')';
break;
case (is_numeric($range_from) AND $range_to === '-'):
echo ' ('.get_string('minimal', 'feedback').
': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
': '.$this->format_float($range_from).')';
break;
case ($range_from === '-' AND $range_to === '-'):
break;
default:
echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).
' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
echo ' ('.$this->format_float($range_from).
' - '.$this->format_float($range_to).')';
break;
}
echo '</div>';
Expand All @@ -435,7 +445,7 @@ public function print_item_show_value($item, $value = '') {
echo '<div class="feedback_item_presentation_'.$align.'">';
echo $OUTPUT->box_start('generalbox boxalign'.$align);
if (is_numeric($value)) {
$str_num_value = number_format($value, 2, $this->sep_dec, $this->sep_thous);
$str_num_value = $this->format_float($value);
} else {
$str_num_value = '&nbsp;';
}
Expand All @@ -445,7 +455,7 @@ public function print_item_show_value($item, $value = '') {
}

public function check_value($value, $item) {
$value = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $value);
$value = unformat_float($value, true);
//if the item is not required, so the check is true if no value is given
if ((!isset($value) OR $value == '') AND $item->required != 1) {
return true;
Expand Down Expand Up @@ -491,7 +501,7 @@ public function check_value($value, $item) {
}

public function create_value($data) {
$data = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data);
$data = unformat_float($data, true);

if (is_numeric($data)) {
$data = floatval($data);
Expand All @@ -512,14 +522,14 @@ public function compare_value($item, $dbvalue, $dependvalue) {
}

public function get_presentation($data) {
$num1 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangefrom);
$num1 = unformat_float($data->numericrangefrom, true);
if (is_numeric($num1)) {
$num1 = floatval($num1);
} else {
$num1 = '-';
}

$num2 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangeto);
$num2 = unformat_float($data->numericrangeto, true);
if (is_numeric($num2)) {
$num2 = floatval($num2);
} else {
Expand All @@ -546,18 +556,18 @@ public function can_switch_require() {
}

public function value_type() {
return PARAM_FLOAT;
return PARAM_TEXT;
}

public function clean_input_value($value) {
$value = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $value);
$value = unformat_float($value, true);
if (!is_numeric($value)) {
if ($value == '') {
return null; //an empty string should be null
} else {
return clean_param($value, PARAM_TEXT); //we have to know the value if it is wrong
}
}
return clean_param($value, $this->value_type());
return clean_param($value, PARAM_FLOAT);
}
}
18 changes: 6 additions & 12 deletions mod/feedback/item/numeric/numeric_form.php
Expand Up @@ -44,13 +44,13 @@ public function definition() {
'rangefrom',
get_string('numeric_range_from', 'feedback'),
array('size'=>10, 'maxlength'=>10));
$mform->setType('rangefrom', PARAM_INT);
$mform->setType('rangefrom', PARAM_RAW);

$mform->addElement('text',
'rangeto',
get_string('numeric_range_to', 'feedback'),
array('size'=>10, 'maxlength'=>10));
$mform->setType('rangeto', PARAM_INT);
$mform->setType('rangeto', PARAM_RAW);

parent::definition();
$this->set_data($item);
Expand All @@ -62,19 +62,13 @@ public function get_data() {
return false;
}

$itemobj = new feedback_item_numeric();

$num1 = str_replace($itemobj->sep_dec, FEEDBACK_DECIMAL, $item->rangefrom);
if (is_numeric($num1)) {
$num1 = floatval($num1);
} else {
$num1 = unformat_float($item->rangefrom, true);
if ($num1 === false || $num1 === null) {
$num1 = '-';
}

$num2 = str_replace($itemobj->sep_dec, FEEDBACK_DECIMAL, $item->rangeto);
if (is_numeric($num2)) {
$num2 = floatval($num2);
} else {
$num2 = unformat_float($item->rangeto, true);
if ($num2 === false || $num2 === null) {
$num2 = '-';
}

Expand Down
3 changes: 2 additions & 1 deletion mod/feedback/lang/en/feedback.php
Expand Up @@ -156,7 +156,8 @@
you can associate them with this feedback using map course(s). Multiple courses may be selected by holding down the Apple or Ctrl key whilst clicking on the course names. A course may be disassociated from a feedback at any time.';
$string['mappedcourses'] = 'Mapped courses';
$string['max_args_exceeded'] = 'Max 6 arguments can be handled, too many arguments for';
$string['maximal'] = 'maximal';
$string['minimal'] = 'minimum';
$string['maximal'] = 'maximum';
$string['messageprovider:message'] = 'Feedback reminder';
$string['messageprovider:submission'] = 'Feedback notifications';
$string['mode'] = 'Mode';
Expand Down
5 changes: 3 additions & 2 deletions mod/feedback/tests/behat/question_types.feature
Expand Up @@ -62,6 +62,7 @@ Feature: Test creating different types of feedback questions
And I add a "Numeric answer" question to the feedback with:
| Question | this is a numeric answer |
| Label | numeric |
| Range from | 0 |
| Range to | 100 |
And I add a "Short text answer" question to the feedback with:
| Question | this is a short text answer |
Expand Down Expand Up @@ -124,8 +125,8 @@ Feature: Test creating different types of feedback questions
And I should see "1 (50.00 %)" in the "option l (1):" "table_row"
And I should see "1 (50.00 %)" in the "option m (5):" "table_row"
And I should see "Average: 3.00" in the "(multichoice4)" "table"
And I should see "35.00" in the "(numeric)" "table"
And I should see "71.00" in the "(numeric)" "table"
And I should see "35" in the "(numeric)" "table"
And I should see "71" in the "(numeric)" "table"
And I should see "Average: 53.00" in the "(numeric)" "table"
And I should see "no way" in the "(shorttext)" "table"
And I should see "hello" in the "(shorttext)" "table"
Expand Down

0 comments on commit 17e1668

Please sign in to comment.