Commit 8120cbd9 authored by Dan Poltawski's avatar Dan Poltawski
Browse files

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

parents 48034807 f24dcf0c
......@@ -104,4 +104,106 @@ class mod_folder_external extends external_api {
);
}
/**
* Describes the parameters for get_folders_by_courses.
*
* @return external_function_parameters
* @since Moodle 3.3
*/
public static function get_folders_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 folders in a provided list of courses.
* If no list is provided all folders that the user can view will be returned.
*
* @param array $courseids course ids
* @return array of warnings and folders
* @since Moodle 3.3
*/
public static function get_folders_by_courses($courseids = array()) {
$warnings = array();
$returnedfolders = array();
$params = array(
'courseids' => $courseids,
);
$params = self::validate_parameters(self::get_folders_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 folders in this course, this function checks users visibility permissions.
// We can avoid then additional validate_context calls.
$folders = get_all_instances_in_courses("folder", $courses);
foreach ($folders as $folder) {
$context = context_module::instance($folder->coursemodule);
// Entry to return.
$folder->name = external_format_string($folder->name, $context->id);
list($folder->intro, $folder->introformat) = external_format_text($folder->intro,
$folder->introformat, $context->id, 'mod_folder', 'intro', null);
$folder->introfiles = external_util::get_area_files($context->id, 'mod_folder', 'intro', false, false);
$returnedfolders[] = $folder;
}
}
$result = array(
'folders' => $returnedfolders,
'warnings' => $warnings
);
return $result;
}
/**
* Describes the get_folders_by_courses return value.
*
* @return external_single_structure
* @since Moodle 3.3
*/
public static function get_folders_by_courses_returns() {
return new external_single_structure(
array(
'folders' => 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'),
'revision' => new external_value(PARAM_INT, 'Incremented when after each file changes, to avoid cache'),
'timemodified' => new external_value(PARAM_INT, 'Last time the folder was modified'),
'display' => new external_value(PARAM_INT, 'Display type of folder contents on a separate page or inline'),
'showexpanded' => new external_value(PARAM_INT, '1 = expanded, 0 = collapsed for sub-folders'),
'showdownloadfolder' => new external_value(PARAM_INT, 'Whether to show the download folder button'),
'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/folder:view',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
'mod_folder_get_folders_by_courses' => array(
'classname' => 'mod_folder_external',
'methodname' => 'get_folders_by_courses',
'description' => 'Returns a list of folders in a provided list of courses, if no list is provided all folders that
the user can view will be returned. Please note that this WS is not returning the folder contents.',
'type' => 'read',
'capabilities' => 'mod/folder:view',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
);
......@@ -109,6 +109,124 @@ class mod_folder_external_testcase extends externallib_advanced_testcase {
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}
}
/**
* Test test_mod_folder_get_folders_by_courses
*/
public function test_mod_folder_get_folders_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 folder.
$record = new stdClass();
$record->course = $course1->id;
$folder1 = self::getDataGenerator()->create_module('folder', $record);
// Second folder.
$record = new stdClass();
$record->course = $course2->id;
$folder2 = self::getDataGenerator()->create_module('folder', $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_folder_external::get_folders_by_courses_returns();
// Create what we expect to be returned when querying the two courses.
$expectedfields = array('id', 'course', 'name', 'intro', 'introformat', 'introfiles', 'revision', 'timemodified',
'display', 'showexpanded', 'showdownloadfolder', 'section', 'visible', 'groupmode', 'groupingid');
// Add expected coursemodule and data.
$folder1->coursemodule = $folder1->cmid;
$folder1->introformat = 1;
$folder1->section = 0;
$folder1->visible = true;
$folder1->groupmode = 0;
$folder1->groupingid = 0;
$folder1->introfiles = [];
$folder2->coursemodule = $folder2->cmid;
$folder2->introformat = 1;
$folder2->section = 0;
$folder2->visible = true;
$folder2->groupmode = 0;
$folder2->groupingid = 0;
$folder2->introfiles = [];
foreach ($expectedfields as $field) {
$expected1[$field] = $folder1->{$field};
$expected2[$field] = $folder2->{$field};
}
$expectedfolders = array($expected2, $expected1);
// Call the external function passing course ids.
$result = mod_folder_external::get_folders_by_courses(array($course2->id, $course1->id));
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedfolders, $result['folders']);
$this->assertCount(0, $result['warnings']);
// Call the external function without passing course id.
$result = mod_folder_external::get_folders_by_courses();
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedfolders, $result['folders']);
$this->assertCount(0, $result['warnings']);
// Add a file to the intro.
$fileintroname = "fileintro.txt";
$filerecordinline = array(
'contextid' => context_module::instance($folder2->cmid)->id,
'component' => 'mod_folder',
'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_folder_external::get_folders_by_courses(array($course2->id, $course1->id));
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertCount(1, $result['folders'][0]['introfiles']);
$this->assertEquals($fileintroname, $result['folders'][0]['introfiles'][0]['filename']);
// Unenrol user from second course.
$enrol->unenrol_user($instance2, $student->id);
array_shift($expectedfolders);
// Call the external function without passing course id.
$result = mod_folder_external::get_folders_by_courses();
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedfolders, $result['folders']);
// Call for the second course we unenrolled the user from, expected warning.
$result = mod_folder_external::get_folders_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_folder'; // 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