/** * Associate the student with the provided terms. * * @since 0.1 * * @param array $terms * * @return bool */ public function associateWithTerms(array $terms) { $dbw = wfGetDB(DB_MASTER); $success = true; $dbw->begin(); foreach ($terms as $term) { $success = $dbw->insert('ep_students_per_term', array('spt_student_id' => $this->getId(), 'spt_term_id' => $term->getId())) && $success; } $dbw->commit(); foreach ($terms as $term) { EPCourse::updateSummaryFields('students', array('id' => $term->getField('course_id'))); EPOrg::updateSummaryFields('students', array('id' => $term->getField('org_id'))); EPTerm::updateSummaryFields('students', array('id' => $this->getId())); } return $success; }
/** * (non-PHPdoc) * @see EPDBObject::updateInDB() */ protected function updateInDB() { if ($this->updateSummaries) { $oldOrgId = $this->hasField('org_id') ? self::selectFieldsRow('org_id', array('id' => $this->getId())) : false; $oldCourseId = $this->hasField('course_id') ? self::selectFieldsRow('course_id', array('id' => $this->getId())) : false; } if ($this->hasField('course_id')) { $oldCourseId = self::selectFieldsRow('course_id', array('id' => $this->getId())); if ($this->getField('course_id') !== $oldCourseId) { $this->setField('org_id', EPCourse::selectFieldsRow('org_id', array('id' => $this->getField('course_id')))); } } $success = parent::updateInDB(); if ($this->updateSummaries && $success) { if ($oldOrgId !== false && $oldOrgId !== $this->getField('org_id')) { $conds = array('id' => array($oldOrgId, $this->getField('org_id'))); EPOrg::updateSummaryFields(array('terms', 'students', 'active'), $conds); } if ($oldCourseId !== false && $oldCourseId !== $this->getField('org_id')) { $conds = array('id' => array($oldCourseId, $this->getField('course_id'))); EPCourse::updateSummaryFields(array('active', 'students'), $conds); } } return $success; }
/** * (non-PHPdoc) * @see EPDBObject::saveExisting() */ protected function saveExisting() { if ($this->updateSummaries) { $currentCourse = false; $currentFields = array('id'); foreach (array('org_id', 'online_ambs', 'campus_ambs') as $field) { if ($this->hasField($field)) { $currentFields[] = $field; } } if (count($currentFields) > 1) { $currentCourse = self::selectRow($currentFields, array('id' => $this->getId())); } } $success = parent::saveExisting(); if ($this->updateSummaries && $currentCourse !== false && $success) { if ($currentCourse->hasField('org_id') && $currentCourse->getField('org_id') !== $this->getField('org_id')) { $conds = array('id' => array($currentCourse->getField('org_id'), $this->getField('org_id'))); EPOrg::updateSummaryFields(array('courses', 'students', 'active', 'instructors', 'oas', 'cas'), $conds); } foreach (array('oas', 'cas') as $ambs) { $field = $ambs === 'oas' ? 'online_ambs' : 'campus_ambs'; if ($currentCourse->hasField($field) && $currentCourse->getField($field) !== $this->getField($field)) { $courseField = $ambs === 'oas' ? 'opc_course_id' : 'cpc_course_id'; $userField = $ambs === 'oas' ? 'opc_user_id' : 'cpc_user_id'; $table = 'ep_' . $ambs . '_per_course'; $addedIds = array_diff($this->getField($field), $currentCourse->getField($field)); $removedIds = array_diff($currentCourse->getField($field), $this->getField($field)); $dbw = wfGetDB(DB_MASTER); if (count($removedIds) > 0) { $dbw->delete($table, array($courseField => $this->getId(), $userField => $removedIds)); } $dbw->begin(); foreach ($addedIds as $ambassadorId) { $dbw->insert($table, array($courseField => $this->getId(), $userField => $ambassadorId)); } $dbw->commit(); } } } return $success; }