Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victorknust/16e5c51f705b63bdffa70a034d9a6a5e to your computer and use it in GitHub Desktop.
Save victorknust/16e5c51f705b63bdffa70a034d9a6a5e to your computer and use it in GitHub Desktop.
Update moodle user's photo programmatically
#!/usr/bin/php
<?php
// USAGE:
// $ php update_moodle_photo.php <user-id-number>
//
// to be able to run this file as command line script
define('CLI_SCRIPT', true);
// include config.php from your moodle's docroot
require_once('/var/www/moodle/config.php');
global $DB;
global $CFG;
// get the idnumber/username from the console
$idnumber = $argv[1];
// get the user by her idnumber, you can use username instead
$user = $DB->get_record('user', array('idnumber'=>$idnumber));
if (!empty($user)) {
require_once( $CFG->libdir . '/gdlib.php' );
// path to the image
$tempfile = '/path/to/photos/' . $idnumber . '.JPG';
if (file_exists($tempfile)) {
$usericonid = process_new_icon( context_user::instance( $user->id, MUST_EXIST ), 'user', 'icon', 0, $tempfile );
if ( $usericonid ) {
$DB->set_field( 'user', 'picture', $usericonid, array( 'id' => $user->id ) );
}
}
unset( $tempfile );
} else {
echo "User not found!";
}
exit;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment