2 ///////////////////////////////////////////////////////////////////////////
4 // NOTICE OF COPYRIGHT //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
9 // Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com //
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. //
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: //
21 // http://www.gnu.org/copyleft/gpl.html //
23 ///////////////////////////////////////////////////////////////////////////
25 class data_field_url extends data_field_base {
28 function display_add_field($recordid=0) {
29 global $CFG, $DB, $OUTPUT, $PAGE;
31 require_once($CFG->dirroot. '/repository/lib.php'); // necessary for the constants used in args
33 $args = new stdClass();
34 $args->accepted_types = '*';
35 $args->return_types = FILE_EXTERNAL;
36 $args->context = $this->context;
38 $fp = new file_picker($args);
39 $options = $fp->options;
41 $fieldid = 'field_url_'.$options->client_id;
43 $straddlink = get_string('choosealink', 'repository');
47 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
48 $url = $content->content;
49 $text = $content->content1;
52 $str = '<div title="'.s($this->field->description).'">';
53 if (!empty($this->field->param1) and empty($this->field->param2)) {
54 $str .= '<table><tr><td align="right">';
55 $str .= get_string('url','data').':</td><td><input type="text" name="field_'.$this->field->id.'_0" id="'.$fieldid.'" value="'.$url.'" size="60" /></td></tr>';
56 $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>';
60 $str .= '<input type="text" name="field_'.$this->field->id.'_0" id="'.$fieldid.'" value="'.s($url).'" size="60" />';
63 $str .= '<button id="filepicker-button-'.$options->client_id.'" style="display:none">'.$straddlink.'</button>';
65 // print out file picker
66 //$str .= $OUTPUT->render($fp);
68 $module = array('name'=>'data_urlpicker', 'fullpath'=>'/mod/data/data.js', 'requires'=>array('core_filepicker'));
69 $PAGE->requires->js_init_call('M.data_urlpicker.init', array($options), true, $module);
70 $PAGE->requires->js_function_call('show_item', array('filepicker-button-'.$options->client_id));
76 function display_search_field($value = '') {
77 return '<input type="text" size="16" name="f_'.$this->field->id.'" value="'.$value.'" />';
80 function parse_search_field() {
81 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
84 function generate_sql($tablealias, $value) {
90 return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
93 function display_browse_field($recordid, $template) {
96 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
97 $url = empty($content->content)? '':$content->content;
98 $text = empty($content->content1)? '':$content->content1;
99 if (empty($url) or ($url == 'http://')) {
102 if (!empty($this->field->param2)) {
103 // param2 forces the text to something
104 $text = $this->field->param2;
106 if ($this->field->param1) {
107 // param1 defines whether we want to autolink the url.
109 $str = '<a href="'.$url.'">'.$text.'</a>';
111 $str = '<a href="'.$url.'">'.$url.'</a>';
121 function update_content($recordid, $value, $name='') {
124 $content = new stdClass();
125 $content->fieldid = $this->field->id;
126 $content->recordid = $recordid;
127 $names = explode('_', $name);
132 $content->content = clean_param($value, PARAM_URL);
136 $content->content1 = clean_param($value, PARAM_NOTAGS);
142 if (!empty($content->content) && (strpos($content->content, '://') === false) && (strpos($content->content, '/', 0) === false)) {
143 $content->content = 'http://' . $content->content;
146 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
147 $content->id = $oldcontent->id;
148 return $DB->update_record('data_content', $content);
150 return $DB->insert_record('data_content', $content);
154 function notemptyfield($value, $name) {
155 $names = explode('_',$name);
156 $value = clean_param($value, PARAM_URL);
158 if ($names[2] == '0') {
159 return ($value!='http://' && !empty($value));
164 function export_text_value($record) {
165 return $record->content . " " . $record->content1;