course.lib.php 231 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397
  1. <?php
  2. /* For licensing terms, see /license.txt*/
  3. use Chamilo\CoreBundle\Entity\Course;
  4. use Chamilo\CoreBundle\Entity\ExtraField as EntityExtraField;
  5. use Chamilo\CourseBundle\Component\CourseCopy\CourseBuilder;
  6. use Chamilo\CourseBundle\Component\CourseCopy\CourseRestorer;
  7. use ChamiloSession as Session;
  8. /**
  9. * Class CourseManager.
  10. *
  11. * This is the course library for Chamilo.
  12. *
  13. * All main course functions should be placed here.
  14. *
  15. * Many functions of this library deal with providing support for
  16. * virtual/linked/combined courses (this was already used in several universities
  17. * but not available in standard Chamilo).
  18. *
  19. * There are probably some places left with the wrong code.
  20. *
  21. * @package chamilo.library
  22. */
  23. class CourseManager
  24. {
  25. const MAX_COURSE_LENGTH_CODE = 40;
  26. /** This constant is used to show separate user names in the course
  27. * list (userportal), footer, etc */
  28. const USER_SEPARATOR = ' |';
  29. const COURSE_FIELD_TYPE_CHECKBOX = 10;
  30. public $columns = [];
  31. /**
  32. * Creates a course.
  33. *
  34. * @param array $params Columns in the main.course table.
  35. * @param int $authorId Optional.
  36. * @param int $accessUrlId Optional.
  37. *
  38. * @return mixed false if the course was not created, array with the course info
  39. */
  40. public static function create_course($params, $authorId = 0, $accessUrlId = 0)
  41. {
  42. global $_configuration;
  43. $hook = HookCreateCourse::create();
  44. // Check portal limits
  45. $accessUrlId = empty($accessUrlId)
  46. ? (api_get_multiple_access_url() ? api_get_current_access_url_id() : 1)
  47. : $accessUrlId;
  48. $authorId = empty($authorId) ? api_get_user_id() : (int) $authorId;
  49. if (isset($_configuration[$accessUrlId]) && is_array($_configuration[$accessUrlId])) {
  50. $return = self::checkCreateCourseAccessUrlParam(
  51. $_configuration,
  52. $accessUrlId,
  53. 'hosting_limit_courses',
  54. 'PortalCoursesLimitReached'
  55. );
  56. if ($return != false) {
  57. return $return;
  58. }
  59. $return = self::checkCreateCourseAccessUrlParam(
  60. $_configuration,
  61. $accessUrlId,
  62. 'hosting_limit_active_courses',
  63. 'PortalActiveCoursesLimitReached'
  64. );
  65. if ($return != false) {
  66. return $return;
  67. }
  68. }
  69. if (empty($params['title'])) {
  70. return false;
  71. }
  72. if (empty($params['wanted_code'])) {
  73. $params['wanted_code'] = $params['title'];
  74. // Check whether the requested course code has already been occupied.
  75. $substring = api_substr($params['title'], 0, self::MAX_COURSE_LENGTH_CODE);
  76. if ($substring === false || empty($substring)) {
  77. return false;
  78. } else {
  79. $params['wanted_code'] = self::generate_course_code($substring);
  80. }
  81. }
  82. // Create the course keys
  83. $keys = AddCourse::define_course_keys($params['wanted_code']);
  84. $params['exemplary_content'] = isset($params['exemplary_content']) ? $params['exemplary_content'] : false;
  85. if (count($keys)) {
  86. $params['code'] = $keys['currentCourseCode'];
  87. $params['visual_code'] = $keys['currentCourseId'];
  88. $params['directory'] = $keys['currentCourseRepository'];
  89. $course_info = api_get_course_info($params['code']);
  90. if (empty($course_info)) {
  91. $course_id = AddCourse::register_course($params, $accessUrlId);
  92. $course_info = api_get_course_info_by_id($course_id);
  93. if ($hook) {
  94. $hook->setEventData(['course_info' => $course_info]);
  95. $hook->notifyCreateCourse(HOOK_EVENT_TYPE_POST);
  96. }
  97. if (!empty($course_info)) {
  98. self::fillCourse($course_info, $params, $authorId);
  99. return $course_info;
  100. }
  101. }
  102. }
  103. return false;
  104. }
  105. /**
  106. * Returns all the information of a given course code.
  107. *
  108. * @param string $course_code , the course code
  109. *
  110. * @return array with all the fields of the course table
  111. *
  112. * @deprecated Use api_get_course_info() instead
  113. *
  114. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  115. * @assert ('') === false
  116. */
  117. public static function get_course_information($course_code)
  118. {
  119. return Database::fetch_array(
  120. Database::query(
  121. "SELECT *, id as real_id FROM ".Database::get_main_table(TABLE_MAIN_COURSE)."
  122. WHERE code = '".Database::escape_string($course_code)."'"
  123. ),
  124. 'ASSOC'
  125. );
  126. }
  127. /**
  128. * Returns a list of courses. Should work with quickform syntax.
  129. *
  130. * @param int $from Offset (from the 7th = '6'). Optional.
  131. * @param int $howmany Number of results we want. Optional.
  132. * @param int $orderby The column we want to order it by. Optional, defaults to first column.
  133. * @param string $orderdirection The direction of the order (ASC or DESC). Optional, defaults to ASC.
  134. * @param int $visibility the visibility of the course, or all by default
  135. * @param string $startwith If defined, only return results for which the course *title* begins with this string
  136. * @param string $urlId The Access URL ID, if using multiple URLs
  137. * @param bool $alsoSearchCode An extension option to indicate that we also want to search for course codes (not *only* titles)
  138. * @param array $conditionsLike
  139. * @param array $onlyThisCourseList
  140. *
  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 = [],
  153. $onlyThisCourseList = []
  154. ) {
  155. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  156. $sql = "SELECT course.*, course.id as real_id
  157. FROM $courseTable course ";
  158. if (!empty($urlId)) {
  159. $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  160. $sql .= " INNER JOIN $table url ON (url.c_id = course.id) ";
  161. }
  162. $visibility = (int) $visibility;
  163. if (!empty($startwith)) {
  164. $sql .= "WHERE (title LIKE '".Database::escape_string($startwith)."%' ";
  165. if ($alsoSearchCode) {
  166. $sql .= "OR code LIKE '".Database::escape_string($startwith)."%' ";
  167. }
  168. $sql .= ') ';
  169. if ($visibility !== -1) {
  170. $sql .= " AND visibility = $visibility ";
  171. }
  172. } else {
  173. $sql .= 'WHERE 1 ';
  174. if ($visibility !== -1) {
  175. $sql .= " AND visibility = $visibility ";
  176. }
  177. }
  178. if (!empty($urlId)) {
  179. $urlId = (int) $urlId;
  180. $sql .= " AND access_url_id = $urlId";
  181. }
  182. if (!empty($onlyThisCourseList)) {
  183. $onlyThisCourseList = array_map('intval', $onlyThisCourseList);
  184. $onlyThisCourseList = implode("','", $onlyThisCourseList);
  185. $sql .= " AND course.id IN ('$onlyThisCourseList') ";
  186. }
  187. $allowedFields = [
  188. 'title',
  189. 'code',
  190. ];
  191. if (count($conditionsLike) > 0) {
  192. $sql .= ' AND ';
  193. $temp_conditions = [];
  194. foreach ($conditionsLike as $field => $value) {
  195. if (!in_array($field, $allowedFields)) {
  196. continue;
  197. }
  198. $field = Database::escape_string($field);
  199. $value = Database::escape_string($value);
  200. $simple_like = false;
  201. if ($simple_like) {
  202. $temp_conditions[] = $field." LIKE '$value%'";
  203. } else {
  204. $temp_conditions[] = $field.' LIKE \'%'.$value.'%\'';
  205. }
  206. }
  207. $condition = ' AND ';
  208. if (!empty($temp_conditions)) {
  209. $sql .= implode(' '.$condition.' ', $temp_conditions);
  210. }
  211. }
  212. if (!empty($orderby)) {
  213. $sql .= " ORDER BY ".Database::escape_string($orderby)." ";
  214. } else {
  215. $sql .= ' ORDER BY 1 ';
  216. }
  217. if (!in_array($orderdirection, ['ASC', 'DESC'])) {
  218. $sql .= 'ASC';
  219. } else {
  220. $sql .= ($orderdirection == 'ASC' ? 'ASC' : 'DESC');
  221. }
  222. if (!empty($howmany) && is_int($howmany) and $howmany > 0) {
  223. $sql .= ' LIMIT '.Database::escape_string($howmany);
  224. } else {
  225. $sql .= ' LIMIT 1000000'; //virtually no limit
  226. }
  227. if (!empty($from)) {
  228. $from = intval($from);
  229. $sql .= ' OFFSET '.intval($from);
  230. } else {
  231. $sql .= ' OFFSET 0';
  232. }
  233. $data = [];
  234. $res = Database::query($sql);
  235. if (Database::num_rows($res) > 0) {
  236. while ($row = Database::fetch_array($res, 'ASSOC')) {
  237. $data[] = $row;
  238. }
  239. }
  240. return $data;
  241. }
  242. /**
  243. * Returns the status of a user in a course, which is COURSEMANAGER or STUDENT.
  244. *
  245. * @param int $userId
  246. * @param int $courseId
  247. *
  248. * @return int|bool the status of the user in that course (or false if the user is not in that course)
  249. */
  250. public static function getUserInCourseStatus($userId, $courseId)
  251. {
  252. $courseId = (int) $courseId;
  253. $userId = (int) $userId;
  254. if (empty($courseId)) {
  255. return false;
  256. }
  257. $result = Database::fetch_array(
  258. Database::query(
  259. "SELECT status FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  260. WHERE
  261. c_id = $courseId AND
  262. user_id = $userId"
  263. )
  264. );
  265. return $result['status'];
  266. }
  267. /**
  268. * @param int $userId
  269. * @param int $courseId
  270. *
  271. * @return mixed
  272. */
  273. public static function getUserCourseInfo($userId, $courseId)
  274. {
  275. $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  276. WHERE
  277. c_id = ".intval($courseId)." AND
  278. user_id = ".intval($userId);
  279. $result = Database::fetch_array(Database::query($sql));
  280. return $result;
  281. }
  282. /**
  283. * @param int $userId
  284. * @param int $courseId
  285. * @param bool $isTutor
  286. *
  287. * @return bool
  288. */
  289. public static function updateUserCourseTutor($userId, $courseId, $isTutor)
  290. {
  291. $table = Database::escape_string(TABLE_MAIN_COURSE_USER);
  292. $courseId = intval($courseId);
  293. $isTutor = intval($isTutor);
  294. $sql = "UPDATE $table SET is_tutor = '".$isTutor."'
  295. WHERE
  296. user_id = ".$userId." AND
  297. c_id = ".$courseId;
  298. $result = Database::query($sql);
  299. if (Database::affected_rows($result) > 0) {
  300. return true;
  301. } else {
  302. return false;
  303. }
  304. }
  305. /**
  306. * @param int $userId
  307. * @param int $courseId
  308. *
  309. * @return mixed
  310. */
  311. public static function get_tutor_in_course_status($userId, $courseId)
  312. {
  313. $userId = intval($userId);
  314. $courseId = intval($courseId);
  315. $result = Database::fetch_array(
  316. Database::query(
  317. "SELECT is_tutor
  318. FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  319. WHERE
  320. c_id = $courseId AND
  321. user_id = $userId"
  322. )
  323. );
  324. return $result['is_tutor'];
  325. }
  326. /**
  327. * Unsubscribe one or more users from a course.
  328. *
  329. * @param mixed user_id or an array with user ids
  330. * @param string course code
  331. * @param int session id
  332. *
  333. * @return bool
  334. *
  335. * @assert ('', '') === false
  336. */
  337. public static function unsubscribe_user($user_id, $course_code, $session_id = 0)
  338. {
  339. if (empty($user_id)) {
  340. return false;
  341. }
  342. if (!is_array($user_id)) {
  343. $user_id = [$user_id];
  344. }
  345. if (count($user_id) == 0) {
  346. return false;
  347. }
  348. if (!empty($session_id)) {
  349. $session_id = (int) $session_id;
  350. } else {
  351. $session_id = api_get_session_id();
  352. }
  353. if (empty($course_code)) {
  354. return false;
  355. }
  356. $userList = [];
  357. // Cleaning the $user_id variable
  358. if (is_array($user_id)) {
  359. $new_user_id_list = [];
  360. foreach ($user_id as $my_user_id) {
  361. $new_user_id_list[] = (int) $my_user_id;
  362. }
  363. $new_user_id_list = array_filter($new_user_id_list);
  364. $userList = $new_user_id_list;
  365. $user_ids = implode(',', $new_user_id_list);
  366. } else {
  367. $user_ids = (int) $user_id;
  368. $userList[] = $user_id;
  369. }
  370. $course_info = api_get_course_info($course_code);
  371. $course_id = $course_info['real_id'];
  372. // Unsubscribe user from all groups in the course.
  373. $sql = "DELETE FROM ".Database::get_course_table(TABLE_GROUP_USER)."
  374. WHERE c_id = $course_id AND user_id IN (".$user_ids.")";
  375. Database::query($sql);
  376. $sql = "DELETE FROM ".Database::get_course_table(TABLE_GROUP_TUTOR)."
  377. WHERE c_id = $course_id AND user_id IN (".$user_ids.")";
  378. Database::query($sql);
  379. // Erase user student publications (works) in the course - by André Boivin
  380. if (!empty($userList)) {
  381. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  382. foreach ($userList as $userId) {
  383. // Getting all work from user
  384. $workList = getWorkPerUser($userId);
  385. if (!empty($workList)) {
  386. foreach ($workList as $work) {
  387. $work = $work['work'];
  388. // Getting user results
  389. if (!empty($work->user_results)) {
  390. foreach ($work->user_results as $workSent) {
  391. deleteWorkItem($workSent['id'], $course_info);
  392. }
  393. }
  394. }
  395. }
  396. }
  397. }
  398. // Unsubscribe user from all blogs in the course.
  399. $sql = "DELETE FROM ".Database::get_course_table(TABLE_BLOGS_REL_USER)."
  400. WHERE c_id = $course_id AND user_id IN ($user_ids)";
  401. Database::query($sql);
  402. $sql = "DELETE FROM ".Database::get_course_table(TABLE_BLOGS_TASKS_REL_USER)."
  403. WHERE c_id = $course_id AND user_id IN ($user_ids)";
  404. Database::query($sql);
  405. // Deleting users in forum_notification and mailqueue course tables
  406. $sql = "DELETE FROM ".Database::get_course_table(TABLE_FORUM_NOTIFICATION)."
  407. WHERE c_id = $course_id AND user_id IN ($user_ids)";
  408. Database::query($sql);
  409. $sql = "DELETE FROM ".Database::get_course_table(TABLE_FORUM_MAIL_QUEUE)."
  410. WHERE c_id = $course_id AND user_id IN ($user_ids)";
  411. Database::query($sql);
  412. // Unsubscribe user from the course.
  413. if (!empty($session_id)) {
  414. foreach ($userList as $uid) {
  415. SessionManager::unSubscribeUserFromCourseSession($uid, $course_id, $session_id);
  416. // check if a user is register in the session with other course
  417. $sql = "SELECT user_id FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)."
  418. WHERE session_id = $session_id AND user_id = $uid";
  419. $rs = Database::query($sql);
  420. if (Database::num_rows($rs) == 0) {
  421. SessionManager::unsubscribe_user_from_session($uid, $session_id);
  422. }
  423. }
  424. Event::addEvent(
  425. LOG_UNSUBSCRIBE_USER_FROM_COURSE,
  426. LOG_COURSE_CODE,
  427. $course_code,
  428. api_get_utc_datetime(),
  429. $user_id,
  430. $course_id,
  431. $session_id
  432. );
  433. } else {
  434. $sql = "DELETE FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  435. WHERE
  436. user_id IN ($user_ids) AND
  437. relation_type <> ".COURSE_RELATION_TYPE_RRHH." AND
  438. c_id = $course_id";
  439. Database::query($sql);
  440. // add event to system log
  441. $user_id = api_get_user_id();
  442. Event::addEvent(
  443. LOG_UNSUBSCRIBE_USER_FROM_COURSE,
  444. LOG_COURSE_CODE,
  445. $course_code,
  446. api_get_utc_datetime(),
  447. $user_id,
  448. $course_id
  449. );
  450. foreach ($userList as $userId) {
  451. $userInfo = api_get_user_info($userId);
  452. Event::addEvent(
  453. LOG_UNSUBSCRIBE_USER_FROM_COURSE,
  454. LOG_USER_OBJECT,
  455. $userInfo,
  456. api_get_utc_datetime(),
  457. $user_id,
  458. $course_id
  459. );
  460. }
  461. }
  462. }
  463. /**
  464. * @param string $courseCode
  465. * @param int $status
  466. *
  467. * @return bool
  468. */
  469. public static function autoSubscribeToCourse($courseCode, $status = STUDENT)
  470. {
  471. $courseInfo = api_get_course_info($courseCode);
  472. if (empty($courseInfo)) {
  473. return false;
  474. }
  475. if (api_is_anonymous()) {
  476. return false;
  477. }
  478. if (in_array(
  479. $courseInfo['visibility'],
  480. [
  481. COURSE_VISIBILITY_CLOSED,
  482. COURSE_VISIBILITY_REGISTERED,
  483. COURSE_VISIBILITY_HIDDEN,
  484. ]
  485. )
  486. ) {
  487. Display::addFlash(
  488. Display::return_message(
  489. get_lang('SubscribingNotAllowed'),
  490. 'warning'
  491. )
  492. );
  493. return false;
  494. }
  495. return self::subscribeUser(api_get_user_id(), $courseInfo['code'], $status);
  496. }
  497. /**
  498. * Subscribe a user to a course. No checks are performed here to see if
  499. * course subscription is allowed.
  500. *
  501. * @param int $userId
  502. * @param string $courseCode
  503. * @param int $status (STUDENT, COURSEMANAGER, COURSE_ADMIN, NORMAL_COURSE_MEMBER)
  504. * @param int $sessionId
  505. * @param int $userCourseCategoryId
  506. * @param bool $checkTeacherPermission
  507. *
  508. * @return bool True on success, false on failure
  509. *
  510. * @assert ('', '') === false
  511. */
  512. public static function subscribeUser(
  513. $userId,
  514. $courseCode,
  515. $status = STUDENT,
  516. $sessionId = 0,
  517. $userCourseCategoryId = 0,
  518. $checkTeacherPermission = true
  519. ) {
  520. $userId = (int) $userId;
  521. $status = (int) $status;
  522. if (empty($userId) || empty($courseCode)) {
  523. return false;
  524. }
  525. $courseInfo = api_get_course_info($courseCode);
  526. if (empty($courseInfo)) {
  527. Display::addFlash(Display::return_message(get_lang('CourseDoesNotExist'), 'warning'));
  528. return false;
  529. }
  530. $userInfo = api_get_user_info($userId);
  531. if (empty($userInfo)) {
  532. Display::addFlash(Display::return_message(get_lang('UserDoesNotExist'), 'warning'));
  533. return false;
  534. }
  535. $courseId = $courseInfo['real_id'];
  536. $courseCode = $courseInfo['code'];
  537. $userCourseCategoryId = (int) $userCourseCategoryId;
  538. $sessionId = empty($sessionId) ? api_get_session_id() : (int) $sessionId;
  539. $status = $status === STUDENT || $status === COURSEMANAGER ? $status : STUDENT;
  540. $courseUserTable = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  541. if (!empty($sessionId)) {
  542. SessionManager::subscribe_users_to_session_course(
  543. [$userId],
  544. $sessionId,
  545. $courseCode
  546. );
  547. // Add event to the system log
  548. Event::addEvent(
  549. LOG_SUBSCRIBE_USER_TO_COURSE,
  550. LOG_COURSE_CODE,
  551. $courseCode,
  552. api_get_utc_datetime(),
  553. api_get_user_id(),
  554. $courseId,
  555. $sessionId
  556. );
  557. Event::addEvent(
  558. LOG_SUBSCRIBE_USER_TO_COURSE,
  559. LOG_USER_OBJECT,
  560. $userInfo,
  561. api_get_utc_datetime(),
  562. api_get_user_id(),
  563. $courseId,
  564. $sessionId
  565. );
  566. return true;
  567. } else {
  568. // Check whether the user has not been already subscribed to the course.
  569. $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  570. WHERE
  571. user_id = $userId AND
  572. relation_type <> ".COURSE_RELATION_TYPE_RRHH." AND
  573. c_id = $courseId
  574. ";
  575. if (Database::num_rows(Database::query($sql)) > 0) {
  576. Display::addFlash(Display::return_message(get_lang('AlreadyRegisteredToCourse'), 'warning'));
  577. return false;
  578. }
  579. if ($checkTeacherPermission && !api_is_course_admin()) {
  580. // Check in advance whether subscription is allowed or not for this course.
  581. if ((int) $courseInfo['subscribe'] === SUBSCRIBE_NOT_ALLOWED) {
  582. Display::addFlash(Display::return_message(get_lang('SubscriptionNotAllowed'), 'warning'));
  583. return false;
  584. }
  585. }
  586. if ($status === STUDENT) {
  587. // Check if max students per course extra field is set
  588. $extraFieldValue = new ExtraFieldValue('course');
  589. $value = $extraFieldValue->get_values_by_handler_and_field_variable($courseId, 'max_subscribed_students');
  590. if (!empty($value) && isset($value['value'])) {
  591. $maxStudents = $value['value'];
  592. if ($maxStudents !== '') {
  593. $maxStudents = (int) $maxStudents;
  594. $count = self::get_user_list_from_course_code(
  595. $courseCode,
  596. 0,
  597. null,
  598. null,
  599. STUDENT,
  600. true,
  601. false
  602. );
  603. if ($count >= $maxStudents) {
  604. Display::addFlash(Display::return_message(get_lang('MaxNumberSubscribedStudentsReached'), 'warning'));
  605. return false;
  606. }
  607. }
  608. }
  609. }
  610. $maxSort = api_max_sort_value('0', $userId);
  611. $params = [
  612. 'c_id' => $courseId,
  613. 'user_id' => $userId,
  614. 'status' => $status,
  615. 'sort' => $maxSort + 1,
  616. 'relation_type' => 0,
  617. 'user_course_cat' => (int) $userCourseCategoryId,
  618. ];
  619. $insertId = Database::insert($courseUserTable, $params);
  620. if ($insertId) {
  621. Display::addFlash(
  622. Display::return_message(
  623. sprintf(
  624. get_lang('UserXAddedToCourseX'),
  625. $userInfo['complete_name_with_username'],
  626. $courseInfo['title']
  627. )
  628. )
  629. );
  630. $send = api_get_course_setting('email_alert_to_teacher_on_new_user_in_course', $courseCode);
  631. if ($send == 1) {
  632. self::email_to_tutor(
  633. $userId,
  634. $courseInfo['real_id'],
  635. false
  636. );
  637. } elseif ($send == 2) {
  638. self::email_to_tutor(
  639. $userId,
  640. $courseInfo['real_id'],
  641. true
  642. );
  643. }
  644. // Add event to the system log
  645. Event::addEvent(
  646. LOG_SUBSCRIBE_USER_TO_COURSE,
  647. LOG_COURSE_CODE,
  648. $courseCode,
  649. api_get_utc_datetime(),
  650. api_get_user_id(),
  651. $courseId
  652. );
  653. Event::addEvent(
  654. LOG_SUBSCRIBE_USER_TO_COURSE,
  655. LOG_USER_OBJECT,
  656. $userInfo,
  657. api_get_utc_datetime(),
  658. api_get_user_id(),
  659. $courseId
  660. );
  661. return true;
  662. }
  663. return false;
  664. }
  665. }
  666. /**
  667. * Get the course id based on the original id and field name in the
  668. * extra fields. Returns 0 if course was not found.
  669. *
  670. * @param string $original_course_id_value
  671. * @param string $original_course_id_name
  672. *
  673. * @return int Course id
  674. *
  675. * @assert ('', '') === false
  676. */
  677. public static function get_course_code_from_original_id(
  678. $original_course_id_value,
  679. $original_course_id_name
  680. ) {
  681. $t_cfv = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  682. $table_field = Database::get_main_table(TABLE_EXTRA_FIELD);
  683. $extraFieldType = EntityExtraField::COURSE_FIELD_TYPE;
  684. $original_course_id_value = Database::escape_string($original_course_id_value);
  685. $original_course_id_name = Database::escape_string($original_course_id_name);
  686. $sql = "SELECT item_id
  687. FROM $table_field cf
  688. INNER JOIN $t_cfv cfv
  689. ON cfv.field_id=cf.id
  690. WHERE
  691. variable = '$original_course_id_name' AND
  692. value = '$original_course_id_value' AND
  693. cf.extra_field_type = $extraFieldType
  694. ";
  695. $res = Database::query($sql);
  696. $row = Database::fetch_object($res);
  697. if ($row) {
  698. return $row->item_id;
  699. } else {
  700. return 0;
  701. }
  702. }
  703. /**
  704. * Gets the course code from the course id. Returns null if course id was not found.
  705. *
  706. * @param int $id Course id
  707. *
  708. * @return string Course code
  709. * @assert ('') === false
  710. */
  711. public static function get_course_code_from_course_id($id)
  712. {
  713. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  714. $id = intval($id);
  715. $sql = "SELECT code FROM $table WHERE id = $id ";
  716. $res = Database::query($sql);
  717. $row = Database::fetch_object($res);
  718. if ($row) {
  719. return $row->code;
  720. } else {
  721. return null;
  722. }
  723. }
  724. /**
  725. * Add the user $userId visibility to the course $courseCode in the catalogue.
  726. *
  727. * @author David Nos (https://github.com/dnos)
  728. *
  729. * @param int $userId the id of the user
  730. * @param string $courseCode the course code
  731. * @param int $visible (optional) The course visibility in the catalogue to the user (1=visible, 0=invisible)
  732. *
  733. * @return bool true if added succesfully, false otherwise
  734. */
  735. public static function addUserVisibilityToCourseInCatalogue(
  736. $userId,
  737. $courseCode,
  738. $visible = 1
  739. ) {
  740. $debug = false;
  741. $userTable = Database::get_main_table(TABLE_MAIN_USER);
  742. $courseUserTable = Database::get_main_table(TABLE_MAIN_COURSE_CATALOGUE_USER);
  743. $visible = (int) $visible;
  744. if (empty($userId) || empty($courseCode) || ($userId != strval(intval($userId)))) {
  745. return false;
  746. }
  747. $courseCode = Database::escape_string($courseCode);
  748. $courseInfo = api_get_course_info($courseCode);
  749. $courseId = $courseInfo['real_id'];
  750. // Check in advance whether the user has already been registered on the platform.
  751. $sql = "SELECT status FROM ".$userTable." WHERE user_id = $userId ";
  752. if (Database::num_rows(Database::query($sql)) == 0) {
  753. if ($debug) {
  754. error_log('The user has not been registered to the platform');
  755. }
  756. return false; // The user has not been registered to the platform.
  757. }
  758. // Check whether the user has already been registered to the course visibility in the catalogue.
  759. $sql = "SELECT * FROM $courseUserTable
  760. WHERE
  761. user_id = $userId AND
  762. visible = $visible AND
  763. c_id = $courseId";
  764. if (Database::num_rows(Database::query($sql)) > 0) {
  765. if ($debug) {
  766. error_log('The user has been already registered to the course visibility in the catalogue');
  767. }
  768. return true; // The visibility of the user to the course in the catalogue does already exist.
  769. }
  770. // Register the user visibility to course in catalogue.
  771. $params = [
  772. 'user_id' => $userId,
  773. 'c_id' => $courseId,
  774. 'visible' => $visible,
  775. ];
  776. $insertId = Database::insert($courseUserTable, $params);
  777. return $insertId;
  778. }
  779. /**
  780. * Remove the user $userId visibility to the course $courseCode in the catalogue.
  781. *
  782. * @author David Nos (https://github.com/dnos)
  783. *
  784. * @param int $userId the id of the user
  785. * @param string $courseCode the course code
  786. * @param int $visible (optional) The course visibility in the catalogue to the user (1=visible, 0=invisible)
  787. *
  788. * @return bool true if removed succesfully or register not found, false otherwise
  789. */
  790. public static function removeUserVisibilityToCourseInCatalogue(
  791. $userId,
  792. $courseCode,
  793. $visible = 1
  794. ) {
  795. $courseUserTable = Database::get_main_table(TABLE_MAIN_COURSE_CATALOGUE_USER);
  796. if (empty($userId) || empty($courseCode) || ($userId != strval(intval($userId)))) {
  797. return false;
  798. }
  799. $courseCode = Database::escape_string($courseCode);
  800. $courseInfo = api_get_course_info($courseCode);
  801. $courseId = $courseInfo['real_id'];
  802. // Check whether the user has already been registered to the course visibility in the catalogue.
  803. $sql = "SELECT * FROM $courseUserTable
  804. WHERE
  805. user_id = $userId AND
  806. visible = $visible AND
  807. c_id = $courseId";
  808. if (Database::num_rows(Database::query($sql)) > 0) {
  809. $cond = [
  810. 'user_id = ? AND c_id = ? AND visible = ? ' => [
  811. $userId,
  812. $courseId,
  813. $visible,
  814. ],
  815. ];
  816. return Database::delete($courseUserTable, $cond);
  817. } else {
  818. return true; // Register does not exist
  819. }
  820. }
  821. /**
  822. * @param string $code
  823. *
  824. * @return bool if there already are one or more courses
  825. * with the same code OR visual_code (visualcode), false otherwise
  826. */
  827. public static function course_code_exists($code)
  828. {
  829. $code = Database::escape_string($code);
  830. $sql = "SELECT COUNT(*) as number
  831. FROM ".Database::get_main_table(TABLE_MAIN_COURSE)."
  832. WHERE code = '$code' OR visual_code = '$code'";
  833. $result = Database::fetch_array(Database::query($sql));
  834. return $result['number'] > 0;
  835. }
  836. /**
  837. * @param int $user_id
  838. * @param string $startsWith Optional
  839. *
  840. * @return array an array with the course info of all the courses (real and virtual)
  841. * of which the current user is course admin
  842. */
  843. public static function get_course_list_of_user_as_course_admin($user_id, $startsWith = '')
  844. {
  845. if ($user_id != strval(intval($user_id))) {
  846. return [];
  847. }
  848. // Definitions database tables and variables
  849. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  850. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  851. $user_id = intval($user_id);
  852. $data = [];
  853. $sql = "SELECT
  854. course.code,
  855. course.title,
  856. course.id,
  857. course.id as real_id,
  858. course.category_code
  859. FROM $tbl_course_user as course_rel_user
  860. INNER JOIN $tbl_course as course
  861. ON course.id = course_rel_user.c_id
  862. WHERE
  863. course_rel_user.user_id = $user_id AND
  864. course_rel_user.status = 1
  865. ";
  866. if (api_get_multiple_access_url()) {
  867. $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  868. $access_url_id = api_get_current_access_url_id();
  869. if ($access_url_id != -1) {
  870. $sql = "
  871. SELECT
  872. course.code,
  873. course.title,
  874. course.id,
  875. course.id as real_id
  876. FROM $tbl_course_user as course_rel_user
  877. INNER JOIN $tbl_course as course
  878. ON course.id = course_rel_user.c_id
  879. INNER JOIN $tbl_course_rel_access_url course_rel_url
  880. ON (course_rel_url.c_id = course.id)
  881. WHERE
  882. access_url_id = $access_url_id AND
  883. course_rel_user.user_id = $user_id AND
  884. course_rel_user.status = 1
  885. ";
  886. }
  887. }
  888. if (!empty($startsWith)) {
  889. $startsWith = Database::escape_string($startsWith);
  890. $sql .= " AND (course.title LIKE '$startsWith%' OR course.code LIKE '$startsWith%')";
  891. }
  892. $sql .= ' ORDER BY course.title';
  893. $result_nb_cours = Database::query($sql);
  894. if (Database::num_rows($result_nb_cours) > 0) {
  895. while ($row = Database::fetch_array($result_nb_cours, 'ASSOC')) {
  896. $data[$row['id']] = $row;
  897. }
  898. }
  899. return $data;
  900. }
  901. /**
  902. * @param int $userId
  903. * @param array $courseInfo
  904. *
  905. * @return bool|null
  906. */
  907. public static function isUserSubscribedInCourseAsDrh($userId, $courseInfo)
  908. {
  909. $userId = intval($userId);
  910. if (!api_is_drh()) {
  911. return false;
  912. }
  913. if (empty($courseInfo) || empty($userId)) {
  914. return false;
  915. }
  916. $courseId = intval($courseInfo['real_id']);
  917. $table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  918. $sql = "SELECT * FROM $table
  919. WHERE
  920. user_id = $userId AND
  921. relation_type = ".COURSE_RELATION_TYPE_RRHH." AND
  922. c_id = $courseId";
  923. $result = Database::fetch_array(Database::query($sql));
  924. if (!empty($result)) {
  925. // The user has been registered in this course.
  926. return true;
  927. }
  928. }
  929. /**
  930. * Check if user is subscribed inside a course.
  931. *
  932. * @param int $user_id
  933. * @param string $course_code , if this parameter is null, it'll check for all courses
  934. * @param bool $in_a_session True for checking inside sessions too, by default is not checked
  935. * @param int $session_id
  936. *
  937. * @return bool $session_id true if the user is registered in the course, false otherwise
  938. */
  939. public static function is_user_subscribed_in_course(
  940. $user_id,
  941. $course_code = null,
  942. $in_a_session = false,
  943. $session_id = 0
  944. ) {
  945. $user_id = (int) $user_id;
  946. $session_id = (int) $session_id;
  947. if (empty($session_id)) {
  948. $session_id = api_get_session_id();
  949. }
  950. $condition_course = '';
  951. if (isset($course_code)) {
  952. $courseInfo = api_get_course_info($course_code);
  953. if (empty($courseInfo)) {
  954. return false;
  955. }
  956. $courseId = $courseInfo['real_id'];
  957. $condition_course = " AND c_id = $courseId";
  958. }
  959. $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  960. WHERE
  961. user_id = $user_id AND
  962. relation_type<>".COURSE_RELATION_TYPE_RRHH."
  963. $condition_course ";
  964. $result = Database::fetch_array(Database::query($sql));
  965. if (!empty($result)) {
  966. // The user has been registered in this course.
  967. return true;
  968. }
  969. if (!$in_a_session) {
  970. // The user has not been registered in this course.
  971. return false;
  972. }
  973. $tableSessionCourseUser = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  974. $sql = "SELECT 1 FROM $tableSessionCourseUser
  975. WHERE user_id = $user_id AND session_id = $session_id $condition_course";
  976. if (Database::num_rows(Database::query($sql)) > 0) {
  977. return true;
  978. }
  979. $sql = "SELECT 1 FROM $tableSessionCourseUser
  980. WHERE user_id = $user_id AND session_id = $session_id AND status = 2 $condition_course";
  981. if (Database::num_rows(Database::query($sql)) > 0) {
  982. return true;
  983. }
  984. $sql = 'SELECT 1 FROM '.Database::get_main_table(TABLE_MAIN_SESSION).
  985. " WHERE id = $session_id AND id_coach = $user_id";
  986. if (Database::num_rows(Database::query($sql)) > 0) {
  987. return true;
  988. }
  989. return false;
  990. }
  991. /**
  992. * Is the user a teacher in the given course?
  993. *
  994. * @param int $user_id , the id (int) of the user
  995. * @param string $course_code , the course code
  996. *
  997. * @return bool if the user is a teacher in the course, false otherwise
  998. */
  999. public static function is_course_teacher($user_id, $course_code)
  1000. {
  1001. if ($user_id != strval(intval($user_id))) {
  1002. return false;
  1003. }
  1004. $courseInfo = api_get_course_info($course_code);
  1005. if (empty($courseInfo)) {
  1006. return false;
  1007. }
  1008. $courseId = $courseInfo['real_id'];
  1009. $sql = "SELECT status FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  1010. WHERE c_id = $courseId AND user_id = $user_id ";
  1011. $result = Database::query($sql);
  1012. if (Database::num_rows($result) > 0) {
  1013. return Database::result($result, 0, 'status') == 1;
  1014. }
  1015. return false;
  1016. }
  1017. /**
  1018. * Is the user subscribed in the real course or linked courses?
  1019. *
  1020. * @param int the id of the user
  1021. * @param int $courseId
  1022. *
  1023. * @deprecated linked_courses definition doesn't exists
  1024. *
  1025. * @return bool if the user is registered in the real course or linked courses, false otherwise
  1026. */
  1027. public static function is_user_subscribed_in_real_or_linked_course($user_id, $courseId, $session_id = 0)
  1028. {
  1029. if ($user_id != strval(intval($user_id))) {
  1030. return false;
  1031. }
  1032. $courseId = intval($courseId);
  1033. $session_id = intval($session_id);
  1034. if (empty($session_id)) {
  1035. $result = Database::fetch_array(
  1036. Database::query(
  1037. "SELECT *
  1038. FROM ".Database::get_main_table(TABLE_MAIN_COURSE)." course
  1039. LEFT JOIN ".Database::get_main_table(TABLE_MAIN_COURSE_USER)." course_user
  1040. ON course.id = course_user.c_id
  1041. WHERE
  1042. course_user.user_id = $user_id AND
  1043. course_user.relation_type<>".COURSE_RELATION_TYPE_RRHH." AND
  1044. ( course.id = $courseId)"
  1045. )
  1046. );
  1047. return !empty($result);
  1048. }
  1049. // From here we trust session id.
  1050. // Is he/she subscribed to the session's course?
  1051. // A user?
  1052. if (Database::num_rows(Database::query("SELECT user_id
  1053. FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)."
  1054. WHERE session_id = $session_id
  1055. AND user_id = $user_id"))
  1056. ) {
  1057. return true;
  1058. }
  1059. // A course coach?
  1060. if (Database::num_rows(Database::query("SELECT user_id
  1061. FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)."
  1062. WHERE session_id = $session_id
  1063. AND user_id = $user_id AND status = 2
  1064. AND c_id = $courseId"))
  1065. ) {
  1066. return true;
  1067. }
  1068. // A session coach?
  1069. if (Database::num_rows(Database::query("SELECT id_coach
  1070. FROM ".Database::get_main_table(TABLE_MAIN_SESSION)." AS session
  1071. WHERE session.id = $session_id
  1072. AND id_coach = $user_id"))
  1073. ) {
  1074. return true;
  1075. }
  1076. return false;
  1077. }
  1078. /**
  1079. * Return user info array of all users registered in a course
  1080. * This only returns the users that are registered in this actual course, not linked courses.
  1081. *
  1082. * @param string $course_code
  1083. * @param int $session_id
  1084. * @param string $limit
  1085. * @param string $order_by the field to order the users by.
  1086. * Valid values are 'lastname', 'firstname', 'username', 'email', 'official_code' OR a part of a SQL statement
  1087. * that starts with ORDER BY ...
  1088. * @param int|null $filter_by_status if using the session_id: 0 or 2 (student, coach),
  1089. * if using session_id = 0 STUDENT or COURSEMANAGER
  1090. * @param bool|null $return_count
  1091. * @param bool $add_reports
  1092. * @param bool $resumed_report
  1093. * @param array $extra_field
  1094. * @param array $courseCodeList
  1095. * @param array $userIdList
  1096. * @param string $filterByActive
  1097. * @param array $sessionIdList
  1098. * @param string $searchByKeyword
  1099. *
  1100. * @return array|int
  1101. */
  1102. public static function get_user_list_from_course_code(
  1103. $course_code = null,
  1104. $session_id = 0,
  1105. $limit = null,
  1106. $order_by = null,
  1107. $filter_by_status = null,
  1108. $return_count = null,
  1109. $add_reports = false,
  1110. $resumed_report = false,
  1111. $extra_field = [],
  1112. $courseCodeList = [],
  1113. $userIdList = [],
  1114. $filterByActive = null,
  1115. $sessionIdList = [],
  1116. $searchByKeyword = ''
  1117. ) {
  1118. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  1119. $sessionTable = Database::get_main_table(TABLE_MAIN_SESSION);
  1120. $session_id = (int) $session_id;
  1121. $course_code = Database::escape_string($course_code);
  1122. $courseInfo = api_get_course_info($course_code);
  1123. $courseId = 0;
  1124. if (!empty($courseInfo)) {
  1125. $courseId = $courseInfo['real_id'];
  1126. }
  1127. $where = [];
  1128. if (empty($order_by)) {
  1129. $order_by = 'user.lastname, user.firstname';
  1130. if (api_is_western_name_order()) {
  1131. $order_by = 'user.firstname, user.lastname';
  1132. }
  1133. }
  1134. // if the $order_by does not contain 'ORDER BY'
  1135. // we have to check if it is a valid field that can be sorted on
  1136. if (!strstr($order_by, 'ORDER BY')) {
  1137. if (!empty($order_by)) {
  1138. $order_by = "ORDER BY $order_by";
  1139. } else {
  1140. $order_by = '';
  1141. }
  1142. }
  1143. $filter_by_status_condition = null;
  1144. if (!empty($session_id) || !empty($sessionIdList)) {
  1145. $sql = 'SELECT DISTINCT
  1146. user.user_id,
  1147. user.email,
  1148. session_course_user.status as status_session,
  1149. session_id,
  1150. user.*,
  1151. course.*,
  1152. course.id AS c_id,
  1153. session.name as session_name
  1154. ';
  1155. if ($return_count) {
  1156. $sql = " SELECT COUNT(user.user_id) as count";
  1157. }
  1158. $sessionCondition = " session_course_user.session_id = $session_id";
  1159. if (!empty($sessionIdList)) {
  1160. $sessionIdListTostring = implode("','", array_map('intval', $sessionIdList));
  1161. $sessionCondition = " session_course_user.session_id IN ('$sessionIdListTostring') ";
  1162. }
  1163. $courseCondition = " course.id = $courseId";
  1164. if (!empty($courseCodeList)) {
  1165. $courseCodeListForSession = array_map(['Database', 'escape_string'], $courseCodeList);
  1166. $courseCodeListForSession = implode('","', $courseCodeListForSession);
  1167. $courseCondition = " course.code IN ('$courseCodeListForSession') ";
  1168. }
  1169. $sql .= ' FROM '.Database::get_main_table(TABLE_MAIN_USER).' as user ';
  1170. $sql .= " LEFT JOIN ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)." as session_course_user
  1171. ON
  1172. user.id = session_course_user.user_id AND
  1173. $sessionCondition
  1174. INNER JOIN $course_table course
  1175. ON session_course_user.c_id = course.id AND
  1176. $courseCondition
  1177. INNER JOIN $sessionTable session
  1178. ON session_course_user.session_id = session.id
  1179. ";
  1180. $where[] = ' session_course_user.c_id IS NOT NULL ';
  1181. // 2 = coach
  1182. // 0 = student
  1183. if (isset($filter_by_status)) {
  1184. $filter_by_status = intval($filter_by_status);
  1185. $filter_by_status_condition = " session_course_user.status = $filter_by_status AND ";
  1186. }
  1187. } else {
  1188. if ($return_count) {
  1189. $sql = " SELECT COUNT(*) as count";
  1190. } else {
  1191. if (empty($course_code)) {
  1192. $sql = 'SELECT DISTINCT
  1193. course.title,
  1194. course.code,
  1195. course.id AS c_id,
  1196. course_rel_user.status as status_rel,
  1197. user.id as user_id,
  1198. user.email,
  1199. course_rel_user.is_tutor,
  1200. user.* ';
  1201. } else {
  1202. $sql = 'SELECT DISTINCT
  1203. course_rel_user.status as status_rel,
  1204. user.id as user_id,
  1205. user.email,
  1206. course_rel_user.is_tutor,
  1207. user.* ';
  1208. }
  1209. }
  1210. $sql .= " FROM ".Database::get_main_table(TABLE_MAIN_USER)." as user
  1211. LEFT JOIN ".Database::get_main_table(TABLE_MAIN_COURSE_USER)." as course_rel_user
  1212. ON
  1213. user.id = course_rel_user.user_id AND
  1214. course_rel_user.relation_type <> ".COURSE_RELATION_TYPE_RRHH."
  1215. INNER JOIN $course_table course
  1216. ON course_rel_user.c_id = course.id ";
  1217. if (!empty($course_code)) {
  1218. $sql .= " AND course_rel_user.c_id = $courseId";
  1219. }
  1220. $where[] = ' course_rel_user.c_id IS NOT NULL ';
  1221. if (isset($filter_by_status) && is_numeric($filter_by_status)) {
  1222. $filter_by_status = (int) $filter_by_status;
  1223. $filter_by_status_condition = " course_rel_user.status = $filter_by_status AND ";
  1224. }
  1225. }
  1226. $multiple_access_url = api_get_multiple_access_url();
  1227. if ($multiple_access_url) {
  1228. $sql .= ' LEFT JOIN '.Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER).' au
  1229. ON (au.user_id = user.id) ';
  1230. }
  1231. $extraFieldWasAdded = false;
  1232. if ($return_count && $resumed_report) {
  1233. foreach ($extra_field as $extraField) {
  1234. $extraFieldInfo = UserManager::get_extra_field_information_by_name($extraField);
  1235. if (!empty($extraFieldInfo)) {
  1236. $fieldValuesTable = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  1237. $sql .= " LEFT JOIN $fieldValuesTable as ufv
  1238. ON (
  1239. user.id = ufv.item_id AND
  1240. (field_id = ".$extraFieldInfo['id']." OR field_id IS NULL)
  1241. )";
  1242. $extraFieldWasAdded = true;
  1243. }
  1244. }
  1245. }
  1246. $sql .= " WHERE $filter_by_status_condition ".implode(' OR ', $where);
  1247. if ($multiple_access_url) {
  1248. $current_access_url_id = api_get_current_access_url_id();
  1249. $sql .= " AND (access_url_id = $current_access_url_id ) ";
  1250. }
  1251. if ($return_count && $resumed_report && $extraFieldWasAdded) {
  1252. $sql .= ' AND field_id IS NOT NULL GROUP BY value ';
  1253. }
  1254. if (!empty($courseCodeList)) {
  1255. $courseCodeList = array_map(['Database', 'escape_string'], $courseCodeList);
  1256. $courseCodeList = implode('","', $courseCodeList);
  1257. if (empty($sessionIdList)) {
  1258. $sql .= ' AND course.code IN ("'.$courseCodeList.'")';
  1259. }
  1260. }
  1261. if (!empty($userIdList)) {
  1262. $userIdList = array_map('intval', $userIdList);
  1263. $userIdList = implode('","', $userIdList);
  1264. $sql .= ' AND user.id IN ("'.$userIdList.'")';
  1265. }
  1266. if (isset($filterByActive)) {
  1267. $filterByActive = (int) $filterByActive;
  1268. $sql .= " AND user.active = $filterByActive";
  1269. }
  1270. if (!empty($searchByKeyword)) {
  1271. $searchByKeyword = Database::escape_string($searchByKeyword);
  1272. $sql .= " AND (
  1273. user.firstname LIKE '$searchByKeyword' OR
  1274. user.username LIKE '$searchByKeyword' OR
  1275. user.lastname LIKE '$searchByKeyword'
  1276. ) ";
  1277. }
  1278. $sql .= " $order_by $limit";
  1279. $rs = Database::query($sql);
  1280. $users = [];
  1281. $extra_fields = UserManager::get_extra_fields(
  1282. 0,
  1283. 100,
  1284. null,
  1285. null,
  1286. true,
  1287. true
  1288. );
  1289. $counter = 1;
  1290. $count_rows = Database::num_rows($rs);
  1291. if ($return_count && $resumed_report) {
  1292. return $count_rows;
  1293. }
  1294. $table_user_field_value = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  1295. $tableExtraField = Database::get_main_table(TABLE_EXTRA_FIELD);
  1296. if ($count_rows) {
  1297. while ($user = Database::fetch_array($rs)) {
  1298. if ($return_count) {
  1299. return $user['count'];
  1300. }
  1301. $report_info = [];
  1302. $user_info = $user;
  1303. $user_info['status'] = $user['status'];
  1304. if (isset($user['is_tutor'])) {
  1305. $user_info['is_tutor'] = $user['is_tutor'];
  1306. }
  1307. if (!empty($session_id)) {
  1308. $user_info['status_session'] = $user['status_session'];
  1309. }
  1310. $sessionId = isset($user['session_id']) ? $user['session_id'] : 0;
  1311. $course_code = isset($user['code']) ? $user['code'] : null;
  1312. if ($add_reports) {
  1313. if ($resumed_report) {
  1314. $extra = [];
  1315. if (!empty($extra_fields)) {
  1316. foreach ($extra_fields as $extra) {
  1317. if (in_array($extra['1'], $extra_field)) {
  1318. $user_data = UserManager::get_extra_user_data_by_field(
  1319. $user['user_id'],
  1320. $extra['1']
  1321. );
  1322. break;
  1323. }
  1324. }
  1325. }
  1326. $row_key = '-1';
  1327. $name = '-';
  1328. if (!empty($extra)) {
  1329. if (!empty($user_data[$extra['1']])) {
  1330. $row_key = $user_data[$extra['1']];
  1331. $name = $user_data[$extra['1']];
  1332. $users[$row_key]['extra_'.$extra['1']] = $name;
  1333. }
  1334. }
  1335. if (empty($users[$row_key])) {
  1336. $users[$row_key] = [];
  1337. }
  1338. if (!array_key_exists('training_hours', $users[$row_key])) {
  1339. $users[$row_key]['training_hours'] = 0;
  1340. }
  1341. $users[$row_key]['training_hours'] += Tracking::get_time_spent_on_the_course(
  1342. $user['user_id'],
  1343. $courseId,
  1344. $sessionId
  1345. );
  1346. if (!array_key_exists('count_users', $users[$row_key])) {
  1347. $users[$row_key]['count_users'] = 0;
  1348. }
  1349. $users[$row_key]['count_users'] += $counter;
  1350. $registered_users_with_extra_field = self::getCountRegisteredUsersWithCourseExtraField(
  1351. $name,
  1352. $tableExtraField,
  1353. $table_user_field_value
  1354. );
  1355. $users[$row_key]['count_users_registered'] = $registered_users_with_extra_field;
  1356. $users[$row_key]['average_hours_per_user'] = $users[$row_key]['training_hours'] / $users[$row_key]['count_users'];
  1357. $category = Category:: load(
  1358. null,
  1359. null,
  1360. $course_code,
  1361. null,
  1362. null,
  1363. $sessionId
  1364. );
  1365. if (!isset($users[$row_key]['count_certificates'])) {
  1366. $users[$row_key]['count_certificates'] = 0;
  1367. }
  1368. if (isset($category[0]) && $category[0]->is_certificate_available($user['user_id'])) {
  1369. $users[$row_key]['count_certificates']++;
  1370. }
  1371. foreach ($extra_fields as $extra) {
  1372. if ($extra['1'] == 'ruc') {
  1373. continue;
  1374. }
  1375. if (!isset($users[$row_key][$extra['1']])) {
  1376. $user_data = UserManager::get_extra_user_data_by_field($user['user_id'], $extra['1']);
  1377. if (!empty($user_data[$extra['1']])) {
  1378. $users[$row_key][$extra['1']] = $user_data[$extra['1']];
  1379. }
  1380. }
  1381. }
  1382. } else {
  1383. $sessionName = !empty($sessionId) ? ' - '.$user['session_name'] : '';
  1384. $report_info['course'] = $user['title'].$sessionName;
  1385. $report_info['user'] = api_get_person_name($user['firstname'], $user['lastname']);
  1386. $report_info['email'] = $user['email'];
  1387. $report_info['time'] = api_time_to_hms(
  1388. Tracking::get_time_spent_on_the_course(
  1389. $user['user_id'],
  1390. empty($user['c_id']) ? $courseId : $user['c_id'],
  1391. $sessionId
  1392. )
  1393. );
  1394. $category = Category:: load(
  1395. null,
  1396. null,
  1397. $course_code,
  1398. null,
  1399. null,
  1400. $sessionId
  1401. );
  1402. $report_info['certificate'] = Display::label(get_lang('No'));
  1403. if (isset($category[0]) && $category[0]->is_certificate_available($user['user_id'])) {
  1404. $report_info['certificate'] = Display::label(get_lang('Yes'), 'success');
  1405. }
  1406. $progress = intval(
  1407. Tracking::get_avg_student_progress(
  1408. $user['user_id'],
  1409. $course_code,
  1410. [],
  1411. $sessionId
  1412. )
  1413. );
  1414. $report_info['progress_100'] = $progress == 100 ? Display::label(get_lang('Yes'), 'success') : Display::label(get_lang('No'));
  1415. $report_info['progress'] = $progress."%";
  1416. foreach ($extra_fields as $extra) {
  1417. $user_data = UserManager::get_extra_user_data_by_field($user['user_id'], $extra['1']);
  1418. $report_info[$extra['1']] = $user_data[$extra['1']];
  1419. }
  1420. $report_info['user_id'] = $user['user_id'];
  1421. $users[] = $report_info;
  1422. }
  1423. } else {
  1424. $users[$user['user_id']] = $user_info;
  1425. }
  1426. }
  1427. }
  1428. return $users;
  1429. }
  1430. /**
  1431. * @param bool $resumed_report
  1432. * @param array $extra_field
  1433. * @param array $courseCodeList
  1434. * @param array $userIdList
  1435. * @param array $sessionIdList
  1436. *
  1437. * @return array|int
  1438. */
  1439. public static function get_count_user_list_from_course_code(
  1440. $resumed_report = false,
  1441. $extra_field = [],
  1442. $courseCodeList = [],
  1443. $userIdList = [],
  1444. $sessionIdList = []
  1445. ) {
  1446. return self::get_user_list_from_course_code(
  1447. null,
  1448. 0,
  1449. null,
  1450. null,
  1451. null,
  1452. true,
  1453. false,
  1454. $resumed_report,
  1455. $extra_field,
  1456. $courseCodeList,
  1457. $userIdList,
  1458. null,
  1459. $sessionIdList
  1460. );
  1461. }
  1462. /**
  1463. * Gets subscribed users in a course or in a course/session.
  1464. *
  1465. * @param string $course_code
  1466. * @param int $session_id
  1467. *
  1468. * @return int
  1469. */
  1470. public static function get_users_count_in_course(
  1471. $course_code,
  1472. $session_id = 0,
  1473. $status = null
  1474. ) {
  1475. // variable initialisation
  1476. $session_id = (int) $session_id;
  1477. $course_code = Database::escape_string($course_code);
  1478. $tblUser = Database::get_main_table(TABLE_MAIN_USER);
  1479. $tblSessionCourseUser = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1480. $tblCourseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1481. $tblUrlUser = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  1482. $courseInfo = api_get_course_info($course_code);
  1483. $courseId = $courseInfo['real_id'];
  1484. $sql = "
  1485. SELECT DISTINCT count(user.id) as count
  1486. FROM $tblUser as user
  1487. ";
  1488. $where = [];
  1489. if (!empty($session_id)) {
  1490. $sql .= "
  1491. LEFT JOIN $tblSessionCourseUser as session_course_user
  1492. ON user.user_id = session_course_user.user_id
  1493. AND session_course_user.c_id = $courseId
  1494. AND session_course_user.session_id = $session_id
  1495. ";
  1496. $where[] = ' session_course_user.c_id IS NOT NULL ';
  1497. } else {
  1498. $sql .= "
  1499. LEFT JOIN $tblCourseUser as course_rel_user
  1500. ON user.user_id = course_rel_user.user_id
  1501. AND course_rel_user.relation_type <> ".COURSE_RELATION_TYPE_RRHH."
  1502. AND course_rel_user.c_id = $courseId
  1503. ";
  1504. $where[] = ' course_rel_user.c_id IS NOT NULL ';
  1505. }
  1506. $multiple_access_url = api_get_multiple_access_url();
  1507. if ($multiple_access_url) {
  1508. $sql .= " LEFT JOIN $tblUrlUser au ON (au.user_id = user.user_id) ";
  1509. }
  1510. $sql .= ' WHERE '.implode(' OR ', $where);
  1511. if ($multiple_access_url) {
  1512. $current_access_url_id = api_get_current_access_url_id();
  1513. $sql .= " AND (access_url_id = $current_access_url_id ) ";
  1514. }
  1515. $rs = Database::query($sql);
  1516. $count = 0;
  1517. if (Database::num_rows($rs)) {
  1518. $user = Database::fetch_array($rs);
  1519. $count = $user['count'];
  1520. }
  1521. return $count;
  1522. }
  1523. /**
  1524. * Get a list of coaches of a course and a session.
  1525. *
  1526. * @param string $course_code
  1527. * @param int $session_id
  1528. * @param bool $addGeneralCoach
  1529. *
  1530. * @return array List of users
  1531. */
  1532. public static function get_coach_list_from_course_code(
  1533. $course_code,
  1534. $session_id,
  1535. $addGeneralCoach = true
  1536. ) {
  1537. if (empty($course_code) || empty($session_id)) {
  1538. return [];
  1539. }
  1540. $course_code = Database::escape_string($course_code);
  1541. $courseInfo = api_get_course_info($course_code);
  1542. $courseId = $courseInfo['real_id'];
  1543. $session_id = (int) $session_id;
  1544. $users = [];
  1545. // We get the coach for the given course in a given session.
  1546. $sql = 'SELECT user_id FROM '.Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER).
  1547. " WHERE session_id = $session_id AND c_id = $courseId AND status = 2";
  1548. $rs = Database::query($sql);
  1549. while ($user = Database::fetch_array($rs)) {
  1550. $userInfo = api_get_user_info($user['user_id']);
  1551. if ($userInfo) {
  1552. $users[$user['user_id']] = $userInfo;
  1553. }
  1554. }
  1555. if ($addGeneralCoach) {
  1556. $table = Database::get_main_table(TABLE_MAIN_SESSION);
  1557. // We get the session coach.
  1558. $sql = "SELECT id_coach FROM $table WHERE id = $session_id";
  1559. $rs = Database::query($sql);
  1560. $session_id_coach = Database::result($rs, 0, 'id_coach');
  1561. if (is_int($session_id_coach)) {
  1562. $userInfo = api_get_user_info($session_id_coach);
  1563. if ($userInfo) {
  1564. $users[$session_id_coach] = $userInfo;
  1565. }
  1566. }
  1567. }
  1568. return $users;
  1569. }
  1570. /**
  1571. * Return user info array of all users registered in a course
  1572. * This only returns the users that are registered in this actual course, not linked courses.
  1573. *
  1574. * @param string $course_code
  1575. * @param bool $with_session
  1576. * @param int $session_id
  1577. * @param string $date_from
  1578. * @param string $date_to
  1579. * @param bool $includeInvitedUsers Whether include the invited users
  1580. * @param int $groupId
  1581. *
  1582. * @return array with user id
  1583. */
  1584. public static function get_student_list_from_course_code(
  1585. $course_code,
  1586. $with_session = false,
  1587. $session_id = 0,
  1588. $date_from = null,
  1589. $date_to = null,
  1590. $includeInvitedUsers = true,
  1591. $groupId = 0
  1592. ) {
  1593. $userTable = Database::get_main_table(TABLE_MAIN_USER);
  1594. $session_id = (int) $session_id;
  1595. $courseInfo = api_get_course_info($course_code);
  1596. if (empty($courseInfo)) {
  1597. return [];
  1598. }
  1599. $courseId = $courseInfo['real_id'];
  1600. $students = [];
  1601. if ($session_id == 0) {
  1602. if (empty($groupId)) {
  1603. // students directly subscribed to the course
  1604. $sql = "SELECT *
  1605. FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)." cu
  1606. INNER JOIN $userTable u
  1607. ON cu.user_id = u.user_id
  1608. WHERE c_id = $courseId AND cu.status = ".STUDENT;
  1609. if (!$includeInvitedUsers) {
  1610. $sql .= " AND u.status != ".INVITEE;
  1611. }
  1612. $rs = Database::query($sql);
  1613. while ($student = Database::fetch_array($rs)) {
  1614. $students[$student['user_id']] = $student;
  1615. }
  1616. } else {
  1617. $students = GroupManager::get_users(
  1618. $groupId,
  1619. false,
  1620. null,
  1621. null,
  1622. false,
  1623. $courseInfo['real_id']
  1624. );
  1625. $students = array_flip($students);
  1626. }
  1627. }
  1628. // students subscribed to the course through a session
  1629. if ($with_session) {
  1630. $joinSession = '';
  1631. //Session creation date
  1632. if (!empty($date_from) && !empty($date_to)) {
  1633. $joinSession = "INNER JOIN ".Database::get_main_table(TABLE_MAIN_SESSION)." s";
  1634. }
  1635. $sql_query = "SELECT *
  1636. FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)." scu
  1637. $joinSession
  1638. INNER JOIN $userTable u ON scu.user_id = u.user_id
  1639. WHERE scu.c_id = $courseId AND scu.status <> 2";
  1640. if (!empty($date_from) && !empty($date_to)) {
  1641. $date_from = Database::escape_string($date_from);
  1642. $date_to = Database::escape_string($date_to);
  1643. $sql_query .= " AND s.access_start_date >= '$date_from' AND s.access_end_date <= '$date_to'";
  1644. }
  1645. if ($session_id != 0) {
  1646. $sql_query .= " AND scu.session_id = $session_id";
  1647. }
  1648. if (!$includeInvitedUsers) {
  1649. $sql_query .= " AND u.status != ".INVITEE;
  1650. }
  1651. $rs = Database::query($sql_query);
  1652. while ($student = Database::fetch_array($rs)) {
  1653. $students[$student['user_id']] = $student;
  1654. }
  1655. }
  1656. return $students;
  1657. }
  1658. /**
  1659. * Return user info array of all teacher-users registered in a course
  1660. * This only returns the users that are registered in this actual course, not linked courses.
  1661. *
  1662. * @param string $course_code
  1663. *
  1664. * @return array with user id
  1665. */
  1666. public static function get_teacher_list_from_course_code($course_code)
  1667. {
  1668. $courseInfo = api_get_course_info($course_code);
  1669. $courseId = $courseInfo['real_id'];
  1670. if (empty($courseId)) {
  1671. return false;
  1672. }
  1673. $sql = "SELECT DISTINCT
  1674. u.id as user_id,
  1675. u.lastname,
  1676. u.firstname,
  1677. u.email,
  1678. u.username,
  1679. u.status
  1680. FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)." cu
  1681. INNER JOIN ".Database::get_main_table(TABLE_MAIN_USER)." u
  1682. ON (cu.user_id = u.id)
  1683. WHERE
  1684. cu.c_id = $courseId AND
  1685. cu.status = 1 ";
  1686. $rs = Database::query($sql);
  1687. $teachers = [];
  1688. while ($teacher = Database::fetch_array($rs)) {
  1689. $teachers[$teacher['user_id']] = $teacher;
  1690. }
  1691. return $teachers;
  1692. }
  1693. /**
  1694. * Return user info array of all teacher-users registered in a course
  1695. * This only returns the users that are registered in this actual course, not linked courses.
  1696. *
  1697. * @param int $courseId
  1698. * @param bool $loadAvatars
  1699. *
  1700. * @return array with user id
  1701. */
  1702. public static function getTeachersFromCourse($courseId, $loadAvatars = true)
  1703. {
  1704. $courseId = (int) $courseId;
  1705. if (empty($courseId)) {
  1706. return false;
  1707. }
  1708. $sql = "SELECT DISTINCT
  1709. u.id as user_id,
  1710. u.lastname,
  1711. u.firstname,
  1712. u.email,
  1713. u.username,
  1714. u.status
  1715. FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)." cu
  1716. INNER JOIN ".Database::get_main_table(TABLE_MAIN_USER)." u
  1717. ON (cu.user_id = u.id)
  1718. WHERE
  1719. cu.c_id = $courseId AND
  1720. cu.status = 1 ";
  1721. $rs = Database::query($sql);
  1722. $listTeachers = [];
  1723. $teachers = [];
  1724. $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=get_user_popup&course_id='.$courseId;
  1725. while ($teacher = Database::fetch_array($rs)) {
  1726. $teachers['id'] = $teacher['user_id'];
  1727. $teachers['lastname'] = $teacher['lastname'];
  1728. $teachers['firstname'] = $teacher['firstname'];
  1729. $teachers['email'] = $teacher['email'];
  1730. $teachers['username'] = $teacher['username'];
  1731. $teachers['status'] = $teacher['status'];
  1732. $teachers['fullname'] = api_get_person_name($teacher['firstname'], $teacher['lastname']);
  1733. $teachers['avatar'] = '';
  1734. if ($loadAvatars) {
  1735. $userPicture = UserManager::getUserPicture($teacher['user_id'], USER_IMAGE_SIZE_SMALL);
  1736. $teachers['avatar'] = $userPicture;
  1737. }
  1738. $teachers['url'] = $url.'&user_id='.$teacher['user_id'];
  1739. $listTeachers[] = $teachers;
  1740. }
  1741. return $listTeachers;
  1742. }
  1743. /**
  1744. * Returns a string list of teachers assigned to the given course.
  1745. *
  1746. * @param string $course_code
  1747. * @param string $separator between teachers names
  1748. * @param bool $add_link_to_profile Whether to add a link to the teacher's profile
  1749. * @param bool $orderList
  1750. *
  1751. * @return string List of teachers teaching the course
  1752. */
  1753. public static function getTeacherListFromCourseCodeToString(
  1754. $course_code,
  1755. $separator = self::USER_SEPARATOR,
  1756. $add_link_to_profile = false,
  1757. $orderList = false
  1758. ) {
  1759. $teacher_list = self::get_teacher_list_from_course_code($course_code);
  1760. $html = '';
  1761. $list = [];
  1762. if (!empty($teacher_list)) {
  1763. foreach ($teacher_list as $teacher) {
  1764. $teacher_name = api_get_person_name(
  1765. $teacher['firstname'],
  1766. $teacher['lastname']
  1767. );
  1768. if ($add_link_to_profile) {
  1769. $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=get_user_popup&user_id='.$teacher['user_id'];
  1770. $teacher_name = Display::url(
  1771. $teacher_name,
  1772. $url,
  1773. [
  1774. 'class' => 'ajax',
  1775. 'data-title' => $teacher_name,
  1776. ]
  1777. );
  1778. }
  1779. $list[] = $teacher_name;
  1780. }
  1781. if (!empty($list)) {
  1782. if ($orderList === true) {
  1783. $html .= '<ul class="user-teacher">';
  1784. foreach ($list as $teacher) {
  1785. $html .= '<li>';
  1786. $html .= Display::return_icon('teacher.png', '', null, ICON_SIZE_TINY);
  1787. $html .= ' '.$teacher;
  1788. $html .= '</li>';
  1789. }
  1790. $html .= '</ul>';
  1791. } else {
  1792. $html .= array_to_string($list, $separator);
  1793. }
  1794. }
  1795. }
  1796. return $html;
  1797. }
  1798. /**
  1799. * This function returns information about coachs from a course in session.
  1800. *
  1801. * @param int $session_id
  1802. * @param int $courseId
  1803. *
  1804. * @return array containing user_id, lastname, firstname, username
  1805. */
  1806. public static function get_coachs_from_course($session_id = 0, $courseId = 0)
  1807. {
  1808. if (!empty($session_id)) {
  1809. $session_id = intval($session_id);
  1810. } else {
  1811. $session_id = api_get_session_id();
  1812. }
  1813. if (!empty($courseId)) {
  1814. $courseId = intval($courseId);
  1815. } else {
  1816. $courseId = api_get_course_int_id();
  1817. }
  1818. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  1819. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1820. $sql = "SELECT DISTINCT
  1821. u.user_id,
  1822. u.lastname,
  1823. u.firstname,
  1824. u.username
  1825. FROM $tbl_user u
  1826. INNER JOIN $tbl_session_course_user scu
  1827. ON (u.user_id = scu.user_id)
  1828. WHERE
  1829. scu.session_id = $session_id AND
  1830. scu.c_id = $courseId AND
  1831. scu.status = 2";
  1832. $rs = Database::query($sql);
  1833. $coaches = [];
  1834. if (Database::num_rows($rs) > 0) {
  1835. while ($row = Database::fetch_array($rs)) {
  1836. $completeName = api_get_person_name($row['firstname'], $row['lastname']);
  1837. $coaches[] = $row + ['full_name' => $completeName];
  1838. }
  1839. }
  1840. return $coaches;
  1841. }
  1842. /**
  1843. * @param int $session_id
  1844. * @param int $courseId
  1845. * @param string $separator
  1846. * @param bool $add_link_to_profile
  1847. * @param bool $orderList
  1848. *
  1849. * @return string
  1850. */
  1851. public static function get_coachs_from_course_to_string(
  1852. $session_id = 0,
  1853. $courseId = 0,
  1854. $separator = self::USER_SEPARATOR,
  1855. $add_link_to_profile = false,
  1856. $orderList = false
  1857. ) {
  1858. $coachList = self::get_coachs_from_course($session_id, $courseId);
  1859. $course_coachs = [];
  1860. if (!empty($coachList)) {
  1861. foreach ($coachList as $coach_course) {
  1862. $coach_name = $coach_course['full_name'];
  1863. if ($add_link_to_profile) {
  1864. $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=get_user_popup&user_id='.$coach_course['user_id'].'&course_id='.$courseId.'&session_id='.$session_id;
  1865. $coach_name = Display::url(
  1866. $coach_name,
  1867. $url,
  1868. [
  1869. 'class' => 'ajax',
  1870. 'data-title' => $coach_name,
  1871. ]
  1872. );
  1873. }
  1874. $course_coachs[] = $coach_name;
  1875. }
  1876. }
  1877. $html = '';
  1878. if (!empty($course_coachs)) {
  1879. if ($orderList === true) {
  1880. $html .= '<ul class="user-coachs">';
  1881. foreach ($course_coachs as $coachs) {
  1882. $html .= Display::tag(
  1883. 'li',
  1884. Display::return_icon(
  1885. 'teacher.png',
  1886. get_lang('Coach'),
  1887. null,
  1888. ICON_SIZE_TINY
  1889. ).' '.$coachs
  1890. );
  1891. }
  1892. $html .= '</ul>';
  1893. } else {
  1894. $html = array_to_string($course_coachs, $separator);
  1895. }
  1896. }
  1897. return $html;
  1898. }
  1899. /**
  1900. * Get the list of groups from the course.
  1901. *
  1902. * @param string $course_code
  1903. * @param int $session_id Session ID (optional)
  1904. * @param int $in_get_empty_group get empty groups (optional)
  1905. *
  1906. * @return array List of groups info
  1907. */
  1908. public static function get_group_list_of_course(
  1909. $course_code,
  1910. $session_id = 0,
  1911. $in_get_empty_group = 0
  1912. ) {
  1913. $course_info = api_get_course_info($course_code);
  1914. if (empty($course_info)) {
  1915. return [];
  1916. }
  1917. $course_id = $course_info['real_id'];
  1918. if (empty($course_id)) {
  1919. return [];
  1920. }
  1921. $session_id != 0 ? $session_condition = ' WHERE g.session_id IN(1,'.intval($session_id).')' : $session_condition = ' WHERE g.session_id = 0';
  1922. if ($in_get_empty_group == 0) {
  1923. // get only groups that are not empty
  1924. $sql = "SELECT DISTINCT g.id, g.iid, g.name
  1925. FROM ".Database::get_course_table(TABLE_GROUP)." AS g
  1926. INNER JOIN ".Database::get_course_table(TABLE_GROUP_USER)." gu
  1927. ON (g.id = gu.group_id AND g.c_id = $course_id AND gu.c_id = $course_id)
  1928. $session_condition
  1929. ORDER BY g.name";
  1930. } else {
  1931. // get all groups even if they are empty
  1932. $sql = "SELECT g.id, g.name, g.iid
  1933. FROM ".Database::get_course_table(TABLE_GROUP)." AS g
  1934. $session_condition
  1935. AND c_id = $course_id";
  1936. }
  1937. $result = Database::query($sql);
  1938. $groupList = [];
  1939. while ($groupData = Database::fetch_array($result)) {
  1940. $groupData['userNb'] = GroupManager::number_of_students($groupData['id'], $course_id);
  1941. $groupList[$groupData['iid']] = $groupData;
  1942. }
  1943. return $groupList;
  1944. }
  1945. /**
  1946. * Delete a course
  1947. * This function deletes a whole course-area from the platform. When the
  1948. * given course is a virtual course, the database and directory will not be
  1949. * deleted.
  1950. * When the given course is a real course, also all virtual courses refering
  1951. * to the given course will be deleted.
  1952. * Considering the fact that we remove all traces of the course in the main
  1953. * database, it makes sense to remove all tracking as well (if stats databases exist)
  1954. * so that a new course created with this code would not use the remains of an older
  1955. * course.
  1956. *
  1957. * @param string $code The code of the course to delete
  1958. *
  1959. * @todo When deleting a virtual course: unsubscribe users from that virtual
  1960. * course from the groups in the real course if they are not subscribed in
  1961. * that real course.
  1962. */
  1963. public static function delete_course($code)
  1964. {
  1965. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  1966. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1967. $table_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  1968. $table_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1969. $table_course_survey = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY);
  1970. $table_course_survey_question = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  1971. $table_course_survey_question_option = Database::get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  1972. $table_course_rel_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  1973. $table_stats_hotpots = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
  1974. $table_stats_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  1975. $table_stats_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  1976. $table_stats_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  1977. $table_stats_lastaccess = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  1978. $table_stats_course_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1979. $table_stats_online = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  1980. $table_stats_default = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
  1981. $table_stats_downloads = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  1982. $table_stats_links = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LINKS);
  1983. $table_stats_uploads = Database::get_main_table(TABLE_STATISTIC_TRACK_E_UPLOADS);
  1984. if (empty($code)) {
  1985. return false;
  1986. }
  1987. $codeFiltered = Database::escape_string($code);
  1988. $sql = "SELECT * FROM $table_course
  1989. WHERE code = '$codeFiltered'";
  1990. $res = Database::query($sql);
  1991. if (Database::num_rows($res) == 0) {
  1992. return;
  1993. }
  1994. $course = Database::fetch_array($res);
  1995. $courseId = $course['id']; // int
  1996. $count = 0;
  1997. if (api_is_multiple_url_enabled()) {
  1998. $url_id = 1;
  1999. if (api_get_current_access_url_id() != -1) {
  2000. $url_id = api_get_current_access_url_id();
  2001. }
  2002. UrlManager::delete_url_rel_course($courseId, $url_id);
  2003. $count = UrlManager::getCountUrlRelCourse($courseId);
  2004. }
  2005. if ($count == 0) {
  2006. self::create_database_dump($code);
  2007. $course_tables = AddCourse::get_course_tables();
  2008. // Cleaning group categories
  2009. $groupCategories = GroupManager::get_categories($course['code']);
  2010. if (!empty($groupCategories)) {
  2011. foreach ($groupCategories as $category) {
  2012. GroupManager::delete_category($category['id'], $course['code']);
  2013. }
  2014. }
  2015. // Cleaning groups
  2016. $groups = GroupManager::get_groups($courseId);
  2017. if (!empty($groups)) {
  2018. foreach ($groups as $group) {
  2019. GroupManager::deleteGroup($group, $course['code']);
  2020. }
  2021. }
  2022. // Cleaning c_x tables
  2023. if (!empty($courseId)) {
  2024. foreach ($course_tables as $table) {
  2025. $table = Database::get_course_table($table);
  2026. $sql = "DELETE FROM $table WHERE c_id = $courseId ";
  2027. Database::query($sql);
  2028. }
  2029. }
  2030. $course_dir = api_get_path(SYS_COURSE_PATH).$course['directory'];
  2031. $archive_dir = api_get_path(SYS_ARCHIVE_PATH).$course['directory'].'_'.time();
  2032. if (is_dir($course_dir)) {
  2033. rename($course_dir, $archive_dir);
  2034. }
  2035. Category::deleteFromCourse($course['code']);
  2036. // Unsubscribe all users from the course
  2037. $sql = "DELETE FROM $table_course_user WHERE c_id = $courseId";
  2038. Database::query($sql);
  2039. // Delete the course from the sessions tables
  2040. $sql = "DELETE FROM $table_session_course WHERE c_id = $courseId";
  2041. Database::query($sql);
  2042. $sql = "DELETE FROM $table_session_course_user WHERE c_id = $courseId";
  2043. Database::query($sql);
  2044. // Delete from Course - URL
  2045. $sql = "DELETE FROM $table_course_rel_url WHERE c_id = $courseId";
  2046. Database::query($sql);
  2047. $sql = "SELECT survey_id FROM $table_course_survey WHERE course_code = '$codeFiltered'";
  2048. $result_surveys = Database::query($sql);
  2049. while ($surveys = Database::fetch_array($result_surveys)) {
  2050. $survey_id = $surveys[0]; //int
  2051. $sql = "DELETE FROM $table_course_survey_question WHERE survey_id = $survey_id";
  2052. Database::query($sql);
  2053. $sql = "DELETE FROM $table_course_survey_question_option WHERE survey_id = $survey_id";
  2054. Database::query($sql);
  2055. $sql = "DELETE FROM $table_course_survey WHERE survey_id = $survey_id";
  2056. Database::query($sql);
  2057. }
  2058. // Delete the course from the stats tables
  2059. $sql = "DELETE FROM $table_stats_hotpots WHERE c_id = $courseId";
  2060. Database::query($sql);
  2061. $sql = "DELETE FROM $table_stats_attempt WHERE c_id = $courseId";
  2062. Database::query($sql);
  2063. $sql = "DELETE FROM $table_stats_exercises WHERE c_id = $courseId";
  2064. Database::query($sql);
  2065. $sql = "DELETE FROM $table_stats_access WHERE c_id = $courseId";
  2066. Database::query($sql);
  2067. $sql = "DELETE FROM $table_stats_lastaccess WHERE c_id = $courseId";
  2068. Database::query($sql);
  2069. $sql = "DELETE FROM $table_stats_course_access WHERE c_id = $courseId";
  2070. Database::query($sql);
  2071. $sql = "DELETE FROM $table_stats_online WHERE c_id = $courseId";
  2072. Database::query($sql);
  2073. // Do not delete rows from track_e_default as these include course
  2074. // creation and other important things that do not take much space
  2075. // but give information on the course history
  2076. //$sql = "DELETE FROM $table_stats_default WHERE c_id = $courseId";
  2077. //Database::query($sql);
  2078. $sql = "DELETE FROM $table_stats_downloads WHERE c_id = $courseId";
  2079. Database::query($sql);
  2080. $sql = "DELETE FROM $table_stats_links WHERE c_id = $courseId";
  2081. Database::query($sql);
  2082. $sql = "DELETE FROM $table_stats_uploads WHERE c_id = $courseId";
  2083. Database::query($sql);
  2084. // Update ticket
  2085. $table = Database::get_main_table(TABLE_TICKET_TICKET);
  2086. $sql = "UPDATE $table SET course_id = NULL WHERE course_id = $courseId";
  2087. Database::query($sql);
  2088. // Class
  2089. $table = Database::get_main_table(TABLE_USERGROUP_REL_COURSE);
  2090. $sql = "DELETE FROM $table
  2091. WHERE course_id = $courseId";
  2092. Database::query($sql);
  2093. // Skills
  2094. $table = Database::get_main_table(TABLE_MAIN_SKILL_REL_USER);
  2095. $argumentation = Database::escape_string(sprintf(get_lang('SkillFromCourseXDeletedSinceThen'), $course['code']));
  2096. $sql = "UPDATE $table SET course_id = NULL, session_id = NULL, argumentation = '$argumentation'
  2097. WHERE course_id = $courseId";
  2098. Database::query($sql);
  2099. if (api_get_configuration_value('allow_skill_rel_items')) {
  2100. $sql = "DELETE FROM skill_rel_course WHERE c_id = $courseId";
  2101. Database::query($sql);
  2102. }
  2103. // Deletes all groups, group-users, group-tutors information
  2104. // To prevent fK mix up on some tables
  2105. GroupManager::deleteAllGroupsFromCourse($courseId);
  2106. $app_plugin = new AppPlugin();
  2107. $app_plugin->performActionsWhenDeletingItem('course', $courseId);
  2108. // Delete the course from the database
  2109. $sql = "DELETE FROM $table_course WHERE id = $courseId";
  2110. Database::query($sql);
  2111. // delete extra course fields
  2112. $extraFieldValues = new ExtraFieldValue('course');
  2113. $extraFieldValues->deleteValuesByItem($courseId);
  2114. // Add event to system log
  2115. Event::addEvent(
  2116. LOG_COURSE_DELETE,
  2117. LOG_COURSE_CODE,
  2118. $code,
  2119. api_get_utc_datetime(),
  2120. api_get_user_id(),
  2121. $courseId
  2122. );
  2123. }
  2124. }
  2125. /**
  2126. * Creates a file called mysql_dump.sql in the course folder.
  2127. *
  2128. * @param string $course_code The code of the course
  2129. *
  2130. * @todo Implementation for single database
  2131. */
  2132. public static function create_database_dump($course_code)
  2133. {
  2134. $sql_dump = '';
  2135. $course_code = Database::escape_string($course_code);
  2136. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  2137. $sql = "SELECT * FROM $table_course WHERE code = '$course_code'";
  2138. $res = Database::query($sql);
  2139. $course = Database::fetch_array($res);
  2140. $course_tables = AddCourse::get_course_tables();
  2141. if (!empty($course['id'])) {
  2142. //Cleaning c_x tables
  2143. foreach ($course_tables as $table) {
  2144. $table = Database::get_course_table($table);
  2145. $sql = "SELECT * FROM $table WHERE c_id = {$course['id']} ";
  2146. $res_table = Database::query($sql);
  2147. while ($row = Database::fetch_array($res_table, 'ASSOC')) {
  2148. $row_to_save = [];
  2149. foreach ($row as $key => $value) {
  2150. $row_to_save[$key] = $key."='".Database::escape_string($row[$key])."'";
  2151. }
  2152. $sql_dump .= "\nINSERT INTO $table SET ".implode(', ', $row_to_save).';';
  2153. }
  2154. }
  2155. }
  2156. if (is_dir(api_get_path(SYS_COURSE_PATH).$course['directory'])) {
  2157. $file_name = api_get_path(SYS_COURSE_PATH).$course['directory'].'/mysql_dump.sql';
  2158. $handle = fopen($file_name, 'a+');
  2159. if ($handle !== false) {
  2160. fwrite($handle, $sql_dump);
  2161. fclose($handle);
  2162. } else {
  2163. //TODO trigger exception in a try-catch
  2164. }
  2165. }
  2166. }
  2167. /**
  2168. * Sort courses for a specific user ??
  2169. *
  2170. * @param int $user_id User ID
  2171. * @param string $course_code Course code
  2172. *
  2173. * @return int Minimum course order
  2174. *
  2175. * @todo Review documentation
  2176. */
  2177. public static function userCourseSort($user_id, $course_code)
  2178. {
  2179. if ($user_id != strval(intval($user_id))) {
  2180. return false;
  2181. }
  2182. $course_code = Database::escape_string($course_code);
  2183. $TABLECOURSE = Database::get_main_table(TABLE_MAIN_COURSE);
  2184. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2185. $course_title = Database::result(
  2186. Database::query(
  2187. "SELECT title FROM $TABLECOURSE WHERE code = '$course_code'"
  2188. ),
  2189. 0,
  2190. 0
  2191. );
  2192. if ($course_title === false) {
  2193. $course_title = '';
  2194. }
  2195. $sql = "SELECT course.code as code, course.title as title, cu.sort as sort
  2196. FROM $TABLECOURSUSER as cu, $TABLECOURSE as course
  2197. WHERE course.id = cu.c_id AND user_id = $user_id AND
  2198. cu.relation_type <> ".COURSE_RELATION_TYPE_RRHH." AND
  2199. user_course_cat = 0
  2200. ORDER BY cu.sort";
  2201. $result = Database::query($sql);
  2202. $course_title_precedent = '';
  2203. $counter = 0;
  2204. $course_found = false;
  2205. $course_sort = 1;
  2206. if (Database::num_rows($result) > 0) {
  2207. while ($courses = Database::fetch_array($result)) {
  2208. if ($course_title_precedent == '') {
  2209. $course_title_precedent = $courses['title'];
  2210. }
  2211. if (api_strcasecmp($course_title_precedent, $course_title) < 0) {
  2212. $course_found = true;
  2213. $course_sort = $courses['sort'];
  2214. if ($counter == 0) {
  2215. $sql = "UPDATE $TABLECOURSUSER
  2216. SET sort = sort+1
  2217. WHERE
  2218. user_id= $user_id AND
  2219. relation_type <> ".COURSE_RELATION_TYPE_RRHH."
  2220. AND user_course_cat = 0
  2221. AND sort > $course_sort";
  2222. $course_sort++;
  2223. } else {
  2224. $sql = "UPDATE $TABLECOURSUSER SET sort = sort+1
  2225. WHERE
  2226. user_id= $user_id AND
  2227. relation_type <> ".COURSE_RELATION_TYPE_RRHH." AND
  2228. user_course_cat = 0 AND
  2229. sort >= $course_sort";
  2230. }
  2231. Database::query($sql);
  2232. break;
  2233. } else {
  2234. $course_title_precedent = $courses['title'];
  2235. }
  2236. $counter++;
  2237. }
  2238. // We must register the course in the beginning of the list
  2239. if (!$course_found) {
  2240. $course_sort = Database::result(
  2241. Database::query(
  2242. 'SELECT min(sort) as min_sort FROM '.$TABLECOURSUSER.' WHERE user_id = "'.$user_id.'" AND user_course_cat="0"'
  2243. ),
  2244. 0,
  2245. 0
  2246. );
  2247. Database::query("UPDATE $TABLECOURSUSER SET sort = sort+1 WHERE user_id = $user_id AND user_course_cat = 0");
  2248. }
  2249. }
  2250. return $course_sort;
  2251. }
  2252. /**
  2253. * check if course exists.
  2254. *
  2255. * @param string $courseCode
  2256. *
  2257. * @return int if exists, false else
  2258. */
  2259. public static function course_exists($courseCode)
  2260. {
  2261. $courseCode = Database::escape_string($courseCode);
  2262. $sql = "SELECT 1 FROM ".Database::get_main_table(TABLE_MAIN_COURSE)."
  2263. WHERE code = '$courseCode'";
  2264. return Database::num_rows(Database::query($sql));
  2265. }
  2266. /**
  2267. * Send an email to tutor after the auth-suscription of a student in your course.
  2268. *
  2269. * @author Carlos Vargas <carlos.vargas@dokeos.com>, Dokeos Latino
  2270. *
  2271. * @param int $user_id the id of the user
  2272. * @param string $courseId the course code
  2273. * @param bool $send_to_tutor_also
  2274. *
  2275. * @return false|null we return the message that is displayed when the action is successful
  2276. */
  2277. public static function email_to_tutor($user_id, $courseId, $send_to_tutor_also = false)
  2278. {
  2279. $user_id = (int) $user_id;
  2280. $courseId = (int) $courseId;
  2281. $information = api_get_course_info_by_id($courseId);
  2282. $course_code = $information['code'];
  2283. $student = api_get_user_info($user_id);
  2284. $name_course = $information['title'];
  2285. $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  2286. WHERE c_id = $courseId";
  2287. // TODO: Ivan: This is a mistake, please, have a look at it. Intention here is diffcult to be guessed.
  2288. //if ($send_to_tutor_also = true)
  2289. // Proposed change:
  2290. if ($send_to_tutor_also) {
  2291. $sql .= ' AND is_tutor = 1';
  2292. } else {
  2293. $sql .= ' AND status = 1';
  2294. }
  2295. $result = Database::query($sql);
  2296. while ($row = Database::fetch_array($result)) {
  2297. $tutor = api_get_user_info($row['user_id']);
  2298. $emailto = $tutor['email'];
  2299. $emailsubject = get_lang('NewUserInTheCourse').': '.$name_course;
  2300. $emailbody = get_lang('Dear').': '.api_get_person_name($tutor['firstname'], $tutor['lastname'])."\n";
  2301. $emailbody .= get_lang('MessageNewUserInTheCourse').': '.$name_course."\n";
  2302. $emailbody .= get_lang('UserName').': '.$student['username']."\n";
  2303. if (api_is_western_name_order()) {
  2304. $emailbody .= get_lang('FirstName').': '.$student['firstname']."\n";
  2305. $emailbody .= get_lang('LastName').': '.$student['lastname']."\n";
  2306. } else {
  2307. $emailbody .= get_lang('LastName').': '.$student['lastname']."\n";
  2308. $emailbody .= get_lang('FirstName').': '.$student['firstname']."\n";
  2309. }
  2310. $emailbody .= get_lang('Email').': <a href="mailto:'.$student['email'].'">'.$student['email']."</a>\n\n";
  2311. $recipient_name = api_get_person_name(
  2312. $tutor['firstname'],
  2313. $tutor['lastname'],
  2314. null,
  2315. PERSON_NAME_EMAIL_ADDRESS
  2316. );
  2317. $sender_name = api_get_person_name(
  2318. api_get_setting('administratorName'),
  2319. api_get_setting('administratorSurname'),
  2320. null,
  2321. PERSON_NAME_EMAIL_ADDRESS
  2322. );
  2323. $email_admin = api_get_setting('emailAdministrator');
  2324. $additionalParameters = [
  2325. 'smsType' => SmsPlugin::NEW_USER_SUBSCRIBED_COURSE,
  2326. 'userId' => $tutor['user_id'],
  2327. 'userUsername' => $student['username'],
  2328. 'courseCode' => $course_code,
  2329. ];
  2330. api_mail_html(
  2331. $recipient_name,
  2332. $emailto,
  2333. $emailsubject,
  2334. $emailbody,
  2335. $sender_name,
  2336. $email_admin,
  2337. null,
  2338. null,
  2339. null,
  2340. $additionalParameters
  2341. );
  2342. }
  2343. }
  2344. /**
  2345. * @return array
  2346. */
  2347. public static function get_special_course_list()
  2348. {
  2349. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  2350. $tbl_course_field = Database::get_main_table(TABLE_EXTRA_FIELD);
  2351. $tbl_course_field_value = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  2352. //we filter the courses from the URL
  2353. $join_access_url = $where_access_url = '';
  2354. if (api_get_multiple_access_url()) {
  2355. $access_url_id = api_get_current_access_url_id();
  2356. if ($access_url_id != -1) {
  2357. $tbl_url_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  2358. $join_access_url = "LEFT JOIN $tbl_url_course url_rel_course
  2359. ON url_rel_course.c_id = tcfv.item_id ";
  2360. $where_access_url = " AND access_url_id = $access_url_id ";
  2361. }
  2362. }
  2363. $extraFieldType = EntityExtraField::COURSE_FIELD_TYPE;
  2364. // get course list auto-register
  2365. $sql = "SELECT DISTINCT(c.id)
  2366. FROM $tbl_course_field_value tcfv
  2367. INNER JOIN $tbl_course_field tcf
  2368. ON tcfv.field_id = tcf.id $join_access_url
  2369. INNER JOIN $courseTable c
  2370. ON (c.id = tcfv.item_id)
  2371. WHERE
  2372. tcf.extra_field_type = $extraFieldType AND
  2373. tcf.variable = 'special_course' AND
  2374. tcfv.value = 1 $where_access_url";
  2375. $result = Database::query($sql);
  2376. $courseList = [];
  2377. if (Database::num_rows($result) > 0) {
  2378. while ($row = Database::fetch_array($result)) {
  2379. $courseList[] = $row['id'];
  2380. }
  2381. }
  2382. return $courseList;
  2383. }
  2384. /**
  2385. * Get the course codes that have been restricted in the catalogue, and if byUserId is set
  2386. * then the courses that the user is allowed or not to see in catalogue.
  2387. *
  2388. * @param bool $allowed Either if the courses have some users that are or are not allowed to see in catalogue
  2389. * @param int $byUserId if the courses are or are not allowed to see to the user
  2390. *
  2391. * @return array Course codes allowed or not to see in catalogue by some user or the user
  2392. */
  2393. public static function getCatalogueCourseList($allowed = true, $byUserId = -1)
  2394. {
  2395. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  2396. $tblCourseRelUserCatalogue = Database::get_main_table(TABLE_MAIN_COURSE_CATALOGUE_USER);
  2397. $visibility = $allowed ? 1 : 0;
  2398. // Restriction by user id
  2399. $currentUserRestriction = '';
  2400. if ($byUserId > 0) {
  2401. $byUserId = (int) $byUserId;
  2402. $currentUserRestriction = " AND tcruc.user_id = $byUserId ";
  2403. }
  2404. //we filter the courses from the URL
  2405. $joinAccessUrl = '';
  2406. $whereAccessUrl = '';
  2407. if (api_get_multiple_access_url()) {
  2408. $accessUrlId = api_get_current_access_url_id();
  2409. if ($accessUrlId != -1) {
  2410. $tblUrlCourse = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  2411. $joinAccessUrl = "LEFT JOIN $tblUrlCourse url_rel_course
  2412. ON url_rel_course.c_id = c.id ";
  2413. $whereAccessUrl = " AND access_url_id = $accessUrlId ";
  2414. }
  2415. }
  2416. // get course list auto-register
  2417. $sql = "SELECT DISTINCT(c.code)
  2418. FROM $tblCourseRelUserCatalogue tcruc
  2419. INNER JOIN $courseTable c
  2420. ON (c.id = tcruc.c_id) $joinAccessUrl
  2421. WHERE tcruc.visible = $visibility $currentUserRestriction $whereAccessUrl";
  2422. $result = Database::query($sql);
  2423. $courseList = [];
  2424. if (Database::num_rows($result) > 0) {
  2425. while ($resultRow = Database::fetch_array($result)) {
  2426. $courseList[] = $resultRow['code'];
  2427. }
  2428. }
  2429. return $courseList;
  2430. }
  2431. /**
  2432. * Get list of courses for a given user.
  2433. *
  2434. * @param int $user_id
  2435. * @param bool $include_sessions Whether to include courses from session or not
  2436. * @param bool $adminGetsAllCourses If the user is platform admin,
  2437. * whether he gets all the courses or just his. Note: This does *not* include all sessions
  2438. * @param bool $loadSpecialCourses
  2439. * @param array $skipCourseList List of course ids to skip
  2440. * @param bool $useUserLanguageFilterIfAvailable
  2441. * @param bool $showCoursesSessionWithDifferentKey
  2442. *
  2443. * @return array List of codes and db name
  2444. *
  2445. * @author isaac flores paz
  2446. */
  2447. public static function get_courses_list_by_user_id(
  2448. $user_id,
  2449. $include_sessions = false,
  2450. $adminGetsAllCourses = false,
  2451. $loadSpecialCourses = true,
  2452. $skipCourseList = [],
  2453. $useUserLanguageFilterIfAvailable = true,
  2454. $showCoursesSessionWithDifferentKey = false
  2455. ) {
  2456. $user_id = intval($user_id);
  2457. $urlId = api_get_current_access_url_id();
  2458. $course_list = [];
  2459. $codes = [];
  2460. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  2461. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2462. $tbl_user_course_category = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  2463. $tableCourseUrl = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  2464. $languageCondition = '';
  2465. $onlyInUserLanguage = api_get_configuration_value('my_courses_show_courses_in_user_language_only');
  2466. if ($useUserLanguageFilterIfAvailable && $onlyInUserLanguage) {
  2467. $userInfo = api_get_user_info(api_get_user_id());
  2468. if (!empty($userInfo['language'])) {
  2469. $languageCondition = " AND course.course_language = '".$userInfo['language']."' ";
  2470. }
  2471. }
  2472. if ($adminGetsAllCourses && UserManager::is_admin($user_id)) {
  2473. // get the whole courses list
  2474. $sql = "SELECT DISTINCT(course.code), course.id as real_id, course.title
  2475. FROM $tbl_course course
  2476. INNER JOIN $tableCourseUrl url
  2477. ON (course.id = url.c_id)
  2478. WHERE
  2479. url.access_url_id = $urlId
  2480. $languageCondition
  2481. ";
  2482. } else {
  2483. $withSpecialCourses = $withoutSpecialCourses = '';
  2484. if ($loadSpecialCourses) {
  2485. $specialCourseList = self::get_special_course_list();
  2486. if (!empty($specialCourseList)) {
  2487. $specialCourseToString = '"'.implode('","', $specialCourseList).'"';
  2488. $withSpecialCourses = ' AND course.id IN ('.$specialCourseToString.')';
  2489. $withoutSpecialCourses = ' AND course.id NOT IN ('.$specialCourseToString.')';
  2490. }
  2491. if (!empty($withSpecialCourses)) {
  2492. $sql = "SELECT DISTINCT (course.code),
  2493. course.id as real_id,
  2494. course.category_code AS category,
  2495. course.title
  2496. FROM $tbl_course_user course_rel_user
  2497. LEFT JOIN $tbl_course course
  2498. ON course.id = course_rel_user.c_id
  2499. LEFT JOIN $tbl_user_course_category user_course_category
  2500. ON course_rel_user.user_course_cat = user_course_category.id
  2501. INNER JOIN $tableCourseUrl url
  2502. ON (course.id = url.c_id)
  2503. WHERE url.access_url_id = $urlId
  2504. $withSpecialCourses
  2505. $languageCondition
  2506. GROUP BY course.code
  2507. ORDER BY user_course_category.sort, course.title, course_rel_user.sort ASC
  2508. ";
  2509. $result = Database::query($sql);
  2510. if (Database::num_rows($result) > 0) {
  2511. while ($result_row = Database::fetch_array($result, 'ASSOC')) {
  2512. $result_row['special_course'] = 1;
  2513. $course_list[] = $result_row;
  2514. $codes[] = $result_row['real_id'];
  2515. }
  2516. }
  2517. }
  2518. }
  2519. // get course list not auto-register. Use Distinct to avoid multiple
  2520. // entries when a course is assigned to a HRD (DRH) as watcher
  2521. $sql = "SELECT
  2522. DISTINCT(course.code),
  2523. course.id as real_id,
  2524. course.category_code AS category,
  2525. course.title
  2526. FROM $tbl_course course
  2527. INNER JOIN $tbl_course_user cru
  2528. ON (course.id = cru.c_id)
  2529. INNER JOIN $tableCourseUrl url
  2530. ON (course.id = url.c_id)
  2531. WHERE
  2532. url.access_url_id = $urlId AND
  2533. cru.user_id = $user_id
  2534. $withoutSpecialCourses
  2535. $languageCondition
  2536. ORDER BY course.title
  2537. ";
  2538. }
  2539. $result = Database::query($sql);
  2540. if (Database::num_rows($result)) {
  2541. while ($row = Database::fetch_array($result, 'ASSOC')) {
  2542. if (!empty($skipCourseList)) {
  2543. if (in_array($row['real_id'], $skipCourseList)) {
  2544. continue;
  2545. }
  2546. }
  2547. $course_list[] = $row;
  2548. $codes[] = $row['real_id'];
  2549. }
  2550. }
  2551. if ($include_sessions === true) {
  2552. $sql = "SELECT DISTINCT (c.code),
  2553. c.id as real_id,
  2554. c.category_code AS category,
  2555. s.id as session_id,
  2556. s.name as session_name
  2557. FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER)." scu
  2558. INNER JOIN $tbl_course c
  2559. ON (scu.c_id = c.id)
  2560. INNER JOIN ".Database::get_main_table(TABLE_MAIN_SESSION)." s
  2561. ON (s.id = scu.session_id)
  2562. WHERE user_id = $user_id ";
  2563. $r = Database::query($sql);
  2564. while ($row = Database::fetch_array($r, 'ASSOC')) {
  2565. if (!empty($skipCourseList)) {
  2566. if (in_array($row['real_id'], $skipCourseList)) {
  2567. continue;
  2568. }
  2569. }
  2570. if ($showCoursesSessionWithDifferentKey) {
  2571. $course_list[] = $row;
  2572. } else {
  2573. if (!in_array($row['real_id'], $codes)) {
  2574. $course_list[] = $row;
  2575. }
  2576. }
  2577. }
  2578. }
  2579. return $course_list;
  2580. }
  2581. /**
  2582. * Get course ID from a given course directory name.
  2583. *
  2584. * @param string $path Course directory (without any slash)
  2585. *
  2586. * @return string Course code, or false if not found
  2587. */
  2588. public static function getCourseCodeFromDirectory($path)
  2589. {
  2590. $path = Database::escape_string(str_replace('.', '', str_replace('/', '', $path)));
  2591. $res = Database::query("SELECT code FROM ".Database::get_main_table(TABLE_MAIN_COURSE)."
  2592. WHERE directory LIKE BINARY '$path'");
  2593. if ($res === false) {
  2594. return false;
  2595. }
  2596. if (Database::num_rows($res) != 1) {
  2597. return false;
  2598. }
  2599. $row = Database::fetch_array($res);
  2600. return $row['code'];
  2601. }
  2602. /**
  2603. * Get course code(s) from visual code.
  2604. *
  2605. * @deprecated
  2606. *
  2607. * @param string Visual code
  2608. *
  2609. * @return array List of codes for the given visual code
  2610. */
  2611. public static function get_courses_info_from_visual_code($code)
  2612. {
  2613. $result = [];
  2614. $sql_result = Database::query("SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE)."
  2615. WHERE visual_code = '".Database::escape_string($code)."'");
  2616. while ($virtual_course = Database::fetch_array($sql_result)) {
  2617. $result[] = $virtual_course;
  2618. }
  2619. return $result;
  2620. }
  2621. /**
  2622. * Creates a new extra field for a given course.
  2623. *
  2624. * @param string $variable Field's internal variable name
  2625. * @param int $fieldType Field's type
  2626. * @param string $displayText Field's language var name
  2627. * @param string $default Optional. The default value
  2628. *
  2629. * @return int New extra field ID
  2630. */
  2631. public static function create_course_extra_field($variable, $fieldType, $displayText, $default = '')
  2632. {
  2633. $extraField = new ExtraField('course');
  2634. $params = [
  2635. 'variable' => $variable,
  2636. 'field_type' => $fieldType,
  2637. 'display_text' => $displayText,
  2638. 'default_value' => $default,
  2639. ];
  2640. return $extraField->save($params);
  2641. }
  2642. /**
  2643. * Update course attributes. Will only update attributes with a non-empty value.
  2644. * Note that you NEED to check that your attributes are valid before using this function.
  2645. *
  2646. * @param int Course id
  2647. * @param array Associative array with field names as keys and field values as values
  2648. *
  2649. * @return Doctrine\DBAL\Driver\Statement|null True if update was successful, false otherwise
  2650. */
  2651. public static function update_attributes($id, $attributes)
  2652. {
  2653. $id = (int) $id;
  2654. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  2655. $sql = "UPDATE $table SET ";
  2656. $i = 0;
  2657. foreach ($attributes as $name => $value) {
  2658. if ($value != '') {
  2659. if ($i > 0) {
  2660. $sql .= ", ";
  2661. }
  2662. $sql .= " $name = '".Database::escape_string($value)."'";
  2663. $i++;
  2664. }
  2665. }
  2666. $sql .= " WHERE id = $id";
  2667. return Database::query($sql);
  2668. }
  2669. /**
  2670. * Update an extra field value for a given course.
  2671. *
  2672. * @param string $course_code Course code
  2673. * @param string $variable Field variable name
  2674. * @param string $value Optional. Default field value
  2675. *
  2676. * @return bool|int An integer when register a new extra field. And boolean when update the extrafield
  2677. */
  2678. public static function update_course_extra_field_value($course_code, $variable, $value = '')
  2679. {
  2680. $courseInfo = api_get_course_info($course_code);
  2681. $courseId = $courseInfo['real_id'];
  2682. $extraFieldValues = new ExtraFieldValue('course');
  2683. $params = [
  2684. 'item_id' => $courseId,
  2685. 'variable' => $variable,
  2686. 'value' => $value,
  2687. ];
  2688. return $extraFieldValues->save($params);
  2689. }
  2690. /**
  2691. * @param int $sessionId
  2692. *
  2693. * @return mixed
  2694. */
  2695. public static function get_session_category_id_by_session_id($sessionId)
  2696. {
  2697. if (empty($sessionId)) {
  2698. return [];
  2699. }
  2700. $sessionId = intval($sessionId);
  2701. $sql = 'SELECT sc.id session_category
  2702. FROM '.Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY).' sc
  2703. INNER JOIN '.Database::get_main_table(TABLE_MAIN_SESSION).' s
  2704. ON sc.id = s.session_category_id
  2705. WHERE s.id = '.$sessionId;
  2706. return Database::result(
  2707. Database::query($sql),
  2708. 0,
  2709. 'session_category'
  2710. );
  2711. }
  2712. /**
  2713. * Gets the value of a course extra field. Returns null if it was not found.
  2714. *
  2715. * @param string $variable Name of the extra field
  2716. * @param string $code Course code
  2717. *
  2718. * @return string Value
  2719. */
  2720. public static function get_course_extra_field_value($variable, $code)
  2721. {
  2722. $courseInfo = api_get_course_info($code);
  2723. $courseId = $courseInfo['real_id'];
  2724. $extraFieldValues = new ExtraFieldValue('course');
  2725. $result = $extraFieldValues->get_values_by_handler_and_field_variable($courseId, $variable);
  2726. if (!empty($result['value'])) {
  2727. return $result['value'];
  2728. }
  2729. return null;
  2730. }
  2731. /**
  2732. * Lists details of the course description.
  2733. *
  2734. * @param array The course description
  2735. * @param string The encoding
  2736. * @param bool If true is displayed if false is hidden
  2737. *
  2738. * @return string The course description in html
  2739. */
  2740. public static function get_details_course_description_html(
  2741. $descriptions,
  2742. $charset,
  2743. $action_show = true
  2744. ) {
  2745. $data = null;
  2746. if (isset($descriptions) && count($descriptions) > 0) {
  2747. foreach ($descriptions as $description) {
  2748. $data .= '<div class="sectiontitle">';
  2749. if (api_is_allowed_to_edit() && $action_show) {
  2750. //delete
  2751. $data .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=delete&description_id='.$description->id.'" onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(
  2752. get_lang('ConfirmYourChoice'),
  2753. ENT_QUOTES,
  2754. $charset
  2755. )).'\')) return false;">';
  2756. $data .= Display::return_icon(
  2757. 'delete.gif',
  2758. get_lang('Delete'),
  2759. ['style' => 'vertical-align:middle;float:right;']
  2760. );
  2761. $data .= '</a> ';
  2762. //edit
  2763. $data .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&description_id='.$description->id.'">';
  2764. $data .= Display::return_icon(
  2765. 'edit.png',
  2766. get_lang('Edit'),
  2767. ['style' => 'vertical-align:middle;float:right; padding-right:4px;'],
  2768. ICON_SIZE_SMALL
  2769. );
  2770. $data .= '</a> ';
  2771. }
  2772. $data .= $description->title;
  2773. $data .= '</div>';
  2774. $data .= '<div class="sectioncomment">';
  2775. $data .= Security::remove_XSS($description->content);
  2776. $data .= '</div>';
  2777. }
  2778. } else {
  2779. $data .= '<em>'.get_lang('ThisCourseDescriptionIsEmpty').'</em>';
  2780. }
  2781. return $data;
  2782. }
  2783. /**
  2784. * Returns the details of a course category.
  2785. *
  2786. * @param string $code Category code
  2787. *
  2788. * @return array Course category
  2789. */
  2790. public static function get_course_category($code)
  2791. {
  2792. $table = Database::get_main_table(TABLE_MAIN_CATEGORY);
  2793. $code = Database::escape_string($code);
  2794. $sql = "SELECT * FROM $table WHERE code = '$code'";
  2795. return Database::fetch_array(Database::query($sql));
  2796. }
  2797. /**
  2798. * Subscribes courses to human resource manager (Dashboard feature).
  2799. *
  2800. * @param int $hr_manager_id Human Resource Manager id
  2801. * @param array $courses_list Courses code
  2802. *
  2803. * @return int
  2804. */
  2805. public static function subscribeCoursesToDrhManager($hr_manager_id, $courses_list)
  2806. {
  2807. $tbl_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2808. $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  2809. $hr_manager_id = intval($hr_manager_id);
  2810. $affected_rows = 0;
  2811. //Deleting assigned courses to hrm_id
  2812. if (api_is_multiple_url_enabled()) {
  2813. $sql = "SELECT s.c_id FROM $tbl_course_rel_user s
  2814. INNER JOIN $tbl_course_rel_access_url a
  2815. ON (a.c_id = s.c_id)
  2816. WHERE
  2817. user_id = $hr_manager_id AND
  2818. relation_type = ".COURSE_RELATION_TYPE_RRHH." AND
  2819. access_url_id = ".api_get_current_access_url_id();
  2820. } else {
  2821. $sql = "SELECT c_id FROM $tbl_course_rel_user
  2822. WHERE user_id = $hr_manager_id AND relation_type = ".COURSE_RELATION_TYPE_RRHH;
  2823. }
  2824. $result = Database::query($sql);
  2825. if (Database::num_rows($result) > 0) {
  2826. while ($row = Database::fetch_array($result)) {
  2827. $sql = "DELETE FROM $tbl_course_rel_user
  2828. WHERE
  2829. c_id = {$row['c_id']} AND
  2830. user_id = $hr_manager_id AND
  2831. relation_type = ".COURSE_RELATION_TYPE_RRHH;
  2832. Database::query($sql);
  2833. }
  2834. }
  2835. // inserting new courses list
  2836. if (is_array($courses_list)) {
  2837. foreach ($courses_list as $course_code) {
  2838. $courseInfo = api_get_course_info($course_code);
  2839. $courseId = $courseInfo['real_id'];
  2840. $sql = "INSERT IGNORE INTO $tbl_course_rel_user(c_id, user_id, status, relation_type)
  2841. VALUES($courseId, $hr_manager_id, ".DRH.", ".COURSE_RELATION_TYPE_RRHH.")";
  2842. $result = Database::query($sql);
  2843. if (Database::affected_rows($result)) {
  2844. $affected_rows++;
  2845. }
  2846. }
  2847. }
  2848. return $affected_rows;
  2849. }
  2850. /**
  2851. * get courses followed by human resources manager.
  2852. *
  2853. * @param int $user_id
  2854. * @param int $status
  2855. * @param int $from
  2856. * @param int $limit
  2857. * @param string $column
  2858. * @param string $direction
  2859. * @param bool $getCount
  2860. *
  2861. * @return array courses
  2862. */
  2863. public static function get_courses_followed_by_drh(
  2864. $user_id,
  2865. $status = DRH,
  2866. $from = null,
  2867. $limit = null,
  2868. $column = null,
  2869. $direction = null,
  2870. $getCount = false
  2871. ) {
  2872. return self::getCoursesFollowedByUser(
  2873. $user_id,
  2874. $status,
  2875. $from,
  2876. $limit,
  2877. $column,
  2878. $direction,
  2879. $getCount
  2880. );
  2881. }
  2882. /**
  2883. * get courses followed by user.
  2884. *
  2885. * @param int $user_id
  2886. * @param int $status
  2887. * @param int $from
  2888. * @param int $limit
  2889. * @param string $column
  2890. * @param string $direction
  2891. * @param bool $getCount
  2892. * @param string $keyword
  2893. * @param int $sessionId
  2894. * @param bool $showAllAssignedCourses
  2895. *
  2896. * @return array courses
  2897. */
  2898. public static function getCoursesFollowedByUser(
  2899. $user_id,
  2900. $status = null,
  2901. $from = null,
  2902. $limit = null,
  2903. $column = null,
  2904. $direction = null,
  2905. $getCount = false,
  2906. $keyword = null,
  2907. $sessionId = 0,
  2908. $showAllAssignedCourses = false
  2909. ) {
  2910. // Database Table Definitions
  2911. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  2912. $tbl_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2913. $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  2914. $sessionId = (int) $sessionId;
  2915. $user_id = (int) $user_id;
  2916. $select = "SELECT DISTINCT c.*, c.id as real_id ";
  2917. if ($getCount) {
  2918. $select = "SELECT COUNT(DISTINCT c.id) as count";
  2919. }
  2920. $whereConditions = '';
  2921. switch ($status) {
  2922. case COURSEMANAGER:
  2923. $whereConditions .= " AND cru.user_id = $user_id";
  2924. if (!$showAllAssignedCourses) {
  2925. $whereConditions .= " AND cru.status = ".COURSEMANAGER;
  2926. } else {
  2927. $whereConditions .= " AND relation_type = ".COURSE_RELATION_TYPE_COURSE_MANAGER;
  2928. }
  2929. break;
  2930. case DRH:
  2931. $whereConditions .= " AND
  2932. cru.user_id = $user_id AND
  2933. cru.status = ".DRH." AND
  2934. relation_type = '".COURSE_RELATION_TYPE_RRHH."'
  2935. ";
  2936. break;
  2937. }
  2938. $keywordCondition = null;
  2939. if (!empty($keyword)) {
  2940. $keyword = Database::escape_string($keyword);
  2941. $keywordCondition = " AND (c.code LIKE '%$keyword%' OR c.title LIKE '%$keyword%' ) ";
  2942. }
  2943. $orderBy = null;
  2944. $extraInnerJoin = null;
  2945. if (!empty($sessionId)) {
  2946. if ($status == COURSEMANAGER) {
  2947. // Teacher of course or teacher inside session
  2948. $whereConditions = " AND (cru.status = ".COURSEMANAGER." OR srcru.status = 2) ";
  2949. }
  2950. $courseList = SessionManager::get_course_list_by_session_id($sessionId);
  2951. if (!empty($courseList)) {
  2952. $courseListToString = implode("','", array_keys($courseList));
  2953. $whereConditions .= " AND c.id IN ('".$courseListToString."')";
  2954. }
  2955. $tableSessionRelCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  2956. $tableSessionRelCourseRelUser = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2957. $orderBy = ' ORDER BY position';
  2958. $extraInnerJoin = " INNER JOIN $tableSessionRelCourse src
  2959. ON (c.id = src.c_id AND src.session_id = $sessionId)
  2960. INNER JOIN $tableSessionRelCourseRelUser srcru
  2961. ON (src.session_id = srcru.session_id AND srcru.c_id = src.c_id)
  2962. ";
  2963. }
  2964. $whereConditions .= $keywordCondition;
  2965. $sql = "$select
  2966. FROM $tbl_course c
  2967. INNER JOIN $tbl_course_rel_user cru
  2968. ON (cru.c_id = c.id)
  2969. INNER JOIN $tbl_course_rel_access_url a
  2970. ON (a.c_id = c.id)
  2971. $extraInnerJoin
  2972. WHERE
  2973. access_url_id = ".api_get_current_access_url_id()."
  2974. $whereConditions
  2975. $orderBy
  2976. ";
  2977. if (isset($from) && isset($limit)) {
  2978. $from = intval($from);
  2979. $limit = intval($limit);
  2980. $sql .= " LIMIT $from, $limit";
  2981. }
  2982. $result = Database::query($sql);
  2983. if ($getCount) {
  2984. $row = Database::fetch_array($result);
  2985. return $row['count'];
  2986. }
  2987. $courses = [];
  2988. if (Database::num_rows($result) > 0) {
  2989. while ($row = Database::fetch_array($result)) {
  2990. $courses[$row['code']] = $row;
  2991. }
  2992. }
  2993. return $courses;
  2994. }
  2995. /**
  2996. * check if a course is special (autoregister).
  2997. *
  2998. * @param int $courseId
  2999. *
  3000. * @return bool
  3001. */
  3002. public static function isSpecialCourse($courseId)
  3003. {
  3004. $extraFieldValue = new ExtraFieldValue('course');
  3005. $result = $extraFieldValue->get_values_by_handler_and_field_variable(
  3006. $courseId,
  3007. 'special_course'
  3008. );
  3009. if (!empty($result)) {
  3010. if ($result['value'] == 1) {
  3011. return true;
  3012. }
  3013. }
  3014. return false;
  3015. }
  3016. /**
  3017. * Update course picture.
  3018. *
  3019. * @param array $courseInfo
  3020. * @param string File name
  3021. * @param string the full system name of the image
  3022. * from which course picture will be created
  3023. * @param string $cropParameters Optional string that contents "x,y,width,height" of a cropped image format
  3024. *
  3025. * @return bool Returns the resulting. In case of internal error or negative validation returns FALSE.
  3026. */
  3027. public static function update_course_picture(
  3028. $courseInfo,
  3029. $filename,
  3030. $source_file = null,
  3031. $cropParameters = null
  3032. ) {
  3033. if (empty($courseInfo)) {
  3034. return false;
  3035. }
  3036. // course path
  3037. $store_path = api_get_path(SYS_COURSE_PATH).$courseInfo['path'];
  3038. // image name for courses
  3039. $course_image = $store_path.'/course-pic.png';
  3040. $course_medium_image = $store_path.'/course-pic85x85.png';
  3041. if (file_exists($course_image)) {
  3042. unlink($course_image);
  3043. }
  3044. if (file_exists($course_medium_image)) {
  3045. unlink($course_medium_image);
  3046. }
  3047. //Crop the image to adjust 4:3 ratio
  3048. $image = new Image($source_file);
  3049. $image->crop($cropParameters);
  3050. //Resize the images in two formats
  3051. $medium = new Image($source_file);
  3052. $medium->resize(85);
  3053. $medium->send_image($course_medium_image, -1, 'png');
  3054. $normal = new Image($source_file);
  3055. $normal->resize(400);
  3056. $normal->send_image($course_image, -1, 'png');
  3057. $result = $medium && $normal;
  3058. return $result ? $result : false;
  3059. }
  3060. /**
  3061. * Deletes the course picture.
  3062. *
  3063. * @param string $courseCode
  3064. */
  3065. public static function deleteCoursePicture($courseCode)
  3066. {
  3067. $course_info = api_get_course_info($courseCode);
  3068. // course path
  3069. $storePath = api_get_path(SYS_COURSE_PATH).$course_info['path'];
  3070. // image name for courses
  3071. $courseImage = $storePath.'/course-pic.png';
  3072. $courseMediumImage = $storePath.'/course-pic85x85.png';
  3073. $courseSmallImage = $storePath.'/course-pic32.png';
  3074. if (file_exists($courseImage)) {
  3075. unlink($courseImage);
  3076. }
  3077. if (file_exists($courseMediumImage)) {
  3078. unlink($courseMediumImage);
  3079. }
  3080. if (file_exists($courseSmallImage)) {
  3081. unlink($courseSmallImage);
  3082. }
  3083. }
  3084. /**
  3085. * Display special courses (and only these) as several HTML divs of class userportal-course-item.
  3086. *
  3087. * Special courses are courses that stick on top of the list and are "auto-registerable"
  3088. * in the sense that any user clicking them is registered as a student
  3089. *
  3090. * @param int $user_id User id
  3091. * @param bool $load_dirs Whether to show the document quick-loader or not
  3092. * @param bool $useUserLanguageFilterIfAvailable
  3093. *
  3094. * @return array
  3095. */
  3096. public static function returnSpecialCourses(
  3097. $user_id,
  3098. $load_dirs = false,
  3099. $useUserLanguageFilterIfAvailable = true
  3100. ) {
  3101. $user_id = (int) $user_id;
  3102. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  3103. $specialCourseList = self::get_special_course_list();
  3104. if (empty($specialCourseList)) {
  3105. return [];
  3106. }
  3107. // Filter by language
  3108. $languageCondition = '';
  3109. $onlyInUserLanguage = api_get_configuration_value('my_courses_show_courses_in_user_language_only');
  3110. if ($useUserLanguageFilterIfAvailable && $onlyInUserLanguage) {
  3111. $userInfo = api_get_user_info(api_get_user_id());
  3112. if (!empty($userInfo['language'])) {
  3113. $languageCondition = " AND course_language = '".$userInfo['language']."' ";
  3114. }
  3115. }
  3116. $sql = "SELECT
  3117. id,
  3118. code,
  3119. subscribe subscr,
  3120. unsubscribe unsubscr
  3121. FROM $table
  3122. WHERE
  3123. id IN ('".implode("','", $specialCourseList)."')
  3124. $languageCondition
  3125. GROUP BY code";
  3126. $rs_special_course = Database::query($sql);
  3127. $number_of_courses = Database::num_rows($rs_special_course);
  3128. $showCustomIcon = api_get_setting('course_images_in_courses_list');
  3129. $courseList = [];
  3130. if ($number_of_courses > 0) {
  3131. while ($course = Database::fetch_array($rs_special_course)) {
  3132. $course_info = api_get_course_info($course['code']);
  3133. $courseId = $course_info['real_id'];
  3134. if ($course_info['visibility'] == COURSE_VISIBILITY_HIDDEN) {
  3135. continue;
  3136. }
  3137. $params = [];
  3138. //Param (course_code) needed to get the student info in page "My courses"
  3139. $params['course_code'] = $course['code'];
  3140. $params['code'] = $course['code'];
  3141. // Get notifications.
  3142. $course_info['id_session'] = null;
  3143. $courseUserInfo = self::getUserCourseInfo($user_id, $courseId);
  3144. if (empty($courseUserInfo)) {
  3145. $course_info['status'] = STUDENT;
  3146. } else {
  3147. $course_info['status'] = $courseUserInfo['status'];
  3148. }
  3149. $show_notification = !api_get_configuration_value('hide_course_notification')
  3150. ? Display::show_notification($course_info)
  3151. : '';
  3152. $params['edit_actions'] = '';
  3153. $params['document'] = '';
  3154. if (api_is_platform_admin()) {
  3155. $params['edit_actions'] .= api_get_path(WEB_CODE_PATH).'course_info/infocours.php?cidReq='.$course['code'];
  3156. if ($load_dirs) {
  3157. $params['document'] = '<a id="document_preview_'.$courseId.'_0" class="document_preview btn btn-default btn-sm" href="javascript:void(0);">'
  3158. .Display::returnFontAwesomeIcon('folder-open').'</a>';
  3159. $params['document'] .= Display::div('', ['id' => 'document_result_'.$courseId.'_0', 'class' => 'document_preview_container']);
  3160. }
  3161. } else {
  3162. if ($course_info['visibility'] != COURSE_VISIBILITY_CLOSED && $load_dirs) {
  3163. $params['document'] = '<a id="document_preview_'.$courseId.'_0" class="document_preview btn btn-default btn-sm" href="javascript:void(0);">'
  3164. .Display::returnFontAwesomeIcon('folder-open').'</a>';
  3165. $params['document'] .= Display::div('', ['id' => 'document_result_'.$courseId.'_0', 'class' => 'document_preview_container']);
  3166. }
  3167. }
  3168. $params['visibility'] = $course_info['visibility'];
  3169. $params['status'] = $course_info['status'];
  3170. $params['category'] = $course_info['categoryName'];
  3171. $params['category_code'] = $course_info['categoryCode'];
  3172. $params['icon'] = Display::return_icon(
  3173. 'drawing-pin.png',
  3174. null,
  3175. null,
  3176. ICON_SIZE_LARGE,
  3177. null
  3178. );
  3179. if (api_get_setting('display_coursecode_in_courselist') == 'true') {
  3180. $params['code_course'] = '('.$course_info['visual_code'].')';
  3181. }
  3182. $params['title'] = $course_info['title'];
  3183. $params['title_cut'] = $course_info['title'];
  3184. $params['link'] = $course_info['course_public_url'].'?id_session=0&autoreg=1';
  3185. if (api_get_setting('display_teacher_in_courselist') === 'true') {
  3186. $params['teachers'] = self::getTeachersFromCourse(
  3187. $courseId,
  3188. true
  3189. );
  3190. }
  3191. if ($showCustomIcon === 'true') {
  3192. $params['thumbnails'] = $course_info['course_image'];
  3193. $params['image'] = $course_info['course_image_large'];
  3194. }
  3195. if ($course_info['visibility'] != COURSE_VISIBILITY_CLOSED) {
  3196. $params['notifications'] = $show_notification;
  3197. }
  3198. $params['is_special_course'] = true;
  3199. $courseList[] = $params;
  3200. }
  3201. }
  3202. return $courseList;
  3203. }
  3204. /**
  3205. * Display courses (without special courses) as several HTML divs
  3206. * of course categories, as class userportal-catalog-item.
  3207. *
  3208. * @uses \displayCoursesInCategory() to display the courses themselves
  3209. *
  3210. * @param int $user_id
  3211. * @param bool $load_dirs Whether to show the document quick-loader or not
  3212. * @param bool $useUserLanguageFilterIfAvailable
  3213. *
  3214. * @return array
  3215. */
  3216. public static function returnCourses(
  3217. $user_id,
  3218. $load_dirs = false,
  3219. $useUserLanguageFilterIfAvailable = true
  3220. ) {
  3221. $user_id = (int) $user_id;
  3222. if (empty($user_id)) {
  3223. $user_id = api_get_user_id();
  3224. }
  3225. // Step 1: We get all the categories of the user
  3226. $table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  3227. $sql = "SELECT * FROM $table
  3228. WHERE user_id = $user_id
  3229. ORDER BY sort ASC";
  3230. $result = Database::query($sql);
  3231. $listItems = [
  3232. 'in_category' => [],
  3233. 'not_category' => [],
  3234. ];
  3235. $collapsable = api_get_configuration_value('allow_user_course_category_collapsable');
  3236. $stok = Security::get_token();
  3237. while ($row = Database::fetch_array($result)) {
  3238. // We simply display the title of the category.
  3239. $courseInCategory = self::returnCoursesCategories(
  3240. $row['id'],
  3241. $load_dirs,
  3242. $user_id,
  3243. $useUserLanguageFilterIfAvailable
  3244. );
  3245. $collapsed = 0;
  3246. $collapsableLink = '';
  3247. if ($collapsable) {
  3248. $url = api_get_path(WEB_CODE_PATH).
  3249. 'auth/courses.php?categoryid='.$row['id'].'&sec_token='.$stok.'&redirect=home';
  3250. $collapsed = isset($row['collapsed']) && $row['collapsed'] ? 1 : 0;
  3251. if ($collapsed === 0) {
  3252. $collapsableLink = Display::url(
  3253. '<i class="fa fa-folder-open"></i>',
  3254. $url.'&action=set_collapsable&option=1'
  3255. );
  3256. } else {
  3257. $collapsableLink = Display::url(
  3258. '<i class="fa fa-folder"></i>',
  3259. $url.'&action=set_collapsable&option=0'
  3260. );
  3261. }
  3262. }
  3263. $params = [
  3264. 'id_category' => $row['id'],
  3265. 'title_category' => $row['title'],
  3266. 'collapsed' => $collapsed,
  3267. 'collapsable_link' => $collapsableLink,
  3268. 'courses' => $courseInCategory,
  3269. ];
  3270. $listItems['in_category'][] = $params;
  3271. }
  3272. // Step 2: We display the course without a user category.
  3273. $coursesNotCategory = self::returnCoursesCategories(
  3274. 0,
  3275. $load_dirs,
  3276. $user_id,
  3277. $useUserLanguageFilterIfAvailable
  3278. );
  3279. if ($coursesNotCategory) {
  3280. $listItems['not_category'] = $coursesNotCategory;
  3281. }
  3282. return $listItems;
  3283. }
  3284. /**
  3285. * Display courses inside a category (without special courses) as HTML dics of
  3286. * class userportal-course-item.
  3287. *
  3288. * @param int $user_category_id User category id
  3289. * @param bool $load_dirs Whether to show the document quick-loader or not
  3290. * @param int $user_id
  3291. * @param bool $useUserLanguageFilterIfAvailable
  3292. *
  3293. * @return array
  3294. */
  3295. public static function returnCoursesCategories(
  3296. $user_category_id,
  3297. $load_dirs = false,
  3298. $user_id = 0,
  3299. $useUserLanguageFilterIfAvailable = true
  3300. ) {
  3301. $user_id = $user_id ? (int) $user_id : api_get_user_id();
  3302. $user_category_id = (int) $user_category_id;
  3303. // Table definitions
  3304. $TABLECOURS = Database::get_main_table(TABLE_MAIN_COURSE);
  3305. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  3306. $TABLE_ACCESS_URL_REL_COURSE = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  3307. $current_url_id = api_get_current_access_url_id();
  3308. // Get course list auto-register
  3309. $special_course_list = self::get_special_course_list();
  3310. $without_special_courses = '';
  3311. if (!empty($special_course_list)) {
  3312. $without_special_courses = ' AND course.id NOT IN ("'.implode('","', $special_course_list).'")';
  3313. }
  3314. $userCategoryCondition = " (course_rel_user.user_course_cat = $user_category_id) ";
  3315. if (empty($user_category_id)) {
  3316. $userCategoryCondition = ' (course_rel_user.user_course_cat = 0 OR course_rel_user.user_course_cat IS NULL) ';
  3317. }
  3318. $languageCondition = '';
  3319. $onlyInUserLanguage = api_get_configuration_value('my_courses_show_courses_in_user_language_only');
  3320. if ($useUserLanguageFilterIfAvailable && $onlyInUserLanguage) {
  3321. $userInfo = api_get_user_info(api_get_user_id());
  3322. if (!empty($userInfo['language'])) {
  3323. $languageCondition = " AND course.course_language = '".$userInfo['language']."' ";
  3324. }
  3325. }
  3326. $sql = "SELECT DISTINCT
  3327. course.id,
  3328. course_rel_user.status status,
  3329. course.code as course_code,
  3330. user_course_cat,
  3331. course_rel_user.sort
  3332. FROM $TABLECOURS course
  3333. INNER JOIN $TABLECOURSUSER course_rel_user
  3334. ON (course.id = course_rel_user.c_id)
  3335. INNER JOIN $TABLE_ACCESS_URL_REL_COURSE url
  3336. ON (url.c_id = course.id)
  3337. WHERE
  3338. course_rel_user.user_id = $user_id AND
  3339. $userCategoryCondition
  3340. $without_special_courses
  3341. $languageCondition
  3342. ";
  3343. // If multiple URL access mode is enabled, only fetch courses
  3344. // corresponding to the current URL.
  3345. if (api_get_multiple_access_url() && $current_url_id != -1) {
  3346. $sql .= " AND access_url_id = $current_url_id";
  3347. }
  3348. // Use user's classification for courses (if any).
  3349. $sql .= " ORDER BY course_rel_user.user_course_cat, course_rel_user.sort ASC";
  3350. $result = Database::query($sql);
  3351. $showCustomIcon = api_get_setting('course_images_in_courses_list');
  3352. // Browse through all courses.
  3353. $courseAdded = [];
  3354. $courseList = [];
  3355. while ($row = Database::fetch_array($result)) {
  3356. $course_info = api_get_course_info_by_id($row['id']);
  3357. if (isset($course_info['visibility']) &&
  3358. $course_info['visibility'] == COURSE_VISIBILITY_HIDDEN
  3359. ) {
  3360. continue;
  3361. }
  3362. // Skip if already in list
  3363. if (in_array($course_info['real_id'], $courseAdded)) {
  3364. continue;
  3365. }
  3366. $course_info['id_session'] = null;
  3367. $course_info['status'] = $row['status'];
  3368. // For each course, get if there is any notification icon to show
  3369. // (something that would have changed since the user's last visit).
  3370. $showNotification = !api_get_configuration_value('hide_course_notification')
  3371. ? Display::show_notification($course_info)
  3372. : '';
  3373. $iconName = basename($course_info['course_image']);
  3374. $params = [];
  3375. //Param (course_code) needed to get the student process
  3376. $params['course_code'] = $row['course_code'];
  3377. $params['code'] = $row['course_code'];
  3378. if ($showCustomIcon === 'true' && $iconName != 'course.png') {
  3379. $params['thumbnails'] = $course_info['course_image'];
  3380. $params['image'] = $course_info['course_image_large'];
  3381. }
  3382. $thumbnails = null;
  3383. $image = null;
  3384. if ($showCustomIcon === 'true' && $iconName != 'course.png') {
  3385. $thumbnails = $course_info['course_image'];
  3386. $image = $course_info['course_image_large'];
  3387. } else {
  3388. $image = Display::return_icon(
  3389. 'session_default.png',
  3390. null,
  3391. null,
  3392. null,
  3393. null,
  3394. true
  3395. );
  3396. }
  3397. $params['course_id'] = $course_info['real_id'];
  3398. $params['edit_actions'] = '';
  3399. $params['document'] = '';
  3400. if (api_is_platform_admin()) {
  3401. $params['edit_actions'] .= api_get_path(WEB_CODE_PATH).'course_info/infocours.php?cidReq='.$course_info['code'];
  3402. if ($load_dirs) {
  3403. $params['document'] = '<a id="document_preview_'.$course_info['real_id'].'_0" class="document_preview btn btn-default btn-sm" href="javascript:void(0);">'
  3404. .Display::returnFontAwesomeIcon('folder-open').'</a>';
  3405. $params['document'] .= Display::div(
  3406. '',
  3407. [
  3408. 'id' => 'document_result_'.$course_info['real_id'].'_0',
  3409. 'class' => 'document_preview_container',
  3410. ]
  3411. );
  3412. }
  3413. }
  3414. if ($load_dirs) {
  3415. $params['document'] = '<a id="document_preview_'.$course_info['real_id'].'_0" class="document_preview btn btn-default btn-sm" href="javascript:void(0);">'
  3416. .Display::returnFontAwesomeIcon('folder-open').'</a>';
  3417. $params['document'] .= Display::div(
  3418. '',
  3419. [
  3420. 'id' => 'document_result_'.$course_info['real_id'].'_0',
  3421. 'class' => 'document_preview_container',
  3422. ]
  3423. );
  3424. }
  3425. $courseUrl = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/index.php?id_session=0';
  3426. $teachers = [];
  3427. if (api_get_setting('display_teacher_in_courselist') === 'true') {
  3428. $teachers = self::getTeachersFromCourse(
  3429. $course_info['real_id'],
  3430. true
  3431. );
  3432. }
  3433. $params['status'] = $row['status'];
  3434. if (api_get_setting('display_coursecode_in_courselist') == 'true') {
  3435. $params['code_course'] = '('.$course_info['visual_code'].') ';
  3436. }
  3437. $params['current_user_is_teacher'] = false;
  3438. /** @var array $teacher */
  3439. foreach ($teachers as $teacher) {
  3440. if ($teacher['id'] != $user_id) {
  3441. continue;
  3442. }
  3443. $params['current_user_is_teacher'] = true;
  3444. }
  3445. $params['visibility'] = $course_info['visibility'];
  3446. $params['link'] = $courseUrl;
  3447. $params['thumbnails'] = $thumbnails;
  3448. $params['image'] = $image;
  3449. $params['title'] = $course_info['title'];
  3450. $params['title_cut'] = $params['title'];
  3451. $params['category'] = $course_info['categoryName'];
  3452. $params['category_code'] = $course_info['categoryCode'];
  3453. $params['teachers'] = $teachers;
  3454. if ($course_info['visibility'] != COURSE_VISIBILITY_CLOSED) {
  3455. $params['notifications'] = $showNotification;
  3456. }
  3457. $courseAdded[] = $course_info['real_id'];
  3458. $courseList[] = $params;
  3459. }
  3460. return $courseList;
  3461. }
  3462. /**
  3463. * Retrieves the user defined course categories.
  3464. *
  3465. * @param int $userId
  3466. *
  3467. * @return array
  3468. */
  3469. public static function get_user_course_categories($userId = 0)
  3470. {
  3471. $userId = empty($userId) ? api_get_user_id() : (int) $userId;
  3472. $table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  3473. $sql = "SELECT * FROM $table
  3474. WHERE user_id = $userId
  3475. ORDER BY sort ASC
  3476. ";
  3477. $result = Database::query($sql);
  3478. $output = [];
  3479. while ($row = Database::fetch_array($result, 'ASSOC')) {
  3480. $output[$row['id']] = $row;
  3481. }
  3482. return $output;
  3483. }
  3484. /**
  3485. * Return an array the user_category id and title for the course $courseId for user $userId.
  3486. *
  3487. * @param $userId
  3488. * @param $courseId
  3489. *
  3490. * @return array
  3491. */
  3492. public static function getUserCourseCategoryForCourse($userId, $courseId)
  3493. {
  3494. $tblCourseRelUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  3495. $tblUserCategory = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  3496. $courseId = intval($courseId);
  3497. $userId = intval($userId);
  3498. $sql = "SELECT user_course_cat, title
  3499. FROM $tblCourseRelUser cru
  3500. LEFT JOIN $tblUserCategory ucc
  3501. ON cru.user_course_cat = ucc.id
  3502. WHERE
  3503. cru.user_id = $userId AND c_id = $courseId ";
  3504. $res = Database::query($sql);
  3505. $data = [];
  3506. if (Database::num_rows($res) > 0) {
  3507. $data = Database::fetch_assoc($res);
  3508. }
  3509. return $data;
  3510. }
  3511. /**
  3512. * Get the course id based on the original id and field name in the extra fields.
  3513. * Returns 0 if course was not found.
  3514. *
  3515. * @param string $value Original course code
  3516. * @param string $variable Original field name
  3517. *
  3518. * @return array
  3519. */
  3520. public static function getCourseInfoFromOriginalId($value, $variable)
  3521. {
  3522. $extraFieldValue = new ExtraFieldValue('course');
  3523. $result = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
  3524. $variable,
  3525. $value
  3526. );
  3527. if (!empty($result)) {
  3528. $courseInfo = api_get_course_info_by_id($result['item_id']);
  3529. return $courseInfo;
  3530. }
  3531. return [];
  3532. }
  3533. /**
  3534. * Display code for one specific course a logged in user is subscribed to.
  3535. * Shows a link to the course, what's new icons...
  3536. *
  3537. * $my_course['d'] - course directory
  3538. * $my_course['i'] - course title
  3539. * $my_course['c'] - visual course code
  3540. * $my_course['k'] - system course code
  3541. *
  3542. * @param array Course details
  3543. * @param int Session ID
  3544. * @param string CSS class to apply to course entry
  3545. * @param bool Whether the session is supposedly accessible now
  3546. * (not in the case it has passed and is in invisible/unaccessible mode)
  3547. * @param bool Whether to show the document quick-loader or not
  3548. *
  3549. * @return string The HTML to be printed for the course entry
  3550. *
  3551. * @version 1.0.3
  3552. *
  3553. * @todo refactor into different functions for database calls | logic | display
  3554. * @todo replace single-character $my_course['d'] indices
  3555. * @todo move code for what's new icons to a separate function to clear things up
  3556. * @todo add a parameter user_id so that it is possible to show the
  3557. * courselist of other users (=generalisation).
  3558. * This will prevent having to write a new function for this.
  3559. */
  3560. public static function get_logged_user_course_html(
  3561. $course,
  3562. $session_id = 0,
  3563. $class = 'courses',
  3564. $session_accessible = true,
  3565. $load_dirs = false
  3566. ) {
  3567. $now = date('Y-m-d h:i:s');
  3568. $user_id = api_get_user_id();
  3569. $course_info = api_get_course_info_by_id($course['real_id']);
  3570. $course_visibility = (int) $course_info['visibility'];
  3571. if ($course_visibility === COURSE_VISIBILITY_HIDDEN) {
  3572. return '';
  3573. }
  3574. $userInCourseStatus = self::getUserInCourseStatus(
  3575. $user_id,
  3576. $course_info['real_id']
  3577. );
  3578. $course_info['status'] = empty($session_id) ? $userInCourseStatus : STUDENT;
  3579. $course_info['id_session'] = $session_id;
  3580. $is_coach = api_is_coach($session_id, $course_info['real_id']);
  3581. // Display course entry.
  3582. // Show a hyperlink to the course, unless the course is closed and user is not course admin.
  3583. $session_url = '';
  3584. $params = [];
  3585. $params['icon'] = Display::return_icon(
  3586. 'blackboard_blue.png',
  3587. null,
  3588. [],
  3589. ICON_SIZE_LARGE,
  3590. null,
  3591. true
  3592. );
  3593. $params['real_id'] = $course_info['real_id'];
  3594. // Display the "what's new" icons
  3595. $notifications = '';
  3596. if (
  3597. ($course_visibility != COURSE_VISIBILITY_CLOSED && $course_visibility != COURSE_VISIBILITY_HIDDEN) ||
  3598. !api_get_configuration_value('hide_course_notification')
  3599. ) {
  3600. $notifications .= Display::show_notification($course_info);
  3601. }
  3602. if ($session_accessible) {
  3603. if ($course_visibility != COURSE_VISIBILITY_CLOSED ||
  3604. $userInCourseStatus == COURSEMANAGER
  3605. ) {
  3606. if (empty($course_info['id_session'])) {
  3607. $course_info['id_session'] = 0;
  3608. }
  3609. $sessionCourseAvailable = false;
  3610. $sessionCourseStatus = api_get_session_visibility($session_id, $course_info['real_id']);
  3611. if (in_array(
  3612. $sessionCourseStatus,
  3613. [SESSION_VISIBLE_READ_ONLY, SESSION_VISIBLE, SESSION_AVAILABLE]
  3614. )) {
  3615. $sessionCourseAvailable = true;
  3616. }
  3617. if ($userInCourseStatus === COURSEMANAGER || $sessionCourseAvailable) {
  3618. $session_url = $course_info['course_public_url'].'?id_session='.$course_info['id_session'];
  3619. $session_title = '<a title="'.$course_info['name'].'" href="'.$session_url.'">'.
  3620. $course_info['name'].'</a>'.$notifications;
  3621. } else {
  3622. $session_title = $course_info['name'];
  3623. }
  3624. } else {
  3625. $session_title =
  3626. $course_info['name'].' '.
  3627. Display::tag('span', get_lang('CourseClosed'), ['class' => 'item_closed']);
  3628. }
  3629. } else {
  3630. $session_title = $course_info['name'];
  3631. }
  3632. $thumbnails = null;
  3633. $image = null;
  3634. $showCustomIcon = api_get_setting('course_images_in_courses_list');
  3635. $iconName = basename($course_info['course_image']);
  3636. if ($showCustomIcon === 'true' && $iconName != 'course.png') {
  3637. $thumbnails = $course_info['course_image'];
  3638. $image = $course_info['course_image_large'];
  3639. } else {
  3640. $image = Display::return_icon(
  3641. 'session_default.png',
  3642. null,
  3643. null,
  3644. null,
  3645. null,
  3646. true
  3647. );
  3648. }
  3649. $params['thumbnails'] = $thumbnails;
  3650. $params['image'] = $image;
  3651. $params['link'] = $session_url;
  3652. $params['title'] = $session_title;
  3653. $params['name'] = $course_info['name'];
  3654. $params['edit_actions'] = '';
  3655. $params['document'] = '';
  3656. $params['category'] = $course_info['categoryName'];
  3657. if ($course_visibility != COURSE_VISIBILITY_CLOSED &&
  3658. $course_visibility != COURSE_VISIBILITY_HIDDEN
  3659. ) {
  3660. if (api_is_platform_admin()) {
  3661. $params['edit_actions'] .= api_get_path(WEB_CODE_PATH).'course_info/infocours.php?cidReq='.$course_info['code'];
  3662. if ($load_dirs) {
  3663. $params['document'] .= '<a
  3664. id="document_preview_'.$course_info['real_id'].'_'.$course_info['id_session'].'"
  3665. class="document_preview btn btn-default btn-sm"
  3666. href="javascript:void(0);">'.
  3667. Display::returnFontAwesomeIcon('folder-open').'</a>';
  3668. $params['document'] .= Display::div('', [
  3669. 'id' => 'document_result_'.$course_info['real_id'].'_'.$course_info['id_session'],
  3670. 'class' => 'document_preview_container',
  3671. ]);
  3672. }
  3673. }
  3674. }
  3675. if (api_get_setting('display_teacher_in_courselist') === 'true') {
  3676. $teacher_list = self::getTeachersFromCourse(
  3677. $course_info['real_id'],
  3678. true
  3679. );
  3680. $course_coachs = self::get_coachs_from_course(
  3681. $course_info['id_session'],
  3682. $course_info['real_id']
  3683. );
  3684. $params['teachers'] = $teacher_list;
  3685. if (($course_info['status'] == STUDENT && !empty($course_info['id_session'])) ||
  3686. ($is_coach && $course_info['status'] != COURSEMANAGER)
  3687. ) {
  3688. $params['coaches'] = $course_coachs;
  3689. }
  3690. }
  3691. $special = isset($course['special_course']) ? true : false;
  3692. $params['title'] = $session_title;
  3693. $params['special'] = $special;
  3694. if (api_get_setting('display_coursecode_in_courselist') === 'true') {
  3695. $params['visual_code'] = '('.$course_info['visual_code'].')';
  3696. }
  3697. $params['extra'] = '';
  3698. $html = $params;
  3699. $session_category_id = null;
  3700. if (1) {
  3701. $session = '';
  3702. $active = false;
  3703. if (!empty($course_info['id_session'])) {
  3704. $session = api_get_session_info($course_info['id_session']);
  3705. $sessionCoachName = '';
  3706. if (!empty($session['id_coach'])) {
  3707. $coachInfo = api_get_user_info($session['id_coach']);
  3708. $sessionCoachName = $coachInfo['complete_name'];
  3709. }
  3710. $session_category_id = self::get_session_category_id_by_session_id($course_info['id_session']);
  3711. if (
  3712. $session['access_start_date'] === '0000-00-00 00:00:00' || empty($session['access_start_date']) ||
  3713. $session['access_start_date'] === '0000-00-00'
  3714. ) {
  3715. $session['dates'] = '';
  3716. if (api_get_setting('show_session_coach') === 'true') {
  3717. $session['coach'] = get_lang('GeneralCoach').': '.$sessionCoachName;
  3718. }
  3719. $active = true;
  3720. } else {
  3721. $session['dates'] = ' - '.
  3722. get_lang('From').' '.$session['access_start_date'].' '.
  3723. get_lang('To').' '.$session['access_end_date'];
  3724. if (api_get_setting('show_session_coach') === 'true') {
  3725. $session['coach'] = get_lang('GeneralCoach').': '.$sessionCoachName;
  3726. }
  3727. $date_start = $session['access_start_date'];
  3728. $date_end = $session['access_end_date'];
  3729. $active = !$date_end ? ($date_start <= $now) : ($date_start <= $now && $date_end >= $now);
  3730. }
  3731. }
  3732. $user_course_category = '';
  3733. if (isset($course_info['user_course_cat'])) {
  3734. $user_course_category = $course_info['user_course_cat'];
  3735. }
  3736. $output = [
  3737. $user_course_category,
  3738. $html,
  3739. $course_info['id_session'],
  3740. $session,
  3741. 'active' => $active,
  3742. 'session_category_id' => $session_category_id,
  3743. ];
  3744. if (Skill::isAllowed($user_id, false)) {
  3745. $em = Database::getManager();
  3746. $objUser = api_get_user_entity($user_id);
  3747. /** @var Course $objCourse */
  3748. $objCourse = $em->find('ChamiloCoreBundle:Course', $course['real_id']);
  3749. $objSession = $em->find('ChamiloCoreBundle:Session', $session_id);
  3750. $skill = $em->getRepository('ChamiloCoreBundle:Skill')->getLastByUser($objUser, $objCourse, $objSession);
  3751. $output['skill'] = null;
  3752. if ($skill) {
  3753. $output['skill']['name'] = $skill->getName();
  3754. $output['skill']['icon'] = $skill->getIcon();
  3755. }
  3756. }
  3757. } else {
  3758. $output = [$course_info['user_course_cat'], $html];
  3759. }
  3760. return $output;
  3761. }
  3762. /**
  3763. * @param string $source_course_code
  3764. * @param int $source_session_id
  3765. * @param string $destination_course_code
  3766. * @param int $destination_session_id
  3767. * @param array $params
  3768. *
  3769. * @return bool
  3770. */
  3771. public static function copy_course(
  3772. $source_course_code,
  3773. $source_session_id,
  3774. $destination_course_code,
  3775. $destination_session_id,
  3776. $params = []
  3777. ) {
  3778. $course_info = api_get_course_info($source_course_code);
  3779. if (!empty($course_info)) {
  3780. $cb = new CourseBuilder('', $course_info);
  3781. $course = $cb->build($source_session_id, $source_course_code, true);
  3782. $course_restorer = new CourseRestorer($course);
  3783. $course_restorer->skip_content = $params;
  3784. $course_restorer->restore(
  3785. $destination_course_code,
  3786. $destination_session_id,
  3787. true,
  3788. true
  3789. );
  3790. return true;
  3791. }
  3792. return false;
  3793. }
  3794. /**
  3795. * A simpler version of the copy_course, the function creates an empty course with an autogenerated course code.
  3796. *
  3797. * @param string $new_title new course title
  3798. * @param string source course code
  3799. * @param int source session id
  3800. * @param int destination session id
  3801. * @param array $params
  3802. *
  3803. * @return array
  3804. */
  3805. public static function copy_course_simple(
  3806. $new_title,
  3807. $source_course_code,
  3808. $source_session_id = 0,
  3809. $destination_session_id = 0,
  3810. $params = []
  3811. ) {
  3812. $source_course_info = api_get_course_info($source_course_code);
  3813. if (!empty($source_course_info)) {
  3814. $new_course_code = self::generate_nice_next_course_code($source_course_code);
  3815. if ($new_course_code) {
  3816. $new_course_info = self::create_course(
  3817. $new_title,
  3818. $new_course_code,
  3819. false
  3820. );
  3821. if (!empty($new_course_info['code'])) {
  3822. $result = self::copy_course(
  3823. $source_course_code,
  3824. $source_session_id,
  3825. $new_course_info['code'],
  3826. $destination_session_id,
  3827. $params
  3828. );
  3829. if ($result) {
  3830. return $new_course_info;
  3831. }
  3832. }
  3833. }
  3834. }
  3835. return false;
  3836. }
  3837. /**
  3838. * Creates a new course code based in a given code.
  3839. *
  3840. * @param string wanted code
  3841. * <code> $wanted_code = 'curse' if there are in the DB codes like curse1 curse2 the function will return: course3</code>
  3842. * if the course code doest not exist in the DB the same course code will be returned
  3843. *
  3844. * @return string wanted unused code
  3845. */
  3846. public static function generate_nice_next_course_code($wanted_code)
  3847. {
  3848. $course_code_ok = !self::course_code_exists($wanted_code);
  3849. if (!$course_code_ok) {
  3850. $wanted_code = self::generate_course_code($wanted_code);
  3851. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  3852. $wanted_code = Database::escape_string($wanted_code);
  3853. $sql = "SELECT count(id) as count
  3854. FROM $table
  3855. WHERE code LIKE '$wanted_code%'";
  3856. $result = Database::query($sql);
  3857. if (Database::num_rows($result) > 0) {
  3858. $row = Database::fetch_array($result);
  3859. $count = $row['count'] + 1;
  3860. $wanted_code = $wanted_code.'_'.$count;
  3861. $result = api_get_course_info($wanted_code);
  3862. if (empty($result)) {
  3863. return $wanted_code;
  3864. }
  3865. }
  3866. return false;
  3867. }
  3868. return $wanted_code;
  3869. }
  3870. /**
  3871. * Gets the status of the users agreement in a course course-session.
  3872. *
  3873. * @param int $user_id
  3874. * @param string $course_code
  3875. * @param int $session_id
  3876. *
  3877. * @return bool
  3878. */
  3879. public static function is_user_accepted_legal($user_id, $course_code, $session_id = 0)
  3880. {
  3881. $user_id = intval($user_id);
  3882. $course_code = Database::escape_string($course_code);
  3883. $session_id = intval($session_id);
  3884. $courseInfo = api_get_course_info($course_code);
  3885. $courseId = $courseInfo['real_id'];
  3886. // Course legal
  3887. $enabled = api_get_plugin_setting('courselegal', 'tool_enable');
  3888. if ($enabled == 'true') {
  3889. require_once api_get_path(SYS_PLUGIN_PATH).'courselegal/config.php';
  3890. $plugin = CourseLegalPlugin::create();
  3891. return $plugin->isUserAcceptedLegal($user_id, $course_code, $session_id);
  3892. }
  3893. if (empty($session_id)) {
  3894. $table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  3895. $sql = "SELECT legal_agreement FROM $table
  3896. WHERE user_id = $user_id AND c_id = $courseId ";
  3897. $result = Database::query($sql);
  3898. if (Database::num_rows($result) > 0) {
  3899. $result = Database::fetch_array($result);
  3900. if ($result['legal_agreement'] == 1) {
  3901. return true;
  3902. }
  3903. }
  3904. return false;
  3905. } else {
  3906. $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  3907. $sql = "SELECT legal_agreement FROM $table
  3908. WHERE user_id = $user_id AND c_id = $courseId AND session_id = $session_id";
  3909. $result = Database::query($sql);
  3910. if (Database::num_rows($result) > 0) {
  3911. $result = Database::fetch_array($result);
  3912. if ($result['legal_agreement'] == 1) {
  3913. return true;
  3914. }
  3915. }
  3916. return false;
  3917. }
  3918. }
  3919. /**
  3920. * Saves the user-course legal agreement.
  3921. *
  3922. * @param int user id
  3923. * @param string course code
  3924. * @param int session id
  3925. *
  3926. * @return mixed
  3927. */
  3928. public static function save_user_legal($user_id, $course_code, $session_id = null)
  3929. {
  3930. // Course plugin legal
  3931. $enabled = api_get_plugin_setting('courselegal', 'tool_enable');
  3932. if ($enabled == 'true') {
  3933. require_once api_get_path(SYS_PLUGIN_PATH).'courselegal/config.php';
  3934. $plugin = CourseLegalPlugin::create();
  3935. return $plugin->saveUserLegal($user_id, $course_code, $session_id);
  3936. }
  3937. $user_id = intval($user_id);
  3938. $course_code = Database::escape_string($course_code);
  3939. $session_id = intval($session_id);
  3940. $courseInfo = api_get_course_info($course_code);
  3941. $courseId = $courseInfo['real_id'];
  3942. if (empty($session_id)) {
  3943. $table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  3944. $sql = "UPDATE $table SET legal_agreement = '1'
  3945. WHERE user_id = $user_id AND c_id = $courseId ";
  3946. Database::query($sql);
  3947. } else {
  3948. $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  3949. $sql = "UPDATE $table SET legal_agreement = '1'
  3950. WHERE user_id = $user_id AND c_id = $courseId AND session_id = $session_id";
  3951. Database::query($sql);
  3952. }
  3953. }
  3954. /**
  3955. * @param int $user_id
  3956. * @param int $course_id
  3957. * @param int $session_id
  3958. * @param int $url_id
  3959. *
  3960. * @return bool
  3961. */
  3962. public static function get_user_course_vote($user_id, $course_id, $session_id = 0, $url_id = 0)
  3963. {
  3964. $table_user_course_vote = Database::get_main_table(TABLE_MAIN_USER_REL_COURSE_VOTE);
  3965. $session_id = !isset($session_id) ? api_get_session_id() : intval($session_id);
  3966. $url_id = empty($url_id) ? api_get_current_access_url_id() : intval($url_id);
  3967. $user_id = intval($user_id);
  3968. if (empty($user_id)) {
  3969. return false;
  3970. }
  3971. $params = [
  3972. 'user_id' => $user_id,
  3973. 'c_id' => $course_id,
  3974. 'session_id' => $session_id,
  3975. 'url_id' => $url_id,
  3976. ];
  3977. $result = Database::select(
  3978. 'vote',
  3979. $table_user_course_vote,
  3980. [
  3981. 'where' => [
  3982. 'user_id = ? AND c_id = ? AND session_id = ? AND url_id = ?' => $params,
  3983. ],
  3984. ],
  3985. 'first'
  3986. );
  3987. if (!empty($result)) {
  3988. return $result['vote'];
  3989. }
  3990. return false;
  3991. }
  3992. /**
  3993. * @param int $course_id
  3994. * @param int $session_id
  3995. * @param int $url_id
  3996. *
  3997. * @return array
  3998. */
  3999. public static function get_course_ranking(
  4000. $course_id,
  4001. $session_id = 0,
  4002. $url_id = 0
  4003. ) {
  4004. $table_course_ranking = Database::get_main_table(TABLE_STATISTIC_TRACK_COURSE_RANKING);
  4005. $session_id = empty($session_id) ? api_get_session_id() : intval($session_id);
  4006. $url_id = empty($url_id) ? api_get_current_access_url_id() : intval($url_id);
  4007. $now = api_get_utc_datetime();
  4008. $params = [
  4009. 'c_id' => $course_id,
  4010. 'session_id' => $session_id,
  4011. 'url_id' => $url_id,
  4012. 'creation_date' => $now,
  4013. ];
  4014. $result = Database::select(
  4015. 'c_id, accesses, total_score, users',
  4016. $table_course_ranking,
  4017. ['where' => ['c_id = ? AND session_id = ? AND url_id = ?' => $params]],
  4018. 'first'
  4019. );
  4020. $point_average_in_percentage = 0;
  4021. $point_average_in_star = 0;
  4022. $users_who_voted = 0;
  4023. if (!empty($result['users'])) {
  4024. $users_who_voted = $result['users'];
  4025. $point_average_in_percentage = round($result['total_score'] / $result['users'] * 100 / 5, 2);
  4026. $point_average_in_star = round($result['total_score'] / $result['users'], 1);
  4027. }
  4028. $result['user_vote'] = false;
  4029. if (!api_is_anonymous()) {
  4030. $result['user_vote'] = self::get_user_course_vote(api_get_user_id(), $course_id, $session_id, $url_id);
  4031. }
  4032. $result['point_average'] = $point_average_in_percentage;
  4033. $result['point_average_star'] = $point_average_in_star;
  4034. $result['users_who_voted'] = $users_who_voted;
  4035. return $result;
  4036. }
  4037. /**
  4038. * Updates the course ranking.
  4039. *
  4040. * @param int course id
  4041. * @param int $session_id
  4042. * @param int url id
  4043. * @param $points_to_add
  4044. * @param bool $add_access
  4045. * @param bool $add_user
  4046. *
  4047. * @return array
  4048. */
  4049. public static function update_course_ranking(
  4050. $course_id = 0,
  4051. $session_id = 0,
  4052. $url_id = 0,
  4053. $points_to_add = null,
  4054. $add_access = true,
  4055. $add_user = true
  4056. ) {
  4057. // Course catalog stats modifications see #4191
  4058. $table_course_ranking = Database::get_main_table(TABLE_STATISTIC_TRACK_COURSE_RANKING);
  4059. $now = api_get_utc_datetime();
  4060. $course_id = empty($course_id) ? api_get_course_int_id() : intval($course_id);
  4061. $session_id = empty($session_id) ? api_get_session_id() : intval($session_id);
  4062. $url_id = empty($url_id) ? api_get_current_access_url_id() : intval($url_id);
  4063. $params = [
  4064. 'c_id' => $course_id,
  4065. 'session_id' => $session_id,
  4066. 'url_id' => $url_id,
  4067. 'creation_date' => $now,
  4068. 'total_score' => 0,
  4069. 'users' => 0,
  4070. ];
  4071. $result = Database::select(
  4072. 'id, accesses, total_score, users',
  4073. $table_course_ranking,
  4074. ['where' => ['c_id = ? AND session_id = ? AND url_id = ?' => $params]],
  4075. 'first'
  4076. );
  4077. // Problem here every time we load the courses/XXXX/index.php course home page we update the access
  4078. if (empty($result)) {
  4079. if ($add_access) {
  4080. $params['accesses'] = 1;
  4081. }
  4082. //The votes and users are empty
  4083. if (isset($points_to_add) && !empty($points_to_add)) {
  4084. $params['total_score'] = intval($points_to_add);
  4085. }
  4086. if ($add_user) {
  4087. $params['users'] = 1;
  4088. }
  4089. $result = Database::insert($table_course_ranking, $params);
  4090. } else {
  4091. $my_params = [];
  4092. if ($add_access) {
  4093. $my_params['accesses'] = intval($result['accesses']) + 1;
  4094. }
  4095. if (isset($points_to_add) && !empty($points_to_add)) {
  4096. $my_params['total_score'] = $result['total_score'] + $points_to_add;
  4097. }
  4098. if ($add_user) {
  4099. $my_params['users'] = $result['users'] + 1;
  4100. }
  4101. if (!empty($my_params)) {
  4102. $result = Database::update(
  4103. $table_course_ranking,
  4104. $my_params,
  4105. ['c_id = ? AND session_id = ? AND url_id = ?' => $params]
  4106. );
  4107. }
  4108. }
  4109. return $result;
  4110. }
  4111. /**
  4112. * Add user vote to a course.
  4113. *
  4114. * @param int user id
  4115. * @param int vote [1..5]
  4116. * @param int course id
  4117. * @param int session id
  4118. * @param int url id (access_url_id)
  4119. *
  4120. * @return false|string 'added', 'updated' or 'nothing'
  4121. */
  4122. public static function add_course_vote(
  4123. $user_id,
  4124. $vote,
  4125. $course_id,
  4126. $session_id = 0,
  4127. $url_id = 0
  4128. ) {
  4129. $table_user_course_vote = Database::get_main_table(TABLE_MAIN_USER_REL_COURSE_VOTE);
  4130. $course_id = empty($course_id) ? api_get_course_int_id() : intval($course_id);
  4131. if (empty($course_id) || empty($user_id)) {
  4132. return false;
  4133. }
  4134. if (!in_array($vote, [1, 2, 3, 4, 5])) {
  4135. return false;
  4136. }
  4137. $session_id = empty($session_id) ? api_get_session_id() : intval($session_id);
  4138. $url_id = empty($url_id) ? api_get_current_access_url_id() : intval($url_id);
  4139. $vote = intval($vote);
  4140. $params = [
  4141. 'user_id' => intval($user_id),
  4142. 'c_id' => $course_id,
  4143. 'session_id' => $session_id,
  4144. 'url_id' => $url_id,
  4145. 'vote' => $vote,
  4146. ];
  4147. $action_done = 'nothing';
  4148. $result = Database::select(
  4149. 'id, vote',
  4150. $table_user_course_vote,
  4151. ['where' => ['user_id = ? AND c_id = ? AND session_id = ? AND url_id = ?' => $params]],
  4152. 'first'
  4153. );
  4154. if (empty($result)) {
  4155. Database::insert($table_user_course_vote, $params);
  4156. $points_to_add = $vote;
  4157. $add_user = true;
  4158. $action_done = 'added';
  4159. } else {
  4160. $my_params = ['vote' => $vote];
  4161. $points_to_add = $vote - $result['vote'];
  4162. $add_user = false;
  4163. Database::update(
  4164. $table_user_course_vote,
  4165. $my_params,
  4166. ['user_id = ? AND c_id = ? AND session_id = ? AND url_id = ?' => $params]
  4167. );
  4168. $action_done = 'updated';
  4169. }
  4170. // Current points
  4171. if (!empty($points_to_add)) {
  4172. self::update_course_ranking(
  4173. $course_id,
  4174. $session_id,
  4175. $url_id,
  4176. $points_to_add,
  4177. false,
  4178. $add_user
  4179. );
  4180. }
  4181. return $action_done;
  4182. }
  4183. /**
  4184. * Remove course ranking + user votes.
  4185. *
  4186. * @param int $course_id
  4187. * @param int $session_id
  4188. * @param int $url_id
  4189. */
  4190. public static function remove_course_ranking($course_id, $session_id, $url_id = null)
  4191. {
  4192. $table_course_ranking = Database::get_main_table(TABLE_STATISTIC_TRACK_COURSE_RANKING);
  4193. $table_user_course_vote = Database::get_main_table(TABLE_MAIN_USER_REL_COURSE_VOTE);
  4194. if (!empty($course_id) && isset($session_id)) {
  4195. $url_id = empty($url_id) ? api_get_current_access_url_id() : intval($url_id);
  4196. $params = [
  4197. 'c_id' => $course_id,
  4198. 'session_id' => $session_id,
  4199. 'url_id' => $url_id,
  4200. ];
  4201. Database::delete($table_course_ranking, ['c_id = ? AND session_id = ? AND url_id = ?' => $params]);
  4202. Database::delete($table_user_course_vote, ['c_id = ? AND session_id = ? AND url_id = ?' => $params]);
  4203. }
  4204. }
  4205. /**
  4206. * Returns an array with the hottest courses.
  4207. *
  4208. * @param int $days number of days
  4209. * @param int $limit number of hottest courses
  4210. *
  4211. * @return array
  4212. */
  4213. public static function return_hot_courses($days = 30, $limit = 6)
  4214. {
  4215. if (api_is_invitee()) {
  4216. return [];
  4217. }
  4218. $limit = intval($limit);
  4219. // Getting my courses
  4220. $my_course_list = self::get_courses_list_by_user_id(api_get_user_id());
  4221. $my_course_code_list = [];
  4222. foreach ($my_course_list as $course) {
  4223. $my_course_code_list[$course['real_id']] = $course['real_id'];
  4224. }
  4225. if (api_is_drh()) {
  4226. $courses = self::get_courses_followed_by_drh(api_get_user_id());
  4227. foreach ($courses as $course) {
  4228. $my_course_code_list[$course['real_id']] = $course['real_id'];
  4229. }
  4230. }
  4231. $table_course_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  4232. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  4233. $table_course_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  4234. $urlId = api_get_current_access_url_id();
  4235. //$table_course_access table uses the now() and interval ...
  4236. $now = api_get_utc_datetime();
  4237. $sql = "SELECT COUNT(course_access_id) course_count, a.c_id, visibility
  4238. FROM $table_course c
  4239. INNER JOIN $table_course_access a
  4240. ON (c.id = a.c_id)
  4241. INNER JOIN $table_course_url u
  4242. ON u.c_id = c.id
  4243. WHERE
  4244. u.access_url_id = $urlId AND
  4245. login_course_date <= '$now' AND
  4246. login_course_date > DATE_SUB('$now', INTERVAL $days DAY) AND
  4247. visibility <> ".COURSE_VISIBILITY_CLOSED." AND
  4248. visibility <> ".COURSE_VISIBILITY_HIDDEN."
  4249. GROUP BY a.c_id
  4250. ORDER BY course_count DESC
  4251. LIMIT $limit
  4252. ";
  4253. $result = Database::query($sql);
  4254. $courses = [];
  4255. if (Database::num_rows($result)) {
  4256. $courses = Database::store_result($result, 'ASSOC');
  4257. $courses = self::processHotCourseItem($courses, $my_course_code_list);
  4258. }
  4259. return $courses;
  4260. }
  4261. /**
  4262. * @param array $courses
  4263. * @param array $my_course_code_list
  4264. *
  4265. * @return mixed
  4266. */
  4267. public static function processHotCourseItem($courses, $my_course_code_list = [])
  4268. {
  4269. $hotCourses = [];
  4270. $ajax_url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=add_course_vote';
  4271. $stok = Security::get_existing_token();
  4272. $user_id = api_get_user_id();
  4273. foreach ($courses as $courseId) {
  4274. $course_info = api_get_course_info_by_id($courseId['c_id']);
  4275. $courseCode = $course_info['code'];
  4276. $categoryCode = !empty($course_info['categoryCode']) ? $course_info['categoryCode'] : "";
  4277. $my_course = $course_info;
  4278. $my_course['go_to_course_button'] = '';
  4279. $my_course['register_button'] = '';
  4280. $access_link = self::get_access_link_by_user(
  4281. api_get_user_id(),
  4282. $course_info,
  4283. $my_course_code_list
  4284. );
  4285. $userRegisteredInCourse = self::is_user_subscribed_in_course($user_id, $course_info['code']);
  4286. $userRegisteredInCourseAsTeacher = self::is_course_teacher($user_id, $course_info['code']);
  4287. $userRegistered = $userRegisteredInCourse && $userRegisteredInCourseAsTeacher;
  4288. $my_course['is_course_student'] = $userRegisteredInCourse;
  4289. $my_course['is_course_teacher'] = $userRegisteredInCourseAsTeacher;
  4290. $my_course['is_registered'] = $userRegistered;
  4291. $my_course['title_cut'] = cut($course_info['title'], 45);
  4292. // Course visibility
  4293. if ($access_link && in_array('register', $access_link)) {
  4294. $my_course['register_button'] = Display::url(
  4295. get_lang('Subscribe').' '.
  4296. Display::returnFontAwesomeIcon('sign-in'),
  4297. api_get_path(WEB_COURSE_PATH).$course_info['path'].
  4298. '/index.php?action=subscribe&sec_token='.$stok,
  4299. [
  4300. 'class' => 'btn btn-success btn-sm',
  4301. 'title' => get_lang('Subscribe'),
  4302. 'aria-label' => get_lang('Subscribe'),
  4303. ]
  4304. );
  4305. }
  4306. if ($access_link && in_array('enter', $access_link) ||
  4307. $course_info['visibility'] == COURSE_VISIBILITY_OPEN_WORLD
  4308. ) {
  4309. $my_course['go_to_course_button'] = Display::url(
  4310. get_lang('GoToCourse').' '.
  4311. Display::returnFontAwesomeIcon('share'),
  4312. api_get_path(WEB_COURSE_PATH).$course_info['path'].'/index.php',
  4313. [
  4314. 'class' => 'btn btn-default btn-sm',
  4315. 'title' => get_lang('GoToCourse'),
  4316. 'aria-label' => get_lang('GoToCourse'),
  4317. ]
  4318. );
  4319. }
  4320. if ($access_link && in_array('unsubscribe', $access_link)) {
  4321. $my_course['unsubscribe_button'] = Display::url(
  4322. get_lang('Unreg').' '.
  4323. Display::returnFontAwesomeIcon('sign-out'),
  4324. api_get_path(WEB_CODE_PATH).'auth/courses.php?action=unsubscribe&unsubscribe='.$courseCode
  4325. .'&sec_token='.$stok.'&category_code='.$categoryCode,
  4326. [
  4327. 'class' => 'btn btn-danger btn-sm',
  4328. 'title' => get_lang('Unreg'),
  4329. 'aria-label' => get_lang('Unreg'),
  4330. ]
  4331. );
  4332. }
  4333. // start buycourse validation
  4334. // display the course price and buy button if the buycourses plugin is enabled and this course is configured
  4335. $plugin = BuyCoursesPlugin::create();
  4336. $isThisCourseInSale = $plugin->buyCoursesForGridCatalogValidator(
  4337. $course_info['real_id'],
  4338. BuyCoursesPlugin::PRODUCT_TYPE_COURSE
  4339. );
  4340. if ($isThisCourseInSale) {
  4341. // set the price label
  4342. $my_course['price'] = $isThisCourseInSale['html'];
  4343. // set the Buy button instead register.
  4344. if ($isThisCourseInSale['verificator'] && !empty($my_course['register_button'])) {
  4345. $my_course['register_button'] = $plugin->returnBuyCourseButton(
  4346. $course_info['real_id'],
  4347. BuyCoursesPlugin::PRODUCT_TYPE_COURSE
  4348. );
  4349. }
  4350. }
  4351. // end buycourse validation
  4352. // Description
  4353. $my_course['description_button'] = self::returnDescriptionButton($course_info);
  4354. $my_course['teachers'] = self::getTeachersFromCourse($course_info['real_id'], true);
  4355. $point_info = self::get_course_ranking($course_info['real_id'], 0);
  4356. $my_course['rating_html'] = '';
  4357. if (api_get_configuration_value('hide_course_rating') === false) {
  4358. $my_course['rating_html'] = Display::return_rating_system(
  4359. 'star_'.$course_info['real_id'],
  4360. $ajax_url.'&course_id='.$course_info['real_id'],
  4361. $point_info
  4362. );
  4363. }
  4364. $hotCourses[] = $my_course;
  4365. }
  4366. return $hotCourses;
  4367. }
  4368. /**
  4369. * Get courses count.
  4370. *
  4371. * @param int $access_url_id Access URL ID (optional)
  4372. * @param int $visibility
  4373. *
  4374. * @return int Number of courses
  4375. */
  4376. public static function count_courses($access_url_id = null, $visibility = null)
  4377. {
  4378. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  4379. $table_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  4380. $sql = "SELECT count(c.id) FROM $table_course c";
  4381. if (!empty($access_url_id) && $access_url_id == intval($access_url_id)) {
  4382. $sql .= ", $table_course_rel_access_url u
  4383. WHERE c.id = u.c_id AND u.access_url_id = $access_url_id";
  4384. if (!empty($visibility)) {
  4385. $visibility = intval($visibility);
  4386. $sql .= " AND visibility = $visibility ";
  4387. }
  4388. } else {
  4389. if (!empty($visibility)) {
  4390. $visibility = intval($visibility);
  4391. $sql .= " WHERE visibility = $visibility ";
  4392. }
  4393. }
  4394. $res = Database::query($sql);
  4395. $row = Database::fetch_row($res);
  4396. return $row[0];
  4397. }
  4398. /**
  4399. * Get active courses count.
  4400. * Active = all courses except the ones with hidden visibility.
  4401. *
  4402. * @param int $urlId Access URL ID (optional)
  4403. *
  4404. * @return int Number of courses
  4405. */
  4406. public static function countActiveCourses($urlId = null)
  4407. {
  4408. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  4409. $table_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  4410. $sql = "SELECT count(id) FROM $table_course c";
  4411. if (!empty($urlId) && $urlId == intval($urlId)) {
  4412. $sql .= ", $table_course_rel_access_url u
  4413. WHERE
  4414. c.id = u.c_id AND
  4415. u.access_url_id = $urlId AND
  4416. visibility <> ".COURSE_VISIBILITY_HIDDEN;
  4417. } else {
  4418. $sql .= " WHERE visibility <> ".COURSE_VISIBILITY_HIDDEN;
  4419. }
  4420. $res = Database::query($sql);
  4421. $row = Database::fetch_row($res);
  4422. return $row[0];
  4423. }
  4424. /**
  4425. * Returns the SQL conditions to filter course only visible by the user in the catalogue.
  4426. *
  4427. * @param string $courseTableAlias Alias of the course table
  4428. * @param bool $hideClosed Whether to hide closed and hidden courses
  4429. *
  4430. * @return string SQL conditions
  4431. */
  4432. public static function getCourseVisibilitySQLCondition(
  4433. $courseTableAlias,
  4434. $hideClosed = false
  4435. ) {
  4436. $visibilityCondition = '';
  4437. $hidePrivate = api_get_setting('course_catalog_hide_private');
  4438. if ($hidePrivate === 'true') {
  4439. $visibilityCondition .= " AND $courseTableAlias.visibility <> ".COURSE_VISIBILITY_REGISTERED;
  4440. }
  4441. if ($hideClosed) {
  4442. $visibilityCondition .= " AND $courseTableAlias.visibility NOT IN (".COURSE_VISIBILITY_CLOSED.','.COURSE_VISIBILITY_HIDDEN.')';
  4443. }
  4444. // Check if course have users allowed to see it in the catalogue, then show only if current user is allowed to see it
  4445. $currentUserId = api_get_user_id();
  4446. $restrictedCourses = self::getCatalogueCourseList(true);
  4447. $allowedCoursesToCurrentUser = self::getCatalogueCourseList(true, $currentUserId);
  4448. if (!empty($restrictedCourses)) {
  4449. $visibilityCondition .= ' AND ('.$courseTableAlias.'.code NOT IN ("'.implode('","', $restrictedCourses).'")';
  4450. $visibilityCondition .= ' OR '.$courseTableAlias.'.code IN ("'.implode('","', $allowedCoursesToCurrentUser).'"))';
  4451. }
  4452. // Check if course have users denied to see it in the catalogue, then show only if current user is not denied to see it
  4453. $restrictedCourses = self::getCatalogueCourseList(false);
  4454. $notAllowedCoursesToCurrentUser = self::getCatalogueCourseList(false, $currentUserId);
  4455. if (!empty($restrictedCourses)) {
  4456. $visibilityCondition .= ' AND ('.$courseTableAlias.'.code NOT IN ("'.implode('","', $restrictedCourses).'")';
  4457. $visibilityCondition .= ' OR '.$courseTableAlias.'.code NOT IN ("'.implode('","', $notAllowedCoursesToCurrentUser).'"))';
  4458. }
  4459. return $visibilityCondition;
  4460. }
  4461. /**
  4462. * Return a link to go to the course, validating the visibility of the
  4463. * course and the user status.
  4464. *
  4465. * @param int $uid User ID
  4466. * @param array Course details array
  4467. * @param array List of courses to which the user is subscribed (if not provided, will be generated)
  4468. *
  4469. * @return mixed 'enter' for a link to go to the course or 'register' for a link to subscribe, or false if no access
  4470. */
  4471. public static function get_access_link_by_user($uid, $course, $user_courses = [])
  4472. {
  4473. if (empty($uid) || empty($course)) {
  4474. return false;
  4475. }
  4476. if (empty($user_courses)) {
  4477. // get the array of courses to which the user is subscribed
  4478. $user_courses = self::get_courses_list_by_user_id($uid);
  4479. foreach ($user_courses as $k => $v) {
  4480. $user_courses[$k] = $v['real_id'];
  4481. }
  4482. }
  4483. if (!isset($course['real_id']) && empty($course['real_id'])) {
  4484. $course = api_get_course_info($course['code']);
  4485. }
  4486. if ($course['visibility'] == COURSE_VISIBILITY_HIDDEN) {
  4487. return [];
  4488. }
  4489. $is_admin = api_is_platform_admin_by_id($uid);
  4490. $options = [];
  4491. // Register button
  4492. if (!api_is_anonymous($uid) &&
  4493. (
  4494. ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD || $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM)
  4495. //$course['visibility'] == COURSE_VISIBILITY_REGISTERED && $course['subscribe'] == SUBSCRIBE_ALLOWED
  4496. ) &&
  4497. $course['subscribe'] == SUBSCRIBE_ALLOWED &&
  4498. (!in_array($course['real_id'], $user_courses) || empty($user_courses))
  4499. ) {
  4500. $options[] = 'register';
  4501. }
  4502. // Go To Course button (only if admin, if course public or if student already subscribed)
  4503. if ($is_admin ||
  4504. $course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD && empty($course['registration_code']) ||
  4505. (api_user_is_login($uid) && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM && empty($course['registration_code'])) ||
  4506. (in_array($course['real_id'], $user_courses) && $course['visibility'] != COURSE_VISIBILITY_CLOSED)
  4507. ) {
  4508. $options[] = 'enter';
  4509. }
  4510. if ($is_admin ||
  4511. $course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD && empty($course['registration_code']) ||
  4512. (api_user_is_login($uid) && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM && empty($course['registration_code'])) ||
  4513. (in_array($course['real_id'], $user_courses) && $course['visibility'] != COURSE_VISIBILITY_CLOSED)
  4514. ) {
  4515. $options[] = 'enter';
  4516. }
  4517. if ($course['visibility'] != COURSE_VISIBILITY_HIDDEN &&
  4518. empty($course['registration_code']) &&
  4519. $course['unsubscribe'] == UNSUBSCRIBE_ALLOWED &&
  4520. api_user_is_login($uid) &&
  4521. in_array($course['real_id'], $user_courses)
  4522. ) {
  4523. $options[] = 'unsubscribe';
  4524. }
  4525. return $options;
  4526. }
  4527. /**
  4528. * @param array $courseInfo
  4529. * @param array $teachers
  4530. * @param bool $deleteTeachersNotInList
  4531. * @param bool $editTeacherInSessions
  4532. * @param bool $deleteSessionTeacherNotInList
  4533. * @param array $teacherBackup
  4534. * @param Monolog\Logger $logger
  4535. *
  4536. * @return false|null
  4537. */
  4538. public static function updateTeachers(
  4539. $courseInfo,
  4540. $teachers,
  4541. $deleteTeachersNotInList = true,
  4542. $editTeacherInSessions = false,
  4543. $deleteSessionTeacherNotInList = false,
  4544. $teacherBackup = [],
  4545. $logger = null
  4546. ) {
  4547. if (!is_array($teachers)) {
  4548. $teachers = [$teachers];
  4549. }
  4550. if (empty($courseInfo) || !isset($courseInfo['real_id'])) {
  4551. return false;
  4552. }
  4553. $teachers = array_filter($teachers);
  4554. $courseId = $courseInfo['real_id'];
  4555. $course_code = $courseInfo['code'];
  4556. $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  4557. $alreadyAddedTeachers = self::get_teacher_list_from_course_code($course_code);
  4558. if ($deleteTeachersNotInList) {
  4559. // Delete only teacher relations that doesn't match the selected teachers
  4560. $cond = null;
  4561. if (count($teachers) > 0) {
  4562. foreach ($teachers as $key) {
  4563. $key = Database::escape_string($key);
  4564. $cond .= " AND user_id <> '".$key."'";
  4565. }
  4566. }
  4567. // Recover user categories
  4568. $sql = "SELECT * FROM $course_user_table
  4569. WHERE c_id = $courseId AND status = 1 AND relation_type = 0 ".$cond;
  4570. $result = Database::query($sql);
  4571. if (Database::num_rows($result)) {
  4572. $teachersToDelete = Database::store_result($result, 'ASSOC');
  4573. foreach ($teachersToDelete as $data) {
  4574. $userId = $data['user_id'];
  4575. $teacherBackup[$userId][$course_code] = $data;
  4576. }
  4577. }
  4578. $sql = "DELETE FROM $course_user_table
  4579. WHERE c_id = $courseId AND status = 1 AND relation_type = 0 ".$cond;
  4580. Database::query($sql);
  4581. }
  4582. if (count($teachers) > 0) {
  4583. foreach ($teachers as $userId) {
  4584. $userId = intval($userId);
  4585. // We check if the teacher is already subscribed in this course
  4586. $sql = "SELECT 1 FROM $course_user_table
  4587. WHERE user_id = $userId AND c_id = $courseId";
  4588. $result = Database::query($sql);
  4589. if (Database::num_rows($result)) {
  4590. $sql = "UPDATE $course_user_table
  4591. SET status = 1
  4592. WHERE c_id = $courseId AND user_id = $userId ";
  4593. } else {
  4594. $userCourseCategory = '0';
  4595. if (isset($teacherBackup[$userId]) &&
  4596. isset($teacherBackup[$userId][$course_code])
  4597. ) {
  4598. $courseUserData = $teacherBackup[$userId][$course_code];
  4599. $userCourseCategory = $courseUserData['user_course_cat'];
  4600. if ($logger) {
  4601. $logger->addInfo("Recovering user_course_cat: $userCourseCategory");
  4602. }
  4603. }
  4604. $sql = "INSERT INTO $course_user_table SET
  4605. c_id = $courseId,
  4606. user_id = $userId,
  4607. status = 1,
  4608. is_tutor = 0,
  4609. sort = 0,
  4610. relation_type = 0,
  4611. user_course_cat = $userCourseCategory
  4612. ";
  4613. }
  4614. Database::query($sql);
  4615. }
  4616. }
  4617. if ($editTeacherInSessions) {
  4618. $sessions = SessionManager::get_session_by_course($courseId);
  4619. if (!empty($sessions)) {
  4620. if ($logger) {
  4621. $logger->addInfo("Edit teachers in sessions");
  4622. }
  4623. foreach ($sessions as $session) {
  4624. $sessionId = $session['id'];
  4625. // Remove old and add new
  4626. if ($deleteSessionTeacherNotInList) {
  4627. foreach ($teachers as $userId) {
  4628. if ($logger) {
  4629. $logger->addInfo("Set coach #$userId in session #$sessionId of course #$courseId ");
  4630. }
  4631. SessionManager::set_coach_to_course_session(
  4632. $userId,
  4633. $sessionId,
  4634. $courseId
  4635. );
  4636. }
  4637. $teachersToDelete = [];
  4638. if (!empty($alreadyAddedTeachers)) {
  4639. $teachersToDelete = array_diff(array_keys($alreadyAddedTeachers), $teachers);
  4640. }
  4641. if (!empty($teachersToDelete)) {
  4642. foreach ($teachersToDelete as $userId) {
  4643. if ($logger) {
  4644. $logger->addInfo("Delete coach #$userId in session #$sessionId of course #$courseId ");
  4645. }
  4646. SessionManager::set_coach_to_course_session(
  4647. $userId,
  4648. $sessionId,
  4649. $courseId,
  4650. true
  4651. );
  4652. }
  4653. }
  4654. } else {
  4655. // Add new teachers only
  4656. foreach ($teachers as $userId) {
  4657. if ($logger) {
  4658. $logger->addInfo("Add coach #$userId in session #$sessionId of course #$courseId ");
  4659. }
  4660. SessionManager::set_coach_to_course_session(
  4661. $userId,
  4662. $sessionId,
  4663. $courseId
  4664. );
  4665. }
  4666. }
  4667. }
  4668. }
  4669. }
  4670. }
  4671. /**
  4672. * Course available settings variables see c_course_setting table.
  4673. *
  4674. * @param AppPlugin $appPlugin
  4675. *
  4676. * @return array
  4677. */
  4678. public static function getCourseSettingVariables(AppPlugin $appPlugin)
  4679. {
  4680. $pluginCourseSettings = $appPlugin->getAllPluginCourseSettings();
  4681. $courseSettings = [
  4682. // Get allow_learning_path_theme from table
  4683. 'allow_learning_path_theme',
  4684. // Get allow_open_chat_window from table
  4685. 'allow_open_chat_window',
  4686. 'allow_public_certificates',
  4687. // Get allow_user_edit_agenda from table
  4688. 'allow_user_edit_agenda',
  4689. // Get allow_user_edit_announcement from table
  4690. 'allow_user_edit_announcement',
  4691. // Get allow_user_image_forum from table
  4692. 'allow_user_image_forum',
  4693. //Get allow show user list
  4694. 'allow_user_view_user_list',
  4695. // Get course_theme from table
  4696. 'course_theme',
  4697. //Get allow show user list
  4698. 'display_info_advance_inside_homecourse',
  4699. 'documents_default_visibility',
  4700. // Get send_mail_setting (work)from table
  4701. 'email_alert_manager_on_new_doc',
  4702. // Get send_mail_setting (work)from table
  4703. 'email_alert_manager_on_new_quiz',
  4704. // Get send_mail_setting (dropbox) from table
  4705. 'email_alert_on_new_doc_dropbox',
  4706. 'email_alert_students_on_new_homework',
  4707. // Get send_mail_setting (auth)from table
  4708. 'email_alert_to_teacher_on_new_user_in_course',
  4709. 'enable_lp_auto_launch',
  4710. 'enable_exercise_auto_launch',
  4711. 'enable_document_auto_launch',
  4712. 'pdf_export_watermark_text',
  4713. 'show_system_folders',
  4714. 'exercise_invisible_in_session',
  4715. 'enable_forum_auto_launch',
  4716. 'show_course_in_user_language',
  4717. 'email_to_teachers_on_new_work_feedback',
  4718. 'student_delete_own_publication',
  4719. 'hide_forum_notifications',
  4720. 'quiz_question_limit_per_day',
  4721. ];
  4722. $courseModels = ExerciseLib::getScoreModels();
  4723. if (!empty($courseModels)) {
  4724. $courseSettings[] = 'score_model_id';
  4725. }
  4726. $allowLPReturnLink = api_get_setting('allow_lp_return_link');
  4727. if ($allowLPReturnLink === 'true') {
  4728. $courseSettings[] = 'lp_return_link';
  4729. }
  4730. if (!empty($pluginCourseSettings)) {
  4731. $courseSettings = array_merge(
  4732. $courseSettings,
  4733. $pluginCourseSettings
  4734. );
  4735. }
  4736. return $courseSettings;
  4737. }
  4738. /**
  4739. * @param AppPlugin $appPlugin
  4740. * @param string $variable
  4741. * @param string|array $value
  4742. * @param int $courseId
  4743. *
  4744. * @return bool
  4745. */
  4746. public static function saveCourseConfigurationSetting(AppPlugin $appPlugin, $variable, $value, $courseId)
  4747. {
  4748. $settingList = self::getCourseSettingVariables($appPlugin);
  4749. if (!in_array($variable, $settingList)) {
  4750. return false;
  4751. }
  4752. $courseSettingTable = Database::get_course_table(TABLE_COURSE_SETTING);
  4753. if (is_array($value)) {
  4754. $value = implode(',', $value);
  4755. }
  4756. if (self::hasCourseSetting($variable, $courseId)) {
  4757. // Update
  4758. Database::update(
  4759. $courseSettingTable,
  4760. ['value' => $value],
  4761. ['variable = ? AND c_id = ?' => [$variable, $courseId]]
  4762. );
  4763. } else {
  4764. // Create
  4765. Database::insert(
  4766. $courseSettingTable,
  4767. [
  4768. 'title' => $variable,
  4769. 'value' => $value,
  4770. 'c_id' => $courseId,
  4771. 'variable' => $variable,
  4772. ]
  4773. );
  4774. }
  4775. return true;
  4776. }
  4777. /**
  4778. * Check if course setting exists.
  4779. *
  4780. * @param string $variable
  4781. * @param int $courseId
  4782. *
  4783. * @return bool
  4784. */
  4785. public static function hasCourseSetting($variable, $courseId)
  4786. {
  4787. $courseSetting = Database::get_course_table(TABLE_COURSE_SETTING);
  4788. $courseId = (int) $courseId;
  4789. $variable = Database::escape_string($variable);
  4790. $sql = "SELECT variable FROM $courseSetting
  4791. WHERE c_id = $courseId AND variable = '$variable'";
  4792. $result = Database::query($sql);
  4793. return Database::num_rows($result) > 0;
  4794. }
  4795. /**
  4796. * Get information from the track_e_course_access table.
  4797. *
  4798. * @param int $courseId
  4799. * @param int $sessionId
  4800. * @param string $startDate
  4801. * @param string $endDate
  4802. *
  4803. * @return array
  4804. */
  4805. public static function getCourseAccessPerCourseAndSession(
  4806. $courseId,
  4807. $sessionId,
  4808. $startDate,
  4809. $endDate
  4810. ) {
  4811. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  4812. $courseId = intval($courseId);
  4813. $sessionId = intval($sessionId);
  4814. $startDate = Database::escape_string($startDate);
  4815. $endDate = Database::escape_string($endDate);
  4816. $sql = "SELECT * FROM $table
  4817. WHERE
  4818. c_id = $courseId AND
  4819. session_id = $sessionId AND
  4820. login_course_date BETWEEN '$startDate' AND '$endDate'
  4821. ";
  4822. $result = Database::query($sql);
  4823. return Database::store_result($result);
  4824. }
  4825. /**
  4826. * Get login information from the track_e_course_access table, for any
  4827. * course in the given session.
  4828. *
  4829. * @param int $sessionId
  4830. * @param int $userId
  4831. *
  4832. * @return array
  4833. */
  4834. public static function getFirstCourseAccessPerSessionAndUser($sessionId, $userId)
  4835. {
  4836. $sessionId = (int) $sessionId;
  4837. $userId = (int) $userId;
  4838. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  4839. $sql = "SELECT * FROM $table
  4840. WHERE session_id = $sessionId AND user_id = $userId
  4841. ORDER BY login_course_date ASC
  4842. LIMIT 1";
  4843. $result = Database::query($sql);
  4844. $courseAccess = [];
  4845. if (Database::num_rows($result)) {
  4846. $courseAccess = Database::fetch_array($result, 'ASSOC');
  4847. }
  4848. return $courseAccess;
  4849. }
  4850. /**
  4851. * @param int $courseId
  4852. * @param int $sessionId
  4853. * @param bool $getAllSessions
  4854. *
  4855. * @return mixed
  4856. */
  4857. public static function getCountForum(
  4858. $courseId,
  4859. $sessionId = 0,
  4860. $getAllSessions = false
  4861. ) {
  4862. $forum = Database::get_course_table(TABLE_FORUM);
  4863. if ($getAllSessions) {
  4864. $sql = "SELECT count(*) as count
  4865. FROM $forum f
  4866. WHERE f.c_id = %s";
  4867. } else {
  4868. $sql = "SELECT count(*) as count
  4869. FROM $forum f
  4870. WHERE f.c_id = %s and f.session_id = %s";
  4871. }
  4872. $sql = sprintf($sql, intval($courseId), intval($sessionId));
  4873. $result = Database::query($sql);
  4874. $row = Database::fetch_array($result);
  4875. return $row['count'];
  4876. }
  4877. /**
  4878. * @param int $userId
  4879. * @param int $courseId
  4880. * @param int $sessionId
  4881. *
  4882. * @return mixed
  4883. */
  4884. public static function getCountPostInForumPerUser(
  4885. $userId,
  4886. $courseId,
  4887. $sessionId = 0
  4888. ) {
  4889. $forum = Database::get_course_table(TABLE_FORUM);
  4890. $forum_post = Database::get_course_table(TABLE_FORUM_POST);
  4891. $sql = "SELECT count(distinct post_id) as count
  4892. FROM $forum_post p
  4893. INNER JOIN $forum f
  4894. ON f.forum_id = p.forum_id AND f.c_id = p.c_id
  4895. WHERE p.poster_id = %s and f.session_id = %s and p.c_id = %s";
  4896. $sql = sprintf(
  4897. $sql,
  4898. intval($userId),
  4899. intval($sessionId),
  4900. intval($courseId)
  4901. );
  4902. $result = Database::query($sql);
  4903. $row = Database::fetch_array($result);
  4904. return $row['count'];
  4905. }
  4906. /**
  4907. * @param int $userId
  4908. * @param int $courseId
  4909. * @param int $sessionId
  4910. *
  4911. * @return mixed
  4912. */
  4913. public static function getCountForumPerUser(
  4914. $userId,
  4915. $courseId,
  4916. $sessionId = 0
  4917. ) {
  4918. $forum = Database::get_course_table(TABLE_FORUM);
  4919. $forum_post = Database::get_course_table(TABLE_FORUM_POST);
  4920. $sql = "SELECT count(distinct f.forum_id) as count
  4921. FROM $forum_post p
  4922. INNER JOIN $forum f
  4923. ON f.forum_id = p.forum_id AND f.c_id = p.c_id
  4924. WHERE p.poster_id = %s and f.session_id = %s and p.c_id = %s";
  4925. $sql = sprintf(
  4926. $sql,
  4927. intval($userId),
  4928. intval($sessionId),
  4929. intval($courseId)
  4930. );
  4931. $result = Database::query($sql);
  4932. $row = Database::fetch_array($result);
  4933. return $row['count'];
  4934. }
  4935. /**
  4936. * Returns the course name from a given code.
  4937. *
  4938. * @param string $code
  4939. *
  4940. * @return string
  4941. */
  4942. public static function getCourseNameFromCode($code)
  4943. {
  4944. $tbl_main_categories = Database::get_main_table(TABLE_MAIN_COURSE);
  4945. $code = Database::escape_string($code);
  4946. $sql = "SELECT title
  4947. FROM $tbl_main_categories
  4948. WHERE code = '$code'";
  4949. $result = Database::query($sql);
  4950. if ($col = Database::fetch_array($result)) {
  4951. return $col['title'];
  4952. }
  4953. }
  4954. /**
  4955. * Generates a course code from a course title.
  4956. *
  4957. * @todo Such a function might be useful in other places too. It might be moved in the CourseManager class.
  4958. * @todo the function might be upgraded for avoiding code duplications (currently,
  4959. * it might suggest a code that is already in use)
  4960. *
  4961. * @param string $title A course title
  4962. *
  4963. * @return string A proposed course code
  4964. * +
  4965. * @assert (null,null) === false
  4966. * @assert ('ABC_DEF', null) === 'ABCDEF'
  4967. * @assert ('ABC09*^[%A', null) === 'ABC09A'
  4968. */
  4969. public static function generate_course_code($title)
  4970. {
  4971. return substr(
  4972. preg_replace('/[^A-Z0-9]/', '', strtoupper(api_replace_dangerous_char($title))),
  4973. 0,
  4974. self::MAX_COURSE_LENGTH_CODE
  4975. );
  4976. }
  4977. /**
  4978. * this function gets all the users of the course,
  4979. * including users from linked courses.
  4980. *
  4981. * @param $filterByActive
  4982. *
  4983. * @return array
  4984. */
  4985. public static function getCourseUsers($filterByActive = null)
  4986. {
  4987. // This would return only the users from real courses:
  4988. $userList = self::get_user_list_from_course_code(
  4989. api_get_course_id(),
  4990. api_get_session_id(),
  4991. null,
  4992. null,
  4993. null,
  4994. null,
  4995. false,
  4996. false,
  4997. [],
  4998. [],
  4999. [],
  5000. $filterByActive
  5001. );
  5002. return $userList;
  5003. }
  5004. /**
  5005. * this function gets all the groups of the course,
  5006. * not including linked courses.
  5007. */
  5008. public static function getCourseGroups()
  5009. {
  5010. $sessionId = api_get_session_id();
  5011. if ($sessionId != 0) {
  5012. $groupList = self::get_group_list_of_course(
  5013. api_get_course_id(),
  5014. $sessionId,
  5015. 1
  5016. );
  5017. } else {
  5018. $groupList = self::get_group_list_of_course(
  5019. api_get_course_id(),
  5020. 0,
  5021. 1
  5022. );
  5023. }
  5024. return $groupList;
  5025. }
  5026. /**
  5027. * @param FormValidator $form
  5028. * @param array $alreadySelected
  5029. *
  5030. * @return HTML_QuickForm_element
  5031. */
  5032. public static function addUserGroupMultiSelect(&$form, $alreadySelected)
  5033. {
  5034. $userList = self::getCourseUsers(true);
  5035. $groupList = self::getCourseGroups();
  5036. $array = self::buildSelectOptions(
  5037. $groupList,
  5038. $userList,
  5039. $alreadySelected
  5040. );
  5041. $result = [];
  5042. foreach ($array as $content) {
  5043. $result[$content['value']] = $content['content'];
  5044. }
  5045. return $form->addElement(
  5046. 'advmultiselect',
  5047. 'users',
  5048. get_lang('Users'),
  5049. $result,
  5050. ['select_all_checkbox' => true]
  5051. );
  5052. }
  5053. /**
  5054. * This function separates the users from the groups
  5055. * users have a value USER:XXX (with XXX the groups id have a value
  5056. * GROUP:YYY (with YYY the group id).
  5057. *
  5058. * @param array $to Array of strings that define the type and id of each destination
  5059. *
  5060. * @return array Array of groups and users (each an array of IDs)
  5061. */
  5062. public static function separateUsersGroups($to)
  5063. {
  5064. $groupList = [];
  5065. $userList = [];
  5066. foreach ($to as $to_item) {
  5067. if (!empty($to_item)) {
  5068. $parts = explode(':', $to_item);
  5069. $type = isset($parts[0]) ? $parts[0] : '';
  5070. $id = isset($parts[1]) ? $parts[1] : '';
  5071. switch ($type) {
  5072. case 'GROUP':
  5073. $groupList[] = (int) $id;
  5074. break;
  5075. case 'USER':
  5076. $userList[] = (int) $id;
  5077. break;
  5078. }
  5079. }
  5080. }
  5081. $send_to['groups'] = $groupList;
  5082. $send_to['users'] = $userList;
  5083. return $send_to;
  5084. }
  5085. /**
  5086. * Shows the form for sending a message to a specific group or user.
  5087. *
  5088. * @param FormValidator $form
  5089. * @param array $groupInfo
  5090. * @param array $to
  5091. *
  5092. * @return HTML_QuickForm_element
  5093. */
  5094. public static function addGroupMultiSelect($form, $groupInfo, $to = [])
  5095. {
  5096. $groupUsers = GroupManager::get_subscribed_users($groupInfo);
  5097. $array = self::buildSelectOptions([$groupInfo], $groupUsers, $to);
  5098. $result = [];
  5099. foreach ($array as $content) {
  5100. $result[$content['value']] = $content['content'];
  5101. }
  5102. return $form->addElement('advmultiselect', 'users', get_lang('Users'), $result);
  5103. }
  5104. /**
  5105. * this function shows the form for sending a message to a specific group or user.
  5106. *
  5107. * @param array $groupList
  5108. * @param array $userList
  5109. * @param array $alreadySelected
  5110. *
  5111. * @return array
  5112. */
  5113. public static function buildSelectOptions(
  5114. $groupList = [],
  5115. $userList = [],
  5116. $alreadySelected = []
  5117. ) {
  5118. if (empty($alreadySelected)) {
  5119. $alreadySelected = [];
  5120. }
  5121. $result = [];
  5122. // adding the groups to the select form
  5123. if ($groupList) {
  5124. foreach ($groupList as $thisGroup) {
  5125. $groupId = $thisGroup['iid'];
  5126. if (is_array($alreadySelected)) {
  5127. if (!in_array(
  5128. "GROUP:".$groupId,
  5129. $alreadySelected
  5130. )
  5131. ) {
  5132. $userCount = isset($thisGroup['userNb']) ? $thisGroup['userNb'] : 0;
  5133. if (empty($userCount)) {
  5134. $userCount = isset($thisGroup['count_users']) ? $thisGroup['count_users'] : 0;
  5135. }
  5136. // $alreadySelected is the array containing the groups (and users) that are already selected
  5137. $user_label = ($userCount > 0) ? get_lang('Users') : get_lang('LowerCaseUser');
  5138. $user_disabled = ($userCount > 0) ? "" : "disabled=disabled";
  5139. $result[] = [
  5140. 'disabled' => $user_disabled,
  5141. 'value' => "GROUP:".$groupId,
  5142. // The space before "G" is needed in order to advmultiselect.php js puts groups first
  5143. 'content' => " G: ".$thisGroup['name']." - ".$userCount." ".$user_label,
  5144. ];
  5145. }
  5146. }
  5147. }
  5148. }
  5149. // adding the individual users to the select form
  5150. if ($userList) {
  5151. foreach ($userList as $user) {
  5152. if (is_array($alreadySelected)) {
  5153. if (!in_array(
  5154. "USER:".$user['user_id'],
  5155. $alreadySelected
  5156. )
  5157. ) {
  5158. // $alreadySelected is the array containing the users (and groups) that are already selected
  5159. $result[] = [
  5160. 'value' => "USER:".$user['user_id'],
  5161. 'content' => api_get_person_name($user['firstname'], $user['lastname']),
  5162. ];
  5163. }
  5164. }
  5165. }
  5166. }
  5167. return $result;
  5168. }
  5169. /**
  5170. * @return array a list (array) of all courses
  5171. */
  5172. public static function get_course_list()
  5173. {
  5174. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  5175. return Database::store_result(Database::query("SELECT *, id as real_id FROM $table"));
  5176. }
  5177. /**
  5178. * Returns course code from a given gradebook category's id.
  5179. *
  5180. * @param int Category ID
  5181. *
  5182. * @return string Course code
  5183. */
  5184. public static function get_course_by_category($category_id)
  5185. {
  5186. $category_id = (int) $category_id;
  5187. $info = Database::fetch_array(
  5188. Database::query(
  5189. 'SELECT course_code
  5190. FROM '.Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY).'
  5191. WHERE id = '.$category_id
  5192. ),
  5193. 'ASSOC'
  5194. );
  5195. return $info ? $info['course_code'] : false;
  5196. }
  5197. /**
  5198. * This function gets all the courses that are not in a session.
  5199. *
  5200. * @param date Start date
  5201. * @param date End date
  5202. * @param bool $includeClosed Whether to include closed and hidden courses
  5203. *
  5204. * @return array Not-in-session courses
  5205. */
  5206. public static function getCoursesWithoutSession(
  5207. $startDate = null,
  5208. $endDate = null,
  5209. $includeClosed = false
  5210. ) {
  5211. $dateConditional = ($startDate && $endDate) ?
  5212. " WHERE session_id IN (SELECT id FROM ".Database::get_main_table(TABLE_MAIN_SESSION).
  5213. " WHERE access_start_date = '$startDate' AND access_end_date = '$endDate')" : null;
  5214. $visibility = ($includeClosed ? '' : 'visibility NOT IN (0, 4) AND ');
  5215. $sql = "SELECT id, code, title
  5216. FROM ".Database::get_main_table(TABLE_MAIN_COURSE)."
  5217. WHERE $visibility code NOT IN (
  5218. SELECT DISTINCT course_code
  5219. FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE).$dateConditional."
  5220. )
  5221. ORDER BY id";
  5222. $result = Database::query($sql);
  5223. $courses = [];
  5224. while ($row = Database::fetch_array($result)) {
  5225. $courses[] = $row;
  5226. }
  5227. return $courses;
  5228. }
  5229. /**
  5230. * Get list of courses based on users of a group for a group admin.
  5231. *
  5232. * @param int $userId The user id
  5233. *
  5234. * @return array
  5235. */
  5236. public static function getCoursesFollowedByGroupAdmin($userId)
  5237. {
  5238. $coursesList = [];
  5239. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  5240. $courseUserTable = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  5241. $userGroup = new UserGroup();
  5242. $userIdList = $userGroup->getGroupUsersByUser($userId);
  5243. if (empty($userIdList)) {
  5244. return [];
  5245. }
  5246. $sql = "SELECT DISTINCT(c.id), c.title
  5247. FROM $courseTable c
  5248. INNER JOIN $courseUserTable cru ON c.id = cru.c_id
  5249. WHERE (
  5250. cru.user_id IN (".implode(', ', $userIdList).")
  5251. AND cru.relation_type = 0
  5252. )";
  5253. if (api_is_multiple_url_enabled()) {
  5254. $courseAccessUrlTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  5255. $accessUrlId = api_get_current_access_url_id();
  5256. if ($accessUrlId != -1) {
  5257. $sql = "SELECT DISTINCT(c.id), c.title
  5258. FROM $courseTable c
  5259. INNER JOIN $courseUserTable cru ON c.id = cru.c_id
  5260. INNER JOIN $courseAccessUrlTable crau ON c.id = crau.c_id
  5261. WHERE crau.access_url_id = $accessUrlId
  5262. AND (
  5263. cru.id_user IN (".implode(', ', $userIdList).") AND
  5264. cru.relation_type = 0
  5265. )";
  5266. }
  5267. }
  5268. $result = Database::query($sql);
  5269. while ($row = Database::fetch_assoc($result)) {
  5270. $coursesList[] = $row;
  5271. }
  5272. return $coursesList;
  5273. }
  5274. /**
  5275. * Direct course link see #5299.
  5276. *
  5277. * You can send to your students an URL like this
  5278. * http://chamilodev.beeznest.com/main/auth/inscription.php?c=ABC&e=3
  5279. * Where "c" is the course code and "e" is the exercise Id, after a successful
  5280. * registration the user will be sent to the course or exercise
  5281. *
  5282. * @param array $form_data
  5283. *
  5284. * @return array
  5285. */
  5286. public static function redirectToCourse($form_data)
  5287. {
  5288. $course_code_redirect = Session::read('course_redirect');
  5289. $_user = api_get_user_info();
  5290. $userId = api_get_user_id();
  5291. if (!empty($course_code_redirect)) {
  5292. $course_info = api_get_course_info($course_code_redirect);
  5293. if (!empty($course_info)) {
  5294. if (in_array(
  5295. $course_info['visibility'],
  5296. [COURSE_VISIBILITY_OPEN_PLATFORM, COURSE_VISIBILITY_OPEN_WORLD]
  5297. )
  5298. ) {
  5299. if (self::is_user_subscribed_in_course($userId, $course_info['code'])) {
  5300. $form_data['action'] = $course_info['course_public_url'];
  5301. $form_data['message'] = sprintf(get_lang('YouHaveBeenRegisteredToCourseX'), $course_info['title']);
  5302. $form_data['button'] = Display::button(
  5303. 'next',
  5304. get_lang('GoToCourse', null, $_user['language']),
  5305. ['class' => 'btn btn-primary btn-large']
  5306. );
  5307. $exercise_redirect = intval(Session::read('exercise_redirect'));
  5308. // Specify the course id as the current context does not
  5309. // hold a global $_course array
  5310. $objExercise = new Exercise($course_info['real_id']);
  5311. $result = $objExercise->read($exercise_redirect);
  5312. if (!empty($exercise_redirect) && !empty($result)) {
  5313. $form_data['action'] = api_get_path(WEB_CODE_PATH).
  5314. 'exercise/overview.php?exerciseId='.$exercise_redirect.'&cidReq='.$course_info['code'];
  5315. $form_data['message'] .= '<br />'.get_lang('YouCanAccessTheExercise');
  5316. $form_data['button'] = Display::button(
  5317. 'next',
  5318. get_lang('Go', null, $_user['language']),
  5319. ['class' => 'btn btn-primary btn-large']
  5320. );
  5321. }
  5322. if (!empty($form_data['action'])) {
  5323. header('Location: '.$form_data['action']);
  5324. exit;
  5325. }
  5326. }
  5327. }
  5328. }
  5329. }
  5330. return $form_data;
  5331. }
  5332. /**
  5333. * Return tab of params to display a course title in the My Courses tab
  5334. * Check visibility, right, and notification icons, and load_dirs option
  5335. * get html course params.
  5336. *
  5337. * @param $courseId
  5338. * @param bool $loadDirs
  5339. *
  5340. * @return array with keys ['right_actions'] ['teachers'] ['notifications']
  5341. */
  5342. public static function getCourseParamsForDisplay($courseId, $loadDirs = false)
  5343. {
  5344. $userId = api_get_user_id();
  5345. $courseId = intval($courseId);
  5346. // Table definitions
  5347. $TABLECOURS = Database::get_main_table(TABLE_MAIN_COURSE);
  5348. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  5349. $TABLE_ACCESS_URL_REL_COURSE = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  5350. $current_url_id = api_get_current_access_url_id();
  5351. // Get course list auto-register
  5352. $special_course_list = self::get_special_course_list();
  5353. $without_special_courses = '';
  5354. if (!empty($special_course_list)) {
  5355. $without_special_courses = ' AND course.id NOT IN ("'.implode('","', $special_course_list).'")';
  5356. }
  5357. //AND course_rel_user.relation_type<>".COURSE_RELATION_TYPE_RRHH."
  5358. $sql = "SELECT
  5359. course.id,
  5360. course.title,
  5361. course.code,
  5362. course.subscribe subscr,
  5363. course.unsubscribe unsubscr,
  5364. course_rel_user.status status,
  5365. course_rel_user.sort sort,
  5366. course_rel_user.user_course_cat user_course_cat
  5367. FROM
  5368. $TABLECOURS course
  5369. INNER JOIN $TABLECOURSUSER course_rel_user
  5370. ON (course.id = course_rel_user.c_id)
  5371. INNER JOIN $TABLE_ACCESS_URL_REL_COURSE url
  5372. ON (url.c_id = course.id)
  5373. WHERE
  5374. course.id = $courseId AND
  5375. course_rel_user.user_id = $userId
  5376. $without_special_courses
  5377. ";
  5378. // If multiple URL access mode is enabled, only fetch courses
  5379. // corresponding to the current URL.
  5380. if (api_get_multiple_access_url() && $current_url_id != -1) {
  5381. $sql .= " AND url.c_id = course.id AND access_url_id = $current_url_id";
  5382. }
  5383. // Use user's classification for courses (if any).
  5384. $sql .= " ORDER BY course_rel_user.user_course_cat, course_rel_user.sort ASC";
  5385. $result = Database::query($sql);
  5386. // Browse through all courses. We can only have one course because
  5387. // of the course.id=".intval($courseId) in sql query
  5388. $course = Database::fetch_array($result);
  5389. $course_info = api_get_course_info_by_id($courseId);
  5390. if (empty($course_info)) {
  5391. return '';
  5392. }
  5393. //$course['id_session'] = null;
  5394. $course_info['id_session'] = null;
  5395. $course_info['status'] = $course['status'];
  5396. // For each course, get if there is any notification icon to show
  5397. // (something that would have changed since the user's last visit).
  5398. $show_notification = !api_get_configuration_value('hide_course_notification')
  5399. ? Display::show_notification($course_info)
  5400. : '';
  5401. // New code displaying the user's status in respect to this course.
  5402. $status_icon = Display::return_icon(
  5403. 'blackboard.png',
  5404. $course_info['title'],
  5405. [],
  5406. ICON_SIZE_LARGE
  5407. );
  5408. $params = [];
  5409. $params['right_actions'] = '';
  5410. if (api_is_platform_admin()) {
  5411. if ($loadDirs) {
  5412. $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'), ['align' => 'absmiddle'], ICON_SIZE_SMALL).'</a>';
  5413. $params['right_actions'] .= '<a href="'.api_get_path(WEB_CODE_PATH).'course_info/infocours.php?cidReq='.$course['code'].'">'.
  5414. Display::return_icon('edit.png', get_lang('Edit'), ['align' => 'absmiddle'], ICON_SIZE_SMALL).
  5415. '</a>';
  5416. $params['right_actions'] .= Display::div(
  5417. '',
  5418. [
  5419. 'id' => 'document_result_'.$course_info['real_id'].'_0',
  5420. 'class' => 'document_preview_container',
  5421. ]
  5422. );
  5423. } else {
  5424. $params['right_actions'] .= '<a class="btn btn-default btn-sm" title="'.get_lang('Edit').'" href="'.api_get_path(WEB_CODE_PATH).'course_info/infocours.php?cidReq='.$course['code'].'">'.
  5425. Display::returnFontAwesomeIcon('pencil').'</a>';
  5426. }
  5427. } else {
  5428. if ($course_info['visibility'] != COURSE_VISIBILITY_CLOSED) {
  5429. if ($loadDirs) {
  5430. $params['right_actions'] .= '<a id="document_preview_'.$course_info['real_id'].'_0" class="document_preview" href="javascript:void(0);">'.
  5431. Display::return_icon('folder.png', get_lang('Documents'), ['align' => 'absmiddle'], ICON_SIZE_SMALL).'</a>';
  5432. $params['right_actions'] .= Display::div(
  5433. '',
  5434. [
  5435. 'id' => 'document_result_'.$course_info['real_id'].'_0',
  5436. 'class' => 'document_preview_container',
  5437. ]
  5438. );
  5439. } else {
  5440. if ($course_info['status'] == COURSEMANAGER) {
  5441. $params['right_actions'] .= '<a class="btn btn-default btn-sm" title="'.get_lang('Edit').'" href="'.api_get_path(WEB_CODE_PATH).'course_info/infocours.php?cidReq='.$course['code'].'">'.
  5442. Display::returnFontAwesomeIcon('pencil').'</a>';
  5443. }
  5444. }
  5445. }
  5446. }
  5447. $course_title_url = '';
  5448. if ($course_info['visibility'] != COURSE_VISIBILITY_CLOSED || $course['status'] == COURSEMANAGER) {
  5449. $course_title_url = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/?id_session=0';
  5450. $course_title = Display::url($course_info['title'], $course_title_url);
  5451. } else {
  5452. $course_title = $course_info['title'].' '.Display::tag(
  5453. 'span',
  5454. get_lang('CourseClosed'),
  5455. ['class' => 'item_closed']
  5456. );
  5457. }
  5458. // Start displaying the course block itself
  5459. if (api_get_setting('display_coursecode_in_courselist') === 'true') {
  5460. $course_title .= ' ('.$course_info['visual_code'].') ';
  5461. }
  5462. $teachers = '';
  5463. if (api_get_setting('display_teacher_in_courselist') === 'true') {
  5464. $teachers = self::getTeacherListFromCourseCodeToString(
  5465. $course['code'],
  5466. self::USER_SEPARATOR,
  5467. true
  5468. );
  5469. }
  5470. $params['link'] = $course_title_url;
  5471. $params['icon'] = $status_icon;
  5472. $params['title'] = $course_title;
  5473. $params['teachers'] = $teachers;
  5474. if ($course_info['visibility'] != COURSE_VISIBILITY_CLOSED) {
  5475. $params['notifications'] = $show_notification;
  5476. }
  5477. return $params;
  5478. }
  5479. /**
  5480. * Get the course id based on the original id and field name in the extra fields.
  5481. * Returns 0 if course was not found.
  5482. *
  5483. * @param string $original_course_id_value Original course id
  5484. * @param string $original_course_id_name Original field name
  5485. *
  5486. * @return int Course id
  5487. */
  5488. public static function get_course_id_from_original_id($original_course_id_value, $original_course_id_name)
  5489. {
  5490. $extraFieldValue = new ExtraFieldValue('course');
  5491. $value = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
  5492. $original_course_id_name,
  5493. $original_course_id_value
  5494. );
  5495. if ($value) {
  5496. return $value['item_id'];
  5497. }
  5498. return 0;
  5499. }
  5500. /**
  5501. * Helper function to create a default gradebook (if necessary) upon course creation.
  5502. *
  5503. * @param int $modelId The gradebook model ID
  5504. * @param string $courseCode Course code
  5505. */
  5506. public static function createDefaultGradebook($modelId, $courseCode)
  5507. {
  5508. if (api_get_setting('gradebook_enable_grade_model') === 'true') {
  5509. //Create gradebook_category for the new course and add
  5510. // a gradebook model for the course
  5511. if (isset($modelId) &&
  5512. !empty($modelId) &&
  5513. $modelId != '-1'
  5514. ) {
  5515. GradebookUtils::create_default_course_gradebook(
  5516. $courseCode,
  5517. $modelId
  5518. );
  5519. }
  5520. }
  5521. }
  5522. /**
  5523. * Helper function to check if there is a course template and, if so, to
  5524. * copy the template as basis for the new course.
  5525. *
  5526. * @param string $courseCode Course code
  5527. * @param int $courseTemplate 0 if no course template is defined
  5528. */
  5529. public static function useTemplateAsBasisIfRequired($courseCode, $courseTemplate)
  5530. {
  5531. $template = api_get_setting('course_creation_use_template');
  5532. $teacherCanSelectCourseTemplate = api_get_setting('teacher_can_select_course_template') === 'true';
  5533. $courseTemplate = isset($courseTemplate) ? intval($courseTemplate) : 0;
  5534. $useTemplate = false;
  5535. if ($teacherCanSelectCourseTemplate && $courseTemplate) {
  5536. $useTemplate = true;
  5537. $originCourse = api_get_course_info_by_id($courseTemplate);
  5538. } elseif (!empty($template)) {
  5539. $useTemplate = true;
  5540. $originCourse = api_get_course_info_by_id($template);
  5541. }
  5542. if ($useTemplate) {
  5543. // Include the necessary libraries to generate a course copy
  5544. // Call the course copy object
  5545. $originCourse['official_code'] = $originCourse['code'];
  5546. $cb = new CourseBuilder(null, $originCourse);
  5547. $course = $cb->build(null, $originCourse['code']);
  5548. $cr = new CourseRestorer($course);
  5549. $cr->set_file_option();
  5550. $cr->restore($courseCode);
  5551. }
  5552. }
  5553. /**
  5554. * Helper method to get the number of users defined with a specific course extra field.
  5555. *
  5556. * @param string $name Field title
  5557. * @param string $tableExtraFields The extra fields table name
  5558. * @param string $tableUserFieldValues The user extra field value table name
  5559. *
  5560. * @return int The number of users with this extra field with a specific value
  5561. */
  5562. public static function getCountRegisteredUsersWithCourseExtraField(
  5563. $name,
  5564. $tableExtraFields = '',
  5565. $tableUserFieldValues = ''
  5566. ) {
  5567. if (empty($tableExtraFields)) {
  5568. $tableExtraFields = Database::get_main_table(TABLE_EXTRA_FIELD);
  5569. }
  5570. if (empty($tableUserFieldValues)) {
  5571. $tableUserFieldValues = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  5572. }
  5573. $registered_users_with_extra_field = 0;
  5574. if (!empty($name) && $name != '-') {
  5575. $extraFieldType = EntityExtraField::COURSE_FIELD_TYPE;
  5576. $name = Database::escape_string($name);
  5577. $sql = "SELECT count(v.item_id) as count
  5578. FROM $tableUserFieldValues v
  5579. INNER JOIN $tableExtraFields f
  5580. ON (f.id = v.field_id)
  5581. WHERE value = '$name' AND extra_field_type = $extraFieldType";
  5582. $result_count = Database::query($sql);
  5583. if (Database::num_rows($result_count)) {
  5584. $row_count = Database::fetch_array($result_count);
  5585. $registered_users_with_extra_field = $row_count['count'];
  5586. }
  5587. }
  5588. return $registered_users_with_extra_field;
  5589. }
  5590. /**
  5591. * Get the course categories form a course list.
  5592. *
  5593. * @param array $courseList
  5594. *
  5595. * @return array
  5596. */
  5597. public static function getCourseCategoriesFromCourseList(array $courseList)
  5598. {
  5599. $allCategories = array_column($courseList, 'category');
  5600. $categories = array_unique($allCategories);
  5601. sort($categories);
  5602. return $categories;
  5603. }
  5604. /**
  5605. * Display the description button of a course in the course catalog.
  5606. *
  5607. * @param array $course
  5608. *
  5609. * @return string HTML string
  5610. */
  5611. public static function returnDescriptionButton($course)
  5612. {
  5613. if (empty($course)) {
  5614. return '';
  5615. }
  5616. if (api_get_setting('show_courses_descriptions_in_catalog') == 'true') {
  5617. $title = $course['title'];
  5618. $url = api_get_path(WEB_CODE_PATH).'inc/ajax/course_home.ajax.php?a=show_course_information&code='.$course['code'];
  5619. $html = Display::url(
  5620. Display::returnFontAwesomeIcon('info-circle', 'lg'),
  5621. $url,
  5622. [
  5623. 'class' => 'ajax btn btn-default btn-sm',
  5624. 'data-title' => $title,
  5625. 'title' => get_lang('Description'),
  5626. 'aria-label' => get_lang('Description'),
  5627. 'data-size' => 'lg',
  5628. ]
  5629. );
  5630. return $html;
  5631. }
  5632. return '';
  5633. }
  5634. /**
  5635. * @param Course $course
  5636. *
  5637. * @return bool
  5638. */
  5639. public static function hasPicture(Course $course)
  5640. {
  5641. return file_exists(api_get_path(SYS_COURSE_PATH).$course->getDirectory().'/course-pic85x85.png');
  5642. }
  5643. /**
  5644. * Get the course picture path.
  5645. *
  5646. * @param Course $course
  5647. * @param bool $fullSize
  5648. *
  5649. * @return string|null
  5650. */
  5651. public static function getPicturePath(Course $course, $fullSize = false)
  5652. {
  5653. if (!self::hasPicture($course)) {
  5654. return null;
  5655. }
  5656. if ($fullSize) {
  5657. return api_get_path(WEB_COURSE_PATH).$course->getDirectory().'/course-pic.png';
  5658. }
  5659. return api_get_path(WEB_COURSE_PATH).$course->getDirectory().'/course-pic85x85.png';
  5660. }
  5661. /**
  5662. * Check if a specific access-url-related setting is a problem or not.
  5663. *
  5664. * @param array $_configuration The $_configuration array
  5665. * @param int $accessUrlId The access URL ID
  5666. * @param string $param
  5667. * @param string $msgLabel
  5668. *
  5669. * @return bool|string
  5670. */
  5671. private static function checkCreateCourseAccessUrlParam($_configuration, $accessUrlId, $param, $msgLabel)
  5672. {
  5673. if (isset($_configuration[$accessUrlId][$param]) && $_configuration[$accessUrlId][$param] > 0) {
  5674. $num = self::count_courses($accessUrlId);
  5675. if ($num >= $_configuration[$accessUrlId][$param]) {
  5676. api_warn_hosting_contact($param);
  5677. Display::addFlash(
  5678. Display::return_message($msgLabel)
  5679. );
  5680. }
  5681. }
  5682. return false;
  5683. }
  5684. /**
  5685. * Fill course with all necessary items.
  5686. *
  5687. * @param array $courseInfo Course info array
  5688. * @param array $params Parameters from the course creation form
  5689. * @param int $authorId
  5690. */
  5691. private static function fillCourse($courseInfo, $params, $authorId = 0)
  5692. {
  5693. $authorId = empty($authorId) ? api_get_user_id() : (int) $authorId;
  5694. AddCourse::prepare_course_repository($courseInfo['directory']);
  5695. AddCourse::fill_db_course(
  5696. $courseInfo['real_id'],
  5697. $courseInfo['directory'],
  5698. $courseInfo['course_language'],
  5699. $params['exemplary_content'],
  5700. $authorId
  5701. );
  5702. if (isset($params['gradebook_model_id'])) {
  5703. self::createDefaultGradebook(
  5704. $params['gradebook_model_id'],
  5705. $courseInfo['code']
  5706. );
  5707. }
  5708. // If parameter defined, copy the contents from a specific
  5709. // template course into this new course
  5710. if (isset($params['course_template'])) {
  5711. self::useTemplateAsBasisIfRequired(
  5712. $courseInfo['id'],
  5713. $params['course_template']
  5714. );
  5715. }
  5716. $params['course_code'] = $courseInfo['code'];
  5717. $params['item_id'] = $courseInfo['real_id'];
  5718. $courseFieldValue = new ExtraFieldValue('course');
  5719. $courseFieldValue->saveFieldValues($params);
  5720. }
  5721. }