[ Index ]

PHP Cross Reference of moodle-2.8

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 <[email protected]>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  define('AJAX_SCRIPT', true);
  26  require_once(dirname(dirname(__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  $discussion     = $DB->get_record('forum_discussions', array('id' => $discussionid), '*', MUST_EXIST);
  36  $cm             = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
  37  $context        = context_module::instance($cm->id);
  38  
  39  require_sesskey();
  40  require_login($course, false, $cm);
  41  require_capability('mod/forum:viewdiscussion', $context);
  42  
  43  $return = new stdClass();
  44  
  45  if (is_guest($context, $USER)) {
  46      // is_guest should be used here as this also checks whether the user is a guest in the current course.
  47      // Guests and visitors cannot subscribe - only enrolled users.
  48      throw new moodle_exception('noguestsubscribe', 'mod_forum');
  49  }
  50  
  51  if (!\mod_forum\subscriptions::is_subscribable($forum)) {
  52      // Nothing to do. We won't actually output any content here though.
  53      echo json_encode($return);
  54      die;
  55  }
  56  
  57  if (\mod_forum\subscriptions::is_subscribed($USER->id, $forum, $discussion->id, $cm)) {
  58      // The user is subscribed, unsubscribe them.
  59      \mod_forum\subscriptions::unsubscribe_user_from_discussion($USER->id, $discussion, $context);
  60  } else {
  61      // The user is unsubscribed, subscribe them.
  62      \mod_forum\subscriptions::subscribe_user_to_discussion($USER->id, $discussion, $context);
  63  }
  64  
  65  // Now return the updated subscription icon.
  66  $return->icon = forum_get_discussion_subscription_icon($forum, $discussion->id, null, $includetext);
  67  echo json_encode($return);
  68  die;


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