1 |
efrain |
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 |
///////////////////////////////////////////////////////////////////////////
|
|
|
24 |
|
|
|
25 |
class data_field_menu extends data_field_base {
|
|
|
26 |
|
|
|
27 |
var $type = 'menu';
|
|
|
28 |
/**
|
|
|
29 |
* priority for globalsearch indexing
|
|
|
30 |
*
|
|
|
31 |
* @var int
|
|
|
32 |
*/
|
|
|
33 |
protected static $priority = self::HIGH_PRIORITY;
|
|
|
34 |
|
|
|
35 |
public function supports_preview(): bool {
|
|
|
36 |
return true;
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
public function get_data_content_preview(int $recordid): stdClass {
|
|
|
40 |
$options = explode("\n", $this->field->param1);
|
|
|
41 |
$options = array_map('trim', $options);
|
|
|
42 |
$selected = $options[$recordid % count($options)];
|
|
|
43 |
return (object)[
|
|
|
44 |
'id' => 0,
|
|
|
45 |
'fieldid' => $this->field->id,
|
|
|
46 |
'recordid' => $recordid,
|
|
|
47 |
'content' => $selected,
|
|
|
48 |
'content1' => null,
|
|
|
49 |
'content2' => null,
|
|
|
50 |
'content3' => null,
|
|
|
51 |
'content4' => null,
|
|
|
52 |
];
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
function display_add_field($recordid = 0, $formdata = null) {
|
|
|
56 |
global $DB, $OUTPUT;
|
|
|
57 |
|
|
|
58 |
if ($formdata) {
|
|
|
59 |
$fieldname = 'field_' . $this->field->id;
|
|
|
60 |
$content = $formdata->$fieldname;
|
|
|
61 |
} else if ($recordid) {
|
|
|
62 |
$content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
|
|
|
63 |
$content = trim($content);
|
|
|
64 |
} else {
|
|
|
65 |
$content = '';
|
|
|
66 |
}
|
|
|
67 |
$str = '<div title="' . s($this->field->description) . '">';
|
|
|
68 |
|
|
|
69 |
$options = array();
|
|
|
70 |
$rawoptions = explode("\n",$this->field->param1);
|
|
|
71 |
foreach ($rawoptions as $option) {
|
|
|
72 |
$option = trim($option);
|
|
|
73 |
if (strlen($option) > 0) {
|
|
|
74 |
$options[$option] = $option;
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
$str .= '<label for="' . 'field_' . $this->field->id . '">';
|
|
|
79 |
$str .= html_writer::span($this->field->name, 'accesshide');
|
|
|
80 |
if ($this->field->required) {
|
|
|
81 |
$image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
|
|
|
82 |
$str .= html_writer::div($image, 'inline-req');
|
|
|
83 |
}
|
|
|
84 |
$str .= '</label>';
|
|
|
85 |
$str .= html_writer::select($options, 'field_'.$this->field->id, $content, array('' => get_string('menuchoose', 'data')),
|
|
|
86 |
array('id' => 'field_'.$this->field->id, 'class' => 'mod-data-input custom-select'));
|
|
|
87 |
|
|
|
88 |
$str .= '</div>';
|
|
|
89 |
|
|
|
90 |
return $str;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
function display_search_field($content = '') {
|
|
|
94 |
global $CFG, $DB;
|
|
|
95 |
|
|
|
96 |
$varcharcontent = $DB->sql_compare_text('content', 255);
|
|
|
97 |
$sql = "SELECT DISTINCT $varcharcontent AS content
|
|
|
98 |
FROM {data_content}
|
|
|
99 |
WHERE fieldid=? AND content IS NOT NULL";
|
|
|
100 |
|
|
|
101 |
$usedoptions = array();
|
|
|
102 |
if ($used = $DB->get_records_sql($sql, array($this->field->id))) {
|
|
|
103 |
foreach ($used as $data) {
|
|
|
104 |
$value = $data->content;
|
|
|
105 |
if ($value === '') {
|
|
|
106 |
continue;
|
|
|
107 |
}
|
|
|
108 |
$usedoptions[$value] = $value;
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
$options = array();
|
|
|
113 |
foreach (explode("\n",$this->field->param1) as $option) {
|
|
|
114 |
$option = trim($option);
|
|
|
115 |
if (!isset($usedoptions[$option])) {
|
|
|
116 |
continue;
|
|
|
117 |
}
|
|
|
118 |
$options[$option] = $option;
|
|
|
119 |
}
|
|
|
120 |
if (!$options) {
|
|
|
121 |
// oh, nothing to search for
|
|
|
122 |
return '';
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
$return = html_writer::label(get_string('fieldtypelabel', "datafield_" . $this->type),
|
|
|
126 |
'menuf_' . $this->field->id, false, array('class' => 'accesshide'));
|
|
|
127 |
$return .= html_writer::select($options, 'f_'.$this->field->id, $content, array('' => get_string('menuchoose', 'data')),
|
|
|
128 |
array('class' => 'custom-select'));
|
|
|
129 |
return $return;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
public function parse_search_field($defaults = null) {
|
|
|
133 |
$param = 'f_'.$this->field->id;
|
|
|
134 |
if (empty($defaults[$param])) {
|
|
|
135 |
$defaults = array($param => '');
|
|
|
136 |
}
|
|
|
137 |
return optional_param($param, $defaults[$param], PARAM_NOTAGS);
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
function generate_sql($tablealias, $value) {
|
|
|
141 |
global $DB;
|
|
|
142 |
|
|
|
143 |
static $i=0;
|
|
|
144 |
$i++;
|
|
|
145 |
$name = "df_menu_$i";
|
|
|
146 |
$varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);
|
|
|
147 |
|
|
|
148 |
return array(" ({$tablealias}.fieldid = {$this->field->id} AND $varcharcontent = :$name) ", array($name=>$value));
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
* Check if a field from an add form is empty
|
|
|
153 |
*
|
|
|
154 |
* @param mixed $value
|
|
|
155 |
* @param mixed $name
|
|
|
156 |
* @return bool
|
|
|
157 |
*/
|
|
|
158 |
function notemptyfield($value, $name) {
|
|
|
159 |
return strval($value) !== '';
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
/**
|
|
|
163 |
* Return the plugin configs for external functions.
|
|
|
164 |
*
|
|
|
165 |
* @return array the list of config parameters
|
|
|
166 |
* @since Moodle 3.3
|
|
|
167 |
*/
|
|
|
168 |
public function get_config_for_external() {
|
|
|
169 |
// Return all the config parameters.
|
|
|
170 |
$configs = [];
|
|
|
171 |
for ($i = 1; $i <= 10; $i++) {
|
|
|
172 |
$configs["param$i"] = $this->field->{"param$i"};
|
|
|
173 |
}
|
|
|
174 |
return $configs;
|
|
|
175 |
}
|
|
|
176 |
}
|