Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/tool/uploaduser/locallib.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00026 defined('MOODLE_INTERNAL') || die();
00027 
00028 define('UU_USER_ADDNEW', 0);
00029 define('UU_USER_ADDINC', 1);
00030 define('UU_USER_ADD_UPDATE', 2);
00031 define('UU_USER_UPDATE', 3);
00032 
00033 define('UU_UPDATE_NOCHANGES', 0);
00034 define('UU_UPDATE_FILEOVERRIDE', 1);
00035 define('UU_UPDATE_ALLOVERRIDE', 2);
00036 define('UU_UPDATE_MISSING', 3);
00037 
00038 define('UU_BULK_NONE', 0);
00039 define('UU_BULK_NEW', 1);
00040 define('UU_BULK_UPDATED', 2);
00041 define('UU_BULK_ALL', 3);
00042 
00043 define('UU_PWRESET_NONE', 0);
00044 define('UU_PWRESET_WEAK', 1);
00045 define('UU_PWRESET_ALL', 2);
00046 
00057 class uu_progress_tracker {
00058     private $_row;
00059     public $columns = array('status', 'line', 'id', 'username', 'firstname', 'lastname', 'email', 'password', 'auth', 'enrolments', 'suspended', 'deleted');
00060 
00065     public function start() {
00066         $ci = 0;
00067         echo '<table id="uuresults" class="generaltable boxaligncenter flexible-wrap" summary="'.get_string('uploadusersresult', 'tool_uploaduser').'">';
00068         echo '<tr class="heading r0">';
00069         echo '<th class="header c'.$ci++.'" scope="col">'.get_string('status').'</th>';
00070         echo '<th class="header c'.$ci++.'" scope="col">'.get_string('uucsvline', 'tool_uploaduser').'</th>';
00071         echo '<th class="header c'.$ci++.'" scope="col">ID</th>';
00072         echo '<th class="header c'.$ci++.'" scope="col">'.get_string('username').'</th>';
00073         echo '<th class="header c'.$ci++.'" scope="col">'.get_string('firstname').'</th>';
00074         echo '<th class="header c'.$ci++.'" scope="col">'.get_string('lastname').'</th>';
00075         echo '<th class="header c'.$ci++.'" scope="col">'.get_string('email').'</th>';
00076         echo '<th class="header c'.$ci++.'" scope="col">'.get_string('password').'</th>';
00077         echo '<th class="header c'.$ci++.'" scope="col">'.get_string('authentication').'</th>';
00078         echo '<th class="header c'.$ci++.'" scope="col">'.get_string('enrolments', 'enrol').'</th>';
00079         echo '<th class="header c'.$ci++.'" scope="col">'.get_string('suspended', 'auth').'</th>';
00080         echo '<th class="header c'.$ci++.'" scope="col">'.get_string('delete').'</th>';
00081         echo '</tr>';
00082         $this->_row = null;
00083     }
00084 
00089     public function flush() {
00090         if (empty($this->_row) or empty($this->_row['line']['normal'])) {
00091             // Nothing to print - each line has to have at least number
00092             $this->_row = array();
00093             foreach ($this->columns as $col) {
00094                 $this->_row[$col] = array('normal'=>'', 'info'=>'', 'warning'=>'', 'error'=>'');
00095             }
00096             return;
00097         }
00098         $ci = 0;
00099         $ri = 1;
00100         echo '<tr class="r'.$ri.'">';
00101         foreach ($this->_row as $key=>$field) {
00102             foreach ($field as $type=>$content) {
00103                 if ($field[$type] !== '') {
00104                     $field[$type] = '<span class="uu'.$type.'">'.$field[$type].'</span>';
00105                 } else {
00106                     unset($field[$type]);
00107                 }
00108             }
00109             echo '<td class="cell c'.$ci++.'">';
00110             if (!empty($field)) {
00111                 echo implode('<br />', $field);
00112             } else {
00113                 echo '&nbsp;';
00114             }
00115             echo '</td>';
00116         }
00117         echo '</tr>';
00118         foreach ($this->columns as $col) {
00119             $this->_row[$col] = array('normal'=>'', 'info'=>'', 'warning'=>'', 'error'=>'');
00120         }
00121     }
00122 
00131     public function track($col, $msg, $level = 'normal', $merge = true) {
00132         if (empty($this->_row)) {
00133             $this->flush(); //init arrays
00134         }
00135         if (!in_array($col, $this->columns)) {
00136             debugging('Incorrect column:'.$col);
00137             return;
00138         }
00139         if ($merge) {
00140             if ($this->_row[$col][$level] != '') {
00141                 $this->_row[$col][$level] .='<br />';
00142             }
00143             $this->_row[$col][$level] .= $msg;
00144         } else {
00145             $this->_row[$col][$level] = $msg;
00146         }
00147     }
00148 
00153     public function close() {
00154         $this->flush();
00155         echo '</table>';
00156     }
00157 }
00158 
00168 function uu_validate_user_upload_columns(csv_import_reader $cir, $stdfields, $profilefields, moodle_url $returnurl) {
00169     $columns = $cir->get_columns();
00170 
00171     if (empty($columns)) {
00172         $cir->close();
00173         $cir->cleanup();
00174         print_error('cannotreadtmpfile', 'error', $returnurl);
00175     }
00176     if (count($columns) < 2) {
00177         $cir->close();
00178         $cir->cleanup();
00179         print_error('csvfewcolumns', 'error', $returnurl);
00180     }
00181 
00182     $textlib = textlib_get_instance(); // profile fields may contain unicode chars
00183 
00184     // test columns
00185     $processed = array();
00186     foreach ($columns as $key=>$unused) {
00187         $field = $columns[$key];
00188         $lcfield = $textlib->strtolower($field);
00189         if (in_array($field, $stdfields) or in_array($lcfield, $stdfields)) {
00190             // standard fields are only lowercase
00191             $newfield = $lcfield;
00192 
00193         } else if (in_array($field, $profilefields)) {
00194             // exact profile field name match - these are case sensitive
00195             $newfield = $field;
00196 
00197         } else if (in_array($lcfield, $profilefields)) {
00198             // hack: somebody wrote uppercase in csv file, but the system knows only lowercase profile field
00199             $newfield = $lcfield;
00200 
00201         } else if (preg_match('/^(cohort|course|group|type|role|enrolperiod)\d+$/', $lcfield)) {
00202             // special fields for enrolments
00203             $newfield = $lcfield;
00204 
00205         } else {
00206             $cir->close();
00207             $cir->cleanup();
00208             print_error('invalidfieldname', 'error', $returnurl, $field);
00209         }
00210         if (in_array($newfield, $processed)) {
00211             $cir->close();
00212             $cir->cleanup();
00213             print_error('duplicatefieldname', 'error', $returnurl, $newfield);
00214         }
00215         $processed[$key] = $newfield;
00216     }
00217 
00218     return $processed;
00219 }
00220 
00227 function uu_increment_username($username) {
00228     global $DB, $CFG;
00229 
00230     if (!preg_match_all('/(.*?)([0-9]+)$/', $username, $matches)) {
00231         $username = $username.'2';
00232     } else {
00233         $username = $matches[1][0].($matches[2][0]+1);
00234     }
00235 
00236     if ($DB->record_exists('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
00237         return uu_increment_username($username);
00238     } else {
00239         return $username;
00240     }
00241 }
00242 
00249 function uu_process_template($template, $user) {
00250     if (is_array($template)) {
00251         // hack for for support of text editors with format
00252         $t = $template['text'];
00253     } else {
00254         $t = $template;
00255     }
00256     if (strpos($t, '%') === false) {
00257         return $template;
00258     }
00259 
00260     $username  = isset($user->username)  ? $user->username  : '';
00261     $firstname = isset($user->firstname) ? $user->firstname : '';
00262     $lastname  = isset($user->lastname)  ? $user->lastname  : '';
00263 
00264     $callback = partial('uu_process_template_callback', $username, $firstname, $lastname);
00265 
00266     $result = preg_replace_callback('/(?<!%)%([+-~])?(\d)*([flu])/', $callback, $t);
00267 
00268     if (is_null($result)) {
00269         return $template; //error during regex processing??
00270     }
00271 
00272     if (is_array($template)) {
00273         $template['text'] = $result;
00274         return $t;
00275     } else {
00276         return $result;
00277     }
00278 }
00279 
00283 function uu_process_template_callback($username, $firstname, $lastname, $block) {
00284     $textlib = textlib_get_instance();
00285 
00286     switch ($block[3]) {
00287         case 'u':
00288             $repl = $username;
00289             break;
00290         case 'f':
00291             $repl = $firstname;
00292             break;
00293         case 'l':
00294             $repl = $lastname;
00295             break;
00296         default:
00297             return $block[0];
00298     }
00299 
00300     switch ($block[1]) {
00301         case '+':
00302             $repl = $textlib->strtoupper($repl);
00303             break;
00304         case '-':
00305             $repl = $textlib->strtolower($repl);
00306             break;
00307         case '~':
00308             $repl = $textlib->strtotitle($repl);
00309             break;
00310     }
00311 
00312     if (!empty($block[2])) {
00313         $repl = $textlib->substr($repl, 0 , $block[2]);
00314     }
00315 
00316     return $repl;
00317 }
00318 
00327 function uu_supported_auths() {
00328     // only following plugins are guaranteed to work properly
00329     $whitelist = array('manual', 'nologin', 'none', 'email');
00330     $plugins = get_enabled_auth_plugins();
00331     $choices = array();
00332     foreach ($plugins as $plugin) {
00333         if (!in_array($plugin, $whitelist)) {
00334             continue;
00335         }
00336         $choices[$plugin] = get_string('pluginname', "auth_{$plugin}");
00337     }
00338 
00339     return $choices;
00340 }
00341 
00346 function uu_allowed_roles() {
00347     // let's cheat a bit, frontpage is guaranteed to exist and has the same list of roles ;-)
00348     $roles = get_assignable_roles(get_context_instance(CONTEXT_COURSE, SITEID), ROLENAME_ORIGINALANDSHORT);
00349     return array_reverse($roles, true);
00350 }
00351 
00356 function uu_allowed_roles_cache() {
00357     $allowedroles = get_assignable_roles(get_context_instance(CONTEXT_COURSE, SITEID), ROLENAME_SHORT);
00358     foreach ($allowedroles as $rid=>$rname) {
00359         $rolecache[$rid] = new stdClass();
00360         $rolecache[$rid]->id   = $rid;
00361         $rolecache[$rid]->name = $rname;
00362         if (!is_numeric($rname)) { // only non-numeric shortnames are supported!!!
00363             $rolecache[$rname] = new stdClass();
00364             $rolecache[$rname]->id   = $rid;
00365             $rolecache[$rname]->name = $rname;
00366         }
00367     }
00368     return $rolecache;
00369 }
 All Data Structures Namespaces Files Functions Variables Enumerations