-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.php
More file actions
217 lines (179 loc) · 8.85 KB
/
view.php
File metadata and controls
217 lines (179 loc) · 8.85 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?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/>.
/**
* Activitymap module view map
*
* @package mod_activitymap
* @copyright 2020 Guenther Hutter, Andreas Poetscher
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../../config.php');
use availability_completion\condition;
require_once($CFG->libdir . '/completionlib.php');
// Parameters
$id = optional_param('id', 0, PARAM_INT); // Course module ID
$plainrendering = optional_param('plain', 0, PARAM_INT); // Display borderless as image
$lightweightrendering = optional_param('lightweight', 0, PARAM_INT); // Display borderless as svg
$cm = get_coursemodule_from_id('activitymap', $id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
$courseid = $cm->course;
require_course_login($course, true, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/activitymap:view', $context);
$PAGE->set_url('/mod/activitymap/view.php', array('id' => $cm->id));
$module = $DB->get_record('activitymap', array('id'=>$cm->instance), '*', MUST_EXIST);
//------------------------------------------------------------------------------
// HELPER FUNCTIONS
//------------------------------------------------------------------------------
/**
Produces the HTML Tags which are loading the needed scripts
*/
function activitymap_html_loadscripts()
{
$ret = "";
$ret = $ret . "<script src='javascript/viz.js'></script>" . PHP_EOL;
$ret = $ret . "<script src='javascript/full.render.js'></script>" . PHP_EOL;
$ret = $ret . "<script src='javascript/svg-pan-zoom.js'></script>" . PHP_EOL;
return $ret;
}
/**
Searches all images from the course activities and returns an array of
images for it
@param courseid The course from where the images should be taken
@returns an array uf unique urls
*/
function activitymap_readImageUrlsFromCourse($courseid)
{
$modinfo = get_fast_modinfo($courseid);
$moduleimageurls = array();
foreach ($modinfo->cms as $otherid => $othercm) {
$cmIconUrl = new moodle_url('/mod/' . $othercm->modname . '/pix/monologo.png');
array_push($moduleimageurls, strval($cmIconUrl));
}
return array_unique($moduleimageurls, SORT_STRING);
}
//------------------------------------------------------------------------------
/**
@param id The id of the activity map (course module id)
@param dynamic What kind of output should be generated.
o false produces an static image element (png)
o true produces an dynamic svg element with pan & zoom
@param injectedImages Injects the given images into the graph where they
can be accessed from afterwards.
*/
function activitymap_html_generateMapScript($id, $injectedImages, $dynamic)
{
$ret = "";
$ret = $ret . "<div id='activitymap_$id'></div>" . PHP_EOL;
$ret = $ret . "<script>" . PHP_EOL;
$ret = $ret . " if (window.XMLHttpRequest)" . PHP_EOL;
$ret = $ret . " {// code for IE7+, Firefox, Chrome, Opera, Safari" . PHP_EOL;
$ret = $ret . " xmlhttp=new XMLHttpRequest();" . PHP_EOL;
$ret = $ret . " }" . PHP_EOL;
$ret = $ret . " else" . PHP_EOL;
$ret = $ret . " {// code for IE6, IE5" . PHP_EOL;
$ret = $ret . " xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');" . PHP_EOL;
$ret = $ret . " }" . PHP_EOL;
$ret = $ret . " xmlhttp.onreadystatechange=function()" . PHP_EOL;
$ret = $ret . " {" . PHP_EOL;
$ret = $ret . " if (xmlhttp.readyState==4 && xmlhttp.status==200)" . PHP_EOL;
$ret = $ret . " {" . PHP_EOL;
$ret = $ret . " // We have received something" . PHP_EOL;
$ret = $ret . " //alert(xmlhttp.responseText); " . PHP_EOL;
$ret = $ret . " var viz = new Viz();" . PHP_EOL;
// Image or SVG ?
if($dynamic == true)
{
$ret = $ret . " viz.renderSVGElement(xmlhttp.responseText" . PHP_EOL;
}
else
{
$ret = $ret . " viz.renderImageElement(xmlhttp.responseText" . PHP_EOL;
}
$ret = $ret . ",{ images: [" . PHP_EOL;
// Inject the images from the course into Emscripten's in-memory filesystem
foreach($injectedImages as $img)
{
$ret = $ret . "{ path: '$img', width: '30px', height: '30px' }," . PHP_EOL;
}
// and build the remeaning java script
$ret = $ret . "]})" . PHP_EOL;
$ret = $ret . " .then(function(element) {" . PHP_EOL;
$ret = $ret . " document.getElementById('activitymap_$id').appendChild(element);" . PHP_EOL;
$ret = $ret . " // Pan and zoom via 3rd party library " . PHP_EOL;
$ret = $ret . " panZoom = svgPanZoom(element, {" . PHP_EOL;
$ret = $ret . " zoomEnabled: true," . PHP_EOL;
$ret = $ret . " controlIconsEnabled: true," . PHP_EOL;
$ret = $ret . " fit: true," . PHP_EOL;
$ret = $ret . " center: true," . PHP_EOL;
$ret = $ret . " minZoom: 0.1" . PHP_EOL;
$ret = $ret . " });" . PHP_EOL;
$ret = $ret . " })" . PHP_EOL;
$ret = $ret . " .catch(error => {" . PHP_EOL;
$ret = $ret . " // Create a new Viz instance (@see Caveats page for more info)" . PHP_EOL;
$ret = $ret . " viz = new Viz();" . PHP_EOL;
$ret = $ret . " // Possibly display the error" . PHP_EOL;
$ret = $ret . " console.error(error);" . PHP_EOL;
$ret = $ret . " });" . PHP_EOL;
$ret = $ret . " }" . PHP_EOL;
$ret = $ret . " }" . PHP_EOL;
$ret = $ret . "xmlhttp.open('GET', 'gvizdot.php?id={$id}', false);" . PHP_EOL;
$ret = $ret . "xmlhttp.send(); " . PHP_EOL;
$ret = $ret . "</script>" . PHP_EOL;
return $ret;
}
//------------------------------------------------------------------------------
// ACTUAL PAGE OUTPUT STARTS HERE
//------------------------------------------------------------------------------
// Start the document
if($plainrendering == false && $lightweightrendering == false)
{
print($OUTPUT->header());
}
else
{
print("<!DOCTYPE html>" . PHP_EOL);
print("<html>" . PHP_EOL);
print(" <head>" . PHP_EOL);
// If embedded then open link in parent frame
print(" <base target='_parent' />" . PHP_EOL);
print(" </head>" . PHP_EOL);
// Start body
print(" <body>" . PHP_EOL);
}
// Include the needed javascripts
print(activitymap_html_loadscripts());
// Load all image urls for this course
$moduleimageurls = activitymap_readImageUrlsFromCourse($courseid);
// Quick and dirty Ajax get (since i dont know how AMD works)
// NOTE: THIS IS ABSOLUTELY UGLY AND HAS TO BE FIXED IN THE FURURE !!!
if($plainrendering == true)
{
print(activitymap_html_generateMapScript($id, $moduleimageurls, false));
}
else
{
print(activitymap_html_generateMapScript($id, $moduleimageurls, true));
}
// Finish the document
if($plainrendering == false && $lightweightrendering == false)
{
print($OUTPUT->footer());
}
else
{
print("</html>" . PHP_EOL);
}