[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 * Forum post renderable. 19 * 20 * @package mod_forum 21 * @copyright 2015 Andrew Nicols <andrew@nicols.co.uk> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace mod_forum\output; 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 /** 30 * Forum post renderable. 31 * 32 * @copyright 2015 Andrew Nicols <andrew@nicols.co.uk> 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 * 35 * @property boolean $viewfullnames Whether to override fullname() 36 */ 37 class forum_post implements \renderable, \templatable { 38 39 /** 40 * The course that the forum post is in. 41 * 42 * @var object $course 43 */ 44 protected $course = null; 45 46 /** 47 * The course module for the forum. 48 * 49 * @var object $cm 50 */ 51 protected $cm = null; 52 53 /** 54 * The forum that the post is in. 55 * 56 * @var object $forum 57 */ 58 protected $forum = null; 59 60 /** 61 * The discussion that the forum post is in. 62 * 63 * @var object $discussion 64 */ 65 protected $discussion = null; 66 67 /** 68 * The forum post being displayed. 69 * 70 * @var object $post 71 */ 72 protected $post = null; 73 74 /** 75 * Whether the user can reply to this post. 76 * 77 * @var boolean $canreply 78 */ 79 protected $canreply = false; 80 81 /** 82 * Whether to override forum display when displaying usernames. 83 * @var boolean $viewfullnames 84 */ 85 protected $viewfullnames = false; 86 87 /** 88 * The user that is reading the post. 89 * 90 * @var object $userto 91 */ 92 protected $userto = null; 93 94 /** 95 * The user that wrote the post. 96 * 97 * @var object $author 98 */ 99 protected $author = null; 100 101 /** 102 * An associative array indicating which keys on this object should be writeable. 103 * 104 * @var array $writablekeys 105 */ 106 protected $writablekeys = array( 107 'viewfullnames' => true, 108 ); 109 110 /** 111 * Builds a renderable forum post 112 * 113 * @param object $course Course of the forum 114 * @param object $cm Course Module of the forum 115 * @param object $forum The forum of the post 116 * @param object $discussion Discussion thread in which the post appears 117 * @param object $post The post 118 * @param object $author Author of the post 119 * @param object $recipient Recipient of the email 120 * @param bool $canreply True if the user can reply to the post 121 */ 122 public function __construct($course, $cm, $forum, $discussion, $post, $author, $recipient, $canreply) { 123 $this->course = $course; 124 $this->cm = $cm; 125 $this->forum = $forum; 126 $this->discussion = $discussion; 127 $this->post = $post; 128 $this->author = $author; 129 $this->userto = $recipient; 130 $this->canreply = $canreply; 131 } 132 133 /** 134 * Export this data so it can be used as the context for a mustache template. 135 * 136 * @param \mod_forum_renderer $renderer The render to be used for formatting the message and attachments 137 * @param bool $plaintext Whethe the target is a plaintext target 138 * @return stdClass Data ready for use in a mustache template 139 */ 140 public function export_for_template(\renderer_base $renderer, $plaintext = false) { 141 if ($plaintext) { 142 return $this->export_for_template_text($renderer); 143 } else { 144 return $this->export_for_template_html($renderer); 145 } 146 } 147 148 /** 149 * Export this data so it can be used as the context for a mustache template. 150 * 151 * @param \mod_forum_renderer $renderer The render to be used for formatting the message and attachments 152 * @return stdClass Data ready for use in a mustache template 153 */ 154 protected function export_for_template_text(\mod_forum_renderer $renderer) { 155 return array( 156 'id' => html_entity_decode($this->post->id), 157 'coursename' => html_entity_decode($this->get_coursename()), 158 'courselink' => html_entity_decode($this->get_courselink()), 159 'forumname' => html_entity_decode($this->get_forumname()), 160 'showdiscussionname' => html_entity_decode($this->get_showdiscussionname()), 161 'discussionname' => html_entity_decode($this->get_discussionname()), 162 'subject' => html_entity_decode($this->get_subject()), 163 'authorfullname' => html_entity_decode($this->get_author_fullname()), 164 'postdate' => html_entity_decode($this->get_postdate()), 165 166 // Format some components according to the renderer. 167 'message' => html_entity_decode($renderer->format_message_text($this->cm, $this->post)), 168 'attachments' => html_entity_decode($renderer->format_message_attachments($this->cm, $this->post)), 169 170 'canreply' => $this->canreply, 171 'permalink' => $this->get_permalink(), 172 'firstpost' => $this->get_is_firstpost(), 173 'replylink' => $this->get_replylink(), 174 'unsubscribediscussionlink' => $this->get_unsubscribediscussionlink(), 175 'unsubscribeforumlink' => $this->get_unsubscribeforumlink(), 176 'parentpostlink' => $this->get_parentpostlink(), 177 178 'forumindexlink' => $this->get_forumindexlink(), 179 'forumviewlink' => $this->get_forumviewlink(), 180 'discussionlink' => $this->get_discussionlink(), 181 182 'authorlink' => $this->get_authorlink(), 183 'authorpicture' => $this->get_author_picture(), 184 185 'grouppicture' => $this->get_group_picture(), 186 ); 187 } 188 189 /** 190 * Export this data so it can be used as the context for a mustache template. 191 * 192 * @param \mod_forum_renderer $renderer The render to be used for formatting the message and attachments 193 * @return stdClass Data ready for use in a mustache template 194 */ 195 protected function export_for_template_html(\mod_forum_renderer $renderer) { 196 return array( 197 'id' => $this->post->id, 198 'coursename' => $this->get_coursename(), 199 'courselink' => $this->get_courselink(), 200 'forumname' => $this->get_forumname(), 201 'showdiscussionname' => $this->get_showdiscussionname(), 202 'discussionname' => $this->get_discussionname(), 203 'subject' => $this->get_subject(), 204 'authorfullname' => $this->get_author_fullname(), 205 'postdate' => $this->get_postdate(), 206 207 // Format some components according to the renderer. 208 'message' => $renderer->format_message_text($this->cm, $this->post), 209 'attachments' => $renderer->format_message_attachments($this->cm, $this->post), 210 211 'canreply' => $this->canreply, 212 'permalink' => $this->get_permalink(), 213 'firstpost' => $this->get_is_firstpost(), 214 'replylink' => $this->get_replylink(), 215 'unsubscribediscussionlink' => $this->get_unsubscribediscussionlink(), 216 'unsubscribeforumlink' => $this->get_unsubscribeforumlink(), 217 'parentpostlink' => $this->get_parentpostlink(), 218 219 'forumindexlink' => $this->get_forumindexlink(), 220 'forumviewlink' => $this->get_forumviewlink(), 221 'discussionlink' => $this->get_discussionlink(), 222 223 'authorlink' => $this->get_authorlink(), 224 'authorpicture' => $this->get_author_picture(), 225 226 'grouppicture' => $this->get_group_picture(), 227 ); 228 } 229 230 /** 231 * Magically sets a property against this object. 232 * 233 * @param string $key 234 * @param mixed $value 235 */ 236 public function __set($key, $value) { 237 // First attempt to use the setter function. 238 $methodname = 'set_' . $key; 239 if (method_exists($this, $methodname)) { 240 return $this->{$methodname}($value); 241 } 242 243 // Fall back to the writable keys list. 244 if (isset($this->writablekeys[$key]) && $this->writablekeys[$key]) { 245 return $this->{$key} = $value; 246 } 247 248 // Throw an error rather than fail silently. 249 throw new \coding_exception('Tried to set unknown property "' . $key . '"'); 250 } 251 252 /** 253 * Whether this is the first post. 254 * 255 * @return boolean 256 */ 257 public function get_is_firstpost() { 258 return empty($this->post->parent); 259 } 260 261 /** 262 * Get the link to the course. 263 * 264 * @return string 265 */ 266 public function get_courselink() { 267 $link = new \moodle_url( 268 // Posts are viewed on the topic. 269 '/course/view.php', array( 270 'id' => $this->course->id, 271 ) 272 ); 273 274 return $link->out(false); 275 } 276 277 /** 278 * Get the link to the forum index for this course. 279 * 280 * @return string 281 */ 282 public function get_forumindexlink() { 283 $link = new \moodle_url( 284 // Posts are viewed on the topic. 285 '/mod/forum/index.php', array( 286 'id' => $this->course->id, 287 ) 288 ); 289 290 return $link->out(false); 291 } 292 293 /** 294 * Get the link to the view page for this forum. 295 * 296 * @return string 297 */ 298 public function get_forumviewlink() { 299 $link = new \moodle_url( 300 // Posts are viewed on the topic. 301 '/mod/forum/view.php', array( 302 'f' => $this->forum->id, 303 ) 304 ); 305 306 return $link->out(false); 307 } 308 309 /** 310 * Get the link to the current discussion. 311 * 312 * @return string 313 */ 314 protected function _get_discussionlink() { 315 return new \moodle_url( 316 // Posts are viewed on the topic. 317 '/mod/forum/discuss.php', array( 318 // Within a discussion. 319 'd' => $this->discussion->id, 320 ) 321 ); 322 } 323 324 /** 325 * Get the link to the current discussion. 326 * 327 * @return string 328 */ 329 public function get_discussionlink() { 330 $link = $this->_get_discussionlink(); 331 332 return $link->out(false); 333 } 334 335 /** 336 * Get the link to the current post, including post anchor. 337 * 338 * @return string 339 */ 340 public function get_permalink() { 341 $link = $this->_get_discussionlink(); 342 $link->set_anchor($this->get_postanchor()); 343 344 return $link->out(false); 345 } 346 347 /** 348 * Get the link to the parent post. 349 * 350 * @return string 351 */ 352 public function get_parentpostlink() { 353 $link = $this->_get_discussionlink(); 354 $link->param('parent', $this->post->parent); 355 356 return $link->out(false); 357 } 358 359 /** 360 * Get the link to the author's profile page. 361 * 362 * @return string 363 */ 364 public function get_authorlink() { 365 $link = new \moodle_url( 366 '/user/view.php', array( 367 'id' => $this->post->userid, 368 'course' => $this->course->id, 369 ) 370 ); 371 372 return $link->out(false); 373 } 374 375 /** 376 * Get the link to unsubscribe from the forum. 377 * 378 * @return string 379 */ 380 public function get_unsubscribeforumlink() { 381 $link = new \moodle_url( 382 '/mod/forum/subscribe.php', array( 383 'id' => $this->forum->id, 384 ) 385 ); 386 387 return $link->out(false); 388 } 389 390 /** 391 * Get the link to unsubscribe from the discussion. 392 * 393 * @return string 394 */ 395 public function get_unsubscribediscussionlink() { 396 $link = new \moodle_url( 397 '/mod/forum/subscribe.php', array( 398 'id' => $this->forum->id, 399 'd' => $this->discussion->id, 400 ) 401 ); 402 403 return $link->out(false); 404 } 405 406 /** 407 * Get the link to reply to the current post. 408 * 409 * @return string 410 */ 411 public function get_replylink() { 412 return new \moodle_url( 413 '/mod/forum/post.php', array( 414 'reply' => $this->post->id, 415 ) 416 ); 417 } 418 419 /** 420 * The formatted subject for the current post. 421 * 422 * @return string 423 */ 424 public function get_subject() { 425 return format_string($this->post->subject, true); 426 } 427 428 /** 429 * The plaintext anchor id for the current post. 430 * 431 * @return string 432 */ 433 public function get_postanchor() { 434 return 'p' . $this->post->id; 435 } 436 437 /** 438 * ID number of the course that the forum is in. 439 * 440 * @return string 441 */ 442 public function get_courseidnumber() { 443 return s($this->course->idnumber); 444 } 445 446 /** 447 * The full name of the course that the forum is in. 448 * 449 * @return string 450 */ 451 public function get_coursefullname() { 452 return format_string($this->course->fullname, true, array( 453 'context' => \context_course::instance($this->course->id), 454 )); 455 } 456 457 /** 458 * The name of the course that the forum is in. 459 * 460 * @return string 461 */ 462 public function get_coursename() { 463 return format_string($this->course->shortname, true, array( 464 'context' => \context_course::instance($this->course->id), 465 )); 466 } 467 468 /** 469 * The name of the forum. 470 * 471 * @return string 472 */ 473 public function get_forumname() { 474 return format_string($this->forum->name, true); 475 } 476 477 /** 478 * The name of the current discussion. 479 * 480 * @return string 481 */ 482 public function get_discussionname() { 483 return format_string($this->discussion->name, true); 484 } 485 486 /** 487 * Whether to show the discussion name. 488 * If the forum name matches the discussion name, the discussion name 489 * is not typically displayed. 490 * 491 * @return boolean 492 */ 493 public function get_showdiscussionname() { 494 return ($this->forum->name !== $this->discussion->name); 495 } 496 497 /** 498 * The fullname of the post author. 499 * 500 * @return string 501 */ 502 public function get_author_fullname() { 503 return fullname($this->author, $this->viewfullnames); 504 } 505 506 /** 507 * The recipient of the post. 508 * 509 * @return string 510 */ 511 protected function get_postto() { 512 global $USER; 513 if (null === $this->userto) { 514 return $USER; 515 } 516 517 return $this->userto; 518 } 519 520 /** 521 * The date of the post, formatted according to the postto user's 522 * preferences. 523 * 524 * @return string. 525 */ 526 public function get_postdate() { 527 global $CFG; 528 529 $postmodified = $this->post->modified; 530 if (!empty($CFG->forum_enabletimedposts) && ($this->discussion->timestart > $postmodified)) { 531 $postmodified = $this->discussion->timestart; 532 } 533 534 return userdate($postmodified, "", \core_date::get_user_timezone($this->get_postto())); 535 } 536 537 /** 538 * The HTML for the author's user picture. 539 * 540 * @return string 541 */ 542 public function get_author_picture() { 543 global $OUTPUT; 544 545 return $OUTPUT->user_picture($this->author, array('courseid' => $this->course->id)); 546 } 547 548 /** 549 * The HTML for a group picture. 550 * 551 * @return string 552 */ 553 public function get_group_picture() { 554 if (isset($this->userfrom->groups)) { 555 $groups = $this->userfrom->groups[$this->forum->id]; 556 } else { 557 $groups = groups_get_all_groups($this->course->id, $this->author->id, $this->cm->groupingid); 558 } 559 560 if ($this->get_is_firstpost()) { 561 return print_group_picture($groups, $this->course->id, false, true, true); 562 } 563 } 564 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |