_db->select(); $select->from($this->_getPair('UserReport'), array('COUNT(DISTINCT UserReport.schoolId)')); $select->where('UserReport.reportId = Report.id'); return new Zend_Db_Expr('(' . $select . ')'); } protected function _getFromColumns() { $columns = parent::_getFromColumns(); $columns['schoolCount'] = $this->_getSchoolCountExpr(); if (($schoolId = $this->getSchoolId())) { $columns['status'] = $this->_getStatusExpr($schoolId); $columns['forcedStatus'] = $this->_getForcedStatusExpr($schoolId); } return $columns; } protected function _prepareList(&$list) { if ($list) { $schoolReports = null; if ($this->getSchoolId()) { $schoolReports = $this->_getReportsBySchool($this->getSchoolId()); } foreach ($list as &$row) { if ($schoolReports && isset($schoolReports[$row['id']])) { $row['reports'] = $schoolReports[$row['id']]; } } } return parent::_prepareList($list); } public function initFromForm(array $data) { if (empty($data['dueDateMonth']) || empty($data['dueDateDay'])) { $data['dueDateMonth'] = null; $data['dueDateDay'] = null; $data['dueIdx'] = 2000; // reports without date should be show in the ned of the list } else { $month = $data['dueDateMonth'] = (int) $data['dueDateMonth']; $day = $data['dueDateDay'] = (int) $data['dueDateDay']; $data['dueIdx'] = $this->_prepareDueIdx($month, $day); } return parent::initFromForm($data); } protected function _updateDependency() { parent::_updateDependency(); $this->_saveDependency(); } protected function _insertDependency() { parent::_insertDependency(); $this->_saveDependency(); } protected function _saveDependency() { if (array_key_exists('excludeSchoolId', $this->_data)) { $this->_saveExcludeSchoolIds((array) $this->_data['excludeSchoolId']); } } protected function _addDependenciesFromDb(&$data) { $data['excludeSchoolId'] = $this->_readExcludeSchoolIds(); return parent::_addDependenciesFromDb($data); } protected function _saveExcludeSchoolIds(array $list) { $this->_updateIds($this->_getTableName('ReportExclude'), 'reportId', 'schoolId', $list); return $this; } protected function _readExcludeSchoolIds() { $select = $this->_db->select(); $select->from($this->_getPair('ReportExclude'), array('schoolId')); //$select->join($this->_getPair('School'), 'School.id = ReportExclude.schoolId AND School.enabled = "y"', array()); $select->join( $this->_getPair('School'), 'School.id = ReportExclude.schoolId AND School.id != "' . App_School_AbstractObj::ADMINISTRATION_ID . '"', array() ); $select->where('ReportExclude.reportId = ?', $this->getPrimaryKey()); return $this->_db->fetchCol($select); } protected function _deleteDependency() { if ($this->getPrimaryKey()) { $this->_deleteIds($this->_getTableName('ReportExclude'), 'reportId'); $this->_deleteUserReports(); } return parent::_deleteDependency(); } protected function _deleteUserReports() { $select = $this->_db->select(); $select->from($this->_getPair('UserReport'), array('file')); $select->where('UserReport.reportId = ?', $this->getPrimaryKey()); if (($fileList = $this->_db->fetchCol($select))) { $adapter = new Qs_File_Transfer_Adapter_Db(); $adapter->setDefaultDestination(App_Report_AbstractObj::REPORT_PATH); foreach ($fileList as $file) { $adapter->delete($file); } $this->_deleteIds($this->_getTableName('UserReport'), 'reportId'); } return $this; } public function getSchoolId() { return $this->_schoolId; } public function setSchoolId($schoolId) { $this->_schoolId = $schoolId; return $this; } public function getSchoolName($schoolId) { $select = $this->_db->select(); $select->from($this->_getPair('School'), array('name')); $select->where('id = ?', $schoolId); //$select->where('enabled = "y"'); $select->limit(1); return $this->_db->fetchOne($select); } public function getAllReports($schoolId = null) { $select = $this->_db->select(); $select->from($this->_getPair('UserReport'), array('schoolId', '*')) ->join( $this->_getPair('School'), '`School`.`id` = `UserReport`.`schoolId`', array('schoolName' => 'name') ) ->join( $this->_getPair('Report'), '`Report`.`id` = `UserReport`.`reportId`', array('dueDateMonth', 'dueDateDay', 'section', 'topic') ); if ($schoolId) { $select->where('UserReport.schoolId = ?', $schoolId, Qs_Db::INT_TYPE); } return $this->_db->fetchAll($select); } public function getUserReports() { $select = $this->_db->select(); $select->from($this->_getPair('UserReport'), 'file'); return $this->_db->fetchCol($select); } public function clearUserReportTable() { $this->_db->query('TRUNCATE ' . $this->_getTableName('UserReport')); return $this; } }