"Error writing to database" error when moving block - Moodle 2.7.2

"Error writing to database" error when moving block - Moodle 2.7.2

by Mike Brinkman -
Number of replies: 16

I have created two custom regions in my theme to display RSS feeds side by side. I have added 'side-center-left' & 'side-center-right' to my front page regions in config.php in addition to  'side-pre' & 'side-post'.

// The site home page.
    'frontpage' => array(
        'file' => 'standard.php',
        'regions' => array('side-pre', 'side-center-left', 'side-center-right', 'side-post'),
        'defaultregion' => 'side-pre',
    ),

I have also added strings for them in my lang/en/theme_test.php file:

$string['region-side-center-left'] = 'Center-Left';
$string['region-side-center-right'] = 'Center-Right';

I also included tests for both of them at the top of my standard layout:

$hassidepre = $PAGE->blocks->region_has_content('side-pre', $OUTPUT);
$hassidecenterleft = $PAGE->blocks->region_has_content('side-center-left', $OUTPUT);
$hassidecenterright = $PAGE->blocks->region_has_content('side-center-right', $OUTPUT);
$hassidepost = $PAGE->blocks->region_has_content('side-post', $OUTPUT);

I've followed that up with code to insert the content:

<?php if ($hassidecenterleft) { ?>
  <div id="region-side-center-left" class="block-region">
    <div class="region-content">
      <?php echo $OUTPUT->blocks_for_region('side-center-left') ?>
    </div>
  </div>
<?php } ?>
<?php if ($hassidecenterright) { ?>
  <div id="region-side-center-right" class="block-region">
    <div class="region-content">
      <?php echo $OUTPUT->blocks_for_region('side-center-right') ?>
    </div>
  </div>
<?php } ?> 

Both "Center-Left" and "Center-Right" show up as options when I look on the configuration page for the RSS block. When I move the RSS block to the Center-Left side, everything works. When I add it to the Center-Right side, I get "Error writing to database".

Writing a simple test within another div I added says that $hassidecenterleft is true, and $hassidecenterright is false. I have been consistent in my naming, and even copied/pasted from one location to another to make sure there weren't any typos. I can't quite figure out what the problem is.

Average of ratings: -
In reply to Mike Brinkman

Re: "Error writing to database" error when moving block - Moodle 2.7.2

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

If it is a JASON Error you can forget it as this seems to be triggered locally in the browser especially when doing too many things at once.

Average of ratings: Useful (1)
In reply to Mary Evans

Re: "Error writing to database" error when moving block - Moodle 2.7.2

by Mike Brinkman -

I don't think it was a JSON error, as this was done in the block configuration instead of drag & drop.

In reply to Mike Brinkman

Re: "Error writing to database" error when moving block - Moodle 2.7.2

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Also with adding new block regions you really need to go to the Notifications page and this should, I think, prime the database ready for use.

Average of ratings: Useful (1)
In reply to Mary Evans

Re: "Error writing to database" error when moving block - Moodle 2.7.2

by Mike Brinkman -

Going to the notifications page didn't upgrade the database, so I decided to uninstall it & reinstall it, which did upgrade the database. That didn't work either. Back to the drawing board. mixed

In reply to Mike Brinkman

Re: "Error writing to database" error when moving block - Moodle 2.7.2

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hi Mike,

Given that "$hassidecenterright is false" when editing is on means that the block region is not there as a valid region to receive the RSS block from the left.  This is because you are not outputting any markup for that region.  Certainly, drag and drop will not work.  So at a hunch that is why the database error is happening, but I don't know for certain without more information.

Therefore when editing then display that block region.  You can use the result of '$PAGE->user_is_editing()'.

Gareth 

Average of ratings: Useful (1)
In reply to Gareth J Barnard

Re: "Error writing to database" error when moving block - Moodle 2.7.2

by Mike Brinkman -

That's the confounding thing. $hassidecenterright should NOT be false, because the region is included in the frontpage regions:

'regions' => array('side-pre', 'side-center-left', 'side-center-right', 'side-post'),

It does not make sense that the left side works and the right side doesn't. Also, this is occuring when I configure the block. I can choose "Center Right" from the drop-down box, and after I submit is when I get the error.

Just for the fun of it, I decided to remove the check on $hassidecenterright. After doing that, I put  <?php echo "<b>Side center right</b>"; ?> , which got echoed, and when I inspected the code it shows that <div id="region-side-center-right" class="block-region"> is indeed on the page. Then tried to move the block on it's configuration page, and I got a whole brand new error:

Coding error detected, it must be fixed by a programmer: Trying to reference an unknown block region side-center-right

This, of course is not entirely unexpected, but I figured I would post it here in case it could give any additional hints. 

I think what I'm going to do next is change my theme, zip it up, remove it and reinstall it. I couldn't force the database to update using the notification link like Mary suggested, so hopefully reinstalling it will help cause it to work.

In reply to Mike Brinkman

Re: "Error writing to database" error when moving block - Moodle 2.7.2

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

The value is correct because:

$PAGE->blocks->region_has_content('side-center-right', $OUTPUT);

would indicate that there are no blocks in that region.  Not that it exists as a region on that page as defined in the config.php file.  If you need to know that then use the 'known_region' method.

Average of ratings: Useful (1)
In reply to Gareth J Barnard

Re: "Error writing to database" error when moving block - Moodle 2.7.2

by Mike Brinkman -
The value is correct because:
$PAGE->blocks->region_has_content('side-center-right', $OUTPUT);
would indicate that there are no blocks in that region.  Not that it exists as a region on that page as defined in the config.php file.  If you need to know that then use the 'known_region' method.

I think you may be mistaken. The purpose of this code is to see if the block should be enabled. That's why further below you have <?php if (hassidecenterrights) ?> as a check to see if the block should appear on the page. The same code with 'side-center-left' reports true, despite not having any blocks loaded in the region.

In reply to Mike Brinkman

Re: "Error writing to database" error when moving block - Moodle 2.7.2

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Mistaken? Really?

Then why in the description of the method in '/lib/blocklib.php' are the words 'Determine whether a region contains anything.'?  And the description for the method 'is_known_region' is "Find out if a region exists on a page"?

Therefore either you need to use 'is_known_region' or display the block when editing is on regardless.

And JSON errors tend to happen on session time-outs.

Average of ratings: Useful (1)
In reply to Gareth J Barnard

Re: "Error writing to database" error when moving block - Moodle 2.7.2

by Mike Brinkman -

I didn't mean any offense, sorry if it came off that way. I appreciate all of the feedback you have given me so far.

I'm having a difficult time reconciling why the left works and the right doesn't when it's the exact same code. Why does the left return true when it doesn't contain any content? I think the easiest thing is to just post my theme here and maybe somebody can find the bone-headed mistake I'm making.

In reply to Mike Brinkman

Re: "Error writing to database" error when moving block - Moodle 2.7.2

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

No problem Mike, I'll look when I get a chance.

Average of ratings: Useful (1)
In reply to Gareth J Barnard

Re: "Error writing to database" error when moving block - Moodle 2.7.2

by Mike Brinkman -

Hey Gareth, have you had a chance to check it out yet?

In reply to Mike Brinkman

Re: "Error writing to database" error when moving block - Moodle 2.7.2

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Nope!

In reply to Gareth J Barnard

Re: "Error writing to database" error when moving block - Moodle 2.7.2

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hi Mike,

It appears that the name of the region 'side-center-right' is one character more than is allowed in the database - hence the error.  Please see attached screen shot.

Gareth

Attachment 2014-10-05 13_59_40-Error_db.png
Average of ratings: Useful (1)
In reply to Gareth J Barnard

Re: "Error writing to database" error when moving block - Moodle 2.7.2

by Mike Brinkman -

Thanks, Gareth! It's working perfectly now! I had the stand-alone version of Moodle with XAMPP, but it didn't have phpMyAdmin, so I didn't have a good way to inspect. My command line knowledge of MySQL is pretty rusty, so I downloaded a new version of XAMPP and was going to install Moodle from there, but figured I'd check here first to see if anyone had posted a response. Thanks for all your help, it's really appreciated! smile