2 ///////////////////////////////////////////////////////////////////////////
4 // NOTICE OF COPYRIGHT //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
9 // Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com //
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. //
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: //
21 // http://www.gnu.org/copyleft/gpl.html //
23 ///////////////////////////////////////////////////////////////////////////
25 class data_field_radiobutton
extends data_field_base
{
27 var $type = 'radiobutton';
29 * priority for globalsearch indexing
33 protected static $priority = self
::HIGH_PRIORITY
;
35 function display_add_field($recordid = 0, $formdata = null) {
36 global $CFG, $DB, $OUTPUT;
39 $fieldname = 'field_' . $this->field
->id
;
40 if (isset($formdata->$fieldname)) {
41 $content = $formdata->$fieldname;
45 } else if ($recordid) {
46 $content = trim($DB->get_field('data_content', 'content', array('fieldid'=>$this->field
->id
, 'recordid'=>$recordid)));
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 .= ' ' . get_string('requiredelement', 'form') . '</span></legend>';
56 $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
57 $str .= html_writer
::div($image, 'inline-req');
59 $str .= '</span></legend>';
64 $options = explode("\n", $this->field
->param1
);
65 foreach ($options as $radio) {
66 $radio = trim($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) {
80 $str .= '<label for="field_'.$this->field
->id
.'_'.$i.'">'.$radio.'</label><br />';
83 $str .= '</fieldset>';
88 function display_search_field($value = '') {
91 $varcharcontent = $DB->sql_compare_text('content', 255);
92 $used = $DB->get_records_sql(
93 "SELECT DISTINCT $varcharcontent AS content
96 ORDER BY $varcharcontent", array($this->field
->id
));
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'));
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) {
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
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
146 public function get_config_for_external() {
147 // Return all the config parameters.
149 for ($i = 1; $i <= 10; $i++
) {
150 $configs["param$i"] = $this->field
->{"param$i"};