MDL-14679 towards enrol conversion
authorskodak <skodak>
Mon, 2 Jun 2008 21:02:52 +0000 (21:02 +0000)
committerskodak <skodak>
Mon, 2 Jun 2008 21:02:52 +0000 (21:02 +0000)
16 files changed:
enrol/database/enrol.php
enrol/database/enrol_database_sync.php
enrol/flatfile/enrol.php
enrol/imsenterprise/enrol.php
enrol/ldap/config.html
enrol/ldap/enrol.php
enrol/ldap/enrol_ldap_sync.php
enrol/manual/enrol.php
enrol/mnet/allowed_courses.php
enrol/mnet/enrol.php
enrol/paypal/ipn.php
enrol/paypal/return.php
lib/editor/htmlarea/coursefiles.php
lib/editor/tinymce/coursefiles.php
lib/grade/simpletest/testgradecategory.php
lib/simpletest/testeventslib.php

index 92fd0462e2874e1813db55aa4945b4a60e12b7bd..1c5086e04e57b8350a6ae2743accb21bf6705907 100644 (file)
@@ -6,13 +6,13 @@ class enrolment_plugin_database {
 
     var $log;
 
-/*
+/**
  * For the given user, let's go out and look in an external database
  * for an authoritative list of enrolments, and then adjust the
  * local Moodle assignments to match.
  */
 function setup_enrolments(&$user) {
-    global $CFG;
+    global $CFG, $DB;
 
     // NOTE: if $this->enrol_connect() succeeds you MUST remember to call
     // $this->enrol_disconnect() as it is doing some nasty vodoo with $CFG->prefix
@@ -26,7 +26,7 @@ function setup_enrolments(&$user) {
     // we execute the below code for every role type.  Otherwise we just
     // execute it once with null (hence the dummy array).
     $roles = !empty($CFG->enrol_db_remoterolefield) && !empty($CFG->enrol_db_localrolefield)
-        ? get_records('role')
+        ? $DB->get_records('role')
         : array(null);
 
     //error_log('[ENROL_DB] found ' . count($roles) . ' roles:');
@@ -46,7 +46,7 @@ function setup_enrolments(&$user) {
         /// Check if a particular role has been forced by the plugin site-wide
         /// (if we aren't doing a role-based select)
         if (!$have_role && $CFG->enrol_db_defaultcourseroleid) {
-            $role = get_record('role', 'id', $CFG->enrol_db_defaultcourseroleid);
+            $role = $DB->get_record('role', array('id'=>$CFG->enrol_db_defaultcourseroleid));
         }
 
         /// Whether to fetch the default role on a per-course basis (below) or not.
@@ -68,11 +68,11 @@ function setup_enrolments(&$user) {
 
             // We'll use this to see what to add and remove
             $existing = $role
-                ? get_records_sql("
-                    SELECT * FROM {$CFG->prefix}role_assignments
-                    WHERE userid = {$user->id}
-                     AND roleid = {$role->id}")
-                : get_records('role_assignments', 'userid', $user->id);
+                ? $DB->get_records_sql("SELECT *
+                                          FROM {role_assignments}
+                                         WHERE userid = ? AND roleid = ?",
+                                       array($user->id, $role->id))
+                : $DB->get_records('role_assignments', array('userid'=>$user->id));
 
             if (!$existing) {
                 $existing = array();
@@ -92,7 +92,7 @@ function setup_enrolments(&$user) {
                 //error_log('[ENROL_DB] Found '.count($existing).' existing roles and '.$count.' in external database');
 
                 foreach ($courselist as $coursefield) {   /// Check the list of courses against existing
-                    $course = get_record('course', $CFG->enrol_localcoursefield, $coursefield);
+                    $course = $DB->get_record('course', array($CFG->enrol_localcoursefield=>$coursefield));
                     if (!is_object($course)) {
                         if (empty($CFG->enrol_db_autocreate)) { // autocreation not allowed
                             if (debugging('',DEBUG_ALL)) {
@@ -107,7 +107,7 @@ function setup_enrolments(&$user) {
                         $course->fullname  = $coursefield;
                         $course->shortname = $coursefield;
                         if (!($newcourseid = $this->create_course($course, true)
-                            and $course = get_record( 'course', 'id', $newcourseid))) {
+                            and $course = $DB->get_record( 'course', array('id'=>$newcourseid)))) {
                             error_log( "Creating course $coursefield failed");
                             continue; // nothing left to do...
                         }
@@ -173,8 +173,7 @@ function setup_enrolments(&$user) {
  * used.
  */
 function sync_enrolments($role = null) {
-    global $CFG;
-    global $db;
+    global $CFG, $db, $DB;
     error_reporting(E_ALL);
 
     // Connect to the external database
@@ -197,7 +196,7 @@ function sync_enrolments($role = null) {
 
     if (!$have_role) {
         if (!empty($CFG->enrol_db_defaultcourseroleid)
-         and $role = get_record('role', 'id', $CFG->enrol_db_defaultcourseroleid)) {
+         and $role = $DB->get_record('role', array('id'=>$CFG->enrol_db_defaultcourseroleid))) {
             echo "=== Using enrol_db_defaultcourseroleid: {$role->id} ({$role->shortname}) ===\n";
         } elseif (isset($role)) {
             echo "!!! WARNING: Role specified by caller, but no (or invalid) role configuration !!!\n";
@@ -219,7 +218,7 @@ function sync_enrolments($role = null) {
         return true;
     }
 
-    begin_sql();
+    $DB->begin_sql();
     $extcourses = array();
     while ($extcourse_obj = rs_fetch_next_record($rs)) { // there are more course records
         $extcourse = $extcourse_obj->{$CFG->enrol_remotecoursefield};
@@ -227,9 +226,7 @@ function sync_enrolments($role = null) {
 
         // does the course exist in moodle already?
         $course = false;
-        $course = get_record( 'course',
-                              $CFG->enrol_localcoursefield,
-                              $extcourse );
+        $course = $DB->get_record('course', array($CFG->enrol_localcoursefield=>$extcourse));
 
         if (!is_object($course)) {
             if (empty($CFG->enrol_db_autocreate)) { // autocreation not allowed
@@ -245,7 +242,7 @@ function sync_enrolments($role = null) {
             $course->fullname  = $extcourse;
             $course->shortname = $extcourse;
             if (!($newcourseid = $this->create_course($course, true)
-             and $course = get_record( 'course', 'id', $newcourseid))) {
+             and $course = $DB->get_record('course', array('id'=>$newcourseid)))) {
                 error_log( "Creating course $extcourse failed");
                 continue; // nothing left to do...
             }
@@ -296,15 +293,23 @@ function sync_enrolments($role = null) {
         // correctly.
         //
         if (!$CFG->enrol_db_disableunenrol) {
-            $to_prune = get_records_sql("
-             SELECT ra.*
-             FROM {$CFG->prefix}role_assignments ra
-              JOIN {$CFG->prefix}user u ON ra.userid = u.id
-             WHERE ra.enrol = 'database'
-              AND ra.contextid = {$context->id}
-              AND ra.roleid = ". $role->id . ($extenrolments
-                ? " AND u.{$CFG->enrol_localuserfield} NOT IN (".join(", ", array_map(array(&$db, 'quote'), $extenrolments)).")"
-                : ''));
+            if ($extenrolments) {
+                list($extlist, $params) = $DB->get_in_or_equal($extenrolments, SQL_PARAMS_NAMED, 'e0', false);
+                $extsql = "AND u.{$CFG->enrol_localuserfield} $extlist";
+            } else {
+                $extsql = "";
+                $params = array();
+            }
+            $params['roleid']    = $role->id;
+            $params['contextid'] = $context->id;
+            
+            $to_prune = $DB->get_records_sql("
+                             SELECT ra.*
+                               FROM {role_assignments} ra
+                               JOIN {user} u ON ra.userid = u.id
+                              WHERE ra.enrol = 'database'
+                                    AND ra.contextid = :contextid
+                                    AND ra.roleid = :roleid", $params);
 
             if ($to_prune) {
                 foreach ($to_prune as $role_assignment) {
@@ -324,28 +329,26 @@ function sync_enrolments($role = null) {
         foreach ($extenrolments as $member) {
             // Get the user id and whether is enrolled in one fell swoop
             $sql = "
-                SELECT u.id AS userid, ra.id AS enrolmentid
-                FROM {$CFG->prefix}user u
-                 LEFT OUTER JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
-                  AND ra.roleid = {$role->id}
-                  AND ra.contextid = {$context->id}
-                 WHERE u.{$CFG->enrol_localuserfield} = ".$db->quote($member) .
-                 " AND (u.deleted IS NULL OR u.deleted=0) ";
-
-            $ers = $db->Execute($sql);
-            if (!$ers) {
-                trigger_error($db->ErrorMsg() .' STATEMENT: '. $sql);
+                SELECT u.id, ra.id AS enrolmentid
+                  FROM {user} u
+                  LEFT JOIN {role_assignments} ra ON u.id = ra.userid
+                       AND ra.roleid = ?
+                       AND ra.contextid = ?
+                 WHERE u.{$CFG->enrol_localuserfield} = ?
+                       AND (u.deleted IS NULL OR u.deleted=0)";
+
+            $eusers = $DB->get_records($sql, array($role->id, $context->id, $member));
+            if ($eusers === false) {
+                trigger_error('error STATEMENT: '. $sql);
                 return false;
             }
-            if ( $ers->EOF ) { // if this returns empty, it means we don't have the student record.
+            if (!$eusers) { // if this returns empty, it means we don't have the student record.
                                               // should not happen -- but skip it anyway
                 trigger_error('weird! no user record entry?');
                 continue;
             }
-            $user_obj = rs_fetch_record($ers);
-            $userid      = $user_obj->userid;
+            $userid      = $user_obj->id;
             $enrolmentid = $user_obj->enrolmentid;
-            rs_close($ers); // release the handle
 
             if ($enrolmentid) { // already enrolled - skip
                 continue;
@@ -373,39 +376,44 @@ function sync_enrolments($role = null) {
     // correctly.
     //
     if (!$CFG->enrol_db_disableunenrol) {
+        if ($extcourses) {
+            list($extlist, $params) = $DB->get_in_or_equal($extcourses, SQL_PARAMS_QM, 'e0', false);
+            $extsql = "AND u.{$CFG->enrol_localcoursefield} $extlist";
+        } else {
+            $extsql = "";
+            $params = array();
+        }
+        $params['roleid']    = $role->id;
+        $params['contextid'] = $context->id;
+
         $sql = "
             SELECT ra.roleid, ra.userid, ra.contextid
-            FROM {$CFG->prefix}role_assignments ra
-                JOIN {$CFG->prefix}context cn ON cn.id = ra.contextid
-                JOIN {$CFG->prefix}course c ON c.id = cn.instanceid
-            WHERE ra.enrol = 'database'
-              AND cn.contextlevel = ".CONTEXT_COURSE." " .
-                ($have_role ? ' AND ra.roleid = '.$role->id : '') .
-                ($extcourses
-                    ? " AND c.{$CFG->enrol_localcoursefield} NOT IN (" . join(",", array_map(array(&$db, 'quote'), $extcourses)) . ")"
-                    : '');
-
-        $ers = $db->Execute($sql);
-        if (!$ers) {
-            trigger_error($db->ErrorMsg() .' STATEMENT: '. $sql);
+              FROM {role_assignments} ra
+              JOIN {context} cn ON cn.id = ra.contextid
+              JOIN {course} c ON c.id = cn.instanceid
+             WHERE ra.enrol = 'database'
+                   AND cn.contextlevel = ".CONTEXT_COURSE." " .
+                  ($have_role ? " AND ra.roleid = :roleid " : '') ."
+                   $extsql";
+
+        if (!$ers = $DB->get_recordset_sql($sql, $params)) {
+            trigger_error('error STATEMENT: '. $sql);
             return false;
         }
-        if ( !$ers->EOF ) {
-            while ($user_obj = rs_fetch_next_record($ers)) {
-                $roleid     = $user_obj->roleid;
-                $user       = $user_obj->userid;
-                $contextid  = $user_obj->contextid;
-                if (role_unassign($roleid, $user, 0, $contextid)){
-                    error_log( "Unassigned role {$roleid} from user $user in context $contextid");
-                } else {
-                    error_log( "Failed unassign role {$roleid} from user $user in context $contextid");
-                }
+        foreach ($ers as $user_obj) {
+            $roleid     = $user_obj->roleid;
+            $user       = $user_obj->userid;
+            $contextid  = $user_obj->contextid;
+            if (role_unassign($roleid, $user, 0, $contextid)){
+                error_log( "Unassigned role {$roleid} from user $user in context $contextid");
+            } else {
+                error_log( "Failed unassign role {$roleid} from user $user in context $contextid");
             }
-            rs_close($ers); // release the handle
         }
+        $ers->close(); // release the handle
     }
 
-    commit_sql();
+    $DB->commit_sql();
 
     // we are done now, a bit of housekeeping
     fix_course_sortorder();
@@ -543,11 +551,11 @@ function process_config($config) {
 // you will want to call fix_course_sortorder() after your are done
 // with course creation
 function create_course ($course,$skip_fix_course_sortorder=0){
-    global $CFG;
+    global $CFG, $DB;
 
     // define a template
     if(!empty($CFG->enrol_db_template)){
-        $template = get_record("course", 'shortname', $CFG->enrol_db_template);
+        $template = $DB->get_record("course", array('shortname'=>$CFG->enrol_db_template));
         $template = (array)$template;
     } else {
         $site = get_site();
@@ -583,9 +591,7 @@ function create_course ($course,$skip_fix_course_sortorder=0){
     }
 
     // define the sortorder
-    $sort = get_field_sql('SELECT COALESCE(MAX(sortorder)+1, 100) AS max ' .
-                          ' FROM ' . $CFG->prefix . 'course ' .
-                          ' WHERE category=' . $course->category);
+    $sort = $DB->get_field_sql('SELECT COALESCE(MAX(sortorder)+1, 100) AS max FROM {course} WHERE category= ?', array($course->category));
     $course->sortorder = $sort;
 
     // override with local data
@@ -601,11 +607,11 @@ function create_course ($course,$skip_fix_course_sortorder=0){
     $course->shortname = substr($course->shortname, 0, 100);
 
     // store it and log
-    if ($newcourseid = insert_record("course", addslashes_object($course))) {  // Set up new course
+    if ($newcourseid = $DB->insert_record("course", $course)) {  // Set up new course
         $section = NULL;
         $section->course = $newcourseid;   // Create a default section.
         $section->section = 0;
-        $section->id = insert_record("course_sections", $section);
+        $section->id = $DB->insert_record("course_sections", $section);
         $page = page_create_object(PAGE_COURSE_VIEW, $newcourseid);
         blocks_repopulate_page($page); // Return value no
 
index 322b1b2395cc90a42f39155536ccea34bf5cbaa9..d9b5d0ade24846e681a53307fc66504b5920722e 100644 (file)
@@ -27,7 +27,7 @@
     // role and update it.  Otherwise, just got through once (with no role
     // specified).
     $roles = !empty($CFG->enrol_db_remoterolefield) && !empty($CFG->enrol_db_localrolefield)
-        ? get_records('role')
+        ? $DB->get_records('role')
         : array(null);
         
     foreach ($roles as $role) {
index 752beba7e5599832d8e883b330133bef031d2e70..d770a8a06f343456a2b4c7b353d9d95897056235 100644 (file)
@@ -13,7 +13,7 @@ class enrolment_plugin_flatfile {
 
 /// Override the base config_form() function
 function config_form($frm) {
-    global $CFG;
+    global $CFG, $DB;
 
     $vars = array('enrol_flatfilelocation', 'enrol_mailstudents', 'enrol_mailteachers', 'enrol_mailadmins');
     foreach ($vars as $var) {
@@ -22,7 +22,7 @@ function config_form($frm) {
         } 
     }
 
-    $roles = get_records('role', '', '', '', 'id, name, shortname');
+    $roles = $DB->get_records('role', null, '', 'id, name, shortname');
     $ffconfig = get_config('enrol_flatfile');
 
     $frm->enrol_flatfilemapping = array();
@@ -40,6 +40,7 @@ function config_form($frm) {
 
 /// Override the base process_config() function
 function process_config($config) {
+    global $DB;
 
     if (!isset($config->enrol_flatfilelocation)) {
         $config->enrol_flatfilelocation = '';
@@ -61,7 +62,7 @@ function process_config($config) {
     }
     set_config('enrol_mailadmins', $config->enrol_mailadmins);
 
-    foreach(get_records('role', '', '', '', 'id, shortname') as $id => $role) {
+    foreach($DB->get_records('role', null, '', 'id, shortname') as $id => $role) {
         if (isset($config->{"enrol_flatfilemapping_{$id}"})) {
             set_config('map_'.$role->shortname, $config->{"enrol_flatfilemapping_{$id}"}, 'enrol_flatfile');
         } else {
@@ -91,7 +92,7 @@ function get_access_icons($course) {
 *   endtime          = end time (in seconds since epoch) - optional
 */
     function cron() {
-        global $CFG;
+        global $CFG, $DB;
 
         if (empty($CFG->enrol_flatfilelocation)) {
             $filename = "$CFG->dataroot/1/enrolments.txt";  // Default location
@@ -157,13 +158,13 @@ function get_access_icons($course) {
                         continue;
                     }
 
-                    if (! $user = get_record("user", "idnumber", $fields[2]) ) {
+                    if (! $user = $DB->get_record("user", array("idnumber"=>$fields[2]))) {
                         $this->log .= "Unknown user idnumber in field 3 - ignoring line\n";
                         continue;
                     }
 
 
-                    if (! $course = get_record("course", "idnumber", $fields[3]) ) {
+                    if (! $course = $DB->get_record("course", array("idnumber"=>$fields[3]))) {
                         $this->log .= "Unknown course idnumber in field 4 - ignoring line\n";
                         continue;
                     }
@@ -265,8 +266,10 @@ function get_access_icons($course) {
      * @return array ($roles, $rolemap)
      */
     function get_roles() {
+        global $DB;
+
         // Get a list of all the roles in the database, indexed by their short names.
-        $roles = get_records('role', '', '', '', 'shortname, id');
+        $roles = $DB->get_records('role', null, '', 'shortname, id');
         array_walk($roles, create_function('&$value', '$value = $value->id;'));
 
         // Get any name mappings. These will be of the form 'map_shortname' => 'flatfilename'.
index 3d364bdf902da0b7cfbf6f1737736597319fbd3f..eb50e86a27ecc035b1a1ba1b89995c6ddfb9f6b6 100644 (file)
@@ -439,7 +439,7 @@ function get_recstatus($tagdata, $tagname){
 * @param string $tagconents The raw contents of the XML element
 */
 function process_group_tag($tagcontents){
-    global $CFG;
+    global $CFG, $DB;
 
     // Process tag contents
     unset($group);
@@ -482,7 +482,7 @@ function process_group_tag($tagcontents){
         // Third, check if the course(s) exist
         foreach($group->coursecode as $coursecode){
             $coursecode = trim($coursecode);
-            if(!get_field('course', 'id', 'idnumber', $coursecode)) {
+            if(!$DB->get_field('course', 'id', array('idnumber'=>$coursecode))) {
               if(!$CFG->enrol_createnewcourses) {
                   $this->log_line("Course $coursecode not found in Moodle's course idnumbers.");
               } else {
@@ -510,13 +510,13 @@ function process_group_tag($tagcontents){
                 // Handle course categorisation (taken from the group.org.orgunit field if present)
                 if(strlen($group->category)>0){
                     // If the category is defined and exists in Moodle, we want to store it in that one
-                    if($catid = get_field('course_categories', 'id', 'name', addslashes($group->category))){
+                    if($catid = $DB->get_field('course_categories', 'id', array('name'=>$group->category))){
                         $course->category = $catid;
                     }elseif($CFG->enrol_createnewcategories){
                         // Else if we're allowed to create new categories, let's create this one
                         $newcat->name = $group->category;
                        $newcat->visible = 0;
-                       if($catid = insert_record('course_categories', $newcat)){
+                       if($catid = $DB->insert_record('course_categories', $newcat)){
                            $course->category = $catid;
                            $this->log_line("Created new (hidden) category, #$catid: $newcat->name");
                        }else{
@@ -534,9 +534,7 @@ function process_group_tag($tagcontents){
                 $course->startdate = time();
                 $course->numsections = 1;
                 // Choose a sort order that puts us at the start of the list!
-                $sortinfo = get_record_sql('SELECT MIN(sortorder) AS min,
-                           MAX(sortorder) AS max
-                            FROM ' . $CFG->prefix . 'course WHERE category<>0');
+                $sortinfo = $DB->get_record_sql('SELECT MIN(sortorder) AS min, MAX(sortorder) AS max FROM {course} WHERE category<>0');
                 if (is_object($sortinfo)) { // no courses?
                     $max   = $sortinfo->max;
                     $min   = $sortinfo->min;
@@ -545,7 +543,7 @@ function process_group_tag($tagcontents){
                 }else{
                     $course->sortorder = 1000;
                 }
-                if($course->id = insert_record('course', addslashes_object($course))){
+                if($course->id = $DB->insert_record('course', $course)){
 
                     // Setup the blocks
                     $page = page_create_object(PAGE_COURSE_VIEW, $course->id);
@@ -554,7 +552,7 @@ function process_group_tag($tagcontents){
                     $section = new object();
                     $section->course = $course->id;   // Create a default section.
                     $section->section = 0;
-                    $section->id = insert_record("course_sections", $section);
+                    $section->id = $DB->insert_record("course_sections", $section);
 
                     add_to_log(SITEID, "course", "new", "view.php?id=$course->id", "$course->fullname (ID $course->id)");
 
@@ -565,7 +563,7 @@ function process_group_tag($tagcontents){
               }
             }elseif($recstatus==3 && ($courseid = get_field('course', 'id', 'idnumber', $coursecode))){
                 // If course does exist, but recstatus==3 (delete), then set the course as hidden
-                set_field('course', 'visible', '0', 'id', $courseid);
+                $DB->set_field('course', 'visible', '0', array('id'=>$courseid));
             }
         } // End of foreach(coursecode)
     }
@@ -576,7 +574,7 @@ function process_group_tag($tagcontents){
 * @param string $tagconents The raw contents of the XML element
 */
 function process_person_tag($tagcontents){
-    global $CFG;
+    global $CFG, $DB;
 
     if(preg_match('{<sourcedid>.*?<id>(.+?)</id>.*?</sourcedid>}is', $tagcontents, $matches)){
         $person->idnumber = trim($matches[1]);
@@ -630,7 +628,7 @@ function process_person_tag($tagcontents){
 
         if($CFG->enrol_imsdeleteusers){ // If we're allowed to delete user records
             // Make sure their "deleted" field is set to one
-            set_field('user', 'deleted', 1, 'username', $person->username);
+            $DB->set_field('user', 'deleted', 1, array('username'=>$person->username));
             $this->log_line("Marked user record for user '$person->username' (ID number $person->idnumber) as deleted.");
         }else{
             $this->log_line("Ignoring deletion request for user '$person->username' (ID number $person->idnumber).");
@@ -640,14 +638,14 @@ function process_person_tag($tagcontents){
 
 
         // If the user exists (matching sourcedid) then we don't need to do anything.
-        if(!get_field('user', 'id', 'idnumber', $person->idnumber) && $CFG->enrol_createnewusers){
+        if(!$DB->get_field('user', 'id', array('idnumber'=>$person->idnumber)) && $CFG->enrol_createnewusers){
             // If they don't exist and haven't a defined username, we log this as a potential problem.
             if((!isset($person->username)) || (strlen($person->username)==0)){
                 $this->log_line("Cannot create new user for ID # $person->idnumber - no username listed in IMS data for this person.");
-            }elseif(get_field('user', 'id', 'username', $person->username)){
+            } else if ($DB->get_field('user', 'id', array('username'=>$person->username))){
                 // If their idnumber is not registered but their user ID is, then add their idnumber to their record
-                set_field('user', 'idnumber', addslashes($person->idnumber), 'username', $person->username);
-            }else{
+                $DB->set_field('user', 'idnumber', $person->idnumber, array('username'=>$person->username));
+            } else {
 
             // If they don't exist and they have a defined username, and $CFG->enrol_createnewusers == true, we create them.
             $person->lang = 'manual'; //TODO: this needs more work due tu multiauth changes
@@ -655,7 +653,7 @@ function process_person_tag($tagcontents){
             $person->confirmed = 1;
             $person->timemodified = time();
             $person->mnethostid = $CFG->mnet_localhost_id;
-            if($id = insert_record('user', addslashes_object($person))){
+            if($id = $DB->insert_record('user', addslashes_object($person))){
     /*
     Photo processing is deactivated until we hear from Moodle dev forum about modification to gdlib.
 
@@ -670,7 +668,7 @@ function process_person_tag($tagcontents){
                                    //Llibreria creada per nosaltres mateixos.
                                    require_once($CFG->dirroot.'/lib/gdlib.php');
                                    if ($usernew->picture = save_profile_image($id, $person->urlphoto,'user')) {
-                                     set_field('user', 'picture', $usernew->picture, 'id', $id);  /// Note picture in DB
+                                     $DB->set_field('user', 'picture', $usernew->picture, array('id'=>$id));  /// Note picture in DB
                                    }
                                  }
     */
@@ -683,7 +681,7 @@ function process_person_tag($tagcontents){
             $this->log_line("User record already exists for user '$person->username' (ID number $person->idnumber).");
 
             // Make sure their "deleted" field is set to zero.
-            set_field('user', 'deleted', 0, 'idnumber', $person->idnumber);
+            $DB->set_field('user', 'deleted', 0, array('idnumber'=>$person->idnumber));
         }else{
             $this->log_line("No user record found for '$person->username' (ID number $person->idnumber).");
         }
@@ -698,7 +696,7 @@ function process_person_tag($tagcontents){
 * @param string $tagconents The raw contents of the XML element
 */
 function process_membership_tag($tagcontents){
-    global $CFG;
+    global $CFG, $DB;
     $memberstally = 0;
     $membersuntally = 0;
 
@@ -709,7 +707,7 @@ function process_membership_tag($tagcontents){
         $ship->coursecode = ($CFG->enrol_truncatecoursecodes > 0)
                                  ? substr(trim($matches[1]), 0, intval($CFG->enrol_truncatecoursecodes))
                                  : trim($matches[1]);
-        $ship->courseid = get_field('course', 'id', 'idnumber', $ship->coursecode);
+        $ship->courseid = $DB->get_field('course', 'id', array('idnumber'=>$ship->coursecode));
     }
     if($ship->courseid && preg_match_all('{<member>(.*?)</member>}is', $tagcontents, $membermatches, PREG_SET_ORDER)){
         foreach($membermatches as $mmatch){
@@ -750,7 +748,7 @@ function process_membership_tag($tagcontents){
 //print_r($rolecontext);
 
             // Add or remove this student or teacher to the course...
-            $memberstoreobj->userid = get_field('user', 'id', 'idnumber', $member->idnumber);
+            $memberstoreobj->userid = $DB->get_field('user', 'id', array('idnumber'=>$member->idnumber));
             $memberstoreobj->enrol = 'imsenterprise';
             $memberstoreobj->course = $ship->courseid;
             $memberstoreobj->time = time();
@@ -781,16 +779,16 @@ function process_membership_tag($tagcontents){
                             if(isset($groupids[$member->groupname])){
                                 $member->groupid = $groupids[$member->groupname]; // Recall the group ID from cache if available
                             }else{
-                                if($groupid = get_field('groups', 'id', 'name', addslashes($member->groupname), 'courseid', $ship->courseid)){
+                                if($groupid = $DB->get_field('groups', 'id', 'name', $member->groupname, array('courseid'=>$ship->courseid))){
                                     $member->groupid = $groupid;
                                     $groupids[$member->groupname] = $groupid; // Store ID in cache
                                 }else{
                                     // Attempt to create the group
-                                    $group->name = addslashes($member->groupname);
+                                    $group->name = $member->groupname;
                                     $group->courseid = $ship->courseid;
                                     $group->timecreated = time();
                                     $group->timemodified = time();
-                                    $groupid = insert_record('groups', $group);
+                                    $groupid = $DB->insert_record('groups', $group);
                                     $this->log_line('Added a new group for this course: '.$group->name);
                                     $groupids[$member->groupname] = $groupid; // Store ID in cache
                                     $member->groupid = $groupid;
@@ -874,10 +872,12 @@ function decode_timeframe($string){ // Pass me the INNER CONTENTS of a <timefram
 * how an IMS-E role corresponds to a Moodle role
 */
 function load_role_mappings() {
+    global $DB;
+
     $this->rolemappings = array();
     foreach($this->imsroles as $imsrolenum=>$imsrolename) {
         $this->rolemappings[$imsrolenum] = $this->rolemappings[$imsrolename]
-            = get_field('config', 'value', 'name', 'enrol_imse_imsrolemap' . $imsrolenum);
+            = $DB->get_field('config', 'value', array('name'=>'enrol_imse_imsrolemap' . $imsrolenum));
     }
 }
 
index 4aedf992391b4b20796490bec1511ae442761372..5bbb1f5c02361a0291b9bcda875c82d8c7da11f1 100755 (executable)
@@ -61,7 +61,7 @@ if (!isset ($frm->enrol_ldap_course_summary_editlock)) {
 }
 
 // Roles
-$roles = get_records('role');
+$roles = $DB->get_records('role');
 foreach($roles as $role) {
     if (!isset($frm->{'enrol_ldap_contexts_role'.$role->id})) {
         $frm->{'enrol_ldap_contexts_role'.$role->id} = '';
index 2b11bee8ff73621d50a6274274fc24890dd910ac..33d73bf5a6d24f6b9bc6a06e9c7ceb469097615b 100755 (executable)
@@ -12,7 +12,7 @@ class enrolment_plugin_ldap {
  * This function syncs a user's enrolments with those on the LDAP server.
  */
 function setup_enrolments(&$user) {
-    global $CFG;
+    global $CFG, $DB;
 
     //error_log('[ENROL_LDAP] setup_enrolments called');
 
@@ -27,13 +27,13 @@ function setup_enrolments(&$user) {
     // we are connected OK, continue...
 
     // Get all the possible roles
-    $roles = get_records('role');
+    $roles = $DB->get_records('role');
 
     // Make sure the config settings have been upgraded.
     $this->check_legacy_config();
 
     // Get the entire list of role assignments that currently exist for this user.
-    $roleassignments = get_records('role_assignments', 'userid', $user->id);
+    $roleassignments = $DB->get_records('role_assignments', array('userid'=>$user->id));
     if (!$roleassignments) {
         $roleassignments = array();
     }
@@ -61,17 +61,13 @@ function setup_enrolments(&$user) {
             }
             
             // create the course  ir required
-            $course_obj = get_record( 'course',
-                                      $this->enrol_localcoursefield,
-                                      $course_ext_id );
+            $course_obj = $DB->get_record('course', array($this->enrol_localcoursefield, $course_ext_id));
 
             if (empty($course_obj)){ // course doesn't exist
                 if($CFG->enrol_ldap_autocreate){ // autocreate
                     error_log("[ENROL_LDAP] CREATE User $user->username enrolled to a nonexistant course $course_ext_id \n");
                     $newcourseid = $this->create_course($enrol);
-                    $course_obj = get_record( 'course',
-                                      $this->enrol_localcoursefield,
-                                       $newcourseid);
+                    $course_obj = $DB->get_record('course', array($this->enrol_localcoursefield=>$newcourseid));
                 } else {
                     error_log("[ENROL_LDAP] User $user->username enrolled to a nonexistant course $course_ext_id \n");
                 }
@@ -83,7 +79,7 @@ function setup_enrolments(&$user) {
                 $context = get_context_instance(CONTEXT_COURSE, $course_obj->id);
                 //$courseroles = get_user_roles($context, $user->id);
             
-                if (!get_record('role_assignments', 'roleid', $role->id, 'userid', $user->id, 'contextid', $context->id)) {
+                if (!$DB->get_record('role_assignments', array('roleid'=>$role->id, 'userid'=>$user->id, 'contextid'=>$context->id))) {
                     //error_log("[ENROL_LDAP] Assigning role '{$role->name}' to {$user->id} ({$user->username}) in course {$course_obj->id} ({$course_obj->shortname})");
                     if (!role_assign($role->id, $user->id, 0, $context->id, 0, 0, 0, 'ldap')){
                         error_log("[ENROL_LDAP] Failed to assign role '{$role->name}' to $user->id ($user->username) into course $course_obj->id ($course_obj->shortname)");
@@ -123,10 +119,10 @@ function setup_enrolments(&$user) {
 
 /// sync enrolments with ldap, create courses if required.
 function sync_enrolments($type, $enrol = false) {
-    global $CFG;
+    global $CFG, $DB;
 
     // Get the role. If it doesn't exist, that is bad.
-    $role = get_record('role', 'shortname', $type);
+    $role = $DB->get_record('role', array('shortname'=>$type));
     if (!$role) {
         notify("No such role: $type");
         return false;
@@ -212,14 +208,12 @@ function sync_enrolments($type, $enrol = false) {
                 print "== Synching $idnumber\n";
                 // does the course exist in moodle already? 
                 $course_obj = false;
-                $course_obj = get_record( 'course',
-                                          $this->enrol_localcoursefield,
-                                          $idnumber );
+                $course_obj = $DB->get_record('course', array($this->enrol_localcoursefield=>$idnumber));
                 if (!is_object($course_obj)) {
                     // ok, now then let's create it!
                     print "Creating Course $idnumber...";
                     $newcourseid = $this->create_course($course, true); // we are skipping fix_course_sortorder()
-                    $course_obj = get_record( 'course', 'id', $newcourseid);
+                    $course_obj = $DB->get_record('course', array('id'=>$newcourseid));
                     if (is_object($course_obj)) {
                         print "OK!\n";
                     } else {
@@ -249,17 +243,19 @@ function sync_enrolments($type, $enrol = false) {
                     // hopefully they'll fit in the max buffer size for the RDBMS
                     $sql = '
                         SELECT enr.userid AS user, 1
-                        FROM '.$CFG->prefix.'role_assignments enr
-                        JOIN '.$CFG->prefix.'user usr ON usr.id=enr.userid
+                        FROM {role_assignments} enr
+                        JOIN {user} usr ON usr.id=enr.userid
                         WHERE enr.roleid = '.$role->id.'
                          AND enr.contextid = '.$context->id.'
                          AND enr.enrol = \'ldap\' ';
                     if (!empty($ldapmembers)) {
-                        $sql .= 'AND usr.idnumber NOT IN (\''. join('\',\'', $ldapmembers).'\')';
+                        list($ldapml, $params) = $DB->get_in_or_equal($ldapmembers, SQL_PARAMS_NAMED, 'm0', false);
+                        $sql .= "AND usr.idnumber $ldapml";
                     } else {
                         print ("Empty enrolment for $course_obj->shortname \n");
+                        $params = array();
                     }
-                    $todelete = get_records_sql($sql);
+                    $todelete = $DB->get_records_sql($sql, $params);
                     if(!empty($todelete)){
                         foreach ($todelete as $member) {
                             $member = $member->user;
@@ -275,18 +271,17 @@ function sync_enrolments($type, $enrol = false) {
                     // insert current enrolments 
                     // bad we can't do INSERT IGNORE with postgres...
                     foreach ($ldapmembers as $ldapmember) {
-                        $sql = 'SELECT id,1 FROM '.$CFG->prefix.'user '
-                                ." WHERE idnumber='$ldapmember'";
-                        $member = get_record_sql($sql); 
+                        $sql = 'SELECT id,1 FROM {user} '
+                                ." WHERE idnumber=?";
+                        $member = $DB->get_record_sql($sql, array($ldapmember)); 
 //                        print "sql: $sql \nidnumber = $ldapmember \n" . var_dump($member); 
                         if(empty($member) || empty($member->id)){
                             print "Could not find user $ldapmember, skipping\n";
                             continue;
                         }
                         $member = $member->id;
-                        if (!get_record('role_assignments', 'roleid', $role->id, 
-                                        'contextid', $context->id, 
-                                        'userid', $member, 'enrol', 'ldap')){
+                        if (!$DB->get_record('role_assignments', array('roleid'=>$role->id, 
+                                             'contextid'=>$context->id, 'userid'=>$member, 'enrol'=>'ldap'))){
                             if (role_assign($role->id, $member, 0, $context->id, 0, 0, 0, 'ldap')){
                                 print "Assigned role $type to $member ($ldapmember) for course $course_obj->id ($course_obj->shortname)\n";
                             } else {
@@ -323,6 +318,7 @@ function config_form($frm) {
 
 /// Override the base process_config() function
 function process_config($config) {
+    global $DB;
 
     $this->check_legacy_config();
 
@@ -401,7 +397,7 @@ function process_config($config) {
     }
     set_config('enrol_ldap_autocreate', $config->enrol_ldap_autocreate);
 
-    $roles = get_records('role');
+    $roles = $DB->get_records('role');
     foreach ($roles as $role) {
         if (!isset($config->{'enrol_ldap_contexts_role'.$role->id})) {
             $config->{'enrol_ldap_contexts_role'.$role->id} = '';
@@ -551,11 +547,11 @@ function find_ext_enrolments ($ldap_connection, $memberuid, $role){
 // you will want to call fix_course_sortorder() after your are done
 // with course creation
 function create_course ($course_ext,$skip_fix_course_sortorder=0){
-    global $CFG;
+    global $CFG, $DB;
 
     // override defaults with template course
     if(!empty($CFG->enrol_ldap_template)){
-        $course = get_record("course", 'shortname', $CFG->enrol_ldap_template);
+        $course = $DB->get_record("course", array('shortname'=>$CFG->enrol_ldap_template));
         unset($course->id); // so we are clear to reinsert the record
         unset($course->sortorder);
     } else {
@@ -593,7 +589,7 @@ function create_course ($course_ext,$skip_fix_course_sortorder=0){
     }
 
     // define the sortorder (yuck)
-    $sort = get_record_sql('SELECT MAX(sortorder) AS max, 1 FROM ' . $CFG->prefix . 'course WHERE category=' . $course->category);
+    $sort = $DB->get_record_sql('SELECT MAX(sortorder) AS max, 1 FROM {course} WHERE category=?', array($course->category));
     $sort = $sort->max;
     $sort++;
     $course->sortorder = $sort; 
@@ -602,15 +598,13 @@ function create_course ($course_ext,$skip_fix_course_sortorder=0){
     $course->startdate = time();
     $course->timecreated = time();
     $course->visible     = 1;
-    
-    $course = addslashes_recursive($course);
 
     // store it and log
-    if ($newcourseid = insert_record("course", $course)) {  // Set up new course
+    if ($newcourseid = $DB->insert_record("course", $course)) {  // Set up new course
         $section = new object();
         $section->course = $newcourseid;   // Create a default section.
         $section->section = 0;
-        $section->id = insert_record("course_sections", $section);
+        $section->id = $DB->insert_record("course_sections", $section);
         $page = page_create_object(PAGE_COURSE_VIEW, $newcourseid);
         blocks_repopulate_page($page); // Return value no
 
@@ -635,10 +629,10 @@ function create_course ($course_ext,$skip_fix_course_sortorder=0){
  * @uses $CFG
  */
 function check_legacy_config () {
-    global $CFG;
+    global $CFG, $DB;
 
     if (isset($CFG->enrol_ldap_student_contexts)) {
-        if ($student_role = get_record('role', 'shortname', 'student')) {
+        if ($student_role = $DB->get_record('role', array('shortname'=>'student'))) {
             set_config('enrol_ldap_contexts_role'.$student_role->id, $CFG->enrol_ldap_student_contexts);
         }
 
@@ -647,7 +641,7 @@ function check_legacy_config () {
 
     if (isset($CFG->enrol_ldap_student_memberattribute)) {
         if (isset($student_role)
-         or $student_role = get_record('role', 'shortname', 'student')) {
+         or $student_role = $DB->get_record('role', array('shortname'=>'student'))) {
             set_config('enrol_ldap_memberattribute_role'.$student_role->id, $CFG->enrol_ldap_student_memberattribute);
         }
 
@@ -655,7 +649,7 @@ function check_legacy_config () {
     }
 
     if (isset($CFG->enrol_ldap_teacher_contexts)) {
-        if ($teacher_role = get_record('role', 'shortname', 'teacher')) {
+        if ($teacher_role = $DB->get_record('role', array('shortname'=>'teacher'))) {
             set_config('enrol_ldap_contexts_role'.$teacher_role->id, $CFG->enrol_ldap_student_contexts);
         }
 
@@ -664,7 +658,7 @@ function check_legacy_config () {
     
     if (isset($CFG->enrol_ldap_teacher_memberattribute)) {
         if (isset($teacher_role)
-         or $teacher_role = get_record('role', 'shortname', 'teacher')) {
+         or $teacher_role = $DB->get_record('role', array('shortname'=>'teacher'))) {
             set_config('enrol_ldap_memberattribute_role'.$teacher_role->id, $CFG->enrol_ldap_teacher_memberattribute);
         }
 
index 7830c45daf7c706bf7ba9b96b6ffa599eb80d242..7a42b0cd141ec787e0fe1d55c979b5fc2a051b79 100755 (executable)
@@ -22,7 +22,7 @@
 
     $enrol->check_legacy_config();
 
-    $roles = get_records('role');
+    $roles = $DB->get_records('role');
     foreach ($roles as $role) {
         $enrol->sync_enrolments($role->shortname, true);
     }
index c71bf047f708a9dfc72352a4e2e55e3bf46bdabc..785fc2630d8ee62189f34131d9cbe0c4a857cfa4 100644 (file)
@@ -245,7 +245,7 @@ function process_config($config) {
 * @return void
 */
 function cron() {
-    global $CFG, $USER, $SITE;
+    global $CFG, $USER, $SITE, $DB;
 
     if (!isset($CFG->lastexpirynotify)) {
         set_config('lastexpirynotify', 0);
@@ -256,13 +256,13 @@ function cron() {
         return;
     }
 
-    if ($rs = get_recordset_select('course', 'enrolperiod > 0 AND expirynotify > 0 AND expirythreshold > 0')) {
+    if ($rs = $DB->get_recordset_select('course', 'enrolperiod > 0 AND expirynotify > 0 AND expirythreshold > 0')) {
 
         $cronuser = clone($USER);
 
         $admin = get_admin();
 
-        while($course = rs_fetch_next_record($rs)) {
+        foreach ($rs as $course) {
             $a = new object();
             $a->coursename = $course->shortname .'/'. $course->fullname; // must be processed by format_string later
             $a->threshold  = $course->expirythreshold / 86400;
@@ -279,10 +279,10 @@ function cron() {
                 continue;
             }
 
-            if ($oldenrolments = get_records_sql("
+            if ($oldenrolments = $DB->get_records_sql("
                       SELECT u.*, ra.timeend
-                        FROM {$CFG->prefix}user u
-                             JOIN {$CFG->prefix}role_assignments ra ON (ra.userid = u.id)
+                        FROM {user} u
+                             JOIN {role_assignments} ra ON (ra.userid = u.id)
                         WHERE ra.contextid = $context->id
                               AND ra.timeend > 0 AND ra.timeend <= $expiry
                               AND ra.enrol = 'manual'")) {
@@ -338,6 +338,7 @@ function cron() {
         }
         $USER = $cronuser;
         course_setup($course);   // More environment
+        $rs->close();
     }
 
     set_config('lastexpirynotify', date('Ymd'));
index abd797c54131946a45429a9b002f283cbc5f9d4b..144aac42ae4d12add761b677072b0d099ce7f850 100644 (file)
@@ -83,7 +83,7 @@ if ($form = data_submitted() and confirm_sesskey()) {
 
 // setup arrays for allowed categories and courses
 $categories = array();
-if ($categories = get_records('course_categories', '', '', 'name', 'id, name')) {
+if ($categories = $DB->get_records('course_categories', null, 'name', 'id, name')) {
     $allowedcategories = array();
     if (empty($CFG->enrol_mnet_allowed_categories)) {
         $potentialcategories = $categories;
@@ -100,7 +100,7 @@ if ($categories = get_records('course_categories', '', '', 'name', 'id, name'))
     }
 }
 $courses = array();
-if ($courses = get_records('course', '', '', 'shortname', 'id, shortname')) {
+if ($courses = $DB->get_records('course', null, 'shortname', 'id, shortname')) {
     unset($courses[SITEID]); // never list or offer the siteid
     $allowedcourses = array();
     if (empty($CFG->enrol_mnet_allowed_courses)) {
index d98a97607b809594d68d26fd02f4f5bd2e37170e..223411318c1aa5b0733750149553859e8f2e4e97 100644 (file)
@@ -83,7 +83,7 @@ class enrolment_plugin_mnet {
     * @return bool              Whether the user can login from the remote host
     */
     function available_courses() {
-        global $CFG;
+        global $CFG, $DB;
 
         if (!empty($CFG->enrol_mnet_allow_allcourses)) {
 
@@ -104,21 +104,19 @@ class enrolment_plugin_mnet {
                 co.defaultrole AS defaultroleid,
                 r.name         AS defaultrolename 
             FROM
-                {$CFG->prefix}course_categories ca
+                {course_categories} ca
             JOIN
-                {$CFG->prefix}course co ON
-                ca.id = co.category
+                {course} co ON ca.id = co.category
             LEFT JOIN
-                {$CFG->prefix}role r ON
-                r.id = co.defaultrole
+                {role} r ON r.id = co.defaultrole
             WHERE
-                co.visible = '1' AND
-                co.enrollable = '1'
+                co.visible = 1 AND
+                co.enrollable = 1
             ORDER BY
                 sortorder ASC
                 ";
 
-            return get_records_sql($query);
+            return $DB->get_records_sql($query);
 
         } elseif (!empty($CFG->enrol_mnet_allowed_categories)) {
 
@@ -132,7 +130,7 @@ class enrolment_plugin_mnet {
             "SELECT
                 id, name
             FROM
-                {$CFG->prefix}course_categories ca
+                {course_categories} ca
             WHERE
                 ca.id IN ({$CFG->enrol_mnet_allowed_categories})
                 OR ( $cats )
@@ -142,7 +140,7 @@ class enrolment_plugin_mnet {
                 ";
             unset($cats);
 
-            $rs = get_records_sql($query);
+            $rs = $DB->get_records_sql($query);
 
             if (!empty($rs)) {
                 $cats = array_keys($rs);
@@ -173,21 +171,19 @@ class enrolment_plugin_mnet {
                 co.defaultrole as defaultroleid,
                 r.name
             FROM
-                {$CFG->prefix}course_categories ca
+                {course_categories} ca
             JOIN
-                {$CFG->prefix}course co ON
-                ca.id = co.category
+                {course} co ON ca.id = co.category
             LEFT JOIN
-                {$CFG->prefix}role r ON
-                r.id = co.defaultrole
+                {role} r ON r.id = co.defaultrole
             WHERE
-                co.visible = '1' AND
-                co.enrollable = '1' $where
+                co.visible = 1 AND
+                co.enrollable = 1 $where
             ORDER BY
                 sortorder ASC
                 ";
 
-            return get_records_sql($query);
+            return $DB->get_records_sql($query);
 
         } elseif (!empty($CFG->enrol_mnet_allowed_courses)) {
 
@@ -208,22 +204,20 @@ class enrolment_plugin_mnet {
                     co.defaultrole as defaultroleid,
                     r.name
                 FROM
-                    {$CFG->prefix}course_categories ca
+                    {course_categories} ca
                 JOIN
-                    {$CFG->prefix}course co ON
-                    ca.id = co.category
+                    {course} co ON ca.id = co.category
                 LEFT JOIN
-                    {$CFG->prefix}role r ON
-                    r.id = co.defaultrole
+                    {role} r ON r.id = co.defaultrole
                 WHERE
-                    co.visible = '1' AND
-                    co.enrollable = '1' AND
-                    co.id in ({$CFG->enrol_mnet_allowed_courses})
+                    co.visible = 1 AND
+                    co.enrollable = 1 AND
+                    co.id IN ({$CFG->enrol_mnet_allowed_courses})
                 ORDER BY
                     sortorder ASC
                     ";
 
-            return get_records_sql($query);
+            return $DB->get_records_sql($query);
 
         }
 
@@ -246,9 +240,9 @@ class enrolment_plugin_mnet {
      *                              client machine
      */
     function course_enrolments($courseid, $roles = '') {
-        global $MNET_REMOTE_CLIENT, $CFG;
+        global $MNET_REMOTE_CLIENT, $CFG, $DB;
 
-        if (! $course = get_record('course', 'id', $courseid) ) {
+        if (! $course = $DB->get_record('course', array('id'=>$courseid))) {
             return 'no course';
             //error("That's an invalid course id");
         }
@@ -264,11 +258,11 @@ class enrolment_plugin_mnet {
                     r.name,
                     r.shortname
                 FROM
-                    {$CFG->prefix}role_assignments a,
-                    {$CFG->prefix}role r,
-                    {$CFG->prefix}user u
+                    {role_assignments} a,
+                    {role} r,
+                    {user} u
                 WHERE
-                    a.contextid = '{$context->id}' AND
+                    a.contextid = {$context->id} AND
                     a.roleid = r.id AND
                     a.userid = u.id AND
                     u.mnethostid = '{$MNET_REMOTE_CLIENT->id}'
@@ -280,7 +274,7 @@ class enrolment_plugin_mnet {
                     a.roleid in ('".str_replace(',',  "', '",  $roles)."')";
         } 
 
-        $enrolments = get_records_sql($sql);
+        $enrolments = $DB->get_records_sql($sql);
 
         $returnarray = array();
         foreach($enrolments as $user) {
@@ -300,27 +294,27 @@ class enrolment_plugin_mnet {
     * @return bool              Whether the enrolment has been successful
     */
     function enrol_user($user, $courseid) {
-        global $MNET, $MNET_REMOTE_CLIENT;
+        global $MNET, $MNET_REMOTE_CLIENT, $DB;
 
-        $userrecord = get_record('user','username',addslashes($user['username']), 'mnethostid',$MNET_REMOTE_CLIENT->id);
+        $userrecord = $DB->get_record('user',array('username'=>$user['username'], 'mnethostid'=>$MNET_REMOTE_CLIENT->id));
 
         if ($userrecord == false) {
             $userrecord = new stdClass();
-            $userrecord->username   = addslashes($user['username']);
-            $userrecord->email      = addslashes($user['email']);
-            $userrecord->firstname  = addslashes($user['firstname']);
-            $userrecord->lastname   = addslashes($user['lastname']);
+            $userrecord->username   = $user['username'];
+            $userrecord->email      = $user['email'];
+            $userrecord->firstname  = $user['firstname'];
+            $userrecord->lastname   = $user['lastname'];
             $userrecord->mnethostid = $MNET_REMOTE_CLIENT->id;
 
-            if ($userrecord->id = insert_record('user', $userrecord)) {
-                $userrecord = get_record('user','id', $userrecord->id);
+            if ($userrecord->id = $DB->insert_record('user', $userrecord)) {
+                $userrecord = $DB->get_record('user', array('id'=>$userrecord->id));
             } else {
                 // TODO: Error out
                 return false;
             }
         }
 
-        if (! $course = get_record('course', 'id', $courseid) ) {
+        if (! $course = $DB->get_record('course', array('id'=>$courseid))) {
             // TODO: Error out
             return false;
         }
@@ -343,15 +337,15 @@ class enrolment_plugin_mnet {
     * @return bool              Whether the user can login from the remote host
     */
     function unenrol_user($user, $courseid) {
-        global $MNET_REMOTE_CLIENT;
+        global $MNET_REMOTE_CLIENT, $DB;
 
-        $userrecord = get_record('user','username',$user['username'], 'mnethostid',$MNET_REMOTE_CLIENT->id);
+        $userrecord = $DB->get_record('user', array('username'=>$user['username'], 'mnethostid'=>$MNET_REMOTE_CLIENT->id));
 
         if ($userrecord == false) {
             // TODO: Error out
         }
 
-        if (! $course = get_record('course', 'id', $courseid) ) {
+        if (! $course = $DB->get_record('course', array('id'=>$courseid))) {
             // TODO: Error out
         }
 
@@ -381,23 +375,23 @@ class enrolment_plugin_mnet {
     * @return array
     */
     function list_remote_servers() {
-        global $CFG;
+        global $CFG, $DB;
 
         $sql = "
             SELECT DISTINCT 
                 h.id, 
                 h.name
             FROM 
-                {$CFG->prefix}mnet_host h,
-                {$CFG->prefix}mnet_host2service h2s,
-                {$CFG->prefix}mnet_service s
+                {mnet_host} h,
+                {mnet_host2service} h2s,
+                {mnet_service} s
             WHERE
                 h.id          = h2s.hostid   AND
                 h2s.serviceid = s.id         AND
                 s.name        = 'mnet_enrol' AND
                 h2s.subscribe = 1";
 
-        $res = get_records_sql($sql);
+        $res = $DB->get_records_sql($sql);
         if (is_array($res)) {
             return $res;
         } else {
@@ -412,9 +406,7 @@ class enrolment_plugin_mnet {
     * @return array              Whether the user can login from the remote host
     */
     function fetch_remote_courses($mnethostid) {
-        global $CFG;
-        global $USER;
-        global $MNET;
+        global $CFG, $USER, $MNET, $DB;
         require_once $CFG->dirroot . '/mnet/xmlrpc/client.php';
 
         // get the Service Provider info
@@ -435,9 +427,7 @@ class enrolment_plugin_mnet {
             $courses = $mnetrequest->response;
 
             // get the cached courses key'd on remote id - only need remoteid and id fields
-            $cachedcourses = get_records('mnet_enrol_course',
-                                         'hostid', $mnethostid,
-                                         'remoteid', 'remoteid, id' );
+            $cachedcourses = $DB->get_records('mnet_enrol_course', array('hostid'=>$mnethostid), 'remoteid', 'remoteid, id' );
 
             // Update cache and transform $courses into objects
             // in-place for the benefit of our caller...
@@ -459,22 +449,22 @@ class enrolment_plugin_mnet {
                 // sanitise strings for DB NOTE - these are not sane
                 // for printing, so we'll use a different object
                 $dbcourse = clone($course);
-                $dbcourse->cat_name        = addslashes($dbcourse->cat_name);
-                $dbcourse->cat_description = addslashes($dbcourse->cat_description);
-                $dbcourse->fullname        = addslashes($dbcourse->fullname);
-                $dbcourse->shortname       = addslashes($dbcourse->shortname);
-                $dbcourse->idnumber        = addslashes($dbcourse->idnumber);
-                $dbcourse->summary         = addslashes($dbcourse->summary);
-                $dbcourse->currency        = addslashes($dbcourse->currency);
-                $dbcourse->defaultrolename = addslashes($dbcourse->defaultrolename);
+                $dbcourse->cat_name        = $dbcourse->cat_name;
+                $dbcourse->cat_description = $dbcourse->cat_description;
+                $dbcourse->fullname        = $dbcourse->fullname;
+                $dbcourse->shortname       = $dbcourse->shortname;
+                $dbcourse->idnumber        = $dbcourse->idnumber;
+                $dbcourse->summary         = $dbcourse->summary;
+                $dbcourse->currency        = $dbcourse->currency;
+                $dbcourse->defaultrolename = $dbcourse->defaultrolename;
 
                 // insert or update
                 if (empty($cachedcourses[$course->remoteid])) {
-                    $course->id = insert_record('mnet_enrol_course', $dbcourse);
+                    $course->id = $DB->insert_record('mnet_enrol_course', $dbcourse);
                 } else {
                     $course->id = $cachedcourses[$course->remoteid]->id;
                     $cachedcourses[$course->remoteid]->seen=true;
-                    update_record('mnet_enrol_course', $dbcourse);
+                    $DB->update_record('mnet_enrol_course', $dbcourse);
                 }
                 // free tmp obj
                 unset($dbcourse);
@@ -490,7 +480,7 @@ class enrolment_plugin_mnet {
                     }
                 }
                 if (!empty($stale)) {
-                    delete_records_select('mnet_enrol_course', 'id IN ('.join(',',$stale).')');
+                    $DB->delete_records_select('mnet_enrol_course', 'id IN ('.join(',',$stale).')');
                 }
             }
 
@@ -512,17 +502,15 @@ class enrolment_plugin_mnet {
     * @return array              Whether the user can login from the remote host
     */
     function req_enrol_user($userid, $courseid) {
-        global $CFG;
-        global $USER;
-        global $MNET;
+        global $CFG, $USER, $MNET, $DB;
         require_once $CFG->dirroot . '/mnet/xmlrpc/client.php';
 
         // Prepare a basic user record
         // in case the remote host doesn't have it
-        $user = get_record('user', 'id', $userid, '','','','', 'username, email, firstname, lastname');
+        $user = $DB->get_record('user', array('id'=>$userid), 'username, email, firstname, lastname');
         $user = (array)$user;
 
-        $course = get_record('mnet_enrol_course', 'id', $courseid);
+        $course = $DB->get_record('mnet_enrol_course', array('id'=>$courseid));
 
         // get the Service Provider info
         $mnet_sp = new mnet_peer();
@@ -544,7 +532,7 @@ class enrolment_plugin_mnet {
                 $assignment->courseid = $course->id;
                 $assignment->enroltype = 'mnet';
                 // TODO: other fields
-                if (insert_record('mnet_enrol_assignments', $assignment)) {
+                if ($DB->insert_record('mnet_enrol_assignments', $assignment)) {
                     return true;
                 }
             }
@@ -560,16 +548,14 @@ class enrolment_plugin_mnet {
     * @return array              Whether the user can login from the remote host
     */
     function req_unenrol_user($userid, $courseid) {
-        global $CFG;
-        global $USER;
-        global $MNET;
+        global $CFG, $USER, $MNET, $DB;
         require_once $CFG->dirroot . '/mnet/xmlrpc/client.php';
 
         // in case the remote host doesn't have it
-        $user = get_record('user', 'id', $userid, '','','','', 'username, email');
+        $user = $DB->get_record('user', array('id'=>$userid), 'username, email');
         $user = $user->username;
 
-        $course = get_record('mnet_enrol_course', 'id', $courseid);
+        $course = $DB->get_record('mnet_enrol_course', array('id'=>$courseid));
 
         // get the Service Provider info
         $mnet_sp = new mnet_peer();
@@ -589,8 +575,8 @@ class enrolment_plugin_mnet {
         if ($mnetrequest->send($mnet_sp) === true) {
             if ($mnetrequest->response == true) {
                 // remove enrolment cached in mnet_enrol_assignments
-                delete_records_select('mnet_enrol_assignments',
-                                      "userid={$userid} AND courseid={$course->id}");
+                $DB->delete_records_select('mnet_enrol_assignments',
+                                      "userid=? AND courseid=?", array($userid, $course->id));
 
                 return true;
             }
index 0781d4ce7ff249003c96adad998988864a2d1dfa..1897d568a5af27ad07638ea39439b8d514be8ac9 100644 (file)
 
 /// get the user and course records
 
-    if (! $user = get_record("user", "id", $data->userid) ) {
+    if (! $user = $DB->get_record("user", array("id"=>$data->userid))) {
         email_paypal_error_to_admin("Not a valid user id", $data);
         die;
     }
 
-    if (! $course = get_record("course", "id", $data->courseid) ) {
+    if (! $course = $DB->get_record("course", array("id"=>$data->courseid))) {
         email_paypal_error_to_admin("Not a valid course id", $data);
         die;
     }
 
 
 
-            if ($existing = get_record("enrol_paypal", "txn_id", addslashes($data->txn_id))) {   // Make sure this transaction doesn't exist already
+            if ($existing = $DB->get_record("enrol_paypal", array("txn_id"=>$data->txn_id))) {   // Make sure this transaction doesn't exist already
                 email_paypal_error_to_admin("Transaction $data->txn_id is being repeated!", $data);
                 die;
 
 
             }
 
-            if (!$user = get_record('user', 'id', $data->userid)) {   // Check that user exists
+            if (!$user = $DB->get_record('user', array('id'=>$data->userid))) {   // Check that user exists
                 email_paypal_error_to_admin("User $data->userid doesn't exist", $data);
                 die;
             }
 
-            if (!$course = get_record('course', 'id', $data->courseid)) { // Check that course exists
+            if (!$course = $DB->get_record('course', array('id'=>$data->courseid))) { // Check that course exists
                 email_paypal_error_to_admin("Course $data->courseid doesn't exist", $data);;
                 die;
             }
 
             // ALL CLEAR !
 
-            if (!insert_record("enrol_paypal", addslashes_object($data))) {       // Insert a transaction record
+            if (!$DB->insert_record("enrol_paypal", $data)) {       // Insert a transaction record
                 email_paypal_error_to_admin("Error while trying to insert valid transaction", $data);
             }
 
 
 
         } else if (strcmp ($result, "INVALID") == 0) { // ERROR
-            insert_record("enrol_paypal", addslashes_object($data), false);
+            $DB->insert_record("enrol_paypal", $data, false);
             email_paypal_error_to_admin("Received an invalid payment notification!! (Fake payment?)", $data);
         }
     }
index 1f2f4c2dc298a211929ebb4136bd2d1f4f4a41d0..8d9ac6ff7ed69299ff333d9177ddc1c6c4844c38 100644 (file)
@@ -5,7 +5,7 @@
 
     $id = required_param('id', PARAM_INT);
 
-    if (!$course = get_record("course", "id", $id)) {
+    if (!$course = $DB->get_record("course", array("id"=>$id))) {
         redirect($CFG->wwwroot);
     }
 
index 435aaf54ca973536148a06cb27bb732191e3e08b..2abcefd07a02063a559afddb22b280ebcf0294a5 100644 (file)
@@ -26,7 +26,7 @@
     $confirm = optional_param('confirm', 0, PARAM_BOOL);
 
 
-    if (! $course = get_record("course", "id", $id) ) {
+    if (! $course = $DB->get_record("course", array("id"=>$id))) {
         print_error('invalidcourseid');
     }
 
index dea570cc77dcb2bdbf3af92b99240f4b4d7cea6a..037324c8d331e4de8930f406a70b8f4e290641c7 100644 (file)
@@ -26,7 +26,7 @@
     $confirm = optional_param('confirm', 0, PARAM_BOOL);\r
 \r
 \r
-    if (! $course = get_record("course", "id", $id) ) {\r
+    if (! $course = $DB->get_record("course", array("id"=>$id))) {\r
         print_error("invalidcourseid");\r
     }\r
 \r
index b87d1995bf96f2ab434401a7d52305a13af40f15..ee352e1da6cf3c7c72560d16157418f79a47a37e 100755 (executable)
@@ -222,11 +222,11 @@ class grade_category_test extends grade_test {
         $this->assertTrue(method_exists($category, 'generate_grades'));
         $category->load_grade_item();
 
-        $grades = get_records('grade_grades', 'itemid', $category->grade_item->id);
+        $grades = $DB->get_records('grade_grades', array('itemid'=>$category->grade_item->id));
         $this->assertFalse($grades);
 
         $category->generate_grades();
-        $grades = get_records('grade_grades', 'itemid', $category->grade_item->id);
+        $grades = $DB->get_records('grade_grades', array('itemid'=>$category->grade_item->id));
         $this->assertEqual(3, count($grades));
 
         $rawvalues = array();
index 52889457087baf58e71e475e79bcc99f10f5e1a2..a5b8ba8012e081764151b013fd5a94062557dc56 100755 (executable)
@@ -122,16 +122,16 @@ class eventslib_test extends UnitTestCase {
      */
     function test__events_update_definition__update() {
         // first modify directly existing handler
-        $handler = get_record('events_handlers', 'handlermodule', 'unittest', 'eventname', 'test_instant');
+        $handler = $DB->get_record('events_handlers', array('handlermodule'=>'unittest', 'eventname'=>'test_instant'));
 
         $original = $handler->handlerfunction;
 
         // change handler in db
-        set_field('events_handlers', 'handlerfunction', serialize('some_other_function_handler'), 'id', $handler->id);
+        $DB->set_field('events_handlers', 'handlerfunction', serialize('some_other_function_handler'), array('id'=>$handler->id));
 
         // update the definition, it should revert the handler back
         events_update_definition('unittest');
-        $handler = get_record('events_handlers', 'handlermodule', 'unittest', 'eventname', 'test_instant');
+        $handler = $DB->get_record('events_handlers', array('handlermodule'=>'unittest', 'eventname'=>'test_instant'));
         $this->assertEqual($handler->handlerfunction, $original, 'update should sync db with file definition: %s');
     }