Exemplo n.º 1
0
 /**
  * Добавление/обновление теста
  * @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;
     }
 }