I am looking into creating some bespoke criteria for Moodle and will be documenting the process as best I can.
In Moodle a lot of things are modular and can be added to a standard Moodle installation by simply copying a folder of files into a particular location on the server. Unfortunately, at the moment (as far as I can tell), this is not the case for adding criteria.
To make the criteria available I firstly created a new file called award_criteria_quiz.php which I placed into the folder moodle/badges/criteria that already contains the six default criteria types. This file contains all of the code for the criteria including the setup forms, storage and achievement checking.
Unfortunately this is not enough to trigger an install process and this new criterion needs to be manually inserted into two core files where the current criteria are defined.
1. editing moodle/badges/criteria/award_criteria.php in two places. Firstly at the top global variables are defined and the code
define ('BADGE_CRITERIA_TYPE_QUIZ', 7);
needs to be appended after the existing six are defined.
Then further down the file an array is set up containing a list of all criteria and this newly defined one needs to be added in there too.
$BADGE_CRITERIA_TYPES = array( ... BADGE_CRITERIA_TYPE_QUIZ => 'quiz' ... }
2. editing moodle/lib/badgeslib.php to add the just defined criteria.
A single line needs to be added into the function
public function get_accepted_criteria()
That function contains all the criteria and whether they are Course or Site related. For this example I add
into the array contained Course (BADGE_TYPE_COURSE) criterion.
After quickly setting up a trial this seems to be enough to add a new criteria but it does mean that it would not be possible for none technical users to add downloaded criteria to their own Moodle installation.
I have raised a tracker ticket to see if this procedure could be made easier