"MDL-23917, display comments for not logged in users"
authorDongsheng Cai <unoter@gmail.com>
Wed, 25 Aug 2010 03:50:19 +0000 (03:50 +0000)
committerDongsheng Cai <unoter@gmail.com>
Wed, 25 Aug 2010 03:50:19 +0000 (03:50 +0000)
blocks/comments/block_comments.php
blog/locallib.php
comment/comment_ajax.php
comment/lib.php
lib/moodlelib.php
mod/data/lib.php
mod/glossary/lib.php
mod/wiki/locallib.php

index f855ad3..4caa8c8 100644 (file)
@@ -37,22 +37,22 @@ class block_comments extends block_base {
         }
         $this->content->footer = '';
         $this->content->text = '';
-        if (isloggedin() && !isguestuser()) {
-            list($context, $course, $cm) = get_context_info_array($PAGE->context->id);
-            $args = new stdclass;
-            $args->context   = $PAGE->context;
-            $args->course    = $course;
-            $args->area      = 'page_comments';
-            $args->itemid    = 0;
-            // set 'env' to tell moodle tweak ui for this block
-            $args->env       = 'block_comments';
-            $args->linktext  = get_string('showcomments');
-            $comment = new comment($args);
+        list($context, $course, $cm) = get_context_info_array($PAGE->context->id);
+        $args = new stdclass;
+        $args->context   = $PAGE->context;
+        $args->course    = $course;
+        $args->area      = 'page_comments';
+        $args->itemid    = 0;
+        // set 'env' to tell moodle tweak ui for this block
+        $args->env       = 'block_comments';
+        $args->component = 'block_comments';
+        $args->linktext  = get_string('showcomments');
+        $comment = new comment($args);
+        $comment->set_view_permission(true);
 
-            $this->content = new stdClass;
-            $this->content->text = $comment->output(true);
-            $this->content->footer = '';
-        }
+        $this->content = new stdClass;
+        $this->content->text = $comment->output(true);
+        $this->content->footer = '';
         return $this->content;
     }
     function instance_delete() {
index cebde0b..e1cfc68 100644 (file)
@@ -106,9 +106,9 @@ class blog_entry {
             $cmt->context = get_context_instance(CONTEXT_USER, $user->id);
             $cmt->courseid = $PAGE->course->id;
             $cmt->area = 'format_blog';
-            $cmt->env = 'blog';
             $cmt->itemid = $this->id;
             $cmt->showcount = $CFG->blogshowcommentscount;
+            $cmt->component = 'blog';
             $comment = new comment($cmt);
             $cmttext = $comment->output(true);
         }
index bb69d80..8aeb4bc 100644 (file)
@@ -29,7 +29,12 @@ list($context, $course, $cm) = get_context_info_array($contextid);
 $PAGE->set_context($context);
 $PAGE->set_url('/comment/comment_ajax.php');
 
-require_login($course, true, $cm);
+// XXX: display comments in frontpage without login
+if ($context->id != get_context_instance(CONTEXT_COURSE, SITEID)->id
+    or $action == 'add'
+    or $action == 'delete') {
+        require_login($course, true, $cm);
+}
 require_sesskey();
 
 $action    = optional_param('action',    '', PARAM_ALPHA);
index ab154fb..12a3c37 100644 (file)
@@ -85,7 +85,7 @@ class comment {
     private static $comment_itemid = null;
     private static $comment_context = null;
     private static $comment_area = null;
-       private static $comment_page = null;
+    private static $comment_page = null;
     /**
      * Construct function of comment class, initialise
      * class members
@@ -119,6 +119,10 @@ class comment {
             print_error('invalidcontext');
         }
 
+        if (!empty($options->component)) {
+            $this->set_component($options->component);
+        }
+
         // setup course
         // course will be used to generate user profile link
         if (!empty($options->course)) {
@@ -129,10 +133,6 @@ class comment {
             $this->courseid = SITEID;
         }
 
-        if (!empty($options->pluginname)) {
-            $this->pluginname = $options->pluginname;
-        }
-
         // setup coursemodule
         if (!empty($options->cm)) {
             $this->cm = $options->cm;
@@ -165,8 +165,6 @@ class comment {
         } else {
             $this->linktext = get_string('comments');
         }
-        // setting post and view permissions
-        $this->check_permissions();
 
         if (!empty($options->showcount)) {
             $count = $this->count();
@@ -179,8 +177,6 @@ class comment {
             $this->count = '';
         }
 
-        $this->setup_plugin();
-
         // setup options for callback functions
         $this->args = new stdclass;
         $this->args->context     = $this->context;
@@ -189,6 +185,9 @@ class comment {
         $this->args->commentarea = $this->commentarea;
         $this->args->itemid      = $this->itemid;
 
+        // setting post and view permissions
+        $this->check_permissions();
+
         // load template
         $this->template = <<<EOD
 <div class="comment-userpicture">___picture___</div>
@@ -221,40 +220,17 @@ EOD;
         $PAGE->requires->string_for_js('comments', 'moodle');
     }
 
-    /**
-     * Setup plugin type and plugin name
-     */
-    private function setup_plugin() {
-        global $DB;
-        // blog needs to set env as "blog"
-        if ($this->env == 'blog') {
-            $this->plugintype = 'moodle';
-            $this->pluginname = 'blog';
-        }
-        // tag page needs to set env as "tag"
-        if ($this->env == 'tag') {
-            $this->plugintype = 'moodle';
-            $this->pluginname = 'tag';
-        }
-        if ($this->context->contextlevel == CONTEXT_BLOCK) {
-            if ($block = $DB->get_record('block_instances', array('id'=>$this->context->instanceid))) {
-                $this->plugintype = 'block';
-                $this->pluginname = $block->blockname;
-            }
-        }
+    public function set_component($component) {
+        list($this->plugintype, $this->pluginname) = normalize_component($component);
+        return null;
+    }
 
-        if ($this->context->contextlevel == CONTEXT_MODULE && $this->env != 'block_comments') {
-            $this->plugintype = 'mod';
-            // to improve performance, pluginname should be assigned before initilise comment object
-            // if it is empty, we will try to guess, it will rarely be used.
-            if (empty($this->pluginname)) {
-                if (empty($this->course)) {
-                    $this->course = $DB->get_record('course', array('id'=>$this->courseid), '*', MUST_EXIST);
-                }
-                $this->modinfo = get_fast_modinfo($this->course);
-                $this->pluginname = $this->modinfo->cms[$this->cm->id]->modname;
-            }
-        }
+    public function set_view_permission($value) {
+        $this->viewcap = $value;
+    }
+
+    public function set_post_permission($value) {
+        $this->postcap = $value;
     }
 
     /**
@@ -268,7 +244,7 @@ EOD;
         $this->postcap = has_capability('moodle/comment:post', $this->context);
         $this->viewcap = has_capability('moodle/comment:view', $this->context);
         if (!empty($this->plugintype)) {
-            $permissions = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'permissions', $this->args, array('post'=>true, 'view'=>true));
+            $permissions = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'permissions', array($this->args), array('post'=>true, 'view'=>true));
             $this->postcap = $this->postcap && $permissions['post'];
             $this->viewcap = $this->viewcap && $permissions['view'];
         }
@@ -302,7 +278,6 @@ EOD;
         $options->contextid = $this->contextid;
         $options->env = $this->env;
         if ($this->env == 'block_comments') {
-            $options->autostart = true;
             $options->notoggle = true;
         }
 
index 4834879..0894e66 100644 (file)
@@ -7087,32 +7087,13 @@ function plugin_callback($type, $name, $feature, $action, $options = null, $defa
 
     $name = clean_param($name, PARAM_SAFEDIR);
     $function = $name.'_'.$feature.'_'.$action;
-
-    switch($type) {
-        case 'mod' :
-            $file = $CFG->dirroot.'/mod/'.$name.'/lib.php';
-            break;
-        case 'block' :
-            // load block_base class
-            require_once($CFG->dirroot . '/blocks/moodleblock.class.php');
-            // block uses class based callback functions
-            // see blocks/moodleblock.class.php
-            $file = $CFG->dirroot.'/blocks/'.$name.'/block_'.$name.'.php';
-            $function = array('block_' . $name, 'comment_'.$action);
-            break;
-        case 'moodle':
-            // for special plugins, such as blog and tag
-            $file = $CFG->dirroot.'/'.$name.'/lib.php';
-            break;
-        default:
-            throw new Exception('Unsupported callback type ('.$type.')');
-    }
+    $file = get_plugin_directory($type, $name) . '/lib.php';
 
     // Load library and look for function
     if (file_exists($file)) {
         require_once($file);
     }
-    if (is_array($function) || function_exists($function)) {
+    if (function_exists($function)) {
         // Function exists, so just return function result
         $ret = call_user_func_array($function, (array)$options);
         if (is_null($ret)) {
@@ -7120,13 +7101,8 @@ function plugin_callback($type, $name, $feature, $action, $options = null, $defa
         } else {
             return $ret;
         }
-    } else {
-        switch($feature) {
-            // If some features can also be checked in other ways
-            // for legacy support, this could be added here
-            default: return $default;
-        }
     }
+    return $default;
 }
 
 /**
index 2616e20..031ea25 100755 (executable)
@@ -1306,13 +1306,13 @@ function data_print_template($template, $records, $data, $search='', $page=0, $r
                 require_once($CFG->dirroot  . '/comment/lib.php');
                 list($context, $course, $cm) = get_context_info_array($context->id);
                 $cmt = new stdclass;
-                $cmt->pluginname = 'data';
                 $cmt->context = $context;
                 $cmt->course  = $course;
                 $cmt->cm      = $cm;
                 $cmt->area    = 'database_entry';
                 $cmt->itemid  = $record->id;
                 $cmt->showcount = true;
+                $cmt->component = 'mod_data';
                 $comment = new comment($cmt);
                 $replacement[] = $comment->output(true);
             }
@@ -1345,13 +1345,13 @@ function data_print_template($template, $records, $data, $search='', $page=0, $r
                     require_once($CFG->dirroot . '/comment/lib.php');
                     list($context, $course, $cm) = get_context_info_array($context->id);
                     $cmt = new stdclass;
-                    $cmt->pluginname = 'data';
                     $cmt->context = $context;
                     $cmt->course  = $course;
                     $cmt->cm      = $cm;
                     $cmt->area    = 'database_entry';
                     $cmt->itemid  = $record->id;
                     $cmt->showcount = true;
+                    $cmt->component = 'mod_data';
                     $comment = new comment($cmt);
                     $comment->output(false);
                 }
@@ -3091,4 +3091,4 @@ function data_presets_export($course, $cm, $data, $tostorage=false) {
 
     // Return the full path to the exported preset file:
     return $exportfile;
-}
\ No newline at end of file
+}
index c3247f0..c94f6cc 100644 (file)
@@ -1057,7 +1057,7 @@ function glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode='',$h
         if (!empty($CFG->usecomments)) {
             require_once($CFG->dirroot . '/comment/lib.php');
             $cmt = new stdclass;
-            $cmt->pluginname = 'glossary';
+            $cmt->component = 'mod_glossary';
             $cmt->context  = $context;
             $cmt->course   = $course;
             $cmt->cm       = $cm;
index 6922674..c02ed09 100644 (file)
@@ -1106,7 +1106,7 @@ function wiki_add_comment($context, $pageid, $content, $editor) {
     $cmt->itemid = $pageid;
     $cmt->area = 'wiki_page';
     $cmt->course = $course;
-    $cmt->pluginname = 'wiki';
+    $cmt->component = 'mod_wiki';
 
     $manager = new comment($cmt);
 
@@ -1136,8 +1136,8 @@ function wiki_delete_comment($idcomment, $context, $pageid) {
     $cmt->context = $context;
     $cmt->itemid = $pageid;
     $cmt->area = 'wiki_page';
-    $cmt->pluginname = 'wiki';
     $cmt->course = $course;
+    $cmt->component = 'mod_wiki';
 
     $manager = new comment($cmt);
     $manager->delete($idcomment);