MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / data / field / checkbox / field.class.php
blob6574c3f417e9d4dce3bc977adcf62bba117d4aeb
1 <?php // $Id$
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 ///////////////////////////////////////////////////////////////////////////
25 class data_field_checkbox extends data_field_base {
27 var $type = 'checkbox';
29 function data_field_checkbox($field=0, $data=0) {
30 parent::data_field_base($field, $data);
33 function display_add_field($recordid=0) {
34 global $CFG;
36 $content = array();
38 if ($recordid) {
39 $content = get_field('data_content', 'content', 'fieldid', $this->field->id, 'recordid', $recordid);
40 $content = explode('##', $content);
43 $str = '<div title="'.s($this->field->description).'">';
44 $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>';
46 $i = 0;
47 foreach (explode("\n", $this->field->param1) as $checkbox) {
48 $checkbox = trim($checkbox);
49 if ($checkbox === '') {
50 continue; // skip empty lines
52 $str .= '<input type="checkbox" id="field_'.$this->field->id.'_'.$i.'" name="field_' . $this->field->id . '[]" ';
53 $str .= 'value="' . s($checkbox) . '" ';
55 if (array_search($checkbox, $content) !== false) {
56 $str .= 'checked />';
57 } else {
58 $str .= '/>';
60 $str .= '<label for="field_'.$this->field->id.'_'.$i.'">'.$checkbox.'</label><br />';
61 $i++;
63 $str .= '</fieldset>';
64 $str .= '</div>';
65 return $str;
68 function display_search_field($value='') {
69 global $CFG;
70 $temp = get_records_sql_menu('SELECT id, content from '.$CFG->prefix.'data_content WHERE fieldid='.$this->field->id.' GROUP BY content ORDER BY content');
71 $options = array();
72 if(!empty($temp)) {
73 $options[''] = ''; //Make first index blank.
74 foreach ($temp as $key) {
75 $options[$key] = $key; //Build following indicies from the sql.
78 return choose_from_menu($options, 'f_'.$this->field->id, $value, 'choose', '', 0, true);
81 function parse_search_field() {
82 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
85 function generate_sql($tablealias, $value) {
86 return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = '$value') ";
89 function update_content($recordid, $value, $name='') {
90 $content = new object();
91 $content->fieldid = $this->field->id;
92 $content->recordid = $recordid;
93 $content->content = $this->format_data_field_checkbox_content($value);
95 if ($oldcontent = get_record('data_content','fieldid', $this->field->id, 'recordid', $recordid)) {
96 $content->id = $oldcontent->id;
97 return update_record('data_content', $content);
98 } else {
99 return insert_record('data_content', $content);
103 function display_browse_field($recordid, $template) {
105 if ($content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)){
106 $contentArr = array();
107 if (!empty($content->content)) {
108 $contentArr = explode('##', $content->content);
110 $str = '';
111 foreach ($contentArr as $line) {
112 $str .= $line . "<br />\n";
114 return $str;
116 return false;
119 function format_data_field_checkbox_content($content) {
120 if (!is_array($content)) {
121 $str = $content;
122 } else {
123 $str = '';
124 foreach ($content as $val) {
125 $str .= $val . '##';
127 $str = substr($str, 0, -2);
129 $str = clean_param($str, PARAM_NOTAGS);
130 return $str;