📄 usermanager.lib.php
字号:
$resf = api_sql_query($sqlf,__FILE__,__LINE__); if(Database::num_rows($resf)>0) { while($rowf = Database::fetch_array($resf)) { $sqlv = "SELECT * FROM $t_ufv WHERE field_id = ".$rowf['id']." AND user_id = ".$user['user_id']." ORDER BY id DESC"; $resv = api_sql_query($sqlv,__FILE__,__LINE__); if(Database::num_rows($resv)>0) { //There should be only one value for a field and a user $rowv = Database::fetch_array($resv); $user['extra'][$rowf['field_variable']] = $rowv['field_value']; } else { $user['extra'][$rowf['field_variable']] = ''; } } } } else { $user = false; } return $user; } //for survey function get_teacher_list($course_id, $sel_teacher='') { $user_course_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER); $user_table = Database :: get_main_table(TABLE_MAIN_USER); $sql_query = "SELECT * FROM $user_table a, $user_course_table b where a.user_id=b.user_id AND b.status=1 AND b.course_code='$course_id'"; $sql_result = api_sql_query($sql_query,__FILE__,__LINE__); echo "<select name=\"author\">"; while ($result = Database::fetch_array($sql_result)) { if($sel_teacher==$result['user_id']) $selected ="selected"; echo "\n<option value=\"".$result['user_id']."\" $selected>".$result['firstname']."</option>"; } echo "</select>"; } /** * Get user picture URL or path from user ID (returns an array). * The return format is a complete path, enabling recovery of the directory * with dirname() or the file with basename(). This also works for the * functions dealing with the user's productions, as they are located in * the same directory. * @param integer User ID * @param string Type of path to return (can be 'none','system','rel','web') * @param bool Whether we want to have the directory name returned 'as if' there was a file or not (in the case we want to know which directory to create - otherwise no file means no split subdir) * @param bool If we want that the function returns the /main/img/unknown.jpg image set it at true * @return array Array of 2 elements: 'dir' and 'file' which contain the dir and file as the name implies if image does not exist it will return the unknow image if anonymous parameter is true if not it returns an empty array */ function get_user_picture_path_by_id($id,$type='none',$preview=false,$anonymous=false) { if(empty($id) or empty($type)) { if ($anonymous) { $dir=''; switch($type) { case 'system': //return the complete path to the file, from root $dir = api_get_path(SYS_CODE_PATH).'img/'; break; case 'rel': //return the relative path to the file, from the Dokeos base dir $dir = api_get_path(REL_CODE_PATH).'img/'; break; case 'web': //return the complete web URL to the file $dir = api_get_path(WEB_CODE_PATH).'img/'; break; case 'none': //return only the picture_uri (as is, without subdir) default: break; } $file_anonymous='unknown.jpg'; return array('dir'=>$dir,'file'=>$file_anonymous); } else { return array('dir'=>'','file'=>''); } } $user_id = intval($id); $user_table = Database :: get_main_table(TABLE_MAIN_USER); $sql = "SELECT picture_uri FROM $user_table WHERE user_id=".$user_id; $res = api_sql_query($sql,__FILE__,__LINE__); $user=array(); if(Database::num_rows($res)>0) { $user = Database::fetch_array($res); } else { if ($anonymous) { $dir=''; switch($type) { case 'system': //return the complete path to the file, from root $dir = api_get_path(SYS_CODE_PATH).'img/'; break; case 'rel': //return the relative path to the file, from the Dokeos base dir $dir = api_get_path(REL_CODE_PATH).'img/'; break; case 'web': //return the complete web URL to the file $dir = api_get_path(WEB_CODE_PATH).'img/'; break; case 'none': //return only the picture_uri (as is, without subdir) default: break; } $file_anonymous='unknown.jpg'; return array('dir'=>$dir,'file'=>$file_anonymous); } else { return array('dir'=>'','file'=>''); } } $path = trim($user['picture_uri']); if (empty($path)) { if ($anonymous) { switch($type) { case 'system': //return the complete path to the file, from root $dir = api_get_path(SYS_CODE_PATH).'img/'; break; case 'rel': //return the relative path to the file, from the Dokeos base dir $dir = api_get_path(REL_CODE_PATH).'img/'; break; case 'web': //return the complete web URL to the file $dir = api_get_path(WEB_CODE_PATH).'img/'; break; case 'none': //return only the picture_uri (as is, without subdir) default: break; } $file_anonymous='unknown.jpg'; return array('dir'=>$dir,'file'=>$file_anonymous); } } $dir = ''; $first = ''; if(api_get_setting('split_users_upload_directory') === 'true') { if(!empty($path)) { $first = substr($path,0,1).'/'; } elseif($preview==true) { $first = substr(''.$user_id,0,1).'/'; } } else { $first = $user_id.'/'; } switch($type) { case 'system': //return the complete path to the file, from root $dir = api_get_path(SYS_CODE_PATH).'upload/users/'.$first; break; case 'rel': //return the relative path to the file, from the Dokeos base dir $dir = api_get_path(REL_CODE_PATH).'upload/users/'.$first; break; case 'web': //return the complete web URL to the file $dir = api_get_path(WEB_CODE_PATH).'upload/users/'.$first; break; case 'none': //return only the picture_uri (as is, without subdir) default: break; } return array('dir'=>$dir,'file'=>$path); }/*----------------------------------------------------------- PRODUCTIONS FUNCTIONS-----------------------------------------------------------*/ /** * Returns an XHTML formatted list of productions for a user, or FALSE if he * doesn't have any. * * If there has been a request to remove a production, the function will return * without building the list unless forced to do so by the optional second * parameter. This increases performance by avoiding to read through the * productions on the filesystem before the removal request has been carried * out because they'll have to be re-read afterwards anyway. * * @param $user_id User id * @param $force Optional parameter to force building after a removal request * @return A string containing the XHTML code to dipslay the production list, or FALSE */ function build_production_list($user_id, $force = false, $showdelete=false) { if (!$force && !empty($_POST['remove_production'])) return true; // postpone reading from the filesystem $productions = UserManager::get_user_productions($user_id); if (empty($productions)) return false; $production_path = UserManager::get_user_picture_path_by_id($user_id,'web',true); $production_dir = $production_path['dir'].$user_id.'/'; $del_image = api_get_path(WEB_CODE_PATH).'img/delete.gif'; $del_text = get_lang('Delete'); $production_list = '<ul id="productions">'; foreach ($productions as $file) { $production_list .= '<li><a href="'.$production_dir.urlencode($file).'" target="_blank">'.htmlentities($file).'</a>'; if ($showdelete) { $production_list .= '<input type="image" name="remove_production['.urlencode($file).']" src="'.$del_image.'" alt="'.$del_text.'" title="'.$del_text.' '.htmlentities($file).'" onclick="return confirmation(\''.htmlentities($file).'\');" /></li>'; } } $production_list .= '</ul>'; return $production_list; } /** * Returns an array with the user's productions. * * @param $user_id User id * @return An array containing the user's productions */ function get_user_productions($user_id) { $production_path = UserManager::get_user_picture_path_by_id($user_id,'system',true); $production_repository = $production_path['dir'].$user_id.'/'; $productions = array(); if (is_dir($production_repository)) { $handle = opendir($production_repository); while ($file = readdir($handle)) { if ($file == '.' || $file == '..' || $file == '.htaccess') continue; // skip current/parent directory and .htaccess $productions[] = $file; } } return $productions; // can be an empty array } /** * Remove a user production. * * @param $user_id User id * @param $production The production to remove */ function remove_user_production($user_id, $production) { $production_path = UserManager::get_user_picture_path_by_id($user_id,'system',true); unlink($production_path['dir'].$user_id.'/'.$production); } /** * Update an extra field * @param integer Field ID * @param array Database columns and their new value * @return boolean true if field updated, false otherwise */ function update_extra_field($fid,$columns) { //TODO check that values added are values proposed for enumerated field types $t_uf = Database::get_main_table(TABLE_MAIN_USER_FIELD); $fid = Database::escape_string($fid); $sqluf = "UPDATE $t_uf SET "; $known_fields = array('id','field_variable','field_type','field_display_text','field_default_value','field_order','field_visible','field_changeable'); $safecolumns = array(); foreach($columns as $index => $newval) { if(in_array($index,$known_fields)) { $safecolumns[$index] = Database::escape_string($newval); $sqluf .= $index." = '".$safecolumns[$index]."', "; } } $time = time(); $sqluf .= " tms = FROM_UNIXTIME($time) WHERE id='$fid'"; $resuf = api_sql_query($sqluf,__FILE__,__LINE__); return $resuf; } /** * Update an extra field value for a given user * @param integer User ID * @param string Field variable name * @param string Field value * @return boolean true if field updated, false otherwise */ function update_extra_field_value($user_id,$fname,$fvalue='') { //TODO check that values added are values proposed for enumerated field types $t_uf = Database::get_main_table(TABLE_MAIN_USER_FIELD); $t_ufo = Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS); $t_ufv = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES); $fname = Database::escape_string($fname); $fvalues = ''; if(is_array($fvalue)) { foreach($fvalue as $val) { $fvalues .= Database::escape_string($val).';'; } if(!empty($fvalues)) { $fvalues = substr($fvalues,0,-1); } } else { $fvalues = Database::escape_string($fvalue); } $sqluf = "SELECT * FROM $t_uf WHERE field_variable='$fname'"; $resuf = api_sql_query($sqluf,__FILE__,__LINE__); if(Database::num_rows($resuf)==1) { //ok, the field exists // Check if enumerated field, if the option is available $rowuf = Database::fetch_array($resuf); switch($rowuf['field_type']) { case 3: case 4: case 5: $sqluo = "SELECT * FROM $t_ufo WHERE field_id = ".$rowuf['id']; $resuo = api_sql_query($sqluo,__FILE__,__LINE__); $values = split(';',$fvalues); if(Database::num_rows($resuo)>0) { $check = false; while($rowuo = Database::fetch_array($resuo)) { if(in_array($rowuo['option_value'],$values)) { $check = true; break; } } if($check == false) { return false; //option value not found } } else { return false; //enumerated type but no option found } break; case 1: case 2: default: break; } $tms = time(); $sqlufv = "SELECT * FROM $t_ufv WHERE user_id = $user_id AND field_id = ".$rowuf['id']." ORDER BY id"; $resufv = api_sql_query($sqlufv,__FILE__,__LINE__); $n = Database::num_rows($resufv); if($n>1) { //problem, we already have to values for this field and user combination - keep last one while($rowufv = Database::fetch_array($resufv)) { if($n > 1) { $sqld = "DELETE FROM $t_ufv WHERE id = ".$rowufv['id']; $resd = api_sql_query($sqld,__FILE__,__LINE__); $n--; } $rowufv = Database::fetch_array($resufv); if($rowufv['field_value'] != $fvalues) { $sqlu = "UPDATE $t_ufv SET field_value = '$fvalues', tms = FROM_UNIXTIME($tms) WHERE id = ".$rowufv['id']; $resu = api_sql_query($sqlu,__FILE__,__LINE__); return($resu?true:false); } return true; } } elseif($n==1) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -