MDL-44620 behat: Automate MDLQA-7
[moodle.git] / mod / imscp / lib.php
blob97c6d518d42e0f1b10e370a6b20731257107d330
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 the actions that correspond to a view of this module.
69 * This is used by the participation report.
71 * Note: This is not used by new logging system. Event with
72 * crud = 'r' and edulevel = LEVEL_PARTICIPATING will
73 * be considered as view action.
75 * @return array
77 function imscp_get_view_actions() {
78 return array('view', 'view all');
81 /**
82 * List the actions that correspond to a post of this module.
83 * This is used by the participation report.
85 * Note: This is not used by new logging system. Event with
86 * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
87 * will be considered as post action.
89 * @return array
91 function imscp_get_post_actions() {
92 return array('update', 'add');
95 /**
96 * Add imscp instance.
97 * @param object $data
98 * @param object $mform
99 * @return int new imscp instance id
101 function imscp_add_instance($data, $mform) {
102 global $CFG, $DB;
103 require_once("$CFG->dirroot/mod/imscp/locallib.php");
105 $cmid = $data->coursemodule;
107 $data->timemodified = time();
108 $data->revision = 1;
109 $data->structure = null;
111 $data->id = $DB->insert_record('imscp', $data);
113 // we need to use context now, so we need to make sure all needed info is already in db
114 $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
115 $context = context_module::instance($cmid);
116 $imscp = $DB->get_record('imscp', array('id'=>$data->id), '*', MUST_EXIST);
118 if (!empty($data->package)) {
119 // Save uploaded files to 'backup' filearea.
120 $fs = get_file_storage();
121 $fs->delete_area_files($context->id, 'mod_imscp', 'backup', 1);
122 file_save_draft_area_files($data->package, $context->id, 'mod_imscp', 'backup',
123 1, array('subdirs' => 0, 'maxfiles' => 1));
124 // Get filename of zip that was uploaded.
125 $files = $fs->get_area_files($context->id, 'mod_imscp', 'backup', 1, '', false);
126 if ($files) {
127 // Extract package content to 'content' filearea.
128 $package = reset($files);
129 $packer = get_file_packer('application/zip');
130 $package->extract_to_storage($packer, $context->id, 'mod_imscp', 'content', 1, '/');
131 $structure = imscp_parse_structure($imscp, $context);
132 $imscp->structure = is_array($structure) ? serialize($structure) : null;
133 $DB->update_record('imscp', $imscp);
137 return $data->id;
141 * Update imscp instance.
142 * @param object $data
143 * @param object $mform
144 * @return bool true
146 function imscp_update_instance($data, $mform) {
147 global $CFG, $DB;
148 require_once("$CFG->dirroot/mod/imscp/locallib.php");
150 $cmid = $data->coursemodule;
152 $data->timemodified = time();
153 $data->id = $data->instance;
154 $data->structure = null; // better reparse structure after each update
156 $DB->update_record('imscp', $data);
158 $context = context_module::instance($cmid);
159 $imscp = $DB->get_record('imscp', array('id'=>$data->id), '*', MUST_EXIST);
161 if (!empty($data->package) && ($draftareainfo = file_get_draft_area_info($data->package)) &&
162 $draftareainfo['filecount']) {
163 $fs = get_file_storage();
165 $imscp->revision++;
166 $DB->update_record('imscp', $imscp);
168 // get a list of existing packages before adding new package
169 if ($imscp->keepold > -1) {
170 $packages = $fs->get_area_files($context->id, 'mod_imscp', 'backup', false, "itemid ASC", false);
171 } else {
172 $packages = array();
175 file_save_draft_area_files($data->package, $context->id, 'mod_imscp', 'backup',
176 $imscp->revision, array('subdirs' => 0, 'maxfiles' => 1));
177 $files = $fs->get_area_files($context->id, 'mod_imscp', 'backup', $imscp->revision, '', false);
178 $package = reset($files);
180 // purge all extracted content
181 $fs->delete_area_files($context->id, 'mod_imscp', 'content');
183 // extract package content
184 if ($package) {
185 $packer = get_file_packer('application/zip');
186 $package->extract_to_storage($packer, $context->id, 'mod_imscp', 'content', $imscp->revision, '/');
189 // cleanup old package files, keep current + keepold
190 while ($packages and (count($packages) > $imscp->keepold)) {
191 $package = array_shift($packages);
192 $fs->delete_area_files($context->id, 'mod_imscp', 'backup', $package->get_itemid());
196 $structure = imscp_parse_structure($imscp, $context);
197 $imscp->structure = is_array($structure) ? serialize($structure) : null;
198 $DB->update_record('imscp', $imscp);
200 return true;
204 * Delete imscp instance.
205 * @param int $id
206 * @return bool true
208 function imscp_delete_instance($id) {
209 global $DB;
211 if (!$imscp = $DB->get_record('imscp', array('id'=>$id))) {
212 return false;
215 // note: all context files are deleted automatically
217 $DB->delete_records('imscp', array('id'=>$imscp->id));
219 return true;
223 * Lists all browsable file areas
225 * @package mod_imscp
226 * @category files
227 * @param stdClass $course course object
228 * @param stdClass $cm course module object
229 * @param stdClass $context context object
230 * @return array
232 function imscp_get_file_areas($course, $cm, $context) {
233 $areas = array();
235 $areas['content'] = get_string('areacontent', 'imscp');
236 $areas['backup'] = get_string('areabackup', 'imscp');
238 return $areas;
242 * File browsing support for imscp module ontent area.
244 * @package mod_imscp
245 * @category files
246 * @param stdClass $browser file browser
247 * @param stdClass $areas file areas
248 * @param stdClass $course course object
249 * @param stdClass $cm course module object
250 * @param stdClass $context context object
251 * @param string $filearea file area
252 * @param int $itemid item ID
253 * @param string $filepath file path
254 * @param string $filename file name
255 * @return file_info instance or null if not found
257 function imscp_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
258 global $CFG, $DB;
260 // note: imscp_intro handled in file_browser automatically
262 if (!has_capability('moodle/course:managefiles', $context)) {
263 // no peaking here for students!!
264 return null;
267 if ($filearea !== 'content' and $filearea !== 'backup') {
268 return null;
271 require_once("$CFG->dirroot/mod/imscp/locallib.php");
273 if (is_null($itemid)) {
274 return new imscp_file_info($browser, $course, $cm, $context, $areas, $filearea, $itemid);
277 $fs = get_file_storage();
278 $filepath = is_null($filepath) ? '/' : $filepath;
279 $filename = is_null($filename) ? '.' : $filename;
280 if (!$storedfile = $fs->get_file($context->id, 'mod_imscp', $filearea, $itemid, $filepath, $filename)) {
281 return null;
284 // do not allow manual modification of any files!
285 $urlbase = $CFG->wwwroot.'/pluginfile.php';
286 return new file_info_stored($browser, $context, $storedfile, $urlbase, $itemid, true, true, false, false); //no writing here!
290 * Serves the imscp files.
292 * @package mod_imscp
293 * @category files
294 * @param stdClass $course course object
295 * @param stdClass $cm course module object
296 * @param stdClass $context context object
297 * @param string $filearea file area
298 * @param array $args extra arguments
299 * @param bool $forcedownload whether or not force download
300 * @param array $options additional options affecting the file serving
301 * @return bool false if file not found, does not return if found - justsend the file
303 function imscp_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
304 global $CFG, $DB;
306 if ($context->contextlevel != CONTEXT_MODULE) {
307 return false;
310 require_login($course, true, $cm);
312 if ($filearea === 'content') {
313 if (!has_capability('mod/imscp:view', $context)) {
314 return false;
316 $revision = array_shift($args);
317 $fs = get_file_storage();
318 $relativepath = implode('/', $args);
319 if ($relativepath === 'imsmanifest.xml') {
320 if (!has_capability('moodle/course:managefiles', $context)) {
321 // no stealing of detailed package info ;-)
322 return false;
325 $fullpath = "/$context->id/mod_imscp/$filearea/$revision/$relativepath";
326 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
327 return false;
330 // finally send the file
331 send_stored_file($file, null, 0, $forcedownload, $options);
333 } else if ($filearea === 'backup') {
334 if (!has_capability('moodle/course:managefiles', $context)) {
335 // no stealing of package backups
336 return false;
338 $revision = array_shift($args);
339 $fs = get_file_storage();
340 $relativepath = implode('/', $args);
341 $fullpath = "/$context->id/mod_imscp/$filearea/$revision/$relativepath";
342 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
343 return false;
346 // finally send the file
347 send_stored_file($file, null, 0, $forcedownload, $options);
349 } else {
350 return false;
355 * Return a list of page types
356 * @param string $pagetype current page type
357 * @param stdClass $parentcontext Block's parent context
358 * @param stdClass $currentcontext Current context of block
360 function imscp_page_type_list($pagetype, $parentcontext, $currentcontext) {
361 $module_pagetype = array('mod-imscp-*'=>get_string('page-mod-imscp-x', 'imscp'));
362 return $module_pagetype;