Added some set_time_limit()s in case of slow servers timing out.
[moodle-linuxchix.git] / mod / forum / rate.php
blob3d5207b35d9c1df4e48f4de4717f4f4c629951c4
1 <?php // $Id$
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
34 $returntoview = true;
38 if ($data) {
40 $lastpostid = 0;
42 foreach ((array)$data as $postid => $rating) {
43 if ($postid == "id") {
44 continue;
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)");
61 } else {
62 unset($newrating);
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"));
76 } else {
77 redirect("$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion", get_string("ratingssaved", "forum"));
79 } else {
80 redirect($_SERVER["HTTP_REFERER"], get_string("ratingssaved", "forum"));
83 } else {
84 error("This page was not accessed correctly");