/** * Test integration. * * @return boolean */ public function integrate(Test $test, $pageStartPos = null, $save = true) { // count pages $count = $test->pages()->count(); $currentCount = $this->pages()->count(); if (is_null($pageStartPos)) { // get last page $lastPage = $this->pages()->orderBy('position', 'desc')->first(); // count current pages $pageStartPos = $currentCount; if (is_null($lastPage)) { $pageStartPos = 1; } elseif ($lastPage->finish) { $lastPage->position += $count; $lastPage->save(); } else { $pageStartPos++; } } else { // page position if ($pageStartPos < 0) { $pageStartPos += $currentCount + 1; } // update pages if ($currentCount >= $pageStartPos) { $this->pages()->orderBy('position')->where('position', '>=', $pageStartPos)->increment('position', $count); } else { $pageStartPos = 1; } } $pages = $test->pages()->where('finish', false)->with('items')->orderBy('position')->get(); // create pages foreach ($pages as $page) { $newPage = $page->replicate(['id', 'test_id']); $newPage->test_id = $this->id; $newPage->position = $pageStartPos; $newPage->save(); $items = []; foreach ($page->items as $item) { $newItem = $item->replicate(['id', 'element_id']); $element = $item->element->replicate(['id']); $element->save(); $newItem->element_id = $element->id; $items[] = $newItem; } $newPage->items()->saveMany($items); $pageStartPos++; } // add assets $this->assets = $test->assets; // save if allowed if ($save) { $this->save(); } return true; }
/** * get export */ public function getExport($pid = null, $ext = 'xlsx') { if ($pid === null) { // get projects $tests = Test::ofType('project')->isActive()->get(); return view('export', ['projects' => $tests]); } else { // get test $test = Test::ofType('project')->find($pid); // check if test exists if (is_null($test)) { return redirect('/export'); } // export test return $test->export(['suffix' => mt_rand(100000, 999999)])->export($ext); } }