"Student Adder"

"Student Adder"

by Richie Foreman -
Number of replies: 24
Hey there,

Lately I've been developing some code that allows for teachers to add students to their course.  I'm also developing several other tweaks and customizations for moodle, that I plan to post at a later date.  I've made the decision that the current "Student Adder" code is clean enough and 'mature' enough to be released to those of you who wish to use it.  Right now I have only coded it to handle "manual" enrollments, but have plans to write in support for an external MySQL dB (which is what we plan to use).

Here's how it works:
  1. Teacher navigates to the student button on the admin sidebar
  2. Student adder asks how many new students need to be added.
  3. It then displays a series of textboxes, and name input is in the following format: "Richie<SPACE>Foreman"
  4. The data from the text boxes is parsed and then usernames are added to the moodle system in the following format: (rforeman, rforeman1, rforeman3).  Also, a password is generated for the user in the following format: ((username)(3digit random number)).Then, the newly created user is enrolled in the current course.  Lastly, the page echoes out all the new usernames and passwords.
It's relatively simple in concept, but it gets the job done.

Here's how to install:

Open up moodle/course/student.php

Near the bottom you'll notice this code:
    print_simple_box_start("center");
    $sesskey = !empty($USER->id) ? $USER->sesskey : '';
    include('student.html');
    print_simple_box_end();

Right above that add these following lines:
        print_simple_box_start("center");
        $inmoodle = 1;
        include_once('studentadder.php');
        print_simple_box_end();

Drop the attached file into moodle/course/ directory.

Thanks,

Richie Foreman
Mesa Public Schools, Mesa Arizona.
Average of ratings: Useful (1)
In reply to Richie Foreman

Re: "Student Adder"

by Geof Duncan -
Many, many thanks, Richie!!

I'm sure many of us will be using this!!

One comment to all interested, don't forget to edit the studentadder.php file to change the city, etc!!
In reply to Richie Foreman

Re: "Student Adder"

by Geof Duncan -
Richie,

In your code you mentioned that we could modify this as needed so... blush

I've added a table where teachers input their requested Student ID, First Name, and Last Name.

From there the ID is checked against the database (modified if necessary by adding random numbers to the end) and the student is enrolled in the class exactly like your script did. I also set a default password of CHANGEME since this will force students to change their password at first login.

I tested this in Moodle 1.5 and it works for me, feel free to make any changes you feel are appropriate. Thanks again Richie, your mod was what I needed to get me started!!

Here's how to install:

Open up moodle/course/student.php

Near the bottom you'll notice this code:
    print_simple_box_start("center");
    $sesskey = !empty($USER->id) ? $USER->sesskey : '';
    include('student.html');
    print_simple_box_end();

Right above that add these following lines:
        print_simple_box_start("center");
        $inmoodle = 1;
        include_once('newstudent.php');
        print_simple_box_end();

Drop the attached file into moodle/course/ directory.


screenie1

screenie2

screenie3
In reply to Geof Duncan

Re: "Student Adder"

by Thom Stricklin -
Good thinking.  Just sparked a thought...  What if a student was already enrolled in another course?  There's really no need to set up another account.  Maybe, with IDs specified, Student Adder can look to see if the username exists and, if so, it doesn't create a new one but enrolls that student into the course.

I'm working with Richie on our Moodle implementation, though he's much more familiar with the Moodle source than I.  I won't put words in his mouth, but he'll probably have a revision up tomorrow.  ;)
In reply to Geof Duncan

Re: "Student Adder"

by Richie Foreman -
Geof,

I looked at your code and found a serious vulnerability in your modification of code. Accessing your moodle/course/newstudent.php presents a login form, in which students can login. Obviously this is a big vulnerability because students can use student adder.  (try visiting newstudent.php and logging in as a student, and see what happens).

require_once("../config.php");

require_login();


Those two lines of code are disasterous, because you are not specifying which type of login is required. And actually, because this page is included inside of another page (which includes config.php), you don't actually need to include the config.php. However, wrap the entire code with this. This will keep intruders away!

<?
if ($inmoodle == 1) {
THE REST OF THE CODE
}
?>

--Richie Foreman
Mesa Public Schools, Mesa Arizona.
In reply to Richie Foreman

Re: "Student Adder"

by Geof Duncan -
Richie,

Thanks for the catch... I've had my Moodle in maintenance mode and didn't catch that one... surprise

I've made the corrections you recommended and reposted the file. Happy Moodling everyone!!

-Geof
In reply to Geof Duncan

Re: "Student Adder"

by X MAN -

i have tried the code but there is an error after i submitted the form.

"A required parameter was missing"

In reply to X MAN

Re: "Student Adder"

by Richie Foreman -
Are you using my version?  Or are you using the Geof's version?  Also, for the record:  Lately we've done a total recode of "Student Adder" that supports LDAP, dB, Manual, and any other authentication method that has functions to create a user.  I plan to release this "total recode" soon.
In reply to Richie Foreman

Re: "Student Adder"

by Colin McQueen -
Using Moodle 1.53 (plus some other hacks and additions) I also get this error with both yours and Geof's versions. Any ideas
In reply to Richie Foreman

Re: "Student Adder"

by Art Lader -
Hi, Richie,

Any news on the new student adder rewrite?

-- Art
In reply to Geof Duncan

Re: "Student Adder"

by Art Lader -
Hi, Geof,

This is working very nicely for me on a couple of 1.5.3 installations, but in 1.6 beta 3, nothing happens when I submit the number of students I want to add.  After I hit the submit button, the number I have entered changes back to zero, but nothing else happens.

Maybe this is useful to know.

Thanks for this very useful plugin.

Screencast here.

Regards,
Art


In reply to Art Lader

Re: "Student Adder"

by Jan Dierckx -

Does adding the following line

$CFG->enableglobalshack = true;

to your config.php fix it?

In reply to Jan Dierckx

Re: "Student Adder"

by Art Lader -
I will try it and get right back to you.

Thanks,
Art
In reply to Art Lader

Re: "Student Adder"

by Art Lader -
Yes, that did it! Woohoo!

Thank you, Jan. That is about the tenth time you have helped me out like that.

One day, I would really like to buy you a beer. smile

-- Art
Attachment woohoo_.gif
In reply to Art Lader

Re: "Student Adder"

by Jan Dierckx -

Accepted! Thanks! big grin

But ... better buy one for Richie as well. wink

The globals hack may leave things working for a while, but to make this nice addon really compatible with Moodle 1.6 all of the code should be converted to using optional_param() and required_param() for all parameters.

In reply to Jan Dierckx

Re: "Student Adder"

by Art Lader -
Is that something I could do with a simple search and replace?

-- Art
In reply to Art Lader

Re: "Student Adder"

by Jan Dierckx -

mmm, I am not sure what exactly all of the variables are for (and you need to supply that information to the parameter functions so they can check if everything is secure).

I guess it's better to leave it to the developer...

In reply to Richie Foreman

Re: "Student Adder"

by Martín Langhoff -
Interesting stuff!

There's code in 1.6/HEAD that, if the user has an email address, and you add a preference to the user account (create_password = 1), Moodle will create a new password using Moodle's internal password creation routines. It might be a good/useful alternative -- take a look at admin/cron.php, search for 'password'. That's what the new Student Upload CSV thingy uses.

Now... this code kind of breaks the rules a bit -- teachers can't create accounts. Only admins can, and the users themselves. This is for a good reason: you don't always trust teachers to not spam random people. Spamming annoys people and gets hosting contracts terminated. That's why it is left to the admin account to do that wink

Average of ratings: Useful (1)
In reply to Martín Langhoff

Re: "Student Adder"

by Thom Stricklin -
Martín,

Richie could probably answer this better than I, and I'm not sure about Geoff's or Art's variations, but I believe Student Adder (and the new version, FAST! Adder) default to a nonfunctional e-mail address (i.e. noreply@localhost.localdomain or the like).  The idea is that teachers will physically deliver passwords to students, in a real classroom environment, so there's no e-mail exchange.

So...  I *think* despite breaking the rules a bit, I don't think it could cause any adverse effects, not without modification.
In reply to Richie Foreman

Re: "Student Adder"

by Richie Foreman -
Hey there guys,

There is a new version that we've been testing down on our server at work. We've revamped the way it works, and it now supports the moodle enrollment plugins (yes, it works with LDAP, External dB, etc). We've also given it a new name: "FAST: FAST Adds Students Today!". We've also eliminated the "Number of students to add" bit, it just seemed a little hacky. I've also cleaned up my coding methods, so the code is just overall better.

The way it works has kinda changed, so I will post a little "flowchart" of how EXACTLY it works.

I'll be sure to post the new version when I get to work Monday afternoon.

Oh, and how about instead of a beer, a soda?  I'm not 21 yet, hahahahh =X

--Richie Foreman
Mesa Public Schools
Mesa, AZ
In reply to Richie Foreman

Re: "Student Adder"

by Art Lader -
A soda? You bet! One for Geof, too, if he prefers that to beer.

Very much looking forward to the new FAST plugin.

-- Art


In reply to Richie Foreman

Re: "Student Adder"

by Thom Stricklin -
One thing that's been holding us back from releasing FAST is that, for full functionality, we had to make changes to some core files, specifically auth/db/lib.php.  In fact we meant to ask about this a while ago, but got sidetracked.

We created two new functions: auth_user_exists and auth_user_create, which take the same input and behave more or less the same way as the ldap versions of the functions.  To complicate matters, I noticed afterwards that someone already contributed a version of auth_user_exists to CVS, and it appears to have different functionality.  (As a matter of fact, I'm stumped as to what it's supposed to actually do.)

So I hesitate to release FAST and its core file changes without first cleaning up the discrepancy between these functions.

If it helps, I'm including my versions of the functions here:

function auth_user_exists($username){
// Checks to see if user exists in database
// Could have problems with ADO and some misc. ODBC dBs.

    global $CFG;

    $config = (array) $CFG;

    $pcfg = get_config('auth/db');
    $pcfg = (array) $pcfg;

    ADOLoadCode($CFG->auth_dbtype);         
    $authdb = &ADONewConnection();        
    $authdb->PConnect($CFG->auth_dbhost,$CFG->auth_dbuser,$CFG->auth_dbpass,$CFG->auth_dbname);

    $rs = $authdb->GetOne("SELECT count(*) FROM $CFG->auth_dbtable
                                       WHERE $CFG->auth_dbfielduser = '$username'");
    if ($rs > 0)
          $result = true;
    else $result = false;

    return $result;
}

function auth_user_create($username,$password){
// Creates a user in the database and returns true.
// If student can't be added (field error, read only), returns false

    global $CFG;

    //$config = (array) $CFG;

    $pcfg = get_config('auth/db');
    $pcfg = (array) $pcfg;

    ADOLoadCode($CFG->auth_dbtype);         
    $authdb = &ADONewConnection();        
    $authdb->PConnect($CFG->auth_dbhost,$CFG->auth_dbuser,$CFG->auth_dbpass,$CFG->auth_dbname);

    switch ($CFG->auth_dbpasstype) {   // Re-format password accordingly
        case "md5":
            $password = md5($password);
        break;
    }
 
    return $authdb->Execute("INSERT INTO $CFG->auth_dbtable
                            ($CFG->auth_dbfielduser,$CFG->auth_dbfieldpass)
                            VALUES ('$username','$password')");

}
In reply to Richie Foreman

Re: "Student Adder"

by Deleted user -

Hi Richie,
I've been following the posts on using the Add Student from student.php and I'm looking for a solution to a problem I have that you maybe able to help with.

Often times students enroled in one course need to be added to another course and this can be done simply by adding the course code to the email address, however when a student puts in their home email address the code vanishes.

This is using the current search and searching on the course code within the email. What I want to be able to do is add the course code to the students profile idnumber then hide it.
My problem is that whilst I can display the u.idnumber in the search simple box it won't search on u.idnumber.

Would you have any idea how I can achieve this, currently the search is only on id, firstname, lastname and email.

Searching on the code would then bring up all students from one course that could be highlighted and added enblock to another.

Regards

Gerald

In reply to Richie Foreman

Student Adder lang problem

by berkay çelik -

hello thanks for student adder,

but i have had a problem, when i add students with studentadder block it's really works but after that the student (who is added by studentadder)signs my moodle page and the lang is not supported.i have the Turkish lang packet but it doesn't support. Some characters are appeared differently.

The other users uploaded from external database or flat file have no  lang problems.

can you make this clear?