80069ace045a3621daf2efa3b2f89bc4fb5b3967
[moodle.git] / mod / data / field / menu / field.class.php
1 <?php
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_menu extends data_field_base {
27     var $type = 'menu';
29     function display_add_field($recordid=0) {
30         global $DB, $OUTPUT;
32         if ($recordid){
33             $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
34             $content = trim($content);
35         } else {
36             $content = '';
37         }
39         $str = '<div title="'.s($this->field->description).'">';
41         $rawoptions = explode("\n",$this->field->param1);
42         foreach ($rawoptions as $option) {
43             $option = trim($option);
44             if ($option) {
45                 $options[$option] = $option;
46             }
47         }
50         $str .= html_writer::select($options, 'field_'.$this->field->id, $content, array(''=>get_string('menuchoose', 'data')), array('id'=>'field_'.$this->field->id));
52         $str .= '</div>';
54         return $str;
55     }
57     function display_search_field($content = '') {
58         global $CFG, $DB, $OUTPUT;
60         $usedoptions = array();
61         $sql = "SELECT DISTINCT content
62                   FROM {data_content}
63                  WHERE fieldid=: AND content IS NOT NULL";
64         if ($used = $DB->get_records_sql($sql, array($this->field->id))) {
65             foreach ($used as $data) {
66                 $value = $data->content;
67                 if ($value === '') {
68                     continue;
69                 }
70                 $usedoptions[$value] = $value;
71             }
72         }
74         $options = array();
75         foreach (explode("\n",$this->field->param1) as $option) {
76             $option = trim($option);
77             if (!isset($usedoptions[$option])) {
78                 continue;
79             }
80             $options[$option] = $option;
81         }
82         if (!$options) {
83             // oh, nothing to search for
84             return '';
85         }
87         return html_writer::select($options, 'f_'.$this->field->id, $content, array(''=>'&nbsp;'));
88     }
90      function parse_search_field() {
91             return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
92      }
94     function generate_sql($tablealias, $value) {
95         static $i=0;
96         $i++;
97         $name = "df_menu_$i";
98         return array(" ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = :$name) ", array($name=>$value));
99     }