/** * Gets an existing watch if it exists * * @param CrmUser $user * @param int $id * @param string $type */ public function getWatch($user, $id, $type = null) { if ($user == null) { throw new Exception("User cannot be null"); } // if passed with 2 params, assume an object as $id if ($type == null) { $type = get_class($id); $id = $id->id; } $select = $this->dbService->select(); $select->from('itemwatch', '*')->where('itemtype = ?', $type)->where('itemid = ?', $id)->where('userid= ?', $user->username); $select->order('created desc'); return $this->dbService->getObject($select, 'ItemWatch'); }
/** * Update a timesheet record * * First off, we make sure that the passed in record * is updateable by checking that the given endtime matches * that stored in the DB * * Then, check to see whether the start / end dates are greater * than a certain number of minutes (eg we don't want a single record * to have more than 8 hours of time on it so that reporting * doesn't get too out of alignment). * * Then, update the timesheet record and save the data * * @param TimesheetRecord $record * @param int $lastEndTime The end time of the last entry, to make suer that we've got a * valid timing situation */ public function updateTimesheetRecord(TimesheetRecord $record, $lastEndTime) { // Make sure it's the right record. $select = $this->dbService->select(); $select->from('timesheetrecord', '*')->where('id = ?', $record->id)->where('endtime = ?', $lastEndTime); $verifyRecord = $this->dbService->getObject($select, 'TimesheetRecord'); if ($verifyRecord == null) { throw new InvalidTimesheetRecordException("Record id " . $record->id . " does not exist with endtime {$lastEndTime}"); } // Get the task this record is associated with so we can update its elapsed time $task = $this->getTask($record->taskid); if (!$task) { throw new Exception("Task for record does not exist"); } $newTime = $record->endtime + self::TASK_UPDATE_TIME; // limit timesheet records to being 4 hours maximum if ($newTime - $record->starttime > self::MAX_RECORD_LENGTH) { // create a new record instead of updating the old $newRecord = $this->addTimesheetRecord($task, za()->getUser(), $record->endtime, $newTime); return $newRecord; } // If the record has been locked (ie someone's locked this while // the user's actively timing it), create a new record if ($record->timesheetid > 0) { $newRecord = $this->addTimesheetRecord($task, za()->getUser(), $record->endtime, $newTime); return $newRecord; } $record->endtime = $newTime; try { // start a transaction $this->dbService->beginTransaction(); $this->dbService->updateObject($record); $this->updateTaskTime($task); $this->dbService->commit(); return $record; } catch (Exception $e) { $this->dbService->rollback(); } }
/** * Creates a new user * * @param Array $params * @return The created user * @throws InvalidModelException * @throws ExistingUserException */ public function createUser($params, $setAsAuthenticated = true, $role = User::ROLE_USER, $userType = null) { if ($userType == null) { $userType = $this->userClass; } // Check if there's a user with this email first. $select = $this->dbService->select(); $select->from(strtolower($userType), '*')->where('username=?', $params['username'])->orWhere('email=?', $params['email']); $existing = $this->dbService->getObject($select, $userType); if ($existing) { throw new ExistingUserException($params['username'] . ' already exists'); } $newPass = null; if (isset($params['password'])) { $newPass = $params['password']; } $params['role'] = $role; // Create a user with initial information $user = $userType; $user = new $user(); $user->generateSaltedPassword($params['password']); unset($params['password']); $user->bind($params); $validator = new ModelValidator(); if (!$validator->isValid($user)) { throw new InvalidModelException($validator->getMessages()); } // so now we save the user, then reset their password which emails, // then we set them as the authenticated user. if ($this->dbService->createObject($user)) { $this->trackerService->track('create-user', $user->id); if ($setAsAuthenticated) { $this->authService->setAuthenticatedUser($user); } return $user; } return null; }