Brian Wolff has submitted this change and it was merged. Change subject: Adding database schema and appropriate hook Grades page now interacts with database need to add functions on moodle end to get internal ids and grades ......................................................................
Adding database schema and appropriate hook Grades page now interacts with database need to add functions on moodle end to get internal ids and grades Change-Id: I038268d84c042ef98c8ac5b8ac95bf7f37646279 --- M MoodleWS.alias.php M MoodleWS.body.php M MoodleWS.i18n.php M MoodleWS.php M SpecialMoodleWS.php A moodleusers.sql 6 files changed, 156 insertions(+), 100 deletions(-) Approvals: Brian Wolff: Verified; Looks good to me, approved diff --git a/MoodleWS.alias.php b/MoodleWS.alias.php index 6f8099a..cb6e76e 100644 --- a/MoodleWS.alias.php +++ b/MoodleWS.alias.php @@ -5,16 +5,16 @@ * @file * @ingroup Extensions */ - + $specialPageAliases = array(); - + /** English * @author Clancer */ $specialPageAliases[ 'en' ] = array( 'Moodle Grades' => array( 'Moodle Grades', 'Moodle Grades' ), ); - + /** Deutsch * @author <your username> * @@ -22,4 +22,4 @@ 'MyExtension' => array( 'MeineErweiterung', 'Meine Erweiterung' ), ); */ -?> +?> \ No newline at end of file diff --git a/MoodleWS.body.php b/MoodleWS.body.php index e630331..5c7a7dc 100644 --- a/MoodleWS.body.php +++ b/MoodleWS.body.php @@ -1,4 +1,20 @@ <?php # move stuff here soon +class MoodleWS { + public static function setupSchema( DatabaseUpdater $updater ) { + $updater->addExtensionTable( 'moodleusers', "/moodleusers.sql" ); -?> \ No newline at end of file + +$file = 'people.txt'; +// Open the file to get existing content +$current = file_get_contents($file); +// Append a new person to the file +$current .= "John Smith\n"; +// Write the contents back to the file +file_put_contents($file, $current); + + + return true; + } +} +?> diff --git a/MoodleWS.i18n.php b/MoodleWS.i18n.php index 394a417..62e8cec 100644 --- a/MoodleWS.i18n.php +++ b/MoodleWS.i18n.php @@ -1,12 +1,8 @@ <?php $messages['en'] = array( - 'uploadwizard' => 'uploadwizard' => '{{doc-special|UploadWizard}}, 'uploadwizard-desc' => 'Upload Wizard, a user-friendly tool for uploading multimedia' ); $messages['qqq'] = array( - 'uploadwizard' => '{{doc-special|UploadWizard}} -UploadWizard is a wizard to upload multimedia files to a MediaWiki website. -For more information, see [[mw:UploadWizard]].', 'uploadwizard-desc' => 'Description of extension. It refers to [//blog.wikimedia.org/blog/2009/07/02/ford-foundation-awards-300k-grant-for-wikimedia-commons/ this event], i.e. the development was paid with this $300,000 grant.' ); -?> \ No newline at end of file +?> diff --git a/MoodleWS.php b/MoodleWS.php index bbcd001..e7b1310 100644 --- a/MoodleWS.php +++ b/MoodleWS.php @@ -1,23 +1,27 @@ <?php //Some globals for later config -$wgMoodleWSInstance = 'local'; -$wgMoodleWSToken = 'abcdefghijklmnopqrstuv1234567890'; +$wgMoodleWSInstance = 'localhost/moodle/'; +$wgMoodleWSToken = '695a8fecb18fb61c2a8bef28080830de'; +$wgMoodleWSServiceShortname = 'moodle_mobile_app'; // Autoload $wgAutoloadClasses['MoodleWS'] = __DIR__ . '/MoodleWS.body.php'; $wgAutoloadClasses[ 'SpecialMoodleWS' ] = __DIR__ . '/SpecialMoodleWS.php'; # Location of the SpecialMoodleWS class (Tell MediaWiki to load this file) + $wgExtensionMessagesFiles[ 'MoodleWS' ] = __DIR__ . '/MoodleWS.i18n.php'; # Location of a messages file (Tell MediaWiki to load this file) + $wgExtensionMessagesFiles[ 'MoodleWSAlias' ] = __DIR__ . '/MoodleWS.alias.php'; # Location of an aliases file (Tell MediaWiki to load this file) $wgSpecialPages[ 'MoodleWS' ] = 'SpecialMoodleWS'; # Tell MediaWiki about the new special page and its class name $wgSpecialPageGroups[ 'MoodleWS' ] = 'other'; + /* Complex loading $wgMyExtensionIncludes = __DIR__ . '/includes'; - + ## Special page class -$wgAutoloadClasses['SpecialMyExtension'] +$wgAutoloadClasses['SpecialMyExtension'] = $wgMyExtensionIncludes . '/SpecialMyExtension.php'; - + ## Tag class $wgAutoloadClasses['TagMyExtension'] = $wgMyExtensionIncludes . '/TagMyExtension.php'; @@ -27,47 +31,57 @@ $wgAvailableRights[] = 'viewgrades'; $wgGroupPermissions['user']['viewgrades'] = true; + // Register $wgExtensionCredits['validextensionclass'][] = array( 'path' => __FILE__, 'name' => 'Example', - 'author' =>'John Doe', - 'url' => 'https://www.mediawiki.org/wiki/Extension:Example', + 'author' =>'John Doe', + 'url' => 'https://www.mediawiki.org/wiki/Extension:Example', 'description' => 'This extension is an example and performs no discernible function', 'version' => 1.5, ); - + $wgHooks['ParserFirstCallInit'][] = 'wfSampleParserInit'; - +//$wgHooks['LoadExtensionSchemaUpdates'][] = 'MoodleWS::setupSchema'; +/*$wgHooks['LoadExtensionSchemaUpdates'][] = 'fnMyHook'; +function fnMyHook( DatabaseUpdater $updater ) { + $updater->addExtensionTable( 'moodleusers', + dirname( __FILE__ ) . '/moodleusers.sql', true ); + return true; +}*/ + // Hook our callback function into the parser function wfSampleParserInit( Parser $parser ) { - // When the parser sees the <sample> tag, it executes + // When the parser sees the <sample> tag, it executes // the wfSampleRender function (see below) $parser->setHook( 'sample', 'wfSampleRender' ); // Always return true from this function. The return value does not denote // success or otherwise have meaning - it just must always be true. return true; } - -// Execute + +// Execute function wfSampleRender( $input, array $args, Parser $parser, PPFrame $frame ) { $parser->disableCache(); - + global $wgMoodleWSInstance; - + //$output = $parser->recursiveTagParse( $text, $frame ); //return '<div class="wonderful">' . $output . '</div>'; - - $attr = array(); + + $attr = array(); // This time, make a list of attributes and their values, // and dump them, along with the user input foreach( $args as $name => $value ) $attr[] = '<strong>' . htmlspecialchars( $name ) . '</strong> = ' . htmlspecialchars( $value ); return implode( '<br />', $attr ) . "\n\n" . htmlspecialchars( $input ) . $wgMoodleWSInstance; + +// The following lines can be used to get the variable values directly: +// $to = $args['to'] ; +// $email = $args['email'] ; -/* The following lines can be used to get the variable values directly: - $to = $args['to'] ; - $email = $args['email'] ; -*/ } + + ?> diff --git a/SpecialMoodleWS.php b/SpecialMoodleWS.php index 1b76215..2d57fcf 100644 --- a/SpecialMoodleWS.php +++ b/SpecialMoodleWS.php @@ -9,115 +9,137 @@ # there is a function to return to main page instead of the hack used in cancel but cant remember it class SpecialMoodleWS extends SpecialPage { function __construct() { - //parent::__construct( 'MoodleWS' ); - // To implement permissions (wont show up in specialpages list) for this page + #parent::__construct( 'MoodleWS' ); + # To implement permissions (wont show up in specialpages list) for this page parent::__construct( 'MoodleWS', 'viewgrades' ); } - - // WIP + + # WIP + # will get sanitised username and password from post function newMoodleUser () { + global $wgUser; + $dbw = wfGetDB( DB_MASTER ); $dbw->begin(); - /* Do queries */ + # Do queries $res = $dbw->insert( - 'moodleUsers', - array('name'=>'rohan'), + 'moodleusers', + array('moodle_id'=>'****', + 'mediawiki_id' => $wgUser->getID() + ), __METHOD__, array() ); $dbw->commit(); - //function insert( $table, $a, $fname = 'Database::insert', $options = array() ); + #function insert( $table, $a, $fname = 'Database::insert', $options = array() ); } - + function execute( $par ) { - //execute permission implementation (will give restriction error on direct url access) + #execute permission implementation (will give restriction error on direct url access) if ( !$this->userCanExecute( $this->getUser() ) ) { $this->displayRestrictionError(); return; } - + global $wgMoodleWSInstance; global $wgMoodleWSToken; + global $wgMoodleWSServiceShortname; global $wgUser; - + $request = $this->getRequest(); $output = $this->getOutput(); $this->setHeaders(); - + # Handle Cancel if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) { $titleObj = Title::newMainPage(); $query = $request->getVal( 'returntoquery' ); $this->getOutput()->redirect( $titleObj->getFullURL( $query ) ); - //$wikitexttest = '\'\'\'Cancel\'\'\''; - //$output->addWikiText( $wikitexttest ); return; } - + + ## WIP # To avoid potential database delay the form should post the moodle user data to be used initially # this should also allow a one time login check to validate the information before associating the user # and the moodle user in the database if ( $request->wasPosted() ) { - # perform moodle login check - - # new moodle user - - # query display grades as normal with user info - } else { - - # Get request data from, e.g. - // $param = $request->getText( 'param' ); - - // WIP - // Get relevant information for getting grades via Moodle Web Service - /*$dbr = wfGetDB( DB_SLAVE ); - $res = $dbr->select( - 'moodleUsers', // $table - array( 'mwname', 'moname' ), // $vars (columns of the table) - array( 'mwname'=>$wgUser->getName() ), // $conds - __METHOD__, // $fname = 'Database::select', - array() // $options = array() - );*/ - /* Test Output - $output = ''; - foreach( $res as $row ) { - $output .= 'Category ' . $row->cat_title . ' contains ' . $row->cat_pages . " entries.\n"; - } - */ - - # Do stuff - # ... - - // If Found user (WIP) - - // Test call to outside using info set in localsettings.php - // Will later use name from table to get grades - $url = $wgMoodleWSInstance . "webservice/rest/server.php?wstoken=" . $wgMoodleWSToken . "&wsfunction=core_webservice_get_site_info"; - //$url = "http://sandbox.wikiversity.wmflabs.org/moodle/webservice/rest/server.php?wstoken=a133c830a3bbc54a41446740797852eb&wsfunction=core_webservice_get_site_info"; - $req = MWHttpRequest::factory ($url); - # $options=array("postData"=>array()) - $status = $req->execute(); - $content = $req->getContent(); - - $wikitext = 'Hello world!' . $content . $url; + # perform moodle login check (needs to get username and password from post) + $url = "http://" . $wgMoodleWSInstance . "login/token.php?username=*****&password=******&service=" . $wgMoodleWSServiceShortname; + + $content = Http::request( 'POST', $url, $params ); + $obj2 = json_decode($content, true); + if (is_null($obj2['error'])) { + $wikitext = 'Hello world!' . $content . $url; + $this->newMoodleUser(); + } else { + $wikitext = 'uh oh' . $obj2['error'] . $url; + } $output->addWikiText( $wikitext ); - // Else allow user to link their moodle account - $this->showRegisterForm(); - } - } + # new moodle user + + # query display grades as normal with user info + } else { + + # WIP + # Get relevant information for getting grades via Moodle Web Service + $dbr = wfGetDB( DB_SLAVE ); + $res = $dbr->select( + 'moodleusers', # $table + array( 'moodle_id', 'mediawiki_id' ), # $vars (columns of the table) + 'mediawiki_id = ' . $wgUser->getID(), # $conds + __METHOD__, # $fname = 'Database::select', + array() # $options = array() + ); + # Test Output + $found = false; + foreach( $res as $row ) { + if (!is_null($row->moodle_id)) { + $output->addWikiText( "current id: " . $wgUser->getID() . " from database: " . $row->mediawiki_id . " moodle: " . $row->moodle_id ); + $found = true; + } + } + if ($found) { + + # If Found user (WIP) + # This currently needs two modifications to a core installation of moodle + # the first is to get a userid from a username + # the second is to be able to get grades from a course + # I am currently working on my own implementation of these that can be added + # to webservices it will be documented soon + /*$functionname = 'core_user_get_users_by_id';#'core_course_get_courses'; - // Needs Messages - // Uses structure taken from: SpecialChangePassword.php + + # Test call to outside using info set in localsettings.php + # Will later use name from table to get grades + $getparam = "&userids[]=2"; + $url = "http://" . $wgMoodleWSInstance . "webservice/rest/server.php?wstoken=" . $wgMoodleWSToken . '&wsfunction='.$functionname . $getparam; + $req = MWHttpRequest::factory ($url, $options=null); + $status = $req->execute(); + $content = $req->getContent(); + $wikitext = $wgUser->getID() . 'Hello world!' . $content . $url; + $output->addWikiText( $wikitext ); + */ + } else { + # Else allow user to link their moodle account + $this->showRegisterForm(); + } + } + + } + + # WIP + # Needs Messages + # Uses structure taken from: SpecialChangePassword.php function showRegisterForm() { global $wgUser; - + $prettyFields = array( array( 'wpName', 'username', 'text', $wgUser->getName() ), array( 'Moodle Name', 'moodleusername', 'input', null ), array( 'Moodle Password', 'moodlepassword', 'password', null ), ); - + $this->getOutput()->addHTML( Xml::fieldset( $this->msg( 'resetpass_header' )->text() ) . Xml::openElement( 'form', @@ -125,11 +147,11 @@ 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'id' => 'mw-resetpass-form' ) ) . "\n" . - //$hiddenFieldsStr . - //$this->msg( 'resetpass_text' )->parseAsBlock() . "\n" . + #$hiddenFieldsStr . + #$this->msg( 'resetpass_text' )->parseAsBlock() . "\n" . Xml::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" . $this->pretty( $prettyFields ) . "\n" . - //$rememberMe . + #$rememberMe . "<tr>\n" . "<td></td>\n" . '<td class="mw-input">' . @@ -142,7 +164,7 @@ Xml::closeElement( 'fieldset' ) . "\n" ); } - + /** * pretty function used in SpecialChangePassword, adapted to autofocus Moodle Name * @param $fields array diff --git a/moodleusers.sql b/moodleusers.sql new file mode 100644 index 0000000..0260b55 --- /dev/null +++ b/moodleusers.sql @@ -0,0 +1,8 @@ +CREATE TABLE /*$wgDBprefix*/moodleusers ( + -- ID of moodle user associated with mediawiki account + moodle_id int(10) unsigned NOT NULL, + -- ID of mediawiki user associated with moodle account + mediawiki_id varchar(255) binary NOT NULL, + + PRIMARY KEY mediawiki_id (mediawiki_id) +) /*$wgDBTableOptions*/; -- To view, visit https://gerrit.wikimedia.org/r/83628 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I038268d84c042ef98c8ac5b8ac95bf7f37646279 Gerrit-PatchSet: 2 Gerrit-Project: mediawiki/extensions/Moodle Gerrit-Branch: master Gerrit-Owner: Clancer <rohan.j.ve...@gmail.com> Gerrit-Reviewer: Brian Wolff <bawolff...@gmail.com> Gerrit-Reviewer: Siebrand <siebr...@wikimedia.org> Gerrit-Reviewer: jan <j...@jans-seite.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits