Blocks Advanced
Introduction
This is a continuation of the simpleHTML block example.
Adding forms
The next thing that we want to do is add a form to create a simpleHTML page.
We are going to create two pages - view.php and simplehtml_form.php All of our logic will be contained in view.php and simplehtml_form.php will just display the form elements.
Create a new file in the blocks/simplehtml directory called simplehtml_form.php. Add the following code:
require_once("$CFG->libdir/formslib.php");
class simplehtml_form extends moodleform {
function definition() {
global $CFG;
$mform =& $this->_form;
$mform->addElement('header','displayinfo', get_string('textfields', 'block_simplehtml'));
}
}
Now let's create view.php
Create a new file in the blocks/simplehtml directory called view.php. In this file add the following code:
<?php
require_once('../../config.php');
require_once('simplehtml_form.php');
global $DB;
// Check for all required variables.
$courseid = required_param('courseid', PARAM_INT);
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('invalidcourse', 'block_simplehtml', $courseid);
}
require_login($course);
$simplehtml = new simplehtml_form();
$simplehtml->display();
?>
Next we will add a link from our block to this page so that we can see what we have created.
In the get_content() method of our block_simplehtml class replace
$this->content->footer = 'Footer here...';
with
global $COURSE
// The other code.
$url = new moodle_url('blocks/simplehtml/view.php', array('blockid' => $this->instance->id, 'courseid' => $COURSE->id));
$this->content->footer = html_writer::link($url, get_string('addpage', 'block_simplehtml'));
Add a Header
If you visit the page now you will see a very basic non-themed form without any of the necessary navigational structure. Note that this happens because the form processing page, in this case view.php, is being accessed directly via the URL and not by being included in another page. This means that the necessary infrastructure must be added directly to the script. Add this by using the print_header function, but a few pieces of data are needed to build appropriate navigation. Make the following adjustments to view.php:
In the global declaration:
global $DB, $OUTPUT, $PAGE;
After require_login($course);
$PAGE->set_url('/blocks/simplehtml/view.php', array('id' => $courseid));
$PAGE->set_pagelayout('standard');
$PAGE->set_heading(get_string('edithtml', 'block_simplehtml'));