r107196 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107195‎ | r107196 | r107197 >
Date:00:42, 24 December 2011
Author:jeroendedauw
Status:deferred
Tags:educationprogram 
Comment:
added a bunch of methods to get linked objects
Modified paths:
  • /trunk/extensions/EducationProgram/EducationProgram.i18n.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPCourse.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPMentor.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPOrg.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPTerm.php (modified) (history)
  • /trunk/extensions/EducationProgram/specials/SpecialCourse.php (modified) (history)
  • /trunk/extensions/EducationProgram/specials/SpecialInstitution.php (modified) (history)
  • /trunk/extensions/EducationProgram/specials/SpecialTerm.php (modified) (history)
  • /trunk/extensions/EducationProgram/sql/EducationProgram.sql (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/sql/EducationProgram.sql
@@ -27,6 +27,7 @@
2828 CREATE TABLE IF NOT EXISTS /*_*/ep_terms (
2929 term_id INT unsigned NOT NULL auto_increment PRIMARY KEY,
3030 term_course_id INT unsigned NOT NULL, -- Foreign key on ep_courses.course_id
 31+ term_org_id INT unsigned NOT NULL, -- Foreign key on ep_orgs.org_id. Helper field, not strictly needed.
3132 term_year SMALLINT unsigned NOT NULL, -- Yeah in which the term takes place
3233 term_start varbinary(14) NOT NULL, -- Start time of the term
3334 term_end varbinary(14) NOT NULL -- End time of the term
Index: trunk/extensions/EducationProgram/specials/SpecialInstitution.php
@@ -35,25 +35,30 @@
3636
3737 $out = $this->getOutput();
3838
39 - $out->setPageTitle( wfMsgExt( 'ep-institution-title', 'parsemag', $this->subPage ) );
40 -
41 - $org = EPOrg::selectRow( null, array( 'name' => $this->subPage ) );
42 -
43 - if ( $org === false ) {
44 - if ( $this->getUser()->isAllowed( 'epadmin' ) ) {
45 - $out->addWikiMsg( 'ep-institution-create', $this->subPage );
46 - EPOrg::displayAddNewControl( $this->getContext(), array( 'name' => $this->subPage ) );
47 - }
48 - else {
49 - $out->addWikiMsg( 'ep-institution-none', $this->subPage );
50 - }
 39+ if ( trim( $subPage ) === '' ) {
 40+ $this->getOutput()->redirect( SpecialPage::getTitleFor( 'Institutions' )->getLocalURL() );
5141 }
5242 else {
53 - $this->displayInfo( $org );
 43+ $out->setPageTitle( wfMsgExt( 'ep-institution-title', 'parsemag', $this->subPage ) );
5444
55 - $out->addHTML( Html::element( 'h2', array(), wfMsg( 'ep-institution-courses' ) ) );
 45+ $org = EPOrg::selectRow( null, array( 'name' => $this->subPage ) );
5646
57 - EPCourse::displayPager( $this->getContext(), array( 'org_id' => $org->getId() ) );
 47+ if ( $org === false ) {
 48+ if ( $this->getUser()->isAllowed( 'epadmin' ) ) {
 49+ $out->addWikiMsg( 'ep-institution-create', $this->subPage );
 50+ EPOrg::displayAddNewControl( $this->getContext(), array( 'name' => $this->subPage ) );
 51+ }
 52+ else {
 53+ $out->addWikiMsg( 'ep-institution-none', $this->subPage );
 54+ }
 55+ }
 56+ else {
 57+ $this->displayInfo( $org );
 58+
 59+ $out->addHTML( Html::element( 'h2', array(), wfMsg( 'ep-institution-courses' ) ) );
 60+
 61+ EPCourse::displayPager( $this->getContext(), array( 'org_id' => $org->getId() ) );
 62+ }
5863 }
5964 }
6065
Index: trunk/extensions/EducationProgram/specials/SpecialCourse.php
@@ -35,26 +35,31 @@
3636
3737 $out = $this->getOutput();
3838
39 - $out->setPageTitle( wfMsgExt( 'ep-course-title', 'parsemag', $this->subPage ) );
 39+ if ( trim( $subPage ) === '' ) {
 40+ $this->getOutput()->redirect( SpecialPage::getTitleFor( 'Courses' )->getLocalURL() );
 41+ }
 42+ else {
 43+ $out->setPageTitle( wfMsgExt( 'ep-course-title', 'parsemag', $this->subPage ) );
4044
41 - $course = EPCourse::selectRow( null, array( 'name' => $this->subPage ) );
42 -
43 - if ( $course === false ) {
44 - if ( $this->getUser()->isAllowed( 'epadmin' ) || $this->getUser()->isAllowed( 'epmentor' ) ) {
45 - $out->addWikiMsg( 'ep-course-create', $this->subPage );
46 - EPCourse::displayAddNewRegion( $this->getContext(), array( 'name' => $this->subPage ) );
 45+ $course = EPCourse::selectRow( null, array( 'name' => $this->subPage ) );
 46+
 47+ if ( $course === false ) {
 48+ if ( $this->getUser()->isAllowed( 'epadmin' ) || $this->getUser()->isAllowed( 'epmentor' ) ) {
 49+ $out->addWikiMsg( 'ep-course-create', $this->subPage );
 50+ EPCourse::displayAddNewRegion( $this->getContext(), array( 'name' => $this->subPage ) );
 51+ }
 52+ else {
 53+ $out->addWikiMsg( 'ep-course-none', $this->subPage );
 54+ }
4755 }
4856 else {
49 - $out->addWikiMsg( 'ep-course-none', $this->subPage );
 57+ $this->displayInfo( $course );
 58+
 59+ $out->addHTML( Html::element( 'h2', array(), wfMsg( 'ep-course-terms' ) ) );
 60+
 61+ EPTerm::displayPager( $this->getContext(), array( 'course_id' => $course->getId() ) );
5062 }
5163 }
52 - else {
53 - $this->displayInfo( $course );
54 -
55 - $out->addHTML( Html::element( 'h2', array(), wfMsg( 'ep-course-terms' ) ) );
56 -
57 - EPTerm::displayPager( $this->getContext(), array( 'course_id' => $course->getId() ) );
58 - }
5964 }
6065
6166 /**
Index: trunk/extensions/EducationProgram/specials/SpecialTerm.php
@@ -35,26 +35,31 @@
3636
3737 $out = $this->getOutput();
3838
39 - $out->setPageTitle( wfMsgExt( 'ep-term-title', 'parsemag', $this->subPage ) );
 39+ if ( trim( $subPage ) === '' ) {
 40+ $this->getOutput()->redirect( SpecialPage::getTitleFor( 'Terms' )->getLocalURL() );
 41+ }
 42+ else {
 43+ $out->setPageTitle( wfMsgExt( 'ep-term-title', 'parsemag', $this->subPage ) );
4044
41 - $term = EPTerm::selectRow( null, array( 'id' => $this->subPage ) );
42 -
43 - if ( $term === false ) {
44 - if ( $this->getUser()->isAllowed( 'epadmin' ) || $this->getUser()->isAllowed( 'epmentor' ) ) {
45 - $out->addWikiMsg( 'ep-term-create', $this->subPage );
46 - EPTerm::displayAddNewRegion( $this->getContext(), array( 'id' => $this->subPage ) );
 45+ $term = EPTerm::selectRow( null, array( 'id' => $this->subPage ) );
 46+
 47+ if ( $term === false ) {
 48+ if ( $this->getUser()->isAllowed( 'epadmin' ) || $this->getUser()->isAllowed( 'epmentor' ) ) {
 49+ $out->addWikiMsg( 'ep-term-create', $this->subPage );
 50+ EPTerm::displayAddNewRegion( $this->getContext(), array( 'id' => $this->subPage ) );
 51+ }
 52+ else {
 53+ $out->addWikiMsg( 'ep-term-none', $this->subPage );
 54+ }
4755 }
4856 else {
49 - $out->addWikiMsg( 'ep-term-none', $this->subPage );
 57+ $this->displayInfo( $term );
 58+
 59+ $out->addHTML( Html::element( 'h2', array(), wfMsg( 'ep-term-students' ) ) );
 60+
 61+ // TODO: students
5062 }
5163 }
52 - else {
53 - $this->displayInfo( $term );
54 -
55 - $out->addHTML( Html::element( 'h2', array(), wfMsg( 'ep-term-students' ) ) );
56 -
57 - // TODO
58 - }
5964 }
6065
6166 /**
Index: trunk/extensions/EducationProgram/includes/EPMentor.php
@@ -14,6 +14,14 @@
1515 class EPMentor extends EPDBObject {
1616
1717 /**
 18+ * Cached array of the linked EPOrg objects.
 19+ *
 20+ * @since 0.1
 21+ * @var array|false
 22+ */
 23+ protected $orgs = false;
 24+
 25+ /**
1826 * @see parent::getFieldTypes
1927 *
2028 * @since 0.1
@@ -37,12 +45,16 @@
3846 * @return array of EPOrg
3947 */
4048 public function getOrgs( array $fields = null ) {
41 - return EPOrg::select(
42 - $fields,
43 - array( array( 'ep_mentors_per_org', 'mentor_id' ), $this->getId() ),
44 - array(),
45 - array( 'orgs' => array( 'INNER JOIN', array( array( array( 'ep_mentors_per_org', 'org_id' ), array( 'orgs', 'id' ) ) ) ) )
46 - );
 49+ if ( $this->orgs === false ) {
 50+ $this->orgs = EPOrg::select(
 51+ $fields,
 52+ array( array( 'ep_mentors_per_org', 'mentor_id' ), $this->getId() ),
 53+ array(),
 54+ array( 'orgs' => array( 'INNER JOIN', array( array( array( 'ep_mentors_per_org', 'org_id' ), array( 'orgs', 'id' ) ) ) ) )
 55+ );
 56+ }
 57+
 58+ return $this->orgs;
4759 }
4860
4961 /**
@@ -55,10 +67,27 @@
5668 * @return array of EPCourse
5769 */
5870 public function getCourses( array $fields = null ) {
59 - return array(); // TODO
 71+ return array_reduce( $this->getOrgs(), function( array $courses, EPOrg $org ) use ( $fields ) {
 72+ return array_merge( $courses, $org->getCourses( $fields ) );
 73+ }, array() );
6074 }
6175
6276 /**
 77+ * Retruns the terms this mentor can manage.
 78+ *
 79+ * @since 0.1
 80+ *
 81+ * @param array|null $fields
 82+ *
 83+ * @return array of EPTerm
 84+ */
 85+ public function getTerms( array $fields = null ) {
 86+ return array_reduce( $this->getOrgs(), function( array $terms, EPOrg $org ) use ( $fields ) {
 87+ return array_merge( $terms, $org->getTerms( $fields ) );
 88+ }, array() );
 89+ }
 90+
 91+ /**
6392 * Retruns if the mentor has any course matching the provided contitions.
6493 *
6594 * @since 0.1
@@ -68,7 +97,7 @@
6998 * @return boolean
7099 */
71100 public function hasCourse( array $conditions = array() ) {
72 - return true; // TODO
 101+ return true;
73102 }
74103
75104 /**
Index: trunk/extensions/EducationProgram/includes/EPOrg.php
@@ -14,6 +14,22 @@
1515 class EPOrg extends EPDBObject {
1616
1717 /**
 18+ * Cached array of the linked EPCourse objects.
 19+ *
 20+ * @since 0.1
 21+ * @var array|false
 22+ */
 23+ protected $courses = false;
 24+
 25+ /**
 26+ * Cached array of the linked EPTerm objects.
 27+ *
 28+ * @since 0.1
 29+ * @var array|false
 30+ */
 31+ protected $terms = false;
 32+
 33+ /**
1834 * @see parent::getFieldTypes
1935 *
2036 * @since 0.1
@@ -182,4 +198,38 @@
183199 }
184200 }
185201
 202+ /**
 203+ * Retruns the courses linked to this org.
 204+ *
 205+ * @since 0.1
 206+ *
 207+ * @param array|null $fields
 208+ *
 209+ * @return array of EPCourse
 210+ */
 211+ public function getCourses( array $fields = null ) {
 212+ if ( $this->courses === false ) {
 213+ $this->courses = EPCourse::select( $fields, array( 'org_id' => $this->getId() ) );
 214+ }
 215+
 216+ return $this->courses;
 217+ }
 218+
 219+ /**
 220+ * Retruns the terms linked to this org.
 221+ *
 222+ * @since 0.1
 223+ *
 224+ * @param array|null $fields
 225+ *
 226+ * @return array of EPTerm
 227+ */
 228+ public function getTerms( array $fields = null ) {
 229+ if ( $this->terms === false ) {
 230+ $this->terms = EPTerm::select( $fields, array( 'org_id' => $this->getId() ) );
 231+ }
 232+
 233+ return $this->terms;
 234+ }
 235+
186236 }
Index: trunk/extensions/EducationProgram/includes/EPTerm.php
@@ -15,6 +15,22 @@
1616 class EPTerm extends EPDBObject {
1717
1818 /**
 19+ * Field for caching the linked course.
 20+ *
 21+ * @since 0.1
 22+ * @var EPCourse|false
 23+ */
 24+ protected $course = false;
 25+
 26+ /**
 27+ * Field for caching the linked org.
 28+ *
 29+ * @since 0.1
 30+ * @var EPOrg|false
 31+ */
 32+ protected $org = false;
 33+
 34+ /**
1935 * @see parent::getFieldTypes
2036 *
2137 * @since 0.1
@@ -25,6 +41,7 @@
2642 return array(
2743 'id' => 'id',
2844 'course_id' => 'id',
 45+ 'org_id' => 'id',
2946
3047 'year' => 'int',
3148 'start' => 'str', // TS_MW
@@ -46,6 +63,19 @@
4764
4865 /**
4966 * (non-PHPdoc)
 67+ * @see EPDBObject::insertIntoDB()
 68+ */
 69+ protected function insertIntoDB() {
 70+ if ( !$this->hasField( 'org_id' ) ) {
 71+ $this->setField( 'org_id', $this->getCourse( 'org_id' )->getField( 'org_id' ) );
 72+ }
 73+
 74+
 75+ parent::insertIntoDB();
 76+ }
 77+
 78+ /**
 79+ * (non-PHPdoc)
5080 * @see EPDBObject::removeFromDB()
5181 */
5282 public function removeFromDB() {
@@ -61,6 +91,40 @@
6292 }
6393
6494 /**
 95+ * Returns the course associated with this term.
 96+ *
 97+ * @since 0.1
 98+ *
 99+ * @param string|array|null $fields
 100+ *
 101+ * @return EPCourse
 102+ */
 103+ public function getCourse( $fields = null ) {
 104+ if ( $this->course === false ) {
 105+ $this->course = EPCourse::selectRow( $fields, array( 'id' => $this->getField( 'course_id' ) ) );
 106+ }
 107+
 108+ return $this->course;
 109+ }
 110+
 111+ /**
 112+ * Returns the org associated with this term.
 113+ *
 114+ * @since 0.1
 115+ *
 116+ * @param string|array|null $fields
 117+ *
 118+ * @return EPOrg
 119+ */
 120+ public function getOrg( $fields = null ) {
 121+ if ( $this->org === false ) {
 122+ $this->org = EPOrg::selectRow( $fields, array( 'id' => $this->getField( 'org_id' ) ) );
 123+ }
 124+
 125+ return $this->org;
 126+ }
 127+
 128+ /**
65129 * Display a pager with terms.
66130 *
67131 * @since 0.1
Index: trunk/extensions/EducationProgram/includes/EPCourse.php
@@ -15,6 +15,14 @@
1616 class EPCourse extends EPDBObject {
1717
1818 /**
 19+ * Field for caching the linked org.
 20+ *
 21+ * @since 0.1
 22+ * @var EPOrg|false
 23+ */
 24+ protected $org = false;
 25+
 26+ /**
1927 * @see parent::getFieldTypes
2028 *
2129 * @since 0.1
@@ -60,6 +68,23 @@
6169 }
6270
6371 /**
 72+ * Returns the org associated with this course.
 73+ *
 74+ * @since 0.1
 75+ *
 76+ * @param string|array|null $fields
 77+ *
 78+ * @return EPOrg
 79+ */
 80+ public function getOrg( $fields = null ) {
 81+ if ( $this->org === false ) {
 82+ $this->org = EPOrg::selectRow( $fields, array( 'id' => $this->getField( 'org_id' ) ) );
 83+ }
 84+
 85+ return $this->org;
 86+ }
 87+
 88+ /**
6489 * Returns a list of courses in an array that can be fed to select inputs.
6590 *
6691 * @since 0.1
Index: trunk/extensions/EducationProgram/EducationProgram.i18n.php
@@ -160,14 +160,14 @@
161161 // Special:Course
162162 'ep-course-title' => 'Course: $1',
163163 'ep-course-terms' => 'Terms',
164 - 'ep-course-none' => 'There is no course with name "$1". See [[Special:Course|here]] for a list of courses.',
 164+ 'ep-course-none' => 'There is no course with name "$1". See [[Special:Courses|here]] for a list of courses.',
165165 'ep-course-create' => 'There is no course with name "$1" yet, but you can create it.',
166166
167167 // Special:Term
168168 'ep-term-title' => 'Term: $1',
169169 'ep-term-students' => 'Students',
170 - 'ep-term-none' => 'There is no term with name "$1". See [[Special:Terms|here]] for a list of terms.',
171 - 'ep-term-create' => 'There is no term with name "$1" yet, but you can create it.',
 170+ 'ep-term-none' => 'There is no term with id "$1". See [[Special:Terms|here]] for a list of terms.',
 171+ 'ep-term-create' => 'There is no term with id "$1", but you can create a new one.',
172172 );
173173
174174 /** Message documentation (Message documentation)

Status & tagging log