Skip to content

Commit

Permalink
Merge pull request #12 from saylordotorg/feat/sync-cohorts
Browse files Browse the repository at this point in the history
Feat/sync cohorts
  • Loading branch information
jazinheira authored May 23, 2019
2 parents ef4cbc8 + 91aa8c1 commit c94a19f
Show file tree
Hide file tree
Showing 13 changed files with 1,553 additions and 9 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,10 @@ Optional:
#### Moodle Setup

1. Navigate to the DiscourseSSO settings page from the administration block.
2. Enter the previously generated secret into the "Shared SSO Secret Key" field. Make sure it matches the secret entered into Discourse!
3. Enter your Discourse site URL into the "Discourse URL field"
2. Enter the Discourse API key into the API key field. This can be found in your Discourse server's settings under Admin->API.
3. Enter the previously generated secret into the "Shared SSO Secret Key" field. Make sure it matches the secret entered into Discourse!
4. Enter your Discourse site URL into the "Discourse URL field"

##### Cohorts

Select Moodle cohorts can now be synced as Discourse groups. On the "Assign cohorts" page under the DiscourseSSO settings, search through your Moodle cohorts and add any that you would like to sync. This will create a new group in Discourse. Any users who are a member of that cohort will be added to the Discourse group when they log in.
36 changes: 36 additions & 0 deletions db/events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* local_discoursesso
*
*
* @package local
* @subpackage discoursesso
* @copyright 2019 Saylor Academy
* @author John Azinheira
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$observers = array(
// Listen for when a user profile is updated.
array(
'eventname' => '\core\event\cohort_deleted',
'includefile' => '/local/discoursesso/locallib.php',
'callback' => 'local_discoursesso_cohort_deleted_handler',
'internal' => false
),
);
18 changes: 18 additions & 0 deletions db/install.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="local/discoursesso/db" VERSION="20141217" COMMENT="XMLDB file for Moodle local discoursesso"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="discoursesso_cohorts" COMMENT="Defines cohorts to sync">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="cohortid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="cohortname" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for cohort to sync"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
60 changes: 60 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* local_discoursesso
*
*
* @package local
* @subpackage discoursesso
* @copyright 2017 Saylor Academy
* @author John Azinheira
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

/**
* upgrade this assignment instance - this function could be skipped but it will be needed later
* @param int $oldversion The old version of the assign module
* @return bool
*/
function xmldb_local_discoursesso_upgrade($oldversion) {
global $CFG, $DB;

$dbman = $DB->get_manager();

if ($oldversion < 2019052200) {

// Define table discoursesso_cohorts to be created.
$table = new xmldb_table('discoursesso_cohorts');

// Adding fields to table discoursesso_cohorts.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('cohortid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('certificatename', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'timecreated');

// Adding keys to table discoursesso_cohorts.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);

// Conditionally launch create table for discoursesso_cohorts.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}

// Discoursesso savepoint reached.
upgrade_plugin_savepoint(true, 2019052200, 'local', 'discoursesso');
}

return true;
}
123 changes: 123 additions & 0 deletions groups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* local_discoursesso
*
*
* @package local
* @subpackage discoursesso
* @copyright 2017 Saylor Academy
* @author John Azinheira
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once('../../config.php');
require_once('./locallib.php');

$confirmadd = optional_param('confirmadd', 0, PARAM_INT);
$confirmdel = optional_param('confirmdel', 0, PARAM_INT);

require_login();
// Add proper permissions check.
if (!is_siteadmin()) {
die;
}

$PAGE->set_context(context_system::instance());
$PAGE->set_url('/local/discoursesso/groups.php');
$PAGE->set_title(format_string(get_string('plugingrouppagename', 'local_discoursesso')));
$PAGE->set_heading(format_string(get_string('plugingrouppagename', 'local_discoursesso')));
$PAGE->navbar->add(get_string('plugingrouppagename', 'local_discoursesso'));

$returnurl = new moodle_url('/admin/search.php');

if (optional_param('cancel', false, PARAM_BOOL)) {
redirect($returnurl);
}

echo $OUTPUT->header();
format_string(get_string('plugingrouppagename', 'local_discoursesso'));


// Get the user_selector we will need.
$potentialuserselector = new discoursesso_cohort_candidate_selector('addselect', array());
$existinguserselector = new discoursesso_cohort_existing_selector('removeselect', array());

// Process incoming user assignments to the cohort

if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
$userstoassign = $potentialuserselector->get_selected_users();
if (!empty($userstoassign)) {

foreach ($userstoassign as $adduser) {
if (discoursesso_add_group($adduser->id) === false) {
echo $OUTPUT->notification('The cohort was not added.', NOTIFY_ERROR);
}
}

$potentialuserselector->invalidate_selected_users();
$existinguserselector->invalidate_selected_users();
}
}

// Process removing user assignments to the cohort
if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
$userstoremove = $existinguserselector->get_selected_users();
if (!empty($userstoremove)) {
foreach ($userstoremove as $removeuser) {
if (discoursesso_remove_group($removeuser->id) === false) {
echo $OUTPUT->notification('The cohort was not removed.', NOTIFY_ERROR);
}
}
$potentialuserselector->invalidate_selected_users();
$existinguserselector->invalidate_selected_users();
}
}

// Print the form.
?>
<form id="assignform" method="post" action="<?php echo $PAGE->url ?>"><div>
<input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
<input type="hidden" name="returnurl" value="<?php echo $returnurl->out_as_local_url() ?>" />

<table summary="" class="generaltable generalbox boxaligncenter" cellspacing="0">
<tr>
<td id="existingcell">
<p><label for="removeselect"><?php print_string('currentcohorts', 'local_discoursesso'); ?></label></p>
<?php $existinguserselector->display() ?>
</td>
<td id="buttonscell">
<div id="addcontrols">
<input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.s(get_string('add')); ?>" title="<?php p(get_string('add')); ?>" /><br />
</div>

<div id="removecontrols">
<input name="remove" id="remove" type="submit" value="<?php echo s(get_string('remove')).'&nbsp;'.$OUTPUT->rarrow(); ?>" title="<?php p(get_string('remove')); ?>" />
</div>
</td>
<td id="potentialcell">
<p><label for="addselect"><?php print_string('potcohorts', 'local_discoursesso'); ?></label></p>
<?php $potentialuserselector->display() ?>
</td>
</tr>
<tr><td colspan="3" id='backcell'>
<input class="btn btn-primary" type="submit" name="cancel" value="<?php p(get_string('back')); ?>" />
</td></tr>
</table>
</div></form>

<?php

echo $OUTPUT->footer();
15 changes: 14 additions & 1 deletion lang/en/local_discoursesso.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,28 @@

$string['adminsynclabel'] = 'Sync administrators?';
$string['adminsynchelp'] = 'When enabled, Moodle site administrators will be an admin in Discourse.';
$string['apikeylabel'] = 'API Key';
$string['apikeyhelp'] = 'Your Discourse API key. This is different from the SSO shared secret key!';
$string['currentcohorts'] = 'Current cohorts';
$string['currentcohortsmatching'] = 'Current matching cohorts';
$string['discourseurllabel'] = 'Discourse URL';
$string['discourseurlhelp'] = 'The URL of your Discourse instance, i.e. \'https://meta.discourse.org\'.';
$string['errorcreaterecorddb'] = 'There was an error creating the record in the database.';
$string['errordeleterecorddb'] = 'There was an error deleting the record from the database.';
$string['errorcreategroupdiscourse'] = 'Something went wrong creating the group in Discourse. Does a group with the name (${a}) already exist in Discourse?';
$string['errornoapikey'] = 'No API key found. Please enter an API key on the DiscourseSSO settings page!';
$string['localelabel'] = 'Sync user locale?';
$string['localehelp'] = 'This setting will send a user\'s locale in Moodle to the Discourse server on log in. This will change the user\'s language in Discourse to match their language in Moodle.';
$string['modulename'] = 'Discourse SSO';
$string['modulename_help'] = 'A plugin to use Moodle as a SSO provider for Discourse.';
$string['modulenameplural'] = 'Discourse SSOs';
$string['pluginadministration'] = 'Discourse SSO Administration';
$string['pluginname'] = 'Discourse SSO';
$string['plugingrouppagename'] = 'Assign cohorts';
$string['potcohortsmatching'] = 'Potential matching cohorts';
$string['potcohorts'] = 'Potential cohorts';
$string['secretkeylabel'] = 'Shared SSO Secret Key';
$string['secretkeyhelp'] = 'Secret key that is shared with Discourse. Use the same key as in \'sso_secret\' of your Discourse settings.';
$string['settingstitle'] = 'Discourse SSO Settings';
$string['settingstitle'] = 'Settings';
$string['toomanycohortsmatchsearch'] = 'Too many cohorts ({$a->count}) match \'{$a->search}\'';
$string['toomanycohortstoshow'] = 'Too many cohorts ({$a}) to show';
Loading

0 comments on commit c94a19f

Please sign in to comment.