|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00018 00028 if (!defined('MOODLE_INTERNAL')) { 00029 die('Direct access to this script is forbidden.'); 00030 } 00031 00032 require_once($CFG->libdir.'/formslib.php'); 00033 00034 class blog_edit_external_form extends moodleform { 00035 public function definition() { 00036 global $CFG; 00037 00038 $mform =& $this->_form; 00039 00040 $mform->addElement('text', 'url', get_string('url', 'blog'), array('size' => 50)); 00041 $mform->addRule('url', get_string('emptyurl', 'blog'), 'required', null, 'client'); 00042 $mform->addHelpButton('url', 'url', 'blog'); 00043 00044 $mform->addElement('text', 'name', get_string('name', 'blog')); 00045 $mform->addHelpButton('name', 'name', 'blog'); 00046 00047 $mform->addElement('textarea', 'description', get_string('description', 'blog'), array('cols' => 50, 'rows' => 7)); 00048 $mform->addHelpButton('description', 'description', 'blog'); 00049 00050 if (!empty($CFG->usetags)) { 00051 $mform->addElement('text', 'filtertags', get_string('filtertags', 'blog'), array('size' => 50)); 00052 $mform->addHelpButton('filtertags', 'filtertags', 'blog'); 00053 $mform->addElement('text', 'autotags', get_string('autotags', 'blog'), array('size' => 50)); 00054 $mform->addHelpButton('autotags', 'autotags', 'blog'); 00055 } 00056 00057 $this->add_action_buttons(); 00058 00059 $mform->addElement('hidden', 'id'); 00060 $mform->setType('id', PARAM_INT); 00061 $mform->setDefault('id', 0); 00062 00063 $mform->addElement('hidden', 'returnurl'); 00064 $mform->setType('returnurl', PARAM_URL); 00065 $mform->setDefault('returnurl', 0); 00066 } 00067 00071 public function validation($data, $files) { 00072 global $CFG; 00073 00074 $errors = parent::validation($data, $files); 00075 00076 require_once($CFG->libdir . '/simplepie/moodle_simplepie.php'); 00077 00078 $rssfile = new moodle_simplepie_file($data['url']); 00079 $filetest = new SimplePie_Locator($rssfile); 00080 00081 if (!$filetest->is_feed($rssfile)) { 00082 $errors['url'] = get_string('feedisinvalid', 'blog'); 00083 } else { 00084 $rss = new moodle_simplepie($data['url']); 00085 if (!$rss->init()) { 00086 $errors['url'] = get_string('emptyrssfeed', 'blog'); 00087 } 00088 } 00089 00090 return $errors; 00091 } 00092 00093 public function definition_after_data() { 00094 global $CFG, $COURSE; 00095 $mform =& $this->_form; 00096 00097 $name = trim($mform->getElementValue('name')); 00098 $description = trim($mform->getElementValue('description')); 00099 $url = $mform->getElementValue('url'); 00100 00101 if (empty($name) || empty($description)) { 00102 $rss = new moodle_simplepie($url); 00103 00104 if (empty($name) && $rss->get_title()) { 00105 $mform->setDefault('name', $rss->get_title()); 00106 } 00107 00108 if (empty($description) && $rss->get_description()) { 00109 $mform->setDefault('description', $rss->get_description()); 00110 } 00111 } 00112 00113 if ($id = $mform->getElementValue('id')) { 00114 $mform->setDefault('autotags', implode(',', tag_get_tags_array('blog_external', $id))); 00115 $mform->freeze('url'); 00116 $mform->freeze('filtertags'); 00117 // TODO change the filtertags element to a multiple select, using the tags of the external blog 00118 // Use $rss->get_channel_tags() 00119 } 00120 } 00121 }