Skip to content

Instantly share code, notes, and snippets.

@dangtrinhnt
Created November 19, 2015 02:00
Show Gist options
  • Save dangtrinhnt/7d2c8050f6bf9d8bc813 to your computer and use it in GitHub Desktop.
Save dangtrinhnt/7d2c8050f6bf9d8bc813 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