COMMENT MDL-23585 delete comments when centain contexts are being deleted
authorDongsheng Cai <unoter@gmail.com>
Mon, 2 Aug 2010 07:44:45 +0000 (07:44 +0000)
committerDongsheng Cai <unoter@gmail.com>
Mon, 2 Aug 2010 07:44:45 +0000 (07:44 +0000)
comment/lib.php
lib/moodlelib.php
mod/data/lib.php
mod/glossary/lib.php

index 5cde7d3..3e9ee5f 100644 (file)
@@ -518,7 +518,7 @@ EOD;
             $newcmt->fullname = fullname($USER);
             $url = new moodle_url('/user/view.php', array('id'=>$USER->id, 'course'=>$this->courseid));
             $newcmt->profileurl = $url->out();
             $newcmt->fullname = fullname($USER);
             $url = new moodle_url('/user/view.php', array('id'=>$USER->id, 'course'=>$this->courseid));
             $newcmt->profileurl = $url->out();
-            $newcmt->content = format_text($newcmt->content);
+            $newcmt->content = format_text($newcmt->content, $format);
             $newcmt->avatar = $OUTPUT->user_picture($USER, array('size'=>16));
             return $newcmt;
         } else {
             $newcmt->avatar = $OUTPUT->user_picture($USER, array('size'=>16));
             return $newcmt;
         } else {
@@ -528,15 +528,20 @@ EOD;
 
     /**
      * delete by context, commentarea and itemid
 
     /**
      * delete by context, commentarea and itemid
-     *
+    * @param object $param {
+    *            contextid => int the context in which the comments exist [required]
+    *            commentarea => string the comment area [optional]
+    *            itemid => int comment itemid [optional]
+    * }
+     * @return boolean
      */
      */
-    public function delete_comments() {
+    public function delete_comments($param) {
         global $DB;
         global $DB;
-        $DB->delete_records('comments', array(
-            'contextid'=>$this->context->id,
-            'commentarea'=>$this->commentarea,
-            'itemid'=>$this->itemid)
-        );
+        $param = (array)$param;
+        if (empty($param['contextid'])) {
+            return false;
+        }
+        $DB->delete_records('comments', $param);
         return true;
     }
 
         return true;
     }
 
index e295c85..06b4318 100644 (file)
@@ -4096,6 +4096,10 @@ function remove_course_contents($courseid, $showfeedback=true) {
     require_once($CFG->dirroot.'/tag/coursetagslib.php');
     coursetag_delete_course_tags($course->id, $showfeedback);
 
     require_once($CFG->dirroot.'/tag/coursetagslib.php');
     coursetag_delete_course_tags($course->id, $showfeedback);
 
+/// Delete comments
+    require_once($CFG->dirroot.'/comment/lib.php');
+    comment::delete_comments(array('contextid'=>$context->id));
+
     // Delete legacy files
     fulldelete($CFG->dataroot.'/'.$courseid);
 
     // Delete legacy files
     fulldelete($CFG->dataroot.'/'.$courseid);
 
index 6ca7cdb..56f8253 100755 (executable)
@@ -889,7 +889,7 @@ function data_update_instance($data) {
  * @return bool
  */
 function data_delete_instance($id) {    // takes the dataid
  * @return bool
  */
 function data_delete_instance($id) {    // takes the dataid
-    global $DB;
+    global $DB, $CFG;
 
     if (!$data = $DB->get_record('data', array('id'=>$id))) {
         return false;
 
     if (!$data = $DB->get_record('data', array('id'=>$id))) {
         return false;
@@ -904,6 +904,10 @@ function data_delete_instance($id) {    // takes the dataid
     $fs = get_file_storage();
     $fs->delete_area_files($context->id, 'mod_data');
 
     $fs = get_file_storage();
     $fs->delete_area_files($context->id, 'mod_data');
 
+    // Delete comments
+    require_once($CFG->dirroot.'/comment/lib.php');
+    comment::delete_comments(array('contextid'=>$context->id));
+
     // get all the records in this data
     $sql = "SELECT r.id
               FROM {data_records} r
     // get all the records in this data
     $sql = "SELECT r.id
               FROM {data_records} r
index 8ba490c..22ef1fb 100644 (file)
@@ -135,7 +135,7 @@ function glossary_update_instance($glossary) {
  * @return bool success
  */
 function glossary_delete_instance($id) {
  * @return bool success
  */
 function glossary_delete_instance($id) {
-    global $DB;
+    global $DB, $CFG;
 
     if (!$glossary = $DB->get_record('glossary', array('id'=>$id))) {
         return false;
 
     if (!$glossary = $DB->get_record('glossary', array('id'=>$id))) {
         return false;
@@ -195,6 +195,10 @@ function glossary_delete_instance($id) {
     $DB->delete_records_select('glossary_entries_categories', "categoryid IN ($category_select)", array($id));
     $DB->delete_records('glossary_categories', array('glossaryid'=>$id));
 
     $DB->delete_records_select('glossary_entries_categories', "categoryid IN ($category_select)", array($id));
     $DB->delete_records('glossary_categories', array('glossaryid'=>$id));
 
+    // Delete comments
+    require_once($CFG->dirroot.'/comment/lib.php');
+    comment::delete_comments(array('contextid'=>$context->id));
+
     // delete all files
     $fs->delete_area_files($context->id);
 
     // delete all files
     $fs->delete_area_files($context->id);