mysql enrolment from ecommerce program.

mysql enrolment from ecommerce program.

by Sandra King -
Number of replies: 1

Hello,

We have been using the same ecommerce program since 2007, a long time ago a programmer inserted some mysql querries into the final page of the order so that in addition to the email sent to the student to confirm the purchase, and the entries made to the ecommerce database, entries were made into the moodle datatables so that students were authorized and enrolled immediately upon registration.

When we made our upgrade to Moodle 2.0 I attempted to switch over to database authentication and enrolment, but I need the ability to put students automatically into groups (my boss doesn't want them to have to insert an enrolment key or take any extra steps).

I have updated the queries so that they work in Moodle 2.0.10
I am using MySQL 5.0.45, and PHP Version 5.2.17

The code I am using works to insert or update the user, context, user_enrolment, role assignment and group members.   The problem is that even though it appears to me that everything is being inserted into the database, there is something missing that causes the users role in a course not to show up.

Could some one please go through my code and help me make it work properly?  

// Beginning of Moodle 2 Authorization of user and Enorlment into moodle
    # ==================================================================== #
        if ( 'moodle2' == $vle )
           {
           $mdldbserve = 'localhost';
           $mdldbname  = 'moodle_2new';
           $mdldbusr   = 'xxx';
           $mdldbpas   = 'bbb';
           $MDLsocket = mysql_connect($mdldbserve, $mdldbusr, $mdldbpas);
           mysql_select_db($mdldbname, $MDLsocket);


// Here we insert new user after checking for repeat customer and get the userid of the new or repeat user//
                   # moodle uses php password hash (md5):
            $passwordh = md5($password);

                $insert=  "INSERT INTO mdl_user  
               (confirmed, mnethostid, `username`, `password`,  firstname,
                lastname, email, phone1, address, city,  country, timecreated)  
                 VALUES ('1','21', '$login', '$passwordh',    '$fname',
                 '$lname','$email',  '$phone1',   '$address', '$city', '$country','$time' )
                 ON DUPLICATE KEY UPDATE password = '$passwordh' " ;
                
                 $result = mysql_query($insert); # enrole user
                        
                        //end of inserting a new or editing password on repeat user
                 $sql = " SELECT  id
                     FROM mdl_user
                     WHERE username = '".$login."' ";
                    $result = mysql_query($sql);
                        }
                    while ($row = mysql_fetch_array($result)) {
                    $userid = $row["id"];
            } // Now we have the $userid for future inserts
            
                // Getting course id from moodle_2new to enrol student in course
        $sql=    "Select id
                    from mdl_course
                    where shortname='$sku'  " ;
        
        $courseid1 = mysql_query( $sql);
        while($row = mysql_fetch_array($courseid1, MYSQL_ASSOC))
        {
         $courseid = "{$row['id']}" ;
        }// end of get course id        

        
// now we have to get the enrolid
            $enrol1='manual';
             $sql = " SELECT  id
                       FROM mdl_enrol
                       WHERE enrol = '$enrol1' and courseid = $courseid  ";
                                 
                        $result = mysql_query($sql);

                    while ($row = mysql_fetch_array($result)) {
                $enrolid = $row["id"];
}//, Now you have the $enrolid
    

      //have to put this person in the userenrolments or it won't work.
        $adduserenrolment = "INSERT INTO mdl_user_enrolments (
                            enrolid,
                             status,
                              userid )
                    VALUES ('".$enrolid."',
                    '0',
                    '$userid' )
                    ";
    
                $result = mysql_query($adduserenrolment);
               
    //start adding to context
                  $roleid = '5';
            $modifierid = '3';
            $itemid = $enrolid;
                   
    //add to context                           
    $addcontext= " INSERT mdl_context (
                    contextlevel, instanceid,depth)
                    VALUES ('30', '$userid','2')
                     ";
               $result = mysql_query($addcontext); # enrole user

               if ($result)
                   {         // If it enrolled the [NEW] student.
                   $contextid = @mysql_insert_id();
                   $path = '/1/'.$contextid ;
        

                   $addcontext2= " UPDATE mdl_context
                   SET path = '".$path."'
                    WHERE    id = '$contextid'
                    ";
                     $result = mysql_query($addcontext2);
                    if ($result)
                   {         // If it enrolled the [NEW] student.
                   $contextid = @mysql_insert_id();
                   }# enroled user
        //inserting student into course with role Student
                    $assign = "INSERT INTO mdl_role_assignments (
                    roleid,contextid, userid , timemodified , modifierid,itemid)
                    VALUES ('$roleid', '$courseid','$userid','$time', '$modifierid','$itemid') ";
                    
               $result2 = mysql_query($assign); # assign role
                       if (!$result2)
                   {         // If  role assignment didn't work
                   send_warning('role assignment not written: '.mysql_error() );
                   }
    
            //Get Group ID from mdl_groups

                    $sql = " SELECT  id
                             FROM mdl_groups
                             WHERE courseid = $courseid and enrolmentkey = '".$compsku."' " ;
                            
//had to add '".  ."' to  $compsku in this where statement but not to courseid - probably because compsku is a VarChar and not a BigInt??? //
    
                $result = mysql_query($sql);

                if (!$result) {
                 send_warning('group id selection had a problem: '.mysql_error() );
                }

                if (mysql_num_rows($result) == 0) {
                        send_warning('group id not selected so inserting: '.mysql_error() );
                    }

            // While a row of data exists, put that row in $row as an associative array
            while ($row = mysql_fetch_array($result)) {

            $groupid = $row["id"];
            }

            $addgroup =" INSERT INTO mdl_groups_members (
                        groupid, userid)
                        VALUES ('$groupid', '$userid' )";

        if (!mysql_query($addgroup))
      {
       send_warning('group not written: '.mysql_error() );
      }
      //group done
                   
                   }// End of Moodle 2 VLE

Average of ratings: -
In reply to Sandra King

Re: mysql enrolment from ecommerce program.

by José Núñez -

Hi Sandra.

Did you solve your problem?

I have to test some SQL statements to manualy insert students and course enrolments before ask the IT department to write a proces / program that can insert employees in training courses , using our HR data.

Any advise will be helpful.

Does anybody have something like this? or have a kind of map / path of tables and fields to affect ?

Thanks.