Browse Source

Debut algo resolution

isundil 10 years ago
parent
commit
c90812e334

+ 10 - 0
src/classes/Secret.Entity.php

@@ -11,6 +11,7 @@ class SecretEntity extends \Entity
 	private $description;
 	private $admin;
 	private $id;
+	private $result;
 
 	public function __construct($data)
 	{
@@ -19,6 +20,7 @@ class SecretEntity extends \Entity
 		$this->description = $data["description"];
 		$this->admin = $data["admin"];
 		$this->id = $data["id"];
+		$this->result = $data["result"];
 	}
 
 	public function getName() { return $this->name; }
@@ -27,6 +29,7 @@ class SecretEntity extends \Entity
 	public function getId() { return hash::encode($this->id); }
 	public function getUsers() { return UserEntity::getForCampain($this->id); }
 	public function getConstraints() { return ConstraintEntity::getForCampain($this->id); }
+	public function isFinished() { return $this->result !== null; }
 
 	public function addUser($email, $name)
 	{
@@ -51,6 +54,13 @@ class SecretEntity extends \Entity
 		return hash::encode($id);
 	}
 
+	public function setResult($result)
+	{
+		$this->result = $result;
+		$req = self::getPdo()->prepare("UPDATE `campain` SET `result`=? WHERE `id`={$this->id}");
+		var_dump($req->execute(array($result)));
+	}
+
 	public static function fromId($id)
 	{
 		$rId = (int) hash::decode($id);

+ 47 - 0
src/classes/SecretRandomizer.class.php

@@ -0,0 +1,47 @@
+<?php
+
+require_once(__DIR__.'/SecretResult.class.php');
+
+class SecretRandomizer
+{
+	private $campain;
+	private $users;
+	private $constraints;
+	private $result;
+
+	public function __construct($campain, $users, $constraints)
+	{
+		$this->campain = $campain;
+		$this->users = $users;
+		$this->constraints = $constraints;
+		$this->result = null;
+	}
+
+	public function getResult()
+	{
+		if ($this->result !== null)
+			return $this->result;
+		return ($this->result = $this->_getResult());
+	}
+
+	private function _getResult()
+	{
+		$r = new SecretResult();
+		$candidates = array();
+		$allUsers = array();
+		foreach ($this->users as $i)
+			$allUsers[] = $i->getEmail();
+		foreach ($allUsers as $i)
+		{
+			$candidates[$i] = array_merge($allUsers);
+			unset($candidates[$i][array_search($i, $candidates[$i])]);
+		}
+		foreach ($this->constraints as $i)
+		{
+			unset($candidates[$i->getUserA()][array_search($i->getUserB(), $candidates[$i->getUserA()])]);
+			unset($candidates[$i->getUserB()][array_search($i->getUserA(), $candidates[$i->getUserB()])]);
+		}
+		return $r;
+	}
+}
+

+ 14 - 0
src/classes/SecretResult.class.php

@@ -0,0 +1,14 @@
+<?php
+
+class SecretResult
+{
+	public function toString()
+	{
+		return "result";
+	}
+
+	public function sendMails()
+	{
+	}
+}
+

+ 16 - 5
src/hash.php

@@ -1,6 +1,7 @@
 <?php
 
 require_once(dirname(__FILE__) . '/classes/Controller.php');
+require_once(dirname(__FILE__) . '/classes/SecretRandomizer.class.php');
 require_once(__DIR__.'/classes/Secret.Entity.php');
 
 class CreateController extends Controller
@@ -13,13 +14,13 @@ class CreateController extends Controller
 	{
 		parent::__construct();
 		$this->campain = SecretEntity::fromId($request);
-		$this->users = $this->campain->getUsers();
-		$this->constraints = $this->campain->getConstraints();
 		if ($this->campain === false)
 		{
 			$this->render("404");
 			die;
 		}
+		$this->users = $this->campain->getUsers();
+		$this->constraints = $this->campain->getConstraints();
 		if (!empty($_POST))
 			$this->managePost();
 		$this->render('hash');
@@ -39,10 +40,15 @@ class CreateController extends Controller
 
 	public function managePost()
 	{
-		if (!empty($_POST['cemail']))
+		if (!empty($_POST['launch']))
 		{
-			$this->campain->addUser($_POST['cemail'], $_POST['cname']);
-			$this->users = $this->campain->getUsers();
+			$s = new SecretRandomizer($this->campain, $this->users, $this->constraints);
+			$result = $s->getResult();
+			var_dump($result);
+			die;
+			$result->sendMails();
+			$this->campain->setResult($result->toString());
+			return;
 		}
 		$this->campain->removeConstraints();
 		$groups = array();
@@ -56,6 +62,11 @@ class CreateController extends Controller
 					$this->campain->addConstraint($i, $j);
 			}
 		$this->constraints = $this->campain->getConstraints();
+		if (!empty($_POST['cemail']))
+		{
+			$this->campain->addUser($_POST['cemail'], $_POST['cname']);
+			$this->users = $this->campain->getUsers();
+		}
 	}
 }
 

+ 12 - 1
src/views/hash.php

@@ -25,12 +25,14 @@
 					?><input type="checkbox" value="1" name="<?php echo md5($i->getEmail()).'-'.md5($j->getEmail()); ?>" <?php
 					if (!$this->hasConstraint($i, $j))
 						echo " checked";
+					if ($this->getCampain()->isFinished())
+						echo " disabled";
 					echo '/>';
 				}
 				echo '</td>';
 				endforeach; ?>
 			</tr>
-			<?php endforeach; ?>
+			<?php endforeach; if (!$this->getCampain()->isFinished()): ?>
 			<tr>
 				<td>
 					<div class="form-group">
@@ -43,8 +45,17 @@
 				<td></td>
 				<?php endfor; ?>
 			</tr>
+			<?php endif; ?>
 		</tbody>
 	</table>
+	<?php if (!$this->getCampain()->isFinished()): ?>
 	<input type="submit" class="btn btn-default" value="Valider" />
+	<?php endif; ?>
 </form>
+<?php if (!$this->getCampain()->isFinished()): ?>
+<form method="POST">
+	<input type="hidden" name="launch" value="1" />
+	<input type="submit" class="btn btn-default" value="Confirmer et envoyer les e-mails" />
+</form>
+<?php endif; ?>
 <?php require(dirname(__FILE__).'/footer.inc.php'); ?>