MDL-21437 data - number and date fields search under mssql and oracle
[moodle.git] / mod / data / field / number / field.class.php
CommitLineData
1adbd2c3 1<?php
59f3b717 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///////////////////////////////////////////////////////////////////////////
24
25class data_field_number extends data_field_base {
59f3b717 26 var $type = 'number';
27
ec536d7a 28 function update_content($recordid, $value, $name='') {
a656d951 29 global $DB;
30
39790bd8 31 $content = new stdClass();
ec536d7a 32 $content->fieldid = $this->field->id;
33 $content->recordid = $recordid;
02ee7f37 34 $value = trim($value);
35 if (strlen($value) > 0) {
36 $content->content = floatval($value);
37 } else {
38 $content->content = null;
39 }
a656d951 40 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
ec536d7a 41 $content->id = $oldcontent->id;
a656d951 42 return $DB->update_record('data_content', $content);
ec536d7a 43 } else {
a656d951 44 return $DB->insert_record('data_content', $content);
ec536d7a 45 }
46 }
a84f7fdd 47
48 function display_browse_field($recordid, $template) {
a656d951 49 global $DB;
50
51 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
a84f7fdd 52 if (strlen($content->content) < 1) {
53 return false;
54 }
55 $number = $content->content;
ed019b50 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)";
a84f7fdd 64 } else {
65 $str = $number;
66 }
67 return $str;
68 }
69 return false;
70 }
71
7900ecb0 72 function display_search_field($value = '') {
ed019b50 73 return '<input type="text" size="16" name="f_'.$this->field->id.'" value="'.$value.'" />';
7900ecb0 74 }
ec536d7a 75
7900ecb0 76 function parse_search_field() {
77 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
78 }
1adbd2c3 79
7900ecb0 80 // need to cast?
81 function generate_sql($tablealias, $value) {
1675129e
EL
82 global $DB;
83
e3487936 84 static $i=0;
85 $i++;
86 $name = "df_number_$i";
1675129e
EL
87 $varcharcontent = $DB->sql_compare_text("{$tablealias}.content");
88 return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharcontent = :$name) ", array($name=>$value));
7900ecb0 89 }
ed019b50 90
59f3b717 91 function get_sort_sql($fieldname) {
cbc08f3b 92 global $DB;
93 switch ($DB->get_db_family()) {
ed019b50 94 case 'mysql':
95 // string in an arithmetic operation is converted to a floating-point number
aab98aaf 96 return '('.$fieldname.'+0.0)';
ed019b50 97 case 'postgres':
98 // cast for PG
aab98aaf 99 return 'CAST('.$fieldname.' AS REAL)';
ed019b50 100 default:
101 // the rest, just the field name. TODO: Analyse behaviour under MSSQL and Oracle
a4bad45c 102 return $fieldname;
59f3b717 103 }
104 }
105
106}
107
1adbd2c3 108