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

This is an old revision of the document!


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
  • 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->_('Studienummer gewijzigd van %1$s naar %2$s!'), $data['gr2o_patient_nr'], $respNr));
            }else{
                $this->addMessage(sprintf($this->_('Studienummer %s toegekend!'), $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 `gr2o_patient_nr` FROM `gems__respondent2org` WHERE `gr2o_patient_nr` LIKE '$orgcode%' ORDER BY `gems__respondent2org`.`gr2o_patient_nr` DESC LIMIT 1;";
        $value = $this->db->fetchRow($sql);
        $nr    = (int) substr($value['gr2o_patient_nr'], $orgcodelength);
        $nr++;
        $respnr   = $orgcode.sprintf('%04d', $nr);
 
        //Tip: use next line (decomment) for debugging:
        //MUtil_Echo::track($respnr);
 
        return $respnr;
    }
 
 
  }
devzone/giftbox/sequentialnrs.1354030676.txt.gz · Last modified: 2020/03/12 12:08 (external edit)