b22e4f84aacf1f0c5422e7304f68cf3178e074bd
[moodle.git] / mod / data / field / url / field.class.php
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 ///////////////////////////////////////////////////////////////////////////
25 class data_field_url extends data_field_base {
26     var $type = 'url';
28     function display_add_field($recordid=0) {
29         global $CFG, $DB, $OUTPUT, $PAGE;
31         $args = new stdClass();
32         $args->accepted_types = '*';
33         $args->return_types = FILE_EXTERNAL;
34         $args->context = $this->context;
35         $args->env = 'url';
36         $fp = new file_picker($args);
37         $options = $fp->options;
39         $fieldid = 'field_url_'.$options->client_id;
41         $straddlink = get_string('choosealink', 'repository');
42         $url = '';
43         $text = '';
44         if ($recordid) {
45             if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
46                 $url  = $content->content;
47                 $text = $content->content1;
48             }
49         }
50         $str = '<div title="'.s($this->field->description).'">';
51         if (!empty($this->field->param1) and empty($this->field->param2)) {
52             $str .= '<table><tr><td align="right">';
53             $str .= get_string('url','data').':</td><td><input type="text" name="field_'.$this->field->id.'_0" id="'.$fieldid.'" value="'.$url.'" size="60" /></td></tr>';
54             $str .= '<tr><td align="right">'.get_string('text','data').':</td><td><input type="text" name="field_'.$this->field->id.'_1" id="field_'.$this->field->id.'_1" value="'.s($text).'" size="60" /></td></tr>';
55             $str .= '</table>';
56         } else {
57             // Just the URL field
58             $str .= '<input type="text" name="field_'.$this->field->id.'_0" id="'.$fieldid.'" value="'.s($url).'" size="60" />';
59         }
61         $str .= '<button id="filepicker-button-'.$options->client_id.'" style="display:none">'.$straddlink.'</button>';
63         // print out file picker
64         //$str .= $OUTPUT->render($fp);
66         $module = array('name'=>'data_urlpicker', 'fullpath'=>'/mod/data/data.js', 'requires'=>array('core_filepicker'));
67         $PAGE->requires->js_init_call('M.data_urlpicker.init', array($options), true, $module);
68         $PAGE->requires->js_function_call('show_item', array('filepicker-button-'.$options->client_id));
70         $str .= '</div>';
71         return $str;
72     }
74     function display_search_field($value = '') {
75         return '<input type="text" size="16" name="f_'.$this->field->id.'" value="'.$value.'" />';
76     }
78     function parse_search_field() {
79         return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
80     }
82     function generate_sql($tablealias, $value) {
83         global $DB;
85         static $i=0;
86         $i++;
87         $name = "df_url_$i";
88         return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
89     }
91     function display_browse_field($recordid, $template) {
92         global $DB;
94         if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
95             $url = empty($content->content)? '':$content->content;
96             $text = empty($content->content1)? '':$content->content1;
97             if (empty($url) or ($url == 'http://')) {
98                 return '';
99             }
100             if (!empty($this->field->param2)) {
101                 // param2 forces the text to something
102                 $text = $this->field->param2;
103             }
104             if ($this->field->param1) {
105                 // param1 defines whether we want to autolink the url.
106                 if (!empty($text)) {
107                     $str = '<a href="'.$url.'">'.$text.'</a>';
108                 } else {
109                     $str = '<a href="'.$url.'">'.$url.'</a>';
110                 }
111             } else {
112                 $str = $url;
113             }
114             return $str;
115         }
116         return false;
117     }
119     function update_content($recordid, $value, $name='') {
120         global $DB;
122         $content = new stdClass();
123         $content->fieldid = $this->field->id;
124         $content->recordid = $recordid;
125         $names = explode('_', $name);
127         switch ($names[2]) {
128             case 0:
129                 // update link
130                 $content->content = clean_param($value, PARAM_URL);
131                 break;
132             case 1:
133                 // add text
134                 $content->content1 = clean_param($value, PARAM_NOTAGS);
135                 break;
136             default:
137                 break;
138         }
140         if (!empty($content->content) && (strpos($content->content, '://') === false) && (strpos($content->content, '/', 0) === false)) {
141             $content->content = 'http://' . $content->content;
142         }
144         if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
145             $content->id = $oldcontent->id;
146             return $DB->update_record('data_content', $content);
147         } else {
148             return $DB->insert_record('data_content', $content);
149         }
150     }
152     function notemptyfield($value, $name) {
153         $names = explode('_',$name);
154         $value = clean_param($value, PARAM_URL);
155         //clean first
156         if ($names[2] == '0') {
157             return ($value!='http://' && !empty($value));
158         }
159         return false;
160     }
162     function export_text_value($record) {
163         return $record->content . " " . $record->content1;
164     }