Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download submissions only #135

Open
1 task done
Rider3268 opened this issue Feb 4, 2022 · 1 comment
Open
1 task done

Download submissions only #135

Rider3268 opened this issue Feb 4, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@Rider3268
Copy link

Rider3268 commented Feb 4, 2022

Avoid duplicates

  • This enhancement request has not already been raised before

Is your feature request related to a problem? Please describe.

Often at the end of a course I need to send all my assignments' submissions to the lecturer/professor for some kind of report or something similar to it. Even with Moodle Downloader I need to manually delete all the lectures, tasks information etc. and leave submissions only.

Describe the solution you'd like

Moodle Downloader has an ability to download course content with or without submissions, but having an option to download course submissions only would be great.

Describe alternatives you've considered

None

Additional context

None

@Rider3268 Rider3268 added the enhancement New feature or request label Feb 4, 2022
@C0D3D3V
Copy link
Owner

C0D3D3V commented Feb 7, 2022

You can use the following script:

import zipfile
import os
import sys
import pathlib

def create_zip(course_path, maintain_hierarchy = False):
    ziped_submissions_path = os.path.join(course_path, 'submissions.zip')
    # creating zip file with write mode
    zip_file = zipfile.ZipFile(ziped_submissions_path, 'w', zipfile.ZIP_DEFLATED)
    # Walk through the files in a directory
    for dir_path, dir_names, files in os.walk(course_path):
        f_path = dir_path.replace(course_path, '')
        if f_path.find('submissions') < 0:
            continue
        # Writing each submission file into the zip
        for file_name in files:
            if not os.path.isfile(os.path.join(dir_path, file_name)):
                continue
            
            if not maintain_hierarchy:
                parsed_path = pathlib.Path(f_path)
                if len(parsed_path.parts) > 2:
                    if parsed_path.parts[0] in ['/', '\\']:
                        f_path = pathlib.Path(*parsed_path.parts[2:])
                    else:
                        f_path = pathlib.Path(*parsed_path.parts[1:])
            zip_file.write(os.path.join(dir_path, file_name), os.path.join(f_path, file_name))
    zip_file.close()
    print("Zip Created successfully..")
    return ziped_submissions_path

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print('Please enter as first argument the path of the course from which you want to zip all submissions')
        print('You can specify the path in quotes like this: python zip_submissions.py "/home/user/moodle/Machine Learning and Security/"')
        exit(1)

    if not os.path.isdir(sys.argv[1]):
        print('The specified path does not point to an existing folder')
        exit(1)

    maintain_hierarchy = False
    create_zip(sys.argv[1], maintain_hierarchy)

Also available here: https://gist.github.com/C0D3D3V/cd0f4530c7178c01943d8a802daf9aa3

This creates a zip that looks like this when called with python zip_submissions.py "/home/user/path/to/your/course/".
grafik

Or if you set maintain_hierarchy to True:

grafik

You can of course adjust the folder structure as you like with small changes. (For example, you could remove the submissions folder from parsed_path.parts to have an even more flatter hierarchy, or just use zip_file.write(os.path.join(dir_path, file_name), file_name) (line 27) to save all submissions directly in the zip without subfolders.
Of course, the feedback PDFs from the teacher are always included.

In general I find the idea interesting to allow such granular settings that you can download for example only your own submissions and nothing else. It's just a bit strange and not the normal use case. And would mean some restructuring, currently this is not so easy to do with moodle-dl without breaking a lot, but you can easily customize moodle-dl to download only the submissions. By either adjusting the filter https://github.com/C0D3D3V/Moodle-Downloader-2/blob/e005f30bb18672b186d79e587eba0b62974fd6f4/moodle_dl/moodle_connector/moodle_service.py#L408

or change what is downloaded at all https://github.com/C0D3D3V/Moodle-Downloader-2/blob/e005f30bb18672b186d79e587eba0b62974fd6f4/moodle_dl/moodle_connector/moodle_service.py#L243

these are of course only quick and dirty changes. If you want to implement this properly so that it works properly and is configurable, feel free to make a pullrequest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants