How to create block like this...

How to create block like this...

by wildan dio -
Number of replies: 17
Hi

How to created block like this / Menu Utama / KTI Online (see Attachment)

Thanks
Attachment block.gif
Average of ratings: -
In reply to wildan dio

Re: How to create block like this...

by Peter Bloomfield -
Hi Wildan,

The best thing to do is usually to copy an existing block, and change the code to do what you need your new block to do.

The block called "blog_menu" is quite simple, so you can use that as an example. Make your own copy of it, and you can delete everything in the "get_content()" function (except the top line that starts with "global"), and replace it with your own code to "echo" your content into the webpage.

Let's say you want to call it "myblock", then the other changes that you need to make are:
  • Remove the "require_once" line at the top
  • In the "init()" function, change the "$this->title =" line to store the title of your block
  • Rename the file to "block_myblock.php"
  • Create a new folder in your Moodle install called "blocks/myblock", and put the PHP file there

After that, visit your admin page in Moodle (usually click "Notifications" or something similar), and you are ready to go!

Hope that helps.
-Peter
In reply to Peter Bloomfield

Re: How to create block like this...

by Samantha Ashton -
At the worry of sounding extremely stupid. Where abouts do you amend all this coding?

I have a new block that I want appearing in the centre, but want to be able to have my own header for it, and put what ever I want in it.

I have played around, hiding the centre blocks and attempting to set them right blocks width to 100% to see if that will make it stretch out, but this doesnt seem to work either.

Any help will be much appreciated

Kind Regards
Sam

In reply to Samantha Ashton

Re: How to create block like this...

by Peter Bloomfield -
You'll find all the block code in the "blocks" folder of your Moodle installation. A common way to create a new block is to copy an existing folder from there, and rename it to your new block. Inside it is a PHP script (you need to rename the file and class in it as well, and make all the code changes in there).

As far as I know, I'm afraid it's not possible to put a block in the centre of a Moodle page (not under normal circumstances anyway... you could probably hack a theme to try it, but you might end up with odd results!).
In reply to Peter Bloomfield

Re: How to create block like this...

by Madhabendra Mandal -
Hi Peter,
Thank you very much.I am trying to create a new block as your suggestion but I am getting error: "Block link_menu: class does not have a constructor"

Here is my code:
<?php //$Id: block_blog_menu.php,v 1.16.2.2 2008/05/02 04:07:31 dongsheng Exp $

//require_once($CFG->dirroot .'/blog/lib.php');

class block_link_menu{

function init() {
$this->title = "My Link";
$this->content_type = BLOCK_TYPE_TEXT;
$this->version = 2007101509;
}



function get_content() {
global $CFG, $USER, $COURSE;
echo "aaaaaaaaaaa";
}
}

?>

can you plz suggest me where is the problems in my code and how I will craete a new block.

Thanks.
Madhabendra
In reply to Madhabendra Mandal

Re: How to create block like this...

by Simon Allison -

Hi

you are missing extends block base

class block_link_menu extends block_base {

Simon

In reply to Simon Allison

Re: How to create block like this...

by Jaswant Tak -
Hi,

I have created a block, but after adding this to front page I am getting an extra line break on top of the page. What may be the issue?

There is nothing special in $this->content->text

$this->content->text = "abc"

Please help.

Thanks,
jaswant
In reply to Jaswant Tak

Re: How to create block like this...

by Frank Ralf -
Hi Jaswant,

Probably your text will be rendered within a HTML DIV tag which is a block level element. That might cause the extra line break.

Try using Firebug for a peek under the hood of your Moodle theme.

hth
Frank
In reply to Frank Ralf

Re: How to create block like this...

by Jaswant Tak -
No Ralf,

No there is no any extra DIV. I already checked with firebug. This is only happens when I set show the block when I hide it there is no extra break.

The block internal HTML is okay have checked using firebug.

Thanks,
Jaswant
In reply to Jaswant Tak

Re: How to create block like this...

by Frank Ralf -
Next try: Use Web developer toolbar for Firefox, especially "Information > Topographic Information" might be useful.

And please post a screenshot.

hth
Frank
In reply to Frank Ralf

Re: How to create block like this...

by Jaswant Tak -
Yeah Ralf,

Its solved, Thanks for the help.

Cheers
In reply to Jaswant Tak

Re: How to create block like this...

by Frank Ralf -
Please post your solution for others who might have similar problems.

tia
Frank
In reply to wildan dio

回應: How to create block like this...

by bonep bonep -

I have done that in moodle 2.0, hope that help:

http://moodlever.blogspot.com/2011/01/create-new-block.html

In reply to bonep bonep

Re: 回應: How to create block like this...

by luciano biondo -
Thanks for the help. I made my custom block.

I have a problem. I would like to have my custom-block with background-color different from all the other blocks.

Somebody can tell me how can I do?

In reply to luciano biondo

Re: 回應: How to create block like this...

by Adam Landow -

Add this to your block code, within the class declaration:

function html_attributes() {
$attributes = parent::html_attributes();
$attributes['style'] .= 'background-color: #0000ff';
return $attributes;
}

where #0000ff is the background colour you want- the code above will result in a blue block!

This function overrides a function in the block_base class and adds to the html attributes of the block HTML element.

In reply to Adam Landow

Re: 回應: How to create block like this...

by luciano biondo -
Thanks, but the result is not what I would obtain. See the picture: a box behind the block become blue; I need the block background-color (now orange) becomes blue. (the theme I'm using is Anomaly)
Attachment Schermata 2011-02-24 a 18.29.32.png
In reply to luciano biondo

Re: 回應: How to create block like this...

by Adam Landow -

OK: here's where it gets interesting. Anomaly has a more complex CSS structure than most, with inner rounded corners.

This means that you'll need to make the changes in the CSS directly.

First: You'll need to undo the changes made to your block code (or comment it out!)

In Moodle 2.0, the file that defines the look of blocks for the 'Anomaly' theme is at (moodle root)\theme\anomaly\style\browser.css

Note that there are some references to block styling:

.block {border-width:0px;background-color:white;}
.block .corner-box {border:1px solid #C8C9C7;margin-bottom:10px;padding-bottom:10px;background-color:#E3E3E3;}
.block .corner-box .rounded-corner {background-image:url([[pix:theme|corners_block]]);position:relative;}
.block .corner-box .rounded-corner.top-left {background-position:0px 0px;float:left;top:-1px;left:-1px;}
.block .corner-box .rounded-corner.top-right {background-position:11px 0px;float:right;top:-1px;right:-1px;}
.block .corner-box .rounded-corner.bottom-left {background-position:0px 11px;float:left;bottom:0px;left:-1px;}
.block .corner-box .rounded-corner.bottom-right {background-position:11px 11px;float:right;bottom:0px;right:-1px;}

These set up the general look of all blocks.

To change the CSS for your specific block, we'll need to add some entries specific to your block. Look at where you defined the block in code- it should look like

class block_your_block extends block {
...

Note the name of the block- in this case 'block_your_block'.

Under the block CSS at (moodle root)\theme\anomaly\style\browser.css, make some new entries like so:

.block_your_block.corner-box {border:1px ;margin-bottom:10px;padding-bottom:10px;background-color:#00ff00;}
.block_your_block .corner-box .rounded-corner {background-image:url([[pix:theme|corners_block]]);position:relative;}

Change the things in boldto achieve the desired result. The above CSS will result in an awful green block- modify #00ff00 to the colour you want.

You'll see funny corners in IE- to fix these you'll need to either remove the line

.block_your_block .corner-box .rounded-corner {background-image:url([[pix:theme|corners_block]]);position:relative;}

and accept that your block will have sharp corners in IE, or, make a copy of the image at (moodle root)\theme\anomaly\pix\corners_block.png- copy it to (moodle root)\theme\anomaly\pix\corners2_block.png and modify it to be the same colour as your block. Then, in browsers.css, change the line

.block_your_block .corner-box .rounded-corner {background-image:url([[pix:theme|corners_block]]);position:relative;}

to

.block_your_block .corner-box .rounded-corner {background-image:url([[pix:theme|corners2_block]]);position:relative;}

Unlike the first approach, this is theme specific- it will only be effective in the 'Anomaly' theme. You'll need to make sure these files are not overwritten when you upgrade.

Once you learn more, another approach could be to write your own theme!

I hope this helps smile

In reply to Adam Landow

Re: 回應: How to create block like this...

by luciano biondo -
Thanks, it works! (If somebody else uses this hint, he must add a space here: '.block_your_block.corner-box' ---> '.block_your_block .corner-box')

I have not IE, I hope to have made everything right.

Luciano