Hack to Allow Non-Admins to Upload Users via CSV File

Hack to Allow Non-Admins to Upload Users via CSV File

by Marty Jacobs -
Number of replies: 2
This isn't an enrolment plugin as such, but I though some of you in the Moodle community might find this information useful.

This hack allows users with the role "moodle:site/uploadusers" to use the upload users facility even if they're not an admin. The hack duplicates the upload users page at the course level to achieve this. I've tried to change as little as possible to make this a workable hack.

1. Make a copy of "uploaduser.php" (found in "moodle/admin) and put it in "moodle/courses".

2. Comment out the following if statement in "uploaduser.php" to stop the script returning an error if a default field is missing from the file:

foreach ($required as $key => $value) {
if ($value) { //required field missing
#error(get_string('fieldrequired', 'error', $key), 'uploaduser.php?sesskey='.$USER->sesskey);
}
}

3. Add the following code to the file (best to put it near the top):

$id = required_param('id', PARAM_INT); // course id to import TO

if (!$course1 = get_record('course', 'id', $id)) {
error("That's an invalid course id");
}

4. You also need to change this line:

require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_SYSTEM, SITEID));

So that it looks like this:

require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_COURSE, $id));

5. Next, find the section of the code that deals with the header (admin_externalpage_print_header($adminroot);) and replace with the following code:

print_header($course1->fullname.': '.$upusers, $course1->fullname.': '.$upusers,'<a href="view.php?id='.$course1->id.'">'.$course1->shortname.'</a> -> '.$upusers);

6. Find the code that prints the form (near the bottom):

echo '<form method="post" enctype="multipart/form-data" action="uploaduser.php"><div>'.

Change the action type to ""uploaduser.php?id='.$course1->id.'".

7. Install the "Add/Update Users" block (download it here). This block only displays if a user has the permission to upload users. It displays a link to the upload users page and a link to a help file, which contains some basic information about how to use the page.

Don't think I've missed any steps out, but best to try the hack out on a test server first if you intend to use it in a production environment.

Enjoy,

Marty
Average of ratings: -
In reply to Marty Jacobs

Re: Hack to Allow Non-Admins to Upload Users via CSV File

by Pedro Almeida -

Dear Mr. Marty,

I am interested in your example. We also must insert users in Moodle but the user that is inserting may be a non-admin. We are trying to change the file uploaduser.php so the options that we want, are hidden. They can insert users and teachers but with certain conditions: just to be able to insert and not update.

I have tried it but my version of Moodle is 1.9. So the lines you say we must comment and change were not the same as the old ones in you example.

For instance: "require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_SYSTEM, SITEID));" in my code is "require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_SYSTEM));" (without the ", SITEID").

Do you have a version of uploaduser.php working for version 1.9 of Moodle?

If so can you email it to me or post-it in here? (I am attaching 1.9 uploaduser.php so you can see what I mean)
Thanks in advance.

In reply to Pedro Almeida

Re: Hack to Allow Non-Admins to Upload Users via CSV File

by Marty Jacobs -
Hi Pedro,

Apologies for not replying sooner, I've been a bit lazy lately and haven't been checking these forums as often as I should!

My original post was a little brief, so I've finally packaged the block up properly. You can download it from moodle.martyjacobs.net/blocks.

This is a pretty ugly hack, but it gets the job done and works with 1.9 as well as 1.8. Let me know if you have any problems installing it.

Happy Moodling,

Marty