MDL-57807 mod_data: Advanced search with menu item.
[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';
2af9ad7d
DG
28 /**
29 * priority for globalsearch indexing
30 *
31 * @var int
32 */
33 protected static $priority = self::HIGH_PRIORITY;
bfadc7ef 34
b89cca19 35 function display_add_field($recordid = 0, $formdata = null) {
601104f2 36 global $DB, $OUTPUT;
bfadc7ef 37
b89cca19
DW
38 if ($formdata) {
39 $fieldname = 'field_' . $this->field->id;
40 $content = $formdata->$fieldname;
1c3b2058 41 } else if ($recordid) {
a656d951 42 $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
0997e51a 43 $content = trim($content);
44 } else {
45 $content = '';
bfadc7ef 46 }
1c3b2058 47 $str = '<div title="' . s($this->field->description) . '">';
0997e51a 48
476999c7 49 $options = array();
505d3123 50 $rawoptions = explode("\n",$this->field->param1);
51 foreach ($rawoptions as $option) {
0997e51a 52 $option = trim($option);
f4a752a2 53 if (strlen($option) > 0) {
505d3123 54 $options[$option] = $option;
55 }
bfadc7ef 56 }
1adbd2c3 57
1c3b2058
JO
58 $str .= '<label for="' . 'field_' . $this->field->id . '">';
59 $str .= html_writer::span($this->field->name, 'accesshide');
b89cca19 60 if ($this->field->required) {
663640f5 61 $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
c07c5e7b 62 $str .= html_writer::div($image, 'inline-req');
b89cca19 63 }
1c3b2058 64 $str .= '</label>';
c07c5e7b 65 $str .= html_writer::select($options, 'field_'.$this->field->id, $content, array('' => get_string('menuchoose', 'data')),
e85fda95 66 array('id' => 'field_'.$this->field->id, 'class' => 'mod-data-input custom-select'));
bfadc7ef 67
a7ee8d45 68 $str .= '</div>';
bfadc7ef 69
0997e51a 70 return $str;
bfadc7ef 71 }
1adbd2c3 72
a4e3818f 73 function display_search_field($content = '') {
eca89126 74 global $CFG, $DB;
a4e3818f 75
eca89126
EL
76 $varcharcontent = $DB->sql_compare_text('content', 255);
77 $sql = "SELECT DISTINCT $varcharcontent AS content
a656d951 78 FROM {data_content}
71762d46 79 WHERE fieldid=? AND content IS NOT NULL";
eca89126
EL
80
81 $usedoptions = array();
a656d951 82 if ($used = $DB->get_records_sql($sql, array($this->field->id))) {
a4e3818f 83 foreach ($used as $data) {
84 $value = $data->content;
85 if ($value === '') {
86 continue;
87 }
88 $usedoptions[$value] = $value;
89 }
90 }
91
7900ecb0 92 $options = array();
a4e3818f 93 foreach (explode("\n",$this->field->param1) as $option) {
94 $option = trim($option);
95 if (!isset($usedoptions[$option])) {
96 continue;
7900ecb0 97 }
a4e3818f 98 $options[$option] = $option;
99 }
100 if (!$options) {
101 // oh, nothing to search for
102 return '';
7900ecb0 103 }
a4e3818f 104
b68de8cf
RT
105 $return = html_writer::label(get_string('fieldtypelabel', "datafield_" . $this->type),
106 'menuf_' . $this->field->id, false, array('class' => 'accesshide'));
0a497898
AG
107 $return .= html_writer::select($options, 'f_'.$this->field->id, $content, array('' => get_string('menuchoose', 'data')),
108 array('class' => 'custom-select'));
01f8e80d 109 return $return;
7900ecb0 110 }
111
c8a804ff
JL
112 public function parse_search_field($defaults = null) {
113 $param = 'f_'.$this->field->id;
114 if (empty($defaults[$param])) {
115 $defaults = array($param => '');
116 }
117 return optional_param($param, $defaults[$param], PARAM_NOTAGS);
118 }
7900ecb0 119
120 function generate_sql($tablealias, $value) {
be4d5a92
EL
121 global $DB;
122
e3487936 123 static $i=0;
124 $i++;
125 $name = "df_menu_$i";
be4d5a92
EL
126 $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);
127
128 return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharcontent = :$name) ", array($name=>$value));
7900ecb0 129 }
0997e51a 130
9b5cf165
AG
131 /**
132 * Check if a field from an add form is empty
133 *
134 * @param mixed $value
135 * @param mixed $name
136 * @return bool
137 */
138 function notemptyfield($value, $name) {
139 return strval($value) !== '';
140 }
141
aefe9c5f
JL
142 /**
143 * Return the plugin configs for external functions.
144 *
145 * @return array the list of config parameters
146 * @since Moodle 3.3
147 */
148 public function get_config_for_external() {
149 // Return all the config parameters.
150 $configs = [];
151 for ($i = 1; $i <= 10; $i++) {
152 $configs["param$i"] = $this->field->{"param$i"};
153 }
154 return $configs;
155 }
bfadc7ef 156}