-
Notifications
You must be signed in to change notification settings - Fork 1
/
block_chatbot.php
executable file
·137 lines (122 loc) · 4.44 KB
/
block_chatbot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/lib.php');
use core_h5p\local\library\autoloader;
class block_chatbot extends block_base {
public function init() {
$this->blockname = get_class($this);
$this->title = get_string('chatbot', 'block_chatbot');
}
function instance_create() {
// the chatbot was inactive before: we need to sync the user course module activity
sync_all_course_module_histories();
return true;
}
function has_config() {return true;} // required to enable global settings
public function get_content() {
global $CFG, $PAGE, $USER, $DB, $COURSE;
// check if plugin is enabled for current course -> if not, return nothing (performance)
$enabled = true;
try {
if(!in_array(strval($COURSE->id), explode(",", $DB->get_field('config_plugins', 'value', array('plugin' => 'block_chatbot', 'name' => 'courseids'))))) {
// check if the chatbot is enabled for the current course.
// if not, don't render it
$enabled = false;
}
} catch(exception $e) {
$enabled = false;
}
if(!$enabled) {
// chatbot is not enabled for this course or user -> don't render
$this->content = new stdClass;
$this->content->footer = '';
$this->content->text = '';
return $this->content;
}
if ($this->content !== null) {
// cache
return $this->content;
}
// check if plugin is enabled for current user -> if not, return only settings button (performance)
try {
if($DB->record_exists('chatbot_usersettings', array('userid' => $USER->id))) {
// check if the user has enabled chatbot.
// if not, don't render it
$usersettings = $DB->get_record('chatbot_usersettings', array('userid' => $USER->id));
if($usersettings->enabled == 0) {
$enabled = false;
}
}
} catch(exception $e) {
$enabled = false;
}
// try to get token for booksearch webservice
try {
// get id of booksearch service
$booksearch_service_id = $DB->get_field('external_services_functions', 'externalserviceid',
array('functionname' => 'block_booksearch_get_searched_locations'),
);
// get token
$booksearch_token = $DB->get_field('external_tokens', 'token',
array('externalserviceid' => $booksearch_service_id),
);
} catch(exception $e) {
$booksearch_token = "";
}
if(!property_exists($this, 'h5presizerurl')) {
try {
$this->h5presizerurl = core_h5p\local\library\autoloader::get_h5p_core_library_url('js/h5p-resizer.js');
} catch (exception $e) {
$this->h5presizerurl = "";
}
}
// Check if user settings exist. If not, create them.
if(!$DB->record_exists('chatbot_usersettings', array('userid'=>$USER->id))) {
$firstturn = true;
$book_id = $DB->get_record('modules', array('name'=>'book'))->id;
$DB->insert_record('chatbot_usersettings', array(
'userid' => $USER->id,
'enabled' => true,
'logging' => false,
'firstturn' => true,
'preferedcontenttype' => $book_id,
'numsearchresults' => 5,
'numreviewquizzes' => 3,
'openonlogin' => true,
'openonquiz' => true,
'openonsection' => false,
'openonbranch' => false,
'openonbadge' => true
));
} else {
$firstturn = $DB->get_field("chatbot_usersettings", "firstturn", array('userid' => $USER->id));
}
// Init javascript
$data = array(
"enabled" => $enabled,
"firstturn" => $firstturn,
"server_name" => block_chatbot_get_server_name(),
"server_port" => block_chatbot_get_server_port(),
"wwwroot" => $CFG->wwwroot,
// block_chatbot_get_chat_container(),
"userid" => $USER->id,
'username' => $USER->username,
"courseid" => $COURSE->id,
"booksearchtoken" => $booksearch_token,
"wsuserid" => $DB->get_field("user", "id", array(
"username" => "kib3_webservice",
"firstname" => "KIB3 Webservice",
"lastname" => "KIB3 Webservice"
)),
"timestamp" => (new DateTime("now", core_date::get_server_timezone_object()))->getTimestamp(),
'resizeurl' => $this->h5presizerurl
);
// Renderer needed to use templates
$renderer = $PAGE->get_renderer($this->blockname);
$text = $renderer->render_from_template('block_chatbot/chatwindow', $data);
$this->content = new stdClass;
$this->content->footer = '';
$this->content->text = $text;
return $this->content;
}
}