Skip to content

Commit

Permalink
New feature: RemoteControl 'Activate survey' function
Browse files Browse the repository at this point in the history
  • Loading branch information
c-schmitz committed Jul 10, 2012
1 parent 89eb91b commit 09266c1
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 185 deletions.
76 changes: 55 additions & 21 deletions application/controllers/admin/remotecontrol.php
Expand Up @@ -73,6 +73,11 @@ public function test()
$iSurveyID=$myJSONRPCClient->import_survey($sSessionKey, $sLSSData);
echo 'Created new survey SID:'.$iSurveyID.'<br>';

$aResult=$myJSONRPCClient->activate_survey($sSessionKey, $iSurveyID);
if ($aResult['status']=='OK')
{
echo 'Survey '.$iSurveyID.' successfully activated.<br>';
}
$aResult=$myJSONRPCClient->delete_survey($sSessionKey, $iSurveyID);
echo 'Deleted survey SID:'.$iSurveyID.'-'.$aResult['status'].'<br>';

Expand Down Expand Up @@ -120,15 +125,15 @@ public function get_session_key($username, $password)
if ($this->_doLogin($username, $password))
{
$this->_jumpStartSession($username);
$session_key = randomChars(32);
$sSessionKey = randomChars(32);

$session = new Sessions;
$session->id = $session_key;
$session->id = $sSessionKey;
$session->expire = time() + Yii::app()->getConfig('iSessionExpirationTime');
$session->data = $username;
$session->save();

return $session_key;
return $sSessionKey;
}
else
return array('status' => 'Invalid user name or password');
Expand All @@ -138,12 +143,12 @@ public function get_session_key($username, $password)
* Closes the RPC session
*
* @access public
* @param string $session_key
* @param string $sSessionKey
* @return string
*/
public function release_session_key($session_key)
public function release_session_key($sSessionKey)
{
Sessions::model()->deleteAllByAttributes(array('id' => $session_key));
Sessions::model()->deleteAllByAttributes(array('id' => $sSessionKey));
$criteria = new CDbCriteria;
$criteria->condition = 'expire < ' . time();
Sessions::model()->deleteAll($criteria);
Expand All @@ -155,14 +160,14 @@ public function release_session_key($session_key)
* RPC routine to import a survey
*
* @access public
* @param string $session_key
* @param string $sSessionKey
* @param string $sLSSData String containing the data of an LSS file
* @param integer $DestSurveyID This is the new ID of the survey - if already used a random one will be taken instead
* @return integer iSurveyID - ID of the new survey
*/
public function import_survey($session_key, $sLSSData, $sNewSurveyName=NULL, $DestSurveyID=NULL)
public function import_survey($sSessionKey, $sLSSData, $sNewSurveyName=NULL, $DestSurveyID=NULL)
{
if ($this->_checkSessionKey($session_key))
if ($this->_checkSessionKey($sSessionKey))
{
if (hasGlobalPermission('USER_RIGHT_CREATE_SURVEY'))
{
Expand All @@ -180,19 +185,48 @@ public function import_survey($session_key, $sLSSData, $sNewSurveyName=NULL, $De
}
}

/**
* RPC routine to activate a survey
*
* @access public
* @param string $sSessionKey
* @param string $sLSSData String containing the data of an LSS file
* @param integer $DestSurveyID This is the new ID of the survey - if already used a random one will be taken instead
* @return integer iSurveyID - ID of the new survey
*/
public function activate_survey($sSessionKey, $iSurveyID)
{
if ($this->_checkSessionKey($sSessionKey))
{
if (hasGlobalPermission('USER_RIGHT_CREATE_SURVEY'))
{
Yii::app()->loadHelper('admin/activate');
$aImportResults = activateSurvey($iSurveyID);

if (isset($aImportResults['error'])) return array('status' => 'Error: '.$aImportResults['error']);
else
{
return $aImportResults;
}
}
else
return array('status' => 'No permission');
}
}


/**
* RPC routine to delete a survey
*
* @access public
* @param string $session_key
* @param string $sSessionKey
* @param int $sid
* @return string
* @throws Zend_XmlRpc_Server_Exception
*/
public function delete_survey($session_key, $sid)
public function delete_survey($sSessionKey, $sid)
{
if ($this->_checkSessionKey($session_key))
if ($this->_checkSessionKey($sSessionKey))
{
if (hasSurveyPermission($sid, 'survey', 'delete'))
{
Expand All @@ -209,15 +243,15 @@ public function delete_survey($session_key, $sid)
* Returns the id of the inserted survey response
*
* @access public
* @param string $session_key
* @param string $sSessionKey
* @param int $sid
* @param struct $aResponseData
* @return int
* @throws Zend_XmlRpc_Server_Exception
*/
public function add_response($session_key, $sid, $aResponseData)
public function add_response($sSessionKey, $sid, $aResponseData)
{
if ($this->_checkSessionKey($session_key))
if ($this->_checkSessionKey($sSessionKey))
{
if (hasSurveyPermission($sid, 'response', 'create'))
{
Expand Down Expand Up @@ -255,16 +289,16 @@ public function add_response($session_key, $sid, $aResponseData)
* Returns the inserted data including additional new information like the Token entry ID and the token
*
* @access public
* @param string $session_key
* @param string $sSessionKey
* @param int $sid
* @param struct $participant_data
* @param bool $create_token
* @return array
* @throws Zend_XmlRpc_Server_Exception
*/
public function add_participants($session_key, $sid, $participant_data, $create_token)
public function add_participants($sSessionKey, $sid, $participant_data, $create_token)
{
if ($this->_checkSessionKey($session_key))
if ($this->_checkSessionKey($sSessionKey))
{
if (hasSurveyPermission($sid, 'tokens', 'create'))
{
Expand Down Expand Up @@ -363,16 +397,16 @@ protected function _jumpStartSession($username)
* This function checks if the XML-RPC session key is valid. If yes returns true, otherwise false and sends an error message with error code 1
*
* @access protected
* @param string $session_key
* @param string $sSessionKey
* @return bool
* @throws Zend_XmlRpc_Server_Exception
*/
protected function _checkSessionKey($session_key)
protected function _checkSessionKey($sSessionKey)
{
$criteria = new CDbCriteria;
$criteria->condition = 'expire < ' . time();
Sessions::model()->deleteAll($criteria);
$oResult = Sessions::model()->findByPk($session_key);
$oResult = Sessions::model()->findByPk($sSessionKey);

if (is_null($oResult))
return false;
Expand Down
51 changes: 49 additions & 2 deletions application/controllers/admin/surveyadmin.php
Expand Up @@ -355,6 +355,8 @@ public function deactivate($iSurveyID = null)
*/
public function activate($iSurveyID)
{
$clang = Yii::app()->lang;

$iSurveyID = (int) $iSurveyID;

$aData = array();
Expand Down Expand Up @@ -398,7 +400,52 @@ public function activate($iSurveyID)
$survey->save();
}

$aViewUrls['output'] = activateSurvey($iSurveyID);
$aResult=activateSurvey($iSurveyID);
if (isset($aResult['error']))
{
$aViewUrls['output']= "<br />\n<div class='messagebox ui-corner-all'>\n" .
"<div class='header ui-widget-header'>".$clang->gT("Activate Survey")." ($surveyid)</div>\n";
if ($aResult['error']=='surveytablecreation')
{
$aViewUrls['output'].="<div class='warningheader'>".$clang->gT("Survey table could not be created.")."</div>\n";
}
else
{
$aViewUrls['output'].="<div class='warningheader'>".$clang->gT("Timings table could not be created.")."</div>\n";
}
$aViewUrls['output'].="<p>" .
$clang->gT("Database error!!")."\n <font color='red'>" ."</font>\n" .
"<pre>".implode(' ', $createsurvey)."</pre>\n
<a href='".Yii::app()->getController()->createUrl("admin/survey/view/surveyid/".$iSurveyID)."'>".$clang->gT("Main Admin Screen")."</a>\n</div>" ;
}
else
{
$aViewUrls['output']= "<br />\n<div class='messagebox ui-corner-all'>\n"
."<div class='header ui-widget-header'>".$clang->gT("Activate Survey")." ({$iSurveyID})</div>\n"
."<div class='successheader'>".$clang->gT("Survey has been activated. Results table has been successfully created.")."</div><br /><br />\n";

if (isset($aResult['warning']))
{
$aViewUrls['output'] .= "<div class='warningheader'>"
.$clang->gT("The required directory for saving the uploaded files couldn't be created. Please check file premissions on the /upload/surveys directory.")
."</div>";
}

if ($survey->allowregister=='Y')
{
$aViewUrls['output'] .= $clang->gT("This survey allows public registration. A token table must also be created.")."<br /><br />\n"
."<input type='submit' value='".$clang->gT("Initialise tokens")."' onclick=\"".convertGETtoPOST(Yii::app()->getController()->createUrl("admin/tokens/index/surveyid/".$iSurveyID))."\" />\n";
}
else
{
$aViewUrls['output'] .= $clang->gT("This survey is now active, and responses can be recorded.")."<br /><br />\n"
."<strong>".$clang->gT("Open-access mode").":</strong> ".$clang->gT("No invitation code is needed to complete the survey.")."<br />".$clang->gT("You can switch to the closed-access mode by initialising a token table with the button below.")."<br /><br />\n"
."<input type='submit' value='".$clang->gT("Switch to closed-access mode")."' onclick=\"".convertGETtoPOST(Yii::app()->getController()->createUrl("admin/tokens/index/surveyid/".$iSurveyID))."\" />\n"
."<input type='submit' value='".$clang->gT("No, thanks.")."' onclick=\"".convertGETtoPOST(Yii::app()->getController()->createUrl("admin/survey/view/surveyid/".$iSurveyID))."\" />\n";
}
$aViewUrls['output'] .= "</div><br />&nbsp;\n";
}


$this->_renderWrappedTemplate('survey', $aViewUrls, $aData);
}
Expand Down Expand Up @@ -943,7 +990,7 @@ public function copy()
unlink($sFullFilepath);
}

// if (isset($aImportResults['error']) && $aImportResults['error']) safeDie($aImportResults['error']);
// if (isset($aImportResults['error']) && $aImportResults['error']) safeDie($aImportResults['error']);

if (!$aData['bFailed'])
{
Expand Down

0 comments on commit 09266c1

Please sign in to comment.