From e998effa3d7cf53d1003f11aaee3c34859a040cb Mon Sep 17 00:00:00 2001 From: Dongsheng Cai Date: Wed, 18 Nov 2009 06:00:48 +0000 Subject: [PATCH] "MDL-20346, comments api for data module" --- lang/en_utf8/error.php | 1 + mod/data/backuplib.php | 25 +++++-- mod/data/db/install.xml | 21 +----- mod/data/db/upgrade.php | 47 +++++++++++++ mod/data/lib.php | 151 ++++++++-------------------------------- mod/data/restorelib.php | 23 +++--- mod/data/version.php | 2 +- mod/data/view.php | 3 + 8 files changed, 119 insertions(+), 154 deletions(-) diff --git a/lang/en_utf8/error.php b/lang/en_utf8/error.php index a1c133098d..4c8809ffb0 100644 --- a/lang/en_utf8/error.php +++ b/lang/en_utf8/error.php @@ -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'; diff --git a/mod/data/backuplib.php b/mod/data/backuplib.php index 4ad6ebbb0c..111f1d4c77 100644 --- a/mod/data/backuplib.php +++ b/mod/data/backuplib.php @@ -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 diff --git a/mod/data/db/install.xml b/mod/data/db/install.xml index b870327d4d..f9b3fde706 100644 --- a/mod/data/db/install.xml +++ b/mod/data/db/install.xml @@ -1,5 +1,5 @@ - @@ -86,7 +86,7 @@ - +
@@ -103,22 +103,7 @@
- - - - - - - - - - - - - - -
- +
diff --git a/mod/data/db/upgrade.php b/mod/data/db/upgrade.php index d6cc255c6a..d905c9e44b 100644 --- a/mod/data/db/upgrade.php +++ b/mod/data/db/upgrade.php @@ -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; } diff --git a/mod/data/lib.php b/mod/data/lib.php index 3dbda9a469..0c2de57a47 100755 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -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[] = ''.get_string('commentsn','data', $comments).''; + + 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 ''; - - if ($comments = $DB->get_records('data_comments', array('recordid'=>$record->id))) { - foreach ($comments as $comment) { - data_print_comment($data, $comment, $page); - } - echo '
'; - } - - 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 ''; - } 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 '
'; - $mform->display(); - echo '
'; - } -} - -/** - * 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 '
'; - - echo ''; - - echo ''; - - echo '
'; - echo $OUTPUT->user_picture(moodle_user_picture::make($user, $data->course)); - echo '
'; - $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context)); - $by = new object(); - $by->name = ''.$fullname.''; - $by->date = userdate($comment->modified); - print_string('bynameondate', 'data', $by); - echo '
'; - if ($groups = groups_get_all_groups($data->course, $comment->userid, $cm->groupingid)) { - print_group_picture($groups, $data->course, false, false, true); - } else { - echo ' '; - } - -// Actual content - - echo ''."\n"; - - // Print whole message - echo format_text($comment->content, $comment->format); - -// Commands - - echo '
'; - if (data_isowner($comment->recordid) or has_capability('mod/data:managecomments', $context)) { - echo ''.$stredit.''; - echo '| '.$strdelete.''; - } - - echo '
'; - - echo '
'."\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); } diff --git a/mod/data/restorelib.php b/mod/data/restorelib.php index 59f05befb8..9b76d31e23 100644 --- a/mod/data/restorelib.php +++ b/mod/data/restorelib.php @@ -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; diff --git a/mod/data/version.php b/mod/data/version.php index ae50653944..518cfd1b92 100644 --- a/mod/data/version.php +++ b/mod/data/version.php @@ -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; diff --git a/mod/data/view.php b/mod/data/view.php index 247e201360..7970172417 100755 --- a/mod/data/view.php +++ b/mod/data/view.php @@ -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); -- 2.39.5