1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515 |
- <?php
- /* For licensing terms, see /license.txt */
- use ChamiloSession as Session;
- use Chamilo\CourseBundle\Entity\CStudentPublication;
- /**
- * @package chamilo.work
- * @author Thomas, Hugues, Christophe - original version
- * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University -
- * ability for course admins to specify wether uploaded documents are visible or invisible by default.
- * @author Roan Embrechts, code refactoring and virtual course support
- * @author Frederic Vauthier, directories management
- * @author Julio Montoya <gugli100@gmail.com> BeezNest 2011 LOTS of bug fixes
- * @todo this lib should be convert in a static class and moved to main/inc/lib
- */
- /**
- * Displays action links (for admins, authorized groups members and authorized students)
- * @param string Current dir
- * @param integer Whether to show tool options
- * @param integer Whether to show upload form option
- * @return void
- */
- function display_action_links($id, $cur_dir_path, $action)
- {
- global $gradebook;
- $id = $my_back_id = intval($id);
- if ($action == 'list') {
- $my_back_id = 0;
- }
- $display_output = '';
- $origin = api_get_origin();
- if (!empty($id)) {
- $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&gradebook='.$gradebook.'&id='.$my_back_id.'">'.
- Display::return_icon('back.png', get_lang('BackToWorksList'), '', ICON_SIZE_MEDIUM).'</a>';
- }
- if (api_is_allowed_to_edit(null, true) && $origin != 'learnpath') {
- // Create dir
- if (empty($id)) {
- $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=create_dir&gradebook='.$gradebook.'">';
- $display_output .= Display::return_icon('new_work.png', get_lang('CreateAssignment'), '', ICON_SIZE_MEDIUM).'</a>';
- }
- if (empty($id)) {
- // Options
- $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=settings&gradebook='.$gradebook.'">';
- $display_output .= Display::return_icon('settings.png', get_lang('EditToolOptions'), '', ICON_SIZE_MEDIUM).'</a>';
- }
- $display_output .= '<a id="open-view-list" href="#">'.Display::return_icon('listwork.png', get_lang('ViewStudents'), '', ICON_SIZE_MEDIUM).'</a>';
- }
- if (api_is_allowed_to_edit(null, true) && $origin != 'learnpath' && api_is_allowed_to_session_edit(false, true)) {
- // Delete all files
- if (api_get_setting('permanently_remove_deleted_files') == 'true') {
- $message = get_lang('ConfirmYourChoiceDeleteAllfiles');
- } else {
- $message = get_lang('ConfirmYourChoice');
- }
- }
- if ($display_output != '') {
- echo '<div class="actions">';
- echo $display_output;
- echo '</div>';
- }
- }
- /**
- * Returns a form displaying all options for this tool.
- * These are
- * - make all files visible / invisible
- * - set the default visibility of uploaded files
- * @param $defaults
- * @return string The HTML form
- */
- function settingsForm($defaults)
- {
- $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
- if (!$is_allowed_to_edit) {
- return;
- }
- $url = api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq().'&action=settings';
- $form = new FormValidator('edit_settings', 'post', $url);
- $form->addElement('hidden', 'changeProperties', 1);
- $form->addElement('header', get_lang('EditToolOptions'));
- $group = array(
- $form->createElement('radio', 'show_score', null, get_lang('NewVisible'), 0),
- $form->createElement('radio', 'show_score', null, get_lang('NewUnvisible'), 1)
- );
- $form->addGroup($group, '', get_lang('DefaultUpload'));
- $group = array(
- $form->createElement('radio', 'student_delete_own_publication', null, get_lang('Yes'), 1),
- $form->createElement('radio', 'student_delete_own_publication', null, get_lang('No'), 0)
- );
- $form->addGroup($group, '', get_lang('StudentAllowedToDeleteOwnPublication'));
- $form->addButtonSave(get_lang('Save'));
- $form->setDefaults($defaults);
- return $form->returnForm();
- }
- /**
- * @param string $path
- * @param int $courseId
- *
- * @return array
- */
- function get_work_data_by_path($path, $courseId = null)
- {
- $path = Database::escape_string($path);
- if (empty($courseId)) {
- $courseId = api_get_course_int_id();
- } else {
- $courseId = intval($courseId);
- }
- $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $sql = "SELECT * FROM ".$work_table."
- WHERE url = '$path' AND c_id = $courseId ";
- $result = Database::query($sql);
- $return = array();
- if (Database::num_rows($result)) {
- $return = Database::fetch_array($result, 'ASSOC');
- }
- return $return;
- }
- /**
- * @param int $id
- * @param int $courseId
- * @param int $sessionId
- * @return array
- */
- function get_work_data_by_id($id, $courseId = null, $sessionId = null)
- {
- $id = intval($id);
- if (!empty($courseId)) {
- $courseId = intval($courseId);
- } else {
- $courseId = api_get_course_int_id();
- }
- $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $sessionCondition = null;
- if (!empty($sessionId)) {
- $sessionCondition = api_get_session_condition($sessionId, true);
- }
- $sql = "SELECT * FROM $table
- WHERE
- id = $id AND c_id = $courseId
- $sessionCondition";
- $result = Database::query($sql);
- $work = array();
- if (Database::num_rows($result)) {
- $work = Database::fetch_array($result, 'ASSOC');
- if (empty($work['title'])) {
- $work['title'] = basename($work['url']);
- }
- $work['download_url'] = api_get_path(WEB_CODE_PATH).'work/download.php?id='.$work['id'].'&'.api_get_cidreq();
- $work['view_url'] = api_get_path(WEB_CODE_PATH).'work/view.php?id='.$work['id'].'&'.api_get_cidreq();
- $work['show_url'] = api_get_path(WEB_CODE_PATH).'work/show_file.php?id='.$work['id'].'&'.api_get_cidreq();
- $work['show_content'] = '';
- if ($work['contains_file']) {
- $fileInfo = pathinfo($work['title']);
- if (is_array($fileInfo) &&
- !empty($fileInfo['extension']) &&
- in_array($fileInfo['extension'], array('jpg', 'png', 'gif'))
- ) {
- $work['show_content'] = '<img src="'.$work['show_url'].'"/>';
- }
- }
- $fieldValue = new ExtraFieldValue('work');
- $work['extra'] = $fieldValue->getAllValuesForAnItem(
- $id,
- true
- );
- }
- return $work;
- }
- /**
- * @param int $user_id
- * @param int $work_id
- *
- * @return int
- */
- function get_work_count_by_student($user_id, $work_id)
- {
- $user_id = intval($user_id);
- $work_id = intval($work_id);
- $course_id = api_get_course_int_id();
- $session_id = api_get_session_id();
- $sessionCondition = api_get_session_condition($session_id);
- $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $sql = "SELECT COUNT(*) as count
- FROM $table
- WHERE
- c_id = $course_id AND
- parent_id = $work_id AND
- user_id = $user_id AND
- active IN (0, 1)
- $sessionCondition";
- $result = Database::query($sql);
- $return = 0;
- if (Database::num_rows($result)) {
- $return = Database::fetch_row($result, 'ASSOC');
- $return = intval($return[0]);
- }
- return $return;
- }
- /**
- * @param int $id
- * @param int $courseId
- *
- * @return array
- */
- function get_work_assignment_by_id($id, $courseId = null)
- {
- if (empty($courseId)) {
- $courseId = api_get_course_int_id();
- } else {
- $courseId = intval($courseId);
- }
- $id = intval($id);
- $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
- $sql = "SELECT * FROM $table
- WHERE c_id = $courseId AND publication_id = $id";
- $result = Database::query($sql);
- $return = array();
- if (Database::num_rows($result)) {
- $return = Database::fetch_array($result, 'ASSOC');
- }
- return $return;
- }
- /**
- * @param int $id
- * @param array $my_folder_data
- * @param string $add_in_where_query
- * @param int $course_id
- * @param int $session_id
- *
- * @return array
- */
- function getWorkList($id, $my_folder_data, $add_in_where_query = null, $course_id = 0, $session_id = 0)
- {
- $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $course_id = $course_id ? $course_id : api_get_course_int_id();
- $session_id = $session_id ? $session_id : api_get_session_id();
- $condition_session = api_get_session_condition($session_id);
- $group_id = api_get_group_id();
- $groupIid = 0;
- if ($group_id) {
- $groupInfo = GroupManager::get_group_properties($group_id);
- $groupIid = $groupInfo['iid'];
- }
- $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
- $linkInfo = GradebookUtils::isResourceInCourseGradebook(
- api_get_course_id(),
- 3,
- $id,
- api_get_session_id()
- );
- if ($linkInfo) {
- $workInGradeBookLinkId = $linkInfo['id'];
- if ($workInGradeBookLinkId) {
- if ($is_allowed_to_edit) {
- if (intval($my_folder_data['qualification']) == 0) {
- echo Display::return_message(
- get_lang('MaxWeightNeedToBeProvided'),
- 'warning'
- );
- }
- }
- }
- }
- $contains_file_query = '';
- // Get list from database
- if ($is_allowed_to_edit) {
- $active_condition = ' active IN (0, 1)';
- $sql = "SELECT * FROM $work_table
- WHERE
- c_id = $course_id
- $add_in_where_query
- $condition_session AND
- $active_condition AND
- (parent_id = 0)
- $contains_file_query AND
- post_group_id = $groupIid
- ORDER BY sent_date DESC";
- } else {
- if (!empty($group_id)) {
- // set to select only messages posted by the user's group
- $group_query = " WHERE c_id = $course_id AND post_group_id = $groupIid";
- $subdirs_query = " AND parent_id = 0";
- } else {
- $group_query = " WHERE c_id = $course_id AND (post_group_id = '0' OR post_group_id is NULL) ";
- $subdirs_query = " AND parent_id = 0";
- }
- //@todo how we can active or not an assignment?
- $active_condition = ' AND active IN (1, 0)';
- $sql = "SELECT * FROM $work_table
- $group_query
- $subdirs_query
- $add_in_where_query
- $active_condition
- $condition_session
- ORDER BY title";
- }
- $work_parents = array();
- $sql_result = Database::query($sql);
- if (Database::num_rows($sql_result)) {
- while ($work = Database::fetch_object($sql_result)) {
- if ($work->parent_id == 0) {
- $work_parents[] = $work;
- }
- }
- }
- return $work_parents;
- }
- /**
- * @param int $userId
- * @param int $courseId
- * @param int $sessionId
- * @return array
- */
- function getWorkPerUser($userId, $courseId = 0, $sessionId = 0)
- {
- $works = getWorkList(null, null, null, $courseId, $sessionId);
- $result = array();
- if (!empty($works)) {
- foreach ($works as $workData) {
- $workId = $workData->id;
- $result[$workId]['work'] = $workData;
- $result[$workId]['work']->user_results = get_work_user_list(
- 0,
- 100,
- null,
- null,
- $workId,
- null,
- $userId,
- false,
- $courseId,
- $sessionId
- );
- }
- }
- return $result;
- }
- /**
- * @param int $workId
- * @param int $groupId
- * @param int $course_id
- * @param int $sessionId
- * @return mixed
- */
- function getUniqueStudentAttemptsTotal($workId, $groupId, $course_id, $sessionId)
- {
- $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $user_table = Database::get_main_table(TABLE_MAIN_USER);
- $course_id = intval($course_id);
- $workId = intval($workId);
- $sessionId = intval($sessionId);
- $groupId = intval($groupId);
- $sessionCondition = api_get_session_condition(
- $sessionId,
- true,
- false,
- 'w.session_id'
- );
- $groupIid = 0;
- if ($groupId) {
- $groupInfo = GroupManager::get_group_properties($groupId);
- $groupIid = $groupInfo['iid'];
- }
- $sql = "SELECT count(DISTINCT u.user_id)
- FROM $work_table w
- INNER JOIN $user_table u
- ON w.user_id = u.user_id
- WHERE
- w.c_id = $course_id
- $sessionCondition AND
- w.parent_id = $workId AND
- w.post_group_id = $groupIid AND
- w.active IN (0, 1)
- ";
- $res_document = Database::query($sql);
- $rowCount = Database::fetch_row($res_document);
- return $rowCount[0];
- }
- /**
- * @param mixed $workId
- * @param int $groupId
- * @param int $course_id
- * @param int $sessionId
- * @param int $userId user id to filter
- * @param array $onlyUserList only parse this user list
- * @return mixed
- */
- function getUniqueStudentAttempts(
- $workId,
- $groupId,
- $course_id,
- $sessionId,
- $userId = null,
- $onlyUserList = array()
- ) {
- $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $user_table = Database::get_main_table(TABLE_MAIN_USER);
- $course_id = intval($course_id);
- $workCondition = null;
- if (is_array($workId)) {
- $workId = array_map('intval', $workId);
- $workId = implode("','", $workId);
- $workCondition = " w.parent_id IN ('".$workId."') AND";
- } else {
- $workId = intval($workId);
- $workCondition = " w.parent_id = ".$workId." AND";
- }
- $sessionId = intval($sessionId);
- $groupId = intval($groupId);
- $studentCondition = null;
- if (!empty($onlyUserList)) {
- $onlyUserList = array_map('intval', $onlyUserList);
- $studentCondition = "AND u.user_id IN ('".implode("', '", $onlyUserList)."') ";
- } else {
- if (empty($userId)) {
- return 0;
- }
- }
- $groupIid = 0;
- if ($groupId) {
- $groupInfo = GroupManager::get_group_properties($groupId);
- $groupIid = $groupInfo['iid'];
- }
- $sessionCondition = api_get_session_condition(
- $sessionId,
- true,
- false,
- 'w.session_id'
- );
- $sql = "SELECT count(*) FROM (
- SELECT count(*), w.parent_id
- FROM $work_table w
- INNER JOIN $user_table u
- ON w.user_id = u.user_id
- WHERE
- w.filetype = 'file' AND
- w.c_id = $course_id
- $sessionCondition AND
- $workCondition
- w.post_group_id = $groupIid AND
- w.active IN (0, 1) $studentCondition
- ";
- if (!empty($userId)) {
- $userId = intval($userId);
- $sql .= " AND u.user_id = ".$userId;
- }
- $sql .= " GROUP BY u.user_id, w.parent_id) as t";
- $result = Database::query($sql);
- $row = Database::fetch_row($result);
- return $row[0];
- }
- /**
- * Shows the work list (student view)
- * @return string
- */
- function showStudentWorkGrid()
- {
- $courseInfo = api_get_course_info();
- $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_work_student&'.api_get_cidreq();
- $columns = array(
- get_lang('Type'),
- get_lang('Title'),
- get_lang('HandOutDateLimit'),
- get_lang('Feedback'),
- get_lang('LastUpload')
- );
- $columnModel = array(
- array('name'=>'type', 'index'=>'type', 'width'=>'30', 'align'=>'center', 'sortable' => 'false'),
- array('name'=>'title', 'index'=>'title', 'width'=>'250', 'align'=>'left'),
- array('name'=>'expires_on', 'index'=>'expires_on', 'width'=>'80', 'align'=>'center', 'sortable'=>'false'),
- array('name'=>'feedback', 'index'=>'feedback', 'width'=>'80', 'align'=>'center', 'sortable'=>'false'),
- array('name'=>'last_upload', 'index'=>'feedback', 'width'=>'125', 'align'=>'center', 'sortable'=>'false')
- );
- if ($courseInfo['show_score'] == 0) {
- $columnModel[] = array(
- 'name' => 'others',
- 'index' => 'others',
- 'width' => '80',
- 'align' => 'left',
- 'sortable' => 'false'
- );
- $columns[] = get_lang('Others');
- }
- $params = array(
- 'autowidth' => 'true',
- 'height' => 'auto'
- );
- $html = '<script>
- $(function() {
- '.Display::grid_js('workList', $url, $columns, $columnModel, $params, array(), null, true).'
- });
- </script>';
- $html .= Display::grid_html('workList');
- return $html;
- }
- /**
- * Shows the work list (teacher view)
- * @return string
- */
- function showTeacherWorkGrid()
- {
- $columnModel = array(
- array('name'=>'type', 'index'=>'type', 'width'=>'35', 'align'=>'center', 'sortable' => 'false'),
- array('name'=>'title', 'index'=>'title', 'width'=>'300', 'align'=>'left', 'wrap_cell' => "true"),
- array('name'=>'sent_date', 'index'=>'sent_date', 'width'=>'125', 'align'=>'center'),
- array('name'=>'expires_on', 'index'=>'expires_on', 'width'=>'125', 'align'=>'center'),
- array('name'=>'amount', 'index'=>'amount', 'width'=>'110', 'align'=>'center', 'sortable' => 'false'),
- array('name'=>'actions', 'index'=>'actions', 'width'=>'110', 'align'=>'left', 'sortable'=>'false')
- );
- $token = null;
- $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_work_teacher&'.api_get_cidreq();
- $deleteUrl = api_get_path(WEB_AJAX_PATH).'work.ajax.php?a=delete_work&'.api_get_cidreq();
- $columns = array(
- get_lang('Type'),
- get_lang('Title'),
- get_lang('SentDate'),
- get_lang('HandOutDateLimit'),
- get_lang('AmountSubmitted'),
- get_lang('Actions')
- );
- $params = array(
- 'multiselect' => true,
- 'autowidth' => 'true',
- 'height' => 'auto'
- );
- $html = '<script>
- $(function() {
- '.Display::grid_js('workList', $url, $columns, $columnModel, $params, array(), null, true).'
- $("#workList").jqGrid(
- "navGrid",
- "#workList_pager",
- { edit: false, add: false, del: true },
- { height:280, reloadAfterSubmit:false }, // edit options
- { height:280, reloadAfterSubmit:false }, // add options
- { reloadAfterSubmit:false, url: "'.$deleteUrl.'" }, // del options
- { width:500 } // search options
- );
- });
- </script>';
- $html .= Display::grid_html('workList');
- return $html;
- }
- /**
- * Builds the form thats enables the user to
- * select a directory to browse/upload in
- * This function has been copied from the document/document.inc.php library
- *
- * @param array $folders
- * @param string $curdirpath
- * @param string $group_dir
- * @return string html form
- */
- // TODO: This function is a candidate for removal, it is not used anywhere.
- function build_work_directory_selector($folders, $curdirpath, $group_dir = '')
- {
- $form = '<form name="selector" action="'.api_get_self().'?'.api_get_cidreq().'" method="POST">';
- $form .= get_lang('CurrentDirectory').' <select name="curdirpath" onchange="javascript: document.selector.submit();">';
- //group documents cannot be uploaded in the root
- if ($group_dir == '') {
- $form .= '<option value="/">/ ('.get_lang('Root').')</option>';
- if (is_array($folders)) {
- foreach ($folders as $folder) {
- $selected = ($curdirpath == $folder) ? ' selected="selected"' : '';
- $form .= '<option'.$selected.' value="'.$folder.'">'.$folder.'</option>'."\n";
- }
- }
- } else {
- foreach ($folders as $folder) {
- $selected = ($curdirpath == $folder) ? ' selected="selected"' : '';
- $display_folder = substr($folder, strlen($group_dir));
- $display_folder = ($display_folder == '') ? '/ ('.get_lang('Root').')' : $display_folder;
- $form .= '<option'.$selected.' value="'.$folder.'">'.$display_folder.'</option>'."\n";
- }
- }
- $form .= '</select>';
- $form .= '<noscript><input type="submit" name="change_path" value="'.get_lang('Ok').'" /></noscript>';
- $form .= '</form>';
- return $form;
- }
- /**
- * Builds the form thats enables the user to
- * move a document from one directory to another
- * This function has been copied from the document/document.inc.php library
- *
- * @param array $folders
- * @param string $curdirpath
- * @param string $move_file
- * @param string $group_dir
- * @return string html form
- */
- function build_work_move_to_selector($folders, $curdirpath, $move_file, $group_dir = '')
- {
- $course_id = api_get_course_int_id();
- $move_file = intval($move_file);
- $tbl_work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $sql = "SELECT title, url FROM $tbl_work
- WHERE c_id = $course_id AND id ='".$move_file."'";
- $result = Database::query($sql);
- $row = Database::fetch_array($result, 'ASSOC');
- $title = empty($row['title']) ? basename($row['url']) : $row['title'];
- $form = new FormValidator(
- 'move_to_form',
- 'post',
- api_get_self().'?'.api_get_cidreq().'&curdirpath='.Security::remove_XSS($curdirpath)
- );
- $form->addHeader(get_lang('MoveFile').' - '.Security::remove_XSS($title));
- $form->addHidden('item_id', $move_file);
- $form->addHidden('action', 'move_to');
- //group documents cannot be uploaded in the root
- if ($group_dir == '') {
- if ($curdirpath != '/') {
- //$form .= '<option value="0">/ ('.get_lang('Root').')</option>';
- }
- if (is_array($folders)) {
- foreach ($folders as $fid => $folder) {
- //you cannot move a file to:
- //1. current directory
- //2. inside the folder you want to move
- //3. inside a subfolder of the folder you want to move
- if (($curdirpath != $folder) && ($folder != $move_file) && (substr($folder, 0, strlen($move_file) + 1) != $move_file.'/')) {
- //$form .= '<option value="'.$fid.'">'.$folder.'</option>';
- $options[$fid] = $folder;
- }
- }
- }
- } else {
- if ($curdirpath != '/') {
- $form .= '<option value="0">/ ('.get_lang('Root').')</option>';
- }
- foreach ($folders as $fid => $folder) {
- if (($curdirpath != $folder) && ($folder != $move_file) && (substr($folder, 0, strlen($move_file) + 1) != $move_file.'/')) {
- //cannot copy dir into his own subdir
- $display_folder = substr($folder, strlen($group_dir));
- $display_folder = ($display_folder == '') ? '/ ('.get_lang('Root').')' : $display_folder;
- //$form .= '<option value="'.$fid.'">'.$display_folder.'</option>'."\n";
- $options[$fid] = $display_folder;
- }
- }
- }
- $form->addSelect('move_to_id', get_lang('Select'), $options);
- $form->addButtonSend(get_lang('MoveFile'), 'move_file_submit');
- return $form->returnForm();
- }
- /**
- * creates a new directory trying to find a directory name
- * that doesn't already exist
- *
- * @author Hugues Peeters <hugues.peeters@claroline.net>
- * @author Bert Vanderkimpen
- * @author Yannick Warnier <ywarnier@beeznest.org> Adaptation for work tool
- * @param string $base_work_dir Base work dir (.../work)
- * @param string $desiredDirName complete path of the desired name
- *
- * @return string actual directory name if it succeeds, boolean false otherwise
- */
- function create_unexisting_work_directory($base_work_dir, $desired_dir_name)
- {
- $nb = '';
- $base_work_dir = (substr($base_work_dir, -1, 1) == '/' ? $base_work_dir : $base_work_dir.'/');
- while (file_exists($base_work_dir.$desired_dir_name.$nb)) {
- $nb += 1;
- }
- if (@mkdir($base_work_dir.$desired_dir_name.$nb, api_get_permissions_for_new_directories())) {
- return $desired_dir_name.$nb;
- } else {
- return false;
- }
- }
- /**
- * Delete a work-tool directory
- * @param int $id work directory id to delete
- * @return integer -1 on error
- */
- function deleteDirWork($id)
- {
- $locked = api_resource_is_locked_by_gradebook($id, LINK_STUDENTPUBLICATION);
- if ($locked == true) {
- echo Display::return_message(get_lang('ResourceLockedByGradebook'), 'warning');
- return false;
- }
- $_course = api_get_course_info();
- $id = intval($id);
- $work_data = get_work_data_by_id($id);
- if (empty($work_data)) {
- return false;
- }
- $base_work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/work';
- $work_data_url = $base_work_dir.$work_data['url'];
- $check = Security::check_abs_path($work_data_url.'/', $base_work_dir.'/');
- $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $TSTDPUBASG = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
- $t_agenda = Database::get_course_table(TABLE_AGENDA);
- $course_id = api_get_course_int_id();
- $sessionId = api_get_session_id();
- if (!empty($work_data['url'])) {
- if ($check) {
- $consideredWorkingTime = api_get_configuration_value('considered_working_time');
- if (!empty($consideredWorkingTime)) {
- $fieldValue = new ExtraFieldValue('work');
- $resultExtra = $fieldValue->getAllValuesForAnItem(
- $work_data['id'],
- true
- );
- $workingTime = null;
- foreach ($resultExtra as $field) {
- $field = $field['value'];
- if ($consideredWorkingTime == $field->getField()->getVariable()) {
- $workingTime = $field->getValue();
- break;
- }
- }
- $courseUsers = CourseManager::get_user_list_from_course_code($_course['code'], $sessionId);
- if (!empty($workingTime)) {
- foreach ($courseUsers as $user) {
- $userWorks = get_work_user_list(
- 0,
- 100,
- null,
- null,
- $work_data['id'],
- null,
- $user['user_id'],
- false,
- $course_id,
- $sessionId
- );
- if (count($userWorks) != 1) {
- continue;
- }
- Event::eventRemoveVirtualCourseTime($course_id, $user['user_id'], $sessionId, $workingTime);
- }
- }
- }
- // Deleting all contents inside the folder
- $sql = "UPDATE $table SET active = 2
- WHERE c_id = $course_id AND filetype = 'folder' AND id = $id";
- Database::query($sql);
- $sql = "UPDATE $table SET active = 2
- WHERE c_id = $course_id AND parent_id = $id";
- Database::query($sql);
- $new_dir = $work_data_url.'_DELETED_'.$id;
- if (api_get_setting('permanently_remove_deleted_files') == 'true') {
- my_delete($work_data_url);
- } else {
- if (file_exists($work_data_url)) {
- rename($work_data_url, $new_dir);
- }
- }
- // Gets calendar_id from student_publication_assigment
- $sql = "SELECT add_to_calendar FROM $TSTDPUBASG
- WHERE c_id = $course_id AND publication_id = $id";
- $res = Database::query($sql);
- $calendar_id = Database::fetch_row($res);
- // delete from agenda if it exists
- if (!empty($calendar_id[0])) {
- $sql = "DELETE FROM $t_agenda
- WHERE c_id = $course_id AND id = '".$calendar_id[0]."'";
- Database::query($sql);
- }
- $sql = "DELETE FROM $TSTDPUBASG
- WHERE c_id = $course_id AND publication_id = $id";
- Database::query($sql);
- Event::addEvent(
- LOG_WORK_DIR_DELETE,
- LOG_WORK_DATA,
- [
- 'id' => $work_data['id'],
- 'url' => $work_data['url'],
- 'title' => $work_data['title']
- ],
- null,
- api_get_user_id(),
- api_get_course_int_id(),
- $sessionId
- );
- $link_info = GradebookUtils::isResourceInCourseGradebook(
- api_get_course_id(),
- 3,
- $id,
- api_get_session_id()
- );
- $link_id = $link_info['id'];
- if ($link_info !== false) {
- GradebookUtils::remove_resource_from_course_gradebook($link_id);
- }
- return true;
- }
- }
- }
- /**
- * Get the path of a document in the student_publication table (path relative to the course directory)
- * @param integer $id
- * @return string Path (or -1 on error)
- */
- function get_work_path($id)
- {
- $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $course_id = api_get_course_int_id();
- $sql = 'SELECT url FROM '.$table.'
- WHERE c_id = '.$course_id.' AND id='.intval($id);
- $res = Database::query($sql);
- if (Database::num_rows($res)) {
- $row = Database::fetch_array($res);
- return $row['url'];
- }
- return -1;
- }
- /**
- * Update the url of a work in the student_publication table
- * @param integer $id of the work to update
- * @param string $new_path Destination directory where the work has been moved (must end with a '/')
- * @param int $parent_id
- *
- * @return mixed Int -1 on error, sql query result on success
- */
- function updateWorkUrl($id, $new_path, $parent_id)
- {
- if (empty($id)) {
- return -1;
- }
- $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $course_id = api_get_course_int_id();
- $id = intval($id);
- $parent_id = intval($parent_id);
- $sql = "SELECT * FROM $table
- WHERE c_id = $course_id AND id = $id";
- $res = Database::query($sql);
- if (Database::num_rows($res) != 1) {
- return -1;
- } else {
- $row = Database::fetch_array($res);
- $filename = basename($row['url']);
- $new_url = $new_path.$filename;
- $new_url = Database::escape_string($new_url);
- $sql = "UPDATE $table SET
- url = '$new_url',
- parent_id = '$parent_id'
- WHERE c_id = $course_id AND id = $id";
- $res = Database::query($sql);
- return $res;
- }
- }
- /**
- * Update the url of a dir in the student_publication table
- * @param array $work_data work original data
- * @param string $newPath Example: "folder1"
- * @return bool
- */
- function updateDirName($work_data, $newPath)
- {
- $course_id = $work_data['c_id'];
- $sessionId = intval($work_data['session_id']);
- $work_id = intval($work_data['iid']);
- $oldPath = $work_data['url'];
- $originalNewPath = Database::escape_string($newPath);
- $newPath = Database::escape_string($newPath);
- $newPath = api_replace_dangerous_char($newPath);
- $newPath = disable_dangerous_file($newPath);
- if ($oldPath == '/'.$newPath) {
- return true;
- }
- if (!empty($newPath)) {
- $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $sql = "UPDATE $table SET
- title = '".$originalNewPath."'
- WHERE
- c_id = $course_id AND
- iid = $work_id";
- Database::query($sql);
- }
- }
- /**
- * Return an array with all the folder's ids that are in the given path
- * @param string Path of the directory
- * @return array The list of ids of all the directories in the path
- * @author Julio Montoya
- * @version April 2008
- */
- function get_parent_directories($id)
- {
- $course_id = api_get_course_int_id();
- $em = Database::getManager();
- $directories = $em
- ->getRepository('ChamiloCourseBundle:CStudentPublication')
- ->findBy([
- 'cId' => $course_id,
- 'parentId' => $id
- ]);
- $list_id = array();
- foreach ($directories as $directory) {
- $list_id[] = $directory->getId();
- }
- return $list_id;
- }
- /**
- * Transform an all directory structure (only directories) in an array
- * @param string path of the directory
- * @return array the directory structure into an array
- * @author Julio Montoya
- * @version April 2008
- */
- function directory_to_array($directory)
- {
- $array_items = array();
- if ($handle = @opendir($directory)) {
- while (false !== ($file = readdir($handle))) {
- if ($file != '.' && $file != '..') {
- if (is_dir($directory.'/'.$file)) {
- $array_items = array_merge($array_items, directory_to_array($directory.'/'.$file));
- $file = $directory.'/'.$file;
- $array_items[] = preg_replace("/\/\//si", '/', $file);
- }
- }
- }
- closedir($handle);
- }
- return $array_items;
- }
- /**
- * Insert into the DB of the course all the directories
- * @param string $base_work_dir path of the /work directory of the course
- * @return mixed Int -1 on error, sql query result on success
- * @author Julio Montoya
- * @version April 2008
- */
- function insert_all_directory_in_course_table($base_work_dir)
- {
- $dir_to_array = directory_to_array($base_work_dir, true);
- $only_dir = array();
- for ($i = 0; $i < count($dir_to_array); $i++) {
- $only_dir[] = substr($dir_to_array[$i], strlen($base_work_dir), strlen($dir_to_array[$i]));
- }
- $course_id = api_get_course_int_id();
- $group_id = api_get_group_id();
- $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $groupIid = 0;
- if ($group_id) {
- $groupInfo = GroupManager::get_group_properties($group_id);
- $groupIid = $groupInfo['iid'];
- }
- for ($i = 0; $i < count($only_dir); $i++) {
- $url = $only_dir[$i];
- $params = [
- 'c_id' => $course_id,
- 'url' => $url,
- 'title' => '',
- 'description' => '',
- 'author' => '',
- 'active' => '1',
- 'accepted' => '1',
- 'filetype' => 'folder',
- 'post_group_id' => $groupIid,
- ];
- Database::insert($work_table, $params);
- }
- }
- /**
- * This function displays the number of files contained in a directory
- *
- * @param string the path of the directory
- * @param boolean true if we want the total quantity of files
- * include in others child directories, false only files in the directory
- * @return array the first element is an integer with the number of files
- * in the folder, the second element is the number of directories
- * @author Julio Montoya
- * @version April 2008
- */
- function count_dir($path_dir, $recurse)
- {
- $count = 0;
- $count_dir = 0;
- $d = dir($path_dir);
- while ($entry = $d->Read()) {
- if (!(($entry == '..') || ($entry == '.'))) {
- if (is_dir($path_dir.'/'.$entry)) {
- $count_dir++;
- if ($recurse) {
- $count += count_dir($path_dir.'/'.$entry, $recurse);
- }
- } else {
- $count++;
- }
- }
- }
- $return_array = array();
- $return_array[] = $count;
- $return_array[] = $count_dir;
- return $return_array;
- }
- /**
- * returns all the javascript that is required for easily
- * validation when you create a work
- * this goes into the $htmlHeadXtra[] array
- */
- function to_javascript_work()
- {
- $js = '<script>
- function updateDocumentTitle(value) {
- var temp = value.indexOf("/");
- //linux path
- if(temp!=-1){
- var temp=value.split("/");
- } else {
- var temp=value.split("\\\");
- }
- document.getElementById("file_upload").value=temp[temp.length-1];
- $("#contains_file_id").attr("value", 1);
- }
- function checkDate(month, day, year) {
- var monthLength =
- new Array(31,28,31,30,31,30,31,31,30,31,30,31);
- if (!day || !month || !year)
- return false;
- // check for bisestile year
- if (year/4 == parseInt(year/4))
- monthLength[1] = 29;
- if (month < 1 || month > 12)
- return false;
- if (day > monthLength[month-1])
- return false;
- return true;
- }
- function mktime() {
- var no, ma = 0, mb = 0, i = 0, d = new Date(), argv = arguments, argc = argv.length;
- d.setHours(0,0,0); d.setDate(1); d.setMonth(1); d.setYear(1972);
- var dateManip = {
- 0: function(tt){ return d.setHours(tt); },
- 1: function(tt){ return d.setMinutes(tt); },
- 2: function(tt){ set = d.setSeconds(tt); mb = d.getDate() - 1; return set; },
- 3: function(tt){ set = d.setMonth(parseInt(tt)-1); ma = d.getFullYear() - 1972; return set; },
- 4: function(tt){ return d.setDate(tt+mb); },
- 5: function(tt){ return d.setYear(tt+ma); }
- };
- for( i = 0; i < argc; i++ ){
- no = parseInt(argv[i]*1);
- if (isNaN(no)) {
- return false;
- } else {
- // arg is number, lets manipulate date object
- if(!dateManip[i](no)){
- // failed
- return false;
- }
- }
- }
- return Math.floor(d.getTime()/1000);
- }
- function setFocus() {
- $("#work_title").focus();
- }
- $(document).ready(function() {
- setFocus();
- var checked = $("#expiry_date").attr("checked");
- if (checked) {
- $("#option2").show();
- } else {
- $("#option2").hide();
- }
-
- var checkedEndDate = $("#end_date").attr("checked");
- if (checkedEndDate) {
- $("#option3").show();
- $("#ends_on").attr("checked", true);
- } else {
- $("#option3").hide();
- $("#ends_on").attr("checked", false);
- }
- $("#expiry_date").click(function() {
- $("#option2").toggle();
- });
- $("#end_date").click(function() {
- $("#option3").toggle();
- });
- });
- </script>';
- return $js;
- }
- /**
- * Gets the id of a student publication with a given path
- * @param string $path
- * @return true if is found / false if not found
- */
- // TODO: The name of this function does not fit with the kind of information it returns. Maybe check_work_id() or is_work_id()?
- function get_work_id($path)
- {
- $TBL_STUDENT_PUBLICATION = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $TBL_PROP_TABLE = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $course_id = api_get_course_int_id();
- $path = Database::escape_string($path);
- if (api_is_allowed_to_edit()) {
- $sql = "SELECT work.id
- FROM $TBL_STUDENT_PUBLICATION AS work, $TBL_PROP_TABLE AS props
- WHERE
- props.c_id = $course_id AND
- work.c_id = $course_id AND
- props.tool='work' AND
- work.id=props.ref AND
- work.url LIKE 'work/".$path."%' AND
- work.filetype='file' AND
- props.visibility<>'2'";
- } else {
- $sql = "SELECT work.id
- FROM $TBL_STUDENT_PUBLICATION AS work, $TBL_PROP_TABLE AS props
- WHERE
- props.c_id = $course_id AND
- work.c_id = $course_id AND
- props.tool='work' AND
- work.id=props.ref AND
- work.url LIKE 'work/".$path."%' AND
- work.filetype='file' AND
- props.visibility<>'2' AND
- props.lastedit_user_id = '".api_get_user_id()."'";
- }
- $result = Database::query($sql);
- $num_rows = Database::num_rows($result);
- if ($result && $num_rows > 0) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * @param int $work_id
- * @param int $onlyMeUserId show only my works
- * @param int $notMeUserId show works from everyone except me
- * @return int
- */
- function get_count_work($work_id, $onlyMeUserId = null, $notMeUserId = null)
- {
- $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $iprop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $user_table = Database::get_main_table(TABLE_MAIN_USER);
- $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
- $session_id = api_get_session_id();
- $condition_session = api_get_session_condition(
- $session_id,
- true,
- false,
- 'work.session_id'
- );
- $group_id = api_get_group_id();
- $course_info = api_get_course_info();
- $course_id = $course_info['real_id'];
- $work_id = intval($work_id);
- $groupIid = 0;
- if ($group_id) {
- $groupInfo = GroupManager::get_group_properties($group_id);
- $groupIid = $groupInfo['iid'];
- }
- if (!empty($group_id)) {
- // set to select only messages posted by the user's group
- $extra_conditions = " work.post_group_id = '".intval($groupIid)."' ";
- } else {
- $extra_conditions = " (work.post_group_id = '0' or work.post_group_id IS NULL) ";
- }
- if ($is_allowed_to_edit) {
- $extra_conditions .= ' AND work.active IN (0, 1) ';
- } else {
- $extra_conditions .= ' AND work.active IN (0, 1) AND accepted = 1';
- if (isset($course_info['show_score']) && $course_info['show_score'] == 1) {
- $extra_conditions .= " AND work.user_id = ".api_get_user_id()." ";
- } else {
- $extra_conditions .= '';
- }
- }
- $extra_conditions .= " AND parent_id = ".$work_id." ";
- $where_condition = null;
- if (!empty($notMeUserId)) {
- $where_condition .= " AND u.user_id <> ".intval($notMeUserId);
- }
- if (!empty($onlyMeUserId)) {
- $where_condition .= " AND u.user_id = ".intval($onlyMeUserId);
- }
- $sql = "SELECT count(*) as count
- FROM $iprop_table prop
- INNER JOIN $work_table work
- ON (
- prop.ref = work.id AND
- prop.c_id = $course_id AND
- prop.tool='work' AND
- prop.visibility <> 2 AND
- work.c_id = $course_id
- )
- INNER JOIN $user_table u
- ON (work.user_id = u.user_id)
- WHERE $extra_conditions $where_condition $condition_session";
- $result = Database::query($sql);
- $users_with_work = 0;
- if (Database::num_rows($result)) {
- $result = Database::fetch_array($result);
- $users_with_work = $result['count'];
- }
- return $users_with_work;
- }
- /**
- * @param int $start
- * @param int $limit
- * @param string $column
- * @param string $direction
- * @param string $where_condition
- * @param bool $getCount
- * @return array
- */
- function getWorkListStudent(
- $start,
- $limit,
- $column,
- $direction,
- $where_condition,
- $getCount = false
- ) {
- $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $workTableAssignment = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
- $courseInfo = api_get_course_info();
- $course_id = $courseInfo['real_id'];
- $session_id = api_get_session_id();
- $condition_session = api_get_session_condition($session_id);
- $group_id = api_get_group_id();
- $userId = api_get_user_id();
- $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(
- api_get_user_id(),
- $courseInfo
- );
- if (!in_array($direction, array('asc', 'desc'))) {
- $direction = 'desc';
- }
- if (!empty($where_condition)) {
- $where_condition = ' AND '.$where_condition;
- }
- $column = !empty($column) ? Database::escape_string($column) : 'sent_date';
- $start = intval($start);
- $limit = intval($limit);
- $groupIid = 0;
- if ($group_id) {
- $groupInfo = GroupManager::get_group_properties($group_id);
- $groupIid = $groupInfo['iid'];
- }
- $groupIid = (int) $groupIid;
- // Get list from database
- if (!empty($groupIid)) {
- $group_query = " WHERE w.c_id = $course_id AND post_group_id = $groupIid";
- $subdirs_query = "AND parent_id = 0";
- } else {
- $group_query = " WHERE w.c_id = $course_id AND (post_group_id = '0' or post_group_id is NULL) ";
- $subdirs_query = "AND parent_id = 0";
- }
- $active_condition = ' AND active IN (1, 0)';
- if ($getCount) {
- $select = "SELECT count(w.id) as count ";
- } else {
- $select = "SELECT w.*, a.expires_on, expires_on, ends_on, enable_qualification ";
- }
- $sql = "$select
- FROM $workTable w
- LEFT JOIN $workTableAssignment a
- ON (a.publication_id = w.id AND a.c_id = w.c_id)
- $group_query
- $subdirs_query
- $active_condition
- $condition_session
- $where_condition
- ";
- $sql .= " ORDER BY $column $direction ";
- if (!empty($start) && !empty($limit)) {
- $sql .= " LIMIT $start, $limit";
- }
- $result = Database::query($sql);
- if ($getCount) {
- $row = Database::fetch_array($result);
- return $row['count'];
- }
- $works = array();
- $url = api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq();
- if ($isDrhOfCourse) {
- $url = api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq();
- }
- $urlOthers = api_get_path(WEB_CODE_PATH).'work/work_list_others.php?'.api_get_cidreq().'&id=';
- while ($work = Database::fetch_array($result, 'ASSOC')) {
- $isSubscribed = userIsSubscribedToWork($userId, $work['id'], $course_id);
- if ($isSubscribed == false) {
- continue;
- }
- $visibility = api_get_item_visibility($courseInfo, 'work', $work['id'], $session_id);
- if ($visibility != 1) {
- continue;
- }
- $work['type'] = Display::return_icon('work.png');
- $work['expires_on'] = empty($work['expires_on']) ? null : api_get_local_time($work['expires_on']);
- if (empty($work['title'])) {
- $work['title'] = basename($work['url']);
- }
- $whereCondition = " AND u.user_id = ".intval($userId);
- $workList = get_work_user_list(
- 0,
- 1000,
- null,
- null,
- $work['id'],
- $whereCondition
- );
- $count = getTotalWorkComment($workList, $courseInfo);
- $lastWork = getLastWorkStudentFromParentByUser($userId, $work, $courseInfo);
- if (!is_null($count) && !empty($count)) {
- $urlView = api_get_path(WEB_CODE_PATH).'work/view.php?id='.$lastWork['id'].'&'.api_get_cidreq();
- $feedback = ' '.Display::url(
- Display::returnFontAwesomeIcon('comments-o'),
- $urlView,
- ['title' => get_lang('View')]
- );
- $work['feedback'] = ' '.Display::label($count.' '.get_lang('Feedback'), 'info').$feedback;
- }
- if (!empty($lastWork)) {
- $work['last_upload'] = (!empty($lastWork['qualification'])) ? $lastWork['qualification_rounded'].' - ' : '';
- $work['last_upload'] .= api_get_local_time($lastWork['sent_date']);
- }
- $work['title'] = Display::url($work['title'], $url.'&id='.$work['id']);
- $work['others'] = Display::url(
- Display::return_icon('group.png', get_lang('Others')),
- $urlOthers.$work['id']
- );
- $works[] = $work;
- }
- return $works;
- }
- /**
- * @param int $start
- * @param int $limit
- * @param string $column
- * @param string $direction
- * @param string $where_condition
- * @param bool $getCount
- * @return array
- */
- function getWorkListTeacher(
- $start,
- $limit,
- $column,
- $direction,
- $where_condition,
- $getCount = false
- ) {
- $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $workTableAssignment = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
- $courseInfo = api_get_course_info();
- $course_id = api_get_course_int_id();
- $session_id = api_get_session_id();
- $condition_session = api_get_session_condition($session_id);
- $group_id = api_get_group_id();
- $groupIid = 0;
- if ($group_id) {
- $groupInfo = GroupManager::get_group_properties($group_id);
- $groupIid = $groupInfo['iid'];
- }
- $groupIid = (int) $groupIid;
- $is_allowed_to_edit = api_is_allowed_to_edit() || api_is_coach();
- if (!in_array($direction, array('asc', 'desc'))) {
- $direction = 'desc';
- }
- if (!empty($where_condition)) {
- $where_condition = ' AND '.$where_condition;
- }
- $column = !empty($column) ? Database::escape_string($column) : 'sent_date';
- $start = intval($start);
- $limit = intval($limit);
- $works = array();
- // Get list from database
- if ($is_allowed_to_edit) {
- $active_condition = ' active IN (0, 1)';
- if ($getCount) {
- $select = " SELECT count(w.id) as count";
- } else {
- $select = " SELECT w.*, a.expires_on, expires_on, ends_on, enable_qualification ";
- }
- $sql = " $select
- FROM $workTable w
- LEFT JOIN $workTableAssignment a
- ON (a.publication_id = w.id AND a.c_id = w.c_id)
- WHERE
- w.c_id = $course_id
- $condition_session AND
- $active_condition AND
- parent_id = 0 AND
- post_group_id = $groupIid
- $where_condition
- ORDER BY $column $direction
- LIMIT $start, $limit";
- $result = Database::query($sql);
- if ($getCount) {
- $row = Database::fetch_array($result);
- return $row['count'];
- }
- $url = api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq();
- while ($work = Database::fetch_array($result, 'ASSOC')) {
- $workId = $work['id'];
- $work['type'] = Display::return_icon('work.png');
- $work['expires_on'] = empty($work['expires_on']) ? null : api_get_local_time($work['expires_on']);
- $countUniqueAttempts = getUniqueStudentAttemptsTotal(
- $workId,
- $group_id,
- $course_id,
- $session_id
- );
- $totalUsers = getStudentSubscribedToWork(
- $workId,
- $course_id,
- $group_id,
- $session_id,
- true
- );
- $work['amount'] = Display::label(
- $countUniqueAttempts.'/'.
- $totalUsers,
- 'success'
- );
- $visibility = api_get_item_visibility($courseInfo, 'work', $workId, $session_id);
- if ($visibility == 1) {
- $icon = 'visible.png';
- $text = get_lang('Visible');
- $action = 'invisible';
- $class = '';
- } else {
- $icon = 'invisible.png';
- $text = get_lang('Invisible');
- $action = 'visible';
- $class = 'muted';
- }
- $visibilityLink = Display::url(
- Display::return_icon($icon, $text, array(), ICON_SIZE_SMALL),
- api_get_path(WEB_CODE_PATH).'work/work.php?id='.$workId.'&action='.$action.'&'.api_get_cidreq()
- );
- if (empty($work['title'])) {
- $work['title'] = basename($work['url']);
- }
- $work['title'] = Display::url($work['title'], $url.'&id='.$workId, ['class' => $class]);
- $work['title'] .= ' '.Display::label(get_count_work($work['id']), 'success');
- $work['sent_date'] = api_get_local_time($work['sent_date']);
- $editLink = Display::url(
- Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL),
- api_get_path(WEB_CODE_PATH).'work/edit_work.php?id='.$workId.'&'.api_get_cidreq()
- );
- $correctionLink = ' '.Display::url(
- Display::return_icon('upload_package.png', get_lang('UploadCorrections'), '', ICON_SIZE_SMALL),
- api_get_path(WEB_CODE_PATH).'work/upload_corrections.php?'.api_get_cidreq().'&id='.$workId
- ).' ';
- if ($countUniqueAttempts > 0) {
- $downloadLink = Display::url(
- Display::return_icon(
- 'save_pack.png',
- get_lang('Save'),
- array(),
- ICON_SIZE_SMALL
- ),
- api_get_path(WEB_CODE_PATH).'work/downloadfolder.inc.php?id='.$workId.'&'.api_get_cidreq()
- );
- } else {
- $downloadLink = Display::url(
- Display::return_icon(
- 'save_pack_na.png',
- get_lang('Save'),
- array(),
- ICON_SIZE_SMALL
- ),
- '#'
- );
- }
- // Remove Delete Work Button from action List
- // Because removeXSS "removes" the onClick JS Event to do the action (See model.ajax.php - Line 1639)
- // But still can use the another jqgrid button to remove works (trash icon)
- //
- // $deleteUrl = api_get_path(WEB_CODE_PATH).'work/work.php?id='.$workId.'&action=delete_dir&'.api_get_cidreq();
- // $deleteLink = '<a href="#" onclick="showConfirmationPopup(this, \'' . $deleteUrl . '\' ) " >' .
- // Display::return_icon(
- // 'delete.png',
- // get_lang('Delete'),
- // array(),
- // ICON_SIZE_SMALL
- // ) . '</a>';
- if (!api_is_allowed_to_edit()) {
- // $deleteLink = null;
- $editLink = null;
- }
- $work['actions'] = $visibilityLink.$correctionLink.$downloadLink.$editLink;
- $works[] = $work;
- }
- }
- return $works;
- }
- /**
- * @param int $start
- * @param int $limit
- * @param string $column
- * @param string $direction
- * @param int $workId
- * @param int $studentId
- * @param string $whereCondition
- * @param bool $getCount
- * @return array
- */
- function get_work_user_list_from_documents(
- $start,
- $limit,
- $column,
- $direction,
- $workId,
- $studentId = null,
- $whereCondition = '',
- $getCount = false
- ) {
- if ($getCount) {
- $select1 = " SELECT count(u.user_id) as count ";
- $select2 = " SELECT count(u.user_id) as count ";
- } else {
- $select1 = " SELECT DISTINCT u.firstname, u.lastname, u.user_id, w.title, w.parent_id, w.document_id document_id, w.id, qualification, qualificator_id";
- $select2 = " SELECT DISTINCT u.firstname, u.lastname, u.user_id, d.title, w.parent_id, d.id document_id, 0, 0, 0";
- }
- $documentTable = Database::get_course_table(TABLE_DOCUMENT);
- $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $workRelDocument = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_DOCUMENT);
- $userTable = Database::get_main_table(TABLE_MAIN_USER);
- $courseId = api_get_course_int_id();
- $sessionId = api_get_session_id();
- if (empty($studentId)) {
- $studentId = api_get_user_id();
- }
- $studentId = intval($studentId);
- $workId = intval($workId);
- $userCondition = " AND u.user_id = $studentId ";
- $sessionCondition = api_get_session_condition($sessionId, true, false, 'w.session_id');
- $workCondition = " AND w_rel.work_id = $workId";
- $workParentCondition = " AND w.parent_id = $workId";
- $sql = "(
- $select1 FROM $userTable u
- INNER JOIN $workTable w
- ON (u.user_id = w.user_id AND w.active IN (0, 1) AND w.filetype = 'file')
- WHERE
- w.c_id = $courseId
- $userCondition
- $sessionCondition
- $whereCondition
- $workParentCondition
- ) UNION (
- $select2 FROM $workTable w
- INNER JOIN $workRelDocument w_rel
- ON (w_rel.work_id = w.id AND w.active IN (0, 1) AND w_rel.c_id = w.c_id)
- INNER JOIN $documentTable d
- ON (w_rel.document_id = d.id AND d.c_id = w.c_id)
- INNER JOIN $userTable u ON (u.user_id = $studentId)
- WHERE
- w.c_id = $courseId
- $workCondition
- $sessionCondition AND
- d.id NOT IN (
- SELECT w.document_id id
- FROM $workTable w
- WHERE
- user_id = $studentId AND
- c_id = $courseId AND
- filetype = 'file' AND
- active IN (0, 1)
- $sessionCondition
- $workParentCondition
- )
- )";
- $start = intval($start);
- $limit = intval($limit);
- $direction = in_array(strtolower($direction), array('desc', 'asc')) ? $direction : 'desc';
- $column = Database::escape_string($column);
- if ($getCount) {
- $result = Database::query($sql);
- $result = Database::fetch_array($result);
- return $result['count'];
- }
- $sql .= " ORDER BY $column $direction";
- $sql .= " LIMIT $start, $limit";
- $result = Database::query($sql);
- $currentUserId = api_get_user_id();
- $work_data = get_work_data_by_id($workId);
- $qualificationExists = false;
- if (!empty($work_data['qualification']) && intval($work_data['qualification']) > 0) {
- $qualificationExists = true;
- }
- $urlAdd = api_get_path(WEB_CODE_PATH).'work/upload_from_template.php?'.api_get_cidreq();
- $urlEdit = api_get_path(WEB_CODE_PATH).'work/edit.php?'.api_get_cidreq();
- $urlDelete = api_get_path(WEB_CODE_PATH).'work/work_list.php?action=delete&'.api_get_cidreq();
- $urlView = api_get_path(WEB_CODE_PATH).'work/view.php?'.api_get_cidreq();
- $editIcon = Display::return_icon('edit.png', get_lang('Edit'));
- $addIcon = Display::return_icon('add.png', get_lang('Add'));
- $deleteIcon = Display::return_icon('delete.png', get_lang('Delete'));
- $viewIcon = Display::return_icon('rate_work.png', get_lang('CorrectAndRate'));
- $allowEdition = api_get_course_setting('student_delete_own_publication');
- $workList = array();
- while ($row = Database::fetch_array($result, 'ASSOC')) {
- $userId = $row['user_id'];
- $documentId = $row['document_id'];
- $itemId = $row['id'];
- $addLinkShowed = false;
- if (empty($documentId)) {
- $url = $urlEdit.'&item_id='.$row['id'].'&id='.$workId;
- $editLink = Display::url($editIcon, $url);
- if ($allowEdition == false) {
- $editLink = null;
- }
- } else {
- $documentToWork = getDocumentToWorkPerUser($documentId, $workId, $courseId, $sessionId, $userId);
- if (empty($documentToWork)) {
- $url = $urlAdd.'&document_id='.$documentId.'&id='.$workId;
- $editLink = Display::url($addIcon, $url);
- $addLinkShowed = true;
- } else {
- $row['title'] = $documentToWork['title'];
- $row['sent_date'] = $documentToWork['sent_date'];
- $newWorkId = $documentToWork['id'];
- $url = $urlEdit.'&item_id='.$newWorkId.'&id='.$workId;
- $editLink = Display::url($editIcon, $url);
- if ($allowEdition == false) {
- $editLink = null;
- }
- }
- }
- if ($allowEdition && !empty($itemId)) {
- $deleteLink = Display::url($deleteIcon, $urlDelete.'&item_id='.$itemId.'&id='.$workId);
- } else {
- $deleteLink = null;
- }
- $viewLink = null;
- if (!empty($itemId)) {
- $viewLink = Display::url($viewIcon, $urlView.'&id='.$itemId);
- }
- $row['type'] = null;
- if ($qualificationExists) {
- if (empty($row['qualificator_id'])) {
- $status = Display::label(get_lang('NotRevised'), 'warning');
- } else {
- $status = Display::label(get_lang('Revised'), 'success');
- }
- $row['qualificator_id'] = $status;
- }
- if (!empty($row['qualification'])) {
- $row['qualification'] = Display::label($row['qualification'], 'info');
- }
- if (!empty($row['sent_date'])) {
- $row['sent_date'] = api_get_local_time($row['sent_date']);
- }
- if ($userId == $currentUserId) {
- $row['actions'] = $viewLink.$editLink.$deleteLink;
- }
- if ($addLinkShowed) {
- $row['qualification'] = '';
- $row['qualificator_id'] = '';
- }
- $workList[] = $row;
- }
- return $workList;
- }
- /**
- * @param int $start
- * @param int $limit
- * @param int $column
- * @param string $direction
- * @param int $work_id
- * @param array $where_condition
- * @param int $studentId
- * @param bool $getCount
- * @param int $courseId
- * @param int $sessionId
- * @return array
- */
- function get_work_user_list(
- $start,
- $limit,
- $column,
- $direction,
- $work_id,
- $where_condition = null,
- $studentId = null,
- $getCount = false,
- $courseId = 0,
- $sessionId = 0
- ) {
- $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $user_table = Database::get_main_table(TABLE_MAIN_USER);
- $session_id = $sessionId ? $sessionId : api_get_session_id();
- $group_id = api_get_group_id();
- $course_info = api_get_course_info();
- $course_info = empty($course_info) ? api_get_course_info_by_id($courseId) : $course_info;
- $course_id = isset($course_info['real_id']) ? $course_info['real_id'] : $courseId;
- $work_id = intval($work_id);
- $column = !empty($column) ? Database::escape_string($column) : 'sent_date';
- $start = intval($start);
- $limit = intval($limit);
- if (!in_array($direction, array('asc', 'desc'))) {
- $direction = 'desc';
- }
- $work_data = get_work_data_by_id($work_id, $courseId, $sessionId);
- $is_allowed_to_edit = api_is_allowed_to_edit() || api_is_coach();
- $condition_session = api_get_session_condition(
- $session_id,
- true,
- false,
- 'work.session_id'
- );
- $locked = api_resource_is_locked_by_gradebook(
- $work_id,
- LINK_STUDENTPUBLICATION,
- $course_info['code']
- );
- $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(
- api_get_user_id(),
- $course_info
- );
- $groupIid = 0;
- if ($group_id) {
- $groupInfo = GroupManager::get_group_properties($group_id);
- $groupIid = $groupInfo['iid'];
- }
- if (!empty($work_data)) {
- if (!empty($group_id)) {
- $extra_conditions = " work.post_group_id = '".intval($groupIid)."' ";
- // set to select only messages posted by the user's group
- } else {
- $extra_conditions = " (work.post_group_id = '0' OR work.post_group_id is NULL) ";
- }
- if ($is_allowed_to_edit || $isDrhOfCourse) {
- $extra_conditions .= ' AND work.active IN (0, 1) ';
- } else {
- if (isset($course_info['show_score']) &&
- $course_info['show_score'] == 1
- ) {
- $extra_conditions .= " AND (u.user_id = ".api_get_user_id()." AND work.active IN (0, 1)) ";
- } else {
- $extra_conditions .= ' AND work.active IN (0, 1) ';
- }
- }
- $extra_conditions .= " AND parent_id = ".$work_id." ";
- $select = 'SELECT DISTINCT
- u.user_id,
- work.id as id,
- title as title,
- description,
- url,
- sent_date,
- contains_file,
- has_properties,
- view_properties,
- qualification,
- weight,
- allow_text_assignment,
- u.firstname,
- u.lastname,
- u.username,
- parent_id,
- accepted,
- qualificator_id,
- url_correction,
- title_correction
- ';
- if ($getCount) {
- $select = "SELECT DISTINCT count(u.user_id) as count ";
- }
- $work_assignment = get_work_assignment_by_id($work_id, $courseId);
- if (!empty($studentId)) {
- $where_condition .= " AND u.user_id = ".intval($studentId);
- }
- $sql = " $select
- FROM $work_table work
- INNER JOIN $user_table u
- ON (work.user_id = u.user_id)
- WHERE
- work.c_id = $course_id AND
- $extra_conditions
- $where_condition
- $condition_session
- AND u.status != ".INVITEE."
- ORDER BY $column $direction";
- if (!empty($start) && !empty($limit)) {
- $sql .= " LIMIT $start, $limit";
- }
- $result = Database::query($sql);
- $works = array();
- if ($getCount) {
- $work = Database::fetch_array($result, 'ASSOC');
- return $work['count'];
- }
- $url = api_get_path(WEB_CODE_PATH).'work/';
- $unoconv = api_get_configuration_value('unoconv.binaries');
- $loadingText = addslashes(get_lang('Loading'));
- $uploadedText = addslashes(get_lang('Uploaded'));
- $failsUploadText = addslashes(get_lang('UplNoFileUploaded'));
- $failsUploadIcon = Display::return_icon(
- 'closed-circle.png',
- '',
- [],
- ICON_SIZE_TINY
- );
- $saveIcon = Display::return_icon(
- 'save.png',
- get_lang('Save'),
- array(),
- ICON_SIZE_SMALL
- );
- $correctionIcon = Display::return_icon(
- 'check-circle.png',
- get_lang('Correction'),
- null,
- ICON_SIZE_SMALL
- );
- $correctionIconSmall = Display::return_icon(
- 'check-circle.png',
- get_lang('Correction'),
- null,
- ICON_SIZE_TINY
- );
- $rateIcon = Display::return_icon(
- 'rate_work.png',
- get_lang('CorrectAndRate'),
- array(),
- ICON_SIZE_SMALL
- );
- while ($work = Database::fetch_array($result, 'ASSOC')) {
- $item_id = $work['id'];
- // Get the author ID for that document from the item_property table
- $is_author = false;
- $can_read = false;
- $owner_id = $work['user_id'];
- /* Because a bug found when saving items using the api_item_property_update()
- the field $item_property_data['insert_user_id'] is not reliable. */
- if (!$is_allowed_to_edit && $owner_id == api_get_user_id()) {
- $is_author = true;
- }
- if ($course_info['show_score'] == 0) {
- $can_read = true;
- }
- $qualification_exists = false;
- if (!empty($work_data['qualification']) &&
- intval($work_data['qualification']) > 0
- ) {
- $qualification_exists = true;
- }
- $qualification_string = '';
- if ($qualification_exists) {
- if ($work['qualification'] == '') {
- $qualification_string = Display::label('-');
- } else {
- $qualification_string = formatWorkScore($work['qualification'], $work_data['qualification']);
- }
- }
- $work['qualification_score'] = $work['qualification'];
- $add_string = '';
- $time_expires = '';
- if (!empty($work_assignment['expires_on'])) {
- $time_expires = api_strtotime(
- $work_assignment['expires_on'],
- 'UTC'
- );
- }
- if (!empty($work_assignment['expires_on']) &&
- !empty($time_expires) && ($time_expires < api_strtotime($work['sent_date'], 'UTC'))) {
- $add_string = Display::label(get_lang('Expired'), 'important').' - ';
- }
- if (($can_read && $work['accepted'] == '1') ||
- ($is_author && in_array($work['accepted'], array('1', '0'))) ||
- ($is_allowed_to_edit || api_is_drh())
- ) {
- // Firstname, lastname, username
- $work['fullname'] = Display::div(
- api_get_person_name($work['firstname'], $work['lastname']),
- ['class' => 'work-name']
- );
- // Title
- $work['title_clean'] = $work['title'];
- $work['title'] = Security::remove_XSS($work['title']);
- if (strlen($work['title']) > 30) {
- $short_title = substr($work['title'], 0, 27).'...';
- $work['title'] = Display::span($short_title, array('class' => 'work-title', 'title' => $work['title']));
- } else {
- $work['title'] = Display::div($work['title'], array('class' => 'work-title'));
- }
- // Type.
- $work['type'] = DocumentManager::build_document_icon_tag('file', $work['url']);
- // File name.
- $link_to_download = null;
- // If URL is present then there's a file to download keep BC.
- if ($work['contains_file'] || !empty($work['url'])) {
- $link_to_download = '<a href="'.$url.'download.php?id='.$item_id.'&'.api_get_cidreq().'">'.$saveIcon.'</a> ';
- }
- $send_to = Portfolio::share(
- 'work',
- $work['id'],
- array('style' => 'white-space:nowrap;')
- );
- $feedback = '';
- $count = getWorkCommentCount($item_id, $course_info);
- if (!is_null($count) && !empty($count)) {
- if ($qualification_exists) {
- $feedback .= ' ';
- }
- $feedback .= '<a href="'.$url.'view.php?'.api_get_cidreq().'&id='.$item_id.'" title="'.get_lang('View').'">'.
- $count.' '.Display::returnFontAwesomeIcon('comments-o').'</a> ';
- }
- $correction = '';
- $hasCorrection = '';
- if (!empty($work['url_correction'])) {
- $hasCorrection = Display::url(
- $correctionIcon,
- api_get_path(WEB_CODE_PATH).'work/download.php?id='.$item_id.'&'.api_get_cidreq().'&correction=1'
- );
- }
- if ($qualification_exists) {
- $work['qualification'] = $qualification_string.$feedback;
- } else {
- $work['qualification'] = $qualification_string.$feedback.$hasCorrection;
- }
- $work['qualification_only'] = $qualification_string;
- // Date.
- $work_date = api_get_local_time($work['sent_date']);
- $date = date_to_str_ago($work['sent_date']).' '.$work_date;
- $work['formatted_date'] = $work_date.' '.$add_string;
- $work['sent_date_from_db'] = $work['sent_date'];
- $work['sent_date'] = '<div class="work-date" title="'.$date.'">'.$add_string.' '.Display::dateToStringAgoAndLongDate($work['sent_date']).'</div>';
- $work['status'] = $hasCorrection;
- $work['has_correction'] = $hasCorrection;
- // Actions.
- $action = '';
- if (api_is_allowed_to_edit()) {
- $action .= '<a href="'.$url.'view.php?'.api_get_cidreq().'&id='.$item_id.'" title="'.get_lang('View').'">'.$rateIcon.'</a> ';
- if ($unoconv && empty($work['contains_file'])) {
- $action .= '<a href="'.$url.'work_list_all.php?'.api_get_cidreq().'&id='.$work_id.'&action=export_to_doc&item_id='.$item_id.'" title="'.get_lang('ExportToDoc').'" >'.
- Display::return_icon('export_doc.png', get_lang('ExportToDoc'), array(), ICON_SIZE_SMALL).'</a> ';
- }
- $alreadyUploaded = '';
- if (!empty($work['url_correction'])) {
- $alreadyUploaded = '<br />'.$work['title_correction'].' '.$correctionIconSmall;
- }
- $correction = '
- <form
- id="file_upload_'.$item_id.'"
- class="work_correction_file_upload file_upload_small fileinput-button"
- 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"
- >
- <div id="progress_'.$item_id.'" class="text-center button-load">
- '.addslashes(get_lang('ClickOrDropOneFileHere')).'
- '.Display::return_icon('upload_file.png', get_lang('Correction'), [], ICON_SIZE_TINY).'
- '.$alreadyUploaded.'
- </div>
- <input id="file_'.$item_id.'" type="file" name="file" class="" multiple>
- </form>
- ';
- $correction .= "<script>
- $(document).ready(function() {
- $('.work_correction_file_upload').each(function () {
- $(this).fileupload({
- dropZone: $(this)
- });
- });
- $('#file_upload_".$item_id."').fileupload({
- add: function (e, data) {
- $('#progress_$item_id').html();
- //$('#file_$item_id').remove();
- data.context = $('#progress_$item_id').html('$loadingText <br /> <em class=\"fa fa-spinner fa-pulse fa-fw\"></em>');
- data.submit();
- $(this).removeClass('hover');
- },
- dragover: function (e, data) {
- $(this).addClass('hover');
- },
- done: function (e, data) {
- if (data._response.result.name) {
- $('#progress_$item_id').html('$uploadedText '+data._response.result.result+'<br />'+data._response.result.name);
- } else {
- $('#progress_$item_id').html('$failsUploadText $failsUploadIcon');
- }
- $(this).removeClass('hover');
- }
- });
- $('#file_upload_".$item_id."').on('dragleave', function (e) {
- // dragleave callback implementation
- $(this).removeClass('hover');
- });
- });
- </script>";
- if ($locked) {
- if ($qualification_exists) {
- $action .= Display::return_icon('edit_na.png', get_lang('CorrectAndRate'), array(), ICON_SIZE_SMALL);
- } else {
- $action .= Display::return_icon('edit_na.png', get_lang('Comment'), array(), ICON_SIZE_SMALL);
- }
- } else {
- if ($qualification_exists) {
- $action .= '<a href="'.$url.'edit.php?'.api_get_cidreq().'&item_id='.$item_id.'&id='.$work['parent_id'].'" title="'.get_lang('Edit').'" >'.
- Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>';
- } else {
- $action .= '<a href="'.$url.'edit.php?'.api_get_cidreq().'&item_id='.$item_id.'&id='.$work['parent_id'].'" title="'.get_lang('Modify').'">'.
- Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>';
- }
- }
- if ($work['contains_file']) {
- if ($locked) {
- $action .= Display::return_icon(
- 'move_na.png',
- get_lang('Move'),
- array(),
- ICON_SIZE_SMALL
- );
- } else {
- $action .= '<a href="'.$url.'work.php?'.api_get_cidreq().'&action=move&item_id='.$item_id.'&id='.$work['parent_id'].'" title="'.get_lang('Move').'">'.
- Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL).'</a>';
- }
- }
- if ($work['accepted'] == '1') {
- $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').'" >'.
- Display::return_icon('visible.png', get_lang('Invisible'), array(), ICON_SIZE_SMALL).'</a>';
- } else {
- $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').'" >'.
- Display::return_icon('invisible.png', get_lang('Visible'), array(), ICON_SIZE_SMALL).'</a> ';
- }
- if ($locked) {
- $action .= Display::return_icon('delete_na.png', get_lang('Delete'), '', ICON_SIZE_SMALL);
- } else {
- $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('ConfirmYourChoice'), ENT_QUOTES))."'".')) return false;" title="'.get_lang('Delete').'" >'.
- Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
- }
- } elseif ($is_author && (empty($work['qualificator_id']) || $work['qualificator_id'] == 0)) {
- $action .= '<a href="'.$url.'view.php?'.api_get_cidreq().'&id='.$item_id.'" title="'.get_lang('View').'">'.
- Display::return_icon('default.png', get_lang('View'), array(), ICON_SIZE_SMALL).'</a>';
- if (api_get_course_setting('student_delete_own_publication') == 1) {
- if (api_is_allowed_to_session_edit(false, true)) {
- $action .= '<a href="'.$url.'edit.php?'.api_get_cidreq().'&item_id='.$item_id.'&id='.$work['parent_id'].'" title="'.get_lang('Modify').'">'.
- Display::return_icon('edit.png', get_lang('Comment'), array(), ICON_SIZE_SMALL).'</a>';
- }
- $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('ConfirmYourChoice'), ENT_QUOTES))."'".')) return false;" title="'.get_lang('Delete').'" >'.
- Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
- }
- } else {
- $action .= '<a href="'.$url.'view.php?'.api_get_cidreq().'&id='.$item_id.'" title="'.get_lang('View').'">'.
- Display::return_icon('default.png', get_lang('View'), array(), ICON_SIZE_SMALL).'</a>';
- }
- // Status.
- if (empty($work['qualificator_id'])) {
- $qualificator_id = Display::label(get_lang('NotRevised'), 'warning');
- } else {
- $qualificator_id = Display::label(get_lang('Revised'), 'success');
- }
- $work['qualificator_id'] = $qualificator_id.' '.$hasCorrection;
- $work['actions'] = '<div class="work-action">'.$send_to.$link_to_download.$action.'</div>';
- $work['correction'] = $correction;
- $works[] = $work;
- }
- }
- return $works;
- }
- }
- /**
- * Send reminder to users who have not given the task
- *
- * @param int
- * @return array
- * @author cvargas carlos.vargas@beeznest.com cfasanando, christian.fasanado@beeznest.com
- */
- function send_reminder_users_without_publication($task_data)
- {
- $_course = api_get_course_info();
- $task_id = $task_data['id'];
- $task_title = !empty($task_data['title']) ? $task_data['title'] : basename($task_data['url']);
- $subject = '['.api_get_setting('siteName').'] ';
- // The body can be as long as you wish, and any combination of text and variables
- $content = get_lang('ReminderToSubmitPendingTask')."\n".get_lang('CourseName').' : '.$_course['name']."\n";
- $content .= get_lang('WorkName').' : '.$task_title."\n";
- $list_users = get_list_users_without_publication($task_id);
- $mails_sent_to = array();
- foreach ($list_users as $user) {
- $name_user = api_get_person_name($user[1], $user[0], null, PERSON_NAME_EMAIL_ADDRESS);
- $dear_line = get_lang('Dear')." ".api_get_person_name($user[1], $user[0]).", \n\n";
- $body = $dear_line.$content;
- MessageManager::send_message($user[3], $subject, $body);
- $mails_sent_to[] = $name_user;
- }
- return $mails_sent_to;
- }
- /**
- * Sends an email to the students of a course when a homework is created
- *
- * @param int $courseId course_id
- * @param int $sessionId session_id
- * @param int $workId work_id
- *
- *
- * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
- * @author Julio Montoya <gugli100@gmail.com> Adding session support - 2011
- */
- function send_email_on_homework_creation($courseId, $sessionId = 0, $workId)
- {
- $courseInfo = api_get_course_info_by_id($courseId);
- $courseCode = $courseInfo['code'];
- // Get the students of the course
- if (empty($session_id)) {
- $students = CourseManager::get_student_list_from_course_code($courseCode);
- } else {
- $students = CourseManager::get_student_list_from_course_code($courseCode, true, $sessionId);
- }
- $emailsubject = '['.api_get_setting('siteName').'] '.get_lang('HomeworkCreated');
- $currentUser = api_get_user_info(api_get_user_id());
- if (!empty($students)) {
- foreach ($students as $student) {
- $user_info = api_get_user_info($student["user_id"]);
- if (!empty($user_info["mail"])) {
- $name_user = api_get_person_name(
- $user_info["firstname"],
- $user_info["lastname"],
- null,
- PERSON_NAME_EMAIL_ADDRESS
- );
- $link = api_get_path(WEB_CODE_PATH).'work/work_list_all.php?'.api_get_cidreq().'&id='.$workId;
- $emailbody = get_lang('Dear')." ".$name_user.",\n\n";
- $emailbody .= get_lang('HomeworkHasBeenCreatedForTheCourse')." ".$courseCode.". "."\n\n".
- '<a href="'.$link.'">'.get_lang('PleaseCheckHomeworkPage').'</a>';
- $emailbody .= "\n\n".api_get_person_name($currentUser["firstname"], $currentUser["lastname"]);
- $additionalParameters = array(
- 'smsType' => SmsPlugin::ASSIGNMENT_BEEN_CREATED_COURSE,
- 'userId' => $student["user_id"],
- 'courseTitle' => $courseCode,
- 'link' => $link
- );
- api_mail_html(
- $name_user,
- $user_info["mail"],
- $emailsubject,
- $emailbody,
- api_get_person_name(
- $currentUser["firstname"],
- $currentUser["lastname"],
- null,
- PERSON_NAME_EMAIL_ADDRESS
- ),
- $currentUser["mail"],
- null,
- null,
- null,
- $additionalParameters
- );
- }
- }
- }
- }
- /**
- * @param string $url
- * @return bool
- */
- function is_work_exist_by_url($url)
- {
- $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $url = Database::escape_string($url);
- $sql = "SELECT id FROM $work_table WHERE url='$url'";
- $result = Database::query($sql);
- if (Database::num_rows($result) > 0) {
- $row = Database::fetch_row($result);
- if (empty($row)) {
- return false;
- } else {
- return true;
- }
- } else {
- return false;
- }
- }
- /**
- * Check if a user is the author of a work document.
- * @param int $itemId
- * @param int $userId
- * @param int $courseId
- * @param int $sessionId
- * @return bool
- */
- function user_is_author($itemId, $userId = null, $courseId = null, $sessionId = null)
- {
- if (empty($itemId)) {
- return false;
- }
- if (empty($userId)) {
- $userId = api_get_user_id();
- }
- $isAuthor = false;
- $is_allowed_to_edit = api_is_allowed_to_edit();
- if ($is_allowed_to_edit) {
- $isAuthor = true;
- } else {
- if (empty($courseId)) {
- $courseId = api_get_course_int_id();
- }
- if (empty($sessionId)) {
- $sessionId = api_get_session_id();
- }
- $data = api_get_item_property_info($courseId, 'work', $itemId, $sessionId);
- if ($data['insert_user_id'] == $userId) {
- $isAuthor = true;
- }
- $workData = get_work_data_by_id($itemId);
- if ($workData['user_id'] == $userId) {
- $isAuthor = true;
- }
- }
- if (!$isAuthor) {
- return false;
- }
- return $isAuthor;
- }
- /**
- * Get list of users who have not given the task
- * @param int
- * @param int
- * @return array
- * @author cvargas
- * @author Julio Montoya <gugli100@gmail.com> Fixing query
- */
- function get_list_users_without_publication($task_id, $studentId = null)
- {
- $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
- $table_user = Database::get_main_table(TABLE_MAIN_USER);
- $session_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
- $users = getAllUserToWork($task_id, api_get_course_int_id());
- $users = array_column($users, 'user_id');
- // Condition for the session
- $session_id = api_get_session_id();
- $course_id = api_get_course_int_id();
- $task_id = intval($task_id);
- $sessionCondition = api_get_session_condition($session_id);
- if ($session_id == 0) {
- $sql = "SELECT user_id as id FROM $work_table
- WHERE
- c_id = $course_id AND
- parent_id = '$task_id' AND
- active IN (0, 1)";
- } else {
- $sql = "SELECT user_id as id FROM $work_table
- WHERE
- c_id = $course_id AND
- parent_id = '$task_id' $sessionCondition AND
- active IN (0, 1)";
- }
- $result = Database::query($sql);
- $users_with_tasks = array();
- while ($row = Database::fetch_array($result)) {
- $users_with_tasks[] = $row['id'];
- }
- if ($session_id == 0) {
- $sql_users = "SELECT cu.user_id, u.lastname, u.firstname, u.email
- FROM $table_course_user AS cu, $table_user AS u
- WHERE u.status != 1 and cu.c_id='".$course_id."' AND u.user_id = cu.user_id";
- } else {
- $sql_users = "SELECT cu.user_id, u.lastname, u.firstname, u.email
- FROM $session_course_rel_user AS cu, $table_user AS u
- WHERE
- u.status != 1 AND
- cu.c_id='".$course_id."' AND
- u.user_id = cu.user_id AND
- cu.session_id = '".$session_id."'";
- }
- if (!empty($studentId)) {
- $sql_users .= " AND u.user_id = ".intval($studentId);
- }
- $group_id = api_get_group_id();
- $new_group_user_list = array();
- if ($group_id) {
- $groupInfo = GroupManager::get_group_properties($group_id);
- $group_user_list = GroupManager::get_subscribed_users($groupInfo);
- if (!empty($group_user_list)) {
- foreach ($group_user_list as $group_user) {
- $new_group_user_list[] = $group_user['user_id'];
- }
- }
- }
- $result_users = Database::query($sql_users);
- $users_without_tasks = array();
- while ($rowUsers = Database::fetch_array($result_users)) {
- $userId = $rowUsers['user_id'];
- if (in_array($userId, $users_with_tasks)) {
- continue;
- }
- if ($group_id && !in_array($userId, $new_group_user_list)) {
- continue;
- }
- if (!empty($users)) {
- if (!in_array($userId, $users)) {
- continue;
- }
- }
- $row_users = [];
- $row_users[0] = $rowUsers['lastname'];
- $row_users[1] = $rowUsers['firstname'];
- $row_users[2] = Display::encrypted_mailto_link($rowUsers['email']);
- $row_users[3] = $userId;
- $users_without_tasks[] = $row_users;
- }
- return $users_without_tasks;
- }
- /**
- * Display list of users who have not given the task
- *
- * @param int task id
- * @param int $studentId
- * @return array
- * @author cvargas carlos.vargas@beeznest.com cfasanando, christian.fasanado@beeznest.com
- * @author Julio Montoya <gugli100@gmail.com> Fixes
- */
- function display_list_users_without_publication($task_id, $studentId = null)
- {
- global $origin;
- $table_header[] = array(get_lang('LastName'), true);
- $table_header[] = array(get_lang('FirstName'), true);
- $table_header[] = array(get_lang('Email'), true);
- $data = get_list_users_without_publication($task_id);
- $sorting_options = array();
- $sorting_options['column'] = 1;
- $paging_options = array();
- $my_params = array();
- if (isset($_GET['edit_dir'])) {
- $my_params['edit_dir'] = Security::remove_XSS($_GET['edit_dir']);
- }
- if (isset($_GET['list'])) {
- $my_params['list'] = Security::remove_XSS($_GET['list']);
- }
- $my_params['origin'] = $origin;
- $my_params['id'] = intval($_GET['id']);
- //$column_show
- $column_show[] = 1;
- $column_show[] = 1;
- $column_show[] = 1;
- Display::display_sortable_config_table(
- 'work',
- $table_header,
- $data,
- $sorting_options,
- $paging_options,
- $my_params,
- $column_show
- );
- }
- /**
- * @param int $documentId
- * @param int $workId
- * @param int $courseId
- */
- function addDocumentToWork($documentId, $workId, $courseId)
- {
- $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_DOCUMENT);
- $params = array(
- 'document_id' => $documentId,
- 'work_id' => $workId,
- 'c_id' => $courseId
- );
- Database::insert($table, $params);
- }
- /**
- * @param int $documentId
- * @param int $workId
- * @param int $courseId
- * @return array
- */
- function getDocumentToWork($documentId, $workId, $courseId)
- {
- $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_DOCUMENT);
- $params = array(
- 'document_id = ? and work_id = ? and c_id = ?' => array($documentId, $workId, $courseId)
- );
- return Database::select('*', $table, array('where' => $params));
- }
- /**
- * @param int $documentId
- * @param int $workId
- * @param int $courseId
- * @param int $sessionId
- * @param int $userId
- * @param int $active
- * @return array
- */
- function getDocumentToWorkPerUser($documentId, $workId, $courseId, $sessionId, $userId, $active = 1)
- {
- $workRel = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_DOCUMENT);
- $work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $documentId = intval($documentId);
- $workId = intval($workId);
- $courseId = intval($courseId);
- $userId = intval($userId);
- $sessionId = intval($sessionId);
- $active = intval($active);
- $sessionCondition = api_get_session_condition($sessionId);
- $sql = "SELECT w.* FROM $work w INNER JOIN $workRel rel ON (w.parent_id = rel.work_id)
- WHERE
- w.document_id = $documentId AND
- w.parent_id = $workId AND
- w.c_id = $courseId
- $sessionCondition AND
- user_id = $userId AND
- active = $active
- ";
- $result = Database::query($sql);
- $workInfo = array();
- if (Database::num_rows($result)) {
- $workInfo = Database::fetch_array($result, 'ASSOC');
- }
- return $workInfo;
- }
- /**
- *
- * @param int $workId
- * @param int $courseId
- * @return array
- */
- function getAllDocumentToWork($workId, $courseId)
- {
- $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_DOCUMENT);
- $params = array(
- 'work_id = ? and c_id = ?' => array($workId, $courseId)
- );
- return Database::select('*', $table, array('where' => $params));
- }
- /**
- * @param int $documentId
- * @param int $workId
- * @param int $courseId
- */
- function deleteDocumentToWork($documentId, $workId, $courseId)
- {
- $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_DOCUMENT);
- $params = array(
- 'document_id = ? and work_id = ? and c_id = ?' => array($documentId, $workId, $courseId)
- );
- Database::delete($table, $params);
- }
- /**
- * @param int $userId
- * @param int $workId
- * @param int $courseId
- */
- function addUserToWork($userId, $workId, $courseId)
- {
- $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_USER);
- $params = array(
- 'user_id' => $userId,
- 'work_id' => $workId,
- 'c_id' => $courseId
- );
- Database::insert($table, $params);
- }
- /**
- * @param int $userId
- * @param int $workId
- * @param int $courseId
- * @return array
- */
- function getUserToWork($userId, $workId, $courseId)
- {
- $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_USER);
- $params = array(
- 'user_id = ? and work_id = ? and c_id = ?' => array($userId, $workId, $courseId)
- );
- return Database::select('*', $table, array('where' => $params));
- }
- /**
- * @param int $workId
- * @param int $courseId
- * @param bool $getCount
- * @return array|int
- */
- function getAllUserToWork($workId, $courseId, $getCount = false)
- {
- $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_USER);
- $params = array(
- 'work_id = ? and c_id = ?' => array($workId, $courseId)
- );
- if ($getCount) {
- $count = 0;
- $result = Database::select(
- 'count(user_id) as count',
- $table,
- array('where' => $params),
- 'simple'
- );
- if (!empty($result)) {
- $count = intval($result['count']);
- }
- return $count;
- } else {
- return Database::select('*', $table, array('where' => $params));
- }
- }
- /**
- * @param int $userId
- * @param int $workId
- * @param int $courseId
- */
- function deleteUserToWork($userId, $workId, $courseId)
- {
- $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_REL_USER);
- $params = array(
- 'user_id = ? and work_id = ? and c_id = ?' => array($userId, $workId, $courseId)
- );
- Database::delete($table, $params);
- }
- /**
- * @param int $userId
- * @param int $workId
- * @param int $courseId
- * @return bool
- */
- function userIsSubscribedToWork($userId, $workId, $courseId)
- {
- $subscribedUsers = getAllUserToWork($workId, $courseId);
- if (empty($subscribedUsers)) {
- return true;
- } else {
- $subscribedUsersList = array();
- foreach ($subscribedUsers as $item) {
- $subscribedUsersList[] = $item['user_id'];
- }
- if (in_array($userId, $subscribedUsersList)) {
- return true;
- }
- }
- return false;
- }
- /**
- * Get the list of students that have to submit their work
- * @param integer $workId The internal ID of the assignment
- * @param integer $courseId The course ID
- * @param integer $groupId The group ID, if any
- * @param integer $sessionId The session ID, if any
- * @param bool $getCount Whether we want just the amount or the full result
- * @return array|int An integer (if we just asked for the count) or an array of users
- */
- function getStudentSubscribedToWork(
- $workId,
- $courseId,
- $groupId = null,
- $sessionId = null,
- $getCount = false
- ) {
- $usersInWork = null;
- $usersInCourse = null;
- if (empty($groupId)) {
- $courseInfo = api_get_course_info_by_id($courseId);
- $status = STUDENT;
- if (!empty($sessionId)) {
- $status = 0;
- }
- $usersInCourse = CourseManager::get_user_list_from_course_code(
- $courseInfo['code'],
- $sessionId,
- null,
- null,
- $status,
- $getCount
- );
- } else {
- $usersInCourse = GroupManager::get_users(
- $groupId,
- false,
- null,
- null,
- $getCount,
- $courseId
- );
- }
- $usersInWork = getAllUserToWork($workId, $courseId, $getCount);
- if (empty($usersInWork)) {
- return $usersInCourse;
- } else {
- return $usersInWork;
- }
- }
- /**
- * @param int $userId
- * @param int $workId
- * @param int $courseId
- * @return bool
- */
- function allowOnlySubscribedUser($userId, $workId, $courseId)
- {
- if (api_is_platform_admin() || api_is_allowed_to_edit()) {
- return true;
- }
- if (userIsSubscribedToWork($userId, $workId, $courseId) == false) {
- api_not_allowed(true);
- }
- }
- /**
- * @param int $workId
- * @param array $courseInfo
- * @param int $documentId
- * @return array
- */
- function getDocumentTemplateFromWork($workId, $courseInfo, $documentId)
- {
- $documents = getAllDocumentToWork($workId, $courseInfo['real_id']);
- if (!empty($documents)) {
- foreach ($documents as $doc) {
- if ($documentId != $doc['document_id']) {
- continue;
- }
- $docData = DocumentManager::get_document_data_by_id($doc['document_id'], $courseInfo['code']);
- $fileInfo = pathinfo($docData['path']);
- if ($fileInfo['extension'] == 'html') {
- if (file_exists($docData['absolute_path']) && is_file($docData['absolute_path'])) {
- $docData['file_content'] = file_get_contents($docData['absolute_path']);
- return $docData;
- }
- }
- }
- }
- return array();
- }
- /**
- * @param int $workId
- * @param array $courseInfo
- * @return string
- */
- function getAllDocumentsFromWorkToString($workId, $courseInfo)
- {
- $documents = getAllDocumentToWork($workId, $courseInfo['real_id']);
- $content = null;
- if (!empty($documents)) {
- $content .= '<ul class="nav nav-list well">';
- $content .= '<li class="nav-header">'.get_lang('Documents').'</li>';
- foreach ($documents as $doc) {
- $docData = DocumentManager::get_document_data_by_id($doc['document_id'], $courseInfo['code']);
- if ($docData) {
- $content .= '<li><a target="_blank" href="'.$docData['url'].'">'.$docData['title'].'</a></li>';
- }
- }
- $content .= '</ul><br />';
- }
- return $content;
- }
- /**
- * Returns fck editor toolbar
- * @return array
- */
- function getWorkDescriptionToolbar()
- {
- return array(
- 'ToolbarStartExpanded' => 'true',
- 'ToolbarSet' => 'Work',
- 'Width' => '100%',
- 'Height' => '400'
- );
- }
- /**
- * @param array $work
- * @return array
- */
- function getWorkComments($work)
- {
- $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
- $userTable = Database::get_main_table(TABLE_MAIN_USER);
- $courseId = intval($work['c_id']);
- $workId = intval($work['id']);
- if (empty($courseId) || empty($workId)) {
- return array();
- }
- $sql = "SELECT
- c.id,
- c.user_id
- FROM $commentTable c
- INNER JOIN $userTable u
- ON (u.id = c.user_id)
- WHERE c_id = $courseId AND work_id = $workId
- ORDER BY sent_at
- ";
- $result = Database::query($sql);
- $comments = Database::store_result($result, 'ASSOC');
- if (!empty($comments)) {
- foreach ($comments as &$comment) {
- $userInfo = api_get_user_info($comment['user_id']);
- $comment['picture'] = $userInfo['avatar'];
- $comment['complete_name'] = $userInfo['complete_name_with_username'];
- $commentInfo = getWorkComment($comment['id']);
- if (!empty($commentInfo)) {
- $comment = array_merge($comment, $commentInfo);
- }
- }
- }
- return $comments;
- }
- /**
- * Get total score from a work list
- * @param $workList
- * @return int|null
- */
- function getTotalWorkScore($workList)
- {
- $count = 0;
- foreach ($workList as $data) {
- $count += $data['qualification_score'];
- }
- return $count;
- }
- /**
- * Get comment count from a work list (docs sent by students)
- * @param array $workList
- * @param array $courseInfo
- * @return int|null
- */
- function getTotalWorkComment($workList, $courseInfo = array())
- {
- if (empty($courseInfo)) {
- $courseInfo = api_get_course_info();
- }
- $count = 0;
- foreach ($workList as $data) {
- $count += getWorkCommentCount($data['id'], $courseInfo);
- }
- return $count;
- }
- /**
- * Get comment count for a specific work sent by a student.
- * @param int $id
- * @param array $courseInfo
- * @return int
- */
- function getWorkCommentCount($id, $courseInfo = array())
- {
- if (empty($courseInfo)) {
- $courseInfo = api_get_course_info();
- }
- $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
- $id = intval($id);
- $sql = "SELECT count(*) as count
- FROM $commentTable
- WHERE work_id = $id AND c_id = ".$courseInfo['real_id'];
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- $comment = Database::fetch_array($result);
- return $comment['count'];
- }
- return 0;
- }
- /**
- * Get comment count for a specific parent
- * @param int $parentId
- * @param array $courseInfo
- * @param int $sessionId
- * @return int
- */
- function getWorkCommentCountFromParent(
- $parentId,
- $courseInfo = array(),
- $sessionId = 0
- ) {
- if (empty($courseInfo)) {
- $courseInfo = api_get_course_info();
- }
- if (empty($sessionId)) {
- $sessionId = api_get_session_id();
- } else {
- $sessionId = intval($sessionId);
- }
- $work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
- $parentId = intval($parentId);
- $sessionCondition = api_get_session_condition($sessionId, false, false, 'w.session_id');
- $sql = "SELECT count(*) as count
- FROM $commentTable c INNER JOIN $work w
- ON c.c_id = w.c_id AND w.id = c.work_id
- WHERE
- $sessionCondition AND
- parent_id = $parentId AND
- w.c_id = ".$courseInfo['real_id'];
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- $comment = Database::fetch_array($result);
- return $comment['count'];
- }
- return 0;
- }
- /**
- * Get last work information from parent
- * @param int $parentId
- * @param array $courseInfo
- * @param int $sessionId
- * @return int
- */
- function getLastWorkStudentFromParent(
- $parentId,
- $courseInfo = array(),
- $sessionId = 0
- ) {
- if (empty($courseInfo)) {
- $courseInfo = api_get_course_info();
- }
- if (empty($sessionId)) {
- $sessionId = api_get_session_id();
- } else {
- $sessionId = intval($sessionId);
- }
- $work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $sessionCondition = api_get_session_condition($sessionId, false);
- $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
- $parentId = intval($parentId);
- $sql = "SELECT w.*
- FROM $commentTable c INNER JOIN $work w
- ON c.c_id = w.c_id AND w.id = c.work_id
- WHERE
- $sessionCondition AND
- parent_id = $parentId AND
- w.c_id = ".$courseInfo['real_id']."
- ORDER BY w.sent_date
- LIMIT 1
- ";
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- $comment = Database::fetch_array($result, 'ASSOC');
- return $comment;
- }
- return array();
- }
- /**
- * Get last work information from parent
- * @param array $parentInfo
- * @param array $courseInfo
- * @param int $sessionId
- * @return int
- */
- function getLastWorkStudentFromParentByUser(
- $userId,
- $parentInfo,
- $courseInfo = array(),
- $sessionId = 0
- ) {
- if (empty($courseInfo)) {
- $courseInfo = api_get_course_info();
- }
- if (empty($sessionId)) {
- $sessionId = api_get_session_id();
- } else {
- $sessionId = intval($sessionId);
- }
- $userId = intval($userId);
- $work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- if (empty($parentInfo)) {
- return false;
- }
- $parentId = $parentInfo['id'];
- $sessionCondition = api_get_session_condition($sessionId);
- $sql = "SELECT *
- FROM $work
- WHERE
- user_id = $userId
- $sessionCondition AND
- parent_id = $parentId AND
- c_id = ".$courseInfo['real_id']."
- ORDER BY sent_date DESC
- LIMIT 1
- ";
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- $work = Database::fetch_array($result, 'ASSOC');
- $work['qualification_rounded'] = formatWorkScore($work['qualification'], $parentInfo['qualification']);
- return $work;
- }
- return array();
- }
- /**
- * @param float $score
- * @param int $weight
- *
- * @return string
- */
- function formatWorkScore($score, $weight)
- {
- $label = 'info';
- $weight = (int) $weight;
- $relativeScore = 0;
- if (!empty($weight)) {
- $relativeScore = $score / $weight;
- }
- if ($relativeScore < 0.5) {
- $label = 'important';
- } elseif ($relativeScore < 0.75) {
- $label = 'warning';
- }
- return Display::label(
- api_number_format($score, 1).' / '.$weight,
- $label
- );
- }
- /**
- * @param int $id comment id
- * @param array $courseInfo
- * @return string
- */
- function getWorkComment($id, $courseInfo = array())
- {
- if (empty($courseInfo)) {
- $courseInfo = api_get_course_info();
- }
- if (empty($courseInfo['real_id'])) {
- return array();
- }
- $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
- $id = intval($id);
- $sql = "SELECT * FROM $commentTable
- WHERE id = $id AND c_id = ".$courseInfo['real_id'];
- $result = Database::query($sql);
- $comment = array();
- if (Database::num_rows($result)) {
- $comment = Database::fetch_array($result, 'ASSOC');
- $filePath = null;
- $fileUrl = null;
- $deleteUrl = null;
- $fileName = null;
- if (!empty($comment['file'])) {
- $work = get_work_data_by_id($comment['work_id']);
- $workParent = get_work_data_by_id($work['parent_id']);
- $filePath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/work/'.$workParent['url'].'/'.$comment['file'];
- $fileUrl = api_get_path(WEB_CODE_PATH).'work/download_comment_file.php?comment_id='.$id.'&'.api_get_cidreq();
- $deleteUrl = api_get_path(WEB_CODE_PATH).'work/view.php?'.api_get_cidreq().'&id='.$comment['work_id'].'&action=delete_attachment&comment_id='.$id;
- $fileParts = explode('_', $comment['file']);
- $fileName = str_replace($fileParts[0].'_'.$fileParts[1].'_', '', $comment['file']);
- }
- $comment['delete_file_url'] = $deleteUrl;
- $comment['file_path'] = $filePath;
- $comment['file_url'] = $fileUrl;
- $comment['file_name_to_show'] = $fileName;
- $comment['sent_at_with_label'] = Display::dateToStringAgoAndLongDate($comment['sent_at']);
- }
- return $comment;
- }
- /**
- * @param int $id
- * @param array $courseInfo
- */
- function deleteCommentFile($id, $courseInfo = array())
- {
- $workComment = getWorkComment($id, $courseInfo);
- if (isset($workComment['file']) && !empty($workComment['file'])) {
- if (file_exists($workComment['file_path'])) {
- $result = my_delete($workComment['file_path']);
- if ($result) {
- $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
- $params = array('file' => '');
- Database::update(
- $commentTable,
- $params,
- array('id = ? AND c_id = ? ' => array($workComment['id'], $workComment['c_id']))
- );
- }
- }
- }
- }
- /**
- * Adds a comments to the work document
- * @param array $courseInfo
- * @param int $userId
- * @param array $parentWork
- * @param array $work
- * @param array $data
- * @return int
- */
- function addWorkComment($courseInfo, $userId, $parentWork, $work, $data)
- {
- $fileData = isset($data['attachment']) ? $data['attachment'] : null;
- $commentTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT_COMMENT);
- // If no attachment and no comment then don't save comment
- if (empty($fileData['name']) && empty($data['comment'])) {
- return false;
- }
- $params = array(
- 'work_id' => $work['id'],
- 'c_id' => $work['c_id'],
- 'user_id' => $userId,
- 'comment' => $data['comment'],
- 'sent_at' => api_get_utc_datetime()
- );
- $commentId = Database::insert($commentTable, $params);
- if ($commentId) {
- Display::addFlash(
- Display::return_message(get_lang('CommentAdded'))
- );
- $sql = "UPDATE $commentTable SET id = iid WHERE iid = $commentId";
- Database::query($sql);
- }
- $userIdListToSend = array();
- if (api_is_allowed_to_edit()) {
- if (isset($data['send_email']) && $data['send_email']) {
- // Teacher sends a feedback
- $userIdListToSend = array($work['user_id']);
- }
- } else {
- $sessionId = api_get_session_id();
- if (empty($sessionId)) {
- $teachers = CourseManager::get_teacher_list_from_course_code(
- $courseInfo['code']
- );
- if (!empty($teachers)) {
- $userIdListToSend = array_keys($teachers);
- }
- } else {
- $teachers = SessionManager::getCoachesByCourseSession(
- $sessionId,
- $courseInfo['code']
- );
- if (!empty($teachers)) {
- $userIdListToSend = array_values($teachers);
- }
- }
- }
- $url = api_get_path(WEB_CODE_PATH).'work/view.php?'.api_get_cidreq().'&id='.$work['id'];
- $subject = sprintf(get_lang('ThereIsANewWorkFeedback'), $parentWork['title']);
- $content = sprintf(get_lang('ThereIsANewWorkFeedbackInWorkXHere'), $work['title'], $url);
- if (!empty($userIdListToSend)) {
- foreach ($userIdListToSend as $userIdToSend) {
- MessageManager::send_message_simple(
- $userIdToSend,
- $subject,
- $content
- );
- }
- }
- if (!empty($commentId) && !empty($fileData)) {
- $workParent = get_work_data_by_id($work['parent_id']);
- if (!empty($workParent)) {
- $uploadDir = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/work'.$workParent['url'];
- $newFileName = 'comment_'.$commentId.'_'.php2phps(api_replace_dangerous_char($fileData['name']));
- $newFilePath = $uploadDir.'/'.$newFileName;
- $result = move_uploaded_file($fileData['tmp_name'], $newFilePath);
- if ($result) {
- $params = array('file' => $newFileName);
- Database::update(
- $commentTable,
- $params,
- array('id = ? AND c_id = ? ' => array($commentId, $work['c_id']))
- );
- }
- }
- }
- }
- /**
- * @param array $work
- *
- * @return string
- */
- function getWorkCommentForm($work, $workParent)
- {
- $url = api_get_path(WEB_CODE_PATH).'work/view.php?id='.$work['id'].'&action=send_comment&'.api_get_cidreq();
- $form = new FormValidator(
- 'work_comment',
- 'post',
- $url,
- '',
- array('enctype' => "multipart/form-data")
- );
- $qualification = $workParent['qualification'];
- if (api_is_allowed_to_edit()) {
- if (!empty($qualification) && intval($qualification) > 0) {
- $form->addFloat(
- 'qualification',
- array(get_lang('Qualification'), " / ".$qualification),
- false,
- [],
- false,
- 0,
- $qualification
- );
- $form->addFile('file', get_lang('Correction'));
- $form->setDefaults(['qualification' => $work['qualification']]);
- }
- }
- $form->addHtmlEditor('comment', get_lang('Comment'), false);
- $form->addFile('attachment', get_lang('Attachment'));
- $form->addElement('hidden', 'id', $work['id']);
- if (api_is_allowed_to_edit()) {
- $form->addCheckBox(
- 'send_email',
- null,
- get_lang('SendMailToStudent')
- );
- }
- $form->addButtonSend(get_lang('Send'), 'button');
- return $form->returnForm();
- }
- /**
- * @param array $homework result of get_work_assignment_by_id()
- * @return string
- */
- function getWorkDateValidationStatus($homework)
- {
- $message = null;
- $has_expired = false;
- $has_ended = false;
- if (!empty($homework)) {
- if (!empty($homework['expires_on']) || !empty($homework['ends_on'])) {
- $time_now = time();
- if (!empty($homework['expires_on'])) {
- $time_expires = api_strtotime($homework['expires_on'], 'UTC');
- $difference = $time_expires - $time_now;
- if ($difference < 0) {
- $has_expired = true;
- }
- }
- if (empty($homework['expires_on'])) {
- $has_expired = false;
- }
- if (!empty($homework['ends_on'])) {
- $time_ends = api_strtotime($homework['ends_on'], 'UTC');
- $difference2 = $time_ends - $time_now;
- if ($difference2 < 0) {
- $has_ended = true;
- }
- }
- $ends_on = api_convert_and_format_date($homework['ends_on']);
- $expires_on = api_convert_and_format_date($homework['expires_on']);
- }
- if ($has_ended) {
- $message = Display::return_message(get_lang('EndDateAlreadyPassed').' '.$ends_on, 'error');
- } elseif ($has_expired) {
- $message = Display::return_message(get_lang('ExpiryDateAlreadyPassed').' '.$expires_on, 'warning');
- } else {
- if ($has_expired) {
- $message = Display::return_message(get_lang('ExpiryDateToSendWorkIs').' '.$expires_on);
- }
- }
- }
- return array(
- 'message' => $message,
- 'has_ended' => $has_ended,
- 'has_expired' => $has_expired
- );
- }
- /**
- * @param FormValidator $form
- * @param int $uploadFormType
- */
- function setWorkUploadForm($form, $uploadFormType = 0)
- {
- $form->addElement('header', get_lang('UploadADocument'));
- $form->addElement('hidden', 'contains_file', 0, array('id'=>'contains_file_id'));
- $form->addElement('hidden', 'active', 1);
- $form->addElement('hidden', 'accepted', 1);
- $form->addElement('text', 'title', get_lang('Title'), array('id' => 'file_upload'));
- $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
- switch ($uploadFormType) {
- case 0:
- // File and text.
- $form->addElement('file', 'file', get_lang('UploadADocument'), 'size="40" onchange="updateDocumentTitle(this.value)"');
- $form->addProgress();
- $form->addHtmlEditor('description', get_lang('Description'), false, false, getWorkDescriptionToolbar());
- break;
- case 1:
- // Only text.
- $form->addHtmlEditor('description', get_lang('Description'), false, false, getWorkDescriptionToolbar());
- $form->addRule('description', get_lang('ThisFieldIsRequired'), 'required');
- break;
- case 2:
- // Only file.
- $form->addElement('file', 'file', get_lang('UploadADocument'), 'size="40" onchange="updateDocumentTitle(this.value)"');
- $form->addProgress();
- $form->addRule('file', get_lang('ThisFieldIsRequired'), 'required');
- break;
- }
- $form->addButtonUpload(get_lang('Upload'), 'submitWork');
- }
- /**
- * @param array $my_folder_data
- * @param array $_course
- * @param bool $isCorrection
- * @param array $workInfo
- * @param array $file
- *
- * @return array
- */
- function uploadWork($my_folder_data, $_course, $isCorrection = false, $workInfo = [], $file = [])
- {
- if (isset($_FILES['file']) && !empty($_FILES['file'])) {
- $file = $_FILES['file'];
- }
- if (empty($file['size'])) {
- return array(
- 'error' => Display:: return_message(
- get_lang('UplUploadFailedSizeIsZero'),
- 'error'
- )
- );
- }
- $updir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/work/'; //directory path to upload
- // Try to add an extension to the file if it has'nt one
- $filename = add_ext_on_mime(stripslashes($file['name']), $file['type']);
- // Replace dangerous characters
- $filename = api_replace_dangerous_char($filename);
- // Transform any .php file in .phps fo security
- $filename = php2phps($filename);
- $filesize = filesize($file['tmp_name']);
- if (empty($filesize)) {
- return array(
- 'error' => Display:: return_message(
- get_lang('UplUploadFailedSizeIsZero'),
- 'error'
- )
- );
- } elseif (!filter_extension($new_file_name)) {
- return array(
- 'error' => Display:: return_message(
- get_lang('UplUnableToSaveFileFilteredExtension'),
- 'error'
- )
- );
- }
- $totalSpace = DocumentManager::documents_total_space($_course['real_id']);
- $course_max_space = DocumentManager::get_course_quota($_course['code']);
- $total_size = $filesize + $totalSpace;
- if ($total_size > $course_max_space) {
- return array(
- 'error' => Display :: return_message(get_lang('NoSpace'), 'error')
- );
- }
- // Compose a unique file name to avoid any conflict
- $new_file_name = api_get_unique_id();
- if ($isCorrection) {
- if (!empty($workInfo['url'])) {
- $new_file_name = basename($workInfo['url']).'_correction';
- } else {
- $new_file_name = $new_file_name.'_correction';
- }
- }
- $curdirpath = basename($my_folder_data['url']);
- // If we come from the group tools the groupid will be saved in $work_table
- if (is_dir($updir.$curdirpath) || empty($curdirpath)) {
- $result = move_uploaded_file(
- $file['tmp_name'],
- $updir.$curdirpath.'/'.$new_file_name
- );
- } else {
- return array(
- 'error' => Display :: return_message(
- get_lang('FolderDoesntExistsInFileSystem'),
- 'error'
- )
- );
- }
- $url = null;
- if ($result) {
- $url = 'work/'.$curdirpath.'/'.$new_file_name;
- } else {
- return false;
- }
- return array(
- 'url' => $url,
- 'filename' => $filename,
- 'filesize' => $filesize,
- 'error' => null
- );
- }
- /**
- * Send an e-mail to users related to this work (course teachers, usually, but
- * might include other group members)
- * @param int $workId
- * @param array $courseInfo
- * @param int $session_id
- */
- function sendAlertToUsers($workId, $courseInfo, $session_id)
- {
- $user_list = array();
- //$workData = get_work_assignment_by_id($workId, $courseInfo['real_id']);
- $workData = get_work_data_by_id($workId, $courseInfo['real_id'], $session_id);
- //last value is to check this is not "just" an edit
- //YW Tis part serve to send a e-mail to the tutors when a new file is sent
- $send = api_get_course_setting('email_alert_manager_on_new_doc');
- if ($send == SEND_EMAIL_EVERYONE || $send == SEND_EMAIL_TEACHERS) {
- // Lets predefine some variables. Be sure to change the from address!
- if (empty($session_id)) {
- //Teachers
- $user_list = CourseManager::get_user_list_from_course_code(
- api_get_course_id(),
- null,
- null,
- null,
- COURSEMANAGER
- );
- } else {
- // Coaches
- $user_list = CourseManager::get_user_list_from_course_code(
- api_get_course_id(),
- $session_id,
- null,
- null,
- 2
- );
- }
- }
- if ($send == SEND_EMAIL_EVERYONE || $send == SEND_EMAIL_STUDENTS) {
- if (!$session_id) {
- $session_id = null;
- }
- $student = CourseManager::get_user_list_from_course_code(
- api_get_course_id(),
- $session_id,
- null,
- null,
- STUDENT,
- null,
- null,
- null,
- null,
- null,
- array(api_get_user_id())
- );
- $user_list = array_merge($user_list, $student);
- }
- if ($send) {
- $senderEmail = api_get_setting('emailAdministrator');
- $senderName = api_get_person_name(
- api_get_setting('administratorName'),
- api_get_setting('administratorSurname'),
- null,
- PERSON_NAME_EMAIL_ADDRESS
- );
- $subject = "[".api_get_setting('siteName')."] ".get_lang('SendMailBody')."\n ".get_lang('CourseName').": ".$courseInfo['name']." ";
- foreach ($user_list as $user_data) {
- $to_user_id = $user_data['user_id'];
- $user_info = api_get_user_info($to_user_id);
- $message = get_lang('SendMailBody')."\n".get_lang('CourseName')." : ".$courseInfo['name']."\n";
- $message .= get_lang('UserName')." : ".api_get_person_name($user_info['firstname'], $user_info['lastname'])."\n";
- $message .= get_lang('DateSent')." : ".api_format_date(api_get_local_time())."\n";
- $url = api_get_path(WEB_CODE_PATH)."work/work.php?cidReq=".$courseInfo['code']."&id_session=".$session_id."&id=".$workData['id'];
- $message .= get_lang('WorkName')." : ".$workData['title']."\n\n".'<a href="'.$url.'">'.get_lang('DownloadLink')."</a>\n";
- //$message .= $url;
- MessageManager::send_message_simple($to_user_id, $subject, $message);
- api_mail_html(
- api_get_person_name(
- $user_info['firstname'].' '.$user_info['lastname'],
- null,
- PERSON_NAME_EMAIL_ADDRESS
- ),
- $user_info['email'],
- $subject,
- $message,
- $senderName,
- $senderEmail
- );
- }
- }
- }
- /**
- * Check if the current uploaded work filename already exists in the current assement
- *
- * @param $filename
- * @param $workId
- * @return mixed
- */
- function checkExistingWorkFileName($filename, $workId)
- {
- $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $filename = Database::escape_string($filename);
- $sql = "SELECT title FROM $work_table
- WHERE parent_id = $workId AND title = '$filename' AND active = 1";
- $result = Database::query($sql);
- return Database::fetch_assoc($result);
- }
- /**
- * @param array $workInfo
- * @param array $values
- * @param array $courseInfo
- * @param int $sessionId
- * @param int $groupId
- * @param int $userId
- * @param array $file
- * @param bool $checkDuplicated
- * @param bool $showFlashMessage
- *
- * @return null|string
- */
- function processWorkForm(
- $workInfo,
- $values,
- $courseInfo,
- $sessionId,
- $groupId,
- $userId,
- $file = [],
- $checkDuplicated = false,
- $showFlashMessage = true
- ) {
- $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $courseId = $courseInfo['real_id'];
- $groupId = intval($groupId);
- $sessionId = intval($sessionId);
- $userId = intval($userId);
- $title = $values['title'];
- $description = $values['description'];
- $contains_file = isset($values['contains_file']) && !empty($values['contains_file']) ? intval($values['contains_file']) : 0;
- $saveWork = true;
- $filename = null;
- $url = null;
- $filesize = null;
- $workData = [];
- $message = null;
- if ($values['contains_file']) {
- if ($checkDuplicated) {
- if (checkExistingWorkFileName($file['name'], $workInfo['id'])) {
- $saveWork = false;
- $result['error'] = get_lang('YouAlreadySentThisFile');
- $workData['error'] = get_lang('UplAlreadyExists');
- } else {
- $result = uploadWork($workInfo, $courseInfo, false, [], $file);
- }
- } else {
- $result = uploadWork($workInfo, $courseInfo, false, [], $file);
- }
- if (isset($result['error'])) {
- if ($showFlashMessage) {
- $message = $result['error'];
- }
- $saveWork = false;
- }
- }
- if ($saveWork) {
- $filename = isset($result['filename']) ? $result['filename'] : null;
- if (empty($title)) {
- $title = isset($result['title']) && !empty($result['title']) ? $result['title'] : get_lang('Untitled');
- }
- $filesize = isset($result['filesize']) ? $result['filesize'] : null;
- $url = isset($result['url']) ? $result['url'] : null;
- }
- if (empty($title)) {
- $title = get_lang('Untitled');
- }
- $groupIid = 0;
- $groupInfo = [];
- if ($groupId) {
- $groupInfo = GroupManager::get_group_properties($groupId);
- $groupIid = $groupInfo['iid'];
- }
- if ($saveWork) {
- $active = '1';
- $params = [
- 'c_id' => $courseId,
- 'url' => $url,
- 'filetype' => 'file',
- 'title' => $title,
- 'description' => $description,
- 'contains_file' => $contains_file,
- 'active' => $active,
- 'accepted' => '1',
- 'qualificator_id' => 0,
- 'document_id' => 0,
- 'weight' => 0,
- 'allow_text_assignment' => 0,
- 'post_group_id' => $groupIid,
- 'sent_date' => api_get_utc_datetime(),
- 'parent_id' => $workInfo['id'],
- 'session_id' => $sessionId ? $sessionId : null,
- 'user_id' => $userId,
- 'has_properties' => 0,
- 'qualification' => 0
- //'filesize' => $filesize
- ];
- $workId = Database::insert($work_table, $params);
- if ($workId) {
- $sql = "UPDATE $work_table SET id = iid WHERE iid = $workId ";
- Database::query($sql);
- if (array_key_exists('filename', $workInfo) && !empty($filename)) {
- $filename = Database::escape_string($filename);
- $sql = "UPDATE $work_table SET
- filename = '$filename'
- WHERE iid = $workId";
- Database::query($sql);
- }
- if (array_key_exists('document_id', $workInfo)) {
- $documentId = isset($values['document_id']) ? intval($values['document_id']) : 0;
- $sql = "UPDATE $work_table SET
- document_id = '$documentId'
- WHERE iid = $workId";
- Database::query($sql);
- }
- api_item_property_update(
- $courseInfo,
- 'work',
- $workId,
- 'DocumentAdded',
- $userId,
- $groupInfo
- );
- sendAlertToUsers($workId, $courseInfo, $sessionId);
- Event::event_upload($workId);
- // The following feature requires the creation of a work-type
- // extra_field and the following setting in the configuration file
- // (until moved to the database). It allows te teacher to set a
- // "considered work time", meaning the time we assume a student
- // would have spent, approximately, to prepare the task before
- // handing it in Chamilo, adding this time to the student total
- // course use time, as a register of time spent *before* his
- // connection to the platform to hand the work in.
- $consideredWorkingTime = api_get_configuration_value('considered_working_time');
- if (!empty($consideredWorkingTime)) {
- // Get the "considered work time" defined for this work
- $fieldValue = new ExtraFieldValue('work');
- $resultExtra = $fieldValue->getAllValuesForAnItem(
- $workInfo['iid'], //the ID of the work *folder*, not the document uploaded by the student
- true
- );
- $workingTime = null;
- foreach ($resultExtra as $field) {
- $field = $field['value'];
- if ($consideredWorkingTime == $field->getField()->getVariable()) {
- $workingTime = $field->getValue();
- }
- }
- // If no time was defined, or a time of "0" was set, do nothing
- if (!empty($workingTime)) {
- // If some time is set, get the list of docs handed in by
- // this student (to make sure we count the time only once)
- $userWorks = get_work_user_list(
- 0,
- 100,
- null,
- null,
- $workInfo['id'],
- null,
- $userId,
- false,
- $courseId,
- $sessionId
- );
- if (count($userWorks) == 1) {
- // The student only uploaded one doc so far, so add the
- // considered work time to his course connection time
- $ip = api_get_real_ip();
- Event::eventAddVirtualCourseTime($courseId, $userId, $sessionId, $workingTime, $ip);
- }
- }
- }
- $workData = get_work_data_by_id($workId);
- if ($showFlashMessage) {
- Display::addFlash(Display::return_message(get_lang('DocAdd')));
- }
- }
- } else {
- if ($showFlashMessage) {
- Display::addFlash(
- Display::return_message(
- $message ? $message : get_lang('IsNotPosibleSaveTheDocument'),
- 'error'
- )
- );
- }
- }
- return $workData;
- }
- /**
- * Creates a new task (directory) in the assignment tool
- * @param array $formValues
- * @param int $user_id
- * @param array $courseInfo
- * @param int $groupId
- * @param int $session_id
- * @return bool|int
- * @note $params can have the following elements, but should at least have the 2 first ones: (
- * 'new_dir' => 'some-name',
- * 'description' => 'some-desc',
- * 'qualification' => 20 (e.g. 20),
- * 'weight' => 50 (percentage) to add to gradebook (e.g. 50),
- * 'allow_text_assignment' => 0/1/2,
- * @todo Rename createAssignment or createWork, or something like that
- */
- function addDir($formValues, $user_id, $courseInfo, $groupId, $session_id)
- {
- $em = Database::getManager();
- $user_id = intval($user_id);
- $groupId = intval($groupId);
- $groupIid = 0;
- $groupInfo = [];
- if (!empty($groupId)) {
- $groupInfo = GroupManager::get_group_properties($groupId);
- $groupIid = $groupInfo['iid'];
- }
- $session = $em->find('ChamiloCoreBundle:Session', $session_id);
- $base_work_dir = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/work';
- $course_id = $courseInfo['real_id'];
- $directory = api_replace_dangerous_char($formValues['new_dir']);
- $directory = disable_dangerous_file($directory);
- $created_dir = create_unexisting_work_directory($base_work_dir, $directory);
- if (empty($created_dir)) {
- return false;
- }
- $enableEndDate = isset($formValues['enableEndDate']) ? true : false;
- $enableExpiryDate = isset($formValues['enableExpiryDate']) ? true : false;
- if ($enableEndDate && $enableExpiryDate) {
- if ($formValues['expires_on'] > $formValues['ends_on']) {
- Display::addFlash(
- Display::return_message(
- get_lang('DateExpiredNotBeLessDeadLine'),
- 'warning'
- )
- );
- return false;
- }
- }
- $dirName = '/'.$created_dir;
- $today = new DateTime(api_get_utc_datetime(), new DateTimeZone('UTC'));
- $workTable = new CStudentPublication();
- $workTable
- ->setCId($course_id)
- ->setUrl($dirName)
- ->setTitle($formValues['new_dir'])
- ->setDescription($formValues['description'])
- ->setActive(true)
- ->setAccepted(true)
- ->setFiletype('folder')
- ->setPostGroupId($groupIid)
- ->setSentDate($today)
- ->setQualification($formValues['qualification'] != '' ? $formValues['qualification'] : 0)
- ->setParentId(0)
- ->setQualificatorId(0)
- ->setWeight(!empty($formValues['weight']) ? $formValues['weight'] : 0)
- ->setSession($session)
- ->setAllowTextAssignment($formValues['allow_text_assignment'])
- ->setContainsFile(0)
- ->setUserId($user_id)
- ->setHasProperties(0)
- ->setDocumentId(0);
- $em->persist($workTable);
- $em->flush();
- $workTable->setId($workTable->getIid());
- $em->merge($workTable);
- $em->flush();
- // Folder created
- api_item_property_update(
- $courseInfo,
- 'work',
- $workTable->getIid(),
- 'DirectoryCreated',
- $user_id,
- $groupInfo
- );
- updatePublicationAssignment(
- $workTable->getIid(),
- $formValues,
- $courseInfo,
- $groupIid
- );
- // Added the new Work ID to the extra field values
- $formValues['item_id'] = $workTable->getIid();
- $workFieldValue = new ExtraFieldValue('work');
- $workFieldValue->saveFieldValues($formValues);
- if (api_get_course_setting('email_alert_students_on_new_homework') == 1) {
- send_email_on_homework_creation(
- $course_id,
- $session ? $session->getId() : 0,
- $workTable->getIid()
- );
- }
- return $workTable->getIid();
- }
- /**
- * @param int $workId
- * @param array $courseInfo
- * @return int
- */
- function agendaExistsForWork($workId, $courseInfo)
- {
- $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
- $courseId = $courseInfo['real_id'];
- $workId = intval($workId);
- $sql = "SELECT add_to_calendar FROM $workTable
- WHERE c_id = $courseId AND publication_id = ".$workId;
- $res = Database::query($sql);
- if (Database::num_rows($res)) {
- $row = Database::fetch_array($res, 'ASSOC');
- if (!empty($row['add_to_calendar'])) {
- return $row['add_to_calendar'];
- }
- }
- return 0;
- }
- /**
- * Update work description, qualification, weight, allow_text_assignment
- * @param int $workId (iid)
- * @param array $params
- * @param array $courseInfo
- * @param int $sessionId
- */
- function updateWork($workId, $params, $courseInfo, $sessionId = 0)
- {
- $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $filteredParams = array(
- 'description' => $params['description'],
- 'qualification' => $params['qualification'],
- 'weight' => $params['weight'],
- 'allow_text_assignment' => $params['allow_text_assignment']
- );
- Database::update(
- $workTable,
- $filteredParams,
- array(
- 'iid = ? AND c_id = ?' => array(
- $workId,
- $courseInfo['real_id']
- )
- )
- );
- $workFieldValue = new ExtraFieldValue('work');
- $workFieldValue->saveFieldValues($params);
- }
- /**
- * @param int $workId
- * @param array $params
- * @param array $courseInfo
- * @param int $groupId
- */
- function updatePublicationAssignment($workId, $params, $courseInfo, $groupId)
- {
- $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
- $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $workId = intval($workId);
- $now = api_get_utc_datetime();
- $course_id = $courseInfo['real_id'];
- // Insert into agenda
- $agendaId = 0;
- if (isset($params['add_to_calendar']) && $params['add_to_calendar'] == 1) {
- // Setting today date
- $date = $end_date = $now;
- if (isset($params['enableExpiryDate'])) {
- $end_date = $params['expires_on'];
- $date = $end_date;
- }
- $title = sprintf(get_lang('HandingOverOfTaskX'), $params['new_dir']);
- $description = isset($params['description']) ? $params['description'] : '';
- $content = '<a href="'.api_get_path(WEB_CODE_PATH).'work/work_list.php?'.api_get_cidreq().'&id='.$workId.'">'
- .$params['new_dir'].'</a>'.$description;
- $agendaId = agendaExistsForWork($workId, $courseInfo);
- // Add/edit agenda
- $agenda = new Agenda('course');
- $agenda->set_course($courseInfo);
- if (!empty($agendaId)) {
- // add_to_calendar is set but it doesnt exists then invalidate
- $eventInfo = $agenda->get_event($agendaId);
- if (empty($eventInfo)) {
- $agendaId = 0;
- }
- }
- $eventColor = $agenda->eventStudentPublicationColor;
- if (empty($agendaId)) {
- $agendaId = $agenda->addEvent(
- $date,
- $end_date,
- 'false',
- $title,
- $content,
- array('GROUP:'.$groupId),
- false,
- null,
- [],
- [],
- null,
- $eventColor
- );
- } else {
- $agenda->editEvent(
- $agendaId,
- $end_date,
- $end_date,
- 'false',
- $title,
- $content,
- [],
- [],
- [],
- null,
- $eventColor
- );
- }
- }
- $qualification = isset($params['qualification']) && !empty($params['qualification']) ? 1 : 0;
- $expiryDate = isset($params['enableExpiryDate']) && (int) $params['enableExpiryDate'] == 1 ? api_get_utc_datetime($params['expires_on']) : '';
- $endDate = isset($params['enableEndDate']) && (int) $params['enableEndDate'] == 1 ? api_get_utc_datetime($params['ends_on']) : '';
- $data = get_work_assignment_by_id($workId, $course_id);
- if (!empty($expiryDate)) {
- $expiryDateCondition = "expires_on = '".Database::escape_string($expiryDate)."', ";
- } else {
- $expiryDateCondition = "expires_on = null, ";
- }
- if (!empty($endDate)) {
- $endOnCondition = "ends_on = '".Database::escape_string($endDate)."', ";
- } else {
- $endOnCondition = "ends_on = null, ";
- }
- if (empty($data)) {
- $sql = "INSERT INTO $table SET
- c_id = $course_id ,
- $expiryDateCondition
- $endOnCondition
- add_to_calendar = $agendaId,
- enable_qualification = '$qualification',
- publication_id = '$workId'";
- Database::query($sql);
- $my_last_id = Database::insert_id();
- if ($my_last_id) {
- $sql = "UPDATE $table SET
- id = iid
- WHERE iid = $my_last_id";
- Database::query($sql);
- $sql = "UPDATE $workTable SET
- has_properties = $my_last_id,
- view_properties = 1
- WHERE c_id = $course_id AND id = $workId";
- Database::query($sql);
- }
- } else {
- $sql = "UPDATE $table SET
- $expiryDateCondition
- $endOnCondition
- add_to_calendar = $agendaId,
- enable_qualification = '".$qualification."'
- WHERE
- publication_id = $workId AND
- c_id = $course_id AND
- iid = ".$data['iid'];
- Database::query($sql);
- }
- if (!empty($params['category_id'])) {
- $link_info = GradebookUtils::isResourceInCourseGradebook(
- $courseInfo['code'],
- LINK_STUDENTPUBLICATION,
- $workId,
- api_get_session_id()
- );
- $linkId = null;
- if (!empty($link_info)) {
- $linkId = $link_info['id'];
- }
- if (isset($params['make_calification']) &&
- $params['make_calification'] == 1
- ) {
- if (empty($linkId)) {
- GradebookUtils::add_resource_to_course_gradebook(
- $params['category_id'],
- $courseInfo['code'],
- LINK_STUDENTPUBLICATION,
- $workId,
- $params['new_dir'],
- api_float_val($params['weight']),
- api_float_val($params['qualification']),
- $params['description'],
- 1,
- api_get_session_id()
- );
- } else {
- GradebookUtils::update_resource_from_course_gradebook(
- $linkId,
- $courseInfo['code'],
- $params['weight']
- );
- }
- } else {
- // Delete everything of the gradebook for this $linkId
- GradebookUtils::remove_resource_from_course_gradebook($linkId);
- }
- }
- }
- /**
- * Delete all work by student
- * @param int $userId
- * @param array $courseInfo
- * @return array return deleted items
- */
- function deleteAllWorkPerUser($userId, $courseInfo)
- {
- $deletedItems = array();
- $workPerUser = getWorkPerUser($userId);
- if (!empty($workPerUser)) {
- foreach ($workPerUser as $work) {
- $work = $work['work'];
- foreach ($work->user_results as $userResult) {
- $result = deleteWorkItem($userResult['id'], $courseInfo);
- if ($result) {
- $deletedItems[] = $userResult;
- }
- }
- }
- }
- return $deletedItems;
- }
- /**
- * @param int $item_id
- * @param array course info
- * @return bool
- */
- function deleteWorkItem($item_id, $courseInfo)
- {
- $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $TSTDPUBASG = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
- $currentCourseRepositorySys = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/';
- $is_allowed_to_edit = api_is_allowed_to_edit();
- $file_deleted = false;
- $item_id = intval($item_id);
- $is_author = user_is_author($item_id);
- $work_data = get_work_data_by_id($item_id);
- $locked = api_resource_is_locked_by_gradebook($work_data['parent_id'], LINK_STUDENTPUBLICATION);
- $course_id = $courseInfo['real_id'];
- if (($is_allowed_to_edit && $locked == false) ||
- (
- $locked == false &&
- $is_author &&
- api_get_course_setting('student_delete_own_publication') == 1 &&
- $work_data['qualificator_id'] == 0
- )
- ) {
- // We found the current user is the author
- $sql = "SELECT url, contains_file, user_id, session_id, parent_id FROM $work_table
- WHERE c_id = $course_id AND id = $item_id";
- $result = Database::query($sql);
- $row = Database::fetch_array($result);
- $count = Database::num_rows($result);
- if ($count > 0) {
- // If the "considered_working_time" option is enabled, check
- // whether some time should be removed from track_e_course_access
- $consideredWorkingTime = api_get_configuration_value('considered_working_time');
- if ($consideredWorkingTime) {
- $userWorks = get_work_user_list(
- 0,
- 100,
- null,
- null,
- $row['parent_id'],
- null,
- $row['user_id'],
- false,
- $course_id,
- $row['session_id']
- );
- // We're only interested in deleting the time if this is the latest work sent
- if (count($userWorks) == 1) {
- // Get the "considered work time" defined for this work
- $fieldValue = new ExtraFieldValue('work');
- $resultExtra = $fieldValue->getAllValuesForAnItem(
- $row['parent_id'],
- true
- );
- $workingTime = null;
- foreach ($resultExtra as $field) {
- $field = $field['value'];
- if ($consideredWorkingTime == $field->getField()->getVariable()) {
- $workingTime = $field->getValue();
- }
- }
- // If no time was defined, or a time of "0" was set, do nothing
- if (!empty($workingTime)) {
- $sessionId = empty($row['session_id']) ? 0 : $row['session_id'];
- // Getting false from the following call would mean the
- // time record
- Event::eventRemoveVirtualCourseTime(
- $course_id,
- $row['user_id'],
- $sessionId,
- $workingTime
- );
- }
- }
- } // end of considered_working_time check section
- $sql = "UPDATE $work_table SET active = 2
- WHERE c_id = $course_id AND id = $item_id";
- Database::query($sql);
- $sql = "DELETE FROM $TSTDPUBASG
- WHERE c_id = $course_id AND publication_id = $item_id";
- Database::query($sql);
- api_item_property_update(
- $courseInfo,
- 'work',
- $item_id,
- 'DocumentDeleted',
- api_get_user_id()
- );
- Event::addEvent(
- LOG_WORK_FILE_DELETE,
- LOG_WORK_DATA,
- [
- 'id' => $work_data['id'],
- 'url' => $work_data['url'],
- 'title' => $work_data['title']
- ],
- null,
- api_get_user_id(),
- api_get_course_int_id(),
- api_get_session_id()
- );
- $work = $row['url'];
- if ($row['contains_file'] == 1) {
- if (!empty($work)) {
- if (api_get_setting('permanently_remove_deleted_files') === 'true') {
- my_delete($currentCourseRepositorySys.'/'.$work);
- $file_deleted = true;
- } else {
- $extension = pathinfo($work, PATHINFO_EXTENSION);
- $new_dir = $work.'_DELETED_'.$item_id.'.'.$extension;
- if (file_exists($currentCourseRepositorySys.'/'.$work)) {
- rename($currentCourseRepositorySys.'/'.$work, $currentCourseRepositorySys.'/'.$new_dir);
- $file_deleted = true;
- }
- }
- }
- } else {
- $file_deleted = true;
- }
- }
- }
- return $file_deleted;
- }
- /**
- * @param FormValidator $form
- * @param array $defaults
- * @param integer $workId
- * @return FormValidator
- */
- function getFormWork($form, $defaults = array(), $workId = 0)
- {
- $sessionId = api_get_session_id();
- if (!empty($defaults)) {
- if (isset($defaults['submit'])) {
- unset($defaults['submit']);
- }
- }
- // Create the form that asks for the directory name
- $form->addText('new_dir', get_lang('AssignmentName'));
- $form->addHtmlEditor(
- 'description',
- get_lang('Description'),
- false,
- false,
- getWorkDescriptionToolbar()
- );
- $form->addButtonAdvancedSettings('advanced_params', get_lang('AdvancedParameters'));
- if (!empty($defaults) && (isset($defaults['enableEndDate']) || isset($defaults['enableExpiryDate']))) {
- $form->addHtml('<div id="advanced_params_options" style="display:block">');
- } else {
- $form->addHtml('<div id="advanced_params_options" style="display:none">');
- }
- // QualificationOfAssignment
- $form->addElement('text', 'qualification', get_lang('QualificationNumeric'));
- if (($sessionId != 0 && Gradebook::is_active()) || $sessionId == 0) {
- $form->addElement(
- 'checkbox',
- 'make_calification',
- null,
- get_lang('MakeQualifiable'),
- array(
- 'id' =>'make_calification_id',
- 'onclick' => "javascript: if(this.checked) { document.getElementById('option1').style.display='block';}else{document.getElementById('option1').style.display='none';}"
- )
- );
- } else {
- // QualificationOfAssignment
- $form->addElement('hidden', 'make_calification', false);
- }
- if (!empty($defaults) && isset($defaults['category_id'])) {
- $form->addHtml('<div id=\'option1\' style="display:block">');
- } else {
- $form->addHtml('<div id=\'option1\' style="display:none">');
- }
- // Loading Gradebook select
- GradebookUtils::load_gradebook_select_in_tool($form);
- $form->addElement('text', 'weight', get_lang('WeightInTheGradebook'));
- $form->addHtml('</div>');
- $form->addElement('checkbox', 'enableExpiryDate', null, get_lang('EnableExpiryDate'), 'id="expiry_date"');
- if (isset($defaults['enableExpiryDate']) && $defaults['enableExpiryDate']) {
- $form->addHtml('<div id="option2" style="display: block;">');
- } else {
- $form->addHtml('<div id="option2" style="display: none;">');
- }
- $timeNextWeek = time()+86400*7;
- $nextWeek = substr(api_get_local_time($timeNextWeek), 0, 10);
- if (!isset($defaults['expires_on'])) {
- $date = substr($nextWeek, 0, 10);
- $defaults['expires_on'] = $date.' 23:59';
- }
- $form->addElement('date_time_picker', 'expires_on', get_lang('ExpiresAt'));
- $form->addHtml('</div>');
- $form->addElement('checkbox', 'enableEndDate', null, get_lang('EnableEndDate'), 'id="end_date"');
- if (!isset($defaults['ends_on'])) {
- $nextDay = substr(api_get_local_time($timeNextWeek+86400), 0, 10);
- $date = substr($nextDay, 0, 10);
- $defaults['ends_on'] = $date.' 23:59';
- }
- if (isset($defaults['enableEndDate']) && $defaults['enableEndDate']) {
- $form->addHtml('<div id="option3" style="display: block;">');
- } else {
- $form->addHtml('<div id="option3" style="display: none;">');
- }
- $form->addElement('date_time_picker', 'ends_on', get_lang('EndsAt'));
- $form->addHtml('</div>');
- $form->addElement('checkbox', 'add_to_calendar', null, get_lang('AddToCalendar'));
- $form->addElement('select', 'allow_text_assignment', get_lang('DocumentType'), getUploadDocumentType());
- // Extra fields
- $extra_field = new ExtraField('work');
- $extra = $extra_field->addElements($form, $workId);
- $htmlHeadXtra[] = '
- <script>
- $(function() {
- ' . $extra['jquery_ready_content'].'
- });
- </script>';
- $form->addHtml('</div>');
- if (!empty($defaults)) {
- $form->setDefaults($defaults);
- }
- return $form;
- }
- /**
- * @return array
- */
- function getUploadDocumentType()
- {
- return array(
- 0 => get_lang('AllowFileOrText'),
- 1 => get_lang('AllowOnlyText'),
- 2 => get_lang('AllowOnlyFiles')
- );
- }
- /**
- * @param array $courseInfo
- * @param bool $showScore
- * @param bool $studentDeleteOwnPublication
- */
- function updateSettings($courseInfo, $showScore, $studentDeleteOwnPublication)
- {
- $showScore = intval($showScore);
- $courseId = api_get_course_int_id();
- $main_course_table = Database::get_main_table(TABLE_MAIN_COURSE);
- $table_course_setting = Database::get_course_table(TOOL_COURSE_SETTING);
- if (empty($courseId)) {
- return false;
- }
- $query = "UPDATE $main_course_table
- SET show_score = '$showScore'
- WHERE id = $courseId";
- Database::query($query);
- /**
- * Course data are cached in session so we need to update both the database
- * and the session data
- */
- $_course['show_score'] = $showScore;
- Session::write('_course', $courseInfo);
- // changing the tool setting: is a student allowed to delete his/her own document
- // counting the number of occurrences of this setting (if 0 => add, if 1 => update)
- $query = "SELECT * FROM $table_course_setting
- WHERE
- c_id = $courseId AND
- variable = 'student_delete_own_publication'";
- $result = Database::query($query);
- $number_of_setting = Database::num_rows($result);
- if ($number_of_setting == 1) {
- $query = "UPDATE ".$table_course_setting." SET
- value='" . Database::escape_string($studentDeleteOwnPublication)."'
- WHERE variable = 'student_delete_own_publication' AND c_id = $courseId";
- Database::query($query);
- } else {
- $params = [
- 'c_id' => $courseId,
- 'variable' => 'student_delete_own_publication',
- 'value' => $studentDeleteOwnPublication,
- 'category' => 'work'
- ];
- Database::insert($table_course_setting, $params);
- }
- }
- /**
- * @param int $item_id
- * @param array $course_info
- */
- function makeVisible($item_id, $course_info)
- {
- $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $course_id = $course_info['real_id'];
- $item_id = intval($item_id);
- $sql = "UPDATE $work_table SET accepted = 1
- WHERE c_id = $course_id AND id = $item_id";
- Database::query($sql);
- api_item_property_update($course_info, 'work', $item_id, 'visible', api_get_user_id());
- }
- /**
- * @param int $item_id
- * @param array $course_info
- */
- function makeInvisible($item_id, $course_info)
- {
- $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $item_id = intval($item_id);
- $course_id = $course_info['real_id'];
- $sql = "UPDATE ".$work_table."
- SET accepted = 0
- WHERE c_id = $course_id AND id = '".$item_id."'";
- Database::query($sql);
- api_item_property_update(
- $course_info,
- 'work',
- $item_id,
- 'invisible',
- api_get_user_id()
- );
- }
- /**
- * @param int $item_id
- * @param string $path
- * @param array $courseInfo
- * @param int $groupId iid
- * @param int $sessionId
- * @return string
- */
- function generateMoveForm($item_id, $path, $courseInfo, $groupId, $sessionId)
- {
- $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $courseId = $courseInfo['real_id'];
- $folders = array();
- $session_id = intval($sessionId);
- $groupId = intval($groupId);
- $sessionCondition = empty($sessionId) ? " AND (session_id = 0 OR session_id IS NULL) " : " AND session_id='".$session_id."'";
- $groupIid = 0;
- if ($groupId) {
- $groupInfo = GroupManager::get_group_properties($groupId);
- $groupIid = $groupInfo['iid'];
- }
- $sql = "SELECT id, url, title
- FROM $work_table
- WHERE
- c_id = $courseId AND
- active IN (0, 1) AND
- url LIKE '/%' AND
- post_group_id = $groupIid
- $sessionCondition";
- $res = Database::query($sql);
- while ($folder = Database::fetch_array($res)) {
- $title = empty($folder['title']) ? basename($folder['url']) : $folder['title'];
- $folders[$folder['id']] = $title;
- }
- return build_work_move_to_selector($folders, $path, $item_id);
- }
- /**
- * @param int $workId
- * @return string
- */
- function showStudentList($workId)
- {
- $columnModel = array(
- array(
- 'name' => 'student',
- 'index' => 'student',
- 'width' => '350px',
- 'align' => 'left',
- 'sortable' => 'false',
- ),
- array(
- 'name' => 'works',
- 'index' => 'works',
- 'align' => 'center',
- 'sortable' => 'false',
- ),
- );
- $token = null;
- $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_work_student_list_overview&work_id='.$workId.'&'.api_get_cidreq();
- $columns = array(
- get_lang('Students'),
- get_lang('Works')
- );
- $order = api_is_western_name_order() ? 'firstname' : 'lastname';
- $params = array(
- 'autowidth' => 'true',
- 'height' => 'auto',
- 'rowNum' => 5,
- 'sortname' => $order,
- 'sortorder' => 'asc'
- );
- $html = '<script>
- $(function() {
- '.Display::grid_js('studentList', $url, $columns, $columnModel, $params, array(), null, true).'
- $("#workList").jqGrid(
- "navGrid",
- "#studentList_pager",
- { edit: false, add: false, del: false },
- { height:280, reloadAfterSubmit:false }, // edit options
- { height:280, reloadAfterSubmit:false }, // add options
- { width:500 } // search options
- );
- });
- </script>';
- $html .= Display::grid_html('studentList');
- return $html;
- }
- /**
- * @param string $courseCode
- * @param int $sessionId
- * @param int $groupId
- * @param int $start
- * @param int $limit
- * @param string $sidx
- * @param string $sord
- * @param $getCount
- * @return array|int
- */
- function getWorkUserList($courseCode, $sessionId, $groupId, $start, $limit, $sidx, $sord, $getCount = false)
- {
- if (!empty($groupId)) {
- $userList = GroupManager::get_users(
- $groupId,
- false,
- $start,
- $limit,
- $getCount,
- null,
- $sidx,
- $sord
- );
- } else {
- $limitString = null;
- if (!empty($start) && !empty($limit)) {
- $start = intval($start);
- $limit = intval($limit);
- $limitString = " LIMIT $start, $limit";
- }
- $orderBy = null;
- if (!empty($sidx) && !empty($sord)) {
- if (in_array($sidx, array('firstname', 'lastname'))) {
- $orderBy = "ORDER BY $sidx $sord";
- }
- }
- if (empty($sessionId)) {
- $userList = CourseManager::get_user_list_from_course_code(
- $courseCode,
- $sessionId,
- $limitString,
- $orderBy,
- STUDENT,
- $getCount
- );
- } else {
- $userList = CourseManager::get_user_list_from_course_code(
- $courseCode,
- $sessionId,
- $limitString,
- $orderBy,
- 0,
- $getCount
- );
- }
- if ($getCount == false) {
- $userList = array_keys($userList);
- }
- }
- return $userList;
- }
- /**
- * @param int $workId
- * @param string $courseCode
- * @param int $sessionId
- * @param int $groupId
- * @param int $start
- * @param int $limit
- * @param int $sidx
- * @param string $sord
- * @param bool $getCount
- * @return array|int
- */
- function getWorkUserListData(
- $workId,
- $courseCode,
- $sessionId,
- $groupId,
- $start,
- $limit,
- $sidx,
- $sord,
- $getCount = false
- ) {
- $my_folder_data = get_work_data_by_id($workId);
- $workParents = array();
- if (empty($my_folder_data)) {
- $workParents = getWorkList($workId, $my_folder_data, null);
- }
- $workIdList = array();
- if (!empty($workParents)) {
- foreach ($workParents as $work) {
- $workIdList[] = $work->id;
- }
- }
- $courseInfo = api_get_course_info($courseCode);
- $userList = getWorkUserList(
- $courseCode,
- $sessionId,
- $groupId,
- $start,
- $limit,
- $sidx,
- $sord,
- $getCount
- );
- if ($getCount) {
- return $userList;
- }
- $results = array();
- if (!empty($userList)) {
- foreach ($userList as $userId) {
- $user = api_get_user_info($userId);
- $link = api_get_path(WEB_CODE_PATH).'work/student_work.php?'.api_get_cidreq().'&studentId='.$user['user_id'];
- $url = Display::url(api_get_person_name($user['firstname'], $user['lastname']), $link);
- $userWorks = 0;
- if (!empty($workIdList)) {
- $userWorks = getUniqueStudentAttempts(
- $workIdList,
- $groupId,
- $courseInfo['real_id'],
- $sessionId,
- $user['user_id']
- );
- }
- $works = $userWorks." / ".count($workParents);
- $results[] = array(
- 'student' => $url,
- 'works' => Display::url($works, $link),
- );
- }
- }
- return $results;
- }
- /**
- * @param int $id
- * @param array $course_info
- * @param bool $isCorrection
- *
- * @return bool
- */
- function downloadFile($id, $course_info, $isCorrection)
- {
- return getFile($id, $course_info, true, $isCorrection);
- }
- /**
- * @param int $id
- * @param array $course_info
- * @param bool $download
- * @param bool $isCorrection
- *
- * @return bool
- */
- function getFile($id, $course_info, $download = true, $isCorrection = false)
- {
- $file = getFileContents($id, $course_info, 0, $isCorrection);
- if (!empty($file) && is_array($file)) {
- return DocumentManager::file_send_for_download(
- $file['path'],
- $download,
- $file['title']
- );
- }
- return false;
- }
- /**
- * Get the file contents for an assigment
- * @param int $id
- * @param array $course_info
- * @param int Session ID
- * @param $correction
- *
- * @return array|bool
- */
- function getFileContents($id, $course_info, $sessionId = 0, $correction = false)
- {
- $id = intval($id);
- if (empty($course_info) || empty($id)) {
- return false;
- }
- if (empty($sessionId)) {
- $sessionId = api_get_session_id();
- }
- $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- if (!empty($course_info['real_id'])) {
- $sql = 'SELECT *
- FROM '.$table.'
- WHERE c_id = '.$course_info['real_id'].' AND id = "'.$id.'"';
- $result = Database::query($sql);
- if ($result && Database::num_rows($result)) {
- $row = Database::fetch_array($result, 'ASSOC');
- if ($correction) {
- $row['url'] = $row['url_correction'];
- }
- if (empty($row['url'])) {
- return false;
- }
- $full_file_name = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/'.$row['url'];
- $item_info = api_get_item_property_info(
- api_get_course_int_id(),
- 'work',
- $row['id'],
- $sessionId
- );
- if (empty($item_info)) {
- return false;
- }
- allowOnlySubscribedUser(
- api_get_user_id(),
- $row['parent_id'],
- $course_info['real_id']
- );
- /*
- field show_score in table course :
- 0 => New documents are visible for all users
- 1 => New documents are only visible for the teacher(s)
- field visibility in table item_property :
- 0 => eye closed, invisible for all students
- 1 => eye open
- field accepted in table c_student_publication :
- 0 => eye closed, invisible for all students
- 1 => eye open
- ( We should have visibility == accepted, otherwise there is an
- inconsistency in the Database)
- field value in table c_course_setting :
- 0 => Allow learners to delete their own publications = NO
- 1 => Allow learners to delete their own publications = YES
- +------------------+-------------------------+------------------------+
- |Can download work?| doc visible for all = 0 | doc visible for all = 1|
- +------------------+-------------------------+------------------------+
- | visibility = 0 | editor only | editor only |
- | | | |
- +------------------+-------------------------+------------------------+
- | visibility = 1 | editor | editor |
- | | + owner of the work | + any student |
- +------------------+-------------------------+------------------------+
- (editor = teacher + admin + anybody with right api_is_allowed_to_edit)
- */
- $work_is_visible = $item_info['visibility'] == 1 && $row['accepted'] == 1;
- $doc_visible_for_all = $course_info['show_score'] == 1;
- $is_editor = api_is_allowed_to_edit(true, true, true);
- $student_is_owner_of_work = user_is_author($row['id'], $row['user_id']);
- if ($is_editor ||
- $student_is_owner_of_work ||
- ($doc_visible_for_all && $work_is_visible)
- ) {
- $title = $row['title'];
- if ($correction) {
- $title = $row['title_correction'];
- }
- if (array_key_exists('filename', $row) && !empty($row['filename'])) {
- $title = $row['filename'];
- }
- $title = str_replace(' ', '_', $title);
- if ($correction == false) {
- $userInfo = api_get_user_info($row['user_id']);
- if ($userInfo) {
- $date = api_get_local_time($row['sent_date']);
- $date = str_replace(array(':', '-', ' '), '_', $date);
- $title = $date.'_'.$userInfo['username'].'_'.$title;
- }
- }
- if (Security::check_abs_path(
- $full_file_name,
- api_get_path(SYS_COURSE_PATH).api_get_course_path().'/')
- ) {
- Event::event_download($title);
- return array(
- 'path' => $full_file_name,
- 'title' => $title,
- 'title_correction' => $row['title_correction']
- );
- }
- }
- }
- }
- return false;
- }
- /**
- * @param int $userId
- * @param array $courseInfo
- * @param string $format
- * @return bool
- */
- function exportAllWork($userId, $courseInfo, $format = 'pdf')
- {
- $userInfo = api_get_user_info($userId);
- if (empty($userInfo) || empty($courseInfo)) {
- return false;
- }
- $workPerUser = getWorkPerUser($userId);
- switch ($format) {
- case 'pdf':
- if (!empty($workPerUser)) {
- $pdf = new PDF();
- $content = null;
- foreach ($workPerUser as $work) {
- $work = $work['work'];
- foreach ($work->user_results as $userResult) {
- $content .= $userResult['title'];
- // No need to use api_get_local_time()
- $content .= $userResult['sent_date'];
- $content .= $userResult['qualification'];
- $content .= $userResult['description'];
- }
- }
- if (!empty($content)) {
- $pdf->content_to_pdf(
- $content,
- null,
- api_replace_dangerous_char($userInfo['complete_name']),
- $courseInfo['code']
- );
- }
- }
- break;
- }
- }
- /**
- * @param int $workId
- * @param array $courseInfo
- * @param int $sessionId
- * @param string $format
- * @return bool
- */
- function exportAllStudentWorkFromPublication(
- $workId,
- $courseInfo,
- $sessionId,
- $format = 'pdf'
- ) {
- if (empty($courseInfo)) {
- return false;
- }
- $workData = get_work_data_by_id($workId);
- if (empty($workData)) {
- return false;
- }
- $assignment = get_work_assignment_by_id($workId);
- $courseCode = $courseInfo['code'];
- $header = get_lang('Course').': '.$courseInfo['title'];
- $teachers = CourseManager::get_teacher_list_from_course_code_to_string(
- $courseCode
- );
- if (!empty($sessionId)) {
- $sessionInfo = api_get_session_info($sessionId);
- if (!empty($sessionInfo)) {
- $header .= ' - '.$sessionInfo['name'];
- $header .= '<br />'.$sessionInfo['description'];
- $teachers = SessionManager::getCoachesByCourseSessionToString(
- $sessionId,
- $courseInfo['real_id']
- );
- }
- }
- $header .= '<br />'.get_lang('Teachers').': '.$teachers.'<br />';
- $header .= '<br />'.get_lang('Date').': '.api_get_local_time().'<br />';
- $header .= '<br />'.get_lang('WorkName').': '.$workData['title'].'<br />';
- $content = null;
- $expiresOn = null;
- if (!empty($assignment) && isset($assignment['expires_on'])) {
- $content .= '<br /><strong>'.get_lang('ExpirationDate').'</strong>: '.api_get_local_time($assignment['expires_on']);
- $expiresOn = api_get_local_time($assignment['expires_on']);
- }
- if (!empty($workData['description'])) {
- $content .= '<br /><strong>'.get_lang('Description').'</strong>: '.$workData['description'];
- }
- $workList = get_work_user_list(null, null, null, null, $workId);
- switch ($format) {
- case 'pdf':
- if (!empty($workList)) {
- $table = new HTML_Table(array('class' => 'data_table'));
- $headers = array(
- get_lang('Name'),
- get_lang('User'),
- get_lang('HandOutDateLimit'),
- get_lang('SentDate'),
- get_lang('FileName'),
- get_lang('Score'),
- get_lang('Feedback')
- );
- $column = 0;
- foreach ($headers as $header) {
- $table->setHeaderContents(0, $column, $header);
- $column++;
- }
- $row = 1;
- //$pdf->set_custom_header($header);
- foreach ($workList as $work) {
- $content .= '<hr />';
- // getWorkComments need c_id
- $work['c_id'] = $courseInfo['real_id'];
- //$content .= get_lang('Date').': '.api_get_local_time($work['sent_date_from_db']).'<br />';
- $score = null;
- if (!empty($work['qualification_only'])) {
- $score = $work['qualification_only'];
- }
- $comments = getWorkComments($work);
- $feedback = null;
- if (!empty($comments)) {
- $content .= '<h4>'.get_lang('Feedback').': </h4>';
- foreach ($comments as $comment) {
- $feedback .= get_lang('User').': '.$comment['complete_name'].
- '<br />';
- $feedback .= $comment['comment'].'<br />';
- }
- }
- $table->setCellContents($row, 0, strip_tags($workData['title']));
- $table->setCellContents($row, 1, strip_tags($work['fullname']));
- $table->setCellContents($row, 2, $expiresOn);
- $table->setCellContents($row, 3, api_get_local_time($work['sent_date_from_db']));
- $table->setCellContents($row, 4, strip_tags($work['title']));
- $table->setCellContents($row, 5, $score);
- $table->setCellContents($row, 6, $feedback);
- $row++;
- }
- $content = $table->toHtml();
- if (!empty($content)) {
- $params = array(
- 'filename' => $workData['title'].'_'.api_get_local_time(),
- 'pdf_title' => api_replace_dangerous_char($workData['title']),
- 'course_code' => $courseInfo['code'],
- );
- $pdf = new PDF('A4', null, $params);
- $pdf->html_to_pdf_with_template($content);
- }
- exit;
- }
- break;
- }
- }
- /**
- * Downloads all user files per user
- * @param int $userId
- * @param array $courseInfo
- * @return bool
- */
- function downloadAllFilesPerUser($userId, $courseInfo)
- {
- $userInfo = api_get_user_info($userId);
- if (empty($userInfo) || empty($courseInfo)) {
- return false;
- }
- $tempZipFile = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().".zip";
- $coursePath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/work/';
- $zip = new PclZip($tempZipFile);
- $workPerUser = getWorkPerUser($userId);
- if (!empty($workPerUser)) {
- $files = array();
- foreach ($workPerUser as $work) {
- $work = $work['work'];
- foreach ($work->user_results as $userResult) {
- if (empty($userResult['url']) || empty($userResult['contains_file'])) {
- continue;
- }
- $data = getFileContents($userResult['id'], $courseInfo);
- if (!empty($data) && isset($data['path'])) {
- $files[basename($data['path'])] = array(
- 'title' => $data['title'],
- 'path' => $data['path']
- );
- }
- }
- }
- if (!empty($files)) {
- Session::write('files', $files);
- foreach ($files as $data) {
- $zip->add(
- $data['path'],
- PCLZIP_OPT_REMOVE_PATH,
- $coursePath,
- PCLZIP_CB_PRE_ADD,
- 'preAddAllWorkStudentCallback'
- );
- }
- }
- // Start download of created file
- $name = basename(api_replace_dangerous_char($userInfo['complete_name'])).'.zip';
- Event::event_download($name.'.zip (folder)');
- if (Security::check_abs_path($tempZipFile, api_get_path(SYS_ARCHIVE_PATH))) {
- DocumentManager::file_send_for_download($tempZipFile, true, $name);
- @unlink($tempZipFile);
- exit;
- }
- }
- exit;
- }
- /**
- * @param $p_event
- * @param array $p_header
- * @return int
- */
- function preAddAllWorkStudentCallback($p_event, &$p_header)
- {
- $files = Session::read('files');
- if (isset($files[basename($p_header['stored_filename'])])) {
- $p_header['stored_filename'] = $files[basename($p_header['stored_filename'])]['title'];
- return 1;
- }
- return 0;
- }
- /**
- * Get all work created by a user
- * @param int $user_id
- * @param int $courseId
- * @param int $sessionId
- * @return array
- */
- function getWorkCreatedByUser($user_id, $courseId, $sessionId)
- {
- $items = api_get_item_property_list_by_tool_by_user(
- $user_id,
- 'work',
- $courseId,
- $sessionId
- );
- $list = array();
- if (!empty($items)) {
- foreach ($items as $work) {
- $item = get_work_data_by_id(
- $work['ref'],
- $courseId,
- $sessionId
- );
- if (!empty($item)) {
- $list[] = array(
- $item['title'],
- api_get_local_time($work['insert_date']),
- api_get_local_time($work['lastedit_date'])
- );
- }
- }
- }
- return $list;
- }
- /**
- * @param array $courseInfo
- * @param int $workId
- * @return bool
- */
- function protectWork($courseInfo, $workId)
- {
- $userId = api_get_user_id();
- $groupId = api_get_group_id();
- $sessionId = api_get_session_id();
- $workData = get_work_data_by_id($workId);
- if (empty($workData) || empty($courseInfo)) {
- api_not_allowed(true);
- }
- if (api_is_platform_admin() || api_is_allowed_to_edit()) {
- return true;
- }
- $workId = $workData['id'];
- if ($workData['active'] != 1) {
- api_not_allowed(true);
- }
- $visibility = api_get_item_visibility($courseInfo, 'work', $workId, $sessionId);
- if ($visibility != 1) {
- api_not_allowed(true);
- }
- allowOnlySubscribedUser($userId, $workId, $courseInfo['real_id']);
- $groupInfo = GroupManager::get_group_properties($groupId);
- if (!empty($groupId)) {
- $showWork = GroupManager::user_has_access(
- $userId,
- $groupInfo['iid'],
- GroupManager::GROUP_TOOL_WORK
- );
- if (!$showWork) {
- api_not_allowed(true);
- }
- }
- }
- /**
- * @param array $courseInfo
- * @param array $work
- */
- function deleteCorrection($courseInfo, $work)
- {
- if (isset($work['url_correction']) && !empty($work['url_correction']) && isset($work['iid'])) {
- $id = $work['iid'];
- $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
- $sql = "UPDATE $table SET
- url_correction = '',
- title_correction = ''
- WHERE iid = $id";
- Database::query($sql);
- $coursePath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/';
- if (file_exists($coursePath.$work['url_correction'])) {
- if (Security::check_abs_path($coursePath.$work['url_correction'], $coursePath)) {
- unlink($coursePath.$work['url_correction']);
- }
- }
- }
- }
|