diff --git a/api/ui/Rearranger.php b/api/ui/Rearranger.php new file mode 100644 index 0000000000..f96720feb2 --- /dev/null +++ b/api/ui/Rearranger.php @@ -0,0 +1,100 @@ + $subarray) { + if ($key == $resultId) { // Check if we're in the right result config + foreach ($subarray as $jobtype => $jobs) { + if (gettype($jobs) == 'array') { + foreach ($jobs as $number => $job) { + // Check if this is the job we want to move + $found = $this->isInside($job, $goal); + if ($found) { + $array[$key][$jobtype] = $this->swap($jobs, $number, $direction); + return $array; + } + } + } + } + } + } + } + return $array; + } + + public function isInside($subarray, $goal) + { + if (gettype($subarray) == 'array') { + foreach ($subarray as $key => $element) { + if (gettype($element) == 'string' && $element == $goal) { + echo "Found " . $element . "\n"; + return true; + } + } + return false; + } + return false; + } + + public function swap($jobs, $goalkey, $direction) + { + $temp = ''; + $tempkey = 0; + $counter = 0; + if ($direction == 'up') { + foreach ($jobs as $number => $job) { + if ($number == $goalkey && $counter > 0) { + $jobs[$tempkey] = $job; + $jobs[$goalkey] = $temp; + return $jobs; + } else if ($number == $goalkey && $counter == 0) { + //found, but already at head + return $jobs; + } + $counter++; + $temp = $job; + $tempkey = $number; + } + } else if ($direction == 'down') { + $tempjob = ''; + foreach ($jobs as $number => $job) { + if ($tempjob != '') { + $jobs[$goalkey] = $job; + $jobs[$number] = $tempjob; + return $jobs; + } // if element is at last position, $temp will be set but nothing is swapped, foreach is over + else if ($number == $goalkey) { + $tempjob = $job; + } + } + } + return $jobs; + } +} \ No newline at end of file diff --git a/api/ui/results.php b/api/ui/results.php index ab79f90636..caf6cba93b 100644 --- a/api/ui/results.php +++ b/api/ui/results.php @@ -25,6 +25,8 @@ SOFTWARE. */ +include "Rearranger.php"; + class Results_API extends API { public $patch_access = Auth_Library::A_LOGGEDIN; @@ -68,9 +70,29 @@ public function get() { throw new Exception("Not enough data provided for action 'newplot'!"); } break; + case 'up': + $system = new System($this->get['systemId']); + $arr = $system->getResultsAll(); + $id = $this->get['uid']; + $resultId = $this->get['resultId']; + $arr = json_decode($arr, true); + $rearranger = new Rearranger(); + $arr['elements'] = $rearranger->seekAndSwap($arr['elements'], $id, 'up', $resultId); + $system->setResultsAll(json_encode($arr)); + break; + case 'down': + $system = new System($this->get['systemId']); + $arr = $system->getResultsAll(); + $id = $this->get['uid']; + $resultId = $this->get['resultId']; + $arr = json_decode($arr, true); + $rearranger = new Rearranger(); + $arr['elements'] = $rearranger->seekAndSwap($arr['elements'], $id, 'down', $resultId); + $system->setResultsAll(json_encode($arr)); + break; default: throw new Exception("Unknown action!"); } } } -} \ No newline at end of file +} diff --git a/core/api.php b/core/api.php index 4f98ac735e..09766803c7 100644 --- a/core/api.php +++ b/core/api.php @@ -61,7 +61,14 @@ public function __construct($getVars) { public function get() { throw new Exception('The action GET is not defined!'); } - + + /** + * @throws Exception + */ + public function set() { + throw new Exception('The action SET is not defined!'); + } + /** * @throws Exception */ @@ -174,7 +181,6 @@ private function buildJsonEnvelope() { echo json_encode($json); } - /** * Destructor: Renders the output * (Has the same function than a view for non api) diff --git a/libraries/graphs/bar-plot/build.template.html b/libraries/graphs/bar-plot/build.template.html index e739af3db3..66cdd9d9b5 100644 --- a/libraries/graphs/bar-plot/build.template.html +++ b/libraries/graphs/bar-plot/build.template.html @@ -1,6 +1,10 @@