From b25ba15b3605e4c2033c7eae4af628ce5371a611 Mon Sep 17 00:00:00 2001 From: Joel Robles Date: Thu, 9 Jun 2022 17:35:26 +0200 Subject: [PATCH] feat(pencil):Added the possiblity to change file visibility --- db/services.php | 9 ++++- lang/en/local_rsync.php | 4 +- section.php | 87 ++++++++++++++++++++++++++++++++++++++++- version.php | 2 +- 4 files changed, 98 insertions(+), 4 deletions(-) diff --git a/db/services.php b/db/services.php index 8d1b066..5e82625 100644 --- a/db/services.php +++ b/db/services.php @@ -61,13 +61,20 @@ 'classpath' => 'local/rsync/section.php', 'description' => 'Allows you to remove a section', 'type' => 'write', + ), + 'local_rsync_set_file_visibility' => array( + 'classname' => 'local_rsync_section', + 'methodname' => 'set_file_visibility', + 'classpath' => 'local/rsync/section.php', + 'description' => 'Allows you to set the visibility of a single file', + 'type' => 'write', ) ); // We define the services to install as pre-build services. A pre-build service is not editable by administrator. $services = array( 'rsync fucntionalities' => array( - 'functions' => array ('local_rsync_create_file_resource', 'local_rsync_set_section_visibility', 'local_rsync_remove_file_from_section', 'local_rsync_rename_section', 'local_rsync_remove_section'), + 'functions' => array ('local_rsync_create_file_resource', 'local_rsync_set_section_visibility', 'local_rsync_remove_file_from_section', 'local_rsync_rename_section', 'local_rsync_remove_section', 'local_rsync_set_file_visibility'), 'restrictedusers' => 0, 'enabled' => 1, ) diff --git a/lang/en/local_rsync.php b/lang/en/local_rsync.php index 3d66a62..05787e7 100644 --- a/lang/en/local_rsync.php +++ b/lang/en/local_rsync.php @@ -34,5 +34,7 @@ $string['successmessage_section_remove_file'] = 'Successfully removed file {$a->filename} in section {$a->sectionnumber} in the course with the id {$a->courseid} by user {$a->username}.'; $string['successmessage_section_rename'] = 'Successfully renamed section with the id {$a->sectionnumber} to {$a->newsectionname} in the course with the id {$a->courseid} by user {$a->username}.'; $string['successmessage_section_remove'] = 'Successfully removed section with the id {$a->sectionnumber} in the course with the id {$a->courseid} by user {$a->username}.'; +$string['successmessage_section_file_visibility'] = 'Successfully {$a->visibility} file with the name {a->filename} in section with the id {$a->sectionnumber} in the course with the id {$a->courseid} by user {$a->username}.'; -$string['errormessage_section_rename'] = 'An error occured while removing section with the id {$a->sectionnumber} in the course with the id {$a->courseid} by user {$a->username}.'; \ No newline at end of file +$string['errormessage_section_rename'] = 'An error occured while removing section with the id {$a->sectionnumber} in the course with the id {$a->courseid} by user {$a->username}.'; +$string['errormessage_section_file_visibility'] = 'An error occured while changing visiblity of file with the name {$a->filename} in section with the id {$a->sectionnumber} in the course with the id {$a->courseid} by user {$a->username}.'; \ No newline at end of file diff --git a/section.php b/section.php index daf8f6c..bf8f020 100644 --- a/section.php +++ b/section.php @@ -70,7 +70,7 @@ public static function rename_section_parameters(){ ); } - /** + /** * Returns description of method parameters * @return external_function_parameters */ @@ -81,6 +81,20 @@ public static function remove_section_parameters(){ ) ); } + + /** + * Returns description of method parameters + * @return external_function_parameters + */ + public static function set_file_visibility_parameters(){ + return new external_function_parameters( + array('courseid' => new external_value(PARAM_INT, 'The course id', VALUE_REQUIRED), + 'sectionnumber' => new external_value(PARAM_INT, 'In which section the files should be deleted', VALUE_REQUIRED), + 'filename' => new external_value(PARAM_TEXT, 'The name of the file', VALUE_REQUIRED), + 'visibility' => new external_value(PARAM_INT, 'The state of visibility to set the file in', VALUE_REQUIRED), + ) + ); + } /** * Lets the user set the visibilty of a section @@ -278,6 +292,69 @@ public static function remove_section($courseid, $sectionnumber){ } } + /** + * Lets the user set the visiblity of a file in a section + * + * @param int $courseud course id + * @param int $sectionnumber section number + * @param string $sectionname the new name of the section + * @param int $visiblity the visiblity of the file + * @return string A string describing the result + */ + public static function set_file_visibility($courseid, $sectionnumber, $filename, $visibility){ + global $USER; + + $params = self::validate_parameters(self::set_file_visibility_parameters(), + array('courseid' => $courseid, + 'sectionnumber' => $sectionnumber, + 'filename' => $filename, + 'visibility' => $visibility)); + + // Context validation. + $context = \context_user::instance($USER->id); + self::validate_context($context); + + + // Capability checking. + // OPTIONAL but in most web service it should present. + if (!has_capability('repository/user:view', $context)) { + throw new moodle_exception('cannotviewprofile'); + } + if (!has_capability('moodle/user:manageownfiles', $context)) { + throw new moodle_exception('cannotviewprofile'); + } + $coursecontext = \context_course::instance($courseid); + if (!has_capability('moodle/course:manageactivities', $coursecontext)) { + throw new moodle_exception('cannotaddcoursemodule'); + } + + $modules = get_array_of_activities($courseid); + + $foundmodule = false; + + foreach($modules as $module){ + if($module->section == $sectionnumber && $module->name == $filename){ + $foundmodule = set_coursemodule_visible($module->cm, $visibility, $visibility); + } + } + + $visibility_long = ''; + + if ($visibility == 0){ + $visibility_long = 'hidden'; + } + else{ + $visibility_long = 'unhidden'; + } + + if($foundmodule){ + return get_string('successmessage_section_file_visibility', 'local_rsync', array('visibility' => $visibility_long,'filename' => $filename, 'sectionnumber' => $sectionnumber, 'courseid' => $courseid, 'username' => fullname($USER))); + } + else{ + return get_string('errormessage_section_file_visibility', 'local_rsync', array('filename' => $filename, 'sectionnumber' => $sectionnumber, 'courseid' => $courseid, 'username' => fullname($USER))); + } + } + /** * Returns description of method result value * @return external_description @@ -309,4 +386,12 @@ public static function rename_section_returns() { public static function remove_section_returns() { return new external_value(PARAM_TEXT, 'Section number, course id and username'); } + + /** + * Returns description of method result value + * @return external_description + */ + public static function set_file_visibility_returns() { + return new external_value(PARAM_TEXT, 'Section number, course id and username'); + } } \ No newline at end of file diff --git a/version.php b/version.php index 0f4f42d..9a63621 100644 --- a/version.php +++ b/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'local_rsync'; # plugin name -$plugin->release = '0.4.0'; # plugin version +$plugin->release = '0.5.0'; # plugin version $plugin->version = 2022060200; # version number YYYYMMDDXX where XX is an incremental number $plugin->requires = 2021051700; # min moodle version, 3.11 $plugin->maturity = MATURITY_ALPHA; # MATURITY_ALPHA, MATURITY_BETA, MATURITY_RC or MATURITY_STABLE \ No newline at end of file