usermanager.lib.php 194 KB

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