3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Mandatory public API of imscp module
22 * @copyright 2009 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 * List of features supported in IMS CP module
30 * @param string $feature FEATURE_xx constant for requested feature
31 * @return mixed True if module supports feature, false if not, null if doesn't know
33 function imscp_supports($feature) {
35 case FEATURE_MOD_ARCHETYPE
: return MOD_ARCHETYPE_RESOURCE
;
36 case FEATURE_GROUPS
: return false;
37 case FEATURE_GROUPINGS
: return false;
38 case FEATURE_GROUPMEMBERSONLY
: return true;
39 case FEATURE_MOD_INTRO
: return true;
40 case FEATURE_COMPLETION_TRACKS_VIEWS
: return true;
41 case FEATURE_GRADE_HAS_GRADE
: return false;
42 case FEATURE_GRADE_OUTCOMES
: return false;
43 case FEATURE_BACKUP_MOODLE2
: return true;
44 case FEATURE_SHOW_DESCRIPTION
: return true;
51 * Returns all other caps used in module
54 function imscp_get_extra_capabilities() {
55 return array('moodle/site:accessallgroups');
59 * This function is used by the reset_course_userdata function in moodlelib.
60 * @param $data the data submitted from the reset course.
61 * @return array status array
63 function imscp_reset_userdata($data) {
68 * List of view style log actions
71 function imscp_get_view_actions() {
72 return array('view', 'view all');
76 * List of update style log actions
79 function imscp_get_post_actions() {
80 return array('update', 'add');
86 * @param object $mform
87 * @return int new imscp instance id
89 function imscp_add_instance($data, $mform) {
91 require_once("$CFG->dirroot/mod/imscp/locallib.php");
93 $cmid = $data->coursemodule
;
95 $data->timemodified
= time();
97 $data->structure
= null;
99 $data->id
= $DB->insert_record('imscp', $data);
101 // we need to use context now, so we need to make sure all needed info is already in db
102 $DB->set_field('course_modules', 'instance', $data->id
, array('id'=>$cmid));
103 $context = context_module
::instance($cmid);
104 $imscp = $DB->get_record('imscp', array('id'=>$data->id
), '*', MUST_EXIST
);
106 if (!empty($data->package
)) {
107 // Save uploaded files to 'backup' filearea.
108 $fs = get_file_storage();
109 $fs->delete_area_files($context->id
, 'mod_imscp', 'backup', 1);
110 file_save_draft_area_files($data->package
, $context->id
, 'mod_imscp', 'backup',
111 1, array('subdirs' => 0, 'maxfiles' => 1));
112 // Get filename of zip that was uploaded.
113 $files = $fs->get_area_files($context->id
, 'mod_imscp', 'backup', 1, '', false);
115 // Extract package content to 'content' filearea.
116 $package = reset($files);
117 $packer = get_file_packer('application/zip');
118 $package->extract_to_storage($packer, $context->id
, 'mod_imscp', 'content', 1, '/');
119 $structure = imscp_parse_structure($imscp, $context);
120 $imscp->structure
= is_array($structure) ?
serialize($structure) : null;
121 $DB->update_record('imscp', $imscp);
129 * Update imscp instance.
130 * @param object $data
131 * @param object $mform
134 function imscp_update_instance($data, $mform) {
136 require_once("$CFG->dirroot/mod/imscp/locallib.php");
138 $cmid = $data->coursemodule
;
140 $data->timemodified
= time();
141 $data->id
= $data->instance
;
142 $data->structure
= null; // better reparse structure after each update
144 $DB->update_record('imscp', $data);
146 $context = context_module
::instance($cmid);
147 $imscp = $DB->get_record('imscp', array('id'=>$data->id
), '*', MUST_EXIST
);
149 if (!empty($data->package
) && ($draftareainfo = file_get_draft_area_info($data->package
)) &&
150 $draftareainfo['filecount']) {
151 $fs = get_file_storage();
154 $DB->update_record('imscp', $imscp);
156 // get a list of existing packages before adding new package
157 if ($imscp->keepold
> -1) {
158 $packages = $fs->get_area_files($context->id
, 'mod_imscp', 'backup', false, "itemid ASC", false);
163 file_save_draft_area_files($data->package
, $context->id
, 'mod_imscp', 'backup',
164 $imscp->revision
, array('subdirs' => 0, 'maxfiles' => 1));
165 $files = $fs->get_area_files($context->id
, 'mod_imscp', 'backup', $imscp->revision
, '', false);
166 $package = reset($files);
168 // purge all extracted content
169 $fs->delete_area_files($context->id
, 'mod_imscp', 'content');
171 // extract package content
173 $packer = get_file_packer('application/zip');
174 $package->extract_to_storage($packer, $context->id
, 'mod_imscp', 'content', $imscp->revision
, '/');
177 // cleanup old package files, keep current + keepold
178 while ($packages and (count($packages) > $imscp->keepold
)) {
179 $package = array_shift($packages);
180 $fs->delete_area_files($context->id
, 'mod_imscp', 'backup', $package->get_itemid());
184 $structure = imscp_parse_structure($imscp, $context);
185 $imscp->structure
= is_array($structure) ?
serialize($structure) : null;
186 $DB->update_record('imscp', $imscp);
192 * Delete imscp instance.
196 function imscp_delete_instance($id) {
199 if (!$imscp = $DB->get_record('imscp', array('id'=>$id))) {
203 // note: all context files are deleted automatically
205 $DB->delete_records('imscp', array('id'=>$imscp->id
));
212 * @param object $course
213 * @param object $user
215 * @param object $imscp
216 * @return object|null
218 function imscp_user_outline($course, $user, $mod, $imscp) {
221 if ($logs = $DB->get_records('log', array('userid'=>$user->id
, 'module'=>'imscp',
222 'action'=>'view', 'info'=>$imscp->id
), 'time ASC')) {
224 $numviews = count($logs);
225 $lastlog = array_pop($logs);
227 $result = new stdClass();
228 $result->info
= get_string('numviews', '', $numviews);
229 $result->time
= $lastlog->time
;
237 * Return use complete
238 * @param object $course
239 * @param object $user
241 * @param object $imscp
243 function imscp_user_complete($course, $user, $mod, $imscp) {
246 if ($logs = $DB->get_records('log', array('userid'=>$user->id
, 'module'=>'imscp',
247 'action'=>'view', 'info'=>$imscp->id
), 'time ASC')) {
248 $numviews = count($logs);
249 $lastlog = array_pop($logs);
251 $strmostrecently = get_string('mostrecently');
252 $strnumviews = get_string('numviews', '', $numviews);
254 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time
);
257 print_string('neverseen', 'imscp');
262 * Lists all browsable file areas
266 * @param stdClass $course course object
267 * @param stdClass $cm course module object
268 * @param stdClass $context context object
271 function imscp_get_file_areas($course, $cm, $context) {
274 $areas['content'] = get_string('areacontent', 'imscp');
275 $areas['backup'] = get_string('areabackup', 'imscp');
281 * File browsing support for imscp module ontent area.
285 * @param stdClass $browser file browser
286 * @param stdClass $areas file areas
287 * @param stdClass $course course object
288 * @param stdClass $cm course module object
289 * @param stdClass $context context object
290 * @param string $filearea file area
291 * @param int $itemid item ID
292 * @param string $filepath file path
293 * @param string $filename file name
294 * @return file_info instance or null if not found
296 function imscp_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
299 // note: imscp_intro handled in file_browser automatically
301 if (!has_capability('moodle/course:managefiles', $context)) {
302 // no peaking here for students!!
306 if ($filearea !== 'content' and $filearea !== 'backup') {
310 require_once("$CFG->dirroot/mod/imscp/locallib.php");
312 if (is_null($itemid)) {
313 return new imscp_file_info($browser, $course, $cm, $context, $areas, $filearea, $itemid);
316 $fs = get_file_storage();
317 $filepath = is_null($filepath) ?
'/' : $filepath;
318 $filename = is_null($filename) ?
'.' : $filename;
319 if (!$storedfile = $fs->get_file($context->id
, 'mod_imscp', $filearea, $itemid, $filepath, $filename)) {
323 // do not allow manual modification of any files!
324 $urlbase = $CFG->wwwroot
.'/pluginfile.php';
325 return new file_info_stored($browser, $context, $storedfile, $urlbase, $itemid, true, true, false, false); //no writing here!
329 * Serves the imscp files.
333 * @param stdClass $course course object
334 * @param stdClass $cm course module object
335 * @param stdClass $context context object
336 * @param string $filearea file area
337 * @param array $args extra arguments
338 * @param bool $forcedownload whether or not force download
339 * @param array $options additional options affecting the file serving
340 * @return bool false if file not found, does not return if found - justsend the file
342 function imscp_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
345 if ($context->contextlevel
!= CONTEXT_MODULE
) {
349 require_login($course, true, $cm);
351 if ($filearea === 'content') {
352 if (!has_capability('mod/imscp:view', $context)) {
355 $revision = array_shift($args);
356 $fs = get_file_storage();
357 $relativepath = implode('/', $args);
358 if ($relativepath === 'imsmanifest.xml') {
359 if (!has_capability('moodle/course:managefiles', $context)) {
360 // no stealing of detailed package info ;-)
364 $fullpath = "/$context->id/mod_imscp/$filearea/$revision/$relativepath";
365 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
369 // finally send the file
370 send_stored_file($file, null, 0, $forcedownload, $options);
372 } else if ($filearea === 'backup') {
373 if (!has_capability('moodle/course:managefiles', $context)) {
374 // no stealing of package backups
377 $revision = array_shift($args);
378 $fs = get_file_storage();
379 $relativepath = implode('/', $args);
380 $fullpath = "/$context->id/mod_imscp/$filearea/$revision/$relativepath";
381 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
385 // finally send the file
386 send_stored_file($file, null, 0, $forcedownload, $options);
394 * Return a list of page types
395 * @param string $pagetype current page type
396 * @param stdClass $parentcontext Block's parent context
397 * @param stdClass $currentcontext Current context of block
399 function imscp_page_type_list($pagetype, $parentcontext, $currentcontext) {
400 $module_pagetype = array('mod-imscp-*'=>get_string('page-mod-imscp-x', 'imscp'));
401 return $module_pagetype;