Guest User

Untitled

a guest
Nov 13th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. $servername = 'localhost';
  2. $username = 'username';
  3. $password = 'password';
  4. $dbname = 'moodle';
  5.  
  6. $u_moodle = 'theusernameyouwant';
  7. $hp_moodle = password_hash('thepasswordyouwant', PASSWORD_DEFAULT); ///IMPORTANT!
  8. $name = 'first name';
  9. $lname = 'last name';
  10. $email = 'e@m.ail'; ///This have to be verified by you as we're inserting it directly
  11. $course = '123'; //Id that you put in moodle admin, not the real id
  12.  
  13. $conn = new mysqli($servername, $username, $password, $dbname);
  14.  
  15. $sql = "INSERT INTO 'mdl_user' (auth, confirmed, mnethostid, username, password, firstname, lastname, email)
  16. VALUES ('manual', 1, 1, '$u_moodle', '$hp_moodle', '$name', '$lname', '$email')";
  17. // auth = 'manual', confirmed = 1, mnethostid = 1 Always. the others are your variables
  18.  
  19. if ($conn->query($sql) === TRUE) {
  20. echo "OKTC";
  21. } else {
  22. ////Manage your errors
  23. }
  24.  
  25. $sql = "SELECT * FROM $m_user WHERE email='$email'";
  26. $result = $conn2->query($sql);
  27. if($row = $result->fetch_assoc()) {
  28. $id = $row['id']; //Id of newly created user. we're using that for to register him on the course
  29. }
  30.  
  31. ////You have to use this if your idnumber for the course is the one you put into moodle (thats not the real id)
  32. $sql = "SELECT id FROM 'mdl_course' WHERE idnumber=$course";
  33. $result = $conn->query($sql);
  34. if(!$result){
  35. ///Not existing course, manage your error
  36. }
  37. if($row = $result->fetch_assoc()) {
  38. $idcourse = $row["id"];
  39. }
  40.  
  41. ///I need now the "enrol" id, so I do this:
  42. $sql = "SELECT id FROM 'mdl_enrol' WHERE courseid=$idcourse AND enrol='manual'";
  43. $result = $conn->query($sql);
  44. if(!$result){
  45. ///Not enrol associated (this shouldn't happen and means you have an error in your moodle database)
  46. }
  47. if($row = $result->fetch_assoc()) {
  48. $idenrol = $row["id"];
  49. }
  50.  
  51. ///Lastly I need the context
  52. $sql = "SELECT id FROM 'mdl_context' WHERE contextlevel=50 AND instanceid=$idcourse"; ///contextlevel = 50 means course in moodle
  53. $result = $conn->query($sql);
  54. if(!$result){
  55. ///Again, weird error, shouldnt happen to you
  56. }
  57. if($row = $result->fetch_assoc()) {
  58. $idcontext = $row["id"];
  59. }
  60.  
  61. ///We were just getting variables from moodle. Here is were the enrolment begins:
  62.  
  63. $time = time();
  64. $ntime = $time + 60*60*24*$duration; //How long will it last enroled $duration = days, this can be 0 for unlimited.
  65. $sql = "INSERT INTO 'mdl_user_enrolments' (status, enrolid, userid, timestart, timeend, timecreated, timemodified)
  66. VALUES (0, $idenrol, $id, '$time', '$ntime', '$time', '$time')";
  67. if ($conn->query($sql) === TRUE) {
  68. } else {
  69. ///Manage your sql error
  70. }
  71.  
  72. $sql = "INSERT INTO 'mdl_role_assignments' (roleid, contextid, userid, timemodified)
  73. VALUES (5, $idcontext, '$id', '$time')"; //Roleid = 5, means student.
  74. if ($conn->query($sql) === TRUE) {
  75. } else {
  76. //manage your errors
  77. }
Add Comment
Please, Sign In to add comment