]> git.mjollnir.org Git - moodle.git/commitdiff
"MDL-20346, comments api for data module"
authorDongsheng Cai <unoter@gmail.com>
Wed, 18 Nov 2009 06:00:48 +0000 (06:00 +0000)
committerDongsheng Cai <unoter@gmail.com>
Wed, 18 Nov 2009 06:00:48 +0000 (06:00 +0000)
lang/en_utf8/error.php
mod/data/backuplib.php
mod/data/db/install.xml
mod/data/db/upgrade.php
mod/data/lib.php
mod/data/restorelib.php
mod/data/version.php
mod/data/view.php

index a1c133098d0083d4fbd9e80d0727ce6883ff3df6..4c8809ffb0f42c3a91fd2c12307465b6912cdd6e 100644 (file)
@@ -85,6 +85,7 @@ $string['cannotmanualctrack'] = 'Activity does not provide manual completion tra
 $string['cannotmapfield'] = 'Mapping collision detected - two fields maps to the same grade item $a';
 $string['cannotmarktopic'] = 'Could not mark that topic for this course';
 $string['cannotmetacourse'] = 'Cannot not add the selected course to this meta course!';
+$string['cannotmigratedatacomments'] = 'Cannot migrate data module comments';
 $string['cannotmoverolewithid'] = 'Cannot move role with ID $a';
 $string['cannotmodulename'] = 'Cannot get the module name in build navigation';
 $string['cannotmoduletype'] = 'Cannot get the module type in build navigation';
index 4ad6ebbb0c94c908cd769ff53e6a20ba9b01ddac..111f1d4c7706f5f18eebc41f6e1f7a9f78c7b0b3 100644 (file)
@@ -229,7 +229,22 @@ function backup_data_comments($bf,$preferences,$recordid){
     global $CFG, $DB;
 
     $status = true;
-    $data_comments = $DB->get_records("data_comments", array("recordid"=>$recordid));
+
+    $lastrecord   = $DB->get_record_sql('SELECT d.id AS dataid, d.course AS course FROM {data} d, {data_records} r
+                                          WHERE r.dataid = d.id AND r.id = ?', array($recordid));
+
+    $params = array();
+    $params[] = $recordid;
+    $sql = 'SELECT d.id, d.course FROM {data_records} r, {data} d WHERE r.dataid = d.id AND r.id=?';
+    $result = $DB->get_record_sql($sql, $params);
+    if ($cm = get_coursemodule_from_instance('data', $result->id, $result->course)) {
+        $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+    }
+    $data_comments = $DB->get_records('comments', array(
+                        'itemid'=>$recordid,
+                        'commentarea'=>'database_entry',
+                        'contextid'=>$context->id)
+                    );
 
     //If there is submissions
     if ($data_comments) {
@@ -239,14 +254,16 @@ function backup_data_comments($bf,$preferences,$recordid){
         foreach ($data_comments as $com_sub) {
             //Start submission
             $status =fwrite ($bf,start_tag("COMMENT",7,true));
+
             //Print submission contents
             fwrite ($bf,full_tag("ID",8,false,$com_sub->id));
-            fwrite ($bf,full_tag("RECORDID",8,false,$com_sub->recordid));
             fwrite ($bf,full_tag("USERID",8,false,$com_sub->userid));
             fwrite ($bf,full_tag("CONTENT",8,false,$com_sub->content));
-            fwrite ($bf,full_tag("CREATED",8,false,$com_sub->created));
-            fwrite ($bf,full_tag("MODIFIED",8,false,$com_sub->modified));
+            fwrite ($bf,full_tag("COMMENTAREA",8,false,'database_entry'));
+            fwrite ($bf,full_tag("FORMAT",8,false,$com_sub->format));
+            fwrite ($bf,full_tag("TIMECREATED",8,false,$com_sub->timecreated));
             //End submission
+
             $status =fwrite ($bf,end_tag("COMMENT",7,true));
         }
         //Write end tag
index b870327d4da4486a43aa49115212a6be249c81b9..f9b3fde706927c20b70adc16620066c8ea151fcd 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" ?>
-<XMLDB PATH="mod/data/db" VERSION="20090420" COMMENT="XMLDB file for Moodle mod/data"
+<XMLDB PATH="mod/data/db" VERSION="20091006" COMMENT="XMLDB file for Moodle mod/data"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
 >
@@ -86,7 +86,7 @@
         <KEY NAME="dataid" TYPE="foreign" FIELDS="dataid" REFTABLE="data" REFFIELDS="id" PREVIOUS="primary"/>
       </KEYS>
     </TABLE>
-    <TABLE NAME="data_content" COMMENT="the content introduced in each record/fields" PREVIOUS="data_records" NEXT="data_comments">
+    <TABLE NAME="data_content" COMMENT="the content introduced in each record/fields" PREVIOUS="data_records" NEXT="data_ratings">
       <FIELDS>
         <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="fieldid"/>
         <FIELD NAME="fieldid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="recordid"/>
         <KEY NAME="fieldid" TYPE="foreign" FIELDS="fieldid" REFTABLE="data_fields" REFFIELDS="id" PREVIOUS="recordid"/>
       </KEYS>
     </TABLE>
-    <TABLE NAME="data_comments" COMMENT="to comment data records" PREVIOUS="data_content" NEXT="data_ratings">
-      <FIELDS>
-        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="userid"/>
-        <FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="recordid"/>
-        <FIELD NAME="recordid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="userid" NEXT="content"/>
-        <FIELD NAME="content" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" PREVIOUS="recordid" NEXT="format"/>
-        <FIELD NAME="format" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="content" NEXT="created"/>
-        <FIELD NAME="created" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="format" NEXT="modified"/>
-        <FIELD NAME="modified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="created"/>
-      </FIELDS>
-      <KEYS>
-        <KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="recordid"/>
-        <KEY NAME="recordid" TYPE="foreign" FIELDS="recordid" REFTABLE="data_records" REFFIELDS="id" PREVIOUS="primary"/>
-      </KEYS>
-    </TABLE>
-    <TABLE NAME="data_ratings" COMMENT="to rate data records" PREVIOUS="data_comments">
+    <TABLE NAME="data_ratings" COMMENT="to rate data records" PREVIOUS="data_content">
       <FIELDS>
         <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="userid"/>
         <FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="recordid"/>
index d6cc255c6a16a72810030bd1ce2127fbceb4f4ba..d905c9e44bfff09e7cbea64c023283edaaba64cc 100644 (file)
@@ -180,6 +180,53 @@ function xmldb_data_upgrade($oldversion) {
         upgrade_mod_savepoint($result, 2009042000, 'data');
     }
 
+    if ($result && $oldversion < 2009111700) {
+        require_once($CFG->libdir . '/commentlib.php');
+    /// Define table data_comments to be dropped
+        $table = new xmldb_table('data_comments');
+
+    /// Conditionally launch drop table for data_comments
+        if ($dbman->table_exists($table)) {
+            $sql = 'SELECT d.id AS dataid,
+                d.course AS courseid,
+                r.id AS itemid,
+                c.content AS comment,
+                c.format AS format,
+                c.created AS timemodified
+                FROM {data_comments} c, {data_records} r, {data} d
+                WHERE c.recordid=r.id AND r.dataid=d.id';
+        /// move data comments to new comments table
+            if ($rs = $DB->get_recordset_sql($sql)) {
+                $error = false;
+                foreach($rs as $res) {
+                    if ($cm = get_coursemodule_from_instance('data', $res->dataid, $res->courseid)) {
+                        $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+                        $cmt = new stdclass;
+                        $cmt->contextid = $context->id;
+                        $cmt->courseid  = $res->courseid;
+                        $cmt->area      = 'database_entry';
+                        $cmt->itemid    = $res->itemid;
+                        $comment = new comment($cmt);
+                        try {
+                            $cmt = $comment->add($res->comment, $res->format);
+                        } catch (comment_exception $e) {
+                            add_to_log($res->courseid, 'comments', 'add', '', 'Cannot migrate data module comment with ID# '.$res->old_id);
+                            $error = true;
+                        }
+                    }
+                }
+            }
+            if (empty($error)) {
+                $dbman->drop_table($table);
+            } else {
+                print_error('cannotmigratedatacomments');
+            }
+        }
+
+    /// data savepoint reached
+        upgrade_mod_savepoint($result, 2009111700, 'data');
+    }
     return $result;
 }
 
index 3dbda9a4691f6efca0daa385e4e3fe5b9ed94522..0c2de57a47557dbe275d99d87a4c7b8b946d77dc 100755 (executable)
@@ -1139,8 +1139,8 @@ function data_get_participants($dataid) {
                                       WHERE r.dataid = ? AND u.id = r.userid", array($dataid));
 
     $comments = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
-                                        FROM {user} u, {data_records} r, {data_comments} c
-                                       WHERE r.dataid = ? AND u.id = r.userid AND r.id = c.recordid", array($dataid));
+                                        FROM {user} u, {data_records} r, {comments} c
+                                       WHERE r.dataid = ? AND u.id = r.userid AND r.id = c.itemid AND c.commentarea='database_entry'", array($dataid));
 
     $ratings = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
                                        FROM {user} u, {data_records} r, {data_ratings} a
@@ -1277,8 +1277,18 @@ function data_print_template($template, $records, $data, $search='', $page=0, $r
 
         $patterns[]='##comments##';
         if (($template == 'listtemplate') && ($data->comments)) {
-            $comments = $DB->count_records('data_comments', array('recordid'=>$record->id));
-            $replacement[] = '<a href="view.php?rid='.$record->id.'#comments">'.get_string('commentsn','data', $comments).'</a>';
+
+            if (!empty($CFG->usecomments)) {
+                require_once($CFG->libdir . '/commentlib.php');
+                $cmt = new stdclass;
+                $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
+                $cmt->area    = 'database_entry';
+                $cmt->context = $context;
+                $cmt->itemid  = $record->id;
+                $cmt->showcount = true;
+                $comment = new comment($cmt);
+                $replacement[] = $comment->init(true);
+            }
         } else {
             $replacement[] = '';
         }
@@ -1304,8 +1314,17 @@ function data_print_template($template, $records, $data, $search='', $page=0, $r
              *    Printing Ratings Form       *
              *********************************/
             if (($template == 'singletemplate') && ($data->comments)) {    //prints ratings options
-
-                data_print_comments($data, $record, $page);
+                if (!empty($CFG->usecomments)) {
+                    require_once($CFG->libdir . '/commentlib.php');
+                    $cmt = new stdclass;
+                    $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
+                    $cmt->area    = 'database_entry';
+                    $cmt->context = $context;
+                    $cmt->itemid  = $record->id;
+                    $cmt->showcount = true;
+                    $comment = new comment($cmt);
+                    $comment->init(false);
+                }
             }
         }
     }
@@ -1668,120 +1687,6 @@ function data_get_ratings($recordid, $sort="u.firstname ASC") {
                                ORDER BY $sort", array($recordid));
 }
 
-/**
- * Prints all comments + a text box for adding additional comment
- *
- * @global object
- * @global object
- * @param object $data
- * @param object $record
- * @param int $page
- * @param bool $mform
- * @return void Output is echo'd
- */
-function data_print_comments($data, $record, $page=0, $mform=false) {
-    global $CFG, $DB;
-
-    $cm = get_coursemodule_from_instance('data', $data->id);
-    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-    $cancomment = has_capability('mod/data:comment', $context);
-    echo '<a name="comments"></a>';
-
-    if ($comments = $DB->get_records('data_comments', array('recordid'=>$record->id))) {
-        foreach ($comments as $comment) {
-            data_print_comment($data, $comment, $page);
-        }
-        echo '<br />';
-    }
-
-    if (!isloggedin() or has_capability('moodle/legacy:guest', get_context_instance(CONTEXT_SYSTEM), 0, false) or !$cancomment) {
-        return;
-    }
-
-    $editor = optional_param('addcomment', 0, PARAM_BOOL);
-
-    if (!$mform and !$editor) {
-        echo '<div class="newcomment" style="text-align:center">';
-        echo '<a href="view.php?d='.$data->id.'&amp;rid='.$record->id.'&amp;mode=single&amp;addcomment=1">'.get_string('addcomment', 'data').'</a>';
-        echo '</div>';
-    } else {
-        if (!$mform) {
-            require_once('comment_form.php');
-            $mform = new mod_data_comment_form('comment.php');
-            $mform->set_data(array('mode'=>'add', 'page'=>$page, 'rid'=>$record->id));
-        }
-        echo '<div class="newcomment" style="text-align:center">';
-        $mform->display();
-        echo '</div>';
-    }
-}
-
-/**
- * prints a single comment entry
- *
- * @global object
- * @global object
- * @global object
- * @uses CONTEXT_MODULE
- * @param object $data
- * @param string $comment
- * @param int $page
- * @return void Output is echo'd
- */
-function data_print_comment($data, $comment, $page=0) {
-    global $USER, $CFG, $DB, $OUTPUT;
-
-    $cm = get_coursemodule_from_instance('data', $data->id);
-    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-
-    $stredit = get_string('edit');
-    $strdelete = get_string('delete');
-
-    $user = $DB->get_record('user', array('id'=>$comment->userid));
-
-    echo '<table cellspacing="0" align="center" width="50%" class="datacomment forumpost">';
-
-    echo '<tr class="header"><td class="picture left">';
-    echo $OUTPUT->user_picture(moodle_user_picture::make($user, $data->course));
-    echo '</td>';
-
-    echo '<td class="topic starter" align="left"><div class="author">';
-    $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
-    $by = new object();
-    $by->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.
-                $user->id.'&amp;course='.$data->course.'">'.$fullname.'</a>';
-    $by->date = userdate($comment->modified);
-    print_string('bynameondate', 'data', $by);
-    echo '</div></td></tr>';
-
-    echo '<tr><td class="left side">';
-    if ($groups = groups_get_all_groups($data->course, $comment->userid, $cm->groupingid)) {
-        print_group_picture($groups, $data->course, false, false, true);
-    } else {
-        echo '&nbsp;';
-    }
-
-// Actual content
-
-    echo '</td><td class="content" align="left">'."\n";
-
-    // Print whole message
-    echo format_text($comment->content, $comment->format);
-
-// Commands
-
-    echo '<div class="commands">';
-    if (data_isowner($comment->recordid) or has_capability('mod/data:managecomments', $context)) {
-            echo '<a href="'.$CFG->wwwroot.'/mod/data/comment.php?rid='.$comment->recordid.'&amp;mode=edit&amp;commentid='.$comment->id.'&amp;page='.$page.'">'.$stredit.'</a>';
-            echo '| <a href="'.$CFG->wwwroot.'/mod/data/comment.php?rid='.$comment->recordid.'&amp;mode=delete&amp;commentid='.$comment->id.'&amp;page='.$page.'">'.$strdelete.'</a>';
-    }
-
-    echo '</div>';
-
-    echo '</td></tr></table>'."\n\n";
-}
-
-
 /**
  * For Participantion Reports
  *
@@ -2576,7 +2481,7 @@ function data_reset_userdata($data) {
     // delete entries if requested
     if (!empty($data->reset_data)) {
         $DB->delete_records_select('data_ratings', "recordid IN ($allrecordssql)", array($data->courseid));
-        $DB->delete_records_select('data_comments', "recordid IN ($allrecordssql)", array($data->courseid));
+        $DB->delete_records_select('comments', "itemid IN ($allrecordssql) AND commentarea='database_entry'", array($data->courseid));
         $DB->delete_records_select('data_content', "recordid IN ($allrecordssql)", array($data->courseid));
         $DB->delete_records_select('data_records', "dataid IN ($alldatassql)", array($data->courseid));
 
@@ -2609,7 +2514,7 @@ function data_reset_userdata($data) {
                 if (array_key_exists($record->userid, $notenrolled) or !$record->userexists or $record->userdeleted
                   or !has_capability('moodle/course:view', $course_context , $record->userid)) {
                     $DB->delete_records('data_ratings', array('recordid'=>$record->id));
-                    $DB->delete_records('data_comments', array('recordid'=>$record->id));
+                    $DB->delete_records('comments', array('itemid'=>$record->id, 'commentarea'=>'database_entry'));
                     $DB->delete_records('data_content', array('recordid'=>$record->id));
                     $DB->delete_records('data_records', array('id'=>$record->id));
                     // HACK: this is ugly - the recordid should be before the fieldid!
@@ -2645,7 +2550,7 @@ function data_reset_userdata($data) {
 
     // remove all comments
     if (!empty($data->reset_data_comments)) {
-        $DB->delete_records_select('data_comments', "recordid IN ($allrecordssql)", array($data->courseid));
+        $DB->delete_records_select('comments', "itemid IN ($allrecordssql) AND commentarea='database_entry'", array($data->courseid));
         $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallcomments'), 'error'=>false);
     }
 
index 59f05befb813b59fe1cd5a7761d9a2d3583de787..9b76d31e23a33f9529c71eea2edc3f5dbdfa7daf 100644 (file)
@@ -265,7 +265,7 @@ function data_records_restore_mods ($old_data_id, $new_data_id, $info, $restore)
 
             $status = $status and data_content_restore_mods ($oldid, $newid, $old_data_id, $new_data_id, $rec_info, $restore);
             $status = $status and data_ratings_restore_mods ($oldid, $newid, $info, $rec_info);
-            $status = $status and data_comments_restore_mods ($oldid, $newid, $info, $rec_info);
+            $status = $status and data_comments_restore_mods ($oldid, $newid, $old_data_id, $new_data_id, $info, $rec_info);
 
         } else {
             $status = false;
@@ -397,11 +397,13 @@ function data_ratings_restore_mods ($oldid, $newid, $info, $rec_info) {
     return $status;
 }
 
-function data_comments_restore_mods ($oldid, $newid, $info, $rec_info) {
+function data_comments_restore_mods ($oldid, $newid, $old_data_id, $new_data_id, $info, $rec_info) {
     global $CFG, $DB;
 
     $status = true;
 
+    $newmodcontext = restore_get_new_context($restore, 'course_modules', CONTEXT_MODULE, $old_gid);
+
     $comments= $rec_info['#']['COMMENTS']['0']['#']['COMMENT'];
 
     if (empty($comments)) { // no comments to restore
@@ -412,12 +414,17 @@ function data_comments_restore_mods ($oldid, $newid, $info, $rec_info) {
 
         $com_info = $comments[$i];
 
-        $comment -> recordid = $newid;
-        $comment -> userid = backup_todb($com_info['#']['USERID']['0']['#']);
-        $comment -> content = backup_todb($com_info['#']['CONTENT']['0']['#']);
-        $comment -> created = backup_todb($com_info['#']['CREATED']['0']['#']);
-        $comment -> modified = backup_todb($com_info['#']['MODIFIED']['0']['#']);
-        $DB->insert_record("data_comments", $comment);
+        $comment->itemid = $newid;
+        $comment->contextid = $newmodcontext->id;
+        $comment->userid = backup_todb($com_info['#']['USERID']['0']['#']);
+        $comment->content = backup_todb($com_info['#']['CONTENT']['0']['#']);
+        $comment->commentarea = backup_todb($com_info['#']['COMMENTAREA']['0']['#']);
+        $comment->timecreated = backup_todb($com_info['#']['TIMECREATED']['0']['#']);
+        $user = backup_getid($restore->backup_unique_code,"user",$comment->userid);
+        if ($user) {
+            $comment->userid = $user->new_id;
+        }
+        $DB->insert_record("comments", $comment);
     }
     return $status;
 
index ae50653944fc008b9735c23256af51001269e9eb..518cfd1b922a11d053b292ce4802c9f0426cc627 100644 (file)
@@ -5,7 +5,7 @@
 //  This fragment is called by /admin/index.php
 ////////////////////////////////////////////////////////////////////////////////
 
-$module->version  = 2009042000;
+$module->version  = 2009111700;
 $module->requires = 2009041700;  // Requires this Moodle version
 $module->cron     = 60;
 
index 247e2013600e3e0cd6e4c78b703fe2c9197d7fe6..797017241704a4d9bfc60ecc794e128b57408fc9 100755 (executable)
@@ -81,6 +81,9 @@
 
     require_course_login($course, true, $cm);
 
+    require_once($CFG->libdir . '/commentlib.php');
+    comment::js();
+
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
     require_capability('mod/data:viewentry', $context);