C0D3D3V / Moodle-DL

Moodle-DL downloads course content fast from Moodle (eg. lecture pdfs)
GNU General Public License v3.0
433 stars 63 forks source link

Download submissions only #135

Open Rider3268 opened 2 years ago

Rider3268 commented 2 years ago

Avoid duplicates

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

C0D3D3V commented 2 years ago

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.