/** * Добавление/обновление вопроса * @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; } }
/** * Добавление/обновление теста * @return void */ public function editAction() { if ($this->_authorize('test', 'edit')) { $objForm = new Form_Test_Edit(); $objCategories = new Categories(); $arrCategory = $objCategories->getCategoryList(); $objForm->setSelectOptions($arrCategory); if ($this->getRequest()->isPost()) { if ($objForm->isValid($_POST)) { // Выполняем update (insert/update данных о категории) $objTests = new Tests(); $testId = $objForm->testId->getValue(); if (!empty($testId)) { $objTest = $objTests->getTestById($testId); } else { $testId = null; $objTest = $objTests->createRow(); } $testName = $objForm->testName->getValue(); $objTest->setName($testName); $objTest->setCategoryId($objForm->categoryId->getValue()); $objTest->setTime($objForm->testTime->getValue()); $objTest->setQuestionAmount((int) $objForm->testQuestionAmount->getValue()); $objTest->save(); if ('questionAdd' == $objForm->formAction->getValue()) { if (!$testId) { $testId = $objTests->getAdapter()->lastInsertId(); } $this->_helper->redirector('edit', 'question', null, array('testId' => $testId)); } else { $this->_helper->redirector('index', 'test'); } } else { $testId = $this->getRequest()->getParam('testId'); if ($testId != '') { // выбираем из базы данные о редактируемом тесте $objTests = new Tests(); $objTest = $objTests->getTestById($testId); if ($objTest) { $arrQuestion = $objTests->getQuestionListByTestId($testId); $arrQuestionCategories = $objTests->getQuestionCategoriesListByTestId($testId); $this->view->arrQuestion = $arrQuestion; $this->view->arrQuestionCategories = $arrQuestionCategories; // $this -> view -> testId = $testId; } } } } else { $testId = $this->getRequest()->getParam('testId'); if ($testId != '') { // выбираем из базы данные о редактируемом тесте $objTests = new Tests(); $objTest = $objTests->getTestById($testId); if ($objTest) { $arrQuestion = $objTests->getQuestionListByTestId($testId); $arrQuestionCategories = $objTests->getQuestionCategoriesListByTestId($testId); $this->view->arrQuestion = $arrQuestion; $this->view->arrQuestionCategories = $arrQuestionCategories; $this->view->testId = $testId; $objForm->populate(array('testName' => $objTest->t_name, 'categoryId' => $objTest->cat_id, 'testTime' => $objTest->time, 'testQuestionAmount' => sizeof($arrQuestion), 'testId' => $objTest->t_id)); } } } $this->view->objForm = $objForm; } }