Created
January 8, 2021 11:13
-
-
Save durzo/7ea2a92646d7c8cecba16144f96cfe48 to your computer and use it in GitHub Desktop.
Workaround fix for https://github.com/moodlehq/moodle-tool_migratehvp2h5p/issues/24
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# | |
# Author: Jordan Tomkinson <jordan.tomkinson@ethinkeducation.com> | |
# | |
# This script automates editing and saving contentbank items to fix a bug in tool_migratehvp2h5p | |
# Requirements: sudo pip3 install mechanize | |
# Generate the csv by running this sql: | |
''' | |
select a.course_context_id, a.instance | |
from ( | |
select mcm.id as module_instanceid, mh5p.name, mcm.instance, ctx.id as course_context_id | |
from mdl_context ctx | |
inner join mdl_course_modules mcm on mcm.course = ctx.instanceid and contextlevel = 50 | |
inner join mdl_modules mm on mcm.module = mm.id | |
inner join mdl_h5pactivity mh5p on mcm.instance = mh5p.id | |
and mm.name = 'h5pactivity' | |
order by course_context_id) as a | |
inner join mdl_context ctx on a.module_instanceid = ctx.instanceid and ctx.contextlevel = 70 | |
inner join mdl_files fil on ctx.id = fil.contextid | |
where fil.filesize = 0 | |
and fil.mimetype = 'application/zip.h5p' | |
group by a.module_instanceid, a.instance; | |
''' | |
import sys | |
import mechanize | |
import argparse | |
import csv | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--url', required=True, help='wwwroot of the site e.g: https://somesite.com - do not use a trailing /') | |
parser.add_argument('--nosaml', action='store_true', default=False, help='use ?saml=off as part of login page') | |
parser.add_argument('--username', required=True, help='username to login with. try using: ethinkadmin') | |
parser.add_argument('--password', required=True, help='password to login with. shh its a secret') | |
parser.add_argument('--csv', required=True, help='path to csv containing contextid and contentbank id which we will fix') | |
if len(sys.argv) == 1: | |
parser.print_help() | |
sys.exit(0) | |
args = parser.parse_args() | |
if args.nosaml == True: | |
loginurl = "%s/login/index.php?saml=off" % args.url | |
else: | |
loginurl = "%s/login/index.php" % args.url | |
print("Logging in to %s" % loginurl) | |
br = mechanize.Browser() | |
br.open(loginurl) | |
#for form in br.forms(): | |
# print("Found form:") | |
# print(form) | |
br.select_form(id="login") | |
br['username'] = args.username | |
br['password'] = args.password | |
response = br.submit() | |
if response.code != 200: | |
print("Couldn't login, http code %d" % response.code) | |
sys.exit(1) | |
print("Processing csv...") | |
with open(args.csv, newline='') as csvfile: | |
reader = csv.DictReader(csvfile, fieldnames=['contextid','id']) | |
for row in reader: | |
print("Opening url %s/contentbank/edit.php?contextid=%s&plugin=h5p&id=%s" % ( args.url, row['contextid'], row['id'] )) | |
br.open("%s/contentbank/edit.php?contextid=%s&plugin=h5p&id=%s" % ( args.url, row['contextid'], row['id'] )) | |
br.select_form(id="coolh5peditor") | |
response = br.submit() | |
print("Processed contextid=%s, id=%s, http code: %d" % ( row['contextid'], row['id'], response.code ) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment