[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/forum/ -> post.php (source)

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Edit and save a new post to a discussion
  20   *
  21   * @package   mod_forum
  22   * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require_once('../../config.php');
  27  require_once ('lib.php');
  28  require_once($CFG->libdir.'/completionlib.php');
  29  
  30  $reply   = optional_param('reply', 0, PARAM_INT);
  31  $forum   = optional_param('forum', 0, PARAM_INT);
  32  $edit    = optional_param('edit', 0, PARAM_INT);
  33  $delete  = optional_param('delete', 0, PARAM_INT);
  34  $prune   = optional_param('prune', 0, PARAM_INT);
  35  $name    = optional_param('name', '', PARAM_CLEAN);
  36  $confirm = optional_param('confirm', 0, PARAM_INT);
  37  $groupid = optional_param('groupid', null, PARAM_INT);
  38  
  39  $PAGE->set_url('/mod/forum/post.php', array(
  40          'reply' => $reply,
  41          'forum' => $forum,
  42          'edit'  => $edit,
  43          'delete'=> $delete,
  44          'prune' => $prune,
  45          'name'  => $name,
  46          'confirm'=>$confirm,
  47          'groupid'=>$groupid,
  48          ));
  49  //these page_params will be passed as hidden variables later in the form.
  50  $page_params = array('reply'=>$reply, 'forum'=>$forum, 'edit'=>$edit);
  51  
  52  $sitecontext = context_system::instance();
  53  
  54  if (!isloggedin() or isguestuser()) {
  55  
  56      if (!isloggedin() and !get_referer()) {
  57          // No referer+not logged in - probably coming in via email  See MDL-9052
  58          require_login();
  59      }
  60  
  61      if (!empty($forum)) {      // User is starting a new discussion in a forum
  62          if (! $forum = $DB->get_record('forum', array('id' => $forum))) {
  63              print_error('invalidforumid', 'forum');
  64          }
  65      } else if (!empty($reply)) {      // User is writing a new reply
  66          if (! $parent = forum_get_post_full($reply)) {
  67              print_error('invalidparentpostid', 'forum');
  68          }
  69          if (! $discussion = $DB->get_record('forum_discussions', array('id' => $parent->discussion))) {
  70              print_error('notpartofdiscussion', 'forum');
  71          }
  72          if (! $forum = $DB->get_record('forum', array('id' => $discussion->forum))) {
  73              print_error('invalidforumid');
  74          }
  75      }
  76      if (! $course = $DB->get_record('course', array('id' => $forum->course))) {
  77          print_error('invalidcourseid');
  78      }
  79  
  80      if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
  81          print_error('invalidcoursemodule');
  82      } else {
  83          $modcontext = context_module::instance($cm->id);
  84      }
  85  
  86      $PAGE->set_cm($cm, $course, $forum);
  87      $PAGE->set_context($modcontext);
  88      $PAGE->set_title($course->shortname);
  89      $PAGE->set_heading($course->fullname);
  90  
  91      echo $OUTPUT->header();
  92      echo $OUTPUT->confirm(get_string('noguestpost', 'forum').'<br /><br />'.get_string('liketologin'), get_login_url(), get_referer(false));
  93      echo $OUTPUT->footer();
  94      exit;
  95  }
  96  
  97  require_login(0, false);   // Script is useless unless they're logged in
  98  
  99  if (!empty($forum)) {      // User is starting a new discussion in a forum
 100      if (! $forum = $DB->get_record("forum", array("id" => $forum))) {
 101          print_error('invalidforumid', 'forum');
 102      }
 103      if (! $course = $DB->get_record("course", array("id" => $forum->course))) {
 104          print_error('invalidcourseid');
 105      }
 106      if (! $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
 107          print_error("invalidcoursemodule");
 108      }
 109  
 110      // Retrieve the contexts.
 111      $modcontext    = context_module::instance($cm->id);
 112      $coursecontext = context_course::instance($course->id);
 113  
 114      if (! forum_user_can_post_discussion($forum, $groupid, -1, $cm)) {
 115          if (!isguestuser()) {
 116              if (!is_enrolled($coursecontext)) {
 117                  if (enrol_selfenrol_available($course->id)) {
 118                      $SESSION->wantsurl = qualified_me();
 119                      $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
 120                      redirect($CFG->wwwroot.'/enrol/index.php?id='.$course->id, get_string('youneedtoenrol'));
 121                  }
 122              }
 123          }
 124          print_error('nopostforum', 'forum');
 125      }
 126  
 127      if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $modcontext)) {
 128          print_error("activityiscurrentlyhidden");
 129      }
 130  
 131      if (isset($_SERVER["HTTP_REFERER"])) {
 132          $SESSION->fromurl = $_SERVER["HTTP_REFERER"];
 133      } else {
 134          $SESSION->fromurl = '';
 135      }
 136  
 137  
 138      // Load up the $post variable.
 139  
 140      $post = new stdClass();
 141      $post->course        = $course->id;
 142      $post->forum         = $forum->id;
 143      $post->discussion    = 0;           // ie discussion # not defined yet
 144      $post->parent        = 0;
 145      $post->subject       = '';
 146      $post->userid        = $USER->id;
 147      $post->message       = '';
 148      $post->messageformat = editors_get_preferred_format();
 149      $post->messagetrust  = 0;
 150  
 151      if (isset($groupid)) {
 152          $post->groupid = $groupid;
 153      } else {
 154          $post->groupid = groups_get_activity_group($cm);
 155      }
 156  
 157      // Unsetting this will allow the correct return URL to be calculated later.
 158      unset($SESSION->fromdiscussion);
 159  
 160  } else if (!empty($reply)) {      // User is writing a new reply
 161  
 162      if (! $parent = forum_get_post_full($reply)) {
 163          print_error('invalidparentpostid', 'forum');
 164      }
 165      if (! $discussion = $DB->get_record("forum_discussions", array("id" => $parent->discussion))) {
 166          print_error('notpartofdiscussion', 'forum');
 167      }
 168      if (! $forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
 169          print_error('invalidforumid', 'forum');
 170      }
 171      if (! $course = $DB->get_record("course", array("id" => $discussion->course))) {
 172          print_error('invalidcourseid');
 173      }
 174      if (! $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
 175          print_error('invalidcoursemodule');
 176      }
 177  
 178      // Ensure lang, theme, etc. is set up properly. MDL-6926
 179      $PAGE->set_cm($cm, $course, $forum);
 180  
 181      // Retrieve the contexts.
 182      $modcontext    = context_module::instance($cm->id);
 183      $coursecontext = context_course::instance($course->id);
 184  
 185      if (! forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext)) {
 186          if (!isguestuser()) {
 187              if (!is_enrolled($coursecontext)) {  // User is a guest here!
 188                  $SESSION->wantsurl = qualified_me();
 189                  $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
 190                  redirect($CFG->wwwroot.'/enrol/index.php?id='.$course->id, get_string('youneedtoenrol'));
 191              }
 192          }
 193          print_error('nopostforum', 'forum');
 194      }
 195  
 196      // Make sure user can post here
 197      if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
 198          $groupmode =  $cm->groupmode;
 199      } else {
 200          $groupmode = $course->groupmode;
 201      }
 202      if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $modcontext)) {
 203          if ($discussion->groupid == -1) {
 204              print_error('nopostforum', 'forum');
 205          } else {
 206              if (!groups_is_member($discussion->groupid)) {
 207                  print_error('nopostforum', 'forum');
 208              }
 209          }
 210      }
 211  
 212      if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $modcontext)) {
 213          print_error("activityiscurrentlyhidden");
 214      }
 215  
 216      // Load up the $post variable.
 217  
 218      $post = new stdClass();
 219      $post->course      = $course->id;
 220      $post->forum       = $forum->id;
 221      $post->discussion  = $parent->discussion;
 222      $post->parent      = $parent->id;
 223      $post->subject     = $parent->subject;
 224      $post->userid      = $USER->id;
 225      $post->message     = '';
 226  
 227      $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
 228  
 229      $strre = get_string('re', 'forum');
 230      if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
 231          $post->subject = $strre.' '.$post->subject;
 232      }
 233  
 234      // Unsetting this will allow the correct return URL to be calculated later.
 235      unset($SESSION->fromdiscussion);
 236  
 237  } else if (!empty($edit)) {  // User is editing their own post
 238  
 239      if (! $post = forum_get_post_full($edit)) {
 240          print_error('invalidpostid', 'forum');
 241      }
 242      if ($post->parent) {
 243          if (! $parent = forum_get_post_full($post->parent)) {
 244              print_error('invalidparentpostid', 'forum');
 245          }
 246      }
 247  
 248      if (! $discussion = $DB->get_record("forum_discussions", array("id" => $post->discussion))) {
 249          print_error('notpartofdiscussion', 'forum');
 250      }
 251      if (! $forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
 252          print_error('invalidforumid', 'forum');
 253      }
 254      if (! $course = $DB->get_record("course", array("id" => $discussion->course))) {
 255          print_error('invalidcourseid');
 256      }
 257      if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
 258          print_error('invalidcoursemodule');
 259      } else {
 260          $modcontext = context_module::instance($cm->id);
 261      }
 262  
 263      $PAGE->set_cm($cm, $course, $forum);
 264  
 265      if (!($forum->type == 'news' && !$post->parent && $discussion->timestart > time())) {
 266          if (((time() - $post->created) > $CFG->maxeditingtime) and
 267                      !has_capability('mod/forum:editanypost', $modcontext)) {
 268              print_error('maxtimehaspassed', 'forum', '', format_time($CFG->maxeditingtime));
 269          }
 270      }
 271      if (($post->userid <> $USER->id) and
 272                  !has_capability('mod/forum:editanypost', $modcontext)) {
 273          print_error('cannoteditposts', 'forum');
 274      }
 275  
 276  
 277      // Load up the $post variable.
 278      $post->edit   = $edit;
 279      $post->course = $course->id;
 280      $post->forum  = $forum->id;
 281      $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
 282  
 283      $post = trusttext_pre_edit($post, 'message', $modcontext);
 284  
 285      // Unsetting this will allow the correct return URL to be calculated later.
 286      unset($SESSION->fromdiscussion);
 287  
 288  }else if (!empty($delete)) {  // User is deleting a post
 289  
 290      if (! $post = forum_get_post_full($delete)) {
 291          print_error('invalidpostid', 'forum');
 292      }
 293      if (! $discussion = $DB->get_record("forum_discussions", array("id" => $post->discussion))) {
 294          print_error('notpartofdiscussion', 'forum');
 295      }
 296      if (! $forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
 297          print_error('invalidforumid', 'forum');
 298      }
 299      if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) {
 300          print_error('invalidcoursemodule');
 301      }
 302      if (!$course = $DB->get_record('course', array('id' => $forum->course))) {
 303          print_error('invalidcourseid');
 304      }
 305  
 306      require_login($course, false, $cm);
 307      $modcontext = context_module::instance($cm->id);
 308  
 309      if ( !(($post->userid == $USER->id && has_capability('mod/forum:deleteownpost', $modcontext))
 310                  || has_capability('mod/forum:deleteanypost', $modcontext)) ) {
 311          print_error('cannotdeletepost', 'forum');
 312      }
 313  
 314  
 315      $replycount = forum_count_replies($post);
 316  
 317      if (!empty($confirm) && confirm_sesskey()) {    // User has confirmed the delete
 318          //check user capability to delete post.
 319          $timepassed = time() - $post->created;
 320          if (($timepassed > $CFG->maxeditingtime) && !has_capability('mod/forum:deleteanypost', $modcontext)) {
 321              print_error("cannotdeletepost", "forum",
 322                        forum_go_back_to("discuss.php?d=$post->discussion"));
 323          }
 324  
 325          if ($post->totalscore) {
 326              notice(get_string('couldnotdeleteratings', 'rating'),
 327                      forum_go_back_to("discuss.php?d=$post->discussion"));
 328  
 329          } else if ($replycount && !has_capability('mod/forum:deleteanypost', $modcontext)) {
 330              print_error("couldnotdeletereplies", "forum",
 331                      forum_go_back_to("discuss.php?d=$post->discussion"));
 332  
 333          } else {
 334              if (! $post->parent) {  // post is a discussion topic as well, so delete discussion
 335                  if ($forum->type == 'single') {
 336                      notice("Sorry, but you are not allowed to delete that discussion!",
 337                              forum_go_back_to("discuss.php?d=$post->discussion"));
 338                  }
 339                  forum_delete_discussion($discussion, false, $course, $cm, $forum);
 340  
 341                  $params = array(
 342                      'objectid' => $discussion->id,
 343                      'context' => $modcontext,
 344                      'other' => array(
 345                          'forumid' => $forum->id,
 346                      )
 347                  );
 348  
 349                  $event = \mod_forum\event\discussion_deleted::create($params);
 350                  $event->add_record_snapshot('forum_discussions', $discussion);
 351                  $event->trigger();
 352  
 353                  redirect("view.php?f=$discussion->forum");
 354  
 355              } else if (forum_delete_post($post, has_capability('mod/forum:deleteanypost', $modcontext),
 356                  $course, $cm, $forum)) {
 357  
 358                  if ($forum->type == 'single') {
 359                      // Single discussion forums are an exception. We show
 360                      // the forum itself since it only has one discussion
 361                      // thread.
 362                      $discussionurl = "view.php?f=$forum->id";
 363                  } else {
 364                      $discussionurl = "discuss.php?d=$post->discussion";
 365                  }
 366  
 367                  $params = array(
 368                      'context' => $modcontext,
 369                      'objectid' => $post->id,
 370                      'other' => array(
 371                          'discussionid' => $discussion->id,
 372                          'forumid' => $forum->id,
 373                          'forumtype' => $forum->type,
 374                      )
 375                  );
 376  
 377                  if ($post->userid !== $USER->id) {
 378                      $params['relateduserid'] = $post->userid;
 379                  }
 380                  $event = \mod_forum\event\post_deleted::create($params);
 381                  $event->add_record_snapshot('forum_posts', $post);
 382                  $event->add_record_snapshot('forum_discussions', $discussion);
 383                  $event->trigger();
 384  
 385                  redirect(forum_go_back_to($discussionurl));
 386              } else {
 387                  print_error('errorwhiledelete', 'forum');
 388              }
 389          }
 390  
 391  
 392      } else { // User just asked to delete something
 393  
 394          forum_set_return();
 395          $PAGE->navbar->add(get_string('delete', 'forum'));
 396          $PAGE->set_title($course->shortname);
 397          $PAGE->set_heading($course->fullname);
 398  
 399          if ($replycount) {
 400              if (!has_capability('mod/forum:deleteanypost', $modcontext)) {
 401                  print_error("couldnotdeletereplies", "forum",
 402                        forum_go_back_to("discuss.php?d=$post->discussion"));
 403              }
 404              echo $OUTPUT->header();
 405              echo $OUTPUT->heading(format_string($forum->name), 2);
 406              echo $OUTPUT->confirm(get_string("deletesureplural", "forum", $replycount+1),
 407                           "post.php?delete=$delete&confirm=$delete",
 408                           $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
 409  
 410              forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false);
 411  
 412              if (empty($post->edit)) {
 413                  $forumtracked = forum_tp_is_tracked($forum);
 414                  $posts = forum_get_all_discussion_posts($discussion->id, "created ASC", $forumtracked);
 415                  forum_print_posts_nested($course, $cm, $forum, $discussion, $post, false, false, $forumtracked, $posts);
 416              }
 417          } else {
 418              echo $OUTPUT->header();
 419              echo $OUTPUT->heading(format_string($forum->name), 2);
 420              echo $OUTPUT->confirm(get_string("deletesure", "forum", $replycount),
 421                           "post.php?delete=$delete&confirm=$delete",
 422                           $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
 423              forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false);
 424          }
 425  
 426      }
 427      echo $OUTPUT->footer();
 428      die;
 429  
 430  
 431  } else if (!empty($prune)) {  // Pruning
 432  
 433      if (!$post = forum_get_post_full($prune)) {
 434          print_error('invalidpostid', 'forum');
 435      }
 436      if (!$discussion = $DB->get_record("forum_discussions", array("id" => $post->discussion))) {
 437          print_error('notpartofdiscussion', 'forum');
 438      }
 439      if (!$forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
 440          print_error('invalidforumid', 'forum');
 441      }
 442      if ($forum->type == 'single') {
 443          print_error('cannotsplit', 'forum');
 444      }
 445      if (!$post->parent) {
 446          print_error('alreadyfirstpost', 'forum');
 447      }
 448      if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) { // For the logs
 449          print_error('invalidcoursemodule');
 450      } else {
 451          $modcontext = context_module::instance($cm->id);
 452      }
 453      if (!has_capability('mod/forum:splitdiscussions', $modcontext)) {
 454          print_error('cannotsplit', 'forum');
 455      }
 456  
 457      if (!empty($name) && confirm_sesskey()) {    // User has confirmed the prune
 458  
 459          $newdiscussion = new stdClass();
 460          $newdiscussion->course       = $discussion->course;
 461          $newdiscussion->forum        = $discussion->forum;
 462          $newdiscussion->name         = $name;
 463          $newdiscussion->firstpost    = $post->id;
 464          $newdiscussion->userid       = $discussion->userid;
 465          $newdiscussion->groupid      = $discussion->groupid;
 466          $newdiscussion->assessed     = $discussion->assessed;
 467          $newdiscussion->usermodified = $post->userid;
 468          $newdiscussion->timestart    = $discussion->timestart;
 469          $newdiscussion->timeend      = $discussion->timeend;
 470  
 471          $newid = $DB->insert_record('forum_discussions', $newdiscussion);
 472  
 473          $newpost = new stdClass();
 474          $newpost->id      = $post->id;
 475          $newpost->parent  = 0;
 476          $newpost->subject = $name;
 477  
 478          $DB->update_record("forum_posts", $newpost);
 479  
 480          forum_change_discussionid($post->id, $newid);
 481  
 482          // update last post in each discussion
 483          forum_discussion_update_last_post($discussion->id);
 484          forum_discussion_update_last_post($newid);
 485  
 486          // Fire events to reflect the split..
 487          $params = array(
 488              'context' => $modcontext,
 489              'objectid' => $discussion->id,
 490              'other' => array(
 491                  'forumid' => $forum->id,
 492              )
 493          );
 494          $event = \mod_forum\event\discussion_updated::create($params);
 495          $event->trigger();
 496  
 497          $params = array(
 498              'context' => $modcontext,
 499              'objectid' => $newid,
 500              'other' => array(
 501                  'forumid' => $forum->id,
 502              )
 503          );
 504          $event = \mod_forum\event\discussion_created::create($params);
 505          $event->trigger();
 506  
 507          $params = array(
 508              'context' => $modcontext,
 509              'objectid' => $post->id,
 510              'other' => array(
 511                  'discussionid' => $newid,
 512                  'forumid' => $forum->id,
 513                  'forumtype' => $forum->type,
 514              )
 515          );
 516          $event = \mod_forum\event\post_updated::create($params);
 517          $event->add_record_snapshot('forum_discussions', $discussion);
 518          $event->trigger();
 519  
 520          redirect(forum_go_back_to("discuss.php?d=$newid"));
 521  
 522      } else { // User just asked to prune something
 523  
 524          $course = $DB->get_record('course', array('id' => $forum->course));
 525  
 526          $PAGE->set_cm($cm);
 527          $PAGE->set_context($modcontext);
 528          $PAGE->navbar->add(format_string($post->subject, true), new moodle_url('/mod/forum/discuss.php', array('d'=>$discussion->id)));
 529          $PAGE->navbar->add(get_string("prune", "forum"));
 530          $PAGE->set_title(format_string($discussion->name).": ".format_string($post->subject));
 531          $PAGE->set_heading($course->fullname);
 532          echo $OUTPUT->header();
 533          echo $OUTPUT->heading(format_string($forum->name), 2);
 534          echo $OUTPUT->heading(get_string('pruneheading', 'forum'), 3);
 535          echo '<center>';
 536  
 537          include ('prune.html');
 538  
 539          forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false);
 540          echo '</center>';
 541      }
 542      echo $OUTPUT->footer();
 543      die;
 544  } else {
 545      print_error('unknowaction');
 546  
 547  }
 548  
 549  if (!isset($coursecontext)) {
 550      // Has not yet been set by post.php.
 551      $coursecontext = context_course::instance($forum->course);
 552  }
 553  
 554  
 555  // from now on user must be logged on properly
 556  
 557  if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
 558      print_error('invalidcoursemodule');
 559  }
 560  $modcontext = context_module::instance($cm->id);
 561  require_login($course, false, $cm);
 562  
 563  if (isguestuser()) {
 564      // just in case
 565      print_error('noguest');
 566  }
 567  
 568  if (!isset($forum->maxattachments)) {  // TODO - delete this once we add a field to the forum table
 569      $forum->maxattachments = 3;
 570  }
 571  
 572  $thresholdwarning = forum_check_throttling($forum, $cm);
 573  $mform_post = new mod_forum_post_form('post.php', array('course' => $course,
 574                                                          'cm' => $cm,
 575                                                          'coursecontext' => $coursecontext,
 576                                                          'modcontext' => $modcontext,
 577                                                          'forum' => $forum,
 578                                                          'post' => $post,
 579                                                          'subscribe' => \mod_forum\subscriptions::is_subscribed($USER->id, $forum,
 580                                                                  null, $cm),
 581                                                          'thresholdwarning' => $thresholdwarning,
 582                                                          'edit' => $edit), 'post', '', array('id' => 'mformforum'));
 583  
 584  $draftitemid = file_get_submitted_draft_itemid('attachments');
 585  file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_forum', 'attachment', empty($post->id)?null:$post->id, mod_forum_post_form::attachment_options($forum));
 586  
 587  //load data into form NOW!
 588  
 589  if ($USER->id != $post->userid) {   // Not the original author, so add a message to the end
 590      $data = new stdClass();
 591      $data->date = userdate($post->modified);
 592      if ($post->messageformat == FORMAT_HTML) {
 593          $data->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$USER->id.'&course='.$post->course.'">'.
 594                         fullname($USER).'</a>';
 595          $post->message .= '<p><span class="edited">('.get_string('editedby', 'forum', $data).')</span></p>';
 596      } else {
 597          $data->name = fullname($USER);
 598          $post->message .= "\n\n(".get_string('editedby', 'forum', $data).')';
 599      }
 600      unset($data);
 601  }
 602  
 603  $formheading = '';
 604  if (!empty($parent)) {
 605      $heading = get_string("yourreply", "forum");
 606      $formheading = get_string('reply', 'forum');
 607  } else {
 608      if ($forum->type == 'qanda') {
 609          $heading = get_string('yournewquestion', 'forum');
 610      } else {
 611          $heading = get_string('yournewtopic', 'forum');
 612      }
 613  }
 614  
 615  $postid = empty($post->id) ? null : $post->id;
 616  $draftid_editor = file_get_submitted_draft_itemid('message');
 617  $currenttext = file_prepare_draft_area($draftid_editor, $modcontext->id, 'mod_forum', 'post', $postid, mod_forum_post_form::editor_options($modcontext, $postid), $post->message);
 618  
 619  // Respect the user's discussion autosubscribe preference unless they have already posted - in which case, use that preference.
 620  $discussionsubscribe = $USER->autosubscribe;
 621  if (isset($discussion) && forum_user_has_posted($forum->id, $discussion->id, $USER->id)) {
 622      $discussionsubscribe = \mod_forum\subscriptions::is_subscribed($USER->id, $forum, $discussion->id, $cm);
 623  }
 624  $mform_post->set_data(array(        'attachments'=>$draftitemid,
 625                                      'general'=>$heading,
 626                                      'subject'=>$post->subject,
 627                                      'message'=>array(
 628                                          'text'=>$currenttext,
 629                                          'format'=>empty($post->messageformat) ? editors_get_preferred_format() : $post->messageformat,
 630                                          'itemid'=>$draftid_editor
 631                                      ),
 632                                      'discussionsubscribe' => $discussionsubscribe,
 633                                      'mailnow'=>!empty($post->mailnow),
 634                                      'userid'=>$post->userid,
 635                                      'parent'=>$post->parent,
 636                                      'discussion'=>$post->discussion,
 637                                      'course'=>$course->id) +
 638                                      $page_params +
 639  
 640                              (isset($post->format)?array(
 641                                      'format'=>$post->format):
 642                                  array())+
 643  
 644                              (isset($discussion->timestart)?array(
 645                                      'timestart'=>$discussion->timestart):
 646                                  array())+
 647  
 648                              (isset($discussion->timeend)?array(
 649                                      'timeend'=>$discussion->timeend):
 650                                  array())+
 651  
 652                              (isset($post->groupid)?array(
 653                                      'groupid'=>$post->groupid):
 654                                  array())+
 655  
 656                              (isset($discussion->id)?
 657                                      array('discussion'=>$discussion->id):
 658                                      array()));
 659  
 660  if ($mform_post->is_cancelled()) {
 661      if (!isset($discussion->id) || $forum->type === 'qanda') {
 662          // Q and A forums don't have a discussion page, so treat them like a new thread..
 663          redirect(new moodle_url('/mod/forum/view.php', array('f' => $forum->id)));
 664      } else {
 665          redirect(new moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id)));
 666      }
 667  } else if ($fromform = $mform_post->get_data()) {
 668  
 669      if (empty($SESSION->fromurl)) {
 670          $errordestination = "$CFG->wwwroot/mod/forum/view.php?f=$forum->id";
 671      } else {
 672          $errordestination = $SESSION->fromurl;
 673      }
 674  
 675      $fromform->itemid        = $fromform->message['itemid'];
 676      $fromform->messageformat = $fromform->message['format'];
 677      $fromform->message       = $fromform->message['text'];
 678      // WARNING: the $fromform->message array has been overwritten, do not use it anymore!
 679      $fromform->messagetrust  = trusttext_trusted($modcontext);
 680  
 681      $contextcheck = isset($fromform->groupinfo) && has_capability('mod/forum:movediscussions', $modcontext);
 682  
 683      if ($fromform->edit) {           // Updating a post
 684          unset($fromform->groupid);
 685          $fromform->id = $fromform->edit;
 686          $message = '';
 687  
 688          //fix for bug #4314
 689          if (!$realpost = $DB->get_record('forum_posts', array('id' => $fromform->id))) {
 690              $realpost = new stdClass();
 691              $realpost->userid = -1;
 692          }
 693  
 694  
 695          // if user has edit any post capability
 696          // or has either startnewdiscussion or reply capability and is editting own post
 697          // then he can proceed
 698          // MDL-7066
 699          if ( !(($realpost->userid == $USER->id && (has_capability('mod/forum:replypost', $modcontext)
 700                              || has_capability('mod/forum:startdiscussion', $modcontext))) ||
 701                              has_capability('mod/forum:editanypost', $modcontext)) ) {
 702              print_error('cannotupdatepost', 'forum');
 703          }
 704  
 705          // If the user has access to all groups and they are changing the group, then update the post.
 706          if ($contextcheck) {
 707              if (empty($fromform->groupinfo)) {
 708                  $fromform->groupinfo = -1;
 709              }
 710              $DB->set_field('forum_discussions' ,'groupid' , $fromform->groupinfo, array('firstpost' => $fromform->id));
 711          }
 712  
 713          $updatepost = $fromform; //realpost
 714          $updatepost->forum = $forum->id;
 715          if (!forum_update_post($updatepost, $mform_post, $message)) {
 716              print_error("couldnotupdate", "forum", $errordestination);
 717          }
 718  
 719          // MDL-11818
 720          if (($forum->type == 'single') && ($updatepost->parent == '0')){ // updating first post of single discussion type -> updating forum intro
 721              $forum->intro = $updatepost->message;
 722              $forum->timemodified = time();
 723              $DB->update_record("forum", $forum);
 724          }
 725  
 726          $timemessage = 2;
 727          if (!empty($message)) { // if we're printing stuff about the file upload
 728              $timemessage = 4;
 729          }
 730  
 731          if ($realpost->userid == $USER->id) {
 732              $message .= '<br />'.get_string("postupdated", "forum");
 733          } else {
 734              $realuser = $DB->get_record('user', array('id' => $realpost->userid));
 735              $message .= '<br />'.get_string("editedpostupdated", "forum", fullname($realuser));
 736          }
 737  
 738          if ($subscribemessage = forum_post_subscription($fromform, $forum, $discussion)) {
 739              $timemessage = 4;
 740          }
 741          if ($forum->type == 'single') {
 742              // Single discussion forums are an exception. We show
 743              // the forum itself since it only has one discussion
 744              // thread.
 745              $discussionurl = "view.php?f=$forum->id";
 746          } else {
 747              $discussionurl = "discuss.php?d=$discussion->id#p$fromform->id";
 748          }
 749  
 750          $params = array(
 751              'context' => $modcontext,
 752              'objectid' => $fromform->id,
 753              'other' => array(
 754                  'discussionid' => $discussion->id,
 755                  'forumid' => $forum->id,
 756                  'forumtype' => $forum->type,
 757              )
 758          );
 759  
 760          if ($realpost->userid !== $USER->id) {
 761              $params['relateduserid'] = $realpost->userid;
 762          }
 763  
 764          $event = \mod_forum\event\post_updated::create($params);
 765          $event->add_record_snapshot('forum_discussions', $discussion);
 766          $event->trigger();
 767  
 768          redirect(forum_go_back_to("$discussionurl"), $message.$subscribemessage, $timemessage);
 769  
 770          exit;
 771  
 772  
 773      } else if ($fromform->discussion) { // Adding a new post to an existing discussion
 774          // Before we add this we must check that the user will not exceed the blocking threshold.
 775          forum_check_blocking_threshold($thresholdwarning);
 776  
 777          unset($fromform->groupid);
 778          $message = '';
 779          $addpost = $fromform;
 780          $addpost->forum=$forum->id;
 781          if ($fromform->id = forum_add_new_post($addpost, $mform_post, $message)) {
 782              $timemessage = 2;
 783              if (!empty($message)) { // if we're printing stuff about the file upload
 784                  $timemessage = 4;
 785              }
 786  
 787              if ($subscribemessage = forum_post_subscription($fromform, $forum, $discussion)) {
 788                  $timemessage = 4;
 789              }
 790  
 791              if (!empty($fromform->mailnow)) {
 792                  $message .= get_string("postmailnow", "forum");
 793                  $timemessage = 4;
 794              } else {
 795                  $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
 796                  $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
 797              }
 798  
 799              if ($forum->type == 'single') {
 800                  // Single discussion forums are an exception. We show
 801                  // the forum itself since it only has one discussion
 802                  // thread.
 803                  $discussionurl = "view.php?f=$forum->id";
 804              } else {
 805                  $discussionurl = "discuss.php?d=$discussion->id";
 806              }
 807  
 808              $params = array(
 809                  'context' => $modcontext,
 810                  'objectid' => $fromform->id,
 811                  'other' => array(
 812                      'discussionid' => $discussion->id,
 813                      'forumid' => $forum->id,
 814                      'forumtype' => $forum->type,
 815                  )
 816              );
 817              $event = \mod_forum\event\post_created::create($params);
 818              $event->add_record_snapshot('forum_posts', $fromform);
 819              $event->add_record_snapshot('forum_discussions', $discussion);
 820              $event->trigger();
 821  
 822              // Update completion state
 823              $completion=new completion_info($course);
 824              if($completion->is_enabled($cm) &&
 825                  ($forum->completionreplies || $forum->completionposts)) {
 826                  $completion->update_state($cm,COMPLETION_COMPLETE);
 827              }
 828  
 829              redirect(forum_go_back_to("$discussionurl#p$fromform->id"), $message.$subscribemessage, $timemessage);
 830  
 831          } else {
 832              print_error("couldnotadd", "forum", $errordestination);
 833          }
 834          exit;
 835  
 836      } else { // Adding a new discussion.
 837          // Before we add this we must check that the user will not exceed the blocking threshold.
 838          forum_check_blocking_threshold($thresholdwarning);
 839  
 840          if (!forum_user_can_post_discussion($forum, $fromform->groupid, -1, $cm, $modcontext)) {
 841              print_error('cannotcreatediscussion', 'forum');
 842          }
 843          // If the user has access all groups capability let them choose the group.
 844          if ($contextcheck) {
 845              $fromform->groupid = $fromform->groupinfo;
 846          }
 847          if (empty($fromform->groupid)) {
 848              $fromform->groupid = -1;
 849          }
 850  
 851          $fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;
 852  
 853          $discussion = $fromform;
 854          $discussion->name    = $fromform->subject;
 855  
 856          $newstopic = false;
 857          if ($forum->type == 'news' && !$fromform->parent) {
 858              $newstopic = true;
 859          }
 860          $discussion->timestart = $fromform->timestart;
 861          $discussion->timeend = $fromform->timeend;
 862  
 863          $message = '';
 864          if ($discussion->id = forum_add_discussion($discussion, $mform_post, $message)) {
 865  
 866              $params = array(
 867                  'context' => $modcontext,
 868                  'objectid' => $discussion->id,
 869                  'other' => array(
 870                      'forumid' => $forum->id,
 871                  )
 872              );
 873              $event = \mod_forum\event\discussion_created::create($params);
 874              $event->add_record_snapshot('forum_discussions', $discussion);
 875              $event->trigger();
 876  
 877              $timemessage = 2;
 878              if (!empty($message)) { // if we're printing stuff about the file upload
 879                  $timemessage = 4;
 880              }
 881  
 882              if ($fromform->mailnow) {
 883                  $message .= get_string("postmailnow", "forum");
 884                  $timemessage = 4;
 885              } else {
 886                  $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
 887                  $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
 888              }
 889  
 890              if ($subscribemessage = forum_post_subscription($fromform, $forum, $discussion)) {
 891                  $timemessage = 6;
 892              }
 893  
 894              // Update completion status
 895              $completion=new completion_info($course);
 896              if($completion->is_enabled($cm) &&
 897                  ($forum->completiondiscussions || $forum->completionposts)) {
 898                  $completion->update_state($cm,COMPLETION_COMPLETE);
 899              }
 900  
 901              redirect(forum_go_back_to("view.php?f=$fromform->forum"), $message.$subscribemessage, $timemessage);
 902  
 903          } else {
 904              print_error("couldnotadd", "forum", $errordestination);
 905          }
 906  
 907          exit;
 908      }
 909  }
 910  
 911  
 912  
 913  // To get here they need to edit a post, and the $post
 914  // variable will be loaded with all the particulars,
 915  // so bring up the form.
 916  
 917  // $course, $forum are defined.  $discussion is for edit and reply only.
 918  
 919  if ($post->discussion) {
 920      if (! $toppost = $DB->get_record("forum_posts", array("discussion" => $post->discussion, "parent" => 0))) {
 921          print_error('cannotfindparentpost', 'forum', '', $post->id);
 922      }
 923  } else {
 924      $toppost = new stdClass();
 925      $toppost->subject = ($forum->type == "news") ? get_string("addanewtopic", "forum") :
 926                                                     get_string("addanewdiscussion", "forum");
 927  }
 928  
 929  if (empty($post->edit)) {
 930      $post->edit = '';
 931  }
 932  
 933  if (empty($discussion->name)) {
 934      if (empty($discussion)) {
 935          $discussion = new stdClass();
 936      }
 937      $discussion->name = $forum->name;
 938  }
 939  if ($forum->type == 'single') {
 940      // There is only one discussion thread for this forum type. We should
 941      // not show the discussion name (same as forum name in this case) in
 942      // the breadcrumbs.
 943      $strdiscussionname = '';
 944  } else {
 945      // Show the discussion name in the breadcrumbs.
 946      $strdiscussionname = format_string($discussion->name).':';
 947  }
 948  
 949  $forcefocus = empty($reply) ? NULL : 'message';
 950  
 951  if (!empty($discussion->id)) {
 952      $PAGE->navbar->add(format_string($toppost->subject, true), "discuss.php?d=$discussion->id");
 953  }
 954  
 955  if ($post->parent) {
 956      $PAGE->navbar->add(get_string('reply', 'forum'));
 957  }
 958  
 959  if ($edit) {
 960      $PAGE->navbar->add(get_string('edit', 'forum'));
 961  }
 962  
 963  $PAGE->set_title("$course->shortname: $strdiscussionname ".format_string($toppost->subject));
 964  $PAGE->set_heading($course->fullname);
 965  
 966  echo $OUTPUT->header();
 967  echo $OUTPUT->heading(format_string($forum->name), 2);
 968  
 969  // checkup
 970  if (!empty($parent) && !forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
 971      print_error('cannotreply', 'forum');
 972  }
 973  if (empty($parent) && empty($edit) && !forum_user_can_post_discussion($forum, $groupid, -1, $cm, $modcontext)) {
 974      print_error('cannotcreatediscussion', 'forum');
 975  }
 976  
 977  if ($forum->type == 'qanda'
 978              && !has_capability('mod/forum:viewqandawithoutposting', $modcontext)
 979              && !empty($discussion->id)
 980              && !forum_user_has_posted($forum->id, $discussion->id, $USER->id)) {
 981      echo $OUTPUT->notification(get_string('qandanotify','forum'));
 982  }
 983  
 984  // If there is a warning message and we are not editing a post we need to handle the warning.
 985  if (!empty($thresholdwarning) && !$edit) {
 986      // Here we want to throw an exception if they are no longer allowed to post.
 987      forum_check_blocking_threshold($thresholdwarning);
 988  }
 989  
 990  if (!empty($parent)) {
 991      if (!$discussion = $DB->get_record('forum_discussions', array('id' => $parent->discussion))) {
 992          print_error('notpartofdiscussion', 'forum');
 993      }
 994  
 995      forum_print_post($parent, $discussion, $forum, $cm, $course, false, false, false);
 996      if (empty($post->edit)) {
 997          if ($forum->type != 'qanda' || forum_user_can_see_discussion($forum, $discussion, $modcontext)) {
 998              $forumtracked = forum_tp_is_tracked($forum);
 999              $posts = forum_get_all_discussion_posts($discussion->id, "created ASC", $forumtracked);
1000              forum_print_posts_threaded($course, $cm, $forum, $discussion, $parent, 0, false, $forumtracked, $posts);
1001          }
1002      }
1003  } else {
1004      if (!empty($forum->intro)) {
1005          echo $OUTPUT->box(format_module_intro('forum', $forum, $cm->id), 'generalbox', 'intro');
1006  
1007          if (!empty($CFG->enableplagiarism)) {
1008              require_once($CFG->libdir.'/plagiarismlib.php');
1009              echo plagiarism_print_disclosure($cm->id);
1010          }
1011      }
1012  }
1013  
1014  if (!empty($formheading)) {
1015      echo $OUTPUT->heading($formheading, 2, array('class' => 'accesshide'));
1016  }
1017  $mform_post->display();
1018  
1019  echo $OUTPUT->footer();
1020  


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