public static function userDashboard($params) { $format = $params['args'][1] != '' ? $params['args'][1] : 'html'; $app_url = Settings::getProtected('app_url'); $user = User::getAuthenticatedUser(); // Put it in the settings cache Settings::setProtected('username', $user->username); // Set up proofing and reviewing objects $proofing = array(); $reviewing = array(); // Load the user's proofing queue $proofQueue = new Queue("user.proof:{$user->username}"); $proofing['items'] = array(); foreach ($proofQueue->getItems() as $item) { array_push($proofing['items'], array('title' => $item->title, 'status' => $item->status, 'project_slug' => $item->project_slug, 'project_type' => $item->project_type, 'project_owner' => $item->project_owner, 'item_id' => $item->item_id, 'type' => $item->type)); } // Load the user's reviewing queue $reviewQueue = new Queue("user.review:{$user->username}"); $reviewing['items'] = array(); foreach ($reviewQueue->getItems() as $item) { array_push($reviewing['items'], array('title' => $item->title, 'status' => $item->status, 'project_slug' => $item->project_slug, 'project_type' => $item->project_type, 'project_owner' => $item->project_owner, 'item_id' => $item->item_id, 'type' => $item->type)); } // Add extra info (edit link and slug) to each item $prooflist = array(); foreach ($proofing['items'] as &$item) { if ($item['project_type'] == 'system') { $item['editlink'] = $app_url . '/projects/' . $item['project_slug'] . '/items/' . $item['item_id'] . '/proof'; } else { if ($item['project_type'] == 'user') { $item['editlink'] = $app_url . '/users/' . $item['project_owner'] . '/projects/' . $item['project_slug'] . '/items/' . $item['item_id'] . '/proof'; } } if (!in_array($item['project_slug'], $prooflist)) { $prooflist[] = $item['project_slug']; } } $reviewlist = array(); foreach ($reviewing['items'] as &$item) { if ($item['project_type'] == 'system') { $item['editlink'] = $app_url . '/projects/' . $item['project_slug'] . '/items/' . $item['item_id'] . '/review'; } else { if ($item['project_type'] == 'user') { $item['editlink'] = $app_url . '/users/' . $item['project_owner'] . '/projects/' . $item['project_slug'] . '/items/' . $item['item_id'] . '/review'; } } if (!in_array($item['project_slug'], $reviewlist)) { $reviewlist[] = $item["project_slug"]; } } // Add link and percentages to each project $projects = $user->getProjectSummaries(); $projectInfo = array(); $proofing['projects'] = array(); $reviewing['projects'] = array(); foreach ($projects as &$project) { $roles = $user->getRolesForProject($project['slug']); // If the project is available for proofing or reviewing (with no items already claimed), // then add it to the appropriate list if (!in_array($project["slug"], $prooflist) && $project["available_to_proof"] > 0 && in_array('proofer', $roles)) { array_push($proofing['projects'], $project['slug']); } if (!in_array($project["slug"], $reviewlist) && $project["available_to_review"] > 0 && in_array('reviewer', $roles)) { array_push($reviewing['projects'], $project['slug']); } // Set up percentage bars if ($project['num_items'] == 0) { $project['percent_proofed'] = 0; $project['percent_reviewed'] = 0; } else { $project['percent_proofed'] = round($project['num_proofed'] / $project['num_items'] * 100, 0); $project['percent_reviewed'] = round($project['num_reviewed'] / $project['num_items'] * 100, 0); } // And the project link if ($project['type'] == 'system') { $project['link'] = $app_url . '/projects/' . $project['slug']; } else { if ($project['type'] == 'user') { $project['link'] = $app_url . '/users/' . $project['owner'] . '/projects/' . $project['slug']; } } $projectInfo[$project['slug']] = $project; } // Blank slate condition if there are no items and no projects $proofing['blankslate'] = count($proofing['items']) == 0 && count($proofing['projects']) == 0 ? true : false; $reviewing['blankslate'] = count($reviewing['items']) == 0 && count($reviewing['projects']) == 0 ? true : false; // Get the user's history and the top proofers information $history = $user->getHistory(); $topusers = User::getTopUsers(); // Prepare user history foreach ($history as &$event) { if ($event['project_type'] == 'system') { $event['editlink'] = "{$app_url}/projects/" . $event['project_slug'] . '/items/' . $event['item_id'] . '/proof'; } else { if ($project['type'] == 'user') { $event['editlink'] = "{$app_url}/users/" . $event['project_owner'] . '/projects/' . $event['project_slug'] . '/items/' . $event['item_id'] . '/proof'; } } $event['title'] = $event['item_title']; } $response = array('page_title' => 'Dashboard', 'user' => $user->getResponse(), 'projects' => $projectInfo, 'proofing' => array('items' => $proofing['items'], 'projects' => $proofing['projects'], 'blankslate' => $proofing['blankslate']), 'reviewing' => array('items' => $reviewing['items'], 'projects' => $reviewing['projects'], 'blankslate' => $reviewing['blankslate']), 'history' => $history, 'history_count' => count($history), 'registered_methods' => array('/users/' . $user->username), 'topusers' => $topusers); switch ($params['method']) { // GET: Get user dashboard case 'GET': switch ($format) { case 'json': echo json_encode($response); break; case 'html': Template::render('dashboard', $response); break; } break; } }
public function testRealUseCaseExample2() { $message1 = new ExampleMessageObject(); $message1->int = 1; $message1->float = 1.1; $message1->string = 'something1'; $message1->bool = true; $message1Serialized = serialize($message1); $message2 = new ExampleMessageObject(); $message2->int = 3; $message2->float = 3.3; $message2->string = 'something2'; $message2->bool = false; $message2Serialized = serialize($message2); $message3 = new ExampleMessageObject(); $message3->int = 4; $message3->float = 4.4; $message3->string = 'something3'; $message3->bool = true; $message3Serialized = serialize($message3); $message4 = new ExampleMessageObject(); $message4->int = 5; $message4->float = 5.5; $message4->string = 'something4'; $message4->bool = false; $message4Serialized = serialize($message4); $message5 = new ExampleMessageObject(); $message5->int = 6; $message5->float = 6.6; $message5->string = 'something5'; $message5->bool = true; $message5Serialized = serialize($message5); $time = time(); $queue = new Queue($this->redis, 'test'); $processingQueue = sprintf('test-processing-%s[%d][%d]', gethostname(), getmypid(), $time); $queue->addItems([$message1, $message2, $message3, $message1, $message4, $message5]); $items = $queue->getItems(4); $this->assertSame([$message1Serialized, $message2Serialized, $message3Serialized, $message1Serialized], $items); $this->assertSame([$message5Serialized, $message4Serialized], $this->redis->lrange('test', 0, 5)); $this->assertSame([$message1Serialized, $message3Serialized, $message2Serialized, $message1Serialized], $this->redis->lrange($processingQueue, 0, 10)); $this->assertKeys(['test', $processingQueue, 'test-timeouts']); $queue->ackItem($message1); $queue->ackItems([$message2, $message3]); $queue->rejectBatch(); $this->assertSame([$message5Serialized, $message4Serialized, $message1Serialized], $this->redis->lrange('test', 0, 5)); $this->assertKeys(['test']); $items = $queue->getItems(5); $this->assertSame([$message1Serialized, $message4Serialized, $message5Serialized], $items); $this->assertSame([$message5Serialized, $message4Serialized, $message1Serialized], $this->redis->lrange($processingQueue, 0, 5)); $this->assertKeys([$processingQueue, 'test-timeouts']); $queue->ackItems([$message1Serialized, $message4Serialized, $message5Serialized]); $this->assertKeys([]); }
public static function itemProof($params) { $i18n = Settings::getProtected('i18n'); $format = Utils::getFormat($params['args'], 0, 2); $projectType = Utils::getProjectType($params['args']); $projectSlugIndex = $projectType == 'system' ? 0 : 2; $projectSlug = $params['args'][$projectSlugIndex]; $project = new Project($projectSlug); $itemIndex = $projectType == 'system' ? 1 : 3; $itemId = $params['args'][$itemIndex]; $proofTypeIndex = $projectType == 'system' ? 2 : 4; $proofType = $params['args'][$proofTypeIndex]; $role = $proofType . "er"; $proofUserIndex = $projectType == 'system' ? 3 : 5; $proofUser = array_key_exists($proofUserIndex, $params['args']) ? $params['args'][$proofUserIndex] : ''; $owner = $projectType == 'user' ? $params['args'][1] : ''; $user = User::getAuthenticatedUser(); switch ($params['method']) { // GET: Get proof/review/edit page for this item case 'GET': // Make sure they have access to the item if ($proofType == 'edit' || $proofUser != '') { // For editing an item or a specific proof/review, user must be project admin or site admin RoleController::forceClearance(array('project.admin', 'project.owner', 'system.admin'), $user, array('project' => $project)); } else { // User has to be a member of the project if (!$user->isMember($projectSlug, $role)) { Utils::redirectToDashboard("", $i18n->t("error.not_a_member")); return; } } // If we're looking at an existing proof/review, load it for that user // Otherwise load it for the existing user $username = $proofUser != '' ? $proofUser : $user->username; // Load the item $itemObj = new Item($itemId, $projectSlug, $username, $proofType); // Make sure it exists (if it fails, it'll return a boolean) if ($itemObj->item_id == -1) { Utils::redirectToDashboard("", $i18n->t("error.nonexistent_item")); return; } $alreadyFinished = false; $moreToProof = false; if ($proofType != 'edit' && $proofUser == '') { // If it's not in their current queue, they're editing it after finishing it // TODO: Make this part more elegant $userCurrentQueue = new Queue("user.{$proofType}:{$user->username}", false); $userCurrentQueueItems = $userCurrentQueue->getItems(); if (!in_array($itemObj, $userCurrentQueueItems)) { $alreadyFinished = true; } // And if it's not in their full queue, they never had it and shouldn't be allowed to proof it $userQueue = new Queue("user.{$proofType}:{$user->username}", false, array('include-removed' => true)); $userQueueItems = $userQueue->getItems(); if (!in_array($itemObj, $userQueueItems)) { Utils::redirectToDashboard("", $i18n->t("error.insufficient_rights")); return; } // See if there are any items left for us to proof $queue = new Queue("project.{$proofType}:{$projectSlug}"); foreach ($queue->getItems() as $item) { if (!in_array($item, $userQueueItems)) { $moreToProof = true; break; } } } $item = array(); $item['id'] = $itemId; $item['title'] = $itemObj->title; // If the user has a transcript for this item, load it instead if ($itemObj->userTranscript && trim($itemObj->userTranscript['transcript']) != '') { $transcript = trim($itemObj->userTranscript['transcript']); } else { $transcript = trim($itemObj->transcript); } $item['transcript'] = stripslashes($transcript); // Get fields, if any if ($itemObj->userTranscript && trim($itemObj->userTranscript['fields']) != '') { $itemFields = json_decode(trim($itemObj->userTranscript['fields']), true); } else { $itemFields = array(); } $item['fields'] = $itemFields; // Prepare the URL $appUrl = Settings::getProtected('app_url'); if ($projectType == 'system') { $projectUrl = "projects/{$projectSlug}"; } else { if ($projectType == 'user') { $projectUrl = "users/{$owner}/{$projectSlug}"; } } $item['href'] = $projectUrl . "/" . $itemObj->href; // Get template type $templateType = $itemObj->type; // Get project fields and parse out $fields = array(); $fieldsText = trim($project->fields); if ($fieldsText != '') { $fieldsLines = explode("\n", $fieldsText); foreach ($fieldsLines as $line) { $fieldLabel = ''; $fieldType = ''; $fieldValues = array(); // Split it by label and type/parameters list($fieldLabel, $fieldSettings) = array_map('trim', explode(":", $line)); if (strpos($fieldSettings, ' - ') == FALSE) { $fieldType = trim($fieldSettings); } else { list($fieldType, $fieldValueStr) = array_map('trim', explode(" - ", $fieldSettings)); $fieldValues = explode(" | ", $fieldValueStr); } // Reformat the field ID $fieldId = str_replace(" ", "_", strtolower($fieldLabel)); $field = array('id' => $fieldId, 'label' => $fieldLabel, 'type' => $fieldType, 'values' => $fieldValues); array_push($fields, $field); } } // Get any editor-specific config settings $editors = Settings::getProtected('editors'); $editorOptions = array_key_exists($templateType, $editors) ? $editors[$templateType] : array(); $pageTitle = ucfirst($proofType) . " " . $item['title']; if ($proofUser) { $pageTitle .= " ({$proofUser})"; } $pageTitle .= " | " . $project->title; // Display the template $options = array('page_title' => $pageTitle, 'user' => $user->getResponse(), 'item' => $item, 'project' => $project->getResponse(), 'more_to_proof' => $moreToProof, 'already_finished' => $alreadyFinished, 'editor_options' => $editorOptions, 'editor_type' => $templateType, 'proof_user' => $proofUser, 'proof_type' => $proofType, 'fields' => $fields, 'css' => array("editors/{$templateType}/{$templateType}.css"), 'js' => array("editors/{$templateType}/{$templateType}.js")); Template::render("editors/{$templateType}", $options); break; } }
try { Queue::register('compare', array('Point', 'compareItems')); Queue::register('destroy', 'destroyQueue'); } catch (Exception $e) { echo "FAIL\n"; echo $e->getMessage(); } $queue = new Queue('2'); echo "queue 1 loaded\n"; $item = $queue->getFirstItem(); echo 'first item: ' . $item . "\n"; $item = $queue->remove(new Point(4, 0)); echo 'fourth item: ' . $item . "\n"; $item = $queue->getFirstItem(); echo 'second item: ' . $item . "\n"; if ($queue->save()) { echo "\nsaved.\n"; } echo json_encode($queue->getItems()); echo "\n"; $items = $queue->getItems(); $items[] = new Point(4, 3); $items[] = new Point(5, 3); $items[] = new Point(1, 1); $queue->setItems($items); $queue->add(new Point(42, 222)); if ($queue->save()) { echo "\nsaved.\n"; } echo "test 2 done\n\n"; $queue->destroy();
public static function getNextAvailableItem($params) { $username = $params['username']; $projectSlug = $params['projectSlug']; $type = $params['type']; $role = $type . "er"; $success = false; $errorCode = ''; $db = Settings::getProtected('db'); $auth = Settings::getProtected('auth'); // Make sure we're authenticated as the user we say we are $auth->forceAuthentication(); $loggedInUsername = $auth->getUsername(); if ($username != $loggedInUsername) { $code = "not-authenticated-as-correct-user"; } // Load user $user = new User($username); // Does this user belong to the project? if (!$user->isMember($projectSlug, $role)) { $code = "not-a-member"; } // Does this user already have an item from this project? if ($user->hasProjectItem($projectSlug)) { $code = "has-unfinished-item"; } // Load the user's queue $userQueue = new Queue("user.{$type}:{$username}", false, array('include-removed' => true)); $userQueueItems = $userQueue->getItems(); // Load the project's queue $queue = new Queue("project.{$type}:{$projectSlug}"); $queueItems = $queue->getItems(); // Go through the project queue and get the first item the user hasn't yet done foreach ($queueItems as $item) { if (!in_array($item, $userQueueItems)) { $nextItem = $item; break; } } if (isset($nextItem) && $nextItem->item_id != -1) { // Concatenate proofed transcripts if ($type == 'review') { // Get proofed transcripts for the new item $transcripts = $db->loadItemTranscripts($nextItem->project_id, $nextItem->item_id, 'proof'); // Only diff them if there's more than one if (count($transcripts) > 1) { $transcriptText = Transcript::diff($transcripts); } else { $transcriptText = $transcripts[0]['transcript']; } // Only get the fields for the first transcript $transcriptFields = $transcripts[0]['fields']; // Create transcript and add to the database $transcript = new Transcript(); $transcript->setText($transcriptText); $transcript->setFields($transcriptFields); $transcript->save(array('item' => $nextItem, 'status' => 'draft', 'type' => 'review')); } // Reload the user's queue, this time ignoring items they've already done // Add it to the user's queue $userQueue = new Queue("user.{$type}:{$username}", false); $userQueue->add($nextItem); $userQueue->save(); // Remove it from the project queue $queue->remove($nextItem); $queue->save(); $success = true; $code = $nextItem->item_id; } else { $code = "no-item-available"; } return array('status' => $success, 'code' => $code); }