Merge branch 'MDL-48718-master' of git://github.com/jleyva/moodle
This commit is contained in:
@@ -361,3 +361,58 @@ function imscp_page_type_list($pagetype, $parentcontext, $currentcontext) {
|
||||
$modulepagetype = array('mod-imscp-*' => get_string('page-mod-imscp-x', 'imscp'));
|
||||
return $modulepagetype;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export imscp resource contents
|
||||
*
|
||||
* @param stdClass $cm Course module object
|
||||
* @param string $baseurl Base URL for file downloads
|
||||
* @return array of file content
|
||||
*/
|
||||
function imscp_export_contents($cm, $baseurl) {
|
||||
global $DB;
|
||||
|
||||
$contents = array();
|
||||
$context = context_module::instance($cm->id);
|
||||
|
||||
$imscp = $DB->get_record('imscp', array('id' => $cm->instance), '*', MUST_EXIST);
|
||||
|
||||
// We export the IMSCP structure as json encoded string.
|
||||
$structure = array();
|
||||
$structure['type'] = 'content';
|
||||
$structure['filename'] = 'structure';
|
||||
$structure['filepath'] = '/';
|
||||
$structure['filesize'] = 0;
|
||||
$structure['fileurl'] = null;
|
||||
$structure['timecreated'] = $imscp->timemodified;
|
||||
$structure['timemodified'] = $imscp->timemodified;
|
||||
$structure['content'] = json_encode(unserialize($imscp->structure));
|
||||
$structure['sortorder'] = 0;
|
||||
$structure['userid'] = null;
|
||||
$structure['author'] = null;
|
||||
$structure['license'] = null;
|
||||
$contents[] = $structure;
|
||||
|
||||
// Area files.
|
||||
$fs = get_file_storage();
|
||||
$files = $fs->get_area_files($context->id, 'mod_imscp', 'content', $imscp->revision, 'id ASC', false);
|
||||
foreach ($files as $fileinfo) {
|
||||
$file = array();
|
||||
$file['type'] = 'file';
|
||||
$file['filename'] = $fileinfo->get_filename();
|
||||
$file['filepath'] = $fileinfo->get_filepath();
|
||||
$file['filesize'] = $fileinfo->get_filesize();
|
||||
$file['fileurl'] = moodle_url::make_webservice_pluginfile_url(
|
||||
$context->id, 'mod_imscp', 'content', $imscp->revision,
|
||||
$fileinfo->get_filepath(), $fileinfo->get_filename())->out(false);
|
||||
$file['timecreated'] = $fileinfo->get_timecreated();
|
||||
$file['timemodified'] = $fileinfo->get_timemodified();
|
||||
$file['sortorder'] = $fileinfo->get_sortorder();
|
||||
$file['userid'] = $fileinfo->get_userid();
|
||||
$file['author'] = $fileinfo->get_author();
|
||||
$file['license'] = $fileinfo->get_license();
|
||||
$contents[] = $file;
|
||||
}
|
||||
|
||||
return $contents;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Unit tests for (some of) mod/imscp/lib.php.
|
||||
*
|
||||
* @package mod_imscp
|
||||
* @category test
|
||||
* @copyright 2015 Juan Leyva <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/mod/imscp/lib.php');
|
||||
|
||||
/**
|
||||
* Unit tests for (some of) mod/imscp/lib.php.
|
||||
*
|
||||
* @package mod_imscp
|
||||
* @category test
|
||||
* @copyright 2015 Juan Leyva <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class mod_imscp_lib_testcase extends advanced_testcase {
|
||||
|
||||
public function test_export_contents() {
|
||||
global $DB, $USER;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
|
||||
|
||||
$this->setAdminUser();
|
||||
$imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id));
|
||||
$cm = get_coursemodule_from_id('imscp', $imscp->cmid);
|
||||
|
||||
$this->setUser($user);
|
||||
$contents = imscp_export_contents($cm, '');
|
||||
|
||||
// The test package contains 47 files.
|
||||
$this->assertCount(47, $contents);
|
||||
// The structure is present.
|
||||
$this->assertEquals('structure', $contents[0]['filename']);
|
||||
// The structure is returned and it maches the expected one.
|
||||
$this->assertEquals(json_encode(unserialize($imscp->structure)), $contents[0]['content']);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user