GemsTracker

GEneric Medical Survey Tracker

User Tools

Site Tools


Sidebar

Quick Links

Main site / download

API Docs
Bug tracker
Demo site
GitHub

Index

devzone:giftbox:sequentialnrs

Sequential numbers

In order to create a sequential (study-)number for each new respondent, you need to create you own respondent controller.
Please see the howto page first, on how to do that.
In the code below, you will see three functions in this controller:

  • 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 as default value (*).
  • getRespNr, which uses the organization ID, gets the latest respondent number for that organization and returns the next respondent number

.

  class RespondentController extends Gems_Default_RespondentAction
  {
 
    /*
     * default value for respnr needed before validation of form
     */
    public function createModel($detailed, $action) {
        $model = parent::createModel($detailed, $action);
 
        $orgcode = $this->loader->getOrganization()->getCode();
        $model->set('gr2o_patient_nr', 'default', $this->getRespnr($orgcode));
 
        return $model;
 
    } 
 
 
 
    /*
     * get new studynr if needed and display appropriate message
     */
    public function beforeSave(array &$data, $isNew, Zend_Form $form = null)
    {
 
        If ($isNew) {
 
            //MUtil_Echo::track($data);
            $orgcode = $this->loader->getOrganization($data['gr2o_id_organization'])->getCode();
 
            $respNr = $this->getRespNr($orgcode);
 
            if ($data['gr2o_patient_nr'] !== $respNr) {
                $this->addMessage(sprintf($this->_('Studynumber changed from %1$s to %2$s!'), $data['gr2o_patient_nr'], $respNr));
            }else{
                $this->addMessage(sprintf($this->_('Studynumber %s assigned!'), $respNr));
            }
 
            $data['gr2o_patient_nr'] = $respNr;
        }
        return true;
    }
 
 
    /*
     * This function will get the next studynumber for the current organization
     * use gor_code that can be set in the organization interface
     * 
     */
 
    protected function getRespNr($orgcode)
    {
        $orgcodelength = strlen($orgcode);
 
        $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);
        $nr = $value['mynr'];
        $nr++;
        $respnr   = $orgcode.sprintf('%04d', $nr);
 
        //MUtil_Echo::track($respnr);
 
        return $respnr;
    }
 
 
  }

(*) In a simalar way you can set defaults for other fields in the createModel function:

        $model->set('gr2o_consent', 'default', 'Yes');

or

        $model->set('grs_gender', 'default', 'M');
devzone/giftbox/sequentialnrs.txt · Last modified: 2020/03/12 12:06 (external edit)