Commit d68d5ce3 authored by Andrew Nicols's avatar Andrew Nicols
Browse files

Merge branch 'MDL-50547-master' of git://github.com/jleyva/moodle

parents 6b8e5ce3 429cc936
......@@ -104,4 +104,111 @@ class mod_resource_external extends external_api {
);
}
/**
* Describes the parameters for get_resources_by_courses.
*
* @return external_function_parameters
* @since Moodle 3.3
*/
public static function get_resources_by_courses_parameters() {
return new external_function_parameters (
array(
'courseids' => new external_multiple_structure(
new external_value(PARAM_INT, 'Course id'), 'Array of course ids', VALUE_DEFAULT, array()
),
)
);
}
/**
* Returns a list of files in a provided list of courses.
* If no list is provided all files that the user can view will be returned.
*
* @param array $courseids course ids
* @return array of warnings and files
* @since Moodle 3.3
*/
public static function get_resources_by_courses($courseids = array()) {
$warnings = array();
$returnedresources = array();
$params = array(
'courseids' => $courseids,
);
$params = self::validate_parameters(self::get_resources_by_courses_parameters(), $params);
$mycourses = array();
if (empty($params['courseids'])) {
$mycourses = enrol_get_my_courses();
$params['courseids'] = array_keys($mycourses);
}
// Ensure there are courseids to loop through.
if (!empty($params['courseids'])) {
list($courses, $warnings) = external_util::validate_courses($params['courseids'], $mycourses);
// Get the resources in this course, this function checks users visibility permissions.
// We can avoid then additional validate_context calls.
$resources = get_all_instances_in_courses("resource", $courses);
foreach ($resources as $resource) {
$context = context_module::instance($resource->coursemodule);
// Entry to return.
$resource->name = external_format_string($resource->name, $context->id);
list($resource->intro, $resource->introformat) = external_format_text($resource->intro,
$resource->introformat, $context->id, 'mod_resource', 'intro', null);
$resource->introfiles = external_util::get_area_files($context->id, 'mod_resource', 'intro', false, false);
$resource->contentfiles = external_util::get_area_files($context->id, 'mod_resource', 'content');
$returnedresources[] = $resource;
}
}
$result = array(
'resources' => $returnedresources,
'warnings' => $warnings
);
return $result;
}
/**
* Describes the get_resources_by_courses return value.
*
* @return external_single_structure
* @since Moodle 3.3
*/
public static function get_resources_by_courses_returns() {
return new external_single_structure(
array(
'resources' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'Module id'),
'course' => new external_value(PARAM_INT, 'Course id'),
'name' => new external_value(PARAM_RAW, 'Page name'),
'intro' => new external_value(PARAM_RAW, 'Summary'),
'introformat' => new external_format_value('intro', 'Summary format'),
'introfiles' => new external_files('Files in the introduction text'),
'contentfiles' => new external_files('Files in the content'),
'tobemigrated' => new external_value(PARAM_INT, 'Whether this resource was migrated'),
'legacyfiles' => new external_value(PARAM_INT, 'Legacy files flag'),
'legacyfileslast' => new external_value(PARAM_INT, 'Legacy files last control flag'),
'display' => new external_value(PARAM_INT, 'How to display the resource'),
'displayoptions' => new external_value(PARAM_RAW, 'Display options (width, height)'),
'filterfiles' => new external_value(PARAM_INT, 'If filters should be applied to the resource content'),
'revision' => new external_value(PARAM_INT, 'Incremented when after each file changes, to avoid cache'),
'timemodified' => new external_value(PARAM_INT, 'Last time the resource was modified'),
'section' => new external_value(PARAM_INT, 'Course section id'),
'visible' => new external_value(PARAM_INT, 'Module visibility'),
'groupmode' => new external_value(PARAM_INT, 'Group mode'),
'groupingid' => new external_value(PARAM_INT, 'Grouping id'),
)
)
),
'warnings' => new external_warnings(),
)
);
}
}
......@@ -36,5 +36,13 @@ $functions = array(
'capabilities' => 'mod/resource:view',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
'mod_resource_get_resources_by_courses' => array(
'classname' => 'mod_resource_external',
'methodname' => 'get_resources_by_courses',
'description' => 'Returns a list of files in a provided list of courses, if no list is provided all files that
the user can view will be returned.',
'type' => 'read',
'capabilities' => 'mod/resource:view',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
);
......@@ -111,4 +111,146 @@ class mod_resource_external_testcase extends externallib_advanced_testcase {
}
}
/**
* Test test_mod_resource_get_resources_by_courses
*/
public function test_mod_resource_get_resources_by_courses() {
global $DB;
$this->resetAfterTest(true);
$course1 = self::getDataGenerator()->create_course();
$course2 = self::getDataGenerator()->create_course();
$student = self::getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id);
self::setUser($student);
// First resource.
$record = new stdClass();
$record->course = $course1->id;
$resource1 = self::getDataGenerator()->create_module('resource', $record);
// Second resource.
$record = new stdClass();
$record->course = $course2->id;
$resource2 = self::getDataGenerator()->create_module('resource', $record);
// Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
$enrol = enrol_get_plugin('manual');
$enrolinstances = enrol_get_instances($course2->id, true);
foreach ($enrolinstances as $courseenrolinstance) {
if ($courseenrolinstance->enrol == "manual") {
$instance2 = $courseenrolinstance;
break;
}
}
$enrol->enrol_user($instance2, $student->id, $studentrole->id);
$returndescription = mod_resource_external::get_resources_by_courses_returns();
// Create what we expect to be returned when querying the two courses.
$expectedfields = array('id', 'course', 'name', 'intro', 'introformat', 'introfiles',
'contentfiles', 'tobemigrated', 'legacyfiles', 'legacyfileslast', 'display', 'displayoptions',
'filterfiles', 'revision', 'timemodified', 'section', 'visible', 'groupmode', 'groupingid');
// Add expected coursemodule and data.
$resource1->coursemodule = $resource1->cmid;
$resource1->introformat = 1;
$resource1->contentformat = 1;
$resource1->section = 0;
$resource1->visible = true;
$resource1->groupmode = 0;
$resource1->groupingid = 0;
$resource1->introfiles = [];
$resource1->contentfiles = [];
$resource2->coursemodule = $resource2->cmid;
$resource2->introformat = 1;
$resource2->contentformat = 1;
$resource2->section = 0;
$resource2->visible = true;
$resource2->groupmode = 0;
$resource2->groupingid = 0;
$resource2->introfiles = [];
$resource2->contentfiles = [];
foreach ($expectedfields as $field) {
$expected1[$field] = $resource1->{$field};
$expected2[$field] = $resource2->{$field};
}
$expectedresources = array($expected2, $expected1);
// Call the external function passing course ids.
$result = mod_resource_external::get_resources_by_courses(array($course2->id, $course1->id));
$result = external_api::clean_returnvalue($returndescription, $result);
// Remove the contentfiles (to be checked bellow).
$result['resources'][0]['contentfiles'] = [];
$result['resources'][1]['contentfiles'] = [];
// Now, check that we retrieve the same data we created.
$this->assertEquals($expectedresources, $result['resources']);
$this->assertCount(0, $result['warnings']);
// Call the external function without passing course id.
$result = mod_resource_external::get_resources_by_courses();
$result = external_api::clean_returnvalue($returndescription, $result);
// Remove the contentfiles (to be checked bellow).
$result['resources'][0]['contentfiles'] = [];
$result['resources'][1]['contentfiles'] = [];
// Check that without course ids we still get the correct data.
$this->assertEquals($expectedresources, $result['resources']);
$this->assertCount(0, $result['warnings']);
// Add a file to the intro.
$fileintroname = "fileintro.txt";
$filerecordinline = array(
'contextid' => context_module::instance($resource2->cmid)->id,
'component' => 'mod_resource',
'filearea' => 'intro',
'itemid' => 0,
'filepath' => '/',
'filename' => $fileintroname,
);
$fs = get_file_storage();
$timepost = time();
$fs->create_file_from_string($filerecordinline, 'image contents (not really)');
$result = mod_resource_external::get_resources_by_courses(array($course2->id, $course1->id));
$result = external_api::clean_returnvalue($returndescription, $result);
// Check that we receive correctly the files.
$this->assertCount(1, $result['resources'][0]['introfiles']);
$this->assertEquals($fileintroname, $result['resources'][0]['introfiles'][0]['filename']);
$this->assertCount(1, $result['resources'][0]['contentfiles']);
$this->assertCount(1, $result['resources'][1]['contentfiles']);
// Test autogenerated resource.
$this->assertEquals('resource2.txt', $result['resources'][0]['contentfiles'][0]['filename']);
$this->assertEquals('resource1.txt', $result['resources'][1]['contentfiles'][0]['filename']);
// Unenrol user from second course.
$enrol->unenrol_user($instance2, $student->id);
array_shift($expectedresources);
// Call the external function without passing course id.
$result = mod_resource_external::get_resources_by_courses();
$result = external_api::clean_returnvalue($returndescription, $result);
// Remove the contentfiles (to be checked bellow).
$result['resources'][0]['contentfiles'] = [];
$this->assertEquals($expectedresources, $result['resources']);
// Call for the second course we unenrolled the user from, expected warning.
$result = mod_resource_external::get_resources_by_courses(array($course2->id));
$this->assertCount(1, $result['warnings']);
$this->assertEquals('1', $result['warnings'][0]['warningcode']);
$this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
}
}
......@@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2016120500; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2016120501; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2016112900; // Requires this Moodle version
$plugin->component = 'mod_resource'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 0;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment