public function actionNdff() { echo "=== Starting export sequence at " . date("d-m-Y h:i") . " ===\r\n"; echo "Fetching all records from the 'observations' table where ndff_id is empty or ndiff_failed is filled\r\n"; $observations = Observations::find()->where(['or', ['is', 'ndff_id', NULL], ['not', ['ndff_failed' => NULL]]]); echo "Found " . $observations->count() . " records, continuing\r\n"; echo "Looping trough all the results\r\n"; foreach ($observations->all() as $observation) { if ($observation->observation_method == Observations::OBSERVATION_TYPE_NULL) { continue; } $ndffObservation = new \NDFF\Observation(); $ndffObservation->setLifestage('http://ndff-ecogrid.nl/codes/domainvalues/observation/lifestages/unknown'); $ndffObservation->setLocation(0, '', 'http://ndff-ecogrid.nl/codes/locationtypes/point'); $ndffObservation->setPeriodstart(date('o-m-d\\TH:i:s', strtotime($observation->visit->date))); $ndffObservation->setScaleidentity('http://ndff-ecogrid.nl/codes/scales/exact_count'); if ($observation->catch_sex != Observations::CATCH_SEX_UNKNOWN) { $sexUrlParameter = $observation->catch_sex == Observations::CATCH_SEX_MALE ? 'male' : 'female'; $ndffObservation->setSex('http://ndff-ecogrid.nl/codes/domainvalues/observation/sexes/' . $sexUrlParameter); } $ndffObservation->setSubjecttypeidentity('http://ndff-ecogrid.nl/codes/subjecttypes/live/individual'); echo "Attempting to write observation " . $observation->id . " to NDFF database\r\n"; $ndffRequest = new \NDFF\ApiRequest('telmee', 'onsjos', '********'); $ndffRequest->set_request_data($ndffObservation); $ndffRequest->resource_post('observation'); } echo "All done!\r\n"; }
public function actionDetail($id) { $visit = Visits::findOne($id); if (!is_object($visit) || !$visit->isAuthorized()) { return $this->redirect(Url::toRoute('visits/index')); } return $this->render('detail', ['visit' => $visit, 'observations' => Observations::find()->all()]); }
public function actionForm($id = NULL) { $model = $id ? Observations::findOne($id) : new Observations(); if (Yii::$app->getRequest()->getQueryParam('visit_id') && is_null($id)) { $model->visit_id = Yii::$app->getRequest()->getQueryParam('visit_id'); } if ($model->load(Yii::$app->request->post())) { /* Load picture file */ $model->pictureFile = UploadedFile::getInstance($model, 'pictureFile'); /* Validate model */ if ($model->validate()) { /* Upload picture */ if ($model->pictureFile) { $model->upload(); } /* Delete picture if selected */ if ($model->deleteImage && !$model->pictureFile) { $model->picture = NULL; } /* Check for auto-validation on observqation */ if (Yii::$app->user->getIdentity()->hasRole(['validator', 'administrator'])) { $model->markAsValidated(); } /* When the observation type equals a null-observation, clear all other fields */ if ($model->observation_type == Observations::OBSERVATION_TYPE_NULL) { foreach ($model->attributes as $attribute => $value) { if (!in_array($attribute, ['id', 'visit_id', 'observation_type', 'validated_by_id', 'validated_date', 'box_id', 'date_created', 'date_updated', 'deleted'])) { $model->{$attribute} = NULL; } } } $model->save(false); if ($model->observation_type != Observations::OBSERVATION_TYPE_NULL && Observations::find()->where(['and', ['deleted' => false], ['box_id' => $model->box_id], ['observation_type' => Observations::OBSERVATION_TYPE_NULL]])->exists()) { foreach (Observations::find()->where(['and', ['deleted' => false], ['observation_type' => Observations::OBSERVATION_TYPE_NULL], ['box_id' => $model->box_id]])->all() as $nullObservation) { $nullObservation->delete(); } } return $this->redirect(Url::toRoute(['visits/detail/' . $model->visit_id])); } } return $this->render('form', ['model' => $model, 'boxes' => Boxes::find()->where(['project_id' => Visits::findOne($model->visit_id)->project_id])->all(), 'species' => Species::find()->asArray()->all(), 'parasites' => Species::find()->where(['taxon' => Species::TAXONOMY_ARTHROPOD])->all()]); }
public function search($params, $personal = false) { /* User identifier */ $userIdentifier = \Yii::$app->user->getId() ? \Yii::$app->user->getId() : 0; /* Base query */ $query = Observations::find(); /* Joins */ $query->join('LEFT JOIN', 'boxes', '"observations"."box_id" = "boxes"."id" AND "boxes"."deleted" = FALSE'); $query->join('LEFT JOIN', 'visits', '"observations"."visit_id" = "visits"."id" AND "visits"."deleted" = FALSE'); $query->join('LEFT JOIN', 'projects', '"boxes"."project_id" = "projects"."id" AND "projects"."deleted" = FALSE'); $query->join('LEFT JOIN', 'project_counters', '"projects"."id" = "project_counters"."project_id" AND "project_counters"."user_id" = :user_id', ['user_id' => $userIdentifier]); /* Conditions */ $query->where(['and', ['observations.deleted' => false], ['not', ['visits.id' => NULL]], ['not', ['boxes.id' => NULL]], ['not', ['projects.id' => NULL]], ['or', ['is', '"visits"."embargo"', NULL], ['<=', '"visits"."embargo"', 'NOW()'], ['in', 'visit_id', (new Query())->select('id')->from(VisitObservers::tableName())->where(['observer_id' => $userIdentifier])]], ['<>', 'observations.observation_type', Observations::OBSERVATION_TYPE_NULL]]); if (!is_object(Yii::$app->user->getIdentity()) || is_object(Yii::$app->user->getIdentity()) && !Yii::$app->user->getIdentity()->hasRole(['validator', 'administrator'])) { $query->andWhere(['or', ['"projects"."owner_id"' => $userIdentifier], ['"projects"."main_observer_id"' => $userIdentifier], ['or', ['is', '"projects"."embargo"', NULL], ['<=', '"projects"."embargo"', 'NOW()']], ['is not', '"project_counters"."user_id"', NULL]]); } $query->andWhere(['and', ['"projects"."deleted"' => false], ['"boxes"."deleted"' => false], ['"visits"."deleted"' => false], ['"observations"."deleted"' => false]]); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); return $dataProvider; }
public static function hasNotNullObservations($visit_id, $observation_id = NULL) { return Observations::find()->where(['and', ['visit_id' => $visit_id], ['<>', 'observation_type', Observations::OBSERVATION_TYPE_NULL], ['<>', 'id', $observation_id], ['deleted' => false]])->exists() ? true : false; }
<?php use fedemotta\datatables\DataTables; use yii\helpers\Url; use yii\helpers\Html; use app\models\Visits; use app\models\Observations; use app\models\Boxtypes; use app\models\ProjectClusters; ?> <div class="table-responsive"> <?php echo DataTables::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['attribute' => 'code', 'format' => 'html', 'value' => function ($model, $key, $index, $column) { return Html::a($model['code'], Url::toRoute('boxes/detail/' . $model['id'])); }], ['attribute' => 'boxtype_id', 'format' => 'html', 'label' => 'Boxtype', 'value' => function ($model, $key, $index, $column) { $boxType = $model['boxtype_id'] ? Boxtypes::findOne($model['boxtype_id']) : NULL; return $boxType ? Html::a($boxType->model, Url::toRoute('boxtypes/detail/' . $boxType->id)) : "-"; }], ['attribute' => 'cluster_id', 'format' => 'html', 'label' => 'Cluster', 'value' => function ($model, $key, $index, $column) { $cluster = $model['cluster_id'] ? ProjectClusters::findOne($model['cluster_id']) : NULL; return $cluster ? $cluster->cluster : '-'; }], ['attribute' => 'placement_date', 'label' => Yii::t('app', 'Plaatsingsdatum')], ['label' => Yii::t('app', 'Laatste waarneming'), 'value' => function ($model, $key, $index, $column) { if (($observationModel = Observations::find()->byBox($model['id'])->one()) !== NULL) { return Visits::findOne($observationModel->visit_id)->date; } else { return "-"; } }]], 'clientOptions' => ['info' => false, 'responsive' => true, 'dom' => 'lfTrtip', 'tableTools' => ['aButtons' => [['sExtends' => 'copy', 'sButtonText' => Yii::t('app', 'Copy to clipboard')], ['sExtends' => 'csv', 'sButtonText' => Yii::t('app', 'Save to CSV')], ['sExtends' => 'pdf', 'sButtonText' => Yii::t('app', 'Save to PDF')], ['sExtends' => 'print', 'sButtonText' => Yii::t('app', 'Print')]]]]]); ?> </div>