Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / data / field / number / field.class.php
blob371d725ca5af922b78343605889054316f729bfb
1 <?php // $Id$
2 ///////////////////////////////////////////////////////////////////////////
3 // //
4 // NOTICE OF COPYRIGHT //
5 // //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
8 // //
9 // Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com //
10 // //
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. //
15 // //
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: //
20 // //
21 // http://www.gnu.org/copyleft/gpl.html //
22 // //
23 ///////////////////////////////////////////////////////////////////////////
25 class data_field_number extends data_field_base {
26 var $type = 'number';
28 function data_field_number($field=0, $data=0) {
29 parent::data_field_base($field, $data);
32 function update_content($recordid, $value, $name='') {
33 $content = new object;
34 $content->fieldid = $this->field->id;
35 $content->recordid = $recordid;
36 $value = trim($value);
37 if (strlen($value) > 0) {
38 $content->content = floatval($value);
39 } else {
40 $content->content = null;
42 if ($oldcontent = get_record('data_content','fieldid', $this->field->id, 'recordid', $recordid)) {
43 $content->id = $oldcontent->id;
44 return update_record('data_content', $content);
45 } else {
46 return insert_record('data_content', $content);
50 function display_browse_field($recordid, $template) {
51 if ($content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)) {
52 if (strlen($content->content) < 1) {
53 return false;
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)";
64 } else {
65 $str = $number;
67 return $str;
69 return false;
72 function display_search_field($value = '') {
73 return '<input type="text" size="16" name="f_'.$this->field->id.'" value="'.$value.'" />';
76 function parse_search_field() {
77 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
80 // need to cast?
81 function generate_sql($tablealias, $value) {
82 return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = '$value') ";
85 function get_sort_sql($fieldname) {
86 global $CFG;
87 switch ($CFG->dbfamily) {
88 case 'mysql':
89 // string in an arithmetic operation is converted to a floating-point number
90 return '('.$fieldname.'+0.0)';
91 case 'postgres':
92 // cast for PG
93 return 'CAST('.$fieldname.' AS REAL)';
94 default:
95 // the rest, just the field name. TODO: Analyse behaviour under MSSQL and Oracle
96 return $fieldname;