/**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate()
 {
     $this->pageTitle = 'Examen - Modificar';
     $id = isset($_REQUEST["id"]) ? $_REQUEST["id"] : $_REQUEST["Examen"]["id"];
     $model = $this->loadModel($id);
     $model->fechaExamen = Utils::dateToDMY($model->fechaExamen);
     // Uncomment the following line if AJAX validation is needed
     //$this->performAjaxValidation($model,"examen-update-form");
     if (isset($_POST['Examen'])) {
         //Si se selecciona un tipo de examen para que no falle la validacion se setea algo al atributo
         if ($_POST['Examen']['tipoexamen_id'] != -1) {
             $_POST['Examen']['TipoExamenPersonalizado'] = "no_vacio :)";
         }
         $transaction = $model->dbConnection->beginTransaction();
         try {
             //Si soy administrador obtendo el materia_id desde el form sino desde el usuario
             $mat_id = Yii::app()->user->isadmin() ? $_POST['Examen']['materia_id'] : Yii::app()->user->name;
             $model->attributes = $_POST['Examen'];
             //$model->fechaExamen=$this->dateToYMD($_POST['Examen']['fechaExamen']);
             if ($_POST['Examen']['tipoexamen_id'] == -1) {
                 //Se eligio un tipo nuevo, se inserta en la base de datos y luego se obtiene el id para
                 //insertarlo en examen
                 $tipoexamen = new Tipoexamen();
                 $tipoexamen->nombreTipoExamen = $_POST['Examen']['TipoExamenPersonalizado'];
                 $tipoexamen->Materia_id = $mat_id;
                 $tipoexamen->save();
                 $lastInsert = Yii::app()->db->getLastInsertID();
                 $model->tipoexamen_id = $lastInsert;
             }
             if ($model->save()) {
                 $transaction->commit();
                 $this->redirect(array('index'));
             }
         } catch (Exception $e) {
             $transaction->rollBack();
             throw new CHttpException('Se produjo un error al intentar almacenar los datos. Contacte al administrador.');
         }
     }
     $this->render('update', array('model' => $model));
 }
Exemplo n.º 2
0
 public function insertWithoutFail($materia_id, $nombreTipoExamen)
 {
     $criteria = new CDbCriteria();
     $criteria->select = 'id';
     $criteria->condition = 'LOWER(nombreTipoExamen)=:nombreTipoExamen AND Materia_id=:Materia_id';
     $criteria->params = array(':nombreTipoExamen' => strtolower($nombreTipoExamen), ':Materia_id' => $materia_id);
     $result = $this->find($criteria);
     if ($result != null) {
         return $result->id;
     } else {
         $tipo = new Tipoexamen();
         $tipo->nombreTipoExamen = $nombreTipoExamen;
         $tipo->Materia_id = $materia_id;
         $tipo->save();
         return $tipo->id;
     }
 }