course.lib.php 206 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739
  1. <?php
  2. /* For licensing terms, see /license.txt*/
  3. /**
  4. * This is the course library for Chamilo.
  5. *
  6. * All main course functions should be placed here.
  7. *
  8. * Many functions of this library deal with providing support for
  9. * virtual/linked/combined courses (this was already used in several universities
  10. * but not available in standard Chamilo).
  11. *
  12. * The implementation changed, initially a course was a real course
  13. * if target_course_code was 0 , this was changed to NULL.
  14. * There are probably some places left with the wrong code.
  15. *
  16. * @package chamilo.library
  17. */
  18. /* INIT SECTION */
  19. require_once api_get_path(CONFIGURATION_PATH).'add_course.conf.php';
  20. require_once api_get_path(LIBRARY_PATH).'add_course.lib.inc.php';
  21. /**
  22. * CourseManager Class
  23. * @package chamilo.library
  24. */
  25. class CourseManager
  26. {
  27. const MAX_COURSE_LENGTH_CODE = 40;
  28. /** This constant is used to show separate user names in the course list (userportal), footer, etc */
  29. const USER_SEPARATOR = ' |';
  30. const COURSE_FIELD_TYPE_CHECKBOX = 10;
  31. public $columns = array();
  32. /**
  33. * Creates a course
  34. * @param array $params columns in the main.course table
  35. *
  36. * @return mixed false if the course was not created, array with the course info
  37. */
  38. public static function create_course($params)
  39. {
  40. global $_configuration;
  41. // Check portal limits
  42. $access_url_id = 1;
  43. if (api_get_multiple_access_url()) {
  44. $access_url_id = api_get_current_access_url_id();
  45. }
  46. if (is_array($_configuration[$access_url_id]) &&
  47. isset($_configuration[$access_url_id]['hosting_limit_courses']) &&
  48. $_configuration[$access_url_id]['hosting_limit_courses'] > 0
  49. ) {
  50. $num = self::count_courses();
  51. if ($num >= $_configuration[$access_url_id]['hosting_limit_courses']) {
  52. return api_set_failure('PortalCoursesLimitReached');
  53. }
  54. }
  55. if (empty($params['title'])) {
  56. return false;
  57. }
  58. if (empty($params['wanted_code'])) {
  59. $params['wanted_code'] = $params['title'];
  60. // Check whether the requested course code has already been occupied.
  61. $params['wanted_code'] = generate_course_code(api_substr($params['title'], 0, self::MAX_COURSE_LENGTH_CODE));
  62. }
  63. // Create the course keys
  64. $keys = define_course_keys($params['wanted_code']);
  65. $params['exemplary_content'] = isset($params['exemplary_content']) ? $params['exemplary_content'] : false;
  66. if (count($keys)) {
  67. $params['code'] = $keys['currentCourseCode'];
  68. $params['visual_code'] = $keys['currentCourseId'];
  69. $params['directory'] = $keys['currentCourseRepository'];
  70. $course_info = api_get_course_info($params['code']);
  71. if (empty($course_info)) {
  72. $course_id = register_course($params);
  73. $course_info = api_get_course_info_by_id($course_id);
  74. if (!empty($course_info)) {
  75. prepare_course_repository($course_info['directory'], $course_info['code']);
  76. fill_db_course($course_id, $course_info['directory'], $course_info['course_language'], $params['exemplary_content']);
  77. if (api_get_setting('gradebook_enable_grade_model') == 'true') {
  78. //Create gradebook_category for the new course and add a gradebook model for the course
  79. if (isset($params['gradebook_model_id']) && !empty($params['gradebook_model_id']) && $params['gradebook_model_id'] != '-1') {
  80. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  81. create_default_course_gradebook($course_info['code'], $params['gradebook_model_id']);
  82. }
  83. }
  84. // If parameter defined, copy the contents from a specific
  85. // template course into this new course
  86. if (!empty($_configuration['course_creation_use_template'])) {
  87. // Include the necessary libraries to generate a course copy
  88. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  89. require_once api_get_path(SYS_CODE_PATH).'coursecopy/classes/CourseBuilder.class.php';
  90. require_once api_get_path(SYS_CODE_PATH).'coursecopy/classes/CourseRestorer.class.php';
  91. require_once api_get_path(SYS_CODE_PATH).'coursecopy/classes/CourseSelectForm.class.php';
  92. // Call the course copy object
  93. $originCourse = api_get_course_info_by_id($_configuration['course_creation_use_template']);
  94. $originCourse['official_code'] = $originCourse['code'];
  95. $cb = new CourseBuilder(null, $originCourse);
  96. $course = $cb->build(null, $originCourse['code']);
  97. $cr = new CourseRestorer($course);
  98. $cr->set_file_option();
  99. $cr->restore($course_info['id']); //course_info[id] is the course.code value (I know...)
  100. }
  101. return $course_info;
  102. }
  103. }
  104. }
  105. return false;
  106. }
  107. /**
  108. * Returns all the information of a given course code
  109. * @param string $course_code, the course code
  110. * @return an array with all the fields of the course table
  111. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  112. * @assert ('') === false
  113. */
  114. public static function get_course_information($course_code)
  115. {
  116. return Database::fetch_array(Database::query(
  117. "SELECT *, id as real_id FROM ".Database::get_main_table(TABLE_MAIN_COURSE)."
  118. WHERE code='".Database::escape_string($course_code)."'"),'ASSOC'
  119. );
  120. }
  121. /**
  122. * Returns all the information of a given coursecode
  123. * @param int the course id
  124. * @return an array with all the fields of the course table
  125. * @assert ('') === false
  126. */
  127. public static function get_course_information_by_id($course_id) {
  128. return Database::select('*, id as real_id', Database::get_main_table(TABLE_MAIN_COURSE), array('where'=>array('id = ?' =>intval($course_id))),'first');
  129. }
  130. /**
  131. * Returns a list of courses. Should work with quickform syntax
  132. * @param integer $from Offset (from the 7th = '6'). Optional.
  133. * @param integer $howmany Number of results we want. Optional.
  134. * @param string $orderby The column we want to order it by. Optional, defaults to first column.
  135. * @param string $orderdirection The direction of the order (ASC or DESC). Optional, defaults to ASC.
  136. * @param string $visibility The visibility of the course, or all by default.
  137. * @param string $startwith If defined, only return results for which the course *title* begins with this string
  138. * @param string $urlId The Access URL ID, if using multiple URLs
  139. * @param string $alsoSearchCode An extension option to indicate that we also want to search for course codes (not *only* titles)
  140. * @param array $conditions
  141. * @return array
  142. */
  143. public static function get_courses_list(
  144. $from = 0,
  145. $howmany = 0,
  146. $orderby = 1,
  147. $orderdirection = 'ASC',
  148. $visibility = -1,
  149. $startwith = '',
  150. $urlId = null,
  151. $alsoSearchCode = false,
  152. $conditionsLike = array()
  153. ) {
  154. $sql = "SELECT course.* FROM ".Database::get_main_table(TABLE_MAIN_COURSE)." course ";
  155. if (!empty($urlId)) {
  156. $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  157. $sql .= " INNER JOIN $table url ON (url.course_code = course.code) ";
  158. }
  159. if (!empty($startwith)) {
  160. $sql .= "WHERE (title LIKE '".Database::escape_string($startwith)."%' ";
  161. if ($alsoSearchCode) {
  162. $sql .= "OR code LIKE '".Database::escape_string($startwith)."%' ";
  163. }
  164. $sql .= ') ';
  165. if ($visibility !== -1 && $visibility == strval(intval($visibility))) {
  166. $sql .= " AND visibility = $visibility ";
  167. }
  168. } else {
  169. $sql .= "WHERE 1 ";
  170. if ($visibility !== -1 && $visibility == strval(intval($visibility))) {
  171. $sql .= " AND visibility = $visibility ";
  172. }
  173. }
  174. if (!empty($urlId)) {
  175. $urlId = intval($urlId);
  176. $sql .= " AND access_url_id= $urlId";
  177. }
  178. $allowedFields = array(
  179. 'title',
  180. 'code'
  181. );
  182. if (count($conditionsLike) > 0) {
  183. $sql .= ' AND ';
  184. $temp_conditions = array();
  185. foreach ($conditionsLike as $field => $value) {
  186. if (!in_array($field, $allowedFields)) {
  187. continue;
  188. }
  189. $field = Database::escape_string($field);
  190. $value = Database::escape_string($value);
  191. $simple_like = false;
  192. if ($simple_like) {
  193. $temp_conditions[] = $field." LIKE '$value%'";
  194. } else {
  195. $temp_conditions[] = $field.' LIKE \'%'.$value.'%\'';
  196. }
  197. }
  198. $condition = ' AND ';
  199. if (!empty($temp_conditions)) {
  200. $sql .= implode(' '.$condition.' ', $temp_conditions);
  201. }
  202. }
  203. if (!empty($orderby)) {
  204. $sql .= " ORDER BY ".Database::escape_string($orderby)." ";
  205. } else {
  206. $sql .= " ORDER BY 1 ";
  207. }
  208. if (!in_array($orderdirection, array('ASC', 'DESC'))) {
  209. $sql .= 'ASC';
  210. } else {
  211. $sql .= Database::escape_string($orderdirection);
  212. }
  213. if (!empty($howmany) && is_int($howmany) and $howmany > 0) {
  214. $sql .= ' LIMIT '.Database::escape_string($howmany);
  215. } else {
  216. $sql .= ' LIMIT 1000000'; //virtually no limit
  217. }
  218. if (!empty($from)) {
  219. $from = intval($from);
  220. $sql .= ' OFFSET '.Database::escape_string($from);
  221. } else {
  222. $sql .= ' OFFSET 0';
  223. }
  224. return Database::store_result(Database::query($sql));
  225. }
  226. /**
  227. * Returns the access settings of the course:
  228. * which visibility;
  229. * wether subscribing is allowed;
  230. * wether unsubscribing is allowed.
  231. *
  232. * @param string $course_code, the course code
  233. * @todo for more consistency: use course_info call from database API
  234. * @return an array with int fields "visibility", "subscribe", "unsubscribe"
  235. * @assert ('') === false
  236. */
  237. public static function get_access_settings($course_code)
  238. {
  239. return Database::fetch_array(Database::query(
  240. "SELECT visibility, subscribe, unsubscribe from ".Database::get_main_table(TABLE_MAIN_COURSE)."
  241. WHERE code = '".Database::escape_string($course_code)."'")
  242. );
  243. }
  244. /**
  245. * Returns the status of a user in a course, which is COURSEMANAGER or STUDENT.
  246. * @param int User ID
  247. * @param string Course code
  248. * @return int the status of the user in that course
  249. */
  250. public static function get_user_in_course_status($user_id, $course_code)
  251. {
  252. $result = Database::fetch_array(Database::query(
  253. "SELECT status FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  254. WHERE course_code = '".Database::escape_string($course_code)."' AND user_id = ".Database::escape_string($user_id))
  255. );
  256. return $result['status'];
  257. }
  258. /**
  259. * @param int $user_id
  260. * @param string $course_code
  261. * @return mixed
  262. */
  263. public static function get_tutor_in_course_status($user_id, $course_code)
  264. {
  265. $result = Database::fetch_array(Database::query(
  266. "SELECT tutor_id FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  267. WHERE course_code = '".Database::escape_string($course_code)."' AND user_id = ".Database::escape_string($user_id))
  268. );
  269. return $result['tutor_id'];
  270. }
  271. /**
  272. * Unsubscribe one or more users from a course
  273. *
  274. * @param mixed user_id or an array with user ids
  275. * @param string course code
  276. * @param int session id
  277. * @assert ('', '') === false
  278. *
  279. */
  280. public static function unsubscribe_user($user_id, $course_code, $session_id = 0)
  281. {
  282. if (!is_array($user_id)) {
  283. $user_id = array($user_id);
  284. }
  285. if (count($user_id) == 0) {
  286. return;
  287. }
  288. if (!empty($session_id)) {
  289. $session_id = intval($session_id);
  290. } else {
  291. $session_id = api_get_session_id();
  292. }
  293. $user_list = array();
  294. // Cleaning the $user_id variable
  295. if (is_array($user_id)) {
  296. $new_user_id_list = array();
  297. foreach ($user_id as $my_user_id) {
  298. $new_user_id_list[]= intval($my_user_id);
  299. }
  300. $new_user_id_list = array_filter($new_user_id_list);
  301. $user_list = $new_user_id_list;
  302. $user_ids = implode(',', $new_user_id_list);
  303. } else {
  304. $user_ids = intval($user_id);
  305. $user_list[] = $user_id;
  306. }
  307. $course_info = api_get_course_info($course_code);
  308. $course_id = $course_info['real_id'];
  309. // Unsubscribe user from all groups in the course.
  310. $sql = "DELETE FROM ".Database::get_course_table(TABLE_GROUP_USER)."
  311. WHERE c_id = $course_id AND user_id IN (".$user_ids.")";
  312. Database::query($sql);
  313. $sql = "DELETE FROM ".Database::get_course_table(TABLE_GROUP_TUTOR)."
  314. WHERE c_id = $course_id AND user_id IN (".$user_ids.")";
  315. Database::query($sql);
  316. // Erase user student publications (works) in the course - by André Boivin
  317. if (!empty($user_list)) {
  318. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  319. foreach ($user_list as $userId) {
  320. // Getting all work from user
  321. $workList = getWorkPerUser($userId);
  322. if (!empty($workList)) {
  323. foreach ($workList as $work) {
  324. $work = $work['work'];
  325. // Getting user results
  326. if (!empty($work->user_results)) {
  327. foreach ($work->user_results as $workSent) {
  328. deleteWorkItem($workSent['id'], $course_info);
  329. }
  330. }
  331. }
  332. }
  333. }
  334. }
  335. // Unsubscribe user from all blogs in the course.
  336. Database::query("DELETE FROM ".Database::get_course_table(TABLE_BLOGS_REL_USER)." WHERE c_id = $course_id AND user_id IN (".$user_ids.")");
  337. Database::query("DELETE FROM ".Database::get_course_table(TABLE_BLOGS_TASKS_REL_USER)." WHERE c_id = $course_id AND user_id IN (".$user_ids.")");
  338. // Deleting users in forum_notification and mailqueue course tables
  339. $sql = "DELETE FROM ".Database::get_course_table(TABLE_FORUM_NOTIFICATION)."
  340. WHERE c_id = $course_id AND user_id IN (".$user_ids.")";
  341. Database::query($sql);
  342. $sql = "DELETE FROM ".Database::get_course_table(TABLE_FORUM_MAIL_QUEUE)."
  343. WHERE c_id = $course_id AND user_id IN (".$user_ids.")";
  344. Database::query($sql);
  345. // Unsubscribe user from the course.
  346. if (!empty($session_id)) {
  347. // Delete in table session_rel_course_rel_user
  348. $sql = "DELETE FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)."
  349. WHERE id_session ='".$session_id."' AND
  350. course_code = '".Database::escape_string($course_info['code'])."' AND
  351. id_user IN ($user_ids)";
  352. Database::query($sql);
  353. foreach ($user_list as $uid) {
  354. // check if a user is register in the session with other course
  355. $sql = "SELECT id_user FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)."
  356. WHERE id_session='$session_id' AND id_user='$uid'";
  357. $rs = Database::query($sql);
  358. if (Database::num_rows($rs) == 0) {
  359. // Delete in table session_rel_user
  360. $sql = "DELETE FROM ".Database::get_main_table(TABLE_MAIN_SESSION_USER)."
  361. WHERE id_session ='".$session_id."' AND id_user='$uid' AND relation_type<>".SESSION_RELATION_TYPE_RRHH."";
  362. Database::query($sql);
  363. }
  364. }
  365. // Update the table session
  366. $sql = "SELECT COUNT(*) FROM ".Database::get_main_table(TABLE_MAIN_SESSION_USER)."
  367. WHERE id_session = '".$session_id."' AND relation_type <> ".SESSION_RELATION_TYPE_RRHH;
  368. $row = Database::fetch_array(Database::query($sql));
  369. $count = $row[0];
  370. // number of users by session
  371. $sql = "UPDATE ".Database::get_main_table(TABLE_MAIN_SESSION)." SET nbr_users = '$count'
  372. WHERE id = '".$session_id."'";
  373. Database::query($sql);
  374. // Update the table session_rel_course
  375. $sql = "SELECT COUNT(*) FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)."
  376. WHERE id_session = '$session_id' AND course_code = '$course_code' AND status<>2";
  377. $row = Database::fetch_array(@Database::query($sql));
  378. $count = $row[0];
  379. // number of users by session and course
  380. $sql = "UPDATE ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE)."
  381. SET nbr_users = '$count'
  382. WHERE id_session = '$session_id' AND course_code = '$course_code'";
  383. Database::query($sql);
  384. } else {
  385. $sql = "DELETE FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  386. WHERE
  387. user_id IN (".$user_ids.") AND
  388. relation_type<>".COURSE_RELATION_TYPE_RRHH." AND
  389. course_code = '".$course_code."'";
  390. Database::query($sql);
  391. // add event to system log
  392. $user_id = api_get_user_id();
  393. event_system(LOG_UNSUBSCRIBE_USER_FROM_COURSE, LOG_COURSE_CODE, $course_code, api_get_utc_datetime(), $user_id);
  394. foreach ($user_list as $userId) {
  395. $userInfo = api_get_user_info($userId);
  396. event_system(LOG_UNSUBSCRIBE_USER_FROM_COURSE, LOG_USER_OBJECT, $userInfo, api_get_utc_datetime(), $user_id);
  397. }
  398. }
  399. }
  400. /**
  401. * Subscribe a user to a course. No checks are performed here to see if
  402. * course subscription is allowed.
  403. * @param int User ID
  404. * @param string Course code
  405. * @param int Status (STUDENT, COURSEMANAGER, COURSE_ADMIN, NORMAL_COURSE_MEMBER)
  406. * @return bool True on success, false on failure
  407. * @see add_user_to_course
  408. * @assert ('', '') === false
  409. */
  410. public static function subscribe_user($user_id, $course_code, $status = STUDENT, $session_id = 0)
  411. {
  412. if ($user_id != strval(intval($user_id))) {
  413. return false; //detected possible SQL injection
  414. }
  415. $course_code = Database::escape_string($course_code);
  416. if (empty($user_id) || empty ($course_code)) {
  417. return false;
  418. }
  419. if (!empty($session_id)) {
  420. $session_id = intval($session_id);
  421. } else {
  422. $session_id = api_get_session_id();
  423. }
  424. $status = ($status == STUDENT || $status == COURSEMANAGER) ? $status : STUDENT;
  425. //$role_id = ($status == COURSEMANAGER) ? COURSE_ADMIN : NORMAL_COURSE_MEMBER;
  426. // A preliminary check whether the user has bben already registered on the platform.
  427. if (Database::num_rows(@Database::query("SELECT status FROM ".Database::get_main_table(TABLE_MAIN_USER)."
  428. WHERE user_id = '$user_id' ")) == 0) {
  429. return false; // The user has not been registered to the platform.
  430. }
  431. // Check whether the user has not been already subscribed to the course.
  432. if (empty($session_id)) {
  433. if (Database::num_rows(@Database::query("SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  434. WHERE user_id = '$user_id' AND relation_type<>".COURSE_RELATION_TYPE_RRHH." AND course_code = '$course_code'")) > 0) {
  435. return false; // The user has been already subscribed to the course.
  436. }
  437. }
  438. if (!empty($session_id)) {
  439. // Check whether the user has not already been stored in the session_rel_course_user table
  440. if (Database::num_rows(@Database::query("SELECT * FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)."
  441. WHERE course_code = '".$course_code."'
  442. AND id_session ='".$session_id."'
  443. AND id_user = '".$user_id."'")) > 0) {
  444. return false;
  445. }
  446. // check if the user is registered in the session with other course
  447. $sql = "SELECT id_user FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)." WHERE id_session='".$session_id."' AND id_user='$user_id'";
  448. $rs = Database::query($sql);
  449. if (Database::num_rows($rs) == 0) {
  450. // Check whether the user has not already been stored in the session_rel_user table
  451. if (Database::num_rows(@Database::query("SELECT * FROM ".Database::get_main_table(TABLE_MAIN_SESSION_USER)."
  452. WHERE id_session ='".$session_id."'
  453. AND id_user = '".$user_id."' AND relation_type<>".SESSION_RELATION_TYPE_RRHH." ")) > 0) {
  454. return false;
  455. }
  456. }
  457. // Add him/her in the table session_rel_course_rel_user
  458. @Database::query("INSERT INTO ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)."
  459. SET id_session ='".$session_id."',
  460. course_code = '".$course_code."',
  461. id_user = '".$user_id."'");
  462. // Add him/her in the table session_rel_user
  463. @Database::query("INSERT INTO ".Database::get_main_table(TABLE_MAIN_SESSION_USER)."
  464. SET id_session ='".$session_id."',
  465. id_user = '".$user_id."'");
  466. // Update the table session
  467. $row = Database::fetch_array(@Database::query("SELECT COUNT(*) FROM ".Database::get_main_table(TABLE_MAIN_SESSION_USER)." WHERE id_session = '".$session_id."' AND relation_type<>".SESSION_RELATION_TYPE_RRHH.""));
  468. $count = $row[0]; // number of users by session
  469. $result = @Database::query("UPDATE ".Database::get_main_table(TABLE_MAIN_SESSION)." SET nbr_users = '$count' WHERE id = '".$session_id."'");
  470. // Update the table session_rel_course
  471. $row = Database::fetch_array(@Database::query("SELECT COUNT(*) FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)." WHERE id_session = '".$session_id."' AND course_code = '$course_code' AND status<>2" ));
  472. $count = $row[0]; // number of users by session
  473. $result = @Database::query("UPDATE ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE)." SET nbr_users = '$count' WHERE id_session = '".$session_id."' AND course_code = '$course_code' ");
  474. } else {
  475. $course_sort = self::userCourseSort($user_id, $course_code);
  476. $result = @Database::query("INSERT INTO ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  477. SET course_code = '$course_code',
  478. user_id = '$user_id',
  479. status = '".$status."',
  480. sort = '". ($course_sort)."'");
  481. // Add event to the system log
  482. event_system(LOG_SUBSCRIBE_USER_TO_COURSE, LOG_COURSE_CODE, $course_code, api_get_utc_datetime(), api_get_user_id());
  483. $user_info = api_get_user_info($user_id);
  484. event_system(LOG_SUBSCRIBE_USER_TO_COURSE, LOG_USER_OBJECT, $user_info, api_get_utc_datetime(), api_get_user_id());
  485. }
  486. return (bool)$result;
  487. }
  488. /**
  489. * Get the course id based on the original id and field name in the extra fields. Returns 0 if course was not found
  490. *
  491. * @param string Original course id
  492. * @param string Original field name
  493. * @return int Course id
  494. * @assert ('', '') === false
  495. */
  496. public static function get_course_code_from_original_id($original_course_id_value, $original_course_id_name) {
  497. $t_cfv = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  498. $table_field = Database::get_main_table(TABLE_MAIN_COURSE_FIELD);
  499. $sql = "SELECT course_code FROM $table_field cf INNER JOIN $t_cfv cfv ON cfv.field_id=cf.id
  500. WHERE field_variable='$original_course_id_name' AND field_value='$original_course_id_value'";
  501. $res = Database::query($sql);
  502. $row = Database::fetch_object($res);
  503. if ($row) {
  504. return $row->course_code;
  505. } else {
  506. return 0;
  507. }
  508. }
  509. /**
  510. * Gets the course code from the course id. Returns null if course id was not found
  511. *
  512. * @param int Course id
  513. * @return string Course code
  514. * @assert ('') === false
  515. */
  516. public static function get_course_code_from_course_id($id)
  517. {
  518. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  519. $id = intval($id);
  520. $sql = "SELECT code FROM $table WHERE id = '$id' ";
  521. $res = Database::query($sql);
  522. $row = Database::fetch_object($res);
  523. if ($row) {
  524. return $row->code;
  525. } else {
  526. return null;
  527. }
  528. }
  529. /**
  530. * Subscribe a user $user_id to a course $course_code.
  531. * @author Hugues Peeters
  532. * @author Roan Embrechts
  533. *
  534. * @param int $user_id the id of the user
  535. * @param string $course_code the course code
  536. * @param string $status (optional) The user's status in the course
  537. *
  538. * @return boolean true if subscription succeeds, boolean false otherwise.
  539. * @assert ('', '') === false
  540. */
  541. public static function add_user_to_course($user_id, $course_code, $status = STUDENT)
  542. {
  543. $debug = false;
  544. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  545. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  546. $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  547. $status = ($status == STUDENT || $status == COURSEMANAGER) ? $status : STUDENT;
  548. if (empty($user_id) || empty($course_code) || ($user_id != strval(intval($user_id)))) {
  549. return false;
  550. }
  551. $course_code = Database::escape_string($course_code);
  552. // Check in advance whether the user has already been registered on the platform.
  553. $sql = "SELECT status FROM ".$user_table." WHERE user_id = '$user_id' ";
  554. if (Database::num_rows(Database::query($sql)) == 0) {
  555. if ($debug) error_log('The user has not been registered to the platform');
  556. return false; // The user has not been registered to the platform.
  557. }
  558. // Check whether the user has already been subscribed to this course.
  559. $sql = "SELECT * FROM $course_user_table
  560. WHERE
  561. user_id = '$user_id' AND
  562. relation_type<>".COURSE_RELATION_TYPE_RRHH." AND
  563. course_code = '$course_code'";
  564. if (Database::num_rows(Database::query($sql)) > 0) {
  565. if ($debug) error_log('The user has been already subscribed to the course');
  566. return false; // The user has been subscribed to the course.
  567. }
  568. // Check in advance whether subscription is allowed or not for this course.
  569. $sql = "SELECT code, visibility FROM $course_table
  570. WHERE code = '$course_code' AND subscribe = '".SUBSCRIBE_NOT_ALLOWED."'";
  571. if (Database::num_rows(Database::query($sql)) > 0) {
  572. if ($debug) error_log('Subscription is not allowed for this course');
  573. return false; // Subscription is not allowed for this course.
  574. }
  575. // Ok, subscribe the user.
  576. $max_sort = api_max_sort_value('0', $user_id);
  577. return (bool)Database::query("INSERT INTO ".$course_user_table."
  578. SET course_code = '$course_code',
  579. user_id = '$user_id',
  580. status = '".$status."',
  581. sort = '". ($max_sort + 1)."'");
  582. }
  583. /**
  584. * Checks wether a parameter exists.
  585. * If it doesn't, the function displays an error message.
  586. *
  587. * @return true if parameter is set and not empty, false otherwise
  588. * @todo move function to better place, main_api ?
  589. */
  590. public static function check_parameter($parameter, $error_message) {
  591. if (empty($parameter)) {
  592. Display::display_normal_message($error_message);
  593. return false;
  594. }
  595. return true;
  596. }
  597. /**
  598. * Lets the script die when a parameter check fails.
  599. * @todo move function to better place, main_api ?
  600. */
  601. public static function check_parameter_or_fail($parameter, $error_message) {
  602. if (!self::check_parameter($parameter, $error_message)) {
  603. die();
  604. }
  605. }
  606. /**
  607. * @return true if there already are one or more courses
  608. * with the same code OR visual_code (visualcode), false otherwise
  609. */
  610. public static function course_code_exists($wanted_course_code) {
  611. $wanted_course_code = Database::escape_string($wanted_course_code);
  612. $sql = "SELECT COUNT(*) as number FROM ".Database::get_main_table(TABLE_MAIN_COURSE)." WHERE code = '$wanted_course_code' OR visual_code = '$wanted_course_code'";
  613. $result = Database::fetch_array(Database::query($sql));
  614. return $result['number'] > 0;
  615. }
  616. /**
  617. * @return an array with the course info of all real courses on the platform
  618. */
  619. public static function get_real_course_list() {
  620. $sql_result = Database::query("SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE)." WHERE target_course_code IS NULL");
  621. $real_course_list = array();
  622. while ($result = Database::fetch_array($sql_result)) {
  623. $real_course_list[$result['code']] = $result;
  624. }
  625. return $real_course_list;
  626. }
  627. /**
  628. * Lists all virtual courses
  629. * @return array Course info (course code => details) of all virtual courses on the platform
  630. * @deprecated virtual course feature is not supported
  631. */
  632. public static function get_virtual_course_list() {
  633. $sql_result = Database::query("SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE)." WHERE target_course_code IS NOT NULL");
  634. $virtual_course_list = array();
  635. while ($result = Database::fetch_array($sql_result)) {
  636. $virtual_course_list[$result['code']] = $result;
  637. }
  638. return $virtual_course_list;
  639. }
  640. /**
  641. * Returns an array with the course info of the real courses of which
  642. * the current user is course admin
  643. * @return array A list of courses details for courses to which the user is subscribed as course admin (status = 1)
  644. */
  645. public static function get_real_course_list_of_user_as_course_admin($user_id) {
  646. $result_array = array();
  647. if ($user_id != strval(intval($user_id))) {
  648. return $result_array;
  649. }
  650. $sql_result = Database::query("SELECT *
  651. FROM ".Database::get_main_table(TABLE_MAIN_COURSE)." course
  652. LEFT JOIN ".Database::get_main_table(TABLE_MAIN_COURSE_USER)." course_user
  653. ON course.code = course_user.course_code
  654. WHERE course.target_course_code IS NULL
  655. AND course_user.user_id = '$user_id'
  656. AND course_user.status = '1'");
  657. if ($sql_result === false) { return $result_array; }
  658. while ($result = Database::fetch_array($sql_result)) {
  659. $result_array[] = $result;
  660. }
  661. return $result_array;
  662. }
  663. /**
  664. * Get course list as coach
  665. *
  666. * @param int $user_id
  667. * @param bool $include_courses_in_sessions
  668. * @return array Course list
  669. *
  670. **/
  671. public static function get_course_list_as_coach($user_id, $include_courses_in_sessions = false)
  672. {
  673. // 1. Getting courses as teacher (No session)
  674. $courses_temp = CourseManager::get_course_list_of_user_as_course_admin($user_id);
  675. $courseList = array();
  676. if (!empty($courses_temp)) {
  677. foreach ($courses_temp as $course_item) {
  678. $courseList[0][$course_item['course_code']] = $course_item['course_code'];
  679. }
  680. }
  681. //2. Include courses in sessions
  682. if ($include_courses_in_sessions) {
  683. $sessions = Tracking::get_sessions_coached_by_user($user_id);
  684. if (!empty($sessions)) {
  685. foreach($sessions as $session_item) {
  686. $courses = Tracking :: get_courses_followed_by_coach($user_id, $session_item['id']);
  687. if (is_array($courses)) {
  688. foreach($courses as $course_item) {
  689. $courseList[$session_item['id']][$course_item] = $course_item;
  690. }
  691. }
  692. }
  693. }
  694. }
  695. return $courseList;
  696. }
  697. /**
  698. * @param int $user_id
  699. * @param bool $include_sessions
  700. * @return array
  701. */
  702. public static function get_user_list_from_courses_as_coach($user_id, $include_sessions = true)
  703. {
  704. $students_in_courses = array();
  705. $sessions = CourseManager::get_course_list_as_coach($user_id, true);
  706. if (!empty($sessions)) {
  707. foreach($sessions as $session_id => $courses) {
  708. if (!$include_sessions) {
  709. if (!empty($session_id)) {
  710. continue;
  711. }
  712. }
  713. if (empty($session_id)) {
  714. foreach($courses as $course_code) {
  715. $students_in_course = CourseManager::get_user_list_from_course_code($course_code);
  716. foreach($students_in_course as $user_item) {
  717. //Only students
  718. if ($user_item['status_rel'] == STUDENT)
  719. $students_in_courses[$user_item['user_id']] = $user_item['user_id'];
  720. }
  721. }
  722. } else {
  723. $students_in_course = SessionManager::get_users_by_session($session_id, '0');
  724. if (is_array($students_in_course)) {
  725. foreach($students_in_course as $user_item) {
  726. $students_in_courses[$user_item['user_id']] = $user_item['user_id'];
  727. }
  728. }
  729. }
  730. }
  731. }
  732. $students = Tracking :: get_student_followed_by_coach($user_id);
  733. if (!empty($students_in_courses)) {
  734. if (!empty($students)) {
  735. $students = array_merge($students, $students_in_courses);
  736. } else {
  737. $students = $students_in_courses;
  738. }
  739. }
  740. if (!empty($students)) {
  741. $students = array_unique($students);
  742. }
  743. return $students;
  744. }
  745. /**
  746. * @param int $user_id
  747. * @return an array with the course info of all the courses (real and virtual)
  748. * of which the current user is course admin.
  749. */
  750. public static function get_course_list_of_user_as_course_admin($user_id)
  751. {
  752. if ($user_id != strval(intval($user_id))) {
  753. return array();
  754. }
  755. // Definitions database tables and variables
  756. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  757. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  758. $user_id = intval($user_id);
  759. $data = array();
  760. $sql = "SELECT
  761. course_rel_user.course_code, course.title, course.id, course.db_name, course.id as real_id
  762. FROM $tbl_course_user as course_rel_user
  763. INNER JOIN $tbl_course as course
  764. ON course.code = course_rel_user.course_code
  765. WHERE course_rel_user.user_id='$user_id' AND course_rel_user.status='1'
  766. ORDER BY course.title";
  767. if (api_get_multiple_access_url()) {
  768. $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  769. $access_url_id = api_get_current_access_url_id();
  770. if ($access_url_id != -1) {
  771. $sql = "
  772. SELECT course_rel_user.course_code, course.title, course.id, course.db_name, course.id as real_id
  773. FROM $tbl_course_user as course_rel_user
  774. INNER JOIN $tbl_course as course
  775. ON course.code = course_rel_user.course_code
  776. INNER JOIN $tbl_course_rel_access_url course_rel_url
  777. ON (course_rel_url.course_code= course.code)
  778. WHERE
  779. access_url_id = $access_url_id AND
  780. course_rel_user.user_id='$user_id' AND
  781. course_rel_user.status='1'
  782. ORDER BY course.title";
  783. }
  784. }
  785. $result_nb_cours = Database::query($sql);
  786. if (Database::num_rows($result_nb_cours) > 0) {
  787. while ($row = Database::fetch_array($result_nb_cours, 'ASSOC')) {
  788. $data[$row['course_code']] = $row;
  789. }
  790. }
  791. return $data;
  792. }
  793. /**
  794. * Find out for which courses the user is registered and determine a visual course code and course title from that.
  795. * Takes virtual courses into account
  796. *
  797. * Default case: the name and code stay what they are.
  798. *
  799. * Scenarios:
  800. * - User is registered in real course and virtual courses; name / code become a mix of all
  801. * - User is registered in real course only: name stays that of real course
  802. * - User is registered in virtual course only: name becomes that of virtual course
  803. * - user is not registered to any of the real/virtual courses: name stays that of real course
  804. * (I'm not sure about the last case, but this seems not too bad)
  805. *
  806. * @author Roan Embrechts
  807. * @param $user_id, the id of the user
  808. * @param $course_info, an array with course info that you get using Database::get_course_info($course_system_code);
  809. * @return an array with indices
  810. * $return_result['title'] - the course title of the combined courses
  811. * $return_result['code'] - the course code of the combined courses
  812. * @deprecated use api_get_course_info()
  813. */
  814. public static function determine_course_title_from_course_info($user_id, $course_info) {
  815. if ($user_id != strval(intval($user_id))) {
  816. return array();
  817. }
  818. $real_course_id = $course_info['system_code'];
  819. $real_course_info = Database::get_course_info($real_course_id);
  820. $real_course_name = $real_course_info['title'];
  821. $real_course_visual_code = $real_course_info['visual_code'];
  822. $real_course_real_code = Database::escape_string($course_info['system_code']);
  823. //is the user registered in the real course?
  824. $result = Database::fetch_array(Database::query("SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  825. WHERE user_id = '$user_id' AND relation_type<>".COURSE_RELATION_TYPE_RRHH." AND course_code = '$real_course_real_code'"));
  826. $user_is_registered_in_real_course = !empty($result);
  827. //get a list of virtual courses linked to the current real course and to which the current user is subscribed
  828. $user_subscribed_virtual_course_list = self::get_list_of_virtual_courses_for_specific_user_and_real_course($user_id, $real_course_id);
  829. $virtual_courses_exist = count($user_subscribed_virtual_course_list) > 0;
  830. //now determine course code and name
  831. if ($user_is_registered_in_real_course && $virtual_courses_exist) {
  832. $course_info['name'] = self::create_combined_name($user_is_registered_in_real_course, $real_course_name, $user_subscribed_virtual_course_list);
  833. $course_info['official_code'] = self::create_combined_code($user_is_registered_in_real_course, $real_course_visual_code, $user_subscribed_virtual_course_list);
  834. } elseif ($user_is_registered_in_real_course) {
  835. //course name remains real course name
  836. $course_info['name'] = $real_course_name;
  837. $course_info['official_code'] = $real_course_visual_code;
  838. } elseif ($virtual_courses_exist) {
  839. $course_info['name'] = self::create_combined_name($user_is_registered_in_real_course, $real_course_name, $user_subscribed_virtual_course_list);
  840. $course_info['official_code'] = self::create_combined_code($user_is_registered_in_real_course, $real_course_visual_code, $user_subscribed_virtual_course_list);
  841. } else {
  842. //course name remains real course name
  843. $course_info['name'] = $real_course_name;
  844. $course_info['official_code'] = $real_course_visual_code;
  845. }
  846. $return_result['title'] = $course_info['name'];
  847. $return_result['code'] = $course_info['official_code'];
  848. return $return_result;
  849. }
  850. /**
  851. * Create a course title based on all real and virtual courses the user is registered in.
  852. * @param boolean $user_is_registered_in_real_course
  853. * @param string $real_course_name, the title of the real course
  854. * @param array $virtual_course_list, the list of virtual courses
  855. */
  856. public static function create_combined_name($user_is_registered_in_real_course, $real_course_name, $virtual_course_list)
  857. {
  858. $complete_course_name = array();
  859. if ($user_is_registered_in_real_course) {
  860. // Add the real name to the result.
  861. $complete_course_name[] = $real_course_name;
  862. }
  863. // Add course titles of all virtual courses.
  864. foreach ($virtual_course_list as $current_course) {
  865. $complete_course_name[] = $current_course['title'];
  866. }
  867. // 'CombinedCourse' is from course_home language file.
  868. return (($user_is_registered_in_real_course || count($virtual_course_list) > 1) ? get_lang('CombinedCourse').' ' : '').implode(' &amp; ', $complete_course_name);
  869. }
  870. /**
  871. * Create a course code based on all real and virtual courses the user is registered in.
  872. */
  873. public static function create_combined_code($user_is_registered_in_real_course, $real_course_code, $virtual_course_list)
  874. {
  875. $complete_course_code = array();
  876. if ($user_is_registered_in_real_course) {
  877. // Add the real code to the result
  878. $complete_course_code[] = $real_course_code;
  879. }
  880. // Add codes of all virtual courses.
  881. foreach ($virtual_course_list as $current_course) {
  882. $complete_course_code[] = $current_course['visual_code'];
  883. }
  884. return implode(' &amp; ', $complete_course_code);
  885. }
  886. /**
  887. * Return course info array of virtual course
  888. *
  889. * Note this is different from getting information about a real course!
  890. *
  891. * @param $real_course_code, the id of the real course which the virtual course is linked to
  892. * @deprecated virtual courses doesn't exist anymore
  893. */
  894. public static function get_virtual_course_info($real_course_code)
  895. {
  896. $sql_result = Database::query("SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE)."
  897. WHERE target_course_code = '".Database::escape_string($real_course_code)."'");
  898. $result = array();
  899. while ($virtual_course = Database::fetch_array($sql_result)) {
  900. $result[] = $virtual_course;
  901. }
  902. return $result;
  903. }
  904. /**
  905. * @param string $system_code, the system code of the course
  906. * @return true if the course is a virtual course, false otherwise
  907. * @deprecated virtual courses doesn't exist anymore
  908. */
  909. public static function is_virtual_course_from_system_code($system_code) {
  910. $result = Database::fetch_array(Database::query("SELECT target_course_code FROM ".Database::get_main_table(TABLE_MAIN_COURSE)."
  911. WHERE code = '".Database::escape_string($system_code)."'"));
  912. return !empty($result['target_course_code']);
  913. }
  914. /**
  915. * Returns whether the course code given is a visual code
  916. * @param string Visual course code
  917. * @return true if the course is a virtual course, false otherwise
  918. * @deprecated virtual courses doesn't exist anymore
  919. */
  920. public static function is_virtual_course_from_visual_code($visual_code) {
  921. $result = Database::fetch_array(Database::query("SELECT target_course_code FROM ".Database::get_main_table(TABLE_MAIN_COURSE)."
  922. WHERE visual_code = '".Database::escape_string($visual_code)."'"));
  923. return !empty($result['target_course_code']);
  924. }
  925. /**
  926. * @return true if the real course has virtual courses that the user is subscribed to, false otherwise
  927. * @deprecated virtual courses doesn't exist anymore
  928. */
  929. public static function has_virtual_courses_from_code($real_course_code, $user_id) {
  930. return count(self::get_list_of_virtual_courses_for_specific_user_and_real_course($user_id, $real_course_code)) > 0;
  931. }
  932. /**
  933. * Return an array of arrays, listing course info of all virtual course
  934. * linked to the real course ID $real_course_code
  935. *
  936. * @param string The id of the real course which the virtual courses are linked to
  937. * @return array List of courses details
  938. * @deprecated virtual courses doesn't exist anymore
  939. */
  940. public static function get_virtual_courses_linked_to_real_course($real_course_code) {
  941. $sql_result = Database::query("SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE)."
  942. WHERE target_course_code = '".Database::get_main_table(TABLE_MAIN_COURSE)."'");
  943. $result_array = array();
  944. while ($result = Database::fetch_array($sql_result)) {
  945. $result_array[] = $result;
  946. }
  947. return $result_array;
  948. }
  949. /**
  950. * This function returns the course code of the real course
  951. * to which a virtual course is linked.
  952. *
  953. * @param the course code of the virtual course
  954. * @return the course code of the real course
  955. */
  956. public static function get_target_of_linked_course($virtual_course_code) {
  957. //get info about the virtual course
  958. $result = Database::fetch_array(Database::query("SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE)."
  959. WHERE code = '".Database::escape_string($virtual_course_code)."'"));
  960. return $result['target_course_code'];
  961. }
  962. /*
  963. USER FUNCTIONS
  964. */
  965. /**
  966. * Check if user is subscribed inside a course
  967. * @param int User id
  968. * @param string Course code, if this parameter is null, it'll check for all courses
  969. * @param bool True for checking inside sessions too, by default is not checked
  970. * @return bool true if the user is registered in the course, false otherwise
  971. */
  972. public static function is_user_subscribed_in_course(
  973. $user_id,
  974. $course_code = null,
  975. $in_a_session = false,
  976. $session_id = null
  977. ) {
  978. $user_id = intval($user_id);
  979. if (empty($session_id)) {
  980. $session_id = api_get_session_id();
  981. } else {
  982. $session_id = intval($session_id);
  983. }
  984. $condition_course = '';
  985. if (isset($course_code)) {
  986. $course_code = Database::escape_string($course_code);
  987. $condition_course = ' AND course_code = "'.$course_code.'" ';
  988. }
  989. $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  990. WHERE user_id = $user_id AND relation_type<>".COURSE_RELATION_TYPE_RRHH." $condition_course ";
  991. $result = Database::fetch_array(Database::query($sql));
  992. if (!empty($result)) {
  993. // The user has been registered in this course.
  994. return true;
  995. }
  996. if (!$in_a_session) {
  997. // The user has not been registered in this course.
  998. return false;
  999. }
  1000. $sql = 'SELECT 1 FROM '.Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER).
  1001. ' WHERE id_user = '.$user_id.' '.$condition_course;
  1002. if (Database::num_rows(Database::query($sql)) > 0) {
  1003. return true;
  1004. }
  1005. $sql = 'SELECT 1 FROM '.Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER).
  1006. ' WHERE id_user = '.$user_id.' AND status=2 '.$condition_course;
  1007. if (Database::num_rows(Database::query($sql)) > 0) {
  1008. return true;
  1009. }
  1010. $sql = 'SELECT 1 FROM '.Database::get_main_table(TABLE_MAIN_SESSION).
  1011. ' WHERE id='.$session_id.' AND id_coach='.$user_id;
  1012. if (Database::num_rows(Database::query($sql)) > 0) {
  1013. return true;
  1014. }
  1015. return false;
  1016. }
  1017. /**
  1018. * Is the user a teacher in the given course?
  1019. *
  1020. * @param $user_id, the id (int) of the user
  1021. * @param $course_code, the course code
  1022. *
  1023. * @return true if the user is a teacher in the course, false otherwise
  1024. */
  1025. public static function is_course_teacher($user_id, $course_code) {
  1026. if ($user_id != strval(intval($user_id))) {
  1027. return false;
  1028. }
  1029. $sql_result = Database::query('SELECT status FROM '.Database::get_main_table(TABLE_MAIN_COURSE_USER).
  1030. ' WHERE course_code="'.Database::escape_string($course_code).'" and user_id="'.$user_id.'"');
  1031. if (Database::num_rows($sql_result) > 0) {
  1032. return Database::result($sql_result, 0, 'status') == 1;
  1033. }
  1034. return false;
  1035. }
  1036. /**
  1037. * Is the user subscribed in the real course or linked courses?
  1038. *
  1039. * @param int the id of the user
  1040. * @param array info about the course (comes from course table, see database lib)
  1041. *
  1042. * @return true if the user is registered in the real course or linked courses, false otherwise
  1043. */
  1044. public static function is_user_subscribed_in_real_or_linked_course($user_id, $course_code, $session_id = '') {
  1045. if ($user_id != strval(intval($user_id))) {
  1046. return false;
  1047. }
  1048. $course_code = Database::escape_string($course_code);
  1049. if ($session_id == '') {
  1050. $result = Database::fetch_array(
  1051. Database::query("SELECT *
  1052. FROM ".Database::get_main_table(TABLE_MAIN_COURSE)." course
  1053. LEFT JOIN ".Database::get_main_table(TABLE_MAIN_COURSE_USER)." course_user
  1054. ON course.code = course_user.course_code
  1055. WHERE
  1056. course_user.user_id = '$user_id' AND
  1057. course_user.relation_type<>".COURSE_RELATION_TYPE_RRHH." AND
  1058. ( course.code = '$course_code' OR target_course_code = '$course_code')"));
  1059. return !empty($result);
  1060. }
  1061. $session_id = intval($session_id);
  1062. // From here we trust session id.
  1063. // Is he/she subscribed to the session's course?
  1064. // A user?
  1065. if (Database::num_rows(Database::query("SELECT id_user
  1066. FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)."
  1067. WHERE id_session='".$session_id."'
  1068. AND id_user='$user_id'"))) {
  1069. return true;
  1070. }
  1071. // A course coach?
  1072. if (Database::num_rows(Database::query("SELECT id_user
  1073. FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)."
  1074. WHERE id_session='".$session_id."'
  1075. AND id_user = '$user_id' AND status = 2
  1076. AND course_code='$course_code'"))) {
  1077. return true;
  1078. }
  1079. // A session coach?
  1080. if (Database::num_rows(Database::query("SELECT id_coach
  1081. FROM ".Database::get_main_table(TABLE_MAIN_SESSION)." AS session
  1082. WHERE session.id='".$session_id."'
  1083. AND id_coach='$user_id'"))) {
  1084. return true;
  1085. }
  1086. return false;
  1087. }
  1088. /**
  1089. * Return user info array of all users registered in the specified real or virtual course
  1090. * This only returns the users that are registered in this actual course, not linked courses.
  1091. * @param string $course_code
  1092. * @param int $session_id
  1093. * @param string $limit
  1094. * @param string $order_by the field to order the users by.
  1095. * Valid values are 'lastname', 'firstname', 'username', 'email', 'official_code' OR a part of a SQL statement
  1096. * that starts with ORDER BY ...
  1097. * @param null $filter_by_status if using the session_id: 0 or 2 (student, coach),
  1098. * if using session_id = 0 STUDENT or COURSEMANAGER
  1099. * @param null $return_count
  1100. * @param bool $add_reports
  1101. * @param bool $resumed_report
  1102. * @param array $extra_field
  1103. * @param array $courseCodeList
  1104. * @param array $userIdList
  1105. * @param string $filterByActive
  1106. * @return array|int
  1107. */
  1108. public static function get_user_list_from_course_code(
  1109. $course_code = null,
  1110. $session_id = 0,
  1111. $limit = null,
  1112. $order_by = null,
  1113. $filter_by_status = null,
  1114. $return_count = null,
  1115. $add_reports = false,
  1116. $resumed_report = false,
  1117. $extra_field = array(),
  1118. $courseCodeList = array(),
  1119. $userIdList = array(),
  1120. $filterByActive = null
  1121. ) {
  1122. // variable initialisation
  1123. $session_id = intval($session_id);
  1124. $course_code = Database::escape_string($course_code);
  1125. $where = array();
  1126. // if the $order_by does not contain 'ORDER BY' we have to check if it is a valid field that can be sorted on
  1127. if (!strstr($order_by,'ORDER BY')) {
  1128. //if (!empty($order_by) AND in_array($order_by, array('lastname', 'firstname', 'username', 'email', 'official_code'))) {
  1129. $order_by = Database::escape_string($order_by);
  1130. if (!empty($order_by)) {
  1131. $order_by = 'ORDER BY '.$order_by;
  1132. } else {
  1133. $order_by = '';
  1134. }
  1135. }
  1136. $filter_by_status_condition = null;
  1137. if (!empty($session_id)) {
  1138. $sql = 'SELECT DISTINCT user.user_id, session_course_user.status as status_session, user.* ';
  1139. if ($return_count) {
  1140. $sql = " SELECT COUNT(user.user_id) as count";
  1141. }
  1142. $sql .= ' FROM '.Database::get_main_table(TABLE_MAIN_USER).' as user ';
  1143. $sql .= ' LEFT JOIN '.Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER).' as session_course_user
  1144. ON user.user_id = session_course_user.id_user
  1145. AND session_course_user.course_code="'.$course_code.'"
  1146. AND session_course_user.id_session = '.$session_id;
  1147. $where[] = ' session_course_user.course_code IS NOT NULL ';
  1148. // 2 = coach
  1149. // 0 = student
  1150. if (isset($filter_by_status)) {
  1151. $filter_by_status = intval($filter_by_status);
  1152. $filter_by_status_condition = " session_course_user.status = $filter_by_status AND ";
  1153. }
  1154. if (SessionManager::orderCourseIsEnabled()) {
  1155. //$order_by = "ORDER BY position";
  1156. }
  1157. } else {
  1158. if ($return_count) {
  1159. $sql = " SELECT COUNT(*) as count";
  1160. if ($resumed_report) {
  1161. //$sql = " SELECT count(field_id) ";
  1162. }
  1163. } else {
  1164. if (empty($course_code)) {
  1165. $sql = 'SELECT DISTINCT course.title, course.code, course_rel_user.status as status_rel, user.user_id, course_rel_user.role, course_rel_user.tutor_id, user.* ';
  1166. } else {
  1167. $sql = 'SELECT DISTINCT course_rel_user.status as status_rel, user.user_id, course_rel_user.role, course_rel_user.tutor_id, user.* ';
  1168. }
  1169. }
  1170. $sql .= ' FROM '.Database::get_main_table(TABLE_MAIN_USER).' as user ';
  1171. $sql .= ' LEFT JOIN '.Database::get_main_table(TABLE_MAIN_COURSE_USER).' as course_rel_user
  1172. ON user.user_id = course_rel_user.user_id AND
  1173. course_rel_user.relation_type <> '.COURSE_RELATION_TYPE_RRHH.' ';
  1174. if (!empty($course_code)) {
  1175. $sql .= ' AND course_rel_user.course_code="'.$course_code.'"';
  1176. } else {
  1177. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  1178. $sql .= " INNER JOIN $course_table course ON course_rel_user.course_code = course.code ";
  1179. }
  1180. $where[] = ' course_rel_user.course_code IS NOT NULL ';
  1181. if (isset($filter_by_status) && $filter_by_status != '') {
  1182. $filter_by_status = intval($filter_by_status);
  1183. $filter_by_status_condition = " course_rel_user.status = $filter_by_status AND ";
  1184. }
  1185. }
  1186. $multiple_access_url = api_get_multiple_access_url();
  1187. if ($multiple_access_url) {
  1188. $sql .= ' LEFT JOIN '.Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER).' au ON (au.user_id = user.user_id) ';
  1189. }
  1190. if ($return_count && $resumed_report) {
  1191. foreach ($extra_field as $extraField) {
  1192. $extraFieldInfo = UserManager::get_extra_field_information_by_name($extraField);
  1193. $sql .= ' LEFT JOIN '.Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES).' as ufv
  1194. ON (user.user_id = ufv.user_id AND (field_id = '.$extraFieldInfo['id'].' OR field_id IS NULL ) )';
  1195. }
  1196. }
  1197. $sql .= ' WHERE '.$filter_by_status_condition.' '.implode(' OR ', $where);
  1198. if ($multiple_access_url) {
  1199. $current_access_url_id = api_get_current_access_url_id();
  1200. $sql .= " AND (access_url_id = $current_access_url_id ) ";
  1201. }
  1202. if ($return_count && $resumed_report) {
  1203. $sql .= ' AND field_id IS NOT NULL GROUP BY field_value ';
  1204. }
  1205. if (!empty($courseCodeList)) {
  1206. $courseCodeList = array_map(array('Database', 'escape_string') , $courseCodeList);
  1207. $courseCodeList = implode('","', $courseCodeList);
  1208. $sql .= ' AND course.code IN ("'.$courseCodeList.'")';
  1209. }
  1210. if (!empty($userIdList)) {
  1211. $userIdList = array_map('intval', $userIdList);
  1212. $userIdList = implode('","', $userIdList);
  1213. $sql .= ' AND user.user_id IN ("'.$userIdList.'")';
  1214. }
  1215. if (isset($filterByActive)) {
  1216. $filterByActive = intval($filterByActive);
  1217. $sql .= ' AND user.active = '.$filterByActive;
  1218. }
  1219. $sql .= ' '.$order_by.' '.$limit;
  1220. $rs = Database::query($sql);
  1221. $users = array();
  1222. $extra_fields = UserManager::get_extra_fields(0, 100, null, null, true, true);
  1223. $counter = 1;
  1224. $count_rows = Database::num_rows($rs);
  1225. if ($return_count && $resumed_report) {
  1226. return $count_rows;
  1227. }
  1228. $table_user_field_value = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  1229. if ($count_rows) {
  1230. while ($user = Database::fetch_array($rs)) {
  1231. if ($return_count) {
  1232. return $user['count'];
  1233. }
  1234. $report_info = array();
  1235. $user_info = $user;
  1236. $user_info['status'] = $user['status'];
  1237. if (isset($user['role'])) {
  1238. $user_info['role'] = $user['role'];
  1239. }
  1240. if (isset($user['tutor_id'])) {
  1241. $user_info['tutor_id'] = $user['tutor_id'];
  1242. }
  1243. if (!empty($session_id)) {
  1244. $user_info['status_session'] = $user['status_session'];
  1245. }
  1246. if ($add_reports) {
  1247. $course_code = $user['code'];
  1248. if ($resumed_report) {
  1249. foreach ($extra_fields as $extra) {
  1250. if (in_array($extra['1'], $extra_field)) {
  1251. $user_data = UserManager::get_extra_user_data_by_field($user['user_id'], $extra['1']);
  1252. break;
  1253. }
  1254. }
  1255. if (empty($user_data[$extra['1']])) {
  1256. $row_key = '-1';
  1257. $name = '-';
  1258. } else {
  1259. $row_key = $user_data[$extra['1']];
  1260. $name = $user_data[$extra['1']];
  1261. }
  1262. $users[$row_key]['extra_'.$extra['1']] = $name;
  1263. $users[$row_key]['training_hours'] += Tracking::get_time_spent_on_the_course($user['user_id'], $course_code, 0);
  1264. $users[$row_key]['count_users'] += $counter;
  1265. $registered_users_with_extra_field = 0;
  1266. if (!empty($name) && $name != '-') {
  1267. $name = Database::escape_string($name);
  1268. $sql = "SELECT count(user_id) as count FROM $table_user_field_value WHERE field_value = '$name'";
  1269. $result_count = Database::query($sql);
  1270. if (Database::num_rows($result_count)) {
  1271. $row_count = Database::fetch_array($result_count);
  1272. $registered_users_with_extra_field = $row_count['count'];
  1273. }
  1274. }
  1275. $users[$row_key]['count_users_registered'] = $registered_users_with_extra_field;
  1276. $users[$row_key]['average_hours_per_user'] = $users[$row_key]['training_hours'] / $users[$row_key]['count_users'];
  1277. $category = Category :: load (null, null, $course_code);
  1278. if (!isset($users[$row_key]['count_certificates'])) {
  1279. $users[$row_key]['count_certificates'] = 0;
  1280. }
  1281. if (isset($category[0]) && $category[0]->is_certificate_available($user['user_id'])) {
  1282. $users[$row_key]['count_certificates']++;
  1283. }
  1284. foreach ($extra_fields as $extra) {
  1285. if ($extra['1'] == 'ruc') {
  1286. continue;
  1287. }
  1288. if (!isset($users[$row_key][$extra['1']])) {
  1289. $user_data = UserManager::get_extra_user_data_by_field($user['user_id'], $extra['1']);
  1290. if (!empty($user_data[$extra['1']])) {
  1291. $users[$row_key][$extra['1']] = $user_data[$extra['1']];
  1292. }
  1293. }
  1294. }
  1295. } else {
  1296. $report_info['course'] = $user['title'];
  1297. $report_info['user'] = api_get_person_name($user['firstname'], $user['lastname']);
  1298. $report_info['time'] = api_time_to_hms(Tracking::get_time_spent_on_the_course($user['user_id'], $course_code, 0));
  1299. $category = Category :: load (null, null, $course_code);
  1300. $report_info['certificate'] = Display::label(get_lang('No'));
  1301. if (isset($category[0]) && $category[0]->is_certificate_available($user['user_id'])) {
  1302. $report_info['certificate'] = Display::label(get_lang('Yes'), 'success');
  1303. }
  1304. $progress = intval(Tracking::get_avg_student_progress($user['user_id'], $course_code, array(), 0));
  1305. $report_info['progress_100'] = $progress == 100 ? Display::label(get_lang('Yes'), 'success') : Display::label(get_lang('No'));
  1306. $report_info['progress'] = $progress."%";
  1307. foreach ($extra_fields as $extra) {
  1308. $user_data = UserManager::get_extra_user_data_by_field($user['user_id'], $extra['1']);
  1309. $report_info[$extra['1']] = $user_data[$extra['1']];
  1310. }
  1311. $users[] = $report_info;
  1312. }
  1313. } else {
  1314. $users[$user['user_id']] = $user_info;
  1315. }
  1316. }
  1317. }
  1318. return $users;
  1319. }
  1320. /**
  1321. * @param bool $resumed_report
  1322. * @param array $extra_field
  1323. * @param array $courseCodeList
  1324. * @param array $userIdList
  1325. * @return array|int
  1326. */
  1327. static function get_count_user_list_from_course_code(
  1328. $resumed_report = false,
  1329. $extra_field = array(),
  1330. $courseCodeList = array(),
  1331. $userIdList = array()
  1332. ) {
  1333. return self::get_user_list_from_course_code(
  1334. null,
  1335. 0,
  1336. null,
  1337. null,
  1338. null,
  1339. true,
  1340. false,
  1341. $resumed_report,
  1342. $extra_field,
  1343. $courseCodeList,
  1344. $userIdList
  1345. );
  1346. }
  1347. /**
  1348. * Gets subscribed users in a course or in a course/session
  1349. *
  1350. * @param string $course_code
  1351. * @param int $session_id
  1352. * @return int
  1353. */
  1354. public static function get_users_count_in_course($course_code, $session_id = 0) {
  1355. // variable initialisation
  1356. $session_id = intval($session_id);
  1357. $course_code = Database::escape_string($course_code);
  1358. $sql = 'SELECT DISTINCT count(*) as count FROM '.Database::get_main_table(TABLE_MAIN_USER).' as user ';
  1359. $where = array();
  1360. if (!empty($session_id)) {
  1361. $sql .= ' LEFT JOIN '.Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER).' as session_course_user
  1362. ON user.user_id = session_course_user.id_user
  1363. AND session_course_user.course_code = "'.$course_code.'"
  1364. AND session_course_user.id_session = '.$session_id;
  1365. $where[] = ' session_course_user.course_code IS NOT NULL ';
  1366. } else {
  1367. $sql .= ' LEFT JOIN '.Database::get_main_table(TABLE_MAIN_COURSE_USER).' as course_rel_user
  1368. ON user.user_id = course_rel_user.user_id AND course_rel_user.relation_type<>'.COURSE_RELATION_TYPE_RRHH.'
  1369. AND course_rel_user.course_code="'.$course_code.'"';
  1370. $where[] = ' course_rel_user.course_code IS NOT NULL ';
  1371. }
  1372. $multiple_access_url = api_get_multiple_access_url();
  1373. if ($multiple_access_url) {
  1374. $sql .= ' LEFT JOIN '.Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER).' au ON (au.user_id = user.user_id) ';
  1375. }
  1376. $sql .= ' WHERE '.implode(' OR ', $where);
  1377. if ($multiple_access_url) {
  1378. $current_access_url_id = api_get_current_access_url_id();
  1379. $sql .= " AND (access_url_id = $current_access_url_id ) ";
  1380. }
  1381. $rs = Database::query($sql);
  1382. $count = 0;
  1383. if (Database::num_rows($rs)) {
  1384. $user = Database::fetch_array($rs);
  1385. $count = $user['count'];
  1386. }
  1387. return $count;
  1388. }
  1389. /**
  1390. * Get a list of coaches of a course and a session
  1391. * @param string Course code
  1392. * @param int Session ID
  1393. * @return array List of users
  1394. */
  1395. public static function get_coach_list_from_course_code($course_code, $session_id) {
  1396. if ($session_id != strval(intval($session_id))) {
  1397. return array();
  1398. }
  1399. $course_code = Database::escape_string($course_code);
  1400. $users = array();
  1401. // We get the coach for the given course in a given session.
  1402. $rs = Database::query('SELECT id_user FROM '.Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER).
  1403. ' WHERE id_session="'.$session_id.'" AND course_code="'.$course_code.'" AND status = 2');
  1404. while ($user = Database::fetch_array($rs)) {
  1405. $user_info = api_get_user_info($user['id_user']);
  1406. $user_info['status'] = $user['status'];
  1407. $user_info['role'] = $user['role'];
  1408. $user_info['tutor_id'] = $user['tutor_id'];
  1409. $user_info['email'] = $user['email'];
  1410. $users[$user['id_user']] = $user_info;
  1411. }
  1412. // We get the session coach.
  1413. $rs = Database::query('SELECT id_coach FROM '.Database::get_main_table(TABLE_MAIN_SESSION).' WHERE id="'.$session_id.'"');
  1414. $session_id_coach = Database::result($rs, 0, 'id_coach');
  1415. $user_info = api_get_user_info($session_id_coach);
  1416. $user_info['status'] = $user['status'];
  1417. $user_info['role'] = $user['role'];
  1418. $user_info['tutor_id'] = $user['tutor_id'];
  1419. $user_info['email'] = $user['email'];
  1420. $users[$session_id_coach] = $user_info;
  1421. return $users;
  1422. }
  1423. /**
  1424. * Return user info array of all users registered in the specified real or virtual course
  1425. * This only returns the users that are registered in this actual course, not linked courses.
  1426. *
  1427. * @param string $course_code
  1428. * @param boolean $with_session
  1429. * @param integer $session_id
  1430. * @param date $date_from
  1431. * @param date $date_to
  1432. * @return array with user id
  1433. */
  1434. public static function get_student_list_from_course_code(
  1435. $course_code,
  1436. $with_session = false,
  1437. $session_id = 0,
  1438. $date_from = null,
  1439. $date_to = null
  1440. ) {
  1441. $session_id = intval($session_id);
  1442. $course_code = Database::escape_string($course_code);
  1443. $students = array();
  1444. if ($session_id == 0) {
  1445. // students directly subscribed to the course
  1446. $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  1447. WHERE course_code = '$course_code' AND status = ".STUDENT;
  1448. $rs = Database::query($sql);
  1449. while ($student = Database::fetch_array($rs)) {
  1450. $students[$student['user_id']] = $student;
  1451. }
  1452. }
  1453. // students subscribed to the course through a session
  1454. if ($with_session) {
  1455. $joinSession = "";
  1456. //Session creation date
  1457. if (!empty($date_from) && !empty($date_to)) {
  1458. $joinSession = "INNER JOIN " . Database::get_main_table(TABLE_MAIN_SESSION) . " s";
  1459. }
  1460. $sql_query = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)." scu
  1461. $joinSession
  1462. WHERE scu.course_code = '$course_code' AND scu.status <> 2";
  1463. if (!empty($date_from) && !empty($date_to)) {
  1464. $date_from = Database::escape_string($date_from);
  1465. $date_to = Database::escape_string($date_to);
  1466. $sql_query .= " AND s.date_start >= '$date_from' AND s.date_end <= '$date_to'";
  1467. }
  1468. if ($session_id != 0) {
  1469. $sql_query .= ' AND scu.id_session = '.$session_id;
  1470. }
  1471. $rs = Database::query($sql_query);
  1472. while($student = Database::fetch_array($rs)) {
  1473. $students[$student['id_user']] = $student;
  1474. }
  1475. }
  1476. return $students;
  1477. }
  1478. /**
  1479. * Return user info array of all teacher-users registered in the specified real or virtual course
  1480. * This only returns the users that are registered in this actual course, not linked courses.
  1481. *
  1482. * @param string $course_code
  1483. * @return array with user id
  1484. */
  1485. public static function get_teacher_list_from_course_code($course_code)
  1486. {
  1487. $course_code = Database::escape_string($course_code);
  1488. $teachers = array();
  1489. $sql = "SELECT DISTINCT u.user_id, u.lastname, u.firstname, u.email, u.username, u.status
  1490. FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)." cu
  1491. INNER JOIN ".Database::get_main_table(TABLE_MAIN_USER)." u
  1492. ON (cu.user_id = u.user_id)
  1493. WHERE cu.course_code = '$course_code' AND
  1494. cu.status = 1 ";
  1495. $rs = Database::query($sql);
  1496. while ($teacher = Database::fetch_array($rs)) {
  1497. $teachers[$teacher['user_id']] = $teacher;
  1498. }
  1499. return $teachers;
  1500. }
  1501. /**
  1502. * Returns a string list of teachers assigned to the given course
  1503. * @param string Course code
  1504. * @param string Separator between teachers names
  1505. * @param bool Whether to add a link to the teacher's profile
  1506. * @return string List of teachers teaching the course
  1507. */
  1508. public static function get_teacher_list_from_course_code_to_string(
  1509. $course_code,
  1510. $separator = self::USER_SEPARATOR,
  1511. $add_link_to_profile = false
  1512. ) {
  1513. $teacher_list = self::get_teacher_list_from_course_code($course_code);
  1514. $teacher_string = '';
  1515. $list = array();
  1516. if (!empty($teacher_list)) {
  1517. foreach($teacher_list as $teacher) {
  1518. $teacher_name = api_get_person_name($teacher['firstname'], $teacher['lastname']);
  1519. if ($add_link_to_profile) {
  1520. $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=get_user_popup&resizable=0&height=300&user_id='.$teacher['user_id'];
  1521. $teacher_name = Display::url($teacher_name, $url, array('class' => 'ajax'));
  1522. }
  1523. $list[]= $teacher_name;
  1524. }
  1525. if (!empty($list)) {
  1526. $teacher_string = array_to_string($list, $separator);
  1527. }
  1528. }
  1529. return $teacher_string;
  1530. }
  1531. /**
  1532. * This function returns information about coachs from a course in session
  1533. * @param int - optional, session id
  1534. * @param string - optional, course code
  1535. * @return array - array containing user_id, lastname, firstname, username
  1536. *
  1537. */
  1538. public static function get_coachs_from_course($session_id=0, $course_code='') {
  1539. if (!empty($session_id)) {
  1540. $session_id = intval($session_id);
  1541. } else {
  1542. $session_id = api_get_session_id();
  1543. }
  1544. if (!empty($course_code)) {
  1545. $course_code = Database::escape_string($course_code);
  1546. } else {
  1547. $course_code = api_get_course_id();
  1548. }
  1549. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  1550. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1551. $coaches = array();
  1552. $sql = "SELECT DISTINCT u.user_id,u.lastname,u.firstname,u.username FROM $tbl_user u,$tbl_session_course_user scu
  1553. WHERE u.user_id = scu.id_user AND scu.id_session = '$session_id' AND scu.course_code = '$course_code' AND scu.status = 2";
  1554. $rs = Database::query($sql);
  1555. if (Database::num_rows($rs) > 0) {
  1556. while ($row = Database::fetch_array($rs)) {
  1557. $coaches[] = $row;
  1558. }
  1559. return $coaches;
  1560. } else {
  1561. return false;
  1562. }
  1563. }
  1564. public static function get_coachs_from_course_to_string($session_id = 0, $course_code = null, $separator = self::USER_SEPARATOR, $add_link_to_profile = false) {
  1565. $coachs_course = self::get_coachs_from_course($session_id, $course_code);
  1566. $course_coachs = array();
  1567. if (is_array($coachs_course)) {
  1568. foreach ($coachs_course as $coach_course) {
  1569. $coach_name = api_get_person_name($coach_course['firstname'], $coach_course['lastname']);
  1570. if ($add_link_to_profile) {
  1571. $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=get_user_popup&resizable=0&height=300&user_id='.$coach_course['user_id'];
  1572. $coach_name = Display::url($coach_name, $url, array('class' => 'ajax'));
  1573. }
  1574. $course_coachs[] = $coach_name;
  1575. }
  1576. }
  1577. $coaches_to_string = null;
  1578. if (is_array($course_coachs) && count($course_coachs)> 0 ) {
  1579. $coaches_to_string = array_to_string($course_coachs, $separator);
  1580. }
  1581. return $coaches_to_string;
  1582. }
  1583. public static function get_coach_list_from_course_code_to_string($course_code, $session_id) {
  1584. $tutor_data = '';
  1585. if ($session_id != 0) {
  1586. $coaches = self::get_email_of_tutor_to_session($session_id, $course_code);
  1587. $coach_list = array();
  1588. foreach ($coaches as $coach) {
  1589. $coach_list[] = $coach['complete_name'];
  1590. }
  1591. if (!empty($coach_list)) {
  1592. $tutor_data = implode(self::USER_SEPARATOR, $coach_list);
  1593. }
  1594. }
  1595. return $tutor_data;
  1596. }
  1597. /**
  1598. * Return user info array of all users registered in the specified course
  1599. * this includes the users of the course itsel and the users of all linked courses.
  1600. *
  1601. * @param array $course_info
  1602. * @return array with user info
  1603. */
  1604. public static function get_real_and_linked_user_list($course_code, $with_sessions = true, $session_id = 0) {
  1605. //get list of virtual courses
  1606. $virtual_course_list = self::get_virtual_courses_linked_to_real_course($course_code);
  1607. $complete_user_list = array();
  1608. //get users from real course
  1609. $user_list = self::get_user_list_from_course_code($course_code, $session_id);
  1610. foreach ($user_list as $this_user) {
  1611. $complete_user_list[] = $this_user;
  1612. }
  1613. //get users from linked courses
  1614. foreach ($virtual_course_list as $this_course) {
  1615. $course_code = $this_course['code'];
  1616. $user_list = self::get_user_list_from_course_code($course_code, $session_id);
  1617. foreach ($user_list as $this_user) {
  1618. $complete_user_list[] = $this_user;
  1619. }
  1620. }
  1621. return $complete_user_list;
  1622. }
  1623. /**
  1624. * Return an array of arrays, listing course info of all courses in the list
  1625. * linked to the real course $real_course_code, to which the user $user_id is subscribed.
  1626. *
  1627. * @param $user_id, the id (int) of the user
  1628. * @param $real_course_code, the id (char) of the real course
  1629. *
  1630. * @return array of course info arrays
  1631. * @deprecated virtual course feature is not supported
  1632. */
  1633. public static function get_list_of_virtual_courses_for_specific_user_and_real_course($user_id, $course_code) {
  1634. $result_array = array();
  1635. if ($user_id != strval(intval($user_id))) {
  1636. return $result_array;
  1637. }
  1638. $course_code = Database::escape_string($course_code);
  1639. $sql = "SELECT *
  1640. FROM ".Database::get_main_table(TABLE_MAIN_COURSE)." course
  1641. LEFT JOIN ".Database::get_main_table(TABLE_MAIN_COURSE_USER)." course_user
  1642. ON course.code = course_user.course_code
  1643. WHERE course.target_course_code = '$course_code' AND course_user.user_id = '$user_id' AND course_user.relation_type<>".COURSE_RELATION_TYPE_RRHH." ";
  1644. $sql_result = Database::query($sql);
  1645. while ($result = Database::fetch_array($sql_result)) {
  1646. $result_array[] = $result;
  1647. }
  1648. return $result_array;
  1649. }
  1650. /**
  1651. * Get the list of groups from the course
  1652. * @param string Course code
  1653. * @param int Session ID (optional)
  1654. * @param boolean get empty groups (optional)
  1655. * @return array List of groups info
  1656. */
  1657. public static function get_group_list_of_course($course_code, $session_id = 0, $in_get_empty_group = 0)
  1658. {
  1659. $course_info = api_get_course_info($course_code);
  1660. if (empty($course_info)) {
  1661. return array();
  1662. }
  1663. $course_id = $course_info['real_id'];
  1664. if (empty($course_id)) {
  1665. return array();
  1666. }
  1667. $group_list = array();
  1668. $session_id != 0 ? $session_condition = ' WHERE g.session_id IN(1,'.intval($session_id).')' : $session_condition = ' WHERE g.session_id = 0';
  1669. if ($in_get_empty_group == 0) {
  1670. // get only groups that are not empty
  1671. $sql = "SELECT DISTINCT g.id, g.name
  1672. FROM ".Database::get_course_table(TABLE_GROUP)." AS g
  1673. INNER JOIN ".Database::get_course_table(TABLE_GROUP_USER)." gu
  1674. ON (g.id = gu.group_id AND g.c_id = $course_id AND gu.c_id = $course_id)
  1675. $session_condition
  1676. ORDER BY g.name";
  1677. } else {
  1678. // get all groups even if they are empty
  1679. $sql = "SELECT g.id, g.name
  1680. FROM ".Database::get_course_table(TABLE_GROUP)." AS g
  1681. $session_condition
  1682. AND c_id = $course_id";
  1683. }
  1684. $result = Database::query($sql);
  1685. while ($group_data = Database::fetch_array($result)) {
  1686. $group_data['userNb'] = GroupManager::number_of_students($group_data['id'], $course_id);
  1687. $group_list[$group_data['id']] = $group_data;
  1688. }
  1689. return $group_list;
  1690. }
  1691. /**
  1692. * Checks all parameters needed to create a virtual course.
  1693. * If they are all set, the virtual course creation procedure is called.
  1694. *
  1695. * Call this function instead of create_virtual_course
  1696. * @param string Course code
  1697. * @param string Course title
  1698. * @param string Wanted course code
  1699. * @param string Course language
  1700. * @param string Course category
  1701. * @return bool True on success, false on error
  1702. * @deprecated virtual course feature is not supported
  1703. */
  1704. public static function attempt_create_virtual_course($real_course_code, $course_title, $wanted_course_code, $course_language, $course_category) {
  1705. //better: create parameter list, check the entire list, when false display errormessage
  1706. self::check_parameter_or_fail($real_course_code, 'Unspecified parameter: real course id.');
  1707. self::check_parameter_or_fail($course_title, 'Unspecified parameter: course title.');
  1708. self::check_parameter_or_fail($wanted_course_code, 'Unspecified parameter: wanted course code.');
  1709. self::check_parameter_or_fail($course_language, 'Unspecified parameter: course language.');
  1710. self::check_parameter_or_fail($course_category, 'Unspecified parameter: course category.');
  1711. return self::create_virtual_course($real_course_code, $course_title, $wanted_course_code, $course_language, $course_category);
  1712. }
  1713. /**
  1714. * This function creates a virtual course.
  1715. * It assumes all parameters have been checked and are not empty.
  1716. * It checks wether a course with the $wanted_course_code already exists.
  1717. *
  1718. * Users of this library should consider this function private,
  1719. * please call attempt_create_virtual_course instead of this one.
  1720. *
  1721. * note: The virtual course 'owner' id (the first course admin) is set to the CURRENT user id.
  1722. * @param string Course code
  1723. * @param string Course title
  1724. * @param string Wanted course code
  1725. * @param string Course language
  1726. * @param string Course category
  1727. * @return true if the course creation succeeded, false otherwise
  1728. * @todo research: expiration date of a course
  1729. * @deprecated virtual course feature is not supported
  1730. */
  1731. public static function create_virtual_course($real_course_code, $course_title, $wanted_course_code, $course_language, $course_category) {
  1732. global $firstExpirationDelay;
  1733. $user_id = api_get_user_id();
  1734. $real_course_info = Database::get_course_info($real_course_code);
  1735. $real_course_code = $real_course_info['system_code'];
  1736. //check: virtual course creation fails if another course has the same
  1737. //code, real or fake.
  1738. if (self::course_code_exists($wanted_course_code)) {
  1739. Display::display_error_message($wanted_course_code.' - '.get_lang('CourseCodeAlreadyExists'));
  1740. return false;
  1741. }
  1742. //add data to course table, course_rel_user
  1743. $course_sys_code = $wanted_course_code;
  1744. $course_screen_code = $wanted_course_code;
  1745. $course_repository = $real_course_info['directory'];
  1746. $course_db_name = $real_course_info['db_name'];
  1747. $responsible_teacher = $real_course_info['tutor_name'];
  1748. $faculty_shortname = $course_category;
  1749. // $course_title = $course_title;
  1750. // $course_language = $course_language;
  1751. $teacher_id = $user_id;
  1752. //HACK ----------------------------------------------------------------
  1753. $expiration_date = time() + $firstExpirationDelay;
  1754. //END HACK ------------------------------------------------------------
  1755. register_course($course_sys_code, $course_screen_code, $course_repository, $course_db_name, $responsible_teacher, $faculty_shortname, $course_title, $course_language, $teacher_id, $expiration_date);
  1756. //above was the normal course creation table update call,
  1757. //now one more thing: fill in the target_course_code field
  1758. Database::query("UPDATE ".Database::get_main_table(TABLE_MAIN_COURSE)." SET target_course_code = '$real_course_code'
  1759. WHERE code = '".Database::escape_string($course_sys_code)."' LIMIT 1 ");
  1760. return true;
  1761. }
  1762. /**
  1763. * Delete a course
  1764. * This function deletes a whole course-area from the platform. When the
  1765. * given course is a virtual course, the database and directory will not be
  1766. * deleted.
  1767. * When the given course is a real course, also all virtual courses refering
  1768. * to the given course will be deleted.
  1769. * Considering the fact that we remove all traces of the course in the main
  1770. * database, it makes sense to remove all tracking as well (if stats databases exist)
  1771. * so that a new course created with this code would not use the remains of an older
  1772. * course.
  1773. *
  1774. * @param string The code of the course to delete
  1775. * @todo When deleting a virtual course: unsubscribe users from that virtual
  1776. * course from the groups in the real course if they are not subscribed in
  1777. * that real course.
  1778. * @todo Remove globals
  1779. */
  1780. public static function delete_course($code)
  1781. {
  1782. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  1783. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1784. //$table_course_class = Database::get_main_table(TABLE_MAIN_COURSE_CLASS);
  1785. $table_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  1786. $table_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1787. $table_course_survey = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY);
  1788. $table_course_survey_question = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  1789. $table_course_survey_question_option= Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  1790. $table_course_rel_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  1791. $table_stats_hotpots = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  1792. $table_stats_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1793. $table_stats_exercises = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  1794. $table_stats_access = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  1795. $table_stats_lastaccess = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  1796. $table_stats_course_access = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1797. $table_stats_online = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  1798. $table_stats_default = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
  1799. $table_stats_downloads = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  1800. $table_stats_links = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LINKS);
  1801. $table_stats_uploads = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_UPLOADS);
  1802. $code = Database::escape_string($code);
  1803. $sql = "SELECT * FROM $table_course WHERE code='".$code."'";
  1804. $res = Database::query($sql);
  1805. if (Database::num_rows($res) == 0) {
  1806. return;
  1807. }
  1808. $this_course = Database::fetch_array($res);
  1809. $count = 0;
  1810. if (api_is_multiple_url_enabled()) {
  1811. require_once api_get_path(LIBRARY_PATH).'urlmanager.lib.php';
  1812. $url_id = 1;
  1813. if (api_get_current_access_url_id() != -1) {
  1814. $url_id = api_get_current_access_url_id();
  1815. }
  1816. UrlManager::delete_url_rel_course($code, $url_id);
  1817. $count = UrlManager::getcountUrlRelCourse($code);
  1818. }
  1819. if ($count == 0) {
  1820. self::create_database_dump($code);
  1821. if (!self::is_virtual_course_from_system_code($code)) {
  1822. // If this is not a virtual course, look for virtual courses that depend on this one, if any
  1823. $virtual_courses = self::get_virtual_courses_linked_to_real_course($code);
  1824. foreach ($virtual_courses as $index => $virtual_course) {
  1825. // Unsubscribe all classes from the virtual course
  1826. /*$sql = "DELETE FROM $table_course_class WHERE course_code='".$virtual_course['code']."'";
  1827. Database::query($sql);*/
  1828. // Unsubscribe all users from the virtual course
  1829. $sql = "DELETE FROM $table_course_user WHERE course_code='".$virtual_course['code']."'";
  1830. Database::query($sql);
  1831. // Delete the course from the sessions tables
  1832. $sql = "DELETE FROM $table_session_course WHERE course_code='".$virtual_course['code']."'";
  1833. Database::query($sql);
  1834. $sql = "DELETE FROM $table_session_course_user WHERE course_code='".$virtual_course['code']."'";
  1835. Database::query($sql);
  1836. // Delete the course from the survey tables
  1837. $sql = "DELETE FROM $table_course_survey WHERE course_code='".$virtual_course['code']."'";
  1838. Database::query($sql);
  1839. /*$sql = "DELETE FROM $table_course_survey_user WHERE db_name='".$virtual_course['db_name']."'";
  1840. Database::query($sql);
  1841. $sql = "DELETE FROM $table_course_survey_reminder WHERE db_name='".$virtual_course['db_name']."'";
  1842. Database::query($sql);*/
  1843. // Delete the course from the stats tables
  1844. $sql = "DELETE FROM $table_stats_hotpots WHERE exe_cours_id = '".$virtual_course['code']."'";
  1845. Database::query($sql);
  1846. $sql = "DELETE FROM $table_stats_attempt WHERE course_code = '".$virtual_course['code']."'";
  1847. Database::query($sql);
  1848. $sql = "DELETE FROM $table_stats_exercises WHERE exe_cours_id = '".$virtual_course['code']."'";
  1849. Database::query($sql);
  1850. $sql = "DELETE FROM $table_stats_access WHERE access_cours_code = '".$virtual_course['code']."'";
  1851. Database::query($sql);
  1852. $sql = "DELETE FROM $table_stats_lastaccess WHERE access_cours_code = '".$virtual_course['code']."'";
  1853. Database::query($sql);
  1854. $sql = "DELETE FROM $table_stats_course_access WHERE course_code = '".$virtual_course['code']."'";
  1855. Database::query($sql);
  1856. $sql = "DELETE FROM $table_stats_online WHERE course = '".$virtual_course['code']."'";
  1857. Database::query($sql);
  1858. $sql = "DELETE FROM $table_stats_default WHERE default_cours_code = '".$virtual_course['code']."'";
  1859. Database::query($sql);
  1860. $sql = "DELETE FROM $table_stats_downloads WHERE down_cours_id = '".$virtual_course['code']."'";
  1861. Database::query($sql);
  1862. $sql = "DELETE FROM $table_stats_links WHERE links_cours_id = '".$virtual_course['code']."'";
  1863. Database::query($sql);
  1864. $sql = "DELETE FROM $table_stats_uploads WHERE upload_cours_id = '".$virtual_course['code']."'";
  1865. Database::query($sql);
  1866. // Delete the course from the course table
  1867. $sql = "DELETE FROM $table_course WHERE code='".$virtual_course['code']."'";
  1868. Database::query($sql);
  1869. }
  1870. $sql = "SELECT * FROM $table_course WHERE code='".$code."'";
  1871. $res = Database::query($sql);
  1872. $course = Database::fetch_array($res);
  1873. $course_tables = get_course_tables();
  1874. //Cleaning c_x tables
  1875. if (!empty($course['id'])) {
  1876. foreach($course_tables as $table) {
  1877. $table = Database::get_course_table($table);
  1878. $sql = "DELETE FROM $table WHERE c_id = {$course['id']} ";
  1879. Database::query($sql);
  1880. }
  1881. }
  1882. $course_dir = api_get_path(SYS_COURSE_PATH).$course['directory'];
  1883. $archive_dir = api_get_path(SYS_ARCHIVE_PATH).$course['directory'].'_'.time();
  1884. if (is_dir($course_dir)) {
  1885. rename($course_dir, $archive_dir);
  1886. }
  1887. }
  1888. // Unsubscribe all classes from the course
  1889. /*$sql = "DELETE FROM $table_course_class WHERE course_code='".$code."'";
  1890. Database::query($sql);*/
  1891. // Unsubscribe all users from the course
  1892. $sql = "DELETE FROM $table_course_user WHERE course_code='".$code."'";
  1893. Database::query($sql);
  1894. // Delete the course from the sessions tables
  1895. $sql = "DELETE FROM $table_session_course WHERE course_code='".$code."'";
  1896. Database::query($sql);
  1897. $sql = "DELETE FROM $table_session_course_user WHERE course_code='".$code."'";
  1898. Database::query($sql);
  1899. // Delete from Course - URL
  1900. $sql = "DELETE FROM $table_course_rel_url WHERE course_code = '".$code."'";
  1901. Database::query($sql);
  1902. $sql = 'SELECT survey_id FROM '.$table_course_survey.' WHERE course_code="'.$code.'"';
  1903. $result_surveys = Database::query($sql);
  1904. while($surveys = Database::fetch_array($result_surveys)) {
  1905. $survey_id = $surveys[0];
  1906. $sql = 'DELETE FROM '.$table_course_survey_question.' WHERE survey_id="'.$survey_id.'"';
  1907. Database::query($sql);
  1908. $sql = 'DELETE FROM '.$table_course_survey_question_option.' WHERE survey_id="'.$survey_id.'"';
  1909. Database::query($sql);
  1910. $sql = 'DELETE FROM '.$table_course_survey.' WHERE survey_id="'.$survey_id.'"';
  1911. Database::query($sql);
  1912. }
  1913. // Delete the course from the stats tables
  1914. $sql = "DELETE FROM $table_stats_hotpots WHERE exe_cours_id = '".$code."'";
  1915. Database::query($sql);
  1916. $sql = "DELETE FROM $table_stats_attempt WHERE course_code = '".$code."'";
  1917. Database::query($sql);
  1918. $sql = "DELETE FROM $table_stats_exercises WHERE exe_cours_id = '".$code."'";
  1919. Database::query($sql);
  1920. $sql = "DELETE FROM $table_stats_access WHERE access_cours_code = '".$code."'";
  1921. Database::query($sql);
  1922. $sql = "DELETE FROM $table_stats_lastaccess WHERE access_cours_code = '".$code."'";
  1923. Database::query($sql);
  1924. $sql = "DELETE FROM $table_stats_course_access WHERE course_code = '".$code."'";
  1925. Database::query($sql);
  1926. $sql = "DELETE FROM $table_stats_online WHERE course = '".$code."'";
  1927. Database::query($sql);
  1928. $sql = "DELETE FROM $table_stats_default WHERE default_cours_code = '".$code."'";
  1929. Database::query($sql);
  1930. $sql = "DELETE FROM $table_stats_downloads WHERE down_cours_id = '".$code."'";
  1931. Database::query($sql);
  1932. $sql = "DELETE FROM $table_stats_links WHERE links_cours_id = '".$code."'";
  1933. Database::query($sql);
  1934. $sql = "DELETE FROM $table_stats_uploads WHERE upload_cours_id = '".$code."'";
  1935. Database::query($sql);
  1936. // Delete the course from the database
  1937. $sql = "DELETE FROM $table_course WHERE code='".$code."'";
  1938. Database::query($sql);
  1939. // delete extra course fields
  1940. $t_cf = Database::get_main_table(TABLE_MAIN_COURSE_FIELD);
  1941. $t_cfv = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  1942. $sql = "SELECT distinct field_id FROM $t_cfv WHERE course_code = '$code'";
  1943. $res_field_ids = @Database::query($sql);
  1944. while($row_field_id = Database::fetch_row($res_field_ids)){
  1945. $field_ids[] = $row_field_id[0];
  1946. }
  1947. //delete from table_course_field_value from a given course_code
  1948. $sql_course_field_value = "DELETE FROM $t_cfv WHERE course_code = '$code'";
  1949. @Database::query($sql_course_field_value);
  1950. $sql = "SELECT distinct field_id FROM $t_cfv";
  1951. $res_field_all_ids = @Database::query($sql);
  1952. while($row_field_all_id = Database::fetch_row($res_field_all_ids)){
  1953. $field_all_ids[] = $row_field_all_id[0];
  1954. }
  1955. if (is_array($field_ids) && count($field_ids) > 0) {
  1956. foreach ($field_ids as $field_id) {
  1957. // check if field id is used into table field value
  1958. if (is_array($field_all_ids)) {
  1959. if (in_array($field_id, $field_all_ids)) {
  1960. continue;
  1961. } else {
  1962. $sql_course_field = "DELETE FROM $t_cf WHERE id = '$field_id'";
  1963. Database::query($sql_course_field);
  1964. }
  1965. }
  1966. }
  1967. }
  1968. // Add event to system log
  1969. $user_id = api_get_user_id();
  1970. event_system(LOG_COURSE_DELETE, LOG_COURSE_CODE, $code, api_get_utc_datetime(), $user_id, $code);
  1971. }
  1972. }
  1973. /**
  1974. * Creates a file called mysql_dump.sql in the course folder
  1975. * @param $course_code The code of the course
  1976. * @todo Implementation for single database
  1977. */
  1978. public static function create_database_dump($course_code) {
  1979. global $_configuration;
  1980. if ($_configuration['single_database']) {
  1981. return;
  1982. }
  1983. $sql_dump = '';
  1984. $course_code = Database::escape_string($course_code);
  1985. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  1986. $sql = "SELECT * FROM $table_course WHERE code = '$course_code'";
  1987. $res = Database::query($sql);
  1988. $course = Database::fetch_array($res);
  1989. $course_tables = get_course_tables();
  1990. if (!empty($course['id'])) {
  1991. //Cleaning c_x tables
  1992. foreach($course_tables as $table) {
  1993. $table = Database::get_course_table($table);
  1994. $sql = "SELECT * FROM $table WHERE c_id = {$course['id']} ";
  1995. $res_table = Database::query($sql);
  1996. while ($row = Database::fetch_array($res_table, 'ASSOC')) {
  1997. $row_to_save = array();
  1998. foreach ($row as $key => $value) {
  1999. $row_to_save[$key] = $key."='".Database::escape_string($row[$key])."'";
  2000. }
  2001. $sql_dump .= "\nINSERT INTO $table SET ".implode(', ', $row_to_save).';';
  2002. }
  2003. }
  2004. }
  2005. if (is_dir(api_get_path(SYS_COURSE_PATH).$course['directory'])) {
  2006. $file_name = api_get_path(SYS_COURSE_PATH).$course['directory'].'/mysql_dump.sql';
  2007. $handle = fopen($file_name, 'a+');
  2008. if ($handle !== false) {
  2009. fwrite($handle, $sql_dump);
  2010. fclose($handle);
  2011. } else {
  2012. //TODO trigger exception in a try-catch
  2013. }
  2014. }
  2015. }
  2016. /**
  2017. * Sort courses for a specific user ??
  2018. * @param int User ID
  2019. * @param string Course code
  2020. * @return int Minimum course order
  2021. * @todo Review documentation
  2022. */
  2023. public static function userCourseSort($user_id, $course_code) {
  2024. if ($user_id != strval(intval($user_id))) {
  2025. return false;
  2026. }
  2027. $course_code = Database::escape_string($course_code);
  2028. $TABLECOURSE = Database::get_main_table(TABLE_MAIN_COURSE);
  2029. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2030. $course_title = Database::result(Database::query('SELECT title FROM '.$TABLECOURSE.' WHERE code="'.$course_code.'"'), 0, 0);
  2031. $sql = 'SELECT course.code as code, course.title as title, cu.sort as sort FROM '.$TABLECOURSUSER.' as cu, '.$TABLECOURSE.' as course
  2032. WHERE course.code = cu.course_code AND user_id = "'.$user_id.'" AND
  2033. cu.relation_type<>'.COURSE_RELATION_TYPE_RRHH.' AND
  2034. user_course_cat = 0
  2035. ORDER BY cu.sort';
  2036. $result = Database::query($sql);
  2037. $course_title_precedent = '';
  2038. $counter = 0;
  2039. $course_found = false;
  2040. $course_sort = 1;
  2041. if (Database::num_rows($result) > 0) {
  2042. while ($courses = Database::fetch_array($result)) {
  2043. if ($course_title_precedent == '') {
  2044. $course_title_precedent = $courses['title'];
  2045. }
  2046. if (api_strcasecmp($course_title_precedent, $course_title) < 0) {
  2047. $course_found = true;
  2048. $course_sort = $courses['sort'];
  2049. if ($counter == 0) {
  2050. $sql = 'UPDATE '.$TABLECOURSUSER.' SET sort = sort+1 WHERE user_id= "'.$user_id.'" AND relation_type<>'.COURSE_RELATION_TYPE_RRHH.' AND user_course_cat="0" AND sort > "'.$course_sort.'"';
  2051. $course_sort++;
  2052. } else {
  2053. $sql = 'UPDATE '.$TABLECOURSUSER.' SET sort = sort+1 WHERE user_id= "'.$user_id.'" AND relation_type<>'.COURSE_RELATION_TYPE_RRHH.' AND user_course_cat="0" AND sort >= "'.$course_sort.'"';
  2054. }
  2055. Database::query($sql);
  2056. break;
  2057. } else {
  2058. $course_title_precedent = $courses['title'];
  2059. }
  2060. $counter++;
  2061. }
  2062. // We must register the course in the beginning of the list
  2063. if (!$course_found) {
  2064. $course_sort = Database::result(Database::query('SELECT min(sort) as min_sort FROM '.$TABLECOURSUSER.' WHERE user_id="'.$user_id.'" AND user_course_cat="0"'), 0, 0);
  2065. Database::query('UPDATE '.$TABLECOURSUSER.' SET sort = sort+1 WHERE user_id= "'.$user_id.'" AND user_course_cat="0"');
  2066. }
  2067. }
  2068. return $course_sort;
  2069. }
  2070. /**
  2071. * create recursively all categories as option of the select passed in parameter.
  2072. *
  2073. * @param HTML_QuickForm_Element $element
  2074. * @param string $defaultCode the option value to select by default (used mainly for edition of courses)
  2075. * @param string $parentCode the parent category of the categories added (default=null for root category)
  2076. * @param string $padding the indent param (you shouldn't indicate something here)
  2077. * @deprecated use the select_ajax solution see admin/course_edit.php
  2078. */
  2079. public static function select_and_sort_categories($element, $defaultCode = null, $parentCode = null , $padding = null)
  2080. {
  2081. require_once api_get_path(LIBRARY_PATH).'course_category.lib.php';
  2082. setCategoriesInForm($element, $defaultCode, $parentCode , $padding);
  2083. }
  2084. /**
  2085. * check if course exists
  2086. * @param string course_code
  2087. * @param string whether to accept virtual course codes or not
  2088. * @return true if exists, false else
  2089. */
  2090. public static function course_exists($course_code, $accept_virtual = false) {
  2091. if ($accept_virtual === true) {
  2092. $sql = 'SELECT 1 FROM '.Database::get_main_table(TABLE_MAIN_COURSE).' WHERE code="'.Database::escape_string($course_code).'" OR visual_code="'.Database::escape_string($course_code).'"';
  2093. } else {
  2094. $sql = 'SELECT 1 FROM '.Database::get_main_table(TABLE_MAIN_COURSE).' WHERE code="'.Database::escape_string($course_code).'"';
  2095. }
  2096. return Database::num_rows(Database::query($sql));
  2097. }
  2098. /**
  2099. * Send an email to tutor after the auth-suscription of a student in your course
  2100. * @author Carlos Vargas <carlos.vargas@dokeos.com>, Dokeos Latino
  2101. * @param int $user_id the id of the user
  2102. * @param string $course_code the course code
  2103. * @param string $send_to_tutor_also
  2104. * @return string we return the message that is displayed when the action is succesfull
  2105. */
  2106. public static function email_to_tutor($user_id, $course_code, $send_to_tutor_also = false) {
  2107. if ($user_id != strval(intval($user_id))) {
  2108. return false;
  2109. }
  2110. $course_code = Database::escape_string($course_code);
  2111. $student = Database::fetch_array(Database::query("SELECT * FROM ".Database::get_main_table(TABLE_MAIN_USER)."
  2112. WHERE user_id='".$user_id."'"));
  2113. $information = self::get_course_information($course_code);
  2114. $name_course = $information['title'];
  2115. $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)." WHERE course_code='".$course_code."'";
  2116. // TODO: Ivan: This is a mistake, please, have a look at it. Intention here is diffcult to be guessed.
  2117. //if ($send_to_tutor_also = true)
  2118. // Proposed change:
  2119. if ($send_to_tutor_also) {
  2120. //
  2121. $sql .= " AND tutor_id=1";
  2122. } else {
  2123. $sql .= " AND status=1";
  2124. }
  2125. $result = Database::query($sql);
  2126. while ($row = Database::fetch_array($result)) {
  2127. $tutor = Database::fetch_array(Database::query("SELECT * FROM ".Database::get_main_table(TABLE_MAIN_USER)."
  2128. WHERE user_id='".$row['user_id']."'"));
  2129. $emailto = $tutor['email'];
  2130. $emailsubject = get_lang('NewUserInTheCourse').': '.$name_course;
  2131. $emailbody = get_lang('Dear').': '. api_get_person_name($tutor['firstname'], $tutor['lastname'])."\n";
  2132. $emailbody .= get_lang('MessageNewUserInTheCourse').': '.$name_course."\n";
  2133. $emailbody .= get_lang('UserName').': '.$student['username']."\n";
  2134. if (api_is_western_name_order()) {
  2135. $emailbody .= get_lang('FirstName').': '.$student['firstname']."\n";
  2136. $emailbody .= get_lang('LastName').': '.$student['lastname']."\n";
  2137. } else {
  2138. $emailbody .= get_lang('LastName').': '.$student['lastname']."\n";
  2139. $emailbody .= get_lang('FirstName').': '.$student['firstname']."\n";
  2140. }
  2141. $emailbody .= get_lang('Email').': '.$student['email']."\n\n";
  2142. $recipient_name = api_get_person_name($tutor['firstname'], $tutor['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  2143. $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  2144. $email_admin = api_get_setting('emailAdministrator');
  2145. $additional_parameters = array(
  2146. 'smsType' => NEW_USER_SUBSCRIBED_COURSE,
  2147. 'userId' => $tutor['user_id'],
  2148. 'userUsername' => $student['username'],
  2149. 'courseCode' => $course_code
  2150. );
  2151. //@api_mail($recipient_name, $emailto, $emailsubject, $emailbody, $sender_name,$email_admin);
  2152. api_mail_html($recipient_name, $emailto, $emailsubject, $emailbody,
  2153. $sender_name, $email_admin, null, null, null, $additional_parameters);
  2154. }
  2155. }
  2156. public static function get_special_course_list() {
  2157. $tbl_course_field = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD);
  2158. $tbl_course_field_value = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  2159. //we filter the courses from the URL
  2160. $join_access_url = $where_access_url='';
  2161. if (api_get_multiple_access_url()) {
  2162. $access_url_id = api_get_current_access_url_id();
  2163. if ($access_url_id != -1) {
  2164. $tbl_url_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  2165. $join_access_url= "LEFT JOIN $tbl_url_course url_rel_course ON url_rel_course.course_code= tcfv.course_code ";
  2166. $where_access_url =" AND access_url_id = $access_url_id ";
  2167. }
  2168. }
  2169. // get course list auto-register
  2170. $sql = "SELECT DISTINCT(tcfv.course_code) FROM $tbl_course_field_value tcfv INNER JOIN $tbl_course_field tcf
  2171. ON tcfv.field_id = tcf.id $join_access_url
  2172. WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 $where_access_url";
  2173. $special_course_result = Database::query($sql);
  2174. $special_course_list = array();
  2175. if (Database::num_rows($special_course_result)>0) {
  2176. $special_course_list = array();
  2177. while ($result_row = Database::fetch_array($special_course_result)) {
  2178. $special_course_list[] = $result_row['course_code'];
  2179. }
  2180. }
  2181. return $special_course_list;
  2182. }
  2183. /**
  2184. * Get list of courses for a given user
  2185. * @param int user ID
  2186. * @param boolean Whether to include courses from session or not
  2187. * @return array List of codes and db names
  2188. * @author isaac flores paz
  2189. */
  2190. public static function get_courses_list_by_user_id($user_id, $include_sessions = false) {
  2191. $user_id = intval($user_id);
  2192. $course_list = array();
  2193. $codes = array();
  2194. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  2195. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2196. $tbl_user_course_category = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  2197. $special_course_list = self::get_special_course_list();
  2198. $with_special_courses = $without_special_courses = '';
  2199. if (!empty($special_course_list)) {
  2200. $sc_string = '"'.implode('","',$special_course_list).'"';
  2201. $with_special_courses = ' course.code IN ('.$sc_string.')';
  2202. $without_special_courses = ' AND course.code NOT IN ('.$sc_string.')';
  2203. }
  2204. if (!empty($with_special_courses)) {
  2205. $sql = "SELECT DISTINCT(course.code), course.id as real_id
  2206. FROM ".$tbl_course_user." course_rel_user
  2207. LEFT JOIN ".$tbl_course." course
  2208. ON course.code = course_rel_user.course_code
  2209. LEFT JOIN ".$tbl_user_course_category." user_course_category
  2210. ON course_rel_user.user_course_cat = user_course_category.id
  2211. WHERE $with_special_courses
  2212. GROUP BY course.code
  2213. ORDER BY user_course_category.sort,course.title,course_rel_user.sort ASC";
  2214. $rs_special_course = Database::query($sql);
  2215. if (Database::num_rows($rs_special_course) > 0) {
  2216. while ($result_row = Database::fetch_array($rs_special_course)) {
  2217. $result_row['special_course'] = 1;
  2218. $course_list[] = $result_row;
  2219. $codes[] = $result_row['real_id'];
  2220. }
  2221. }
  2222. }
  2223. // get course list not auto-register. Use Distinct to avoid multiple
  2224. // entries when a course is assigned to a HRD (DRH) as watcher
  2225. $sql = "SELECT DISTINCT(course.code), course.id as real_id
  2226. FROM $tbl_course course
  2227. INNER JOIN $tbl_course_user cru ON course.code=cru.course_code
  2228. WHERE cru.user_id='$user_id' $without_special_courses";
  2229. $result = Database::query($sql);
  2230. if (Database::num_rows($result)) {
  2231. while ($row = Database::fetch_array($result, 'ASSOC')) {
  2232. $course_list[] = $row;
  2233. $codes[] = $row['real_id'];
  2234. }
  2235. }
  2236. if ($include_sessions === true) {
  2237. $sql = "SELECT DISTINCT(c.code), c.id as real_id
  2238. FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)." s, ".Database::get_main_table(TABLE_MAIN_COURSE)." c
  2239. WHERE id_user = $user_id AND s.course_code=c.code";
  2240. $r = Database::query($sql);
  2241. while ($row = Database::fetch_array($r, 'ASSOC')) {
  2242. if (!in_array($row['real_id'], $codes)) {
  2243. $course_list[] = $row;
  2244. }
  2245. }
  2246. }
  2247. return $course_list;
  2248. }
  2249. /**
  2250. * Get course ID from a given course directory name
  2251. * @param string Course directory (without any slash)
  2252. * @return string Course code, or false if not found
  2253. */
  2254. public static function get_course_id_from_path($path) {
  2255. $path = Database::escape_string(str_replace('.', '', str_replace('/', '', $path)));
  2256. $res = Database::query("SELECT code FROM ".Database::get_main_table(TABLE_MAIN_COURSE)."
  2257. WHERE directory LIKE BINARY '$path'");
  2258. if ($res === false) {
  2259. return false;
  2260. }
  2261. if (Database::num_rows($res) != 1) {
  2262. return false;
  2263. }
  2264. $row = Database::fetch_array($res);
  2265. return $row['code'];
  2266. }
  2267. /**
  2268. * Get course code(s) from visual code
  2269. * @deprecated
  2270. * @param string Visual code
  2271. * @return array List of codes for the given visual code
  2272. */
  2273. public static function get_courses_info_from_visual_code($code) {
  2274. $result = array();
  2275. $sql_result = Database::query("SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE)."
  2276. WHERE visual_code = '".Database::escape_string($code)."'");
  2277. while ($virtual_course = Database::fetch_array($sql_result)) {
  2278. $result[] = $virtual_course;
  2279. }
  2280. return $result;
  2281. }
  2282. /**
  2283. * Get emails of tutors to course
  2284. * @param string Visual code
  2285. * @return array List of emails of tutors to course
  2286. * @author @author Carlos Vargas <carlos.vargas@dokeos.com>, Dokeos Latino
  2287. * */
  2288. public static function get_emails_of_tutors_to_course($code) {
  2289. $list = array();
  2290. $res = Database::query("SELECT user_id FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  2291. WHERE course_code='".Database::escape_string($code)."' AND status=1");
  2292. while ($list_users = Database::fetch_array($res)) {
  2293. $result = Database::query("SELECT * FROM ".Database::get_main_table(TABLE_MAIN_USER)."
  2294. WHERE user_id=".$list_users['user_id']);
  2295. while ($row_user = Database::fetch_array($result)){
  2296. $name_teacher = api_get_person_name($row_user['firstname'], $row_user['lastname']);
  2297. $list[] = array($row_user['email'] => $name_teacher);
  2298. }
  2299. }
  2300. return $list;
  2301. }
  2302. /**
  2303. * Get coaches emails by session
  2304. * @param int session id
  2305. * @param string course code
  2306. * @return array array(email => name_tutor) by coach
  2307. * @author Carlos Vargas <carlos.vargas@dokeos.com>
  2308. */
  2309. public static function get_email_of_tutor_to_session($session_id, $course_code)
  2310. {
  2311. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2312. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  2313. $coachs_emails = array();
  2314. $course_code = Database::escape_string($course_code);
  2315. $session_id = intval($session_id);
  2316. $sql = "SELECT id_user FROM $tbl_session_course_user
  2317. WHERE id_session='$session_id' AND course_code='$course_code' AND status = 2";
  2318. $rs = Database::query($sql);
  2319. if (Database::num_rows($rs) > 0) {
  2320. $user_ids = array();
  2321. while ($row = Database::fetch_array($rs)) {
  2322. $user_ids[] = $row['id_user'];
  2323. }
  2324. $sql = "SELECT firstname, lastname, email FROM $tbl_user
  2325. WHERE user_id IN (".implode(",",$user_ids).")";
  2326. $rs_user = Database::query($sql);
  2327. while ($row_emails = Database::fetch_array($rs_user)) {
  2328. $mail_tutor = array(
  2329. 'email' => $row_emails['email'],
  2330. 'complete_name' => api_get_person_name($row_emails['firstname'], $row_emails['lastname'])
  2331. );
  2332. $coachs_emails[] = $mail_tutor;
  2333. }
  2334. }
  2335. return $coachs_emails;
  2336. }
  2337. /**
  2338. * Creates a new extra field for a given course
  2339. * @param string Field's internal variable name
  2340. * @param int Field's type
  2341. * @param string Field's language var name
  2342. * @return int new extra field id
  2343. */
  2344. public static function create_course_extra_field($fieldvarname, $fieldtype, $fieldtitle)
  2345. {
  2346. // database table definition
  2347. $t_cfv = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  2348. $t_cf = Database::get_main_table(TABLE_MAIN_COURSE_FIELD);
  2349. $fieldvarname = Database::escape_string($fieldvarname);
  2350. $fieldtitle = Database::escape_string($fieldtitle);
  2351. $fieldtype = (int)$fieldtype;
  2352. $time = time();
  2353. $sql_field = "SELECT id FROM $t_cf WHERE field_variable = '$fieldvarname'";
  2354. $res_field = Database::query($sql_field);
  2355. $r_field = Database::fetch_row($res_field);
  2356. if (Database::num_rows($res_field) > 0) {
  2357. return $r_field[0];
  2358. }
  2359. // save new fieldlabel into course_field table
  2360. $sql = "SELECT MAX(field_order) FROM $t_cf";
  2361. $res = Database::query($sql);
  2362. $order = 0;
  2363. if (Database::num_rows($res) > 0) {
  2364. $row = Database::fetch_row($res);
  2365. $order = $row[0] + 1;
  2366. }
  2367. $sql = "INSERT INTO $t_cf SET
  2368. field_type = '$fieldtype',
  2369. field_variable = '$fieldvarname',
  2370. field_display_text = '$fieldtitle',
  2371. field_order = '$order',
  2372. tms = FROM_UNIXTIME($time)";
  2373. Database::query($sql);
  2374. return Database::insert_id();
  2375. }
  2376. /**
  2377. * Updates course attribute. Note that you need to check that your attribute is valid before you use this function
  2378. *
  2379. * @param int Course id
  2380. * @param string Attribute name
  2381. * @param string Attribute value
  2382. * @return bool True if attribute was successfully updated, false if course was not found or attribute name is invalid
  2383. */
  2384. public static function update_attribute($id, $name, $value)
  2385. {
  2386. $id = (int)$id;
  2387. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  2388. $sql = "UPDATE $table SET $name = '".Database::escape_string($value)."' WHERE id = '$id';";
  2389. return Database::query($sql);
  2390. }
  2391. /**
  2392. * Update course attributes. Will only update attributes with a non-empty value. Note that you NEED to check that your attributes are valid before using this function
  2393. *
  2394. * @param int Course id
  2395. * @param array Associative array with field names as keys and field values as values
  2396. * @return bool True if update was successful, false otherwise
  2397. */
  2398. public static function update_attributes($id, $attributes)
  2399. {
  2400. $id = (int)$id;
  2401. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  2402. $sql = "UPDATE $table SET ";
  2403. $i = 0;
  2404. foreach($attributes as $name => $value) {
  2405. if($value != '') {
  2406. if($i > 0) {
  2407. $sql .= ", ";
  2408. }
  2409. $sql .= " $name = '".Database::escape_string($value)."'";
  2410. $i++;
  2411. }
  2412. }
  2413. $sql .= " WHERE id = '$id';";
  2414. return Database::query($sql);
  2415. }
  2416. /**
  2417. * Update an extra field value for a given course
  2418. * @param integer Course ID
  2419. * @param string Field variable name
  2420. * @param string Field value
  2421. * @return boolean true if field updated, false otherwise
  2422. */
  2423. public static function update_course_extra_field_value($course_code, $fname, $fvalue = '')
  2424. {
  2425. $t_cfv = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  2426. $t_cf = Database::get_main_table(TABLE_MAIN_COURSE_FIELD);
  2427. $fname = Database::escape_string($fname);
  2428. $course_code = Database::escape_string($course_code);
  2429. $fvalues = '';
  2430. if (is_array($fvalue)) {
  2431. foreach ($fvalue as $val) {
  2432. $fvalues .= Database::escape_string($val).';';
  2433. }
  2434. if (!empty($fvalues)) {
  2435. $fvalues = substr($fvalues, 0, -1);
  2436. }
  2437. } else {
  2438. $fvalues = Database::escape_string($fvalue);
  2439. }
  2440. $sqlcf = "SELECT * FROM $t_cf WHERE field_variable='$fname'";
  2441. $rescf = Database::query($sqlcf);
  2442. if (Database::num_rows($rescf) == 1) {
  2443. // Ok, the field exists
  2444. // Check if enumerated field, if the option is available
  2445. $rowcf = Database::fetch_array($rescf);
  2446. $tms = time();
  2447. $sqlcfv = "SELECT * FROM $t_cfv
  2448. WHERE course_code = '$course_code' AND field_id = '".$rowcf['id']."'
  2449. ORDER BY id";
  2450. $rescfv = Database::query($sqlcfv);
  2451. $n = Database::num_rows($rescfv);
  2452. if ($n > 1) {
  2453. //problem, we already have to values for this field and user combination - keep last one
  2454. while ($rowcfv = Database::fetch_array($rescfv)) { // See the TODO note below.
  2455. if ($n > 1) {
  2456. $sqld = "DELETE FROM $t_cfv WHERE id = ".$rowcfv['id'];
  2457. $resd = Database::query($sqld);
  2458. $n--;
  2459. }
  2460. $rowcfv = Database::fetch_array($rescfv);
  2461. if ($rowcfv['field_value'] != $fvalues) {
  2462. $sqlu = "UPDATE $t_cfv SET field_value = '$fvalues', tms = FROM_UNIXTIME($tms)
  2463. WHERE id = ".$rowcfv['id'];
  2464. $resu = Database::query($sqlu);
  2465. return ($resu ? true : false);
  2466. }
  2467. return true;
  2468. /* TODO: Sure exit from the function occures in this "while" cycle.
  2469. Logic should checked. Maybe "if" instead of "while"? It is not clear... */
  2470. }
  2471. } elseif ($n == 1) {
  2472. //we need to update the current record
  2473. $rowcfv = Database::fetch_array($rescfv);
  2474. if ($rowcfv['field_value'] != $fvalues) {
  2475. $sqlu = "UPDATE $t_cfv SET field_value = '$fvalues', tms = FROM_UNIXTIME($tms) WHERE id = ".$rowcfv['id'];
  2476. //error_log('UM::update_extra_field_value: '.$sqlu);
  2477. $resu = Database::query($sqlu);
  2478. return ($resu ? true : false);
  2479. }
  2480. return true;
  2481. } else {
  2482. $sqli = "INSERT INTO $t_cfv (course_code,field_id,field_value,tms) " .
  2483. "VALUES ('$course_code',".$rowcf['id'].",'$fvalues',FROM_UNIXTIME($tms))";
  2484. //error_log('UM::update_extra_field_value: '.$sqli);
  2485. $resi = Database::query($sqli);
  2486. return ($resi ? true : false);
  2487. }
  2488. } else {
  2489. return false; //field not found
  2490. }
  2491. }
  2492. /**
  2493. * Get the course id of an course by the database name
  2494. * @deprecated
  2495. * @param string The database name
  2496. * @return string The course id
  2497. */
  2498. public static function get_course_id_by_database_name($db_name)
  2499. {
  2500. return Database::result(Database::query('SELECT code FROM '.Database::get_main_table(TABLE_MAIN_COURSE).
  2501. ' WHERE db_name="'.Database::escape_string($db_name).'"'), 0, 'code');
  2502. }
  2503. public static function get_session_category_id_by_session_id($session_id)
  2504. {
  2505. return Database::result(Database::query('SELECT sc.id session_category
  2506. FROM '.Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY).' sc
  2507. INNER JOIN '.Database::get_main_table(TABLE_MAIN_SESSION).' s
  2508. ON sc.id=s.session_category_id WHERE s.id="'.Database::escape_string($session_id).'"'),
  2509. 0, 'session_category');
  2510. }
  2511. /**
  2512. * Get the course id of an course by the database name
  2513. * @param string The database name
  2514. * @return string The course id
  2515. */
  2516. public static function get_course_extra_field_list($code)
  2517. {
  2518. $tbl_course_field = Database::get_main_table(TABLE_MAIN_COURSE_FIELD);
  2519. $tbl_course_field_value = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  2520. $sql_field = "SELECT id, field_type, field_variable, field_display_text, field_default_value
  2521. FROM $tbl_course_field WHERE field_visible = '1' ";
  2522. $res_field = Database::query($sql_field);
  2523. $extra_fields = array();
  2524. while($rowcf = Database::fetch_array($res_field)) {
  2525. $extra_field_id = $rowcf['id'];
  2526. $sql_field_value = "SELECT field_value FROM $tbl_course_field_value WHERE course_code = '$code' AND field_id = '$extra_field_id' ";
  2527. $res_field_value = Database::query($sql_field_value);
  2528. if(Database::num_rows($res_field_value) > 0 ) {
  2529. $r_field_value = Database::fetch_row($res_field_value);
  2530. $rowcf['extra_field_value'] = $r_field_value[0];
  2531. }
  2532. $extra_fields[] = $rowcf;
  2533. }
  2534. return $extra_fields;
  2535. }
  2536. /**
  2537. * Gets the value of a course extra field. Returns null if it was not found
  2538. *
  2539. * @param string Name of the extra field
  2540. * @param string Course code
  2541. * @return string Value
  2542. */
  2543. public static function get_course_extra_field_value($field_name, $code)
  2544. {
  2545. $tbl_course_field = Database::get_main_table(TABLE_MAIN_COURSE_FIELD);
  2546. $tbl_course_field_value = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  2547. $sql = "SELECT id FROM $tbl_course_field WHERE field_visible = '1' AND field_variable = '$field_name';";
  2548. $res = Database::query($sql);
  2549. $row = Database::fetch_object($res);
  2550. if(!$row) {
  2551. return null;
  2552. } else {
  2553. $sql_field_value = "SELECT field_value FROM $tbl_course_field_value WHERE course_code = '$code' AND field_id = '{$row->id}';";
  2554. $res_field_value = Database::query($sql_field_value);
  2555. $row_field_value = Database::fetch_object($res_field_value);
  2556. if(!$row_field_value) {
  2557. return null;
  2558. } else {
  2559. return $row_field_value['field_value'];
  2560. }
  2561. }
  2562. }
  2563. /**
  2564. * Get the database name of a course by the code
  2565. * @deprecated
  2566. * @param string The course code
  2567. * @return string The database name
  2568. */
  2569. public static function get_name_database_course($course_code)
  2570. {
  2571. return Database::result(Database::query('SELECT db_name FROM '.Database::get_main_table(TABLE_MAIN_COURSE).
  2572. ' WHERE code="'.Database::escape_string($course_code).'"'), 0, 'db_name');
  2573. }
  2574. /**
  2575. * Lists details of the course description
  2576. * @param array The course description
  2577. * @param string The encoding
  2578. * @param bool If true is displayed if false is hidden
  2579. * @return string The course description in html
  2580. */
  2581. public static function get_details_course_description_html($descriptions, $charset, $action_show = true)
  2582. {
  2583. $data = null;
  2584. if (isset($descriptions) && count($descriptions) > 0) {
  2585. foreach ($descriptions as $description) {
  2586. $data .= '<div class="sectiontitle">';
  2587. if (api_is_allowed_to_edit() && $action_show) {
  2588. //delete
  2589. $data .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;action=delete&amp;description_id='.$description->id.'" onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset)).'\')) return false;">';
  2590. $data .= Display::return_icon('delete.gif', get_lang('Delete'), array('style' => 'vertical-align:middle;float:right;'));
  2591. $data .= '</a> ';
  2592. //edit
  2593. $data .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;description_id='.$description->id.'">';
  2594. $data .= Display::return_icon('edit.png', get_lang('Edit'), array('style' => 'vertical-align:middle;float:right; padding-right:4px;'), ICON_SIZE_SMALL);
  2595. $data .= '</a> ';
  2596. }
  2597. $data .= $description->title;
  2598. $data .= '</div>';
  2599. $data .= '<div class="sectioncomment">';
  2600. $data .= $description->content;
  2601. $data .= '</div>';
  2602. }
  2603. } else {
  2604. $data .= '<em>'.get_lang('ThisCourseDescriptionIsEmpty').'</em>';
  2605. }
  2606. return $data;
  2607. }
  2608. /**
  2609. * Returns the details of a course category
  2610. *
  2611. * @param string Category code
  2612. * @return array Course category
  2613. */
  2614. public static function get_course_category($code) {
  2615. $table_categories = Database::get_main_table(TABLE_MAIN_CATEGORY);
  2616. $sql = "SELECT * FROM $table_categories WHERE code = '$code';";
  2617. return Database::fetch_array(Database::query($sql));
  2618. }
  2619. /**
  2620. * This code creates a select form element to let the user
  2621. * choose a real course to link to.
  2622. *
  2623. * A good non-display library should not use echo statements, but just return text/html
  2624. * so users of the library can choose when to display.
  2625. *
  2626. * We display the course code, but internally store the course id.
  2627. *
  2628. * @param boolean $has_size, true the select tag gets a size element, false it stays a dropdownmenu
  2629. * @param boolean $only_current_user_courses, true only the real courses of which the
  2630. * current user is course admin are displayed, false all real courses are shown.
  2631. * @param string $element_name the name of the select element
  2632. * @return a string containing html code for a form select element.
  2633. * @deprecated Function not in use
  2634. */
  2635. public static function get_real_course_code_select_html($element_name, $has_size = true, $only_current_user_courses = true, $user_id) {
  2636. if ($only_current_user_courses) {
  2637. $real_course_list = self::get_real_course_list_of_user_as_course_admin($user_id);
  2638. } else {
  2639. $real_course_list = self::get_real_course_list();
  2640. }
  2641. if ($has_size) {
  2642. $size_element = "size=\"".SELECT_BOX_SIZE."\"";
  2643. } else {
  2644. $size_element = "";
  2645. }
  2646. $html_code = "<select name=\"$element_name\" $size_element >\n";
  2647. foreach ($real_course_list as $real_course) {
  2648. $course_code = $real_course["code"];
  2649. $html_code .= "<option value=\"".$course_code."\">";
  2650. $html_code .= $course_code;
  2651. $html_code .= "</option>\n";
  2652. }
  2653. $html_code .= "</select>\n";
  2654. return $html_code;
  2655. }
  2656. /**
  2657. * Get count rows of a table inside a course database
  2658. * @param string The table of which the rows should be counted
  2659. * @param int optionally count rows by session id
  2660. * @return int The number of rows in the given table.
  2661. * @deprecated
  2662. */
  2663. public static function count_rows_course_table($table, $session_id = '', $course_id = null) {
  2664. $condition_session = '';
  2665. if ($session_id !== '') {
  2666. $session_id = intval($session_id);
  2667. $condition_session = " AND session_id = '$session_id' ";
  2668. }
  2669. if (!empty($course_id)) {
  2670. $course_id = intval($course_id);
  2671. } else {
  2672. $course_id = api_get_course_int_id();
  2673. }
  2674. $condition_session .= " AND c_id = '$course_id' ";
  2675. $sql = "SELECT COUNT(*) AS n FROM $table WHERE 1=1 $condition_session ";
  2676. $rs = Database::query($sql);
  2677. $row = Database::fetch_row($rs);
  2678. return $row[0];
  2679. }
  2680. /**
  2681. * Subscribes courses to human resource manager (Dashboard feature)
  2682. * @param int Human Resource Manager id
  2683. * @param array Courses code
  2684. * @param int Relation type
  2685. **/
  2686. public static function suscribe_courses_to_hr_manager($hr_manager_id,$courses_list)
  2687. {
  2688. global $_configuration;
  2689. // Database Table Definitions
  2690. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  2691. $tbl_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2692. $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  2693. $hr_manager_id = intval($hr_manager_id);
  2694. $affected_rows = 0;
  2695. //Deleting assigned courses to hrm_id
  2696. if ($_configuration['multiple_access_urls']) {
  2697. $sql = "SELECT s.course_code FROM $tbl_course_rel_user s INNER JOIN $tbl_course_rel_access_url a ON (a.course_code = s.course_code) WHERE user_id = $hr_manager_id AND relation_type=".COURSE_RELATION_TYPE_RRHH." AND access_url_id = ".api_get_current_access_url_id()."";
  2698. } else {
  2699. $sql = "SELECT course_code FROM $tbl_course_rel_user WHERE user_id = $hr_manager_id AND relation_type=".COURSE_RELATION_TYPE_RRHH." ";
  2700. }
  2701. $result = Database::query($sql);
  2702. if (Database::num_rows($result) > 0) {
  2703. while ($row = Database::fetch_array($result)) {
  2704. $sql = "DELETE FROM $tbl_course_rel_user WHERE course_code = '{$row['course_code']}' AND user_id = $hr_manager_id AND relation_type=".COURSE_RELATION_TYPE_RRHH." ";
  2705. Database::query($sql);
  2706. }
  2707. }
  2708. // inserting new courses list
  2709. if (is_array($courses_list)) {
  2710. foreach ($courses_list as $course_code) {
  2711. $course_code = Database::escape_string($course_code);
  2712. $insert_sql = "INSERT IGNORE INTO $tbl_course_rel_user(course_code, user_id, status, relation_type) VALUES('$course_code', $hr_manager_id, '".DRH."', '".COURSE_RELATION_TYPE_RRHH."')";
  2713. Database::query($insert_sql);
  2714. if (Database::affected_rows()) {
  2715. $affected_rows++;
  2716. }
  2717. }
  2718. }
  2719. return $affected_rows;
  2720. }
  2721. /**
  2722. * get courses followed by human resources manager
  2723. * @param int $user_id
  2724. * @param int $from
  2725. * @param int $limit
  2726. * @param string $column
  2727. * @param string $direction
  2728. * @return array courses
  2729. */
  2730. public static function get_courses_followed_by_drh(
  2731. $user_id,
  2732. $from = null,
  2733. $limit = null,
  2734. $column = null,
  2735. $direction = null,
  2736. $getCount = false
  2737. ) {
  2738. return self::getCoursesFollowedByUser(
  2739. $user_id,
  2740. $from,
  2741. $limit,
  2742. $column,
  2743. $direction,
  2744. $getCount
  2745. );
  2746. }
  2747. /**
  2748. * get courses followed by user
  2749. * @param int $user_id
  2750. * @param int $from
  2751. * @param int $limit
  2752. * @param string $column
  2753. * @param string $direction
  2754. * @return array courses
  2755. */
  2756. public static function getCoursesFollowedByUser(
  2757. $user_id,
  2758. $status = null,
  2759. $from = null,
  2760. $limit = null,
  2761. $column = null,
  2762. $direction = null,
  2763. $getCount = false,
  2764. $keyword = null,
  2765. $sessionId = null
  2766. ) {
  2767. // Database Table Definitions
  2768. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  2769. $tbl_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2770. $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  2771. $sessionId = intval($sessionId);
  2772. $user_id = intval($user_id);
  2773. $select = "SELECT DISTINCT *, id as real_id ";
  2774. if ($getCount) {
  2775. $select = "SELECT COUNT(DISTINCT id) as count";
  2776. }
  2777. $whereConditions = null;
  2778. switch ($status) {
  2779. case COURSEMANAGER:
  2780. $whereConditions .= " AND
  2781. cru.user_id = '$user_id' AND
  2782. status = ".COURSEMANAGER."
  2783. ";
  2784. break;
  2785. case DRH:
  2786. $whereConditions .= " AND
  2787. cru.user_id = '$user_id' AND
  2788. status = ".DRH." AND
  2789. relation_type = '".COURSE_RELATION_TYPE_RRHH."'
  2790. ";
  2791. break;
  2792. }
  2793. $keywordCondition = null;
  2794. if (!empty($keyword)) {
  2795. $keyword = Database::escape_string($keyword);
  2796. $keywordCondition = " AND (c.code LIKE '%$keyword%' OR c.title LIKE '%$keyword%' ) ";
  2797. }
  2798. $orderBy = null;
  2799. $extraInnerJoin = null;
  2800. if (!empty($sessionId)) {
  2801. if (!empty($sessionId)) {
  2802. $courseList = SessionManager::get_course_list_by_session_id(
  2803. $sessionId
  2804. );
  2805. if (!empty($courseList)) {
  2806. $courseListToString = implode("','", array_keys($courseList));
  2807. $whereConditions .= " AND c.id IN ('".$courseListToString."')";
  2808. }
  2809. }
  2810. if (SessionManager::orderCourseIsEnabled() && !empty($sessionId)) {
  2811. $tableSessionRelCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  2812. $orderBy = ' ORDER BY position';
  2813. $extraInnerJoin = " INNER JOIN $tableSessionRelCourse src
  2814. ON (c.code = src.course_code AND id_session = $sessionId) ";
  2815. }
  2816. }
  2817. $whereConditions .= $keywordCondition;
  2818. $sql = "$select
  2819. FROM $tbl_course c
  2820. INNER JOIN $tbl_course_rel_user cru ON (cru.course_code = c.code)
  2821. INNER JOIN $tbl_course_rel_access_url a ON (a.course_code = c.code)
  2822. $extraInnerJoin
  2823. WHERE
  2824. access_url_id = ".api_get_current_access_url_id()."
  2825. $whereConditions
  2826. $orderBy
  2827. ";
  2828. if (isset($from) && isset($limit)) {
  2829. $from = intval($from);
  2830. $limit = intval($limit);
  2831. $sql .= " LIMIT $from, $limit";
  2832. }
  2833. $result = Database::query($sql);
  2834. if ($getCount) {
  2835. $row = Database::fetch_array($result);
  2836. return $row['count'];
  2837. }
  2838. $courses = array();
  2839. if (Database::num_rows($result) > 0) {
  2840. while ($row = Database::fetch_array($result)) {
  2841. $courses[$row['code']] = $row;
  2842. }
  2843. }
  2844. return $courses;
  2845. }
  2846. /**
  2847. * check if a course is special (autoregister)
  2848. * @param string course code
  2849. */
  2850. public static function is_special_course($course_code)
  2851. {
  2852. $tbl_course_field_value = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  2853. $tbl_course_field = Database::get_main_table(TABLE_MAIN_COURSE_FIELD);
  2854. $is_special = false;
  2855. $sql = "SELECT course_code
  2856. FROM $tbl_course_field_value tcfv
  2857. INNER JOIN $tbl_course_field tcf ON tcfv.field_id = tcf.id
  2858. WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 AND course_code='$course_code'";
  2859. $result = Database::query($sql);
  2860. $num_rows = Database::num_rows($result);
  2861. if ($num_rows > 0){
  2862. $is_special = true;
  2863. }
  2864. return $is_special;
  2865. }
  2866. /**
  2867. * Update course picture
  2868. * @param string Course code
  2869. * @param string File name
  2870. * @param string The full system name of the image from which course picture will be created.
  2871. * @return bool Returns the resulting. In case of internal error or negative validation returns FALSE.
  2872. */
  2873. public static function update_course_picture($course_code, $filename, $source_file = null)
  2874. {
  2875. $course_info = api_get_course_info($course_code);
  2876. $store_path = api_get_path(SYS_COURSE_PATH).$course_info['path']; // course path
  2877. $course_image = $store_path.'/course-pic.png'; // image name for courses
  2878. $course_medium_image = $store_path.'/course-pic85x85.png';
  2879. //$extension = strtolower(substr(strrchr($filename, '.'), 1));
  2880. if (file_exists($course_image)) {
  2881. @unlink($course_image);
  2882. }
  2883. if (file_exists($course_medium_image)) {
  2884. @unlink($course_medium_image);
  2885. }
  2886. $my_course_image = new Image($source_file);
  2887. $result = $my_course_image->send_image($course_image, -1, 'png');
  2888. //Redimension image to 100x85 (should be 85x85 but 100x85 visually gives
  2889. // better results for most images people put as course icon)
  2890. if ($result) {
  2891. $medium = new Image($course_image);
  2892. //$picture_infos = $medium->get_image_size();
  2893. $medium->resize(100, 85, 0, false);
  2894. $medium->send_image($store_path.'/course-pic85x85.png', -1, 'png');
  2895. }
  2896. return $result;
  2897. }
  2898. /**
  2899. * @deprecated See CourseManager::course_code_exists()
  2900. */
  2901. public static function is_existing_course_code($wanted_course_code) {
  2902. return self::course_code_exists($wanted_course_code);
  2903. }
  2904. /**
  2905. * Builds the course block in user_portal.php
  2906. * @todo use Twig
  2907. */
  2908. public static function course_item_html_no_icon($params)
  2909. {
  2910. $html = '<div class="course_item">';
  2911. $html .= '<div class="row">';
  2912. $html .= '<div class="span7">';
  2913. $notifications = isset($params['notifications']) ? $params['notifications'] : null;
  2914. $html .='<h3>'.$params['title'].$notifications.'</h3> ';
  2915. if (isset($params['description'])) {
  2916. $html .= '<p>'.$params['description'].'</p>';
  2917. }
  2918. if (!empty($params['subtitle'])) {
  2919. $html .= '<small>'.$params['subtitle'].'</small>';
  2920. }
  2921. if (!empty($params['teachers'])) {
  2922. $html .= '<h5>'.Display::return_icon('teacher.png', get_lang('Teacher'), array(), ICON_SIZE_TINY).$params['teachers'].'</h5>';
  2923. }
  2924. if (!empty($params['coaches'])) {
  2925. $html .= '<h5>'.Display::return_icon('teacher.png', get_lang('Coach'), array(), ICON_SIZE_TINY).$params['coaches'].'</h5>';
  2926. }
  2927. $html .= '</div>';
  2928. $params['right_actions'] = isset($params['right_actions']) ? $params['right_actions'] : null;
  2929. $html .= '<div class="span1 pull-right course-box-actions">'.$params['right_actions'].'</div>';
  2930. $html .= '</div>';
  2931. $html .= '</div>';
  2932. return $html;
  2933. }
  2934. /**
  2935. * Builds the course block in user_portal.php
  2936. * @todo use Twig
  2937. */
  2938. public static function course_item_html($params, $is_sub_content = false) {
  2939. $html = '';
  2940. $class = "well course-box";
  2941. if ($is_sub_content) {
  2942. $class = "course_item";
  2943. }
  2944. $html .= '<div class="'.$class.'">';
  2945. $html .= '<div class="row">';
  2946. $html .= '<div class="span7">';
  2947. $html .= ' <div class="row">';
  2948. $html .= '<div class="span1 course-box-thumbnail-box">';
  2949. if (!empty($params['link'])) {
  2950. $html .= '<a class="thumbnail" href="'.$params['link'].'">';
  2951. $html .= $params['icon'];
  2952. $html .= '</a>';
  2953. } else {
  2954. $html .= '<div class="thumbnail">';
  2955. $html .= $params['icon'];
  2956. $html .= '</div>';
  2957. }
  2958. $html .= '</div>';
  2959. $notifications = isset($params['notifications']) ? $params['notifications'] : null;
  2960. $param_class = isset($params['class']) ? $params['class'] : null;
  2961. $html .= '<div class="span6 '.$param_class.'">';
  2962. $html .='<h3>'.$params['title'].$notifications.'</h3> ';
  2963. if (isset($params['description'])) {
  2964. $html .= '<p>'.$params['description'].'</p>';
  2965. }
  2966. if (!empty($params['subtitle'])) {
  2967. $html .= '<small>'.$params['subtitle'].'</small>';
  2968. }
  2969. if (!empty($params['teachers'])) {
  2970. $html .= '<h5>'.Display::return_icon('teacher.png', get_lang('Teacher'), array(), ICON_SIZE_TINY).$params['teachers'].'</h5>';
  2971. }
  2972. if (!empty($params['coaches'])) {
  2973. $html .= '<h5>'.Display::return_icon('teacher.png', get_lang('Coach'), array(), ICON_SIZE_TINY).$params['coaches'].'</h5>';
  2974. }
  2975. $html .= '</div>';
  2976. $html .= '</div>';
  2977. $html .= '</div>';
  2978. $params['right_actions'] = isset($params['right_actions']) ? $params['right_actions'] : null;
  2979. $html .= '<div class="span1 pull-right course-box-actions">'.$params['right_actions'].'</div>';
  2980. $html .= '</div>';
  2981. $html .= '</div>';
  2982. return $html;
  2983. }
  2984. public static function course_item_parent($main_content, $sub_content, $sub_sub_content = null)
  2985. {
  2986. return '<div class="well">'.$main_content.$sub_content.$sub_sub_content.'</div>';
  2987. }
  2988. /**
  2989. * Display special courses (and only these) as several HTML divs of class userportal-course-item
  2990. *
  2991. * Special courses are courses that stick on top of the list and are "auto-registerable"
  2992. * in the sense that any user clicking them is registered as a student
  2993. * @param int User id
  2994. * @param bool Whether to show the document quick-loader or not
  2995. * @return void
  2996. */
  2997. public static function display_special_courses($user_id, $load_dirs = false)
  2998. {
  2999. $user_id = intval($user_id);
  3000. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  3001. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  3002. $special_course_list = self::get_special_course_list();
  3003. $with_special_courses = $without_special_courses = '';
  3004. if (!empty($special_course_list)) {
  3005. $with_special_courses = ' course.code IN ("'.implode('","',$special_course_list).'")';
  3006. }
  3007. $html = null;
  3008. if (!empty($with_special_courses)) {
  3009. $sql = "SELECT course.id, course.code, course.subscribe subscr, course.unsubscribe unsubscr, course_rel_user.status status,
  3010. course_rel_user.sort sort, course_rel_user.user_course_cat user_course_cat, course_rel_user.user_id
  3011. FROM $tbl_course course
  3012. LEFT JOIN $tbl_course_user course_rel_user ON course.code = course_rel_user.course_code AND course_rel_user.user_id = '$user_id'
  3013. WHERE $with_special_courses group by course.code";
  3014. $rs_special_course = Database::query($sql);
  3015. $number_of_courses = Database::num_rows($rs_special_course);
  3016. $key = 0;
  3017. if ($number_of_courses > 0) {
  3018. while ($course = Database::fetch_array($rs_special_course)) {
  3019. $course_info = api_get_course_info($course['code']);
  3020. if ($course_info['visibility'] == COURSE_VISIBILITY_HIDDEN) {
  3021. continue;
  3022. }
  3023. $params = array();
  3024. // Get notifications.
  3025. //$course['id_session'] = null;
  3026. //$course['status'] = $course['status'];
  3027. $course_info['id_session'] = null;
  3028. $course_info['status'] = $course['status'];
  3029. $show_notification = Display::show_notification($course_info);
  3030. if (empty($course['user_id'])) {
  3031. $course['status'] = STUDENT;
  3032. }
  3033. $params['icon'] = Display::return_icon('blackboard.png', api_htmlentities($course_info['title']), array(), ICON_SIZE_LARGE);
  3034. $params['right_actions'] = '';
  3035. if (api_is_platform_admin()) {
  3036. if ($load_dirs) {
  3037. $params['right_actions'] .= '<a id="document_preview_'.$course['real_id'].'_0" class="document_preview" href="javascript:void(0);">'.Display::return_icon('folder.png', get_lang('Documents'), array('align' => 'absmiddle'),ICON_SIZE_SMALL).'</a>';
  3038. $params['right_actions'] .= '<a href="'.api_get_path(WEB_CODE_PATH).'course_info/infocours.php?cidReq='.$course['code'].'">'.Display::return_icon('edit.png', get_lang('Edit'), array('align' => 'absmiddle'),ICON_SIZE_SMALL).'</a>';
  3039. $params['right_actions'] .= Display::div('', array('id' => 'document_result_'.$course['real_id'].'_0', 'class'=>'document_preview_container'));
  3040. } else {
  3041. $params['right_actions'] .= '<a href="'.api_get_path(WEB_CODE_PATH).'course_info/infocours.php?cidReq='.$course['code'].'">'.Display::return_icon('edit.png', get_lang('Edit'), array('align' => 'absmiddle'),ICON_SIZE_SMALL).'</a>';
  3042. }
  3043. if ($course['status'] == COURSEMANAGER) {
  3044. //echo Display::return_icon('teachers.gif', get_lang('Status').': '.get_lang('Teacher'), array('style'=>'width: 11px; height: 11px;'));
  3045. }
  3046. } else {
  3047. if ($course_info['visibility'] != COURSE_VISIBILITY_CLOSED) {
  3048. if ($load_dirs) {
  3049. $params['right_actions'] .= '<a id="document_preview_'.$course['real_id'].'_0" class="document_preview" href="javascript:void(0);">'.Display::return_icon('folder.png', get_lang('Documents'), array('align' => 'absmiddle'),ICON_SIZE_SMALL).'</a>';
  3050. $params['right_actions'] .= Display::div('', array('id' => 'document_result_'.$course['real_id'].'_0', 'class'=>'document_preview_container'));
  3051. }
  3052. }
  3053. }
  3054. if ($course_info['visibility'] != COURSE_VISIBILITY_CLOSED || $course['status'] == COURSEMANAGER) {
  3055. $course_title = '<a href="'.api_get_path(WEB_COURSE_PATH).$course_info['path'].'/?id_session=0&amp;autoreg=1">'.$course_info['title'].'</a>';
  3056. } else {
  3057. $course_title = $course_info['title']." ".Display::tag('span',get_lang('CourseClosed'), array('class'=>'item_closed'));
  3058. }
  3059. if (api_get_setting('display_coursecode_in_courselist') == 'true') {
  3060. $course_title .= ' ('.$course_info['visual_code'].') ';
  3061. }
  3062. if (api_get_setting('display_teacher_in_courselist') == 'true') {
  3063. $params['teachers'] = CourseManager::get_teacher_list_from_course_code_to_string($course['code'], self::USER_SEPARATOR, true);
  3064. }
  3065. $course_title .= '&nbsp;';
  3066. $course_title .= Display::return_icon('klipper.png', get_lang('CourseAutoRegister'));
  3067. $params['title'] = $course_title;
  3068. $params['link'] = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/?id_session=0&amp;autoreg=1';
  3069. if ($course_info['visibility'] != COURSE_VISIBILITY_CLOSED) {
  3070. $params['notifications'] = $show_notification;
  3071. }
  3072. $html .= self::course_item_html($params, false);
  3073. $key++;
  3074. }
  3075. }
  3076. }
  3077. return $html;
  3078. }
  3079. /**
  3080. * Display courses (without special courses) as several HTML divs
  3081. * of course categories, as class userportal-catalog-item.
  3082. * @uses display_courses_in_category() to display the courses themselves
  3083. * @param int user id
  3084. * @param bool Whether to show the document quick-loader or not
  3085. * @return void
  3086. */
  3087. public static function display_courses($user_id, $load_dirs = false)
  3088. {
  3089. $user_id = intval($user_id);
  3090. if (empty($user_id)) {
  3091. $user_id = api_get_user_id();
  3092. }
  3093. // Step 1: We get all the categories of the user
  3094. $tucc = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  3095. $sql = "SELECT id, title FROM $tucc WHERE user_id='".$user_id."' ORDER BY sort ASC";
  3096. $result = Database::query($sql);
  3097. $html = null;
  3098. while ($row = Database::fetch_array($result)) {
  3099. // We simply display the title of the category.
  3100. $params = array(
  3101. 'icon' => Display::return_icon('folder_yellow.png', api_htmlentities($row['title']), array(), ICON_SIZE_LARGE),
  3102. 'title' => $row['title'],
  3103. 'class' => 'table_user_course_category'
  3104. );
  3105. $html .= self::course_item_parent(
  3106. self::course_item_html($params, true),
  3107. self :: display_courses_in_category($row['id'], $load_dirs)
  3108. );
  3109. }
  3110. // Step 2: We display the course without a user category.
  3111. $html .= self :: display_courses_in_category(0, $load_dirs);
  3112. return $html;
  3113. }
  3114. /**
  3115. * Display courses inside a category (without special courses) as HTML dics of
  3116. * class userportal-course-item.
  3117. * @param int User category id
  3118. * @param bool Whether to show the document quick-loader or not
  3119. * @return void
  3120. */
  3121. public static function display_courses_in_category($user_category_id, $load_dirs = false)
  3122. {
  3123. $user_id = api_get_user_id();
  3124. // Table definitions
  3125. $TABLECOURS = Database :: get_main_table(TABLE_MAIN_COURSE);
  3126. $TABLECOURSUSER = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  3127. $TABLE_ACCESS_URL_REL_COURSE = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  3128. $current_url_id = api_get_current_access_url_id();
  3129. // Get course list auto-register
  3130. $special_course_list = self::get_special_course_list();
  3131. $without_special_courses = '';
  3132. if (!empty($special_course_list)) {
  3133. $without_special_courses = ' AND course.code NOT IN ("'.implode('","',$special_course_list).'")';
  3134. }
  3135. //AND course_rel_user.relation_type<>".COURSE_RELATION_TYPE_RRHH."
  3136. $sql = "SELECT
  3137. course.id,
  3138. course.title,
  3139. course.code,
  3140. course.subscribe subscr,
  3141. course.unsubscribe unsubscr,
  3142. course_rel_user.status status,
  3143. course_rel_user.sort sort,
  3144. course_rel_user.user_course_cat user_course_cat
  3145. FROM $TABLECOURS course,
  3146. $TABLECOURSUSER course_rel_user,
  3147. $TABLE_ACCESS_URL_REL_COURSE url
  3148. WHERE
  3149. course.code = course_rel_user.course_code AND
  3150. url.course_code = course.code AND
  3151. course_rel_user.user_id = '".$user_id."' AND
  3152. course_rel_user.user_course_cat='".$user_category_id."'
  3153. $without_special_courses ";
  3154. // If multiple URL access mode is enabled, only fetch courses
  3155. // corresponding to the current URL.
  3156. if (api_get_multiple_access_url() && $current_url_id != -1) {
  3157. $sql .= " AND url.course_code = course.code AND access_url_id='".$current_url_id."'";
  3158. }
  3159. // Use user's classification for courses (if any).
  3160. $sql .= " ORDER BY course_rel_user.user_course_cat, course_rel_user.sort ASC";
  3161. $result = Database::query($sql);
  3162. $status_icon = '';
  3163. $html = '';
  3164. $course_list = array();
  3165. // Browse through all courses.
  3166. while ($course = Database::fetch_array($result)) {
  3167. $course_info = api_get_course_info($course['code']);
  3168. if ($course_info['visibility'] == COURSE_VISIBILITY_HIDDEN) {
  3169. continue;
  3170. }
  3171. $course_info['id_session'] = null;
  3172. $course_info['status'] = $course['status'];
  3173. //In order to avoid doubles
  3174. if (in_array($course_info['real_id'], $course_list)) {
  3175. continue;
  3176. } else {
  3177. $course_list[] = $course_info['real_id'];
  3178. }
  3179. // For each course, get if there is any notification icon to show
  3180. // (something that would have changed since the user's last visit).
  3181. $show_notification = Display::show_notification($course_info);
  3182. // New code displaying the user's status in respect to this course.
  3183. $status_icon = Display::return_icon('blackboard.png', api_htmlentities($course_info['title']), array(), ICON_SIZE_LARGE);
  3184. $params = array();
  3185. $params['right_actions'] = '';
  3186. if (api_is_platform_admin()) {
  3187. if ($load_dirs) {
  3188. $params['right_actions'] .= '<a id="document_preview_'.$course_info['real_id'].'_0" class="document_preview" href="javascript:void(0);">'.Display::return_icon('folder.png', get_lang('Documents'), array('align' => 'absmiddle'),ICON_SIZE_SMALL).'</a>';
  3189. $params['right_actions'] .= '<a href="'.api_get_path(WEB_CODE_PATH).'course_info/infocours.php?cidReq='.$course['code'].'">'.Display::return_icon('edit.png', get_lang('Edit'), array('align' => 'absmiddle'),ICON_SIZE_SMALL).'</a>';
  3190. $params['right_actions'] .= Display::div('', array('id' => 'document_result_'.$course_info['real_id'].'_0', 'class'=>'document_preview_container'));
  3191. } else {
  3192. $params['right_actions'].= '<a href="'.api_get_path(WEB_CODE_PATH).'course_info/infocours.php?cidReq='.$course['code'].'">'.Display::return_icon('edit.png', get_lang('Edit'), array('align' => 'absmiddle'),ICON_SIZE_SMALL).'</a>';
  3193. }
  3194. if ($course_info['status'] == COURSEMANAGER) {
  3195. //echo Display::return_icon('teachers.gif', get_lang('Status').': '.get_lang('Teacher'), array('style'=>'width: 11px; height: 11px;'));
  3196. }
  3197. } else {
  3198. if ($course_info['visibility'] != COURSE_VISIBILITY_CLOSED) {
  3199. if ($load_dirs) {
  3200. $params['right_actions'] .= '<a id="document_preview_'.$course_info['real_id'].'_0" class="document_preview" href="javascript:void(0);">'.Display::return_icon('folder.png', get_lang('Documents'), array('align' => 'absmiddle'),ICON_SIZE_SMALL).'</a>';
  3201. $params['right_actions'] .= Display::div('', array('id' => 'document_result_'.$course_info['real_id'].'_0', 'class'=>'document_preview_container'));
  3202. } else {
  3203. if ($course_info['status'] == COURSEMANAGER) {
  3204. $params['right_actions'].= '<a href="'.api_get_path(WEB_CODE_PATH).'course_info/infocours.php?cidReq='.$course['code'].'">'.Display::return_icon('edit.png', get_lang('Edit'), array('align' => 'absmiddle'),ICON_SIZE_SMALL).'</a>';
  3205. }
  3206. }
  3207. }
  3208. }
  3209. $course_title = $course_info['title'];
  3210. $course_title_url = '';
  3211. if ($course_info['visibility'] != COURSE_VISIBILITY_CLOSED || $course['status'] == COURSEMANAGER) {
  3212. $course_title_url = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/?id_session=0';
  3213. $course_title = Display::url($course_info['title'], $course_title_url);
  3214. } else {
  3215. $course_title = $course_info['title']." ".Display::tag('span',get_lang('CourseClosed'), array('class'=>'item_closed'));
  3216. }
  3217. // Start displaying the course block itself
  3218. if (api_get_setting('display_coursecode_in_courselist') == 'true') {
  3219. $course_title .= ' ('.$course_info['visual_code'].') ';
  3220. }
  3221. if (api_get_setting('display_teacher_in_courselist') == 'true') {
  3222. $teachers = CourseManager::get_teacher_list_from_course_code_to_string($course['code'], self::USER_SEPARATOR, true);
  3223. }
  3224. $params['link'] = $course_title_url;
  3225. $params['icon'] = $status_icon;
  3226. $params['title'] = $course_title;
  3227. $params['teachers'] = $teachers;
  3228. if ($course_info['visibility'] != COURSE_VISIBILITY_CLOSED) {
  3229. $params['notifications'] = $show_notification;
  3230. }
  3231. $is_subcontent = true;
  3232. if (empty($user_category_id)) {
  3233. $is_subcontent = false;
  3234. }
  3235. $html .= self::course_item_html($params, $is_subcontent);
  3236. }
  3237. return $html;
  3238. }
  3239. /**
  3240. * Retrieves the user defined course categories
  3241. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3242. * @return array containing all the titles of the user defined courses with the id as key of the array
  3243. */
  3244. function get_user_course_categories() {
  3245. global $_user;
  3246. $output = array();
  3247. $table_category = Database::get_user_personal_table(TABLE_USER_COURSE_CATEGORY);
  3248. $sql = "SELECT * FROM ".$table_category." WHERE user_id='".Database::escape_string($_user['user_id'])."'";
  3249. $result = Database::query($sql);
  3250. while ($row = Database::fetch_array($result)) {
  3251. $output[$row['id']] = $row['title'];
  3252. }
  3253. return $output;
  3254. }
  3255. /**
  3256. * Get the course id based on the original id and field name in the extra fields. Returns 0 if course was not found
  3257. *
  3258. * @param string Original course code
  3259. * @param string Original field name
  3260. * @return int Course id
  3261. */
  3262. public static function get_course_id_from_original_id($original_course_id_value, $original_course_id_name) {
  3263. $t_cfv = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  3264. $table_field = Database::get_main_table(TABLE_MAIN_COURSE_FIELD);
  3265. $sql_course = "SELECT course_code FROM $table_field cf INNER JOIN $t_cfv cfv ON cfv.field_id=cf.id WHERE field_variable='$original_course_id_name' AND field_value='$original_course_id_value'";
  3266. $res = Database::query($sql_course);
  3267. $row = Database::fetch_object($res);
  3268. if ($row != false) {
  3269. return $row->course_code;
  3270. } else {
  3271. return 0;
  3272. }
  3273. }
  3274. /**
  3275. * Display code for one specific course a logged in user is subscribed to.
  3276. * Shows a link to the course, what's new icons...
  3277. *
  3278. * $my_course['d'] - course directory
  3279. * $my_course['i'] - course title
  3280. * $my_course['c'] - visual course code
  3281. * $my_course['k'] - system course code
  3282. *
  3283. * @param array Course details
  3284. * @param integer Session ID
  3285. * @param string CSS class to apply to course entry
  3286. * @param boolean Whether the session is supposedly accessible now (not in the case it has passed and is in invisible/unaccessible mode)
  3287. * @param bool Whether to show the document quick-loader or not
  3288. * @return string The HTML to be printed for the course entry
  3289. *
  3290. * @version 1.0.3
  3291. * @todo refactor into different functions for database calls | logic | display
  3292. * @todo replace single-character $my_course['d'] indices
  3293. * @todo move code for what's new icons to a separate function to clear things up
  3294. * @todo add a parameter user_id so that it is possible to show the courselist of other users (=generalisation). This will prevent having to write a new function for this.
  3295. */
  3296. public static function get_logged_user_course_html($course, $session_id = 0, $class = 'courses', $session_accessible = true, $load_dirs = false)
  3297. {
  3298. global $nosession, $nbDigestEntries, $digest, $thisCourseSysCode, $orderKey;
  3299. $user_id = api_get_user_id();
  3300. $course_info = api_get_course_info($course['code']);
  3301. $status_course = CourseManager::get_user_in_course_status($user_id, $course_info['code']);
  3302. $course_info['status'] = empty($session_id) ? $status_course : STUDENT;
  3303. $course_info['id_session'] = $session_id;
  3304. if (!$nosession) {
  3305. global $now, $date_start, $date_end;
  3306. }
  3307. if (empty($date_start) or empty($date_end)) {
  3308. $sess = SessionManager::get_sessions_list(array('s.id = ' => $course_info['id_session']));
  3309. $date_start = $sess[$course_info['id_session']]['date_start'];
  3310. $date_end = $sess[$course_info['id_session']]['date_end'];
  3311. }
  3312. if (empty($now)) {
  3313. // maybe use api_get_utcdate() here?
  3314. $now = date('Y-m-d h:i:s');
  3315. }
  3316. // Table definitions
  3317. $main_user_table = Database :: get_main_table(TABLE_MAIN_USER);
  3318. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  3319. $tbl_session_category = Database :: get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  3320. $course_access_settings = CourseManager::get_access_settings($course_info['code']);
  3321. $course_visibility = $course_access_settings['visibility'];
  3322. if ($course_visibility == COURSE_VISIBILITY_HIDDEN) {
  3323. return '';
  3324. }
  3325. $user_in_course_status = CourseManager::get_user_in_course_status(api_get_user_id(), $course_info['code']);
  3326. $is_coach = api_is_coach($course_info['id_session'], $course['code']);
  3327. // Display course entry.
  3328. // Show a hyperlink to the course, unless the course is closed and user is not course admin.
  3329. $session_url = '';
  3330. $session_title = '';
  3331. if ($session_accessible) {
  3332. if ($course_visibility != COURSE_VISIBILITY_CLOSED || $user_in_course_status == COURSEMANAGER) {
  3333. if (!$nosession) {
  3334. if (empty($course_info['id_session'])) {
  3335. $course_info['id_session'] = 0;
  3336. }
  3337. $sessionCourseAvailable = false;
  3338. $sessionCourseStatus = api_get_session_visibility($session_id, $course_info['code']);
  3339. if (in_array($sessionCourseStatus, array(SESSION_VISIBLE_READ_ONLY, SESSION_VISIBLE, SESSION_AVAILABLE))) {
  3340. $sessionCourseAvailable = true;
  3341. }
  3342. if ($user_in_course_status == COURSEMANAGER || $sessionCourseAvailable) {
  3343. $session_url = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/?id_session='.$course_info['id_session'];
  3344. $session_title = '<a href="'.api_get_path(WEB_COURSE_PATH).$course_info['path'].'/?id_session='.$course_info['id_session'].'">'.$course_info['name'].'</a>';
  3345. } else {
  3346. $session_title = $course_info['name'];
  3347. }
  3348. } else {
  3349. $session_url = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/';
  3350. $session_title = '<a href="'.api_get_path(WEB_COURSE_PATH).$course_info['path'].'/">'.$course_info['name'].'</a>';
  3351. }
  3352. } else {
  3353. $session_title = $course_info['name'].' '.Display::tag('span',get_lang('CourseClosed'), array('class'=>'item_closed'));
  3354. }
  3355. } else {
  3356. $session_title = $course_info['name'];
  3357. }
  3358. $params = array();
  3359. $params['icon'] = Display::return_icon('blackboard_blue.png', api_htmlentities($course_info['name']), array(), ICON_SIZE_LARGE);
  3360. $params['link'] = $session_url;
  3361. $params['title'] = $session_title;
  3362. $params['right_actions'] = '';
  3363. if ($course_visibility != COURSE_VISIBILITY_CLOSED && $course_visibility != COURSE_VISIBILITY_HIDDEN) {
  3364. if ($load_dirs) {
  3365. $params['right_actions'] .= '<a id="document_preview_'.$course_info['real_id'].'_'.$course_info['id_session'].'" class="document_preview" href="javascript:void(0);">'.Display::return_icon('folder.png', get_lang('Documents'), array('align' => 'absmiddle'),ICON_SIZE_SMALL).'</a>';
  3366. $params['right_actions'] .= Display::div('', array('id' => 'document_result_'.$course_info['real_id'].'_'.$course_info['id_session'], 'class'=>'document_preview_container'));
  3367. }
  3368. }
  3369. if (api_get_setting('display_coursecode_in_courselist') == 'true') {
  3370. $session_title .= ' ('.$course_info['visual_code'].') ';
  3371. }
  3372. if (api_get_setting('display_teacher_in_courselist') == 'true') {
  3373. if (!$nosession) {
  3374. $teacher_list = CourseManager::get_teacher_list_from_course_code_to_string($course_info['code'], self::USER_SEPARATOR, true);
  3375. $course_coachs = CourseManager::get_coachs_from_course_to_string($course_info['id_session'], $course['code'], self::USER_SEPARATOR, true);
  3376. if ($course_info['status'] == COURSEMANAGER || ($course_info['status'] == STUDENT && empty($course_info['id_session'])) || empty($course_info['status'])) {
  3377. $params['teachers'] = $teacher_list;
  3378. }
  3379. if (($course_info['status'] == STUDENT && !empty($course_info['id_session'])) || ($is_coach && $course_info['status'] != COURSEMANAGER)) {
  3380. $params['coaches'] = $course_coachs;
  3381. }
  3382. } else {
  3383. $params['teachers'] = $teacher_list;
  3384. }
  3385. }
  3386. $session_title .= isset($course['special_course']) ? ' '.Display::return_icon('klipper.png', get_lang('CourseAutoRegister')) : '';
  3387. // Display the "what's new" icons
  3388. if ($course_visibility != COURSE_VISIBILITY_CLOSED && $course_visibility != COURSE_VISIBILITY_HIDDEN) {
  3389. $session_title .= Display :: show_notification($course_info);
  3390. }
  3391. $params['title'] = $session_title;
  3392. $params['extra'] = '';
  3393. $html = self::course_item_html($params, true);
  3394. $session_category_id = null;
  3395. if (!$nosession) {
  3396. $session = '';
  3397. $active = false;
  3398. if (!empty($course_info['session_name'])) {
  3399. // Request for the name of the general coach
  3400. $sql = 'SELECT lastname, firstname,sc.name
  3401. FROM '.$tbl_session.' ts
  3402. LEFT JOIN '.$main_user_table .' tu
  3403. ON ts.id_coach = tu.user_id
  3404. INNER JOIN '.$tbl_session_category.' sc ON ts.session_category_id = sc.id
  3405. WHERE ts.id='.(int) $course_info['id_session']. ' LIMIT 1';
  3406. $rs = Database::query($sql);
  3407. $sessioncoach = Database::store_result($rs);
  3408. $sessioncoach = $sessioncoach[0];
  3409. $session = array();
  3410. $session['title'] = $course_info['session_name'];
  3411. $session_category_id = CourseManager::get_session_category_id_by_session_id($course_info['id_session']);
  3412. $session['category'] = $sessioncoach['name'];
  3413. if ($course_info['date_start'] == '0000-00-00') {
  3414. //$session['dates'] = get_lang('WithoutTimeLimits');
  3415. $session['dates'] = '';
  3416. if (api_get_setting('show_session_coach') === 'true') {
  3417. $session['coach'] = get_lang('GeneralCoach').': '.api_get_person_name($sessioncoach['firstname'], $sessioncoach['lastname']);
  3418. }
  3419. $active = true;
  3420. } else {
  3421. $session ['dates'] = ' - '.get_lang('From').' '.$course_info['date_start'].' '.get_lang('To').' '.$course_info['date_end'];
  3422. if (api_get_setting('show_session_coach') === 'true') {
  3423. $session['coach'] = get_lang('GeneralCoach').': '.api_get_person_name($sessioncoach['firstname'], $sessioncoach['lastname']);
  3424. }
  3425. $active = ($date_start <= $now && $date_end >= $now);
  3426. }
  3427. }
  3428. $user_course_category = '';
  3429. if (isset($course_info['user_course_cat'])) {
  3430. $user_course_category = $course_info['user_course_cat'];
  3431. }
  3432. $output = array ($user_course_category, $html, $course_info['id_session'], $session, 'active' => $active, 'session_category_id' => $session_category_id);
  3433. } else {
  3434. $output = array ($course_info['user_course_cat'], $html);
  3435. }
  3436. return $output;
  3437. }
  3438. /**
  3439. *
  3440. * @param string source course code
  3441. * @param int source session id
  3442. * @param string destination course code
  3443. * @param int destination session id
  3444. * @return bool
  3445. */
  3446. public static function copy_course($source_course_code, $source_session_id, $destination_course_code, $destination_session_id, $params = array()) {
  3447. require_once api_get_path(SYS_CODE_PATH).'coursecopy/classes/CourseBuilder.class.php';
  3448. require_once api_get_path(SYS_CODE_PATH).'coursecopy/classes/CourseRestorer.class.php';
  3449. require_once api_get_path(SYS_CODE_PATH).'coursecopy/classes/CourseSelectForm.class.php';
  3450. $course_info = api_get_course_info($source_course_code);
  3451. if (!empty($course_info)) {
  3452. $cb = new CourseBuilder('', $course_info);
  3453. $course = $cb->build($source_session_id, $source_course_code, true);
  3454. $course_restorer = new CourseRestorer($course);
  3455. $course_restorer->skip_content = $params;
  3456. $course_restorer->restore($destination_course_code, $destination_session_id, true, true);
  3457. return true;
  3458. }
  3459. return false;
  3460. }
  3461. /**
  3462. * A simpler version of the copy_course, the function creates an empty course with an autogenerated course code
  3463. *
  3464. * @param string new course title
  3465. * @param string source course code
  3466. * @param int source session id
  3467. * @param int destination session id
  3468. * @param bool new copied tools (Exercises and LPs)will be set to invisible by default?
  3469. *
  3470. * @return array
  3471. */
  3472. public static function copy_course_simple($new_title, $source_course_code, $source_session_id = 0, $destination_session_id = 0, $params = array()) {
  3473. $source_course_info = api_get_course_info($source_course_code);
  3474. if (!empty($source_course_info)) {
  3475. $new_course_code = self::generate_nice_next_course_code($source_course_code);
  3476. if ($new_course_code) {
  3477. $new_course_info = self::create_course($new_title, $new_course_code, false);
  3478. if (!empty($new_course_info['code'])) {
  3479. $result = self::copy_course($source_course_code, $source_session_id, $new_course_info['code'], $destination_session_id, $params);
  3480. if ($result) {
  3481. return $new_course_info;
  3482. }
  3483. }
  3484. }
  3485. }
  3486. return false;
  3487. }
  3488. /**
  3489. * Creates a new course code based in a given code
  3490. *
  3491. * @param string wanted code
  3492. * <code> $wanted_code = 'curse' if there are in the DB codes like curse1 curse2 the function will return: course3</code>
  3493. * if the course code doest not exist in the DB the same course code will be returned
  3494. * @return string wanted unused code
  3495. */
  3496. public static function generate_nice_next_course_code($wanted_code) {
  3497. require_once api_get_path(LIBRARY_PATH).'add_course.lib.inc.php';
  3498. $course_code_ok = !self::course_code_exists($wanted_code);
  3499. if (!$course_code_ok) {
  3500. $wanted_code = generate_course_code($wanted_code);
  3501. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  3502. $wanted_code = Database::escape_string($wanted_code);
  3503. $sql = "SELECT count(*) as count FROM $table WHERE code LIKE '$wanted_code%'";
  3504. $result = Database::query($sql);
  3505. if (Database::num_rows($result) > 0 ) {
  3506. $row = Database::fetch_array($result);
  3507. $count = $row['count'] + 1;
  3508. $wanted_code = $wanted_code.'_'.$count;
  3509. $result = api_get_course_info($wanted_code);
  3510. if (empty($result)) {
  3511. return $wanted_code;
  3512. }
  3513. }
  3514. return false;
  3515. }
  3516. return $wanted_code;
  3517. }
  3518. /**
  3519. * Gets the status of the users agreement in a course course-session
  3520. *
  3521. * @param int user id
  3522. * @param string course code
  3523. * @param int session id
  3524. * @return boolean
  3525. */
  3526. public static function is_user_accepted_legal($user_id, $course_code, $session_id = null) {
  3527. $user_id = intval($user_id);
  3528. $course_code = Database::escape_string($course_code);
  3529. $session_id = intval($session_id);
  3530. if (empty($session_id)) {
  3531. $table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  3532. $sql = "SELECT legal_agreement FROM $table
  3533. WHERE user_id = $user_id AND course_code ='$course_code' ";
  3534. $result = Database::query($sql);
  3535. if (Database::num_rows($result) > 0 ) {
  3536. $result = Database::fetch_array($result);
  3537. if ($result['legal_agreement'] == 1 ) {
  3538. return true;
  3539. }
  3540. }
  3541. return false;
  3542. } else {
  3543. $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  3544. $sql = "SELECT legal_agreement FROM $table
  3545. WHERE id_user = $user_id AND course_code ='$course_code' AND id_session = $session_id";
  3546. $result = Database::query($sql);
  3547. if (Database::num_rows($result) > 0 ) {
  3548. $result = Database::fetch_array($result);
  3549. if ($result['legal_agreement'] == 1 ) {
  3550. return true;
  3551. }
  3552. }
  3553. return false;
  3554. }
  3555. return false;
  3556. }
  3557. /**
  3558. * Saves the user-course legal agreement
  3559. * @param int user id
  3560. * @param string course code
  3561. * @param int session id
  3562. */
  3563. function save_user_legal($user_id, $course_code, $session_id = null) {
  3564. $user_id = intval($user_id);
  3565. $course_code = Database::escape_string($course_code);
  3566. $session_id = intval($session_id);
  3567. if (empty($session_id)) {
  3568. $table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  3569. $sql = "UPDATE $table SET legal_agreement = '1'
  3570. WHERE user_id = $user_id AND course_code ='$course_code' ";
  3571. $result = Database::query($sql);
  3572. } else {
  3573. $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  3574. $sql = "UPDATE $table SET legal_agreement = '1'
  3575. WHERE id_user = $user_id AND course_code = '$course_code' AND id_session = $session_id";
  3576. $result = Database::query($sql);
  3577. }
  3578. }
  3579. public static function get_user_course_vote($user_id, $course_id, $session_id = null, $url_id = null) {
  3580. $table_user_course_vote = Database::get_main_table(TABLE_MAIN_USER_REL_COURSE_VOTE);
  3581. $session_id = !isset($session_id) ? api_get_session_id() : intval($session_id);
  3582. $url_id = empty($url_id) ? api_get_current_access_url_id() : intval($url_id);
  3583. $user_id = intval($user_id);
  3584. if (empty($user_id)) {
  3585. return false;
  3586. }
  3587. $params = array(
  3588. 'user_id' => $user_id,
  3589. 'c_id' => $course_id,
  3590. 'session_id' => $session_id,
  3591. 'url_id' => $url_id
  3592. );
  3593. $result = Database::select('vote', $table_user_course_vote, array('where' => array('user_id = ? AND c_id = ? AND session_id = ? AND url_id = ?' => $params)), 'first');
  3594. if (!empty($result)) {
  3595. return $result['vote'];
  3596. }
  3597. return false;
  3598. }
  3599. /**
  3600. * @param int $course_id
  3601. * @param int $session_id
  3602. * @param int $url_id
  3603. * @return array
  3604. */
  3605. public static function get_course_ranking($course_id, $session_id = null, $url_id = null) {
  3606. $table_course_ranking = Database::get_main_table(TABLE_STATISTIC_TRACK_COURSE_RANKING);
  3607. $session_id = !isset($session_id) ? api_get_session_id() : intval($session_id);
  3608. $url_id = empty($url_id) ? api_get_current_access_url_id() : intval($url_id);
  3609. $now = api_get_utc_datetime();
  3610. $params = array(
  3611. 'c_id' => $course_id,
  3612. 'session_id' => $session_id,
  3613. 'url_id' => $url_id,
  3614. 'creation_date' => $now,
  3615. );
  3616. $result = Database::select(
  3617. 'c_id, accesses, total_score, users',
  3618. $table_course_ranking,
  3619. array('where' => array('c_id = ? AND session_id = ? AND url_id = ?' => $params)),
  3620. 'first'
  3621. );
  3622. $point_average_in_percentage = 0;
  3623. $point_average_in_star = 0;
  3624. $users_who_voted = 0;
  3625. if (!empty($result['users'])) {
  3626. $users_who_voted = $result['users'];
  3627. $point_average_in_percentage = round($result['total_score']/$result['users'] * 100 / 5, 2);
  3628. $point_average_in_star = round($result['total_score']/$result['users'], 1);
  3629. }
  3630. $result['user_vote'] = false;
  3631. if (!api_is_anonymous()) {
  3632. $result['user_vote'] = self::get_user_course_vote(api_get_user_id(), $course_id, $session_id,$url_id);
  3633. }
  3634. $result['point_average'] = $point_average_in_percentage;
  3635. $result['point_average_star'] = $point_average_in_star;
  3636. $result['users_who_voted'] = $users_who_voted;
  3637. return $result;
  3638. }
  3639. /**
  3640. *
  3641. * Updates the course ranking
  3642. * @param int course id
  3643. * @param int session id
  3644. * @param id url id
  3645. *
  3646. **/
  3647. public static function update_course_ranking($course_id = null, $session_id = null, $url_id = null, $points_to_add = null, $add_access = true, $add_user = true) {
  3648. //Course catalog stats modifications see #4191
  3649. $table_course_ranking = Database::get_main_table(TABLE_STATISTIC_TRACK_COURSE_RANKING);
  3650. $now = api_get_utc_datetime();
  3651. $course_id = empty($course_id) ? api_get_course_int_id() : intval($course_id);
  3652. $session_id = !isset($session_id) ? api_get_session_id() : intval($session_id);
  3653. $url_id = empty($url_id) ? api_get_current_access_url_id() : intval($url_id);
  3654. $params = array(
  3655. 'c_id' => $course_id,
  3656. 'session_id' => $session_id,
  3657. 'url_id' => $url_id,
  3658. 'creation_date' => $now,
  3659. );
  3660. $result = Database::select('id, accesses, total_score, users', $table_course_ranking, array('where' => array('c_id = ? AND session_id = ? AND url_id = ?' => $params)), 'first');
  3661. // Problem here every time we load the courses/XXXX/index.php course home page we update the access
  3662. if (empty($result)) {
  3663. if ($add_access) {
  3664. $params['accesses'] = 1;
  3665. }
  3666. //The votes and users are empty
  3667. if (isset($points_to_add) && !empty($points_to_add)) {
  3668. $params['total_score'] = intval($points_to_add);
  3669. }
  3670. if ($add_user) {
  3671. $params['users'] = 1;
  3672. }
  3673. $result = Database::insert($table_course_ranking, $params);
  3674. } else {
  3675. $my_params = array();
  3676. if ($add_access) {
  3677. $my_params['accesses'] = intval($result['accesses']) + 1;
  3678. }
  3679. if (isset($points_to_add) && !empty($points_to_add)) {
  3680. $my_params['total_score'] = $result['total_score'] + $points_to_add;
  3681. }
  3682. if ($add_user) {
  3683. $my_params['users'] = $result['users'] + 1;
  3684. }
  3685. if (!empty($my_params)) {
  3686. $result = Database::update($table_course_ranking, $my_params, array('c_id = ? AND session_id = ? AND url_id = ?' => $params));
  3687. }
  3688. }
  3689. return $result;
  3690. }
  3691. /**
  3692. * Add user vote to a course
  3693. *
  3694. * @param int user id
  3695. * @param int vote [1..5]
  3696. * @param int course id
  3697. * @param int session id
  3698. * @param int url id (access_url_id)
  3699. * @return mixed 'added', 'updated' or 'nothing'
  3700. *
  3701. */
  3702. public static function add_course_vote($user_id, $vote, $course_id, $session_id = null, $url_id = null) {
  3703. $table_user_course_vote = Database::get_main_table(TABLE_MAIN_USER_REL_COURSE_VOTE);
  3704. $course_id = empty($course_id) ? api_get_course_int_id() : intval($course_id);
  3705. if (empty($course_id) || empty($user_id)) {
  3706. return false;
  3707. }
  3708. if (!in_array($vote, array(1,2,3,4,5))) {
  3709. return false;
  3710. }
  3711. $session_id = !isset($session_id) ? api_get_session_id() : intval($session_id);
  3712. $url_id = empty($url_id) ? api_get_current_access_url_id() : intval($url_id);
  3713. $vote = intval($vote);
  3714. $params = array(
  3715. 'user_id' => intval($user_id),
  3716. 'c_id' => $course_id,
  3717. 'session_id' => $session_id,
  3718. 'url_id' => $url_id,
  3719. 'vote' => $vote
  3720. );
  3721. $action_done = 'nothing';
  3722. $result = Database::select('id, vote', $table_user_course_vote, array('where' => array('user_id = ? AND c_id = ? AND session_id = ? AND url_id = ?' => $params)), 'first');
  3723. if (empty($result)) {
  3724. $result = Database::insert($table_user_course_vote, $params);
  3725. $points_to_add = $vote;
  3726. $add_user = true;
  3727. $action_done = 'added';
  3728. } else {
  3729. $my_params = array('vote' => $vote);
  3730. $points_to_add = $vote - $result['vote'];
  3731. $add_user = false;
  3732. $result = Database::update($table_user_course_vote, $my_params, array('user_id = ? AND c_id = ? AND session_id = ? AND url_id = ?' => $params));
  3733. $action_done = 'updated';
  3734. }
  3735. //Current points
  3736. if (!empty($points_to_add)) {
  3737. self::update_course_ranking($course_id, $session_id, $url_id, $points_to_add, false, $add_user);
  3738. }
  3739. return $action_done;
  3740. }
  3741. /**
  3742. * Remove course ranking + user votes
  3743. *
  3744. * @param int course id
  3745. * @param int session id
  3746. * @param int url id
  3747. *
  3748. */
  3749. public function remove_course_ranking($course_id, $session_id, $url_id = null) {
  3750. $table_course_ranking = Database::get_main_table(TABLE_STATISTIC_TRACK_COURSE_RANKING);
  3751. $table_user_course_vote = Database::get_main_table(TABLE_MAIN_USER_REL_COURSE_VOTE);
  3752. if (!empty($course_id) && isset($session_id)) {
  3753. $url_id = empty($url_id) ? api_get_current_access_url_id() : intval($url_id);
  3754. $params = array(
  3755. 'c_id' => $course_id,
  3756. 'session_id' => $session_id,
  3757. 'url_id' => $url_id,
  3758. );
  3759. Database::delete($table_course_ranking, array('c_id = ? AND session_id = ? AND url_id = ?' => $params));
  3760. Database::delete($table_user_course_vote, array('c_id = ? AND session_id = ? AND url_id = ?' => $params));
  3761. }
  3762. }
  3763. /**
  3764. * Returns an array with the hottest courses
  3765. * @param int number of days
  3766. * @param int number of hottest courses
  3767. */
  3768. public static function return_hot_courses($days = 30, $limit = 5) {
  3769. global $_configuration;
  3770. $limit = intval($limit);
  3771. //Getting my courses
  3772. $my_course_list = CourseManager::get_courses_list_by_user_id(api_get_user_id());
  3773. $my_course_code_list = array();
  3774. foreach ($my_course_list as $course) {
  3775. $my_course_code_list[$course['real_id']] = $course['real_id'];
  3776. }
  3777. if (api_is_drh()) {
  3778. $courses = CourseManager::get_courses_followed_by_drh(api_get_user_id());
  3779. foreach ($courses as $course) {
  3780. $my_course_code_list[$course['real_id']] = $course['real_id'];
  3781. }
  3782. }
  3783. $table_course_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  3784. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  3785. $table_course_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  3786. //@todo all dates in the tracking_course_access, last_access are in the DB time (NOW) not UTC
  3787. /*
  3788. $today = api_get_utc_datetime();
  3789. $today_diff = time() -intval($days)*24*60*60;
  3790. $today_diff = api_get_utc_datetime($today_diff);
  3791. * */
  3792. //WHERE login_course_date <= '$today' AND login_course_date >= '$today_diff'
  3793. //$table_course_access table uses the now() and interval ...
  3794. $now = api_get_utc_datetime(time());
  3795. $sql = "SELECT COUNT(course_access_id) course_count, a.course_code, visibility ".
  3796. "FROM $table_course c INNER JOIN $table_course_access a ".
  3797. " ON (c.code = a.course_code) INNER JOIN $table_course_url u ON u.course_code = a.course_code ".
  3798. " WHERE u.access_url_id = ".$_configuration['access_url']." AND".
  3799. " login_course_date <= '$now' AND ".
  3800. " login_course_date > DATE_SUB(now(), INTERVAL $days DAY) AND".
  3801. " visibility <> '".COURSE_VISIBILITY_CLOSED."' AND visibility <> '".COURSE_VISIBILITY_HIDDEN."'".
  3802. " GROUP BY course_code".
  3803. " ORDER BY course_count DESC".
  3804. " LIMIT $limit";
  3805. $result = Database::query($sql);
  3806. $courses = array();
  3807. if (Database::num_rows($result)) {
  3808. $courses = Database::store_result($result, 'ASSOC');
  3809. $courses = self::process_hot_course_item($courses, $my_course_code_list);
  3810. }
  3811. return $courses;
  3812. }
  3813. public static function process_hot_course_item($courses, $my_course_code_list = array()) {
  3814. $ajax_url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=add_course_vote';
  3815. $stok = Security::get_existing_token();
  3816. foreach ($courses as &$my_course) {
  3817. $course_info = api_get_course_info($my_course['course_code']);
  3818. $courseCode = $my_course['course_code'];
  3819. $categoryCode = !empty($my_course['categoryCode']) ? $my_course['categoryCode'] : "";
  3820. $my_course['extra_info'] = $course_info;
  3821. $my_course['extra_info']['go_to_course_button'] = '';
  3822. $my_course['extra_info']['register_button'] = '';
  3823. $access_link = self::get_access_link_by_user(api_get_user_id(), $course_info, $my_course_code_list);
  3824. //Course visibility
  3825. if ($access_link && in_array('register', $access_link)) {
  3826. $my_course['extra_info']['register_button'] = Display::url(get_lang('Subscribe'), api_get_path(WEB_COURSE_PATH).$course_info['path'].'/index.php?action=subscribe&amp;sec_token='.$stok, array('class' => 'btn btn-primary'));
  3827. }
  3828. if ($access_link && in_array('enter', $access_link) || $course_info['visibility'] == COURSE_VISIBILITY_OPEN_WORLD) {
  3829. $my_course['extra_info']['go_to_course_button'] = Display::url(get_lang('GoToCourse'), api_get_path(WEB_COURSE_PATH).$course_info['path'].'/index.php', array('class' => 'btn btn-primary'));
  3830. }
  3831. if ($access_link && in_array('unsubscribe', $access_link)) {
  3832. $my_course['extra_info']['unsubscribe_button'] = Display::url(get_lang('Unsubscribe'), api_get_path(WEB_CODE_PATH).'auth/courses.php?action=unsubscribe&amp;unsubscribe='.$courseCode.'&amp;sec_token='.$stok.'&amp;category_code='.$categoryCode, array('class' => 'btn btn-primary'));
  3833. }
  3834. //Description
  3835. $my_course['extra_info']['description_button'] = '';
  3836. if ($course_info['visibility'] == COURSE_VISIBILITY_OPEN_WORLD || in_array($course_info['real_id'], $my_course_code_list) ) {
  3837. $my_course['extra_info']['description_button'] = Display::url(get_lang('Description'), api_get_path(WEB_AJAX_PATH).'course_home.ajax.php?a=show_course_information&amp;code='.$course_info['code'], array('class' => 'ajax btn'));
  3838. }
  3839. $my_course['extra_info']['teachers'] = CourseManager::get_teacher_list_from_course_code_to_string($course_info['code']);
  3840. $point_info = self::get_course_ranking($course_info['real_id'], 0);
  3841. $my_course['extra_info']['rating_html'] = Display::return_rating_system('star_'.$course_info['real_id'], $ajax_url.'&amp;course_id='.$course_info['real_id'], $point_info);
  3842. }
  3843. return $courses;
  3844. }
  3845. public static function return_most_accessed_courses($limit = 5) {
  3846. $table_course_ranking = Database::get_main_table(TABLE_STATISTIC_TRACK_COURSE_RANKING);
  3847. $params['url_id'] = api_get_current_access_url_id();
  3848. $result = Database::select('c_id, accesses, total_score, users', $table_course_ranking, array('where' => array('url_id = ?' => $params), 'order' => 'accesses DESC', 'limit' => $limit), 'all', true);
  3849. return $result;
  3850. }
  3851. /**
  3852. * @deprecated seems not to be used
  3853. */
  3854. static function list_inactive_courses($ceiling, $visibility_level = COURSE_VISIBILITY_REGISTERED)
  3855. {
  3856. $ceiling = is_numeric($ceiling) ? (int) $ceiling : strtotime($ceiling);
  3857. $ceiling = date('Y-m-d H:i:s', $ceiling);
  3858. $visibility_level = $visibility_level ? $visibility_level : '0';
  3859. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  3860. $table_category = Database::get_main_table(TABLE_MAIN_CATEGORY);
  3861. $sql = "SELECT
  3862. c.*,
  3863. cat.name AS category
  3864. FROM
  3865. $table_course AS c
  3866. LEFT JOIN
  3867. $table_category AS cat
  3868. ON
  3869. c.category_code = cat.code
  3870. WHERE
  3871. c.visibility >= $visibility_level AND
  3872. c.last_visit<='$ceiling'
  3873. ";
  3874. return ResultSet::create($sql);
  3875. }
  3876. /**
  3877. * Get courses count
  3878. * @param int Access URL ID (optional)
  3879. * @return int Number of courses
  3880. */
  3881. public static function count_courses($access_url_id = null) {
  3882. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  3883. $table_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  3884. $sql = "SELECT count(id) FROM $table_course c";
  3885. if (!empty($access_url_id) && $access_url_id == intval($access_url_id)) {
  3886. $sql .= ", $table_course_rel_access_url u WHERE c.code = u.course_code AND u.access_url_id = $access_url_id";
  3887. }
  3888. $res = Database::query($sql);
  3889. $row = Database::fetch_row($res);
  3890. return $row[0];
  3891. }
  3892. /**
  3893. * Get availab le courses count
  3894. * @param int Access URL ID (optional)
  3895. * @return int Number of courses
  3896. */
  3897. public static function countAvailableCourses($accessUrlId = null)
  3898. {
  3899. $tableCourse = Database::get_main_table(TABLE_MAIN_COURSE);
  3900. $tableCourseRelAccessUrl = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  3901. $specialCourseList = self::get_special_course_list();
  3902. $withoutSpecialCourses = '';
  3903. if (!empty($specialCourseList)) {
  3904. $withoutSpecialCourses = ' AND c.code NOT IN ("'.implode('","',$specialCourseList).'")';
  3905. }
  3906. if (!empty($accessUrlId) && $accessUrlId == intval($accessUrlId)) {
  3907. $sql = "SELECT count(id) FROM $tableCourse c, $tableCourseRelAccessUrl u WHERE c.code = u.course_code AND u.access_url_id = $accessUrlId AND c.visibility != 0 AND c.visibility != 4 $withoutSpecialCourses";
  3908. }
  3909. $res = Database::query($sql);
  3910. $row = Database::fetch_row($res);
  3911. return $row[0];
  3912. }
  3913. /**
  3914. * Return a link to go to the course, validating the visibility of the
  3915. * course and the user status
  3916. * @param int User ID
  3917. * @param array Course details array
  3918. * @param array List of courses to which the user is subscribed (if not provided, will be generated)
  3919. * @return mixed 'enter' for a link to go to the course or 'register' for a link to subscribe, or false if no access
  3920. */
  3921. static function get_access_link_by_user($uid, $course, $user_courses = array())
  3922. {
  3923. if (empty($uid) or empty($course)) {
  3924. return false;
  3925. }
  3926. if (empty($user_courses)) {
  3927. // get the array of courses to which the user is subscribed
  3928. $user_courses = CourseManager::get_courses_list_by_user_id($uid);
  3929. foreach ($user_courses as $k => $v) {
  3930. $user_courses[$k] = $v['real_id'];
  3931. }
  3932. }
  3933. if (!isset($course['real_id']) && empty($course['real_id'])) {
  3934. $course = api_get_course_info($course['code']);
  3935. }
  3936. if ($course['visibility'] == COURSE_VISIBILITY_HIDDEN) {
  3937. return array();
  3938. }
  3939. $is_admin = api_is_platform_admin_by_id($uid);
  3940. $options = array();
  3941. // Register button
  3942. if (!api_is_anonymous($uid) &&
  3943. (
  3944. ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD || $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM)
  3945. //$course['visibility'] == COURSE_VISIBILITY_REGISTERED && $course['subscribe'] == SUBSCRIBE_ALLOWED
  3946. ) &&
  3947. $course['subscribe'] == SUBSCRIBE_ALLOWED &&
  3948. (!in_array($course['real_id'], $user_courses) || empty($user_courses))
  3949. ) {
  3950. $options[]= 'register';
  3951. }
  3952. // Go To Course button (only if admin, if course public or if student already subscribed)
  3953. if ($is_admin ||
  3954. $course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD && empty($course['registration_code']) ||
  3955. (api_user_is_login($uid) && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM && empty($course['registration_code']) ) ||
  3956. (in_array($course['real_id'], $user_courses) && $course['visibility'] != COURSE_VISIBILITY_CLOSED)
  3957. ) {
  3958. $options[]= 'enter';
  3959. }
  3960. if ($is_admin ||
  3961. $course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD && empty($course['registration_code']) ||
  3962. (api_user_is_login($uid) && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM && empty($course['registration_code']) ) ||
  3963. (in_array($course['real_id'], $user_courses) && $course['visibility'] != COURSE_VISIBILITY_CLOSED)
  3964. ) {
  3965. $options[]= 'enter';
  3966. }
  3967. if ($course['visibility'] != COURSE_VISIBILITY_HIDDEN && empty($course['registration_code']) && $course['unsubscribe'] == UNSUBSCRIBE_ALLOWED && api_user_is_login($uid) && (in_array($course['real_id'], $user_courses))) {
  3968. $options[]= 'unsubscribe';
  3969. }
  3970. return $options;
  3971. }
  3972. /**
  3973. * @param string $course_code
  3974. * @param array $teachers
  3975. * @param bool $deleteTeachersNotInList
  3976. * @param bool $editTeacherInSessions
  3977. * @param bool $deleteSessionTeacherNotInList
  3978. * @return bool
  3979. */
  3980. public static function updateTeachers(
  3981. $course_code,
  3982. $teachers,
  3983. $deleteTeachersNotInList = true,
  3984. $editTeacherInSessions = false,
  3985. $deleteSessionTeacherNotInList = false
  3986. ) {
  3987. if (empty($teachers)) {
  3988. return false;
  3989. }
  3990. if (!is_array($teachers)) {
  3991. $teachers = array($teachers);
  3992. }
  3993. $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  3994. $alreadyAddedTeachers = CourseManager::get_teacher_list_from_course_code($course_code);
  3995. if ($deleteTeachersNotInList) {
  3996. // Delete only teacher relations that doesn't match the selected teachers
  3997. $cond = null;
  3998. if (count($teachers)>0) {
  3999. foreach($teachers as $key) {
  4000. $cond.= " AND user_id <> '".$key."'";
  4001. }
  4002. }
  4003. $sql = 'DELETE FROM '.$course_user_table.' WHERE course_code="'.Database::escape_string($course_code).'" AND status="1"'.$cond;
  4004. Database::query($sql);
  4005. }
  4006. if (count($teachers) > 0) {
  4007. foreach ($teachers as $userId) {
  4008. // We check if the teacher is already subscribed in this course
  4009. $sql = 'SELECT 1 FROM '.$course_user_table.' WHERE user_id = "'.$userId.'" AND course_code = "'.$course_code.'" ';
  4010. $result = Database::query($sql);
  4011. if (Database::num_rows($result)) {
  4012. $sql = 'UPDATE '.$course_user_table.' SET status = "1" WHERE course_code = "'.$course_code.'" AND user_id = "'.$userId.'" ';
  4013. } else {
  4014. $sql = "INSERT INTO ".$course_user_table . " SET
  4015. course_code = '".Database::escape_string($course_code). "',
  4016. user_id = '".$userId."',
  4017. status = '1',
  4018. role = '',
  4019. tutor_id = '0',
  4020. sort = '0',
  4021. user_course_cat='0'";
  4022. }
  4023. Database::query($sql);
  4024. }
  4025. }
  4026. if ($editTeacherInSessions) {
  4027. $sessions = SessionManager::get_session_by_course($course_code);
  4028. if (!empty($sessions)) {
  4029. foreach ($sessions as $session) {
  4030. // Remove old and add new
  4031. if ($deleteSessionTeacherNotInList) {
  4032. foreach ($teachers as $userId) {
  4033. SessionManager::set_coach_to_course_session($userId, $session['id'], $course_code);
  4034. }
  4035. $teachersToDelete = array();
  4036. if (!empty($alreadyAddedTeachers)) {
  4037. $teachersToDelete = array_diff(array_keys($alreadyAddedTeachers), $teachers);
  4038. }
  4039. if (!empty($teachersToDelete)) {
  4040. foreach ($teachersToDelete as $userId) {
  4041. SessionManager::set_coach_to_course_session($userId, $session['id'], $course_code, true);
  4042. }
  4043. }
  4044. } else {
  4045. // Add new teachers only
  4046. foreach ($teachers as $userId) {
  4047. SessionManager::set_coach_to_course_session($userId, $session['id'], $course_code);
  4048. }
  4049. }
  4050. }
  4051. }
  4052. }
  4053. }
  4054. /**
  4055. * Course available settings variables see c_course_setting table
  4056. * @param AppPlugin $appPlugin
  4057. * @return array
  4058. */
  4059. public static function getCourseSettingVariables(AppPlugin $appPlugin)
  4060. {
  4061. $pluginCourseSettings = $appPlugin->getAllPluginCourseSettings();
  4062. $courseSettings = array(
  4063. // Get allow_learning_path_theme from table
  4064. 'allow_learning_path_theme',
  4065. // Get allow_open_chat_window from table
  4066. 'allow_open_chat_window',
  4067. 'allow_public_certificates',
  4068. // Get allow_user_edit_agenda from table
  4069. 'allow_user_edit_agenda',
  4070. // Get allow_user_edit_announcement from table
  4071. 'allow_user_edit_announcement',
  4072. // Get allow_user_image_forum from table
  4073. 'allow_user_image_forum',
  4074. //Get allow show user list
  4075. 'allow_user_view_user_list',
  4076. // Get course_theme from table
  4077. 'course_theme',
  4078. //Get allow show user list
  4079. 'display_info_advance_inside_homecourse',
  4080. 'documents_default_visibility',
  4081. // Get send_mail_setting (work)from table
  4082. 'email_alert_manager_on_new_doc',
  4083. // Get send_mail_setting (work)from table
  4084. 'email_alert_manager_on_new_quiz',
  4085. // Get send_mail_setting (dropbox) from table
  4086. 'email_alert_on_new_doc_dropbox',
  4087. 'email_alert_students_on_new_homework',
  4088. // Get send_mail_setting (auth)from table
  4089. 'email_alert_to_teacher_on_new_user_in_course',
  4090. 'enable_lp_auto_launch',
  4091. 'pdf_export_watermark_text',
  4092. 'show_system_folders'
  4093. );
  4094. global $_configuration;
  4095. if (isset($_configuration['allow_lp_return_link']) && $_configuration['allow_lp_return_link']) {
  4096. $courseSettings[] = 'lp_return_link';
  4097. }
  4098. if (!empty($pluginCourseSettings)) {
  4099. $courseSettings = array_merge(
  4100. $courseSettings,
  4101. $pluginCourseSettings
  4102. );
  4103. }
  4104. return $courseSettings;
  4105. }
  4106. /**
  4107. * @param AppPlugin $appPlugin
  4108. * @param string $variable
  4109. * @param string $value
  4110. * @param int $courseId
  4111. * @return bool
  4112. */
  4113. public static function saveCourseConfigurationSetting(AppPlugin $appPlugin, $variable, $value, $courseId)
  4114. {
  4115. $settingList = self::getCourseSettingVariables($appPlugin);
  4116. if (!in_array($variable, $settingList)) {
  4117. return false;
  4118. }
  4119. $courseSettingTable = Database::get_course_table(TABLE_COURSE_SETTING);
  4120. if (self::hasCourseSetting($variable, $courseId)) {
  4121. // Update
  4122. Database::update(
  4123. $courseSettingTable,
  4124. array('value' => $value),
  4125. array('variable = ? AND c_id = ?' => array($variable, $courseId))
  4126. );
  4127. } else {
  4128. // Create
  4129. Database::insert(
  4130. $courseSettingTable,
  4131. array('value' => $value, 'c_id' => $courseId, 'variable' => $variable)
  4132. );
  4133. }
  4134. return true;
  4135. }
  4136. /**
  4137. * Check if course setting exists
  4138. * @param string $variable
  4139. * @param int $courseId
  4140. * @return bool
  4141. */
  4142. public static function hasCourseSetting($variable, $courseId)
  4143. {
  4144. $courseSetting = Database::get_course_table(TABLE_COURSE_SETTING);
  4145. $courseId = intval($courseId);
  4146. $variable = Database::escape_string($variable);
  4147. $sql = "SELECT variable FROM $courseSetting WHERE c_id = $courseId AND variable = '$variable'";
  4148. $result = Database::query($sql);
  4149. return Database::num_rows($result) > 0;
  4150. }
  4151. /**
  4152. * Get information from the track_e_course_access table
  4153. * @param int $sessionId
  4154. * @param int $userId
  4155. * @return array
  4156. */
  4157. public static function getCourseAccessPerSessionAndUser($sessionId, $userId, $limit = null)
  4158. {
  4159. $table = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  4160. $sql = "SELECT * FROM $table
  4161. WHERE session_id = $sessionId AND user_id = $userId";
  4162. if (!empty($limit)) {
  4163. $limit = intval($limit);
  4164. $sql .= " LIMIT $limit";
  4165. }
  4166. $result = Database::query($sql);
  4167. return Database::store_result($result);
  4168. }
  4169. /**
  4170. * Get login information from the track_e_course_access table, for any
  4171. * course in the given session
  4172. * @param int $sessionId
  4173. * @param int $userId
  4174. * @return array
  4175. */
  4176. public static function getFirstCourseAccessPerSessionAndUser($sessionId, $userId)
  4177. {
  4178. $table = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  4179. $sql = "SELECT * FROM $table
  4180. WHERE session_id = $sessionId AND user_id = $userId
  4181. ORDER BY login_course_date ASC
  4182. LIMIT 1";
  4183. $result = Database::query($sql);
  4184. $courseAccess = array();
  4185. if (Database::num_rows($result)) {
  4186. $courseAccess = Database::fetch_array($result, 'ASSOC');
  4187. }
  4188. return $courseAccess;
  4189. }
  4190. }