Skip to content

Commit

Permalink
DEV: implemented functions in new controller QuestionEditorController…
Browse files Browse the repository at this point in the history
….php
  • Loading branch information
Trischi80 committed Jun 2, 2020
1 parent b1fb0bb commit 7be516f
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 40 deletions.
219 changes: 180 additions & 39 deletions application/controllers/QuestionEditorController.php
Expand Up @@ -25,7 +25,6 @@ public function accessRules()
/**
* This part comes from _renderWrappedTemplate
*
*
* @param string $view
* @return bool
*/
Expand All @@ -39,17 +38,14 @@ protected function beforeRender($view)
LimeExpressionManager::SetSurveyId($this->aData['surveyid']);
LimeExpressionManager::StartProcessingPage(false, true);

//$basePath = (string) Yii::getPathOfAlias('application.views.layouts');
//$this->layout = $basePath.'/layout_questioneditor.php';

$this->layout = 'layout_questioneditor';
}

return parent::beforeRender($view);
}

/**
* Main view function prepares the necessary global js parts and renders the HTML
* Main view function prepares the necessary global js parts and renders the HTML for the question editor
*
* @param integer $surveyid
* @param integer $gid
Expand Down Expand Up @@ -214,38 +210,6 @@ public function actionView($surveyid, $gid = null, $qid = null, $landOnSideMenuT
]);
}

/**
* Creates a question object
* This is either an instance of the placeholder model QuestionCreate for new questions,
* or of Question for already existing ones
*
* todo: this should be moved to model ...
*
* @param int $iQuestionId
* @param string $sQuestionType
* @param int $gid
* @return Question
* @throws CException
*/
private function getQuestionObject($iQuestionId = null, $sQuestionType = null, $gid = null)
{
$iSurveyId = App()->request->getParam('sid') ?? App()->request->getParam('surveyid'); //todo: this should be done in the action directly
$oQuestion = Question::model()->findByPk($iQuestionId);

if ($oQuestion == null) {
$oQuestion = QuestionCreate::getInstance($iSurveyId, $sQuestionType);
}

if ($sQuestionType != null) {
$oQuestion->type = $sQuestionType;
}

if ($gid != null) {
$oQuestion->gid = $gid;
}

return $oQuestion;
}

/****
* *** A lot of getter function regarding functionalities and views.
Expand Down Expand Up @@ -441,7 +405,7 @@ public function actionReloadQuestionData($iQuestionId = null, $type = null, $gid

$aCompiledQuestionData = $this->getCompiledQuestionData($oQuestion);
$aQuestionGeneralOptions = $this->getGeneralOptions($oQuestion->qid, $type, $oQuestion->gid, $question_template);
$aAdvancedOptions = $this->getAdvancedOptions($oQuestion->qid, $type, true, $question_template);
$aAdvancedOptions = $this->getAdvancedOptions($oQuestion->qid, $type, $question_template);

$aLanguages = [];
$aAllLanguages = getLanguageData(false, App()->session['adminlang']);
Expand Down Expand Up @@ -484,7 +448,7 @@ public function actionGetGeneralOptions(
$iQuestionId = null,
$sQuestionType = null,
$gid = null,
$returnArray = false,
$returnArray = false, //todo see were this ajaxrequest is done and take out the parameter there and here
$question_template = 'core'
) {
$aGeneralOptionsArray = $this->getGeneralOptions($iQuestionId,$sQuestionType,$gid,$question_template);
Expand Down Expand Up @@ -573,4 +537,181 @@ private function getCompiledQuestionData(&$oQuestion)
];
}

/**
* Action (called by ajaxrequest and returning json)
* Returns a preformatted json of advanced settings.
*
* @param int $iQuestionId
* @param string $sQuestionType
* @param boolean $returnArray
* @param string $question_template
*
* @return void|array
* @throws CException
*/
public function actionGetAdvancedOptions(
$iQuestionId = null,
$sQuestionType = null,
$returnArray = false, //todo see were this ajaxrequest is done and take out the parameter there and here
$question_template = 'core'
) {
//here we get a Question object (also if question is new --> QuestionCreate)
$oQuestion = $this->getQuestionObject($iQuestionId, $sQuestionType);
$aAdvancedOptionsArray = $this->getAdvancedOptions($iQuestionId, $sQuestionType, $question_template);

$this->renderJSON(
[
'advancedSettings' => $aAdvancedOptionsArray,
'questionTypeDefinition' => $oQuestion->questionType,
]
);
}

/**
* It returns a preformatted array of advanced settings.
*
* @param null $iQuestionId
* @param null $sQuestionType
* @param string $question_template
* @return array
* @throws CException
*/
private function getAdvancedOptions($iQuestionId = null, $sQuestionType = null, $question_template = 'core')
{
//here we get a Question object (also if question is new --> QuestionCreate)
$oQuestion = $this->getQuestionObject($iQuestionId, $sQuestionType);

return $oQuestion->getDataSetObject()->getPreformattedBlockOfAdvancedSettings(
$oQuestion,
$question_template);
}

/**
* Collect initial question data
* This either creates a temporary question object, or calls a question object from the database
*
* @param int $iQuestionId
* @param int $gid
* @param string $type
*
* @return void
* @throws CException
*/
public function actionGetQuestionData($iQuestionId = null, $gid = null, $type = null)
{
$iQuestionId = (int) $iQuestionId;
$oQuestion = $this->getQuestionObject($iQuestionId, $type, $gid);

$aQuestionInformationObject = $this->getCompiledQuestionData($oQuestion);
$surveyInfo = $this->getCompiledSurveyInfo($oQuestion);

$aLanguages = [];
$aAllLanguages = getLanguageData(false, App()->session['adminlang']);
$aSurveyLanguages = $oQuestion->survey->getAllLanguages();
array_walk(
$aSurveyLanguages,
function ($lngString) use (&$aLanguages, $aAllLanguages) {
$aLanguages[$lngString] = $aAllLanguages[$lngString]['description'];
}
);

$this->renderJSON(
array_merge(
$aQuestionInformationObject,
[
'surveyInfo' => $surveyInfo,
'languages' => $aLanguages,
'mainLanguage' => $oQuestion->survey->language,
]
)
);
}

/**
*
* todo: this should be moved to model, not a controller function ...
*
* @param $oQuestion
* @return array
*/
private function getCompiledSurveyInfo($oQuestion) {
$oSurvey = $oQuestion->survey;
$aQuestionTitles = $oCommand = Yii::app()->db->createCommand()
->select('title')
->from('{{questions}}')
->where('sid=:sid and parent_qid=0')
->queryColumn([':sid'=>$oSurvey->sid]);
$isActive = $oSurvey->isActive;
$questionCount = safecount($aQuestionTitles);
$groupCount = safecount($oSurvey->groups);

return [
"aQuestionTitles" => $aQuestionTitles,
"isActive" => $isActive,
"questionCount" => $questionCount,
"groupCount" => $groupCount,
];
}

/**
* Collect the permissions available for a specific question
*
* @param $iQuestionId
*
* @return void
* @throws CException
*/
public function actionGetQuestionPermissions($iQuestionId = null)
{
$iQuestionId = (int) $iQuestionId;
$oQuestion = $this->getQuestionObject($iQuestionId);

$aPermissions = [
"read" => Permission::model()->hasSurveyPermission($oQuestion->sid, 'survey', 'read'),
"update" => Permission::model()->hasSurveyPermission($oQuestion->sid, 'survey', 'update'),
"editorpreset" => App()->session['htmleditormode'],
"script" =>
Permission::model()->hasSurveyPermission($oQuestion->sid, 'survey', 'update')
&& SettingsUser::getUserSetting('showScriptEdit', App()->user->id),
];

$this->renderJSON($aPermissions);
}


/** ++++++++++++ the following functions should be moved to model or a service clas ++++++++++++++++++++++++++ */

/**
* Creates a question object
* This is either an instance of the placeholder model QuestionCreate for new questions,
* or of Question for already existing ones
*
* todo: this should be moved to model ...
*
* @param int $iQuestionId
* @param string $sQuestionType
* @param int $gid
* @return Question
* @throws CException
*/
private function getQuestionObject($iQuestionId = null, $sQuestionType = null, $gid = null)
{
$iSurveyId = App()->request->getParam('sid') ?? App()->request->getParam('surveyid'); //todo: this should be done in the action directly
$oQuestion = Question::model()->findByPk($iQuestionId);

if ($oQuestion == null) {
$oQuestion = QuestionCreate::getInstance($iSurveyId, $sQuestionType);
}

if ($sQuestionType != null) {
$oQuestion->type = $sQuestionType;
}

if ($gid != null) {
$oQuestion->gid = $gid;
}

return $oQuestion;
}

}
15 changes: 14 additions & 1 deletion application/controllers/admin/questionedit.php
Expand Up @@ -26,6 +26,8 @@ class questionedit extends Survey_Common_Action
/**
* Main view function prepares the necessary global js parts and renders the HTML
*
* REFACTORED in QuestionEditorController
*
* @param integer $surveyid
* @param integer $gid
* @param integer $qid
Expand Down Expand Up @@ -420,6 +422,8 @@ function ($lngString) use (&$aLanguages, $aAllLanguages) {
* Collect initial question data
* This either creates a temporary question object, or calls a question object from the database
*
* * REFACTORED in QuestionEditorController
*
* @param int $iQuestionId
* @param int $gid
* @param string $type
Expand Down Expand Up @@ -460,6 +464,8 @@ function ($lngString) use (&$aLanguages, $aAllLanguages) {
/**
* Collect the permissions available for a specific question
*
* REFACTORED in QuestionEditorController
*
* @param $iQuestionId
*
* @return void
Expand Down Expand Up @@ -560,6 +566,8 @@ public function getGeneralOptions(
* This is a controller action and also a used function called by other actions.
* It returns a preformatted array of advanced settings.
*
* REFACTORED in QuestionEditorController
*
* @param int $iQuestionId
* @param string $sQuestionType
* @param boolean $returnArray
Expand Down Expand Up @@ -1361,6 +1369,12 @@ private function getCompiledQuestionData(&$oQuestion)
];
}

/**
* REFACTORED in QuestionEditorController
*
* @param $oQuestion
* @return array
*/
private function getCompiledSurveyInfo(&$oQuestion) {
$oSurvey = $oQuestion->survey;
$aQuestionTitles = $oCommand = Yii::app()->db->createCommand()
Expand All @@ -1378,7 +1392,6 @@ private function getCompiledSurveyInfo(&$oQuestion) {
"questionCount" => $questionCount,
"groupCount" => $groupCount,
];

}

/**
Expand Down

0 comments on commit 7be516f

Please sign in to comment.