Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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      function display_add_field($recordid = 0, $formdata = null) {
  35          global $CFG, $DB, $OUTPUT, $PAGE;
  36  
  37          require_once($CFG->dirroot. '/repository/lib.php'); // necessary for the constants used in args
  38  
  39          $args = new stdClass();
  40          $args->accepted_types = '*';
  41          $args->return_types = FILE_EXTERNAL;
  42          $args->context = $this->context;
  43          $args->env = 'url';
  44          $fp = new file_picker($args);
  45          $options = $fp->options;
  46  
  47          $fieldid = 'field_url_'.$options->client_id;
  48  
  49          $straddlink = get_string('choosealink', 'repository');
  50          $url = '';
  51          $text = '';
  52          if ($formdata) {
  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;
  58              }
  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;
  63              }
  64          }
  65  
  66          $autolinkable = !empty($this->field->param1) && empty($this->field->param2);
  67  
  68          $str = '<div title="' . s($this->field->description) . '" class="form-inline">';
  69  
  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'));
  73              if ($autolinkable) {
  74                  $label .= html_writer::div(get_string('requiredelement', 'form'), 'accesshide');
  75              } else {
  76                  $label .= html_writer::div($image, 'inline-req');
  77              }
  78          }
  79          $label .= '</label>';
  80  
  81          if ($autolinkable) {
  82              $str .= '<table><tr><td align="right">';
  83              $str .= '<span class="mod-data-input">' . get_string('url', 'data') . ':</span>';
  84              if (!empty($image)) {
  85                  $str .= $image;
  86              }
  87              $str .= '</td><td>';
  88              $str .= $label;
  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 ml-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>';
  96              $str .= '</table>';
  97          } else {
  98              // Just the URL field
  99              $str .= $label;
 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 ml-1">' .
 104                          $straddlink . '</button>';
 105              }
 106          }
 107  
 108          // print out file picker
 109          //$str .= $OUTPUT->render($fp);
 110  
 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);
 113          $str .= '</div>';
 114          return $str;
 115      }
 116  
 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"/>';
 121      }
 122  
 123      public function parse_search_field($defaults = null) {
 124          $param = 'f_'.$this->field->id;
 125          if (empty($defaults[$param])) {
 126              $defaults = array($param => '');
 127          }
 128          return optional_param($param, $defaults[$param], PARAM_NOTAGS);
 129      }
 130  
 131      function generate_sql($tablealias, $value) {
 132          global $DB;
 133  
 134          static $i=0;
 135          $i++;
 136          $name = "df_url_$i";
 137          return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
 138      }
 139  
 140      function display_browse_field($recordid, $template) {
 141          global $DB;
 142  
 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://')) {
 147                  return '';
 148              }
 149              if (!empty($this->field->param2)) {
 150                  // param2 forces the text to something
 151                  $text = $this->field->param2;
 152              }
 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';
 160                  }
 161  
 162                  if (empty($text)) {
 163                      $text = $url;
 164                  }
 165  
 166                  $str = html_writer::link($url, $text, $attributes);
 167              } else {
 168                  $str = $url;
 169              }
 170              return $str;
 171          }
 172          return false;
 173      }
 174  
 175      function update_content_import($recordid, $value, $name='') {
 176          $values = explode(" ", $value, 2);
 177  
 178          foreach ($values as $index => $value) {
 179              $this->update_content($recordid, $value, $name . '_' . $index);
 180          }
 181      }
 182  
 183      function update_content($recordid, $value, $name='') {
 184          global $DB;
 185  
 186          $content = new stdClass();
 187          $content->fieldid = $this->field->id;
 188          $content->recordid = $recordid;
 189          $names = explode('_', $name);
 190  
 191          switch ($names[2]) {
 192              case 0:
 193                  // update link
 194                  $content->content = clean_param($value, PARAM_URL);
 195                  break;
 196              case 1:
 197                  // add text
 198                  $content->content1 = clean_param($value, PARAM_NOTAGS);
 199                  break;
 200              default:
 201                  break;
 202          }
 203  
 204          if (!empty($content->content) && (strpos($content->content, '://') === false)
 205                  && (strpos($content->content, '/') !== 0)) {
 206              $content->content = 'http://' . $content->content;
 207          }
 208  
 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);
 212          } else {
 213              return $DB->insert_record('data_content', $content);
 214          }
 215      }
 216  
 217      function notemptyfield($value, $name) {
 218          $names = explode('_',$name);
 219          $value = clean_param($value, PARAM_URL);
 220          //clean first
 221          if ($names[2] == '0') {
 222              return ($value!='http://' && !empty($value));
 223          }
 224          return false;
 225      }
 226  
 227      function export_text_value($record) {
 228          return $record->content . " " . $record->content1;
 229      }
 230  
 231      /**
 232       * Return the plugin configs for external functions.
 233       *
 234       * @return array the list of config parameters
 235       * @since Moodle 3.3
 236       */
 237      public function get_config_for_external() {
 238          // Return all the config parameters.
 239          $configs = [];
 240          for ($i = 1; $i <= 10; $i++) {
 241              $configs["param$i"] = $this->field->{"param$i"};
 242          }
 243          return $configs;
 244      }
 245  }