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 * priority for globalsearch indexing
32 protected static $priority = self
::MIN_PRIORITY
;
34 function display_add_field($recordid = 0, $formdata = null) {
35 global $CFG, $DB, $OUTPUT, $PAGE;
37 require_once($CFG->dirroot
. '/repository/lib.php'); // necessary for the constants used in args
39 $args = new stdClass();
40 $args->accepted_types
= '*';
41 $args->return_types
= FILE_EXTERNAL
;
42 $args->context
= $this->context
;
44 $fp = new file_picker($args);
45 $options = $fp->options
;
47 $fieldid = 'field_url_'.$options->client_id
;
49 $straddlink = get_string('choosealink', 'repository');
53 $fieldname = 'field_' . $this->field
->id
. '_0';
54 $url = $formdata->$fieldname;
55 $fieldname = 'field_' . $this->field
->id
. '_1';
56 if (isset($formdata->$fieldname)) {
57 $text = $formdata->$fieldname;
59 } else if ($recordid) {
60 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field
->id
, 'recordid'=>$recordid))) {
61 $url = $content->content
;
62 $text = $content->content1
;
66 $autolinkable = !empty($this->field
->param1
) && empty($this->field
->param2
);
68 $str = '<div title="' . s($this->field
->description
) . '" class="form-inline">';
70 $label = '<label for="' . $fieldid . '"><span class="accesshide">' . $this->field
->name
. '</span>';
71 if ($this->field
->required
) {
72 $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form'));
74 $label .= html_writer
::div(get_string('requiredelement', 'form'), 'accesshide');
76 $label .= html_writer
::div($image, 'inline-req');
82 $str .= '<table><tr><td align="right">';
83 $str .= '<span class="mod-data-input">' . get_string('url', 'data') . ':</span>';
89 $str .= '<input type="text" name="field_' . $this->field
->id
. '_0" id="' . $fieldid . '" value="' . s($url) . '" ' .
90 'size="40" class="form-control d-inline"/>';
91 $str .= '<button class="btn btn-secondary m-l-1" id="filepicker-button-' . $options->client_id
. '" ' .
92 'style="display:none">' . $straddlink . '</button></td></tr>';
93 $str .= '<tr><td align="right"><span class="mod-data-input">' . get_string('text', 'data') . ':</span></td><td>';
94 $str .= '<input type="text" name="field_' . $this->field
->id
. '_1" id="field_' . $this->field
->id
. '_1" ' .
95 'value="' . s($text) . '" size="40" class="form-control d-inline"/></td></tr>';
100 $str .= '<input type="text" name="field_'.$this->field
->id
.'_0" id="'.$fieldid.'" value="'.s($url).'"';
101 $str .= ' size="40" class="mod-data-input form-control d-inline" />';
102 if (count($options->repositories
) > 0) {
103 $str .= '<button id="filepicker-button-' . $options->client_id
. '" class="visibleifjs btn btn-secondary m-l-1">' .
104 $straddlink . '</button>';
108 // print out file picker
109 //$str .= $OUTPUT->render($fp);
111 $module = array('name'=>'data_urlpicker', 'fullpath'=>'/mod/data/data.js', 'requires'=>array('core_filepicker'));
112 $PAGE->requires
->js_init_call('M.data_urlpicker.init', array($options), true, $module);
117 function display_search_field($value = '') {
118 return '<label class="accesshide" for="f_' . $this->field
->id
. '">' . get_string('fieldname', 'data') . '</label>' .
119 '<input type="text" size="16" id="f_' . $this->field
->id
. '" '.
120 ' name="f_' . $this->field
->id
. '" value="' . s($value) . '" class="form-control d-inline"/>';
123 public function parse_search_field($defaults = null) {
124 $param = 'f_'.$this->field
->id
;
125 if (empty($defaults[$param])) {
126 $defaults = array($param => '');
128 return optional_param($param, $defaults[$param], PARAM_NOTAGS
);
131 function generate_sql($tablealias, $value) {
137 return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
140 function display_browse_field($recordid, $template) {
143 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field
->id
, 'recordid'=>$recordid))) {
144 $url = empty($content->content
)?
'':$content->content
;
145 $text = empty($content->content1
)?
'':$content->content1
;
146 if (empty($url) or ($url == 'http://')) {
149 if (!empty($this->field
->param2
)) {
150 // param2 forces the text to something
151 $text = $this->field
->param2
;
153 if ($this->field
->param1
) {
154 // param1 defines whether we want to autolink the url.
155 $attributes = array();
156 if ($this->field
->param3
) {
157 // param3 defines whether this URL should open in a new window.
158 $attributes['target'] = '_blank';
159 $attributes['rel'] = 'noreferrer';
166 $str = html_writer
::link($url, $text, $attributes);
175 function update_content_import($recordid, $value, $name='') {
176 $values = explode(" ", $value, 2);
178 foreach ($values as $index => $value) {
179 $this->update_content($recordid, $value, $name . '_' . $index);
183 function update_content($recordid, $value, $name='') {
186 $content = new stdClass();
187 $content->fieldid
= $this->field
->id
;
188 $content->recordid
= $recordid;
189 $names = explode('_', $name);
194 $content->content
= clean_param($value, PARAM_URL
);
198 $content->content1
= clean_param($value, PARAM_NOTAGS
);
204 if (!empty($content->content
) && (strpos($content->content
, '://') === false)
205 && (strpos($content->content
, '/') !== 0)) {
206 $content->content
= 'http://' . $content->content
;
209 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field
->id
, 'recordid'=>$recordid))) {
210 $content->id
= $oldcontent->id
;
211 return $DB->update_record('data_content', $content);
213 return $DB->insert_record('data_content', $content);
217 function notemptyfield($value, $name) {
218 $names = explode('_',$name);
219 $value = clean_param($value, PARAM_URL
);
221 if ($names[2] == '0') {
222 return ($value!='http://' && !empty($value));
227 function export_text_value($record) {
228 return $record->content
. " " . $record->content1
;
232 * Return the plugin configs for external functions.
234 * @return array the list of config parameters
237 public function get_config_for_external() {
238 // Return all the config parameters.
240 for ($i = 1; $i <= 10; $i++
) {
241 $configs["param$i"] = $this->field
->{"param$i"};