Posted by & filed under Code Tips.

For a site I’m working on I needed a way to intercept a comment and block if a specific condition wasn’t met. This is what I wound up with.

function preprocess_comment_handler($comment_data) {
        if (/* Some condition if they can comment */) {
            return $comment_data; 
        }
        wp_die('An error message that they cannot comment'); 
    }
add_filter( 'preprocess_comment' , 'preprocess_comment_handler', 1 );

This hooks into the preprocess_comment filter which normally lets you edit comment data before it’s submitted to the database. In this case we’re not doing anything to the comment data if the comment is allowed to pass through, but we’re just killing the process if they’re not allowed to leave a comment.

One thing that I might go back and do later is redirect to an error page rather than just using wp_die. But for now, this is working well enough.

Posted by & filed under Code Tips.

private String rotateString(String s, int distance) {
    String out = "";
    for (int i=0; i<s.length(); i++) {
        out += s.charAt((i + distance) % s.length()); 
    }
    return out; 
}

This comes from needing a quick and easy way to rotate a String in Java.

Posted by & filed under Code Tips.

Needed a way to count how many quiz attempts students had on a specific quiz over a certain grade. The query below works with the Ad-Hoc database query Moodle plugin and lets you enter a module ID and what grade you want to count for a minimum.

SELECT u.lastname, u.firstname, u.idnumber,
(
	SELECT COUNT(*)
	FROM prefix_quiz_attempts attempts
	JOIN prefix_quiz quiz
	ON attempts.quiz=quiz.id
	JOIN prefix_course_modules cm
	ON cm.instance=quiz.id
	WHERE
	attempts.userid=u.id
	AND state='finished'
	AND cm.id=:module_id
	AND (attempts.sumgrades / quiz.sumgrades * quiz.grade) >= :minimum_grade
) AS quizcount
FROM prefix_user u
HAVING quizcount>0
ORDER BY u.lastname ASC, u.firstname ASC

Posted by & filed under Code Tips.

Quick little snippet to toggle the value of a checkbox with jQuery.

jQuery('#checkbox_id').prop('checked', !jQuery('#checkbox_id').prop('checked'));

I needed this a couple days ago using a span with a font awesome image to take the place of a checkbox. Still needed an actual checkbox underneath for the form handler to work with. So I used this little piece of code to toggle the state of a hidden checkbox and then used the state of that checkbox to determine what CSS class to use for the fake checkbox.

Posted by & filed under Programming.

Is XAMPP slow?

I’ve used XAMPP for pretty much as long as I’ve been doing web development. It makes it trivial to get Apache, MySQL, and PHP set up.Since upgrading from Windows 7 to Windows 10 though I’ve noticed it was running much slower. I just assumed that XAMPP is slow. There was also an upgrade to XAMPP as part of that, although I don’t remember what versions.

The slowness was especially noticeable working on Moodle plugins. It was almost unusable, although that’s not too surprising with the 250-300 database tables that Moodle uses.

Googling brought a bunch of potential solutions

  • Disable IPv6
  • Edit hosts file to point 127.0.0.1 to localhost
  • Exclude Apache, MySQL, and PHP from firewall

And all of these seemed to help just a little bit, but so little that it could have just been psychological. Read more »

Posted by & filed under WordPress.

Always Edit in HTML Plugin ScreenshotThis is another one of those plugins that I’d thought about writing and Google kept me from needing to.

Sometimes when building a WordPress site I’ll need to create pages using straight HTML instead of the WYSIWYG editor. Without fail though I’ll accidentally click on the Visual tab and the editor will clear out most of my HTML I’ve thought that it would be nice to be able to disable the visual editor, but only for specific posts. Turns out, I wasn’t the only one.

The Always Edit in HTML WordPress plugin takes care of it.

Take a look at the screenshot on the right. Check the box and save your post and the visual editor is gone. Nothing to foul up the HTML.

Posted by & filed under Computers & Internet.

Epson DS-510 ScannerA couple of months ago we bought an Epson DS-510 scanner. Although I haven’t run it long enough to know how well it holds up, it’s been doing really well so far.

One big snag though. The goal was to scan receipts. And most of the time it worked flawlessly. But really long receipts would always jam, even though it’s supposed to support scans up to 36 inches. And it wasn’t really even jamming. It was just stopping and showing a jam message.

Thanks to a comment reply on Amazon I discovered it’s not really jamming. It’s just that the scanner only scans up to about 16 inches if auto paper size is selected. By changing from auto size to a manually selected size it scans longer receipts without a hitch.

Posted by & filed under Moodle.

Moodle LogoIt’s happened at least a dozen times. I’ll punch in a bunch of short answer questions into Examview so that I could inmport into Moodle. Then I go through the hoops of exporting it from Examview into Blackboard so I can import into Moodle.

Then, I remember. Moodle imports short answer questions exported like this as an essay question. That means that the automatic grading doesn’t work.

Apparently this is a pretty common issue. I came across an explanation in the Moodle forums explaining why it works that way, and I totally get the reason. But it’s still annoying.

Turns out all of the explanations stop a little short of a quick and easy fix. Instead of creating short answer questions in Examview, create Completion questions. The fields in Examview are exactly the same. You type in the question on the top and the answer on the bottom. But, when Moodle imports them they come in as Short Answer along with all the automatic grading goodness.

Posted by & filed under Coding.

Spent more time than I’d like to admin banging my head against my keyboard when NetBeans wouldn’t minify a specific JavaScript file. Instead of correctly minifying, the minified version would end up blank.

Finally got frustrated enough to start tracking it down and found the cause in the line 2 pasted here.

Turned out that I was using a keyword as a property name. Wrapping class in quotes cleared everything up and NetBeans is happily minifying away again.

 

 

Posted by & filed under Coding.

For a project that I’m working on I needed a way to fade out an image before overlaying text on to it. Text I’ve done. That’s not an issue. But I haven’t ever done anything with PHP with fading out an image.

Turns out there’s a library called Jaguar that makes it, and other image manipulation, really easy for PHP.

The code below loads an image and covers it with a white overlay to fade it out.

I left out the part where the text is added, but Jaguar makes adding text pretty easy as well.

And if you’re interested, the final result is used on the featured image on a font website that I’m setting up.