Example #1
0
 /**
  * Добавление/обновление вопроса
  * @return void
  */
 public function editAction()
 {
     if ($this->_authorize('question', 'edit')) {
         $objForm = new Form_Question_Edit();
         $arrParams = $this->getRequest()->getParams();
         $testId = $this->getRequest()->getParam('testId');
         if ($testId != '') {
             // выбираем из базы категории вопросов
             $objCategories = new QuestionCategories();
             $arrCategories = $objCategories->getCategoryShortListByTestId($testId);
             $objForm->setCategoriesSelectOptions($arrCategories);
         }
         if ($this->getRequest()->isPost()) {
             if ($objForm->isValid($_POST)) {
                 // Выполняем update (insert/update данных о вопросе)
                 $strQuestionText = $objForm->questionText->getValue();
                 $intQuestionWeight = $objForm->questionWeight->getValue();
                 $objQuestions = new Questions();
                 $intMaxSortIndex = $objQuestions->getMaxSortIndex($testId);
                 $questionId = $objForm->questionId->getValue();
                 if (!empty($questionId)) {
                     $objQuestion = $objQuestions->getQuestionById($questionId);
                 } else {
                     $objQuestion = $objQuestions->createRow();
                     $questionId = null;
                     $objQuestion->setSortIndex($intMaxSortIndex + 1);
                 }
                 $objQuestion->setText($strQuestionText);
                 $objQuestion->setTestId($testId);
                 $objQuestion->setWeight($intQuestionWeight);
                 if ($arrCategories) {
                     $objQuestion->setCategoryId($objForm->categoryId->getValue());
                 }
                 if (array_key_exists('answer', $arrParams)) {
                     $intAnswerAmount = sizeof($arrParams['answer']);
                     $objQuestion->setAnswerAmount($intAnswerAmount);
                 }
                 $objQuestion->save();
                 if (!$questionId) {
                     $questionId = $objQuestions->getAdapter()->lastInsertId();
                 }
                 // Вносим в базу варианты ответов и обг=новляем их количество,
                 // поскольку не валидные в базу не добавляются и количество
                 // элементов в массиве $arrParams['answer'] может не совпадать
                 // с количеством ответов, фактически внесенных в БД
                 if (array_key_exists('answer', $arrParams) && !empty($arrParams['answer'])) {
                     $intAnswerAmount = $objQuestions->saveAnswerList($questionId, $arrParams['answer']);
                     $objQuestion->setAnswerAmount($intAnswerAmount);
                 }
                 $objQuestion->updateRightAnswersAmount();
                 $objQuestion->save();
                 $this->_helper->redirector('edit', 'test', null, array('testId' => $testId));
             } else {
                 if ($testId != '') {
                     // выбираем из базы данные о редактируемом тесте
                     $objTests = new Tests();
                     $objTest = $objTests->getTestById($testId);
                     $this->view->objTest = $objTest;
                 }
                 $arrAnswer = array();
                 if (array_key_exists('questionId', $arrParams) && !empty($arrParams['questionId'])) {
                     $questionId = (int) $arrParams['questionId'];
                     // выбираем из базы данные о редактируемом вопросе
                     $objQuestions = new Questions();
                     $objQuestion = $objQuestions->getQuestionById($questionId);
                     $arrAnswer = $objQuestions->getAnswerListByQuestionId($questionId);
                 }
                 $objForm->addAnswersSubForm($arrAnswer);
                 // @todo: пререформатировать массив answer, полученный через POST для функции addAnswersSubForm()
             }
         } else {
             $arrAnswer = array();
             if (array_key_exists('questionId', $arrParams) && !empty($arrParams['questionId'])) {
                 $questionId = (int) $arrParams['questionId'];
                 // выбираем из базы данные о редактируемом вопросе
                 $objQuestions = new Questions();
                 $objQuestion = $objQuestions->getQuestionById($questionId);
                 $arrAnswer = $objQuestions->getAnswerListByQuestionId($questionId);
                 $objForm->populate(array('questionText' => $objQuestion->tq_text, 'questionId' => $objQuestion->tq_id, 'questionWeight' => $objQuestion->getWeight(), 'categoryId' => $objQuestion->getCategoryId(), 'testId' => $testId));
             }
             $objForm->addAnswersSubForm($arrAnswer);
             if (!empty($testId)) {
                 // выбираем из базы данные о редактируемом тесте
                 $objTests = new Tests();
                 $objTest = $objTests->getTestById($testId);
                 $this->view->objTest = $objTest;
                 $objForm->populate(array('testId' => $testId));
             }
         }
         $this->view->objForm = $objForm;
     }
 }
Example #2
0
 /**
  * Get array of Question Categories by TestId
  *
  * @param int $testId
  * @return array | bool
  */
 public function getQuestionCategoriesListByTestId($testId)
 {
     $objQuestionsCategories = new QuestionCategories();
     return $objQuestionsCategories->getCategoryShortListByTestId($testId);
 }
 /**
  * Удаление категории
  * @return void
  */
 public function removeAction()
 {
     if ($this->_authorize('test', 'edit')) {
         $objCategories = new QuestionCategories();
         $arrParams = $this->getRequest()->getParams();
         $reqParams['testId'] = $arrParams['testId'];
         if (array_key_exists('categoryId', $arrParams) && !empty($arrParams['categoryId'])) {
             $objCategories->removeCategoryById($arrParams['categoryId']);
         }
         $this->_helper->redirector('index', 'questioncategory', null, $reqParams);
     }
 }