forked from moodlehu/moodle-mod_etherpadlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.php
137 lines (114 loc) · 4.78 KB
/
view.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
// 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/>.
/**
* This page prints a particular instance of etherpadlite
*
* @package mod_etherpadlite
*
* @author Timo Welde <[email protected]>
* @copyright 2012 Humboldt-Universität zu Berlin <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once(dirname(__FILE__).'/lib.php');
$id = optional_param('id', 0, PARAM_INT); // The course_module id, or
$a = optional_param('a', 0, PARAM_INT); // etherpadlite instance id.
if ($id) {
$cm = get_coursemodule_from_id('etherpadlite', $id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
$etherpadlite = $DB->get_record('etherpadlite', ['id' => $cm->instance], '*', MUST_EXIST);
} else if ($a) {
$etherpadlite = $DB->get_record('etherpadlite', ['id' => $a], '*', MUST_EXIST);
$course = $DB->get_record('course', ['id' => $etherpadlite->course], '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('etherpadlite', $etherpadlite->id, $course->id, false, MUST_EXIST);
} else {
error('You must specify a course_module ID or an instance ID');
}
// This must be here, so that require login doesn't throw a warning.
$PAGE->set_url('/mod/etherpadlite/view.php', ['id' => $cm->id]);
require_login($course, true, $cm);
$config = get_config('etherpadlite');
if ($config->ssl) {
// The https_required doesn't work, if $CFG->loginhttps doesn't work.
$CFG->httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
if (!isset($_SERVER['HTTPS'])) {
$url = $CFG->httpswwwroot.'/mod/etherpadlite/view.php?id='.$id;
redirect($url);
}
}
// START of Initialise the session for the Author.
// Set vars.
$domain = $config->url;
$padid = $etherpadlite->uri;
$fullurl = 'domain.tld';
// Make a new intance from the etherpadlite client.
$instance = new \mod_etherpadlite\client($config->apikey, $domain.'api');
// Fullurl generation.
if (isguestuser() && !etherpadlite_guestsallowed($etherpadlite)) {
try {
$readonlyid = $instance->get_readonly_id($padid);
$fullurl = $domain.'ro/'.$readonlyid;
} catch (Exception $e) {
throw $e;
}
} else {
$fullurl = $domain.'p/'.$padid;
}
// Get the groupID.
$groupid = explode('$', $padid);
$groupid = $groupid[0];
// Create author if not exists for logged in user (with full name as it is obtained from Moodle core library).
try {
if (isguestuser() && etherpadlite_guestsallowed($etherpadlite)) {
$authorid = $instance->create_author('Guest-'.etherpadlite_gen_random_string());
} else {
$authorid = $instance->create_author_if_not_exists_for($USER->id, fullname($USER));
}
} catch (Exception $e) {
// The pad already exists or something else went wrong.
throw $e;
}
$validuntil = time() + $config->cookietime;
try {
$sessionid = $instance->create_session($groupid, $authorid, $validuntil);
} catch (Exception $e) {
throw $e;
}
// If we reach the etherpadlite server over https, then the cookie should only be delivered over ssl.
$ssl = (stripos($config->url, 'https://') === 0) ? true : false;
setcookie('sessionID', $sessionid, $validuntil, '/', $config->cookiedomain, $ssl); // Set a cookie.
// END of Etherpad Lite init.
$context = context_module::instance($cm->id);
// Display the etherpadlite and possibly results.
$eventparams = [
'context' => $context,
'objectid' => $etherpadlite->id
];
$event = \mod_etherpadlite\event\course_module_viewed::create($eventparams);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('etherpadlite', $etherpadlite);
$event->trigger();
$PAGE->set_title(get_string('modulename', 'mod_etherpadlite').': '.format_string($etherpadlite->name));
$PAGE->set_heading(format_string($course->fullname));
$PAGE->set_context($context);
$renderer = $PAGE->get_renderer('mod_etherpadlite');
// Print the page header.
echo $renderer->header();
// Print the etherpad content.
echo $renderer->render_etherpad($etherpadlite, $cm, $fullurl);
// Close the page.
echo $renderer->footer();