Updated the 19 build version
[moodle.git] / mod / forum / rate.php
blob74c7fb92e8aac413612ce83fcb7fdd5a3fbecefc
1 <?php // $Id$
3 // Collect ratings, store them, then return to where we came from
5 /// TODO: Centralise duplicate code in rate.php and rate_ajax.php
7 require_once('../../config.php');
8 require_once('lib.php');
10 $forumid = required_param('forumid', PARAM_INT); // The forum the rated posts are from
12 if (!$forum = get_record('forum', 'id', $forumid)) {
13 error("Forum ID was incorrect");
16 if (!$course = get_record('course', 'id', $forum->course)) {
17 error("Course ID was incorrect");
20 if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) {
21 error("Course Module ID was incorrect");
22 } else {
23 $forum->cmidnumber = $cm->id; //MDL-12961
26 require_login($course, false, $cm);
28 if (isguestuser()) {
29 error("Guests are not allowed to rate entries.");
32 if (!$forum->assessed) {
33 error("Rating of items not allowed!");
36 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
37 require_capability('mod/forum:rate', $context);
39 if ($data = data_submitted() and confirm_sesskey()) {
41 $discussionid = false;
43 /// Calculate scale values
44 $scale_values = make_grades_menu($forum->scale);
46 foreach ((array)$data as $postid => $rating) {
47 if (!is_numeric($postid)) {
48 continue;
51 // following query validates the submitted postid too
52 $sql = "SELECT fp.*
53 FROM {$CFG->prefix}forum_posts fp, {$CFG->prefix}forum_discussions fd
54 WHERE fp.id = '$postid' AND fp.discussion = fd.id AND fd.forum = $forum->id";
56 if (!$post = get_record_sql($sql)) {
57 error("Incorrect postid - $postid");
60 $discussionid = $post->discussion;
62 if ($forum->assesstimestart and $forum->assesstimefinish) {
63 if ($post->created < $forum->assesstimestart or $post->created > $forum->assesstimefinish) {
64 // we can not rate this, ignore it - this should not happen anyway unless teacher changes setting
65 continue;
69 /// Check rate is valid for for that forum scale values
70 if (!array_key_exists($rating, $scale_values) && $rating != FORUM_UNSET_POST_RATING) {
71 print_error('invalidrate', 'forum', '', $rating);
74 if ($rating == FORUM_UNSET_POST_RATING) {
75 delete_records('forum_ratings', 'post', $postid, 'userid', $USER->id);
76 forum_update_grades($forum, $post->userid);
78 } else if ($oldrating = get_record('forum_ratings', 'userid', $USER->id, 'post', $post->id)) {
79 if ($rating != $oldrating->rating) {
80 $oldrating->rating = $rating;
81 $oldrating->time = time();
82 if (! update_record('forum_ratings', $oldrating)) {
83 error("Could not update an old rating ($post->id = $rating)");
85 forum_update_grades($forum, $post->userid);
88 } else {
89 $newrating = new object();
90 $newrating->userid = $USER->id;
91 $newrating->time = time();
92 $newrating->post = $post->id;
93 $newrating->rating = $rating;
95 if (! insert_record('forum_ratings', $newrating)) {
96 error("Could not insert a new rating ($postid = $rating)");
98 forum_update_grades($forum, $post->userid);
102 if ($forum->type == 'single' or !$discussionid) {
103 redirect("$CFG->wwwroot/mod/forum/view.php?id=$cm->id", get_string('ratingssaved', 'forum'));
104 } else {
105 redirect("$CFG->wwwroot/mod/forum/discuss.php?d=$discussionid", get_string('ratingssaved', 'forum'));
108 } else {
109 error("This page was not accessed correctly");