|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00003 // // 00004 // NOTICE OF COPYRIGHT // 00005 // // 00006 // Moodle - Modular Object-Oriented Dynamic Learning Environment // 00007 // http://moodle.org // 00008 // // 00009 // Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com // 00010 // // 00011 // This program is free software; you can redistribute it and/or modify // 00012 // it under the terms of the GNU General Public License as published by // 00013 // the Free Software Foundation; either version 2 of the License, or // 00014 // (at your option) any later version. // 00015 // // 00016 // This program is distributed in the hope that it will be useful, // 00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of // 00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 00019 // GNU General Public License for more details: // 00020 // // 00021 // http://www.gnu.org/copyleft/gpl.html // 00022 // // 00024 00025 class data_field_number extends data_field_base { 00026 var $type = 'number'; 00027 00028 function update_content($recordid, $value, $name='') { 00029 global $DB; 00030 00031 $content = new stdClass(); 00032 $content->fieldid = $this->field->id; 00033 $content->recordid = $recordid; 00034 $value = trim($value); 00035 if (strlen($value) > 0) { 00036 $content->content = floatval($value); 00037 } else { 00038 $content->content = null; 00039 } 00040 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 00041 $content->id = $oldcontent->id; 00042 return $DB->update_record('data_content', $content); 00043 } else { 00044 return $DB->insert_record('data_content', $content); 00045 } 00046 } 00047 00048 function display_browse_field($recordid, $template) { 00049 global $DB; 00050 00051 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 00052 if (strlen($content->content) < 1) { 00053 return false; 00054 } 00055 $number = $content->content; 00056 $decimals = trim($this->field->param1); 00057 // only apply number formatting if param1 contains an integer number >= 0: 00058 if (preg_match("/^\d+$/", $decimals)) { 00059 $decimals = $decimals * 1; 00060 // removes leading zeros (eg. '007' -> '7'; '00' -> '0') 00061 $str = format_float($number, $decimals, true); 00062 // For debugging only: 00063 # $str .= " ($decimals)"; 00064 } else { 00065 $str = $number; 00066 } 00067 return $str; 00068 } 00069 return false; 00070 } 00071 00072 function display_search_field($value = '') { 00073 return '<input type="text" size="16" name="f_'.$this->field->id.'" value="'.$value.'" />'; 00074 } 00075 00076 function parse_search_field() { 00077 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS); 00078 } 00079 00080 // need to cast? 00081 function generate_sql($tablealias, $value) { 00082 global $DB; 00083 00084 static $i=0; 00085 $i++; 00086 $name = "df_number_$i"; 00087 $varcharcontent = $DB->sql_compare_text("{$tablealias}.content"); 00088 return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharcontent = :$name) ", array($name=>$value)); 00089 } 00090 00091 function get_sort_sql($fieldname) { 00092 global $DB; 00093 return $DB->sql_cast_char2real($fieldname, true); 00094 } 00095 00096 } 00097 00098