Skip to content

Instantly share code, notes, and snippets.

@arozellia
arozellia / Law-of-Demeter-with-PHP.md
Created February 10, 2021 04:49 — forked from k1paris/Law-of-Demeter-with-PHP.md
Law of Demeter with PHP (LoD)

Law of Demeter with PHP (LoD)

Law of Demeter or principle of least knowledge is a design guideline, that is using to make code more simple and stable.

Rules for module

  • Each unit should have only limited knowledge about other units: only units "closely" related to the current unit.
  • Each unit should only talk to his friends; don't talk to strangers.
  • Only talk to your immediate friends.
@arozellia
arozellia / reset_increments.sql
Created April 18, 2018 04:34
Reset lost mysql auto increments
SELECT tab.table_name,
consts.column_name,
Concat("ALTER TABLE ", tab.table_name , " MODIFY ", consts.column_name , " INT AUTO_INCREMENT ;") AS sqlquery
FROM information_schema.tables tab
INNER JOIN
(
SELECT kc.column_name,
tc.table_schema,
tc.table_name
FROM information_schema.table_constraints tc
@arozellia
arozellia / moodle_list_admins.sql
Created February 8, 2018 17:07
List all current Moodle admins via SQL.
SELECT
u.id, u.auth, u.username, c.value
FROM
mdl_config c
CROSS JOIN
mdl_user u
WHERE
c.name = 'siteadmins'
AND FIND_IN_SET(u.id, c.value) <> 0;
@arozellia
arozellia / moodle_users.sql
Created February 8, 2018 16:06
Create and remove Moodle Users via MySQL Commands
/**
Create user.
Although this is the insert query, it is almost always better to use a web service rather than doing this manually. This way enforces Moodle standards and ensures quality data. Use at your own risk.
Webservice example: https://stackoverflow.com/questions/35881584/using-moodle-create-users-and-enroll-them-in-courses-via-sql
Code typically used: https://github.com/moodle/moodle/blob/a409707794cc6d74063787d27adb42154426c803/user/lib.php#L42
Note: The password is set using MD5, however Moodle dutifully transforms this to a much more secure password on initial login.
See: https://github.com/moodle/moodle/blob/06e3b6d8bab42b8e56d169d006f31f4a15684830/auth/manual/auth.php#L100
See: https://github.com/moodle/moodle/blob/a4f914b54dbafcaf1cc2bf1cce8bde30cc69db57/lib/moodlelib.php#L4575
@arozellia
arozellia / moodle_no_role.sql
Last active February 8, 2018 17:09
Find Moodle users enrolled with no role
SELECT
u.id,
u.idnumber,
u.firstname,
u.lastname,
u.email,
c.fullname,
c.shortname AS courseshortname,
r.id AS roleid,
r.shortname AS roleshortname
@arozellia
arozellia / moodle_subscriptions.sql
Created November 5, 2017 20:22
Find Moodle Forum Subscriptions
select u.firstname,u.lastname, c.fullname as course, f.name as forum
from mdl_forum_subscriptions s
inner join mdl_forum f on f.id = s.forum
inner join mdl_course c on c.id = f.course
inner join mdl_user u on u.id = s.userid
where forcesubscribe = 0 # 0 = Optional subscription, 1 = Forced subscription, 2 = Auto subscription, 3 = Subscription disabled
order by c.fullname, f.name;
@arozellia
arozellia / gitstatus.py
Created August 9, 2017 21:30
Check the git status of multiple directories. Raw
"""
This script checks all directories in a given location for a git folder and runs git status.
Update debug IP to set to your local server IP for testing if on a development machine.
"""
import os
import sys
import socket
import git
@arozellia
arozellia / moodlegoogleconnection.php
Created July 3, 2017 19:44
Example of connecting to Google OAUTH in Moodle
<?php
/** @var TYPE_NAME $scopelist */
$scopelist = [
'https://www.googleapis.com/auth/admin.directory.user',
'https://www.googleapis.com/auth/admin.directory.user.readonly'
];
/** @var TYPE_NAME $gclassconfig */
$gclassconfig = get_config("local_google_class");
@arozellia
arozellia / moodlegooglepull.php
Last active July 3, 2017 19:41
Example of retrieving users in Moodle with Google OAUTH
<?php
echo 'Connecting to Google...' . "\n";
$dir = new \Google_Service_Directory($this->client);
/** @var TYPE_NAME $nexttoken */
$nexttoken = '';
/** @var TYPE_NAME $users */
$usersfulllist = [];