3 // Collect ratings, store them, then return to where we came from
6 require_once("../../config.php");
7 require_once("lib.php");
10 $id = required_param('id',PARAM_INT
); // The course these ratings are part of
11 $forumid = required_param('forumid',PARAM_INT
); // The forum the rated posts are from
13 if (! $cm = get_coursemodule_from_instance('forum', $forumid, $id)) {
14 error('Course Module ID was incorrect');
17 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
19 if (!has_capability('mod/forum:rate', $context)) {
20 error('You do not have the permission to rate this post');
24 if (! $course = get_record("course", "id", $id)) {
25 error("Course ID was incorrect");
28 require_login($course->id
);
30 $returntoview = false;
32 if (!$data = data_submitted("$CFG->wwwroot/mod/forum/discuss.php")) { // form submitted
33 if ($data = data_submitted("$CFG->wwwroot/mod/forum/view.php")) { // Single forums are special case
42 foreach ((array)$data as $postid => $rating) {
43 if ($postid == "id") {
47 $postid = (int)$postid;
48 $lastpostid = $postid;
50 if ($rating == FORUM_UNSET_POST_RATING
) {
51 delete_records('forum_ratings', 'post', $postid, 'userid', $USER->id
);
53 } else if ($oldrating = get_record("forum_ratings", "userid", $USER->id
, "post", $postid)) {
54 if ($rating != $oldrating->rating
) {
55 $oldrating->rating
= $rating;
56 $oldrating->time
= time();
57 if (! update_record("forum_ratings", $oldrating)) {
58 error("Could not update an old rating ($postid = $rating)");
63 $newrating->userid
= $USER->id
;
64 $newrating->time
= time();
65 $newrating->post
= $postid;
66 $newrating->rating
= $rating;
68 if (! insert_record("forum_ratings", $newrating)) {
69 error("Could not insert a new rating ($postid = $rating)");
73 if ($post = get_record('forum_posts', 'id', $lastpostid)) { // To find discussion we're in
74 if ($returntoview and ($discussion = get_record('forum_discussions', 'id', $post->discussion
))) {
75 redirect("$CFG->wwwroot/mod/forum/view.php?f=$discussion->forum", get_string("ratingssaved", "forum"));
77 redirect("$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion", get_string("ratingssaved", "forum"));
80 redirect($_SERVER["HTTP_REFERER"], get_string("ratingssaved", "forum"));
84 error("This page was not accessed correctly");