Skip to content

Commit

Permalink
feat(pencil):Added the possiblity to change file visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
roblesjoel committed Jun 9, 2022
1 parent d9db1e0 commit b25ba15
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
9 changes: 8 additions & 1 deletion db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
4 changes: 3 additions & 1 deletion lang/en/local_rsync.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}.';
$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}.';
87 changes: 86 additions & 1 deletion section.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static function rename_section_parameters(){
);
}

/**
/**
* Returns description of method parameters
* @return external_function_parameters
*/
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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');
}
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b25ba15

Please sign in to comment.