MDL-57920 mod_data: Refactor search array creation
[moodle.git] / mod / data / field / radiobutton / field.class.php
blob2cf8827dd2c3ba98a5b9e040c824ff46d4bdd8e1
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_radiobutton extends data_field_base {
27 var $type = 'radiobutton';
28 /**
29 * priority for globalsearch indexing
31 * @var int
33 protected static $priority = self::HIGH_PRIORITY;
35 function display_add_field($recordid = 0, $formdata = null) {
36 global $CFG, $DB, $OUTPUT;
38 if ($formdata) {
39 $fieldname = 'field_' . $this->field->id;
40 if (isset($formdata->$fieldname)) {
41 $content = $formdata->$fieldname;
42 } else {
43 $content = '';
45 } else if ($recordid) {
46 $content = trim($DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid)));
47 } else {
48 $content = '';
51 $str = '<div title="' . s($this->field->description) . '">';
52 $str .= '<fieldset><legend><span class="accesshide">' . $this->field->name;
54 if ($this->field->required) {
55 $str .= '&nbsp;' . get_string('requiredelement', 'form') . '</span></legend>';
56 $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
57 $str .= html_writer::div($image, 'inline-req');
58 } else {
59 $str .= '</span></legend>';
62 $i = 0;
63 $requiredstr = '';
64 $options = explode("\n", $this->field->param1);
65 foreach ($options as $radio) {
66 $radio = trim($radio);
67 if ($radio === '') {
68 continue; // skip empty lines
70 $str .= '<input type="radio" id="field_'.$this->field->id.'_'.$i.'" name="field_' . $this->field->id . '" ';
71 $str .= 'value="' . s($radio) . '" class="mod-data-input m-r-1" ';
73 if ($content == $radio) {
74 // Selected by user.
75 $str .= 'checked />';
76 } else {
77 $str .= '/>';
80 $str .= '<label for="field_'.$this->field->id.'_'.$i.'">'.$radio.'</label><br />';
81 $i++;
83 $str .= '</fieldset>';
84 $str .= '</div>';
85 return $str;
88 function display_search_field($value = '') {
89 global $CFG, $DB;
91 $varcharcontent = $DB->sql_compare_text('content', 255);
92 $used = $DB->get_records_sql(
93 "SELECT DISTINCT $varcharcontent AS content
94 FROM {data_content}
95 WHERE fieldid=?
96 ORDER BY $varcharcontent", array($this->field->id));
98 $options = array();
99 if(!empty($used)) {
100 foreach ($used as $rec) {
101 $options[$rec->content] = $rec->content; //Build following indicies from the sql.
104 $return = html_writer::label(get_string('fieldtypelabel', "datafield_" . $this->type),
105 'menuf_' . $this->field->id, false, array('class' => 'accesshide'));
106 $return .= html_writer::select($options, 'f_'.$this->field->id, $value, null, array('class' => 'custom-select'));
107 return $return;
110 public function parse_search_field($defaults = null) {
111 $param = 'f_'.$this->field->id;
112 if (empty($defaults[$param])) {
113 $defaults = array($param => '');
115 return optional_param($param, $defaults[$param], PARAM_NOTAGS);
118 function generate_sql($tablealias, $value) {
119 global $DB;
121 static $i=0;
122 $i++;
123 $name = "df_radiobutton_$i";
124 $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);
126 return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharcontent = :$name) ", array($name=>$value));
130 * Check if a field from an add form is empty
132 * @param mixed $value
133 * @param mixed $name
134 * @return bool
136 function notemptyfield($value, $name) {
137 return strval($value) !== '';
141 * Return the plugin configs for external functions.
143 * @return array the list of config parameters
144 * @since Moodle 3.3
146 public function get_config_for_external() {
147 // Return all the config parameters.
148 $configs = [];
149 for ($i = 1; $i <= 10; $i++) {
150 $configs["param$i"] = $this->field->{"param$i"};
152 return $configs;