Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Blocks Advanced: Difference between revisions

From MoodleDocs
(Created page with "=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. W...")
 
No edit summary
Line 15: Line 15:
      
      
     function definition() {
     function definition() {
        global $CFG;
          
          
         $mform =& $this->_form;
         $mform =& $this->_form;
Line 22: Line 21:
}
}
</code>
</code>
Now let's create view.php
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:
Create a new file in the blocks/simplehtml directory called view.php. In this file add the following code:


Line 78: Line 78:
$PAGE->set_heading(get_string('edithtml', 'block_simplehtml'));
$PAGE->set_heading(get_string('edithtml', 'block_simplehtml'));
</code>
</code>
One last line to add. The following line should be added after $simplehtml = new simplehtml_form();
<code php>
echo $OUTPUT->header();
</code>
===Navigation breadcrumbs===
To really make this look like it's a part of moodle we will add the navigation breadcrumbs to the header of our page.
Add the following after $courseid = required_param('courseid', PARAM_INT);
<code php>
$blockid = required_param('blockid', PARAM_INT);
// Next look for optional variables.
$id = optional_param('id', 0, PARAM_INT);
</code>
Then add the following after the header code
<code php>
$settingsnode = $PAGE->settingsnav->add(get_string('simplehtmlsettings', 'block_simplehtml'));
$editurl = new moodle_url('/blocks/simplehtml/view.php', array('id' => $id, 'courseid' => $courseid, 'blockid' => $blockid));
$editnode = $settingsnode->add(get_string('editpage', 'block_simplehtml'), $editurl);
$editnode->make_active();
</code>
Don't forget to include definitions for the new strings that you are creating, into block_simplehtml.php

Revision as of 02:31, 5 April 2012

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() {
       
       $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')); One last line to add. The following line should be added after $simplehtml = new simplehtml_form(); echo $OUTPUT->header();

Navigation breadcrumbs

To really make this look like it's a part of moodle we will add the navigation breadcrumbs to the header of our page.

Add the following after $courseid = required_param('courseid', PARAM_INT); $blockid = required_param('blockid', PARAM_INT);

// Next look for optional variables. $id = optional_param('id', 0, PARAM_INT); Then add the following after the header code $settingsnode = $PAGE->settingsnav->add(get_string('simplehtmlsettings', 'block_simplehtml')); $editurl = new moodle_url('/blocks/simplehtml/view.php', array('id' => $id, 'courseid' => $courseid, 'blockid' => $blockid)); $editnode = $settingsnode->add(get_string('editpage', 'block_simplehtml'), $editurl); $editnode->make_active(); Don't forget to include definitions for the new strings that you are creating, into block_simplehtml.php