contributions/voting/behaviour/commands/base/createvotes.cmd.php
Go to the documentation of this file.00001 <?php 00002 /** 00003 * Comamdn to create a vote 00004 */ 00005 class CreateVotesBaseCommand extends CommandChain { 00006 protected function do_execute() { 00007 $params = $this->get_params(); 00008 $ret = new Status(); 00009 00010 Load::commands('generics/create'); 00011 $create_cmd = new CreateCommand('votes', $params); 00012 $ret->merge($create_cmd->execute()); 00013 /* @var $created_vote DAOVotes */ 00014 $created_vote = $create_cmd->get_result(); 00015 $this->set_result($created_vote); 00016 00017 if ($ret->is_ok()) { 00018 $ret->merge($this->on_success($created_vote)); 00019 } 00020 if ($ret->is_ok()) { 00021 $aggr_params = VotesAggregates::aggregate_for_instance($created_vote->instance); 00022 $ret->merge($this->aggregate($created_vote, $aggr_params)); 00023 } 00024 return $ret; 00025 } 00026 00027 /** 00028 * Called after vote was created 00029 * 00030 * @param DAOVotes $vote 00031 * @return Status 00032 */ 00033 protected function on_success(DAOVotes $vote) { 00034 Load::commands('generics/clearcache'); 00035 $this->append(new ClearCacheCommand($vote->instance)); 00036 00037 return new Status(); 00038 } 00039 00040 /** 00041 * Aggregates results and saves them 00042 * 00043 * @param DAOVotes $vote 00044 * @param array $params 00045 * @return Status 00046 */ 00047 protected function aggregate(DAOVotes $vote, $params) { 00048 $aggregate = VotesAggregates::get_for_instance($vote->instance); 00049 if ($aggregate) { 00050 // Update 00051 $this->append(CommandsFactory::create_command($aggregate, 'update', $params)); 00052 } 00053 else { 00054 // Create 00055 $this->append(CommandsFactory::create_command('votesaggregates', 'create', $params)); 00056 } 00057 00058 return new Status(); 00059 } 00060 00061 /** 00062 * Returns title of command. 00063 */ 00064 public function get_name() { 00065 return 'create'; 00066 } 00067 00068 }