MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / data / field / multimenu / field.class.php
blobec37adb8e54675f82234d0f65fc9f6c426bd0a86
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_multimenu extends data_field_base {
27 var $type = 'multimenu';
29 function data_field_multimenu($field=0, $data=0) {
30 parent::data_field_base($field, $data);
34 function display_add_field($recordid=0) {
36 if ($recordid){
37 $content = get_field('data_content', 'content', 'fieldid', $this->field->id, 'recordid', $recordid);
38 $content = explode('##', $content);
39 } else {
40 $content = array();
43 $str = '<div title="'.s($this->field->description).'">';
44 $str .= '<select name="field_' . $this->field->id . '[]" id="field_' . $this->field->id . '" multiple="multiple">';
46 foreach (explode("\n",$this->field->param1) as $option) {
47 $option = trim($option);
48 $str .= '<option value="' . s($option) . '"';
50 if (array_search($option, $content) !== false) {
51 // Selected by user.
52 $str .= ' selected >';
53 } else {
54 $str .= '>';
56 $str .= $option . '</option>';
58 $str .= '</select>';
59 $str .= '</div>';
61 return $str;
64 function display_search_field($value = '') {
65 global $CFG;
66 $temp = get_records_sql_menu('SELECT id, content from '.$CFG->prefix.'data_content WHERE fieldid='.$this->field->id.' GROUP BY content ORDER BY content');
67 $options = array();
68 if(!empty($temp)) {
69 $options[''] = ''; //Make first index blank.
70 foreach ($temp as $key) {
71 $options[$key] = $key; //Build following indicies from the sql.
74 return choose_from_menu($options, 'f_'.$this->field->id, $value, 'choose', '', 0, true);
77 function parse_search_field() {
78 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
81 function generate_sql($tablealias, $value) {
82 return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content = '$value') ";
85 function update_content($recordid, $value, $name='') {
86 $content = new object;
87 $content->fieldid = $this->field->id;
88 $content->recordid = $recordid;
89 $content->content = $this->format_data_field_multimenu_content($value);
91 if ($oldcontent = get_record('data_content','fieldid', $this->field->id, 'recordid', $recordid)) {
92 $content->id = $oldcontent->id;
93 return update_record('data_content', $content);
94 } else {
95 return insert_record('data_content', $content);
99 function format_data_field_multimenu_content($content) {
100 if (!is_array($content)) {
101 $str = $content;
102 } else {
103 $str = '';
104 foreach ($content as $val) {
105 $str .= $val . '##';
107 $str = substr($str, 0, -2);
109 $str = clean_param($str, PARAM_NOTAGS);
110 return $str;
114 function display_browse_field($recordid, $template) {
116 if ($content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)){
117 $contentArr = array();
118 if (!empty($content->content)) {
119 $contentArr = explode('##', $content->content);
121 $str = '';
122 foreach ($contentArr as $line) {
123 $str .= $line . "<br />\n";
125 return $str;
127 return false;