$string['columns'] = 'columns';
$string['commentdeleted'] = 'Comment deleted';
$string['commentempty'] = 'Comment was empty';
+$string['comment'] = 'Comment';
$string['comments'] = 'Comments';
$string['commentsaved'] = 'Comment saved';
$string['commentsn'] = '$a comment(s)';
require_once('../../config.php');
require_once('lib.php');
+ require_once('comment_form.php');
//param needed to go back to view.php
- $rid = required_param('rid', PARAM_INT); // Record ID
- $page = optional_param('page', 0, PARAM_INT); // Page ID
+ $rid = required_param('rid', PARAM_INT); // Record ID
+ $page = optional_param('page', 0, PARAM_INT); // Page ID
//param needed for comment operations
- $mode = optional_param('mode','',PARAM_ALPHA);
+ $mode = optional_param('mode','add',PARAM_ALPHA);
$commentid = optional_param('commentid','',PARAM_INT);
$confirm = optional_param('confirm','',PARAM_INT);
- $commentcontent = trim(optional_param('commentcontent','',PARAM_NOTAGS));
- $template = optional_param('template','',PARAM_ALPHA);
if (! $record = get_record('data_records', 'id', $rid)) {
if (!has_capability('mod/data:managecomments', $context) && $comment->userid != $USER->id) {
error('Comment is not yours to edit!');
}
+ } else {
+ $comment = false;
+ }
+
+
+ $mform = new data_comment_form('comment.php');
+ $mform->set_defaults(array('mode'=>$mode, 'page'=>$page, 'rid'=>$record->id, 'commentid'=>$commentid));
+ if ($comment) {
+ $format = $comment->format;
+ $content = $comment->content;
+ if (can_use_html_editor()) {
+ $options = new object();
+ $options->smiley = false;
+ $options->filter = false;
+ $content = format_text($content, $format, $options);
+ $format = FORMAT_HTML;
+ }
+ $mform->set_defaults(array('content'=>$content, 'format'=>$format));
+ }
+
+
+ if ($mform->is_cancelled()) {
+ redirect('view.php?rid='.$record->id.'&page='.$page);
}
switch ($mode) {
case 'add':
- if (empty($commentcontent)) {
- redirect('view.php?rid='.$record->id.'&page='.$page, get_string('commentempty', 'data'));
+ if (!$formadata = $mform->data_submitted()) {
+ break; // something is wrong here, try again
}
- $newcomment = new object;
- $newcomment->userid = $USER->id;
- $newcomment->created = time();
+ $newcomment = new object();
+ $newcomment->userid = $USER->id;
+ $newcomment->created = time();
$newcomment->modified = time();
- if (($newcomment->content = $commentcontent) && ($newcomment->recordid = $record->id)) {
- insert_record('data_comments',$newcomment);
+ $newcomment->content = $formadata->content;
+ $newcomment->recordid = $formadata->rid;
+ if (insert_record('data_comments',$newcomment)) {
+ redirect('view.php?rid='.$record->id.'&page='.$page);
+ } else {
+ error('Error while saving comment.');
}
- redirect('view.php?rid='.$record->id.'&page='.$page, get_string('commentsaved', 'data'));
- break;
- case 'edit': //print edit form
- print_header();
- print_heading(get_string('edit'));
- echo '<div align="center">';
- echo '<form action="comment.php" method="post">';
- echo '<input type="hidden" name="commentid" value="'.$comment->id.'" />';
- echo '<input type="hidden" name="rid" value="'.$record->id.'" />';
- 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
- if (empty($commentcontent)) {
- redirect('view.php?rid='.$record->id.'&page='.$page, get_string('commentempty', 'data'));
+ case 'edit': //print edit form
+ if (!$formadata = $mform->data_submitted()) {
+ break; // something is wrong here, try again
}
- if ($comment) {
- $newcomment = new object;
- $newcomment->id = $comment->id;
- $newcomment->content = $commentcontent;
- $newcomment->modified = time();
- update_record('data_comments',$newcomment);
+ $updatedcomment = new object();
+ $updatedcomment->id = $formadata->commentid;
+ $updatedcomment->content = $formadata->content;
+ $updatedcomment->format = $formadata->format;
+ $updatedcomment->modified = time();
+
+ if (update_record('data_comments',$updatedcomment)) {
+ redirect('view.php?rid='.$record->id.'&page='.$page);
+ } else {
+ error('Error while saving comment.');
}
- redirect('view.php?rid='.$record->id.'&page='.$page, get_string('commentsaved', 'data'));
break;
case 'delete': //deletes single comment from db
'view.php?rid='.$record->id.'&page='.$page);
print_footer();
}
-
- break;
-
- default: //print all listing, and add comment form
- print_header();
- data_print_comments($data, $record, $page);
- print_footer();
+ die;
break;
}
+ print_header();
+ data_print_comments($data, $record, $page, $mform);
+ print_footer();
+
?>
--- /dev/null
+<?php //$Id$
+
+require_once $CFG->libdir.'/formslib.php';
+
+class data_comment_form extends moodleform {
+ function definition() {
+ $mform =& $this->_form;
+
+ // visible elements
+ $mform->addElement('htmleditor', 'content', get_string('comment', 'data'), array('cols'=>85, 'rows'=>18));
+ $mform->addRule('content', get_string('required'), 'required', null, 'client');
+ $mform->setType('content', PARAM_RAW); // cleaned before the display
+
+ $mform->addElement('format', 'format', get_string('format'));
+ $mform->setHelpButton('format', array('textformat', get_string('helpformatting')));
+
+ // hidden optional params
+ $mform->addElement('hidden', 'mode', 'add');
+ $mform->setType('mode', PARAM_ALPHA);
+
+ $mform->addElement('hidden', 'page', 0);
+ $mform->setType('page', PARAM_INT);
+
+ $mform->addElement('hidden', 'rid', 0);
+ $mform->setType('rid', PARAM_INT);
+
+ $mform->addElement('hidden', 'commentid', 0);
+ $mform->setType('commentid', PARAM_INT);
+
+ // buttons
+ $buttonarray = array();
+ $buttonarray[] =& $mform->createElement('submit', 'submitbutton', get_string('savechanges'));
+ $buttonarray[] =& $mform->createElement('reset', 'reset', get_string('revert'));
+ $buttonarray[] =& $mform->createElement('cancel');
+
+ $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
+ }
+}
+?>
\ No newline at end of file
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" ENUM="false" NEXT="userid"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="recordid"/>
<FIELD NAME="recordid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="userid" NEXT="content"/>
- <FIELD NAME="content" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="recordid" NEXT="created"/>
- <FIELD NAME="created" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="content" NEXT="modified"/>
+ <FIELD NAME="content" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="recordid" NEXT="format"/>
+ <FIELD NAME="format" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="content" NEXT="created"/>
+ <FIELD NAME="created" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="format" NEXT="modified"/>
<FIELD NAME="modified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="created"/>
</FIELDS>
<KEYS>
/// $result = result of "/lib/ddllib.php" function calls
/// }
+ if ($result && $oldversion < 2006121300) {
+
+ /// Define field format to be added to data_comments
+ $table = new XMLDBTable('data_comments');
+ $field = new XMLDBField('format');
+ $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'content');
+
+ /// Launch add field format
+ $result = $result && add_field($table, $field);
+
+ }
+
return $result;
}
* Printing Ratings Form *
*********************************/
if (($template == 'singletemplate') && ($data->comments)) { //prints ratings options
+
data_print_comments($data, $record, $page);
}
//prints all comments + a text box for adding additional comment
-function data_print_comments($data, $record, $page=0) {
+function data_print_comments($data, $record, $page=0, $mform=false) {
global $CFG;
foreach ($comments as $comment) {
data_print_comment($data, $comment, $page);
}
+ echo '<br />';
+ }
+
+ if (!isloggedin() or isguest()) {
+ return;
}
- if (isloggedin() and !isguest()) {
- echo '<div class="newcomment" align="center"><form method="post" action="'.$CFG->wwwroot.'/mod/data/comment.php">';
- echo '<input type="hidden" name="mode" value="add" />';
- echo '<input type="hidden" name="page" value="'.$page.'" />';
- echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
- echo '<input type="hidden" name="rid" value="'.$record->id.'" />';
+ $editor = optional_param('addcomment', 0, PARAM_BOOL);
- echo '<textarea rows="8" cols="50" name="commentcontent"></textarea>';
- echo '<br /><input type="submit" value="'.get_string('addcomment','data').'" />';
- echo '</form></div>';
+ if (!$mform and !$editor) {
+ echo '<div class="newcomment" align="center">';
+ echo '<a href="view.php?d='.$data->id.'&page='.$page.'&mode=single&addcomment=1">'.get_string('addcomment', 'data').'</a>';
+ echo '</div>';
+ } else {
+ if (!$mform) {
+ require_once('comment_form.php');
+ $mform = new data_comment_form('comment.php');
+ $mform->set_defaults(array('mode'=>'add', 'page'=>$page, 'rid'=>$record->id));
+ }
+ echo '<div class="newcomment" align="center">';
+ $mform->display();
+ echo '</div>';
}
}
echo '</td><td class="content" align="left">'."\n";
// Print whole message
- $options->para = false;
- echo format_text($comment->content, FORMAT_MOODLE, $options);
+ echo format_text($comment->content, $comment->format);
/// Commands
$row[] = new tabobject('list', $CFG->wwwroot.'/mod/data/view.php?d='.$data->id, get_string('list','data'), '', true);
if (isset($record)) {
- $row[] = new tabobject('single', $CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$record->id, get_string('single','data'), '', true);
+ $row[] = new tabobject('single', $CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&rid='.$record->id, get_string('single','data'), '', true);
} else {
$row[] = new tabobject('single', $CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&mode=single', get_string('single','data'), '', true);
}
echo '<option value="##approve##">' .get_string('approve', 'data'). ' - ##approve##</option>';
echo '<option value="##user##">' .get_string('user'). ' - ##user##</option>';
if ($mode != 'singletemplate') {
- // more points to single template - not useable there
+ // more points to single template - not useable there
echo '<option value="##more##">' .get_string('more', 'data'). ' - ##more##</option>';
echo '<option value="##moreurl##">' .get_string('moreurl', 'data'). ' - ##moreurl##</option>';
echo '<option value="##comments##">' .get_string('comments', 'data'). ' - ##comments##</option>';
// This fragment is called by /admin/index.php
////////////////////////////////////////////////////////////////////////////////
-$module->version = 2006100201;
-$module->requires = 2006080900; // Requires this Moodle version
+$module->version = 2006121300;
+$module->requires = 2006120700; // Requires this Moodle version
$module->cron = 60;
?>