[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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_multimenu extends data_field_base { 26 27 var $type = 'multimenu'; 28 29 function display_add_field($recordid = 0, $formdata = null) { 30 global $DB, $OUTPUT; 31 32 if ($formdata) { 33 $fieldname = 'field_' . $this->field->id; 34 if (isset($formdata->$fieldname)) { 35 $content = $formdata->$fieldname; 36 } else { 37 $content = array(); 38 } 39 } else if ($recordid) { 40 $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid)); 41 $content = explode('##', $content); 42 } else { 43 $content = array(); 44 } 45 46 $str = '<div title="'.s($this->field->description).'">'; 47 $str .= '<input name="field_' . $this->field->id . '[xxx]" type="hidden" value="xxx"/>'; // hidden field - needed for empty selection 48 49 $str .= '<label for="field_' . $this->field->id . '">'; 50 $str .= html_writer::span($this->field->name, 'accesshide'); 51 if ($this->field->required) { 52 $str .= '<div class="inline-req">'; 53 $str .= html_writer::img($OUTPUT->pix_url('req'), get_string('requiredelement', 'form'), 54 array('class' => 'req', 'title' => get_string('requiredelement', 'form'))); 55 $str .= '</div>'; 56 } 57 $str .= '</label>'; 58 $str .= '<select name="field_' . $this->field->id . '[]" id="field_' . $this->field->id . '"'; 59 $str .= ' multiple="multiple" class="mod-data-input">'; 60 61 foreach (explode("\n", $this->field->param1) as $option) { 62 $option = trim($option); 63 $str .= '<option value="' . s($option) . '"'; 64 65 if (in_array($option, $content)) { 66 // Selected by user. 67 $str .= ' selected = "selected"'; 68 } 69 70 $str .= '>'; 71 $str .= $option . '</option>'; 72 } 73 $str .= '</select>'; 74 $str .= '</div>'; 75 76 return $str; 77 } 78 79 function display_search_field($value = '') { 80 global $CFG, $DB; 81 82 if (is_array($value)){ 83 $content = $value['selected']; 84 $allrequired = $value['allrequired'] ? true : false; 85 } else { 86 $content = array(); 87 $allrequired = false; 88 } 89 90 static $c = 0; 91 92 $str = '<label class="accesshide" for="f_' . $this->field->id . '">' . $this->field->name . '</label>'; 93 $str .= '<select id="f_'.$this->field->id.'" name="f_'.$this->field->id.'[]" multiple="multiple">'; 94 95 // display only used options 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 $valuestr = $data->content; 105 if ($valuestr === '') { 106 continue; 107 } 108 $values = explode('##', $valuestr); 109 foreach ($values as $value) { 110 $usedoptions[$value] = $value; 111 } 112 } 113 } 114 115 $found = false; 116 foreach (explode("\n",$this->field->param1) as $option) { 117 $option = trim($option); 118 if (!isset($usedoptions[$option])) { 119 continue; 120 } 121 $found = true; 122 $str .= '<option value="' . s($option) . '"'; 123 124 if (in_array($option, $content)) { 125 // Selected by user. 126 $str .= ' selected = "selected"'; 127 } 128 $str .= '>' . $option . '</option>'; 129 } 130 if (!$found) { 131 // oh, nothing to search for 132 return ''; 133 } 134 135 $str .= '</select>'; 136 137 $str .= html_writer::checkbox('f_'.$this->field->id.'_allreq', null, $allrequired, get_string('selectedrequired', 'data')); 138 139 return $str; 140 141 } 142 143 function parse_search_field() { 144 $selected = optional_param_array('f_'.$this->field->id, array(), PARAM_NOTAGS); 145 $allrequired = optional_param('f_'.$this->field->id.'_allreq', 0, PARAM_BOOL); 146 if (empty($selected)) { 147 // no searching 148 return ''; 149 } 150 return array('selected'=>$selected, 'allrequired'=>$allrequired); 151 } 152 153 function generate_sql($tablealias, $value) { 154 global $DB; 155 156 static $i=0; 157 $i++; 158 $name = "df_multimenu_{$i}_"; 159 $params = array(); 160 $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255); 161 162 $allrequired = $value['allrequired']; 163 $selected = $value['selected']; 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_multimenu_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 format_data_field_multimenu_content($content) { 209 if (!is_array($content)) { 210 return NULL; 211 } 212 $options = explode("\n", $this->field->param1); 213 $options = array_map('trim', $options); 214 215 $vals = array(); 216 foreach ($content as $key=>$val) { 217 if ($key === 'xxx') { 218 continue; 219 } 220 if (!in_array($val, $options)) { 221 continue; 222 } 223 $vals[] = $val; 224 } 225 226 if (empty($vals)) { 227 return NULL; 228 } 229 230 return implode('##', $vals); 231 } 232 233 234 function display_browse_field($recordid, $template) { 235 global $DB; 236 237 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 238 if (strval($content->content) === '') { 239 return false; 240 } 241 242 $options = explode("\n",$this->field->param1); 243 $options = array_map('trim', $options); 244 245 $contentArr = explode('##', $content->content); 246 $str = ''; 247 foreach ($contentArr as $line) { 248 if (!in_array($line, $options)) { 249 // hmm, looks like somebody edited the field definition 250 continue; 251 } 252 $str .= $line . "<br />\n"; 253 } 254 return $str; 255 } 256 return false; 257 } 258 259 /** 260 * Check if a field from an add form is empty 261 * 262 * @param mixed $value 263 * @param mixed $name 264 * @return bool 265 */ 266 function notemptyfield($value, $name) { 267 unset($value['xxx']); 268 return !empty($value); 269 } 270 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |