2004-09-12 16:24:41 +00:00
|
|
|
<?php // $Id$
|
2002-07-31 14:19:35 +00:00
|
|
|
|
|
|
|
// Collect ratings, store them, then return to where we came from
|
|
|
|
|
|
|
|
|
2007-06-03 16:24:03 +00:00
|
|
|
require_once('../../config.php');
|
|
|
|
require_once('lib.php');
|
|
|
|
|
|
|
|
$forumid = required_param('forumid', PARAM_INT); // The forum the rated posts are from
|
|
|
|
|
2008-06-05 20:16:09 +00:00
|
|
|
if (!$forum = $DB->get_record('forum', array('id' => $forumid))) {
|
2008-05-27 03:20:47 +00:00
|
|
|
print_error('invalidforumid', 'forum');
|
2006-08-08 05:13:06 +00:00
|
|
|
}
|
2007-06-03 16:24:03 +00:00
|
|
|
|
2008-06-05 20:16:09 +00:00
|
|
|
if (!$course = $DB->get_record('course', array('id' => $forum->course))) {
|
2008-05-27 03:20:47 +00:00
|
|
|
print_error('invalidcourseid');
|
2002-07-31 14:19:35 +00:00
|
|
|
}
|
|
|
|
|
2007-06-03 16:24:03 +00:00
|
|
|
if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) {
|
2008-05-27 03:20:47 +00:00
|
|
|
print_error('invalidcoursemodule');
|
2008-02-05 11:22:13 +00:00
|
|
|
} else {
|
|
|
|
$forum->cmidnumber = $cm->id; //MDL-12961
|
|
|
|
}
|
2002-07-31 14:19:35 +00:00
|
|
|
|
2007-06-03 16:24:03 +00:00
|
|
|
require_login($course, false, $cm);
|
2004-03-25 05:29:38 +00:00
|
|
|
|
2007-06-03 16:24:03 +00:00
|
|
|
if (isguestuser()) {
|
2008-05-27 03:20:47 +00:00
|
|
|
print_error('noguestrate', 'forum');
|
2007-06-03 16:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$forum->assessed) {
|
2008-05-27 03:20:47 +00:00
|
|
|
print_error('norate', 'forum');
|
2004-03-25 05:29:38 +00:00
|
|
|
}
|
|
|
|
|
2007-06-03 16:24:03 +00:00
|
|
|
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
|
|
|
|
require_capability('mod/forum:rate', $context);
|
|
|
|
|
|
|
|
if ($data = data_submitted()) {
|
|
|
|
|
|
|
|
$discussionid = false;
|
2004-11-30 02:53:08 +00:00
|
|
|
|
2004-09-23 07:50:05 +00:00
|
|
|
foreach ((array)$data as $postid => $rating) {
|
2007-06-03 16:24:03 +00:00
|
|
|
if (!is_numeric($postid)) {
|
2002-07-31 14:19:35 +00:00
|
|
|
continue;
|
|
|
|
}
|
2004-09-23 07:50:05 +00:00
|
|
|
|
2007-06-03 16:24:03 +00:00
|
|
|
// following query validates the submitted postid too
|
|
|
|
$sql = "SELECT fp.*
|
2008-06-05 20:16:09 +00:00
|
|
|
FROM {forum_posts} fp, {forum_discussions} fd
|
|
|
|
WHERE fp.id = ? AND fp.discussion = fd.id AND fd.forum = ?";
|
2007-06-03 16:24:03 +00:00
|
|
|
|
2008-06-05 20:16:09 +00:00
|
|
|
if (!$post = $DB->get_record_sql($sql, array($postid, $forum->id))) {
|
2008-05-27 03:20:47 +00:00
|
|
|
print_error('invalidpostid', 'forum', '', $postid);
|
2007-06-03 16:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$discussionid = $post->discussion;
|
|
|
|
|
|
|
|
if ($forum->assesstimestart and $forum->assesstimefinish) {
|
|
|
|
if ($post->created < $forum->assesstimestart or $post->created > $forum->assesstimefinish) {
|
|
|
|
// we can not rate this, ignore it - this should not happen anyway unless teacher changes setting
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2004-11-30 02:53:08 +00:00
|
|
|
|
2006-10-05 03:18:29 +00:00
|
|
|
if ($rating == FORUM_UNSET_POST_RATING) {
|
2008-06-05 20:16:09 +00:00
|
|
|
$DB->delete_records('forum_ratings', array('post' => $postid, 'userid' => $USER->id));
|
2007-06-05 22:58:37 +00:00
|
|
|
forum_update_grades($forum, $post->userid);
|
2006-10-05 03:18:29 +00:00
|
|
|
|
2008-06-05 20:16:09 +00:00
|
|
|
} else if ($oldrating = $DB->get_record('forum_ratings', 'userid', $USER->id, 'post', $post->id)) {
|
2003-08-15 13:59:24 +00:00
|
|
|
if ($rating != $oldrating->rating) {
|
|
|
|
$oldrating->rating = $rating;
|
2007-06-03 16:24:03 +00:00
|
|
|
$oldrating->time = time();
|
2008-06-05 20:16:09 +00:00
|
|
|
if (!$DB->update_record('forum_ratings', $oldrating)) {
|
2008-05-27 03:20:47 +00:00
|
|
|
print_error('cannotupdaterate', 'forum', '',
|
|
|
|
array($post->id, $rating));
|
2002-07-31 14:19:35 +00:00
|
|
|
}
|
2007-06-05 22:58:37 +00:00
|
|
|
forum_update_grades($forum, $post->userid);
|
2002-07-31 14:19:35 +00:00
|
|
|
}
|
2007-06-03 16:24:03 +00:00
|
|
|
|
2006-10-05 03:18:29 +00:00
|
|
|
} else {
|
2007-06-03 16:24:03 +00:00
|
|
|
$newrating = new object();
|
2003-08-15 13:59:24 +00:00
|
|
|
$newrating->userid = $USER->id;
|
2007-06-03 16:24:03 +00:00
|
|
|
$newrating->time = time();
|
|
|
|
$newrating->post = $post->id;
|
2003-08-15 13:59:24 +00:00
|
|
|
$newrating->rating = $rating;
|
|
|
|
|
2008-06-05 20:16:09 +00:00
|
|
|
if (! $DB->insert_record('forum_ratings', $newrating)) {
|
2008-05-27 03:20:47 +00:00
|
|
|
print_error('cannotinsertrate', 'forum', '',
|
|
|
|
array($postid, $rating));
|
2003-08-15 13:59:24 +00:00
|
|
|
}
|
2007-06-05 22:58:37 +00:00
|
|
|
forum_update_grades($forum, $post->userid);
|
2002-07-31 14:19:35 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-03 16:24:03 +00:00
|
|
|
|
|
|
|
if ($forum->type == 'single' or !$discussionid) {
|
|
|
|
redirect("$CFG->wwwroot/mod/forum/view.php?id=$cm->id", get_string('ratingssaved', 'forum'));
|
2004-03-22 05:59:43 +00:00
|
|
|
} else {
|
2007-06-03 16:24:03 +00:00
|
|
|
redirect("$CFG->wwwroot/mod/forum/discuss.php?d=$discussionid", get_string('ratingssaved', 'forum'));
|
2004-03-22 05:59:43 +00:00
|
|
|
}
|
2002-07-31 14:19:35 +00:00
|
|
|
|
|
|
|
} else {
|
2008-05-27 03:20:47 +00:00
|
|
|
print_error('invalidaccess', 'forum');
|
2002-07-31 14:19:35 +00:00
|
|
|
}
|
|
|
|
|
2006-10-23 03:29:54 +00:00
|
|
|
?>
|