3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.org //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
13 // This program is free software; you can redistribute it and/or modify //
14 // it under the terms of the GNU General Public License as published by //
15 // the Free Software Foundation; either version 2 of the License, or //
16 // (at your option) any later version. //
18 // This program is distributed in the hope that it will be useful, //
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21 // GNU General Public License for more details: //
23 // http://www.gnu.org/copyleft/gpl.html //
25 ///////////////////////////////////////////////////////////////////////////
27 /// Accept, process and reply to ajax calls to rate forums
29 /// TODO: Centralise duplicate code in rate.php and rate_ajax.php
31 require_once('../../config.php');
32 require_once($CFG->dirroot
. '/mod/forum/lib.php');
34 /// In developer debug mode, when there is a debug=1 in the URL send as plain text
35 /// for easier debugging.
36 if (debugging('', DEBUG_DEVELOPER
) && optional_param('debug', false, PARAM_BOOL
)) {
37 header('Content-type: text/plain; charset=UTF-8');
40 header('Content-type: application/json');
44 /// Here we maintain response contents
45 $response = array('status'=> 'Error', 'message'=>'kk');
49 print_error('mustbeloggedin');
52 print_error('noguestrate', 'forum');
54 if (!confirm_sesskey()) {
55 print_error('invalidsesskey');
59 /// Check required params
60 $postid = required_param('postid', PARAM_INT
); // The postid to rate
61 $rate = required_param('rate', PARAM_INT
); // The rate to apply
63 /// Check postid is valid
64 if (!$post = get_record_sql("SELECT p.*,
66 FROM {$CFG->prefix}forum_posts p
67 JOIN {$CFG->prefix}forum_discussions d ON p.discussion = d.id
68 WHERE p.id = $postid")) {
69 print_error('invalidpostid', 'forum', '', $postid);
73 if (!$forum = get_record('forum', 'id', $post->forumid
)) {
74 print_error('invalidforumid', 'forum');
78 if (!$course = get_record('course', 'id', $forum->course
)) {
79 print_error('invalidcourseid');
82 /// Check coursemodule
83 if (!$cm = get_coursemodule_from_instance('forum', $forum->id
)) {
84 print_error('invalidcoursemodule');
86 $forum->cmidnumber
= $cm->id
; //MDL-12961
89 /// Check forum can be rated
90 if (!$forum->assessed
) {
91 print_error('norate', 'forum');
94 /// Check user can rate
95 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
96 require_capability('mod/forum:rate', $context);
98 /// Check timed ratings
99 if ($forum->assesstimestart
and $forum->assesstimefinish
) {
100 if ($post->created
< $forum->assesstimestart
or $post->created
> $forum->assesstimefinish
) {
101 // we can not rate this, ignore it - this should not happen anyway unless teacher changes setting
102 print_error('norate', 'forum');
106 /// Calculate scale values
107 $scale_values = make_grades_menu($forum->scale
);
109 /// Check rate is valid for for that forum scale values
110 if (!array_key_exists($rate, $scale_values) && $rate != FORUM_UNSET_POST_RATING
) {
111 print_error('invalidrate', 'forum');
114 /// Everything ready, process rate
117 if ($rate == FORUM_UNSET_POST_RATING
) {
118 delete_records('forum_ratings', 'post', $postid, 'userid', $USER->id
);
121 } else if ($oldrating = get_record('forum_ratings', 'userid', $USER->id
, 'post', $post->id
)) {
122 if ($rate != $oldrating->rating
) {
123 $oldrating->rating
= $rate;
124 $oldrating->time
= time();
125 if (!update_record('forum_ratings', $oldrating)) {
126 error("Could not update an old rating ($post->id = $rate)");
132 $newrating = new object();
133 $newrating->userid
= $USER->id
;
134 $newrating->time
= time();
135 $newrating->post
= $post->id
;
136 $newrating->rating
= $rate;
138 if (!insert_record('forum_ratings', $newrating)) {
139 print_error('cannotinsertrate', 'error', '', (object)array('id'=>$postid, 'rating'=>$rate));
144 forum_update_grades($forum, $post->userid
);
146 /// Check user can see any rate
147 $canviewanyrating = has_capability('mod/forum:viewanyrating', $context);
149 /// Decide if rates info is displayed
151 if ($canviewanyrating) {
152 $rateinfo = forum_print_ratings($postid, $scale_values, $forum->assessed
, true, NULL, true);
155 /// Calculate response
156 $response['status'] = 'Ok';
157 $response['message'] = $rateinfo;
158 echo json_encode($response);