[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/forum/ -> subscribe_ajax.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   * Subscribe to or unsubscribe from a forum discussion.
  19   *
  20   * @package    mod_forum
  21   * @copyright  2014 Andrew Nicols <andrew@nicols.co.uk>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  define('AJAX_SCRIPT', true);
  26  require(__DIR__.'/../../config.php');
  27  require_once($CFG->dirroot . '/mod/forum/lib.php');
  28  
  29  $forumid        = required_param('forumid', PARAM_INT);             // The forum to subscribe or unsubscribe.
  30  $discussionid   = optional_param('discussionid', null, PARAM_INT);  // The discussionid to subscribe.
  31  $includetext    = optional_param('includetext', false, PARAM_BOOL);
  32  
  33  $forum          = $DB->get_record('forum', array('id' => $forumid), '*', MUST_EXIST);
  34  $course         = $DB->get_record('course', array('id' => $forum->course), '*', MUST_EXIST);
  35  if (!$discussion = $DB->get_record('forum_discussions', array('id' => $discussionid, 'forum' => $forumid))) {
  36      print_error('invaliddiscussionid', 'forum');
  37  }
  38  $cm             = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
  39  $context        = context_module::instance($cm->id);
  40  
  41  require_sesskey();
  42  require_login($course, false, $cm);
  43  require_capability('mod/forum:viewdiscussion', $context);
  44  
  45  $return = new stdClass();
  46  
  47  if (is_guest($context, $USER)) {
  48      // is_guest should be used here as this also checks whether the user is a guest in the current course.
  49      // Guests and visitors cannot subscribe - only enrolled users.
  50      throw new moodle_exception('noguestsubscribe', 'mod_forum');
  51  }
  52  
  53  if (!\mod_forum\subscriptions::is_subscribable($forum)) {
  54      // Nothing to do. We won't actually output any content here though.
  55      echo json_encode($return);
  56      die;
  57  }
  58  
  59  if (\mod_forum\subscriptions::is_subscribed($USER->id, $forum, $discussion->id, $cm)) {
  60      // The user is subscribed, unsubscribe them.
  61      \mod_forum\subscriptions::unsubscribe_user_from_discussion($USER->id, $discussion, $context);
  62  } else {
  63      // The user is unsubscribed, subscribe them.
  64      \mod_forum\subscriptions::subscribe_user_to_discussion($USER->id, $discussion, $context);
  65  }
  66  
  67  // Now return the updated subscription icon.
  68  $return->icon = forum_get_discussion_subscription_icon($forum, $discussion->id, null, $includetext);
  69  echo json_encode($return);
  70  die;


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1