/
lib.php
291 lines (234 loc) · 7.92 KB
/
lib.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?PHP
/**
* This code is copyright to EuroMotor AutoTrain LLP and is licenced under the GNU GPLv3 Licence
**/
/// Library of functions and constants for module AutoView
define("AUTOVIEW_MAX_NAME_LENGTH", 50);
define("AUTOVIEW_STORAGE_INTERNAL", 0);
require_once("avphplib/avlib.php");
global $CFG;
function autoview_add_instance($autoview) {
/// Given an object containing all the necessary data,
/// (defined by the form in mod.html) this function
/// will create a new instance and return the id number
/// of the new instance.
global $CFG, $DB;
$mainrecord = new stdClass;
$mainrecord->name = strip_tags($autoview->content);
if (strlen($mainrecord->name) > AUTOVIEW_MAX_NAME_LENGTH)
$mainrecord->name = substr($mainrecord->name, 0, AUTOVIEW_MAX_NAME_LENGTH)."...";
else
if (strlen($mainrecord->name) == 0)
$mainrecord->name = "AutoView ".time();
$mainrecord->timemodified = time();
$mainrecord->course=$autoview->course;
$mainrecord->content=$autoview->content;
$mainrecord->summary=$autoview->summary;
if (property_exists($autoview, 'noframe') && $autoview->noframe=="1")
$mainrecord->noframe=1;
else
$mainrecord->noframe=0;
//$mainrecord->storage=mdl21_getconfigparam("autoview", "storage_type");
$av_config=get_config("autoview");
$avs=autoview_get_file_storage($av_config->storage_type);
$avs->check_course_dir($mainrecord->course);
if (isset($autoview->configfile) && strlen($autoview->configfile)>0)
$mainrecord->configfile=$autoview->configfile;
else
{
$fname=clean_filename(str_replace(' ', '_', trim($mainrecord->name)));
if ($autoview->usedir)
{
$fileloc=$CFG->dataroot.'/'.$mainrecord->course.'/'.$fname;
if (!file_exists($fileloc))
mkdir($fileloc, $CFG->directorypermissions);
$mainrecord->configfile=$fname.'/config.avx';
}
else
$mainrecord->configfile=$fname.'.avx';
}
$instance_id=$DB->insert_record("autoview", $mainrecord);
$avs->check_configfile($mainrecord->configfile, $mainrecord->course);
return $instance_id;
}
function autoview_update_instance($autoview) {
/// Given an object containing all the necessary data,
/// (defined by the form in mod.html) this function
/// will update an existing instance with new data.
global $DB;
$mainrecord=new stdClass;
$mainrecord->name = strip_tags($autoview->content);
if (strlen($mainrecord->name) > AUTOVIEW_MAX_NAME_LENGTH) {
$mainrecord->name = substr($mainrecord->name, 0, AUTOVIEW_MAX_NAME_LENGTH)."...";
}
$mainrecord->id=$autoview->instance;
$mainrecord->timemodified = time();
$mainrecord->course=$autoview->course;
$mainrecord->content=$autoview->content;
$mainrecord->summary=$autoview->summary;
$mainrecord->configfile=$autoview->configfile;
if (property_exists($autoview, 'noframe') && $autoview->noframe=="1")
$mainrecord->noframe=1;
else
$mainrecord->noframe=0;
$ret=$DB->update_record("autoview", $mainrecord);
$avrec = $DB->get_record("autoview", array("id"=>$mainrecord->id));
$avs=autoview_get_file_storage($avrec->storage);
$avs->check_course_dir($mainrecord->course);
$avs->check_configfile($mainrecord->configfile, $mainrecord->course);
return $ret;
}
function autoview_delete_instance($id) {
/// Given an ID of an instance of this module,
/// this function will permanently delete the instance
/// and any data that depends on it.
global $DB;
if (! $autoview = $DB->get_record("autoview", array("id"=>$id))) {
return false;
}
$result = true;
if (!$DB->delete_records("autoview", array("id"=>$autoview->id))) {
$result = false;
}
return $result;
}
function autoview_get_participants($autoviewid) {
//Returns the users with data in one resource
//(NONE, but must exist on EVERY mod !!)
return false;
}
function autoview_get_coursemodule_info($coursemodule) {
/// Given a course_module object, this function returns any
/// "extra" information that may be needed when printing
/// this activity in a course listing.
///
/// See get_array_of_activities() in course/lib.php
return false;
}
function autoview_cron()
{
global $CFG, $DB;
$t=time();
$sql=" `time` < ".($t-($CFG->sessiontimeout));
$DB->delete_records_select("autoview_keys", $sql);
return true;
}
/***File storage handlers***/
function autoview_get_file_storage($type)
{
if ($type==AUTOVIEW_STORAGE_INTERNAL)
{
include_once("storage/internal.php");
return new av_internal_file_storage();
}
include_once("storage/generic.php");
return new av_generic_file_storage();
}
/***End of Moodle specific functions***/
/***Prepares the authorisation for AV Live callbacks***/
function autoview_prepare_auth($course, $USER, $autoview, $cm)
{
global $CFG, $DB;
$data="c:".$course->id.";x:".$cm->id;
$av_config=get_config("autoview");
if ($av_config->flashsecurity=="randomkey")
{
$skey=rand(1000000,9999999)."".rand(1000000,9999999)."".rand(1000000,9999999);
$data=$data.";k:".$skey;
$record=new stdclass;
$record->accesskey=$skey;
$record->ip=$_SERVER['REMOTE_ADDR'];
$record->userid=$USER->id;
$record->cmid=$cm->id;
$record->time = time();
$DB->insert_record("autoview_keys", $record);
}
return base64_encode($data);
}
function autoview_has_dependencies($rm=false)
{
global $DB;
$ok=true;
$m="";
$br=$DB->get_record("block", array("name"=>"repo_filemanager"));
if (!$br)
{
$m.="<div class=\"errorbox\" style=\"color:#ff0000\"><p style=\"warning\">".get_string("no_repofileman", "autoview")."</p></div>\n";
$ok=false;
}
$rr=$DB->get_record("repository", array("type"=>"coursefilearea"));
if (!$rr)
{
$m.="<div class=\"errorbox\" style=\"color:#ff0000\"><p style=\"warning\">".get_string("no_coursefilearea", "autoview")."</p></div>\n";
$ok=false;
}
if ($rm)
return $m;
echo $m;
return $ok;
}
/**
* @uses FEATURE_GROUPS
* @uses FEATURE_GROUPINGS
* @uses FEATURE_GROUPMEMBERSONLY
* @uses FEATURE_MOD_INTRO
* @uses FEATURE_COMPLETION_TRACKS_VIEWS
* @uses FEATURE_GRADE_HAS_GRADE
* @uses FEATURE_GRADE_OUTCOMES
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed True if module supports feature, false if not, null if doesn't know
*/
function autoview_supports($feature)
{
switch($feature)
{
case FEATURE_GROUPS: return false;
case FEATURE_GROUPINGS: return false;
case FEATURE_GROUPMEMBERSONLY: return false;
case FEATURE_MOD_INTRO: return false;
case FEATURE_COMPLETION_TRACKS_VIEWS: return false;
case FEATURE_GRADE_HAS_GRADE: return false;
case FEATURE_GRADE_OUTCOMES: return false;
case FEATURE_BACKUP_MOODLE2: return true;
default: return null;
}
}
function autoview_get_coursefilesarea_id($context)
{
global $CFG;
require_once('../../repository/lib.php');
$params = array(
'context'=>array($context, context_system::instance()),
'currentcontext'=>$context,
'onlyvisible'=>true,
'type'=>"coursefilearea");
$repolist = repository::get_instances($params);
if (current($repolist))
return current($repolist)->id;
else
return -1;
}
function autoview_get_context_instance($id)
{
return context_module::instance($id);
}
function autoview_get_course_context_instance($id)
{
return context_course::instance($id);
}
function autoview_add_to_log($course, $mod, $text, $link='', $info='', $cmid=0, $uid=0)
{
global $CFG;
if ($CFG->version >= 2014051200)
{
/** This needs to be re-written to use the events system for Moodle 2.7 and greater**/
/** For now use the code from deprecated lib **/
$manager = get_log_manager();
if (method_exists($manager, 'legacy_add_to_log')) {
$manager->legacy_add_to_log($course, $mod, $text, $link, $info, $cmid, $uid);
}
}
else
add_to_log($course, $mod, $text, $link, $info, $cmid, $uid);
}
?>