Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]
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 /////////////////////////////////////////////////////////////////////////// 24 25 class data_field_url extends data_field_base { 26 var $type = 'url'; 27 /** 28 * priority for globalsearch indexing 29 * 30 * @var int 31 */ 32 protected static $priority = self::MIN_PRIORITY; 33 34 public function supports_preview(): bool { 35 return true; 36 } 37 38 public function get_data_content_preview(int $recordid): stdClass { 39 return (object)[ 40 'id' => 0, 41 'fieldid' => $this->field->id, 42 'recordid' => $recordid, 43 'content' => 'https://example.com', 44 'content1' => null, 45 'content2' => null, 46 'content3' => null, 47 'content4' => null, 48 ]; 49 } 50 51 function display_add_field($recordid = 0, $formdata = null) { 52 global $CFG, $DB, $OUTPUT, $PAGE; 53 54 require_once($CFG->dirroot. '/repository/lib.php'); // necessary for the constants used in args 55 56 $args = new stdClass(); 57 $args->accepted_types = '*'; 58 $args->return_types = FILE_EXTERNAL; 59 $args->context = $this->context; 60 $args->env = 'url'; 61 $fp = new file_picker($args); 62 $options = $fp->options; 63 64 $fieldid = 'field_url_'.$options->client_id; 65 66 $straddlink = get_string('choosealink', 'repository'); 67 $url = ''; 68 $text = ''; 69 if ($formdata) { 70 $fieldname = 'field_' . $this->field->id . '_0'; 71 $url = $formdata->$fieldname; 72 $fieldname = 'field_' . $this->field->id . '_1'; 73 if (isset($formdata->$fieldname)) { 74 $text = $formdata->$fieldname; 75 } 76 } else if ($recordid) { 77 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 78 $url = $content->content; 79 $text = $content->content1; 80 } 81 } 82 83 $autolinkable = !empty($this->field->param1) && empty($this->field->param2); 84 85 $str = '<div title="' . s($this->field->description) . '" class="form-inline">'; 86 87 $label = '<label for="' . $fieldid . '"><span class="accesshide">' . $this->field->name . '</span>'; 88 if ($this->field->required) { 89 $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form')); 90 if ($autolinkable) { 91 $label .= html_writer::div(get_string('requiredelement', 'form'), 'accesshide'); 92 } else { 93 $label .= html_writer::div($image, 'inline-req'); 94 } 95 } 96 $label .= '</label>'; 97 98 if ($autolinkable) { 99 $str .= '<table><tr><td align="right">'; 100 $str .= '<span class="mod-data-input">' . get_string('url', 'data') . ':</span>'; 101 if (!empty($image)) { 102 $str .= $image; 103 } 104 $str .= '</td><td>'; 105 $str .= $label; 106 $str .= '<input type="text" name="field_' . $this->field->id . '_0" id="' . $fieldid . '" value="' . s($url) . '" ' . 107 'size="40" class="form-control d-inline"/>'; 108 $str .= '<button class="btn btn-secondary ml-1" id="filepicker-button-' . $options->client_id . '" ' . 109 'style="display:none">' . $straddlink . '</button></td></tr>'; 110 $str .= '<tr><td align="right"><span class="mod-data-input">' . get_string('text', 'data') . ':</span></td><td>'; 111 $str .= '<input type="text" name="field_' . $this->field->id . '_1" id="field_' . $this->field->id . '_1" ' . 112 'value="' . s($text) . '" size="40" class="form-control d-inline"/></td></tr>'; 113 $str .= '</table>'; 114 } else { 115 // Just the URL field 116 $str .= $label; 117 $str .= '<input type="text" name="field_'.$this->field->id.'_0" id="'.$fieldid.'" value="'.s($url).'"'; 118 $str .= ' size="40" class="mod-data-input form-control d-inline" />'; 119 if (count($options->repositories) > 0) { 120 $str .= '<button id="filepicker-button-' . $options->client_id . '" class="visibleifjs btn btn-secondary ml-1">' . 121 $straddlink . '</button>'; 122 } 123 } 124 125 // print out file picker 126 //$str .= $OUTPUT->render($fp); 127 128 $module = array('name'=>'data_urlpicker', 'fullpath'=>'/mod/data/data.js', 'requires'=>array('core_filepicker')); 129 $PAGE->requires->js_init_call('M.data_urlpicker.init', array($options), true, $module); 130 $str .= '</div>'; 131 return $str; 132 } 133 134 function display_search_field($value = '') { 135 return '<label class="accesshide" for="f_' . $this->field->id . '">' . get_string('fieldname', 'data') . '</label>' . 136 '<input type="text" size="16" id="f_' . $this->field->id . '" '. 137 ' name="f_' . $this->field->id . '" value="' . s($value) . '" class="form-control d-inline"/>'; 138 } 139 140 public function parse_search_field($defaults = null) { 141 $param = 'f_'.$this->field->id; 142 if (empty($defaults[$param])) { 143 $defaults = array($param => ''); 144 } 145 return optional_param($param, $defaults[$param], PARAM_NOTAGS); 146 } 147 148 function generate_sql($tablealias, $value) { 149 global $DB; 150 151 static $i=0; 152 $i++; 153 $name = "df_url_$i"; 154 return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%")); 155 } 156 157 function display_browse_field($recordid, $template) { 158 159 $content = $this->get_data_content($recordid); 160 if (!$content) { 161 return ''; 162 } 163 164 $url = empty($content->content) ? '' : $content->content; 165 $text = empty($content->content1) ? '' : $content->content1; 166 if (empty($url) || ($url == 'http://')) { 167 return ''; 168 } 169 if (!empty($this->field->param2)) { 170 // Param2 forces the text to something. 171 $text = $this->field->param2; 172 } 173 if ($this->field->param1) { 174 // Param1 defines whether we want to autolink the url. 175 $attributes = ['class' => 'data-field-link']; 176 if ($this->field->param3) { 177 // Param3 defines whether this URL should open in a new window. 178 $attributes['target'] = '_blank'; 179 $attributes['rel'] = 'noreferrer'; 180 } 181 182 if (empty($text)) { 183 $text = $url; 184 } 185 186 $str = html_writer::link($url, $text, $attributes); 187 } else { 188 $str = $url; 189 } 190 return $str; 191 } 192 193 function update_content_import($recordid, $value, $name='') { 194 $values = explode(" ", $value, 2); 195 196 foreach ($values as $index => $value) { 197 $this->update_content($recordid, $value, $name . '_' . $index); 198 } 199 } 200 201 function update_content($recordid, $value, $name='') { 202 global $DB; 203 204 $content = new stdClass(); 205 $content->fieldid = $this->field->id; 206 $content->recordid = $recordid; 207 $names = explode('_', $name); 208 209 switch ($names[2]) { 210 case 0: 211 // update link 212 $content->content = clean_param($value, PARAM_URL); 213 break; 214 case 1: 215 // add text 216 $content->content1 = clean_param($value, PARAM_NOTAGS); 217 break; 218 default: 219 break; 220 } 221 222 if (!empty($content->content) && (strpos($content->content, '://') === false) 223 && (strpos($content->content, '/') !== 0)) { 224 $content->content = 'http://' . $content->content; 225 } 226 227 if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 228 $content->id = $oldcontent->id; 229 return $DB->update_record('data_content', $content); 230 } else { 231 return $DB->insert_record('data_content', $content); 232 } 233 } 234 235 function notemptyfield($value, $name) { 236 $names = explode('_',$name); 237 $value = clean_param($value, PARAM_URL); 238 //clean first 239 if ($names[2] == '0') { 240 return ($value!='http://' && !empty($value)); 241 } 242 return false; 243 } 244 245 function export_text_value($record) { 246 return $record->content . " " . $record->content1; 247 } 248 249 /** 250 * Return the plugin configs for external functions. 251 * 252 * @return array the list of config parameters 253 * @since Moodle 3.3 254 */ 255 public function get_config_for_external() { 256 // Return all the config parameters. 257 $configs = []; 258 for ($i = 1; $i <= 10; $i++) { 259 $configs["param$i"] = $this->field->{"param$i"}; 260 } 261 return $configs; 262 } 263 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body