/** * Toggle Post Flags */ public function flag_action() { global $DB; require_sesskey(); $postid = required_param('postid', PARAM_INT); $flag = required_param('flag', PARAM_ALPHA); $returnurl = required_param('returnurl', PARAM_LOCALURL); $flags = $DB->get_field('hsuforum_posts', 'flags', array('id' => $postid), MUST_EXIST); $flaglib = new \hsuforum_lib_flag(); $newflags = $flaglib->toggle_flag($flags, $flag); if ($newflags != $flags) { $updateok = $DB->set_field('hsuforum_posts', 'flags', $newflags, array('id' => $postid)); if (AJAX_SCRIPT && !$updateok) { http_response_code(500); } } if (!AJAX_SCRIPT) { redirect(new \moodle_url($returnurl)); } }
/** * @param $userid * @param $cm * @param null|stdClass $showonlypreference * * @return string * @author Mark Nielsen */ public function user_posts_overview($userid, $cm, $showonlypreference = null) { global $PAGE; require_once __DIR__ . '/lib/flag.php'; $config = get_config('hsuforum'); $forum = hsuforum_get_cm_forum($cm); $showonlypreferencebutton = ''; if (!empty($showonlypreference) and !empty($showonlypreference->button) and !$forum->anonymous) { $showonlypreferencebutton = $showonlypreference->button; } $output = ''; $postcount = $discussioncount = $flagcount = 0; $flaglib = new hsuforum_lib_flag(); if ($posts = hsuforum_get_user_posts($forum->id, $userid, context_module::instance($cm->id))) { $discussions = hsuforum_get_user_involved_discussions($forum->id, $userid); if (!empty($showonlypreference) and !empty($showonlypreference->preference)) { foreach ($discussions as $discussion) { if ($discussion->userid == $userid and array_key_exists($discussion->firstpost, $posts)) { $discussionpost = $posts[$discussion->firstpost]; $discussioncount++; if ($flaglib->is_flagged($discussionpost->flags, 'substantive')) { $flagcount++; } } else { if (!($discussionpost = hsuforum_get_post_full($discussion->firstpost))) { continue; } } if (!$forum->anonymous) { $output .= $this->post($cm, $discussion, $discussionpost, false, null, false); $output .= html_writer::start_tag('div', array('class' => 'indent')); } foreach ($posts as $post) { if ($post->discussion == $discussion->id and !empty($post->parent)) { $postcount++; if ($flaglib->is_flagged($post->flags, 'substantive')) { $flagcount++; } $command = html_writer::link(new moodle_url('/mod/hsuforum/discuss.php', array('d' => $discussion->id), 'p' . $post->id), get_string('postincontext', 'hsuforum'), array('target' => '_blank')); if (!$forum->anonymous) { $output .= $this->post($cm, $discussion, $post, false, null, false); } } } if (!$forum->anonymous) { $output .= html_writer::end_tag('div'); } } } else { foreach ($posts as $post) { if (!empty($post->parent)) { $postcount++; } else { $discussioncount++; } if ($flaglib->is_flagged($post->flags, 'substantive')) { $flagcount++; } if (!$forum->anonymous) { $command = html_writer::link(new moodle_url('/mod/hsuforum/discuss.php', array('d' => $post->discussion), 'p' . $post->id), get_string('postincontext', 'hsuforum'), array('target' => '_blank')); $output .= $this->post($cm, $discussions[$post->discussion], $post, false, null, false); } } } } if (!empty($postcount) or !empty($discussioncount)) { if ($forum->anonymous) { $output = html_writer::tag('h3', get_string('thisisanonymous', 'hsuforum')); } $counts = array(get_string('totalpostsanddiscussions', 'hsuforum', $discussioncount + $postcount), get_string('totaldiscussions', 'hsuforum', $discussioncount), get_string('totalreplies', 'hsuforum', $postcount)); if (!empty($config->showsubstantive)) { $counts[] = get_string('totalsubstantive', 'hsuforum', $flagcount); } if ($grade = hsuforum_get_user_formatted_rating_grade($forum, $userid)) { $counts[] = get_string('totalrating', 'hsuforum', $grade); } $countshtml = ''; foreach ($counts as $count) { $countshtml .= html_writer::tag('div', $count, array('class' => 'hsuforum_count')); } $output = html_writer::div($countshtml, 'hsuforum_counts') . $showonlypreferencebutton . $output; $output = html_writer::div($output, 'mod-hsuforum-posts-container article'); } return $output; }