コメントAPI
Moodle 2.0
作成中です - Mitsuhiro Yoshida (トーク)
目的
コメントAPIのゴールは以下のとおりです:
- コメントを一元管理する
- Moodle全体を通して、すべてのコメントに一貫したアプローチを使用する
- コメントAPIを既存モジュールに簡単に統合できる
- JavaScriptが有効か否かに関係なく動作する
概要
コメントAPIでは以下の機能を提供します:
- コメントを追加すr
- コメントを管理する
- コメントを削除する
また、ページを再読み込みすることなくコメントを追加/削除するための派手なAjaxインターフェイスも提供します。
コメントAPIデータベーステーブル
フィールド | タイプ | デフォルト | 情報 |
---|---|---|---|
id | int(10) | auto-incrementing | このコメントのユニークID |
userid | int(10) | このコメントの投稿者 | |
contextid | int(10) | コンテクストテーブルで定義された コンテクストID - コメントの所有者であるプラグインのインスタンスを識別します。 | |
itemid | int(10) | プラグイン固有のアイテムID (例: フォーラム投稿、ブログエントリ、課題提出等) | |
commentarea | int(10) | 例えばあなたはユーザプロファイルではユーザ説明または趣味をコメントできますが、これらは同じitemid (==userid) を共有しているため、comment_areaで分離する必要があります。 | |
timecreated | int(10) | ||
timemodified | int(10) | ||
content | text | コメントのコンテンツ |
コメントAPIを使用する
format_text関数にオプションを追加する
このformat_text関数を使用した場合、テキスト末尾にコメントアイコンが自動的に追加されます:
例えばフォーラムモジュールで次のコードを使用した場合、すべての投稿にコメントアイコンが追加されます:
$cmt = new stdclass;
$cmt->contextid = $modcontext->id;
$cmt->area = 'format_post';
$cmt->itemid = $post->id;
$options->comments = $cmt;
echo format_text($post->message, $post->messageformat, $options, $course->id)."<hr />";
コメントクラスを使用する
コメントAPIを他の場所で使用する場合、以下のコードを使用してください:
$options->area = 'database_entry';
$options->context = $context;
$options->itemid = $record->id;
$options->component = 'mod_data';
$options->showcount = true;
$comment = new comment($options);
$comment->output(false);
コメントAPI概要
一般的にコメントAPIを動作させるためにあなたが知る必要のある関数は以下2つのみです:
- comment::init (コメントAPIの初期化に使用する)
- $comment->output (コメントの表示に使用する)
コメントクラスはcomment/lib.phpに実装されています。
class comment()
__construct($contextid, $comment_area, $itemid))
クラスメンバを初期化します。
init()
コメントの初期化および言語の設定に使用される静的関数です。HTMLのヘッダ表示の前に必ず呼び出されます。
output($return = false)
コメントインターフェースのHTMLスニペットをプリントします。$returnにtrueに設定した場合、プリントの代わりにHTML文字列を返します。
print_comments($params = array())
Used by non-javascript comment interface, will print a list of comments. 非JavaScriptのコメントインターフェイスで使用されてコメントリストを表示します。
add($content)
Public instance funciton, add a comment to database, used in comment/comment_ajax.php
count()
Counting the number of comments
delete($id)
Delete a comment from database, used in comment/comment_ajax.php
delete_comments
Delete all comments in a specific contexts (like all comments belonging to a forum post)
Javascript API
Comment API implemented a YUI3 module M.core_comment to deal with the communication between browsers and moodle. It can be found in comment/comment.js
Call M.core_comment.init will create an instance of CommentHelper class. You don't need to make any calls to this instance, it simply works out of box.
Moodle modules callback
Comment API allows modules/blocks/blog to decide how comments display.
データ妥当性検証 Data validation
This callback method is required, it must be implemented. Otherwise, new comment will be rejected by default. Plugins must implement pluginname_comment_validate callback to validate comments parameters, it must return true to pass validation.
パーミッションコントロール Permission control
Modules must implement function pluginname_comment_permissions to return post and view permission.
Blocks need to overwrite blockname_comment_permissions function of block_base.
Blog need to implement blog_comment_permissions function.
This function will return an array: array('post'=>true, 'view'=>true)
新しい追加コメントを確認する Check new added comment
The callback function allows you to change the comment content before inserting into database or reject this comment.
It takes two arguments, the comment object which contains comment details, and $params which contains context and course information.
Modules can implement a function named modname_comment_add.
Blocks need to overwrite blockname_comment_add function.
Blog need to implement blog_comment_add function.
This function should return a boolean value.
コメントをフィルタ/フォーマットする Filter/format comments
This callback allows modules check/format comments when user request to display comments.
It takes the same arguments as pluginname_comment_add
Modules can implement a function named pluginname_comment_display.
Blocks need to overwrite blockname_comment_display function.
Blog need to implement blog_comment_display function.
It will return the comment object.
コメントテンプレートを定義する Define a comment template
Modules can implement a function named pluginname_comment_template, which allow modules define a comment template. The template must have 4 embedding variables, ___id___, ___content___, ___time___, ___name___, they will be replaced with html id, comments content, comment time and commenter name
バックアップおよびリストア Backup and Restore
Comments will automatically be included in the activity/course backups and restored provided the itemids can be updated during the restore process. To do this either:
- do not use item ids and rely just on the context
- make the commentarea the same as one of the mappings in the restore (set_mapping); or
- override the function "get_comment_mapping_itemname" in the restore activity class
If you do not do one of these the comments will be silently ignored on restore.
関連情報
- コアAPI (Dev_docs)
- MDL-19118 - Comment API issue