Merge branch 'wip-mdl-41266' of https://github.com/rajeshtaneja/moodle
[moodle.git] / mod / imscp / lib.php
blob56bd5227b2def697cc1f077bd104104fe9cdb0c8
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
18 /**
19 * Mandatory public API of imscp module
21 * @package mod_imscp
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();
28 /**
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) {
34 switch($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;
46 default: return null;
50 /**
51 * Returns all other caps used in module
52 * @return array
54 function imscp_get_extra_capabilities() {
55 return array('moodle/site:accessallgroups');
58 /**
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) {
64 return array();
67 /**
68 * List of view style log actions
69 * @return array
71 function imscp_get_view_actions() {
72 return array('view', 'view all');
75 /**
76 * List of update style log actions
77 * @return array
79 function imscp_get_post_actions() {
80 return array('update', 'add');
83 /**
84 * Add imscp instance.
85 * @param object $data
86 * @param object $mform
87 * @return int new imscp instance id
89 function imscp_add_instance($data, $mform) {
90 global $CFG, $DB;
91 require_once("$CFG->dirroot/mod/imscp/locallib.php");
93 $cmid = $data->coursemodule;
95 $data->timemodified = time();
96 $data->revision = 1;
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);
114 if ($files) {
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);
125 return $data->id;
129 * Update imscp instance.
130 * @param object $data
131 * @param object $mform
132 * @return bool true
134 function imscp_update_instance($data, $mform) {
135 global $CFG, $DB;
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();
153 $imscp->revision++;
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);
159 } else {
160 $packages = array();
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
172 if ($package) {
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);
188 return true;
192 * Delete imscp instance.
193 * @param int $id
194 * @return bool true
196 function imscp_delete_instance($id) {
197 global $DB;
199 if (!$imscp = $DB->get_record('imscp', array('id'=>$id))) {
200 return false;
203 // note: all context files are deleted automatically
205 $DB->delete_records('imscp', array('id'=>$imscp->id));
207 return true;
211 * Return use outline
212 * @param object $course
213 * @param object $user
214 * @param object $mod
215 * @param object $imscp
216 * @return object|null
218 function imscp_user_outline($course, $user, $mod, $imscp) {
219 global $DB;
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;
231 return $result;
233 return NULL;
237 * Return use complete
238 * @param object $course
239 * @param object $user
240 * @param object $mod
241 * @param object $imscp
243 function imscp_user_complete($course, $user, $mod, $imscp) {
244 global $CFG, $DB;
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);
256 } else {
257 print_string('neverseen', 'imscp');
262 * Lists all browsable file areas
264 * @package mod_imscp
265 * @category files
266 * @param stdClass $course course object
267 * @param stdClass $cm course module object
268 * @param stdClass $context context object
269 * @return array
271 function imscp_get_file_areas($course, $cm, $context) {
272 $areas = array();
274 $areas['content'] = get_string('areacontent', 'imscp');
275 $areas['backup'] = get_string('areabackup', 'imscp');
277 return $areas;
281 * File browsing support for imscp module ontent area.
283 * @package mod_imscp
284 * @category files
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) {
297 global $CFG, $DB;
299 // note: imscp_intro handled in file_browser automatically
301 if (!has_capability('moodle/course:managefiles', $context)) {
302 // no peaking here for students!!
303 return null;
306 if ($filearea !== 'content' and $filearea !== 'backup') {
307 return null;
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)) {
320 return null;
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.
331 * @package mod_imscp
332 * @category 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()) {
343 global $CFG, $DB;
345 if ($context->contextlevel != CONTEXT_MODULE) {
346 return false;
349 require_login($course, true, $cm);
351 if ($filearea === 'content') {
352 if (!has_capability('mod/imscp:view', $context)) {
353 return false;
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 ;-)
361 return false;
364 $fullpath = "/$context->id/mod_imscp/$filearea/$revision/$relativepath";
365 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
366 return false;
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
375 return false;
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()) {
382 return false;
385 // finally send the file
386 send_stored_file($file, null, 0, $forcedownload, $options);
388 } else {
389 return false;
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;