diff --git a/locallib.php b/locallib.php index 9b5de7e..2a37ff0 100644 --- a/locallib.php +++ b/locallib.php @@ -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; } @@ -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))."
Response: [".$r->http_code."] ".$r->apiresult->errors[0], \core\output\notification::NOTIFY_WARNING); return false; } diff --git a/vendor/discourse-api-php/example.php b/vendor/discourse-api-php/example.php index 3804525..92046c4 100644 --- a/vendor/discourse-api-php/example.php +++ b/vendor/discourse-api-php/example.php @@ -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" @@ -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); \ No newline at end of file diff --git a/vendor/discourse-api-php/lib/DiscourseAPI.php b/vendor/discourse-api-php/lib/DiscourseAPI.php index deef4d3..32858cd 100644 --- a/vendor/discourse-api-php/lib/DiscourseAPI.php +++ b/vendor/discourse-api-php/lib/DiscourseAPI.php @@ -1,18 +1,18 @@ - * @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 + * @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 { @@ -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; } @@ -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); } @@ -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); } @@ -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; } @@ -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); @@ -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 * @@ -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 @@ -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 @@ -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 * @@ -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, @@ -395,7 +416,7 @@ function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId ); return $this->_postRequest('/posts', $params, $userName); } - + /** * watchTopic * @@ -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); @@ -435,7 +457,7 @@ 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) @@ -443,7 +465,7 @@ function changeSiteSetting($siteSetting, $value) $params = array($siteSetting => $value); return $this->_putRequest('/admin/site_settings/' . $siteSetting, $params); } - + function getIDByEmail($email) { $username = $this->getUsernameByEmail($email); @@ -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"); } -} - +} \ No newline at end of file diff --git a/version.php b/version.php index 5d02784..4fc47c7 100644 --- a/version.php +++ b/version.php @@ -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; \ No newline at end of file