]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-10181, user management improvement fixes and new features
authortoyomoyo <toyomoyo>
Wed, 22 Aug 2007 06:17:11 +0000 (06:17 +0000)
committertoyomoyo <toyomoyo>
Wed, 22 Aug 2007 06:17:11 +0000 (06:17 +0000)
admin/uploaduser.php
admin/user/user_bulk.php
admin/user/user_bulk_form.php
lang/en_utf8/admin.php
lang/en_utf8/error.php
lang/en_utf8/filters.php
lang/en_utf8/help/uploadusers.html
user/filters/date.php [new file with mode: 0755]
user/filters/profilefield.php
user/filters/radios.php
user/filters/select.php

index 6a4d5811b345dad5fd69aa9549e4d6174eb70ef7..bbaab07be6ae9440fe1bf1dd965c9e881e3d69f8 100755 (executable)
@@ -32,10 +32,15 @@ $strusernotrenamedmissing = get_string('usernotrenamedmissing', 'error');
 
 $struserupdated = get_string('useraccountupdated', 'admin');
 $strusernotupdated = get_string('usernotupdatederror', 'error');
-$strusernotadded = get_string('usernotaddedregistered', 'error');
 
 $struseradded = get_string('newuser');
+$strusernotadded = get_string('usernotaddedregistered', 'error');
 $strusernotaddederror = get_string('usernotaddederror', 'error');
+
+$struserdeleted = get_string('userdeleted', 'admin');
+$strusernotdeletederror = get_string('usernotdeletederror', 'error');
+$strusernotdeletedmissing = get_string('usernotdeletedmissing', 'error');
+
 $strcannotassignrole = get_string('cannotassignrole', 'error');
 $strduplicateusername = get_string('duplicateusername', 'error');
 $strindent = '-->';
@@ -69,7 +74,7 @@ if ( $formdata = $mform->get_data() ) {
         'maildisplay' => 1,
         'htmleditor' => 1,
         'autosubscribe' => 1,
-        'mnethostid' => 1
+        'mnethostid' => 0
         'institution' => 0,
         'department' => 0,
         'idnumber' => 0,
@@ -82,6 +87,7 @@ if ( $formdata = $mform->get_data() ) {
         'icq' => 0,
         'oldusername' => 0,
         'emailstop' => 1,
+        'deleted' => 0,
         'password' => !$createpassword,
     );
 
@@ -101,258 +107,302 @@ if ( $formdata = $mform->get_data() ) {
         $headers[$key] = $value; 
     }
 
-    $usersnew     = 0;
-    $usersupdated = 0;
-    $userserrors  = 0;
-    $renames      = 0;
-    $renameerrors = 0;
-    $newusernames = array();
-    // We'll need courses a lot, so fetch it early and keep it in memory, indexed by their shortname
-    $tmp =& get_courses('all','','id,shortname,visible');
-    $courses = array();
-    foreach ($tmp as $c) {
-        $courses[$c->shortname] = $c;
+    // check that required fields are present or a default value for them exists
+    $headersOk = true;
+    // disable the check if we also have deleting information (ie. deleted column)
+    if (!in_array('deleted', $headers)) {
+        foreach ($fields as $key => $required) {
+            if($required && !in_array($key, $headers) && (!isset($formdata->$key) || $formdata->$key==='')) { 
+                notify(get_string('missingfield', 'error', $key));
+                $headersOk = false;
+            }
+        }
     }
-    unset($tmp);
+    if($headersOk) {
+        $usersnew     = 0;
+        $usersupdated = 0;
+        $userserrors  = 0;
+        $usersdeleted = 0;
+        $renames      = 0;
+        $renameerrors = 0;
+        $deleteerrors = 0;
+        $newusernames = array();
+        // We'll need courses a lot, so fetch it early and keep it in memory, indexed by their shortname
+        $tmp =& get_courses('all','','id,shortname,visible');
+        $courses = array();
+        foreach ($tmp as $c) {
+            $courses[$c->shortname] = $c;
+        }
+        unset($tmp);
 
-    echo '<p id="results">';
-    while (!feof ($fp)) {
-        $user = new object();
-        // by default, use the local mnet id (this may be changed in the file)
-        $user->mnethostid = $CFG->mnet_localhost_id;
-        $line = explode($csv_delimiter, fgets($fp,LINE_MAX_SIZE));
-        ++$linenum;
-        $ok = true;
-        // add fields to user object
-        foreach ($line as $key => $value) {
-            if($value !== '') {
-                $key = $headers[$key];
-                //decode encoded commas
-                $value = str_replace($csv_encode,$csv_delimiter,trim($value));
-                // special fields: password and username
-                if ($key == 'password' && !empty($value)) {
-                    $user->$key = hash_internal_user_password($value);
-                } else if($key == 'username') {
-                    $user->$key = addslashes($textlib->strtolower($value));
-                } else {
-                    $user->$key = addslashes($value);
+        echo '<p id="results">';
+        while (!feof ($fp)) {
+            $errors = '';
+            $user = new object();
+            // by default, use the local mnet id (this may be changed in the file)
+            $user->mnethostid = $CFG->mnet_localhost_id;
+            $line = explode($csv_delimiter, fgets($fp,LINE_MAX_SIZE));
+            ++$linenum;
+            // add fields to user object
+            foreach ($line as $key => $value) {
+                if($value !== '') {
+                    $key = $headers[$key];
+                    //decode encoded commas
+                    $value = str_replace($csv_encode,$csv_delimiter,trim($value));
+                    // special fields: password and username
+                    if ($key == 'password' && !empty($value)) {
+                        $user->$key = hash_internal_user_password($value);
+                    } else if($key == 'username') {
+                        $value = $textlib->strtolower(addslashes($value));
+                        if(empty($CFG->extendedusernamechars)) {
+                            $value = eregi_replace('[^(-\.[:alnum:])]', '', $value);
+                        }
+                        @$newusernames[$value]++;
+                        $user->$key = $value;
+                    } else {
+                        $user->$key = addslashes($value);
+                    }
                 }
             }
-        }
-        // add default values for remaining fields
-        foreach ($fields as $key => $value) {
-            if(isset($user->$key)) {
-                continue;
-            }
-            if(!isset($formdata->$key) || $formdata->$key==='') { // no default value was submited
-                if($value) { // the field is required
-                    notify(get_string('erroronline', 'error', $linenum). ': ' .get_string('missingfield', 'error', $key));
-                    $ok = false;
+            
+            // add default values for remaining fields
+            foreach ($fields as $key => $required) {
+                if(isset($user->$key)) {
+                    continue;
                 }
-                continue;
-            }
-            // process templates
-            $template = $formdata->$key;
-            $templatelen = strlen($template);
-            $value = '';
-            for ($i = 0 ; $i < $templatelen; ++$i) {
-                if($template[$i] == '%') {
-                    $case = 0; // 1=lowercase, 2=uppercase
-                    $len = 0; // number of characters to keep
-                    $info = null; // data to process
-                    for($j = $i + 1; is_null($info) && $j < $templatelen; ++$j) {
-                        $car = $template[$j];
-                        if ($car >= '0' && $car <= '9') {
-                            $len = $len * 10 + (int)$car;
-                        } else if($car == '-') {
-                            $case = 1;
-                        } else if($car == '+') { 
-                            $case = 2;
-                        } else if($car == 'f') { // first name
-                            $info = @$user->firstname;
-                        } else if($car == 'l') { // last name
-                            $info = @$user->lastname;
-                        } else if($car == 'u') { // username
-                            $info = @$user->username;
-                        } else if($car == '%' && $j == $i+1) {
-                            $info = '%';
-                        } else { // invalid character
-                            $info = '';
-                        }
+                if(!isset($formdata->$key) || $formdata->$key==='') { // no default value was submited
+                    // if the field is required, give an error only if we are adding the user or deleting a user with unkown username
+                    if($required && (empty($user->deleted) || $key == 'username')) { 
+                        $errors .= get_string('missingfield', 'error', $key) . ' ';
                     }
-                    if($info==='' || is_null($info)) { // invalid template
-                        continue;
+                    continue;
+                }
+                // process templates
+                $template = $formdata->$key;
+                $templatelen = strlen($template);
+                $value = '';
+                for ($i = 0 ; $i < $templatelen; ++$i) {
+                    if($template[$i] == '%') {
+                        $case = 0; // 1=lowercase, 2=uppercase
+                        $len = 0; // number of characters to keep
+                        $info = null; // data to process
+                        for($j = $i + 1; is_null($info) && $j < $templatelen; ++$j) {
+                            $car = $template[$j];
+                            if ($car >= '0' && $car <= '9') {
+                                $len = $len * 10 + (int)$car;
+                            } else if($car == '-') {
+                                $case = 1;
+                            } else if($car == '+') { 
+                                $case = 2;
+                            } else if($car == 'f') { // first name
+                                $info = @$user->firstname;
+                            } else if($car == 'l') { // last name
+                                $info = @$user->lastname;
+                            } else if($car == 'u') { // username
+                                $info = @$user->username;
+                            } else if($car == '%' && $j == $i+1) {
+                                $info = '%';
+                            } else { // invalid character
+                                $info = '';
+                            }
+                        }
+                        if($info==='' || is_null($info)) { // invalid template
+                            continue;
+                        }
+                        $i = $j - 1;
+                        // change case
+                        if($case == 1) { 
+                            $info = $textlib->strtolower($info);
+                        } else if($case == 2) {
+                            $info = $textlib->strtoupper($info);
+                        }
+                        if($len) { // truncate data
+                            $info = $textlib->substr($info, 0, $len);
+                        }
+                        $value .= $info;
+                    } else {
+                        $value .= $template[$i];
                     }
-                    $i = $j - 1;
-                    // change case
-                    if($case == 1) { 
-                        $info = $textlib->strtolower($info);
-                    } else if($case == 2) {
-                        $info = $textlib->strtoupper($info);
+                }
+                
+                if($key == 'username') {
+                    $value = $textlib->strtolower($value);
+                    if(empty($CFG->extendedusernamechars)) {
+                        $value = eregi_replace('[^(-\.[:alnum:])]', '', $value);
                     }
-                    if($len) { // truncate data
-                        $info = $textlib->substr($info, 0, $len);
+                    @$newusernames[$value]++;
+                    // check for new username duplicates
+                    if($newusernames[$value] > 1) {
+                        if($skipduplicates) {
+                            $errors .= $strduplicateusername . ' (' . stripslashes($value) . '). ';
+                            continue;
+                        } else {
+                            $value .= $newusernames[$value];
+                        }
                     }
-                    $value .= $info;
-                } else {
-                    $value .= $template[$i];
                 }
+                $user->$key = $value;
             }
-            
-            if($key == 'username') {
-                $value = $textlib->strtolower($value);
-                if(empty($CFG->extendedusernamechars)) {
-                    $value = eregi_replace('[^(-\.[:alnum:])]', '', $value);
-                }
-                // check for new username duplicates
-                if(empty($newusernames[$value])) {
-                    $newusernames[$value] = 1;
-                } else {
-                    if($skipduplicates) {
-                        notify($strduplicateusername . ': ' . $value);
-                        $ok = false;
-                        continue;
+            if($errors) {
+                notify(get_string('erroronline', 'error', $linenum). ': ' . $errors);
+                ++$userserrors;
+                continue;
+            }
+
+            // delete user 
+            if(@$user->deleted) {
+                $info = ': ' . stripslashes($user->username) . '. ';
+                if($user =& get_record('user', 'username', $user->username, 'mnethostid', $user->mnethostid)) {
+                    $user->timemodified = time();
+                    $user->username     = addslashes($user->email . $user->timemodified);  // Remember it just in case
+                    $user->deleted      = 1;
+                    $user->email        = '';    // Clear this field to free it up
+                    $user->idnumber     = '';    // Clear this field to free it up
+                    if (update_record('user', $user)) {
+                        // not sure if this is needed. unenrol_student($user->id);  // From all courses
+                        delete_records('role_assignments', 'userid', $user->id); // unassign all roles
+                        // remove all context assigned on this user?
+                        echo $struserdeleted . $info . '<br />';
+                        ++$usersdeleted;
                     } else {
-                        ++$newusernames[$value];
-                        $value .= $newusernames[$value];
+                        notify(get_string('erroronline', 'error', $linenum). ': ' . $strusernotdeletederror . $info);
+                        ++$deleteerrors;
                     }
-                }
+                } else {
+                    notify(get_string('erroronline', 'error', $linenum). ': ' . $strusernotdeletedmissing . $info);
+                    ++$deleteerrors;
+                }            
+                continue;
             }
-            $user->$key = $value;
-        }
-        if(!$ok) {
-            ++$userserrors;
-            continue;
-        }
-
-        $user->confirmed = 1;
-        $user->timemodified = time();
-
-        // save the user to the database
-        $username = $user->username;
+            
+            // save the user to the database
+            $user->confirmed = 1;
+            $user->timemodified = time();
 
-        // before insert/update, check whether we should be updating an old record instead
-        if ($allowrenames && !empty($user->oldusername) ) {
-            $user->oldusername = $textlib->strtolower($user->oldusername);
-            $info = ': ' . $user->oldusername . '-->' . $user->username . '. ';
-            if ($olduser = get_record('user', 'username', $user->oldusername, 'mnethostid', $user->mnethostid)) {
-                if (set_field('user', 'username', $user->username, 'id', $olduser->id)) {
-                    echo $struserrenamed . $info;
-                    $renames++;
+            // before insert/update, check whether we should be updating an old record instead
+            if ($allowrenames && !empty($user->oldusername) ) {
+                $user->oldusername = $textlib->strtolower($user->oldusername);
+                $info = ': ' . stripslashes($user->oldusername) . '-->' . stripslashes($user->username) . '. ';
+                if ($olduser =& get_record('user', 'username', $user->oldusername, 'mnethostid', $user->mnethostid)) {
+                    if (set_field('user', 'username', $user->username, 'id', $olduser->id)) {
+                        echo $struserrenamed . $info;
+                        $renames++;
+                    } else {
+                        notify(get_string('erroronline', 'error', $linenum). ': ' . $strusernotrenamedexists . $info);
+                        $renameerrors++;
+                        continue;
+                    }
                 } else {
-                    notify($strusernotrenamedexists . $info);
+                    notify(get_string('erroronline', 'error', $linenum). ': ' . $strusernotrenamedmissing . $info);
                     $renameerrors++;
                     continue;
                 }
-            } else {
-                notify($strusernotrenamedmissing . $info);
-                $renameerrors++;
-                continue;
             }
-        }
 
-        // save the information
-        if ($olduser = get_record('user', 'username', $username, 'mnethostid', $user->mnethostid)) {
-            $user->id = $olduser->id;
-            $info = ': ' . $username .' (ID = ' . $user->id . ')';
-            if ($updateaccounts) {
-                // Record is being updated
-                if (update_record('user', $user)) {
-                    echo $struserupdated . $info . '<br />';
-                    $usersupdated++;
+            // save the information
+            if ($olduser =& get_record('user', 'username', $user->username, 'mnethostid', $user->mnethostid)) {
+                $user->id = $olduser->id;
+                $info = ': ' . stripslashes($user->username) .' (ID = ' . $user->id . ')';
+                if ($updateaccounts) {
+                    // Record is being updated
+                    if (update_record('user', $user)) {
+                        echo $struserupdated . $info . '<br />';
+                        $usersupdated++;
+                    } else {
+                        notify(get_string('erroronline', 'error', $linenum). ': ' . $strusernotupdated . $info);
+                        $userserrors++;
+                        continue;
+                    }
                 } else {
-                    notify($strusernotupdated . $info);
+                    //Record not added - user is already registered
+                    //In this case, output userid from previous registration
+                    //This can be used to obtain a list of userids for existing users
+                    echo $strusernotadded . $info . '<br />';
                     $userserrors++;
-                    continue;
                 }
-            } else {
-                //Record not added - user is already registered
-                //In this case, output userid from previous registration
-                //This can be used to obtain a list of userids for existing users
-                echo $strusernotadded . $info . '<br />';
-                $userserrors++;
-            }
-        } else { // new user
-            if ($user->id = insert_record('user', $user)) {
-                $info = ': ' . $username .' (ID = ' . $user->id . ')';
-                echo $struseradded . $info . '<br />';
-                $usersnew++;
-                if (empty($user->password) && $createpassword) {
-                    // passwords will be created and sent out on cron
-                    insert_record('user_preferences', array( 'userid' => $user->id, 'name'   => 'create_password', 'value'  => 1));
-                    insert_record('user_preferences', array( 'userid' => $user->id, 'name'   => 'auth_forcepasswordchange', 'value'  => 1));
+            } else { // new user
+                if ($user->id = insert_record('user', $user)) {
+                    $info = ': ' . stripslashes($user->username) .' (ID = ' . $user->id . ')';
+                    echo $struseradded . $info . '<br />';
+                    $usersnew++;
+                    if (empty($user->password) && $createpassword) {
+                        // passwords will be created and sent out on cron
+                        insert_record('user_preferences', array( 'userid' => $user->id, 'name'   => 'create_password', 'value'  => 1));
+                        insert_record('user_preferences', array( 'userid' => $user->id, 'name'   => 'auth_forcepasswordchange', 'value'  => 1));
+                    }
+                } else {
+                    // Record not added -- possibly some other error
+                    notify(get_string('erroronline', 'error', $linenum). ': ' . $strusernotaddederror . ': ' . stripslashes($user->username));
+                    $userserrors++;
+                    continue;
                 }
-            } else {
-                // Record not added -- possibly some other error
-                notify($strusernotaddederror . ': ' . $username);
-                $userserrors++;
-                continue;
             }
-        }
 
-        // find course enrolments, groups and roles/types
-        for($ncourses = 1; $addcourse = @$user->{'course' . $ncourses}; ++$ncourses) {
-            // find course
-            if(!$course = @$courses[$addcourse]) {
-                notify(get_string('unknowncourse', 'error', $addcourse));
-                continue;
-            }
-            // find role
-            if ($addrole = @$user->{'role' . $ncourses}) {
-                $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
-                if (!$ok = role_assign($addrole, $user->id, 0, $coursecontext->id)) {
-                    echo $strindent . $strcannotassignrole . '<br >';
+            // find course enrolments, groups and roles/types
+            for($ncourses = 1; $addcourse = @$user->{'course' . $ncourses}; ++$ncourses) {
+                // find course
+                if(!$course = @$courses[$addcourse]) {
+                    notify(get_string('erroronline', 'error', $linenum). ': ' . get_string('unknowncourse', 'error', $addcourse));
+                    continue;
                 }
-            } else {
-                // if no role, then find "old" enrolment type
-                switch ($addtype = @$user->{'type' . $ncourses}) {
-                    case 2:   // teacher
-                        $ok = add_teacher($user->id, $course->id, 1);
-                        break;
-                    case 3:   // non-editing teacher
-                        $ok = add_teacher($user->id, $course->id, 0);
-                        break;
-                    case 1:   // student
-                    default:  
-                        $ok = enrol_student($user->id, $course->id);
-                        break;
+                // find role
+                if ($addrole = @$user->{'role' . $ncourses}) {
+                    $coursecontext =& get_context_instance(CONTEXT_COURSE, $course->id);
+                    if (!$ok = role_assign($addrole, $user->id, 0, $coursecontext->id)) {
+                        echo $strindent . $strcannotassignrole . '<br >';
+                    }
+                } else {
+                    // if no role, then find "old" enrolment type
+                    switch ($addtype = @$user->{'type' . $ncourses}) {
+                        case 2:   // teacher
+                            $ok = add_teacher($user->id, $course->id, 1);
+                            break;
+                        case 3:   // non-editing teacher
+                            $ok = add_teacher($user->id, $course->id, 0);
+                            break;
+                        case 1:   // student
+                        default:  
+                            $ok = enrol_student($user->id, $course->id);
+                            break;
+                    }
+                }
+                if ($ok) {   // OK
+                    echo $strindent . get_string('enrolledincourse', '', $addcourse) . '<br />';
+                } else {
+                    notify(get_string('erroronline', 'error', $linenum). ': ' . get_string('enrolledincoursenot', '', $addcourse));
                 }
-            }
-            if ($ok) {   // OK
-                echo $strindent . get_string('enrolledincourse', '', $addcourse) . '<br />';
-            } else {
-                notify(get_string('enrolledincoursenot', '', $addcourse));
-            }
 
-            // find group to add to
-            if ($addgroup = @$user->{'group' . $ncourses}) {
-                if ($gid = groups_get_group_by_name($course->id, $addgroup)) {
-                    $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
-                    if (count(get_user_roles($coursecontext, $user->id))) {
-                        if (groups_add_member($gid, $user->id)) {
-                            echo $strindent . get_string('addedtogroup','',$addgroup) . '<br />';
+                // find group to add to
+                if ($addgroup = @$user->{'group' . $ncourses}) {
+                    if ($gid =& groups_get_group_by_name($course->id, $addgroup)) {
+                        $coursecontext =& get_context_instance(CONTEXT_COURSE, $course->id);
+                        if (count(get_user_roles($coursecontext, $user->id))) {
+                            if (groups_add_member($gid, $user->id)) {
+                                echo $strindent . get_string('addedtogroup','',$addgroup) . '<br />';
+                            } else {
+                                notify(get_string('erroronline', 'error', $linenum). ': ' . get_string('addedtogroupnot','',$addgroup));
+                            }
                         } else {
-                            notify(get_string('addedtogroupnot','',$addgroup));
+                            notify(get_string('erroronline', 'error', $linenum). ': ' . get_string('addedtogroupnotenrolled','',$addgroup));
                         }
                     } else {
-                        notify(get_string('addedtogroupnotenrolled','',$addgroup));
+                        notify(get_string('erroronline', 'error', $linenum). ': ' . get_string('groupunknown','error',$addgroup));
                     }
-                } else {
-                    notify(get_string('groupunknown','error',$addgroup));
                 }
             }
         }
+        echo '</p>';
+        notify(get_string('userscreated', 'admin') . ': ' . $usersnew);
+        notify(get_string('usersupdated', 'admin') . ': ' . $usersupdated);
+        notify(get_string('usersdeleted', 'admin') . ': ' . $usersdeleted);
+        notify(get_string('deleteerrors', 'admin') . ': ' . $deleteerrors);
+        if ($allowrenames) {
+            notify(get_string('usersrenamed', 'admin') . ': ' . $renames);
+            notify(get_string('renameerrors', 'admin') . ': ' . $renameerrors);
+        }
+        notify(get_string('errors', 'admin') . ': ' . $userserrors);
     }
-    echo '</p>';
-    
     fclose($fp);
-    notify(get_string('userscreated', 'admin') . ': ' . $usersnew);
-    notify(get_string('usersupdated', 'admin') . ': ' . $usersupdated);
-    notify(get_string('errors', 'admin') . ': ' . $userserrors);
-    if ($allowrenames) {
-        notify(get_string('usersrenamed', 'admin') . ': ' . $renames);
-        notify(get_string('renameerrors', 'admin') . ': ' . $renameerrors);
-    }
     echo '<hr />';
 }
 
index 3cad89c4fa2291c815add6394095887de6a750b5..78695d5dcdd45b16fe7f0cc66b11d174f495c53e 100755 (executable)
@@ -4,6 +4,7 @@
     require_once($CFG->libdir.'/dmllib.php');
 
     require_once($CFG->dirroot.'/user/filters/text.php');
+    require_once($CFG->dirroot.'/user/filters/date.php');
     require_once($CFG->dirroot.'/user/filters/select.php');
     require_once($CFG->dirroot.'/user/filters/courserole.php');
     require_once($CFG->dirroot.'/user/filters/globalrole.php');
@@ -47,6 +48,7 @@
     $filters[] = new user_filter_profilefield('profile', get_string('profile'));
     $filters[] = new user_filter_courserole('course', get_string('courserole', 'filters'));
     $filters[] = new user_filter_globalrole('system', get_string('globalrole', 'role'));
+    $filters[] = new user_filter_date('date', get_string('date'), 'lastaccess', array('lastlogin'=> get_string('lastlogin'), 'firstaccess' => get_string('firstaccess', 'filters'), 'lastaccess' => get_string('lastaccess'), 'timemodified' => get_string('lastmodified')));
     
     // create the user filter form
     $user_filter_form =& new user_filter_form(null, $filters);
index d4b403836894348d3d2fb12cecf125c049201fbf..1fcae380f1247f69946ebb57d4d820eba55d01be 100644 (file)
@@ -53,7 +53,7 @@ class user_bulk_form extends moodleform {
         }
         $obj =& $this->_form->getElement('comment');
         $obj->setLabel($comment);
-        $obj->setText(get_string('usersfound', 'bulkusers', $count) . ' ' .get_string('usersselected', 'bulkusers', count($SESSION->bulk_susers)));
+        $obj->setText(get_string('usersfound', 'bulkusers', $count) . ' ' .get_string('usersselected', 'bulkusers', count(@$SESSION->bulk_susers)));
     }
     
     function definition_after_data() {
index 3b481262a54d1b750cd31fdd99ce90570935e2af..e0534354176e1afdaf112e60cfcde2c4aa2f5692 100644 (file)
@@ -254,6 +254,7 @@ $string['defaultcourseroleid'] = 'Default role for users in a course';
 $string['defaultrequestcategory'] = 'Default category for course requests';
 $string['defaultuserroleid'] = 'Default role for all users';
 $string['defaultvalues'] = 'Default values';
+$string['deleteerrors'] = 'Delete errors';
 $string['deleteunconfirmed'] = 'Delete unconfirmed users after';
 $string['deleteuser'] = 'Delete user';
 $string['density'] = 'Density';
@@ -526,7 +527,7 @@ $string['rcache'] = 'Record cache';
 $string['rcachettl'] = 'Record cache TTL';
 $string['releasenoteslink'] = 'For information about this version of Moodle, please see the online <a target=\"_new\" href=\"$a\">Release Notes</a>';
 $string['remotelangnotavailable'] = 'Because Moodle can not connect to download.moodle.org, we are unable to do language pack installation automatically. Please download the appropriate zip file(s) from the list below, copy them to your $a directory and unzip them manually.';
-$string['renameerrors'] = 'Errors in renames';
+$string['renameerrors'] = 'Rename errors';
 $string['restrictbydefault'] = 'Restrict modules by default';
 $string['restrictmodulesfor'] = 'Restrict modules for';
 $string['riskconfig'] = 'Users could change site configuration and behaviour';
@@ -626,12 +627,14 @@ $string['upwards'] = 'upwards';
 $string['usehtmleditor'] = 'Use HTML editor';
 $string['useraccountupdated'] = 'User updated';
 $string['userbulk'] = 'Bulk user actions';
+$string['userdeleted'] = 'User deleted';
 $string['userlist'] = 'Browse list of users';
 $string['userpolicies'] = 'User policies';
 $string['userrenamed'] = 'User renamed';
 $string['users'] = 'Users';
 $string['userscreated'] = 'Users created';
 $string['usersrenamed'] = 'Users renamed';
+$string['usersdeleted'] = 'Users deleted';
 $string['usersupdated'] = 'Users updated';
 $string['usetags'] = 'Enable tags functionality';
 $string['validateerror'] = 'This value was not valid:';
index a66a34d48ea4a5d16d2921ed1d871225b75b0330..f3e084d8072690a3e3acda4c8ee6979a5fadc60e 100644 (file)
@@ -58,7 +58,7 @@ $string['listcantmoveleft'] = 'Failed to move item left, it has no parent.';
 $string['listcantmoveright'] = 'Failed to move item right, their is no peer to make it a child of. Move it below another peer and then you can move it right.';
 $string['loginasonecourse'] = 'You can not enter this course.<br /> You have to terminate the \"Login as\" session before entering any other course.';
 $string['loginasnoenrol'] = 'You can not use enrol or unenrol when in course \"Login as\" session.';
-$string['missingfield'] = 'Field \"$a\" is missing';
+$string['missingfield'] = 'Field \"$a\" is missing.';
 $string['missingrequiredfield'] = 'Some required field is missing';
 $string['modulemissingcode'] = 'Module $a is missing the code needed to perform this function';
 $string['modulerequirementsnotmet'] = 'Module \"$a->modulename\" ($a->moduleversion) could not be installed.  It requires a newer version of Moodle (currently you are using $a->currentmoodle, you need $a->requiremoodle).';
@@ -90,6 +90,8 @@ $string['unknowncourseidnumber'] = 'Unknown Course ID \"$a\"';
 $string['unknownuseraction'] = 'Sorry, I do not understand this user action.';
 $string['usernotaddederror'] = 'User not added - unknown error';
 $string['usernotaddedregistered'] = 'User not added - already registered';
+$string['usernotdeletederror'] = 'User not deleted - unknown error';
+$string['usernotdeletedmissing'] = 'User not deleted - could not find the username.';
 $string['usernotavailable'] = 'The details of this user are not available to you.';
 $string['usernotrenamedexists'] = 'User not renamed -- the new username is already in use.';
 $string['usernotrenamedmissing'] = 'User not renamed -- could not find the old username.';
index 93c69919b2b058d76389945495ded4930a586b5c..1795bb3cf97f51db0303082eb71fc70acaa093d0 100644 (file)
@@ -17,8 +17,12 @@ $string['isnotequalto'] = 'isn\'t equal to';
 $string['startswith'] = 'starts with';
 $string['endswith'] = 'ends with';
 $string['isempty'] = 'is empty';
+$string['isbefore'] = 'is before $a';
+$string['isafter'] = 'is after $a';
+$string['isbetween'] = 'is between $a[0] and $a[1]';
 $string['isnotdefined'] = 'isn\'t defined';
 $string['isdefined'] = 'is defined';
 $string['isanyvalue'] = 'is any value';
 $string['categoryrole'] = 'Category role';
 $string['courserole'] = 'Course role';
+$string['firstaccess'] = 'First access';
\ No newline at end of file
index 63f020d6d31eff7fa4092faad8cfa56d452c84c5..4d091abadf00eee10a095d3e178bdb2ffbb090aa 100644 (file)
@@ -9,11 +9,11 @@
   <li>The first record of the file is special, and contains a list of fieldnames. This defines the format of the rest of the file.
     <blockquote>
       <p><strong>Required fieldnames:</strong> these fields must be included in the first record, and defined for each user</p>
-      <p><code class="example1">firstname, lastname</code></p>
+      <p><code>firstname, lastname</code></p>
       <p><strong>Optional fieldnames: </strong>all of these are completely optional. If a values is present for the field in the file, then that value is used; else, the default value for that field is used.</p>
-      <p> <code class="example1">institution, department, city, country, lang, auth, timezone, idnumber, icq, phone1, phone2, address, url, description, mailformat, maildisplay, htmleditor, autosubscribe, emailstop</code></p>
+      <p> <code>institution, department, city, country, lang, auth, timezone, idnumber, icq, phone1, phone2, address, url, description, mailformat, maildisplay, htmleditor, autosubscribe, emailstop, deleted</code></p>
       <p><strong>Enrolment fieldnames (optional): </strong>The  course names are the &quot;shortnames&quot; of the courses - if present then the user will be enrolled in those courses. For groups use group name; for roles use id. Group names must be associated to the corresponding courses, i.e. group1 to course1, etc.</p>
-      <p><code class="example1">course1, group1, type1, role1, course2, group2, type2, role2, etc.</code></p>
+      <p><code>course1, group1, type1, role1, course2, group2, type2, role2, etc.</code></p>
     </blockquote>
     </li>
   <li>Commas within the data should be encoded as &amp;#44 - the script will automatically decode these back to commas. </li>
 jonest, verysecret, Tom, Jones, jonest@someplace.edu, en, 3663737, 1, Intro101, Section 1, 1<br />
 reznort, somesecret, Trent, Reznor, reznort@someplace.edu, en_us, 6736733, 0, Advanced202, Section 3, 3
 </code></p>
+<p>The CSV file may contain full informations for some users and use default values for others (use extra commas to corectly associate data to headers). For example, the following file will use default values for username, city and country for user Trent Reznor:</p>
+<p><code>username, password, firstname, lastname, country, city<br />
+jonest, verysecret, Tom, Jones, RO, Constanta<br />
+, somesecret, Trent, Reznor, ,
+</code></p>
 
 <h2>Templates</h2>
 <p>The default values are processed as templates in which the following codes are allowed:</p>
@@ -54,7 +59,7 @@ reznort, somesecret, Trent, Reznor, reznort@someplace.edu, en_us, 6736733, 0, Ad
 </ul>
 <p>Template processing is done only on default values, and not on the values retrieved from the CSV file.</p>
 <p>In order to create corect Moodle usernames, the username is always converted to lowercase. Moreover, if the &quot;Allow extended characters in usernames&quot; option in the Site policies page is off, characters different to letters, digits, dash (-) and dot (.) are removed. 
-For example if the firstname is John Jr. and the lastname is Doe, the username %-f%-l will produce john jr.doe when Allow extended characters in usernames is on, and johnjr.doe when off (notice the extra space).</p>
+For example if the firstname is John Jr. and the lastname is Doe, the username %-f_%-l will produce john jr._doe when Allow extended characters in usernames is on, and johnjr.doe when off.</p>
 <p>When the &quot;New username duplicate handling&quot; setting is set to Append counter, an auto-increment counter will be append to duplicate usernames produced by the template.
 For example, if the CSV file contains the users named John Doe, Jane Doe and Jenny Doe without explicit usernames, the default username is %-1f%-l and New username duplicate handling is set to Append counter, then the usernames produced will be jdoe, jdoe2 and jdoe3.
 </p>
@@ -63,6 +68,14 @@ For example, if the CSV file contains the users named John Doe, Jane Doe and Jen
 
 <p>By default Moodle assumes that you will be creating new user accounts, and skips records where the username matches an existing account. However, if you set "Update existing accounts" to <b>Yes</b>, the existing user account will be updated. </p>
 
-<p>When updating existing accounts you can change usernames as well. Set "Allow renames" to <b>Yes</b> and include in your file a field called <code class="example1">oldusername</code>.</p>
+<p>When updating existing accounts you can change usernames as well. Set "Allow renames" to <b>Yes</b> and include in your file a field called <code>oldusername</code>.</p>
 
 <p><b>Warning:</b> any errors updating existing accounts can affect your users badly. Be careful when using the options to update.</p>
+
+<h2>Deleting accounts</h2>
+<p>If the <code>deleted</code> field is present, users with value 1 for it will be deleted. In this case, all the fields may be omitted, except for <code>username</code> (which should be present in the CSV file, or a default value for it should be available).</p>
+<p>Deleting and uploading accounts could be done with a single CSV file. For example, the following file will add the user Tom Jones and delete the user reznort:</p>
+<p><code>username, firstname, lastname, deleted<br />
+jonest, Tom, Jones, 0<br />
+reznort, , , 1
+</code></p>
diff --git a/user/filters/date.php b/user/filters/date.php
new file mode 100755 (executable)
index 0000000..78f6c34
--- /dev/null
@@ -0,0 +1,112 @@
+<?php //$Id$
+
+require_once($CFG->dirroot . '/user/filters/lib.php'); 
+
+/**
+ * Generic filter based on a date.
+ */
+class user_filter_date extends user_filter_type {
+    /**
+     * the end Unix timestamp (0 if disabled) 
+     */
+    var $_value2;
+    /**
+     * the fields available for comparisson
+     */
+    var $_fields;
+    /**
+     * Constructor
+     * @param string $name the name of the filter instance
+     * @param string $label the label of the filter instance
+     * @param string $field the field used for filtering data
+     * @param int $start the start Unix timestamp (0 if disabled) 
+     * @param int $end the end Unix timestamp (0 if disabled)
+     */
+    function user_filter_date($name, $label, $field, $fields=null, $start=0, $end=0) {
+        parent::user_filter_type($name, $label, $field, $start);
+        $this->_value2 = $end;
+        $this->_fields = $fields;
+    }
+    
+    /**
+     * Adds controls specific to this filter in the form.
+     * @param object $mform a MoodleForm object to setup
+     */
+    function setupForm(&$mform) {
+        $objs = array();
+        if(is_array($this->_fields)) {
+            $objs[] =& $mform->createElement('select', $this->_name . '_fld', null, $this->_fields);
+        }
+        $objs[] =& $mform->createElement('checkbox', $this->_name . '_sck', null, get_string('isafter', 'filters'));
+        $objs[] =& $mform->createElement('date_selector', $this->_name . '_sdt', null);
+        $objs[] =& $mform->createElement('checkbox', $this->_name . '_eck', null, get_string('isbefore', 'filters'));
+        $objs[] =& $mform->createElement('date_selector', $this->_name . '_edt', null);
+        $grp =& $mform->addElement('group', $this->_name . '_grp', $this->_label, $objs, '', false);
+        $grp->setHelpButton(array('date','','filters'));
+        $mform->setDefault($this->_name . '_sck', !empty($this->_value));
+        $mform->setDefault($this->_name . '_eck', !empty($this->_value2));
+        $mform->setDefault($this->_name . '_sdt', $this->_value);
+        $mform->setDefault($this->_name . '_edt', $this->_value2);
+        if(is_array($this->_fields)) {
+            $mform->setDefault($this->_name . '_fld', $this->_field);
+        }
+        $mform->disabledIf($this->_name . '_sdt', $this->_name . '_sck');
+        $mform->disabledIf($this->_name . '_edt', $this->_name . '_eck');
+    }
+    
+    /**
+     * Retrieves data from the form data
+     * @param object $formdata data submited with the form
+     */
+    function checkData($formdata) {
+        $fld = $this->_name . '_fld';
+        $sdt = $this->_name . '_sdt';
+        $edt = $this->_name . '_edt';
+        $sck = $this->_name . '_sck';
+        $eck = $this->_name . '_eck';
+        if(@$formdata->$fld) {
+            $this->_field = @$formdata->$fld;
+        }
+        $this->_value = @$formdata->$sck ? (int)@$formdata->$sdt : 0;
+        $this->_value2 = @$formdata->$eck ? (int)@$formdata->$edt : 0;
+    }
+
+    /**
+     * Returns the condition to be used with SQL where
+     * @return string the filtering condition or null if the filter is disabled
+     */
+    function getSQLFilter() {
+        if(empty($this->_value) && empty($this->_value2)) {
+            return null;
+        }
+        $res = $this->_field . '>0' ;
+        if($this->_value) {
+            $res .= ' AND ' . $this->_field . '>=' . $this->_value;
+        }
+        if($this->_value2) {
+            $res .= ' AND ' . $this->_field . '<=' . $this->_value2;
+        }
+        return $res;
+    }
+    
+    /**
+     * Returns a human friendly description of the filter.
+     * @return string filter description
+     */
+    function getDescription() {
+        if(is_array($this->_fields)) {
+            $res = $this->_fields[$this->_field] . ' ';
+        } else {
+            $res = $this->_label . ' ';
+        }
+        if($this->_value && $this->_value2) {
+            $res .= get_string('isbetween', 'filters', array(userdate($this->_value), userdate($this->_value2)));
+        } else if($this->_value) {
+            $res .= get_string('isafter', 'filters', userdate($this->_value));
+        } else {
+            $res .= get_string('isbefore', 'filters', userdate($this->_value2));
+        }
+        return $res;
+    }
+}
+?>
\ No newline at end of file
index 7a9ffd4f3b98562b0d422ac887b8ed3941df6e18..40ed2ae649f2aa6c32bd95b60295c6aac3958b68 100644 (file)
@@ -77,6 +77,9 @@ class user_filter_profilefield extends user_filter_type {
         $objs[] =& $mform->createElement('text', $this->_name, null);
         $grp =& $mform->addElement('group', $this->_name . '_grp', $this->_label, $objs, '', false);
         $grp->setHelpButton(array('profilefield','','filters'));
+        $mform->setDefault($this->_name . '_fld', $this->_profile_field);
+        $mform->setDefault($this->_name . '_op', $this->_operator);
+        $mform->setDefault($this->_name, $this->_value);
     }
     
     /**
index fcad77e58ee4a9aada007324edc8c94f17fc874e..ae7162a9f77bc8ae4d81abbae4775e5de3fd81b9 100644 (file)
@@ -46,7 +46,7 @@ class user_filter_radios extends user_filter_type {
             $objs[] =& $mform->createElement('radio', $this->_name, null, $v, $k);
         }
         $grp =& $mform->addElement('group', $this->_name . '_grp', $this->_label, $objs, '', false);
-        $mform->setDefault($this->_name, -1);
+        $mform->setDefault($this->_name, $this->_value);
         $grp->setHelpButton(array('radios','','filters'));
     }
     
index 03c2ee9c07a3e5b4eed12106cb4a7f0caaf50219..4b1777b651e1005b8164455f0857b0b936453a21 100644 (file)
@@ -50,6 +50,8 @@ class user_filter_select extends user_filter_type {
         $objs[] =& $mform->createElement('select', $this->_name, null, $this->_options);
         $grp =& $mform->addElement('group', $this->_name . '_grp', $this->_label, $objs, '', false);
         $grp->setHelpButton(array('select','','filters'));
+        $mform->setDefault($this->_name . '_op', $this->_operator);
+        $mform->setDefault($this->_name, $this->_value);
     }
     
     /**