Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / data / field / menu / field.class.php
blob16aba4d0e30a935dd3c34551ae39e04e9c0d3103
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_menu extends data_field_base {
27 var $type = 'menu';
29 function data_field_menu($field=0, $data=0) {
30 parent::data_field_base($field, $data);
33 function display_add_field($recordid=0) {
35 if ($recordid){
36 $content = get_field('data_content', 'content', 'fieldid', $this->field->id, 'recordid', $recordid);
37 $content = trim($content);
38 } else {
39 $content = '';
42 $str = '<div title="'.s($this->field->description).'">';
44 $rawoptions = explode("\n",$this->field->param1);
45 foreach ($rawoptions as $option) {
46 $option = trim($option);
47 if ($option) {
48 $options[$option] = $option;
52 $str .= choose_from_menu($options, 'field_'.$this->field->id, $content,
53 get_string('menuchoose', 'data'), '', '', true, false, 0, 'field_'.$this->field->id);
55 $str .= '</div>';
57 return $str;
60 function display_search_field($content = '') {
61 global $CFG;
63 $usedoptions = array();
64 $sql = "SELECT DISTINCT content
65 FROM {$CFG->prefix}data_content
66 WHERE fieldid={$this->field->id} AND content IS NOT NULL";
67 if ($used = get_records_sql($sql)) {
68 foreach ($used as $data) {
69 $value = $data->content;
70 if ($value === '') {
71 continue;
73 $usedoptions[$value] = $value;
77 $options = array();
78 foreach (explode("\n",$this->field->param1) as $option) {
79 $option = trim($option);
80 if (!isset($usedoptions[$option])) {
81 continue;
83 $options[$option] = $option;
85 if (!$options) {
86 // oh, nothing to search for
87 return '';
90 return choose_from_menu($options, 'f_'.$this->field->id, stripslashes($content), '&nbsp;', '', 0, true);
93 function parse_search_field() {
94 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
97 function generate_sql($tablealias, $value) {
98 return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = '$value') ";