]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-5964 need html editor for comments in database mod + added format sql table field
authorskodak <skodak>
Wed, 13 Dec 2006 23:09:34 +0000 (23:09 +0000)
committerskodak <skodak>
Wed, 13 Dec 2006 23:09:34 +0000 (23:09 +0000)
MDL-7813 accessiblity fixes in comments

lang/en_utf8/data.php
mod/data/comment.php
mod/data/comment_form.php [new file with mode: 0644]
mod/data/db/install.xml
mod/data/db/upgrade.php
mod/data/lib.php
mod/data/tabs.php
mod/data/templates.php
mod/data/version.php

index 89c2f22ac1325f49fa2cbba3ca5698faa9c189ea..be397ac236715a4cec66db6a44aea2fb09e17d7f 100644 (file)
@@ -28,6 +28,7 @@ $string['chooseorupload'] = 'Choose file';
 $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)';
index bc2cea1f6a4ab7c7c7b38e32e9757c2550a6e80f..191f120dc8415d862dcf2f1229f9cbfead86afdc 100755 (executable)
@@ -2,17 +2,16 @@
 
     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.'&amp;page='.$page);
     }
 
     switch ($mode) {
         case 'add':
-            if (empty($commentcontent)) {
-                redirect('view.php?rid='.$record->id.'&amp;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.'&amp;page='.$page);
+            } else {
+                error('Error while saving comment.');
             }
-            redirect('view.php?rid='.$record->id.'&amp;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.'&amp;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.'&amp;page='.$page);
+            } else {
+                error('Error while saving comment.');
             }
-            redirect('view.php?rid='.$record->id.'&amp;page='.$page, get_string('commentsaved', 'data'));
         break;
 
         case 'delete':    //deletes single comment from db
                   'view.php?rid='.$record->id.'&amp;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();
+
 
 ?>
diff --git a/mod/data/comment_form.php b/mod/data/comment_form.php
new file mode 100644 (file)
index 0000000..b6a5754
--- /dev/null
@@ -0,0 +1,39 @@
+<?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
index ba49e9f8db5563724d68a6f44da9624dd5f66466..d71f92eafd4c9d117dd5af5c8b9a4627b28d17c9 100644 (file)
@@ -99,8 +99,9 @@
         <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>
index 954fbdd28126308f089ff14d260cdbfabfae6d71..94c9754c9f44de3d4eed181a0d911d27f8e6fb5c 100644 (file)
@@ -32,6 +32,18 @@ function xmldb_data_upgrade($oldversion=0) {
 ///     $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;
 }
 
index 105d6019608d9857f6d78cd50c4bf6355468c13b..4658352f93b87ea7a45e7d4673b0317f1076070b 100755 (executable)
@@ -890,6 +890,7 @@ function data_print_template($template, $records, $data, $search='',$page=0, $re
          *    Printing Ratings Form       *
          *********************************/
         if (($template == 'singletemplate') && ($data->comments)) {    //prints ratings options
+
             data_print_comments($data, $record, $page);
         }
 
@@ -1081,7 +1082,7 @@ function data_get_ratings($recordid, $sort="u.firstname ASC") {
 
 
 //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;
 
@@ -1091,18 +1092,28 @@ function data_print_comments($data, $record, $page=0) {
         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.'&amp;page='.$page.'&amp;mode=single&amp;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>';
     }
 }
 
@@ -1146,8 +1157,7 @@ function data_print_comment($data, $comment, $page=0) {
     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
 
index f17d53a8dfb034893533e8abbdcb318a5e9a584c..037abeabb41d3c9628dd2beb3bc33418e96a95fe 100755 (executable)
@@ -37,7 +37,7 @@
     $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.'&amp;rid='.$record->id, get_string('single','data'), '', true);
     } else {
         $row[] = new tabobject('single', $CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&amp;mode=single', get_string('single','data'), '', true);
     }
index 37bccb3b6c7172edc612a30261495f27a007d5c4..3b9fe44a86d23910cf066f3f189e939c1259218d 100755 (executable)
             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>';
index d4eb8b2d41f80c5be9b2d3bda4b03b44793a5a90..626454a94c70553f072240344a78f4fe1d800d0c 100644 (file)
@@ -5,8 +5,8 @@
 //  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;
 
 ?>