PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/blog/external_blog_edit_form.php

https://bitbucket.org/moodle/moodle
PHP | 126 lines | 72 code | 26 blank | 28 comment | 12 complexity | 6d2211b002dc8daa8976ef065f9dc699 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * Moodleform for the user interface for managing external blog links.
  18. *
  19. * @package moodlecore
  20. * @subpackage blog
  21. * @copyright 2009 Nicolas Connault
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. if (!defined('MOODLE_INTERNAL')) {
  25. die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
  26. }
  27. require_once($CFG->libdir.'/formslib.php');
  28. class blog_edit_external_form extends moodleform {
  29. public function definition() {
  30. global $CFG;
  31. $mform =& $this->_form;
  32. $mform->addElement('url', 'url', get_string('url', 'blog'), array('size' => 60), array('usefilepicker' => false));
  33. $mform->setType('url', PARAM_URL);
  34. $mform->addRule('url', get_string('emptyurl', 'blog'), 'required', null, 'client');
  35. $mform->addHelpButton('url', 'url', 'blog');
  36. $mform->addElement('text', 'name', get_string('name', 'blog'));
  37. $mform->setType('name', PARAM_TEXT);
  38. $mform->addHelpButton('name', 'name', 'blog');
  39. $mform->addElement('textarea', 'description', get_string('description', 'blog'), array('cols' => 50, 'rows' => 7));
  40. $mform->addHelpButton('description', 'description', 'blog');
  41. // To filter external blogs by their tags we do not need to check if tags in moodle are enabled.
  42. $mform->addElement('text', 'filtertags', get_string('filtertags', 'blog'), array('size' => 50));
  43. $mform->setType('filtertags', PARAM_TAGLIST);
  44. $mform->addHelpButton('filtertags', 'filtertags', 'blog');
  45. $mform->addElement('tags', 'autotags', get_string('autotags', 'blog'),
  46. array('itemtype' => 'blog_external', 'component' => 'core'));
  47. $mform->addHelpButton('autotags', 'autotags', 'blog');
  48. $this->add_action_buttons();
  49. $mform->addElement('hidden', 'id');
  50. $mform->setType('id', PARAM_INT);
  51. $mform->setDefault('id', 0);
  52. $mform->addElement('hidden', 'returnurl');
  53. $mform->setType('returnurl', PARAM_URL);
  54. $mform->setDefault('returnurl', 0);
  55. }
  56. /**
  57. * Additional validation includes checking URL and tags
  58. */
  59. public function validation($data, $files) {
  60. global $CFG;
  61. $errors = parent::validation($data, $files);
  62. require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
  63. $rss = new moodle_simplepie();
  64. $rssfile = $rss->registry->create('File', array($data['url']));
  65. $filetest = $rss->registry->create('Locator', array($rssfile));
  66. if (!$filetest->is_feed($rssfile)) {
  67. $errors['url'] = get_string('feedisinvalid', 'blog');
  68. } else {
  69. $rss->set_feed_url($data['url']);
  70. if (!$rss->init()) {
  71. $errors['url'] = get_string('emptyrssfeed', 'blog');
  72. }
  73. }
  74. return $errors;
  75. }
  76. public function definition_after_data() {
  77. global $CFG, $COURSE;
  78. $mform =& $this->_form;
  79. $name = trim($mform->getElementValue('name'));
  80. $description = trim($mform->getElementValue('description'));
  81. $url = $mform->getElementValue('url');
  82. if (empty($name) || empty($description)) {
  83. $rss = new moodle_simplepie($url);
  84. if (empty($name) && $rss->get_title()) {
  85. $mform->setDefault('name', $rss->get_title());
  86. }
  87. if (empty($description) && $rss->get_description()) {
  88. $mform->setDefault('description', $rss->get_description());
  89. }
  90. }
  91. if ($id = $mform->getElementValue('id')) {
  92. $mform->freeze('url');
  93. if ($mform->elementExists('filtertags')) {
  94. $mform->freeze('filtertags');
  95. }
  96. // TODO change the filtertags element to a multiple select, using the tags of the external blog
  97. // Use $rss->get_channel_tags().
  98. }
  99. }
  100. }