adding ratings and comments functionality

This commit is contained in:
toyomoyo 2006-02-14 03:17:21 +00:00
parent 81ef8c1f48
commit 4d3d87f0a4
6 changed files with 438 additions and 4 deletions

View File

@ -89,6 +89,9 @@ function data_backup_one_mod($bf,$preferences,$data) {
fwrite ($bf,full_tag("LISTTEMPLATEHEADER",4,false,$data->listtemplateheader));
fwrite ($bf,full_tag("LISTTEMPLATEFOOTER",4,false,$data->listtemplatefooter));
fwrite ($bf,full_tag("APPROVAL",4,false,$data->approval));
fwrite ($bf,full_tag("SCALE",4,false,$data->scale));
fwrite ($bf,full_tag("ASSESSED",4,false,$data->assessed));
fwrite ($bf,full_tag("ASSESSPUBLIC",4,false,$data->assesspublic));
// if we've selected to backup users info, then call any other functions we need
// including backing up individual files

118
mod/data/comment.php Executable file
View File

@ -0,0 +1,118 @@
<?php
require_once('../../config.php');
require_once('lib.php');
//param needed to go back to view.php
$d = optional_param('d', 0, PARAM_INT); // database id
$search = optional_param('search','',PARAM_NOTAGS); //search string
$page = optional_param('page', 0, PARAM_INT); //offset of the current record
$rid = optional_param('rid', 0, PARAM_INT); //record id
$sort = optional_param('sort',0,PARAM_INT); //sort by field
$order = optional_param('order','ASC',PARAM_ALPHA); //sort order
$group = optional_param('group','0',PARAM_INT); //groupid
//param needed for comment operations
$mode = optional_param('mode','',PARAM_ALPHA);
$recordid = optional_param('recordid','',PARAM_INT);
$commentid = optional_param('commentid','',PARAM_INT);
$confirm = optional_param('confirm','',PARAM_INT);
$commentcontent = optional_param('commentcontent','',PARAM_NOTAGS);
if ((!$record = get_record('data_records','id',$recordid))) {
if (!$comment = get_record('data_comments','id',$commentid)) {
error ('this record does not exist');
} else {
$record = get_record('data_records','id',$comment->recordid);
}
}
if (!$data = get_record('data','id',$record->dataid)) {
error ('this database does not exist');
}
switch ($mode) {
case 'add':
$newcomment = new object;
$newcomment->userid = $USER->id;
if (($newcomment->content = $commentcontent) && ($newcomment->recordid = $recordid)) {
insert_record('data_comments',$newcomment);
}
redirect('view.php?d='.s($d).'&amp;search='.s($search).'&amp;sort='.s($sort).'&amp;order='.s($order).'&amp;group='.s($group).'&amp;page='.s($page).'&amp;rid='.s($rid), get_string("commentsaved", "forum"));
break;
case 'edit': //print edit form
print_header();
$comment = get_record('data_comments','id',$commentid);
print_heading('Edit');
echo '<div align="center">';
echo '<form action="comment.php" method="post">';
echo '<input type="hidden" name="commentid" value="'.$commentid.'" />';
echo '<input type="hidden" name="d" value="'.$d.'" />';
echo '<input type="hidden" name="search" value="'.$search.'" />';
echo '<input type="hidden" name="rid" value="'.$rid.'" />';
echo '<input type="hidden" name="sort" value="'.$sort.'" />';
echo '<input type="hidden" name="order" value="'.$order.'" />';
echo '<input type="hidden" name="group" value="'.$group.'" />';
echo '<input type="hidden" name="page" value="'.$page.'" />';
echo '<textarea name="commentcontent">'.s($comment->content).'</textarea>';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<input type="hidden" name="mode" value="editcommit" />';
echo '<br /><input type="submit" value="'.get_string('ok').'" />';
echo '<input type="button" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)" />';
echo '</form></div>';
print_footer();
break;
case 'editcommit': //update db
print_object(data_submitted());
$newcomment = new object;
$newcomment->id = $commentid;
$newcomment->content = $commentcontent;
update_record('data_comments',$newcomment);
redirect('view.php?d='.s($d).'&amp;search='.s($search).'&amp;sort='.s($sort).'&amp;order='.s($order).'&amp;group='.s($group).'&amp;page='.s($page).'&amp;rid='.s($rid), get_string("commentsaved", "forum"));
break;
case 'delete': //deletes single comment from db
if ($confirm and confirm_sesskey()) {
delete_records('data_comments','id',$commentid);
redirect('view.php?d='.s($d).'&amp;search='.s($search).'&amp;sort='.s($sort).'&amp;order='.s($order).'&amp;group='.s($group).'&amp;page='.s($page).'&amp;rid='.s($rid), get_string("commentsaved", "forum"));
} else { //print confirm delete form
print_header();
print_heading('Delete Confirm');
echo '<div align="center">';
echo '<form action="comment.php" method="post">';
echo '<input type="hidden" name="commentid" value="'.$commentid.'" />';
echo '<input type="hidden" name="d" value="'.$d.'" />';
echo '<input type="hidden" name="search" value="'.$search.'" />';
echo '<input type="hidden" name="rid" value="'.$rid.'" />';
echo '<input type="hidden" name="sort" value="'.$sort.'" />';
echo '<input type="hidden" name="order" value="'.$order.'" />';
echo '<input type="hidden" name="group" value="'.$group.'" />';
echo '<input type="hidden" name="page" value="'.$page.'" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<br />'.$comment->content.'<br />';
echo '<input type="hidden" name="mode" value="delete" />';
echo '<input type="hidden" name="confirm" value="1" />';
echo '<br /><input type="submit" value="'.get_string('ok').'" />';
echo '<input type="button" value="'.get_string('cancel').'" onclick="javascript:history.go(-1)" />';
echo '</form></div>';
print_footer();
}
break;
default: //print all listing, and add comment form
print_header();
data_print_comments($data, $record, $search, $template, $sort, $page, $rid, $order, $group);
print_footer();
break;
}
?>

View File

@ -762,6 +762,7 @@ function data_print_template($records, $data, $search, $template, $sort, $page=0
$patterns[]='/\#\#Delete\#\#/i';
$patterns[]='/\#\#More\#\#/i';
$patterns[]='/\#\#Approve\#\#/i';
$patterns[]='/\#\#Comment\#\#/i';
if (data_isowner($record->id) or isteacheredit($course->id)){
$replacement[] = '<a href="'.$CFG->wwwroot.'/mod/data/add.php?d='
@ -783,16 +784,36 @@ function data_print_template($records, $data, $search, $template, $sort, $page=0
}else {
$replacement[] = '';
}
if (($template == 'listtemplate') && ($data->comments)) {
$comments = count_records('data_comments','recordid',$record->id);
$replacement[] = '<a href="comment.php?recordid='.$record->id.'&amp;d='.$data->id.'&amp;search='.$search.'&amp;sort='.$sort.'&amp;order='.$order.'&amp;group='.$group.'&amp;page='.$page.'">'.$comments.' '.get_string('comment','data').'</a>';
} else {
$replacement[] = '';
}
///actual replacement of the tags
$newtext = preg_replace($patterns, $replacement, $data->{$template});
if ($return) {
return $newtext;
}
else {
} else {
echo $newtext; //prints the template with tags replaced
}
/**********************************
* Printing Ratings Form *
*********************************/
if ($template == 'singletemplate') { //prints ratings options
data_print_ratings($data, $record);
}
/**********************************
* Printing Ratings Form *
*********************************/
if (($template == 'singletemplate') && ($data->comments)) { //prints ratings options
data_print_comments($data, $record, $search, $template, $sort, $page, $rid, $order, $group);
}
//if this record is not yet approved, and database requires approval, print silly button
}
}
@ -859,7 +880,7 @@ function data_print_preference_form($data, $perpage, $search, $sort='', $order='
//silly function that prints a button
function data_print_approve_button($recordid, $d, $page='0', $rid='0', $search='', $sort='', $order='') {
$str= '<div align="center"><form action="approve.php" method="GET">';
$str.= '<input type="hidden" name="d" value="'.$d.'" />';
$str.= '<input type="hidden" name="d" value="'.$d.'" />';
$str.= '<input type="hidden" name="rid" value="'.$rid.'" />';
$str.= '<input type="hidden" name="page" value="'.$page.'" />';
$str.= '<input type="hidden" name="search" value="'.$search.'" />';
@ -878,4 +899,232 @@ function data_approve_record($recordid) {
$record->approved = 1;
update_record('data_records',$record);
}
//silly function that prints the a form to do ratings
function data_print_ratings($data, $record) {
global $USER, $course;
$ratingsmenuused = false;
if ($data->ratings and !empty($USER->id)) {
if ($ratings->scale = make_grades_menu($data->scale)) {
$ratings->assesspublic = $data->assesspublic;
$ratings->allow = (($data->assessed != 2 or isteacher($course->id)) && !isguest());
if ($ratings->allow) {
echo '<p /><form name="form" method="post" action="rate.php">';
echo '<div class="ratings" align="center">';
$useratings = true;
if ($useratings) {
if ((isteacher($course->id) or $ratings->assesspublic) and !data_isowner($record->id)) {
data_print_ratings_mean($record->id, $ratings->scale, isteacher($course->id));
if (!empty($ratings->allow)) {
echo '&nbsp;';
data_print_rating_menu($record->id, $USER->id, $ratings->scale);
$ratingsmenuused = true;
}
} else if (data_isowner($record->id)) {
data_print_ratings_mean($record->id, $ratings->scale, true);
} else if (!empty($ratings->allow) ) {
data_print_rating_menu($record->id, $USER->id, $ratings->scale);
$ratingsmenuused = true;
}
}
if ($data->scale < 0) {
if ($scale = get_record("scale", "id", abs($data->scale))) {
print_scale_menu_helpbutton($course->id, $scale );
}
}
if ($ratingsmenuused) {
echo '<input type="hidden" name="id" value="'.$course->id.'" />';
echo "<input type=\"submit\" value=\"".get_string("sendinratings", "forum")."\" />";
}
echo '</div>';
echo "</form>";
}
}
}
}
function data_print_ratings_mean($recordid, $scale, $link=true) {
/// Print the multiple ratings on a post given to the current user by others.
/// Scale is an array of ratings
static $strrate;
$mean = data_get_ratings_mean($recordid, $scale);
if ($mean !== "") {
if (empty($strratings)) {
$strratings = get_string("ratings", "forum");
}
echo "$strratings: ";
if ($link) {
link_to_popup_window ("/mod/data/report.php?id=$recordid", "ratings", $mean, 400, 600);
} else {
echo "$mean ";
}
}
}
function data_get_ratings_mean($recordid, $scale, $ratings=NULL) {
/// Return the mean rating of a post given to the current user by others.
/// Scale is an array of possible ratings in the scale
/// Ratings is an optional simple array of actual ratings (just integers)
if (!$ratings) {
$ratings = array();
if ($rates = get_records("data_ratings", "recordid", $recordid)) {
foreach ($rates as $rate) {
$ratings[] = $rate->rating;
}
}
}
$count = count($ratings);
if ($count == 0) {
return "";
} else if ($count == 1) {
return $scale[$ratings[0]];
} else {
$total = 0;
foreach ($ratings as $rating) {
$total += $rating;
}
$mean = round( ((float)$total/(float)$count) + 0.001); // Little fudge factor so that 0.5 goes UP
if (isset($scale[$mean])) {
return $scale[$mean]." ($count)";
} else {
return "$mean ($count)"; // Should never happen, hopefully
}
}
}
function data_print_rating_menu($recordid, $userid, $scale) {
/// Print the menu of ratings as part of a larger form.
/// If the post has already been - set that value.
/// Scale is an array of ratings
static $strrate;
if (!$rating = get_record("data_ratings", "userid", $userid, "recordid", $recordid)) {
$rating->rating = 0;
}
if (empty($strrate)) {
$strrate = get_string("rate", "forum");
}
choose_from_menu($scale, $recordid, $rating->rating, "$strrate...");
}
function data_get_ratings($recordid, $sort="u.firstname ASC") {
/// Returns a list of ratings for a particular post - sorted.
global $CFG;
return get_records_sql("SELECT u.*, r.rating
FROM {$CFG->prefix}data_ratings r,
{$CFG->prefix}user u
WHERE r.recordid = $recordid
AND r.userid = u.id
ORDER BY $sort");
}
//prints all comments + a text box for adding additional comment
function data_print_comments($data, $record , $search, $template, $sort, $page=0, $rid=0, $order='', $group='') {
//foreach comment, print it!
//(with links to edit, remove etc, but no reply!!!!!)
if ($comments = get_records('data_comments','recordid',$record->id)) {
foreach ($comments as $comment) {
data_print_comment($data, $comment->id);
}
}
//prints silly comment form
echo '<p /><div align="center"><form method="POST" action="comment.php">';
echo '<input type="hidden" name="mode" value="add" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<input type="hidden" name="recordid" value="'.$record->id.'" />';
echo '<input type="hidden" name="d" value="'.$data->id.'" />';
echo '<input type="hidden" name="search" value="'.$search.'" />';
echo '<input type="hidden" name="rid" value="'.$rid.'" />';
echo '<input type="hidden" name="sort" value="'.$sort.'" />';
echo '<input type="hidden" name="order" value="'.$order.'" />';
echo '<input type="hidden" name="group" value="'.$group.'" />';
echo '<input type="hidden" name="page" value="'.$page.'" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<textarea name="commentcontent"></textarea>';
echo '<br><input type="submit" value="'.get_string('addcomment','data').'" />';
echo '</form></div>';
}
//prints a single comment entry
function data_print_comment($data, $commentid) {
global $USER, $CFG, $course;
$stredit = get_string('edit');
$strdelete = get_string('delete');
$comment = get_record('data_comments','id',$commentid);
$user = get_record('user','id',$comment->userid);
echo '<div align="center"><table cellspacing="0" width ="50%" class="forumpost">';
echo '<tr class="header"><td class="picture left">';
print_user_picture($comment->userid, $course->id, false);
echo '</td>';
echo '<td width="90%"><div class="author">';
$fullname = fullname($user, isteacher($comment->userid));
$by = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.
$user->id.'&amp;course='.$course->id.'">'.$fullname.'</a>';
print_string('byname', 'data', $by);
echo '</div></td></tr>';
echo '<tr><td class="left side">';
if ($group = user_group($course->id, $comment->userid)) {
print_group_picture($group, $course->id, false, false, true);
} else {
echo '&nbsp;';
}
/// Actual content
echo '</td><td class="content">'."\n";
// Print whole message
echo format_text($comment->content);
/// Commands
/// Hack for allow to edit news posts those are not displayed yet until they are displayed
echo '<div class="commands">';
if (data_isowner($comment->recordid) or isteacher($course->id)) {
echo '<a href="'.$CFG->wwwroot.'/mod/data/comment.php?mode=edit&amp;commentid='.$comment->id.'">'.$stredit.'</a>';
}
if (data_isowner($comment->recordid) or isteacher($course->id)) {
echo '| <a href="'.$CFG->wwwroot.'/mod/data/comment.php?mode=delete&amp;commentid='.$comment->id.'">'.$strdelete.'</a>';
}
echo '</div>';
echo '</td></tr></table><div>'."\n\n";
}
?>

59
mod/data/rate.php Executable file
View File

@ -0,0 +1,59 @@
<?php
require_once("../../config.php");
require_once("lib.php");
print_object(data_submitted());
if (isguest()) {
error("Guests are not allowed to rate posts.", $_SERVER["HTTP_REFERER"]);
}
$id = required_param('id',PARAM_INT); // The course these ratings are part of
if (! $course = get_record("course", "id", $id)) {
error("Course ID was incorrect");
}
require_login($course->id);
$CFG->debug = 0; /// Temporarily
$returntoview = false;
if ($data = data_submitted()) {
$lastrecordid = 0;
foreach ((array)$data as $recordid => $rating) {
if ($recordid == "id") {
continue;
}
$recordid = (int)$recordid;
$lastrecordid = $recordid;
echo "recordid ".$recordid;
if ($oldrating = get_record("data_ratings", "userid", $USER->id, "recordid", $recordid)) {
if ($rating != $oldrating->rating) {
$oldrating->rating = $rating;
if (! update_record("data_ratings", $oldrating)) {
error("Could not update an old rating ($recordid = $rating)");
}
}
} else if ($rating) {
unset($newrating);
$newrating->userid = $USER->id;
$newrating->recordid = $recordid;
$newrating->rating = $rating;
if (! insert_record("data_ratings", $newrating)) {
error("Could not insert a new rating ($recordid = $rating)");
}
}
}
redirect($_SERVER["HTTP_REFERER"], get_string("ratingssaved", "forum"));
} else {
error("This page was not accessed correctly");
}
?>

View File

@ -74,6 +74,10 @@ function data_restore_mods($mod,$restore) {
$database->listtemplateheader = backup_todb($info['MOD']['#']['LISTTEMPLATEHEADER']['0']['#']);
$database->listtemplatefooter = backup_todb($info['MOD']['#']['LISTTEMPLATEFOOTER']['0']['#']);
$database->approval = backup_todb($info['MOD']['#']['APPROVAL']['0']['#']);
$database->scale = backup_todb($info['MOD']['#']['SCALE']['0']['#']);
$database->assessed = backup_todb($info['MOD']['#']['ASSESSED']['0']['#']);
$database->assesspublic = backup_todb($info['MOD']['#']['ASSESSPUBLIC']['0']['#']);
$newid = insert_record ("data",$database);
//Do some output

View File

@ -145,6 +145,7 @@
echo '<option value="##more##">##More##</option>';
echo '<option value="##delete##">##Delete##</option>';
echo '<option value="##approve##">##Approve##</option>';
echo '<option value="##comment##">##Comments##</option>';
echo '</select>';
///add the HTML editor(s)