Skip to content

Commit

Permalink
Merge pull request #15 from saylordotorg/feat/new-discourse-api-2020
Browse files Browse the repository at this point in the history
Feat/new discourse api 2020
  • Loading branch information
jazinheira authored Apr 28, 2020
2 parents 5fdb8c2 + 54458e4 commit 3a58b38
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 88 deletions.
9 changes: 4 additions & 5 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@ function get_discourse_locale($moodleuserlang) {
return $discourselocale;
}

function clean_name($string) {
$cleaned = str_replace ("'", "", strtolower($string));
$cleaned = str_replace (" ", "_", $cleaned);
$cleaned = preg_replace('/[^\p{L}\p{N}]/u', '_', $cleaned);
function clean_name($name) {
$cleaned = preg_replace('/[^\p{L}\p{N}]/u', '_', $name);
$cleaned = rtrim($cleaned, '_');

return $cleaned;
}
Expand Down Expand Up @@ -130,7 +129,7 @@ function discoursesso_add_group($cohortid) {
$r = $api->createGroup(clean_name($ssocohort->cohortname));

if ($r->http_code != 200) {
echo $OUTPUT->notification(get_string('errorcreategroupdiscourse', 'local_discoursesso', clean_name($ssocohort->cohortname)), \core\output\notification::NOTIFY_WARNING);
echo $OUTPUT->notification(get_string('errorcreategroupdiscourse', 'local_discoursesso', clean_name($ssocohort->cohortname))."<br>Response: [".$r->http_code."] ".$r->apiresult->errors[0], \core\output\notification::NOTIFY_WARNING);
return false;
}

Expand Down
9 changes: 3 additions & 6 deletions vendor/discourse-api-php/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

// create a topic
$r = $api->createTopic(
'This is the title of a brand new topic',
'This is the title of a brand new topic',
"This is the body text of a brand new topic. I really don't know what to say",
$catId,
"johndoe"
Expand All @@ -36,14 +36,11 @@
$r = $api->createPost(
'This is the body of a new post in an existing topic',
$topicId,
$catId,
'johndoe'
);

// change sitesetting
// use 'true' and 'false' between quotes

$r = $api->changeSiteSetting('invite_expiry_days', 29);
print_r($r);


print_r($r);
171 changes: 96 additions & 75 deletions vendor/discourse-api-php/lib/DiscourseAPI.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?php

/**
* Discourse API client for PHP
*
* This is the Discourse API client for PHP
* This is a very experimental API implementation.
*
* @category DiscourseAPI
* @package DiscourseAPI
* @author Original author DiscourseHosting <[email protected]>
* @copyright 2013, DiscourseHosting.com
* @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2
* @link https://github.com/discoursehosting/discourse-api-php
*/
* Discourse API client for PHP
*
* This is the Discourse API client for PHP
* This is a very experimental API implementation.
*
* @category DiscourseAPI
* @package DiscourseAPI
* @author Original author DiscourseHosting <[email protected]>
* @copyright 2013, DiscourseHosting.com
* @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2
* @link https://github.com/discoursehosting/discourse-api-php
*/

class DiscourseAPI
{
Expand All @@ -22,11 +22,11 @@ class DiscourseAPI
private $_httpAuthName = '';
private $_httpAuthPass = '';

function __construct($dcHostname, $apiKey = null, $protocol='http', $httpAuthName='', $httpAuthPass='')
function __construct($dcHostname, $apiKey = null, $protocol = 'http', $httpAuthName = '', $httpAuthPass = '')
{
$this->_dcHostname = $dcHostname;
$this->_apiKey = $apiKey;
$this->_protocol=$protocol;
$this->_protocol = $protocol;
$this->_httpAuthName = $httpAuthName;
$this->_httpAuthPass = $httpAuthPass;
}
Expand Down Expand Up @@ -74,18 +74,21 @@ private function _getRequest($reqString, $paramArray = null, $apiUser = 'system'
if ($paramArray == null) {
$paramArray = array();
}
$paramArray['api_key'] = $this->_apiKey;
$paramArray['api_username'] = $apiUser;
$ch = curl_init();
$url = sprintf(
'%s://%s%s?%s',
$this->_protocol,
$this->_dcHostname,
$reqString,
$this->_protocol,
$this->_dcHostname,
$reqString,
http_build_query($paramArray)
);

if (!empty($this->_httpAuthName) && !empty($this->_httpAuthPass)) {

curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Api-Key: " . $this->_apiKey,
"Api-Username: $apiUser"
]);

if (!empty($this->_httpAuthName) && !empty($this->_httpAuthPass)) {
curl_setopt($ch, CURLOPT_USERPWD, $this->_httpAuthName . ":" . $this->_httpAuthPass);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
Expand All @@ -109,28 +112,34 @@ private function _putRequest($reqString, $paramArray, $apiUser = 'system')

private function _postRequest($reqString, $paramArray, $apiUser = 'system')
{

return $this->_putpostRequest($reqString, $paramArray, $apiUser, false);
}

private function _putpostRequest($reqString, $paramArray, $apiUser = 'system', $putMethod = false)
{

$ch = curl_init();
$url = sprintf(
'%s://%s%s?api_key=%s&api_username=%s',
$this->_protocol,
$this->_dcHostname,
$reqString,
$this->_apiKey,
$apiUser
'%s://%s%s',
$this->_protocol,
$this->_dcHostname,
$reqString
);

curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Api-Key: " . $this->_apiKey,
"Api-Username: $apiUser"
]);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($paramArray));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($putMethod) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
}
if (!empty($this->_httpAuthName) && !empty($this->_httpAuthPass)) {

if (!empty($this->_httpAuthName) && !empty($this->_httpAuthPass)) {
curl_setopt($ch, CURLOPT_USERPWD, $this->_httpAuthName . ":" . $this->_httpAuthPass);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
Expand Down Expand Up @@ -161,8 +170,8 @@ function group($groupname, $usernames = array())
return false;
}

foreach($obj->apiresult as $group) {
if($group->name === $groupname) {
foreach ($obj->apiresult as $group) {
if ($group->name === $groupname) {
$groupId = $group->id;
break;
}
Expand All @@ -176,7 +185,7 @@ function group($groupname, $usernames = array())
)
);

if($groupId) {
if ($groupId) {
return $this->_putRequest('/admin/groups/' . $groupId, $params);
} else {
return $this->_postRequest('/admin/groups', $params);
Expand Down Expand Up @@ -291,19 +300,19 @@ function activateUser($userId)
return $this->_putRequest("/admin/users/{$userId}/activate", array());
}

/**
* suspendUser
*
* @param integer $userId id of user to suspend
*
* @return mixed HTTP return code
*/
/**
* suspendUser
*
* @param integer $userId id of user to suspend
*
* @return mixed HTTP return code
*/

function suspendUser($userId)
{
return $this->_putRequest("/admin/users/{$userId}/suspend", array());
}

/**
* getUsernameByEmail
*
Expand All @@ -314,19 +323,20 @@ function suspendUser($userId)

function getUsernameByEmail($email)
{
$users = $this->_getRequest('/admin/users/list/active.json',
[ 'filter' => $email, 'show_emails' => 'true' ]
$users = $this->_getRequest(
'/admin/users/list/active.json',
['filter' => $email, 'show_emails' => 'true']
);
foreach($users->apiresult as $user) {
if($user->email === $email) {
foreach ($users->apiresult as $user) {
if ($user->email === $email) {
return $user->username;
}
}

return false;
}

/**
/**
* getUserByUsername
*
* @param string $userName username of user
Expand All @@ -338,18 +348,18 @@ function getUserByUsername($userName)
{
return $this->_getRequest("/users/{$userName}.json");
}

/**
* getUserByExternalID
*
* @param string $externalID external id of sso user
*
* @return mixed HTTP return code and API return object
*/
function getUserByExternalID($externalID)
{
return $this->_getRequest("/users/by-external/{$externalID}.json");
}
* getUserByExternalID
*
* @param string $externalID external id of sso user
*
* @return mixed HTTP return code and API return object
*/
function getUserByExternalID($externalID)
{
return $this->_getRequest("/users/by-external/{$externalID}.json");
}

/**
* createCategory
Expand All @@ -372,6 +382,18 @@ function createCategory($categoryName, $color, $textColor = '000000', $userName
return $this->_postRequest('/categories', $params, $userName);
}

/**
* getTopic
*
* @param string $topicId id of topic
*
* @return mixed HTTP return code and API return object
*/
function getTopic($topicID)
{
return $this->_getRequest('/t/' . $topicID . '.json');
}

/**
* createTopic
*
Expand All @@ -383,8 +405,7 @@ function createCategory($categoryName, $color, $textColor = '000000', $userName
*
* @return mixed HTTP return code and API return object
*/

function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId = 0)
function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId = 0)
{
$params = array(
'title' => $topicTitle,
Expand All @@ -395,7 +416,7 @@ function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId
);
return $this->_postRequest('/posts', $params, $userName);
}

/**
* watchTopic
*
Expand All @@ -404,26 +425,27 @@ function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId
* If no username is given, topic will be watched with
* the system API username
*/
function watchTopic($topicId, $userName = 'system')
{
function watchTopic($topicId, $userName = 'system')
{
$params = array(
'notification_level' => '3'
'notification_level' => '3'
);
return $this->_postRequest("/t/{$topicId}/notifications.json", $params, $userName);
}
}

/**
* createPost
*
* NOT WORKING YET
* @param string $bodyText body text of topic post
* @param string $topicId topic id - must me a string not array
* @param string $userName user to create topic as
*
* @return mixed HTTP return code and API return object
*/

function createPost($bodyText, $topicId, $categoryId, $userName)
function createPost($bodyText, $topicId, $userName)
{
$params = array(
'raw' => $bodyText,
'archetype' => 'regular',
'category' => $categoryId,
'topic_id' => $topicId
);
return $this->_postRequest('/posts', $params, $userName);
Expand All @@ -435,15 +457,15 @@ function inviteUser($email, $topicId, $userName = 'system')
'email' => $email,
'topic_id' => $topicId
);
return $this->_postRequest('/t/'.intval($topicId).'/invite.json', $params, $userName);
return $this->_postRequest('/t/' . intval($topicId) . '/invite.json', $params, $userName);
}

function changeSiteSetting($siteSetting, $value)
{
$params = array($siteSetting => $value);
return $this->_putRequest('/admin/site_settings/' . $siteSetting, $params);
}

function getIDByEmail($email)
{
$username = $this->getUsernameByEmail($email);
Expand All @@ -460,10 +482,9 @@ function logoutByEmail($email)
$params = array('username_or_email' => $email);
return $this->_postRequest('/admin/users/' . $user_id . '/log_out', $params);
}
function getUserinfoByName($username)

function getUserinfoByName($username)
{
return $this->_getRequest("/users/{$username}.json");
}
}

}
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@

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

$plugin->version = 2020042200;
$plugin->version = 2020042400;
$plugin->requires = 2016052306; // Requires this Moodle version - at least 3.1
$plugin->cron = 0;
$plugin->component = 'local_discoursesso';

$plugin->release = '1.3.1';
$plugin->release = '1.4.0';
$plugin->maturity = MATURITY_STABLE;

0 comments on commit 3a58b38

Please sign in to comment.