/
lib.php
233 lines (194 loc) · 9.62 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
<?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/>.
/**
* @package block_sharedresources
* @category blocks
* @author Valery Fremaux <valery.fremaux@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/mod/sharedresource/classes/sharedresource_plugin_base.class.php');
require_once($CFG->dirroot.'/mod/sharedresource/locallib.php');
/**
* This function is not implemented in this plugin, but is needed to mark
* the vf documentation custom volume availability.
*/
function block_sharedresources_supports_feature($feature) {
assert(1);
}
/**
* processes a set of file entries to convert them as file ressources
* 1. create a Moodle resource that uses the file
* 2. create a Moodle course_module that attaches the resource to the course
* 3. create a page format page_item that puts the resource in the page
* @param arrayref &$data a bulk of entries and metadata.
* @param objectref &$course the course
*/
function sharedresources_process_entries(&$data, &$course) {
global $USER;
$usercontext = context_user::instance($USER->id);
$fs = get_file_storage();
$filestoprocess = $fs->get_area_files($usercontext->id, 'user', 'draft', $data->entries, "itemid, filepath, filename", false);
foreach ($filestoprocess as $file) {
$fileid = $file->get_id();
$metadata = new StdClass;
$metadata->title = $data->{'title'.$fileid};
$metadata->description = $data->{'description'.$fileid};
$metadata->keywords = $data->{'keywords'.$fileid};
$metadata->context = $data->{'context'.$fileid};
$metadata->publish = @$data->{'coursepublish'.$fileid};
$metadata->overwrite = @$data->{'overwritedata'.$fileid};
sharedresources_process_single_entry($file, $metadata, $course);
}
// Full cleans out file area after processing.
$fs->delete_area_files($usercontext->id, 'user', 'draft', $data->entries);
}
/**
* Processes one single file to import in course as sharedresource
* @param stored_file $file file to process
* @param array $metadata metadata array
* @param objectref &$course
*/
function sharedresources_process_single_entry(stored_file $file, $metadata = array(), &$course) {
global $DB, $CFG, $PAGE;
static $pbm = null;
$config = get_config('sharedresource');
if (is_null($pbm) && $course->format == 'page') {
$pbm = new page_enabled_block_manager($PAGE);
}
// Create moodle resource.
$module = $DB->get_record('modules', array('name' => 'sharedresource'));
// Check for sharedresourceentry and add if not here.
$identifier = $file->get_contenthash();
if (!$sharedentry = $DB->get_record('sharedresource_entry', array('identifier' => $identifier))) {
$shentry = new StdClass;
$shentry->title = $metadata->title;
$shentry->type = 'file';
$shentry->mimetype = $file->get_mimetype();
$shentry->identifier = $identifier;
$shentry->remoteid = '';
$shentry->file = $file->get_id();
$shentry->url = $CFG->wwwroot.'/mod/sharedresource/view.php?identifier='.$identifier;
$shentry->lang = ''; // Not more used.
$shentry->description = @$metadata->description;
$shentry->keywords = @$metadata->keywords;
$shentry->timemodified = time();
$shentry->provider = 'local';
$shentry->isvalid = 1;
$shentry->context = $metadata->context;
$shentry->scoreview = 0;
$shentry->scorelike = 0;
try {
$sharedresourceentryid = $DB->insert_record('sharedresource_entry', $shentry);
// Give some traces.
print_string('builtentry', 'block_sharedresources', $shentry->identifier);
// Dispatch metadata into entry.
$mtdstandard = \mod_sharedresource\plugin_base::load_mtdstandard($config->schema);
$mtdstandard->setEntry($sharedresourceentryid);
$mtdstandard->setTextElementValue($mtdstandard->getDescriptionElement()->node, '', $shentry->description);
$mtdstandard->setTextElementValue($mtdstandard->getTitleElement()->node, '', $shentry->title);
if (!empty($shentry->keywords)) {
$mtdstandard->setKeywords($shentry->keywords);
}
// Store now the draft file in sharedresource filearea.
$systemcontext = context_system::instance();
$fs = get_file_storage();
$filerecord = new StdClass;
$filerecord->contextid = $systemcontext->id;
$filerecord->component = 'mod_sharedresource';
$filerecord->filearea = 'sharedresource';
$filerecord->itemid = $sharedresourceentryid;
$filerecord->path = '/';
$newfile = $fs->create_file_from_storedfile($filerecord, $file);
// Remap sharedresource with new file record.
$DB->set_field('sharedresource_entry', 'file', $newfile->get_id(), array('identifier' => $identifier));
} catch (Exception $e) {
mtrace('Error while saving file');
}
} else {
if ($metadata->overwrite) {
print_string('exists', 'block_sharedresources', $sharedentry);
// Give some traces.
$sharedentry->context = $metadata->context;
$sharedentry->description = @$metadata->description;
$sharedentry->keywords = @$metadata->keywords;
$DB->update_record('sharedresource_entry', $sharedentry);
// Dispatch metadata into entry.
$mtdstandard = \mod_sharedresource\plugin_base::load_mtdstandard($config->schema);
$mtdstandard->setEntry($sharedentry->id);
$mtdstandard->setTextElementValue($mtdstandard->getDescriptionElement()->node, '', $sharedentry->description);
$mtdstandard->setTextElementValue($mtdstandard->getTitleElement()->node, '', $sharedentry->title);
if (!empty($shentry->keywords)) {
$mtdstandard->setKeywords($shentry->keywords);
}
} else {
// Give some traces.
print_string('existsignorechanges', 'block_sharedresources', $sharedentry);
}
}
if ($metadata->publish) {
if ($course->id != SITEID) {
// Complete a sharedresource record.
$sharedresource = new StdClass;
$sharedresource->course = $course->id;
$sharedresource->type = 'file';
$sharedresource->name = $metadata->title;
$sharedresource->summary = @$metadata->description;
$sharedresource->identifier = $identifier;
$sharedresource->alltext = '';
$sharedresource->popup = 0;
$sharedresource->options = '';
$sharedresource->timemodified = time();
$sharedresource->id = $DB->insert_record('sharedresource', $sharedresource);
$cm = new StdClass;
$cm->course = $course->id;
$cm->module = $module->id;
$cm->instance = $sharedresource->id;
$cm->section = 1;
$cm->visible = 1;
$cm->coursemodule = add_course_module($cm);
course_add_cm_to_section($course, $cm->coursemodule, sharedresource_get_course_section_to_add($course));
// Finish with a pageitem if we are in a page format.
if ($course->format == 'page') {
// Add a page_item.
include_once($CFG->dirroot.'/course/format/page/page.class.php');
$page = \format\page\course_page::get_current_page($course->id);
$pageitem = new StdClass;
$pageitem->pageid = $page->id;
$pageitem->cmid = $cm->coursemodule;
$pageitem->blockinstance = 0;
$pageitem->position = 'c';
$select = " pageid = ? AND position = 'c' ";
$params = array($pageitem->pageid);
$pageitem->sortorder = $DB->get_field_select('format_page_items', 'MAX(sortorder)', $select, $params) + 1;
$pageitem->visible = 1;
$pageitem->id = $DB->insert_record('format_page_items', $pageitem);
if ($instance = $pbm->add_block_at_end_of_page_region('page_module', $page->id)) {
$pageitem = $DB->get_record('format_page_items', array('blockinstance' => $instance->id));
$DB->set_field('format_page_items', 'cmid', $cm->coursemodule, array('id' => $pageitem->id));
$DB->set_field('format_page_items', 'blockinstance', $instance->id, array('id' => $pageitem->id));
}
// Now add cminstance id to configuration.
$block = block_instance('page_module', $instance);
$block->config->cmid = $cm->coursemodule;
$block->instance_config_save($block->config);
}
// Give some traces.
print_string('constructed', 'block_sharedresources', $sharedresource->id);
}
}
}