Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/data/field/url/field.class.php
Go to the documentation of this file.
00001 <?php
00003 //                                                                       //
00004 // NOTICE OF COPYRIGHT                                                   //
00005 //                                                                       //
00006 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
00007 //          http://moodle.org                                            //
00008 //                                                                       //
00009 // Copyright (C) 1999-onwards Moodle Pty Ltd  http://moodle.com          //
00010 //                                                                       //
00011 // This program is free software; you can redistribute it and/or modify  //
00012 // it under the terms of the GNU General Public License as published by  //
00013 // the Free Software Foundation; either version 2 of the License, or     //
00014 // (at your option) any later version.                                   //
00015 //                                                                       //
00016 // This program is distributed in the hope that it will be useful,       //
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
00019 // GNU General Public License for more details:                          //
00020 //                                                                       //
00021 //          http://www.gnu.org/copyleft/gpl.html                         //
00022 //                                                                       //
00024 
00025 class data_field_url extends data_field_base {
00026     var $type = 'url';
00027 
00028     function display_add_field($recordid=0) {
00029         global $CFG, $DB, $OUTPUT, $PAGE;
00030 
00031         require_once($CFG->dirroot. '/repository/lib.php'); // necessary for the constants used in args
00032 
00033         $args = new stdClass();
00034         $args->accepted_types = '*';
00035         $args->return_types = FILE_EXTERNAL;
00036         $args->context = $this->context;
00037         $args->env = 'url';
00038         $fp = new file_picker($args);
00039         $options = $fp->options;
00040 
00041         $fieldid = 'field_url_'.$options->client_id;
00042 
00043         $straddlink = get_string('choosealink', 'repository');
00044         $url = '';
00045         $text = '';
00046         if ($recordid) {
00047             if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
00048                 $url  = $content->content;
00049                 $text = $content->content1;
00050             }
00051         }
00052         $str = '<div title="'.s($this->field->description).'">';
00053         if (!empty($this->field->param1) and empty($this->field->param2)) {
00054             $str .= '<table><tr><td align="right">';
00055             $str .= get_string('url','data').':</td><td><input type="text" name="field_'.$this->field->id.'_0" id="'.$fieldid.'" value="'.$url.'" size="60" /></td></tr>';
00056             $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>';
00057             $str .= '</table>';
00058         } else {
00059             // Just the URL field
00060             $str .= '<input type="text" name="field_'.$this->field->id.'_0" id="'.$fieldid.'" value="'.s($url).'" size="60" />';
00061         }
00062 
00063         $str .= '<button id="filepicker-button-'.$options->client_id.'" style="display:none">'.$straddlink.'</button>';
00064 
00065         // print out file picker
00066         //$str .= $OUTPUT->render($fp);
00067 
00068         $module = array('name'=>'data_urlpicker', 'fullpath'=>'/mod/data/data.js', 'requires'=>array('core_filepicker'));
00069         $PAGE->requires->js_init_call('M.data_urlpicker.init', array($options), true, $module);
00070         $PAGE->requires->js_function_call('show_item', array('filepicker-button-'.$options->client_id));
00071 
00072         $str .= '</div>';
00073         return $str;
00074     }
00075 
00076     function display_search_field($value = '') {
00077         return '<input type="text" size="16" name="f_'.$this->field->id.'" value="'.$value.'" />';
00078     }
00079 
00080     function parse_search_field() {
00081         return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
00082     }
00083 
00084     function generate_sql($tablealias, $value) {
00085         global $DB;
00086 
00087         static $i=0;
00088         $i++;
00089         $name = "df_url_$i";
00090         return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
00091     }
00092 
00093     function display_browse_field($recordid, $template) {
00094         global $DB;
00095 
00096         if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
00097             $url = empty($content->content)? '':$content->content;
00098             $text = empty($content->content1)? '':$content->content1;
00099             if (empty($url) or ($url == 'http://')) {
00100                 return '';
00101             }
00102             if (!empty($this->field->param2)) {
00103                 // param2 forces the text to something
00104                 $text = $this->field->param2;
00105             }
00106             if ($this->field->param1) {
00107                 // param1 defines whether we want to autolink the url.
00108                 if (!empty($text)) {
00109                     $str = '<a href="'.$url.'">'.$text.'</a>';
00110                 } else {
00111                     $str = '<a href="'.$url.'">'.$url.'</a>';
00112                 }
00113             } else {
00114                 $str = $url;
00115             }
00116             return $str;
00117         }
00118         return false;
00119     }
00120 
00121     function update_content($recordid, $value, $name='') {
00122         global $DB;
00123 
00124         $content = new stdClass();
00125         $content->fieldid = $this->field->id;
00126         $content->recordid = $recordid;
00127         $names = explode('_', $name);
00128 
00129         switch ($names[2]) {
00130             case 0:
00131                 // update link
00132                 $content->content = clean_param($value, PARAM_URL);
00133                 break;
00134             case 1:
00135                 // add text
00136                 $content->content1 = clean_param($value, PARAM_NOTAGS);
00137                 break;
00138             default:
00139                 break;
00140         }
00141 
00142         if (!empty($content->content) && (strpos($content->content, '://') === false) && (strpos($content->content, '/', 0) === false)) {
00143             $content->content = 'http://' . $content->content;
00144         }
00145 
00146         if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
00147             $content->id = $oldcontent->id;
00148             return $DB->update_record('data_content', $content);
00149         } else {
00150             return $DB->insert_record('data_content', $content);
00151         }
00152     }
00153 
00154     function notemptyfield($value, $name) {
00155         $names = explode('_',$name);
00156         $value = clean_param($value, PARAM_URL);
00157         //clean first
00158         if ($names[2] == '0') {
00159             return ($value!='http://' && !empty($value));
00160         }
00161         return false;
00162     }
00163 
00164     function export_text_value($record) {
00165         return $record->content . " " . $record->content1;
00166     }
00167 
00168 }
00169 
00170 
 All Data Structures Namespaces Files Functions Variables Enumerations