work.lib.php 180 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. use Chamilo\CourseBundle\Entity\CStudentPublication;
  5. use ChamiloSession as Session;
  6. /**
  7. * @package chamilo.work
  8. *
  9. * @author Thomas, Hugues, Christophe - original version
  10. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University -
  11. * ability for course admins to specify wether uploaded documents are visible or invisible by default.
  12. * @author Roan Embrechts, code refactoring and virtual course support
  13. * @author Frederic Vauthier, directories management
  14. * @author Julio Montoya <gugli100@gmail.com> BeezNest 2011 LOTS of bug fixes
  15. *
  16. * @todo this lib should be convert in a static class and moved to main/inc/lib
  17. */
  18. /**
  19. * Displays action links (for admins, authorized groups members and authorized students).
  20. *
  21. * @param int Whether to show tool options
  22. * @param int Whether to show upload form option
  23. * @param bool $isTutor
  24. */
  25. function displayWorkActionLinks($id, $action, $isTutor)
  26. {
  27. $id = $my_back_id = intval($id);
  28. if ($action == 'list') {
  29. $my_back_id = 0;
  30. }
  31. $output = '';
  32. $origin = api_get_origin();
  33. if (!empty($id)) {
  34. $output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&id='.$my_back_id.'">'.
  35. Display::return_icon('back.png', get_lang('Back to Assignments list'), '', ICON_SIZE_MEDIUM).
  36. '</a>';
  37. }
  38. if (($isTutor || api_is_allowed_to_edit(null, true)) &&
  39. $origin != 'learnpath'
  40. ) {
  41. // Create dir
  42. if (empty($id)) {
  43. $output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=create_dir">';
  44. $output .= Display::return_icon(
  45. 'new_work.png',
  46. get_lang('Create assignment'),
  47. '',
  48. ICON_SIZE_MEDIUM
  49. );
  50. $output .= '</a>';
  51. }
  52. }
  53. if (api_is_allowed_to_edit(null, true) && $origin != 'learnpath' && $action == 'list') {
  54. $output .= '<a id="open-view-list" href="#">'.
  55. Display::return_icon(
  56. 'listwork.png',
  57. get_lang('View students'),
  58. '',
  59. ICON_SIZE_MEDIUM
  60. ).
  61. '</a>';
  62. }
  63. if ($output != '') {
  64. echo '<div class="actions">';
  65. echo $output;
  66. echo '</div>';
  67. }
  68. }
  69. /**
  70. * @param string $path
  71. * @param int $courseId
  72. *
  73. * @return array
  74. */
  75. function get_work_data_by_path($path, $courseId = 0)
  76. {
  77. $path = Database::escape_string($path);
  78. $courseId = intval($courseId);
  79. if (empty($courseId)) {
  80. $courseId = api_get_course_int_id();
  81. }
  82. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  83. $sql = "SELECT * FROM $table
  84. WHERE url = '$path' AND c_id = $courseId ";
  85. $result = Database::query($sql);
  86. $return = [];
  87. if (Database::num_rows($result)) {
  88. $return = Database::fetch_array($result, 'ASSOC');
  89. }
  90. return $return;
  91. }
  92. /**
  93. * @param int $id
  94. * @param int $courseId
  95. * @param int $sessionId
  96. *
  97. * @return array
  98. */
  99. function get_work_data_by_id($id, $courseId = 0, $sessionId = 0)
  100. {
  101. $id = (int) $id;
  102. $courseId = ((int) $courseId) ?: api_get_course_int_id();
  103. $course = api_get_course_entity($courseId);
  104. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  105. $sessionCondition = '';
  106. if (!empty($sessionId)) {
  107. $sessionCondition = api_get_session_condition($sessionId, true);
  108. }
  109. $webCodePath = api_get_path(WEB_CODE_PATH);
  110. $sql = "SELECT * FROM $table
  111. WHERE
  112. id = $id AND c_id = $courseId
  113. $sessionCondition";
  114. $result = Database::query($sql);
  115. $work = [];
  116. if (Database::num_rows($result)) {
  117. $work = Database::fetch_array($result, 'ASSOC');
  118. if (empty($work['title'])) {
  119. $work['title'] = basename($work['url']);
  120. }
  121. $work['download_url'] = $webCodePath.'work/download.php?id='.$work['id'].'&'.api_get_cidreq();
  122. $work['view_url'] = $webCodePath.'work/view.php?id='.$work['id'].'&'.api_get_cidreq();
  123. $work['show_url'] = $webCodePath.'work/show_file.php?id='.$work['id'].'&'.api_get_cidreq();
  124. $work['show_content'] = '';
  125. if ($work['contains_file']) {
  126. $fileType = '';
  127. $file = api_get_path(SYS_COURSE_PATH).$course->getDirectory().'/'.$work['url'];
  128. if (file_exists($file)) {
  129. $fileType = mime_content_type($file);
  130. }
  131. if (in_array($fileType, ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'])) {
  132. $work['show_content'] = Display::img($work['show_url'], $work['title'], null, false);
  133. } elseif (false !== strpos($fileType, 'video/')) {
  134. $work['show_content'] = Display::tag(
  135. 'video',
  136. get_lang('File format not supported'),
  137. ['src' => $work['show_url']]
  138. );
  139. }
  140. }
  141. $fieldValue = new ExtraFieldValue('work');
  142. $work['extra'] = $fieldValue->getAllValuesForAnItem($id, true);
  143. }
  144. return $work;
  145. }
  146. /**
  147. * @param int $user_id
  148. * @param int $work_id
  149. *
  150. * @return int
  151. */
  152. function get_work_count_by_student($user_id, $work_id)
  153. {
  154. $user_id = intval($user_id);
  155. $work_id = intval($work_id);
  156. $course_id = api_get_course_int_id();
  157. $session_id = api_get_session_id();
  158. $sessionCondition = api_get_session_condition($session_id);
  159. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  160. $sql = "SELECT COUNT(*) as count
  161. FROM $table
  162. WHERE
  163. c_id = $course_id AND
  164. parent_id = $work_id AND
  165. user_id = $user_id AND
  166. active IN (0, 1)
  167. $sessionCondition";
  168. $result = Database::query($sql);
  169. $return = 0;
  170. if (Database::num_rows($result)) {
  171. $return = Database::fetch_row($result, 'ASSOC');
  172. $return = intval($return[0]);
  173. }
  174. return $return;
  175. }
  176. /**
  177. * @param int $id
  178. * @param int $courseId
  179. *
  180. * @return array
  181. */
  182. function get_work_assignment_by_id($id, $courseId = 0)
  183. {
  184. $courseId = intval($courseId);
  185. if (empty($courseId)) {
  186. $courseId = api_get_course_int_id();
  187. }
  188. $id = intval($id);
  189. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  190. $sql = "SELECT * FROM $table
  191. WHERE c_id = $courseId AND publication_id = $id";
  192. $result = Database::query($sql);
  193. $return = [];
  194. if (Database::num_rows($result)) {
  195. $return = Database::fetch_array($result, 'ASSOC');
  196. }
  197. return $return;
  198. }
  199. /**
  200. * @param int $id
  201. * @param array $my_folder_data
  202. * @param string $add_in_where_query
  203. * @param int $course_id
  204. * @param int $session_id
  205. *
  206. * @return array
  207. */
  208. function getWorkList($id, $my_folder_data, $add_in_where_query = null, $course_id = 0, $session_id = 0)
  209. {
  210. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  211. $course_id = $course_id ? $course_id : api_get_course_int_id();
  212. $session_id = $session_id ? $session_id : api_get_session_id();
  213. $condition_session = api_get_session_condition($session_id);
  214. $group_id = api_get_group_id();
  215. $groupIid = 0;
  216. if ($group_id) {
  217. $groupInfo = GroupManager::get_group_properties($group_id);
  218. if ($groupInfo) {
  219. $groupIid = $groupInfo['iid'];
  220. }
  221. }
  222. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  223. $linkInfo = GradebookUtils::isResourceInCourseGradebook(
  224. api_get_course_id(),
  225. 3,
  226. $id,
  227. api_get_session_id()
  228. );
  229. if ($linkInfo) {
  230. $workInGradeBookLinkId = $linkInfo['id'];
  231. if ($workInGradeBookLinkId) {
  232. if ($is_allowed_to_edit) {
  233. if (intval($my_folder_data['qualification']) == 0) {
  234. echo Display::return_message(
  235. get_lang('Max weight need to be provided'),
  236. 'warning'
  237. );
  238. }
  239. }
  240. }
  241. }
  242. $contains_file_query = '';
  243. // Get list from database
  244. if ($is_allowed_to_edit) {
  245. $active_condition = ' active IN (0, 1)';
  246. $sql = "SELECT * FROM $work_table
  247. WHERE
  248. c_id = $course_id
  249. $add_in_where_query
  250. $condition_session AND
  251. $active_condition AND
  252. (parent_id = 0)
  253. $contains_file_query AND
  254. post_group_id = $groupIid
  255. ORDER BY sent_date DESC";
  256. } else {
  257. if (!empty($group_id)) {
  258. // set to select only messages posted by the user's group
  259. $group_query = " WHERE c_id = $course_id AND post_group_id = $groupIid";
  260. $subdirs_query = " AND parent_id = 0";
  261. } else {
  262. $group_query = " WHERE c_id = $course_id AND (post_group_id = '0' OR post_group_id is NULL) ";
  263. $subdirs_query = " AND parent_id = 0";
  264. }
  265. //@todo how we can active or not an assignment?
  266. $active_condition = ' AND active IN (1, 0)';
  267. $sql = "SELECT * FROM $work_table
  268. $group_query
  269. $subdirs_query
  270. $add_in_where_query
  271. $active_condition
  272. $condition_session
  273. ORDER BY title";
  274. }
  275. $work_parents = [];
  276. $sql_result = Database::query($sql);
  277. if (Database::num_rows($sql_result)) {
  278. while ($work = Database::fetch_object($sql_result)) {
  279. if ($work->parent_id == 0) {
  280. $work_parents[] = $work;
  281. }
  282. }
  283. }
  284. return $work_parents;
  285. }
  286. /**
  287. * @param int $userId
  288. * @param int $courseId
  289. * @param int $sessionId
  290. *
  291. * @return array
  292. */
  293. function getWorkPerUser($userId, $courseId = 0, $sessionId = 0)
  294. {
  295. $works = getWorkList(null, null, null, $courseId, $sessionId);
  296. $result = [];
  297. if (!empty($works)) {
  298. foreach ($works as $workData) {
  299. $workId = $workData->id;
  300. $result[$workId]['work'] = $workData;
  301. $result[$workId]['work']->user_results = get_work_user_list(
  302. 0,
  303. 100,
  304. null,
  305. null,
  306. $workId,
  307. null,
  308. $userId,
  309. false,
  310. $courseId,
  311. $sessionId
  312. );
  313. }
  314. }
  315. return $result;
  316. }
  317. /**
  318. * @param int $workId
  319. * @param int $groupId
  320. * @param int $course_id
  321. * @param int $sessionId
  322. *
  323. * @return mixed
  324. */
  325. function getUniqueStudentAttemptsTotal($workId, $groupId, $course_id, $sessionId)
  326. {
  327. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  328. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  329. $course_id = intval($course_id);
  330. $workId = intval($workId);
  331. $sessionId = intval($sessionId);
  332. $groupId = intval($groupId);
  333. $sessionCondition = api_get_session_condition(
  334. $sessionId,
  335. true,
  336. false,
  337. 'w.session_id'
  338. );
  339. $groupIid = 0;
  340. if ($groupId) {
  341. $groupInfo = GroupManager::get_group_properties($groupId);
  342. $groupIid = $groupInfo['iid'];
  343. }
  344. $sql = "SELECT count(DISTINCT u.user_id)
  345. FROM $work_table w
  346. INNER JOIN $user_table u
  347. ON w.user_id = u.user_id
  348. WHERE
  349. w.c_id = $course_id
  350. $sessionCondition AND
  351. w.parent_id = $workId AND
  352. w.post_group_id = $groupIid AND
  353. w.active IN (0, 1)
  354. ";
  355. $res_document = Database::query($sql);
  356. $rowCount = Database::fetch_row($res_document);
  357. return $rowCount[0];
  358. }
  359. /**
  360. * @param mixed $workId
  361. * @param int $groupId
  362. * @param int $course_id
  363. * @param int $sessionId
  364. * @param int $userId user id to filter
  365. * @param array $onlyUserList only parse this user list
  366. *
  367. * @return mixed
  368. */
  369. function getUniqueStudentAttempts(
  370. $workId,
  371. $groupId,
  372. $course_id,
  373. $sessionId,
  374. $userId = null,
  375. $onlyUserList = []
  376. ) {
  377. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  378. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  379. $course_id = intval($course_id);
  380. $workCondition = null;
  381. if (is_array($workId)) {
  382. $workId = array_map('intval', $workId);
  383. $workId = implode("','", $workId);
  384. $workCondition = " w.parent_id IN ('".$workId."') AND";
  385. } else {
  386. $workId = intval($workId);
  387. $workCondition = " w.parent_id = ".$workId." AND";
  388. }
  389. $sessionId = intval($sessionId);
  390. $groupId = intval($groupId);
  391. $studentCondition = null;
  392. if (!empty($onlyUserList)) {
  393. $onlyUserList = array_map('intval', $onlyUserList);
  394. $studentCondition = "AND u.user_id IN ('".implode("', '", $onlyUserList)."') ";
  395. } else {
  396. if (empty($userId)) {
  397. return 0;
  398. }
  399. }
  400. $groupIid = 0;
  401. if ($groupId) {
  402. $groupInfo = GroupManager::get_group_properties($groupId);
  403. $groupIid = $groupInfo['iid'];
  404. }
  405. $sessionCondition = api_get_session_condition(
  406. $sessionId,
  407. true,
  408. false,
  409. 'w.session_id'
  410. );
  411. $sql = "SELECT count(*) FROM (
  412. SELECT count(*), w.parent_id
  413. FROM $work_table w
  414. INNER JOIN $user_table u
  415. ON w.user_id = u.user_id
  416. WHERE
  417. w.filetype = 'file' AND
  418. w.c_id = $course_id
  419. $sessionCondition AND
  420. $workCondition
  421. w.post_group_id = $groupIid AND
  422. w.active IN (0, 1) $studentCondition
  423. ";
  424. if (!empty($userId)) {
  425. $userId = intval($userId);
  426. $sql .= " AND u.user_id = ".$userId;
  427. }
  428. $sql .= " GROUP BY u.user_id, w.parent_id) as t";
  429. $result = Database::query($sql);
  430. $row = Database::fetch_row($result);
  431. return $row[0];
  432. }
  433. /**
  434. * Shows the work list (student view).
  435. *
  436. * @return string
  437. */
  438. function showStudentWorkGrid()
  439. {
  440. $courseInfo = api_get_course_info();
  441. $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_work_student&'.api_get_cidreq();
  442. $columns = [
  443. get_lang('Type'),
  444. get_lang('Title'),
  445. get_lang('Deadline'),
  446. get_lang('Feedback'),
  447. get_lang('Last upload'),
  448. ];
  449. $columnModel = [
  450. ['name' => 'type', 'index' => 'type', 'width' => '30', 'align' => 'center', 'sortable' => 'false'],
  451. ['name' => 'title', 'index' => 'title', 'width' => '250', 'align' => 'left'],
  452. ['name' => 'expires_on', 'index' => 'expires_on', 'width' => '80', 'align' => 'center', 'sortable' => 'false'],
  453. ['name' => 'feedback', 'index' => 'feedback', 'width' => '80', 'align' => 'center', 'sortable' => 'false'],
  454. ['name' => 'last_upload', 'index' => 'feedback', 'width' => '125', 'align' => 'center', 'sortable' => 'false'],
  455. ];
  456. if ($courseInfo['show_score'] == 0) {
  457. $columnModel[] = [
  458. 'name' => 'others',
  459. 'index' => 'others',
  460. 'width' => '80',
  461. 'align' => 'left',
  462. 'sortable' => 'false',
  463. ];
  464. $columns[] = get_lang('Others');
  465. }
  466. $params = [
  467. 'autowidth' => 'true',
  468. 'height' => 'auto',
  469. ];
  470. $html = '<script>
  471. $(function() {
  472. '.Display::grid_js('workList', $url, $columns, $columnModel, $params, [], null, true).'
  473. });
  474. </script>';
  475. $html .= Display::grid_html('workList');
  476. return $html;
  477. }
  478. /**
  479. * Shows the work list (teacher view).
  480. *
  481. * @return string
  482. */
  483. function showTeacherWorkGrid()
  484. {
  485. $columnModel = [
  486. [
  487. 'name' => 'type',
  488. 'index' => 'type',
  489. 'width' => '35',
  490. 'align' => 'center',
  491. 'sortable' => 'false',
  492. ],
  493. [
  494. 'name' => 'title',
  495. 'index' => 'title',
  496. 'width' => '300',
  497. 'align' => 'left',
  498. 'wrap_cell' => "true",
  499. ],
  500. ['name' => 'sent_date', 'index' => 'sent_date', 'width' => '125', 'align' => 'center'],
  501. ['name' => 'expires_on', 'index' => 'expires_on', 'width' => '125', 'align' => 'center'],
  502. [
  503. 'name' => 'amount',
  504. 'index' => 'amount',
  505. 'width' => '110',
  506. 'align' => 'center',
  507. 'sortable' => 'false',
  508. ],
  509. [
  510. 'name' => 'actions',
  511. 'index' => 'actions',
  512. 'width' => '110',
  513. 'align' => 'left',
  514. 'sortable' => 'false',
  515. ],
  516. ];
  517. $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_work_teacher&'.api_get_cidreq();
  518. $deleteUrl = api_get_path(WEB_AJAX_PATH).'work.ajax.php?a=delete_work&'.api_get_cidreq();
  519. $columns = [
  520. get_lang('Type'),
  521. get_lang('Title'),
  522. get_lang('Sent date'),
  523. get_lang('Deadline'),
  524. get_lang('Number submitted'),
  525. get_lang('Detail'),
  526. ];
  527. $params = [
  528. 'multiselect' => true,
  529. 'autowidth' => 'true',
  530. 'height' => 'auto',
  531. ];
  532. $html = '<script>
  533. $(function() {
  534. '.Display::grid_js('workList', $url, $columns, $columnModel, $params, [], null, true).'
  535. $("#workList").jqGrid(
  536. "navGrid",
  537. "#workList_pager",
  538. { edit: false, add: false, del: true },
  539. { height:280, reloadAfterSubmit:false }, // edit options
  540. { height:280, reloadAfterSubmit:false }, // add options
  541. { reloadAfterSubmit:false, url: "'.$deleteUrl.'" }, // del options
  542. { width:500 } // search options
  543. );
  544. });
  545. </script>';
  546. $html .= Display::grid_html('workList');
  547. return $html;
  548. }
  549. /**
  550. * Builds the form thats enables the user to
  551. * select a directory to browse/upload in
  552. * This function has been copied from the document/document.inc.php library.
  553. *
  554. * @param array $folders
  555. * @param string $curdirpath
  556. * @param string $group_dir
  557. *
  558. * @return string html form
  559. */
  560. // TODO: This function is a candidate for removal, it is not used anywhere.
  561. function build_work_directory_selector($folders, $curdirpath, $group_dir = '')
  562. {
  563. $form = '<form name="selector" action="'.api_get_self().'?'.api_get_cidreq().'" method="POST">';
  564. $form .= get_lang('Current folder').'
  565. <select name="curdirpath" onchange="javascript: document.selector.submit();">';
  566. //group documents cannot be uploaded in the root
  567. if ($group_dir == '') {
  568. $form .= '<option value="/">/ ('.get_lang('root').')</option>';
  569. if (is_array($folders)) {
  570. foreach ($folders as $folder) {
  571. $selected = ($curdirpath == $folder) ? ' selected="selected"' : '';
  572. $form .= '<option'.$selected.' value="'.$folder.'">'.$folder.'</option>'."\n";
  573. }
  574. }
  575. } else {
  576. foreach ($folders as $folder) {
  577. $selected = ($curdirpath == $folder) ? ' selected="selected"' : '';
  578. $display_folder = substr($folder, strlen($group_dir));
  579. $display_folder = ($display_folder == '') ? '/ ('.get_lang('root').')' : $display_folder;
  580. $form .= '<option'.$selected.' value="'.$folder.'">'.$display_folder.'</option>'."\n";
  581. }
  582. }
  583. $form .= '</select>';
  584. $form .= '<noscript><input type="submit" name="change_path" value="'.get_lang('Validate').'" /></noscript>';
  585. $form .= '</form>';
  586. return $form;
  587. }
  588. /**
  589. * Builds the form that enables the user to
  590. * move a document from one directory to another
  591. * This function has been copied from the document/document.inc.php library.
  592. *
  593. * @param array $folders
  594. * @param string $curdirpath
  595. * @param string $move_file
  596. * @param string $group_dir
  597. *
  598. * @return string html form
  599. */
  600. function build_work_move_to_selector($folders, $curdirpath, $move_file, $group_dir = '')
  601. {
  602. $course_id = api_get_course_int_id();
  603. $move_file = intval($move_file);
  604. $tbl_work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  605. $sql = "SELECT title, url FROM $tbl_work
  606. WHERE c_id = $course_id AND id ='".$move_file."'";
  607. $result = Database::query($sql);
  608. $row = Database::fetch_array($result, 'ASSOC');
  609. $title = empty($row['title']) ? basename($row['url']) : $row['title'];
  610. $form = new FormValidator(
  611. 'move_to_form',
  612. 'post',
  613. api_get_self().'?'.api_get_cidreq().'&curdirpath='.Security::remove_XSS($curdirpath)
  614. );
  615. $form->addHeader(get_lang('Move the file').' - '.Security::remove_XSS($title));
  616. $form->addHidden('item_id', $move_file);
  617. $form->addHidden('action', 'move_to');
  618. // Group documents cannot be uploaded in the root
  619. if ($group_dir == '') {
  620. if (is_array($folders)) {
  621. foreach ($folders as $fid => $folder) {
  622. //you cannot move a file to:
  623. //1. current directory
  624. //2. inside the folder you want to move
  625. //3. inside a subfolder of the folder you want to move
  626. if (($curdirpath != $folder) &&
  627. ($folder != $move_file) &&
  628. (substr($folder, 0, strlen($move_file) + 1) != $move_file.'/')
  629. ) {
  630. $options[$fid] = $folder;
  631. }
  632. }
  633. }
  634. } else {
  635. if ($curdirpath != '/') {
  636. $form .= '<option value="0">/ ('.get_lang('root').')</option>';
  637. }
  638. foreach ($folders as $fid => $folder) {
  639. if (($curdirpath != $folder) && ($folder != $move_file) &&
  640. (substr($folder, 0, strlen($move_file) + 1) != $move_file.'/')
  641. ) {
  642. //cannot copy dir into his own subdir
  643. $display_folder = substr($folder, strlen($group_dir));
  644. $display_folder = ($display_folder == '') ? '/ ('.get_lang('root').')' : $display_folder;
  645. //$form .= '<option value="'.$fid.'">'.$display_folder.'</option>'."\n";
  646. $options[$fid] = $display_folder;
  647. }
  648. }
  649. }
  650. $form->addSelect('move_to_id', get_lang('Select'), $options);
  651. $form->addButtonSend(get_lang('Move the file'), 'move_file_submit');
  652. return $form->returnForm();
  653. }
  654. /**
  655. * creates a new directory trying to find a directory name
  656. * that doesn't already exist.
  657. *
  658. * @author Hugues Peeters <hugues.peeters@claroline.net>
  659. * @author Bert Vanderkimpen
  660. * @author Yannick Warnier <ywarnier@beeznest.org> Adaptation for work tool
  661. *
  662. * @param string $workDir Base work dir (.../work)
  663. * @param string $desiredDirName complete path of the desired name
  664. *
  665. * @return string actual directory name if it succeeds, boolean false otherwise
  666. */
  667. function create_unexisting_work_directory($workDir, $desiredDirName)
  668. {
  669. $counter = 0;
  670. $workDir = (substr($workDir, -1, 1) == '/' ? $workDir : $workDir.'/');
  671. $checkDirName = $desiredDirName;
  672. while (file_exists($workDir.$checkDirName)) {
  673. $counter++;
  674. $checkDirName = $desiredDirName.$counter;
  675. }
  676. if (@mkdir($workDir.$checkDirName, api_get_permissions_for_new_directories())) {
  677. return $checkDirName;
  678. } else {
  679. return false;
  680. }
  681. }
  682. /**
  683. * Delete a work-tool directory.
  684. *
  685. * @param int $id work directory id to delete
  686. *
  687. * @return int -1 on error
  688. */
  689. function deleteDirWork($id)
  690. {
  691. $locked = api_resource_is_locked_by_gradebook($id, LINK_STUDENTPUBLICATION);
  692. if ($locked == true) {
  693. echo Display::return_message(get_lang('This option is not available because this activity is contained by an assessment, which is currently locked. To unlock the assessment, ask your platform administrator.'), 'warning');
  694. return false;
  695. }
  696. $_course = api_get_course_info();
  697. $id = intval($id);
  698. $work_data = get_work_data_by_id($id);
  699. if (empty($work_data)) {
  700. return false;
  701. }
  702. $base_work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/work';
  703. $work_data_url = $base_work_dir.$work_data['url'];
  704. $check = Security::check_abs_path($work_data_url.'/', $base_work_dir.'/');
  705. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  706. $TSTDPUBASG = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  707. $t_agenda = Database::get_course_table(TABLE_AGENDA);
  708. $course_id = api_get_course_int_id();
  709. $sessionId = api_get_session_id();
  710. if (!empty($work_data['url'])) {
  711. if ($check) {
  712. $consideredWorkingTime = api_get_configuration_value('considered_working_time');
  713. if (!empty($consideredWorkingTime)) {
  714. $fieldValue = new ExtraFieldValue('work');
  715. $resultExtra = $fieldValue->getAllValuesForAnItem(
  716. $work_data['id'],
  717. true
  718. );
  719. $workingTime = null;
  720. foreach ($resultExtra as $field) {
  721. $field = $field['value'];
  722. if ($consideredWorkingTime == $field->getField()->getVariable()) {
  723. $workingTime = $field->getValue();
  724. break;
  725. }
  726. }
  727. $courseUsers = CourseManager::get_user_list_from_course_code($_course['code'], $sessionId);
  728. if (!empty($workingTime)) {
  729. foreach ($courseUsers as $user) {
  730. $userWorks = get_work_user_list(
  731. 0,
  732. 100,
  733. null,
  734. null,
  735. $work_data['id'],
  736. null,
  737. $user['user_id'],
  738. false,
  739. $course_id,
  740. $sessionId
  741. );
  742. if (count($userWorks) != 1) {
  743. continue;
  744. }
  745. Event::eventRemoveVirtualCourseTime($course_id, $user['user_id'], $sessionId, $workingTime);
  746. }
  747. }
  748. }
  749. // Deleting all contents inside the folder
  750. $sql = "UPDATE $table SET active = 2
  751. WHERE c_id = $course_id AND filetype = 'folder' AND id = $id";
  752. Database::query($sql);
  753. $sql = "UPDATE $table SET active = 2
  754. WHERE c_id = $course_id AND parent_id = $id";
  755. Database::query($sql);
  756. $new_dir = $work_data_url.'_DELETED_'.$id;
  757. if (api_get_setting('permanently_remove_deleted_files') == 'true') {
  758. my_delete($work_data_url);
  759. } else {
  760. if (file_exists($work_data_url)) {
  761. rename($work_data_url, $new_dir);
  762. }
  763. }
  764. // Gets calendar_id from student_publication_assigment
  765. $sql = "SELECT add_to_calendar FROM $TSTDPUBASG
  766. WHERE c_id = $course_id AND publication_id = $id";
  767. $res = Database::query($sql);
  768. $calendar_id = Database::fetch_row($res);
  769. // delete from agenda if it exists
  770. if (!empty($calendar_id[0])) {
  771. $sql = "DELETE FROM $t_agenda
  772. WHERE c_id = $course_id AND id = '".$calendar_id[0]."'";
  773. Database::query($sql);
  774. }
  775. $sql = "DELETE FROM $TSTDPUBASG
  776. WHERE c_id = $course_id AND publication_id = $id";
  777. Database::query($sql);
  778. Skill::deleteSkillsFromItem($id, ITEM_TYPE_STUDENT_PUBLICATION);
  779. Event::addEvent(
  780. LOG_WORK_DIR_DELETE,
  781. LOG_WORK_DATA,
  782. [
  783. 'id' => $work_data['id'],
  784. 'url' => $work_data['url'],
  785. 'title' => $work_data['title'],
  786. ],
  787. null,
  788. api_get_user_id(),
  789. api_get_course_int_id(),
  790. $sessionId
  791. );
  792. $linkInfo = GradebookUtils::isResourceInCourseGradebook(
  793. api_get_course_id(),
  794. 3,
  795. $id,
  796. api_get_session_id()
  797. );
  798. $link_id = $linkInfo['id'];
  799. if ($linkInfo !== false) {
  800. GradebookUtils::remove_resource_from_course_gradebook($link_id);
  801. }
  802. return true;
  803. }
  804. }
  805. }
  806. /**
  807. * Get the path of a document in the student_publication table (path relative to the course directory).
  808. *
  809. * @param int $id
  810. *
  811. * @return string Path (or -1 on error)
  812. */
  813. function get_work_path($id)
  814. {
  815. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  816. $course_id = api_get_course_int_id();
  817. $sql = 'SELECT url FROM '.$table.'
  818. WHERE c_id = '.$course_id.' AND id='.intval($id);
  819. $res = Database::query($sql);
  820. if (Database::num_rows($res)) {
  821. $row = Database::fetch_array($res);
  822. return $row['url'];
  823. }
  824. return -1;
  825. }
  826. /**
  827. * Update the url of a work in the student_publication table.
  828. *
  829. * @param int $id of the work to update
  830. * @param string $new_path Destination directory where the work has been moved (must end with a '/')
  831. * @param int $parent_id
  832. *
  833. * @return mixed Int -1 on error, sql query result on success
  834. */
  835. function updateWorkUrl($id, $new_path, $parent_id)
  836. {
  837. if (empty($id)) {
  838. return -1;
  839. }
  840. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  841. $course_id = api_get_course_int_id();
  842. $id = intval($id);
  843. $parent_id = intval($parent_id);
  844. $sql = "SELECT * FROM $table
  845. WHERE c_id = $course_id AND id = $id";
  846. $res = Database::query($sql);
  847. if (Database::num_rows($res) != 1) {
  848. return -1;
  849. } else {
  850. $row = Database::fetch_array($res);
  851. $filename = basename($row['url']);
  852. $new_url = $new_path.$filename;
  853. $new_url = Database::escape_string($new_url);
  854. $sql = "UPDATE $table SET
  855. url = '$new_url',
  856. parent_id = '$parent_id'
  857. WHERE c_id = $course_id AND id = $id";
  858. $res = Database::query($sql);
  859. return $res;
  860. }
  861. }
  862. /**
  863. * Update the url of a dir in the student_publication table.
  864. *
  865. * @param array $work_data work original data
  866. * @param string $newPath Example: "folder1"
  867. *
  868. * @return bool
  869. */
  870. function updateDirName($work_data, $newPath)
  871. {
  872. $course_id = $work_data['c_id'];
  873. $work_id = intval($work_data['iid']);
  874. $oldPath = $work_data['url'];
  875. $originalNewPath = Database::escape_string($newPath);
  876. $newPath = Database::escape_string($newPath);
  877. $newPath = api_replace_dangerous_char($newPath);
  878. $newPath = disable_dangerous_file($newPath);
  879. if ($oldPath == '/'.$newPath) {
  880. return true;
  881. }
  882. if (!empty($newPath)) {
  883. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  884. $sql = "UPDATE $table SET
  885. title = '".$originalNewPath."'
  886. WHERE
  887. c_id = $course_id AND
  888. iid = $work_id";
  889. Database::query($sql);
  890. }
  891. }
  892. /**
  893. * returns all the javascript that is required for easily
  894. * validation when you create a work
  895. * this goes into the $htmlHeadXtra[] array.
  896. */
  897. function to_javascript_work()
  898. {
  899. $js = '<script>
  900. function updateDocumentTitle(value) {
  901. var temp = value.indexOf("/");
  902. //linux path
  903. if(temp != -1){
  904. temp=value.split("/");
  905. } else {
  906. temp=value.split("\\\");
  907. }
  908. var fullFilename = temp[temp.length - 1];
  909. var baseFilename = fullFilename;
  910. // get file extension
  911. var fileExtension = "";
  912. if (fullFilename.match(/\..+/)) {
  913. fileInfo = fullFilename.match(/(.*)\.([^.]+)$/);
  914. if (fileInfo.length > 1) {
  915. fileExtension = "."+fileInfo[fileInfo.length - 1];
  916. baseFilename = fileInfo[fileInfo.length - 2];
  917. }
  918. }
  919. document.getElementById("file_upload").value = baseFilename;
  920. document.getElementById("file_extension").value = fileExtension;
  921. $("#contains_file_id").attr("value", 1);
  922. }
  923. function setFocus() {
  924. $("#work_title").focus();
  925. }
  926. $(function() {
  927. setFocus();
  928. var checked = $("#expiry_date").attr("checked");
  929. if (checked) {
  930. $("#option2").show();
  931. } else {
  932. $("#option2").hide();
  933. }
  934. var checkedEndDate = $("#end_date").attr("checked");
  935. if (checkedEndDate) {
  936. $("#option3").show();
  937. $("#ends_on").attr("checked", true);
  938. } else {
  939. $("#option3").hide();
  940. $("#ends_on").attr("checked", false);
  941. }
  942. $("#expiry_date").click(function() {
  943. $("#option2").toggle();
  944. });
  945. $("#end_date").click(function() {
  946. $("#option3").toggle();
  947. });
  948. });
  949. </script>';
  950. return $js;
  951. }
  952. /**
  953. * Gets the id of a student publication with a given path.
  954. *
  955. * @param string $path
  956. *
  957. * @return true if is found / false if not found
  958. */
  959. // TODO: The name of this function does not fit with the kind of information it returns.
  960. // Maybe check_work_id() or is_work_id()?
  961. function get_work_id($path)
  962. {
  963. $TBL_STUDENT_PUBLICATION = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  964. $TBL_PROP_TABLE = Database::get_course_table(TABLE_ITEM_PROPERTY);
  965. $course_id = api_get_course_int_id();
  966. $path = Database::escape_string($path);
  967. if (api_is_allowed_to_edit()) {
  968. $sql = "SELECT work.id
  969. FROM $TBL_STUDENT_PUBLICATION AS work, $TBL_PROP_TABLE AS props
  970. WHERE
  971. props.c_id = $course_id AND
  972. work.c_id = $course_id AND
  973. props.tool='work' AND
  974. work.id=props.ref AND
  975. work.url LIKE 'work/".$path."%' AND
  976. work.filetype='file' AND
  977. props.visibility<>'2'";
  978. } else {
  979. $sql = "SELECT work.id
  980. FROM $TBL_STUDENT_PUBLICATION AS work, $TBL_PROP_TABLE AS props
  981. WHERE
  982. props.c_id = $course_id AND
  983. work.c_id = $course_id AND
  984. props.tool='work' AND
  985. work.id=props.ref AND
  986. work.url LIKE 'work/".$path."%' AND
  987. work.filetype='file' AND
  988. props.visibility<>'2' AND
  989. props.lastedit_user_id = '".api_get_user_id()."'";
  990. }
  991. $result = Database::query($sql);
  992. $num_rows = Database::num_rows($result);
  993. if ($result && $num_rows > 0) {
  994. return true;
  995. } else {
  996. return false;
  997. }
  998. }
  999. /**
  1000. * @param int $work_id
  1001. * @param int $onlyMeUserId show only my works
  1002. * @param int $notMeUserId show works from everyone except me
  1003. *
  1004. * @return int
  1005. */
  1006. function get_count_work($work_id, $onlyMeUserId = null, $notMeUserId = null)
  1007. {
  1008. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1009. $iprop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1010. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  1011. $is_allowed_to_edit = api_is_allowed_to_edit(null, true) || api_is_coach();
  1012. $session_id = api_get_session_id();
  1013. $condition_session = api_get_session_condition(
  1014. $session_id,
  1015. true,
  1016. false,
  1017. 'work.session_id'
  1018. );
  1019. $group_id = api_get_group_id();
  1020. $course_info = api_get_course_info();
  1021. $course_id = $course_info['real_id'];
  1022. $work_id = (int) $work_id;
  1023. $groupIid = 0;
  1024. if ($group_id) {
  1025. $groupInfo = GroupManager::get_group_properties($group_id);
  1026. if ($groupInfo && isset($groupInfo['iid'])) {
  1027. $groupIid = (int) $groupInfo['iid'];
  1028. }
  1029. }
  1030. if (!empty($group_id)) {
  1031. // set to select only messages posted by the user's group
  1032. $extra_conditions = " work.post_group_id = '".$groupIid."' ";
  1033. } else {
  1034. $extra_conditions = " (work.post_group_id = '0' or work.post_group_id IS NULL) ";
  1035. }
  1036. if ($is_allowed_to_edit) {
  1037. $extra_conditions .= ' AND work.active IN (0, 1) ';
  1038. } else {
  1039. $extra_conditions .= ' AND work.active IN (0, 1) AND accepted = 1';
  1040. if (isset($course_info['show_score']) && $course_info['show_score'] == 1) {
  1041. $extra_conditions .= " AND work.user_id = ".api_get_user_id()." ";
  1042. } else {
  1043. $extra_conditions .= '';
  1044. }
  1045. }
  1046. $extra_conditions .= " AND parent_id = ".$work_id." ";
  1047. $where_condition = null;
  1048. if (!empty($notMeUserId)) {
  1049. $where_condition .= " AND u.user_id <> ".intval($notMeUserId);
  1050. }
  1051. if (!empty($onlyMeUserId)) {
  1052. $where_condition .= " AND u.user_id = ".intval($onlyMeUserId);
  1053. }
  1054. $sql = "SELECT count(*) as count
  1055. FROM $iprop_table prop
  1056. INNER JOIN $work_table work
  1057. ON (
  1058. prop.ref = work.id AND
  1059. prop.c_id = $course_id AND
  1060. prop.tool='work' AND
  1061. prop.visibility <> 2 AND
  1062. work.c_id = $course_id
  1063. )
  1064. INNER JOIN $user_table u
  1065. ON (work.user_id = u.user_id)
  1066. WHERE $extra_conditions $where_condition $condition_session";
  1067. $result = Database::query($sql);
  1068. $users_with_work = 0;
  1069. if (Database::num_rows($result)) {
  1070. $result = Database::fetch_array($result);
  1071. $users_with_work = $result['count'];
  1072. }
  1073. return $users_with_work;
  1074. }
  1075. /**
  1076. * @param int $start
  1077. * @param int $limit
  1078. * @param string $column
  1079. * @param string $direction
  1080. * @param string $where_condition
  1081. * @param bool $getCount
  1082. *
  1083. * @return array
  1084. */
  1085. function getWorkListStudent(
  1086. $start,
  1087. $limit,
  1088. $column,
  1089. $direction,
  1090. $where_condition,
  1091. $getCount = false
  1092. ) {
  1093. $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1094. $workTableAssignment = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  1095. $courseInfo = api_get_course_info();
  1096. $course_id = $courseInfo['real_id'];
  1097. $session_id = api_get_session_id();
  1098. $condition_session = api_get_session_condition($session_id);
  1099. $group_id = api_get_group_id();
  1100. $userId = api_get_user_id();
  1101. $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(
  1102. api_get_user_id(),
  1103. $courseInfo
  1104. );
  1105. if (!in_array($direction, ['asc', 'desc'])) {
  1106. $direction = 'desc';
  1107. }
  1108. if (!empty($where_condition)) {
  1109. $where_condition = ' AND '.$where_condition;
  1110. }
  1111. $column = !empty($column) ? Database::escape_string($column) : 'sent_date';
  1112. $start = (int) $start;
  1113. $limit = (int) $limit;
  1114. $groupIid = 0;
  1115. if ($group_id) {
  1116. $groupInfo = GroupManager::get_group_properties($group_id);
  1117. if ($groupInfo) {
  1118. $groupIid = (int) $groupInfo['iid'];
  1119. }
  1120. }
  1121. if (!empty($groupIid)) {
  1122. $group_query = " WHERE w.c_id = $course_id AND post_group_id = $groupIid";
  1123. $subdirs_query = 'AND parent_id = 0';
  1124. } else {
  1125. $group_query = " WHERE w.c_id = $course_id AND (post_group_id = '0' or post_group_id is NULL) ";
  1126. $subdirs_query = 'AND parent_id = 0';
  1127. }
  1128. $active_condition = ' AND active IN (1, 0)';
  1129. if ($getCount) {
  1130. $select = 'SELECT count(w.id) as count ';
  1131. } else {
  1132. $select = 'SELECT w.*, a.expires_on, expires_on, ends_on, enable_qualification ';
  1133. }
  1134. $sql = "$select
  1135. FROM $workTable w
  1136. LEFT JOIN $workTableAssignment a
  1137. ON (a.publication_id = w.id AND a.c_id = w.c_id)
  1138. $group_query
  1139. $subdirs_query
  1140. $active_condition
  1141. $condition_session
  1142. $where_condition
  1143. ";
  1144. $sql .= " ORDER BY $column $direction ";
  1145. if (!empty($start) && !empty($limit)) {
  1146. $sql .= " LIMIT $start, $limit";
  1147. }
  1148. $result = Database::query($sql);
  1149. if ($getCount) {
  1150. $row = Database::fetch_array($result);
  1151. return $row['count'];
  1152. }
  1153. $works = [];
  1154. $url = api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq();
  1155. if ($isDrhOfCourse) {
  1156. $url = api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq();
  1157. }
  1158. $urlOthers = api_get_path(WEB_CODE_PATH).'work/work_list_others.php?'.api_get_cidreq().'&id=';
  1159. while ($work = Database::fetch_array($result, 'ASSOC')) {
  1160. $isSubscribed = userIsSubscribedToWork($userId, $work['id'], $course_id);
  1161. if ($isSubscribed == false) {
  1162. continue;
  1163. }
  1164. $visibility = api_get_item_visibility($courseInfo, 'work', $work['id'], $session_id);
  1165. if ($visibility != 1) {
  1166. continue;
  1167. }
  1168. $work['type'] = Display::return_icon('work.png');
  1169. $work['expires_on'] = empty($work['expires_on']) ? null : api_get_local_time($work['expires_on']);
  1170. if (empty($work['title'])) {
  1171. $work['title'] = basename($work['url']);
  1172. }
  1173. $whereCondition = " AND u.user_id = $userId ";
  1174. $workList = get_work_user_list(
  1175. 0,
  1176. 1000,
  1177. null,
  1178. null,
  1179. $work['id'],
  1180. $whereCondition
  1181. );
  1182. $count = getTotalWorkComment($workList, $courseInfo);
  1183. $lastWork = getLastWorkStudentFromParentByUser($userId, $work, $courseInfo);
  1184. if (!is_null($count) && !empty($count)) {
  1185. $urlView = api_get_path(WEB_CODE_PATH).'work/view.php?id='.$lastWork['id'].'&'.api_get_cidreq();
  1186. $feedback = '&nbsp;'.Display::url(
  1187. Display::returnFontAwesomeIcon('comments-o'),
  1188. $urlView,
  1189. ['title' => get_lang('View')]
  1190. );
  1191. $work['feedback'] = ' '.Display::label($count.' '.get_lang('Feedback'), 'info').$feedback;
  1192. }
  1193. if (!empty($lastWork)) {
  1194. $work['last_upload'] = (!empty($lastWork['qualification'])) ? $lastWork['qualification_rounded'].' - ' : '';
  1195. $work['last_upload'] .= api_get_local_time($lastWork['sent_date']);
  1196. }
  1197. $work['title'] = Display::url($work['title'], $url.'&id='.$work['id']);
  1198. $work['others'] = Display::url(
  1199. Display::return_icon('group.png', get_lang('Others')),
  1200. $urlOthers.$work['id']
  1201. );
  1202. $works[] = $work;
  1203. }
  1204. return $works;
  1205. }
  1206. /**
  1207. * @param int $start
  1208. * @param int $limit
  1209. * @param string $column
  1210. * @param string $direction
  1211. * @param string $where_condition
  1212. * @param bool $getCount
  1213. *
  1214. * @return array
  1215. */
  1216. function getWorkListTeacher(
  1217. $start,
  1218. $limit,
  1219. $column,
  1220. $direction,
  1221. $where_condition,
  1222. $getCount = false
  1223. ) {
  1224. $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1225. $workTableAssignment = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  1226. $courseInfo = api_get_course_info();
  1227. $course_id = api_get_course_int_id();
  1228. $session_id = api_get_session_id();
  1229. $condition_session = api_get_session_condition($session_id);
  1230. $group_id = api_get_group_id();
  1231. $groupIid = 0;
  1232. if ($group_id) {
  1233. $groupInfo = GroupManager::get_group_properties($group_id);
  1234. $groupIid = $groupInfo['iid'];
  1235. }
  1236. $groupIid = (int) $groupIid;
  1237. $is_allowed_to_edit = api_is_allowed_to_edit() || api_is_coach();
  1238. if (!in_array($direction, ['asc', 'desc'])) {
  1239. $direction = 'desc';
  1240. }
  1241. if (!empty($where_condition)) {
  1242. $where_condition = ' AND '.$where_condition;
  1243. }
  1244. $column = !empty($column) ? Database::escape_string($column) : 'sent_date';
  1245. $start = intval($start);
  1246. $limit = intval($limit);
  1247. $works = [];
  1248. // Get list from database
  1249. if ($is_allowed_to_edit) {
  1250. $active_condition = ' active IN (0, 1)';
  1251. if ($getCount) {
  1252. $select = " SELECT count(w.id) as count";
  1253. } else {
  1254. $select = " SELECT w.*, a.expires_on, expires_on, ends_on, enable_qualification ";
  1255. }
  1256. $sql = " $select
  1257. FROM $workTable w
  1258. LEFT JOIN $workTableAssignment a
  1259. ON (a.publication_id = w.id AND a.c_id = w.c_id)
  1260. WHERE
  1261. w.c_id = $course_id
  1262. $condition_session AND
  1263. $active_condition AND
  1264. parent_id = 0 AND
  1265. post_group_id = $groupIid
  1266. $where_condition
  1267. ORDER BY $column $direction
  1268. LIMIT $start, $limit";
  1269. $result = Database::query($sql);
  1270. if ($getCount) {
  1271. $row = Database::fetch_array($result);
  1272. return $row['count'];
  1273. }
  1274. $url = api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq();
  1275. $blockEdition = api_get_configuration_value('block_student_publication_edition');
  1276. while ($work = Database::fetch_array($result, 'ASSOC')) {
  1277. $workId = $work['id'];
  1278. $work['type'] = Display::return_icon('work.png');
  1279. $work['expires_on'] = empty($work['expires_on']) ? null : api_get_local_time($work['expires_on']);
  1280. $countUniqueAttempts = getUniqueStudentAttemptsTotal(
  1281. $workId,
  1282. $group_id,
  1283. $course_id,
  1284. $session_id
  1285. );
  1286. $totalUsers = getStudentSubscribedToWork(
  1287. $workId,
  1288. $course_id,
  1289. $group_id,
  1290. $session_id,
  1291. true
  1292. );
  1293. $work['amount'] = Display::label(
  1294. $countUniqueAttempts.'/'.
  1295. $totalUsers,
  1296. 'success'
  1297. );
  1298. $visibility = api_get_item_visibility($courseInfo, 'work', $workId, $session_id);
  1299. if ($visibility == 1) {
  1300. $icon = 'visible.png';
  1301. $text = get_lang('Visible');
  1302. $action = 'invisible';
  1303. $class = '';
  1304. } else {
  1305. $icon = 'invisible.png';
  1306. $text = get_lang('invisible');
  1307. $action = 'visible';
  1308. $class = 'muted';
  1309. }
  1310. $visibilityLink = Display::url(
  1311. Display::return_icon($icon, $text, [], ICON_SIZE_SMALL),
  1312. api_get_path(WEB_CODE_PATH).'work/work.php?id='.$workId.'&action='.$action.'&'.api_get_cidreq()
  1313. );
  1314. if (empty($work['title'])) {
  1315. $work['title'] = basename($work['url']);
  1316. }
  1317. $work['title'] = Display::url($work['title'], $url.'&id='.$workId, ['class' => $class]);
  1318. $work['title'] .= ' '.Display::label(get_count_work($work['id']), 'success');
  1319. $work['sent_date'] = api_get_local_time($work['sent_date']);
  1320. if ($blockEdition && !api_is_platform_admin()) {
  1321. $editLink = '';
  1322. } else {
  1323. $editLink = Display::url(
  1324. Display::return_icon('edit.png', get_lang('Edit'), [], ICON_SIZE_SMALL),
  1325. api_get_path(WEB_CODE_PATH).'work/edit_work.php?id='.$workId.'&'.api_get_cidreq()
  1326. );
  1327. }
  1328. $correctionLink = '&nbsp;'.Display::url(
  1329. Display::return_icon('upload_package.png', get_lang('Upload corrections'), '', ICON_SIZE_SMALL),
  1330. api_get_path(WEB_CODE_PATH).'work/upload_corrections.php?'.api_get_cidreq().'&id='.$workId
  1331. ).'&nbsp;';
  1332. if ($countUniqueAttempts > 0) {
  1333. $downloadLink = Display::url(
  1334. Display::return_icon(
  1335. 'save_pack.png',
  1336. get_lang('Save'),
  1337. [],
  1338. ICON_SIZE_SMALL
  1339. ),
  1340. api_get_path(WEB_CODE_PATH).'work/downloadfolder.inc.php?id='.$workId.'&'.api_get_cidreq()
  1341. );
  1342. } else {
  1343. $downloadLink = Display::url(
  1344. Display::return_icon(
  1345. 'save_pack_na.png',
  1346. get_lang('Save'),
  1347. [],
  1348. ICON_SIZE_SMALL
  1349. ),
  1350. '#'
  1351. );
  1352. }
  1353. // Remove Delete Work Button from action List
  1354. // Because removeXSS "removes" the onClick JS Event to do the action (See model.ajax.php - Line 1639)
  1355. // But still can use the another jqgrid button to remove works (trash icon)
  1356. //
  1357. // $deleteUrl = api_get_path(WEB_CODE_PATH).'work/work.php?id='.$workId.'&action=delete_dir&'.api_get_cidreq();
  1358. // $deleteLink = '<a href="#" onclick="showConfirmationPopup(this, \'' . $deleteUrl . '\' ) " >' .
  1359. // Display::return_icon(
  1360. // 'delete.png',
  1361. // get_lang('Delete'),
  1362. // [],
  1363. // ICON_SIZE_SMALL
  1364. // ) . '</a>';
  1365. if (!api_is_allowed_to_edit()) {
  1366. // $deleteLink = null;
  1367. $editLink = null;
  1368. }
  1369. $work['actions'] = $visibilityLink.$correctionLink.$downloadLink.$editLink;
  1370. $works[] = $work;
  1371. }
  1372. }
  1373. return $works;
  1374. }
  1375. /**
  1376. * @param int $start
  1377. * @param int $limit
  1378. * @param string $column
  1379. * @param string $direction
  1380. * @param int $workId
  1381. * @param int $studentId
  1382. * @param string $whereCondition
  1383. * @param bool $getCount
  1384. *
  1385. * @return array
  1386. */
  1387. function get_work_user_list_from_documents(
  1388. $start,
  1389. $limit,
  1390. $column,
  1391. $direction,
  1392. $workId,
  1393. $studentId = null,
  1394. $whereCondition = '',
  1395. $getCount = false
  1396. ) {
  1397. if ($getCount) {
  1398. $select1 = " SELECT count(u.user_id) as count ";
  1399. $select2 = " SELECT count(u.user_id) as count ";
  1400. } else {
  1401. $select1 = " SELECT DISTINCT
  1402. u.firstname,
  1403. u.lastname,
  1404. u.user_id,
  1405. w.title,
  1406. w.parent_id,
  1407. w.document_id document_id,
  1408. w.id, qualification,
  1409. qualificator_id,
  1410. w.sent_date,
  1411. w.contains_file,
  1412. w.url
  1413. ";
  1414. $select2 = " SELECT DISTINCT
  1415. u.firstname, u.lastname,
  1416. u.user_id,
  1417. d.title,
  1418. w.parent_id,
  1419. d.id document_id,
  1420. 0,
  1421. 0,
  1422. 0,
  1423. w.sent_date,
  1424. w.contains_file,
  1425. w.url
  1426. ";
  1427. }
  1428. $documentTable = Database::get_course_table(TABLE_DOCUMENT);
  1429. $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1430. $workRelDocument = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_DOCUMENT);
  1431. $userTable = Database::get_main_table(TABLE_MAIN_USER);
  1432. $courseId = api_get_course_int_id();
  1433. $sessionId = api_get_session_id();
  1434. if (empty($studentId)) {
  1435. $studentId = api_get_user_id();
  1436. }
  1437. $studentId = intval($studentId);
  1438. $workId = intval($workId);
  1439. $userCondition = " AND u.user_id = $studentId ";
  1440. $sessionCondition = api_get_session_condition($sessionId, true, false, 'w.session_id');
  1441. $workCondition = " AND w_rel.work_id = $workId";
  1442. $workParentCondition = " AND w.parent_id = $workId";
  1443. $sql = "(
  1444. $select1 FROM $userTable u
  1445. INNER JOIN $workTable w
  1446. ON (u.user_id = w.user_id AND w.active IN (0, 1) AND w.filetype = 'file')
  1447. WHERE
  1448. w.c_id = $courseId
  1449. $userCondition
  1450. $sessionCondition
  1451. $whereCondition
  1452. $workParentCondition
  1453. ) UNION (
  1454. $select2 FROM $workTable w
  1455. INNER JOIN $workRelDocument w_rel
  1456. ON (w_rel.work_id = w.id AND w.active IN (0, 1) AND w_rel.c_id = w.c_id)
  1457. INNER JOIN $documentTable d
  1458. ON (w_rel.document_id = d.id AND d.c_id = w.c_id)
  1459. INNER JOIN $userTable u ON (u.user_id = $studentId)
  1460. WHERE
  1461. w.c_id = $courseId
  1462. $workCondition
  1463. $sessionCondition AND
  1464. d.id NOT IN (
  1465. SELECT w.document_id id
  1466. FROM $workTable w
  1467. WHERE
  1468. user_id = $studentId AND
  1469. c_id = $courseId AND
  1470. filetype = 'file' AND
  1471. active IN (0, 1)
  1472. $sessionCondition
  1473. $workParentCondition
  1474. )
  1475. )";
  1476. $start = intval($start);
  1477. $limit = intval($limit);
  1478. $direction = in_array(strtolower($direction), ['desc', 'asc']) ? $direction : 'desc';
  1479. $column = Database::escape_string($column);
  1480. if ($getCount) {
  1481. $result = Database::query($sql);
  1482. $result = Database::fetch_array($result);
  1483. return $result['count'];
  1484. }
  1485. $sql .= " ORDER BY $column $direction";
  1486. $sql .= " LIMIT $start, $limit";
  1487. $result = Database::query($sql);
  1488. $currentUserId = api_get_user_id();
  1489. $work_data = get_work_data_by_id($workId);
  1490. $qualificationExists = false;
  1491. if (!empty($work_data['qualification']) && intval($work_data['qualification']) > 0) {
  1492. $qualificationExists = true;
  1493. }
  1494. $urlAdd = api_get_path(WEB_CODE_PATH).'work/upload_from_template.php?'.api_get_cidreq();
  1495. $urlEdit = api_get_path(WEB_CODE_PATH).'work/edit.php?'.api_get_cidreq();
  1496. $urlDelete = api_get_path(WEB_CODE_PATH).'work/work_list.php?action=delete&'.api_get_cidreq();
  1497. $urlView = api_get_path(WEB_CODE_PATH).'work/view.php?'.api_get_cidreq();
  1498. $urlDownload = api_get_path(WEB_CODE_PATH).'work/download.php?'.api_get_cidreq();
  1499. $editIcon = Display::return_icon('edit.png', get_lang('Edit'));
  1500. $addIcon = Display::return_icon('add.png', get_lang('Add'));
  1501. $deleteIcon = Display::return_icon('delete.png', get_lang('Delete'));
  1502. $viewIcon = Display::return_icon('default.png', get_lang('View'));
  1503. $saveIcon = Display::return_icon(
  1504. 'save.png',
  1505. get_lang('Save'),
  1506. [],
  1507. ICON_SIZE_SMALL
  1508. );
  1509. $allowEdition = api_get_course_setting('student_delete_own_publication') == 1;
  1510. $workList = [];
  1511. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1512. $userId = $row['user_id'];
  1513. $documentId = $row['document_id'];
  1514. $itemId = $row['id'];
  1515. $addLinkShowed = false;
  1516. if (empty($documentId)) {
  1517. $url = $urlEdit.'&item_id='.$row['id'].'&id='.$workId;
  1518. $editLink = Display::url($editIcon, $url);
  1519. if ($allowEdition != 1) {
  1520. $editLink = null;
  1521. }
  1522. } else {
  1523. $documentToWork = getDocumentToWorkPerUser($documentId, $workId, $courseId, $sessionId, $userId);
  1524. if (empty($documentToWork)) {
  1525. $url = $urlAdd.'&document_id='.$documentId.'&id='.$workId;
  1526. $editLink = Display::url($addIcon, $url);
  1527. $addLinkShowed = true;
  1528. } else {
  1529. $row['title'] = $documentToWork['title'];
  1530. $row['sent_date'] = $documentToWork['sent_date'];
  1531. $newWorkId = $documentToWork['id'];
  1532. $url = $urlEdit.'&item_id='.$newWorkId.'&id='.$workId;
  1533. $editLink = Display::url($editIcon, $url);
  1534. if ($allowEdition != 1) {
  1535. $editLink = '';
  1536. }
  1537. }
  1538. }
  1539. $downloadLink = '';
  1540. // If URL is present then there's a file to download keep BC.
  1541. if ($row['contains_file'] || !empty($row['url'])) {
  1542. $downloadLink = Display::url($saveIcon, $urlDownload.'&id='.$row['id']).'&nbsp;';
  1543. }
  1544. $viewLink = '';
  1545. if (!empty($itemId)) {
  1546. $viewLink = Display::url($viewIcon, $urlView.'&id='.$itemId);
  1547. }
  1548. $deleteLink = '';
  1549. if ($allowEdition == 1 && !empty($itemId)) {
  1550. $deleteLink = Display::url($deleteIcon, $urlDelete.'&item_id='.$itemId.'&id='.$workId);
  1551. }
  1552. $row['type'] = null;
  1553. if ($qualificationExists) {
  1554. if (empty($row['qualificator_id'])) {
  1555. $status = Display::label(get_lang('Not reviewed'), 'warning');
  1556. } else {
  1557. $status = Display::label(get_lang('Revised'), 'success');
  1558. }
  1559. $row['qualificator_id'] = $status;
  1560. }
  1561. if (!empty($row['qualification'])) {
  1562. $row['qualification'] = Display::label($row['qualification'], 'info');
  1563. }
  1564. if (!empty($row['sent_date'])) {
  1565. $row['sent_date'] = Display::dateToStringAgoAndLongDate($row['sent_date']);
  1566. }
  1567. if ($userId == $currentUserId) {
  1568. $row['actions'] = $downloadLink.$viewLink.$editLink.$deleteLink;
  1569. }
  1570. if ($addLinkShowed) {
  1571. $row['qualification'] = '';
  1572. $row['qualificator_id'] = '';
  1573. }
  1574. $workList[] = $row;
  1575. }
  1576. return $workList;
  1577. }
  1578. /**
  1579. * @param int $start
  1580. * @param int $limit
  1581. * @param int $column
  1582. * @param string $direction
  1583. * @param int $work_id
  1584. * @param string $whereCondition
  1585. * @param int $studentId
  1586. * @param bool $getCount
  1587. * @param int $courseId
  1588. * @param int $sessionId
  1589. *
  1590. * @return array
  1591. */
  1592. function get_work_user_list(
  1593. $start,
  1594. $limit,
  1595. $column,
  1596. $direction,
  1597. $work_id,
  1598. $whereCondition = '',
  1599. $studentId = null,
  1600. $getCount = false,
  1601. $courseId = 0,
  1602. $sessionId = 0
  1603. ) {
  1604. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1605. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  1606. $session_id = $sessionId ? $sessionId : api_get_session_id();
  1607. $group_id = api_get_group_id();
  1608. $course_info = api_get_course_info();
  1609. $course_info = empty($course_info) ? api_get_course_info_by_id($courseId) : $course_info;
  1610. $course_id = isset($course_info['real_id']) ? $course_info['real_id'] : $courseId;
  1611. $work_id = (int) $work_id;
  1612. $start = (int) $start;
  1613. $limit = (int) $limit;
  1614. $column = !empty($column) ? Database::escape_string($column) : 'sent_date';
  1615. $compilatio_web_folder = api_get_path(WEB_CODE_PATH).'plagiarism/compilatio/';
  1616. $compilation = null;
  1617. if (api_get_configuration_value('allow_compilatio_tool')) {
  1618. $compilation = new Compilatio();
  1619. }
  1620. if (!in_array($direction, ['asc', 'desc'])) {
  1621. $direction = 'desc';
  1622. }
  1623. $work_data = get_work_data_by_id($work_id, $courseId, $sessionId);
  1624. $is_allowed_to_edit = api_is_allowed_to_edit() || api_is_coach();
  1625. $condition_session = api_get_session_condition(
  1626. $session_id,
  1627. true,
  1628. false,
  1629. 'work.session_id'
  1630. );
  1631. $locked = api_resource_is_locked_by_gradebook(
  1632. $work_id,
  1633. LINK_STUDENTPUBLICATION,
  1634. $course_info['code']
  1635. );
  1636. $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(
  1637. api_get_user_id(),
  1638. $course_info
  1639. );
  1640. $groupIid = 0;
  1641. if ($group_id) {
  1642. $groupInfo = GroupManager::get_group_properties($group_id);
  1643. if ($groupInfo) {
  1644. $groupIid = $groupInfo['iid'];
  1645. }
  1646. }
  1647. if (!empty($work_data)) {
  1648. if (!empty($group_id)) {
  1649. // set to select only messages posted by the user's group
  1650. $extra_conditions = " work.post_group_id = '".$groupIid."' ";
  1651. } else {
  1652. $extra_conditions = " (work.post_group_id = '0' OR work.post_group_id is NULL) ";
  1653. }
  1654. if ($is_allowed_to_edit || $isDrhOfCourse) {
  1655. $extra_conditions .= ' AND work.active IN (0, 1) ';
  1656. } else {
  1657. if (isset($course_info['show_score']) &&
  1658. $course_info['show_score'] == 1
  1659. ) {
  1660. $extra_conditions .= " AND (u.user_id = ".api_get_user_id()." AND work.active IN (0, 1)) ";
  1661. } else {
  1662. $extra_conditions .= ' AND work.active IN (0, 1) ';
  1663. }
  1664. }
  1665. $extra_conditions .= " AND parent_id = $work_id ";
  1666. $select = 'SELECT DISTINCT
  1667. u.user_id,
  1668. work.id as id,
  1669. title as title,
  1670. description,
  1671. url,
  1672. sent_date,
  1673. contains_file,
  1674. has_properties,
  1675. view_properties,
  1676. qualification,
  1677. weight,
  1678. allow_text_assignment,
  1679. u.firstname,
  1680. u.lastname,
  1681. u.username,
  1682. parent_id,
  1683. accepted,
  1684. qualificator_id,
  1685. url_correction,
  1686. title_correction
  1687. ';
  1688. if ($getCount) {
  1689. $select = 'SELECT DISTINCT count(u.user_id) as count ';
  1690. }
  1691. $work_assignment = get_work_assignment_by_id($work_id, $courseId);
  1692. if (!empty($studentId)) {
  1693. $studentId = (int) $studentId;
  1694. $whereCondition .= " AND u.user_id = $studentId ";
  1695. }
  1696. $sql = " $select
  1697. FROM $work_table work
  1698. INNER JOIN $user_table u
  1699. ON (work.user_id = u.user_id)
  1700. WHERE
  1701. work.c_id = $course_id AND
  1702. $extra_conditions
  1703. $whereCondition
  1704. $condition_session
  1705. AND u.status != ".INVITEE."
  1706. ORDER BY $column $direction";
  1707. if (!empty($start) && !empty($limit)) {
  1708. $sql .= " LIMIT $start, $limit";
  1709. }
  1710. $result = Database::query($sql);
  1711. $works = [];
  1712. if ($getCount) {
  1713. $work = Database::fetch_array($result, 'ASSOC');
  1714. return $work['count'];
  1715. }
  1716. $url = api_get_path(WEB_CODE_PATH).'work/';
  1717. $unoconv = api_get_configuration_value('unoconv.binaries');
  1718. $loadingText = addslashes(get_lang('Loading'));
  1719. $uploadedText = addslashes(get_lang('Uploaded.'));
  1720. $failsUploadText = addslashes(get_lang('No file was uploaded..'));
  1721. $failsUploadIcon = Display::return_icon(
  1722. 'closed-circle.png',
  1723. '',
  1724. [],
  1725. ICON_SIZE_TINY
  1726. );
  1727. $saveIcon = Display::return_icon(
  1728. 'save.png',
  1729. get_lang('Save'),
  1730. [],
  1731. ICON_SIZE_SMALL
  1732. );
  1733. $correctionIcon = Display::return_icon(
  1734. 'check-circle.png',
  1735. get_lang('Correction'),
  1736. null,
  1737. ICON_SIZE_SMALL
  1738. );
  1739. $correctionIconSmall = Display::return_icon(
  1740. 'check-circle.png',
  1741. get_lang('Correction'),
  1742. null,
  1743. ICON_SIZE_TINY
  1744. );
  1745. $rateIcon = Display::return_icon(
  1746. 'rate_work.png',
  1747. get_lang('Correct and rate'),
  1748. [],
  1749. ICON_SIZE_SMALL
  1750. );
  1751. $blockEdition = api_get_configuration_value('block_student_publication_edition');
  1752. $blockScoreEdition = api_get_configuration_value('block_student_publication_score_edition');
  1753. $loading = Display::returnFontAwesomeIcon('spinner', null, true, 'fa-spin');
  1754. while ($work = Database::fetch_array($result, 'ASSOC')) {
  1755. $item_id = $work['id'];
  1756. $dbTitle = $work['title'];
  1757. // Get the author ID for that document from the item_property table
  1758. $is_author = false;
  1759. $can_read = false;
  1760. $owner_id = $work['user_id'];
  1761. /* Because a bug found when saving items using the api_item_property_update()
  1762. the field $item_property_data['insert_user_id'] is not reliable. */
  1763. if (!$is_allowed_to_edit && $owner_id == api_get_user_id()) {
  1764. $is_author = true;
  1765. }
  1766. if ($course_info['show_score'] == 0) {
  1767. $can_read = true;
  1768. }
  1769. $qualification_exists = false;
  1770. if (!empty($work_data['qualification']) &&
  1771. intval($work_data['qualification']) > 0
  1772. ) {
  1773. $qualification_exists = true;
  1774. }
  1775. $qualification_string = '';
  1776. if ($qualification_exists) {
  1777. if ($work['qualification'] == '') {
  1778. $qualification_string = Display::label('-');
  1779. } else {
  1780. $qualification_string = formatWorkScore($work['qualification'], $work_data['qualification']);
  1781. }
  1782. }
  1783. $work['qualification_score'] = $work['qualification'];
  1784. $add_string = '';
  1785. $time_expires = '';
  1786. if (!empty($work_assignment['expires_on'])) {
  1787. $time_expires = api_strtotime(
  1788. $work_assignment['expires_on'],
  1789. 'UTC'
  1790. );
  1791. }
  1792. if (!empty($work_assignment['expires_on']) &&
  1793. !empty($time_expires) && ($time_expires < api_strtotime($work['sent_date'], 'UTC'))) {
  1794. $add_string = Display::label(get_lang('Expired'), 'important').' - ';
  1795. }
  1796. if (($can_read && $work['accepted'] == '1') ||
  1797. ($is_author && in_array($work['accepted'], ['1', '0'])) ||
  1798. ($is_allowed_to_edit || api_is_drh())
  1799. ) {
  1800. // Firstname, lastname, username
  1801. $work['fullname'] = Display::div(
  1802. api_get_person_name($work['firstname'], $work['lastname']),
  1803. ['class' => 'work-name']
  1804. );
  1805. // Title
  1806. $work['title_clean'] = $work['title'];
  1807. $work['title'] = Security::remove_XSS($work['title']);
  1808. if (strlen($work['title']) > 30) {
  1809. $short_title = substr($work['title'], 0, 27).'...';
  1810. $work['title'] = Display::span($short_title, ['class' => 'work-title', 'title' => $work['title']]);
  1811. } else {
  1812. $work['title'] = Display::div($work['title'], ['class' => 'work-title']);
  1813. }
  1814. // Type.
  1815. $work['type'] = DocumentManager::build_document_icon_tag('file', $work['url']);
  1816. // File name.
  1817. $linkToDownload = '';
  1818. // If URL is present then there's a file to download keep BC.
  1819. if ($work['contains_file'] || !empty($work['url'])) {
  1820. $linkToDownload = '<a href="'.$url.'download.php?id='.$item_id.'&'.api_get_cidreq().'">'.$saveIcon.'</a> ';
  1821. }
  1822. $feedback = '';
  1823. $count = getWorkCommentCount($item_id, $course_info);
  1824. if (!is_null($count) && !empty($count)) {
  1825. if ($qualification_exists) {
  1826. $feedback .= ' ';
  1827. }
  1828. $feedback .= Display::url(
  1829. $count.' '.Display::returnFontAwesomeIcon('comments-o'),
  1830. $url.'view.php?'.api_get_cidreq().'&id='.$item_id
  1831. );
  1832. }
  1833. $correction = '';
  1834. $hasCorrection = '';
  1835. if (!empty($work['url_correction'])) {
  1836. $hasCorrection = Display::url(
  1837. $correctionIcon,
  1838. api_get_path(WEB_CODE_PATH).'work/download.php?id='.$item_id.'&'.api_get_cidreq().'&correction=1'
  1839. );
  1840. }
  1841. if ($qualification_exists) {
  1842. $work['qualification'] = $qualification_string.$feedback;
  1843. } else {
  1844. $work['qualification'] = $qualification_string.$feedback.$hasCorrection;
  1845. }
  1846. $work['qualification_only'] = $qualification_string;
  1847. // Date.
  1848. $work_date = api_get_local_time($work['sent_date']);
  1849. $date = date_to_str_ago($work['sent_date']).' '.$work_date;
  1850. $work['formatted_date'] = $work_date.' '.$add_string;
  1851. $work['sent_date_from_db'] = $work['sent_date'];
  1852. $work['sent_date'] = '<div class="work-date" title="'.$date.'">'.
  1853. $add_string.' '.Display::dateToStringAgoAndLongDate($work['sent_date']).'</div>';
  1854. $work['status'] = $hasCorrection;
  1855. $work['has_correction'] = $hasCorrection;
  1856. // Actions.
  1857. $action = '';
  1858. if (api_is_allowed_to_edit()) {
  1859. if ($blockScoreEdition && !api_is_platform_admin() && !empty($work['qualification_score'])) {
  1860. $rateLink = '';
  1861. } else {
  1862. $rateLink = '<a href="'.$url.'view.php?'.api_get_cidreq().'&id='.$item_id.'" title="'.get_lang('View').'">'.
  1863. $rateIcon.'</a> ';
  1864. }
  1865. $action .= $rateLink;
  1866. if ($unoconv && empty($work['contains_file'])) {
  1867. $action .= '<a f
  1868. href="'.$url.'work_list_all.php?'.api_get_cidreq().'&id='.$work_id.'&action=export_to_doc&item_id='.$item_id.'"
  1869. title="'.get_lang('Export to .doc').'" >'.
  1870. Display::return_icon('export_doc.png', get_lang('Export to .doc'), [], ICON_SIZE_SMALL).'</a> ';
  1871. }
  1872. $alreadyUploaded = '';
  1873. if (!empty($work['url_correction'])) {
  1874. $alreadyUploaded = '<br />'.$work['title_correction'].' '.$correctionIconSmall;
  1875. }
  1876. $correction = '
  1877. <form
  1878. id="file_upload_'.$item_id.'"
  1879. class="work_correction_file_upload file_upload_small fileinput-button"
  1880. action="'.api_get_path(WEB_AJAX_PATH).'work.ajax.php?'.api_get_cidreq().'&a=upload_correction_file&item_id='.$item_id.'" method="POST" enctype="multipart/form-data"
  1881. >
  1882. <div id="progress_'.$item_id.'" class="text-center button-load">
  1883. '.addslashes(get_lang('Click or drop one file here')).'
  1884. '.Display::return_icon('upload_file.png', get_lang('Correction'), [], ICON_SIZE_TINY).'
  1885. '.$alreadyUploaded.'
  1886. </div>
  1887. <input id="file_'.$item_id.'" type="file" name="file" class="" multiple>
  1888. </form>
  1889. ';
  1890. $correction .= "<script>
  1891. $(function() {
  1892. $('.work_correction_file_upload').each(function () {
  1893. $(this).fileupload({
  1894. dropZone: $(this)
  1895. });
  1896. });
  1897. $('#file_upload_".$item_id."').fileupload({
  1898. add: function (e, data) {
  1899. $('#progress_$item_id').html();
  1900. //$('#file_$item_id').remove();
  1901. data.context = $('#progress_$item_id').html('$loadingText <br /> <em class=\"fa fa-spinner fa-pulse fa-fw\"></em>');
  1902. data.submit();
  1903. $(this).removeClass('hover');
  1904. },
  1905. dragover: function (e, data) {
  1906. $(this).addClass('hover');
  1907. },
  1908. done: function (e, data) {
  1909. if (data._response.result.name) {
  1910. $('#progress_$item_id').html('$uploadedText '+data._response.result.result+'<br />'+data._response.result.name);
  1911. } else {
  1912. $('#progress_$item_id').html('$failsUploadText $failsUploadIcon');
  1913. }
  1914. $(this).removeClass('hover');
  1915. }
  1916. });
  1917. $('#file_upload_".$item_id."').on('dragleave', function (e) {
  1918. // dragleave callback implementation
  1919. $(this).removeClass('hover');
  1920. });
  1921. });
  1922. </script>";
  1923. if ($locked) {
  1924. if ($qualification_exists) {
  1925. $action .= Display::return_icon(
  1926. 'edit_na.png',
  1927. get_lang('Correct and rate'),
  1928. [],
  1929. ICON_SIZE_SMALL
  1930. );
  1931. } else {
  1932. $action .= Display::return_icon('edit_na.png', get_lang('Comment'), [], ICON_SIZE_SMALL);
  1933. }
  1934. } else {
  1935. if ($blockEdition && !api_is_platform_admin()) {
  1936. $editLink = '';
  1937. } else {
  1938. if ($qualification_exists) {
  1939. $editLink = '<a href="'.$url.'edit.php?'.api_get_cidreq(
  1940. ).'&item_id='.$item_id.'&id='.$work['parent_id'].'" title="'.get_lang(
  1941. 'Edit'
  1942. ).'" >'.
  1943. Display::return_icon('edit.png', get_lang('Edit'), [], ICON_SIZE_SMALL).'</a>';
  1944. } else {
  1945. $editLink = '<a href="'.$url.'edit.php?'.api_get_cidreq(
  1946. ).'&item_id='.$item_id.'&id='.$work['parent_id'].'" title="'.get_lang(
  1947. 'Edit'
  1948. ).'">'.
  1949. Display::return_icon('edit.png', get_lang('Edit'), [], ICON_SIZE_SMALL).'</a>';
  1950. }
  1951. }
  1952. $action .= $editLink;
  1953. }
  1954. if ($work['contains_file']) {
  1955. if ($locked) {
  1956. $action .= Display::return_icon(
  1957. 'move_na.png',
  1958. get_lang('Move'),
  1959. [],
  1960. ICON_SIZE_SMALL
  1961. );
  1962. } else {
  1963. $action .= '<a href="'.$url.'work.php?'.api_get_cidreq().'&action=move&item_id='.$item_id.'&id='.$work['parent_id'].'" title="'.get_lang('Move').'">'.
  1964. Display::return_icon('move.png', get_lang('Move'), [], ICON_SIZE_SMALL).'</a>';
  1965. }
  1966. }
  1967. if ($work['accepted'] == '1') {
  1968. $action .= '<a href="'.$url.'work_list_all.php?'.api_get_cidreq().'&id='.$work_id.'&action=make_invisible&item_id='.$item_id.'" title="'.get_lang('invisible').'" >'.
  1969. Display::return_icon('visible.png', get_lang('invisible'), [], ICON_SIZE_SMALL).'</a>';
  1970. } else {
  1971. $action .= '<a href="'.$url.'work_list_all.php?'.api_get_cidreq().'&id='.$work_id.'&action=make_visible&item_id='.$item_id.'" title="'.get_lang('Visible').'" >'.
  1972. Display::return_icon('invisible.png', get_lang('Visible'), [], ICON_SIZE_SMALL).'</a> ';
  1973. }
  1974. if ($locked) {
  1975. $action .= Display::return_icon('delete_na.png', get_lang('Delete'), '', ICON_SIZE_SMALL);
  1976. } else {
  1977. $action .= '<a href="'.$url.'work_list_all.php?'.api_get_cidreq().'&id='.$work_id.'&action=delete&item_id='.$item_id.'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('Please confirm your choice'), ENT_QUOTES))."'".')) return false;" title="'.get_lang('Delete').'" >'.
  1978. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
  1979. }
  1980. } elseif ($is_author && (empty($work['qualificator_id']) || $work['qualificator_id'] == 0)) {
  1981. $action .= '<a href="'.$url.'view.php?'.api_get_cidreq().'&id='.$item_id.'" title="'.get_lang('View').'">'.
  1982. Display::return_icon('default.png', get_lang('View'), [], ICON_SIZE_SMALL).'</a>';
  1983. if (api_get_course_setting('student_delete_own_publication') == 1) {
  1984. if (api_is_allowed_to_session_edit(false, true)) {
  1985. $action .= '<a href="'.$url.'edit.php?'.api_get_cidreq().'&item_id='.$item_id.'&id='.$work['parent_id'].'" title="'.get_lang('Edit').'">'.
  1986. Display::return_icon('edit.png', get_lang('Comment'), [], ICON_SIZE_SMALL).'</a>';
  1987. }
  1988. $action .= ' <a href="'.$url.'work_list.php?'.api_get_cidreq().'&action=delete&item_id='.$item_id.'&id='.$work['parent_id'].'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('Please confirm your choice'), ENT_QUOTES))."'".')) return false;" title="'.get_lang('Delete').'" >'.
  1989. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
  1990. }
  1991. } else {
  1992. $action .= '<a href="'.$url.'view.php?'.api_get_cidreq().'&id='.$item_id.'" title="'.get_lang('View').'">'.
  1993. Display::return_icon('default.png', get_lang('View'), [], ICON_SIZE_SMALL).'</a>';
  1994. }
  1995. // Status.
  1996. if (empty($work['qualificator_id'])) {
  1997. $qualificator_id = Display::label(get_lang('Not reviewed'), 'warning');
  1998. } else {
  1999. $qualificator_id = Display::label(get_lang('Revised'), 'success');
  2000. }
  2001. $work['qualificator_id'] = $qualificator_id.' '.$hasCorrection;
  2002. $work['actions'] = '<div class="work-action">'.$linkToDownload.$action.'</div>';
  2003. $work['correction'] = $correction;
  2004. if (!empty($compilation)) {
  2005. $compilationId = $compilation->getCompilatioId($item_id, $course_id);
  2006. if ($compilationId) {
  2007. $actionCompilatio = "<div id='id_avancement".$item_id."' class='compilation_block'>
  2008. ".$loading.'&nbsp;'.get_lang('Connecting with the Compilatio server').'</div>';
  2009. } else {
  2010. $workDirectory = api_get_path(SYS_COURSE_PATH).$course_info['directory'];
  2011. if (!Compilatio::verifiFileType($dbTitle)) {
  2012. $actionCompilatio = get_lang('File format not supported');
  2013. } elseif (filesize($workDirectory.'/'.$work['url']) > $compilation->getMaxFileSize()) {
  2014. $sizeFile = round(filesize($workDirectory.'/'.$work['url']) / 1000000);
  2015. $actionCompilatio = get_lang('The file is too big to upload.').': '.format_file_size($sizeFile).'<br />';
  2016. } else {
  2017. $actionCompilatio = "<div id='id_avancement".$item_id."' class='compilation_block'>";
  2018. $actionCompilatio .= Display::url(
  2019. get_lang('Analyse'),
  2020. 'javascript:void(0)',
  2021. [
  2022. 'class' => 'getSingleCompilatio btn btn-primary btn-xs',
  2023. 'onclick' => "getSingleCompilatio($item_id);",
  2024. ]
  2025. );
  2026. $actionCompilatio .= get_lang('with Compilatio');
  2027. }
  2028. }
  2029. $work['compilatio'] = $actionCompilatio;
  2030. }
  2031. $works[] = $work;
  2032. }
  2033. }
  2034. return $works;
  2035. }
  2036. }
  2037. /**
  2038. * Send reminder to users who have not given the task.
  2039. *
  2040. * @param int
  2041. *
  2042. * @return array
  2043. *
  2044. * @author cvargas carlos.vargas@beeznest.com cfasanando, christian.fasanado@beeznest.com
  2045. */
  2046. function send_reminder_users_without_publication($task_data)
  2047. {
  2048. $_course = api_get_course_info();
  2049. $task_id = $task_data['id'];
  2050. $task_title = !empty($task_data['title']) ? $task_data['title'] : basename($task_data['url']);
  2051. $subject = '['.api_get_setting('siteName').'] ';
  2052. // The body can be as long as you wish, and any combination of text and variables
  2053. $content = get_lang('Please remember you still have to send an assignment')."\n".get_lang('Course name').' : '.$_course['name']."\n";
  2054. $content .= get_lang('Assignment name').' : '.$task_title."\n";
  2055. $list_users = get_list_users_without_publication($task_id);
  2056. $mails_sent_to = [];
  2057. foreach ($list_users as $user) {
  2058. $name_user = api_get_person_name($user[1], $user[0], null, PERSON_NAME_EMAIL_ADDRESS);
  2059. $dear_line = get_lang('Dear')." ".api_get_person_name($user[1], $user[0]).", \n\n";
  2060. $body = $dear_line.$content;
  2061. MessageManager::send_message($user[3], $subject, $body);
  2062. $mails_sent_to[] = $name_user;
  2063. }
  2064. return $mails_sent_to;
  2065. }
  2066. /**
  2067. * @param int $workId The work ID
  2068. * @param int $courseId The course ID
  2069. * @param int $sessionId Optional. The session ID
  2070. */
  2071. function sendEmailToDrhOnHomeworkCreation($workId, $courseId, $sessionId = 0)
  2072. {
  2073. $courseInfo = api_get_course_info_by_id($courseId);
  2074. $assignment = get_work_assignment_by_id($workId, $courseId);
  2075. $work = get_work_data_by_id($workId, $courseId, $sessionId);
  2076. $workInfo = array_merge($assignment, $work);
  2077. if (empty($sessionId)) {
  2078. $students = CourseManager::get_student_list_from_course_code($courseInfo['code']);
  2079. } else {
  2080. $students = CourseManager::get_student_list_from_course_code($courseInfo['code'], true, $sessionId);
  2081. }
  2082. $bodyView = new Template(null, false, false, false, false, false);
  2083. foreach ($students as $student) {
  2084. $studentInfo = api_get_user_info($student['user_id']);
  2085. if (empty($studentInfo)) {
  2086. continue;
  2087. }
  2088. $hrms = UserManager::getDrhListFromUser($student['id']);
  2089. foreach ($hrms as $hrm) {
  2090. $hrmName = api_get_person_name($hrm['firstname'], $hrm['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  2091. $bodyView->assign('hrm_name', $hrmName);
  2092. $bodyView->assign('student', $studentInfo);
  2093. $bodyView->assign('course', $courseInfo);
  2094. $bodyView->assign('course_link', api_get_course_url($courseInfo['code'], $sessionId));
  2095. $bodyView->assign('work', $workInfo);
  2096. $bodyTemplate = $bodyView->get_template('mail/new_work_alert_hrm.tpl');
  2097. MessageManager::send_message(
  2098. $hrm['id'],
  2099. sprintf(
  2100. get_lang('%s got a new assignment in course %s'),
  2101. $student['firstname'],
  2102. $courseInfo['title']
  2103. ),
  2104. $bodyView->fetch($bodyTemplate)
  2105. );
  2106. }
  2107. }
  2108. }
  2109. /**
  2110. * Sends an email to the students of a course when a homework is created.
  2111. *
  2112. * @param int $workId
  2113. * @param int $courseId
  2114. * @param int $sessionId
  2115. *
  2116. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  2117. * @author Julio Montoya <gugli100@gmail.com> Adding session support - 2011
  2118. */
  2119. function sendEmailToStudentsOnHomeworkCreation($workId, $courseId, $sessionId = 0)
  2120. {
  2121. $courseInfo = api_get_course_info_by_id($courseId);
  2122. $courseCode = $courseInfo['code'];
  2123. // Get the students of the course
  2124. if (empty($sessionId)) {
  2125. $students = CourseManager::get_student_list_from_course_code($courseCode);
  2126. } else {
  2127. $students = CourseManager::get_student_list_from_course_code($courseCode, true, $sessionId);
  2128. }
  2129. $emailsubject = '['.api_get_setting('siteName').'] '.get_lang('An assignment was created');
  2130. $currentUser = api_get_user_info(api_get_user_id());
  2131. if (!empty($students)) {
  2132. foreach ($students as $student) {
  2133. $user_info = api_get_user_info($student['user_id']);
  2134. if (!empty($user_info)) {
  2135. $link = api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq().'&id='.$workId;
  2136. $emailbody = get_lang('Dear')." ".$user_info['complete_name'].",\n\n";
  2137. $emailbody .= get_lang('An assignment has been added to course')." ".$courseCode.". "."\n\n".
  2138. '<a href="'.$link.'">'.get_lang('Please check the assignments page.').'</a>';
  2139. $emailbody .= "\n\n".$currentUser['complete_name'];
  2140. $additionalParameters = [
  2141. 'smsType' => SmsPlugin::ASSIGNMENT_BEEN_CREATED_COURSE,
  2142. 'userId' => $student['user_id'],
  2143. 'courseTitle' => $courseCode,
  2144. 'link' => $link,
  2145. ];
  2146. MessageManager::send_message_simple(
  2147. $student['user_id'],
  2148. $emailsubject,
  2149. $emailbody,
  2150. null,
  2151. false,
  2152. false,
  2153. $additionalParameters,
  2154. false
  2155. );
  2156. }
  2157. }
  2158. }
  2159. }
  2160. /**
  2161. * @param string $url
  2162. *
  2163. * @return bool
  2164. */
  2165. function is_work_exist_by_url($url)
  2166. {
  2167. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  2168. $url = Database::escape_string($url);
  2169. $sql = "SELECT id FROM $table WHERE url='$url'";
  2170. $result = Database::query($sql);
  2171. if (Database::num_rows($result) > 0) {
  2172. $row = Database::fetch_row($result);
  2173. if (empty($row)) {
  2174. return false;
  2175. } else {
  2176. return true;
  2177. }
  2178. } else {
  2179. return false;
  2180. }
  2181. }
  2182. /**
  2183. * Check if a user is the author of a work document.
  2184. *
  2185. * @param int $itemId
  2186. * @param int $userId
  2187. * @param int $courseId
  2188. * @param int $sessionId
  2189. *
  2190. * @return bool
  2191. */
  2192. function user_is_author($itemId, $userId = null, $courseId = 0, $sessionId = 0)
  2193. {
  2194. if (empty($itemId)) {
  2195. return false;
  2196. }
  2197. if (empty($userId)) {
  2198. $userId = api_get_user_id();
  2199. }
  2200. $isAuthor = false;
  2201. $is_allowed_to_edit = api_is_allowed_to_edit();
  2202. if ($is_allowed_to_edit) {
  2203. $isAuthor = true;
  2204. } else {
  2205. if (empty($courseId)) {
  2206. $courseId = api_get_course_int_id();
  2207. }
  2208. if (empty($sessionId)) {
  2209. $sessionId = api_get_session_id();
  2210. }
  2211. $data = api_get_item_property_info($courseId, 'work', $itemId, $sessionId);
  2212. if ($data['insert_user_id'] == $userId) {
  2213. $isAuthor = true;
  2214. }
  2215. $workData = get_work_data_by_id($itemId);
  2216. if ($workData['user_id'] == $userId) {
  2217. $isAuthor = true;
  2218. }
  2219. }
  2220. if (!$isAuthor) {
  2221. return false;
  2222. }
  2223. return $isAuthor;
  2224. }
  2225. /**
  2226. * Get list of users who have not given the task.
  2227. *
  2228. * @param int
  2229. * @param int
  2230. *
  2231. * @return array
  2232. *
  2233. * @author cvargas
  2234. * @author Julio Montoya <gugli100@gmail.com> Fixing query
  2235. */
  2236. function get_list_users_without_publication($task_id, $studentId = 0)
  2237. {
  2238. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  2239. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2240. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  2241. $session_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2242. $users = getAllUserToWork($task_id, api_get_course_int_id());
  2243. $users = array_column($users, 'user_id');
  2244. // Condition for the session
  2245. $session_id = api_get_session_id();
  2246. $course_id = api_get_course_int_id();
  2247. $task_id = intval($task_id);
  2248. $sessionCondition = api_get_session_condition($session_id);
  2249. if ($session_id == 0) {
  2250. $sql = "SELECT user_id as id FROM $work_table
  2251. WHERE
  2252. c_id = $course_id AND
  2253. parent_id = '$task_id' AND
  2254. active IN (0, 1)";
  2255. } else {
  2256. $sql = "SELECT user_id as id FROM $work_table
  2257. WHERE
  2258. c_id = $course_id AND
  2259. parent_id = '$task_id' $sessionCondition AND
  2260. active IN (0, 1)";
  2261. }
  2262. $result = Database::query($sql);
  2263. $users_with_tasks = [];
  2264. while ($row = Database::fetch_array($result)) {
  2265. $users_with_tasks[] = $row['id'];
  2266. }
  2267. if ($session_id == 0) {
  2268. $sql_users = "SELECT cu.user_id, u.lastname, u.firstname, u.email
  2269. FROM $table_course_user AS cu, $table_user AS u
  2270. WHERE u.status != 1 and cu.c_id='".$course_id."' AND u.user_id = cu.user_id";
  2271. } else {
  2272. $sql_users = "SELECT cu.user_id, u.lastname, u.firstname, u.email
  2273. FROM $session_course_rel_user AS cu, $table_user AS u
  2274. WHERE
  2275. u.status != 1 AND
  2276. cu.c_id='".$course_id."' AND
  2277. u.user_id = cu.user_id AND
  2278. cu.session_id = '".$session_id."'";
  2279. }
  2280. if (!empty($studentId)) {
  2281. $sql_users .= " AND u.user_id = ".intval($studentId);
  2282. }
  2283. $group_id = api_get_group_id();
  2284. $new_group_user_list = [];
  2285. if ($group_id) {
  2286. $groupInfo = GroupManager::get_group_properties($group_id);
  2287. $group_user_list = GroupManager::get_subscribed_users($groupInfo);
  2288. if (!empty($group_user_list)) {
  2289. foreach ($group_user_list as $group_user) {
  2290. $new_group_user_list[] = $group_user['user_id'];
  2291. }
  2292. }
  2293. }
  2294. $result_users = Database::query($sql_users);
  2295. $users_without_tasks = [];
  2296. while ($rowUsers = Database::fetch_array($result_users)) {
  2297. $userId = $rowUsers['user_id'];
  2298. if (in_array($userId, $users_with_tasks)) {
  2299. continue;
  2300. }
  2301. if ($group_id && !in_array($userId, $new_group_user_list)) {
  2302. continue;
  2303. }
  2304. if (!empty($users)) {
  2305. if (!in_array($userId, $users)) {
  2306. continue;
  2307. }
  2308. }
  2309. $row_users = [];
  2310. $row_users[0] = $rowUsers['lastname'];
  2311. $row_users[1] = $rowUsers['firstname'];
  2312. $row_users[2] = Display::encrypted_mailto_link($rowUsers['email']);
  2313. $row_users[3] = $userId;
  2314. $users_without_tasks[] = $row_users;
  2315. }
  2316. return $users_without_tasks;
  2317. }
  2318. /**
  2319. * Display list of users who have not given the task.
  2320. *
  2321. * @param int task id
  2322. * @param int $studentId
  2323. *
  2324. * @author cvargas carlos.vargas@beeznest.com cfasanando, christian.fasanado@beeznest.com
  2325. * @author Julio Montoya <gugli100@gmail.com> Fixes
  2326. */
  2327. function display_list_users_without_publication($task_id, $studentId = null)
  2328. {
  2329. $origin = api_get_origin();
  2330. $table_header[] = [get_lang('Last name'), true];
  2331. $table_header[] = [get_lang('First name'), true];
  2332. $table_header[] = [get_lang('e-mail'), true];
  2333. $data = get_list_users_without_publication($task_id);
  2334. $sorting_options = [];
  2335. $sorting_options['column'] = 1;
  2336. $paging_options = [];
  2337. $my_params = [];
  2338. if (isset($_GET['edit_dir'])) {
  2339. $my_params['edit_dir'] = Security::remove_XSS($_GET['edit_dir']);
  2340. }
  2341. if (isset($_GET['list'])) {
  2342. $my_params['list'] = Security::remove_XSS($_GET['list']);
  2343. }
  2344. $my_params['origin'] = $origin;
  2345. $my_params['id'] = intval($_GET['id']);
  2346. //$column_show
  2347. $column_show[] = 1;
  2348. $column_show[] = 1;
  2349. $column_show[] = 1;
  2350. Display::display_sortable_config_table(
  2351. 'work',
  2352. $table_header,
  2353. $data,
  2354. $sorting_options,
  2355. $paging_options,
  2356. $my_params,
  2357. $column_show
  2358. );
  2359. }
  2360. /**
  2361. * @param int $documentId
  2362. * @param int $workId
  2363. * @param int $courseId
  2364. */
  2365. function addDocumentToWork($documentId, $workId, $courseId)
  2366. {
  2367. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_DOCUMENT);
  2368. $params = [
  2369. 'document_id' => $documentId,
  2370. 'work_id' => $workId,
  2371. 'c_id' => $courseId,
  2372. ];
  2373. Database::insert($table, $params);
  2374. }
  2375. /**
  2376. * @param int $documentId
  2377. * @param int $workId
  2378. * @param int $courseId
  2379. *
  2380. * @return array
  2381. */
  2382. function getDocumentToWork($documentId, $workId, $courseId)
  2383. {
  2384. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_DOCUMENT);
  2385. $params = [
  2386. 'document_id = ? and work_id = ? and c_id = ?' => [$documentId, $workId, $courseId],
  2387. ];
  2388. return Database::select('*', $table, ['where' => $params]);
  2389. }
  2390. /**
  2391. * @param int $documentId
  2392. * @param int $workId
  2393. * @param int $courseId
  2394. * @param int $sessionId
  2395. * @param int $userId
  2396. * @param int $active
  2397. *
  2398. * @return array
  2399. */
  2400. function getDocumentToWorkPerUser($documentId, $workId, $courseId, $sessionId, $userId, $active = 1)
  2401. {
  2402. $workRel = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_DOCUMENT);
  2403. $work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  2404. $documentId = intval($documentId);
  2405. $workId = intval($workId);
  2406. $courseId = intval($courseId);
  2407. $userId = intval($userId);
  2408. $sessionId = intval($sessionId);
  2409. $active = intval($active);
  2410. $sessionCondition = api_get_session_condition($sessionId);
  2411. $sql = "SELECT w.* FROM $work w
  2412. INNER JOIN $workRel rel
  2413. ON (w.parent_id = rel.work_id)
  2414. WHERE
  2415. w.document_id = $documentId AND
  2416. w.parent_id = $workId AND
  2417. w.c_id = $courseId
  2418. $sessionCondition AND
  2419. user_id = $userId AND
  2420. active = $active
  2421. ";
  2422. $result = Database::query($sql);
  2423. $workInfo = [];
  2424. if (Database::num_rows($result)) {
  2425. $workInfo = Database::fetch_array($result, 'ASSOC');
  2426. }
  2427. return $workInfo;
  2428. }
  2429. /**
  2430. * @param int $workId
  2431. * @param int $courseId
  2432. *
  2433. * @return array
  2434. */
  2435. function getAllDocumentToWork($workId, $courseId)
  2436. {
  2437. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_DOCUMENT);
  2438. $params = [
  2439. 'work_id = ? and c_id = ?' => [$workId, $courseId],
  2440. ];
  2441. return Database::select('*', $table, ['where' => $params]);
  2442. }
  2443. /**
  2444. * @param int $documentId
  2445. * @param int $workId
  2446. * @param int $courseId
  2447. */
  2448. function deleteDocumentToWork($documentId, $workId, $courseId)
  2449. {
  2450. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_DOCUMENT);
  2451. $params = [
  2452. 'document_id = ? and work_id = ? and c_id = ?' => [$documentId, $workId, $courseId],
  2453. ];
  2454. Database::delete($table, $params);
  2455. }
  2456. /**
  2457. * @param int $userId
  2458. * @param int $workId
  2459. * @param int $courseId
  2460. */
  2461. function addUserToWork($userId, $workId, $courseId)
  2462. {
  2463. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_USER);
  2464. $params = [
  2465. 'user_id' => $userId,
  2466. 'work_id' => $workId,
  2467. 'c_id' => $courseId,
  2468. ];
  2469. Database::insert($table, $params);
  2470. }
  2471. /**
  2472. * @param int $userId
  2473. * @param int $workId
  2474. * @param int $courseId
  2475. *
  2476. * @return array
  2477. */
  2478. function getUserToWork($userId, $workId, $courseId)
  2479. {
  2480. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_USER);
  2481. $params = [
  2482. 'user_id = ? and work_id = ? and c_id = ?' => [$userId, $workId, $courseId],
  2483. ];
  2484. return Database::select('*', $table, ['where' => $params]);
  2485. }
  2486. /**
  2487. * @param int $workId
  2488. * @param int $courseId
  2489. * @param bool $getCount
  2490. *
  2491. * @return array|int
  2492. */
  2493. function getAllUserToWork($workId, $courseId, $getCount = false)
  2494. {
  2495. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_USER);
  2496. $params = [
  2497. 'work_id = ? and c_id = ?' => [$workId, $courseId],
  2498. ];
  2499. if ($getCount) {
  2500. $count = 0;
  2501. $result = Database::select(
  2502. 'count(user_id) as count',
  2503. $table,
  2504. ['where' => $params],
  2505. 'simple'
  2506. );
  2507. if (!empty($result)) {
  2508. $count = intval($result['count']);
  2509. }
  2510. return $count;
  2511. } else {
  2512. return Database::select('*', $table, ['where' => $params]);
  2513. }
  2514. }
  2515. /**
  2516. * @param int $userId
  2517. * @param int $workId
  2518. * @param int $courseId
  2519. */
  2520. function deleteUserToWork($userId, $workId, $courseId)
  2521. {
  2522. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_USER);
  2523. $params = [
  2524. 'user_id = ? and work_id = ? and c_id = ?' => [$userId, $workId, $courseId],
  2525. ];
  2526. Database::delete($table, $params);
  2527. }
  2528. /**
  2529. * @param int $userId
  2530. * @param int $workId
  2531. * @param int $courseId
  2532. *
  2533. * @return bool
  2534. */
  2535. function userIsSubscribedToWork($userId, $workId, $courseId)
  2536. {
  2537. $subscribedUsers = getAllUserToWork($workId, $courseId);
  2538. if (empty($subscribedUsers)) {
  2539. return true;
  2540. } else {
  2541. $subscribedUsersList = [];
  2542. foreach ($subscribedUsers as $item) {
  2543. $subscribedUsersList[] = $item['user_id'];
  2544. }
  2545. if (in_array($userId, $subscribedUsersList)) {
  2546. return true;
  2547. }
  2548. }
  2549. return false;
  2550. }
  2551. /**
  2552. * Get the list of students that have to submit their work.
  2553. *
  2554. * @param int $workId The internal ID of the assignment
  2555. * @param int $courseId The course ID
  2556. * @param int $groupId The group ID, if any
  2557. * @param int $sessionId The session ID, if any
  2558. * @param bool $getCount Whether we want just the amount or the full result
  2559. *
  2560. * @return array|int An integer (if we just asked for the count) or an array of users
  2561. */
  2562. function getStudentSubscribedToWork(
  2563. $workId,
  2564. $courseId,
  2565. $groupId = null,
  2566. $sessionId = null,
  2567. $getCount = false
  2568. ) {
  2569. $usersInWork = null;
  2570. $usersInCourse = null;
  2571. if (empty($groupId)) {
  2572. $courseInfo = api_get_course_info_by_id($courseId);
  2573. $status = STUDENT;
  2574. if (!empty($sessionId)) {
  2575. $status = 0;
  2576. }
  2577. $usersInCourse = CourseManager::get_user_list_from_course_code(
  2578. $courseInfo['code'],
  2579. $sessionId,
  2580. null,
  2581. null,
  2582. $status,
  2583. $getCount
  2584. );
  2585. } else {
  2586. $usersInCourse = GroupManager::get_users(
  2587. $groupId,
  2588. false,
  2589. null,
  2590. null,
  2591. $getCount,
  2592. $courseId
  2593. );
  2594. }
  2595. $usersInWork = getAllUserToWork($workId, $courseId, $getCount);
  2596. if (empty($usersInWork)) {
  2597. return $usersInCourse;
  2598. } else {
  2599. return $usersInWork;
  2600. }
  2601. }
  2602. /**
  2603. * @param int $userId
  2604. * @param int $workId
  2605. * @param int $courseId
  2606. * @param bool $forceAccessForCourseAdmins
  2607. *
  2608. * @return bool
  2609. */
  2610. function allowOnlySubscribedUser($userId, $workId, $courseId, $forceAccessForCourseAdmins = false)
  2611. {
  2612. if (api_is_platform_admin() || api_is_allowed_to_edit()) {
  2613. return true;
  2614. }
  2615. if ($forceAccessForCourseAdmins) {
  2616. if (api_is_course_admin() || api_is_coach()) {
  2617. return true;
  2618. }
  2619. }
  2620. return userIsSubscribedToWork($userId, $workId, $courseId);
  2621. }
  2622. /**
  2623. * @param int $workId
  2624. * @param array $courseInfo
  2625. * @param int $documentId
  2626. *
  2627. * @return array
  2628. */
  2629. function getDocumentTemplateFromWork($workId, $courseInfo, $documentId)
  2630. {
  2631. $documents = getAllDocumentToWork($workId, $courseInfo['real_id']);
  2632. if (!empty($documents)) {
  2633. foreach ($documents as $doc) {
  2634. if ($documentId != $doc['document_id']) {
  2635. continue;
  2636. }
  2637. $docData = DocumentManager::get_document_data_by_id($doc['document_id'], $courseInfo['code']);
  2638. $fileInfo = pathinfo($docData['path']);
  2639. if ($fileInfo['extension'] == 'html') {
  2640. if (file_exists($docData['absolute_path']) && is_file($docData['absolute_path'])) {
  2641. $docData['file_content'] = file_get_contents($docData['absolute_path']);
  2642. return $docData;
  2643. }
  2644. }
  2645. }
  2646. }
  2647. return [];
  2648. }
  2649. /**
  2650. * @param int $workId
  2651. * @param array $courseInfo
  2652. *
  2653. * @return string
  2654. */
  2655. function getAllDocumentsFromWorkToString($workId, $courseInfo)
  2656. {
  2657. $documents = getAllDocumentToWork($workId, $courseInfo['real_id']);
  2658. $content = null;
  2659. if (!empty($documents)) {
  2660. $content .= '<ul class="nav nav-list well">';
  2661. $content .= '<li class="nav-header">'.get_lang('Documents').'</li>';
  2662. foreach ($documents as $doc) {
  2663. $docData = DocumentManager::get_document_data_by_id($doc['document_id'], $courseInfo['code']);
  2664. if ($docData) {
  2665. $content .= '<li><a class="link_to_download" target="_blank" href="'.$docData['url'].'">'.$docData['title'].'</a></li>';
  2666. }
  2667. }
  2668. $content .= '</ul><br />';
  2669. }
  2670. return $content;
  2671. }
  2672. /**
  2673. * Returns fck editor toolbar.
  2674. *
  2675. * @return array
  2676. */
  2677. function getWorkDescriptionToolbar()
  2678. {
  2679. return [
  2680. 'ToolbarStartExpanded' => 'true',
  2681. 'ToolbarSet' => 'Work',
  2682. 'Width' => '100%',
  2683. 'Height' => '400',
  2684. ];
  2685. }
  2686. /**
  2687. * @param array $work
  2688. *
  2689. * @return array
  2690. */
  2691. function getWorkComments($work)
  2692. {
  2693. $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
  2694. $userTable = Database::get_main_table(TABLE_MAIN_USER);
  2695. $courseId = intval($work['c_id']);
  2696. $workId = intval($work['id']);
  2697. if (empty($courseId) || empty($workId)) {
  2698. return [];
  2699. }
  2700. $sql = "SELECT
  2701. c.id,
  2702. c.user_id
  2703. FROM $commentTable c
  2704. INNER JOIN $userTable u
  2705. ON (u.id = c.user_id)
  2706. WHERE c_id = $courseId AND work_id = $workId
  2707. ORDER BY sent_at
  2708. ";
  2709. $result = Database::query($sql);
  2710. $comments = Database::store_result($result, 'ASSOC');
  2711. if (!empty($comments)) {
  2712. foreach ($comments as &$comment) {
  2713. $userInfo = api_get_user_info($comment['user_id']);
  2714. $comment['picture'] = $userInfo['avatar'];
  2715. $comment['complete_name'] = $userInfo['complete_name_with_username'];
  2716. $commentInfo = getWorkComment($comment['id']);
  2717. if (!empty($commentInfo)) {
  2718. $comment = array_merge($comment, $commentInfo);
  2719. }
  2720. }
  2721. }
  2722. return $comments;
  2723. }
  2724. /**
  2725. * Get total score from a work list.
  2726. *
  2727. * @param $workList
  2728. *
  2729. * @return int|null
  2730. */
  2731. function getTotalWorkScore($workList)
  2732. {
  2733. $count = 0;
  2734. foreach ($workList as $data) {
  2735. $count += $data['qualification_score'];
  2736. }
  2737. return $count;
  2738. }
  2739. /**
  2740. * Get comment count from a work list (docs sent by students).
  2741. *
  2742. * @param array $workList
  2743. * @param array $courseInfo
  2744. *
  2745. * @return int|null
  2746. */
  2747. function getTotalWorkComment($workList, $courseInfo = [])
  2748. {
  2749. if (empty($courseInfo)) {
  2750. $courseInfo = api_get_course_info();
  2751. }
  2752. $count = 0;
  2753. foreach ($workList as $data) {
  2754. $count += getWorkCommentCount($data['id'], $courseInfo);
  2755. }
  2756. return $count;
  2757. }
  2758. /**
  2759. * Get comment count for a specific work sent by a student.
  2760. *
  2761. * @param int $id
  2762. * @param array $courseInfo
  2763. *
  2764. * @return int
  2765. */
  2766. function getWorkCommentCount($id, $courseInfo = [])
  2767. {
  2768. if (empty($courseInfo)) {
  2769. $courseInfo = api_get_course_info();
  2770. }
  2771. $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
  2772. $id = intval($id);
  2773. $sql = "SELECT count(*) as count
  2774. FROM $commentTable
  2775. WHERE work_id = $id AND c_id = ".$courseInfo['real_id'];
  2776. $result = Database::query($sql);
  2777. if (Database::num_rows($result)) {
  2778. $comment = Database::fetch_array($result);
  2779. return $comment['count'];
  2780. }
  2781. return 0;
  2782. }
  2783. /**
  2784. * Get comment count for a specific parent.
  2785. *
  2786. * @param int $parentId
  2787. * @param array $courseInfo
  2788. * @param int $sessionId
  2789. *
  2790. * @return int
  2791. */
  2792. function getWorkCommentCountFromParent(
  2793. $parentId,
  2794. $courseInfo = [],
  2795. $sessionId = 0
  2796. ) {
  2797. if (empty($courseInfo)) {
  2798. $courseInfo = api_get_course_info();
  2799. }
  2800. if (empty($sessionId)) {
  2801. $sessionId = api_get_session_id();
  2802. } else {
  2803. $sessionId = intval($sessionId);
  2804. }
  2805. $work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  2806. $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
  2807. $parentId = intval($parentId);
  2808. $sessionCondition = api_get_session_condition($sessionId, false, false, 'w.session_id');
  2809. $sql = "SELECT count(*) as count
  2810. FROM $commentTable c INNER JOIN $work w
  2811. ON c.c_id = w.c_id AND w.id = c.work_id
  2812. WHERE
  2813. $sessionCondition AND
  2814. parent_id = $parentId AND
  2815. w.c_id = ".$courseInfo['real_id'];
  2816. $result = Database::query($sql);
  2817. if (Database::num_rows($result)) {
  2818. $comment = Database::fetch_array($result);
  2819. return $comment['count'];
  2820. }
  2821. return 0;
  2822. }
  2823. /**
  2824. * Get last work information from parent.
  2825. *
  2826. * @param int $parentId
  2827. * @param array $courseInfo
  2828. * @param int $sessionId
  2829. *
  2830. * @return int
  2831. */
  2832. function getLastWorkStudentFromParent(
  2833. $parentId,
  2834. $courseInfo = [],
  2835. $sessionId = 0
  2836. ) {
  2837. if (empty($courseInfo)) {
  2838. $courseInfo = api_get_course_info();
  2839. }
  2840. if (empty($sessionId)) {
  2841. $sessionId = api_get_session_id();
  2842. } else {
  2843. $sessionId = intval($sessionId);
  2844. }
  2845. $work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  2846. $sessionCondition = api_get_session_condition($sessionId, false);
  2847. $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
  2848. $parentId = intval($parentId);
  2849. $sql = "SELECT w.*
  2850. FROM $commentTable c INNER JOIN $work w
  2851. ON c.c_id = w.c_id AND w.id = c.work_id
  2852. WHERE
  2853. $sessionCondition AND
  2854. parent_id = $parentId AND
  2855. w.c_id = ".$courseInfo['real_id']."
  2856. ORDER BY w.sent_date
  2857. LIMIT 1
  2858. ";
  2859. $result = Database::query($sql);
  2860. if (Database::num_rows($result)) {
  2861. $comment = Database::fetch_array($result, 'ASSOC');
  2862. return $comment;
  2863. }
  2864. return [];
  2865. }
  2866. /**
  2867. * Get last work information from parent.
  2868. *
  2869. * @param int $userId
  2870. * @param array $parentInfo
  2871. * @param array $courseInfo
  2872. * @param int $sessionId
  2873. *
  2874. * @return int
  2875. */
  2876. function getLastWorkStudentFromParentByUser(
  2877. $userId,
  2878. $parentInfo,
  2879. $courseInfo = [],
  2880. $sessionId = 0
  2881. ) {
  2882. if (empty($courseInfo)) {
  2883. $courseInfo = api_get_course_info();
  2884. }
  2885. if (empty($sessionId)) {
  2886. $sessionId = api_get_session_id();
  2887. } else {
  2888. $sessionId = intval($sessionId);
  2889. }
  2890. $userId = intval($userId);
  2891. $work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  2892. if (empty($parentInfo)) {
  2893. return false;
  2894. }
  2895. $parentId = $parentInfo['id'];
  2896. $sessionCondition = api_get_session_condition($sessionId);
  2897. $sql = "SELECT *
  2898. FROM $work
  2899. WHERE
  2900. user_id = $userId
  2901. $sessionCondition AND
  2902. parent_id = $parentId AND
  2903. c_id = ".$courseInfo['real_id']."
  2904. ORDER BY sent_date DESC
  2905. LIMIT 1
  2906. ";
  2907. $result = Database::query($sql);
  2908. if (Database::num_rows($result)) {
  2909. $work = Database::fetch_array($result, 'ASSOC');
  2910. $work['qualification_rounded'] = formatWorkScore($work['qualification'], $parentInfo['qualification']);
  2911. return $work;
  2912. }
  2913. return [];
  2914. }
  2915. /**
  2916. * @param float $score
  2917. * @param int $weight
  2918. *
  2919. * @return string
  2920. */
  2921. function formatWorkScore($score, $weight)
  2922. {
  2923. $label = 'info';
  2924. $weight = (int) $weight;
  2925. $relativeScore = 0;
  2926. if (!empty($weight)) {
  2927. $relativeScore = $score / $weight;
  2928. }
  2929. if ($relativeScore < 0.5) {
  2930. $label = 'important';
  2931. } elseif ($relativeScore < 0.75) {
  2932. $label = 'warning';
  2933. }
  2934. $scoreBasedInModel = ExerciseLib::convertScoreToModel($relativeScore * 100);
  2935. if (empty($scoreBasedInModel)) {
  2936. $finalScore = api_number_format($score, 1).' / '.$weight;
  2937. return Display::label(
  2938. $finalScore,
  2939. $label
  2940. );
  2941. } else {
  2942. $finalScore = $scoreBasedInModel;
  2943. return $finalScore;
  2944. }
  2945. }
  2946. /**
  2947. * @param int $id comment id
  2948. * @param array $courseInfo
  2949. *
  2950. * @return string
  2951. */
  2952. function getWorkComment($id, $courseInfo = [])
  2953. {
  2954. if (empty($courseInfo)) {
  2955. $courseInfo = api_get_course_info();
  2956. }
  2957. if (empty($courseInfo['real_id'])) {
  2958. return [];
  2959. }
  2960. $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
  2961. $id = intval($id);
  2962. $sql = "SELECT * FROM $commentTable
  2963. WHERE id = $id AND c_id = ".$courseInfo['real_id'];
  2964. $result = Database::query($sql);
  2965. $comment = [];
  2966. if (Database::num_rows($result)) {
  2967. $comment = Database::fetch_array($result, 'ASSOC');
  2968. $filePath = null;
  2969. $fileUrl = null;
  2970. $deleteUrl = null;
  2971. $fileName = null;
  2972. if (!empty($comment['file'])) {
  2973. $work = get_work_data_by_id($comment['work_id']);
  2974. $workParent = get_work_data_by_id($work['parent_id']);
  2975. $filePath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/work/'.$workParent['url'].'/'.$comment['file'];
  2976. $fileUrl = api_get_path(WEB_CODE_PATH).'work/download_comment_file.php?comment_id='.$id.'&'.api_get_cidreq();
  2977. $deleteUrl = api_get_path(WEB_CODE_PATH).'work/view.php?'.api_get_cidreq().'&id='.$comment['work_id'].'&action=delete_attachment&comment_id='.$id;
  2978. $fileParts = explode('_', $comment['file']);
  2979. $fileName = str_replace($fileParts[0].'_'.$fileParts[1].'_', '', $comment['file']);
  2980. }
  2981. $comment['delete_file_url'] = $deleteUrl;
  2982. $comment['file_path'] = $filePath;
  2983. $comment['file_url'] = $fileUrl;
  2984. $comment['file_name_to_show'] = $fileName;
  2985. $comment['sent_at_with_label'] = Display::dateToStringAgoAndLongDate($comment['sent_at']);
  2986. }
  2987. return $comment;
  2988. }
  2989. /**
  2990. * @param int $id
  2991. * @param array $courseInfo
  2992. */
  2993. function deleteCommentFile($id, $courseInfo = [])
  2994. {
  2995. $workComment = getWorkComment($id, $courseInfo);
  2996. if (isset($workComment['file']) && !empty($workComment['file'])) {
  2997. if (file_exists($workComment['file_path'])) {
  2998. $result = my_delete($workComment['file_path']);
  2999. if ($result) {
  3000. $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
  3001. $params = ['file' => ''];
  3002. Database::update(
  3003. $commentTable,
  3004. $params,
  3005. ['id = ? AND c_id = ? ' => [$workComment['id'], $workComment['c_id']]]
  3006. );
  3007. }
  3008. }
  3009. }
  3010. }
  3011. /**
  3012. * Adds a comments to the work document.
  3013. *
  3014. * @param array $courseInfo
  3015. * @param int $userId
  3016. * @param array $parentWork
  3017. * @param array $work
  3018. * @param array $data
  3019. *
  3020. * @return int
  3021. */
  3022. function addWorkComment($courseInfo, $userId, $parentWork, $work, $data)
  3023. {
  3024. $fileData = isset($data['attachment']) ? $data['attachment'] : null;
  3025. $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
  3026. // If no attachment and no comment then don't save comment
  3027. if (empty($fileData['name']) && empty($data['comment'])) {
  3028. return false;
  3029. }
  3030. $params = [
  3031. 'work_id' => $work['id'],
  3032. 'c_id' => $work['c_id'],
  3033. 'user_id' => $userId,
  3034. 'comment' => $data['comment'],
  3035. 'sent_at' => api_get_utc_datetime(),
  3036. ];
  3037. $commentId = Database::insert($commentTable, $params);
  3038. if ($commentId) {
  3039. Display::addFlash(
  3040. Display::return_message(get_lang('You comment has been added'))
  3041. );
  3042. $sql = "UPDATE $commentTable SET id = iid WHERE iid = $commentId";
  3043. Database::query($sql);
  3044. }
  3045. $userIdListToSend = [];
  3046. if (api_is_allowed_to_edit()) {
  3047. if (isset($data['send_email']) && $data['send_email']) {
  3048. // Teacher sends a feedback
  3049. $userIdListToSend = [$work['user_id']];
  3050. }
  3051. } else {
  3052. $sessionId = api_get_session_id();
  3053. if (empty($sessionId)) {
  3054. $teachers = CourseManager::get_teacher_list_from_course_code(
  3055. $courseInfo['code']
  3056. );
  3057. if (!empty($teachers)) {
  3058. $userIdListToSend = array_keys($teachers);
  3059. }
  3060. } else {
  3061. $teachers = SessionManager::getCoachesByCourseSession(
  3062. $sessionId,
  3063. $courseInfo['real_id']
  3064. );
  3065. if (!empty($teachers)) {
  3066. $userIdListToSend = array_values($teachers);
  3067. }
  3068. }
  3069. $sendNotification = api_get_course_setting('email_to_teachers_on_new_work_feedback');
  3070. if ($sendNotification != 1) {
  3071. $userIdListToSend = [];
  3072. }
  3073. }
  3074. $url = api_get_path(WEB_CODE_PATH).'work/view.php?'.api_get_cidreq().'&id='.$work['id'];
  3075. $subject = sprintf(get_lang('There\'s a new feedback in work: %s'), $parentWork['title']);
  3076. $content = sprintf(get_lang('There\'s a new feedback in work: %sInWorkXHere'), $work['title'], $url);
  3077. if (!empty($data['comment'])) {
  3078. $content .= '<br /><b>'.get_lang('Comment').':</b><br />'.$data['comment'];
  3079. }
  3080. if (!empty($userIdListToSend)) {
  3081. foreach ($userIdListToSend as $userIdToSend) {
  3082. MessageManager::send_message_simple(
  3083. $userIdToSend,
  3084. $subject,
  3085. $content
  3086. );
  3087. }
  3088. }
  3089. if (!empty($commentId) && !empty($fileData)) {
  3090. $workParent = get_work_data_by_id($work['parent_id']);
  3091. if (!empty($workParent)) {
  3092. $uploadDir = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/work'.$workParent['url'];
  3093. $newFileName = 'comment_'.$commentId.'_'.php2phps(api_replace_dangerous_char($fileData['name']));
  3094. $newFilePath = $uploadDir.'/'.$newFileName;
  3095. $result = move_uploaded_file($fileData['tmp_name'], $newFilePath);
  3096. if ($result) {
  3097. $params = ['file' => $newFileName];
  3098. Database::update(
  3099. $commentTable,
  3100. $params,
  3101. ['id = ? AND c_id = ? ' => [$commentId, $work['c_id']]]
  3102. );
  3103. }
  3104. }
  3105. }
  3106. }
  3107. /**
  3108. * @param array $work
  3109. * @param array $workParent
  3110. *
  3111. * @return string
  3112. */
  3113. function getWorkCommentForm($work, $workParent)
  3114. {
  3115. $url = api_get_path(WEB_CODE_PATH).'work/view.php?id='.$work['id'].'&action=send_comment&'.api_get_cidreq();
  3116. $form = new FormValidator(
  3117. 'work_comment',
  3118. 'post',
  3119. $url,
  3120. '',
  3121. ['enctype' => "multipart/form-data"]
  3122. );
  3123. $qualification = $workParent['qualification'];
  3124. if (api_is_allowed_to_edit()) {
  3125. if (!empty($qualification) && intval($qualification) > 0) {
  3126. $model = ExerciseLib::getCourseScoreModel();
  3127. if (empty($model)) {
  3128. $form->addFloat(
  3129. 'qualification',
  3130. [get_lang('Score'), " / ".$qualification],
  3131. false,
  3132. [],
  3133. false,
  3134. 0,
  3135. $qualification
  3136. );
  3137. } else {
  3138. ExerciseLib::addScoreModelInput(
  3139. $form,
  3140. 'qualification',
  3141. $qualification,
  3142. $work['qualification']
  3143. );
  3144. }
  3145. $form->addFile('file', get_lang('Correction'));
  3146. $form->setDefaults(['qualification' => $work['qualification']]);
  3147. }
  3148. }
  3149. Skill::addSkillsToUserForm($form, ITEM_TYPE_STUDENT_PUBLICATION, $workParent['id'], $work['user_id'], $work['id']);
  3150. $form->addHtmlEditor('comment', get_lang('Comment'), false);
  3151. $form->addFile('attachment', get_lang('Attachment'));
  3152. $form->addElement('hidden', 'id', $work['id']);
  3153. if (api_is_allowed_to_edit()) {
  3154. $form->addCheckBox(
  3155. 'send_email',
  3156. null,
  3157. get_lang('Send message mail to student')
  3158. );
  3159. }
  3160. $form->addButtonSend(get_lang('Send'), 'button');
  3161. return $form->returnForm();
  3162. }
  3163. /**
  3164. * @param array $homework result of get_work_assignment_by_id()
  3165. *
  3166. * @return array
  3167. */
  3168. function getWorkDateValidationStatus($homework)
  3169. {
  3170. $message = null;
  3171. $has_expired = false;
  3172. $has_ended = false;
  3173. if (!empty($homework)) {
  3174. if (!empty($homework['expires_on']) || !empty($homework['ends_on'])) {
  3175. $time_now = time();
  3176. if (!empty($homework['expires_on'])) {
  3177. $time_expires = api_strtotime($homework['expires_on'], 'UTC');
  3178. $difference = $time_expires - $time_now;
  3179. if ($difference < 0) {
  3180. $has_expired = true;
  3181. }
  3182. }
  3183. if (empty($homework['expires_on'])) {
  3184. $has_expired = false;
  3185. }
  3186. if (!empty($homework['ends_on'])) {
  3187. $time_ends = api_strtotime($homework['ends_on'], 'UTC');
  3188. $difference2 = $time_ends - $time_now;
  3189. if ($difference2 < 0) {
  3190. $has_ended = true;
  3191. }
  3192. }
  3193. $ends_on = api_convert_and_format_date($homework['ends_on']);
  3194. $expires_on = api_convert_and_format_date($homework['expires_on']);
  3195. }
  3196. if ($has_ended) {
  3197. $message = Display::return_message(get_lang('End date already passed').' '.$ends_on, 'error');
  3198. } elseif ($has_expired) {
  3199. $message = Display::return_message(get_lang('Expiry date already passed').' '.$expires_on, 'warning');
  3200. } else {
  3201. if ($has_expired) {
  3202. $message = Display::return_message(get_lang('ExpiryDateToSend messageWorkIs').' '.$expires_on);
  3203. }
  3204. }
  3205. }
  3206. return [
  3207. 'message' => $message,
  3208. 'has_ended' => $has_ended,
  3209. 'has_expired' => $has_expired,
  3210. ];
  3211. }
  3212. /**
  3213. * @param FormValidator $form
  3214. * @param int $uploadFormType
  3215. */
  3216. function setWorkUploadForm($form, $uploadFormType = 0)
  3217. {
  3218. $form->addHeader(get_lang('Upload a document'));
  3219. $form->addHidden('contains_file', 0, ['id' => 'contains_file_id']);
  3220. $form->addHidden('active', 1);
  3221. $form->addHidden('accepted', 1);
  3222. $form->addElement('text', 'title', get_lang('Title'), ['id' => 'file_upload']);
  3223. $form->addElement(
  3224. 'text',
  3225. 'extension',
  3226. get_lang('File extension'),
  3227. ['id' => 'file_extension', 'readonly' => 'readonly']
  3228. );
  3229. $form->addRule('title', get_lang('Required field'), 'required');
  3230. switch ($uploadFormType) {
  3231. case 0:
  3232. // File and text.
  3233. $form->addElement(
  3234. 'file',
  3235. 'file',
  3236. get_lang('Upload a document'),
  3237. 'size="40" onchange="updateDocumentTitle(this.value)"'
  3238. );
  3239. $form->addProgress();
  3240. $form->addHtmlEditor('description', get_lang('Description'), false, false, getWorkDescriptionToolbar());
  3241. break;
  3242. case 1:
  3243. // Only text.
  3244. $form->addHtmlEditor('description', get_lang('Description'), false, false, getWorkDescriptionToolbar());
  3245. $form->addRule('description', get_lang('Required field'), 'required');
  3246. break;
  3247. case 2:
  3248. // Only file.
  3249. $form->addElement(
  3250. 'file',
  3251. 'file',
  3252. get_lang('Upload a document'),
  3253. 'size="40" onchange="updateDocumentTitle(this.value)"'
  3254. );
  3255. $form->addProgress();
  3256. $form->addRule('file', get_lang('Required field'), 'required');
  3257. break;
  3258. }
  3259. $form->addButtonUpload(get_lang('Upload'), 'submitWork');
  3260. }
  3261. /**
  3262. * @param array $my_folder_data
  3263. * @param array $_course
  3264. * @param bool $isCorrection
  3265. * @param array $workInfo
  3266. * @param array $file
  3267. *
  3268. * @return array
  3269. */
  3270. function uploadWork($my_folder_data, $_course, $isCorrection = false, $workInfo = [], $file = [])
  3271. {
  3272. if (isset($_FILES['file']) && !empty($_FILES['file'])) {
  3273. $file = $_FILES['file'];
  3274. }
  3275. if (empty($file['size'])) {
  3276. return [
  3277. 'error' => Display:: return_message(
  3278. get_lang('There was a problem uploading your document: the received file had a 0 bytes size on the server. Please, review your local file for any corruption or damage, then try again.'),
  3279. 'error'
  3280. ),
  3281. ];
  3282. }
  3283. $updir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/work/'; //directory path to upload
  3284. // Try to add an extension to the file if it has'nt one
  3285. $filename = add_ext_on_mime(stripslashes($file['name']), $file['type']);
  3286. // Replace dangerous characters
  3287. $filename = api_replace_dangerous_char($filename);
  3288. // Transform any .php file in .phps fo security
  3289. $filename = php2phps($filename);
  3290. $filesize = filesize($file['tmp_name']);
  3291. if (empty($filesize)) {
  3292. return [
  3293. 'error' => Display::return_message(
  3294. get_lang('There was a problem uploading your document: the received file had a 0 bytes size on the server. Please, review your local file for any corruption or damage, then try again.'),
  3295. 'error'
  3296. ),
  3297. ];
  3298. } elseif (!filter_extension($new_file_name)) {
  3299. return [
  3300. 'error' => Display::return_message(
  3301. get_lang('File upload failed: this file extension or file type is prohibited'),
  3302. 'error'
  3303. ),
  3304. ];
  3305. }
  3306. $repo = Container::getDocumentRepository();
  3307. $totalSpace = $repo->getTotalSpace($_course['real_id']);
  3308. $course_max_space = DocumentManager::get_course_quota($_course['code']);
  3309. $total_size = $filesize + $totalSpace;
  3310. if ($total_size > $course_max_space) {
  3311. return [
  3312. 'error' => Display::return_message(get_lang('The upload has failed. Either you have exceeded your maximum quota, or there is not enough disk space.'), 'error'),
  3313. ];
  3314. }
  3315. // Compose a unique file name to avoid any conflict
  3316. $new_file_name = api_get_unique_id();
  3317. if ($isCorrection) {
  3318. if (!empty($workInfo['url'])) {
  3319. $new_file_name = basename($workInfo['url']).'_correction';
  3320. } else {
  3321. $new_file_name = $new_file_name.'_correction';
  3322. }
  3323. }
  3324. $curdirpath = basename($my_folder_data['url']);
  3325. // If we come from the group tools the groupid will be saved in $work_table
  3326. if (is_dir($updir.$curdirpath) || empty($curdirpath)) {
  3327. $result = move_uploaded_file(
  3328. $file['tmp_name'],
  3329. $updir.$curdirpath.'/'.$new_file_name
  3330. );
  3331. } else {
  3332. return [
  3333. 'error' => Display :: return_message(
  3334. get_lang('Target folder doesn\'t exist on the server.'),
  3335. 'error'
  3336. ),
  3337. ];
  3338. }
  3339. if ($result) {
  3340. $url = 'work/'.$curdirpath.'/'.$new_file_name;
  3341. } else {
  3342. return false;
  3343. }
  3344. return [
  3345. 'url' => $url,
  3346. 'filename' => $filename,
  3347. 'filesize' => $filesize,
  3348. 'error' => '',
  3349. ];
  3350. }
  3351. /**
  3352. * Send an e-mail to users related to this work (course teachers, usually, but
  3353. * might include other group members).
  3354. *
  3355. * @param int $workId
  3356. * @param array $courseInfo
  3357. * @param int $sessionId
  3358. */
  3359. function sendAlertToUsers($workId, $courseInfo, $sessionId)
  3360. {
  3361. $sessionId = (int) $sessionId;
  3362. $workData = get_work_data_by_id($workId, $courseInfo['real_id'], $sessionId);
  3363. // last value is to check this is not "just" an edit
  3364. // YW Tis part serve to send a e-mail to the tutors when a new file is sent
  3365. $send = api_get_course_setting('email_alert_manager_on_new_doc');
  3366. $userList = [];
  3367. if ($send == SEND_EMAIL_EVERYONE || $send == SEND_EMAIL_TEACHERS) {
  3368. // Lets predefine some variables. Be sure to change the from address!
  3369. if (empty($sessionId)) {
  3370. // Teachers
  3371. $userList = CourseManager::get_user_list_from_course_code(
  3372. api_get_course_id(),
  3373. null,
  3374. null,
  3375. null,
  3376. COURSEMANAGER
  3377. );
  3378. } else {
  3379. // Coaches
  3380. $userList = CourseManager::get_user_list_from_course_code(
  3381. api_get_course_id(),
  3382. $sessionId,
  3383. null,
  3384. null,
  3385. 2
  3386. );
  3387. }
  3388. }
  3389. if ($send == SEND_EMAIL_EVERYONE || $send == SEND_EMAIL_STUDENTS) {
  3390. // Send mail only to sender
  3391. $studentList = [[
  3392. 'user_id' => api_get_user_id(),
  3393. ]];
  3394. $userList = array_merge($userList, $studentList);
  3395. }
  3396. if ($send) {
  3397. $subject = "[".api_get_setting('siteName')."] ".get_lang('Send messageMailBody')."\n ".get_lang('Course name').": ".$courseInfo['name']." ";
  3398. foreach ($userList as $user_data) {
  3399. $to_user_id = $user_data['user_id'];
  3400. $user_info = api_get_user_info($to_user_id);
  3401. $message = get_lang('Send messageMailBody')."\n".get_lang('Course name')." : ".$courseInfo['name']."\n";
  3402. $message .= get_lang('Username')." : ".$user_info['complete_name']."\n";
  3403. $message .= get_lang('Date sent')." : ".api_format_date(api_get_local_time())."\n";
  3404. $url = api_get_path(WEB_CODE_PATH)."work/work.php?cidReq=".$courseInfo['code']."&id_session=".$sessionId."&id=".$workData['id'];
  3405. $message .= get_lang('Assignment name')." : ".$workData['title']."\n\n".'<a href="'.$url.'">'.get_lang('Download link')."</a>\n";
  3406. MessageManager::send_message_simple(
  3407. $to_user_id,
  3408. $subject,
  3409. $message,
  3410. 0,
  3411. false,
  3412. false,
  3413. [],
  3414. false
  3415. );
  3416. }
  3417. }
  3418. }
  3419. /**
  3420. * Check if the current uploaded work filename already exists in the current assement.
  3421. *
  3422. * @param string $filename
  3423. * @param int $workId
  3424. *
  3425. * @return array
  3426. */
  3427. function checkExistingWorkFileName($filename, $workId)
  3428. {
  3429. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  3430. $filename = Database::escape_string($filename);
  3431. $workId = (int) $workId;
  3432. $sql = "SELECT title FROM $table
  3433. WHERE parent_id = $workId AND title = '$filename' AND active = 1";
  3434. $result = Database::query($sql);
  3435. return Database::fetch_assoc($result);
  3436. }
  3437. /**
  3438. * @param array $workInfo
  3439. * @param array $values
  3440. * @param array $courseInfo
  3441. * @param int $sessionId
  3442. * @param int $groupId
  3443. * @param int $userId
  3444. * @param array $file
  3445. * @param bool $checkDuplicated
  3446. * @param bool $showFlashMessage
  3447. *
  3448. * @return string|null
  3449. */
  3450. function processWorkForm(
  3451. $workInfo,
  3452. $values,
  3453. $courseInfo,
  3454. $sessionId,
  3455. $groupId,
  3456. $userId,
  3457. $file = [],
  3458. $checkDuplicated = false,
  3459. $showFlashMessage = true
  3460. ) {
  3461. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  3462. $courseId = $courseInfo['real_id'];
  3463. $groupId = (int) $groupId;
  3464. $sessionId = (int) $sessionId;
  3465. $userId = (int) $userId;
  3466. $extension = '';
  3467. if (isset($values['extension'])) {
  3468. $extension = $values['extension'];
  3469. } else {
  3470. $fileInfo = pathinfo($values['title']);
  3471. if (isset($fileInfo['extension']) && !empty($fileInfo['extension'])) {
  3472. $extension = '.'.$fileInfo['extension'];
  3473. $values['title'] = $fileInfo['filename'];
  3474. }
  3475. }
  3476. $title = $values['title'].$extension;
  3477. $description = isset($values['description']) ? $values['description'] : '';
  3478. $containsFile = isset($values['contains_file']) && !empty($values['contains_file']) ? (int) $values['contains_file'] : 0;
  3479. $saveWork = true;
  3480. $filename = null;
  3481. $url = null;
  3482. $filesize = null;
  3483. $workData = [];
  3484. $message = null;
  3485. if ($containsFile) {
  3486. $saveWork = false;
  3487. if ($checkDuplicated) {
  3488. if (checkExistingWorkFileName($file['name'], $workInfo['id'])) {
  3489. $saveWork = false;
  3490. $result['error'] = get_lang('You have already sent this file or another file with the same name. Please make sure you only upload each file once.');
  3491. $workData['error'] = get_lang(' already exists.');
  3492. } else {
  3493. $result = uploadWork($workInfo, $courseInfo, false, [], $file);
  3494. }
  3495. } else {
  3496. $result = uploadWork($workInfo, $courseInfo, false, [], $file);
  3497. }
  3498. if (isset($result['error'])) {
  3499. $saveWork = false;
  3500. if ($showFlashMessage) {
  3501. $message = $result['error'];
  3502. }
  3503. if (empty($result['error']) && isset($result['url']) && !empty($result['url'])) {
  3504. $saveWork = true;
  3505. }
  3506. }
  3507. }
  3508. if ($saveWork) {
  3509. $filename = isset($result['filename']) ? $result['filename'] : null;
  3510. if (empty($title)) {
  3511. $title = isset($result['title']) && !empty($result['title']) ? $result['title'] : get_lang('Untitled');
  3512. }
  3513. $filesize = isset($result['filesize']) ? $result['filesize'] : null;
  3514. $url = isset($result['url']) ? $result['url'] : null;
  3515. }
  3516. if (empty($title)) {
  3517. $title = get_lang('Untitled');
  3518. }
  3519. $groupIid = 0;
  3520. $groupInfo = [];
  3521. if ($groupId) {
  3522. $groupInfo = GroupManager::get_group_properties($groupId);
  3523. $groupIid = $groupInfo['iid'];
  3524. }
  3525. if ($saveWork) {
  3526. $active = '1';
  3527. $params = [
  3528. 'c_id' => $courseId,
  3529. 'url' => $url,
  3530. 'filetype' => 'file',
  3531. 'title' => $title,
  3532. 'description' => $description,
  3533. 'contains_file' => $containsFile,
  3534. 'active' => $active,
  3535. 'accepted' => '1',
  3536. 'qualificator_id' => 0,
  3537. 'document_id' => 0,
  3538. 'weight' => 0,
  3539. 'allow_text_assignment' => 0,
  3540. 'post_group_id' => $groupIid,
  3541. 'sent_date' => api_get_utc_datetime(),
  3542. 'parent_id' => $workInfo['id'],
  3543. 'session_id' => $sessionId ? $sessionId : null,
  3544. 'user_id' => $userId,
  3545. 'has_properties' => 0,
  3546. 'qualification' => 0,
  3547. //'filesize' => $filesize
  3548. ];
  3549. $workId = Database::insert($work_table, $params);
  3550. if ($workId) {
  3551. $sql = "UPDATE $work_table SET id = iid WHERE iid = $workId ";
  3552. Database::query($sql);
  3553. if (array_key_exists('filename', $workInfo) && !empty($filename)) {
  3554. $filename = Database::escape_string($filename);
  3555. $sql = "UPDATE $work_table SET
  3556. filename = '$filename'
  3557. WHERE iid = $workId";
  3558. Database::query($sql);
  3559. }
  3560. if (array_key_exists('document_id', $workInfo)) {
  3561. $documentId = isset($values['document_id']) ? (int) $values['document_id'] : 0;
  3562. $sql = "UPDATE $work_table SET
  3563. document_id = '$documentId'
  3564. WHERE iid = $workId";
  3565. Database::query($sql);
  3566. }
  3567. api_item_property_update(
  3568. $courseInfo,
  3569. 'work',
  3570. $workId,
  3571. 'DocumentAdded',
  3572. $userId,
  3573. $groupInfo
  3574. );
  3575. sendAlertToUsers($workId, $courseInfo, $sessionId);
  3576. Event::event_upload($workId);
  3577. // The following feature requires the creation of a work-type
  3578. // extra_field and the following setting in the configuration file
  3579. // (until moved to the database). It allows te teacher to set a
  3580. // "considered work time", meaning the time we assume a student
  3581. // would have spent, approximately, to prepare the task before
  3582. // handing it in Chamilo, adding this time to the student total
  3583. // course use time, as a register of time spent *before* his
  3584. // connection to the platform to hand the work in.
  3585. $consideredWorkingTime = api_get_configuration_value('considered_working_time');
  3586. if (!empty($consideredWorkingTime)) {
  3587. // Get the "considered work time" defined for this work
  3588. $fieldValue = new ExtraFieldValue('work');
  3589. $resultExtra = $fieldValue->getAllValuesForAnItem(
  3590. $workInfo['iid'], //the ID of the work *folder*, not the document uploaded by the student
  3591. true
  3592. );
  3593. $workingTime = null;
  3594. foreach ($resultExtra as $field) {
  3595. $field = $field['value'];
  3596. if ($consideredWorkingTime == $field->getField()->getVariable()) {
  3597. $workingTime = $field->getValue();
  3598. }
  3599. }
  3600. // If no time was defined, or a time of "0" was set, do nothing
  3601. if (!empty($workingTime)) {
  3602. // If some time is set, get the list of docs handed in by
  3603. // this student (to make sure we count the time only once)
  3604. $userWorks = get_work_user_list(
  3605. 0,
  3606. 100,
  3607. null,
  3608. null,
  3609. $workInfo['id'],
  3610. null,
  3611. $userId,
  3612. false,
  3613. $courseId,
  3614. $sessionId
  3615. );
  3616. if (count($userWorks) == 1) {
  3617. // The student only uploaded one doc so far, so add the
  3618. // considered work time to his course connection time
  3619. Event::eventAddVirtualCourseTime($courseId, $userId, $sessionId, $workingTime);
  3620. }
  3621. }
  3622. }
  3623. $workData = get_work_data_by_id($workId);
  3624. if ($workData && $showFlashMessage) {
  3625. Display::addFlash(Display::return_message(get_lang('The file has been added to the list of publications.')));
  3626. }
  3627. }
  3628. } else {
  3629. if ($showFlashMessage) {
  3630. Display::addFlash(
  3631. Display::return_message(
  3632. $message ? $message : get_lang('Impossible to save the document'),
  3633. 'error'
  3634. )
  3635. );
  3636. }
  3637. }
  3638. return $workData;
  3639. }
  3640. /**
  3641. * Creates a new task (directory) in the assignment tool.
  3642. *
  3643. * @param array $formValues
  3644. * @param int $user_id
  3645. * @param array $courseInfo
  3646. * @param int $groupId
  3647. * @param int $sessionId
  3648. *
  3649. * @return bool|int
  3650. * @note $params can have the following elements, but should at least have the 2 first ones: (
  3651. * 'new_dir' => 'some-name',
  3652. * 'description' => 'some-desc',
  3653. * 'qualification' => 20 (e.g. 20),
  3654. * 'weight' => 50 (percentage) to add to gradebook (e.g. 50),
  3655. * 'allow_text_assignment' => 0/1/2,
  3656. *
  3657. * @todo Rename createAssignment or createWork, or something like that
  3658. */
  3659. function addDir($formValues, $user_id, $courseInfo, $groupId, $sessionId = 0)
  3660. {
  3661. $em = Database::getManager();
  3662. $user_id = (int) $user_id;
  3663. $groupId = (int) $groupId;
  3664. $sessionId = (int) $sessionId;
  3665. $groupIid = 0;
  3666. $groupInfo = [];
  3667. if (!empty($groupId)) {
  3668. $groupInfo = GroupManager::get_group_properties($groupId);
  3669. $groupIid = $groupInfo['iid'];
  3670. }
  3671. $session = $em->find('ChamiloCoreBundle:Session', $sessionId);
  3672. $base_work_dir = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/work';
  3673. $course_id = $courseInfo['real_id'];
  3674. $directory = api_replace_dangerous_char($formValues['new_dir']);
  3675. $directory = disable_dangerous_file($directory);
  3676. $created_dir = create_unexisting_work_directory($base_work_dir, $directory);
  3677. if (empty($created_dir)) {
  3678. return false;
  3679. }
  3680. $enableEndDate = isset($formValues['enableEndDate']) ? true : false;
  3681. $enableExpiryDate = isset($formValues['enableExpiryDate']) ? true : false;
  3682. if ($enableEndDate && $enableExpiryDate) {
  3683. if ($formValues['expires_on'] > $formValues['ends_on']) {
  3684. Display::addFlash(
  3685. Display::return_message(
  3686. get_lang('The date of effective blocking of sending the work can not be before the displayed posting deadline.'),
  3687. 'warning'
  3688. )
  3689. );
  3690. return false;
  3691. }
  3692. }
  3693. $dirName = '/'.$created_dir;
  3694. $today = new DateTime(api_get_utc_datetime(), new DateTimeZone('UTC'));
  3695. $title = isset($formValues['work_title']) ? $formValues['work_title'] : $formValues['new_dir'];
  3696. $workTable = new CStudentPublication();
  3697. $workTable
  3698. ->setCId($course_id)
  3699. ->setUrl($dirName)
  3700. ->setTitle($title)
  3701. ->setDescription($formValues['description'])
  3702. ->setActive(true)
  3703. ->setAccepted(true)
  3704. ->setFiletype('folder')
  3705. ->setPostGroupId($groupIid)
  3706. ->setSentDate($today)
  3707. ->setQualification($formValues['qualification'] != '' ? $formValues['qualification'] : 0)
  3708. ->setParentId(0)
  3709. ->setQualificatorId(0)
  3710. ->setWeight(!empty($formValues['weight']) ? $formValues['weight'] : 0)
  3711. ->setSession($session)
  3712. ->setAllowTextAssignment($formValues['allow_text_assignment'])
  3713. ->setContainsFile(0)
  3714. ->setUserId($user_id)
  3715. ->setHasProperties(0)
  3716. ->setDocumentId(0);
  3717. $em->persist($workTable);
  3718. $em->flush();
  3719. $workTable->setId($workTable->getIid());
  3720. $em->merge($workTable);
  3721. $em->flush();
  3722. // Folder created
  3723. api_item_property_update(
  3724. $courseInfo,
  3725. 'work',
  3726. $workTable->getIid(),
  3727. 'DirectoryCreated',
  3728. $user_id,
  3729. $groupInfo
  3730. );
  3731. updatePublicationAssignment(
  3732. $workTable->getIid(),
  3733. $formValues,
  3734. $courseInfo,
  3735. $groupIid
  3736. );
  3737. // Added the new Work ID to the extra field values
  3738. $formValues['item_id'] = $workTable->getIid();
  3739. $workFieldValue = new ExtraFieldValue('work');
  3740. $workFieldValue->saveFieldValues($formValues);
  3741. $sendEmailAlert = api_get_course_setting('email_alert_students_on_new_homework');
  3742. switch ($sendEmailAlert) {
  3743. case 1:
  3744. sendEmailToStudentsOnHomeworkCreation(
  3745. $workTable->getIid(),
  3746. $course_id,
  3747. $sessionId
  3748. );
  3749. //no break
  3750. case 2:
  3751. sendEmailToDrhOnHomeworkCreation(
  3752. $workTable->getIid(),
  3753. $course_id,
  3754. $sessionId
  3755. );
  3756. break;
  3757. }
  3758. return $workTable->getIid();
  3759. }
  3760. /**
  3761. * @param int $workId
  3762. * @param array $courseInfo
  3763. *
  3764. * @return int
  3765. */
  3766. function agendaExistsForWork($workId, $courseInfo)
  3767. {
  3768. $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  3769. $courseId = $courseInfo['real_id'];
  3770. $workId = intval($workId);
  3771. $sql = "SELECT add_to_calendar FROM $workTable
  3772. WHERE c_id = $courseId AND publication_id = ".$workId;
  3773. $res = Database::query($sql);
  3774. if (Database::num_rows($res)) {
  3775. $row = Database::fetch_array($res, 'ASSOC');
  3776. if (!empty($row['add_to_calendar'])) {
  3777. return $row['add_to_calendar'];
  3778. }
  3779. }
  3780. return 0;
  3781. }
  3782. /**
  3783. * Update work description, qualification, weight, allow_text_assignment.
  3784. *
  3785. * @param int $workId (iid)
  3786. * @param array $params
  3787. * @param array $courseInfo
  3788. * @param int $sessionId
  3789. */
  3790. function updateWork($workId, $params, $courseInfo, $sessionId = 0)
  3791. {
  3792. $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  3793. $filteredParams = [
  3794. 'description' => $params['description'],
  3795. 'qualification' => $params['qualification'],
  3796. 'weight' => $params['weight'],
  3797. 'allow_text_assignment' => $params['allow_text_assignment'],
  3798. ];
  3799. Database::update(
  3800. $workTable,
  3801. $filteredParams,
  3802. [
  3803. 'iid = ? AND c_id = ?' => [
  3804. $workId,
  3805. $courseInfo['real_id'],
  3806. ],
  3807. ]
  3808. );
  3809. $workFieldValue = new ExtraFieldValue('work');
  3810. $workFieldValue->saveFieldValues($params);
  3811. }
  3812. /**
  3813. * @param int $workId
  3814. * @param array $params
  3815. * @param array $courseInfo
  3816. * @param int $groupId
  3817. */
  3818. function updatePublicationAssignment($workId, $params, $courseInfo, $groupId)
  3819. {
  3820. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  3821. $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  3822. $workId = intval($workId);
  3823. $now = api_get_utc_datetime();
  3824. $course_id = $courseInfo['real_id'];
  3825. // Insert into agenda
  3826. $agendaId = 0;
  3827. if (isset($params['add_to_calendar']) && $params['add_to_calendar'] == 1) {
  3828. // Setting today date
  3829. $date = $end_date = $now;
  3830. if (isset($params['enableExpiryDate'])) {
  3831. $end_date = $params['expires_on'];
  3832. $date = $end_date;
  3833. }
  3834. $title = sprintf(get_lang('Handing over of task %s'), $params['new_dir']);
  3835. $description = isset($params['description']) ? $params['description'] : '';
  3836. $content = '<a href="'.api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq().'&id='.$workId.'">'
  3837. .$params['new_dir'].'</a>'.$description;
  3838. $agendaId = agendaExistsForWork($workId, $courseInfo);
  3839. // Add/edit agenda
  3840. $agenda = new Agenda('course');
  3841. $agenda->set_course($courseInfo);
  3842. if (!empty($agendaId)) {
  3843. // add_to_calendar is set but it doesnt exists then invalidate
  3844. $eventInfo = $agenda->get_event($agendaId);
  3845. if (empty($eventInfo)) {
  3846. $agendaId = 0;
  3847. }
  3848. }
  3849. $eventColor = $agenda->eventStudentPublicationColor;
  3850. if (empty($agendaId)) {
  3851. $agendaId = $agenda->addEvent(
  3852. $date,
  3853. $end_date,
  3854. 'false',
  3855. $title,
  3856. $content,
  3857. ['GROUP:'.$groupId],
  3858. false,
  3859. null,
  3860. [],
  3861. [],
  3862. null,
  3863. $eventColor
  3864. );
  3865. } else {
  3866. $agenda->editEvent(
  3867. $agendaId,
  3868. $end_date,
  3869. $end_date,
  3870. 'false',
  3871. $title,
  3872. $content,
  3873. [],
  3874. [],
  3875. [],
  3876. null,
  3877. $eventColor
  3878. );
  3879. }
  3880. }
  3881. $qualification = isset($params['qualification']) && !empty($params['qualification']) ? 1 : 0;
  3882. $expiryDate = isset($params['enableExpiryDate']) && (int) $params['enableExpiryDate'] == 1 ? api_get_utc_datetime($params['expires_on']) : '';
  3883. $endDate = isset($params['enableEndDate']) && (int) $params['enableEndDate'] == 1 ? api_get_utc_datetime($params['ends_on']) : '';
  3884. $data = get_work_assignment_by_id($workId, $course_id);
  3885. if (!empty($expiryDate)) {
  3886. $expiryDateCondition = "expires_on = '".Database::escape_string($expiryDate)."', ";
  3887. } else {
  3888. $expiryDateCondition = "expires_on = null, ";
  3889. }
  3890. if (!empty($endDate)) {
  3891. $endOnCondition = "ends_on = '".Database::escape_string($endDate)."', ";
  3892. } else {
  3893. $endOnCondition = "ends_on = null, ";
  3894. }
  3895. if (empty($data)) {
  3896. $sql = "INSERT INTO $table SET
  3897. c_id = $course_id ,
  3898. $expiryDateCondition
  3899. $endOnCondition
  3900. add_to_calendar = $agendaId,
  3901. enable_qualification = '$qualification',
  3902. publication_id = '$workId'";
  3903. Database::query($sql);
  3904. $my_last_id = Database::insert_id();
  3905. if ($my_last_id) {
  3906. $sql = "UPDATE $table SET
  3907. id = iid
  3908. WHERE iid = $my_last_id";
  3909. Database::query($sql);
  3910. $sql = "UPDATE $workTable SET
  3911. has_properties = $my_last_id,
  3912. view_properties = 1
  3913. WHERE c_id = $course_id AND id = $workId";
  3914. Database::query($sql);
  3915. }
  3916. } else {
  3917. $sql = "UPDATE $table SET
  3918. $expiryDateCondition
  3919. $endOnCondition
  3920. add_to_calendar = $agendaId,
  3921. enable_qualification = '".$qualification."'
  3922. WHERE
  3923. publication_id = $workId AND
  3924. c_id = $course_id AND
  3925. iid = ".$data['iid'];
  3926. Database::query($sql);
  3927. }
  3928. if (!empty($params['category_id'])) {
  3929. $link_info = GradebookUtils::isResourceInCourseGradebook(
  3930. $courseInfo['code'],
  3931. LINK_STUDENTPUBLICATION,
  3932. $workId,
  3933. api_get_session_id()
  3934. );
  3935. $linkId = null;
  3936. if (!empty($link_info)) {
  3937. $linkId = $link_info['id'];
  3938. }
  3939. if (isset($params['make_calification']) &&
  3940. $params['make_calification'] == 1
  3941. ) {
  3942. if (empty($linkId)) {
  3943. GradebookUtils::add_resource_to_course_gradebook(
  3944. $params['category_id'],
  3945. $courseInfo['code'],
  3946. LINK_STUDENTPUBLICATION,
  3947. $workId,
  3948. $params['new_dir'],
  3949. api_float_val($params['weight']),
  3950. api_float_val($params['qualification']),
  3951. $params['description'],
  3952. 1,
  3953. api_get_session_id()
  3954. );
  3955. } else {
  3956. GradebookUtils::updateResourceFromCourseGradebook(
  3957. $linkId,
  3958. $courseInfo['code'],
  3959. $params['weight']
  3960. );
  3961. }
  3962. } else {
  3963. // Delete everything of the gradebook for this $linkId
  3964. GradebookUtils::remove_resource_from_course_gradebook($linkId);
  3965. }
  3966. }
  3967. }
  3968. /**
  3969. * Delete all work by student.
  3970. *
  3971. * @param int $userId
  3972. * @param array $courseInfo
  3973. *
  3974. * @return array return deleted items
  3975. */
  3976. function deleteAllWorkPerUser($userId, $courseInfo)
  3977. {
  3978. $deletedItems = [];
  3979. $workPerUser = getWorkPerUser($userId);
  3980. if (!empty($workPerUser)) {
  3981. foreach ($workPerUser as $work) {
  3982. $work = $work['work'];
  3983. foreach ($work->user_results as $userResult) {
  3984. $result = deleteWorkItem($userResult['id'], $courseInfo);
  3985. if ($result) {
  3986. $deletedItems[] = $userResult;
  3987. }
  3988. }
  3989. }
  3990. }
  3991. return $deletedItems;
  3992. }
  3993. /**
  3994. * @param int $item_id
  3995. * @param array course info
  3996. *
  3997. * @return bool
  3998. */
  3999. function deleteWorkItem($item_id, $courseInfo)
  4000. {
  4001. $item_id = (int) $item_id;
  4002. if (empty($item_id) || empty($courseInfo)) {
  4003. return false;
  4004. }
  4005. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  4006. $TSTDPUBASG = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  4007. $currentCourseRepositorySys = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/';
  4008. $is_allowed_to_edit = api_is_allowed_to_edit();
  4009. $file_deleted = false;
  4010. $is_author = user_is_author($item_id);
  4011. $work_data = get_work_data_by_id($item_id);
  4012. $locked = api_resource_is_locked_by_gradebook($work_data['parent_id'], LINK_STUDENTPUBLICATION);
  4013. $course_id = $courseInfo['real_id'];
  4014. if (($is_allowed_to_edit && $locked == false) ||
  4015. (
  4016. $locked == false &&
  4017. $is_author &&
  4018. api_get_course_setting('student_delete_own_publication') == 1 &&
  4019. $work_data['qualificator_id'] == 0
  4020. )
  4021. ) {
  4022. // We found the current user is the author
  4023. $sql = "SELECT url, contains_file, user_id, session_id, parent_id
  4024. FROM $work_table
  4025. WHERE c_id = $course_id AND id = $item_id";
  4026. $result = Database::query($sql);
  4027. $row = Database::fetch_array($result);
  4028. $count = Database::num_rows($result);
  4029. if ($count > 0) {
  4030. // If the "considered_working_time" option is enabled, check
  4031. // whether some time should be removed from track_e_course_access
  4032. $consideredWorkingTime = api_get_configuration_value('considered_working_time');
  4033. if ($consideredWorkingTime) {
  4034. $userWorks = get_work_user_list(
  4035. 0,
  4036. 100,
  4037. null,
  4038. null,
  4039. $row['parent_id'],
  4040. null,
  4041. $row['user_id'],
  4042. false,
  4043. $course_id,
  4044. $row['session_id']
  4045. );
  4046. // We're only interested in deleting the time if this is the latest work sent
  4047. if (count($userWorks) == 1) {
  4048. // Get the "considered work time" defined for this work
  4049. $fieldValue = new ExtraFieldValue('work');
  4050. $resultExtra = $fieldValue->getAllValuesForAnItem(
  4051. $row['parent_id'],
  4052. true
  4053. );
  4054. $workingTime = null;
  4055. foreach ($resultExtra as $field) {
  4056. $field = $field['value'];
  4057. if ($consideredWorkingTime == $field->getField()->getVariable()) {
  4058. $workingTime = $field->getValue();
  4059. }
  4060. }
  4061. // If no time was defined, or a time of "0" was set, do nothing
  4062. if (!empty($workingTime)) {
  4063. $sessionId = empty($row['session_id']) ? 0 : $row['session_id'];
  4064. // Getting false from the following call would mean the
  4065. // time record
  4066. Event::eventRemoveVirtualCourseTime(
  4067. $course_id,
  4068. $row['user_id'],
  4069. $sessionId,
  4070. $workingTime
  4071. );
  4072. }
  4073. }
  4074. } // end of considered_working_time check section
  4075. $sql = "UPDATE $work_table SET active = 2
  4076. WHERE c_id = $course_id AND id = $item_id";
  4077. Database::query($sql);
  4078. $sql = "DELETE FROM $TSTDPUBASG
  4079. WHERE c_id = $course_id AND publication_id = $item_id";
  4080. Database::query($sql);
  4081. Compilatio::plagiarismDeleteDoc($course_id, $item_id);
  4082. api_item_property_update(
  4083. $courseInfo,
  4084. 'work',
  4085. $item_id,
  4086. 'DocumentDeleted',
  4087. api_get_user_id()
  4088. );
  4089. Event::addEvent(
  4090. LOG_WORK_FILE_DELETE,
  4091. LOG_WORK_DATA,
  4092. [
  4093. 'id' => $work_data['id'],
  4094. 'url' => $work_data['url'],
  4095. 'title' => $work_data['title'],
  4096. ],
  4097. null,
  4098. api_get_user_id(),
  4099. api_get_course_int_id(),
  4100. api_get_session_id()
  4101. );
  4102. $work = $row['url'];
  4103. if ($row['contains_file'] == 1) {
  4104. if (!empty($work)) {
  4105. if (api_get_setting('permanently_remove_deleted_files') === 'true') {
  4106. my_delete($currentCourseRepositorySys.'/'.$work);
  4107. $file_deleted = true;
  4108. } else {
  4109. $extension = pathinfo($work, PATHINFO_EXTENSION);
  4110. $new_dir = $work.'_DELETED_'.$item_id.'.'.$extension;
  4111. if (file_exists($currentCourseRepositorySys.'/'.$work)) {
  4112. rename($currentCourseRepositorySys.'/'.$work, $currentCourseRepositorySys.'/'.$new_dir);
  4113. $file_deleted = true;
  4114. }
  4115. }
  4116. }
  4117. } else {
  4118. $file_deleted = true;
  4119. }
  4120. }
  4121. }
  4122. return $file_deleted;
  4123. }
  4124. /**
  4125. * @param FormValidator $form
  4126. * @param array $defaults
  4127. * @param int $workId
  4128. *
  4129. * @return FormValidator
  4130. */
  4131. function getFormWork($form, $defaults = [], $workId = 0)
  4132. {
  4133. $sessionId = api_get_session_id();
  4134. if (!empty($defaults)) {
  4135. if (isset($defaults['submit'])) {
  4136. unset($defaults['submit']);
  4137. }
  4138. }
  4139. // Create the form that asks for the directory name
  4140. $form->addText('new_dir', get_lang('Assignment name'));
  4141. $form->addHtmlEditor(
  4142. 'description',
  4143. get_lang('Description'),
  4144. false,
  4145. false,
  4146. getWorkDescriptionToolbar()
  4147. );
  4148. $form->addButtonAdvancedSettings('advanced_params', get_lang('Advanced settings'));
  4149. if (!empty($defaults) && (isset($defaults['enableEndDate']) || isset($defaults['enableExpiryDate']))) {
  4150. $form->addHtml('<div id="advanced_params_options" style="display:block">');
  4151. } else {
  4152. $form->addHtml('<div id="advanced_params_options" style="display:none">');
  4153. }
  4154. // ScoreOfAssignment
  4155. $form->addElement('text', 'qualification', get_lang('ScoreNumeric'));
  4156. if (($sessionId != 0 && Gradebook::is_active()) || $sessionId == 0) {
  4157. $form->addElement(
  4158. 'checkbox',
  4159. 'make_calification',
  4160. null,
  4161. get_lang('Add to gradebook'),
  4162. [
  4163. 'id' => 'make_calification_id',
  4164. 'onclick' => "javascript: if(this.checked) { document.getElementById('option1').style.display='block';}else{document.getElementById('option1').style.display='none';}",
  4165. ]
  4166. );
  4167. } else {
  4168. // QualificationOfAssignment
  4169. $form->addElement('hidden', 'make_calification', false);
  4170. }
  4171. if (!empty($defaults) && isset($defaults['category_id'])) {
  4172. $form->addHtml('<div id=\'option1\' style="display:block">');
  4173. } else {
  4174. $form->addHtml('<div id=\'option1\' style="display:none">');
  4175. }
  4176. // Loading Gradebook select
  4177. GradebookUtils::load_gradebook_select_in_tool($form);
  4178. $form->addElement('text', 'weight', get_lang('Weight inside assessment'));
  4179. $form->addHtml('</div>');
  4180. $form->addElement('checkbox', 'enableExpiryDate', null, get_lang('Enable handing over deadline (visible to learners)'), 'id="expiry_date"');
  4181. if (isset($defaults['enableExpiryDate']) && $defaults['enableExpiryDate']) {
  4182. $form->addHtml('<div id="option2" style="display: block;">');
  4183. } else {
  4184. $form->addHtml('<div id="option2" style="display: none;">');
  4185. }
  4186. $timeNextWeek = time() + 86400 * 7;
  4187. $nextWeek = substr(api_get_local_time($timeNextWeek), 0, 10);
  4188. if (!isset($defaults['expires_on'])) {
  4189. $date = substr($nextWeek, 0, 10);
  4190. $defaults['expires_on'] = $date.' 23:59';
  4191. }
  4192. $form->addElement('date_time_picker', 'expires_on', get_lang('Posted sending deadline'));
  4193. $form->addHtml('</div>');
  4194. $form->addElement('checkbox', 'enableEndDate', null, get_lang('Enable final acceptance date (invisible to learners)'), 'id="end_date"');
  4195. if (!isset($defaults['ends_on'])) {
  4196. $nextDay = substr(api_get_local_time($timeNextWeek + 86400), 0, 10);
  4197. $date = substr($nextDay, 0, 10);
  4198. $defaults['ends_on'] = $date.' 23:59';
  4199. }
  4200. if (isset($defaults['enableEndDate']) && $defaults['enableEndDate']) {
  4201. $form->addHtml('<div id="option3" style="display: block;">');
  4202. } else {
  4203. $form->addHtml('<div id="option3" style="display: none;">');
  4204. }
  4205. $form->addElement('date_time_picker', 'ends_on', get_lang('Ends at (completely closed)'));
  4206. $form->addHtml('</div>');
  4207. $form->addElement('checkbox', 'add_to_calendar', null, get_lang('Add to calendar'));
  4208. $form->addElement('select', 'allow_text_assignment', get_lang('Document type'), getUploadDocumentType());
  4209. // Extra fields
  4210. $extraField = new ExtraField('work');
  4211. $extra = $extraField->addElements($form, $workId);
  4212. $htmlHeadXtra[] = '
  4213. <script>
  4214. $(function() {
  4215. '.$extra['jquery_ready_content'].'
  4216. });
  4217. </script>';
  4218. $form->addHtml('</div>');
  4219. $skillList = Skill::addSkillsToForm($form, ITEM_TYPE_STUDENT_PUBLICATION, $workId);
  4220. if (!empty($defaults)) {
  4221. $defaults['skills'] = array_keys($skillList);
  4222. $form->setDefaults($defaults);
  4223. }
  4224. return $form;
  4225. }
  4226. /**
  4227. * @return array
  4228. */
  4229. function getUploadDocumentType()
  4230. {
  4231. return [
  4232. 0 => get_lang('Allow files or online text'),
  4233. 1 => get_lang('Allow only text'),
  4234. 2 => get_lang('Allow only files'),
  4235. ];
  4236. }
  4237. /**
  4238. * @param int $itemId
  4239. * @param array $course_info
  4240. *
  4241. * @return bool
  4242. */
  4243. function makeVisible($itemId, $course_info)
  4244. {
  4245. $itemId = (int) $itemId;
  4246. if (empty($course_info) || empty($itemId)) {
  4247. return false;
  4248. }
  4249. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  4250. $course_id = $course_info['real_id'];
  4251. $sql = "UPDATE $work_table SET accepted = 1
  4252. WHERE c_id = $course_id AND id = $itemId";
  4253. Database::query($sql);
  4254. api_item_property_update($course_info, 'work', $itemId, 'visible', api_get_user_id());
  4255. return true;
  4256. }
  4257. /**
  4258. * @param int $itemId
  4259. * @param array $course_info
  4260. *
  4261. * @return int
  4262. */
  4263. function makeInvisible($itemId, $course_info)
  4264. {
  4265. $itemId = (int) $itemId;
  4266. if (empty($course_info) || empty($itemId)) {
  4267. return false;
  4268. }
  4269. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  4270. $course_id = $course_info['real_id'];
  4271. $sql = "UPDATE $table
  4272. SET accepted = 0
  4273. WHERE c_id = $course_id AND id = '".$itemId."'";
  4274. Database::query($sql);
  4275. api_item_property_update(
  4276. $course_info,
  4277. 'work',
  4278. $itemId,
  4279. 'invisible',
  4280. api_get_user_id()
  4281. );
  4282. return true;
  4283. }
  4284. /**
  4285. * @param int $item_id
  4286. * @param string $path
  4287. * @param array $courseInfo
  4288. * @param int $groupId iid
  4289. * @param int $sessionId
  4290. *
  4291. * @return string
  4292. */
  4293. function generateMoveForm($item_id, $path, $courseInfo, $groupId, $sessionId)
  4294. {
  4295. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  4296. $courseId = $courseInfo['real_id'];
  4297. $folders = [];
  4298. $session_id = (int) $sessionId;
  4299. $groupId = (int) $groupId;
  4300. $sessionCondition = empty($sessionId) ? ' AND (session_id = 0 OR session_id IS NULL) ' : " AND session_id='".$session_id."'";
  4301. $groupIid = 0;
  4302. if ($groupId) {
  4303. $groupInfo = GroupManager::get_group_properties($groupId);
  4304. $groupIid = $groupInfo['iid'];
  4305. }
  4306. $sql = "SELECT id, url, title
  4307. FROM $work_table
  4308. WHERE
  4309. c_id = $courseId AND
  4310. active IN (0, 1) AND
  4311. url LIKE '/%' AND
  4312. post_group_id = $groupIid
  4313. $sessionCondition";
  4314. $res = Database::query($sql);
  4315. while ($folder = Database::fetch_array($res)) {
  4316. $title = empty($folder['title']) ? basename($folder['url']) : $folder['title'];
  4317. $folders[$folder['id']] = $title;
  4318. }
  4319. return build_work_move_to_selector($folders, $path, $item_id);
  4320. }
  4321. /**
  4322. * @param int $workId
  4323. *
  4324. * @return string
  4325. */
  4326. function showStudentList($workId)
  4327. {
  4328. $columnModel = [
  4329. [
  4330. 'name' => 'student',
  4331. 'index' => 'student',
  4332. 'width' => '350px',
  4333. 'align' => 'left',
  4334. 'sortable' => 'false',
  4335. ],
  4336. [
  4337. 'name' => 'works',
  4338. 'index' => 'works',
  4339. 'align' => 'center',
  4340. 'sortable' => 'false',
  4341. ],
  4342. ];
  4343. $token = null;
  4344. $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_work_student_list_overview&work_id='.$workId.'&'.api_get_cidreq();
  4345. $columns = [
  4346. get_lang('Learners'),
  4347. get_lang('Assignments'),
  4348. ];
  4349. $order = api_is_western_name_order() ? 'firstname' : 'lastname';
  4350. $params = [
  4351. 'autowidth' => 'true',
  4352. 'height' => 'auto',
  4353. 'rowNum' => 5,
  4354. 'sortname' => $order,
  4355. 'sortorder' => 'asc',
  4356. ];
  4357. $html = '<script>
  4358. $(function() {
  4359. '.Display::grid_js('studentList', $url, $columns, $columnModel, $params, [], null, true).'
  4360. $("#workList").jqGrid(
  4361. "navGrid",
  4362. "#studentList_pager",
  4363. { edit: false, add: false, del: false },
  4364. { height:280, reloadAfterSubmit:false }, // edit options
  4365. { height:280, reloadAfterSubmit:false }, // add options
  4366. { width:500 } // search options
  4367. );
  4368. });
  4369. </script>';
  4370. $html .= Display::grid_html('studentList');
  4371. return $html;
  4372. }
  4373. /**
  4374. * @param string $courseCode
  4375. * @param int $sessionId
  4376. * @param int $groupId
  4377. * @param int $start
  4378. * @param int $limit
  4379. * @param string $sidx
  4380. * @param string $sord
  4381. * @param $getCount
  4382. *
  4383. * @return array|int
  4384. */
  4385. function getWorkUserList($courseCode, $sessionId, $groupId, $start, $limit, $sidx, $sord, $getCount = false)
  4386. {
  4387. if (!empty($groupId)) {
  4388. $userList = GroupManager::get_users(
  4389. $groupId,
  4390. false,
  4391. $start,
  4392. $limit,
  4393. $getCount,
  4394. null,
  4395. $sidx,
  4396. $sord
  4397. );
  4398. } else {
  4399. $limitString = null;
  4400. if (!empty($start) && !empty($limit)) {
  4401. $start = intval($start);
  4402. $limit = intval($limit);
  4403. $limitString = " LIMIT $start, $limit";
  4404. }
  4405. $orderBy = null;
  4406. if (!empty($sidx) && !empty($sord)) {
  4407. if (in_array($sidx, ['firstname', 'lastname'])) {
  4408. $orderBy = "ORDER BY $sidx $sord";
  4409. }
  4410. }
  4411. if (empty($sessionId)) {
  4412. $userList = CourseManager::get_user_list_from_course_code(
  4413. $courseCode,
  4414. $sessionId,
  4415. $limitString,
  4416. $orderBy,
  4417. STUDENT,
  4418. $getCount
  4419. );
  4420. } else {
  4421. $userList = CourseManager::get_user_list_from_course_code(
  4422. $courseCode,
  4423. $sessionId,
  4424. $limitString,
  4425. $orderBy,
  4426. 0,
  4427. $getCount
  4428. );
  4429. }
  4430. if ($getCount == false) {
  4431. $userList = array_keys($userList);
  4432. }
  4433. }
  4434. return $userList;
  4435. }
  4436. /**
  4437. * @param int $workId
  4438. * @param string $courseCode
  4439. * @param int $sessionId
  4440. * @param int $groupId
  4441. * @param int $start
  4442. * @param int $limit
  4443. * @param int $sidx
  4444. * @param string $sord
  4445. * @param bool $getCount
  4446. *
  4447. * @return array|int
  4448. */
  4449. function getWorkUserListData(
  4450. $workId,
  4451. $courseCode,
  4452. $sessionId,
  4453. $groupId,
  4454. $start,
  4455. $limit,
  4456. $sidx,
  4457. $sord,
  4458. $getCount = false
  4459. ) {
  4460. $my_folder_data = get_work_data_by_id($workId);
  4461. $workParents = [];
  4462. if (empty($my_folder_data)) {
  4463. $workParents = getWorkList($workId, $my_folder_data, null);
  4464. }
  4465. $workIdList = [];
  4466. if (!empty($workParents)) {
  4467. foreach ($workParents as $work) {
  4468. $workIdList[] = $work->id;
  4469. }
  4470. }
  4471. $courseInfo = api_get_course_info($courseCode);
  4472. $userList = getWorkUserList(
  4473. $courseCode,
  4474. $sessionId,
  4475. $groupId,
  4476. $start,
  4477. $limit,
  4478. $sidx,
  4479. $sord,
  4480. $getCount
  4481. );
  4482. if ($getCount) {
  4483. return $userList;
  4484. }
  4485. $results = [];
  4486. if (!empty($userList)) {
  4487. foreach ($userList as $userId) {
  4488. $user = api_get_user_info($userId);
  4489. $link = api_get_path(WEB_CODE_PATH).'work/student_work.php?'.api_get_cidreq().'&studentId='.$user['user_id'];
  4490. $url = Display::url(api_get_person_name($user['firstname'], $user['lastname']), $link);
  4491. $userWorks = 0;
  4492. if (!empty($workIdList)) {
  4493. $userWorks = getUniqueStudentAttempts(
  4494. $workIdList,
  4495. $groupId,
  4496. $courseInfo['real_id'],
  4497. $sessionId,
  4498. $user['user_id']
  4499. );
  4500. }
  4501. $works = $userWorks." / ".count($workParents);
  4502. $results[] = [
  4503. 'student' => $url,
  4504. 'works' => Display::url($works, $link),
  4505. ];
  4506. }
  4507. }
  4508. return $results;
  4509. }
  4510. /**
  4511. * @param int $id
  4512. * @param array $course_info
  4513. * @param bool $isCorrection
  4514. *
  4515. * @return bool
  4516. */
  4517. function downloadFile($id, $course_info, $isCorrection)
  4518. {
  4519. return getFile($id, $course_info, true, $isCorrection, true);
  4520. }
  4521. /**
  4522. * @param int $id
  4523. * @param array $course_info
  4524. * @param bool $download
  4525. * @param bool $isCorrection
  4526. * @param bool $forceAccessForCourseAdmins
  4527. *
  4528. * @return bool
  4529. */
  4530. function getFile($id, $course_info, $download = true, $isCorrection = false, $forceAccessForCourseAdmins = false)
  4531. {
  4532. $file = getFileContents($id, $course_info, 0, $isCorrection, $forceAccessForCourseAdmins);
  4533. if (!empty($file) && is_array($file)) {
  4534. return DocumentManager::file_send_for_download(
  4535. $file['path'],
  4536. $download,
  4537. $file['title']
  4538. );
  4539. }
  4540. return false;
  4541. }
  4542. /**
  4543. * Get the file contents for an assigment.
  4544. *
  4545. * @param int $id
  4546. * @param array $courseInfo
  4547. * @param int $sessionId
  4548. * @param bool $correction
  4549. * @param bool $forceAccessForCourseAdmins
  4550. *
  4551. * @return array|bool
  4552. */
  4553. function getFileContents($id, $courseInfo, $sessionId = 0, $correction = false, $forceAccessForCourseAdmins = false)
  4554. {
  4555. $id = (int) $id;
  4556. if (empty($courseInfo) || empty($id)) {
  4557. return false;
  4558. }
  4559. if (empty($sessionId)) {
  4560. $sessionId = api_get_session_id();
  4561. }
  4562. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  4563. if (!empty($courseInfo['real_id'])) {
  4564. $sql = "SELECT *
  4565. FROM $table
  4566. WHERE c_id = ".$courseInfo['real_id']." AND id = $id";
  4567. $result = Database::query($sql);
  4568. if ($result && Database::num_rows($result)) {
  4569. $row = Database::fetch_array($result, 'ASSOC');
  4570. if ($correction) {
  4571. $row['url'] = $row['url_correction'];
  4572. }
  4573. if (empty($row['url'])) {
  4574. return false;
  4575. }
  4576. $full_file_name = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/'.$row['url'];
  4577. $item_info = api_get_item_property_info(
  4578. api_get_course_int_id(),
  4579. 'work',
  4580. $row['id'],
  4581. $sessionId
  4582. );
  4583. if (empty($item_info)) {
  4584. return false;
  4585. }
  4586. $isAllow = allowOnlySubscribedUser(
  4587. api_get_user_id(),
  4588. $row['parent_id'],
  4589. $courseInfo['real_id'],
  4590. $forceAccessForCourseAdmins
  4591. );
  4592. if (empty($isAllow)) {
  4593. return false;
  4594. }
  4595. /*
  4596. field show_score in table course :
  4597. 0 => New documents are visible for all users
  4598. 1 => New documents are only visible for the teacher(s)
  4599. field visibility in table item_property :
  4600. 0 => eye closed, invisible for all students
  4601. 1 => eye open
  4602. field accepted in table c_student_publication :
  4603. 0 => eye closed, invisible for all students
  4604. 1 => eye open
  4605. ( We should have visibility == accepted, otherwise there is an
  4606. inconsistency in the Database)
  4607. field value in table c_course_setting :
  4608. 0 => Allow learners to delete their own publications = NO
  4609. 1 => Allow learners to delete their own publications = YES
  4610. +------------------+-------------------------+------------------------+
  4611. |Can download work?| doc visible for all = 0 | doc visible for all = 1|
  4612. +------------------+-------------------------+------------------------+
  4613. | visibility = 0 | editor only | editor only |
  4614. | | | |
  4615. +------------------+-------------------------+------------------------+
  4616. | visibility = 1 | editor | editor |
  4617. | | + owner of the work | + any student |
  4618. +------------------+-------------------------+------------------------+
  4619. (editor = teacher + admin + anybody with right api_is_allowed_to_edit)
  4620. */
  4621. $work_is_visible = $item_info['visibility'] == 1 && $row['accepted'] == 1;
  4622. $doc_visible_for_all = (int) $courseInfo['show_score'] === 0;
  4623. $is_editor = api_is_allowed_to_edit(true, true, true);
  4624. $student_is_owner_of_work = user_is_author($row['id'], api_get_user_id());
  4625. if (($forceAccessForCourseAdmins && $isAllow) ||
  4626. $is_editor ||
  4627. $student_is_owner_of_work ||
  4628. ($doc_visible_for_all && $work_is_visible)
  4629. ) {
  4630. $title = $row['title'];
  4631. if ($correction) {
  4632. $title = $row['title_correction'];
  4633. }
  4634. if (array_key_exists('filename', $row) && !empty($row['filename'])) {
  4635. $title = $row['filename'];
  4636. }
  4637. $title = str_replace(' ', '_', $title);
  4638. if ($correction == false) {
  4639. $userInfo = api_get_user_info($row['user_id']);
  4640. if ($userInfo) {
  4641. $date = api_get_local_time($row['sent_date']);
  4642. $date = str_replace([':', '-', ' '], '_', $date);
  4643. $title = $date.'_'.$userInfo['username'].'_'.$title;
  4644. }
  4645. }
  4646. if (Security::check_abs_path(
  4647. $full_file_name,
  4648. api_get_path(SYS_COURSE_PATH).api_get_course_path().'/'
  4649. )) {
  4650. Event::event_download($title);
  4651. return [
  4652. 'path' => $full_file_name,
  4653. 'title' => $title,
  4654. 'title_correction' => $row['title_correction'],
  4655. ];
  4656. }
  4657. }
  4658. }
  4659. }
  4660. return false;
  4661. }
  4662. /**
  4663. * @param int $userId
  4664. * @param array $courseInfo
  4665. * @param string $format
  4666. *
  4667. * @return bool
  4668. */
  4669. function exportAllWork($userId, $courseInfo, $format = 'pdf')
  4670. {
  4671. $userInfo = api_get_user_info($userId);
  4672. if (empty($userInfo) || empty($courseInfo)) {
  4673. return false;
  4674. }
  4675. $workPerUser = getWorkPerUser($userId);
  4676. switch ($format) {
  4677. case 'pdf':
  4678. if (!empty($workPerUser)) {
  4679. $pdf = new PDF();
  4680. $content = null;
  4681. foreach ($workPerUser as $work) {
  4682. $work = $work['work'];
  4683. foreach ($work->user_results as $userResult) {
  4684. $content .= $userResult['title'];
  4685. // No need to use api_get_local_time()
  4686. $content .= $userResult['sent_date'];
  4687. $content .= $userResult['qualification'];
  4688. $content .= $userResult['description'];
  4689. }
  4690. }
  4691. if (!empty($content)) {
  4692. $pdf->content_to_pdf(
  4693. $content,
  4694. null,
  4695. api_replace_dangerous_char($userInfo['complete_name']),
  4696. $courseInfo['code']
  4697. );
  4698. }
  4699. }
  4700. break;
  4701. }
  4702. }
  4703. /**
  4704. * @param int $workId
  4705. * @param array $courseInfo
  4706. * @param int $sessionId
  4707. * @param string $format
  4708. *
  4709. * @return bool
  4710. */
  4711. function exportAllStudentWorkFromPublication(
  4712. $workId,
  4713. $courseInfo,
  4714. $sessionId,
  4715. $format = 'pdf'
  4716. ) {
  4717. if (empty($courseInfo)) {
  4718. return false;
  4719. }
  4720. $workData = get_work_data_by_id($workId);
  4721. if (empty($workData)) {
  4722. return false;
  4723. }
  4724. $assignment = get_work_assignment_by_id($workId);
  4725. $courseCode = $courseInfo['code'];
  4726. $header = get_lang('Course').': '.$courseInfo['title'];
  4727. $teachers = CourseManager::getTeacherListFromCourseCodeToString(
  4728. $courseCode
  4729. );
  4730. if (!empty($sessionId)) {
  4731. $sessionInfo = api_get_session_info($sessionId);
  4732. if (!empty($sessionInfo)) {
  4733. $header .= ' - '.$sessionInfo['name'];
  4734. $header .= '<br />'.$sessionInfo['description'];
  4735. $teachers = SessionManager::getCoachesByCourseSessionToString(
  4736. $sessionId,
  4737. $courseInfo['real_id']
  4738. );
  4739. }
  4740. }
  4741. $header .= '<br />'.get_lang('Trainers').': '.$teachers.'<br />';
  4742. $header .= '<br />'.get_lang('Date').': '.api_get_local_time().'<br />';
  4743. $header .= '<br />'.get_lang('Assignment name').': '.$workData['title'].'<br />';
  4744. $content = null;
  4745. $expiresOn = null;
  4746. if (!empty($assignment) && isset($assignment['expires_on'])) {
  4747. $content .= '<br /><strong>'.get_lang('Posted deadline for sending the work (Visible to the learner)').'</strong>: '.api_get_local_time($assignment['expires_on']);
  4748. $expiresOn = api_get_local_time($assignment['expires_on']);
  4749. }
  4750. if (!empty($workData['description'])) {
  4751. $content .= '<br /><strong>'.get_lang('Description').'</strong>: '.$workData['description'];
  4752. }
  4753. $workList = get_work_user_list(null, null, null, null, $workId);
  4754. switch ($format) {
  4755. case 'pdf':
  4756. if (!empty($workList)) {
  4757. $table = new HTML_Table(['class' => 'data_table']);
  4758. $headers = [
  4759. get_lang('Name'),
  4760. get_lang('User'),
  4761. get_lang('Deadline'),
  4762. get_lang('Sent date'),
  4763. get_lang('Filename'),
  4764. get_lang('Score'),
  4765. get_lang('Feedback'),
  4766. ];
  4767. $column = 0;
  4768. foreach ($headers as $header) {
  4769. $table->setHeaderContents(0, $column, $header);
  4770. $column++;
  4771. }
  4772. $row = 1;
  4773. //$pdf->set_custom_header($header);
  4774. foreach ($workList as $work) {
  4775. $content .= '<hr />';
  4776. // getWorkComments need c_id
  4777. $work['c_id'] = $courseInfo['real_id'];
  4778. //$content .= get_lang('Date').': '.api_get_local_time($work['sent_date_from_db']).'<br />';
  4779. $score = null;
  4780. if (!empty($work['qualification_only'])) {
  4781. $score = $work['qualification_only'];
  4782. }
  4783. $comments = getWorkComments($work);
  4784. $feedback = null;
  4785. if (!empty($comments)) {
  4786. $content .= '<h4>'.get_lang('Feedback').': </h4>';
  4787. foreach ($comments as $comment) {
  4788. $feedback .= get_lang('User').': '.$comment['complete_name'].
  4789. '<br />';
  4790. $feedback .= $comment['comment'].'<br />';
  4791. }
  4792. }
  4793. $table->setCellContents($row, 0, strip_tags($workData['title']));
  4794. $table->setCellContents($row, 1, strip_tags($work['fullname']));
  4795. $table->setCellContents($row, 2, $expiresOn);
  4796. $table->setCellContents($row, 3, api_get_local_time($work['sent_date_from_db']));
  4797. $table->setCellContents($row, 4, strip_tags($work['title']));
  4798. $table->setCellContents($row, 5, $score);
  4799. $table->setCellContents($row, 6, $feedback);
  4800. $row++;
  4801. }
  4802. $content = $table->toHtml();
  4803. if (!empty($content)) {
  4804. $params = [
  4805. 'filename' => $workData['title'].'_'.api_get_local_time(),
  4806. 'pdf_title' => api_replace_dangerous_char($workData['title']),
  4807. 'course_code' => $courseInfo['code'],
  4808. ];
  4809. $pdf = new PDF('A4', null, $params);
  4810. $pdf->html_to_pdf_with_template($content);
  4811. }
  4812. exit;
  4813. }
  4814. break;
  4815. }
  4816. }
  4817. /**
  4818. * Downloads all user files per user.
  4819. *
  4820. * @param int $userId
  4821. * @param array $courseInfo
  4822. *
  4823. * @return bool
  4824. */
  4825. function downloadAllFilesPerUser($userId, $courseInfo)
  4826. {
  4827. $userInfo = api_get_user_info($userId);
  4828. if (empty($userInfo) || empty($courseInfo)) {
  4829. return false;
  4830. }
  4831. $tempZipFile = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().".zip";
  4832. $coursePath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/work/';
  4833. $zip = new PclZip($tempZipFile);
  4834. $workPerUser = getWorkPerUser($userId);
  4835. if (!empty($workPerUser)) {
  4836. $files = [];
  4837. foreach ($workPerUser as $work) {
  4838. $work = $work['work'];
  4839. foreach ($work->user_results as $userResult) {
  4840. if (empty($userResult['url']) || empty($userResult['contains_file'])) {
  4841. continue;
  4842. }
  4843. $data = getFileContents($userResult['id'], $courseInfo);
  4844. if (!empty($data) && isset($data['path'])) {
  4845. $files[basename($data['path'])] = [
  4846. 'title' => $data['title'],
  4847. 'path' => $data['path'],
  4848. ];
  4849. }
  4850. }
  4851. }
  4852. if (!empty($files)) {
  4853. Session::write('files', $files);
  4854. foreach ($files as $data) {
  4855. $zip->add(
  4856. $data['path'],
  4857. PCLZIP_OPT_REMOVE_PATH,
  4858. $coursePath,
  4859. PCLZIP_CB_PRE_ADD,
  4860. 'preAddAllWorkStudentCallback'
  4861. );
  4862. }
  4863. }
  4864. // Start download of created file
  4865. $name = basename(api_replace_dangerous_char($userInfo['complete_name'])).'.zip';
  4866. Event::event_download($name.'.zip (folder)');
  4867. if (Security::check_abs_path($tempZipFile, api_get_path(SYS_ARCHIVE_PATH))) {
  4868. DocumentManager::file_send_for_download($tempZipFile, true, $name);
  4869. @unlink($tempZipFile);
  4870. exit;
  4871. }
  4872. }
  4873. exit;
  4874. }
  4875. /**
  4876. * @param $p_event
  4877. * @param array $p_header
  4878. *
  4879. * @return int
  4880. */
  4881. function preAddAllWorkStudentCallback($p_event, &$p_header)
  4882. {
  4883. $files = Session::read('files');
  4884. if (isset($files[basename($p_header['stored_filename'])])) {
  4885. $p_header['stored_filename'] = $files[basename($p_header['stored_filename'])]['title'];
  4886. return 1;
  4887. }
  4888. return 0;
  4889. }
  4890. /**
  4891. * Get all work created by a user.
  4892. *
  4893. * @param int $user_id
  4894. * @param int $courseId
  4895. * @param int $sessionId
  4896. *
  4897. * @return array
  4898. */
  4899. function getWorkCreatedByUser($user_id, $courseId, $sessionId)
  4900. {
  4901. $items = api_get_item_property_list_by_tool_by_user(
  4902. $user_id,
  4903. 'work',
  4904. $courseId,
  4905. $sessionId
  4906. );
  4907. $list = [];
  4908. if (!empty($items)) {
  4909. foreach ($items as $work) {
  4910. $item = get_work_data_by_id(
  4911. $work['ref'],
  4912. $courseId,
  4913. $sessionId
  4914. );
  4915. if (!empty($item)) {
  4916. $list[] = [
  4917. $item['title'],
  4918. api_get_local_time($work['insert_date']),
  4919. api_get_local_time($work['lastedit_date']),
  4920. ];
  4921. }
  4922. }
  4923. }
  4924. return $list;
  4925. }
  4926. /**
  4927. * @param array $courseInfo
  4928. * @param int $workId
  4929. *
  4930. * @return bool
  4931. */
  4932. function protectWork($courseInfo, $workId)
  4933. {
  4934. $userId = api_get_user_id();
  4935. $groupId = api_get_group_id();
  4936. $sessionId = api_get_session_id();
  4937. $workData = get_work_data_by_id($workId);
  4938. if (empty($workData) || empty($courseInfo)) {
  4939. api_not_allowed(true);
  4940. }
  4941. if (api_is_platform_admin() || api_is_allowed_to_edit()) {
  4942. return true;
  4943. }
  4944. $workId = $workData['id'];
  4945. if ($workData['active'] != 1) {
  4946. api_not_allowed(true);
  4947. }
  4948. $visibility = api_get_item_visibility($courseInfo, 'work', $workId, $sessionId);
  4949. if ($visibility != 1) {
  4950. api_not_allowed(true);
  4951. }
  4952. $isAllow = allowOnlySubscribedUser($userId, $workId, $courseInfo['real_id']);
  4953. if (empty($isAllow)) {
  4954. api_not_allowed(true);
  4955. }
  4956. $groupInfo = GroupManager::get_group_properties($groupId);
  4957. if (!empty($groupId)) {
  4958. $showWork = GroupManager::user_has_access(
  4959. $userId,
  4960. $groupInfo['iid'],
  4961. GroupManager::GROUP_TOOL_WORK
  4962. );
  4963. if (!$showWork) {
  4964. api_not_allowed(true);
  4965. }
  4966. }
  4967. }
  4968. /**
  4969. * @param array $courseInfo
  4970. * @param array $work
  4971. */
  4972. function deleteCorrection($courseInfo, $work)
  4973. {
  4974. if (isset($work['url_correction']) && !empty($work['url_correction']) && isset($work['iid'])) {
  4975. $id = $work['iid'];
  4976. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  4977. $sql = "UPDATE $table SET
  4978. url_correction = '',
  4979. title_correction = ''
  4980. WHERE iid = $id";
  4981. Database::query($sql);
  4982. $coursePath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/';
  4983. if (file_exists($coursePath.$work['url_correction'])) {
  4984. if (Security::check_abs_path($coursePath.$work['url_correction'], $coursePath)) {
  4985. unlink($coursePath.$work['url_correction']);
  4986. }
  4987. }
  4988. }
  4989. }
  4990. /**
  4991. * @param int $workId
  4992. *
  4993. * @return string
  4994. */
  4995. function workGetExtraFieldData($workId)
  4996. {
  4997. $sessionField = new ExtraField('work');
  4998. $extraFieldData = $sessionField->getDataAndFormattedValues($workId);
  4999. $result = '';
  5000. if (!empty($extraFieldData)) {
  5001. $result .= '<div class="well">';
  5002. foreach ($extraFieldData as $data) {
  5003. $result .= $data['text'].': <b>'.$data['value'].'</b>';
  5004. }
  5005. $result .= '</div>';
  5006. }
  5007. return $result;
  5008. }