
การสร้าง Block บน moodle 2.0
สร้างโฟลเดอร์ simplemyblock ใน mymoodle/blocks/
สร้างไฟล์ block_simplemybock.php ใน simplemyblock
<?php
class block_simplemyblcok extends block_base {
public function init() {
$this->title = get_string('simplemyblock', 'block_simplemyblock');
}
public function instance_allow_multiple() {
return true;
}
public function has_config() {
return true;
}
public function applicable_formats() {
return array('all' => true);
}
public function instance_allow_config() {
return true;
}
public function specialization() {
if (!empty($this->config->title)) {
$this->title = $this->config->title;
} else {
$this->config->title = 'Default title ...';
}
if (empty($this->config->text)) {
$this->config->text = 'Default text ...';
}
}
public function get_content() {//ฟังชันก์สำหรับแสดงเนื้อหาของบล็อกครับ
global $CFG, $USER, $PAGE, $OUTPUT;
if ($this->content !== null) {
return $this->content;
}
$this->content = new stdClass;
$this->content->text = 'The content of our SimpleHTML block!';
$this->content->footer = 'Footer here...';
$this->content->footer = html_writer::link(
new moodle_url('user/view.php', array()),'profile');
return $this->content;
}
}
สร้างไฟล์ db/access.php ใน simplemyblock
<?php
$capabilities = array(
'block/simplemyblock :myaddinstance' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
),
'clonepermissionsfrom' => 'moodle/my:manageblocks'
),
'block/simplemyblock:addinstance' => array(
'riskbitmask' => RISK_SPAM | RISK_XSS,
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => array(
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
),
'clonepermissionsfrom' => 'moodle/site:manageblocks'
),
);
สร้างไฟล์ lang/en/block_simplemyblock.php ใน simplemyblock
<?php
$string['pluginname'] = 'Simple My Block';
$string['simplemyblock'] = 'Course create';
$string['simplemyblock:addinstance'] = 'Add a new simple my block';
$string['simplemyblock:myaddinstance'] = 'Add a new simple my block block to the My Moodle page';
สร้างไฟล์ version.php ใน simplemyblock
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2014111000; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2014110400; // Requires this Moodle version
$plugin->component = 'block_simplemyblock'; // Full name of the plugin (used for diagnostics)
หลังจากที่เราได้สร้างไฟล์ต่างๆข้างต้นใน Block ของเราแล้ว ให้เข้าระบบในฐานะผู้ดูแลระบบ ระบบจะให้เราทำการติดตั้งบล็อกของเรา เท่านี้เราก็ได้บล็อกที่เราทำขึ้นมาครับ
สำหรับบทความนี้เป็นแค่การสร้างบล็อกแบบพื้นฐานเท่านั้น ท่านสามารถศึกษาเพิ่มเติมได้ที่
https://docs.moodle.org/dev/Blocks
สร้างโฟลเดอร์ simplemyblock ใน mymoodle/blocks/
สร้างไฟล์ block_simplemybock.php ใน simplemyblock
<?php
class block_simplemyblcok extends block_base {
public function init() {
$this->title = get_string('simplemyblock', 'block_simplemyblock');
}
public function instance_allow_multiple() {
return true;
}
public function has_config() {
return true;
}
public function applicable_formats() {
return array('all' => true);
}
public function instance_allow_config() {
return true;
}
public function specialization() {
if (!empty($this->config->title)) {
$this->title = $this->config->title;
} else {
$this->config->title = 'Default title ...';
}
if (empty($this->config->text)) {
$this->config->text = 'Default text ...';
}
}
public function get_content() {//ฟังชันก์สำหรับแสดงเนื้อหาของบล็อกครับ
global $CFG, $USER, $PAGE, $OUTPUT;
if ($this->content !== null) {
return $this->content;
}
$this->content = new stdClass;
$this->content->text = 'The content of our SimpleHTML block!';
$this->content->footer = 'Footer here...';
$this->content->footer = html_writer::link(
new moodle_url('user/view.php', array()),'profile');
return $this->content;
}
}
สร้างไฟล์ db/access.php ใน simplemyblock
<?php
$capabilities = array(
'block/simplemyblock :myaddinstance' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
),
'clonepermissionsfrom' => 'moodle/my:manageblocks'
),
'block/simplemyblock:addinstance' => array(
'riskbitmask' => RISK_SPAM | RISK_XSS,
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => array(
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
),
'clonepermissionsfrom' => 'moodle/site:manageblocks'
),
);
สร้างไฟล์ lang/en/block_simplemyblock.php ใน simplemyblock
<?php
$string['pluginname'] = 'Simple My Block';
$string['simplemyblock'] = 'Course create';
$string['simplemyblock:addinstance'] = 'Add a new simple my block';
$string['simplemyblock:myaddinstance'] = 'Add a new simple my block block to the My Moodle page';
สร้างไฟล์ version.php ใน simplemyblock
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2014111000; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2014110400; // Requires this Moodle version
$plugin->component = 'block_simplemyblock'; // Full name of the plugin (used for diagnostics)
หลังจากที่เราได้สร้างไฟล์ต่างๆข้างต้นใน Block ของเราแล้ว ให้เข้าระบบในฐานะผู้ดูแลระบบ ระบบจะให้เราทำการติดตั้งบล็อกของเรา เท่านี้เราก็ได้บล็อกที่เราทำขึ้นมาครับ
สำหรับบทความนี้เป็นแค่การสร้างบล็อกแบบพื้นฐานเท่านั้น ท่านสามารถศึกษาเพิ่มเติมได้ที่
https://docs.moodle.org/dev/Blocks