Modification to gdlib.php - is this feasible?

Modification to gdlib.php - is this feasible?

by Dan Stowell -
Number of replies: 17
Hi -

In working on something related to enrolments, Antoni Mas suggested a modification to gdlib.php which allows one of the functions to take a URL argument, not just a file-path argument.

I'd like to ask if this change could feasibly be committed to the main code tree. I haven't done any other work with gdlib so there may be issues I'm unaware of. However, I'd like the IMS enrolment module to be available for use in "vanilla" installations in future, without users having to modify any Moodle system files.

The modified gdlib can be downloaded from here:
http://moodle.org/mod/forum/discuss.php?d=34351&parent=166292

Here is the diff against the current HEAD code:

85c85
< function save_profile_image($id, $uploadmanager, $dir='users') {
---
> function save_profile_image($id, $uploadmanager, $dir='users', $url=false) {
112,115d111
<    $destination = $CFG->dataroot .'/'. $dir .'/'. $id;
<    if (!$uploadmanager->save_files($destination)) {
<        return false;
<    }
117c113,122
<    $originalfile = $uploadmanager->get_new_filepath();
---
>    if (!$url) {
>      $destination = $CFG->dataroot .'/'. $dir .'/'. $id;
>      if (!$uploadmanager->save_files($destination)) {
>          return false;
>      }
>
>      $originalfile = $uploadmanager->get_new_filepath();
>    }else{
>      $originalfile = $uploadmanager;
>    }
163c168,170
<    unlink($originalfile);
---
>    if (!$url) {
>      unlink($originalfile);
>    }
Average of ratings: -
In reply to Dan Stowell

Re: Modification to gdlib.php - is this feasible?

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
I too made some modification to the gdlib.php file. I was looking for a way to do a batch import of user pics where the picname matched the userid. While not directly related I would support the changes you suggest as they provide for greater flexibility in the usage of save_profile_image function.
In reply to Anthony Borrow

Re: Modification to gdlib.php - is this feasible?

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hi Anthony,

I don't know if you are still interested in batch importing user pics, but I've recently made a patch (against 1.8.2+) that allows exactly that, but using the username instead of the userid. The change to use the later is trivial though.

There is a new admin page, 'Upload user pictures', where you upload a zip file that contains the user pictures. The users pictures are named username1.jpg, username2.jpg and so on. They don't have to be JPEG images. Any Moodle supported format will do it.

The patch if fully integrated and translatable. In case you are insterested, I'm attaching the patch here.

The attached file has two patches that have to be applied in order (self evident from the names smile).

The first one splits the file upload process apart from the picture assigning process, so any authentication plugin can set the user picture (as requested in MDL-4520, which is what sent me to this thread in the first place wink).

The second one creates the new admin menu entry to upload the zip file with the pictures.

Beware this second patch doesn't work with the new 1.8.3 and 1.9beta2 versions, as the user data directory structure has recently changed and the patch predates those changes. I plan to update the patch to cover 1.8.3 and 1.9 in the following days though.

Saludos. Iñaki.

In reply to Iñaki Arenaza

Re: Modification to gdlib.php - is this feasible?

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
Iñaki - I will take a look at the patches although I may wait to see the 1.8+ and 1.9 versions based on the new structure. Perhaps we could use something like $CFG->uploadimagename and allow that to be set to either id, username, or idnumber (since those should all be unique ways of identifying the user. I thought that the patch I had written used username but having an option seems like it might be more useful for system administrators. I think this would be a nice improvement for administrators and could easily be incorporated into Moodle core. I have created MDL-11752 and submitted your patch along with my diff file for /lib/gdlib.php as a way that it could be done. Let's work on getting 2 good diffs that we can add that might be able to be worked in to Moodle core. It seems like a good time since Moodle development team recently has been working on the user profiles. Peace - Anthony
In reply to Anthony Borrow

Re: Modification to gdlib.php - is this feasible?

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Anthony,

here are the patches for the lastes 1.8.3+ and 1.9beta2 (current as of today).

Instead of using $CFG->whatever to configure this, I think it's better to add a field to the upload form so the user can choose what user attribute wants to use to assign the pictures (this is not part of the current patch, but will add it in a day or two)..

With respect to the location of the upload image file, my patch uses an upload form, so you just upload a zip file containing the pictures, and the rest is automatic.

Saludos. Iñaki.

In reply to Iñaki Arenaza

Re: Modification to gdlib.php - is this feasible?

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Ok, so here is the patch that adds a drop down list to specify which user attribute (username, id, id number) you want to use to match users and picture files.

This is the patch against 1.8.3+ current as of today. I'll attach the one against 1.9beta2 in another message.

Saludos. Iñaki.

In reply to Iñaki Arenaza

Re: Modification to gdlib.php - is this feasible?

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Here's the one for 1.9beta2.

Saludos. Iñaki.

In reply to Iñaki Arenaza

Re: Modification to gdlib.php - is this feasible?

by Jacob Kier -
I'd like to try this out, but I'm not sure how to apply a patch... ?
In reply to Jacob Kier

Re: Modification to gdlib.php - is this feasible?

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
Jacob - Let me know what version of Moodle you are using. I have the files for Moodle 1.8+. If you send me a Moodle message with your email I can email you the files as a zip file which you could unzip into your installation. Peace - Anthony
In reply to Anthony Borrow

Re: Modification to gdlib.php - is this feasible?

by Brian Pool -
Anthony,

Could you possibly send me this patch for 1.8+ (current 1.8 release)?

Thanks in advance!

Brian Pool
In reply to Brian Pool

Re: Modification to gdlib.php - is this feasible?

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
Brian - It has been a while since I've looked at this. I think the most recent and best download for Moodle 1.8 would be located as part of MDL-11752. Let me know if you have any questions or run into any troubles with that download. Peace - Anthony
In reply to Anthony Borrow

Re: Modification to gdlib.php - is this feasible?

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Anthony,

there have been a couple of enhancements in the 1.9 code that are not present in the patch for 1.8 available as part of MDL-11752. I'm not sure if I still have the patches for 1.8 around, but if I don't I'll create a new one with all the bells and whistles and upload it to MDL-11752.

[Edit: I still have the git branch here; I'll rebase the patch on top of current 1.8 and add the missing bits]

Saludos. Iñaki.

In reply to Iñaki Arenaza

Re: Modification to gdlib.php - is this feasible?

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Done! wink

Saludos. Iñaki.

In reply to Iñaki Arenaza

Re: Modification to gdlib.php - is this feasible?

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
Iñaki - Mil gracias for updating the 1.8 version of the patch with the latest whistles and bells. Peace - Anthony
In reply to Iñaki Arenaza

Re: Modification to gdlib.php - is this feasible?

by Brian Pool -
I just did the most recent one onto our 1.87+ Moodle. Although the dropdown box is there to select what field to link the pictures on...it doesn't work. It always reverts to username.

I did go in and hardcode it to use idnumber instead of username and it works great. I only wanted you to know about it if you do a revision. Otherwise it is a great addition and I really appreciated it!

Brian