Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/data/field/checkbox/field.class.php
Go to the documentation of this file.
00001 <?php
00003 //                                                                       //
00004 // NOTICE OF COPYRIGHT                                                   //
00005 //                                                                       //
00006 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
00007 //          http://moodle.org                                            //
00008 //                                                                       //
00009 // Copyright (C) 1999-onwards Moodle Pty Ltd  http://moodle.com          //
00010 //                                                                       //
00011 // This program is free software; you can redistribute it and/or modify  //
00012 // it under the terms of the GNU General Public License as published by  //
00013 // the Free Software Foundation; either version 2 of the License, or     //
00014 // (at your option) any later version.                                   //
00015 //                                                                       //
00016 // This program is distributed in the hope that it will be useful,       //
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
00019 // GNU General Public License for more details:                          //
00020 //                                                                       //
00021 //          http://www.gnu.org/copyleft/gpl.html                         //
00022 //                                                                       //
00024 
00025 class data_field_checkbox extends data_field_base {
00026 
00027     var $type = 'checkbox';
00028 
00029     function display_add_field($recordid=0) {
00030         global $CFG, $DB;
00031 
00032         $content = array();
00033 
00034         if ($recordid) {
00035             $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
00036             $content = explode('##', $content);
00037         } else {
00038             $content = array();
00039         }
00040 
00041         $str = '<div title="'.s($this->field->description).'">';
00042         $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>';
00043 
00044         $i = 0;
00045         foreach (explode("\n", $this->field->param1) as $checkbox) {
00046             $checkbox = trim($checkbox);
00047             if ($checkbox === '') {
00048                 continue; // skip empty lines
00049             }
00050             $str .= '<input type="hidden" name="field_' . $this->field->id . '[]" value="" />';
00051             $str .= '<input type="checkbox" id="field_'.$this->field->id.'_'.$i.'" name="field_' . $this->field->id . '[]" ';
00052             $str .= 'value="' . s($checkbox) . '" ';
00053 
00054             if (array_search($checkbox, $content) !== false) {
00055                 $str .= 'checked />';
00056             } else {
00057                 $str .= '/>';
00058             }
00059             $str .= '<label for="field_'.$this->field->id.'_'.$i.'">'.$checkbox.'</label><br />';
00060             $i++;
00061         }
00062         $str .= '</fieldset>';
00063         $str .= '</div>';
00064         return $str;
00065     }
00066 
00067     function display_search_field($value='') {
00068         global $CFG, $DB;
00069 
00070         if (is_array($value)) {
00071             $content = $value['checked'];
00072             $allrequired = $value['allrequired'] ? true : false;
00073         } else {
00074             $content = array();
00075             $allrequired = false;
00076         }
00077 
00078         $str = '';
00079         $found = false;
00080         foreach (explode("\n",$this->field->param1) as $checkbox) {
00081             $checkbox = trim($checkbox);
00082 
00083             if (in_array($checkbox, $content)) {
00084                 $str .= html_writer::checkbox('f_'.$this->field->id.'[]', s($checkbox), true, $checkbox);
00085             } else {
00086                 $str .= html_writer::checkbox('f_'.$this->field->id.'[]', s($checkbox), false, $checkbox);
00087             }
00088             $found = true;
00089         }
00090         if (!$found) {
00091             return '';
00092         }
00093 
00094         $str .= html_writer::checkbox('f_'.$this->field->id.'_allreq', null, $allrequired, get_string('selectedrequired', 'data'));
00095         return $str;
00096     }
00097 
00098     function parse_search_field() {
00099         $selected    = optional_param_array('f_'.$this->field->id, array(), PARAM_NOTAGS);
00100         $allrequired = optional_param('f_'.$this->field->id.'_allreq', 0, PARAM_BOOL);
00101         if (empty($selected)) {
00102             // no searching
00103             return '';
00104         }
00105         return array('checked'=>$selected, 'allrequired'=>$allrequired);
00106     }
00107 
00108     function generate_sql($tablealias, $value) {
00109         global $DB;
00110 
00111         static $i=0;
00112         $i++;
00113         $name = "df_checkbox_{$i}_";
00114         $params = array();
00115         $varcharcontent = $DB->sql_compare_text("{$tablealias}.content", 255);
00116 
00117         $allrequired = $value['allrequired'];
00118         $selected    = $value['checked'];
00119 
00120         if ($selected) {
00121             $conditions = array();
00122             $j=0;
00123             foreach ($selected as $sel) {
00124                 $j++;
00125                 $xname = $name.$j;
00126                 $likesel = str_replace('%', '\%', $sel);
00127                 $likeselsel = str_replace('_', '\_', $likesel);
00128                 $conditions[] = "({$tablealias}.fieldid = {$this->field->id} AND ({$varcharcontent} = :{$xname}a
00129                                                                                OR {$tablealias}.content LIKE :{$xname}b
00130                                                                                OR {$tablealias}.content LIKE :{$xname}c
00131                                                                                OR {$tablealias}.content LIKE :{$xname}d))";
00132                 $params[$xname.'a'] = $sel;
00133                 $params[$xname.'b'] = "$likesel##%";
00134                 $params[$xname.'c'] = "%##$likesel";
00135                 $params[$xname.'d'] = "%##$likesel##%";
00136             }
00137             if ($allrequired) {
00138                 return array(" (".implode(" AND ", $conditions).") ", $params);
00139             } else {
00140                 return array(" (".implode(" OR ", $conditions).") ", $params);
00141             }
00142         } else {
00143             return array(" ", array());
00144         }
00145     }
00146 
00147     function update_content($recordid, $value, $name='') {
00148         global $DB;
00149 
00150         $content = new stdClass();
00151         $content->fieldid = $this->field->id;
00152         $content->recordid = $recordid;
00153         $content->content = $this->format_data_field_checkbox_content($value);
00154 
00155         if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
00156             $content->id = $oldcontent->id;
00157             return $DB->update_record('data_content', $content);
00158         } else {
00159             return $DB->insert_record('data_content', $content);
00160         }
00161     }
00162 
00163     function display_browse_field($recordid, $template) {
00164         global $DB;
00165 
00166         if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
00167             if (empty($content->content)) {
00168                 return false;
00169             }
00170 
00171             $options = explode("\n",$this->field->param1);
00172             $options = array_map('trim', $options);
00173 
00174             $contentArr = explode('##', $content->content);
00175             $str = '';
00176             foreach ($contentArr as $line) {
00177                 if (!in_array($line, $options)) {
00178                     // hmm, looks like somebody edited the field definition
00179                     continue;
00180                 }
00181                 $str .= $line . "<br />\n";
00182             }
00183             return $str;
00184         }
00185         return false;
00186     }
00187 
00188     function format_data_field_checkbox_content($content) {
00189         if (!is_array($content)) {
00190             return NULL;
00191         }
00192         $options = explode("\n", $this->field->param1);
00193         $options = array_map('trim', $options);
00194 
00195         $vals = array();
00196         foreach ($content as $key=>$val) {
00197             if ($key === 'xxx') {
00198                 continue;
00199             }
00200             if (!in_array($val, $options)) {
00201                 continue;
00202 
00203             }
00204             $vals[] = $val;
00205         }
00206 
00207         if (empty($vals)) {
00208             return NULL;
00209         }
00210 
00211         return implode('##', $vals);
00212     }
00213 
00214 }
00215 
 All Data Structures Namespaces Files Functions Variables Enumerations