[ Index ] |
PHP Cross Reference of moodle-2.8 |
[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) { 30 global $DB; 31 32 if ($recordid){ 33 $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid)); 34 $content = explode('##', $content); 35 } else { 36 $content = array(); 37 } 38 39 $str = '<div title="'.s($this->field->description).'">'; 40 $str .= '<input name="field_' . $this->field->id . '[xxx]" type="hidden" value="xxx"/>'; // hidden field - needed for empty selection 41 $str .= '<label class="accesshide" for="field_' . $this->field->id . '">' . $this->field->name. '</label>'; 42 $str .= '<select name="field_' . $this->field->id . '[]" id="field_' . $this->field->id . '" multiple="multiple">'; 43 44 foreach (explode("\n",$this->field->param1) as $option) { 45 $option = trim($option); 46 $str .= '<option value="' . s($option) . '"'; 47 48 if (in_array($option, $content)) { 49 // Selected by user. 50 $str .= ' selected = "selected"'; 51 } 52 53 $str .= '>'; 54 $str .= $option . '</option>'; 55 } 56 $str .= '</select>'; 57 $str .= '</div>'; 58 59 return $str; 60 } 61 62 function display_search_field($value = '') { 63 global $CFG, $DB; 64 65 if (is_array($value)){ 66 $content = $value['selected']; 67 $allrequired = $value['allrequired'] ? true : false; 68 } else { 69 $content = array(); 70 $allrequired = false; 71 } 72 73 static $c = 0; 74 75 $str = '<label class="accesshide" for="f_' . $this->field->id . '">' . $this->field->name . '</label>'; 76 $str .= '<select id="f_'.$this->field->id.'" name="f_'.$this->field->id.'[]" multiple="multiple">'; 77 78 // display only used options 79 $varcharcontent = $DB->sql_compare_text('content', 255); 80 $sql = "SELECT DISTINCT $varcharcontent AS content 81 FROM {data_content} 82 WHERE fieldid=? AND content IS NOT NULL"; 83 84 $usedoptions = array(); 85 if ($used = $DB->get_records_sql($sql, array($this->field->id))) { 86 foreach ($used as $data) { 87 $valuestr = $data->content; 88 if ($valuestr === '') { 89 continue; 90 } 91 $values = explode('##', $valuestr); 92 foreach ($values as $value) { 93 $usedoptions[$value] = $value; 94 } 95 } 96 } 97 98 $found = false; 99 foreach (explode("\n",$this->field->param1) as $option) { 100 $option = trim($option); 101 if (!isset($usedoptions[$option])) { 102 continue; 103 } 104 $found = true; 105 $str .= '<option value="' . s($option) . '"'; 106 107 if (in_array($option, $content)) { 108 // Selected by user. 109 $str .= ' selected = "selected"'; 110 } 111 $str .= '>' . $option . '</option>'; 112 } 113 if (!$found) { 114 // oh, nothing to search for 115 return ''; 116 } 117 118 $str .= '</select>'; 119 120 $str .= html_writer::checkbox('f_'.$this->field->id.'_allreq', null, $allrequired, get_string('selectedrequired', 'data')); 121 122 return $str; 123 124 } 125 126 function parse_search_field() { 127 $selected = optional_param_array('f_'.$this->field->id, array(), PARAM_NOTAGS); 128 $allrequired = optional_param('f_'.$this->field->id.'_allreq', 0, PARAM_BOOL); 129 if (empty($selected)) { 130 // no searching 131 return ''; 132 } 133 return array('selected'=>$selected, 'allrequired'=>$allrequired); 134 } 135 136 function generate_sql($tablealias, $value) { 137 global $DB; 138 139 static $i=0; 140 $i++; 141 $name = "df_multimenu_{$i}_"; 142 $params = array(); 143 $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255); 144 145 $allrequired = $value['allrequired']; 146 $selected = $value['selected']; 147 148 if ($selected) { 149 $conditions = array(); 150 $j=0; 151 foreach ($selected as $sel) { 152 $j++; 153 $xname = $name.$j; 154 $likesel = str_replace('%', '\%', $sel); 155 $likeselsel = str_replace('_', '\_', $likesel); 156 $conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$varcharcontent} = :{$xname}a 157 OR {$tablealias}.content LIKE :{$xname}b 158 OR {$tablealias}.content LIKE :{$xname}c 159 OR {$tablealias}.content LIKE :{$xname}d))"; 160 $params[$xname.'a'] = $sel; 161 $params[$xname.'b'] = "$likesel##%"; 162 $params[$xname.'c'] = "%##$likesel"; 163 $params[$xname.'d'] = "%##$likesel##%"; 164 } 165 if ($allrequired) { 166 return array(" (".implode(" AND ", $conditions).") ", $params); 167 } else { 168 return array(" (".implode(" OR ", $conditions).") ", $params); 169 } 170 } else { 171 return array(" ", array()); 172 } 173 } 174 175 function update_content($recordid, $value, $name='') { 176 global $DB; 177 178 $content = new stdClass(); 179 $content->fieldid = $this->field->id; 180 $content->recordid = $recordid; 181 $content->content = $this->format_data_field_multimenu_content($value); 182 183 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 184 $content->id = $oldcontent->id; 185 return $DB->update_record('data_content', $content); 186 } else { 187 return $DB->insert_record('data_content', $content); 188 } 189 } 190 191 function format_data_field_multimenu_content($content) { 192 if (!is_array($content)) { 193 return NULL; 194 } 195 $options = explode("\n", $this->field->param1); 196 $options = array_map('trim', $options); 197 198 $vals = array(); 199 foreach ($content as $key=>$val) { 200 if ($key === 'xxx') { 201 continue; 202 } 203 if (!in_array($val, $options)) { 204 continue; 205 } 206 $vals[] = $val; 207 } 208 209 if (empty($vals)) { 210 return NULL; 211 } 212 213 return implode('##', $vals); 214 } 215 216 217 function display_browse_field($recordid, $template) { 218 global $DB; 219 220 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 221 if (empty($content->content)) { 222 return false; 223 } 224 225 $options = explode("\n",$this->field->param1); 226 $options = array_map('trim', $options); 227 228 $contentArr = explode('##', $content->content); 229 $str = ''; 230 foreach ($contentArr as $line) { 231 if (!in_array($line, $options)) { 232 // hmm, looks like somebody edited the field definition 233 continue; 234 } 235 $str .= $line . "<br />\n"; 236 } 237 return $str; 238 } 239 return false; 240 } 241 } 242
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |