Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/blocks/rss_client/editfeed.php
Go to the documentation of this file.
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 
00027 require_once(dirname(__FILE__) . '/../../config.php');
00028 require_once($CFG->libdir . '/formslib.php');
00029 require_once($CFG->libdir .'/simplepie/moodle_simplepie.php');
00030 
00031 class feed_edit_form extends moodleform {
00032     protected $isadding;
00033     protected $caneditshared;
00034     protected $title = '';
00035     protected $description = '';
00036 
00037     function __construct($actionurl, $isadding, $caneditshared) {
00038         $this->isadding = $isadding;
00039         $this->caneditshared = $caneditshared;
00040         parent::moodleform($actionurl);
00041     }
00042 
00043     function definition() {
00044         $mform =& $this->_form;
00045 
00046         // Then show the fields about where this block appears.
00047         $mform->addElement('header', 'rsseditfeedheader', get_string('feed', 'block_rss_client'));
00048 
00049         $mform->addElement('text', 'url', get_string('feedurl', 'block_rss_client'), array('size' => 60));
00050         $mform->setType('url', PARAM_URL);
00051         $mform->addRule('url', null, 'required');
00052 
00053         $mform->addElement('checkbox', 'autodiscovery', get_string('enableautodiscovery', 'block_rss_client'));
00054         $mform->setDefault('autodiscovery', 1);
00055         $mform->setAdvanced('autodiscovery');
00056         $mform->addHelpButton('autodiscovery', 'enableautodiscovery', 'block_rss_client');
00057 
00058         $mform->addElement('text', 'preferredtitle', get_string('customtitlelabel', 'block_rss_client'), array('size' => 60));
00059         $mform->setType('preferredtitle', PARAM_NOTAGS);
00060 
00061         if ($this->caneditshared) {
00062             $mform->addElement('selectyesno', 'shared', get_string('sharedfeed', 'block_rss_client'));
00063             $mform->setDefault('shared', 0);
00064         }
00065 
00066         $submitlabal = null; // Default
00067         if ($this->isadding) {
00068             $submitlabal = get_string('addnewfeed', 'block_rss_client');
00069         }
00070         $this->add_action_buttons(true, $submitlabal);
00071     }
00072 
00073     function definition_after_data(){
00074         $mform =& $this->_form;
00075 
00076         if($mform->getElementValue('autodiscovery')){
00077             $mform->applyFilter('url', 'feed_edit_form::autodiscover_feed_url');
00078         }
00079     }
00080 
00081     function validation($data, $files) {
00082         $errors = parent::validation($data, $files);
00083 
00084         $rss =  new moodle_simplepie();
00085         // set timeout for longer than normal to try and grab the feed
00086         $rss->set_timeout(10);
00087         $rss->set_feed_url($data['url']);
00088         $rss->set_autodiscovery_cache_duration(0);
00089         $rss->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE);
00090         $rss->init();
00091 
00092         if ($rss->error()) {
00093             $errors['url'] = get_string('errorloadingfeed', 'block_rss_client', $rss->error());
00094         } else {
00095             $this->title = $rss->get_title();
00096             $this->description = $rss->get_description();
00097         }
00098 
00099         return $errors;
00100     }
00101 
00102     function get_data() {
00103         $data = parent::get_data();
00104         if ($data) {
00105             $data->title = '';
00106             $data->description = '';
00107 
00108             if($this->title){
00109                 $data->title = $this->title;
00110             }
00111 
00112             if($this->description){
00113                 $data->description = $this->description;
00114             }
00115         }
00116         return $data;
00117     }
00118 
00130     public static function autodiscover_feed_url($url){
00131             $rss =  new moodle_simplepie();
00132             $rss->set_feed_url($url);
00133             $rss->set_autodiscovery_level(SIMPLEPIE_LOCATOR_ALL);
00134             // When autodiscovering an RSS feed, simplepie will try lots of
00135             // rss links on a page, so set the timeout high
00136             $rss->set_timeout(20);
00137             $rss->init();
00138 
00139             if($rss->error()){
00140                 return $url;
00141             }
00142 
00143             // return URL without quoting..
00144             $discoveredurl = new moodle_url($rss->subscribe_url());
00145             return $discoveredurl->out(false);
00146     }
00147 }
00148 
00149 $returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
00150 $courseid = optional_param('courseid', 0, PARAM_INTEGER);
00151 $rssid = optional_param('rssid', 0, PARAM_INTEGER); // 0 mean create new.
00152 
00153 if ($courseid == SITEID) {
00154     $courseid = 0;
00155 }
00156 if ($courseid) {
00157     $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
00158     $PAGE->set_course($course);
00159     $context = $PAGE->context;
00160 } else {
00161     $context = get_context_instance(CONTEXT_SYSTEM);
00162     $PAGE->set_context($context);
00163 }
00164 
00165 $managesharedfeeds = has_capability('block/rss_client:manageanyfeeds', $context);
00166 if (!$managesharedfeeds) {
00167     require_capability('block/rss_client:manageownfeeds', $context);
00168 }
00169 
00170 $urlparams = array('rssid' => $rssid);
00171 if ($courseid) {
00172     $urlparams['courseid'] = $courseid;
00173 }
00174 if ($returnurl) {
00175     $urlparams['returnurl'] = $returnurl;
00176 }
00177 $managefeeds = new moodle_url('/blocks/rss_client/managefeeds.php', $urlparams);
00178 
00179 $PAGE->set_url('/blocks/rss_client/editfeed.php', $urlparams);
00180 $PAGE->set_pagelayout('base');
00181 
00182 if ($rssid) {
00183     $isadding = false;
00184     $rssrecord = $DB->get_record('block_rss_client', array('id' => $rssid), '*', MUST_EXIST);
00185 } else {
00186     $isadding = true;
00187     $rssrecord = new stdClass;
00188 }
00189 
00190 $mform = new feed_edit_form($PAGE->url, $isadding, $managesharedfeeds);
00191 $mform->set_data($rssrecord);
00192 
00193 if ($mform->is_cancelled()) {
00194     redirect($managefeeds);
00195 
00196 } else if ($data = $mform->get_data()) {
00197     $data->userid = $USER->id;
00198     if (!$managesharedfeeds) {
00199         $data->shared = 0;
00200     }
00201 
00202     if ($isadding) {
00203         $DB->insert_record('block_rss_client', $data);
00204     } else {
00205         $data->id = $rssid;
00206         $DB->update_record('block_rss_client', $data);
00207     }
00208 
00209     redirect($managefeeds);
00210 
00211 } else {
00212     if ($isadding) {
00213         $strtitle = get_string('addnewfeed', 'block_rss_client');
00214     } else {
00215         $strtitle = get_string('editafeed', 'block_rss_client');
00216     }
00217 
00218     $PAGE->set_title($strtitle);
00219     $PAGE->set_heading($strtitle);
00220 
00221     $settingsurl = new moodle_url('/admin/settings.php?section=blocksettingrss_client');
00222     $PAGE->navbar->add(get_string('blocks'));
00223     $PAGE->navbar->add(get_string('feedstitle', 'block_rss_client'), $settingsurl);
00224     $PAGE->navbar->add(get_string('managefeeds', 'block_rss_client'));
00225     $PAGE->navbar->add($strtitle);
00226 
00227     echo $OUTPUT->header();
00228     echo $OUTPUT->heading($strtitle, 2);
00229 
00230     $mform->display();
00231 
00232     echo $OUTPUT->footer();
00233 }
00234 
 All Data Structures Namespaces Files Functions Variables Enumerations