Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]
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_checkbox extends data_field_base { 26 27 var $type = 'checkbox'; 28 /** 29 * priority for globalsearch indexing 30 * 31 * @var int 32 */ 33 protected static $priority = self::LOW_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', ['fieldid' => $this->field->id, 'recordid' => $recordid]); 63 $content = explode('##', $content); 64 } else { 65 $content = []; 66 } 67 68 $str = '<div title="' . s($this->field->description) . '">'; 69 $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name; 70 if ($this->field->required) { 71 $str .= '$nbsp;' . get_string('requiredelement', 'form'); 72 $str .= '</span></legend>'; 73 $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form')); 74 $str .= html_writer::div($image, 'inline-req'); 75 } else { 76 $str .= '</span></legend>'; 77 } 78 79 $i = 0; 80 foreach (explode("\n", $this->field->param1) as $checkbox) { 81 $checkbox = trim($checkbox); 82 if ($checkbox === '') { 83 continue; // skip empty lines 84 } 85 $str .= '<input type="hidden" name="field_' . $this->field->id . '[]" value="" />'; 86 $str .= '<input type="checkbox" id="field_'.$this->field->id.'_'.$i.'" name="field_' . $this->field->id . '[]" '; 87 $str .= 'value="' . s($checkbox) . '" class="mod-data-input mr-1" '; 88 89 if (array_search($checkbox, $content) !== false) { 90 $str .= 'checked />'; 91 } else { 92 $str .= '/>'; 93 } 94 $str .= '<label for="field_'.$this->field->id.'_'.$i.'">'.$checkbox.'</label><br />'; 95 $i++; 96 } 97 $str .= '</fieldset>'; 98 $str .= '</div>'; 99 return $str; 100 } 101 102 function display_search_field($value='') { 103 global $CFG, $DB; 104 105 if (is_array($value)) { 106 $content = $value['checked']; 107 $allrequired = $value['allrequired'] ? true : false; 108 } else { 109 $content = array(); 110 $allrequired = false; 111 } 112 113 $str = ''; 114 $found = false; 115 $marginclass = ['class' => 'mr-1']; 116 foreach (explode("\n",$this->field->param1) as $checkbox) { 117 $checkbox = trim($checkbox); 118 if (in_array($checkbox, $content)) { 119 $str .= html_writer::checkbox('f_'.$this->field->id.'[]', s($checkbox), true, $checkbox, $marginclass); 120 } else { 121 $str .= html_writer::checkbox('f_'.$this->field->id.'[]', s($checkbox), false, $checkbox, $marginclass); 122 } 123 $str .= html_writer::empty_tag('br'); 124 $found = true; 125 } 126 if (!$found) { 127 return ''; 128 } 129 130 $requiredstr = get_string('selectedrequired', 'data'); 131 $str .= html_writer::checkbox('f_'.$this->field->id.'_allreq', null, $allrequired, $requiredstr, $marginclass); 132 return $str; 133 } 134 135 public function parse_search_field($defaults = null) { 136 $paramselected = 'f_'.$this->field->id; 137 $paramallrequired = 'f_'.$this->field->id.'_allreq'; 138 139 if (empty($defaults[$paramselected])) { // One empty means the other ones are empty too. 140 $defaults = array($paramselected => array(), $paramallrequired => 0); 141 } 142 143 $selected = optional_param_array($paramselected, $defaults[$paramselected], PARAM_NOTAGS); 144 $allrequired = optional_param($paramallrequired, $defaults[$paramallrequired], PARAM_BOOL); 145 146 if (empty($selected)) { 147 // no searching 148 return ''; 149 } 150 return array('checked'=>$selected, 'allrequired'=>$allrequired); 151 } 152 153 function generate_sql($tablealias, $value) { 154 global $DB; 155 156 static $i=0; 157 $i++; 158 $name = "df_checkbox_{$i}_"; 159 $params = array(); 160 $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255); 161 162 $allrequired = $value['allrequired']; 163 $selected = $value['checked']; 164 165 if ($selected) { 166 $conditions = array(); 167 $j=0; 168 foreach ($selected as $sel) { 169 $j++; 170 $xname = $name.$j; 171 $likesel = str_replace('%', '\%', $sel); 172 $likeselsel = str_replace('_', '\_', $likesel); 173 $conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$varcharcontent} = :{$xname}a 174 OR {$tablealias}.content LIKE :{$xname}b 175 OR {$tablealias}.content LIKE :{$xname}c 176 OR {$tablealias}.content LIKE :{$xname}d))"; 177 $params[$xname.'a'] = $sel; 178 $params[$xname.'b'] = "$likesel##%"; 179 $params[$xname.'c'] = "%##$likesel"; 180 $params[$xname.'d'] = "%##$likesel##%"; 181 } 182 if ($allrequired) { 183 return array(" (".implode(" AND ", $conditions).") ", $params); 184 } else { 185 return array(" (".implode(" OR ", $conditions).") ", $params); 186 } 187 } else { 188 return array(" ", array()); 189 } 190 } 191 192 function update_content($recordid, $value, $name='') { 193 global $DB; 194 195 $content = new stdClass(); 196 $content->fieldid = $this->field->id; 197 $content->recordid = $recordid; 198 $content->content = $this->format_data_field_checkbox_content($value); 199 200 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 201 $content->id = $oldcontent->id; 202 return $DB->update_record('data_content', $content); 203 } else { 204 return $DB->insert_record('data_content', $content); 205 } 206 } 207 208 function display_browse_field($recordid, $template) { 209 $content = $this->get_data_content($recordid); 210 if (!$content || empty($content->content)) { 211 return ''; 212 } 213 214 $options = explode("\n", $this->field->param1); 215 $options = array_map('trim', $options); 216 217 $contentarray = explode('##', $content->content); 218 $str = ''; 219 foreach ($contentarray as $line) { 220 if (!in_array($line, $options)) { 221 // Hmm, looks like somebody edited the field definition. 222 continue; 223 } 224 $str .= $line . "<br />\n"; 225 } 226 return $str; 227 } 228 229 function format_data_field_checkbox_content($content) { 230 if (!is_array($content)) { 231 return NULL; 232 } 233 $options = explode("\n", $this->field->param1); 234 $options = array_map('trim', $options); 235 236 $vals = array(); 237 foreach ($content as $key=>$val) { 238 if ($key === 'xxx') { 239 continue; 240 } 241 if (!in_array($val, $options)) { 242 continue; 243 244 } 245 $vals[] = $val; 246 } 247 248 if (empty($vals)) { 249 return NULL; 250 } 251 252 return implode('##', $vals); 253 } 254 255 /** 256 * Check whether any boxes in the checkbox where checked. 257 * 258 * @param mixed $value The submitted values 259 * @param mixed $name 260 * @return bool 261 */ 262 function notemptyfield($value, $name) { 263 $found = false; 264 foreach ($value as $checkboxitem) { 265 if (strval($checkboxitem) !== '') { 266 $found = true; 267 break; 268 } 269 } 270 return $found; 271 } 272 273 /** 274 * Returns the presentable string value for a field content. 275 * 276 * The returned string should be plain text. 277 * 278 * @param stdClass $content 279 * @return string 280 */ 281 public static function get_content_value($content) { 282 $arr = explode('##', $content->content); 283 284 $strvalue = ''; 285 foreach ($arr as $a) { 286 $strvalue .= $a . ' '; 287 } 288 289 return trim($strvalue, "\r\n "); 290 } 291 292 /** 293 * Return the plugin configs for external functions. 294 * 295 * @return array the list of config parameters 296 * @since Moodle 3.3 297 */ 298 public function get_config_for_external() { 299 // Return all the config parameters. 300 $configs = []; 301 for ($i = 1; $i <= 10; $i++) { 302 $configs["param$i"] = $this->field->{"param$i"}; 303 } 304 return $configs; 305 } 306 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body