Index: mod/forum/discuss.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/forum/discuss.php,v
retrieving revision 1.131
diff -u -r1.131 discuss.php
--- mod/forum/discuss.php 11 Sep 2008 13:44:34 -0000 1.131
+++ mod/forum/discuss.php 23 Nov 2008 12:48:14 -0000
@@ -31,6 +31,10 @@
require_course_login($course, true, $cm);
+/// Add ajax-related libs
+ require_js(array('yui_yahoo', 'yui_event', 'yui_dom', 'yui_connection', 'yui_json'));
+ require_js('mod/forum/rate_ajax.js');
+
$modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
require_capability('mod/forum:viewdiscussion', $modcontext, NULL, true, 'noviewdiscussionspermission', 'forum');
Index: mod/forum/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/forum/lib.php,v
retrieving revision 1.752
diff -u -r1.752 lib.php
--- mod/forum/lib.php 20 Nov 2008 13:09:45 -0000 1.752
+++ mod/forum/lib.php 23 Nov 2008 12:48:19 -0000
@@ -3228,7 +3228,9 @@
}
if ($canviewallratings and !$mypost) {
- forum_print_ratings($post->id, $ratings->scale, $forum->assessed, $canviewallratings, $allratings);
+ echo '' .
+ forum_print_ratings($post->id, $ratings->scale, $forum->assessed, $canviewallratings, $allratings, true) .
+ '';
if (!empty($ratings->allow)) {
echo ' ';
forum_print_rating_menu($post->id, $USER->id, $ratings->scale, $myrating);
@@ -3236,7 +3238,9 @@
}
} else if ($mypost) {
- forum_print_ratings($post->id, $ratings->scale, $forum->assessed, true, $allratings);
+ echo '' .
+ forum_print_ratings($post->id, $ratings->scale, $forum->assessed, true, $allratings, true) .
+ '';
} else if (!empty($ratings->allow) ) {
forum_print_rating_menu($post->id, $USER->id, $ratings->scale, $myrating);
@@ -3460,7 +3464,7 @@
* Forumid prevents the double lookup of the forumid in discussion to determine the aggregate type
* Scale is an array of ratings
*/
-function forum_print_ratings($postid, $scale, $aggregatetype, $link=true, $ratings=null) {
+function forum_print_ratings($postid, $scale, $aggregatetype, $link=true, $ratings=null, $return=false) {
$strratings = '';
@@ -3493,11 +3497,18 @@
$strratings = get_string("ratings", "forum");
}
- echo "$strratings: ";
+ $strratings .= ': ';
+
if ($link) {
- link_to_popup_window ("/mod/forum/report.php?id=$postid", "ratings", $agg, 400, 600);
+ $strratings .= link_to_popup_window ("/mod/forum/report.php?id=$postid", "ratings", $agg, 400, 600, null, null, true);
} else {
- echo "$agg ";
+ $strratings .= "$agg ";
+ }
+
+ if ($return) {
+ return $strratings;
+ } else {
+ echo $strratings;
}
}
}
@@ -3741,7 +3752,7 @@
$strrate = get_string("rate", "forum");
}
$scale = array(FORUM_UNSET_POST_RATING => $strrate.'...') + $scale;
- choose_from_menu($scale, $postid, $myrating, '');
+ choose_from_menu($scale, $postid, $myrating, '', '', '0', false, false, 0, '', false, false, 'forumpostratingmenu');
}
/**
@@ -5290,7 +5301,10 @@
if ($ratingsformused) {
if ($ratingsmenuused) {
echo '
';
- echo '';
+ echo '';
+ if (ajaxenabled()) { /// AJAX enabled, standard submission form
+ print_js_call('init_rate_ajax');
+ }
if ($forum->scale < 0) {
if ($scale = $DB->get_record("scale", array("id" => abs($forum->scale)))) {
print_scale_menu_helpbutton($course->id, $scale );
Index: mod/forum/rate_ajax.js
===================================================================
RCS file: mod/forum/rate_ajax.js
diff -N mod/forum/rate_ajax.js
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ mod/forum/rate_ajax.js 23 Nov 2008 12:48:19 -0000
@@ -0,0 +1,168 @@
+// $Id$
+
+///////////////////////////////////////////////////////////////////////////
+// //
+// NOTICE OF COPYRIGHT //
+// //
+// Moodle - Modular Object-Oriented Dynamic Learning Environment //
+// http://moodle.org //
+// //
+// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
+// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
+// //
+// This program is free software; you can redistribute it and/or modify //
+// it under the terms of the GNU General Public License as published by //
+// the Free Software Foundation; either version 2 of the License, or //
+// (at your option) any later version. //
+// //
+// This program is distributed in the hope that it will be useful, //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
+// GNU General Public License for more details: //
+// //
+// http://www.gnu.org/copyleft/gpl.html //
+// //
+///////////////////////////////////////////////////////////////////////////
+
+/// Javascript used to handle AJAX forum ratings
+
+/**
+ * This function initializes all the stuff needed to have forum ratings
+ * working under AJAX. Basically it adds one onload listener that triggers
+ * the add_menu_listeners() function to add menu listeners
+ */
+function init_rate_ajax () {
+ YAHOO.util.Event.onDOMReady(add_menu_listeners);
+}
+
+/**
+ * This function adds event listeners to any rating
+ * menu found in he page (class = forumpostratingmenu)
+ * and prevents manual submission
+ */
+function add_menu_listeners(e) {
+
+ /** hide the submit button */
+ var submitbutton = YAHOO.util.Dom.get('forumpostratingsubmit');
+ submitbutton.style.display = 'none';
+
+ /** prevent form submission **/
+ var form = YAHOO.util.Dom.get('form');
+ YAHOO.util.Event.addListener(form, 'submit', prevent_form_submission);
+
+ /** add listeners to all rating menus */
+ var menus = YAHOO.util.Dom.getElementsByClassName('forumpostratingmenu', 'select');
+ for (var i=0; idirroot . '/mod/forum/lib.php');
+
+/// In developer debug mode, when there is a debug=1 in the URL send as plain text
+/// for easier debugging.
+ if (debugging('', DEBUG_DEVELOPER) && optional_param('debug', false, PARAM_BOOL)) {
+ header('Content-type: text/plain; charset=UTF-8');
+ $debugmode = true;
+ } else {
+ header('Content-type: application/json');
+ $debugmode = false;
+ }
+
+/// Here we maintain response contents
+ $response = array('status'=> 'Error', 'message'=>'kk');
+
+/// Check access.
+ if (!isloggedin()) {
+ print_error('mustbeloggedin');
+ }
+ if (isguestuser()) {
+ print_error('noguestrate', 'forum');
+ }
+ if (!confirm_sesskey()) {
+ print_error('invalidsesskey');
+ }
+
+
+/// Check required params
+ $postid = required_param('postid', PARAM_INT); // The postid to rate
+ $rate = required_param('rate', PARAM_INT); // The rate to apply
+
+/// Check postid is valid
+ if (!$post = $DB->get_record_sql('SELECT p.*,
+ d.forum AS forumid
+ FROM {forum_posts} p
+ JOIN {forum_discussions} d ON p.discussion = d.id
+ WHERE p.id = ?', array($postid))) {
+ print_error('invalidpostid', 'forum', '', $postid);;
+ }
+
+/// Check forum
+ if (!$forum = $DB->get_record('forum', array('id' => $post->forumid))) {
+ print_error('invalidforumid', 'forum');
+ }
+
+/// Check course
+ if (!$course = $DB->get_record('course', array('id' => $forum->course))) {
+ print_error('invalidcourseid');
+ }
+
+/// Check coursemodule
+ if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) {
+ print_error('invalidcoursemodule');
+ } else {
+ $forum->cmidnumber = $cm->id; //MDL-12961
+ }
+
+/// Check forum can be rated
+ if (!$forum->assessed) {
+ print_error('norate', 'forum');
+ }
+
+/// Check user can rate
+ $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+ require_capability('mod/forum:rate', $context);
+
+/// Check timed ratings
+ 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
+ print_error('norate', 'forum');
+ }
+ }
+
+/// Calculate scale values
+ $scale_values = make_grades_menu($forum->scale);
+
+/// Check rate is valid for for that forum scale values
+ if (!array_key_exists($rate, $scale_values) && $rate != FORUM_UNSET_POST_RATING) {
+ print_error('invalidrate', 'forum');
+ }
+
+/// Everything ready, process rate
+
+/// Deleting rate
+ if ($rate == FORUM_UNSET_POST_RATING) {
+ $DB->delete_records('forum_ratings', array('post' => $postid, 'userid' => $USER->id));
+
+/// Updating rate
+ } else if ($oldrating = $DB->get_record('forum_ratings', array('userid' => $USER->id, 'post' => $post->id))) {
+ if ($rate != $oldrating->rating) {
+ $oldrating->rating = $rate;
+ $oldrating->time = time();
+ if (!$DB->update_record('forum_ratings', $oldrating)) {
+ print_error('cannotupdaterate', 'error', '', (object)array('id'=>$post->id, 'rating'=>$rate));
+ }
+ }
+
+/// Inserting rate
+ } else {
+ $newrating = new object();
+ $newrating->userid = $USER->id;
+ $newrating->time = time();
+ $newrating->post = $post->id;
+ $newrating->rating = $rate;
+
+ if (! $DB->insert_record('forum_ratings', $newrating)) {
+ print_error('cannotinsertrate', 'error', '', (object)array('id'=>$postid, 'rating'=>$rate));
+ }
+ }
+
+/// Update grades
+ forum_update_grades($forum, $post->userid);
+
+/// Check user can see any rate
+ $canviewanyrating = has_capability('mod/forum:viewanyrating', $context);
+
+/// Decide if rates info is displayed
+ $rateinfo = '';
+ if ($canviewanyrating) {
+ $rateinfo = forum_print_ratings($postid, $scale_values, $forum->assessed, true, NULL, true);
+ }
+
+/// Calculate response
+ $response['status'] = 'Ok';
+ $response['message'] = $rateinfo;
+ echo json_encode($response);
+
+?>