How to use the Moodle comment API?

How to use the Moodle comment API?

by Winfried Schüler -
Number of replies: 6

Hello Moodle-Developers,


I am trying to integrate the moodle comment-functionality to an local plugin. I was using the code snippets from the Moodle documentation here: https://docs.moodle.org/dev/Comment_API. It sound so easy, but unfortunately the reality isn't quite so simple as the theory. Also I am a moodle beginner. In my DB there is also an comment table, just like the one in the documentation, but the column order is a bit different.

Here is my Code so far (the red marked lines are my changes): 



Without my changes in the Code, the page looks like this in the browser:



When I try to load my page with my red marked changes, I run into some errors:

1. Notice: Undefined variable: context in C:\xampp\htdocs\moodle2_9\local\alv\views\request_view.php on line 54

2. Notice: Undefined variable: record in C:\xampp\htdocs\moodle2_9\local\alv\views\request_view.php on line 55

3. Notice: Trying to get property of non-object in C:\xampp\htdocs\moodle2_9\local\alv\views\request_view.php on line 55

4. 


With what do I fill the variables Context & Record? There is nothing explained in the documentation of the comment API. Am I doing something wrong? 


I am thankful for any advice! smile

Average of ratings: -
In reply to Winfried Schüler

Re: How to use the Moodle comment API?

by Winfried Schüler -

An working example would be really helpful smile

In reply to Winfried Schüler

Re: How to use the Moodle comment API?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Well, how about:

https://github.com/moodle/moodle/tree/master/blocks/comments

https://github.com/moodle/moodle/tree/master/blog

https://github.com/moodle/moodle/tree/master/mod/assign/submission/comments

All of these make use of the comments API (in addition there is also mod_data, mod_glossary and mod_wiki).


Average of ratings: Useful (1)
In reply to Davo Smith

Re: How to use the Moodle comment API?

by Winfried Schüler -

First of all, thank you very much Davo Smith for your quick answer. Of course I also checked Google, Youtube and else for examples first. But as Xavier de Pauw already said in 2014: "The Comment API is a little vague and I'm having problems understanding how to use it" (https://moodle.org/mod/forum/discuss.php?d=262056)...


I took a look into your examples, if I am not wrong, none of them is using the moodle native comment API (or am I wrong?). I was just looking for "normal" and easy comments. The code from Xavier de Pauw seems to be really near by the one of the comment API documentation: https://moodle.org/mod/forum/discuss.php?d=262056. Sadly it doesn't work sad

I will continue and try your examples Davo Smith, but I am still thankfull for any further help.

In reply to Winfried Schüler

Re: How to use the Moodle comment API?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

To be clear - all of the items I link to make use of the Moodle comment API (I just asked my IDE for a list of all the places that instantiated the 'comment' class).

I'd guess that block_comment is probably the simplest starting place.


In reply to Davo Smith

Re: How to use the Moodle comment API?

by Winfried Schüler -

Yes, the code from block_comment is definitly easier than the others. Since yesterday I am trying to use the block_comments class, I tried it different ways. Now I pasted the code into my request_view.php (under my form class) and made an instance, but no comment field is showing up.


<?php
...

class block_comments extends block_base { 
		function init() {
			$this->title = get_string('pluginname', 'block_comments');
		}
		function specialization() {
			// require js for commenting
			comment::init();
		}
		function applicable_formats() {
			return array('all' => true);
		}
		function instance_allow_multiple() {
			return false;
		}
		function get_content() {
			global $CFG, $PAGE;
			if ($this->content !== NULL) {
				return $this->content;
			}
			if (!$CFG->usecomments) {
				$this->content = new stdClass();
				$this->content->text = '';
				if ($this->page->user_is_editing()) {
					$this->content->text = get_string('disabledcomments');
				}
				return $this->content;
			}
			$this->content = new stdClass();
			$this->content->footer = '';
			$this->content->text = '';
			if (empty($this->instance)) {
				return $this->content;
			}
			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;
			$args->component = 'block_comments';
			$args->linktext  = get_string('showcomments');
			$args->notoggle  = true;
			$args->autostart = true;
			$args->displaycancel = false;
			$comment = new comment($args);
			$comment->set_view_permission(true);
			$comment->set_fullwidth();
			$this->content = new stdClass();
			$this->content->text = $comment->output(true);
			$this->content->footer = '';
			return $this->content;
		}
	}
	// Make an comment instance
	$blockcomments = new block_comments();
	echo var_dump($blockcomments);
	$blockcomments->init();
	$blockcomments->specialization();
	$blockcomments->get_content(); 
	
        // Make an form instance
	echo "<h1>Leihantrag stellen</h1>";
	$form = new request_form($moodle_url . "&test=true", $moodle_url, $devices);
	$form->display();
?>


Am I doing something wrong or do I need all the other files too? (If I need all the other files....why there are so less hints in the moodle comment API documentation?) smile

In reply to Winfried Schüler

Re: How to use the Moodle comment API?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

You also need to implement the functions in:

https://github.com/moodle/moodle/blob/master/blocks/comments/lib.php

These are the callbacks used by Moodle to check whether the current user can read/write comments (generally for the plugin), as well as, for any given comment, whether they are allowed to read/write it.

The functions will need to go in your plugins 'lib.php' file and will need to be named with the prefix 'PLUGINTYPE_PLUGINNAME_', being the correct name of your plugin, rather than 'block_comments_'.