Permalink
Browse files

Cleanup of Method order in Generator class.

  • Loading branch information...
1 parent c250406 commit e10c8b3a4b85d9b7c6dc7faacd0559a8a620a3e5 @Potherca Potherca committed Feb 20, 2012
Showing with 121 additions and 117 deletions.
  1. +121 −117 class.Generator.php
View
238 class.Generator.php
@@ -16,6 +16,7 @@
class Generator
{
+////////////////////////////////// PROPERTIES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
protected $numberOfExercises=12;
protected $resultSize=28; // multiple of 4
@@ -35,83 +36,67 @@ class Generator
protected $assignment;
- function fetchAssignmentArray()
+////////////////////////////// GETTERS AND SETTERS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
+ public function getRandomLayout()
{
- $assignment = array();
-
- $assignmentTemp = $_POST['assignment'];
- $outcomeTemp = $_POST['outcome'];
-
- for ($i = 1; $i <= $this->getNumberOfExercises(); $i++) {
- $tempAssignment = $assignmentTemp[$i];
- $tempOutcome = $outcomeTemp[$i];
- //@FIXME: This creates a bug where, if two or more assignment have the same value, we overwrite $assignmentKeys and reduce the actual amount of keys available later on (also messing up the order.
- $assignment[$tempAssignment] = $tempOutcome;
- }#for
-
- return $assignment;
- }
-
- function getRandomLayout(){
return $this->layouts[array_rand($this->layouts)];
}
- function populateResultPreview(DOMElement $tableNode) {
-
- $allSlices = $this->buildSlicesForLayout();
+ public function getAssignment()
+ {
+ if(!isset($this->assignment)){
+ $this->assignment = $this->fetchAssignmentArray();
+ }#if
- $this->addTilesToTableNode($allSlices, $tableNode);
+ return $this->assignment;
}
- function addTilesToTableNode($allSlices, DOMElement $tableNode)
+ protected function getAssignmentKeys()
{
- $resultSize = $this->getResultSize();
-
- $template = $tableNode->ownerDocument;
-
- $tableBodyNode = $tableNode->getElementsByTagName('tbody')->item(0);
- /** @noinspection PhpUndefinedMethodInspection Method removeChildrenFromNode() is defined in the Template Class*/
- $template->removeChildrenFromNode($tableBodyNode);
+ $assignment = $this->getAssignment();
+ $assignmentKeys = array_keys($assignment);
+ return $assignmentKeys;
+ }
+ public function setNumberOfExercises($numberOfExercises)
+ {
+ $this->numberOfExercises = $numberOfExercises;
+ }
- foreach ($allSlices as $key => $value) {
- if ($key % 6 === 0) {
- // first cell of this row of 6
- $currentRow = $template->createElement('tr');
- $tableBodyNode->appendChild($currentRow);
- }#if
+ public function getNumberOfExercises()
+ {
+ return $this->numberOfExercises;
+ }
- createTile($resultSize, $value);
+ public function setResultSize($resultSize)
+ {
+ $this->resultSize = $resultSize;
+ }
- // @TODO: $tableCell and $image should be created outside of the loop and cloned here as the only thing that changes is the images "src" attribute
- $tableCell = $template->createElement('td');
- $tableCell->setAttribute('class', 'result');
+ public function getResultSize()
+ {
+ return $this->resultSize;
+ }
- $image = $template->createElement('img');
- $image->setAttribute('src', 'images/'.$value.'.png');
+ public function getLayout()
+ {
+ if(!isset($this->layout)){
+ $this->layout = $this->getRandomLayout();
+ }#if
- /** @noinspection PhpUndefinedVariableInspection */
- $currentRow->appendChild($tableCell);
- $tableCell->appendChild($image);
- }#foreach
+ return $this->layout;
}
- function buildSlicesForLayout()
+////////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
+ public function populateResultPreview(DOMElement $tableNode)
{
- $layout = $this->getLayout();
-
- $layoutFlip = array_flip($layout);
- ksort($layoutFlip);
- //flip rows..
- $upperRow = array_slice($layoutFlip, 6, 6);
- $lowerRow = array_slice($layoutFlip, 0, 6);
- $allSlices = array_merge($upperRow, $lowerRow);
+ $allSlices = $this->buildSlicesForLayout();
- return $allSlices;
+ $this->addTilesToTableNode($allSlices, $tableNode);
}
- function populateAssignmentContent(\DOMElement $assignmentTable)
+ public function populateAssignmentContent(\DOMElement $assignmentTable)
{
$numberOfExercises = $this->getNumberOfExercises();
@@ -152,8 +137,42 @@ function populateAssignmentContent(\DOMElement $assignmentTable)
#for
}
+ public function populateAssignmentResult(DOMElement $tableNode)
+ {
+ $assignmentKeys = $this->getAssignmentKeys();
+
+ $layout = $this->getLayout();
+ $template = $tableNode->ownerDocument;
+
+ $tableBodyNode = $tableNode->getElementsByTagName('tbody')->item(0);
+ /** @noinspection PhpUndefinedMethodInspection Method removeChildrenFromNode() is defined in the Template Class*/
+ $template->removeChildrenFromNode($tableBodyNode);
+
+ foreach ($layout as $key => $value) {
+ if (($key - 1) % 6 === 0) {
+ $currentRow = $template->createElement('tr');
+ $tableBodyNode->appendChild($currentRow);
+ }#if
+
+ $tableCell = $template->createElement('td');
+ $tableCell->setAttribute('class', 'assignment');
+
+ $font = $template->createElement('span', $key . ')');
+ $font->setAttribute('class', 'number');
- function populateResultContent(DOMElement $tableNode)
+ // $content .= "<br><br>";
+ $center = $template->createElement('center', $assignmentKeys[$key - 1]);
+ // $content .= "<br><br>";
+
+ $tableCell->appendChild($font);
+ $tableCell->appendChild($center);
+
+ /** @noinspection PhpUndefinedVariableInspection */
+ $currentRow->appendChild($tableCell);
+ }#foreach
+ }
+
+ public function populateResultContent(DOMElement $tableNode)
{
$layout = $this->getLayout();
$template = $tableNode->ownerDocument;
@@ -166,103 +185,88 @@ function populateResultContent(DOMElement $tableNode)
$layoutFlip = array_flip($layout);
ksort($layoutFlip);
- $count = 0;
foreach ($layoutFlip as $key => $value) {
$value--;
$assignmentKey = $assignmentKeys[$value];
- if ($count % 6 === 0) {
+ if (($key-1) % 6 === 0) {
$currentRow = $template->createElement('tr');
$tableBodyNode->appendChild($currentRow);
- }
+ }#if
$tableCell = $template->createElement('td', $assignment[$assignmentKey]);
$tableCell->setAttribute('class', 'answer');
/** @noinspection PhpUndefinedVariableInspection */
$currentRow->appendChild($tableCell);
+ }#foreach
+ }
- $count++;
- }
- #foreach
+
+//////////////////////////////// HELPER METHODS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
+ function fetchAssignmentArray()
+ {
+ $assignment = array();
+
+ $assignmentTemp = $_POST['assignment'];
+ $outcomeTemp = $_POST['outcome'];
+
+ for ($i = 1; $i <= $this->getNumberOfExercises(); $i++) {
+ $tempAssignment = $assignmentTemp[$i];
+ $tempOutcome = $outcomeTemp[$i];
+ //@FIXME: This creates a bug where, if two or more assignment have the same value, we overwrite $assignmentKeys and reduce the actual amount of keys available later on (also messing up the order.
+ $assignment[$tempAssignment] = $tempOutcome;
+ }#for
+
+ return $assignment;
}
- function populateAssignmentResult(DOMElement $tableNode)
+ function addTilesToTableNode($allSlices, DOMElement $tableNode)
{
- $assignmentKeys = $this->getAssignmentKeys();
+ $resultSize = $this->getResultSize();
- $layout = $this->getLayout();
$template = $tableNode->ownerDocument;
$tableBodyNode = $tableNode->getElementsByTagName('tbody')->item(0);
/** @noinspection PhpUndefinedMethodInspection Method removeChildrenFromNode() is defined in the Template Class*/
$template->removeChildrenFromNode($tableBodyNode);
- foreach ($layout as $key => $value) {
- if (($key - 1) % 6 === 0) {
+
+ foreach ($allSlices as $key => $value) {
+ if ($key % 6 === 0) {
+ // first cell of this row of 6
$currentRow = $template->createElement('tr');
$tableBodyNode->appendChild($currentRow);
}#if
- $tableCell = $template->createElement('td');
- $tableCell->setAttribute('class', 'assignment');
-
- $font = $template->createElement('span', $key . ')');
- $font->setAttribute('class', 'number');
+ createTile($resultSize, $value);
- // $content .= "<br><br>";
- $center = $template->createElement('center', $assignmentKeys[$key - 1]);
- // $content .= "<br><br>";
+ // @TODO: $tableCell and $image should be created outside of the loop and cloned here as the only thing that changes is the images "src" attribute
+ $tableCell = $template->createElement('td');
+ $tableCell->setAttribute('class', 'result');
- $tableCell->appendChild($font);
- $tableCell->appendChild($center);
+ $image = $template->createElement('img');
+ $image->setAttribute('src', 'images/'.$value.'.png');
/** @noinspection PhpUndefinedVariableInspection */
$currentRow->appendChild($tableCell);
+ $tableCell->appendChild($image);
}#foreach
}
- protected function getAssignmentKeys()
- {
- $assignment = $this->getAssignment();
- $assignmentKeys = array_keys($assignment);
- return $assignmentKeys;
- }
-
- public function setNumberOfExercises($numberOfExercises)
- {
- $this->numberOfExercises = $numberOfExercises;
- }
-
- public function getNumberOfExercises()
- {
- return $this->numberOfExercises;
- }
-
- public function setResultSize($resultSize)
- {
- $this->resultSize = $resultSize;
- }
-
- public function getResultSize()
- {
- return $this->resultSize;
- }
-
- public function getLayout()
+ function buildSlicesForLayout()
{
- if(!isset($this->layout)){
- $this->layout = $this->getRandomLayout();
- }#if
+ $layout = $this->getLayout();
- return $this->layout;
- }
+ $layoutFlip = array_flip($layout);
+ ksort($layoutFlip);
+ //flip rows..
+ $upperRow = array_slice($layoutFlip, 6, 6);
+ $lowerRow = array_slice($layoutFlip, 0, 6);
- public function getAssignment()
- {
- if(!isset($this->assignment)){
- $this->assignment = $this->fetchAssignmentArray();
- }#if
+ $allSlices = array_merge($upperRow, $lowerRow);
- return $this->assignment;
+ return $allSlices;
}
}
+
+#EOF

0 comments on commit e10c8b3

Please sign in to comment.