user profiles MDL-16213 prevent ul tags in user profiles leading to invalid xhtml
[moodle.git] / mod / data / field / checkbox / field.class.php
CommitLineData
1adbd2c3 1<?php
4728968f 2///////////////////////////////////////////////////////////////////////////
3// //
4// NOTICE OF COPYRIGHT //
5// //
6// Moodle - Modular Object-Oriented Dynamic Learning Environment //
7// http://moodle.org //
8// //
0997e51a 9// Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com //
4728968f 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
4728968f 25class data_field_checkbox extends data_field_base {
26
27 var $type = 'checkbox';
aab98aaf 28
0997e51a 29 function display_add_field($recordid=0) {
a656d951 30 global $CFG, $DB;
aab98aaf 31
c87fbb27 32 $content = array();
0997e51a 33
34 if ($recordid) {
a656d951 35 $content = $DB->get_field('data_content', 'content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid));
0997e51a 36 $content = explode('##', $content);
4728968f 37 }
0997e51a 38
e357c206 39 $str = '<div title="'.s($this->field->description).'">';
40 $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>';
aab98aaf 41
e357c206 42 $i = 0;
423bd918 43 foreach (explode("\n", $this->field->param1) as $checkbox) {
44 $checkbox = trim($checkbox);
6c31d376 45 if ($checkbox === '') {
46 continue; // skip empty lines
47 }
f62f7d8f 48 $str .= '<input type="hidden" name="field_' . $this->field->id . '[]" value="" />';
e357c206 49 $str .= '<input type="checkbox" id="field_'.$this->field->id.'_'.$i.'" name="field_' . $this->field->id . '[]" ';
50 $str .= 'value="' . s($checkbox) . '" ';
aab98aaf 51
4728968f 52 if (array_search($checkbox, $content) !== false) {
cf0e8bdb 53 $str .= 'checked="checked" />';
0997e51a 54 } else {
4728968f 55 $str .= '/>';
56 }
e357c206 57 $str .= '<label for="field_'.$this->field->id.'_'.$i.'">'.$checkbox.'</label><br />';
58 $i++;
4728968f 59 }
aab98aaf 60 $str .= '</fieldset>';
bbe39b6c 61 $str .= '</div>';
4728968f 62 return $str;
63 }
64
7900ecb0 65 function display_search_field($value='') {
601104f2 66 global $CFG, $DB, $OUTPUT;
c6307ef2 67 $temp = $DB->get_records_sql_menu('SELECT id, content FROM {data_content} WHERE fieldid=? GROUP BY content ORDER BY content', array($this->field->id));
7900ecb0 68 $options = array();
69 if(!empty($temp)) {
70 $options[''] = ''; //Make first index blank.
71 foreach ($temp as $key) {
72 $options[$key] = $key; //Build following indicies from the sql.
73 }
74 }
1adbd2c3 75 return $OUTPUT->select(html_select::make($options, 'f_'.$this->field->id, $value));
7900ecb0 76 }
1adbd2c3 77
7900ecb0 78 function parse_search_field() {
79 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
80 }
1adbd2c3 81
7900ecb0 82 function generate_sql($tablealias, $value) {
e3487936 83 static $i=0;
84 $i++;
85 $name = "df_checkbox_$i";
1adbd2c3 86 return array(" ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = :$name) ", array($name=>$value));
7900ecb0 87 }
88
0997e51a 89 function update_content($recordid, $value, $name='') {
a656d951 90 global $DB;
91
e357c206 92 $content = new object();
0997e51a 93 $content->fieldid = $this->field->id;
4728968f 94 $content->recordid = $recordid;
95 $content->content = $this->format_data_field_checkbox_content($value);
aab98aaf 96
a656d951 97 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
4728968f 98 $content->id = $oldcontent->id;
a656d951 99 return $DB->update_record('data_content', $content);
e1791b72 100 } else {
a656d951 101 return $DB->insert_record('data_content', $content);
e1791b72 102 }
4728968f 103 }
aab98aaf 104
0997e51a 105 function display_browse_field($recordid, $template) {
a656d951 106 global $DB;
aab98aaf 107
a656d951 108 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
4728968f 109 $contentArr = array();
110 if (!empty($content->content)) {
111 $contentArr = explode('##', $content->content);
112 }
113 $str = '';
114 foreach ($contentArr as $line) {
115 $str .= $line . "<br />\n";
116 }
117 return $str;
118 }
119 return false;
120 }
0997e51a 121
122 function format_data_field_checkbox_content($content) {
123 if (!is_array($content)) {
124 $str = $content;
125 } else {
126 $str = '';
127 foreach ($content as $val) {
128 $str .= $val . '##';
129 }
130 $str = substr($str, 0, -2);
131 }
132 $str = clean_param($str, PARAM_NOTAGS);
133 return $str;
134 }
4728968f 135}
1adbd2c3 136