editfeed.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Script to let a user edit the properties of a particular RSS feed.
  18. *
  19. * @package block_rss_client
  20. * @copyright 2009 Tim Hunt
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require_once(__DIR__ . '/../../config.php');
  24. require_once($CFG->libdir . '/formslib.php');
  25. require_once($CFG->libdir .'/simplepie/moodle_simplepie.php');
  26. class feed_edit_form extends moodleform {
  27. protected $isadding;
  28. protected $caneditshared;
  29. protected $title = '';
  30. protected $description = '';
  31. function __construct($actionurl, $isadding, $caneditshared) {
  32. $this->isadding = $isadding;
  33. $this->caneditshared = $caneditshared;
  34. parent::__construct($actionurl);
  35. }
  36. function definition() {
  37. $mform =& $this->_form;
  38. // Then show the fields about where this block appears.
  39. $mform->addElement('header', 'rsseditfeedheader', get_string('feed', 'block_rss_client'));
  40. $mform->addElement('text', 'url', get_string('feedurl', 'block_rss_client'), array('size' => 60));
  41. $mform->setType('url', PARAM_URL);
  42. $mform->addRule('url', null, 'required');
  43. $mform->addElement('checkbox', 'autodiscovery', get_string('enableautodiscovery', 'block_rss_client'));
  44. $mform->setDefault('autodiscovery', 1);
  45. $mform->setAdvanced('autodiscovery');
  46. $mform->addHelpButton('autodiscovery', 'enableautodiscovery', 'block_rss_client');
  47. $mform->addElement('text', 'preferredtitle', get_string('customtitlelabel', 'block_rss_client'), array('size' => 60));
  48. $mform->setType('preferredtitle', PARAM_NOTAGS);
  49. if ($this->caneditshared) {
  50. $mform->addElement('selectyesno', 'shared', get_string('sharedfeed', 'block_rss_client'));
  51. $mform->setDefault('shared', 0);
  52. }
  53. $submitlabal = null; // Default
  54. if ($this->isadding) {
  55. $submitlabal = get_string('addnewfeed', 'block_rss_client');
  56. }
  57. $this->add_action_buttons(true, $submitlabal);
  58. }
  59. function definition_after_data(){
  60. $mform =& $this->_form;
  61. if($mform->getElementValue('autodiscovery')){
  62. $mform->applyFilter('url', 'feed_edit_form::autodiscover_feed_url');
  63. }
  64. }
  65. function validation($data, $files) {
  66. $errors = parent::validation($data, $files);
  67. $rss = new moodle_simplepie();
  68. // set timeout for longer than normal to try and grab the feed
  69. $rss->set_timeout(10);
  70. $rss->set_feed_url($data['url']);
  71. $rss->set_autodiscovery_cache_duration(0);
  72. $rss->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE);
  73. $rss->init();
  74. if ($rss->error()) {
  75. $errors['url'] = get_string('couldnotfindloadrssfeed', 'block_rss_client');
  76. } else {
  77. $this->title = $rss->get_title();
  78. $this->description = $rss->get_description();
  79. }
  80. return $errors;
  81. }
  82. function get_data() {
  83. $data = parent::get_data();
  84. if ($data) {
  85. $data->title = '';
  86. $data->description = '';
  87. if($this->title){
  88. $data->title = $this->title;
  89. }
  90. if($this->description){
  91. $data->description = $this->description;
  92. }
  93. }
  94. return $data;
  95. }
  96. /**
  97. * Autodiscovers a feed url from a given url, to be used by the formslibs
  98. * filter function
  99. *
  100. * Uses simplepie with autodiscovery set to maximum level to try and find
  101. * a feed to subscribe to.
  102. * See: http://simplepie.org/wiki/reference/simplepie/set_autodiscovery_level
  103. *
  104. * @param string URL to autodiscover a url
  105. * @return string URL of feed or original url if none found
  106. */
  107. public static function autodiscover_feed_url($url){
  108. $rss = new moodle_simplepie();
  109. $rss->set_feed_url($url);
  110. $rss->set_autodiscovery_level(SIMPLEPIE_LOCATOR_ALL);
  111. // When autodiscovering an RSS feed, simplepie will try lots of
  112. // rss links on a page, so set the timeout high
  113. $rss->set_timeout(20);
  114. $rss->init();
  115. if($rss->error()){
  116. return $url;
  117. }
  118. // return URL without quoting..
  119. $discoveredurl = new moodle_url($rss->subscribe_url());
  120. return $discoveredurl->out(false);
  121. }
  122. }
  123. $returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
  124. $courseid = optional_param('courseid', 0, PARAM_INT);
  125. $rssid = optional_param('rssid', 0, PARAM_INT); // 0 mean create new.
  126. if ($courseid == SITEID) {
  127. $courseid = 0;
  128. }
  129. if ($courseid) {
  130. $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
  131. $PAGE->set_course($course);
  132. $context = $PAGE->context;
  133. } else {
  134. $context = context_system::instance();
  135. $PAGE->set_context($context);
  136. }
  137. $managesharedfeeds = has_capability('block/rss_client:manageanyfeeds', $context);
  138. if (!$managesharedfeeds) {
  139. require_capability('block/rss_client:manageownfeeds', $context);
  140. }
  141. $urlparams = array('rssid' => $rssid);
  142. if ($courseid) {
  143. $urlparams['courseid'] = $courseid;
  144. }
  145. if ($returnurl) {
  146. $urlparams['returnurl'] = $returnurl;
  147. }
  148. $managefeeds = new moodle_url('/blocks/rss_client/managefeeds.php', $urlparams);
  149. $PAGE->set_url('/blocks/rss_client/editfeed.php', $urlparams);
  150. $PAGE->set_pagelayout('admin');
  151. if ($rssid) {
  152. $isadding = false;
  153. $rssrecord = $DB->get_record('block_rss_client', array('id' => $rssid), '*', MUST_EXIST);
  154. } else {
  155. $isadding = true;
  156. $rssrecord = new stdClass;
  157. }
  158. $mform = new feed_edit_form($PAGE->url, $isadding, $managesharedfeeds);
  159. $mform->set_data($rssrecord);
  160. if ($mform->is_cancelled()) {
  161. redirect($managefeeds);
  162. } else if ($data = $mform->get_data()) {
  163. $data->userid = $USER->id;
  164. if (!$managesharedfeeds) {
  165. $data->shared = 0;
  166. }
  167. if ($isadding) {
  168. $DB->insert_record('block_rss_client', $data);
  169. } else {
  170. $data->id = $rssid;
  171. $DB->update_record('block_rss_client', $data);
  172. }
  173. redirect($managefeeds);
  174. } else {
  175. if ($isadding) {
  176. $strtitle = get_string('addnewfeed', 'block_rss_client');
  177. } else {
  178. $strtitle = get_string('editafeed', 'block_rss_client');
  179. }
  180. $PAGE->set_title($strtitle);
  181. $PAGE->set_heading($strtitle);
  182. $PAGE->navbar->add(get_string('blocks'));
  183. $PAGE->navbar->add(get_string('pluginname', 'block_rss_client'));
  184. $PAGE->navbar->add(get_string('managefeeds', 'block_rss_client'), $managefeeds );
  185. $PAGE->navbar->add($strtitle);
  186. echo $OUTPUT->header();
  187. echo $OUTPUT->heading($strtitle, 2);
  188. $mform->display();
  189. echo $OUTPUT->footer();
  190. }