From: toyomoyo Date: Wed, 22 Aug 2007 06:17:11 +0000 (+0000) Subject: MDL-10181, user management improvement fixes and new features X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=a7db355af191e3cbe45f5187e8add81deb2b165e;p=moodle.git MDL-10181, user management improvement fixes and new features --- diff --git a/admin/uploaduser.php b/admin/uploaduser.php index 6a4d5811b3..bbaab07be6 100755 --- a/admin/uploaduser.php +++ b/admin/uploaduser.php @@ -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 '

'; - 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 '

'; + 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 . '
'; + ++$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 . '
'; - $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 . '
'; + $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 . '
'; $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 . '
'; - $userserrors++; - } - } else { // new user - if ($user->id = insert_record('user', $user)) { - $info = ': ' . $username .' (ID = ' . $user->id . ')'; - echo $struseradded . $info . '
'; - $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 . '
'; + $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 . '
'; + // 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 . '
'; + } + } 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) . '
'; + } else { + notify(get_string('erroronline', 'error', $linenum). ': ' . get_string('enrolledincoursenot', '', $addcourse)); } - } - if ($ok) { // OK - echo $strindent . get_string('enrolledincourse', '', $addcourse) . '
'; - } 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) . '
'; + // 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) . '
'; + } 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 '

'; + 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 '

'; - 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 '
'; } diff --git a/admin/user/user_bulk.php b/admin/user/user_bulk.php index 3cad89c4fa..78695d5dcd 100755 --- a/admin/user/user_bulk.php +++ b/admin/user/user_bulk.php @@ -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); diff --git a/admin/user/user_bulk_form.php b/admin/user/user_bulk_form.php index d4b4038368..1fcae380f1 100644 --- a/admin/user/user_bulk_form.php +++ b/admin/user/user_bulk_form.php @@ -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() { diff --git a/lang/en_utf8/admin.php b/lang/en_utf8/admin.php index 3b481262a5..e053435417 100644 --- a/lang/en_utf8/admin.php +++ b/lang/en_utf8/admin.php @@ -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 Release Notes'; $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:'; diff --git a/lang/en_utf8/error.php b/lang/en_utf8/error.php index a66a34d48e..f3e084d807 100644 --- a/lang/en_utf8/error.php +++ b/lang/en_utf8/error.php @@ -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.
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.'; diff --git a/lang/en_utf8/filters.php b/lang/en_utf8/filters.php index 93c69919b2..1795bb3cf9 100644 --- a/lang/en_utf8/filters.php +++ b/lang/en_utf8/filters.php @@ -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 diff --git a/lang/en_utf8/help/uploadusers.html b/lang/en_utf8/help/uploadusers.html index 63f020d6d3..4d091abadf 100644 --- a/lang/en_utf8/help/uploadusers.html +++ b/lang/en_utf8/help/uploadusers.html @@ -9,11 +9,11 @@
  • The first record of the file is special, and contains a list of fieldnames. This defines the format of the rest of the file.

    Required fieldnames: these fields must be included in the first record, and defined for each user

    -

    firstname, lastname

    +

    firstname, lastname

    Optional fieldnames: 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.

    -

    institution, department, city, country, lang, auth, timezone, idnumber, icq, phone1, phone2, address, url, description, mailformat, maildisplay, htmleditor, autosubscribe, emailstop

    +

    institution, department, city, country, lang, auth, timezone, idnumber, icq, phone1, phone2, address, url, description, mailformat, maildisplay, htmleditor, autosubscribe, emailstop, deleted

    Enrolment fieldnames (optional): The course names are the "shortnames" 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.

    -

    course1, group1, type1, role1, course2, group2, type2, role2, etc.

    +

    course1, group1, type1, role1, course2, group2, type2, role2, etc.

  • Commas within the data should be encoded as &#44 - the script will automatically decode these back to commas.
  • @@ -28,6 +28,11 @@ jonest, verysecret, Tom, Jones, jonest@someplace.edu, en, 3663737, 1, Intro101, Section 1, 1
    reznort, somesecret, Trent, Reznor, reznort@someplace.edu, en_us, 6736733, 0, Advanced202, Section 3, 3

    +

    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:

    +

    username, password, firstname, lastname, country, city
    +jonest, verysecret, Tom, Jones, RO, Constanta
    +, somesecret, Trent, Reznor, , +

    Templates

    The default values are processed as templates in which the following codes are allowed:

    @@ -54,7 +59,7 @@ reznort, somesecret, Trent, Reznor, reznort@someplace.edu, en_us, 6736733, 0, Ad

    Template processing is done only on default values, and not on the values retrieved from the CSV file.

    In order to create corect Moodle usernames, the username is always converted to lowercase. Moreover, if the "Allow extended characters in usernames" 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).

    +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.

    When the "New username duplicate handling" 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.

    @@ -63,6 +68,14 @@ For example, if the CSV file contains the users named John Doe, Jane Doe and Jen

    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 Yes, the existing user account will be updated.

    -

    When updating existing accounts you can change usernames as well. Set "Allow renames" to Yes and include in your file a field called oldusername.

    +

    When updating existing accounts you can change usernames as well. Set "Allow renames" to Yes and include in your file a field called oldusername.

    Warning: any errors updating existing accounts can affect your users badly. Be careful when using the options to update.

    + +

    Deleting accounts

    +

    If the deleted field is present, users with value 1 for it will be deleted. In this case, all the fields may be omitted, except for username (which should be present in the CSV file, or a default value for it should be available).

    +

    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:

    +

    username, firstname, lastname, deleted
    +jonest, Tom, Jones, 0
    +reznort, , , 1 +

    diff --git a/user/filters/date.php b/user/filters/date.php new file mode 100755 index 0000000000..78f6c34577 --- /dev/null +++ b/user/filters/date.php @@ -0,0 +1,112 @@ +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 diff --git a/user/filters/profilefield.php b/user/filters/profilefield.php index 7a9ffd4f3b..40ed2ae649 100644 --- a/user/filters/profilefield.php +++ b/user/filters/profilefield.php @@ -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); } /** diff --git a/user/filters/radios.php b/user/filters/radios.php index fcad77e58e..ae7162a9f7 100644 --- a/user/filters/radios.php +++ b/user/filters/radios.php @@ -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')); } diff --git a/user/filters/select.php b/user/filters/select.php index 03c2ee9c07..4b1777b651 100644 --- a/user/filters/select.php +++ b/user/filters/select.php @@ -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); } /**