2019-03-08 09:22:37 +01:00
< ? php
App :: uses ( 'AppController' , 'Controller' );
class DecayingModelController extends AppController
{
public $components = array ( 'Security' , 'RequestHandler' );
public $paginate = array (
'limit' => 50 ,
'order' => array (
'DecayingModel.name' => 'asc'
)
);
2019-04-03 10:05:45 +02:00
public function update ( $force = false ) {
if ( ! $this -> _isSiteAdmin ()) {
throw new MethodNotAllowedException ( _ ( 'You are not authorised to edit it.' ));
}
if ( $this -> request -> is ( 'post' )) {
$this -> DecayingModel -> update ( $force );
$message = 'Default decaying models updated' ;
if ( $this -> _isRest ()) {
return $this -> RestResponse -> saveSuccessResponse ( 'DecayingModel' , 'update' , false , $this -> response -> type (), $message );
} else {
$this -> Flash -> success ( $message );
$this -> redirect ( array ( 'controller' => 'decayingModel' , 'action' => 'index' ));
// return $this->RestResponse->viewData($message, $this->response->type());
}
} else {
2019-04-04 09:46:57 +02:00
throw new Exception ( _ ( " This method is not allowed " ));
2019-04-03 10:05:45 +02:00
}
}
2019-03-08 09:22:37 +01:00
public function view ( $id ) {
if ( ! $this -> request -> is ( 'get' )) {
throw new Exception ( " This method is not allowed " );
}
2019-04-03 10:05:45 +02:00
$decaying_model = $this -> DecayingModel -> checkAuthorisation ( $this -> Auth -> user (), $id , true );
2019-03-08 09:22:37 +01:00
if ( ! $this -> _isSiteAdmin () && ! $decModel ) {
2019-04-04 09:46:57 +02:00
throw new MethodNotAllowedException ( _ ( 'No Decaying Model with the provided ID exists, or you are not authorised to edit it.' ));
2019-03-08 09:22:37 +01:00
}
$this -> set ( 'mayModify' , true );
$this -> set ( 'id' , $id );
2019-04-03 10:05:45 +02:00
$this -> set ( 'decaying_model' , $decaying_model );
2019-03-08 09:22:37 +01:00
}
public function index () {
$conditions = array ();
if ( ! $this -> _isSiteAdmin ()) {
$conditions [ 'OR' ] = array ( 'org_id' => $this -> Auth -> user ( 'Organisation' )[ 'id' ]);
}
if ( ! $this -> _isSiteAdmin ()) {
$this -> paginate = Set :: merge ( $this -> paginate , array (
'conditions' => $conditions
));
}
$this -> set ( 'decayingModel' , $this -> paginate ());
}
public function add () {
if ( $this -> request -> is ( 'post' )) {
if ( ! isset ( $this -> request -> data [ 'DecayingModel' ][ 'org_id' ])) {
$this -> request -> data [ 'DecayingModel' ][ 'org_id' ] = $this -> Auth -> user ()[ 'org_id' ];
}
if ( empty ( $this -> request -> data [ 'DecayingModel' ][ 'name' ])) {
2019-04-04 09:46:57 +02:00
throw new MethodNotAllowedException ( _ ( " The model must have a name " ));
2019-03-08 09:22:37 +01:00
}
if ( $this -> DecayingModel -> save ( $this -> request -> data )) {
2019-03-08 16:19:07 +01:00
if ( $this -> request -> is ( 'ajax' )) {
$saved = $this -> DecayingModel -> checkAuthorisation ( $this -> Auth -> user (), $this -> DecayingModel -> id );
$response = array ( 'data' => $saved , 'action' => 'add' );
return $this -> RestResponse -> viewData ( $response , $this -> response -> type ());
} else {
2019-04-04 09:46:57 +02:00
$this -> Flash -> success ( _ ( 'The model has been saved.' ));
2019-03-08 16:19:07 +01:00
$this -> redirect ( array ( 'action' => 'index' ));
}
2019-03-08 09:22:37 +01:00
}
}
}
public function edit ( $id ) {
$decayingModel = $this -> DecayingModel -> checkAuthorisation ( $this -> Auth -> user (), $id );
if ( ! $this -> _isSiteAdmin () && ! $decModel ) {
2019-04-04 09:46:57 +02:00
throw new NotFoundException ( _ ( 'No Decaying Model with the provided ID exists, or you are not authorised to edit it.' ));
2019-03-08 09:22:37 +01:00
}
$this -> set ( 'mayModify' , true );
2019-03-08 16:19:07 +01:00
if ( $this -> request -> is ( 'post' ) || $this -> request -> is ( 'put' )) {
2019-03-08 09:22:37 +01:00
$this -> request -> data [ 'DecayingModel' ][ 'id' ] = $id ;
2019-03-11 08:58:32 +01:00
if ( ! isset ( $this -> request -> data [ 'DecayingModel' ][ 'parameters' ])) {
$this -> request -> data [ 'DecayingModel' ][ 'parameters' ] = array ();
} else {
if ( ! isset ( $this -> request -> data [ 'DecayingModel' ][ 'parameters' ][ 'tau' ])) {
2019-04-04 09:46:57 +02:00
$this -> Flash -> error ( _ ( 'Invalid parameter `tau`.' ));
2019-03-11 08:58:32 +01:00
return true ;
}
if ( ! isset ( $this -> request -> data [ 'DecayingModel' ][ 'parameters' ][ 'delta' ])) {
2019-04-04 09:46:57 +02:00
$this -> Flash -> error ( _ ( 'Invalid parameter `delta`.' ));
2019-03-11 08:58:32 +01:00
return true ;
}
if ( ! isset ( $this -> request -> data [ 'DecayingModel' ][ 'parameters' ][ 'threshold' ])) {
2019-04-04 09:46:57 +02:00
$this -> Flash -> error ( _ ( 'Invalid parameter `threshold`.' ));
2019-03-11 08:58:32 +01:00
return true ;
}
}
$this -> request -> data [ 'DecayingModel' ][ 'parameters' ] = json_encode ( $this -> request -> data [ 'DecayingModel' ][ 'parameters' ]);
2019-03-08 16:19:07 +01:00
$fieldList = array ( 'name' , 'description' , 'parameters' );
if ( $this -> DecayingModel -> save ( $this -> request -> data , true , $fieldList )) {
if ( $this -> request -> is ( 'ajax' )) {
$saved = $this -> DecayingModel -> checkAuthorisation ( $this -> Auth -> user (), $this -> DecayingModel -> id );
$response = array ( 'data' => $saved , 'action' => 'edit' );
return $this -> RestResponse -> viewData ( $response , $this -> response -> type ());
} else {
2019-04-04 09:46:57 +02:00
$this -> Flash -> success ( _ ( 'The model has been saved.' ));
2019-03-08 16:19:07 +01:00
$this -> redirect ( array ( 'action' => 'index' ));
}
2019-03-08 09:22:37 +01:00
}
}
$this -> request -> data = $decayingModel ;
$this -> set ( 'id' , $id );
$this -> set ( 'decayingModel' , $decayingModel );
}
public function delete ( $id ) {
if ( $this -> request -> is ( 'post' )) {
$decayingModel = $this -> DecayingModel -> checkAuthorisation ( $this -> Auth -> user (), $id );
if ( ! $this -> _isSiteAdmin () && ! $decModel ) {
2019-04-04 09:46:57 +02:00
throw new MethodNotAllowedException ( _ ( 'No Decaying Model with the provided ID exists, or you are not authorised to edit it.' ));
2019-03-08 09:22:37 +01:00
}
if ( $this -> DecayingModel -> delete ( $id , true )) {
2019-04-04 09:46:57 +02:00
$this -> Flash -> success ( _ ( 'Decaying Model deleted.' ));
2019-03-08 09:22:37 +01:00
$this -> redirect ( array ( 'action' => 'index' ));
} else {
2019-04-04 09:46:57 +02:00
$this -> Flash -> error ( _ ( 'The Decaying Model could not be deleted.' ));
2019-03-08 09:22:37 +01:00
$this -> redirect ( array ( 'action' => 'index' ));
}
}
}
public function decayingTool () {
$parameters = array (
2019-03-19 11:00:52 +01:00
'Tau' => array ( 'value' => 30 , 'step' => 1 , 'max' => 365 , 'greek' => 'τ' , 'unit' => 'days' , 'name' => 'Lifetime' , 'info' => 'Lifetime of the attribute, or time after which the score will be 0' ),
'Delta' => array ( 'value' => 0.3 , 'step' => 0.1 , 'max' => 10 , 'greek' => 'δ' , 'name' => 'Decay speed' , 'info' => 'Decay speed at which an indicator will loose score' ),
'Threshold' => array ( 'value' => 30 , 'step' => 1 , 'name' => 'Cutoff threshold' , 'info' => 'Cutoff value at which an indicator will be marked as decayed instead of 0' )
2019-03-08 09:22:37 +01:00
);
$types = $this -> User -> Event -> Attribute -> typeDefinitions ;
2019-03-19 11:00:52 +01:00
// $types = array_filter($types, function($v, $k) {
// return $v['to_ids'] == 1;
// }, ARRAY_FILTER_USE_BOTH);
2019-03-19 16:04:47 +01:00
$this -> loadModel ( 'ObjectTemplateElement' );
$objectTypes = $this -> ObjectTemplateElement -> getAllAvailableTypes ();
array_walk ( $objectTypes , function ( & $key ) {
$key [ " isObject " ] = true ;
$key [ " default_category " ] = $key [ " category " ];
});
$types = array_merge ( $types , $objectTypes );
2019-03-08 09:22:37 +01:00
ksort ( $types );
$savedDecayingModels = $this -> DecayingModel -> fetchAllowedModels ( $this -> Auth -> user ());
$this -> set ( 'parameters' , $parameters );
$this -> set ( 'types' , $types );
$this -> set ( 'savedModels' , $savedDecayingModels );
2019-04-08 16:28:49 +02:00
$associated_models = $this -> DecayingModel -> DecayingModelMapping -> getAssociatedModels ( $this -> Auth -> user ());
$this -> set ( 'associated_models' , $associated_models );
$associated_types = array ();
foreach ( $associated_models as $type => $models ) {
foreach ( array_keys ( $models ) as $model_id ) {
$associated_types [ $model_id ][] = $type ;
}
}
$this -> set ( 'associated_types' , $associated_types );
2019-03-08 09:22:37 +01:00
}
}