Skip to content

Update mvp #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ jobs:
run: ~/.local/bin/gitlint --commits origin/main..HEAD

- name: Setup PHP ${{ matrix.php }}
if: ${{ !cancelled() }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
ini-values: max_input_vars=5000
coverage: xdebug

- name: Initialise moodle-plugin-ci
if: ${{ !cancelled() }}
run: |
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^4
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
Expand All @@ -62,6 +64,7 @@ jobs:
echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV

- name: Install moodle-plugin-ci
if: ${{ !cancelled() }}
run: moodle-plugin-ci install --plugin ./plugin --db-host=127.0.0.1
env:
DB: ${{ matrix.database }}
Expand Down
3 changes: 3 additions & 0 deletions amd/build/iagora.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions amd/build/iagora.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions amd/src/iagora.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import WebChat from 'botframework-webchat';
/**
* Initializes the chat with given parameters.
* @param {Object} params - The initialization parameters.
* @param {string} params.copiloturl - The copilot.
* @param {string} params.categoryid - The category ID.
* @param {string} params.chatId - The chat container ID.
*/

export const init = ({copiloturl}) => {
// eslint-disable-next-line no-console
console.log('Copilot URL:', copiloturl);
//initIagoraChat(chatId);
};
/**
*
* @param {string} chatId
*/
export function initIagoraChat(chatId) {
const chatContainer = document.getElementById(chatId);

// Static token and directLine URL (example values)
const directLineURL = 'https://directline.botframework.com';
const token = chatId ;
try {
const directLine = WebChat.createDirectLine({domain: new URL("v3/directline", directLineURL), token});

const subscription = directLine.connectionStatus$.subscribe({
next(value) {
if (value === 2) {
directLine
.postActivity({
localTimezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
locale: document.documentElement.lang || "en",
name: "startConversation",
type: "event"
})
.subscribe();

subscription.unsubscribe();
}
}
});

WebChat.renderWebChat({directLine, locale: document.documentElement.lang || "en"}, chatContainer);
} catch (error) {
// eslint-disable-next-line no-console
console.error("Error initializing chat:", error);
chatContainer.innerHTML = "Failed to load chat. Please try again later.";
}
}

export default {
init,
initIagoraChat
};
65 changes: 60 additions & 5 deletions block_iagora.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
*/

/**
* The iagora block class.
* Store configuration data for the iagora block.
*/
class block_iagora extends block_base {

/**
* Initialises the block.
* Initialize the block.
*
* @return void
*/
Expand All @@ -37,7 +37,7 @@ public function init() {
}

/**
* Gets the block contents.
* Get the block content.
*
* @return string The block HTML.
*/
Expand All @@ -48,17 +48,72 @@ public function get_content() {

$this->content = new stdClass();
$this->content->footer = '';
$this->content->text = '';
$iframeurl = isset($this->config->iframeurl) ? $this->config->iframeurl : '';

// if (empty($iframeurl)) {
// $this->content->text = get_string('noiframeurl', 'block_iagora');
// } else {
// $this->content->text = '<iframe src="' . $iframeurl . '" width="100%" height="400px" frameborder="0"></iframe>';
// }
$this->content->text = $this->generate_chat_content($iframeurl);

return $this->content;
}

/**
* Defines in which pages this block can be added.
* Define in which pages this block can be added.
*
* @param string $iframeurl The URL to be used in the iframe.
* @return string The generated HTML content.
*/
private function generate_chat_content($iframeurl) {
global $PAGE, $OUTPUT; die($iframeurl);

// Load necessary styles and scripts
$PAGE->requires->js(new moodle_url('https://cdn.botframework.com/botframework-webchat/latest/webchat.js'), true);
$PAGE->requires->js_call_amd('block_iagora/iagora', 'init', [[
'copiloturl' => $iframeurl
]]);

// Generate a unique identifier for the chat container
$chat_id = uniqid('iagora_chat_');

// Chat button icon (using Moodle icons)
$chat_icon = $OUTPUT->pix_icon('t/message', get_string('openchat', 'block_iagora'));

$output = html_writer::div($chat_icon, 'iagora-chat-toggle', ['id' => 'iagora-toggle-' . $chat_id,'data-url' => 'iframe_url']);
$output .= html_writer::div('', 'iagora-chat-container', ['id' => $chat_id]);

// Add initialization script
// $output .= html_writer::script("
// document.addEventListener('DOMContentLoaded', function() {
// initIagoraChat('{$chat_id}', " . json_encode($this->config) . ");
// });
// ");

return $output;
}

/**
* Define in which pages this block can be added.
*
* @return array of the pages where the block can be added.
*/
public function applicable_formats() {
return ['all' => true];
}

/**
* Save instance configuration.
*
* @param stdClass $data The configuration data.
* @param bool $nolongerused Indicates if $data is no longer used.
* @return bool True if data was saved successfully, false otherwise.
*/
public function instance_config_save($data, $nolongerused = false) {
if (isset($data->iframeurl)) {
$data->iframeurl = clean_param($data->iframeurl, PARAM_URL);
}
return parent::instance_config_save($data, $nolongerused);
}
}
59 changes: 59 additions & 0 deletions edit_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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/>.

/**
* Block edit form class for the block_iagora plugin.
*
* @package block_iagora
* @copyright 2024, Datum Academy <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Class block_iagora_edit_form extending block_edit_form.
*
* This form class allows configuration of specific settings for the iagora block,
* including the iframe URL.
*/
class block_iagora_edit_form extends block_edit_form {

/**
* Specific definition of the form elements.
*
* @param MoodleQuickForm $mform The Moodle form to be configured.
*/
protected function specific_definition($mform) {
// Section header title according to language file.
$mform->addElement('header', 'config_header', get_string('blocksettings', 'block'));

// Iframe URL.
$mform->addElement('text', 'config_iframeurl', get_string('iframeurl', 'block_iagora'));
$mform->setType('config_iframeurl', PARAM_URL);
$mform->addHelpButton('config_iframeurl', 'iframeurl', 'block_iagora');
$mform->setDefault('config_iframeurl', '');
$mform->addElement('static', 'iframeurl_desc', '', get_string('iframeurl_desc', 'block_iagora'));

// Background color
$mform->addElement('text', 'config_backgroundcolor', get_string('backgroundcolor', 'block_iagora'));
$mform->setDefault('config_backgroundcolor', '#A9D1F3');
$mform->setType('config_backgroundcolor', PARAM_TEXT);

// Bubble background color
$mform->addElement('text', 'config_bubblebackground', get_string('bubblebackground', 'block_iagora'));
$mform->setDefault('config_bubblebackground', '#F3CBA9');
$mform->setType('config_bubblebackground', PARAM_TEXT);
}

}
10 changes: 9 additions & 1 deletion lang/en/block_iagora.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['pluginname'] = 'IAGORA';
$string['backgroundcolor'] = 'Background Color';
$string['bubblebackground'] = 'Bubble Background Color';
$string['pluginname'] = 'IAGORA';
$string['iframeurl'] = 'Iframe URL';
$string['iframeurl_desc'] = 'Public URL available for chat iframe';
$string['iframeurl_help'] = 'Public URL for the Microsoft Copilot chat iframe';
$string['noiframeurl'] = 'No url defined for this block. Please configure a URL in the block parameters';


16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "iagora",
"version": "1.0.0",
"description": "iagora Chat block for Moodle",
"author": "Datum Academy <[email protected]>",
"license": "GPL-3.0",
"private": true,
"dependencies": {
"botframework-webchat": "^4.15.0"
},
"scripts": {
"build": "grunt amd",
"watch": "grunt watch",
"lint": "grunt eslint stylelint"
}
}