Skip to content

Commit

Permalink
feat(pencil): Added the function to remove files from sections
Browse files Browse the repository at this point in the history
  • Loading branch information
roblesjoel committed Jun 8, 2022
1 parent a0443bb commit f902a48
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lang/en/local_rsync.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@
$string['local/rsync:managefiles'] = 'Manage courses using the rsync web service';
$string['successmessage'] = 'Added file {$a->folder}{$a->file} by user {$a->username} to course id {$a->courseid} in section {$a->coursesection} now having name {$a->newname} and resource id {$a->resourceid}.';
$string['successmessage_file_upload'] = 'Added file {$a->file} by user {$a->username} to course id {$a->courseid} in section {a->coursesection} with the name {$a->newname}.';
$string['successmessage_section_visibility'] = 'Successfully {$a->visibility} section {$a->sectionnumber} in course with the id {$a->courseid} by user {$a->username}.';
$string['successmessage_section_visibility'] = 'Successfully {$a->visibility} section {$a->sectionnumber} in course with the id {$a->courseid} by user {$a->username}.';
$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}.';
46 changes: 42 additions & 4 deletions section.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ public static function set_section_visibilty_parameters(){
);
}

/**
* Returns description of method parameters
* @return external_function_parameters
*/
public static function remove_file_from_section_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 that should be deleted', VALUE_REQUIRED),
)
);
}

/**
* Lets the user set the visibilty of a section
*
Expand Down Expand Up @@ -100,16 +113,41 @@ public static function set_section_visibilty($courseid, $sectionnumber, $visibil
* @param int $courseud course id
* @param int $sectionnumber section number
*/
public static function remove_file_from_section($courseid, $sectionnumber){
public static function remove_file_from_section($courseid, $sectionnumber, $filename){
global $USER;

$params = self::validate_parameters(self::remove_file_from_section_parameters(),
array('courseid' => $courseid,
'sectionnumber' => $sectionnumber,
'filename' => $filename));

// 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);

foreach($modules as $module){
if($module->section == $sectionnumber){
break;
if($module->section == $sectionnumber && $module->name == $filename){
course_delete_module($module->cm);
}
}

return implode(';', $modules);
return get_string('successmessage_section_remove_file', 'local_rsync', array('filename' => $filename, 'sectionnumber' => $sectionnumber, 'courseid' => $courseid, 'username' => fullname($USER)));
}

/**
Expand Down

0 comments on commit f902a48

Please sign in to comment.