Skip to content

Instantly share code, notes, and snippets.

Avatar
🌹
"you're right, no human being would stack books like this"

Eric Phetteplace phette23

🌹
"you're right, no human being would stack books like this"
View GitHub Profile
@phette23
phette23 / unenroll-students.sh
Last active Jun 11, 2021
unenroll all students from Moodle course
View unenroll-students.sh
# used for instance when First Year wants to drop sophomores from their home & replace with new incoming students
# COURSE = course id
USERS=$(moosh -n user-list --course $COURSE --course-role student)
moosh -n course-unenrol $COURSE $USERS
@phette23
phette23 / npm-lsg-unlinked.js
Created May 25, 2021
list global npm packages that are not linked
View npm-lsg-unlinked.js
#!/usr/bin/env node
const { exec } = require("child_process")
exec('npm ls --global --json', (err, stdout, stderr) => {
if (err) throw err
const deps = JSON.parse(stdout).dependencies
// dependencies hash looks like:
// "linked-pkg": { "version": "1.0.0", "resolved": "file:..." },
// "global-pkg": { "version": "1.0.0" }, ...
@phette23
phette23 / hide-pdf-urls.js
Last active Apr 19, 2021
hide 856$u PDFs in Koha if users aren't logged in
View hide-pdf-urls.js
// run 1) on opac-detail pages & 2) if no user is signed in
if (!!location.pathname.match('/cgi-bin/koha/opac-detail.pl') && !$('.loggedinusername').length) {
// replace 856$u links with a link to login instead
// this would need to be tweaked if there are multiple URLs per record
$('.results_summary.online_resources a')
.replaceWith('<a href="/cgi-bin/koha/opac-user.pl">login to view PDF</a>')
}
@phette23
phette23 / db-table-size.sql
Created Mar 31, 2021
get size of mysql tables in a database
View db-table-size.sql
SELECT table_name AS "Table",
ROUND(((data_length + index_length) / 1024 / 1024), 2) AS "Size (MB)"
FROM information_schema.TABLES
-- replace DATABASE with the name of the db
WHERE table_schema = 'DATABASE'
ORDER BY (data_length + index_length) DESC
@phette23
phette23 / files.py
Created Mar 30, 2021
iterate over list of files & write information about them (including MIME type) to CSV
View files.py
#!/usr/bin/env python3
import csv
import os
import subprocess
with open('files.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['path', 'size (bytes)', 'time last accessed', 'time last modified', 'time created', 'mime type'])
with open('dbcheck-files.txt', 'r') as listfile:
for path in listfile:
@phette23
phette23 / download.fish
Last active Mar 19, 2021
bulk download Archive-It WARC files
View download.fish
#!/usr/bin/env fish
set USER username
set PASS password
set COLLECTION 123456
set JSONFILE data.json
set URLSFILE urls.txt
set DONEFILE done.txt
set LIMIT 8
@phette23
phette23 / delete-empty-subfolders.py
Last active Jan 14, 2021
use Panopto API to delete all empty subfolders of given folder
View delete-empty-subfolders.py
#!python3
import sys
import argparse
import requests
import urllib3
from panopto_folders import PanoptoFolders
from os.path import dirname, join, abspath
sys.path.insert(0, abspath(join(dirname(__file__), '..', 'common')))
@phette23
phette23 / bulk-template.sh
Created Dec 21, 2020
bulk Moodle template restore
View bulk-template.sh
# NOTE: make sure the course-list query returns the courses you want and also that the right backup is
# referenced in the course-restore command. The query should probably use shortnames (e.g. example is
# all First Year 4D courses), as opposed to something like categories, because you can target metacourses.
for id in $(moosh -n course-list -i 'shortname LIKE "FYCST-1120%-2021SP"'); do
moosh -n course-restore --overwrite ~/backup-*.mbz $id;
done
@phette23
phette23 / blocks-with-duplicates.sql
Last active Dec 4, 2020
Moodle - delete duplicate blocks in courses
View blocks-with-duplicates.sql
SELECT b.id, b.blockname, b.parentcontextid, c.id, c.shortname
FROM {prefix}_block_instances b
JOIN (SELECT * FROM {prefix}_context WHERE contextlevel = 50) ctx ON b.parentcontextid = ctx.id
JOIN {prefix}_course c ON c.id = ctx.instanceid
-- this list of block types might need to be changed depending on Moodle instance
WHERE b.blockname IN ('search_forums', 'recent_activity', 'calendar_upcoming', 'news_items')
-- must be a better way but I just look for blocks in courses we know have duplicates
-- so we use the other query as a sub-query
AND c.id IN (
SELECT c.id
@phette23
phette23 / fix-drive-file-modes.sh
Created Oct 9, 2020
fix files modes in Drive git repos
View fix-drive-file-modes.sh
#/usr/bin/env bash
# Google Drive messes up file modes by setting everything to 644 so all your executables are screwed up
# I store a bunch of git repos in a Drive folder (don't ask why) & this fixes the file modes by just checking out everything
for dir in $(ls -d */); do
cd ${dir}
git status 2&>/dev/null && git checkout -- .
cd -
done