「開発:ブロック/付録B」の版間の差分

提供:MoodleDocs
移動先:案内検索
(Done :))
 
(同じ利用者による、間の35版が非表示)
1行目: 1行目:
作成中です - [[利用者:Mitsuhiro Yoshida|Mitsuhiro Yoshida]] 2009年5月26日 (火) 15:58 (UTC)
== Moodle 1.5以前のバージョンとのブロックAPIの違い ==
== Moodle 1.5以前のバージョンとのブロックAPIの違い ==


This Appendix will discuss what changes in the Blocks API were introduced by Moodle 1.5 and what steps developers need to take to update their blocks to be fully compatible with Moodle 1.5. Unfortunately, with these changes backward compatibility is broken; this means that blocks from Moodle 1.4 will never work with 1.5 and vice versa.
この付録では、Moodle 1.5に取り入れられたブロックAPIがどのように変わったのか説明します。また、Moodle 1.5との完全互換性を持つため、開発者がどのようなステップでブロックをアップデートする必要があるのか説明します。残念ですが、これらの変更で下位互換性が壊れてしまいます。これは、Moodle 1.4からのブロックが1.5で動作しないこと、またその逆も同じであることを意味します。


=== クラス命名規則が変わりました。 ===
=== クラス命名規則が変わりました。 ===
In Moodle 1.4, all block classes were required to have a name like '''CourseBlock_something''' and the base class from which the derived was '''MoodleBlock'''. This has changed in Moodle 1.5, to bring the naming conventions in line with other object-oriented aspects of Moodle (for example there are classes enrolment_base, resource_base etc). The new block classes should instead be named like '''block_something''' and derive from '''block_base'''. This means that in order to make a block compatible with Moodle 1.5, you need to change the class definition
Moodle 1.4では、すべてのクラスは'''CourseBlock_something'''のような名称にする必要がありました。また、すべてのクラスが派生するベースクラスは、'''MoodleBlock'''でした。Moodleのオブジェクト指向の側面と一致させるため、これが変わったのがMoodle 1.5です (例えば、クラス enrolment_base、resource_base等があります)。新しいブロッククラスは、代わりに'''block_something'''のように命名され、'''block_base'''から派生します。つまり、Moodle 1.5互換性があるブロックを作成するには、あなたはクラス定義を、


<code php>
<code php>
12行目: 10行目:
</code>
</code>


to
から


<code php>  
<code php>  
18行目: 16行目:
</code>
</code>


An exception to the above is the special case where the block is intended to display a list of items instead of arbitrary text; in this case the block class must derive from class '''block_list''' instead, like this:
に変更する必要があることを意味します。
 
上記の例外は、任意のテキストを表示する代わりに、ブロックがアイテム一覧を表示する特別なケースです。この場合、ブロッククラスは、代わりに次のように'''block_list'''から派生すべきです:


<code php>  
<code php>  
26行目: 26行目:
=== コンストラクタ VS ''init()'' ===
=== コンストラクタ VS ''init()'' ===


In Moodle 1.4, in each block class it was mandatory to define a constructor which accepted a course data record as an argument (the example is from the actual Online Users block):
Moodle 1.4における、それぞれのブロッククラスでは、コースデータを引数として受けるコンストラクタの定義は必須でした (以下の例は、実際のオンラインユーザブロックからです):


<code php>
<code php>
37行目: 37行目:
</code>
</code>


In contrast, Moodle 1.5 does away with the constructor and instead requires you to define an [[Development:Blocks/Appendix_A#init.28.29| init()]] method that takes no arguments:
対照的にMoodle 1.5において、コンストラクタは取り除かれ、あなたは、代わりに引数を取得しない[[開発:ブロック/付録A#init.28.29| init()]]メソッドを定義する必要があります:


<code php>
<code php>
46行目: 46行目:
</code>
</code>


Of course, this leaves you without access to the $course object, which you might actually need. Since that's probably going to be needed inside [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]], the way to retrieve it is by using this code:
もちろん、これでは、あなたが実際に必要とする$courseオブジェクトにアクセスしないままになります。[[開発:ブロック/付録A#get_content.28.29| get_content()]]内部で必要なため、$courseオブジェクトを取得するコードは以下のようになります:


<code php>  
<code php>  
52行目: 52行目:
</code>
</code>


If you are going to need access to $course from inside other methods in addition to [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]], you might fetch the $course object inside the [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method and save it as a class variable for later use, in order to avoid executing the same query multiple times:
[[開発:ブロック/付録A#get_content.28.29| get_content()]]に加えて、他のメソッド内部から$courseにアクセスする必要がある場合、同じクエリの複数回実行を避けるため、あなたは、[[開発:ブロック/付録A#specialization.28.29| specialization()]]メソッド内部から$courseオブジェクトを取得して、後で使用するクラス変数として保存することができます。:


<code php>
<code php>
60行目: 60行目:
</code>
</code>


=== Blocks with configuration ===
=== 設定付きブロック ===


In Moodle 1.4, blocks could only have what are now (in Moodle 1.5) called "global configuration" options, to differentiate from the new "instance configuration" options. If your block has support for configuration, you will need to take these steps:
Moodle 1.4では、ブロックは、新しい「インスタンス設定 (instance configuration)」と区別するため、現在 (Moodle 1.5) 「グローバル設定 (global configuration)」と呼ばれるオプションのみ持つことができました。ブロックが設定をサポートする場合、あなたは以下の対策を講じる必要があります:


# Rename your '''config.html''' file to '''config_global.html'''.
# あなたの'''config.html'''ファイルを'''config_global.html'''にリネームします。
# Edit the newly renamed file and completely remove the <form> tag (Moodle now wraps your configuration in a form automatically).
# 新しくリネームしたファイルを編集して、<form>タグを完全に取り除いてください (Moodleは、あなたの設定をフォーム内に自動的に組み込みます)
# If you are using any HTML <input> tags other than those that directly affect your configuration (for example, "sesskey"), REMOVE those too (Moodle will add them automatically as required).
# HTMLの<input>タグを使用している場合、あなたの設定以外にも影響を及ぼします ("sesskey")。ですから、同様に<input>タグも取り除いてください (Moodleは必要に応じて自動的に<input>タグ等を追加します)
# If you have overridden '''print_config''', rename your method to [[Development:Blocks/Appendix_A#config_print.28.29|config_print()]].
# '''print_config'''をオーバーライドしている場合、あなたのメソッドを[[開発:ブロック/付録A#config_print.28.29|config_print()]]にリネームしてください。
# If you have overridden '''handle_config''', rename your method to [[Development:Blocks/Appendix_A#config_save.28.29|config_save()]].
# '''handle_config'''をオーバーライドしている場合、あなたのメソッドを[[開発:ブロック/付録A#config_save.28.29|config_save()]]にリネームしてください。


=== Blocks with customized applicable formats ===
=== 特別に適用可能なフォーマットのブロック ===


The correct way to specify the formats you want to allow or disallow your block to exist has been reworked for Moodle 1.5 to take account of the fact that blocks are no longer restricted to just courses. To have a block retain its intended behavior, you must change these format names (array keys in the return value of [[Development:Blocks/Appendix_A#applicable_formats.28.29| applicable_formats()]] if they are used in your block:
ブロックがコースだけに制限されないことを考慮して、あなたが許可または許可したくないブロックの存在を指定する正しい方法が、Moodle 1.5のために開発されました。意図された動作をブロックに保持するには、あなたのブロックで使用されている場合、以下のフォーマット名 ([[開発:ブロック/付録A#applicable_formats.28.29| applicable_formats()]] の戻り値の配列キー) を変更する必要があります:


# '''social''' should become '''course-view-social'''
# '''social''' は、'''course-view-social''' になるべきです。
# '''topics''' should become '''course-view-topics'''
# '''topics''' は、'''course-view-topics''' になるべきです。
# '''weeks''' should become '''course-view-weeks'''
# '''weeks''' は、'''course-view-weeks''' になるべきです。


You should also keep in mind that there is now the possibility of blocks being displayed in other pages too, like the introductory page that users see when they enter an activity module. You might therefore need to make the specification for applicable formats more restrictive to keep your block out of pages it is not supposed to be shown in. Also, there are subtle changes to the way that the final decision to allow or disallow a block is made. For the technical details refer to the definition of [[Development:Blocks/Appendix_A#applicable_formats.28.29| applicable_formats()]], and for a more extended example read [[Development:Blocks#Authorized_Personnel_Only|Authorized Personnel Only]], the section dedicated to this subject.
あなたは、ユーザが活動モジュールに入ったときに表示されるイントロダクションのように、ブロックを他のページでも表示できるようになったことに留意してください。表示されてはいけないブロックをページから遠ざけるため、適用できるフォーマットをさらに制限するようにしてください。また、ブロック作成を許可または許可しない最終決定のための僅かな修正があります。技術的な詳細は、[[開発:ブロック/付録A#applicable_formats.28.29| applicable_formats()]]の定義をご覧ください。さらに拡大された例は、[[開発:ブロック#Authorized_Personnel_Only|Authorized Personnel Only]]内にある、このテーマに関連するセクションをご覧ください。


That's everything; your block will now be ready for use in Moodle 1.5!
これですべてです。あなたのブロックをMoodle 1.5で使用する準備ができました!


[[Category:開発者|ブロック]]
[[Category:開発者|ブロック]]

2009年6月21日 (日) 15:53時点における最新版

Moodle 1.5以前のバージョンとのブロックAPIの違い

この付録では、Moodle 1.5に取り入れられたブロックAPIがどのように変わったのか説明します。また、Moodle 1.5との完全互換性を持つため、開発者がどのようなステップでブロックをアップデートする必要があるのか説明します。残念ですが、これらの変更で下位互換性が壊れてしまいます。これは、Moodle 1.4からのブロックが1.5で動作しないこと、またその逆も同じであることを意味します。

クラス命名規則が変わりました。

Moodle 1.4では、すべてのクラスはCourseBlock_somethingのような名称にする必要がありました。また、すべてのクラスが派生するベースクラスは、MoodleBlockでした。Moodleのオブジェクト指向の側面と一致させるため、これが変わったのがMoodle 1.5です (例えば、クラス enrolment_base、resource_base等があります)。新しいブロッククラスは、代わりにblock_somethingのように命名され、block_baseから派生します。つまり、Moodle 1.5互換性があるブロックを作成するには、あなたはクラス定義を、

class CourseBlock_online_users extends MoodleBlock { ... }

から

class block_online_users extends block_base { ... }

に変更する必要があることを意味します。

上記の例外は、任意のテキストを表示する代わりに、ブロックがアイテム一覧を表示する特別なケースです。この場合、ブロッククラスは、代わりに次のようにblock_listから派生すべきです:

class block_admin extends block_list { ... }

コンストラクタ VS init()

Moodle 1.4における、それぞれのブロッククラスでは、コースデータを引数として受けるコンストラクタの定義は必須でした (以下の例は、実際のオンラインユーザブロックからです):

function CourseBlock_online_users ($course) {

 $this->title        = get_string('blockname','block_online_users');
 $this->content_type = BLOCK_TYPE_TEXT;
 $this->course       = $course;
 $this->version      = 2004052700;

}

対照的にMoodle 1.5において、コンストラクタは取り除かれ、あなたは、代わりに引数を取得しない init()メソッドを定義する必要があります:

function init() {

 $this->title   = get_string('blockname','block_online_users');
 $this->version = 2004111600;

}

もちろん、これでは、あなたが実際に必要とする$courseオブジェクトにアクセスしないままになります。 get_content()内部で必要なため、$courseオブジェクトを取得するコードは以下のようになります:

 $course = get_record('course', 'id', $this->instance->pageid);

get_content()に加えて、他のメソッド内部から$courseにアクセスする必要がある場合、同じクエリの複数回実行を避けるため、あなたは、 specialization()メソッド内部から$courseオブジェクトを取得して、後で使用するクラス変数として保存することができます。:

function specialization() {

 $this->course = get_record('course', 'id', $this->instance->pageid);

}

設定付きブロック

Moodle 1.4では、ブロックは、新しい「インスタンス設定 (instance configuration)」と区別するため、現在 (Moodle 1.5) 「グローバル設定 (global configuration)」と呼ばれるオプションのみ持つことができました。ブロックが設定をサポートする場合、あなたは以下の対策を講じる必要があります:

  1. あなたのconfig.htmlファイルをconfig_global.htmlにリネームします。
  2. 新しくリネームしたファイルを編集して、<form>タグを完全に取り除いてください (Moodleは、あなたの設定をフォーム内に自動的に組み込みます)。
  3. HTMLの<input>タグを使用している場合、あなたの設定以外にも影響を及ぼします (例 "sesskey")。ですから、同様に<input>タグも取り除いてください (Moodleは必要に応じて自動的に<input>タグ等を追加します)。
  4. print_configをオーバーライドしている場合、あなたのメソッドをconfig_print()にリネームしてください。
  5. handle_configをオーバーライドしている場合、あなたのメソッドをconfig_save()にリネームしてください。

特別に適用可能なフォーマットのブロック

ブロックがコースだけに制限されないことを考慮して、あなたが許可または許可したくないブロックの存在を指定する正しい方法が、Moodle 1.5のために開発されました。意図された動作をブロックに保持するには、あなたのブロックで使用されている場合、以下のフォーマット名 ( applicable_formats() の戻り値の配列キー) を変更する必要があります:

  1. social は、course-view-social になるべきです。
  2. topics は、course-view-topics になるべきです。
  3. weeks は、course-view-weeks になるべきです。

あなたは、ユーザが活動モジュールに入ったときに表示されるイントロダクションのように、ブロックを他のページでも表示できるようになったことに留意してください。表示されてはいけないブロックをページから遠ざけるため、適用できるフォーマットをさらに制限するようにしてください。また、ブロック作成を許可または許可しない最終決定のための僅かな修正があります。技術的な詳細は、 applicable_formats()の定義をご覧ください。さらに拡大された例は、Authorized Personnel Only内にある、このテーマに関連するセクションをご覧ください。

これですべてです。あなたのブロックをMoodle 1.5で使用する準備ができました!