usermanager.lib.php 209 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. *
  5. * Class UserManager
  6. *
  7. * This library provides functions for user management.
  8. * Include/require it in your code to use its functionality.
  9. * @package chamilo.library
  10. * @author Julio Montoya <gugli100@gmail.com> Social network groups added 2009/12
  11. *
  12. */
  13. class UserManager
  14. {
  15. // Constants for user extra field types.
  16. const USER_FIELD_TYPE_TEXT = 1;
  17. const USER_FIELD_TYPE_TEXTAREA = 2;
  18. const USER_FIELD_TYPE_RADIO = 3;
  19. const USER_FIELD_TYPE_SELECT = 4;
  20. const USER_FIELD_TYPE_SELECT_MULTIPLE = 5;
  21. const USER_FIELD_TYPE_DATE = 6;
  22. const USER_FIELD_TYPE_DATETIME = 7;
  23. const USER_FIELD_TYPE_DOUBLE_SELECT = 8;
  24. const USER_FIELD_TYPE_DIVIDER = 9;
  25. const USER_FIELD_TYPE_TAG = 10;
  26. const USER_FIELD_TYPE_TIMEZONE = 11;
  27. const USER_FIELD_TYPE_SOCIAL_PROFILE = 12;
  28. const USER_FIELD_TYPE_FILE = 13;
  29. const USER_FIELD_TYPE_MOBILE_PHONE_NUMBER = 14;
  30. /**
  31. * The default constructor only instanciates an empty user object
  32. */
  33. public function __construct()
  34. {
  35. }
  36. /**
  37. * Creates a new user for the platform
  38. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>,
  39. * @author Roan Embrechts <roan_embrechts@yahoo.com>
  40. * @param string Firstname
  41. * @param string Lastname
  42. * @param int Status (1 for course tutor, 5 for student, 6 for anonymous)
  43. * @param string e-mail address
  44. * @param string Login
  45. * @param string Password
  46. * @param string Any official code (optional)
  47. * @param string User language (optional)
  48. * @param string Phone number (optional)
  49. * @param string Picture URI (optional)
  50. * @param string Authentication source (optional, defaults to 'platform', dependind on constant)
  51. * @param string Account expiration date (optional, defaults to '0000-00-00 00:00:00')
  52. * @param int Whether the account is enabled or disabled by default
  53. * @param int The department of HR in which the user is registered (optional, defaults to 0)
  54. * @param array Extra fields
  55. * @param string Encrypt method used if password is given encrypted. Set to an empty string by default
  56. * @return mixed new user id - if the new user creation succeeds, false otherwise
  57. * @desc The function tries to retrieve $_user['user_id'] from the global space. If it exists, $_user['user_id'] is the creator id. If a problem arises, it stores the error message in global $api_failureList
  58. * @assert ('Sam','Gamegie',5,'sam@example.com','jo','jo') > 1
  59. * @assert ('Pippin','Took',null,null,'jo','jo') === false
  60. */
  61. public static function create_user(
  62. $firstName,
  63. $lastName,
  64. $status,
  65. $email,
  66. $loginName,
  67. $password,
  68. $official_code = '',
  69. $language = '',
  70. $phone = '',
  71. $picture_uri = '',
  72. $auth_source = PLATFORM_AUTH_SOURCE,
  73. $expiration_date = '0000-00-00 00:00:00',
  74. $active = 1,
  75. $hr_dept_id = 0,
  76. $extra = null,
  77. $encrypt_method = '',
  78. $send_mail = false
  79. ) {
  80. global $_user, $_configuration;
  81. $original_password = $password;
  82. $access_url_id = 1;
  83. if (api_get_multiple_access_url()) {
  84. $access_url_id = api_get_current_access_url_id();
  85. }
  86. if (is_array($_configuration[$access_url_id]) &&
  87. isset($_configuration[$access_url_id]['hosting_limit_users']) &&
  88. $_configuration[$access_url_id]['hosting_limit_users'] > 0) {
  89. $num = self::get_number_of_users();
  90. if ($num >= $_configuration[$access_url_id]['hosting_limit_users']) {
  91. api_warn_hosting_contact('hosting_limit_users');
  92. return api_set_failure('portal users limit reached');
  93. }
  94. }
  95. if ($status === 1 &&
  96. is_array($_configuration[$access_url_id]) &&
  97. isset($_configuration[$access_url_id]['hosting_limit_teachers']) &&
  98. $_configuration[$access_url_id]['hosting_limit_teachers'] > 0
  99. ) {
  100. $num = self::get_number_of_users(1);
  101. if ($num >= $_configuration[$access_url_id]['hosting_limit_teachers']) {
  102. api_warn_hosting_contact('hosting_limit_teachers');
  103. return api_set_failure('portal teachers limit reached');
  104. }
  105. }
  106. $firstName = Security::remove_XSS($firstName);
  107. $lastName = Security::remove_XSS($lastName);
  108. $loginName = Security::remove_XSS($loginName);
  109. $phone = Security::remove_XSS($phone);
  110. // database table definition
  111. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  112. //Checking the user language
  113. $languages = api_get_languages();
  114. $language = strtolower($language);
  115. if (!in_array($language, $languages['folder'])) {
  116. $language = api_get_setting('platformLanguage');
  117. }
  118. if ($_user['user_id']) {
  119. $creator_id = intval($_user['user_id']);
  120. } else {
  121. $creator_id = '';
  122. }
  123. // First check wether the login already exists
  124. if (!self::is_username_available($loginName)) {
  125. return api_set_failure('login-pass already taken');
  126. }
  127. //$password = "PLACEHOLDER";
  128. if (empty($encrypt_method)) {
  129. $password = api_get_encrypted_password($password);
  130. } else {
  131. if ($_configuration['password_encryption'] === $encrypt_method) {
  132. if ($encrypt_method == 'md5' && !preg_match('/^[A-Fa-f0-9]{32}$/', $password)) {
  133. return api_set_failure('encrypt_method invalid');
  134. } else if ($encrypt_method == 'sha1' && !preg_match('/^[A-Fa-f0-9]{40}$/', $password)) {
  135. return api_set_failure('encrypt_method invalid');
  136. }
  137. } else {
  138. return api_set_failure('encrypt_method invalid');
  139. }
  140. }
  141. $current_date = api_get_utc_datetime();
  142. $sql = "INSERT INTO $table_user
  143. SET lastname = '".Database::escape_string(trim($lastName))."',
  144. firstname = '".Database::escape_string(trim($firstName))."',
  145. username = '".Database::escape_string(trim($loginName))."',
  146. status = '".Database::escape_string($status)."',
  147. password = '".Database::escape_string($password)."',
  148. email = '".Database::escape_string($email)."',
  149. official_code = '".Database::escape_string($official_code)."',
  150. picture_uri = '".Database::escape_string($picture_uri)."',
  151. creator_id = '".Database::escape_string($creator_id)."',
  152. auth_source = '".Database::escape_string($auth_source)."',
  153. phone = '".Database::escape_string($phone)."',
  154. language = '".Database::escape_string($language)."',
  155. registration_date = '".$current_date."',
  156. expiration_date = '".Database::escape_string($expiration_date)."',
  157. hr_dept_id = '".Database::escape_string($hr_dept_id)."',
  158. active = '".Database::escape_string($active)."'";
  159. $result = Database::query($sql);
  160. if ($result) {
  161. //echo "id returned";
  162. $return = Database::insert_id();
  163. if (api_get_multiple_access_url()) {
  164. UrlManager::add_user_to_url($return, api_get_current_access_url_id());
  165. } else {
  166. //we are adding by default the access_url_user table with access_url_id = 1
  167. UrlManager::add_user_to_url($return, 1);
  168. }
  169. if (!empty($email) && $send_mail) {
  170. $recipient_name = api_get_person_name($firstName, $lastName, null, PERSON_NAME_EMAIL_ADDRESS);
  171. $emailsubject = '['.api_get_setting('siteName').'] '.get_lang('YourReg').' '.api_get_setting('siteName');
  172. $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  173. $email_admin = api_get_setting('emailAdministrator');
  174. if (api_is_multiple_url_enabled()) {
  175. $access_url_id = api_get_current_access_url_id();
  176. if ($access_url_id != -1) {
  177. $url = api_get_access_url($access_url_id);
  178. $emailbody = get_lang('Dear')." ".stripslashes(api_get_person_name($firstName, $lastName)).",\n\n".get_lang('YouAreReg')." ".api_get_setting('siteName')." ".get_lang('WithTheFollowingSettings')."\n\n".get_lang('Username')." : ".$loginName."\n".get_lang('Pass')." : ".stripslashes($original_password)."\n\n".get_lang('Address')." ".api_get_setting('siteName')." ".get_lang('Is')." : ".$url['url']."\n\n".get_lang('Problem')."\n\n".get_lang('SignatureFormula').",\n\n".api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n".get_lang('Manager')." ".api_get_setting('siteName')."\nT. ".api_get_setting('administratorTelephone')."\n".get_lang('Email')." : ".api_get_setting('emailAdministrator');
  179. }
  180. } else {
  181. $emailbody = get_lang('Dear')." ".stripslashes(api_get_person_name($firstName, $lastName)).",\n\n".get_lang('YouAreReg')." ".api_get_setting('siteName')." ".get_lang('WithTheFollowingSettings')."\n\n".get_lang('Username')." : ".$loginName."\n".get_lang('Pass')." : ".stripslashes($original_password)."\n\n".get_lang('Address')." ".api_get_setting('siteName')." ".get_lang('Is')." : ".$_configuration['root_web']."\n\n".get_lang('Problem')."\n\n".get_lang('SignatureFormula').",\n\n".api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n".get_lang('Manager')." ".api_get_setting('siteName')."\nT. ".api_get_setting('administratorTelephone')."\n".get_lang('Email')." : ".api_get_setting('emailAdministrator');
  182. }
  183. /* MANAGE EVENT WITH MAIL */
  184. if (EventsMail::check_if_using_class('user_registration')) {
  185. $values["about_user"] = $return;
  186. $values["password"] = $original_password;
  187. $values["send_to"] = array($return);
  188. $values["prior_lang"] = null;
  189. EventsDispatcher::events('user_registration', $values);
  190. } else {
  191. $phoneNumber = isset($extra['mobile_phone_number']) ? $extra['mobile_phone_number'] : null;
  192. $plugin = new AppPlugin();
  193. $additionalParameters = array(
  194. 'smsType' => @constant($plugin->getSMSPluginName().'::WELCOME_LOGIN_PASSWORD'),
  195. 'userId' => $return,
  196. 'mobilePhoneNumber' => $phoneNumber,
  197. 'password' => $original_password
  198. );
  199. api_mail_html(
  200. $recipient_name,
  201. $email,
  202. $emailsubject,
  203. $emailbody,
  204. $sender_name,
  205. $email_admin,
  206. null,
  207. null,
  208. null,
  209. $additionalParameters
  210. );
  211. }
  212. /* ENDS MANAGE EVENT WITH MAIL */
  213. }
  214. event_system(LOG_USER_CREATE, LOG_USER_ID, $return);
  215. } else {
  216. return api_set_failure('error inserting in Database');
  217. }
  218. if (is_array($extra) && count($extra) > 0) {
  219. $res = true;
  220. foreach ($extra as $fname => $fvalue) {
  221. $res = $res && self::update_extra_field_value($return, $fname, $fvalue);
  222. }
  223. }
  224. self::update_extra_field_value($return, 'already_logged_in', 'false');
  225. return $return;
  226. }
  227. /**
  228. * Can user be deleted? This function checks whether there's a course
  229. * in which the given user is the
  230. * only course administrator. If that is the case, the user can't be
  231. * deleted because the course would remain without a course admin.
  232. * @param int $user_id The user id
  233. * @return boolean true if user can be deleted
  234. * @assert (null) === false
  235. * @assert (-1) === false
  236. * @assert ('abc') === false
  237. */
  238. public static function can_delete_user($user_id)
  239. {
  240. global $_configuration;
  241. if (isset($_configuration['deny_delete_users']) &&
  242. $_configuration['deny_delete_users'] == true
  243. ) {
  244. return false;
  245. }
  246. $table_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  247. if ($user_id != strval(intval($user_id)))
  248. return false;
  249. if ($user_id === false)
  250. return false;
  251. $sql = "SELECT * FROM $table_course_user
  252. WHERE status = '1' AND user_id = '".$user_id."'";
  253. $res = Database::query($sql);
  254. while ($course = Database::fetch_object($res)) {
  255. $sql = "SELECT user_id FROM $table_course_user
  256. WHERE status='1' AND course_code ='".Database::escape_string($course->course_code)."'";
  257. $res2 = Database::query($sql);
  258. if (Database::num_rows($res2) == 1) {
  259. return false;
  260. }
  261. }
  262. return true;
  263. }
  264. /**
  265. * Delete a user from the platform, and all its belongings. This is a
  266. * very dangerous function that should only be accessible by
  267. * super-admins. Other roles should only be able to disable a user,
  268. * which removes access to the platform but doesn't delete anything.
  269. * @param int The ID of th user to be deleted
  270. * @return boolean true if user is successfully deleted, false otherwise
  271. * @assert (null) === false
  272. * @assert ('abc') === false
  273. */
  274. public static function delete_user($user_id)
  275. {
  276. if ($user_id != strval(intval($user_id))) {
  277. return false;
  278. }
  279. if ($user_id === false) {
  280. return false;
  281. }
  282. if (!self::can_delete_user($user_id)) {
  283. return false;
  284. }
  285. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  286. $usergroup_rel_user = Database :: get_main_table(TABLE_USERGROUP_REL_USER);
  287. $table_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  288. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  289. $table_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  290. $table_admin = Database :: get_main_table(TABLE_MAIN_ADMIN);
  291. $table_session_user = Database :: get_main_table(TABLE_MAIN_SESSION_USER);
  292. $table_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  293. $table_group = Database :: get_course_table(TABLE_GROUP_USER);
  294. $table_work = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  295. // Unsubscribe the user from all groups in all his courses
  296. $sql = "SELECT c.id FROM $table_course c, $table_course_user cu
  297. WHERE
  298. cu.user_id = '".$user_id."' AND
  299. relation_type<>".COURSE_RELATION_TYPE_RRHH." AND
  300. c.code = cu.course_code";
  301. $res = Database::query($sql);
  302. while ($course = Database::fetch_object($res)) {
  303. $sql = "DELETE FROM $table_group
  304. WHERE c_id = {$course->id} AND user_id = $user_id";
  305. Database::query($sql);
  306. }
  307. // Unsubscribe user from all classes
  308. //Classes are not longer supported
  309. /* $sql = "DELETE FROM $table_class_user WHERE user_id = '".$user_id."'";
  310. Database::query($sql); */
  311. // Unsubscribe user from usergroup_rel_user
  312. $sql = "DELETE FROM $usergroup_rel_user WHERE user_id = '".$user_id."'";
  313. Database::query($sql);
  314. // Unsubscribe user from all courses
  315. $sql = "DELETE FROM $table_course_user WHERE user_id = '".$user_id."'";
  316. Database::query($sql);
  317. // Unsubscribe user from all courses in sessions
  318. $sql = "DELETE FROM $table_session_course_user WHERE id_user = '".$user_id."'";
  319. Database::query($sql);
  320. // If the user was added as a id_coach then set the current admin as coach see BT#
  321. $currentUserId = api_get_user_id();
  322. $sql = "UPDATE $table_session SET id_coach = $currentUserId WHERE id_coach = '".$user_id."'";
  323. Database::query($sql);
  324. $sql = "UPDATE $table_session SET id_coach = $currentUserId WHERE session_admin_id = '".$user_id."'";
  325. Database::query($sql);
  326. // Unsubscribe user from all sessions
  327. $sql = "DELETE FROM $table_session_user WHERE id_user = '".$user_id."'";
  328. Database::query($sql);
  329. // Delete user picture
  330. /* TODO: Logic about api_get_setting('split_users_upload_directory') == 'true'
  331. a user has 4 differnt sized photos to be deleted. */
  332. $user_info = api_get_user_info($user_id);
  333. if (strlen($user_info['picture_uri']) > 0) {
  334. $img_path = api_get_path(SYS_CODE_PATH).'upload/users/'.$user_id.'/'.$user_info['picture_uri'];
  335. if (file_exists($img_path))
  336. unlink($img_path);
  337. }
  338. // Delete the personal course categories
  339. $course_cat_table = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  340. $sql = "DELETE FROM $course_cat_table WHERE user_id = '".$user_id."'";
  341. Database::query($sql);
  342. // Delete user from database
  343. $sql = "DELETE FROM $table_user WHERE user_id = '".$user_id."'";
  344. Database::query($sql);
  345. // Delete user from the admin table
  346. $sql = "DELETE FROM $table_admin WHERE user_id = '".$user_id."'";
  347. Database::query($sql);
  348. // Delete the personal agenda-items from this user
  349. $agenda_table = Database :: get_user_personal_table(TABLE_PERSONAL_AGENDA);
  350. $sql = "DELETE FROM $agenda_table WHERE user = '".$user_id."'";
  351. Database::query($sql);
  352. $gradebook_results_table = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  353. $sql = 'DELETE FROM '.$gradebook_results_table.' WHERE user_id = '.$user_id;
  354. Database::query($sql);
  355. $t_ufv = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  356. $sqlv = "DELETE FROM $t_ufv WHERE user_id = $user_id";
  357. Database::query($sqlv);
  358. require_once api_get_path(LIBRARY_PATH).'urlmanager.lib.php';
  359. if (api_get_multiple_access_url()) {
  360. $url_id = api_get_current_access_url_id();
  361. UrlManager::delete_url_rel_user($user_id, $url_id);
  362. } else {
  363. //we delete the user from the url_id =1
  364. UrlManager::delete_url_rel_user($user_id, 1);
  365. }
  366. if (api_get_setting('allow_social_tool') == 'true') {
  367. require_once api_get_path(LIBRARY_PATH).'group_portal_manager.lib.php';
  368. //Delete user from portal groups
  369. $group_list = GroupPortalManager::get_groups_by_user($user_id);
  370. if (!empty($group_list)) {
  371. foreach ($group_list as $group_id => $data) {
  372. GroupPortalManager::delete_user_rel_group($user_id, $group_id);
  373. }
  374. }
  375. // Delete user from friend lists
  376. SocialManager::remove_user_rel_user($user_id, true);
  377. }
  378. // Removing survey invitation
  379. survey_manager::delete_all_survey_invitations_by_user($user_id);
  380. // Delete students works
  381. $sql = "DELETE FROM $table_work WHERE user_id = $user_id AND c_id <> 0";
  382. Database::query($sql);
  383. // Add event to system log
  384. $user_id_manager = api_get_user_id();
  385. event_system(LOG_USER_DELETE, LOG_USER_ID, $user_id, api_get_utc_datetime(), $user_id_manager, null, $user_info);
  386. event_system(LOG_USER_DELETE, LOG_USER_OBJECT, $user_info, api_get_utc_datetime(), $user_id_manager, null, $user_info);
  387. return true;
  388. }
  389. /**
  390. * Deletes users completely. Can be called either as:
  391. * - UserManager :: delete_users(1, 2, 3); or
  392. * - UserManager :: delete_users(array(1, 2, 3));
  393. * @param array|int $ids
  394. * @return boolean True if at least one user was successfuly deleted. False otherwise.
  395. * @author Laurent Opprecht
  396. * @uses UserManager::delete_user() to actually delete each user
  397. * @assert (null) === false
  398. * @assert (-1) === false
  399. * @assert (array(-1)) === false
  400. */
  401. static function delete_users($ids = array())
  402. {
  403. $result = false;
  404. $ids = is_array($ids) ? $ids : func_get_args();
  405. if (!is_array($ids) or count($ids) == 0) { return false; }
  406. $ids = array_map('intval', $ids);
  407. foreach ($ids as $id) {
  408. if (empty($id) or $id < 1) { continue; }
  409. $deleted = self::delete_user($id);
  410. $result = $deleted || $result;
  411. }
  412. return $result;
  413. }
  414. /**
  415. * Disable users. Can be called either as:
  416. * - UserManager :: deactivate_users(1, 2, 3);
  417. * - UserManager :: deactivate_users(array(1, 2, 3));
  418. * @param array|int $ids
  419. * @return boolean
  420. * @author Laurent Opprecht
  421. * @assert (null) === false
  422. * @assert (array(-1)) === false
  423. */
  424. static function deactivate_users($ids = array())
  425. {
  426. if (empty($ids)) {
  427. return false;
  428. }
  429. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  430. $ids = is_array($ids) ? $ids : func_get_args();
  431. $ids = array_map('intval', $ids);
  432. $ids = implode(',', $ids);
  433. $sql = "UPDATE $table_user SET active = 0 WHERE user_id IN ($ids)";
  434. $r = Database::query($sql);
  435. if ($r !== false) {
  436. event_system(LOG_USER_DISABLE,LOG_USER_ID,$ids);
  437. }
  438. return $r;
  439. }
  440. /**
  441. * Enable users. Can be called either as:
  442. * - UserManager :: activate_users(1, 2, 3);
  443. * - UserManager :: activate_users(array(1, 2, 3));
  444. * @param array|int IDs of the users to enable
  445. * @return boolean
  446. * @author Laurent Opprecht
  447. * @assert (null) === false
  448. * @assert (array(-1)) === false
  449. */
  450. static function activate_users($ids = array())
  451. {
  452. if (empty($ids)) {
  453. return false;
  454. }
  455. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  456. $ids = is_array($ids) ? $ids : func_get_args();
  457. $ids = array_map('intval', $ids);
  458. $ids = implode(',', $ids);
  459. $sql = "UPDATE $table_user SET active = 1 WHERE user_id IN ($ids)";
  460. $r = Database::query($sql);
  461. if ($r !== false) {
  462. event_system(LOG_USER_ENABLE,LOG_USER_ID,$ids);
  463. }
  464. return $r;
  465. }
  466. /**
  467. * Update user information with new openid
  468. * @param int $user_id
  469. * @param string $openid
  470. * @return boolean true if the user information was updated
  471. * @assert (false,'') === false
  472. * @assert (-1,'') === false
  473. */
  474. public static function update_openid($user_id, $openid)
  475. {
  476. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  477. if ($user_id != strval(intval($user_id)))
  478. return false;
  479. if ($user_id === false)
  480. return false;
  481. $sql = "UPDATE $table_user SET
  482. openid='".Database::escape_string($openid)."'";
  483. $sql .= " WHERE user_id='$user_id'";
  484. return Database::query($sql);
  485. }
  486. /**
  487. * Update user information with all the parameters passed to this function
  488. * @param int The ID of the user to be updated
  489. * @param string The user's firstname
  490. * @param string The user's lastname
  491. * @param string The user's username (login)
  492. * @param string The user's password
  493. * @param string The authentication source (default: "platform")
  494. * @param string The user's e-mail address
  495. * @param int The user's status
  496. * @param string The user's official code (usually just an internal institutional code)
  497. * @param string The user's phone number
  498. * @param string The user's picture URL (internal to the Chamilo directory)
  499. * @param int The user ID of the person who registered this user (optional, defaults to null)
  500. * @param int The department of HR in which the user is registered (optional, defaults to 0)
  501. * @param array A series of additional fields to add to this user as extra fields (optional, defaults to null)
  502. * @return boolean true if the user information was updated
  503. * @assert (false, false, false, false, false, false, false, false, false, false, false, false, false) === false
  504. */
  505. public static function update_user(
  506. $user_id,
  507. $firstname,
  508. $lastname,
  509. $username,
  510. $password = null,
  511. $auth_source = null,
  512. $email,
  513. $status,
  514. $official_code,
  515. $phone,
  516. $picture_uri,
  517. $expiration_date,
  518. $active,
  519. $creator_id = null,
  520. $hr_dept_id = 0,
  521. $extra = null,
  522. $language = 'english',
  523. $encrypt_method = '',
  524. $send_email = false,
  525. $reset_password = 0
  526. ) {
  527. global $_configuration;
  528. $original_password = $password;
  529. if (empty($user_id)) { return false; }
  530. $user_info = api_get_user_info($user_id, false, true);
  531. if ($reset_password == 0) {
  532. $password = null;
  533. } elseif ($reset_password == 1) {
  534. $original_password = $password = api_generate_password();
  535. } elseif ($reset_password == 2) {
  536. $password = $password;
  537. } elseif ($reset_password == 3) {
  538. $password = $password;
  539. }
  540. $auth_source = !empty($auth_source) ? $auth_source : PLATFORM_AUTH_SOURCE;
  541. if ($user_id != strval(intval($user_id)))
  542. return false;
  543. if ($user_id === false)
  544. return false;
  545. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  546. //Checking the user language
  547. $languages = api_get_languages();
  548. if (!in_array($language, $languages['folder'])) {
  549. $language = api_get_setting('platformLanguage');
  550. }
  551. $change_active = 0;
  552. if ($user_info['active'] != $active) {
  553. $change_active = 1;
  554. }
  555. $sql = "UPDATE $table_user SET
  556. lastname='".Database::escape_string($lastname)."',
  557. firstname='".Database::escape_string($firstname)."',
  558. username='".Database::escape_string($username)."',
  559. language='".Database::escape_string($language)."',";
  560. if (!is_null($password)) {
  561. if ($encrypt_method == '') {
  562. $password = api_get_encrypted_password($password);
  563. } else {
  564. if ($_configuration['password_encryption'] === $encrypt_method) {
  565. if ($encrypt_method == 'md5' && !preg_match('/^[A-Fa-f0-9]{32}$/', $password)) {
  566. return api_set_failure('encrypt_method invalid');
  567. } else if ($encrypt_method == 'sha1' && !preg_match('/^[A-Fa-f0-9]{40}$/', $password)) {
  568. return api_set_failure('encrypt_method invalid');
  569. }
  570. } else {
  571. return api_set_failure('encrypt_method invalid');
  572. }
  573. }
  574. $sql .= " password='".Database::escape_string($password)."',";
  575. }
  576. if (!is_null($auth_source)) {
  577. $sql .= " auth_source='".Database::escape_string($auth_source)."',";
  578. }
  579. $sql .= "
  580. email='".Database::escape_string($email)."',
  581. status='".Database::escape_string($status)."',
  582. official_code='".Database::escape_string($official_code)."',
  583. phone='".Database::escape_string($phone)."',
  584. picture_uri='".Database::escape_string($picture_uri)."',
  585. expiration_date='".Database::escape_string($expiration_date)."',
  586. active='".Database::escape_string($active)."',
  587. hr_dept_id=".intval($hr_dept_id);
  588. if (!is_null($creator_id)) {
  589. $sql .= ", creator_id='".intval($creator_id)."'";
  590. }
  591. $sql .= " WHERE user_id='$user_id'";
  592. $return = Database::query($sql);
  593. if ($change_active == 1 && $return) {
  594. if ($active == 1) {
  595. $event_title = LOG_USER_ENABLE;
  596. } else {
  597. $event_title = LOG_USER_DISABLE;
  598. }
  599. event_system($event_title, LOG_USER_ID, $user_id);
  600. }
  601. if (is_array($extra) && count($extra) > 0) {
  602. $res = true;
  603. foreach ($extra as $fname => $fvalue) {
  604. $res = $res && self::update_extra_field_value($user_id, $fname, $fvalue);
  605. }
  606. }
  607. if (!empty($email) && $send_email) {
  608. $recipient_name = api_get_person_name($firstname, $lastname, null, PERSON_NAME_EMAIL_ADDRESS);
  609. $emailsubject = '['.api_get_setting('siteName').'] '.get_lang('YourReg').' '.api_get_setting('siteName');
  610. $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  611. $email_admin = api_get_setting('emailAdministrator');
  612. if (api_is_multiple_url_enabled()) {
  613. $access_url_id = api_get_current_access_url_id();
  614. if ($access_url_id != -1) {
  615. $url = api_get_access_url($access_url_id);
  616. $emailbody = get_lang('Dear')." ".stripslashes(api_get_person_name($firstname, $lastname)).",\n\n".get_lang('YouAreReg')." ".api_get_setting('siteName')." ".get_lang('WithTheFollowingSettings')."\n\n".get_lang('Username')." : ".$username.(($reset_password > 0) ? "\n".get_lang('Pass')." : ".stripslashes($original_password) : "")."\n\n".get_lang('Address')." ".api_get_setting('siteName')." ".get_lang('Is')." : ".$url['url']."\n\n".get_lang('Problem')."\n\n".get_lang('SignatureFormula').",\n\n".api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n".get_lang('Manager')." ".api_get_setting('siteName')."\nT. ".api_get_setting('administratorTelephone')."\n".get_lang('Email')." : ".api_get_setting('emailAdministrator');
  617. }
  618. } else {
  619. $emailbody = get_lang('Dear')." ".stripslashes(api_get_person_name($firstname, $lastname)).",\n\n".get_lang('YouAreReg')." ".api_get_setting('siteName')." ".get_lang('WithTheFollowingSettings')."\n\n".get_lang('Username')." : ".$username.(($reset_password > 0) ? "\n".get_lang('Pass')." : ".stripslashes($original_password) : "")."\n\n".get_lang('Address')." ".api_get_setting('siteName')." ".get_lang('Is')." : ".$_configuration['root_web']."\n\n".get_lang('Problem')."\n\n".get_lang('SignatureFormula').",\n\n".api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n".get_lang('Manager')." ".api_get_setting('siteName')."\nT. ".api_get_setting('administratorTelephone')."\n".get_lang('Email')." : ".api_get_setting('emailAdministrator');
  620. }
  621. @api_mail_html($recipient_name, $email, $emailsubject, $emailbody, $sender_name, $email_admin);
  622. }
  623. return $return;
  624. }
  625. /**
  626. * Disables or enables a user
  627. * @param int user_id
  628. * @param int Enable or disable
  629. * @return void
  630. * @assert (-1,0) === false
  631. * @assert (1,1) === true
  632. */
  633. private static function change_active_state($user_id, $active)
  634. {
  635. if (strval(intval($user_id)) != $user_id) {
  636. return false;
  637. }
  638. if ($user_id < 1) {
  639. return false;
  640. }
  641. $user_id = intval($user_id);
  642. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  643. $sql = "UPDATE $table_user SET active = '$active' WHERE user_id = '$user_id';";
  644. $r = Database::query($sql);
  645. $ev = LOG_USER_DISABLE;
  646. if ($active == 1) {
  647. $ev = LOG_USER_ENABLE;
  648. }
  649. if ($r !== false) {
  650. event_system($ev,LOG_USER_ID,$user_id);
  651. }
  652. return $r;
  653. }
  654. /**
  655. * Disables a user
  656. * @param int User id
  657. * @uses UserManager::change_active_state() to actually disable the user
  658. * @assert (0) === false
  659. */
  660. public static function disable($user_id)
  661. {
  662. if (empty($user_id)) {
  663. return false;
  664. }
  665. self::change_active_state($user_id, 0);
  666. }
  667. /**
  668. * Enable a user
  669. * @param int User id
  670. * @uses UserManager::change_active_state() to actually disable the user
  671. * @assert (0) === false
  672. */
  673. public static function enable($user_id)
  674. {
  675. if (empty($user_id)) {
  676. return false;
  677. }
  678. self::change_active_state($user_id, 1);
  679. }
  680. /**
  681. * Returns the user's id based on the original id and field name in
  682. * the extra fields. Returns 0 if no user was found. This function is
  683. * mostly useful in the context of a web services-based sinchronization
  684. * @param string Original user id
  685. * @param string Original field name
  686. * @return int User id
  687. * @assert ('0','---') === 0
  688. */
  689. public static function get_user_id_from_original_id($original_user_id_value, $original_user_id_name)
  690. {
  691. $t_uf = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  692. $t_ufv = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  693. $sql = "SELECT user_id
  694. FROM $t_uf uf
  695. INNER JOIN $t_ufv ufv
  696. ON ufv.field_id=uf.id
  697. WHERE
  698. field_variable='$original_user_id_name' AND
  699. field_value='$original_user_id_value';";
  700. $res = Database::query($sql);
  701. $row = Database::fetch_object($res);
  702. if ($row) {
  703. return $row->user_id;
  704. } else {
  705. return 0;
  706. }
  707. }
  708. /**
  709. * Check if a username is available
  710. * @param string the wanted username
  711. * @return boolean true if the wanted username is available
  712. * @assert ('') === false
  713. * @assert ('xyzxyzxyz') === true
  714. */
  715. public static function is_username_available($username)
  716. {
  717. if (empty($username)) {
  718. return false;
  719. }
  720. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  721. $sql = "SELECT username FROM $table_user
  722. WHERE username = '".Database::escape_string($username)."'";
  723. $res = Database::query($sql);
  724. return Database::num_rows($res) == 0;
  725. }
  726. /**
  727. * Creates a username using person's names, i.e. creates jmontoya from Julio Montoya.
  728. * @param string $firstname The first name of the user.
  729. * @param string $lastname The last name of the user.
  730. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  731. * @param string $encoding (optional) The character encoding for the input names. If it is omitted, the platform character set will be used by default.
  732. * @return string Suggests a username that contains only ASCII-letters and digits, without check for uniqueness within the system.
  733. * @author Julio Montoya Armas
  734. * @author Ivan Tcholakov, 2009 - rework about internationalization.
  735. * @assert ('','') === false
  736. * @assert ('a','b') === 'ab'
  737. */
  738. public static function create_username($firstname, $lastname, $language = null, $encoding = null)
  739. {
  740. if (is_null($encoding)) {
  741. $encoding = api_get_system_encoding();
  742. }
  743. if (is_null($language)) {
  744. $language = api_get_interface_language();
  745. }
  746. if (empty($firstname) && empty($lastname)) {
  747. return false;
  748. }
  749. $firstname = api_substr(preg_replace(USERNAME_PURIFIER, '', api_transliterate($firstname, '', $encoding)), 0, 1); // The first letter only.
  750. //Looking for a space in the lastname
  751. $pos = api_strpos($lastname, ' ');
  752. if ($pos !== false) {
  753. $lastname = api_substr($lastname, 0, $pos);
  754. }
  755. $lastname = preg_replace(USERNAME_PURIFIER, '', api_transliterate($lastname, '', $encoding));
  756. //$username = api_is_western_name_order(null, $language) ? $firstname.$lastname : $lastname.$firstname;
  757. $username = $firstname.$lastname;
  758. if (empty($username)) {
  759. $username = 'user';
  760. }
  761. return strtolower(substr($username, 0, USERNAME_MAX_LENGTH - 3));
  762. }
  763. /**
  764. * Creates a unique username, using:
  765. * 1. the first name and the last name of a user;
  766. * 2. an already created username but not checked for uniqueness yet.
  767. * @param string $firstname The first name of a given user. If the second parameter $lastname is NULL, then this
  768. * parameter is treated as username which is to be checked for uniqueness and to be modified when it is necessary.
  769. * @param string $lastname The last name of the user.
  770. * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  771. * @param string $encoding (optional) The character encoding for the input names. If it is omitted, the platform character set will be used by default.
  772. * @return string Returns a username that contains only ASCII-letters and digits, and that is unique within the system.
  773. * Note: When the method is called several times with same parameters, its results look like the following sequence: ivan, ivan2, ivan3, ivan4, ...
  774. * @author Ivan Tcholakov, 2009
  775. */
  776. public static function create_unique_username($firstname, $lastname = null, $language = null, $encoding = null)
  777. {
  778. if (is_null($lastname)) {
  779. // In this case the actual input parameter $firstname should contain ASCII-letters and digits only.
  780. // For making this method tolerant of mistakes, let us transliterate and purify the suggested input username anyway.
  781. // So, instead of the sentence $username = $firstname; we place the following:
  782. $username = strtolower(preg_replace(USERNAME_PURIFIER, '', api_transliterate($firstname, '', $encoding)));
  783. } else {
  784. $username = self::create_username($firstname, $lastname, $language, $encoding);
  785. }
  786. if (!self::is_username_available($username)) {
  787. $i = 2;
  788. $temp_username = substr($username, 0, USERNAME_MAX_LENGTH - strlen((string) $i)).$i;
  789. while (!self::is_username_available($temp_username)) {
  790. $i++;
  791. $temp_username = substr($username, 0, USERNAME_MAX_LENGTH - strlen((string) $i)).$i;
  792. }
  793. $username = $temp_username;
  794. }
  795. return $username;
  796. }
  797. /**
  798. * Modifies a given username accordingly to the specification for valid characters and length.
  799. * @param $username string The input username.
  800. * @param bool $strict (optional) When this flag is TRUE, the result is guaranteed for full compliance, otherwise compliance may be partial. The default value is FALSE.
  801. * @param string $encoding (optional) The character encoding for the input names. If it is omitted, the platform character set will be used by default.
  802. * @return string The resulting purified username.
  803. */
  804. public static function purify_username($username, $strict = false, $encoding = null)
  805. {
  806. if ($strict) {
  807. // 1. Conversion of unacceptable letters (latinian letters with accents for example) into ASCII letters in order they not to be totally removed.
  808. // 2. Applying the strict purifier.
  809. // 3. Length limitation.
  810. $toreturn = api_get_setting('login_is_email') == 'true' ? substr(preg_replace(USERNAME_PURIFIER_MAIL, '', api_transliterate($username, '', $encoding)), 0, USERNAME_MAX_LENGTH) : substr(preg_replace(USERNAME_PURIFIER, '', api_transliterate($username, '', $encoding)), 0, USERNAME_MAX_LENGTH);
  811. return $toreturn;
  812. }
  813. // 1. Applying the shallow purifier.
  814. // 2. Length limitation.
  815. return substr(preg_replace(USERNAME_PURIFIER_SHALLOW, '', $username), 0, USERNAME_MAX_LENGTH);
  816. }
  817. /**
  818. * Checks whether the user id exists in the database
  819. *
  820. * @param int User id
  821. * @return bool True if user id was found, false otherwise
  822. */
  823. public static function is_user_id_valid($user_id)
  824. {
  825. $user_id = (int) $user_id;
  826. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  827. $sql = "SELECT user_id FROM $table_user WHERE user_id = '".$user_id."'";
  828. $res = Database::query($sql);
  829. $num_rows = Database::num_rows($res);
  830. if ($num_rows == 0) {
  831. return false;
  832. } else {
  833. return true;
  834. }
  835. }
  836. /**
  837. * Checks whether a given username matches to the specification strictly. The empty username is assumed here as invalid.
  838. * Mostly this function is to be used in the user interface built-in validation routines for providing feedback while usernames are enterd manually.
  839. * @param string $username The input username.
  840. * @param string $encoding (optional) The character encoding for the input names. If it is omitted, the platform character set will be used by default.
  841. * @return bool Returns TRUE if the username is valid, FALSE otherwise.
  842. */
  843. public static function is_username_valid($username, $encoding = null)
  844. {
  845. return !empty($username) && $username == self::purify_username($username, true);
  846. }
  847. /**
  848. * Checks whether a username is empty. If the username contains whitespace characters, such as spaces, tabulators, newlines, etc.,
  849. * it is assumed as empty too. This function is safe for validation unpurified data (during importing).
  850. * @param string $username The given username.
  851. * @return bool Returns TRUE if length of the username exceeds the limit, FALSE otherwise.
  852. */
  853. public static function is_username_empty($username)
  854. {
  855. return (strlen(self::purify_username($username, false)) == 0);
  856. }
  857. /**
  858. * Checks whether a username is too long or not.
  859. * @param string $username The given username, it should contain only ASCII-letters and digits.
  860. * @return bool Returns TRUE if length of the username exceeds the limit, FALSE otherwise.
  861. */
  862. public static function is_username_too_long($username)
  863. {
  864. return (strlen($username) > USERNAME_MAX_LENGTH);
  865. }
  866. /**
  867. * Get the users by ID
  868. * @param array $ids student ids
  869. * @param string $active
  870. * @param string $order
  871. * @param string $limit
  872. * @return array $result student information
  873. */
  874. public static function get_user_list_by_ids($ids = array(), $active = null, $order = null, $limit = null)
  875. {
  876. if (empty($ids)) {
  877. return array();
  878. }
  879. $ids = is_array($ids) ? $ids : array($ids);
  880. $ids = array_map('intval', $ids);
  881. $ids = implode(',', $ids);
  882. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  883. $sql = "SELECT * FROM $tbl_user WHERE user_id IN ($ids)";
  884. if (!is_null($active)) {
  885. $sql .= ' AND active='.($active ? '1' : '0');
  886. }
  887. if (!is_null($order)) {
  888. $order = Database::escape_string($order);
  889. $sql .= ' ORDER BY ' . $order;
  890. }
  891. if (!is_null($limit)) {
  892. $limit = Database::escape_string($limit);
  893. $sql .= ' LIMIT ' . $limit;
  894. }
  895. $rs = Database::query($sql);
  896. $result = array();
  897. while ($row = Database::fetch_array($rs)) {
  898. $result[] = $row;
  899. }
  900. return $result;
  901. }
  902. /**
  903. * Get a list of users of which the given conditions match with an = 'cond'
  904. * @param array $conditions a list of condition (exemple : status=>STUDENT)
  905. * @param array $order_by a list of fields on which sort
  906. * @return array An array with all users of the platform.
  907. * @todo optional course code parameter, optional sorting parameters...
  908. * @todo security filter order by
  909. */
  910. public static function get_user_list($conditions = array(), $order_by = array(), $limit_from = false, $limit_to = false)
  911. {
  912. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  913. $return_array = array();
  914. $sql_query = "SELECT * FROM $user_table";
  915. if (count($conditions) > 0) {
  916. $sql_query .= ' WHERE ';
  917. foreach ($conditions as $field => $value) {
  918. $field = Database::escape_string($field);
  919. $value = Database::escape_string($value);
  920. $sql_query .= "$field = '$value'";
  921. }
  922. }
  923. if (count($order_by) > 0) {
  924. $sql_query .= ' ORDER BY '.Database::escape_string(implode(',', $order_by), null, false);
  925. }
  926. if (is_numeric($limit_from) && is_numeric($limit_from)) {
  927. $limit_from = intval($limit_from);
  928. $limit_to = intval($limit_to);
  929. $sql_query .= " LIMIT $limit_from, $limit_to";
  930. }
  931. $sql_result = Database::query($sql_query);
  932. while ($result = Database::fetch_array($sql_result)) {
  933. $return_array[] = $result;
  934. }
  935. return $return_array;
  936. }
  937. /**
  938. * Get a list of users of which the given conditions match with a LIKE '%cond%'
  939. * @param array $conditions a list of condition (exemple : status=>STUDENT)
  940. * @param array $order_by a list of fields on which sort
  941. * @return array An array with all users of the platform.
  942. * @todo optional course code parameter, optional sorting parameters...
  943. * @todo security filter order_by
  944. */
  945. public static function get_user_list_like($conditions = array(), $order_by = array(), $simple_like = false, $condition = 'AND')
  946. {
  947. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  948. $return_array = array();
  949. $sql_query = "SELECT * FROM $user_table";
  950. if (count($conditions) > 0) {
  951. $sql_query .= ' WHERE ';
  952. $temp_conditions = array();
  953. foreach ($conditions as $field => $value) {
  954. $field = Database::escape_string($field);
  955. $value = Database::escape_string($value);
  956. if ($simple_like) {
  957. $temp_conditions[] = $field." LIKE '$value%'";
  958. } else {
  959. $temp_conditions[] = $field.' LIKE \'%'.$value.'%\'';
  960. }
  961. }
  962. if (!empty($temp_conditions)) {
  963. $sql_query .= implode(' '.$condition.' ', $temp_conditions);
  964. }
  965. }
  966. if (count($order_by) > 0) {
  967. $sql_query .= ' ORDER BY '.Database::escape_string(implode(',', $order_by), null, false);
  968. }
  969. $sql_result = Database::query($sql_query);
  970. while ($result = Database::fetch_array($sql_result)) {
  971. $return_array[] = $result;
  972. }
  973. return $return_array;
  974. }
  975. /**
  976. * Get user information
  977. * @param string The username
  978. * @return array All user information as an associative array
  979. */
  980. public static function get_user_info($username)
  981. {
  982. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  983. $username = Database::escape_string($username);
  984. $sql = "SELECT * FROM $user_table WHERE username='".$username."'";
  985. $res = Database::query($sql);
  986. if (Database::num_rows($res) > 0) {
  987. return Database::fetch_array($res);
  988. }
  989. return false;
  990. }
  991. /**
  992. * Get user information
  993. * @param string The id
  994. * @param boolean Whether to return the user's extra fields (defaults to false)
  995. * @return array All user information as an associative array
  996. * @todo Use api_get_user_info() instead
  997. */
  998. public static function get_user_info_by_id($user_id, $user_fields = false)
  999. {
  1000. $user_id = intval($user_id);
  1001. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  1002. $sql = "SELECT * FROM $user_table WHERE user_id=".$user_id;
  1003. $res = Database::query($sql);
  1004. if (Database::num_rows($res) > 0) {
  1005. $user = Database::fetch_array($res);
  1006. $t_uf = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  1007. $t_ufv = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  1008. $sqlf = "SELECT * FROM $t_uf ORDER BY field_order";
  1009. $resf = Database::query($sqlf);
  1010. if (Database::num_rows($resf) > 0) {
  1011. while ($rowf = Database::fetch_array($resf)) {
  1012. $sqlv = "SELECT * FROM $t_ufv WHERE field_id = ".$rowf['id']." AND user_id = ".$user['user_id']." ORDER BY id DESC";
  1013. $resv = Database::query($sqlv);
  1014. if (Database::num_rows($resv) > 0) {
  1015. //There should be only one value for a field and a user
  1016. $rowv = Database::fetch_array($resv);
  1017. $user['extra'][$rowf['field_variable']] = $rowv['field_value'];
  1018. } else {
  1019. $user['extra'][$rowf['field_variable']] = '';
  1020. }
  1021. }
  1022. }
  1023. return $user;
  1024. }
  1025. return false;
  1026. }
  1027. /**
  1028. * Get the teacher list
  1029. * @param int the course ID
  1030. * @param array Content the list ID of user_id selected
  1031. */
  1032. //for survey
  1033. // TODO: Ivan, 14-SEP-2009: It seems that this method is not used at all (it can be located in a test unit only. To be deprecated?
  1034. public static function get_teacher_list($course_id, $sel_teacher = '')
  1035. {
  1036. $user_course_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1037. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  1038. $course_id = Database::escape_string($course_id);
  1039. $sql = "SELECT * FROM $user_table a, $user_course_table b
  1040. WHERE a.user_id=b.user_id AND b.status=1 AND b.course_code='$course_id'";
  1041. $sql_result = Database::query($sql);
  1042. echo "<select name=\"author\">";
  1043. while ($result = Database::fetch_array($sql_result)) {
  1044. if ($sel_teacher == $result['user_id'])
  1045. $selected = "selected";
  1046. echo "\n<option value=\"".$result['user_id']."\" $selected>".$result['firstname']."</option>";
  1047. }
  1048. echo "</select>";
  1049. }
  1050. /**
  1051. * Get user picture URL or path from user ID (returns an array).
  1052. * The return format is a complete path, enabling recovery of the directory
  1053. * with dirname() or the file with basename(). This also works for the
  1054. * functions dealing with the user's productions, as they are located in
  1055. * the same directory.
  1056. * @param integer User ID
  1057. * @param string Type of path to return (can be 'none', 'system', 'rel', 'web')
  1058. * @param bool deprecated see #7110
  1059. * @param bool If we want that the function returns the /main/img/unknown.jpg image set it at true
  1060. * @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 er's
  1061. */
  1062. public static function get_user_picture_path_by_id($id, $type = 'none', $preview = false, $anonymous = false)
  1063. {
  1064. switch ($type) {
  1065. case 'system': // Base: absolute system path.
  1066. $base = api_get_path(SYS_CODE_PATH);
  1067. break;
  1068. case 'rel': // Base: semi-absolute web path (no server base).
  1069. $base = api_get_path(REL_CODE_PATH);
  1070. break;
  1071. case 'web': // Base: absolute web path.
  1072. $base = api_get_path(WEB_CODE_PATH);
  1073. break;
  1074. case 'none':
  1075. default: // Base: empty, the result path below will be relative.
  1076. $base = '';
  1077. }
  1078. if (empty($id) || empty($type)) {
  1079. return $anonymous ? array('dir' => $base.'img/', 'file' => 'unknown.jpg') : array('dir' => '', 'file' => '');
  1080. }
  1081. $user_id = intval($id);
  1082. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  1083. $sql = "SELECT picture_uri FROM $user_table WHERE user_id=".$user_id;
  1084. $res = Database::query($sql);
  1085. if (!Database::num_rows($res)) {
  1086. return $anonymous ? array('dir' => $base.'img/', 'file' => 'unknown.jpg') : array('dir' => '', 'file' => '');
  1087. }
  1088. $user = Database::fetch_array($res);
  1089. $picture_filename = trim($user['picture_uri']);
  1090. if (api_get_setting('split_users_upload_directory') === 'true') {
  1091. /*if (!empty($picture_filename) or $preview) {
  1092. $dir = $base.'upload/users/'.substr((string) $user_id, 0, 1).'/'.$user_id.'/';
  1093. } else {
  1094. $dir = $base.'upload/users/'.$user_id.'/';
  1095. }*/
  1096. $dir = $base.'upload/users/'.substr((string) $user_id, 0, 1).'/'.$user_id.'/';
  1097. } else {
  1098. $dir = $base.'upload/users/'.$user_id.'/';
  1099. }
  1100. if (empty($picture_filename) && $anonymous) {
  1101. return array('dir' => $base.'img/', 'file' => 'unknown.jpg');
  1102. }
  1103. return array('dir' => $dir, 'file' => $picture_filename);
  1104. }
  1105. /**
  1106. * Creates new user photos in various sizes of a user, or deletes user photos.
  1107. * Note: This method relies on configuration setting from main/inc/conf/profile.conf.php
  1108. * @param int $user_id The user internal identitfication number.
  1109. * @param string $file The common file name for the newly created photos.
  1110. * It will be checked and modified for compatibility with the file system.
  1111. * If full name is provided, path component is ignored.
  1112. * If an empty name is provided, then old user photos are deleted only,
  1113. * @see UserManager::delete_user_picture() as the prefered way for deletion.
  1114. * @param string $source_file The full system name of the image from which user photos will be created.
  1115. * @return string/bool Returns the resulting common file name of created images which usually should be stored in database.
  1116. * When deletion is recuested returns empty string. In case of internal error or negative validation returns FALSE.
  1117. */
  1118. public static function update_user_picture($user_id, $file = null, $source_file = null)
  1119. {
  1120. if (empty($user_id)) {
  1121. return false;
  1122. }
  1123. $delete = empty($file);
  1124. if (empty($source_file)) {
  1125. $source_file = $file;
  1126. }
  1127. // Configuration options about user photos.
  1128. require_once api_get_path(CONFIGURATION_PATH).'profile.conf.php';
  1129. // User-reserved directory where photos have to be placed.
  1130. $path_info = self::get_user_picture_path_by_id($user_id, 'system', true);
  1131. $path = $path_info['dir'];
  1132. // If this directory does not exist - we create it.
  1133. if (!file_exists($path)) {
  1134. @mkdir($path, api_get_permissions_for_new_directories(), true);
  1135. }
  1136. // The old photos (if any).
  1137. $old_file = $path_info['file'];
  1138. // Let us delete them.
  1139. if (!empty($old_file)) {
  1140. if (KEEP_THE_OLD_IMAGE_AFTER_CHANGE) {
  1141. $prefix = 'saved_'.date('Y_m_d_H_i_s').'_'.uniqid('').'_';
  1142. @rename($path.'small_'.$old_file, $path.$prefix.'small_'.$old_file);
  1143. @rename($path.'medium_'.$old_file, $path.$prefix.'medium_'.$old_file);
  1144. @rename($path.'big_'.$old_file, $path.$prefix.'big_'.$old_file);
  1145. @rename($path.$old_file, $path.$prefix.$old_file);
  1146. } else {
  1147. @unlink($path.'small_'.$old_file);
  1148. @unlink($path.'medium_'.$old_file);
  1149. @unlink($path.'big_'.$old_file);
  1150. @unlink($path.$old_file);
  1151. }
  1152. }
  1153. // Exit if only deletion has been requested. Return an empty picture name.
  1154. if ($delete) {
  1155. return '';
  1156. }
  1157. // Validation 2.
  1158. $allowed_types = api_get_supported_image_extensions();
  1159. $file = str_replace('\\', '/', $file);
  1160. $filename = (($pos = strrpos($file, '/')) !== false) ? substr($file, $pos + 1) : $file;
  1161. $extension = strtolower(substr(strrchr($filename, '.'), 1));
  1162. if (!in_array($extension, $allowed_types)) {
  1163. return false;
  1164. }
  1165. // This is the common name for the new photos.
  1166. if (KEEP_THE_NAME_WHEN_CHANGE_IMAGE && !empty($old_file)) {
  1167. $old_extension = strtolower(substr(strrchr($old_file, '.'), 1));
  1168. $filename = in_array($old_extension, $allowed_types) ? substr($old_file, 0, -strlen($old_extension)) : $old_file;
  1169. $filename = (substr($filename, -1) == '.') ? $filename.$extension : $filename.'.'.$extension;
  1170. } else {
  1171. $filename = replace_dangerous_char($filename);
  1172. if (PREFIX_IMAGE_FILENAME_WITH_UID) {
  1173. $filename = uniqid('').'_'.$filename;
  1174. }
  1175. // We always prefix user photos with user ids, so on setting
  1176. // api_get_setting('split_users_upload_directory') === 'true'
  1177. // the correspondent directories to be found successfully.
  1178. $filename = $user_id.'_'.$filename;
  1179. }
  1180. // Storing the new photos in 4 versions with various sizes.
  1181. $small = self::resize_picture($source_file, 22);
  1182. $medium = self::resize_picture($source_file, 85);
  1183. $normal = self::resize_picture($source_file, 200);
  1184. $big = new Image($source_file); // This is the original picture.
  1185. $ok = $small && $small->send_image($path.'small_'.$filename) &&
  1186. $medium && $medium->send_image($path.'medium_'.$filename) &&
  1187. $normal && $normal->send_image($path.$filename) &&
  1188. $big && $big->send_image($path.'big_'.$filename);
  1189. return $ok ? $filename : false;
  1190. }
  1191. /**
  1192. * Update User extra field file type into {user_folder}/{$extra_field}
  1193. * @param int $user_id The user internal identification number
  1194. * @param string $extra_field The $extra_field The extra field name
  1195. * @param null $file The filename
  1196. * @param null $source_file The temporal filename
  1197. * @return bool|null return filename if success, but false
  1198. */
  1199. public static function update_user_extra_file($user_id, $extra_field = '', $file = null, $source_file = null)
  1200. {
  1201. // Add Filter
  1202. $source_file = Security::filter_filename($source_file);
  1203. $file = Security::filter_filename($file);
  1204. if (empty($user_id)) {
  1205. return false;
  1206. }
  1207. if (empty($source_file)) {
  1208. $source_file = $file;
  1209. }
  1210. // User-reserved directory where extra file have to be placed.
  1211. $path_info = self::get_user_picture_path_by_id($user_id, 'system', true);
  1212. $path = $path_info['dir'];
  1213. if (!empty($extra_field)) {
  1214. $path .= $extra_field . '/';
  1215. }
  1216. // If this directory does not exist - we create it.
  1217. if (!file_exists($path)) {
  1218. @mkdir($path, api_get_permissions_for_new_directories(), true);
  1219. }
  1220. if (filter_extension($file)) {
  1221. if (@move_uploaded_file($source_file,$path.$file)) {
  1222. if ($extra_field) {
  1223. return $extra_field.'/'.$file;
  1224. } else {
  1225. return $file;
  1226. }
  1227. }
  1228. }
  1229. return false; // this should be returned if anything went wrong with the upload
  1230. }
  1231. /**
  1232. * Deletes user photos.
  1233. * Note: This method relies on configuration setting from main/inc/conf/profile.conf.php
  1234. * @param int $user_id The user internal identitfication number.
  1235. * @return string/bool Returns empty string on success, FALSE on error.
  1236. */
  1237. public static function delete_user_picture($user_id)
  1238. {
  1239. return self::update_user_picture($user_id);
  1240. }
  1241. /**
  1242. * Returns an XHTML formatted list of productions for a user, or FALSE if he
  1243. * doesn't have any.
  1244. *
  1245. * If there has been a request to remove a production, the function will return
  1246. * without building the list unless forced to do so by the optional second
  1247. * parameter. This increases performance by avoiding to read through the
  1248. * productions on the filesystem before the removal request has been carried
  1249. * out because they'll have to be re-read afterwards anyway.
  1250. *
  1251. * @param $user_id User id
  1252. * @param $force Optional parameter to force building after a removal request
  1253. * @return A string containing the XHTML code to dipslay the production list, or FALSE
  1254. */
  1255. public static function build_production_list($user_id, $force = false, $showdelete = false)
  1256. {
  1257. if (!$force && !empty($_POST['remove_production'])) {
  1258. return true; // postpone reading from the filesystem
  1259. }
  1260. $productions = self::get_user_productions($user_id);
  1261. if (empty($productions)) {
  1262. return false;
  1263. }
  1264. $production_path = self::get_user_picture_path_by_id($user_id, 'web');
  1265. $production_dir = $production_path['dir'];
  1266. $del_image = api_get_path(WEB_CODE_PATH).'img/delete.png';
  1267. $add_image = api_get_path(WEB_CODE_PATH).'img/archive.png';
  1268. $del_text = get_lang('Delete');
  1269. $production_list = '';
  1270. if (count($productions) > 0) {
  1271. $production_list = '<div class="files-production"> <ul id="productions">';
  1272. foreach ($productions as $file) {
  1273. $production_list .= '<li><img src="'.$add_image.'" /><a href="'.$production_dir.urlencode($file).'" target="_blank">'.htmlentities($file).'</a>';
  1274. if ($showdelete) {
  1275. $production_list .= '&nbsp;&nbsp;<input style="width:16px;" type="image" name="remove_production['.urlencode($file).']" src="'.$del_image.'" alt="'.$del_text.'" title="'.$del_text.' '.htmlentities($file).'" onclick="javascript: return confirmation(\''.htmlentities($file).'\');" /></li>';
  1276. }
  1277. }
  1278. $production_list .= '</ul></div>';
  1279. }
  1280. return $production_list;
  1281. }
  1282. /**
  1283. * Returns an array with the user's productions.
  1284. *
  1285. * @param $user_id User id
  1286. * @return An array containing the user's productions
  1287. */
  1288. public static function get_user_productions($user_id)
  1289. {
  1290. $production_path = self::get_user_picture_path_by_id($user_id, 'system');
  1291. $production_repository = $production_path['dir'];
  1292. $productions = array();
  1293. if (is_dir($production_repository)) {
  1294. $handle = opendir($production_repository);
  1295. while ($file = readdir($handle)) {
  1296. if ($file == '.' ||
  1297. $file == '..' ||
  1298. $file == '.htaccess' ||
  1299. is_dir($production_repository.$file)
  1300. ) {
  1301. // skip current/parent directory and .htaccess
  1302. continue;
  1303. }
  1304. if (preg_match('/('.$user_id.'|[0-9a-f]{13}|saved)_.+\.(png|jpg|jpeg|gif)$/i', $file)) {
  1305. // User's photos should not be listed as productions.
  1306. continue;
  1307. }
  1308. $productions[] = $file;
  1309. }
  1310. }
  1311. return $productions;
  1312. }
  1313. /**
  1314. * Remove a user production.
  1315. *
  1316. * @param $user_id User id
  1317. * @param $production The production to remove
  1318. */
  1319. public static function remove_user_production($user_id, $production)
  1320. {
  1321. $production_path = self::get_user_picture_path_by_id($user_id, 'system');
  1322. $production_file = $production_path['dir'].$production;
  1323. if (is_file($production_file)) {
  1324. unlink($production_file);
  1325. return true;
  1326. }
  1327. return false;
  1328. }
  1329. /**
  1330. * Update an extra field. This function is called when a user changes his/her profile
  1331. * and by consequence fills or edits his/her extra fields.
  1332. *
  1333. * @param integer Field ID
  1334. * @param array Database columns and their new value
  1335. * @return boolean true if field updated, false otherwise
  1336. */
  1337. public static function update_extra_field($fid, $columns)
  1338. {
  1339. //TODO check that values added are values proposed for enumerated field types
  1340. $t_uf = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  1341. $fid = Database::escape_string($fid);
  1342. $sqluf = "UPDATE $t_uf SET ";
  1343. $known_fields = array(
  1344. 'id',
  1345. 'field_variable',
  1346. 'field_type',
  1347. 'field_display_text',
  1348. 'field_default_value',
  1349. 'field_order',
  1350. 'field_visible',
  1351. 'field_changeable',
  1352. 'field_filter'
  1353. );
  1354. $safecolumns = array();
  1355. foreach ($columns as $index => $newval) {
  1356. if (in_array($index, $known_fields)) {
  1357. $safecolumns[$index] = Database::escape_string($newval);
  1358. $sqluf .= $index." = '".$safecolumns[$index]."', ";
  1359. }
  1360. }
  1361. $time = time();
  1362. $sqluf .= " tms = FROM_UNIXTIME($time) WHERE id='$fid'";
  1363. $resuf = Database::query($sqluf);
  1364. return $resuf;
  1365. }
  1366. /**
  1367. * Update an extra field value for a given user
  1368. * @param integer $user_id User ID
  1369. * @param string $fname Field variable name
  1370. * @param string $fvalue Field value
  1371. *
  1372. * @return boolean true if field updated, false otherwise
  1373. */
  1374. public static function update_extra_field_value($user_id, $fname, $fvalue = '')
  1375. {
  1376. //TODO check that values added are values proposed for enumerated field types
  1377. $t_uf = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  1378. $t_ufo = Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  1379. $t_ufv = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  1380. $fname = Database::escape_string($fname);
  1381. if ($user_id != strval(intval($user_id))) {
  1382. return false;
  1383. }
  1384. if ($user_id === false) {
  1385. return false;
  1386. }
  1387. $fvalues = '';
  1388. if (is_array($fvalue)) {
  1389. foreach ($fvalue as $val) {
  1390. $fvalues .= $val.';';
  1391. }
  1392. if (!empty($fvalues)) {
  1393. $fvalues = substr($fvalues, 0, -1);
  1394. }
  1395. } else {
  1396. $fvalues = $fvalue;
  1397. }
  1398. $sqluf = "SELECT * FROM $t_uf WHERE field_variable='$fname'";
  1399. $resuf = Database::query($sqluf);
  1400. $is_extra_file = false;
  1401. if (Database::num_rows($resuf) == 1) {
  1402. //ok, the field exists
  1403. // Check if enumerated field, if the option is available
  1404. $rowuf = Database::fetch_array($resuf);
  1405. switch ($rowuf['field_type']) {
  1406. case self::USER_FIELD_TYPE_TAG :
  1407. //4. Tags are process here comes from main/auth/profile.php
  1408. UserManager::process_tags(explode(';', $fvalues), $user_id, $rowuf['id']);
  1409. return true;
  1410. break;
  1411. /*case self::USER_FIELD_TYPE_SELECT_MULTIPLE :
  1412. // check code from UserManager::update_user_picture() to use something similar here
  1413. break;*/
  1414. case self::USER_FIELD_TYPE_RADIO:
  1415. case self::USER_FIELD_TYPE_SELECT:
  1416. case self::USER_FIELD_TYPE_SELECT_MULTIPLE:
  1417. $sqluo = "SELECT * FROM $t_ufo WHERE field_id = ".$rowuf['id'];
  1418. $resuo = Database::query($sqluo);
  1419. $values = explode(';', $fvalues);
  1420. if (Database::num_rows($resuo) > 0) {
  1421. $check = false;
  1422. while ($rowuo = Database::fetch_array($resuo)) {
  1423. if (in_array($rowuo['option_value'], $values)) {
  1424. $check = true;
  1425. break;
  1426. }
  1427. }
  1428. if (!$check) {
  1429. return false; //option value not found
  1430. }
  1431. } else {
  1432. return false; //enumerated type but no option found
  1433. }
  1434. break;
  1435. case self::USER_FIELD_TYPE_FILE:
  1436. $is_extra_file = true;
  1437. if (isset($fvalue['tmp_name'])) {
  1438. // Filter against dangerous filenames
  1439. $fvalue['name'] = Security::filter_filename($fvalue['name']);
  1440. $fvalue['tmp_name'] = Security::filter_filename($fvalue['tmp_name']);
  1441. // Update and recover the filename
  1442. $fvalues = UserManager::update_user_extra_file(
  1443. $user_id,
  1444. $rowuf['field_variable'],
  1445. $fvalue['name'],
  1446. $fvalue['tmp_name']
  1447. );
  1448. } else {
  1449. // Set empty string to $fvalues to delete it
  1450. $fvalues = '';
  1451. }
  1452. break;
  1453. case 1:
  1454. case 2:
  1455. default:
  1456. break;
  1457. }
  1458. $tms = time();
  1459. $sqlufv = "SELECT * FROM $t_ufv
  1460. WHERE user_id = $user_id AND field_id = ".$rowuf['id']."
  1461. ORDER BY id";
  1462. $resufv = Database::query($sqlufv);
  1463. $n = Database::num_rows($resufv);
  1464. if ($n > 1) {
  1465. //problem, we already have two values for this field and user combination - keep last one
  1466. while ($rowufv = Database::fetch_array($resufv)) {
  1467. if ($n > 1) {
  1468. $sqld = "DELETE FROM $t_ufv WHERE id = ".$rowufv['id'];
  1469. Database::query($sqld);
  1470. $n--;
  1471. }
  1472. $rowufv = Database::fetch_array($resufv);
  1473. if ($rowufv['field_value'] != $fvalues) {
  1474. $sqlu = "UPDATE $t_ufv SET
  1475. field_value = '".Database::escape_string($fvalues)."',
  1476. tms = FROM_UNIXTIME($tms)
  1477. WHERE id = ".$rowufv['id'];
  1478. $resu = Database::query($sqlu);
  1479. return ($resu ? true : false);
  1480. }
  1481. return true;
  1482. }
  1483. } elseif ($n == 1) {
  1484. //we need to update the current record
  1485. $rowufv = Database::fetch_array($resufv);
  1486. if ($rowufv['field_value'] != $fvalues) {
  1487. if ($is_extra_file) {
  1488. // To remove from user folder
  1489. self::remove_user_extra_file(
  1490. $user_id,
  1491. $fname,
  1492. $rowufv['field_value']
  1493. );
  1494. }
  1495. // If the new field is empty, delete it
  1496. if ($fvalues == '') {
  1497. $sql_query = "DELETE FROM $t_ufv
  1498. WHERE id = ".$rowufv['id'].";";
  1499. } else {
  1500. // Otherwise update it
  1501. $sql_query = "UPDATE $t_ufv SET
  1502. field_value = '".Database::escape_string($fvalues)."',
  1503. tms = FROM_UNIXTIME($tms)
  1504. WHERE id = ".$rowufv['id'];
  1505. }
  1506. $resu = Database::query($sql_query);
  1507. return ($resu ? true : false);
  1508. }
  1509. return true;
  1510. } else {
  1511. $sql = "INSERT INTO $t_ufv (user_id,field_id,field_value,tms)
  1512. VALUES ( $user_id, ".$rowuf['id'].", '".Database::escape_string($fvalues)."', FROM_UNIXTIME($tms))";
  1513. $res = Database::query($sql);
  1514. return $res ? true : false;
  1515. }
  1516. } else {
  1517. // Field not found
  1518. return false;
  1519. }
  1520. }
  1521. /**
  1522. * Get an array of extra fields with field details (type, default value and options)
  1523. * @param integer Offset (from which row)
  1524. * @param integer Number of items
  1525. * @param integer Column on which sorting is made
  1526. * @param string Sorting direction
  1527. * @param boolean Optional. Whether we get all the fields or just the visible ones
  1528. * @param int Optional. Whether we get all the fields with field_filter 1 or 0 or everything
  1529. * @return array Extra fields details (e.g. $list[2]['type'], $list[4]['options'][2]['title']
  1530. */
  1531. public static function get_extra_fields(
  1532. $from = 0,
  1533. $number_of_items = 0,
  1534. $column = 5,
  1535. $direction = 'ASC',
  1536. $all_visibility = true,
  1537. $field_filter = null
  1538. ) {
  1539. $fields = array();
  1540. $t_uf = Database :: get_main_table(TABLE_MAIN_USER_FIELD);
  1541. $t_ufo = Database :: get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  1542. $columns = array('id', 'field_variable', 'field_type', 'field_display_text', 'field_default_value', 'field_order', 'field_filter', 'tms');
  1543. $column = intval($column);
  1544. $sort_direction = '';
  1545. if (in_array(strtoupper($direction), array('ASC', 'DESC'))) {
  1546. $sort_direction = strtoupper($direction);
  1547. }
  1548. $sqlf = "SELECT * FROM $t_uf WHERE 1 = 1 ";
  1549. if (!$all_visibility) {
  1550. $sqlf .= " AND field_visible = 1 ";
  1551. }
  1552. if (!is_null($field_filter)) {
  1553. $field_filter = intval($field_filter);
  1554. $sqlf .= " AND field_filter = $field_filter ";
  1555. }
  1556. $sqlf .= " ORDER BY ".$columns[$column]." $sort_direction ";
  1557. if ($number_of_items != 0) {
  1558. $sqlf .= " LIMIT ".intval($from).','.intval($number_of_items);
  1559. }
  1560. $resf = Database::query($sqlf);
  1561. if (Database::num_rows($resf) > 0) {
  1562. while ($rowf = Database::fetch_array($resf)) {
  1563. $fields[$rowf['id']] = array(
  1564. 0 => $rowf['id'],
  1565. 1 => $rowf['field_variable'],
  1566. 2 => $rowf['field_type'],
  1567. //3 => (empty($rowf['field_display_text']) ? '' : get_lang($rowf['field_display_text'], '')),
  1568. // Temporarily removed auto-translation. Need update to get_lang() to know if translation exists (todo)
  1569. // Ivan, 15-SEP-2009: get_lang() has been modified accordingly in order this issue to be solved.
  1570. 3 => (empty($rowf['field_display_text']) ? '' : $rowf['field_display_text']),
  1571. 4 => $rowf['field_default_value'],
  1572. 5 => $rowf['field_order'],
  1573. 6 => $rowf['field_visible'],
  1574. 7 => $rowf['field_changeable'],
  1575. 8 => $rowf['field_filter'],
  1576. 9 => array(),
  1577. 10 => '<a name="'.$rowf['id'].'"></a>',
  1578. );
  1579. $sqlo = "SELECT * FROM $t_ufo WHERE field_id = ".$rowf['id']." ORDER BY option_order ASC";
  1580. $reso = Database::query($sqlo);
  1581. if (Database::num_rows($reso) > 0) {
  1582. while ($rowo = Database::fetch_array($reso)) {
  1583. $fields[$rowf['id']][9][$rowo['id']] = array(
  1584. 0 => $rowo['id'],
  1585. 1 => $rowo['option_value'],
  1586. //2 => (empty($rowo['option_display_text']) ? '' : get_lang($rowo['option_display_text'], '')),
  1587. 2 => (empty($rowo['option_display_text']) ? '' : $rowo['option_display_text']),
  1588. 3 => $rowo['option_order']
  1589. );
  1590. }
  1591. }
  1592. }
  1593. }
  1594. return $fields;
  1595. }
  1596. /**
  1597. * Build a list of extra file already uploaded in $user_folder/{$extra_field}/
  1598. * @param $user_id
  1599. * @param $extra_field
  1600. * @param bool $force
  1601. * @param bool $showdelete
  1602. * @return bool|string
  1603. */
  1604. public static function build_user_extra_file_list($user_id, $extra_field, $force = false, $showdelete = false)
  1605. {
  1606. if (!$force && !empty($_POST['remove_'.$extra_field])) {
  1607. return true; // postpone reading from the filesystem
  1608. }
  1609. $extra_files = self::get_user_extra_files($user_id, $extra_field);
  1610. if (empty($extra_files)) {
  1611. return false;
  1612. }
  1613. $path_info = self::get_user_picture_path_by_id($user_id, 'web', true);
  1614. $path = $path_info['dir'];
  1615. $del_image = api_get_path(WEB_CODE_PATH).'img/delete.png';
  1616. $del_text = get_lang('Delete');
  1617. $extra_file_list = '';
  1618. if (count($extra_files) > 0) {
  1619. $extra_file_list = '<div class="files-production"><ul id="productions">';
  1620. foreach ($extra_files as $file) {
  1621. $filename = substr($file,strlen($extra_field)+1);
  1622. $extra_file_list .= '<li>'.Display::return_icon('archive.png').'<a href="'.$path.$extra_field.'/'.urlencode($filename).'" target="_blank">'.htmlentities($filename).'</a> ';
  1623. if ($showdelete) {
  1624. $extra_file_list .= '<input style="width:16px;" type="image" name="remove_extra_' . $extra_field . '['.urlencode($file).']" src="'.$del_image.'" alt="'.$del_text.'" title="'.$del_text.' '.htmlentities($filename).'" onclick="javascript: return confirmation(\''.htmlentities($filename).'\');" /></li>';
  1625. }
  1626. }
  1627. $extra_file_list .= '</ul></div>';
  1628. }
  1629. return $extra_file_list;
  1630. }
  1631. /**
  1632. * Get valid filenames in $user_folder/{$extra_field}/
  1633. * @param $user_id
  1634. * @param $extra_field
  1635. * @param bool $full_path
  1636. * @return array
  1637. */
  1638. public static function get_user_extra_files($user_id, $extra_field, $full_path = false)
  1639. {
  1640. if (!$full_path) {
  1641. // Nothing to do
  1642. } else {
  1643. $path_info = self::get_user_picture_path_by_id($user_id, 'system', true);
  1644. $path = $path_info['dir'];
  1645. }
  1646. $extra_data = self::get_extra_user_data_by_field($user_id, $extra_field);
  1647. $extra_files = $extra_data[$extra_field];
  1648. if (is_array($extra_files)) {
  1649. foreach ($extra_files as $key => $value) {
  1650. if (!$full_path) {
  1651. // Relative path from user folder
  1652. $files[] = $value;
  1653. } else {
  1654. $files[] = $path.$value;
  1655. }
  1656. }
  1657. } elseif (!empty($extra_files)) {
  1658. if (!$full_path) {
  1659. // Relative path from user folder
  1660. $files[] = $extra_files;
  1661. } else {
  1662. $files[] = $path.$extra_files;
  1663. }
  1664. }
  1665. return $files; // can be an empty array
  1666. }
  1667. /**
  1668. * Remove an {$extra_file} from the user folder $user_folder/{$extra_field}/
  1669. * @param $user_id
  1670. * @param $extra_field
  1671. * @param $extra_file
  1672. * @return bool
  1673. */
  1674. public static function remove_user_extra_file($user_id, $extra_field, $extra_file)
  1675. {
  1676. $extra_file = Security::filter_filename($extra_file);
  1677. $path_info = self::get_user_picture_path_by_id($user_id, 'system', true);
  1678. if (strpos($extra_file, $extra_field) !== false) {
  1679. $path_extra_file = $path_info['dir'].$extra_file;
  1680. } else {
  1681. $path_extra_file = $path_info['dir'].$extra_field.'/'.$extra_file;
  1682. }
  1683. if (is_file($path_extra_file)) {
  1684. unlink($path_extra_file);
  1685. return true;
  1686. }
  1687. return false;
  1688. }
  1689. /**
  1690. * Get the list of options attached to an extra field
  1691. * @param string $fieldname the name of the field
  1692. * @return array the list of options
  1693. */
  1694. public static function get_extra_field_options($field_name)
  1695. {
  1696. $t_uf = Database :: get_main_table(TABLE_MAIN_USER_FIELD);
  1697. $t_ufo = Database :: get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  1698. $sql = 'SELECT options.*
  1699. FROM '.$t_ufo.' options
  1700. INNER JOIN '.$t_uf.' fields
  1701. ON fields.id = options.field_id
  1702. AND fields.field_variable="'.Database::escape_string($field_name).'"';
  1703. $rs = Database::query($sql);
  1704. return Database::store_result($rs);
  1705. }
  1706. /**
  1707. * Get the number of extra fields currently recorded
  1708. * @param boolean Optional switch. true (default) returns all fields, false returns only visible fields
  1709. * @return integer Number of fields
  1710. */
  1711. public static function get_number_of_extra_fields($all_visibility = true)
  1712. {
  1713. $t_uf = Database :: get_main_table(TABLE_MAIN_USER_FIELD);
  1714. $sqlf = "SELECT * FROM $t_uf ";
  1715. if (!$all_visibility) {
  1716. $sqlf .= " WHERE field_visible = 1 ";
  1717. }
  1718. $sqlf .= " ORDER BY field_order";
  1719. $resf = Database::query($sqlf);
  1720. return Database::num_rows($resf);
  1721. }
  1722. /**
  1723. * Creates a new extra field
  1724. * @param string Field's internal variable name
  1725. * @param int Field's type
  1726. * @param string Field's language var name
  1727. * @param string Field's default value
  1728. * @param string Optional comma-separated list of options to provide for select and radio
  1729. * @return int new user id - if the new user creation succeeds, false otherwise
  1730. */
  1731. public static function create_extra_field($fieldvarname, $fieldtype, $fieldtitle, $fielddefault, $fieldoptions = '')
  1732. {
  1733. // database table definition
  1734. $table_field = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  1735. $table_field_options = Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  1736. // First check wether the login already exists
  1737. if (self::is_extra_field_available($fieldvarname)) {
  1738. return api_set_failure('login-pass already taken');
  1739. }
  1740. $sql = "SELECT MAX(field_order) FROM $table_field";
  1741. $res = Database::query($sql);
  1742. $order = 0;
  1743. if (Database::num_rows($res) > 0) {
  1744. $row = Database::fetch_array($res);
  1745. $order = $row[0] + 1;
  1746. }
  1747. $time = time();
  1748. $sql = "INSERT INTO $table_field
  1749. SET field_type = '".Database::escape_string($fieldtype)."',
  1750. field_variable = '".Database::escape_string($fieldvarname)."',
  1751. field_display_text = '".Database::escape_string($fieldtitle)."',
  1752. field_default_value = '".Database::escape_string($fielddefault)."',
  1753. field_order = '$order',
  1754. tms = FROM_UNIXTIME($time)";
  1755. $result = Database::query($sql);
  1756. if ($result) {
  1757. //echo "id returned";
  1758. $return = Database::insert_id();
  1759. event_system(LOG_USER_FIELD_CREATE, LOG_USER_FIELD_VARIABLE, Database::escape_string($fieldvarname));
  1760. } else {
  1761. //echo "false - failed" ;
  1762. return false;
  1763. }
  1764. if (!empty($fieldoptions) && in_array($fieldtype, array(self::USER_FIELD_TYPE_RADIO, self::USER_FIELD_TYPE_SELECT, self::USER_FIELD_TYPE_SELECT_MULTIPLE, self::USER_FIELD_TYPE_DOUBLE_SELECT))) {
  1765. if ($fieldtype == self::USER_FIELD_TYPE_DOUBLE_SELECT) {
  1766. $twolist = explode('|', $fieldoptions);
  1767. $counter = 0;
  1768. foreach ($twolist as $individual_list) {
  1769. $splitted_individual_list = explode(';', $individual_list);
  1770. foreach ($splitted_individual_list as $individual_list_option) {
  1771. //echo 'counter:'.$counter;
  1772. if ($counter == 0) {
  1773. $list[] = $individual_list_option;
  1774. } else {
  1775. $list[] = str_repeat('*', $counter).$individual_list_option;
  1776. }
  1777. }
  1778. $counter++;
  1779. }
  1780. } else {
  1781. $list = explode(';', $fieldoptions);
  1782. }
  1783. foreach ($list as $option) {
  1784. $option = Database::escape_string($option);
  1785. $sql = "SELECT * FROM $table_field_options WHERE field_id = $return AND option_value = '".$option."'";
  1786. $res = Database::query($sql);
  1787. if (Database::num_rows($res) > 0) {
  1788. //the option already exists, do nothing
  1789. } else {
  1790. $sql = "SELECT MAX(option_order) FROM $table_field_options WHERE field_id = $return";
  1791. $res = Database::query($sql);
  1792. $max = 1;
  1793. if (Database::num_rows($res) > 0) {
  1794. $row = Database::fetch_array($res);
  1795. $max = $row[0] + 1;
  1796. }
  1797. $time = time();
  1798. $sql = "INSERT INTO $table_field_options (field_id,option_value,option_display_text,option_order,tms)
  1799. VALUES ($return,'$option','$option',$max,FROM_UNIXTIME($time))";
  1800. $res = Database::query($sql);
  1801. if ($res === false) {
  1802. $return = false;
  1803. }
  1804. }
  1805. }
  1806. }
  1807. return $return;
  1808. }
  1809. /**
  1810. * Save the changes in the definition of the extra user profile field
  1811. * The function is called after you (as admin) decide to store the changes you have made to one of the fields you defined
  1812. *
  1813. * There is quite some logic in this field
  1814. * 1. store the changes to the field (tupe, name, label, default text)
  1815. * 2. remove the options and the choices of the users from the database that no longer occur in the form field 'possible values'. We should only remove
  1816. * the options (and choices) that do no longer have to appear. We cannot remove all options and choices because if you remove them all
  1817. * and simply re-add them all then all the user who have already filled this form will loose their selected value.
  1818. * 3. we add the options that are newly added
  1819. *
  1820. * <code> current options are a;b;c and the user changes this to a;b;x (removing c and adding x)
  1821. * we first remove c (and also the entry in the option_value table for the users who have chosen this)
  1822. * we then add x
  1823. * a and b are neither removed nor added
  1824. * </code>
  1825. * @param integer $fieldid the id of the field we are editing
  1826. * @param string $fieldvarname the internal variable name of the field
  1827. * @param int $fieldtype the type of the field
  1828. * @param string $fieldtitle the title of the field
  1829. * @param string $fielddefault the default value of the field
  1830. * @param string $fieldoptions Optional comma-separated list of options to provide for select and radio
  1831. * @return boolean true
  1832. *
  1833. *
  1834. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  1835. * @version July 2008
  1836. * @since v1.8.6
  1837. */
  1838. public static function save_extra_field_changes($fieldid, $fieldvarname, $fieldtype, $fieldtitle, $fielddefault, $fieldoptions = '')
  1839. {
  1840. // database table definition
  1841. $table_field = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  1842. $table_field_options = Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  1843. $table_field_options_values = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  1844. // we first update the field definition with the new values
  1845. $time = time();
  1846. $sql = "UPDATE $table_field
  1847. SET field_type = '".Database::escape_string($fieldtype)."',
  1848. field_variable = '".Database::escape_string($fieldvarname)."',
  1849. field_display_text = '".Database::escape_string($fieldtitle)."',
  1850. field_default_value = '".Database::escape_string($fielddefault)."',
  1851. tms = FROM_UNIXTIME($time)
  1852. WHERE id = '".intval($fieldid)."'";
  1853. $result = Database::query($sql);
  1854. // we create an array with all the options (will be used later in the script)
  1855. if ($fieldtype == self::USER_FIELD_TYPE_DOUBLE_SELECT) {
  1856. $twolist = explode('|', $fieldoptions);
  1857. $counter = 0;
  1858. foreach ($twolist as $individual_list) {
  1859. $splitted_individual_list = explode(';', $individual_list);
  1860. foreach ($splitted_individual_list as $individual_list_option) {
  1861. //echo 'counter:'.$counter;
  1862. if ($counter == 0) {
  1863. $list[] = trim($individual_list_option);
  1864. } else {
  1865. $list[] = str_repeat('*', $counter).trim($individual_list_option);
  1866. }
  1867. }
  1868. $counter++;
  1869. }
  1870. } else {
  1871. $templist = explode(';', $fieldoptions);
  1872. $list = array_map('trim', $templist);
  1873. }
  1874. // Remove all the field options (and also the choices of the user) that are NOT in the new list of options
  1875. $sql = "SELECT * FROM $table_field_options WHERE option_value NOT IN ('".implode("','", $list)."') AND field_id = '".intval($fieldid)."'";
  1876. $result = Database::query($sql);
  1877. $return['deleted_options'] = 0;
  1878. while ($row = Database::fetch_array($result)) {
  1879. // deleting the option
  1880. $sql_delete_option = "DELETE FROM $table_field_options WHERE id='".intval($row['id'])."'";
  1881. Database::query($sql_delete_option);
  1882. $return['deleted_options']++;
  1883. // deleting the answer of the user who has chosen this option
  1884. $sql_delete_option_value = "DELETE FROM $table_field_options_values
  1885. WHERE field_id = '".intval($fieldid)."' AND field_value = '".Database::escape_string($row['option_value'])."'";
  1886. Database::query($sql_delete_option_value);
  1887. $return['deleted_option_values'] = $return['deleted_option_values'] + Database::affected_rows();
  1888. }
  1889. // we now try to find the field options that are newly added
  1890. $sql = "SELECT * FROM $table_field_options WHERE field_id = '".intval($fieldid)."'";
  1891. $result = Database::query($sql);
  1892. while ($row = Database::fetch_array($result)) {
  1893. // we remove every option that is already in the database from the $list
  1894. if (in_array(trim($row['option_display_text']), $list)) {
  1895. $key = array_search(trim($row['option_display_text']), $list);
  1896. unset($list[$key]);
  1897. }
  1898. }
  1899. // we store the new field options in the database
  1900. foreach ($list as $key => $option) {
  1901. $sql = "SELECT MAX(option_order) FROM $table_field_options WHERE field_id = '".intval($fieldid)."'";
  1902. $res = Database::query($sql);
  1903. $max = 1;
  1904. if (Database::num_rows($res) > 0) {
  1905. $row = Database::fetch_array($res);
  1906. $max = $row[0] + 1;
  1907. }
  1908. $time = time();
  1909. $sql = "INSERT INTO $table_field_options (field_id,option_value,option_display_text,option_order,tms)
  1910. VALUES ('".intval($fieldid)."','".Database::escape_string($option)."','".Database::escape_string($option)."',$max,FROM_UNIXTIME($time))";
  1911. $result = Database::query($sql);
  1912. }
  1913. return true;
  1914. }
  1915. /**
  1916. * Check if a field is available
  1917. * @param string the wanted fieldname
  1918. * @return boolean true if the wanted username is available
  1919. */
  1920. public static function is_extra_field_available($fieldname)
  1921. {
  1922. $t_uf = Database :: get_main_table(TABLE_MAIN_USER_FIELD);
  1923. $sql = "SELECT * FROM $t_uf WHERE field_variable = '".Database::escape_string($fieldname)."'";
  1924. $res = Database::query($sql);
  1925. return Database::num_rows($res) > 0;
  1926. }
  1927. /**
  1928. * Gets user extra fields data
  1929. * @param integer User ID
  1930. * @param boolean Whether to prefix the fields indexes with "extra_" (might be used by formvalidator)
  1931. * @param boolean Whether to return invisible fields as well
  1932. * @param boolean Whether to split multiple-selection fields or not
  1933. * @return array Array of fields => value for the given user
  1934. */
  1935. public static function get_extra_user_data($user_id, $prefix = false, $all_visibility = true, $splitmultiple = false, $field_filter = null)
  1936. {
  1937. // A sanity check.
  1938. if (empty($user_id)) {
  1939. $user_id = 0;
  1940. } else {
  1941. if ($user_id != strval(intval($user_id)))
  1942. return array();
  1943. }
  1944. $extra_data = array();
  1945. $t_uf = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  1946. $t_ufv = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  1947. $user_id = intval($user_id);
  1948. $sql = "SELECT f.id as id, f.field_variable as fvar, f.field_type as type FROM $t_uf f ";
  1949. $filter_cond = '';
  1950. if (!$all_visibility) {
  1951. if (isset($field_filter)) {
  1952. $field_filter = intval($field_filter);
  1953. $filter_cond .= " AND field_filter = $field_filter ";
  1954. }
  1955. $sql .= " WHERE f.field_visible = 1 $filter_cond ";
  1956. } else {
  1957. if (isset($field_filter)) {
  1958. $field_filter = intval($field_filter);
  1959. $sql .= " WHERE field_filter = $field_filter ";
  1960. }
  1961. }
  1962. $sql .= " ORDER BY f.field_order";
  1963. $res = Database::query($sql);
  1964. if (Database::num_rows($res) > 0) {
  1965. while ($row = Database::fetch_array($res)) {
  1966. if ($row['type'] == self::USER_FIELD_TYPE_TAG) {
  1967. $tags = self::get_user_tags_to_string($user_id, $row['id'], false);
  1968. $extra_data['extra_'.$row['fvar']] = $tags;
  1969. } else {
  1970. $sqlu = "SELECT field_value as fval FROM $t_ufv WHERE field_id=".$row['id']." AND user_id = ".$user_id;
  1971. $resu = Database::query($sqlu);
  1972. $fval = '';
  1973. // get default value
  1974. $sql_df = "SELECT field_default_value as fval_df ".
  1975. " FROM $t_uf ".
  1976. " WHERE id=".$row['id'];
  1977. $res_df = Database::query($sql_df);
  1978. if (Database::num_rows($resu) > 0) {
  1979. $rowu = Database::fetch_array($resu);
  1980. $fval = $rowu['fval'];
  1981. if ($row['type'] == self::USER_FIELD_TYPE_SELECT_MULTIPLE) {
  1982. $fval = explode(';', $rowu['fval']);
  1983. }
  1984. } else {
  1985. $row_df = Database::fetch_array($res_df);
  1986. $fval = $row_df['fval_df'];
  1987. }
  1988. // We get here (and fill the $extra_data array) even if there is no user with data (we fill it with default values)
  1989. if ($prefix) {
  1990. if ($row['type'] == self::USER_FIELD_TYPE_RADIO) {
  1991. $extra_data['extra_'.$row['fvar']]['extra_'.$row['fvar']] = $fval;
  1992. } else {
  1993. $extra_data['extra_'.$row['fvar']] = $fval;
  1994. }
  1995. } else {
  1996. if ($row['type'] == self::USER_FIELD_TYPE_RADIO) {
  1997. $extra_data['extra_'.$row['fvar']]['extra_'.$row['fvar']] = $fval;
  1998. } else {
  1999. $extra_data[$row['fvar']] = $fval;
  2000. }
  2001. }
  2002. }
  2003. }
  2004. }
  2005. return $extra_data;
  2006. }
  2007. /** Get extra user data by field
  2008. * @param int user ID
  2009. * @param string the internal variable name of the field
  2010. * @return array with extra data info of a user i.e array('field_variable'=>'value');
  2011. */
  2012. public static function get_extra_user_data_by_field($user_id, $field_variable, $prefix = false, $all_visibility = true, $splitmultiple = false)
  2013. {
  2014. // A sanity check.
  2015. if (empty($user_id)) {
  2016. $user_id = 0;
  2017. } else {
  2018. if ($user_id != strval(intval($user_id)))
  2019. return array();
  2020. }
  2021. $extra_data = array();
  2022. $t_uf = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  2023. $t_ufv = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  2024. $user_id = intval($user_id);
  2025. $sql = "SELECT f.id as id, f.field_variable as fvar, f.field_type as type FROM $t_uf f ";
  2026. $sql .= " WHERE f.field_variable = '$field_variable' ";
  2027. if (!$all_visibility) {
  2028. $sql .= " AND f.field_visible = 1 ";
  2029. }
  2030. $sql .= " ORDER BY f.field_order";
  2031. $res = Database::query($sql);
  2032. if (Database::num_rows($res) > 0) {
  2033. while ($row = Database::fetch_array($res)) {
  2034. $sqlu = "SELECT field_value as fval ".
  2035. " FROM $t_ufv ".
  2036. " WHERE field_id=".$row['id']."".
  2037. " AND user_id=".$user_id;
  2038. $resu = Database::query($sqlu);
  2039. $fval = '';
  2040. if (Database::num_rows($resu) > 0) {
  2041. $rowu = Database::fetch_array($resu);
  2042. $fval = $rowu['fval'];
  2043. if ($row['type'] == self::USER_FIELD_TYPE_SELECT_MULTIPLE) {
  2044. $fval = explode(';', $rowu['fval']);
  2045. }
  2046. }
  2047. if ($prefix) {
  2048. $extra_data['extra_'.$row['fvar']] = $fval;
  2049. } else {
  2050. $extra_data[$row['fvar']] = $fval;
  2051. }
  2052. }
  2053. }
  2054. return $extra_data;
  2055. }
  2056. /**
  2057. * Get the extra field information for a certain field (the options as well)
  2058. * @param int The name of the field we want to know everything about
  2059. * @return array Array containing all the information about the extra profile field
  2060. * (first level of array contains field details, then 'options' sub-array contains options details,
  2061. * as returned by the database)
  2062. * @author Julio Montoya
  2063. * @since v1.8.6
  2064. */
  2065. public static function get_extra_field_information_by_name($field_variable, $fuzzy = false)
  2066. {
  2067. // database table definition
  2068. $table_field = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  2069. $table_field_options = Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  2070. // all the information of the field
  2071. $sql = "SELECT * FROM $table_field WHERE field_variable='".Database::escape_string($field_variable)."'";
  2072. $result = Database::query($sql);
  2073. $return = Database::fetch_array($result);
  2074. // all the options of the field
  2075. $sql = "SELECT * FROM $table_field_options WHERE field_id='".intval($return['id'])."' ORDER BY option_order ASC";
  2076. $result = Database::query($sql);
  2077. while ($row = Database::fetch_array($result)) {
  2078. $return['options'][$row['id']] = $row;
  2079. }
  2080. return $return;
  2081. }
  2082. public static function get_all_extra_field_by_type($field_type)
  2083. {
  2084. // database table definition
  2085. $table_field = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  2086. // all the information of the field
  2087. $sql = "SELECT * FROM $table_field WHERE field_type='".Database::escape_string($field_type)."'";
  2088. $result = Database::query($sql);
  2089. $return = array();
  2090. while ($row = Database::fetch_array($result)) {
  2091. $return[] = $row['id'];
  2092. }
  2093. return $return;
  2094. }
  2095. /**
  2096. * Get all the extra field information of a certain field (also the options)
  2097. *
  2098. * @param int $field_name the name of the field we want to know everything of
  2099. * @return array $return containing all th information about the extra profile field
  2100. * @author Julio Montoya
  2101. * @since v1.8.6
  2102. */
  2103. public static function get_extra_field_information($field_id)
  2104. {
  2105. // database table definition
  2106. $table_field = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  2107. $table_field_options = Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  2108. // all the information of the field
  2109. $sql = "SELECT * FROM $table_field WHERE id='".intval($field_id)."'";
  2110. $result = Database::query($sql);
  2111. $return = Database::fetch_array($result);
  2112. // all the options of the field
  2113. $sql = "SELECT * FROM $table_field_options WHERE field_id='".intval($field_id)."' ORDER BY option_order ASC";
  2114. $result = Database::query($sql);
  2115. while ($row = Database::fetch_array($result)) {
  2116. $return['options'][$row['id']] = $row;
  2117. }
  2118. return $return;
  2119. }
  2120. /** Get extra user data by value
  2121. * @param string the internal variable name of the field
  2122. * @param string the internal value of the field
  2123. * @return array with extra data info of a user i.e array('field_variable'=>'value');
  2124. */
  2125. public static function get_extra_user_data_by_value($field_variable, $field_value, $all_visibility = true)
  2126. {
  2127. $extra_data = array();
  2128. $table_user_field = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  2129. $table_user_field_values = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  2130. $table_user_field_options = Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  2131. $where = '';
  2132. /*
  2133. if (is_array($field_variable_array) && is_array($field_value_array)) {
  2134. if (count($field_variable_array) == count($field_value_array)) {
  2135. $field_var_count = count($field_variable_array);
  2136. for ($i = 0; $i < $field_var_count; $i++) {
  2137. if ($i != 0 && $i != $field_var_count) {
  2138. $where.= ' AND ';
  2139. }
  2140. $where.= "field_variable='".Database::escape_string($field_variable_array[$i])."' AND user_field_options.id='".Database::escape_string($field_value_array[$i])."'";
  2141. }
  2142. }
  2143. } */
  2144. $where = "field_variable='".Database::escape_string($field_variable)."' AND field_value='".Database::escape_string($field_value)."'";
  2145. $sql = "SELECT user_id FROM $table_user_field user_field INNER JOIN $table_user_field_values user_field_values
  2146. ON (user_field.id = user_field_values.field_id)
  2147. WHERE $where";
  2148. if ($all_visibility) {
  2149. $sql .= " AND user_field.field_visible = 1 ";
  2150. } else {
  2151. $sql .= " AND user_field.field_visible = 0 ";
  2152. }
  2153. $res = Database::query($sql);
  2154. $result_data = array();
  2155. if (Database::num_rows($res) > 0) {
  2156. while ($row = Database::fetch_array($res)) {
  2157. $result_data[] = $row['user_id'];
  2158. }
  2159. }
  2160. return $result_data;
  2161. }
  2162. /**
  2163. * Get extra user data by field variable
  2164. * @param string field variable
  2165. * @return array data
  2166. */
  2167. public static function get_extra_user_data_by_field_variable($field_variable)
  2168. {
  2169. $tbl_user_field_values = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  2170. $extra_information_by_variable = self::get_extra_field_information_by_name($field_variable);
  2171. $field_id = intval($extra_information_by_variable['id']);
  2172. $data = array();
  2173. $sql = "SELECT * FROM $tbl_user_field_values WHERE field_id='$field_id'";
  2174. $rs = Database::query($sql);
  2175. if (Database::num_rows($rs) > 0) {
  2176. while ($row = Database::fetch_array($rs)) {
  2177. $user_id = $row['user_id'];
  2178. $data[$user_id] = $row;
  2179. }
  2180. }
  2181. return $data;
  2182. }
  2183. /**
  2184. * Gives a list of [session_category][session_id] for the current user.
  2185. * @param integer $user_id
  2186. * @param boolean whether to fill the first element or not (to give space for courses out of categories)
  2187. * @param boolean optional true if limit time from session is over, false otherwise
  2188. * @return array list of statuses [session_category][session_id]
  2189. * @todo ensure multiple access urls are managed correctly
  2190. */
  2191. public static function get_sessions_by_category(
  2192. $user_id,
  2193. $is_time_over = false,
  2194. $ignore_visibility_for_admins = false
  2195. ) {
  2196. // Database Table Definitions
  2197. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  2198. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2199. $tbl_session_category = Database :: get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  2200. if ($user_id != strval(intval($user_id))) {
  2201. return array();
  2202. }
  2203. // Get the list of sessions per user
  2204. $now = api_get_utc_datetime();
  2205. $sql = "SELECT DISTINCT
  2206. session.id,
  2207. session.name,
  2208. session.date_start,
  2209. session.date_end,
  2210. session_category_id,
  2211. session_category.name as session_category_name,
  2212. session_category.date_start session_category_date_start,
  2213. session_category.date_end session_category_date_end,
  2214. nb_days_access_before_beginning,
  2215. nb_days_access_after_end
  2216. FROM $tbl_session as session
  2217. LEFT JOIN $tbl_session_category session_category
  2218. ON (session_category_id = session_category.id)
  2219. LEFT JOIN $tbl_session_course_user as session_rel_course_user
  2220. ON (session_rel_course_user.id_session = session.id)
  2221. WHERE (
  2222. session_rel_course_user.id_user = $user_id OR
  2223. session.id_coach = $user_id
  2224. )
  2225. ORDER BY session_category_name, name";
  2226. $result = Database::query($sql);
  2227. $categories = array();
  2228. if (Database::num_rows($result) > 0) {
  2229. while ($row = Database::fetch_array($result)) {
  2230. // User portal filters:
  2231. if ($is_time_over) {
  2232. // History
  2233. if (isset($row['date_end']) && $row['date_end'] != '0000-00-00') {
  2234. if ($row['date_end'].' 23:59:59' > $now) {
  2235. continue;
  2236. }
  2237. }
  2238. if ($row['date_end'] == '0000-00-00') {
  2239. continue;
  2240. }
  2241. } else {
  2242. // Current user portal
  2243. if (api_is_allowed_to_create_course()) {
  2244. // Teachers can access the session depending in the access_coach date
  2245. } else {
  2246. if (isset($row['date_end']) && $row['date_end'] != '0000-00-00') {
  2247. if ($row['date_end'].' 23:59:59' <= $now) {
  2248. continue;
  2249. }
  2250. }
  2251. }
  2252. }
  2253. $categories[$row['session_category_id']]['session_category'] = array(
  2254. 'id' => $row['session_category_id'],
  2255. 'name' => $row['session_category_name'],
  2256. 'date_start' => $row['session_category_date_start'],
  2257. 'date_end' => $row['session_category_date_end']
  2258. );
  2259. $session_id = $row['id'];
  2260. $courseList = UserManager::get_courses_list_by_session(
  2261. $user_id,
  2262. $row['id']
  2263. );
  2264. // Session visibility.
  2265. $visibility = api_get_session_visibility(
  2266. $session_id,
  2267. null,
  2268. $ignore_visibility_for_admins
  2269. );
  2270. // Course Coach session visibility.
  2271. $blockedCourseCount = 0;
  2272. $closedVisibilityList = array(
  2273. COURSE_VISIBILITY_CLOSED,
  2274. COURSE_VISIBILITY_HIDDEN
  2275. );
  2276. foreach ($courseList as $course) {
  2277. // Checking session visibility
  2278. $visibility = api_get_session_visibility(
  2279. $session_id,
  2280. $course['code'],
  2281. $ignore_visibility_for_admins
  2282. );
  2283. $courseIsVisible = !in_array($course['visibility'], $closedVisibilityList);
  2284. if ($courseIsVisible == false || $visibility == SESSION_INVISIBLE) {
  2285. $blockedCourseCount++;
  2286. }
  2287. }
  2288. // If all courses are blocked then no show in the list.
  2289. if ($blockedCourseCount == count($courseList)) {
  2290. $visibility = SESSION_INVISIBLE;
  2291. }
  2292. switch ($visibility) {
  2293. case SESSION_VISIBLE_READ_ONLY:
  2294. case SESSION_VISIBLE:
  2295. case SESSION_AVAILABLE:
  2296. break;
  2297. case SESSION_INVISIBLE:
  2298. continue(2);
  2299. }
  2300. $categories[$row['session_category_id']]['sessions'][$row['id']] = array(
  2301. 'session_name' => $row['name'],
  2302. 'session_id' => $row['id'],
  2303. 'date_start' => $row['date_start'],
  2304. 'date_end' => $row['date_end'],
  2305. 'nb_days_access_before_beginning' => $row['nb_days_access_before_beginning'],
  2306. 'nb_days_access_after_end' => $row['nb_days_access_after_end'],
  2307. 'courses' => $courseList
  2308. );
  2309. }
  2310. }
  2311. return $categories;
  2312. }
  2313. /**
  2314. * Gives a list of [session_id-course_code] => [status] for the current user.
  2315. * @param integer $user_id
  2316. * @return array list of statuses (session_id-course_code => status)
  2317. */
  2318. public static function get_personal_session_course_list($user_id)
  2319. {
  2320. // Database Table Definitions
  2321. $tbl_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  2322. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  2323. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  2324. $tbl_session_user = Database :: get_main_table(TABLE_MAIN_SESSION_USER);
  2325. $tbl_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  2326. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2327. if ($user_id != strval(intval($user_id))) {
  2328. return array();
  2329. }
  2330. // We filter the courses from the URL
  2331. $join_access_url = $where_access_url = '';
  2332. if (api_get_multiple_access_url()) {
  2333. $access_url_id = api_get_current_access_url_id();
  2334. if ($access_url_id != -1) {
  2335. $tbl_url_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  2336. $join_access_url = "LEFT JOIN $tbl_url_course url_rel_course ON url_rel_course.course_code= course.code";
  2337. $where_access_url = " AND access_url_id = $access_url_id ";
  2338. }
  2339. }
  2340. // Courses in which we subscribed out of any session
  2341. $tbl_user_course_category = Database :: get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  2342. $sql = "SELECT
  2343. course.code,
  2344. course_rel_user.status course_rel_status,
  2345. course_rel_user.sort sort,
  2346. course_rel_user.user_course_cat user_course_cat
  2347. FROM ".$tbl_course_user." course_rel_user
  2348. LEFT JOIN ".$tbl_course." course
  2349. ON course.code = course_rel_user.course_code
  2350. LEFT JOIN ".$tbl_user_course_category." user_course_category
  2351. ON course_rel_user.user_course_cat = user_course_category.id
  2352. $join_access_url
  2353. WHERE
  2354. course_rel_user.user_id = '".$user_id."' AND
  2355. course_rel_user.relation_type <> ".COURSE_RELATION_TYPE_RRHH."
  2356. $where_access_url
  2357. ORDER BY user_course_category.sort, course_rel_user.sort, course.title ASC";
  2358. $course_list_sql_result = Database::query($sql);
  2359. $personal_course_list = array();
  2360. if (Database::num_rows($course_list_sql_result) > 0) {
  2361. while ($result_row = Database::fetch_array($course_list_sql_result, 'ASSOC')) {
  2362. $course_info = api_get_course_info($result_row['code']);
  2363. $result_row['course_info'] = $course_info;
  2364. $personal_course_list[] = $result_row;
  2365. }
  2366. }
  2367. $coachCourseConditions = null;
  2368. // Getting sessions that are related to a coach in the session_rel_course_rel_user table
  2369. if (api_is_allowed_to_create_course()) {
  2370. $sessionListFromCourseCoach = array();
  2371. $sql =" SELECT DISTINCT id_session FROM $tbl_session_course_user
  2372. WHERE id_user = $user_id AND status = 2 ";
  2373. if (SessionManager::orderCourseIsEnabled()) {
  2374. //$sql .= "ORDER BY position";
  2375. }
  2376. $result = Database::query($sql);
  2377. if (Database::num_rows($result)) {
  2378. $result = Database::store_result($result);
  2379. foreach ($result as $session) {
  2380. $sessionListFromCourseCoach[]= $session['id_session'];
  2381. }
  2382. }
  2383. if (!empty($sessionListFromCourseCoach)) {
  2384. $condition = implode("','", $sessionListFromCourseCoach);
  2385. $coachCourseConditions = " OR ( id IN ('$condition'))";
  2386. }
  2387. }
  2388. // Get the list of sessions where the user is subscribed
  2389. // This is divided into two different queries
  2390. $sessions = array();
  2391. $sql = "SELECT DISTINCT id, name, date_start, date_end
  2392. FROM $tbl_session_user, $tbl_session
  2393. WHERE (
  2394. id_session = id AND
  2395. id_user = $user_id AND
  2396. relation_type <> ".SESSION_RELATION_TYPE_RRHH."
  2397. )
  2398. $coachCourseConditions
  2399. ORDER BY date_start, date_end, name";
  2400. $result = Database::query($sql);
  2401. if (Database::num_rows($result)>0) {
  2402. while ($row = Database::fetch_assoc($result)) {
  2403. $sessions[$row['id']] = $row;
  2404. }
  2405. }
  2406. $sql = "SELECT DISTINCT id, name, date_start, date_end
  2407. FROM $tbl_session_user, $tbl_session
  2408. WHERE (
  2409. id_coach = $user_id
  2410. )
  2411. $coachCourseConditions
  2412. ORDER BY date_start, date_end, name";
  2413. $result = Database::query($sql);
  2414. if (Database::num_rows($result)>0) {
  2415. while ($row = Database::fetch_assoc($result)) {
  2416. if (empty($sessions[$row['id']])) {
  2417. $sessions[$row['id']] = $row;
  2418. }
  2419. }
  2420. }
  2421. if (api_is_allowed_to_create_course()) {
  2422. foreach ($sessions as $enreg) {
  2423. $session_id = $enreg['id'];
  2424. $session_visibility = api_get_session_visibility($session_id);
  2425. if ($session_visibility == SESSION_INVISIBLE) {
  2426. continue;
  2427. }
  2428. $id_session = $enreg['id'];
  2429. $personal_course_list_sql = "SELECT DISTINCT
  2430. course.code code,
  2431. course.title i,
  2432. ".(api_is_western_name_order() ? "CONCAT(user.firstname,' ',user.lastname)" : "CONCAT(user.lastname,' ',user.firstname)")." t,
  2433. email, course.course_language l,
  2434. 1 sort,
  2435. category_code user_course_cat,
  2436. date_start,
  2437. date_end,
  2438. session.id as id_session,
  2439. session.name as session_name
  2440. FROM $tbl_session_course_user as session_course_user
  2441. INNER JOIN $tbl_course AS course
  2442. ON course.code = session_course_user.course_code
  2443. INNER JOIN $tbl_session as session
  2444. ON session.id = session_course_user.id_session
  2445. LEFT JOIN $tbl_user as user
  2446. ON user.user_id = session_course_user.id_user OR session.id_coach = user.user_id
  2447. WHERE
  2448. session_course_user.id_session = $id_session AND (
  2449. (session_course_user.id_user = $user_id AND session_course_user.status = 2)
  2450. OR session.id_coach = $user_id
  2451. )
  2452. ORDER BY i";
  2453. $course_list_sql_result = Database::query($personal_course_list_sql);
  2454. while ($result_row = Database::fetch_array($course_list_sql_result, 'ASSOC')) {
  2455. $result_row['course_info'] = api_get_course_info($result_row['code']);
  2456. $key = $result_row['id_session'].' - '.$result_row['code'];
  2457. $personal_course_list[$key] = $result_row;
  2458. }
  2459. }
  2460. }
  2461. foreach ($sessions as $enreg) {
  2462. $session_id = $enreg['id'];
  2463. $session_visibility = api_get_session_visibility($session_id);
  2464. if ($session_visibility == SESSION_INVISIBLE) {
  2465. continue;
  2466. }
  2467. /* This query is very similar to the above query,
  2468. but it will check the session_rel_course_user table if there are courses registered to our user or not */
  2469. $personal_course_list_sql = "SELECT DISTINCT
  2470. course.code code,
  2471. course.title i, CONCAT(user.lastname,' ',user.firstname) t,
  2472. email,
  2473. course.course_language l,
  2474. 1 sort,
  2475. category_code user_course_cat,
  2476. date_start,
  2477. date_end,
  2478. session.id as id_session,
  2479. session.name as session_name,
  2480. IF((session_course_user.id_user = 3 AND session_course_user.status=2),'2', '5')
  2481. FROM $tbl_session_course_user as session_course_user
  2482. INNER JOIN $tbl_course AS course
  2483. ON course.code = session_course_user.course_code AND session_course_user.id_session = $session_id
  2484. INNER JOIN $tbl_session as session ON session_course_user.id_session = session.id
  2485. LEFT JOIN $tbl_user as user ON user.user_id = session_course_user.id_user
  2486. WHERE session_course_user.id_user = $user_id
  2487. ORDER BY i";
  2488. $course_list_sql_result = Database::query($personal_course_list_sql);
  2489. while ($result_row = Database::fetch_array($course_list_sql_result, 'ASSOC')) {
  2490. $result_row['course_info'] = api_get_course_info($result_row['code']);
  2491. $key = $result_row['id_session'].' - '.$result_row['code'];
  2492. if (!isset($personal_course_list[$key])) {
  2493. $personal_course_list[$key] = $result_row;
  2494. }
  2495. }
  2496. }
  2497. return $personal_course_list;
  2498. }
  2499. /**
  2500. * Gives a list of courses for the given user in the given session
  2501. * @param integer $user_id
  2502. * @param integer $session_id
  2503. * @return array list of statuses (session_id-course_code => status)
  2504. */
  2505. public static function get_courses_list_by_session($user_id, $session_id)
  2506. {
  2507. // Database Table Definitions
  2508. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  2509. $tableCourse = Database :: get_main_table(TABLE_MAIN_COURSE);
  2510. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2511. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  2512. $user_id = intval($user_id);
  2513. $session_id = intval($session_id);
  2514. //we filter the courses from the URL
  2515. $join_access_url = $where_access_url = '';
  2516. if (api_get_multiple_access_url()) {
  2517. $access_url_id = api_get_current_access_url_id();
  2518. if ($access_url_id != -1) {
  2519. $tbl_url_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  2520. $join_access_url = " , $tbl_url_session url_rel_session ";
  2521. $where_access_url = " AND access_url_id = $access_url_id AND url_rel_session.session_id = $session_id ";
  2522. }
  2523. }
  2524. $personal_course_list = array();
  2525. $courses = array();
  2526. /* This query is very similar to the above query,but it will check the
  2527. session_rel_course_user table if there are courses registered
  2528. to our user or not*/
  2529. $sql = "SELECT DISTINCT
  2530. scu.course_code as code,
  2531. c.visibility,
  2532. c.id as real_id
  2533. FROM $tbl_session_course_user as scu
  2534. INNER JOIN $tbl_session_course sc
  2535. ON (scu.id_session = sc.id_session AND scu.course_code = sc.course_code)
  2536. INNER JOIN $tableCourse as c
  2537. ON (scu.course_code = c.code)
  2538. $join_access_url
  2539. WHERE
  2540. scu.id_user = $user_id AND
  2541. scu.id_session = $session_id
  2542. $where_access_url";
  2543. $orderBy = " ORDER BY code ";
  2544. if (SessionManager::orderCourseIsEnabled()) {
  2545. $orderBy = ' ORDER BY position';
  2546. }
  2547. $sql .= $orderBy;
  2548. $result = Database::query($sql);
  2549. if (Database::num_rows($result) > 0) {
  2550. while ($result_row = Database::fetch_array($result)) {
  2551. $result_row['status'] = 5;
  2552. if (!in_array($result_row['code'], $courses)) {
  2553. $personal_course_list[] = $result_row;
  2554. $courses[] = $result_row['code'];
  2555. }
  2556. }
  2557. }
  2558. if (api_is_allowed_to_create_course()) {
  2559. $sql = "SELECT DISTINCT
  2560. scu.course_code as code, c.visibility, c.id as real_id
  2561. FROM $tbl_session_course_user as scu
  2562. INNER JOIN $tbl_session as s
  2563. ON (scu.id_session = s.id)
  2564. INNER JOIN $tbl_session_course sc
  2565. ON (scu.id_session = sc.id_session AND scu.course_code = sc.course_code)
  2566. INNER JOIN $tableCourse as c
  2567. ON (scu.course_code = c.code)
  2568. $join_access_url
  2569. WHERE
  2570. s.id = $session_id AND
  2571. (
  2572. (scu.id_user=$user_id AND scu.status=2) OR
  2573. s.id_coach = $user_id
  2574. )
  2575. $where_access_url
  2576. $orderBy";
  2577. $result = Database::query($sql);
  2578. if (Database::num_rows($result) > 0) {
  2579. while ($result_row = Database::fetch_array($result)) {
  2580. $result_row['status'] = 2;
  2581. if (!in_array($result_row['code'], $courses)) {
  2582. $personal_course_list[] = $result_row;
  2583. $courses[] = $result_row['code'];
  2584. }
  2585. }
  2586. }
  2587. }
  2588. if (api_is_drh()) {
  2589. $session_list = SessionManager::get_sessions_followed_by_drh($user_id);
  2590. $session_list = array_keys($session_list);
  2591. if (in_array($session_id, $session_list)) {
  2592. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  2593. if (!empty($course_list)) {
  2594. foreach ($course_list as $course) {
  2595. $personal_course_list[] = $course;
  2596. }
  2597. }
  2598. }
  2599. } else {
  2600. //check if user is general coach for this session
  2601. $s = api_get_session_info($session_id);
  2602. if ($s['id_coach'] == $user_id) {
  2603. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  2604. if (!empty($course_list)) {
  2605. foreach ($course_list as $course) {
  2606. if (!in_array($course['code'], $courses)) {
  2607. $personal_course_list[] = $course;
  2608. }
  2609. }
  2610. }
  2611. }
  2612. }
  2613. return $personal_course_list;
  2614. }
  2615. /**
  2616. * Get user id from a username
  2617. * @param string Username
  2618. * @return int User ID (or false if not found)
  2619. */
  2620. public static function get_user_id_from_username($username)
  2621. {
  2622. if (empty($username)) {
  2623. return false;
  2624. }
  2625. $username = trim($username);
  2626. $username = Database::escape_string($username);
  2627. $t_user = Database::get_main_table(TABLE_MAIN_USER);
  2628. $sql = "SELECT user_id FROM $t_user WHERE username = '$username'";
  2629. $res = Database::query($sql);
  2630. if ($res === false) {
  2631. return false;
  2632. }
  2633. if (Database::num_rows($res) !== 1) {
  2634. return false;
  2635. }
  2636. $row = Database::fetch_array($res);
  2637. return $row['user_id'];
  2638. }
  2639. /**
  2640. * Get the users files upload from his share_folder
  2641. * @param string User ID
  2642. * @param string course directory
  2643. * @param string resourcetype: images, all
  2644. * @return int User ID (or false if not found)
  2645. */
  2646. public static function get_user_upload_files_by_course($user_id, $course, $resourcetype = 'all')
  2647. {
  2648. $return = '';
  2649. if (!empty($user_id) && !empty($course)) {
  2650. $user_id = intval($user_id);
  2651. $path = api_get_path(SYS_COURSE_PATH).$course.'/document/shared_folder/sf_user_'.$user_id.'/';
  2652. $web_path = api_get_path(WEB_COURSE_PATH).$course.'/document/shared_folder/sf_user_'.$user_id.'/';
  2653. $file_list = array();
  2654. if (is_dir($path)) {
  2655. $handle = opendir($path);
  2656. while ($file = readdir($handle)) {
  2657. if ($file == '.' || $file == '..' || $file == '.htaccess' || is_dir($path.$file)) {
  2658. continue; // skip current/parent directory and .htaccess
  2659. }
  2660. $file_list[] = $file;
  2661. }
  2662. if (count($file_list) > 0) {
  2663. $return = "<h4>$course</h4>";
  2664. $return .= '<ul class="thumbnails">';
  2665. }
  2666. foreach ($file_list as $file) {
  2667. if ($resourcetype == "all") {
  2668. $return .= '<li><a href="'.$web_path.urlencode($file).'" target="_blank">'.htmlentities($file).'</a></li>';
  2669. } elseif ($resourcetype == "images") {
  2670. //get extension
  2671. $ext = explode('.', $file);
  2672. if ($ext[1] == 'jpg' || $ext[1] == 'jpeg' || $ext[1] == 'png' || $ext[1] == 'gif' || $ext[1] == 'bmp' || $ext[1] == 'tif') {
  2673. $return .= '<li class="span2"><a class="thumbnail" href="'.$web_path.urlencode($file).'" target="_blank">
  2674. <img src="'.$web_path.urlencode($file).'" ></a>
  2675. </li>';
  2676. }
  2677. }
  2678. }
  2679. if (count($file_list) > 0) {
  2680. $return .= '</ul>';
  2681. }
  2682. }
  2683. }
  2684. return $return;
  2685. }
  2686. /**
  2687. * Gets the API key (or keys) and return them into an array
  2688. * @param int Optional user id (defaults to the result of api_get_user_id())
  2689. * @return array Non-indexed array containing the list of API keys for this user, or FALSE on error
  2690. */
  2691. public static function get_api_keys($user_id = null, $api_service = 'dokeos')
  2692. {
  2693. if ($user_id != strval(intval($user_id)))
  2694. return false;
  2695. if (empty($user_id)) {
  2696. $user_id = api_get_user_id();
  2697. }
  2698. if ($user_id === false)
  2699. return false;
  2700. $service_name = Database::escape_string($api_service);
  2701. if (is_string($service_name) === false) {
  2702. return false;
  2703. }
  2704. $t_api = Database::get_main_table(TABLE_MAIN_USER_API_KEY);
  2705. $sql = "SELECT * FROM $t_api WHERE user_id = $user_id AND api_service='$api_service';";
  2706. $res = Database::query($sql);
  2707. if ($res === false)
  2708. return false; //error during query
  2709. $num = Database::num_rows($res);
  2710. if ($num == 0)
  2711. return false;
  2712. $list = array();
  2713. while ($row = Database::fetch_array($res)) {
  2714. $list[$row['id']] = $row['api_key'];
  2715. }
  2716. return $list;
  2717. }
  2718. /**
  2719. * Adds a new API key to the users' account
  2720. * @param int Optional user ID (defaults to the results of api_get_user_id())
  2721. * @return boolean True on success, false on failure
  2722. */
  2723. public static function add_api_key($user_id = null, $api_service = 'dokeos')
  2724. {
  2725. if ($user_id != strval(intval($user_id)))
  2726. return false;
  2727. if (empty($user_id)) {
  2728. $user_id = api_get_user_id();
  2729. }
  2730. if ($user_id === false)
  2731. return false;
  2732. $service_name = Database::escape_string($api_service);
  2733. if (is_string($service_name) === false) {
  2734. return false;
  2735. }
  2736. $t_api = Database::get_main_table(TABLE_MAIN_USER_API_KEY);
  2737. $md5 = md5((time() + ($user_id * 5)) - rand(10000, 10000)); //generate some kind of random key
  2738. $sql = "INSERT INTO $t_api (user_id, api_key,api_service) VALUES ($user_id,'$md5','$service_name')";
  2739. $res = Database::query($sql);
  2740. if ($res === false)
  2741. return false; //error during query
  2742. $num = Database::insert_id();
  2743. return ($num == 0) ? false : $num;
  2744. }
  2745. /**
  2746. * Deletes an API key from the user's account
  2747. * @param int API key's internal ID
  2748. * @return boolean True on success, false on failure
  2749. */
  2750. public static function delete_api_key($key_id)
  2751. {
  2752. if ($key_id != strval(intval($key_id)))
  2753. return false;
  2754. if ($key_id === false)
  2755. return false;
  2756. $t_api = Database::get_main_table(TABLE_MAIN_USER_API_KEY);
  2757. $sql = "SELECT * FROM $t_api WHERE id = ".$key_id;
  2758. $res = Database::query($sql);
  2759. if ($res === false)
  2760. return false; //error during query
  2761. $num = Database::num_rows($res);
  2762. if ($num !== 1)
  2763. return false;
  2764. $sql = "DELETE FROM $t_api WHERE id = ".$key_id;
  2765. $res = Database::query($sql);
  2766. if ($res === false)
  2767. return false; //error during query
  2768. return true;
  2769. }
  2770. /**
  2771. * Regenerate an API key from the user's account
  2772. * @param int user ID (defaults to the results of api_get_user_id())
  2773. * @param string API key's internal ID
  2774. * @return int num
  2775. */
  2776. public static function update_api_key($user_id, $api_service)
  2777. {
  2778. if ($user_id != strval(intval($user_id)))
  2779. return false;
  2780. if ($user_id === false)
  2781. return false;
  2782. $service_name = Database::escape_string($api_service);
  2783. if (is_string($service_name) === false) {
  2784. return false;
  2785. }
  2786. $t_api = Database::get_main_table(TABLE_MAIN_USER_API_KEY);
  2787. $sql = "SELECT id FROM $t_api WHERE user_id=".$user_id." AND api_service='".$api_service."'";
  2788. $res = Database::query($sql);
  2789. $num = Database::num_rows($res);
  2790. if ($num == 1) {
  2791. $id_key = Database::fetch_array($res, 'ASSOC');
  2792. self::delete_api_key($id_key['id']);
  2793. $num = self::add_api_key($user_id, $api_service);
  2794. } elseif ($num == 0) {
  2795. $num = self::add_api_key($user_id);
  2796. }
  2797. return $num;
  2798. }
  2799. /**
  2800. * @param int user ID (defaults to the results of api_get_user_id())
  2801. * @param string API key's internal ID
  2802. * @return int row ID, or return false if not found
  2803. */
  2804. public static function get_api_key_id($user_id, $api_service)
  2805. {
  2806. if ($user_id != strval(intval($user_id)))
  2807. return false;
  2808. if ($user_id === false)
  2809. return false;
  2810. if (empty($api_service))
  2811. return false;
  2812. $t_api = Database::get_main_table(TABLE_MAIN_USER_API_KEY);
  2813. $service_name = Database::escape_string($api_service);
  2814. $sql = "SELECT id FROM $t_api WHERE user_id=".$user_id." AND api_service='".$api_service."'";
  2815. $res = Database::query($sql);
  2816. if (Database::num_rows($res) < 1) {
  2817. return false;
  2818. }
  2819. $row = Database::fetch_array($res, 'ASSOC');
  2820. return $row['id'];
  2821. }
  2822. /**
  2823. * Checks if a user_id is platform admin
  2824. * @param int user ID
  2825. * @return boolean True if is admin, false otherwise
  2826. * @see main_api.lib.php::api_is_platform_admin() for a context-based check
  2827. */
  2828. public static function is_admin($user_id)
  2829. {
  2830. if (empty($user_id) or $user_id != strval(intval($user_id))) {
  2831. return false;
  2832. }
  2833. $admin_table = Database::get_main_table(TABLE_MAIN_ADMIN);
  2834. $sql = "SELECT * FROM $admin_table WHERE user_id = $user_id";
  2835. $res = Database::query($sql);
  2836. return Database::num_rows($res) === 1;
  2837. }
  2838. /**
  2839. * Get the total count of users
  2840. * @param int Status of users to be counted
  2841. * @param int Access URL ID (optional)
  2842. * @return mixed Number of users or false on error
  2843. */
  2844. public static function get_number_of_users($status = 0, $access_url_id = null)
  2845. {
  2846. $t_u = Database::get_main_table(TABLE_MAIN_USER);
  2847. $t_a = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  2848. $sql = "SELECT count(*) FROM $t_u u";
  2849. $sql2 = '';
  2850. if (is_int($status) && $status > 0) {
  2851. $sql2 .= " WHERE u.status = $status ";
  2852. }
  2853. if (!empty($access_url_id) && $access_url_id == intval($access_url_id)) {
  2854. $sql .= ", $t_a a ";
  2855. $sql2 .= " AND a.access_url_id = $access_url_id AND u.user_id = a.user_id ";
  2856. }
  2857. $sql = $sql.$sql2;
  2858. $res = Database::query($sql);
  2859. if (Database::num_rows($res) === 1) {
  2860. return (int) Database::result($res, 0, 0);
  2861. }
  2862. return false;
  2863. }
  2864. /**
  2865. * Resize a picture
  2866. *
  2867. * @param string file picture
  2868. * @param int size in pixels
  2869. * @todo move this function somewhere else image.lib?
  2870. * @return obj image object
  2871. */
  2872. public static function resize_picture($file, $max_size_for_picture)
  2873. {
  2874. $temp = false;
  2875. if (file_exists($file)) {
  2876. $temp = new Image($file);
  2877. $image_size = $temp->get_image_size($file);
  2878. $width = $image_size['width'];
  2879. $height = $image_size['height'];
  2880. if ($width >= $height) {
  2881. if ($width >= $max_size_for_picture) {
  2882. // scale height
  2883. $new_height = round($height * ($max_size_for_picture / $width));
  2884. $temp->resize($max_size_for_picture, $new_height, 0);
  2885. }
  2886. } else { // height > $width
  2887. if ($height >= $max_size_for_picture) {
  2888. // scale width
  2889. $new_width = round($width * ($max_size_for_picture / $height));
  2890. $temp->resize($new_width, $max_size_for_picture, 0);
  2891. }
  2892. }
  2893. }
  2894. return $temp;
  2895. }
  2896. /**
  2897. * Gets the current user image
  2898. * @param string user id
  2899. * @param string picture user name
  2900. * @param string height
  2901. * @param string picture size it can be USER_IMAGE_SIZE_SMALL, USER_IMAGE_SIZE_MEDIUM, USER_IMAGE_SIZE_BIG or USER_IMAGE_SIZE_ORIGINAL
  2902. * @param string style css
  2903. * @return array with the file and the style of an image i.e $array['file'] $array['style']
  2904. */
  2905. public static function get_picture_user($user_id, $picture_file, $height, $size_picture = USER_IMAGE_SIZE_MEDIUM, $style = '')
  2906. {
  2907. $picture = array();
  2908. $picture['style'] = $style;
  2909. if ($picture_file == 'unknown.jpg') {
  2910. switch ($size_picture) {
  2911. case USER_IMAGE_SIZE_ORIGINAL :
  2912. case USER_IMAGE_SIZE_BIG :
  2913. case USER_IMAGE_SIZE_MEDIUM :
  2914. $picture_file = 'unknown.jpg';
  2915. break;
  2916. case USER_IMAGE_SIZE_SMALL:
  2917. $picture_file = 'unknown_22.jpg';
  2918. break;
  2919. }
  2920. $picture['file'] = api_get_path(WEB_CODE_PATH).'img/'.$picture_file;
  2921. return $picture;
  2922. }
  2923. switch ($size_picture) {
  2924. case USER_IMAGE_SIZE_ORIGINAL :
  2925. $size_picture = '';
  2926. break;
  2927. case USER_IMAGE_SIZE_BIG :
  2928. $size_picture = 'big_';
  2929. break;
  2930. case USER_IMAGE_SIZE_MEDIUM :
  2931. $size_picture = 'medium_';
  2932. break;
  2933. case USER_IMAGE_SIZE_SMALL :
  2934. $size_picture = 'small_';
  2935. break;
  2936. default:
  2937. $size_picture = 'medium_';
  2938. }
  2939. $image_array_sys = self::get_user_picture_path_by_id($user_id, 'system', false, true);
  2940. $image_array = self::get_user_picture_path_by_id($user_id, 'web', false, true);
  2941. $file = $image_array_sys['dir'].$size_picture.$picture_file;
  2942. if (file_exists($file)) {
  2943. $picture['file'] = $image_array['dir'].$size_picture.$picture_file;
  2944. $picture['style'] = '';
  2945. if ($height > 0) {
  2946. $dimension = api_getimagesize($picture['file']);
  2947. $margin = (($height - $dimension['width']) / 2);
  2948. //@ todo the padding-top should not be here
  2949. $picture['style'] = ' style="padding-top:'.$margin.'px; width:'.$dimension['width'].'px; height:'.$dimension['height'].'px;" ';
  2950. $picture['original_height'] = $dimension['width'];
  2951. $picture['original_width'] = $dimension['height'];
  2952. }
  2953. } else {
  2954. $file = $image_array_sys['dir'].$picture_file;
  2955. if (file_exists($file) && !is_dir($file)) {
  2956. $picture['file'] = $image_array['dir'].$picture_file;
  2957. } else {
  2958. switch ($size_picture) {
  2959. case 'big_' :
  2960. $picture['file'] = api_get_path(WEB_CODE_PATH).'img/unknown.jpg';
  2961. break;
  2962. case 'medium_' :
  2963. $picture['file'] = api_get_path(WEB_CODE_PATH).'img/unknown_50_50.jpg';
  2964. break;
  2965. case 'small_' :
  2966. $picture['file'] = api_get_path(WEB_CODE_PATH).'img/unknown.jpg';
  2967. break;
  2968. default:
  2969. $picture['file'] = api_get_path(WEB_CODE_PATH).'img/unknown.jpg';
  2970. break;
  2971. }
  2972. }
  2973. }
  2974. return $picture;
  2975. }
  2976. /**
  2977. * @author Isaac flores <isaac.flores@dokeos.com>
  2978. * @param string The email administrator
  2979. * @param integer The user id
  2980. * @param string The message title
  2981. * @param string The content message
  2982. */
  2983. public static function send_message_in_outbox($email_administrator, $user_id, $title, $content)
  2984. {
  2985. $table_message = Database::get_main_table(TABLE_MESSAGE);
  2986. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  2987. $title = api_utf8_decode($title);
  2988. $content = api_utf8_decode($content);
  2989. $email_administrator = Database::escape_string($email_administrator);
  2990. //message in inbox
  2991. $sql_message_outbox = 'SELECT user_id from '.$table_user.' WHERE email="'.$email_administrator.'" ';
  2992. //$num_row_query = Database::num_rows($sql_message_outbox);
  2993. $res_message_outbox = Database::query($sql_message_outbox);
  2994. $array_users_administrator = array();
  2995. while ($row_message_outbox = Database::fetch_array($res_message_outbox, 'ASSOC')) {
  2996. $array_users_administrator[] = $row_message_outbox['user_id'];
  2997. }
  2998. //allow to insert messages in outbox
  2999. for ($i = 0; $i < count($array_users_administrator); $i++) {
  3000. $sql_insert_outbox = "INSERT INTO $table_message(user_sender_id, user_receiver_id, msg_status, send_date, title, content ) ".
  3001. " VALUES (".
  3002. "'".(int) $user_id."', '".(int) ($array_users_administrator[$i])."', '4', '".date('Y-m-d H:i:s')."','".Database::escape_string($title)."','".Database::escape_string($content)."'".
  3003. ")";
  3004. $rs = Database::query($sql_insert_outbox);
  3005. }
  3006. }
  3007. /*
  3008. *
  3009. * USER TAGS
  3010. *
  3011. * Intructions to create a new user tag by Julio Montoya <gugli100@gmail.com>
  3012. *
  3013. * 1. Create a new extra field in main/admin/user_fields.php with the "TAG" field type make it available and visible. Called it "books" for example.
  3014. * 2. Go to profile main/auth/profile.php There you will see a special input (facebook style) that will show suggestions of tags.
  3015. * 3. All the tags are registered in the user_tag table and the relationship between user and tags is in the user_rel_tag table
  3016. * 4. Tags are independent this means that tags can't be shared between tags + book + hobbies.
  3017. * 5. Test and enjoy.
  3018. *
  3019. */
  3020. /**
  3021. * Gets the tags of a specific field_id
  3022. *
  3023. * @param int field_id
  3024. * @param string how we are going to result value in array or in a string (json)
  3025. * @return mixed
  3026. * @since Nov 2009
  3027. * @version 1.8.6.2
  3028. */
  3029. public static function get_tags($tag, $field_id, $return_format = 'json', $limit = 10)
  3030. {
  3031. // database table definition
  3032. $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
  3033. $field_id = intval($field_id);
  3034. $limit = intval($limit);
  3035. $tag = trim(Database::escape_string($tag));
  3036. // all the information of the field
  3037. $sql = "SELECT DISTINCT id, tag from $table_user_tag
  3038. WHERE field_id = $field_id AND tag LIKE '$tag%' ORDER BY tag LIMIT $limit";
  3039. $result = Database::query($sql);
  3040. $return = array();
  3041. if (Database::num_rows($result) > 0) {
  3042. while ($row = Database::fetch_array($result, 'ASSOC')) {
  3043. $return[] = array('caption' => $row['tag'], 'value' => $row['tag']);
  3044. }
  3045. }
  3046. if ($return_format == 'json') {
  3047. $return = json_encode($return);
  3048. }
  3049. return $return;
  3050. }
  3051. /**
  3052. * @param int $field_id
  3053. * @param int $limit
  3054. * @return array
  3055. */
  3056. public static function get_top_tags($field_id, $limit = 100)
  3057. {
  3058. // database table definition
  3059. $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
  3060. $table_user_tag_values = Database::get_main_table(TABLE_MAIN_USER_REL_TAG);
  3061. $field_id = intval($field_id);
  3062. $limit = intval($limit);
  3063. // all the information of the field
  3064. $sql = "SELECT count(*) count, tag FROM $table_user_tag_values uv INNER JOIN $table_user_tag ut ON(ut.id = uv.tag_id)
  3065. WHERE field_id = $field_id GROUP BY tag_id ORDER BY count DESC LIMIT $limit";
  3066. $result = Database::query($sql);
  3067. $return = array();
  3068. if (Database::num_rows($result) > 0) {
  3069. while ($row = Database::fetch_array($result, 'ASSOC')) {
  3070. $return[] = $row;
  3071. }
  3072. }
  3073. return $return;
  3074. }
  3075. /**
  3076. * Get user's tags
  3077. * @param int field_id
  3078. * @param int user_id
  3079. * @return array
  3080. */
  3081. public static function get_user_tags($user_id, $field_id)
  3082. {
  3083. // database table definition
  3084. $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
  3085. $table_user_tag_values = Database::get_main_table(TABLE_MAIN_USER_REL_TAG);
  3086. $field_id = intval($field_id);
  3087. $user_id = intval($user_id);
  3088. // all the information of the field
  3089. $sql = "SELECT ut.id, tag,count FROM $table_user_tag ut INNER JOIN $table_user_tag_values uv ON (uv.tag_id=ut.ID)
  3090. WHERE field_id = $field_id AND user_id = $user_id ORDER BY tag";
  3091. $result = Database::query($sql);
  3092. $return = array();
  3093. if (Database::num_rows($result) > 0) {
  3094. while ($row = Database::fetch_array($result, 'ASSOC')) {
  3095. $return[$row['id']] = array('tag' => $row['tag'], 'count' => $row['count']);
  3096. }
  3097. }
  3098. return $return;
  3099. }
  3100. /**
  3101. * Get user's tags
  3102. * @param int user_id
  3103. * @param int field_id
  3104. * @param bool show links or not
  3105. * @return array
  3106. */
  3107. public static function get_user_tags_to_string($user_id, $field_id, $show_links = true)
  3108. {
  3109. // database table definition
  3110. $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
  3111. $table_user_tag_values = Database::get_main_table(TABLE_MAIN_USER_REL_TAG);
  3112. $field_id = intval($field_id);
  3113. $user_id = intval($user_id);
  3114. // all the information of the field
  3115. $sql = "SELECT ut.id, tag,count FROM $table_user_tag ut INNER JOIN $table_user_tag_values uv ON (uv.tag_id=ut.ID)
  3116. WHERE field_id = $field_id AND user_id = $user_id ORDER BY tag";
  3117. $result = Database::query($sql);
  3118. $return = array();
  3119. if (Database::num_rows($result) > 0) {
  3120. while ($row = Database::fetch_array($result, 'ASSOC')) {
  3121. $return[$row['id']] = array('tag' => $row['tag'], 'count' => $row['count']);
  3122. }
  3123. }
  3124. $user_tags = $return;
  3125. $tag_tmp = array();
  3126. foreach ($user_tags as $tag) {
  3127. if ($show_links) {
  3128. $tag_tmp[] = '<a href="'.api_get_path(WEB_PATH).'main/search/index.php?q='.$tag['tag'].'">'.$tag['tag'].'</a>';
  3129. } else {
  3130. $tag_tmp[] = $tag['tag'];
  3131. }
  3132. }
  3133. if (is_array($user_tags) && count($user_tags) > 0) {
  3134. $return = implode(', ', $tag_tmp);
  3135. } else {
  3136. return '';
  3137. }
  3138. return $return;
  3139. }
  3140. /**
  3141. * Get the tag id
  3142. * @param int tag
  3143. * @param int field_id
  3144. * @return int returns 0 if fails otherwise the tag id
  3145. */
  3146. public static function get_tag_id($tag, $field_id)
  3147. {
  3148. $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
  3149. $tag = Database::escape_string($tag);
  3150. $field_id = intval($field_id);
  3151. //with COLLATE latin1_bin to select query in a case sensitive mode
  3152. $sql = "SELECT id FROM $table_user_tag WHERE tag LIKE '$tag' AND field_id = $field_id";
  3153. $result = Database::query($sql);
  3154. if (Database::num_rows($result) > 0) {
  3155. $row = Database::fetch_array($result, 'ASSOC');
  3156. return $row['id'];
  3157. } else {
  3158. return 0;
  3159. }
  3160. }
  3161. /**
  3162. * Get the tag id
  3163. * @param int tag
  3164. * @param int field_id
  3165. * @return int 0 if fails otherwise the tag id
  3166. */
  3167. public static function get_tag_id_from_id($tag_id, $field_id)
  3168. {
  3169. $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
  3170. $tag_id = intval($tag_id);
  3171. $field_id = intval($field_id);
  3172. $sql = "SELECT id FROM $table_user_tag WHERE id = '$tag_id' AND field_id = $field_id";
  3173. $result = Database::query($sql);
  3174. if (Database::num_rows($result) > 0) {
  3175. $row = Database::fetch_array($result, 'ASSOC');
  3176. return $row['id'];
  3177. } else {
  3178. return false;
  3179. }
  3180. }
  3181. /**
  3182. * Adds a user-tag value
  3183. * @param mixed tag
  3184. * @param int The user id
  3185. * @param int field id of the tag
  3186. * @return bool
  3187. */
  3188. public static function add_tag($tag, $user_id, $field_id)
  3189. {
  3190. // database table definition
  3191. $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
  3192. $table_user_tag_values = Database::get_main_table(TABLE_MAIN_USER_REL_TAG);
  3193. $tag = trim(Database::escape_string($tag));
  3194. $user_id = intval($user_id);
  3195. $field_id = intval($field_id);
  3196. $tag_id = UserManager::get_tag_id($tag, $field_id);
  3197. /* IMPORTANT
  3198. * @todo we don't create tags with numbers
  3199. *
  3200. */
  3201. if (is_numeric($tag)) {
  3202. //the form is sending an id this means that the user select it from the list so it MUST exists
  3203. /* $new_tag_id = UserManager::get_tag_id_from_id($tag,$field_id);
  3204. if ($new_tag_id !== false) {
  3205. $sql = "UPDATE $table_user_tag SET count = count + 1 WHERE id = $new_tag_id";
  3206. $result = Database::query($sql);
  3207. $last_insert_id = $new_tag_id;
  3208. } else {
  3209. $sql = "INSERT INTO $table_user_tag (tag, field_id,count) VALUES ('$tag','$field_id', count + 1)";
  3210. $result = Database::query($sql);
  3211. $last_insert_id = Database::get_last_insert_id();
  3212. } */
  3213. } else {
  3214. }
  3215. //this is a new tag
  3216. if ($tag_id == 0) {
  3217. //the tag doesn't exist
  3218. $sql = "INSERT INTO $table_user_tag (tag, field_id,count) VALUES ('$tag','$field_id', count + 1)";
  3219. Database::query($sql);
  3220. $last_insert_id = Database::insert_id();
  3221. } else {
  3222. //the tag exists we update it
  3223. $sql = "UPDATE $table_user_tag SET count = count + 1 WHERE id = $tag_id";
  3224. Database::query($sql);
  3225. $last_insert_id = $tag_id;
  3226. }
  3227. if (!empty($last_insert_id) && ($last_insert_id != 0)) {
  3228. //we insert the relationship user-tag
  3229. $sql_select = "SELECT tag_id FROM $table_user_tag_values WHERE user_id = $user_id AND tag_id = $last_insert_id ";
  3230. $result = Database::query($sql_select);
  3231. //if the relationship does not exist we create it
  3232. if (Database::num_rows($result) == 0) {
  3233. $sql = "INSERT INTO $table_user_tag_values SET user_id = $user_id, tag_id = $last_insert_id";
  3234. Database::query($sql);
  3235. }
  3236. }
  3237. }
  3238. /**
  3239. * Deletes an user tag
  3240. * @param int user id
  3241. * @param int field id
  3242. *
  3243. */
  3244. public static function delete_user_tags($user_id, $field_id)
  3245. {
  3246. // database table definition
  3247. $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
  3248. $table_user_tag_values = Database::get_main_table(TABLE_MAIN_USER_REL_TAG);
  3249. $tags = UserManager::get_user_tags($user_id, $field_id);
  3250. if (is_array($tags) && count($tags) > 0) {
  3251. foreach ($tags as $key => $tag) {
  3252. if ($tag['count'] > '0') {
  3253. $sql = "UPDATE $table_user_tag SET count = count - 1 WHERE id = $key ";
  3254. Database::query($sql);
  3255. }
  3256. $sql = "DELETE FROM $table_user_tag_values WHERE user_id = $user_id AND tag_id = $key";
  3257. Database::query($sql);
  3258. }
  3259. }
  3260. }
  3261. /**
  3262. * Process the tag list comes from the UserManager::update_extra_field_value() function
  3263. * @param array the tag list that will be added
  3264. * @param int user id
  3265. * @param int field id
  3266. * @return bool
  3267. */
  3268. public static function process_tags($tags, $user_id, $field_id)
  3269. {
  3270. //We loop the tags and add it to the DB
  3271. if (is_array($tags)) {
  3272. foreach ($tags as $tag) {
  3273. UserManager::add_tag($tag, $user_id, $field_id);
  3274. }
  3275. } else {
  3276. UserManager::add_tag($tags, $user_id, $field_id);
  3277. }
  3278. return true;
  3279. }
  3280. /**
  3281. * Returns a list of all administrators
  3282. * @author jmontoya
  3283. * @return array
  3284. */
  3285. public static function get_all_administrators()
  3286. {
  3287. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  3288. $table_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  3289. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  3290. $access_url_id = api_get_current_access_url_id();
  3291. if (api_get_multiple_access_url()) {
  3292. $sql = "SELECT admin.user_id, username, firstname, lastname, email
  3293. FROM $tbl_url_rel_user as url
  3294. INNER JOIN $table_admin as admin
  3295. ON (admin.user_id=url.user_id)
  3296. INNER JOIN $table_user u
  3297. ON (u.user_id=admin.user_id)
  3298. WHERE access_url_id ='".$access_url_id."'";
  3299. } else {
  3300. $sql = "SELECT admin.user_id, username, firstname, lastname, email
  3301. FROM $table_admin as admin
  3302. INNER JOIN $table_user u
  3303. ON (u.user_id=admin.user_id)";
  3304. }
  3305. $result = Database::query($sql);
  3306. $return = array();
  3307. if (Database::num_rows($result) > 0) {
  3308. while ($row = Database::fetch_array($result, 'ASSOC')) {
  3309. $return[$row['user_id']] = $row;
  3310. }
  3311. }
  3312. return $return;
  3313. }
  3314. /**
  3315. * Search an user (tags, first name, last name and email )
  3316. * @param string $tag
  3317. * @param int $field_id field id of the tag
  3318. * @param int $from where to start in the query
  3319. * @param int $number_of_items
  3320. * @param bool $getCount get count or not
  3321. * @return array
  3322. */
  3323. public static function get_all_user_tags(
  3324. $tag,
  3325. $field_id = 0,
  3326. $from = 0,
  3327. $number_of_items = 10,
  3328. $getCount = false
  3329. ) {
  3330. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  3331. $table_user_tag = Database::get_main_table(TABLE_MAIN_TAG);
  3332. $table_user_tag_values = Database::get_main_table(TABLE_MAIN_USER_REL_TAG);
  3333. $access_url_rel_user_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  3334. $field_id = intval($field_id);
  3335. $from = intval($from);
  3336. $number_of_items = intval($number_of_items);
  3337. $where_field = "";
  3338. $where_extra_fields = UserManager::get_search_form_where_extra_fields();
  3339. if ($field_id != 0) {
  3340. $where_field = " field_id = $field_id AND ";
  3341. }
  3342. // all the information of the field
  3343. if ($getCount) {
  3344. $select = "SELECT count(DISTINCT u.user_id) count";
  3345. } else {
  3346. $select = "SELECT DISTINCT u.user_id, u.username, firstname, lastname, email, tag, picture_uri";
  3347. }
  3348. $sql = " $select
  3349. FROM $user_table u
  3350. INNER JOIN $access_url_rel_user_table url_rel_user ON (u.user_id = url_rel_user.user_id)
  3351. LEFT JOIN $table_user_tag_values uv ON (u.user_id AND uv.user_id AND uv.user_id = url_rel_user.user_id)
  3352. LEFT JOIN $table_user_tag ut ON (uv.tag_id = ut.id)
  3353. WHERE
  3354. ($where_field tag LIKE '".Database::escape_string($tag."%")."') OR
  3355. (
  3356. u.firstname LIKE '".Database::escape_string("%".$tag."%")."' OR
  3357. u.lastname LIKE '".Database::escape_string("%".$tag."%")."' OR
  3358. u.username LIKE '".Database::escape_string("%".$tag."%")."' OR
  3359. concat(u.firstname, ' ', u.lastname) LIKE '".Database::escape_string("%".$tag."%")."' OR
  3360. concat(u.lastname, ' ', u.firstname) LIKE '".Database::escape_string("%".$tag."%")."'
  3361. )
  3362. ".(!empty($where_extra_fields) ? $where_extra_fields : '')."
  3363. AND
  3364. url_rel_user.access_url_id=".api_get_current_access_url_id();
  3365. $keyword_active = true;
  3366. // only active users
  3367. if ($keyword_active) {
  3368. $sql .= " AND u.active='1'";
  3369. }
  3370. // avoid anonymous
  3371. $sql .= " AND u.status <> 6 ";
  3372. $sql .= " ORDER BY username";
  3373. $sql .= " LIMIT $from , $number_of_items";
  3374. $result = Database::query($sql);
  3375. $return = array();
  3376. if (Database::num_rows($result) > 0) {
  3377. if ($getCount) {
  3378. $row = Database::fetch_array($result, 'ASSOC');
  3379. return $row['count'];
  3380. }
  3381. while ($row = Database::fetch_array($result, 'ASSOC')) {
  3382. if (isset($return[$row['user_id']]) &&
  3383. !empty($return[$row['user_id']]['tag'])
  3384. ) {
  3385. $url = Display::url(
  3386. $row['tag'],
  3387. api_get_path(WEB_PATH).'main/social/search.php?q='.$row['tag'],
  3388. array('class' => 'tag')
  3389. );
  3390. $row['tag'] = $url;
  3391. }
  3392. $return[$row['user_id']] = $row;
  3393. }
  3394. }
  3395. return $return;
  3396. }
  3397. /**
  3398. * Get extra filtrable user fields (type select)
  3399. * @return array
  3400. */
  3401. public static function get_extra_filtrable_fields()
  3402. {
  3403. $extraFieldList = UserManager::get_extra_fields();
  3404. $extraFiltrableFields = array();
  3405. if (is_array($extraFieldList)) {
  3406. foreach ($extraFieldList as $extraField) {
  3407. //if is enabled to filter and is a "<select>" field type
  3408. if ($extraField[8] == 1 && $extraField[2] == 4) {
  3409. $extraFiltrableFields[] = array(
  3410. 'name' => $extraField[3],
  3411. 'variable' => $extraField[1],
  3412. 'data' => $extraField[9]
  3413. );
  3414. }
  3415. }
  3416. }
  3417. if (is_array($extraFiltrableFields) && count($extraFiltrableFields) > 0 ) {
  3418. return $extraFiltrableFields;
  3419. }
  3420. }
  3421. /**
  3422. * Get extra where clauses for finding users based on extra filtrable user fields (type select)
  3423. * @return string With AND clauses based on user's ID which have the values to search in extra user fields
  3424. */
  3425. public static function get_search_form_where_extra_fields()
  3426. {
  3427. $useExtraFields = false;
  3428. $extraFields = UserManager::get_extra_filtrable_fields();
  3429. $extraFieldResult = array();
  3430. if (is_array($extraFields) && count($extraFields)>0 ) {
  3431. foreach ($extraFields as $extraField) {
  3432. $varName = 'field_'.$extraField['variable'];
  3433. if (UserManager::is_extra_field_available($extraField['variable'])) {
  3434. if (isset($_GET[$varName]) && $_GET[$varName]!='0') {
  3435. $useExtraFields = true;
  3436. $extraFieldResult[]= UserManager::get_extra_user_data_by_value($extraField['variable'], $_GET[$varName]);
  3437. }
  3438. }
  3439. }
  3440. }
  3441. if ($useExtraFields) {
  3442. $finalResult = array();
  3443. if (count($extraFieldResult)>1) {
  3444. for ($i=0; $i < count($extraFieldResult) -1; $i++) {
  3445. if (is_array($extraFieldResult[$i+1])) {
  3446. $finalResult = array_intersect($extraFieldResult[$i], $extraFieldResult[$i+1]);
  3447. }
  3448. }
  3449. } else {
  3450. $finalResult = $extraFieldResult[0];
  3451. }
  3452. if (is_array($finalResult) && count($finalResult)>0) {
  3453. $whereFilter = " AND u.user_id IN ('".implode("','", $finalResult)."') ";
  3454. } else {
  3455. //no results
  3456. $whereFilter = " AND u.user_id = -1 ";
  3457. }
  3458. return $whereFilter;
  3459. }
  3460. }
  3461. /**
  3462. * Show the search form
  3463. * @param string $query the value of the search box
  3464. * @return string HTML form
  3465. */
  3466. public static function get_search_form($query)
  3467. {
  3468. $extraFiltrableFields = UserManager::get_extra_filtrable_fields();
  3469. $extraFields = null;
  3470. if (is_array($extraFiltrableFields) && count($extraFiltrableFields)>0 ) {
  3471. foreach ($extraFiltrableFields as $extraField) {
  3472. $extraFields .= '<label class="extra_field">'.$extraField['name'].'</label>';
  3473. $varName = 'field_'.$extraField['variable'];
  3474. $extraFields .= '&nbsp;<select name="'.$varName.'" class="extra_field">';
  3475. $extraFields .= '<option value="0">--'.get_lang('Select').'--</option>';
  3476. foreach ($extraField['data'] as $option) {
  3477. $checked='';
  3478. if (isset($_GET[$varName])) {
  3479. if ($_GET[$varName]==$option[1]) {
  3480. $checked = 'selected="true"';
  3481. }
  3482. }
  3483. $extraFields .= '<option value="'.$option[1].'" '.$checked.'>'.$option[1].'</option>';
  3484. }
  3485. $extraFields .= '</select>';
  3486. $extraFields .= '&nbsp;&nbsp;';
  3487. }
  3488. }
  3489. $searchType = isset($_GET['search_type']) ? $_GET['search_type'] : null;
  3490. return '
  3491. <form method="GET" class="well form-search" action="'.api_get_path(WEB_PATH).'main/social/search.php">
  3492. <input placeholder="'.get_lang('UsersGroups').'" type="text" class="input-small" value="'.api_htmlentities(Security::remove_XSS($query)).'" name="q"/> &nbsp;
  3493. ' . get_lang('Type') .'
  3494. <select name="search_type" onchange="javascript: extra_field_toogle();">
  3495. <option value="0">--'.get_lang('Select').'--</option>
  3496. <option value="1"' . (($searchType=='1')?'selected="selected"':"") . '>--' . get_lang('User') .'--</option>
  3497. <option value="2"' . (($searchType=='2')?'selected="selected"':"") . '>--' . get_lang('Group') . '--</option>
  3498. </select>
  3499. '.$extraFields.'
  3500. <button class="btn" type="submit" value="search">'.get_lang('Search').'</button>
  3501. </form>
  3502. <script>
  3503. extra_field_toogle();
  3504. function extra_field_toogle()
  3505. {
  3506. if (jQuery("select[name=search_type]").val() != "1") { jQuery(".extra_field").hide(); } else { jQuery(".extra_field").show(); }
  3507. }
  3508. </script>
  3509. ';
  3510. }
  3511. /**
  3512. * Shows the user menu
  3513. */
  3514. public static function show_menu()
  3515. {
  3516. echo '<div class="actions">';
  3517. echo '<a href="/main/auth/profile.php">'.Display::return_icon('profile.png').' '.get_lang('PersonalData').'</a>';
  3518. echo '<a href="/main/messages/inbox.php">'.Display::return_icon('inbox.png').' '.get_lang('Inbox').'</a>';
  3519. echo '<a href="/main/messages/outbox.php">'.Display::return_icon('outbox.png').' '.get_lang('Outbox').'</a>';
  3520. echo '<span style="float:right; padding-top:7px;">'.
  3521. '<a href="/main/auth/profile.php?show=1">'.Display::return_icon('edit.gif').' '.get_lang('Configuration').'</a>';
  3522. '</span>';
  3523. echo '</div>';
  3524. }
  3525. /**
  3526. * Gives a list of course auto-register (field special_course)
  3527. * @return array list of course
  3528. * @author Jhon Hinojosa <jhon.hinojosa@dokeos.com>
  3529. * @deprecated this function is never use in chamilo, use CourseManager::get_special_course_list
  3530. * @since v1.8.6.2
  3531. */
  3532. public static function get_special_course_list()
  3533. {
  3534. // Database Table Definitions
  3535. $tbl_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  3536. $tbl_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  3537. $tbl_course_field = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD);
  3538. $tbl_course_field_value = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  3539. $tbl_user_course_category = Database :: get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  3540. //we filter the courses from the URL
  3541. $join_access_url = $where_access_url = '';
  3542. if (api_get_multiple_access_url()) {
  3543. $access_url_id = api_get_current_access_url_id();
  3544. $tbl_url_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  3545. $join_access_url = "LEFT JOIN $tbl_url_course url_rel_course ON url_rel_course.course_code= course.code";
  3546. $where_access_url = " AND access_url_id = $access_url_id ";
  3547. }
  3548. // Filter special courses
  3549. $sql_special_course = "SELECT course_code FROM $tbl_course_field_value tcfv INNER JOIN $tbl_course_field tcf ON ".
  3550. " tcfv.field_id = tcf.id WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 ";
  3551. $special_course_result = Database::query($sql_special_course);
  3552. $code_special_courses = '';
  3553. if (Database::num_rows($special_course_result) > 0) {
  3554. $special_course_list = array();
  3555. while ($result_row = Database::fetch_array($special_course_result)) {
  3556. $special_course_list[] = '"'.$result_row['course_code'].'"';
  3557. }
  3558. $code_special_courses = ' course.code IN ('.join($special_course_list, ',').') ';
  3559. }
  3560. $course_list = array();
  3561. if (!empty($code_special_courses)) {
  3562. $course_list_sql = "SELECT course.code k, course.directory d, course.visual_code c, course.db_name db, course.title i, course.tutor_name t, course.course_language l, course_rel_user.status s, course_rel_user.sort sort, course_rel_user.user_course_cat user_course_cat
  3563. FROM ".$tbl_course_user." course_rel_user
  3564. LEFT JOIN ".$tbl_course." course
  3565. ON course.code = course_rel_user.course_code
  3566. LEFT JOIN ".$tbl_user_course_category." user_course_category
  3567. ON course_rel_user.user_course_cat = user_course_category.id
  3568. $join_access_url
  3569. WHERE $code_special_courses $where_access_url
  3570. GROUP BY course.code
  3571. ORDER BY user_course_category.sort,course.title,course_rel_user.sort ASC";
  3572. $course_list_sql_result = Database::query($course_list_sql);
  3573. while ($result_row = Database::fetch_array($course_list_sql_result)) {
  3574. $course_list[] = $result_row;
  3575. }
  3576. }
  3577. return $course_list;
  3578. }
  3579. /**
  3580. * Allow to register contact to social network
  3581. * @param int $friend_id user friend id
  3582. * @param int $my_user_id user id
  3583. * @param int $relation_type relation between users see constants definition
  3584. */
  3585. public static function relate_users($friend_id, $my_user_id, $relation_type)
  3586. {
  3587. $tbl_my_friend = Database :: get_main_table(TABLE_MAIN_USER_REL_USER);
  3588. $friend_id = intval($friend_id);
  3589. $my_user_id = intval($my_user_id);
  3590. $relation_type = intval($relation_type);
  3591. $sql = 'SELECT COUNT(*) as count FROM '.$tbl_my_friend.' WHERE friend_user_id='.$friend_id.' AND user_id='.$my_user_id.' AND relation_type <> '.USER_RELATION_TYPE_RRHH.' ';
  3592. $result = Database::query($sql);
  3593. $row = Database :: fetch_array($result, 'ASSOC');
  3594. $current_date = date('Y-m-d H:i:s');
  3595. if ($row['count'] == 0) {
  3596. $sql_i = 'INSERT INTO '.$tbl_my_friend.'(friend_user_id,user_id,relation_type,last_edit)values('.$friend_id.','.$my_user_id.','.$relation_type.',"'.$current_date.'");';
  3597. Database::query($sql_i);
  3598. return true;
  3599. } else {
  3600. $sql = 'SELECT COUNT(*) as count, relation_type FROM '.$tbl_my_friend.' WHERE friend_user_id='.$friend_id.' AND user_id='.$my_user_id.' AND relation_type <> '.USER_RELATION_TYPE_RRHH.' ';
  3601. $result = Database::query($sql);
  3602. $row = Database :: fetch_array($result, 'ASSOC');
  3603. if ($row['count'] == 1) {
  3604. //only for the case of a RRHH
  3605. if ($row['relation_type'] != $relation_type && $relation_type == USER_RELATION_TYPE_RRHH) {
  3606. $sql_i = 'INSERT INTO '.$tbl_my_friend.'(friend_user_id,user_id,relation_type,last_edit)values('.$friend_id.','.$my_user_id.','.$relation_type.',"'.$current_date.'");';
  3607. } else {
  3608. $sql_i = 'UPDATE '.$tbl_my_friend.' SET relation_type='.$relation_type.' WHERE friend_user_id='.$friend_id.' AND user_id='.$my_user_id;
  3609. }
  3610. Database::query($sql_i);
  3611. return true;
  3612. } else {
  3613. return false;
  3614. }
  3615. }
  3616. }
  3617. /**
  3618. * Deletes a contact
  3619. * @param int user friend id
  3620. * @param bool true will delete ALL friends relationship from $friend_id
  3621. * @author isaac flores paz <isaac.flores@dokeos.com>
  3622. * @author Julio Montoya <gugli100@gmail.com> Cleaning code
  3623. */
  3624. public static function remove_user_rel_user($friend_id, $real_removed = false, $with_status_condition = '')
  3625. {
  3626. $tbl_my_friend = Database :: get_main_table(TABLE_MAIN_USER_REL_USER);
  3627. $tbl_my_message = Database :: get_main_table(TABLE_MAIN_MESSAGE);
  3628. $friend_id = intval($friend_id);
  3629. if ($real_removed) {
  3630. //Delete user friend
  3631. /*
  3632. $sql_delete_relationship1 = 'UPDATE ' . $tbl_my_friend .' SET relation_type='.USER_RELATION_TYPE_DELETED.' WHERE friend_user_id='.$friend_id;
  3633. $sql_delete_relationship2 = 'UPDATE ' . $tbl_my_friend . ' SET relation_type='.USER_RELATION_TYPE_DELETED.' WHERE user_id=' . $friend_id;
  3634. Database::query($sql_delete_relationship1);
  3635. Database::query($sql_delete_relationship2); */
  3636. $extra_condition = '';
  3637. if ($with_status_condition != '') {
  3638. $extra_condition = ' AND relation_type = '.intval($with_status_condition);
  3639. }
  3640. $sql_delete_relationship1 = 'DELETE FROM '.$tbl_my_friend.' WHERE relation_type <> '.USER_RELATION_TYPE_RRHH.' AND friend_user_id='.$friend_id.' '.$extra_condition;
  3641. $sql_delete_relationship2 = 'DELETE FROM '.$tbl_my_friend.' WHERE relation_type <> '.USER_RELATION_TYPE_RRHH.' AND user_id='.$friend_id.' '.$extra_condition;
  3642. Database::query($sql_delete_relationship1);
  3643. Database::query($sql_delete_relationship2);
  3644. } else {
  3645. $user_id = api_get_user_id();
  3646. $sql = 'SELECT COUNT(*) as count FROM '.$tbl_my_friend.' WHERE user_id='.$user_id.' AND relation_type NOT IN('.USER_RELATION_TYPE_DELETED.', '.USER_RELATION_TYPE_RRHH.') AND friend_user_id='.$friend_id;
  3647. $result = Database::query($sql);
  3648. $row = Database :: fetch_array($result, 'ASSOC');
  3649. if ($row['count'] == 1) {
  3650. //Delete user rel user
  3651. $sql_i = 'UPDATE '.$tbl_my_friend.' SET relation_type='.USER_RELATION_TYPE_DELETED.' WHERE user_id='.$user_id.' AND friend_user_id='.$friend_id;
  3652. $sql_j = 'UPDATE '.$tbl_my_message.' SET msg_status='.MESSAGE_STATUS_INVITATION_DENIED.' WHERE user_receiver_id='.$user_id.' AND user_sender_id='.$friend_id.' AND update_date="0000-00-00 00:00:00" ';
  3653. //Delete user
  3654. $sql_ij = 'UPDATE '.$tbl_my_friend.' SET relation_type='.USER_RELATION_TYPE_DELETED.' WHERE user_id='.$friend_id.' AND friend_user_id='.$user_id;
  3655. $sql_ji = 'UPDATE '.$tbl_my_message.' SET msg_status='.MESSAGE_STATUS_INVITATION_DENIED.' WHERE user_receiver_id='.$friend_id.' AND user_sender_id='.$user_id.' AND update_date="0000-00-00 00:00:00" ';
  3656. Database::query($sql_i);
  3657. Database::query($sql_j);
  3658. Database::query($sql_ij);
  3659. Database::query($sql_ji);
  3660. }
  3661. }
  3662. }
  3663. /**
  3664. * get users followed by human resource manager
  3665. * @param int $userId
  3666. * @param int $userStatus (STUDENT, COURSEMANAGER, etc)
  3667. * @param bool $getOnlyUserId
  3668. * @param bool $getSql
  3669. * @param bool $getCount
  3670. * @param int $from
  3671. * @param int $numberItems
  3672. * @param int $column
  3673. * @param string $direction
  3674. * @param int $active
  3675. * @param string $lastConnectionDate
  3676. * @return array users
  3677. */
  3678. public static function get_users_followed_by_drh(
  3679. $userId,
  3680. $userStatus = 0,
  3681. $getOnlyUserId = false,
  3682. $getSql = false,
  3683. $getCount = false,
  3684. $from = null,
  3685. $numberItems = null,
  3686. $column = null,
  3687. $direction = null,
  3688. $active = null,
  3689. $lastConnectionDate = null
  3690. ) {
  3691. return self::getUsersFollowedByUser(
  3692. $userId,
  3693. $userStatus,
  3694. $getOnlyUserId,
  3695. $getSql,
  3696. $getCount,
  3697. $from,
  3698. $numberItems,
  3699. $column,
  3700. $direction,
  3701. $active,
  3702. $lastConnectionDate,
  3703. DRH
  3704. );
  3705. }
  3706. /**
  3707. * Get users followed by human resource manager
  3708. * @param int $userId
  3709. * @param int $userStatus Filter users by status (STUDENT, COURSEMANAGER, etc)
  3710. * @param bool $getOnlyUserId
  3711. * @param bool $getSql
  3712. * @param bool $getCount
  3713. * @param int $from
  3714. * @param int $numberItems
  3715. * @param int $column
  3716. * @param string $direction
  3717. * @param int $active
  3718. * @param string $lastConnectionDate
  3719. * @param int $status the function is called by who? COURSEMANAGER, DRH?
  3720. * @param string $keyword
  3721. *
  3722. * @return array user list
  3723. */
  3724. public static function getUsersFollowedByUser(
  3725. $userId,
  3726. $userStatus = null,
  3727. $getOnlyUserId = false,
  3728. $getSql = false,
  3729. $getCount = false,
  3730. $from = null,
  3731. $numberItems = null,
  3732. $column = null,
  3733. $direction = null,
  3734. $active = null,
  3735. $lastConnectionDate = null,
  3736. $status = null,
  3737. $keyword = null
  3738. ) {
  3739. // Database Table Definitions
  3740. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  3741. $tbl_user_rel_user = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  3742. $tbl_user_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  3743. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  3744. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  3745. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  3746. $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  3747. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  3748. $userId = intval($userId);
  3749. $limitCondition = null;
  3750. if (isset($from) && isset($numberItems)) {
  3751. $from = intval($from);
  3752. $numberItems = intval($numberItems);
  3753. $limitCondition = "LIMIT $from, $numberItems";
  3754. }
  3755. $column = Database::escape_string($column);
  3756. $direction = in_array(strtolower($direction), array('asc', 'desc')) ? $direction : null;
  3757. $userConditions = '';
  3758. if (!empty($userStatus)) {
  3759. $userConditions .= ' AND u.status = '.intval($userStatus);
  3760. }
  3761. $select = " SELECT DISTINCT u.user_id, u.username, u.lastname, u.firstname, u.email ";
  3762. if ($getOnlyUserId) {
  3763. $select = " SELECT DISTINCT u.user_id";
  3764. }
  3765. $masterSelect = "SELECT DISTINCT * FROM ";
  3766. if ($getCount) {
  3767. $masterSelect = "SELECT COUNT(DISTINCT(user_id)) as count FROM ";
  3768. $select = " SELECT DISTINCT(u.user_id) ";
  3769. }
  3770. if (!is_null($active)) {
  3771. $active = intval($active);
  3772. $userConditions .= " AND u.active = $active ";
  3773. }
  3774. if (!empty($keyword)) {
  3775. $keyword = Database::escape_string($keyword);
  3776. $userConditions .= " AND (
  3777. u.username LIKE '%$keyword%' OR
  3778. u.firstname LIKE '%$keyword%' OR
  3779. u.lastname LIKE '%$keyword%' OR
  3780. u.official_code LIKE '%$keyword%' OR
  3781. u.email LIKE '%$keyword%'
  3782. )";
  3783. }
  3784. if (!empty($lastConnectionDate)) {
  3785. if (isset($_configuration['save_user_last_login']) &&
  3786. $_configuration['save_user_last_login']
  3787. ) {
  3788. $lastConnectionDate = Database::escape_string($lastConnectionDate);
  3789. $userConditions .= " AND u.last_login <= '$lastConnectionDate' ";
  3790. }
  3791. }
  3792. $courseConditions = null;
  3793. $sessionConditionsCoach = null;
  3794. $sessionConditionsTeacher = null;
  3795. $drhConditions = null;
  3796. $teacherSelect = null;
  3797. switch($status) {
  3798. case DRH:
  3799. $drhConditions .= " AND
  3800. friend_user_id = '$userId' AND
  3801. relation_type = '".USER_RELATION_TYPE_RRHH."'
  3802. ";
  3803. break;
  3804. case COURSEMANAGER:
  3805. $drhConditions .= " AND
  3806. friend_user_id = '$userId' AND
  3807. relation_type = '".USER_RELATION_TYPE_RRHH."'
  3808. ";
  3809. $sessionConditionsCoach .= " AND
  3810. (s.id_coach = '$userId')
  3811. ";
  3812. $sessionConditionsTeacher .= " AND
  3813. (scu.status = 2 AND scu.id_user = '$userId')
  3814. ";
  3815. $teacherSelect =
  3816. "UNION ALL (
  3817. $select
  3818. FROM $tbl_user u
  3819. INNER JOIN $tbl_session_rel_user sru ON (sru.id_user = u.user_id)
  3820. WHERE
  3821. sru.id_session IN (
  3822. SELECT DISTINCT(s.id) FROM $tbl_session s INNER JOIN
  3823. $tbl_session_rel_access_url
  3824. WHERE access_url_id = ".api_get_current_access_url_id()."
  3825. $sessionConditionsCoach
  3826. UNION (
  3827. SELECT DISTINCT(s.id) FROM $tbl_session s
  3828. INNER JOIN $tbl_session_rel_access_url url
  3829. ON (url.session_id = s.id)
  3830. INNER JOIN $tbl_session_rel_course_rel_user scu
  3831. ON (scu.id_session = s.id)
  3832. WHERE access_url_id = ".api_get_current_access_url_id()."
  3833. $sessionConditionsTeacher
  3834. )
  3835. )
  3836. $userConditions
  3837. )
  3838. UNION ALL(
  3839. $select
  3840. FROM $tbl_user u
  3841. INNER JOIN $tbl_course_user cu ON (cu.user_id = u.user_id)
  3842. WHERE cu.course_code IN (
  3843. SELECT DISTINCT(course_code) FROM $tbl_course_user
  3844. WHERE user_id = $userId AND status = ".COURSEMANAGER."
  3845. )
  3846. $userConditions
  3847. )"
  3848. ;
  3849. break;
  3850. }
  3851. $join = null;
  3852. $sql = " $masterSelect
  3853. (
  3854. (
  3855. $select
  3856. FROM $tbl_user u
  3857. INNER JOIN $tbl_user_rel_user uru ON (uru.user_id = u.user_id)
  3858. LEFT JOIN $tbl_user_rel_access_url a ON (a.user_id = u.user_id)
  3859. $join
  3860. WHERE
  3861. access_url_id = ".api_get_current_access_url_id()."
  3862. $drhConditions
  3863. $userConditions
  3864. )
  3865. $teacherSelect
  3866. ) as t1";
  3867. if ($getSql) {
  3868. return $sql;
  3869. }
  3870. if ($getCount) {
  3871. $result = Database::query($sql);
  3872. $row = Database::fetch_array($result);
  3873. return $row['count'];
  3874. }
  3875. $orderBy = null;
  3876. if (api_is_western_name_order()) {
  3877. $orderBy .= " ORDER BY firstname, lastname ";
  3878. } else {
  3879. $orderBy .= " ORDER BY lastname, firstname ";
  3880. }
  3881. if (!empty($column) && !empty($direction)) {
  3882. // Fixing order due the UNIONs
  3883. $column = str_replace('u.', '', $column);
  3884. $orderBy = " ORDER BY $column $direction ";
  3885. }
  3886. $sql .= $orderBy;
  3887. $sql .= $limitCondition;
  3888. $result = Database::query($sql);
  3889. $users = array();
  3890. if (Database::num_rows($result) > 0) {
  3891. while ($row = Database::fetch_array($result)) {
  3892. $users[$row['user_id']] = $row;
  3893. }
  3894. }
  3895. return $users;
  3896. }
  3897. /**
  3898. * Subscribes users to human resource manager (Dashboard feature)
  3899. * @param int hr dept id
  3900. * @param array Users id
  3901. * @param int affected rows
  3902. * */
  3903. public static function suscribe_users_to_hr_manager($hr_dept_id, $users_id)
  3904. {
  3905. // Database Table Definitions
  3906. $tbl_user_rel_user = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  3907. $tbl_user_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  3908. $hr_dept_id = intval($hr_dept_id);
  3909. $affected_rows = 0;
  3910. if (api_get_multiple_access_url()) {
  3911. //Deleting assigned users to hrm_id
  3912. $sql = "SELECT s.user_id FROM $tbl_user_rel_user s
  3913. INNER JOIN $tbl_user_rel_access_url a
  3914. ON (a.user_id = s.user_id)
  3915. WHERE
  3916. friend_user_id = $hr_dept_id AND
  3917. relation_type = '".USER_RELATION_TYPE_RRHH."' AND
  3918. access_url_id = ".api_get_current_access_url_id()."";
  3919. } else {
  3920. $sql = "SELECT user_id FROM $tbl_user_rel_user
  3921. WHERE
  3922. friend_user_id = $hr_dept_id AND
  3923. relation_type = '".USER_RELATION_TYPE_RRHH."' ";
  3924. }
  3925. $result = Database::query($sql);
  3926. if (Database::num_rows($result) > 0) {
  3927. while ($row = Database::fetch_array($result)) {
  3928. $sql = "DELETE FROM $tbl_user_rel_user
  3929. WHERE
  3930. user_id = '{$row['user_id']}' AND
  3931. friend_user_id = $hr_dept_id AND
  3932. relation_type = '".USER_RELATION_TYPE_RRHH."' ";
  3933. Database::query($sql);
  3934. }
  3935. }
  3936. // Inserting new user list
  3937. if (is_array($users_id)) {
  3938. foreach ($users_id as $user_id) {
  3939. $user_id = intval($user_id);
  3940. $sql = "INSERT IGNORE INTO $tbl_user_rel_user(user_id, friend_user_id, relation_type)
  3941. VALUES ('$user_id', $hr_dept_id, '".USER_RELATION_TYPE_RRHH."')";
  3942. Database::query($sql);
  3943. $affected_rows = Database::affected_rows();
  3944. }
  3945. }
  3946. return $affected_rows;
  3947. }
  3948. /**
  3949. * This function check if an user is followed by human resources manager
  3950. * @param int User id
  3951. * @param int Human resources manager
  3952. * @return bool
  3953. */
  3954. public static function is_user_followed_by_drh($user_id, $hr_dept_id)
  3955. {
  3956. // Database table and variables Definitions
  3957. $tbl_user_rel_user = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
  3958. $user_id = intval($user_id);
  3959. $hr_dept_id = intval($hr_dept_id);
  3960. $result = false;
  3961. $sql = "SELECT user_id FROM $tbl_user_rel_user
  3962. WHERE
  3963. user_id='$user_id' AND
  3964. friend_user_id='$hr_dept_id' AND
  3965. relation_type = ".USER_RELATION_TYPE_RRHH;
  3966. $rs = Database::query($sql);
  3967. if (Database::num_rows($rs) > 0) {
  3968. $result = true;
  3969. }
  3970. return $result;
  3971. }
  3972. /**
  3973. * get user id of teacher or session administrator
  3974. * @param string The course id
  3975. * @return int The user id
  3976. */
  3977. public static function get_user_id_of_course_admin_or_session_admin($course_id)
  3978. {
  3979. $session = api_get_session_id();
  3980. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  3981. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  3982. $table_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  3983. if ($session == 0 || is_null($session)) {
  3984. $sql = 'SELECT u.user_id FROM '.$table_user.' u
  3985. INNER JOIN '.$table_course_user.' ru ON ru.user_id=u.user_id
  3986. WHERE
  3987. ru.status = 1 AND
  3988. ru.course_code = "'.Database::escape_string($course_id).'" ';
  3989. $rs = Database::query($sql);
  3990. $num_rows = Database::num_rows($rs);
  3991. if ($num_rows == 1) {
  3992. $row = Database::fetch_array($rs);
  3993. return $row['user_id'];
  3994. } else {
  3995. $my_num_rows = $num_rows;
  3996. $my_user_id = Database::result($rs, $my_num_rows - 1, 'user_id');
  3997. return $my_user_id;
  3998. }
  3999. } elseif ($session > 0) {
  4000. $sql = 'SELECT u.user_id FROM '.$table_user.' u
  4001. INNER JOIN '.$table_session_course_user.' sru
  4002. ON sru.id_user=u.user_id
  4003. WHERE
  4004. sru.course_code="'.Database::escape_string($course_id).'" AND
  4005. sru.status=2';
  4006. $rs = Database::query($sql);
  4007. $row = Database::fetch_array($rs);
  4008. return $row['user_id'];
  4009. }
  4010. }
  4011. /**
  4012. * Determines if a user is a gradebook certified
  4013. * @param int The category id of gradebook
  4014. * @param int The user id
  4015. * @return boolean
  4016. */
  4017. public static function is_user_certified($cat_id, $user_id)
  4018. {
  4019. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  4020. $sql = 'SELECT path_certificate FROM '.$table_certificate.'
  4021. WHERE
  4022. cat_id="'.intval($cat_id).'" AND
  4023. user_id="'.intval($user_id).'"';
  4024. $rs = Database::query($sql);
  4025. $row = Database::fetch_array($rs);
  4026. if ($row['path_certificate'] == '' || is_null($row['path_certificate'])) {
  4027. return false;
  4028. } else {
  4029. return true;
  4030. }
  4031. }
  4032. /**
  4033. * Gets the info about a gradebook certificate for a user by course
  4034. * @param string The course code
  4035. * @param int The user id
  4036. * @return array if there is not information return false
  4037. */
  4038. public static function get_info_gradebook_certificate($course_code, $user_id)
  4039. {
  4040. $tbl_grade_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  4041. $tbl_grade_category = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  4042. $session_id = api_get_session_id();
  4043. if (empty($session_id)) {
  4044. $session_condition = ' AND (session_id = "" OR session_id = 0 OR session_id IS NULL )';
  4045. } else {
  4046. $session_condition = " AND session_id = $session_id";
  4047. }
  4048. // Getting gradebook score.
  4049. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be.inc.php';
  4050. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/scoredisplay.class.php';
  4051. $sql = 'SELECT * FROM '.$tbl_grade_certificate.' WHERE cat_id = (SELECT id FROM '.$tbl_grade_category.'
  4052. WHERE
  4053. course_code = "'.Database::escape_string($course_code).'" '.$session_condition.' LIMIT 1 ) AND
  4054. user_id='.intval($user_id);
  4055. $rs = Database::query($sql);
  4056. if (Database::num_rows($rs) > 0) {
  4057. $row = Database::fetch_array($rs, 'ASSOC');
  4058. $score = $row['score_certificate'];
  4059. $category_id = $row['cat_id'];
  4060. $cat = Category::load($category_id);
  4061. $displayscore = ScoreDisplay::instance();
  4062. if (isset($cat) && $displayscore->is_custom()) {
  4063. $grade = $displayscore->display_score(array($score, $cat[0]->get_weight()), SCORE_DIV_PERCENT_WITH_CUSTOM);
  4064. } else {
  4065. $grade = $displayscore->display_score(array($score, $cat[0]->get_weight()));
  4066. }
  4067. $row['grade'] = $grade;
  4068. return $row;
  4069. }
  4070. return false;
  4071. }
  4072. /**
  4073. * Gets the user path of user certificated
  4074. * @param int The user id
  4075. * @return array containing path_certificate and cat_id
  4076. */
  4077. public static function get_user_path_certificate($user_id)
  4078. {
  4079. $my_certificate = array();
  4080. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  4081. $table_gradebook_category = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  4082. $session_id = api_get_session_id();
  4083. $user_id = intval($user_id);
  4084. if ($session_id == 0 || is_null($session_id)) {
  4085. $sql_session = 'AND (session_id='.intval($session_id).' OR isnull(session_id)) ';
  4086. } elseif ($session_id > 0) {
  4087. $sql_session = 'AND session_id='.intval($session_id);
  4088. } else {
  4089. $sql_session = '';
  4090. }
  4091. $sql = "SELECT tc.path_certificate,tc.cat_id,tgc.course_code,tgc.name
  4092. FROM $table_certificate tc, $table_gradebook_category tgc
  4093. WHERE tgc.id = tc.cat_id AND tc.user_id='$user_id'
  4094. ORDER BY tc.date_certificate DESC limit 5";
  4095. $rs = Database::query($sql);
  4096. while ($row = Database::fetch_array($rs)) {
  4097. $my_certificate[] = $row;
  4098. }
  4099. return $my_certificate;
  4100. }
  4101. /**
  4102. * This function check if the user is a coach inside session course
  4103. * @param int User id
  4104. * @param string Course code
  4105. * @param int Session id
  4106. * @return bool True if the user is a coach
  4107. *
  4108. */
  4109. public static function is_session_course_coach($user_id, $course_code, $session_id)
  4110. {
  4111. $tbl_session_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  4112. // Protect data
  4113. $user_id = intval($user_id);
  4114. $course_code = Database::escape_string($course_code);
  4115. $session_id = intval($session_id);
  4116. $result = false;
  4117. $sql = "SELECT id_session FROM $tbl_session_course_rel_user
  4118. WHERE
  4119. id_session=$session_id AND
  4120. course_code='$course_code' AND
  4121. id_user = $user_id AND
  4122. status = 2 ";
  4123. $res = Database::query($sql);
  4124. if (Database::num_rows($res) > 0) {
  4125. $result = true;
  4126. }
  4127. return $result;
  4128. }
  4129. /**
  4130. * This function returns an icon path that represents the favicon of the website of which the url given.
  4131. * Defaults to the current Chamilo favicon
  4132. * @param string URL of website where to look for favicon.ico
  4133. * @param string Optional second URL of website where to look for favicon.ico
  4134. * @return string Path of icon to load
  4135. */
  4136. public static function get_favicon_from_url($url1, $url2 = null)
  4137. {
  4138. $icon_link = '';
  4139. $url = $url1;
  4140. if (empty($url1)) {
  4141. $url = $url2;
  4142. if (empty($url)) {
  4143. $url = api_get_access_url(api_get_current_access_url_id());
  4144. $url = $url[0];
  4145. }
  4146. }
  4147. if (!empty($url)) {
  4148. $pieces = parse_url($url);
  4149. $icon_link = $pieces['scheme'].'://'.$pieces['host'].'/favicon.ico';
  4150. }
  4151. return $icon_link;
  4152. }
  4153. /**
  4154. *
  4155. * @param int student id
  4156. * @param int years
  4157. * @param bool show warning_message
  4158. * @param bool return_timestamp
  4159. */
  4160. public static function delete_inactive_student($student_id, $years = 2, $warning_message = false, $return_timestamp = false)
  4161. {
  4162. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  4163. $sql = 'SELECT login_date FROM '.$tbl_track_login.'
  4164. WHERE login_user_id = '.intval($student_id).'
  4165. ORDER BY login_date DESC LIMIT 0,1';
  4166. if (empty($years)) {
  4167. $years = 1;
  4168. }
  4169. $inactive_time = $years * 31536000; //1 year
  4170. $rs = Database::query($sql);
  4171. if (Database::num_rows($rs) > 0) {
  4172. if ($last_login_date = Database::result($rs, 0, 0)) {
  4173. $last_login_date = api_get_local_time($last_login_date, null, date_default_timezone_get());
  4174. if ($return_timestamp) {
  4175. return api_strtotime($last_login_date);
  4176. } else {
  4177. if (!$warning_message) {
  4178. return api_format_date($last_login_date, DATE_FORMAT_SHORT);
  4179. } else {
  4180. $timestamp = api_strtotime($last_login_date);
  4181. $currentTimestamp = time();
  4182. //If the last connection is > than 7 days, the text is red
  4183. //345600 = 7 days in seconds 63072000= 2 ans
  4184. // if ($currentTimestamp - $timestamp > 184590 )
  4185. if ($currentTimestamp - $timestamp > $inactive_time && UserManager::delete_user($student_id)) {
  4186. Display :: display_normal_message(get_lang('UserDeleted'));
  4187. echo '<p>', 'id', $student_id, ':', $last_login_date, '</p>';
  4188. }
  4189. }
  4190. }
  4191. }
  4192. }
  4193. return false;
  4194. }
  4195. /**
  4196. * @param FormValidator $form
  4197. * @param $extra_data
  4198. * @param $form_name
  4199. * @param bool $admin_permissions
  4200. * @param null $user_id
  4201. * @return array
  4202. */
  4203. static function set_extra_fields_in_form($form, $extra_data, $form_name, $admin_permissions = false, $user_id = null)
  4204. {
  4205. $user_id = intval($user_id);
  4206. // EXTRA FIELDS
  4207. $extra = UserManager::get_extra_fields(0, 50, 5, 'ASC');
  4208. $jquery_ready_content = null;
  4209. foreach ($extra as $field_details) {
  4210. if (!$admin_permissions) {
  4211. if ($field_details[6] == 0) {
  4212. continue;
  4213. }
  4214. }
  4215. switch ($field_details[2]) {
  4216. case self::USER_FIELD_TYPE_TEXT:
  4217. $form->addElement('text', 'extra_'.$field_details[1], $field_details[3], array('size' => 40));
  4218. $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
  4219. $form->applyFilter('extra_'.$field_details[1], 'trim');
  4220. $form->applyFilter('extra_'.$field_details[1], 'html_filter');
  4221. if (!$admin_permissions) {
  4222. if ($field_details[7] == 0) {
  4223. $form->freeze('extra_'.$field_details[1]);
  4224. }
  4225. }
  4226. break;
  4227. case self::USER_FIELD_TYPE_TEXTAREA:
  4228. $form->add_html_editor(
  4229. 'extra_'.$field_details[1],
  4230. $field_details[3],
  4231. false,
  4232. false,
  4233. array('ToolbarSet' => 'Profile', 'Width' => '100%', 'Height' => '130')
  4234. );
  4235. //$form->addElement('textarea', 'extra_'.$field_details[1], $field_details[3], array('size' => 80));
  4236. $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
  4237. $form->applyFilter('extra_'.$field_details[1], 'trim');
  4238. if (!$admin_permissions) {
  4239. if ($field_details[7] == 0)
  4240. $form->freeze('extra_'.$field_details[1]);
  4241. }
  4242. break;
  4243. case self::USER_FIELD_TYPE_RADIO:
  4244. $group = array();
  4245. foreach ($field_details[9] as $option_id => $option_details) {
  4246. $options[$option_details[1]] = $option_details[2];
  4247. $group[] = $form->createElement(
  4248. 'radio',
  4249. 'extra_'.$field_details[1],
  4250. $option_details[1],
  4251. $option_details[2].'<br />',
  4252. $option_details[1]
  4253. );
  4254. }
  4255. $form->addGroup($group, 'extra_'.$field_details[1], $field_details[3], '');
  4256. if (!$admin_permissions) {
  4257. if ($field_details[7] == 0)
  4258. $form->freeze('extra_'.$field_details[1]);
  4259. }
  4260. break;
  4261. case self::USER_FIELD_TYPE_SELECT:
  4262. $get_lang_variables = false;
  4263. if (in_array($field_details[1], array('mail_notify_message', 'mail_notify_invitation', 'mail_notify_group_message'))) {
  4264. $get_lang_variables = true;
  4265. }
  4266. $options = array();
  4267. foreach ($field_details[9] as $option_id => $option_details) {
  4268. //$options[$option_details[1]] = $option_details[2];
  4269. if ($get_lang_variables) {
  4270. $options[$option_details[1]] = get_lang($option_details[2]);
  4271. } else {
  4272. $options[$option_details[1]] = $option_details[2];
  4273. }
  4274. }
  4275. if ($get_lang_variables) {
  4276. $field_details[3] = get_lang($field_details[3]);
  4277. }
  4278. $form->addElement(
  4279. 'select',
  4280. 'extra_'.$field_details[1],
  4281. $field_details[3],
  4282. $options,
  4283. array('class' => 'chzn-select', 'id' => 'extra_'.$field_details[1])
  4284. );
  4285. if (!$admin_permissions) {
  4286. if ($field_details[7] == 0)
  4287. $form->freeze('extra_'.$field_details[1]);
  4288. }
  4289. break;
  4290. case self::USER_FIELD_TYPE_SELECT_MULTIPLE:
  4291. $options = array();
  4292. foreach ($field_details[9] as $option_id => $option_details) {
  4293. $options[$option_details[1]] = $option_details[2];
  4294. }
  4295. $form->addElement('select', 'extra_'.$field_details[1], $field_details[3], $options, array('multiple' => 'multiple'));
  4296. if (!$admin_permissions) {
  4297. if ($field_details[7] == 0)
  4298. $form->freeze('extra_'.$field_details[1]);
  4299. }
  4300. break;
  4301. case self::USER_FIELD_TYPE_DATE:
  4302. $form->addElement('date_picker', 'extra_'.$field_details[1], $field_details[3]);
  4303. //$form->_elements[$form->_elementIndex['extra_'.$field_details[1]]]->setLocalOption('minYear', 1900);
  4304. $defaults['extra_'.$field_details[1]] = date('Y-m-d 12:00:00');
  4305. $form->setDefaults($defaults);
  4306. if (!$admin_permissions) {
  4307. if ($field_details[7] == 0)
  4308. $form->freeze('extra_'.$field_details[1]);
  4309. }
  4310. $form->applyFilter('theme', 'trim');
  4311. break;
  4312. case self::USER_FIELD_TYPE_DATETIME:
  4313. $form->addElement('date_time_picker', 'extra_'.$field_details[1], $field_details[3]);
  4314. //$form->_elements[$form->_elementIndex['extra_'.$field_details[1]]]->setLocalOption('minYear', 1900);
  4315. $defaults['extra_'.$field_details[1]] = date('Y-m-d 12:00:00');
  4316. $form->setDefaults($defaults);
  4317. if (!$admin_permissions) {
  4318. if ($field_details[7] == 0)
  4319. $form->freeze('extra_'.$field_details[1]);
  4320. }
  4321. $form->applyFilter('theme', 'trim');
  4322. break;
  4323. case self::USER_FIELD_TYPE_DOUBLE_SELECT:
  4324. foreach ($field_details[9] as $key => $element) {
  4325. if ($element[2][0] == '*') {
  4326. $values['*'][$element[0]] = str_replace('*', '', $element[2]);
  4327. } else {
  4328. $values[0][$element[0]] = $element[2];
  4329. }
  4330. }
  4331. $group = '';
  4332. $group[] = & HTML_QuickForm::createElement('select', 'extra_'.$field_details[1], '', $values[0], '');
  4333. $group[] = & HTML_QuickForm::createElement('select', 'extra_'.$field_details[1].'*', '', $values['*'], '');
  4334. $form->addGroup($group, 'extra_'.$field_details[1], $field_details[3], '&nbsp;');
  4335. if (!$admin_permissions) {
  4336. if ($field_details[7] == 0)
  4337. $form->freeze('extra_'.$field_details[1]);
  4338. }
  4339. /* Recoding the selected values for double : if the user has
  4340. selected certain values, we have to assign them to the
  4341. correct select form */
  4342. if (array_key_exists('extra_'.$field_details[1], $extra_data)) {
  4343. // exploding all the selected values (of both select forms)
  4344. $selected_values = explode(';', $extra_data['extra_'.$field_details[1]]);
  4345. $extra_data['extra_'.$field_details[1]] = array();
  4346. // looping through the selected values and assigning the selected values to either the first or second select form
  4347. foreach ($selected_values as $key => $selected_value) {
  4348. if (array_key_exists($selected_value, $values[0])) {
  4349. $extra_data['extra_'.$field_details[1]]['extra_'.$field_details[1]] = $selected_value;
  4350. } else {
  4351. $extra_data['extra_'.$field_details[1]]['extra_'.$field_details[1].'*'] = $selected_value;
  4352. }
  4353. }
  4354. }
  4355. break;
  4356. case self::USER_FIELD_TYPE_DIVIDER:
  4357. $form->addElement('static', $field_details[1], '<br /><strong>'.$field_details[3].'</strong>');
  4358. break;
  4359. case self::USER_FIELD_TYPE_TAG:
  4360. //the magic should be here
  4361. $user_tags = UserManager::get_user_tags($user_id, $field_details[0]);
  4362. $tag_list = '';
  4363. if (is_array($user_tags) && count($user_tags) > 0) {
  4364. foreach ($user_tags as $tag) {
  4365. $tag_list .= '<option value="'.$tag['tag'].'" class="selected">'.$tag['tag'].'</option>';
  4366. }
  4367. }
  4368. $multi_select = '<select id="extra_'.$field_details[1].'" name="extra_'.$field_details[1].'">
  4369. '.$tag_list.'
  4370. </select>';
  4371. $form->addElement('label', $field_details[3], $multi_select);
  4372. $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php';
  4373. $complete_text = get_lang('StartToType');
  4374. //if cache is set to true the jquery will be called 1 time
  4375. $jquery_ready_content = <<<EOF
  4376. $("#extra_$field_details[1]").fcbkcomplete({
  4377. json_url: "$url?a=search_tags&field_id=$field_details[0]",
  4378. cache: false,
  4379. filter_case: true,
  4380. filter_hide: true,
  4381. complete_text:"$complete_text",
  4382. firstselected: true,
  4383. //onremove: "testme",
  4384. //onselect: "testme",
  4385. filter_selected: true,
  4386. newel: true
  4387. });
  4388. EOF;
  4389. break;
  4390. case self::USER_FIELD_TYPE_TIMEZONE:
  4391. $form->addElement('select', 'extra_'.$field_details[1], $field_details[3], api_get_timezones(), '');
  4392. if ($field_details[7] == 0)
  4393. $form->freeze('extra_'.$field_details[1]);
  4394. break;
  4395. case self::USER_FIELD_TYPE_SOCIAL_PROFILE:
  4396. // get the social network's favicon
  4397. $icon_path = UserManager::get_favicon_from_url($extra_data['extra_'.$field_details[1]], $field_details[4]);
  4398. // special hack for hi5
  4399. $leftpad = '1.7';
  4400. $top = '0.4';
  4401. $domain = parse_url($icon_path, PHP_URL_HOST);
  4402. if ($domain == 'www.hi5.com' or $domain == 'hi5.com') {
  4403. $leftpad = '3';
  4404. $top = '0';
  4405. }
  4406. // print the input field
  4407. $form->addElement(
  4408. 'text',
  4409. 'extra_'.$field_details[1],
  4410. $field_details[3],
  4411. array(
  4412. 'size' => 60,
  4413. 'style' => 'background-image: url(\''.$icon_path.'\'); background-repeat: no-repeat; background-position: 0.4em '.$top.'em; padding-left: '.$leftpad.'em; '
  4414. )
  4415. );
  4416. $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
  4417. $form->applyFilter('extra_'.$field_details[1], 'trim');
  4418. if ($field_details[7] == 0)
  4419. $form->freeze('extra_'.$field_details[1]);
  4420. break;
  4421. case self::USER_FIELD_TYPE_FILE:
  4422. $extra_field = 'extra_'.$field_details[1];
  4423. $form->addElement('file', $extra_field, $field_details[3], null, '');
  4424. if ($extra_file_list = UserManager::build_user_extra_file_list($user_id, $field_details[1], '', true)) {
  4425. $form->addElement('static', $extra_field . '_list', null, $extra_file_list);
  4426. }
  4427. if ($field_details[7] == 0) {
  4428. $form->freeze($extra_field);
  4429. }
  4430. break;
  4431. case self::USER_FIELD_TYPE_MOBILE_PHONE_NUMBER:
  4432. $form->addElement(
  4433. 'text',
  4434. 'extra_'.$field_details[1],
  4435. $field_details[3]." (".get_lang('CountryDialCode').")",
  4436. array('size' => 40, 'placeholder' => '(xx)xxxxxxxxx')
  4437. );
  4438. $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
  4439. $form->applyFilter('extra_'.$field_details[1], 'trim');
  4440. $form->applyFilter('extra_'.$field_details[1], 'mobile_phone_number_filter');
  4441. $form->addRule(
  4442. 'extra_'.$field_details[1],
  4443. get_lang('MobilePhoneNumberWrong'),
  4444. 'mobile_phone_number'
  4445. );
  4446. if (!$admin_permissions) {
  4447. if ($field_details[7] == 0) {
  4448. $form->freeze('extra_'.$field_details[1]);
  4449. }
  4450. }
  4451. break;
  4452. }
  4453. }
  4454. $return = array();
  4455. $return['jquery_ready_content'] = $jquery_ready_content;
  4456. return $return;
  4457. }
  4458. /**
  4459. * @return array
  4460. */
  4461. static function get_user_field_types()
  4462. {
  4463. $types = array();
  4464. $types[self::USER_FIELD_TYPE_TEXT] = get_lang('FieldTypeText');
  4465. $types[self::USER_FIELD_TYPE_TEXTAREA] = get_lang('FieldTypeTextarea');
  4466. $types[self::USER_FIELD_TYPE_RADIO] = get_lang('FieldTypeRadio');
  4467. $types[self::USER_FIELD_TYPE_SELECT] = get_lang('FieldTypeSelect');
  4468. $types[self::USER_FIELD_TYPE_SELECT_MULTIPLE] = get_lang('FieldTypeSelectMultiple');
  4469. $types[self::USER_FIELD_TYPE_DATE] = get_lang('FieldTypeDate');
  4470. $types[self::USER_FIELD_TYPE_DATETIME] = get_lang('FieldTypeDatetime');
  4471. $types[self::USER_FIELD_TYPE_DOUBLE_SELECT] = get_lang('FieldTypeDoubleSelect');
  4472. $types[self::USER_FIELD_TYPE_DIVIDER] = get_lang('FieldTypeDivider');
  4473. $types[self::USER_FIELD_TYPE_TAG] = get_lang('FieldTypeTag');
  4474. $types[self::USER_FIELD_TYPE_TIMEZONE] = get_lang('FieldTypeTimezone');
  4475. $types[self::USER_FIELD_TYPE_SOCIAL_PROFILE] = get_lang('FieldTypeSocialProfile');
  4476. $types[self::USER_FIELD_TYPE_FILE] = get_lang('FieldTypeFile');
  4477. $types[self::USER_FIELD_TYPE_MOBILE_PHONE_NUMBER] = get_lang('FieldTypeMobilePhoneNumber');
  4478. return $types;
  4479. }
  4480. /**
  4481. * @param int $user_id
  4482. */
  4483. static function add_user_as_admin($user_id)
  4484. {
  4485. $table_admin = Database :: get_main_table(TABLE_MAIN_ADMIN);
  4486. $user_id = intval($user_id);
  4487. if (!self::is_admin($user_id)) {
  4488. $sql = "INSERT INTO $table_admin SET user_id = '".$user_id."'";
  4489. Database::query($sql);
  4490. }
  4491. }
  4492. /**
  4493. * @param int $user_id
  4494. */
  4495. public static function remove_user_admin($user_id)
  4496. {
  4497. $table_admin = Database :: get_main_table(TABLE_MAIN_ADMIN);
  4498. $user_id = intval($user_id);
  4499. if (self::is_admin($user_id)) {
  4500. $sql = "DELETE FROM $table_admin WHERE user_id = '".$user_id."'";
  4501. Database::query($sql);
  4502. }
  4503. }
  4504. /**
  4505. * @param string $from
  4506. * @param string $to
  4507. */
  4508. public static function update_all_user_languages($from, $to)
  4509. {
  4510. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  4511. $from = Database::escape_string($from);
  4512. $to = Database::escape_string($to);
  4513. if (!empty($to) && !empty($from)) {
  4514. $sql = "UPDATE $table_user SET language = '$to'
  4515. WHERE language = '$from'";
  4516. Database::query($sql);
  4517. }
  4518. }
  4519. /**
  4520. * @return array
  4521. */
  4522. public static function getOfficialCodeGrouped()
  4523. {
  4524. $user = Database::get_main_table(TABLE_MAIN_USER);
  4525. $sql = "SELECT DISTINCT official_code
  4526. FROM $user
  4527. GROUP BY official_code";
  4528. $result = Database::query($sql);
  4529. $values = Database::store_result($result, 'ASSOC');
  4530. $result = array();
  4531. foreach ($values as $value) {
  4532. $result[$value['official_code']] = $value['official_code'];
  4533. }
  4534. return $result;
  4535. }
  4536. /**
  4537. * @param string $officialCode
  4538. * @return array
  4539. */
  4540. public static function getUsersByOfficialCode($officialCode)
  4541. {
  4542. $user = Database::get_main_table(TABLE_MAIN_USER);
  4543. $officialCode = Database::escape_string($officialCode);
  4544. $sql = "SELECT DISTINCT user_id
  4545. FROM $user
  4546. WHERE official_code = '$officialCode'
  4547. ";
  4548. $result = Database::query($sql);
  4549. $users = array();
  4550. while ($row = Database::fetch_array($result)) {
  4551. $users[] = $row['user_id'];
  4552. }
  4553. return $users;
  4554. }
  4555. }