フォーラムへの投稿のタグ機能について

フォーラムへの投稿のタグ機能について

- Yosuke Tanaka の投稿
返信数: 16

フォーラムに新規投稿や返信を行う際, 文書にタグをつける機能があります.

このタグを, 投稿者が手動でつけることなく自動で付与することは可能でしょうか?

イメージとしましては, 予めタグがいくつか用意されており, 文書内にタグと一致する文字列があった場合に投稿者が手動で設定しなくとも該当のタグが付与された状態で投稿される, といった感じです.

Yosuke Tanaka への返信

Re: フォーラムへの投稿のタグ機能について

- Mitsuhiro Yoshida の投稿
画像 Developers 画像 Particularly helpful Moodlers 画像 Translators

> このタグを, 投稿者が手動でつけることなく自動で付与することは可能でしょうか?

不可能ではないと思います。

もしかしたら、以下のプログラム修正でご希望の動きに近くなるのではないでしょうか。

------------------------------------------------
Moodleバージョン:
3.7.3 (Build: 20191111)

修正対象プログラム:
config.php

修正箇所:
28行目

[ 修正前 ]
require_once(__DIR__ . '/lib/setup.php');

// Some servers may need a special rewrite rule to work around internal path length limitations:
// RewriteRule (^.*/theme/yui_combo\.php)(/.*) $1?file=$2

// There is no php closing tag in this file,
// it is intentional because it prevents trailing whitespace problems!

[ 修正後 (例) ]
require_once(__DIR__ . '/lib/setup.php');

$CFG->forum_post_tags = array (
    'English001' => 'E001',
    'English002' => 'E002',
    '日本語001' => '日001',
    '日本語002' => '日002',
);

// Some servers may need a special rewrite rule to work around internal path length limitations:
// RewriteRule (^.*/theme/yui_combo\.php)(/.*) $1?file=$2

// There is no php closing tag in this file,
// it is intentional because it prevents trailing whitespace problems!

------------------------------------------------

修正対象プログラム:
mod/forum/lib.php

修正箇所:
3110行目

[ 修正前 ]
function forum_add_new_post($post, $mform, $unused = null) {
    global $USER, $DB;


[ 修正後 ]
function forum_add_new_post($post, $mform, $unused = null) {
    global $USER, $CFG, $DB;

-------------

修正箇所:
3142行目

[ 修正前 ]
    if (!isset($post->mailnow)) {
        $post->mailnow    = 0;
    }

    $post->id = $DB->insert_record("forum_posts", $post);

[ 修正後 ]
    if (!isset($post->mailnow)) {
        $post->mailnow    = 0;
    }

    $default_tags = $CFG->forum_post_tags;

    foreach ($default_tags as $key=>$val) {
        $pos = 0;
        if (function_exists('mb_strpos')) {
            $pos = mb_strpos($post->message, $key);
        } else {
            $pos = strpos($post->message, $key);
        }
        if ($pos > 0) {
            $post->tags[] = $val;
        };
    }

    $post->id = $DB->insert_record("forum_posts", $post);

-------------

修正箇所:
3292行目

[ 修正前 ]
    $discussion->firstpost    = $post->id;
    $discussion->timemodified = $timenow;
    $discussion->usermodified = $post->userid;
    $discussion->userid       = $userid;
    $discussion->assessed     = 0;

    $post->discussion = $DB->insert_record("forum_discussions", $discussion);

[ 修正後 ]
    $discussion->firstpost    = $post->id;
    $discussion->timemodified = $timenow;
    $discussion->usermodified = $post->userid;
    $discussion->userid       = $userid;
    $discussion->assessed     = 0;

    $default_tags = $CFG->forum_post_tags;

    foreach ($default_tags as $key=>$val) {
        $pos = 0;
        if (function_exists('mb_strpos')) {
            $pos = mb_strpos($discussion->message, $key);
        } else {
            $pos = strpos($discussion->message, $key);
        }
        if ($pos > 0) {
            $discussion->tags[] = $val;
        };
    }

    $post->discussion = $DB->insert_record("forum_discussions", $discussion);
------------------------------------------------
Mitsuhiro Yoshida への返信

Re: フォーラムへの投稿のタグ機能について

- Yosuke Tanaka の投稿
ご回答いただいたのに返信が遅くなってしまい申し訳ありません。
ありがとうございます、試してみます。
Yosuke Tanaka への返信

Re: フォーラムへの投稿のタグ機能について

- Yosuke Tanaka の投稿
すみません, 「config.php」や「mod/forum/lib.php」のファイルやディレクトリが見つからないのですが, 該当のファイル/ディレクトリの場所を教えていただけないでしょうか?
Yosuke Tanaka への返信

Re: フォーラムへの投稿のタグ機能について

- Mitsuhiro Yoshida の投稿
画像 Developers 画像 Particularly helpful Moodlers 画像 Translators
よろしければ、どのような環境にどのような方法でMoodleをインストールされたのか、お分かりになる範囲でお教え頂けませんでしょうか。

例)
環境:
所属組織内に設置したサーバ、ホスティングサービスを利用等
CentOS Linux release 7.7.1908
Apache 2.4.41
PHP 7.1.33
MariaDB 10.3.20

方法:
自分で手動インストール、他の人に依頼してインストール等
Mitsuhiro Yoshida への返信

Re: フォーラムへの投稿のタグ機能について

- Yosuke Tanaka の投稿
返信ありがとうございます.
はじめに, 返信が大変遅くなってしまったことをお詫びいたします.
通知メールを見落としておりました.
申し訳ありません.

moodleの環境・バージョンは以下の通りです.
---
所属組織内(大学内)に設置したサーバ
mysql Ver 15.1 Distrib 5.5.64-MariaDB, for Linux (x86_64) using readline 5.1
CentOS Linux release 7.7.1908 (Core)
Server version: Apache/2.4.6 (CentOS)
PHP 7.1.32 (cli)

方法
ほかの人(先生)がインスト―ル

です.
よろしくお願いします.
Yosuke Tanaka への返信

Re: フォーラムへの投稿のタグ機能について

- Mitsuhiro Yoshida の投稿
画像 Developers 画像 Particularly helpful Moodlers 画像 Translators

ありがとうございます。

まず、Apacheのドキュメントルートを探してください。

例)
# grep -i 'DocumentRoot' /etc/httpd/conf/httpd.conf
# DocumentRoot: The directory out of which you will serve your
DocumentRoot "/var/www/html"
    # access content that does not live under the DocumentRoot.

次に「find」コマンドを使って設定ファイル (config.php) およびフォーラムディレクトリ (/mod/forum) の場所を探してください。上のドキュメントルートの例の場合、以下のコマンドでファイルおよびディレクトリを探せます。

例)
# find /var/www/html -name config.php
/var/www/html/moodle/config.php

# find /var/www/html -name 'forum' -type d
/var/www/html/moodle/mod/forum
Mitsuhiro Yoshida への返信

Re: フォーラムへの投稿のタグ機能について

- Yosuke Tanaka の投稿
ありがとうございます.
実行してみたところ, 以下のように出力されました.

---
$ grep -i 'DocumentRoot' /etc/httpd/conf/httpd.conf
# DocumentRoot: The directory out of which you will serve your
DocumentRoot "/var/www/html"
# access content that does not live under the DocumentRoot.

$ find /var/www/html/ -name config.php
/var/www/html/cache/classes/config.php
/var/www/html/lib/editor/tinymce/plugins/spellchecker/config.php
/var/www/html/mod/chat/gui_ajax/theme/bubble/config.php
/var/www/html/mod/chat/gui_ajax/theme/course_theme/config.php
/var/www/html/mod/chat/gui_ajax/theme/compact/config.php
/var/www/html/theme/classic/config.php
/var/www/html/theme/boost/config.php
/var/www/html/config.php

$ find /var/www/html/ -name 'forum' -type d
/var/www/html/mod/forum

---

「/var/www/html/moodle/config.php」のディレクトリが見つからないようです.
Yosuke Tanaka への返信

Re: フォーラムへの投稿のタグ機能について

- Mitsuhiro Yoshida の投稿
画像 Developers 画像 Particularly helpful Moodlers 画像 Translators

現在お使いの環境ではApacheのドキュメントルート (/var/www/html) に直接Moodleのプログラムを設置されているようですので、「「/var/www/html/moodle/config.php」のディレクトリが見つからない」状態でも問題ありません。

「 「config.php」や「mod/forum/lib.php」のファイルやディレクトリが見つからないのですが,」ということですが、以下の場所にあります。
/var/www/html/config.php
/var/www/html/mod/forum/lib.php

Mitsuhiro Yoshida への返信

Re: フォーラムへの投稿のタグ機能について

- Yosuke Tanaka の投稿
ありがとうございます!
該当ファイルに変更を加えてみます.

1つ確認したいことがあるのですが, 11/14の返信にて教えていただいた変更方法において
3142行目の変更に関する部分の

if ($pos > 0) {
$post->tags[] = $val;
};

および3292行目の変更に関する部分の

if ($pos > 0) {
$discussion->tags[] = $val;
};

の中括弧の後ろの「;」は必要でしょうか?

よろしくお願いいたします.
Yosuke Tanaka への返信

Re: フォーラムへの投稿のタグ機能について

- Mitsuhiro Yoshida の投稿
画像 Developers 画像 Particularly helpful Moodlers 画像 Translators
> 中括弧の後ろの「;」は必要でしょうか?

大変失礼致しました。
不要です。
以下のとおり訂正致します。
------------------------------------------------
Moodleバージョン:
3.7.3 (Build: 20191111)

修正対象プログラム:
config.php

修正箇所:
28行目

[ 修正前 ]
require_once(__DIR__ . '/lib/setup.php');

// Some servers may need a special rewrite rule to work around internal path length limitations:
// RewriteRule (^.*/theme/yui_combo\.php)(/.*) $1?file=$2

// There is no php closing tag in this file,
// it is intentional because it prevents trailing whitespace problems!

[ 修正後 (例) ]
require_once(__DIR__ . '/lib/setup.php');

$CFG->forum_post_tags = array (
    'English001' => 'E001',
    'English002' => 'E002',
    '日本語001' => '日001',
    '日本語002' => '日002',
);

// Some servers may need a special rewrite rule to work around internal path length limitations:
// RewriteRule (^.*/theme/yui_combo\.php)(/.*) $1?file=$2

// There is no php closing tag in this file,
// it is intentional because it prevents trailing whitespace problems!

------------------------------------------------

修正対象プログラム:
mod/forum/lib.php

修正箇所:
3110行目

[ 修正前 ]
function forum_add_new_post($post, $mform, $unused = null) {
    global $USER, $DB;


[ 修正後 ]
function forum_add_new_post($post, $mform, $unused = null) {
    global $USER, $CFG, $DB;

-------------

修正箇所:
3142行目

[ 修正前 ]
    if (!isset($post->mailnow)) {
        $post->mailnow    = 0;
    }

    $post->id = $DB->insert_record("forum_posts", $post);

[ 修正後 ]
    if (!isset($post->mailnow)) {
        $post->mailnow    = 0;
    }

    $default_tags = $CFG->forum_post_tags;

    foreach ($default_tags as $key=>$val) {
        $pos = 0;
        if (function_exists('mb_strpos')) {
            $pos = mb_strpos($post->message, $key);
        } else {
            $pos = strpos($post->message, $key);
        }
        if ($pos > 0) {
            $post->tags[] = $val;
        }
    }

    $post->id = $DB->insert_record("forum_posts", $post);

-------------

修正箇所:
3292行目

[ 修正前 ]
    $discussion->firstpost    = $post->id;
    $discussion->timemodified = $timenow;
    $discussion->usermodified = $post->userid;
    $discussion->userid       = $userid;
    $discussion->assessed     = 0;

    $post->discussion = $DB->insert_record("forum_discussions", $discussion);

[ 修正後 ]
    $discussion->firstpost    = $post->id;
    $discussion->timemodified = $timenow;
    $discussion->usermodified = $post->userid;
    $discussion->userid       = $userid;
    $discussion->assessed     = 0;

    $default_tags = $CFG->forum_post_tags;

    foreach ($default_tags as $key=>$val) {
        $pos = 0;
        if (function_exists('mb_strpos')) {
            $pos = mb_strpos($discussion->message, $key);
        } else {
            $pos = strpos($discussion->message, $key);
        }
        if ($pos > 0) {
            $discussion->tags[] = $val;
        }
    }

    $post->discussion = $DB->insert_record("forum_discussions", $discussion);
------------------------------------------------
Mitsuhiro Yoshida への返信

Re: フォーラムへの投稿のタグ機能について

- Yosuke Tanaka の投稿
ありがとうございます.
変更してみます.

変更後はrebootコマンド等で再起動などの操作は必要でしょうか?
Yosuke Tanaka への返信

Re: フォーラムへの投稿のタグ機能について

- Mitsuhiro Yoshida の投稿
画像 Developers 画像 Particularly helpful Moodlers 画像 Translators
> 変更後はrebootコマンド等で再起動などの操作は必要でしょうか?

不要です。
Mitsuhiro Yoshida への返信

Re: フォーラムへの投稿のタグ機能について

- Yosuke Tanaka の投稿
ありがとうございます.
Yosuke Tanaka への返信

Re: フォーラムへの投稿のタグ機能について

- Yosuke Tanaka の投稿
すみません, 以前教えていただいた方法で該当ファイル(config.phpおよびlib.php)を編集して運用しているのですが, ディスカッションの投稿に対してタグが自動で付与される様子が確認できません.
「サイト管理/アピアランス/タグを管理する/デフォルトコレクション」で標準タグをいくつか追加し, そのタグと一致する単語をメッセージ内に含むようなディスカッショントピックをテストとして投稿しているのですが, タグは付与されませんでした.
私の中の動作イメージが間違っている, または動作の検証方法が適していないなどあればご指摘をお願いします.
また, コードリーディングが不得手なため, もしよろしければ教えていただいた追加部分のコードがどういった意図で, どのような挙動をするか教えていただけないでしょうか.
お忙しいところ恐縮ではございますが, よろしくお願いいたします.

Yosuke Tanaka への返信

Re: フォーラムへの投稿のタグ機能について

- Mitsuhiro Yoshida の投稿
画像 Developers 画像 Particularly helpful Moodlers 画像 Translators

大変失礼しました。

これはタグ付けの条件をフォーラム内のみに限定するため、データベースは使わずに代わりにMoodle設定ファイル (config.php) に条件を記述しております。

例えば、Moodle設定ファイル (config.php) の以下の記述例の場合、フォーラム投稿内に「English001」とい文字が入力された場合、タグ「E001」が自動的に付加されます。同じく「日本語001」という文字が入力された場合、タグ「日001」が自動的に付加されます。

$CFG->forum_post_tags = array (
    'English001' => 'E001',
    'English002' => 'E002',
    '日本語001' => '日001',
    '日本語002' => '日002',
);

大変お手数ですが、「サイト管理/アピアランス/タグを管理する/デフォルトコレクション」で標準タグを追加する代わりにMoodle設定ファイル (config.php) 内にタグ付けの条件を記述してください。記述にあたりまして、文字コードはUTF-8を指定してください。

Mitsuhiro Yoshida への返信

Re: フォーラムへの投稿のタグ機能について

- Yosuke Tanaka の投稿
ご返信ありがとうございます.

なるほど, E001などは一例だったのですね.
config.php内で直接単語とタグの対応を記述したところ, タグ付けが無事実行されました.
本当にありがとうございました.