Skip to content

Commit

Permalink
logando también la creación desde dataentry, antes solo el update
Browse files Browse the repository at this point in the history
  • Loading branch information
Aestu committed Nov 5, 2016
1 parent 75a6c45 commit 197b020
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 16 deletions.
13 changes: 9 additions & 4 deletions application/controllers/admin/dataentry.php
Expand Up @@ -1527,10 +1527,10 @@ public function update()
$updateqr = substr($updateqr, 0, -3);
$updateqr .= " WHERE id=$id";

$beforeDataEntrySave = new PluginEvent('beforeDataEntrySave');
$beforeDataEntrySave->set('iSurveyID',$surveyid);
$beforeDataEntrySave->set('iResponseID',$id);
App()->getPluginManager()->dispatchEvent($beforeDataEntrySave);
$beforeDataEntryUpdate = new PluginEvent('beforeDataEntryUpdate');
$beforeDataEntryUpdate->set('iSurveyID',$surveyid);
$beforeDataEntryUpdate->set('iResponseID',$id);
App()->getPluginManager()->dispatchEvent($beforeDataEntryUpdate);

$updateres = dbExecuteAssoc($updateqr) or safeDie("Update failed:<br />\n<br />$updateqr");

Expand Down Expand Up @@ -1770,6 +1770,11 @@ public function insert()
{
$new_response->$column = $value;
}

$beforeDataEntryCreate = new PluginEvent('beforeDataEntryCreate');
$beforeDataEntryCreate->set('iSurveyID',$surveyid);
App()->getPluginManager()->dispatchEvent($beforeDataEntryCreate);

$new_response->save();
$last_db_id = $new_response->getPrimaryKey();
if (isset($_POST['closerecord']) && isset($_POST['token']) && $_POST['token'] != '') // submittoken
Expand Down
69 changes: 57 additions & 12 deletions plugins/AuditLog/AuditLog.php
Expand Up @@ -31,9 +31,14 @@ class AuditLog extends \ls\pluginmanager\PluginBase {
'label' => 'Log if a user was deleted',
'default' => '1',
),
'AuditLog_Log_DataEntrySave' => array(
'AuditLog_Log_DataEntryCreate' => array(
'type' => 'checkbox',
'label' => 'Log if a user modify survey responses',
'label' => 'Log if a survey admin create a response',
'default' => '1',
),
'AuditLog_Log_DataEntryUpdate' => array(
'type' => 'checkbox',
'label' => 'Log if a survey admin modify a response',
'default' => '1',
),
'AuditLog_Log_TokenSave' => array(
Expand Down Expand Up @@ -77,7 +82,8 @@ public function init() {
$this->subscribe('beforeUserSave');
$this->subscribe('beforeUserDelete');
$this->subscribe('beforePermissionSetSave');
$this->subscribe('beforeDataEntrySave');
$this->subscribe('beforeDataEntryCreate');
$this->subscribe('beforeDataEntryUpdate');
$this->subscribe('beforeTokenSave');
$this->subscribe('beforeTokenDelete');
$this->subscribe('beforeParticipantSave');
Expand Down Expand Up @@ -192,11 +198,12 @@ public function beforePermissionSetSave()
}

/**
* show login form
* show reason form
*
* @param string action: creation or update
* @return mixed
*/
public function askReasonForModification(){
public function askReasonForModification($sAction){
$cs = Yii::app()->clientScript;
$cs->reset();
\Yii::app()->bootstrap;
Expand Down Expand Up @@ -231,7 +238,13 @@ public function askReasonForModification(){

echo (CHtml::tag('div', array('class' =>'Absolute-Center is-Responsive')));
echo (CHtml::tag('div', array('class' =>'col-sm-12 col-md-12 col-md-offset-1')));
echo (CHtml::tag('div', array(), CHtml::form(array("admin/dataentry/sa/update", "sid"=>$this->getEvent()->get('iSurveyID')), 'post', array('name'=>'editresponse', 'id'=>'editresponse'))));
if ($sAction == "create") {
$sForm = CHtml::form(array("admin/dataentry/sa/insert"), 'post', array('name'=>'addsurvey', 'id'=>'addsurvey', 'enctype'=>'multipart/form-data'));
}
else {
$sForm = CHtml::form(array("admin/dataentry/sa/update", "sid"=>$this->getEvent()->get('iSurveyID')), 'post', array('name'=>'editresponse', 'id'=>'editresponse'));
}
echo (CHtml::tag('div', array(), $sForm));
// for each post, un hidden con ese nombre y valor
echo (CHtml::tag('div', array('class' => 'form-group input-group'), "<span class='input-group-addon'><i class='glyphicon glyphicon-user'></i></span></label> <textarea class='form-control' id='reason' required='' name='reason'></textarea>"));
foreach ($_POST as $sName => $sValue) {
Expand All @@ -245,23 +258,55 @@ public function askReasonForModification(){
}

/**
* Function catches if a response was modified or created
* Function catches if a response was created
* @return unknown_type
*/
public function beforeDataEntryCreate()
{
$event = $this->getEvent();
$iSurveyID=$event->get('iSurveyID');
if (!$this->checkSetting('AuditLog_Log_DataEntryCreate') || !$this->get('auditing', 'Survey', $iSurveyID, false)) {
return;
}

if (App()->request->getPost("reason") == null) {
$this->askReasonForModification("create");
}
$oCurrentUser = $this->api->getCurrentUser();
$currentUID = $oCurrentUser ? $oCurrentUser->uid : null;

$aValues = $_POST;
unset($aValues["YII_CSRF_TOKEN"]);
unset($aValues["reason"]);
if (count($aValues)){
$oAutoLog = $this->api->newModel($this, 'log');
$oAutoLog->uid=$currentUID;
$oAutoLog->entity='survey_' . $iSurveyID;
$oAutoLog->action="create";
$oAutoLog->newvalues=json_encode($aValues);
$oAutoLog->reason=App()->request->getPost("reason");
$oAutoLog->fields=implode(',',array_keys($aValues));
$oAutoLog->save();
}
}

/**
* Function catches if a response was modified
* @return unknown_type
*/
public function beforeDataEntrySave()
public function beforeDataEntryUpdate()
{
$event = $this->getEvent();
$iSurveyID=$event->get('iSurveyID');
if (!$this->checkSetting('AuditLog_Log_DataEntrySave') || !$this->get('auditing', 'Survey', $iSurveyID, false)) {
if (!$this->checkSetting('AuditLog_Log_DataEntryUpdate') || !$this->get('auditing', 'Survey', $iSurveyID, false)) {
return;
}

if (App()->request->getPost("reason") == null) {
$this->askReasonForModification();
$this->askReasonForModification("update");
}
$oCurrentUser = $this->api->getCurrentUser();
$currentUID = $oCurrentUser ? $oCurrentUser->uid : null;
$sAction = 'update';
$oldvalues= $this->api->getResponse($iSurveyID, $event->get('iResponseID'), false);

$aDiffOld = array();
Expand All @@ -281,7 +326,7 @@ public function beforeDataEntrySave()
$oAutoLog = $this->api->newModel($this, 'log');
$oAutoLog->uid=$currentUID;
$oAutoLog->entity='survey_' . $iSurveyID;
$oAutoLog->action=$sAction;
$oAutoLog->action="update";
$oAutoLog->entityid=$event->get('iResponseID');
$oAutoLog->oldvalues=json_encode($aDiffOld);
$oAutoLog->newvalues=json_encode($aDiffNew);
Expand Down

0 comments on commit 197b020

Please sign in to comment.