2 ///////////////////////////////////////////////////////////////////////////
4 // NOTICE OF COPYRIGHT //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
9 // Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com //
11 // This program is free software; you can redistribute it and/or modify //
12 // it under the terms of the GNU General Public License as published by //
13 // the Free Software Foundation; either version 2 of the License, or //
14 // (at your option) any later version. //
16 // This program is distributed in the hope that it will be useful, //
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
19 // GNU General Public License for more details: //
21 // http://www.gnu.org/copyleft/gpl.html //
23 ///////////////////////////////////////////////////////////////////////////
25 class data_field_number extends data_field_base {
28 function update_content($recordid, $value, $name='') {
31 $content = new stdClass();
32 $content->fieldid = $this->field->id;
33 $content->recordid = $recordid;
34 $value = trim($value);
35 if (strlen($value) > 0) {
36 $content->content = floatval($value);
38 $content->content = null;
40 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
41 $content->id = $oldcontent->id;
42 return $DB->update_record('data_content', $content);
44 return $DB->insert_record('data_content', $content);
48 function display_browse_field($recordid, $template) {
51 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
52 if (strlen($content->content) < 1) {
55 $number = $content->content;
56 $decimals = trim($this->field->param1);
57 // only apply number formatting if param1 contains an integer number >= 0:
58 if (preg_match("/^\d+$/", $decimals)) {
59 $decimals = $decimals * 1;
60 // removes leading zeros (eg. '007' -> '7'; '00' -> '0')
61 $str = format_float($number, $decimals, true);
62 // For debugging only:
63 # $str .= " ($decimals)";
72 function display_search_field($value = '') {
73 return '<label class="accesshide" for="f_'.$this->field->id.'">' . get_string('fieldname', 'data') . '</label>' .
74 '<input type="text" size="16" id="f_'.$this->field->id.'" name="f_'.$this->field->id.'" value="'.s($value).'" />';
77 function parse_search_field() {
78 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
82 function generate_sql($tablealias, $value) {
87 $name = "df_number_$i";
88 $varcharcontent = $DB->sql_compare_text("{$tablealias}.content");
89 return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharcontent = :$name) ", array($name=>$value));
92 function get_sort_sql($fieldname) {
94 return $DB->sql_cast_char2real($fieldname, true);