Index: trunk/extensions/EducationProgram/includes/EPMentor.php |
— | — | @@ -57,5 +57,31 @@ |
58 | 58 | public function getCourses( array $fields = null ) { |
59 | 59 | return array(); // TODO |
60 | 60 | } |
| 61 | + |
| 62 | + /** |
| 63 | + * Retruns if the mentor has any course matching the provided contitions. |
| 64 | + * |
| 65 | + * @since 0.1 |
| 66 | + * |
| 67 | + * @param array $conditions |
| 68 | + * |
| 69 | + * @return boolean |
| 70 | + */ |
| 71 | + public function hasCourse( array $conditions = array() ) { |
| 72 | + return true; // TODO |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Retruns if the mentor has any term matching the provided contitions. |
| 77 | + * |
| 78 | + * @since 0.1 |
| 79 | + * |
| 80 | + * @param array $conditions |
| 81 | + * |
| 82 | + * @return boolean |
| 83 | + */ |
| 84 | + public function hasTerm( array $conditions = array() ) { |
| 85 | + return true; // TODO |
| 86 | + } |
61 | 87 | |
62 | 88 | } |
Index: trunk/extensions/EducationProgram/includes/EPOrgPager.php |
— | — | @@ -120,11 +120,27 @@ |
121 | 121 | $links = parent::getControlLinks( $item ); |
122 | 122 | |
123 | 123 | $links[] = $value = Linker::linkKnown( |
124 | | - SpecialPage::getTitleFor( 'EditInstitution', $item->getField( 'name' ) ), |
125 | | - wfMsg( 'edit' ) |
| 124 | + SpecialPage::getTitleFor( 'Institution', $item->getField( 'name' ) ), |
| 125 | + wfMsg( 'view' ) |
126 | 126 | ); |
127 | 127 | |
128 | | - // TODO |
| 128 | + if ( $this->getUser()->isAllowed( 'epadmin' ) ) { |
| 129 | + $links[] = $value = Linker::linkKnown( |
| 130 | + SpecialPage::getTitleFor( 'EditInstitution', $item->getField( 'name' ) ), |
| 131 | + wfMsg( 'edit' ) |
| 132 | + ); |
| 133 | + |
| 134 | + $links[] = $value = Html::element( |
| 135 | + 'a', |
| 136 | + array( |
| 137 | + 'href' => '#', |
| 138 | + 'class' => 'ep-pager-delete', |
| 139 | + 'data-id' => $item->getId(), |
| 140 | + 'data-type' => 'org', |
| 141 | + ), |
| 142 | + wfMsg( 'delete' ) |
| 143 | + ); |
| 144 | + } |
129 | 145 | |
130 | 146 | return $links; |
131 | 147 | } |
Index: trunk/extensions/EducationProgram/includes/EPDBObject.php |
— | — | @@ -748,6 +748,12 @@ |
749 | 749 | * conditions and returns them as associative arrays. |
750 | 750 | * Provided field names get prefixed. |
751 | 751 | * Returned field names will not have a prefix. |
| 752 | + * |
| 753 | + * When $collapse is true: |
| 754 | + * If one field is selected, each item in the result array will be this field. |
| 755 | + * If two fields are selected, each item in the result array will have as key |
| 756 | + * the first field and as value the second field. |
| 757 | + * If more then two fields are selected, each item will be an associative array. |
752 | 758 | * |
753 | 759 | * @since 0.1 |
754 | 760 | * |
— | — | @@ -755,11 +761,11 @@ |
756 | 762 | * @param array $conditions |
757 | 763 | * @param array $options |
758 | 764 | * @param array $joinConds |
759 | | - * @param boolean $collapse |
| 765 | + * @param boolean $collapse Set to false to always return each result row as associative array. |
760 | 766 | * |
761 | 767 | * @return array of array |
762 | 768 | */ |
763 | | - public static function selectFields( $fields = null, array $conditions = array(), array $options = array(), array $joinConds = array(), $collapse = false ) { |
| 769 | + public static function selectFields( $fields = null, array $conditions = array(), array $options = array(), array $joinConds = array(), $collapse = true ) { |
764 | 770 | if ( is_null( $fields ) ) { |
765 | 771 | $fields = array_keys( static::getFieldTypes() ); |
766 | 772 | } |
— | — | @@ -858,7 +864,32 @@ |
859 | 865 | |
860 | 866 | return count( $objects ) > 0 ? $objects[0] : false; |
861 | 867 | } |
| 868 | + |
| 869 | + /** |
| 870 | + * Selects the the specified fields of the first record matching the provided |
| 871 | + * conditions and returns it as an associative array, or false when nothing matches. |
| 872 | + * This method makes use of selectFields and expects the same parameters and |
| 873 | + * returns the same results (if there are any, if there are none, this method returns false). |
| 874 | + * @see EPDBObject::selectFields |
| 875 | + * |
| 876 | + * @since 0.1 |
| 877 | + * |
| 878 | + * @param array|string|null $fields |
| 879 | + * @param array $conditions |
| 880 | + * @param array $options |
| 881 | + * @param array $joinConds |
| 882 | + * @param boolean $collapse Set to false to always return each result row as associative array. |
| 883 | + * |
| 884 | + * @return mixed|array|false |
| 885 | + */ |
| 886 | + public static function selectFieldsRow( $fields = null, array $conditions = array(), array $options = array(), array $joinConds = array(), $collapse = true ) { |
| 887 | + $options['LIMIT'] = 1; |
862 | 888 | |
| 889 | + $objects = static::selectFields( $fields, $conditions, $options, $joinConds, $collapse ); |
| 890 | + |
| 891 | + return count( $objects ) > 0 ? $objects[0] : false; |
| 892 | + } |
| 893 | + |
863 | 894 | /** |
864 | 895 | * Returns if there is at least one record matching the provided conditions. |
865 | 896 | * Condition field names get prefixed. |
Index: trunk/extensions/EducationProgram/EducationProgram.i18n.php |
— | — | @@ -143,6 +143,10 @@ |
144 | 144 | |
145 | 145 | 'ep-term-invalid-year' => 'The year needs to be number.', |
146 | 146 | 'ep-term-invalid-course' => 'This course does not exist.', |
| 147 | + |
| 148 | + // ep.pager |
| 149 | + 'ep-pager-confirm-delete' => 'Are you sure you want to delete this item?', |
| 150 | + 'ep-pager-delete-fail' => 'Could not delete this item.', |
147 | 151 | ); |
148 | 152 | |
149 | 153 | /** Message documentation (Message documentation) |
Index: trunk/extensions/EducationProgram/EducationProgram.php |
— | — | @@ -49,6 +49,8 @@ |
50 | 50 | $wgAutoloadClasses['EPHooks'] = dirname( __FILE__ ) . '/EducationProgram.hooks.php'; |
51 | 51 | $wgAutoloadClasses['EPSettings'] = dirname( __FILE__ ) . '/EducationProgram.settings.php'; |
52 | 52 | |
| 53 | +$wgAutoloadClasses['ApiDeleteEducation'] = dirname( __FILE__ ) . '/api/ApiDeleteEducation.php'; |
| 54 | + |
53 | 55 | $wgAutoloadClasses['EPCourse'] = dirname( __FILE__ ) . '/includes/EPCourse.php'; |
54 | 56 | $wgAutoloadClasses['EPCoursePager'] = dirname( __FILE__ ) . '/includes/EPCoursePager.php'; |
55 | 57 | $wgAutoloadClasses['EPDBObject'] = dirname( __FILE__ ) . '/includes/EPDBObject.php'; |
— | — | @@ -117,8 +119,8 @@ |
118 | 120 | $egEPDBObjects[] = array( 'table' => 'ep_mentors_per_org', 'prefix' => 'mpo_' ); |
119 | 121 | |
120 | 122 | // API |
| 123 | +$wgAPIModules['deleteeducation'] = 'ApiDeleteEducation'; |
121 | 124 | |
122 | | - |
123 | 125 | // Hooks |
124 | 126 | $wgHooks['LoadExtensionSchemaUpdates'][] = 'EPHooks::onSchemaUpdate'; |
125 | 127 | $wgHooks['UnitTestsList'][] = 'EPHooks::registerUnitTests'; |
— | — | @@ -164,10 +166,23 @@ |
165 | 167 | 'remoteExtPath' => 'EducationProgram/resources' |
166 | 168 | ); |
167 | 169 | |
| 170 | +$wgResourceModules['ep.api'] = $moduleTemplate + array( |
| 171 | + 'scripts' => array( |
| 172 | + 'ep.api.js', |
| 173 | + ), |
| 174 | +); |
| 175 | + |
168 | 176 | $wgResourceModules['ep.pager'] = $moduleTemplate + array( |
169 | 177 | 'scripts' => array( |
170 | 178 | 'ep.pager.js', |
171 | 179 | ), |
| 180 | + 'dependencies' => array( |
| 181 | + 'ep.api', |
| 182 | + ), |
| 183 | + 'messages' => array( |
| 184 | + 'ep-pager-confirm-delete', |
| 185 | + 'ep-pager-delete-fail', |
| 186 | + ), |
172 | 187 | ); |
173 | 188 | |
174 | 189 | $wgResourceModules['ep.datepicker'] = $moduleTemplate + array( |
Index: trunk/extensions/EducationProgram/api/ApiDeleteEducation.php |
— | — | @@ -0,0 +1,140 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * API module to delete objects stored by the Education Program extension. |
| 6 | + * |
| 7 | + * @since 0.1 |
| 8 | + * |
| 9 | + * @file ApiDeleteEducation.php |
| 10 | + * @ingroup Education Program |
| 11 | + * @ingroup API |
| 12 | + * |
| 13 | + * @licence GNU GPL v3+ |
| 14 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 15 | + */ |
| 16 | +class ApiDeleteEducation extends ApiBase { |
| 17 | + |
| 18 | + protected static $typeMap = array( |
| 19 | + 'org' => 'EPOrg', |
| 20 | + 'course' => 'EPCourse', |
| 21 | + 'trem' => 'EPTerm', |
| 22 | + 'student' => 'EPStudent', |
| 23 | + 'mentor' => 'EPMentor', |
| 24 | + ); |
| 25 | + |
| 26 | + public function execute() { |
| 27 | + $params = $this->extractRequestParams(); |
| 28 | + |
| 29 | + if ( !$this->userIsAllowed( $params['type'], $params ) || $this->getUser()->isBlocked() ) { |
| 30 | + $this->dieUsageMsg( array( 'badaccess-groups' ) ); |
| 31 | + } |
| 32 | + |
| 33 | + $everythingOk = true; |
| 34 | + |
| 35 | + foreach ( $params['ids'] as $id ) { |
| 36 | + // $instance->removeFromDB is used instead of Class::delete, |
| 37 | + // so that linked data also gets deleted. |
| 38 | + $c = self::$typeMap[$params['type']]; |
| 39 | + $object = new $c( array( 'id' => $id ) ); |
| 40 | + $everythingOk = $object->removeFromDB() && $everythingOk; |
| 41 | + } |
| 42 | + |
| 43 | + $this->getResult()->addValue( |
| 44 | + null, |
| 45 | + 'success', |
| 46 | + $everythingOk |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @since 0.1 |
| 52 | + * |
| 53 | + * @param string $type |
| 54 | + * @param array $params |
| 55 | + */ |
| 56 | + protected function userIsAllowed( $type, array $params ) { |
| 57 | + $user = $this->getUser(); |
| 58 | + |
| 59 | + if ( $type === 'student' ) { |
| 60 | + return EPStudent::selectField( 'id', array( 'user_id' => $user->getId() ) ) === $params['id']; |
| 61 | + } |
| 62 | + |
| 63 | + if ( $type === 'mentor' ) { |
| 64 | + return EPMentor::selectField( 'id', array( 'user_id' => $user->getId() ) ) === $params['id']; |
| 65 | + } |
| 66 | + |
| 67 | + if ( $user->isAllowed( 'epadmin' ) ) { |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + if ( $user->isAllowed( 'epmentor' ) ) { |
| 72 | + $mentor = new EPMentor( null, array( 'user_id' => $user->getId() ) ); |
| 73 | + |
| 74 | + if ( $mentor !== false ) { |
| 75 | + if ( $type === 'course' ) { |
| 76 | + return $mentor->hasCourse( array( 'id' => $params['id'] ) ); |
| 77 | + } |
| 78 | + elseif ( $type === 'term' ) { |
| 79 | + return $mentor->hasTerm( array( 'id' => $params['id'] ) ); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + public function needsToken() { |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + public function mustBePosted() { |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + public function getAllowedParams() { |
| 96 | + return array( |
| 97 | + 'ids' => array( |
| 98 | + ApiBase::PARAM_TYPE => 'integer', |
| 99 | + ApiBase::PARAM_REQUIRED => true, |
| 100 | + ApiBase::PARAM_ISMULTI => true, |
| 101 | + ), |
| 102 | + 'type' => array( |
| 103 | + ApiBase::PARAM_TYPE => array_keys( self::$typeMap ), |
| 104 | + ApiBase::PARAM_REQUIRED => true, |
| 105 | + ApiBase::PARAM_ISMULTI => false, |
| 106 | + ), |
| 107 | + 'token' => null, |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + public function getParamDescription() { |
| 112 | + return array( |
| 113 | + 'ids' => 'The IDs of the reviews to delete', |
| 114 | + 'token' => 'Edit token. You can get one of these through prop=info.', |
| 115 | + 'type' => 'Type of object to delete.', |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + public function getDescription() { |
| 120 | + return array( |
| 121 | + 'API module for deleting objects parts of the Education Program extension.' |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + public function getPossibleErrors() { |
| 126 | + return array_merge( parent::getPossibleErrors(), array( |
| 127 | + ) ); |
| 128 | + } |
| 129 | + |
| 130 | + protected function getExamples() { |
| 131 | + return array( |
| 132 | + 'api.php?action=deleteeducation&ids=42&type=course', |
| 133 | + 'api.php?action=deleteeducation&ids=4|2&type=student', |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + public function getVersion() { |
| 138 | + return __CLASS__ . ': $Id$'; |
| 139 | + } |
| 140 | + |
| 141 | +} |
Property changes on: trunk/extensions/EducationProgram/api/ApiDeleteEducation.php |
___________________________________________________________________ |
Added: svn:keywords |
1 | 142 | + Id |
Index: trunk/extensions/EducationProgram/resources/ep.api.js |
— | — | @@ -0,0 +1,38 @@ |
| 2 | +/** |
| 3 | + * JavasSript for the Education Program MediaWiki extension. |
| 4 | + * @see https://www.mediawiki.org/wiki/Extension:Reviews |
| 5 | + * |
| 6 | + * @licence GNU GPL v3 or later |
| 7 | + * @author Jeroen De Dauw <jeroendedauw at gmail dot com> |
| 8 | + */ |
| 9 | + |
| 10 | +window.educationProgram = new( function() { |
| 11 | + |
| 12 | + this.api = new( function() { |
| 13 | + |
| 14 | + this.remove = function( data, callback ) { |
| 15 | + var requestArgs = { |
| 16 | + 'action': 'deleteeducation', |
| 17 | + 'format': 'json', |
| 18 | + 'token': mw.user.tokens.get( 'editToken' ), |
| 19 | + 'ids': data.id, |
| 20 | + 'type': data.type |
| 21 | + }; |
| 22 | + |
| 23 | + $.post( |
| 24 | + wgScriptPath + '/api.php', |
| 25 | + requestArgs, |
| 26 | + function( data ) { |
| 27 | + var success = data.hasOwnProperty( 'success' ) && data.success; |
| 28 | + |
| 29 | + callback( { |
| 30 | + 'success': success |
| 31 | + } ); |
| 32 | + } |
| 33 | + ); |
| 34 | + }; |
| 35 | + |
| 36 | + } ); |
| 37 | + |
| 38 | +} ); |
| 39 | + |
Index: trunk/extensions/EducationProgram/resources/ep.pager.js |
— | — | @@ -6,8 +6,8 @@ |
7 | 7 | * @author Jeroen De Dauw <jeroendedauw at gmail dot com> |
8 | 8 | */ |
9 | 9 | |
10 | | -(function( $, mw ) { |
11 | | - |
| 10 | +(function( $, mw, ep ) { |
| 11 | + |
12 | 12 | $( document ).ready( function() { |
13 | 13 | |
14 | 14 | $( '.ep-pager-clear' ).click( function() { |
— | — | @@ -17,6 +17,37 @@ |
18 | 18 | return false; |
19 | 19 | } ); |
20 | 20 | |
| 21 | + $( '.ep-pager-delete' ).click( function() { |
| 22 | + if ( confirm( mw.msg( 'ep-pager-confirm-delete' ) ) ) { |
| 23 | + $this = $( this ); |
| 24 | + |
| 25 | + ep.api.remove( |
| 26 | + { |
| 27 | + 'type': $this.attr( 'data-type' ), |
| 28 | + 'id': $this.attr( 'data-id' ) |
| 29 | + }, |
| 30 | + function( result ) { |
| 31 | + if ( result.success ) { |
| 32 | + $tr = $this.closest( 'tr' ); |
| 33 | + $table = $tr.closest( 'table' ); |
| 34 | + |
| 35 | + if ( $table.find( 'tr' ).length > 2 ) { |
| 36 | + $tr.slideUp( 'slow', function() { $tr.remove(); } ); |
| 37 | + } |
| 38 | + else { |
| 39 | + $table.slideUp( 'slow', function() { |
| 40 | + $table.remove(); |
| 41 | + } ); |
| 42 | + } |
| 43 | + } |
| 44 | + else { |
| 45 | + alert( mw.msg( 'ep-pager-delete-fail' ) ); // TODO |
| 46 | + } |
| 47 | + } |
| 48 | + ); |
| 49 | + } |
| 50 | + } ); |
| 51 | + |
21 | 52 | } ); |
22 | 53 | |
23 | | -})( window.jQuery, window.mediaWiki ); |
\ No newline at end of file |
| 54 | +})( window.jQuery, window.mediaWiki, window.educationProgram ); |
\ No newline at end of file |