title: how to enroll a user in all courses on Moodle
body: <p>I have a Moodle with a SSO.</p>
  
  <p>Users sign onto a site and click on a link to come to my Moodle.</p>
  
  <p>When they arrive, I want to enroll them on every course on my Moodle.</p>
  
  <p>However, I am unable to get the sql to work.</p>
  
  <p>Here is what I have:</p>
  
  <pre><code>$ra = new object();
  $ra-&gt;roleid = 5;
  $ra-&gt;contextid = $contextid;
  $ra-&gt;userid = $user-&gt;id;
  $ra-&gt;hidden = 0;
  $ra-&gt;enrol = 'manual';
  //$ra-&gt;enrol = 'self';
  /// Always round timestart downto 100 secs to help DBs to use their own caching algorithms
  /// by repeating queries with the same exact parameters in a 100 secs time window
  $ra-&gt;timestart = 0;
  $ra-&gt;timeend = 0;
  $ra-&gt;timemodified = time();
  $ra-&gt;modifierid = 0;
  
  // Enrol the User for the Course
  $ra-&gt;id = $DB-&gt;insert_record('role_assignments',$ra);
  </code></pre>