Index: trunk/extensions/EducationProgram/includes/EPOrg.php |
— | — | @@ -105,13 +105,18 @@ |
106 | 106 | if ( in_array( 'students', $summaryFields ) ) { |
107 | 107 | $termIds = EPTerm::selectFields( 'id', array( 'org_id' => $this->getId() ) ); |
108 | 108 | |
109 | | - $fields['students'] = $dbr->select( |
110 | | - 'ep_students_per_term', |
111 | | - 'COUNT(*) AS rowcount', |
112 | | - array( 'spt_term_id' => $termIds ) |
113 | | - ); |
| 109 | + if ( count( $termIds ) > 0 ) { |
| 110 | + $fields['students'] = $dbr->select( |
| 111 | + 'ep_students_per_term', |
| 112 | + 'COUNT(*) AS rowcount', |
| 113 | + array( 'spt_term_id' => $termIds ) |
| 114 | + ); |
114 | 115 | |
115 | | - $fields['students'] = $fields['students']->rowcount; |
| 116 | + $fields['students'] = $fields['students']->fetchObject()->rowcount; |
| 117 | + } |
| 118 | + else { |
| 119 | + $fields['students'] = 0; |
| 120 | + } |
116 | 121 | } |
117 | 122 | |
118 | 123 | $this->setFields( $fields ); |
Index: trunk/extensions/EducationProgram/includes/EPTerm.php |
— | — | @@ -64,9 +64,30 @@ |
65 | 65 | 'token' => '', |
66 | 66 | ); |
67 | 67 | } |
68 | | - |
| 68 | + |
69 | 69 | /** |
70 | 70 | * (non-PHPdoc) |
| 71 | + * @see EPDBObject::loadSummaryFields() |
| 72 | + */ |
| 73 | + public function loadSummaryFields( $summaryFields = null ) { |
| 74 | + if ( is_null( $summaryFields ) ) { |
| 75 | + $summaryFields = array( 'org_id' ); |
| 76 | + } |
| 77 | + else { |
| 78 | + $summaryFields = (array)$summaryFields; |
| 79 | + } |
| 80 | + |
| 81 | + $fields = array(); |
| 82 | + |
| 83 | + if ( in_array( 'org_id', $summaryFields ) ) { |
| 84 | + $fields['org_id'] = $this->getCourse( 'org_id' )->getField( 'org_id' ); |
| 85 | + } |
| 86 | + |
| 87 | + $this->setFields( $fields ); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * (non-PHPdoc) |
71 | 92 | * @see EPDBObject::insertIntoDB() |
72 | 93 | */ |
73 | 94 | protected function insertIntoDB() { |
— | — | @@ -74,7 +95,9 @@ |
75 | 96 | $this->setField( 'org_id', $this->getCourse( 'org_id' )->getField( 'org_id' ) ); |
76 | 97 | } |
77 | 98 | |
78 | | - EPOrg::updateSummaryFields( 'terms', array( 'id' => $this->getField( 'org_id' ) ) ); |
| 99 | + if ( $this->updateSummaries ) { |
| 100 | + EPOrg::updateSummaryFields( 'terms', array( 'id' => $this->getField( 'org_id' ) ) ); |
| 101 | + } |
79 | 102 | |
80 | 103 | return parent::insertIntoDB(); |
81 | 104 | } |
— | — | @@ -85,16 +108,19 @@ |
86 | 109 | */ |
87 | 110 | public function removeFromDB() { |
88 | 111 | $id = $this->getId(); |
89 | | - $this->loadFields( array( 'org_id' ) ); |
90 | | - $orgId = $this->getField( 'org_id', false ); |
91 | | - |
| 112 | + |
| 113 | + if ( $this->updateSummaries ) { |
| 114 | + $this->loadFields( array( 'org_id' ) ); |
| 115 | + $orgId = $this->getField( 'org_id', false ); |
| 116 | + } |
| 117 | + |
92 | 118 | $success = parent::removeFromDB(); |
93 | 119 | |
94 | 120 | if ( $success ) { |
95 | 121 | $success = wfGetDB( DB_MASTER )->delete( 'ep_students_per_term', array( 'spt_term_id' => $id ) ) && $success; |
96 | 122 | } |
97 | 123 | |
98 | | - if ( $orgId !== false ) { |
| 124 | + if ( $this->updateSummaries && $orgId !== false ) { |
99 | 125 | EPOrg::updateSummaryFields( array( 'terms', 'students' ), array( 'id' => $orgId ) ); |
100 | 126 | } |
101 | 127 | |
— | — | @@ -106,11 +132,21 @@ |
107 | 133 | * @see EPDBObject::updateInDB() |
108 | 134 | */ |
109 | 135 | protected function updateInDB() { |
110 | | - $oldOrgId = $this->hasField( 'org_id' ) ? self::selectFieldsRow( 'org_id', array( 'id' => $this->getId() ) ) : false; |
| 136 | + if ( $this->updateSummaries ) { |
| 137 | + $oldOrgId = $this->hasField( 'org_id' ) ? self::selectFieldsRow( 'org_id', array( 'id' => $this->getId() ) ) : false; |
| 138 | + } |
111 | 139 | |
| 140 | + if ( $this->hasField( 'course_id' ) ) { |
| 141 | + $oldCourseId = self::selectFieldsRow( 'course_id', array( 'id' => $this->getId() ) ); |
| 142 | + |
| 143 | + if ( $this->getField( 'course_id' ) !== $oldCourseId ) { |
| 144 | + $this->setField( 'org_id', EPCourse::selectFieldsRow( 'org_id', array( 'id' => $this->getField( 'course_id' ) ) ) ); |
| 145 | + } |
| 146 | + } |
| 147 | + |
112 | 148 | $success = parent::updateInDB(); |
113 | 149 | |
114 | | - if ( $success && $oldOrgId !== false && $oldOrgId !== $this->getField( 'org_id' ) ) { |
| 150 | + if ( $this->updateSummaries && $success && $oldOrgId !== false && $oldOrgId !== $this->getField( 'org_id' ) ) { |
115 | 151 | $conds = array( 'id' => array( $oldOrgId, $this->getField( 'org_id' ) ) ); |
116 | 152 | EPOrg::updateSummaryFields( array( 'terms', 'students' ), $conds ); |
117 | 153 | } |
— | — | @@ -129,7 +165,7 @@ |
130 | 166 | */ |
131 | 167 | public function getCourse( $fields = null ) { |
132 | 168 | if ( $this->course === false ) { |
133 | | - $this->course = EPCourse::selectRow( $fields, array( 'id' => $this->getField( 'course_id' ) ) ); |
| 169 | + $this->course = EPCourse::selectRow( $fields, array( 'id' => $this->loadAndGetField( 'course_id' ) ) ); |
134 | 170 | } |
135 | 171 | |
136 | 172 | return $this->course; |
— | — | @@ -146,7 +182,7 @@ |
147 | 183 | */ |
148 | 184 | public function getOrg( $fields = null ) { |
149 | 185 | if ( $this->org === false ) { |
150 | | - $this->org = EPOrg::selectRow( $fields, array( 'id' => $this->getField( 'org_id' ) ) ); |
| 186 | + $this->org = EPOrg::selectRow( $fields, array( 'id' => $this->loadAndGetField( 'org_id' ) ) ); |
151 | 187 | } |
152 | 188 | |
153 | 189 | return $this->org; |
Index: trunk/extensions/EducationProgram/includes/EPDBObject.php |
— | — | @@ -31,6 +31,17 @@ |
32 | 32 | protected $fields = array( 'id' => null ); |
33 | 33 | |
34 | 34 | /** |
| 35 | + * If the object should update summaries of linked items when changed. |
| 36 | + * For example, update the course_count field in universities when a course in courses is deleted. |
| 37 | + * Settings this to false can prevent needless updating work in situations |
| 38 | + * such as deleting a university, which will then delete all it's courses. |
| 39 | + * |
| 40 | + * @since 0.1 |
| 41 | + * @var bool |
| 42 | + */ |
| 43 | + protected $updateSummaries = true; |
| 44 | + |
| 45 | + /** |
35 | 46 | * The database connection to use for read operations. |
36 | 47 | * |
37 | 48 | * @since 0.2 |
— | — | @@ -184,6 +195,23 @@ |
185 | 196 | } |
186 | 197 | |
187 | 198 | /** |
| 199 | + * Gets the value of a field but first loads it if not done so already. |
| 200 | + * |
| 201 | + * @since 0.1 |
| 202 | + * |
| 203 | + * @param string$name |
| 204 | + * |
| 205 | + * @return mixed |
| 206 | + */ |
| 207 | + public function loadAndGetField( $name ) { |
| 208 | + if ( !$this->hasField( $name ) ) { |
| 209 | + $this->loadFields( array( $name ) ); |
| 210 | + } |
| 211 | + |
| 212 | + return $this->getField( $name ); |
| 213 | + } |
| 214 | + |
| 215 | + /** |
188 | 216 | * Remove a field. |
189 | 217 | * |
190 | 218 | * @since 0.1 |
— | — | @@ -1102,4 +1130,15 @@ |
1103 | 1131 | self::setReadDb( DB_SLAVE ); |
1104 | 1132 | } |
1105 | 1133 | |
| 1134 | + /** |
| 1135 | + * Sets the value for the @see $updateSummaries field. |
| 1136 | + * |
| 1137 | + * @since 0.1 |
| 1138 | + * |
| 1139 | + * @param boolean $update |
| 1140 | + */ |
| 1141 | + public function setUpdateSummaries( $update ) { |
| 1142 | + $this->updateSummaries = $update; |
| 1143 | + } |
| 1144 | + |
1106 | 1145 | } |
Index: trunk/extensions/EducationProgram/includes/EPCourse.php |
— | — | @@ -51,6 +51,40 @@ |
52 | 52 | 'description' => '', |
53 | 53 | ); |
54 | 54 | } |
| 55 | + |
| 56 | + /** |
| 57 | + * (non-PHPdoc) |
| 58 | + * @see EPDBObject::loadSummaryFields() |
| 59 | + */ |
| 60 | + public function loadSummaryFields( $summaryFields = null ) { |
| 61 | + if ( is_null( $summaryFields ) ) { |
| 62 | + $summaryFields = array( 'students' ); |
| 63 | + } |
| 64 | + else { |
| 65 | + $summaryFields = (array)$summaryFields; |
| 66 | + } |
| 67 | + |
| 68 | + $fields = array(); |
| 69 | + |
| 70 | + if ( in_array( 'students', $summaryFields ) ) { |
| 71 | + $termIds = EPTerm::selectFields( 'id', array( 'course_id' => $this->getId() ) ); |
| 72 | + |
| 73 | + if ( count( $termIds ) > 0 ) { |
| 74 | + $fields['students'] = wfGetDB( DB_SLAVE )->select( |
| 75 | + 'ep_students_per_term', |
| 76 | + 'COUNT(*) AS rowcount', |
| 77 | + array( 'spt_term_id' => $termIds ) |
| 78 | + ); |
| 79 | + |
| 80 | + $fields['students'] = $fields['students']->fetchObject()->rowcount; |
| 81 | + } |
| 82 | + else { |
| 83 | + $fields['students'] = 0; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + $this->setFields( $fields ); |
| 88 | + } |
55 | 89 | |
56 | 90 | /** |
57 | 91 | * (non-PHPdoc) |
— | — | @@ -58,19 +92,61 @@ |
59 | 93 | */ |
60 | 94 | public function removeFromDB() { |
61 | 95 | $id = $this->getId(); |
62 | | - |
| 96 | + |
| 97 | + if ( $this->updateSummaries ) { |
| 98 | + $this->loadFields( array( 'org_id' ) ); |
| 99 | + $orgId = $this->getField( 'org_id', false ); |
| 100 | + } |
| 101 | + |
63 | 102 | $success = parent::removeFromDB(); |
64 | 103 | |
65 | 104 | if ( $success ) { |
66 | 105 | foreach ( EPTerm::select( 'id', array( 'course_id' => $id ) ) as /* EPTerm */ $term ) { |
| 106 | + $term->setUpdateSummaries( false ); |
67 | 107 | $success = $term->removeFromDB() && $success; |
68 | 108 | } |
69 | 109 | } |
| 110 | + |
| 111 | + if ( $this->updateSummaries && $orgId !== false ) { |
| 112 | + EPOrg::updateSummaryFields( array( 'terms', 'students', 'courses' ), array( 'id' => $orgId ) ); |
| 113 | + } |
70 | 114 | |
71 | 115 | return $success; |
72 | 116 | } |
73 | | - |
| 117 | + |
74 | 118 | /** |
| 119 | + * (non-PHPdoc) |
| 120 | + * @see EPDBObject::insertIntoDB() |
| 121 | + */ |
| 122 | + protected function insertIntoDB() { |
| 123 | + $success = parent::insertIntoDB(); |
| 124 | + |
| 125 | + if ( $this->updateSummaries ) { |
| 126 | + EPOrg::updateSummaryFields( 'courses', array( 'id' => $this->getField( 'org_id' ) ) ); |
| 127 | + } |
| 128 | + |
| 129 | + return $success; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * (non-PHPdoc) |
| 134 | + * @see EPDBObject::updateInDB() |
| 135 | + */ |
| 136 | + protected function updateInDB() { |
| 137 | + $oldOrgId = $this->hasField( 'org_id' ) ? self::selectFieldsRow( 'org_id', array( 'id' => $this->getId() ) ) : false; |
| 138 | + |
| 139 | + $success = parent::updateInDB(); |
| 140 | + |
| 141 | + if ( $this->updateSummaries && $success && $oldOrgId !== false && $oldOrgId !== $this->getField( 'org_id' ) ) { |
| 142 | + $conds = array( 'id' => array( $oldOrgId, $this->getField( 'org_id' ) ) ); |
| 143 | + EPOrg::updateSummaryFields( array( 'terms', 'students', 'courses' ), $conds ); |
| 144 | + EPTerm::updateSummaryFields( 'org_id', array( 'course_id' => $this->getId() ) ); |
| 145 | + } |
| 146 | + |
| 147 | + return $success; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
75 | 151 | * Returns the org associated with this course. |
76 | 152 | * |
77 | 153 | * @since 0.1 |