mod-data MDLSITE-970 Raarrrrrrrr fixed way old bug from dml conversion
[moodle.git] / mod / data / field / menu / field.class.php
CommitLineData
1adbd2c3 1<?php
bfadc7ef 2///////////////////////////////////////////////////////////////////////////
3// //
4// NOTICE OF COPYRIGHT //
5// //
6// Moodle - Modular Object-Oriented Dynamic Learning Environment //
7// http://moodle.org //
8// //
0997e51a 9// Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com //
bfadc7ef 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
bfadc7ef 25class data_field_menu extends data_field_base {
26
bfadc7ef 27 var $type = 'menu';
bfadc7ef 28
0997e51a 29 function display_add_field($recordid=0) {
601104f2 30 global $DB, $OUTPUT;
bfadc7ef 31
0997e51a 32 if ($recordid){
a656d951 33 $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
0997e51a 34 $content = trim($content);
35 } else {
36 $content = '';
bfadc7ef 37 }
0997e51a 38
e357c206 39 $str = '<div title="'.s($this->field->description).'">';
0997e51a 40
505d3123 41 $rawoptions = explode("\n",$this->field->param1);
42 foreach ($rawoptions as $option) {
0997e51a 43 $option = trim($option);
505d3123 44 if ($option) {
45 $options[$option] = $option;
46 }
bfadc7ef 47 }
1adbd2c3 48
bfadc7ef 49
d776d59e 50 $str .= html_writer::select($options, 'field_'.$this->field->id, $content, array(''=>get_string('menuchoose', 'data')), array('id'=>'field_'.$this->field->id));
bfadc7ef 51
a7ee8d45 52 $str .= '</div>';
bfadc7ef 53
0997e51a 54 return $str;
bfadc7ef 55 }
1adbd2c3 56
a4e3818f 57 function display_search_field($content = '') {
601104f2 58 global $CFG, $DB, $OUTPUT;
a4e3818f 59
60 $usedoptions = array();
61 $sql = "SELECT DISTINCT content
a656d951 62 FROM {data_content}
71762d46 63 WHERE fieldid=? AND content IS NOT NULL";
a656d951 64 if ($used = $DB->get_records_sql($sql, array($this->field->id))) {
a4e3818f 65 foreach ($used as $data) {
66 $value = $data->content;
67 if ($value === '') {
68 continue;
69 }
70 $usedoptions[$value] = $value;
71 }
72 }
73
7900ecb0 74 $options = array();
a4e3818f 75 foreach (explode("\n",$this->field->param1) as $option) {
76 $option = trim($option);
77 if (!isset($usedoptions[$option])) {
78 continue;
7900ecb0 79 }
a4e3818f 80 $options[$option] = $option;
81 }
82 if (!$options) {
83 // oh, nothing to search for
84 return '';
7900ecb0 85 }
a4e3818f 86
d776d59e 87 return html_writer::select($options, 'f_'.$this->field->id, $content, array(''=>'&nbsp;'));
7900ecb0 88 }
89
90 function parse_search_field() {
91 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
92 }
93
94 function generate_sql($tablealias, $value) {
e3487936 95 static $i=0;
96 $i++;
97 $name = "df_menu_$i";
1adbd2c3 98 return array(" ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = :$name) ", array($name=>$value));
7900ecb0 99 }
0997e51a 100
bfadc7ef 101}
102
1adbd2c3 103