This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
devzone:giftbox:sequentialnrs [2012/11/27 16:47] Roel dutch to english |
devzone:giftbox:sequentialnrs [2020/03/12 12:06] (current) |
||
---|---|---|---|
Line 2: | Line 2: | ||
In order to create a sequential (study-)number for each new respondent, you need to create you own respondent controller.\\ | In order to create a sequential (study-)number for each new respondent, you need to create you own respondent controller.\\ | ||
- | Please see the [[devzone:howto|howto]] page first, on how to do that.\\ | + | Please see the [[devzone:howto:start|howto]] page first, on how to do that.\\ |
In the code below, you will see three functions in this controller: | In the code below, you will see three functions in this controller: | ||
* beforeSave, which will check the respondent number just before saving it | * beforeSave, which will check the respondent number just before saving it | ||
- | * createModel, which uses the parent model and creates the new respondent number using an extra variable for organization id | + | * createModel, which uses the parent model and creates the new respondent number using an extra variable for organization id as default value (*). |
* getRespNr, which uses the organization ID, gets the latest respondent number for that organization and returns the next respondent number | * getRespNr, which uses the organization ID, gets the latest respondent number for that organization and returns the next respondent number | ||
. | . | ||
Line 62: | Line 62: | ||
$orgcodelength = strlen($orgcode); | $orgcodelength = strlen($orgcode); | ||
- | $sql = "SELECT `gr2o_patient_nr` FROM `gems__respondent2org` WHERE `gr2o_patient_nr` LIKE '$orgcode%' ORDER BY `gems__respondent2org`.`gr2o_patient_nr` DESC LIMIT 1;"; | + | $sql = "SELECT CAST(substr(`gr2o_patient_nr`,$orgcodelength+1) AS UNSIGNED) AS mynr, `gr2o_patient_nr` |
+ | FROM `gems__respondent2org` | ||
+ | WHERE `gr2o_patient_nr` LIKE '$orgcode%' | ||
+ | ORDER BY mynr DESC LIMIT 1;"; | ||
+ | /* | ||
+ | * This can result in different organisations having identical respnrs if you use orgcodes like A, AB, AC or N, N1, N3, etc ! | ||
+ | * It depends on your situation if this is a feature or an unwanted option. | ||
+ | * If unwanted you should change this code to fit your needs. | ||
+ | * Please note that this is not a problem for Gemstracker as it keeps track of the patient and organization ids. | ||
+ | */ | ||
$value = $this->db->fetchRow($sql); | $value = $this->db->fetchRow($sql); | ||
- | $nr = (int) substr($value['gr2o_patient_nr'], $orgcodelength); | + | $nr = $value['mynr']; |
$nr++; | $nr++; | ||
$respnr = $orgcode.sprintf('%04d', $nr); | $respnr = $orgcode.sprintf('%04d', $nr); | ||
Line 77: | Line 87: | ||
</code> | </code> | ||
+ | |||
+ | (*) In a simalar way you can set defaults for other fields in the createModel function:\\ | ||
+ | <code php> | ||
+ | $model->set('gr2o_consent', 'default', 'Yes'); | ||
+ | </code> | ||
+ | or\\ | ||
+ | <code php> | ||
+ | $model->set('grs_gender', 'default', 'M'); | ||
+ | </code> | ||
+ | |||
+ |