Change a string in html block

Change a string in html block

by Teemu Sumi -
Number of replies: 3
With admin/replace.php you can change a string to an another string. For example if you want to change your moodle address. Content of your html blocks look weird in configdata field on your block_instance table, and admin/replace.php doesn't change internal links there. With this script you can change internal links of your html blocks:
<?php

require_once("config.php");

require_login();

if (isadmin()) {

$from = "http://www.moodle.fi";
$to = "https://www.moodle.fi";

$block = get_record("block", "name", "html");

$htmls = get_records("block_instance", "blockid", $block->id);
foreach ($htmls as $html) {
if ($html->configdata != "") {
$read = unserialize(base64_decode($html->configdata));
if ($change = str_replace($from, $to, $read->text)) {
print($change);
$read->text = $change;
$up->configdata = base64_encode(serialize($read));
$up->id = $html->id;
update_record("block_instance", $up);
}
}
}
} else {
error("Admin users only");
}

?>
Average of ratings: Useful (1)
In reply to Teemu Sumi

Re: Change a string in html block

by Mike Wilson -

Teemu, you are a legend! Thanks for this code. I've been writing something similar for the last day or so but was struggling with the unserialize(base64_decode part for some reason.

Cheers buddy smile

In reply to Mike Wilson

Re: Change a string in html block

by Pascal Maury -
Picture of Plugin developers

Hi,

Thanks for this code ! Is it still true in Moodle 2.2 ?

In reply to Teemu Sumi

Re: Change a string in html block

by Allison Soo -

Our institutional are moving the Moodle 1.9 resources to another respository. What I did was to replace some string in the HTML block (CONFIGDATA) using the base64 encode/decode in the moodle.xml.

However after these change, the HTML block went missing after I restored the backup to the same Moodle 1.9. I have verified the CONFIGDATA content with online encoder/decoder, both the content were exactly same apart from the hyperlink that I changed to.

Any one can shed light on this?  Appreciate any help. Thanks.