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
23 * @copyright 2009 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
30 * List of features supported in IMS CP module
31 * @param string $feature FEATURE_xx constant for requested feature
32 * @return mixed True if module supports feature, false if not, null if doesn't know
34 function imscp_supports($feature) {
36 case FEATURE_MOD_ARCHETYPE
: return MOD_ARCHETYPE_RESOURCE
;
37 case FEATURE_GROUPS
: return false;
38 case FEATURE_GROUPINGS
: return false;
39 case FEATURE_GROUPMEMBERSONLY
: return true;
40 case FEATURE_MOD_INTRO
: return true;
41 case FEATURE_COMPLETION_TRACKS_VIEWS
: return true;
42 case FEATURE_GRADE_HAS_GRADE
: return false;
43 case FEATURE_GRADE_OUTCOMES
: return false;
44 case FEATURE_BACKUP_MOODLE2
: return true;
45 case FEATURE_SHOW_DESCRIPTION
: return true;
52 * Returns all other caps used in module
55 function imscp_get_extra_capabilities() {
56 return array('moodle/site:accessallgroups');
60 * This function is used by the reset_course_userdata function in moodlelib.
61 * @param $data the data submitted from the reset course.
62 * @return array status array
64 function imscp_reset_userdata($data) {
69 * List of view style log actions
72 function imscp_get_view_actions() {
73 return array('view', 'view all');
77 * List of update style log actions
80 function imscp_get_post_actions() {
81 return array('update', 'add');
87 * @param object $mform
88 * @return int new imscp instance id
90 function imscp_add_instance($data, $mform) {
92 require_once("$CFG->dirroot/mod/imscp/locallib.php");
94 $cmid = $data->coursemodule
;
96 $data->timemodified
= time();
98 $data->structure
= null;
100 $data->id
= $DB->insert_record('imscp', $data);
102 // we need to use context now, so we need to make sure all needed info is already in db
103 $DB->set_field('course_modules', 'instance', $data->id
, array('id'=>$cmid));
104 $context = get_context_instance(CONTEXT_MODULE
, $cmid);
105 $imscp = $DB->get_record('imscp', array('id'=>$data->id
), '*', MUST_EXIST
);
107 if ($filename = $mform->get_new_filename('package')) {
108 if ($package = $mform->save_stored_file('package', $context->id
, 'mod_imscp', 'backup', 1, '/', $filename)) {
109 // extract package content
110 $packer = get_file_packer('application/zip');
111 $package->extract_to_storage($packer, $context->id
, 'mod_imscp', 'content', 1, '/');
112 $structure = imscp_parse_structure($imscp, $context);
113 $imscp->structure
= is_array($structure) ?
serialize($structure) : null;
114 $DB->update_record('imscp', $imscp);
122 * Update imscp instance.
123 * @param object $data
124 * @param object $mform
127 function imscp_update_instance($data, $mform) {
129 require_once("$CFG->dirroot/mod/imscp/locallib.php");
131 $cmid = $data->coursemodule
;
133 $data->timemodified
= time();
134 $data->id
= $data->instance
;
135 $data->structure
= null; // better reparse structure after each update
137 $DB->update_record('imscp', $data);
139 $context = get_context_instance(CONTEXT_MODULE
, $cmid);
140 $imscp = $DB->get_record('imscp', array('id'=>$data->id
), '*', MUST_EXIST
);
142 if ($filename = $mform->get_new_filename('package')) {
143 $fs = get_file_storage();
146 $DB->update_record('imscp', $imscp);
148 // get a list of existing packages before adding new package
149 if ($imscp->keepold
> -1) {
150 $packages = $fs->get_area_files($context->id
, 'mod_imscp', 'backup', false, "itemid ASC", false);
155 $package = $mform->save_stored_file('package', $context->id
, 'mod_imscp', 'backup', $imscp->revision
, '/', $filename);
157 // purge all extracted content
158 $fs->delete_area_files($context->id
, 'mod_imscp', 'content');
160 // extract package content
162 $packer = get_file_packer('application/zip');
163 $package->extract_to_storage($packer, $context->id
, 'mod_imscp', 'content', $imscp->revision
, '/');
166 // cleanup old package files, keep current + keepold
167 while ($packages and (count($packages) > $imscp->keepold
)) {
168 $package = array_shift($packages);
169 $fs->delete_area_files($context->id
, 'mod_imscp', 'backup', $package->get_itemid());
173 $structure = imscp_parse_structure($imscp, $context);
174 $imscp->structure
= is_array($structure) ?
serialize($structure) : null;
175 $DB->update_record('imscp', $imscp);
181 * Delete imscp instance.
185 function imscp_delete_instance($id) {
188 if (!$imscp = $DB->get_record('imscp', array('id'=>$id))) {
192 // note: all context files are deleted automatically
194 $DB->delete_records('imscp', array('id'=>$imscp->id
));
201 * @param object $course
202 * @param object $user
204 * @param object $imscp
205 * @return object|null
207 function imscp_user_outline($course, $user, $mod, $imscp) {
210 if ($logs = $DB->get_records('log', array('userid'=>$user->id
, 'module'=>'imscp',
211 'action'=>'view', 'info'=>$imscp->id
), 'time ASC')) {
213 $numviews = count($logs);
214 $lastlog = array_pop($logs);
216 $result = new stdClass();
217 $result->info
= get_string('numviews', '', $numviews);
218 $result->time
= $lastlog->time
;
226 * Return use complete
227 * @param object $course
228 * @param object $user
230 * @param object $imscp
232 function imscp_user_complete($course, $user, $mod, $imscp) {
235 if ($logs = $DB->get_records('log', array('userid'=>$user->id
, 'module'=>'imscp',
236 'action'=>'view', 'info'=>$imscp->id
), 'time ASC')) {
237 $numviews = count($logs);
238 $lastlog = array_pop($logs);
240 $strmostrecently = get_string('mostrecently');
241 $strnumviews = get_string('numviews', '', $numviews);
243 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time
);
246 print_string('neverseen', 'imscp');
251 * Returns the users with data in one imscp
253 * @todo: deprecated - to be deleted in 2.2
255 * @param int $imscpid
258 function imscp_get_participants($imscpid) {
263 * Lists all browsable file areas
264 * @param object $course
266 * @param object $context
269 function imscp_get_file_areas($course, $cm, $context) {
272 $areas['content'] = get_string('areacontent', 'imscp');
273 $areas['backup'] = get_string('areabackup', 'imscp');
279 * File browsing support for imscp module ontent area.
280 * @param object $browser
281 * @param object $areas
282 * @param object $course
284 * @param object $context
285 * @param string $filearea
287 * @param string $filepath
288 * @param string $filename
289 * @return object file_info instance or null if not found
291 function imscp_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
294 // note: imscp_intro handled in file_browser automatically
296 if (!has_capability('moodle/course:managefiles', $context)) {
297 // no peaking here for students!!
301 if ($filearea !== 'content' and $filearea !== 'backup') {
305 require_once("$CFG->dirroot/mod/imscp/locallib.php");
307 if (is_null($itemid)) {
308 return new imscp_file_info($browser, $course, $cm, $context, $areas, $filearea, $itemid);
311 $fs = get_file_storage();
312 $filepath = is_null($filepath) ?
'/' : $filepath;
313 $filename = is_null($filename) ?
'.' : $filename;
314 if (!$storedfile = $fs->get_file($context->id
, 'mod_imscp', $filearea, $itemid, $filepath, $filename)) {
318 // do not allow manual modification of any files!
319 $urlbase = $CFG->wwwroot
.'/pluginfile.php';
320 return new file_info_stored($browser, $context, $storedfile, $urlbase, $itemid, true, true, false, false); //no writing here!
324 * Serves the imscp files.
326 * @param object $course
328 * @param object $context
329 * @param string $filearea
331 * @param bool $forcedownload
332 * @return bool false if file not found, does not return if found - justsend the file
334 function imscp_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
337 if ($context->contextlevel
!= CONTEXT_MODULE
) {
341 require_login($course, true, $cm);
343 if ($filearea === 'content') {
344 if (!has_capability('mod/imscp:view', $context)) {
347 $revision = array_shift($args);
348 $fs = get_file_storage();
349 $relativepath = implode('/', $args);
350 if ($relativepath === 'imsmanifest.xml') {
351 if (!has_capability('moodle/course:managefiles', $context)) {
352 // no stealing of detailed package info ;-)
356 $fullpath = "/$context->id/mod_imscp/$filearea/$revision/$relativepath";
357 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
361 // finally send the file
362 send_stored_file($file, 86400, 0, $forcedownload);
364 } else if ($filearea === 'backup') {
365 if (!has_capability('moodle/course:managefiles', $context)) {
366 // no stealing of package backups
369 $revision = array_shift($args);
370 $fs = get_file_storage();
371 $relativepath = implode('/', $args);
372 $fullpath = "/$context->id/mod_imscp/$filearea/$revision/$relativepath";
373 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
377 // finally send the file
378 send_stored_file($file, 86400, 0, $forcedownload);
386 * Return a list of page types
387 * @param string $pagetype current page type
388 * @param stdClass $parentcontext Block's parent context
389 * @param stdClass $currentcontext Current context of block
391 function imscp_page_type_list($pagetype, $parentcontext, $currentcontext) {
392 $module_pagetype = array('mod-imscp-*'=>get_string('page-mod-imscp-x', 'imscp'));
393 return $module_pagetype;