public function save() { $projectId = $this->getValue('projectId'); $activityId = $this->getValue('activityId'); if (!empty($activityId)) { $activity = $this->getProjectService()->getProjectActivityById($activityId); $this->edited = true; } else { $activity = new ProjectActivity(); } $activity->setProjectId($projectId); $activity->setName($this->getValue('activityName')); $activity->setIsDeleted(ProjectActivity::ACTIVE_PROJECT); $activity->save(); return $projectId; }
/** * Add an activity to the database and saves it in the $this->activities * array * * @param int $projectId The project Id * @param string $name The Activity name * @param bool $deleted Create activity in deleted state * * @return ProjectActivity The activity object that was created. */ private function _addActivity($projectId, $name, $deleted = false) { $activity = new ProjectActivity(); $activity->setName($name); $activity->setProjectId($projectId); $activity->setDeleted($deleted); $activity->save(); $this->activities[$projectId][$name] = $activity; return $activity; }
/** * Tests the save() method. */ public function testSave() { // Test that saving an activity without a project ID or a name is not allowed. $activity = new ProjectActivity(); try { $activity->save(); $this->fail("Exception not thrown"); } catch (ProjectActivityException $e) { $this->assertEquals(0, $this->_getNumActivities(), "No rows should be inserted"); } // Test that saving an activity without a project ID is not allowed. $activity = new ProjectActivity(); $activity->setName("Test Project Activity"); try { $activity->save(); $this->fail("Exception not thrown"); } catch (ProjectActivityException $e) { $this->assertEquals(0, $this->_getNumActivities(), "No rows should be inserted"); } // Test that saving an activity without a name is not allowed. $activity = new ProjectActivity(); $activity->setProjectId(1); try { $activity->save(); $this->fail("Exception not thrown"); } catch (ProjectActivityException $e) { $this->assertEquals(0, $this->_getNumActivities(), "No rows should be inserted"); } // Save a valid new activity $activity1Id = UniqueIDGenerator::getInstance()->getLastId("ohrm_project_activity", "activity_id") + 1; $activity1 = new ProjectActivity(); $activity1->setProjectId(1); $activity1->setName("Development"); $activity1->save(); $this->assertEquals($activity1Id, $activity1->getId(), "activity ID not updated with auto_increment value"); $result = mysql_query("SELECT * FROM ohrm_project_activity"); $this->assertEquals(1, mysql_num_rows($result), "Only one row should be inserted"); $row = mysql_fetch_assoc($result); $this->_checkRow($activity1, $row); // Save a second activity. $activity2Id = UniqueIDGenerator::getInstance()->getLastId("ohrm_project_activity", "activity_id") + 1; $activity2 = new ProjectActivity(); $activity2->setProjectId(1); $activity2->setName("QA Testing"); $activity2->save(); $this->assertEquals($activity2Id, $activity2->getId(), "activity ID not updated with auto_increment value"); $result = mysql_query("SELECT * FROM ohrm_project_activity ORDER BY activity_id ASC"); $this->assertEquals(2, mysql_num_rows($result), "Only one row should be inserted"); // check both rows $this->_checkRow($activity1, mysql_fetch_assoc($result)); $this->_checkRow($activity2, mysql_fetch_assoc($result)); // Change attributes and save activity using existing object $activity1->setName("Updated activity"); $activity1->setProjectId(2); $activity1->save(); $this->assertEquals($activity1Id, $activity1->getId(), "activity ID should not change"); $result = mysql_query("SELECT * FROM ohrm_project_activity WHERE activity_id = {$activity1Id}"); $this->_checkRow($activity1, mysql_fetch_assoc($result)); // Change attributes and save activity using new object $activity3 = new ProjectActivity($activity2Id); $activity3->setProjectId(1); $activity3->setName("Installing"); $activity3->save(); $result = mysql_query("SELECT * FROM ohrm_project_activity WHERE activity_id = {$activity2Id}"); $this->_checkRow($activity3, mysql_fetch_assoc($result)); // Verify that saving an activity without changes does not throw an exception try { $activity3->save(); } catch (ProjectActivityException $e) { $this->fail("Saving without changes should not throw an exception"); } // Verify that setting name to null and saving throws an exception $activity1->setName(null); try { $activity1->save(); $this->fail("Exception not thrown"); } catch (ProjectActivityException $e) { // expected } // Save an activity for the project 0 $activity3Id = UniqueIDGenerator::getInstance()->getLastId("ohrm_project_activity", "activity_id") + 1; $activity3 = new ProjectActivity(); $activity3->setProjectId(0); $activity3->setName("Test internal"); $activity3->save(); $this->assertEquals($activity3Id, $activity3->getId(), "activity ID not updated with auto_increment value"); $result = mysql_query("SELECT * FROM ohrm_project_activity WHERE activity_id = {$activity3Id}"); $this->_checkRow($activity3, mysql_fetch_assoc($result)); }
/** * Log a ProjectActivity on a changed record * * @param array $params */ protected static function log_project_activity($params) { $def = $params['def']; $rec = $params['rec']; // log the activity $pa = new ProjectActivity(); $pa->pa_actm_id = $def['actm_id']; $pa->pa_dtim = air2_date(); $pa->pa_desc = $def['desc']; $pa->pa_notes = json_encode(array('old' => $params['old'], 'new' => $params['new'])); // optional xid if (isset($def['xid'])) { $pa->pa_xid = $rec->{$def}['xid'][0]; $pa->pa_ref_type = $def['xid'][1]; } // get the actual project and add an activity $project = $rec->hasRelation('Project') ? $rec->Project : $rec; if ($project->exists()) { $pa->pa_prj_id = $project->prj_id; $pa->save(); } else { $project->ProjectActivity[] = $pa; //saving will happen later } }