[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/blog/ -> external_blog_edit.php (source)

   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  
  18  /**
  19   * Form page for an external blog link.
  20   *
  21   * @package    moodlecore
  22   * @subpackage blog
  23   * @copyright  2009 Nicolas Connault
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  require_once('../config.php');
  28  require_once ('lib.php');
  29  require_once ('external_blog_edit_form.php');
  30  require_once($CFG->libdir . '/simplepie/moodle_simplepie.php');
  31  require_once($CFG->dirroot.'/tag/lib.php');
  32  
  33  require_login();
  34  $context = context_system::instance();
  35  require_capability('moodle/blog:manageexternal', $context);
  36  
  37  // TODO redirect if $CFG->useexternalblogs is off,
  38  //                  $CFG->maxexternalblogsperuser == 0,
  39  //                  or if user doesn't have caps to manage external blogs.
  40  
  41  $id = optional_param('id', null, PARAM_INT);
  42  $url = new moodle_url('/blog/external_blog_edit.php');
  43  if ($id !== null) {
  44      $url->param('id', $id);
  45  }
  46  $PAGE->set_url($url);
  47  $PAGE->set_context($context);
  48  $PAGE->set_pagelayout('admin');
  49  
  50  $returnurl = new moodle_url('/blog/external_blogs.php');
  51  
  52  $action = (empty($id)) ? 'add' : 'edit';
  53  
  54  $external = new stdClass();
  55  
  56  // Check that this id exists.
  57  if (!empty($id) && !$DB->record_exists('blog_external', array('id' => $id))) {
  58      print_error('wrongexternalid', 'blog');
  59  } else if (!empty($id)) {
  60      $external = $DB->get_record('blog_external', array('id' => $id));
  61  }
  62  
  63  $strformheading = ($action == 'edit') ? get_string('editexternalblog', 'blog') : get_string('addnewexternalblog', 'blog');
  64  $strexternalblogs = get_string('externalblogs', 'blog');
  65  $strblogs = get_string('blogs', 'blog');
  66  
  67  $externalblogform = new blog_edit_external_form();
  68  
  69  if ($externalblogform->is_cancelled()) {
  70      redirect($returnurl);
  71  
  72  } else if ($data = $externalblogform->get_data()) {
  73      // Save stuff in db.
  74      switch ($action) {
  75          case 'add':
  76              $rss = new moodle_simplepie($data->url);
  77  
  78              $newexternal = new stdClass();
  79              $newexternal->name = (empty($data->name)) ? $rss->get_title() : $data->name;
  80              $newexternal->description = (empty($data->description)) ? $rss->get_description() : $data->description;
  81              $newexternal->userid = $USER->id;
  82              $newexternal->url = $data->url;
  83              $newexternal->filtertags = (!empty($data->filtertags)) ? $data->filtertags : null;
  84              $newexternal->timemodified = time();
  85  
  86              $newexternal->id = $DB->insert_record('blog_external', $newexternal);
  87              blog_sync_external_entries($newexternal);
  88              if ($CFG->usetags) {
  89                  $autotags = (!empty($data->autotags)) ? $data->autotags : null;
  90                  tag_set('blog_external', $newexternal->id, explode(',', $autotags), 'core',
  91                      context_user::instance($newexternal->userid)->id);
  92              }
  93  
  94              break;
  95  
  96          case 'edit':
  97              if ($data->id && $DB->record_exists('blog_external', array('id' => $data->id))) {
  98  
  99                  $rss = new moodle_simplepie($data->url);
 100  
 101                  $external->id = $data->id;
 102                  $external->name = (empty($data->name)) ? $rss->get_title() : $data->name;
 103                  $external->description = (empty($data->description)) ? $rss->get_description() : $data->description;
 104                  $external->userid = $USER->id;
 105                  $external->url = $data->url;
 106                  $external->filtertags = (!empty($data->filtertags)) ? $data->filtertags : null;
 107                  $external->timemodified = time();
 108  
 109                  $DB->update_record('blog_external', $external);
 110                  if ($CFG->usetags) {
 111                      $autotags = (!empty($data->autotags)) ? $data->autotags : null;
 112                      tag_set('blog_external', $external->id, explode(',', $autotags), 'core',
 113                          context_user::instance($external->userid)->id);
 114                  }
 115              } else {
 116                  print_error('wrongexternalid', 'blog');
 117              }
 118  
 119              break;
 120  
 121          default :
 122              print_error('invalidaction');
 123      }
 124  
 125      redirect($returnurl);
 126  }
 127  
 128  $PAGE->set_heading("$SITE->shortname: $strblogs: $strexternalblogs", $SITE->fullname);
 129  $PAGE->set_title("$SITE->shortname: $strblogs: $strexternalblogs");
 130  
 131  echo $OUTPUT->header();
 132  echo $OUTPUT->heading($strformheading, 2);
 133  
 134  $externalblogform->set_data($external);
 135  $externalblogform->display();
 136  
 137  echo $OUTPUT->footer();


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1