From 7be516f47121a80eca264b9760d812eb57b7e164 Mon Sep 17 00:00:00 2001 From: Patricia Stelling Date: Fri, 22 May 2020 13:03:42 +0200 Subject: [PATCH] DEV: implemented functions in new controller QuestionEditorController.php --- .../controllers/QuestionEditorController.php | 219 ++++++++++++++---- .../controllers/admin/questionedit.php | 15 +- 2 files changed, 194 insertions(+), 40 deletions(-) diff --git a/application/controllers/QuestionEditorController.php b/application/controllers/QuestionEditorController.php index e786b3c32c3..f157cd80160 100644 --- a/application/controllers/QuestionEditorController.php +++ b/application/controllers/QuestionEditorController.php @@ -25,7 +25,6 @@ public function accessRules() /** * This part comes from _renderWrappedTemplate * - * * @param string $view * @return bool */ @@ -39,9 +38,6 @@ 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'; } @@ -49,7 +45,7 @@ protected function 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 @@ -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. @@ -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']); @@ -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); @@ -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; + } + } \ No newline at end of file diff --git a/application/controllers/admin/questionedit.php b/application/controllers/admin/questionedit.php index ac84eb774b1..fad00ad5ad1 100644 --- a/application/controllers/admin/questionedit.php +++ b/application/controllers/admin/questionedit.php @@ -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 @@ -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 @@ -460,6 +464,8 @@ function ($lngString) use (&$aLanguages, $aAllLanguages) { /** * Collect the permissions available for a specific question * + * REFACTORED in QuestionEditorController + * * @param $iQuestionId * * @return void @@ -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 @@ -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() @@ -1378,7 +1392,6 @@ private function getCompiledSurveyInfo(&$oQuestion) { "questionCount" => $questionCount, "groupCount" => $groupCount, ]; - } /**