-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupload_word.php
88 lines (69 loc) · 2.89 KB
/
upload_word.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
<?php
/**
* ************************************************************************
* * Speech Coach **
* ************************************************************************
* @package mod **
* @subpackage Speech Coach **
* @name Speech Coach **
* @copyright oohoo.biz **
* @link http://oohoo.biz **
* @author Andrew McCann **
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later **
* ************************************************************************
* ************************************************************************ */
/**
* This file uploads a new recording into moodle. (Done only by teachers.)
*/
require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
require_once(dirname(__FILE__) . '/lib.php');
$id = required_param('id', PARAM_INT); // course_module ID, or
$wordname = required_param('word_name', PARAM_TEXT);
$cm = get_coursemodule_from_id('speechcoach', $id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
require_login($course, true, $cm);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_capability('mod/speechcoach:edit', $context);
if (isset($_REQUEST['sendsound']) && $_REQUEST['sendsound'] == true) {
//If in mode sendsound, save the file from php://input to $contentSound'
$contentSound = file_get_contents('php://input');
} else if (isset($_FILES['uploadsound']) && $_FILES['uploadsound'] == true) {
// $contentSound = $_FILES['']
}
$tempname = tempnam(sys_get_temp_dir(), 'sch');
clearstatcache();
$bytes = file_put_contents($tempname, $contentSound);
$filepath = uniqid();
while (filepath_exists($filepath)) {
$filepath = uniqid();
}
$file_record = array(
'contextid' => $context->id,
'component' => 'mod_speechcoach',
'filearea' => 'words_audio',
'itemid' => 0,
'filepath' => '/' . $filepath . '/',
'filename' => uniqid(),
'mimetype' => 'audio/wav'
);
$fs = get_file_storage();
$myfile = $fs->create_file_from_pathname($file_record, $tempname);
$record = new stdClass();
$record->course_module_id = $id;
$record->word = $wordname;
$record->file_id = $myfile->get_id();
$record->active = 1;
$DB->insert_record("speechcoach_words", $record);
unlink($tempname);
function filepath_exists($filepath) {
global $context;
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'mod_speechcoach', 'history_audio');
foreach ($files as $file) {
if ($file->get_filepath() == $filepath) {
return true;
}
}
return false;
}
?>