/**
  * Render the form to write a timeline comment or a reply to a comment
  * 
  * @param string $actionlink the HTML output, where this HTML will be added
  * @param course_context $coursecontext
  * @param object $post
  * @param object $replycomment, if not null we render a form to write a reply to a comment.
  * @return type
  */
 protected function render_timeline_comments_form($actionlink, $coursecontext, $post, $replycomment = null)
 {
     $actionarea = '';
     $capcomment = has_capability('format/socialwall:writecomment', $coursecontext);
     // Replies are not in threaded fashion, is this a toplevel comment?
     $replycommentid = !isset($replycomment) ? 0 : $replycomment->id;
     $canreply = ($replycommentid == 0 or $replycomment->replycommentid == 0);
     if ($capcomment and $canreply) {
         $style = empty($post->locked) ? '' : 'display:none';
         $urlparams = array('courseid' => $post->courseid, 'postid' => $post->id, 'commentsformshow' => $post->id . '_' . $replycommentid);
         $url = new moodle_url('/course/view.php', $urlparams);
         $urltext = empty($replycommentid) ? get_string('writecomment', 'format_socialwall') : get_string('replycomment', 'format_socialwall');
         $actionlink .= html_writer::link($url, $urltext, array('id' => "showcommentform_{$post->id}_{$replycommentid}", 'style' => $style));
         $actionarea .= html_writer::tag('div', $actionlink, array('class' => 'tl-actionlink'));
     }
     if ($capcomment and $canreply) {
         $formparams = array('postid' => $post->id, 'courseid' => $post->courseid, 'replycommentid' => $replycommentid);
         $url = new moodle_url('/course/format/socialwall/action.php', $formparams);
         $commentform = new comment_form($url, $formparams, 'post', '', array('id' => 'tlcommentform_' . $post->id . '_' . $replycommentid));
         if (!$commentform->has_errors() and $this->commentsformshow != $post->id . '_' . $replycommentid) {
             $style = 'display:none';
         }
         $params = array('class' => 'tl-commentform', 'id' => "tlcommentformwrap_{$post->id}_{$replycommentid}", 'style' => $style);
         $actionarea .= html_writer::tag('div', $commentform->render(), $params);
     }
     return $actionarea;
 }
 /** renders a timeline post.
  * 
  * @global object $OUTPUT
  * @global record $USER
  * @param record $course
  * @param record $post
  * @param object $completion
  * @param array $authors the already retrieved authors for posts and comments
  * @return string HTML for post
  */
 protected function render_timeline_post($course, $post, $completion, $authors)
 {
     global $OUTPUT, $USER;
     $coursecontext = context_course::instance($post->courseid);
     list($postauthor, $l) = $this->get_timeline_author($post->fromuserid, $authors);
     $o = html_writer::tag('div', $l, array('class' => 'tl-leftcol'));
     // ... determine group for headline.
     if (empty($post->togroupid)) {
         $to = get_string('allparticipants');
     } else {
         $groups = groups_get_all_groups($post->courseid);
         $to = isset($groups[$post->togroupid]) ? get_string('group') . ' ' . $groups[$post->togroupid]->name : get_string('nonexistinggroup', 'format_socialwall');
     }
     $date = userdate($post->timecreated);
     $authorspan = html_writer::tag('span', fullname($postauthor), array('class' => 'tl-authorname'));
     $headline = get_string('postedonto', 'format_socialwall', array('author' => $authorspan, 'date' => $date, 'to' => $to));
     // ... add delete icon, if delete is possible.
     $candeletepost = ($post->fromuserid == $USER->id and has_capability('format/socialwall:deleteownpost', $coursecontext));
     $candeletepost = ($candeletepost or has_capability('format/socialwall:deleteanypost', $coursecontext));
     if ($candeletepost) {
         $url = new moodle_url('/course/format/socialwall/action.php', array('id' => $post->courseid, 'action' => 'deletepost', 'pid' => $post->id, 'sesskey' => sesskey()));
         $deletelink = html_writer::link($url, $OUTPUT->pix_icon('t/delete', get_string('delete')));
         $headline .= html_writer::tag('span', $deletelink, array('class' => 'tl-action-icons'));
     }
     // ...
     if (!empty($post->posttext)) {
         // ...user with cap format/socialwall:posthtml should be able to html.
         $headline .= html_writer::tag('div', format_text($post->posttext), array('class' => 'tl-posttext'));
     }
     if (!empty($post->attaches)) {
         $modinfo = get_fast_modinfo($post->courseid);
         $modulehtml = '';
         foreach ($post->attaches as $attachment) {
             $cm = $modinfo->get_cm($attachment->coursemoduleid);
             $modulehtml .= $this->courserenderer->course_section_cm_list_item($course, $completion, $cm, 0);
             if (isset($post->grades[$attachment->coursemoduleid])) {
                 $modulehtml .= $this->render_timeline_grades($authors, $post->grades[$attachment->coursemoduleid]);
             }
         }
         if (!empty($modulehtml)) {
             $headline .= html_writer::tag('ul', $modulehtml, array('class' => 'section tl-postattachment'));
         }
     }
     $p = '';
     if (has_capability('format/socialwall:lockcomment', $coursecontext)) {
         $class = !empty($post->locked) ? 'locked' : 'unlocked';
         $urlparams = array('id' => $post->courseid, 'postid' => $post->id, 'action' => 'lockpost', 'locked' => empty($post->locked), 'sesskey' => sesskey());
         $url = new moodle_url('/course/format/socialwall/action.php', $urlparams);
         if (!empty($post->locked)) {
             $pixicon = $OUTPUT->pix_icon('lockedpost', get_string('unlockpost', 'format_socialwall'), 'format_socialwall');
         } else {
             $pixicon = $OUTPUT->pix_icon('unlockedpost', get_string('lockpost', 'format_socialwall'), 'format_socialwall');
         }
         $link = html_writer::link($url, $pixicon, array('id' => 'lockpost_' . $post->id, 'class' => $class));
         $p .= html_writer::tag('div', $link, array('class' => 'tl-locked'));
     }
     if (has_capability('format/socialwall:makesticky', $coursecontext)) {
         $urlparams = array('id' => $post->courseid, 'postid' => $post->id, 'action' => 'makesticky', 'sticky' => empty($post->sticky), 'sesskey' => sesskey());
         $url = new moodle_url('/course/format/socialwall/action.php', $urlparams);
         if (!empty($post->sticky)) {
             $pixicon = $OUTPUT->pix_icon('stickypost', get_string('makeunsticky', 'format_socialwall'), 'format_socialwall');
         } else {
             $pixicon = $OUTPUT->pix_icon('unstickypost', get_string('makesticky', 'format_socialwall'), 'format_socialwall');
         }
         $link = html_writer::link($url, $pixicon);
         $p .= html_writer::tag('div', $link, array('class' => 'tl-sticky'));
     } else {
         // ...cannot edit stickyness of post.
         if (!empty($post->sticky)) {
             $pixicon = $OUTPUT->pix_icon('stickypost', get_string('sticky', 'format_socialwall'), 'format_socialwall');
             $p .= html_writer::tag('div', $pixicon, array('class' => 'tl-sticky'));
         }
     }
     $p .= html_writer::tag('div', $headline);
     $countoutput = '';
     if (!empty($course->enablelikes) and has_capability('format/socialwall:viewlikes', $coursecontext)) {
         $countlikessstr = get_string('countlikes', 'format_socialwall', $post->countlikes);
         $countoutput .= html_writer::tag('span', $countlikessstr, array('id' => 'tlcountlikes_' . $post->id));
     }
     $countcommentsstr = get_string('countcomments', 'format_socialwall', $post->countcomments);
     $countoutput .= html_writer::tag('span', $countcommentsstr, array('id' => 'tlcountcomments_' . $post->id));
     $actionarea = html_writer::tag('div', $countoutput, array('class' => 'tl-counts'));
     $actionlink = '';
     if (!empty($course->enablelikes) and has_capability('format/socialwall:like', $coursecontext)) {
         $class = !empty($post->userlike) ? 'likenomore' : 'like';
         $urlparams = array('id' => $post->courseid, 'postid' => $post->id, 'action' => 'likepost', 'userlike' => empty($post->userlike), 'sesskey' => sesskey());
         $url = new moodle_url('/course/format/socialwall/action.php', $urlparams);
         $urlparams = array('class' => $class, 'id' => "userlike_{$post->id}");
         $actionlink .= html_writer::link($url, get_string($class, 'format_socialwall'), $urlparams);
     }
     $capcomment = has_capability('format/socialwall:writecomment', $coursecontext);
     if ($capcomment) {
         $style = empty($post->locked) ? '' : 'display:none';
         $urlparams = array('id' => $post->courseid, 'postid' => $post->id, 'commentsformshow' => $post->id);
         $url = new moodle_url('/course/view.php', $urlparams);
         $urltext = get_string('writecomment', 'format_socialwall');
         $actionlink .= html_writer::link($url, $urltext, array('id' => "showcommentform_{$post->id}", 'style' => $style));
     }
     $actionarea .= html_writer::tag('div', $actionlink, array('class' => 'tl-actionlink'));
     if ($capcomment) {
         $url = new moodle_url('/course/format/socialwall/action.php');
         $formparams = array('postid' => $post->id, 'id' => $post->courseid);
         $commentform = new comment_form($url, $formparams, 'post', '', array('id' => 'tlcommentform_' . $post->id));
         if (!$commentform->has_errors() and $this->commentsformshow != $post->id) {
             $style = 'display:none';
         }
         $params = array('class' => 'tl-commentform', 'id' => "tlcommentformwrap_{$post->id}", 'style' => $style);
         $actionarea .= html_writer::tag('div', $commentform->render(), $params);
     }
     $p .= html_writer::tag('div', $actionarea, array('class' => 'tl-post-actionarea'));
     // ... print out all comments.
     $c = $this->render_timeline_comments($post, $authors, $coursecontext);
     $p .= html_writer::tag('ul', $c, array('class' => 'tl-comments', 'id' => 'tlcomments_' . $post->id));
     $morecommentscount = $post->countcomments - $course->tlnumcomments;
     if ($morecommentscount > 0) {
         $url = new moodle_url('/course/format/socialwall/action.php');
         $strmore = get_string('showall', 'format_socialwall', $morecommentscount);
         $l = html_writer::link('#', $strmore, array('id' => 'tlshowall_' . $post->id));
         $p .= html_writer::tag('div', $l, array('class' => 'tl-showall'));
     }
     $o .= html_writer::tag('div', $p, array('class' => 'tl-text'));
     $text = html_writer::tag('li', $o, array('class' => 'tl-post'));
     return filter_manager::instance()->filter_text($text, $coursecontext);
 }