[ 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_checkbox extends data_field_base { 26 27 var $type = 'checkbox'; 28 29 function display_add_field($recordid=0) { 30 global $CFG, $DB; 31 32 $content = array(); 33 34 if ($recordid) { 35 $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid)); 36 $content = explode('##', $content); 37 } else { 38 $content = array(); 39 } 40 41 $str = '<div title="'.s($this->field->description).'">'; 42 $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>'; 43 44 $i = 0; 45 foreach (explode("\n", $this->field->param1) as $checkbox) { 46 $checkbox = trim($checkbox); 47 if ($checkbox === '') { 48 continue; // skip empty lines 49 } 50 $str .= '<input type="hidden" name="field_' . $this->field->id . '[]" value="" />'; 51 $str .= '<input type="checkbox" id="field_'.$this->field->id.'_'.$i.'" name="field_' . $this->field->id . '[]" '; 52 $str .= 'value="' . s($checkbox) . '" '; 53 54 if (array_search($checkbox, $content) !== false) { 55 $str .= 'checked />'; 56 } else { 57 $str .= '/>'; 58 } 59 $str .= '<label for="field_'.$this->field->id.'_'.$i.'">'.$checkbox.'</label><br />'; 60 $i++; 61 } 62 $str .= '</fieldset>'; 63 $str .= '</div>'; 64 return $str; 65 } 66 67 function display_search_field($value='') { 68 global $CFG, $DB; 69 70 if (is_array($value)) { 71 $content = $value['checked']; 72 $allrequired = $value['allrequired'] ? true : false; 73 } else { 74 $content = array(); 75 $allrequired = false; 76 } 77 78 $str = ''; 79 $found = false; 80 foreach (explode("\n",$this->field->param1) as $checkbox) { 81 $checkbox = trim($checkbox); 82 83 if (in_array($checkbox, $content)) { 84 $str .= html_writer::checkbox('f_'.$this->field->id.'[]', s($checkbox), true, $checkbox); 85 } else { 86 $str .= html_writer::checkbox('f_'.$this->field->id.'[]', s($checkbox), false, $checkbox); 87 } 88 $found = true; 89 } 90 if (!$found) { 91 return ''; 92 } 93 94 $str .= html_writer::checkbox('f_'.$this->field->id.'_allreq', null, $allrequired, get_string('selectedrequired', 'data')); 95 return $str; 96 } 97 98 function parse_search_field() { 99 $selected = optional_param_array('f_'.$this->field->id, array(), PARAM_NOTAGS); 100 $allrequired = optional_param('f_'.$this->field->id.'_allreq', 0, PARAM_BOOL); 101 if (empty($selected)) { 102 // no searching 103 return ''; 104 } 105 return array('checked'=>$selected, 'allrequired'=>$allrequired); 106 } 107 108 function generate_sql($tablealias, $value) { 109 global $DB; 110 111 static $i=0; 112 $i++; 113 $name = "df_checkbox_{$i}_"; 114 $params = array(); 115 $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255); 116 117 $allrequired = $value['allrequired']; 118 $selected = $value['checked']; 119 120 if ($selected) { 121 $conditions = array(); 122 $j=0; 123 foreach ($selected as $sel) { 124 $j++; 125 $xname = $name.$j; 126 $likesel = str_replace('%', '\%', $sel); 127 $likeselsel = str_replace('_', '\_', $likesel); 128 $conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$varcharcontent} = :{$xname}a 129 OR {$tablealias}.content LIKE :{$xname}b 130 OR {$tablealias}.content LIKE :{$xname}c 131 OR {$tablealias}.content LIKE :{$xname}d))"; 132 $params[$xname.'a'] = $sel; 133 $params[$xname.'b'] = "$likesel##%"; 134 $params[$xname.'c'] = "%##$likesel"; 135 $params[$xname.'d'] = "%##$likesel##%"; 136 } 137 if ($allrequired) { 138 return array(" (".implode(" AND ", $conditions).") ", $params); 139 } else { 140 return array(" (".implode(" OR ", $conditions).") ", $params); 141 } 142 } else { 143 return array(" ", array()); 144 } 145 } 146 147 function update_content($recordid, $value, $name='') { 148 global $DB; 149 150 $content = new stdClass(); 151 $content->fieldid = $this->field->id; 152 $content->recordid = $recordid; 153 $content->content = $this->format_data_field_checkbox_content($value); 154 155 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 156 $content->id = $oldcontent->id; 157 return $DB->update_record('data_content', $content); 158 } else { 159 return $DB->insert_record('data_content', $content); 160 } 161 } 162 163 function display_browse_field($recordid, $template) { 164 global $DB; 165 166 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 167 if (empty($content->content)) { 168 return false; 169 } 170 171 $options = explode("\n",$this->field->param1); 172 $options = array_map('trim', $options); 173 174 $contentArr = explode('##', $content->content); 175 $str = ''; 176 foreach ($contentArr as $line) { 177 if (!in_array($line, $options)) { 178 // hmm, looks like somebody edited the field definition 179 continue; 180 } 181 $str .= $line . "<br />\n"; 182 } 183 return $str; 184 } 185 return false; 186 } 187 188 function format_data_field_checkbox_content($content) { 189 if (!is_array($content)) { 190 return NULL; 191 } 192 $options = explode("\n", $this->field->param1); 193 $options = array_map('trim', $options); 194 195 $vals = array(); 196 foreach ($content as $key=>$val) { 197 if ($key === 'xxx') { 198 continue; 199 } 200 if (!in_array($val, $options)) { 201 continue; 202 203 } 204 $vals[] = $val; 205 } 206 207 if (empty($vals)) { 208 return NULL; 209 } 210 211 return implode('##', $vals); 212 } 213 214 } 215
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 |