locallib.php 516 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775
  1. <?php
  2. /**
  3. * This class provides all the functionality for an ia (interactive activities).
  4. *
  5. * Release Notes:
  6. * - v 5.0.0 2027/06/27
  7. * + Fix use of Linux separator "/" in favor of DIRECTORY_SEPARATOR (allow Win server use)
  8. * + Improved iAssign report: now is possible to see students' submission even of "tests" (not only "exercises")
  9. *
  10. * - v 4.9.9 2023/04/20
  11. * + Fix in ias:={iassign_statement}/{files}:=f association. Methods in 'files_functions.php' to try associations: f.itemid=ias.id or f.itemid=ias.file (old)
  12. * Mainly in 'add_edit_iassign()'
  13. *
  14. * - v 4.9.8 2022/09/15
  15. * + Fix iLM edition (now is possible to update fields 'reevaluate', 'editingbehavior' and 'evaluate' of table 'iassign_ilm')
  16. *
  17. * - v 4.9.7 2022/02/11
  18. * + Try to fix teacher edit of activity changing 'statement.id' to 'statement.file' in 'add_edit_iassign()';
  19. * + Changed fix to activities order in 'show_iassign()', from "student view" to "teacher view"
  20. *
  21. * - v 4.9.6 2022/01/21
  22. * + Try to fix view activity not sent through report ("view_iassign_current()": else if (!$student_answer) $loadTeacherActivity=true;)
  23. *
  24. * - v 4.9.5 2021/12/28
  25. * + Detail to fix order o activities inside a block ('iassign_statement.position' - old instalation could generate order error)
  26. *
  27. * - v 4.9.4 2020/05/28
  28. * + Detail fixed in old line 6789/7240, avoid error with file without extension ($filetype = $tmp[1];)
  29. *
  30. * - v 4.9.3 2020/01/16
  31. * + Several fixes to allow to move activities in a block and improvements in repository and to appear comment submission button
  32. *
  33. * - v 4.9.2 2019/03/13
  34. * + Several fixes to allow iLM under HTML stack
  35. *
  36. * - v 4.9.1 2017/11/02
  37. * + Fixed a bug with iGeom script - the view was not loading the CEO with activity (resulting in an error "Erro ao gravar na base de dados")
  38. * + function view_iassign_current(): it were inserted 2 'if(substr($ilm_name,0,5)=="igeom") if($iassign_statement_activity_item->special_param1==1) $lTA=true;' to call '$ilm->view_iLM(...lTA)'
  39. *
  40. * - v 4.9 2017/03/13
  41. * + Great number of changes to allow the use o iVProgH5 (e.g. function changed: from 'applet_ilm' to 'build_ilm_tags')
  42. *
  43. * - v 4.8 2016/05/12
  44. * + Function add_to_log() is deprecated it was then rewritten to the new events API
  45. * + Resolved: the field "description" table "iassign_ilm" was being filled with the codes in HTML when importing magnet package and
  46. * due to this the language field was not being displayed because in place of the quotes was recorded in the & quot field.
  47. * ("$description_str = htmlentities(str_replace(array('<description>','</description>'), array('',''), $application_xml->description->asXML()));"
  48. * replaced by
  49. * "description_str = str_replace(array('<description>','</description>'), array('',''), $application_xml->description->asXML());")
  50. * - v 4.7 2016/02/17
  51. * + Moodle 3.X: now iAssign is working fine under version 3.X (iassign/version.php: '$module->' changed to '$plugin->'; iassign/locallib.php: 'format_text(...)' replaced 'filter_text(...)')
  52. * + Improved: now is possible to see the iGeom menus in preview (from iAssign Repository) - it depends on the version 1.3 of iAssign filter!
  53. * + Improved: new names for 'form.input.MA_POST_Archive' and 'form.input.MA_POST_Value', now: 'iLM_PARAM_ArchiveContent' and 'iLM_PARAM_ActivityEvaluation'
  54. * + BUG fixed: now it is fine the "online" edition of activities (in iAssign Repository) - inserted 'iLM_PARAM_Authoring' (iLM 2) and 'MA_POST_ArchiveTeacher' (iLM 1)
  55. * + BUG fixed: it is possible to change the name of any file in iAssign Repository - problems was in 'optional_param(...)', 'PARAM_TEXT' replaced 'PARAM_ALPHANUMEXT'
  56. * + BUG fixed: it is possible to duplicate any file in iAssign Repository - problems also in 'optional_param(...)', 'PARAM_TEXT' replaced 'PARAM_ALPHANUMEXT'
  57. * + BUG fixed: now is possible to edit an iAssign activity with no new object been created (in iGeom: turn an example in exercise)
  58. *
  59. * --------------- (code bellow was used at our MOOC 2014)
  60. * - v 4.6 2014/02/25
  61. * + Fix bugs in filter function for open applets.
  62. * - v 4.5 2014/02/24
  63. * + Fix bugs in params.
  64. * + Insert new param type.
  65. * - v 4.4 2014/01/24
  66. * + Allow select type of params.
  67. * + Insert the use of applet params specific for activities.
  68. * - v 4.3 2014/01/23
  69. * + Insert function for move activities for other iLM (ilm_settings::confirm_move_iassign, ilm_settings::move_iassign).
  70. * - v 4.2 2016/02/13
  71. * + Fixed API usage to work fine under Moodle 3.X: ilm_editor_new()
  72. * --------------- (above code used at our MOOC 2014)
  73. *
  74. * - v 4.1 2013/12/13
  75. * + Insert log in iAssign actions.
  76. * + Allow use the language in iLM description (ilm_settings::new_file_ilm, ilm_settings::new_ilm, ilm_settings::edit_ilm, ilm_settings::copy_new_version_ilm, ilm_settings::add_edit_copy_ilm, iassign_language::get_description_lang, iassign_language::get_all_lang).
  77. * + Insert class for Log actions in system.
  78. * - v 4.0 2013/10/31
  79. * + Insert support of export iLM in zip packages (ilm_settings::export_ilm).
  80. * + Insert support of import iLM from zip packages (ilm_settings::import_ilm).
  81. * + Fix bugs in message alert in iassign title and remove message alert of the description by cache error.
  82. * - v 3.9 2013/10/25
  83. * + Insert support of upgrade iLM.
  84. * + Insert support for more than one extension in iLM.
  85. * + Fix bugs in verion control.
  86. * - v 3.8 2013/09/19
  87. * + Get data of general fields in iassign statement table (iassign::add_edit_iassign).
  88. * - v 3.7 2013/09/12
  89. * + Change tag APPLET in all functions of module (ilm::view_iLM, ilm_manager::ilm_editor_new, ilm_manager::ilm_editor_update).
  90. * + Insert tool for manage aditional params for iLM (ilm_settings::add_edit_copy_param, ilm_settings::visible_param, ilm_settings::add_param, ilm_settings::edit_param, ilm_settings::copy_param, ilm_settings::delete_param).
  91. * - v 3.6 2013/09/05
  92. * + Insert function ilm_settings::applet_ilm for create APPLET html tag.
  93. * + Insert function ilm_settings::applet_filetime for get modified date of iLM file.
  94. * + Change tag APPLET in function ilm_settings::view_ilm.
  95. * - v 3.5 2013/08/26
  96. * + Fix bug in download package iassign without answers (iassign::report).
  97. * - v 3.4 2013/08/23
  98. * + Fix bug in export package iassign.
  99. * - v 3.3 2013/08/22
  100. * + Insert functions for export users answer in iassign (iassign::export_file_answer, iassign::export_package_answer, iassign::view_iassign_current, iassign::report).
  101. * + Insert function for rename iassign file (ilm_manager::rename_file_ilm, ilm_manager::view_files_ilm).
  102. * - v 3.2 2013/08/21
  103. * + Change title link with message for get file for donwload file (ilm_manager::view_files_ilm).
  104. * + Change functions for import files for ilm_manager.php.
  105. * + Create static utils class for functions system utils (iassign_utils::format_filename, iassign_utils::version_filename).
  106. * - v 3.1 2013/08/15
  107. * + Change return file selected (ilm_manager::add_ilm).
  108. * + Insert functions for import files, export files and remove selected files (ilm_manager::view_files_ilm, ilm_manager::import_files_ilm, ilm_manager::export_files_ilm, ilm_manager::delete_selected_ilm).
  109. * - v 3.0 2013/08/02
  110. * + Insert link for view informations of iLMs in teacher view, same screen of admin view but wiht some features hide (ilm_settings::list_ilm, ilm_settings::view_ilm, iassign::view_iassigns).
  111. * - v 2.9 2013/08/01
  112. * + Fix bugs in functions ilm_settings::new_file_ilm, ilm_settings::copy_new_version_ilm, ilm_settings::add_edit_copy_ilm.
  113. * - v 2.8 2013/07/25
  114. * + Insert the activity name in header of view (activity::view_dates).
  115. * + Set function default iLM in view iLMs versions (ilm_settings::default_ilm and ilm_settings::confirm_default_ilm).
  116. * - v 2.7 2013/07/24
  117. * + Create link previous and next for student view in one activity (activity::view_dates).
  118. * + Fix bugs for view error in iLM not on DB in function iassign::view_iassign_current.
  119. * - v 2.6 2013/07/23
  120. * + Fix bugs for view files in function ilm_manager::view_files_ilm.
  121. * + Fix bugs for comment on teacher view in function iassign::view_iassign_current.
  122. * - v 2.5 2013/07/12
  123. * + Change iLM settings for accept versions (ilm_settings::new_file_ilm, ilm_settings::new_ilm, ilm_settings::edit_ilm, ilm_settings::copy_new_version_ilm).
  124. * + Insert new informations in iLMs table: created date, modified date, author, version, modified date of JAR (ilm_settings::view_ilm).
  125. * + Added support for PHP 7.0 Constructors and fallback for previous Moodle Versions (<3.1) (@contributor Ma'rcio Passos)
  126. *
  127. * @author Patricia Alves Rodrigues
  128. * @author Leo^nidas de Oliveira Branda~o
  129. * @contributor Igor Moreira Fe'lix (2018-)
  130. * @contributor Ma'rcio de Lima Passos (2014)
  131. * @version v 4.9.9 2023/04/20
  132. * @version v 4.9.2 2019/03/13
  133. * @version v 4.8 2016/05/12
  134. * @package mod_iassign_lib
  135. * @since 2010/09/27
  136. * @copyright iMath (http://www.matematica.br) and LInE (http://line.ime.usp.br) - Computer Science Dep. of IME-USP (Brazil)
  137. *
  138. * <b>License</b>
  139. * - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  140. */
  141. //TODO Review: eliminate iLM JAR under MoodleData? It is necessary to HTML5 packages (like iVProgH5), anyway
  142. //TODO Whenever under HTTPS, verify if MoodleData is working, if it is not, please use iLM JAR under WWW setting $CONF_WWW = 1
  143. //TODO (under HTTPS could fail 'pluginfile', like 'https://saw.atp.usp.br/pluginfile.php/1/mod_iassign/ilm/182563135/iassign/ilm/igeom/5920/iGeom.jar"'
  144. $CONF_WWW = 1; //TODO get iLM (JAR) from WWW, avoiding MoodleData
  145. /// Standard base class for all iAssign
  146. // Basic class to iAssign block of activities
  147. class iassign {
  148. var $cm;
  149. var $course;
  150. var $iassign;
  151. var $striassign;
  152. var $striassigns;
  153. var $context;
  154. var $activity;
  155. var $iassign_up;
  156. var $iassign_down;
  157. var $action;
  158. var $iassign_submission_current;
  159. var $userid_iassign;
  160. var $allowPostButton;
  161. var $write_solution;
  162. var $view_iassign;
  163. var $coursecontext, $return; // if missing, PHP8 launch warning bellow
  164. // Attention with PHP8: Deprecated: Creation of dynamic property iassign::X is deprecated in /var/www/html/saw/mod/iassign/locallib.php on line Y
  165. // 3.1 update PHP 7.0 compatibility for all moodle versions
  166. //D public function iassign($iassign, $cm, $course) { self::__construct($iassign, $cm, $course); }
  167. /// Constructor for the base iassign class
  168. // @calledby ./mod/iassign/view.php : $iassigninstance = new iassign($iassign, $cm, $course)
  169. // @calledby ./mod/iassign/grade.php : $iassigninstance = new iassign($iassign, $cm, $course);
  170. // @calledby ./mod/iassign/renderer.php : return $this->render(new iassign_files($context, $itemid, $filearea));
  171. function __construct ($iassign, $cm, $course) {
  172. global $COURSE, $CFG, $USER, $DB;
  173. $botton = optional_param('botton', NULL, PARAM_TEXT);
  174. $this->userid_iassign = optional_param('userid_iassign', 0, PARAM_INT);
  175. if (!is_null($botton))
  176. $USER->iassignEdit = $botton;
  177. // The Moodle function 'optional_param(...)' allow to filter GET parameters over a click that launches, e.g., './mod/iassign/view.php&userid_iassign=6'
  178. // If "$var1 = optional_param('userid_iassign', 'NOT', PARAM_TEXT);", $var1 will be set to 6 (if 'userid_iassign' is not present, $var1 will be set to 'NOT'
  179. $this->iassign_up = optional_param('iassign_up', 0, PARAM_INT); // if parameter 'iassign_up' does not exists or it is not integer => use 0 as "default"
  180. $this->iassign_down = optional_param('iassign_down', 0, PARAM_INT);
  181. $this->iassign_submission_current = optional_param('iassign_submission_current', 0, PARAM_INT);
  182. $this->write_solution = optional_param('write_solution', 0, PARAM_INT);
  183. $this->action = optional_param('action', NULL, PARAM_TEXT);
  184. $this->cm = $cm;
  185. $this->context = context_module::instance($this->cm->id);
  186. if (!has_capability('mod/iassign:evaluateiassign', $this->context, $USER->id))
  187. $this->userid_iassign = $USER->id;
  188. if ($course) {
  189. $this->course = $course;
  190. }
  191. else if ($this->cm->course == $COURSE->id) {
  192. $this->course = $COURSE;
  193. }
  194. else if (!$this->course = $DB->get_record('course', array('id' => $this->cm->course))) {
  195. print_error('invalidid', 'iassign');
  196. }
  197. $this->coursecontext = context_course::instance($this->course->id);
  198. if ($iassign) {
  199. $this->iassign = $iassign;
  200. }
  201. else if (!$this->iassign = $DB->get_record('iassign', array('id' => $this->cm->instance))) {
  202. print_error('invalidid', 'iassign');
  203. }
  204. $USER->context = context_module::instance($this->cm->id);
  205. $USER->cm = $this->cm->id;
  206. $this->iassign->cmidnumber = $this->cm->idnumber; // compatibility with modedit ia obj
  207. $this->iassign->courseid = $this->course->id; // compatibility with modedit ia obj
  208. $this->context = context_module::instance($this->cm->id);
  209. $this->striassign = get_string('modulename', 'iassign');
  210. $this->striassigns = get_string('modulenameplural', 'iassign');
  211. $this->return = $CFG->wwwroot . "/mod/iassign/view.php?id=" . $this->cm->id;
  212. $this->allowPostButton = 0; // hide submit button
  213. $this->view_iassign = optional_param('action', false, PARAM_BOOL);
  214. $this->activity = new activity(optional_param('iassign_current', NULL, PARAM_TEXT)); // this is the activity inside iAssign block (content file)
  215. $this->view();
  216. } // function __construct($iassign, $cm, $course)
  217. //DEBUG To be used in debug process
  218. //DEBUG Present the calling stack
  219. function generateCallTrace () { //DEBUG
  220. $e = new Exception();
  221. $trace = explode("\n", $e->getTraceAsString());
  222. // reverse array to make steps line up chronologically
  223. $trace = array_reverse($trace);
  224. array_shift($trace); // remove {main}
  225. array_pop($trace); // remove call to this method
  226. $length = count($trace);
  227. $result = array();
  228. for ($i = 0; $i < $length; $i++) {
  229. $result[] = ($i + 1) . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', set the right ordering
  230. }
  231. return "\t" . implode("\n\t", $result);
  232. }
  233. //DEBUG "code_to_write_file_through_javascript_calling"
  234. function code_to_write_file_through_javascript_calling () {
  235. // Copy this code in any point to debug JavaScript calling
  236. // Used to write data in /mod/iassign/ilm_debug/
  237. // Usefull when view() is called through JavaScript
  238. //leo REMOVER! xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  239. // function write_comment_submission(): 5610/9739
  240. // if (!empty($submission_comment) && (strcmp($str1, $str2) != 0)) // there is comment and it is different from "previous"
  241. require_once("ilm_debug/escreva.php");
  242. $jj = 0; for ($ii=0; $ii<100000; $ii++) $jj += 1; //retardo para diferencia $timestamp de "view.php" e de "locallib.php"
  243. $userid = $USER->id;
  244. $data = date('Y_m_d_h_s');
  245. $timestamp = time(); // timestamp
  246. if (!isset($iassign_submission_current)) $iassign_submission_current = $_GET["iassign_submission_current"]; // http://localhost/saw/mod/iassign/view.php?action=viewsubmission&id=26981&iassign_submission_current=35665&userid_iassign=10823&iassign_current=6848&view_iassign=1&row=6&column=2
  247. if (!isset($iassign_current)) $iassign_current = $_GET["iassign_current"]; // iassign_current
  248. if (!isset($id)) $id = $_GET["id"]; // id => course_modules.id : course_modules.instance = iassign.id
  249. // http://localhost/saw/mod/iassign/view.php?action=viewsubmission&id=26981&iassign_submission_current=35665&userid_iassign=10823&iassign_current=6848&view_iassign=1&row=6&column=2
  250. // course_modules : id , course , module , instance = 6981 , 519 , 9 , 1787
  251. $texto = " userid=" . $userid . ", data=" . $data . ", timestamp=" . $timestamp . "\n id=" . $id . ", iassign_submission_current=" . $iassign_submission_current . ", iassign_current=" . $iassign_current . "\n";
  252. $texto .= " this->action = " . (isset($this->action) ? $this->action : "<>") . "\n";
  253. $contexto = $this->generateCallTrace(); // debug_backtrace()
  254. //$resp = writeContent("", "", "comentario_write_comment_submission_leo.txt", $texto);
  255. $resp = writeContent("", "", "comentario_locallib_view.txt", $contexto . "\n" . $texto);
  256. //leo REMOVER! xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  257. //PHP8.4 Warning: Undefined variable $id_submission in /var/www/html/saw/mod/iassign/locallib.php on line 276
  258. //PHP8.4 Warning: Undefined variable $sum_comment in /var/www/html/saw/mod/iassign/locallib.php on line 276
  259. //PHP8.4 Warning: Undefined variable $submission_comment in /var/www/html/saw/mod/iassign/locallib.php on line 276
  260. }
  261. /// Show iAssign by using the security filter (temporary data on '*_iassign_security')
  262. // This method provides the page to view each iLM with any interactive activity
  263. // @calledby view.php : $iassigninstance->view($id); that instantiate 'iassign' (constructor above)
  264. function view () { // This is an standard function for each iAssign instance (to not use parameters, it is ignored)
  265. global $USER, $DB, $OUTPUT;
  266. // $iassign_statementid = $this->iassign->iassign_statementid;
  267. if ($this->activity!=null && $this->activity->get_activity()!=null && isset($this->activity->get_activity()->id))
  268. $iassign_statementid = $this->activity->get_activity()->id;
  269. else
  270. $iassign_statementid = "";
  271. // '$this' has : iassign Object = cm ; course ; iassign ; striassign ; striassigns ; context ; activity ; iassign_up ; iassign_down ; action ; ... ; coursecontext ; return
  272. // If this user has no capability to View 'iassign': stop here
  273. require_capability('mod/iassign:view', $this->context); // /lib/accesslib.php: if (!has_capability($capability, $context, $userid, $doanything))
  274. // /lib/accesslib.php [873/5177]: function require_capability($capability, context $context, $userid = null, $doanything = true, $errormessage = 'nopermissions', $stringfile = '')
  275. // /lib/accesslib.php [432/5177]: function has_capability($capability, context $context, $user = null, $doanything = true) {
  276. //DEBUG To debug JavaScript calling, copy here the code in code_to_write_file_through_javascript_calling()
  277. // Use code to write data in /mod/iassign/ilm_debug/
  278. // Usefull when view() is called through JavaScript
  279. // Put here the code "code_to_write_file_through_javascript_calling"
  280. // Trigger module view event - in lib/classes/event/base.php!create(array $data = null)
  281. $event = \mod_iassign\event\course_module_viewed::create(array(
  282. 'objectid' => $this->iassign->id,
  283. 'context' => $this->context
  284. ));
  285. $event->add_record_snapshot('course', $this->course);
  286. $event->trigger();
  287. if ($this->action) { // when student do/redo activity or teacher see student solution
  288. $this->action(); // calls '$this->view_iLM();'
  289. }
  290. else {
  291. print $OUTPUT->header();
  292. $this->view_iassigns(); // show the iLM with the content
  293. print $OUTPUT->footer();
  294. }
  295. // Security: delete all records with an error loading iLM - 'iassign_security : id iassign_statementid userid file timecreated view'
  296. //D $DB->delete_records("iassign_security", array("userid" => $USER->id, "view" => 1));
  297. //D $iassign_iLM_security = $DB->get_record("iassign_security", array("iassign_statementid" => $iassign_statementid));
  298. //D foreach ($iassign_iLM_security as $item) { echo $iassign_iLM_security->id . " ; " . $iassign_iLM_security->iassign_statementid . " ; " . $iassign_iLM_security->userid . " ; " . $iassign_iLM_security->timecreated . " ; " . $iassign_iLM_security->view . " ; " . $iassign_iLM_security->file . "<br/>\n" }
  299. $DB->delete_records("iassign_security", array("userid" => $USER->id, "iassign_statementid" => $iassign_statementid));
  300. //D $iassign_iLM_security = $DB->get_record("iassign_security", array("iassign_statementid" => $iassign_statementid));
  301. //D if ($iassign_iLM_security) foreach ($iassign_iLM_security as $item) { echo $iassign_iLM_security->id . " ; " . $iassign_iLM_security->iassign_statementid . " ; " . $iassign_iLM_security->userid . " ; " . $iassign_iLM_security->timecreated . " ; " . $iassign_iLM_security->view . " ; " . $iassign_iLM_security->file . "<br/>\n" }
  302. //D else echo "Apagou!<br/>";
  303. die();
  304. } // function view()
  305. /// Execute the action from Moodle (move, make visible, register the exercise (teacher) or its answer (learner)...)
  306. function action () {
  307. global $USER;
  308. // action:
  309. // up - move up activity (mover atividade para cima)
  310. // down - move down activity (mover atividade para baixo)
  311. // visible - view/hide activity (exibir/ocultar atividade)
  312. // delete - delete activity (excluir atividade)
  313. // deleteyes - confirms exclusion of activity (confirm the activity exclusion)
  314. // deleteno - does not erase activity (do not erase activity)
  315. // add - add activity (adicionar atividade)
  316. // edit - edit activity (modificar atividade)
  317. // Role 'teacher': reach this point if the user has role 'teacher' above
  318. $action_iassign = array(
  319. 'newcomment' => '$this->add_comment();',
  320. 'view' => '$this->view_iassign_current();',
  321. 'get_answer' => '$this->get_answer();',
  322. 'repeat' => '$this->view_iassign_current();',
  323. 'overwrite' => '$this->get_answer();',
  324. 'stats_student' => '$this->stats_students();',
  325. 'download_answer' => '$this->export_file_answer();',
  326. 'download_all_answer' => '$this->export_package_answer();',
  327. 'notifications' => '$this->view_notifications();');
  328. // Role 'nonediting-teacher': reach this point if the user has role 'nonediting-teacher'
  329. $action_iassign_limit = array( // user with no capacity 'mod/iassign:editiassign' (e.g. 'nonediting-teacher')
  330. 'view' => '$this->view_iassign_current();',
  331. 'newcomment' => '$this->add_comment();', // this is necessary to allow "noneditingteacher" to send "new comment" in the student submission
  332. 'viewsubmission' => '$this->view_iassign_current();',
  333. 'edit_status' => '$this->edit_status();',
  334. 'edit_grade' => '$this->edit_grade();',
  335. 'stats_student' => '$this->stats_students();', // this is necessary to allow "noneditingteacher" to see iAssign survey
  336. 'export_csv' => '$this->export_csv();',
  337. 'report' => '$this->report();',
  338. 'print' => '$this->report();',
  339. 'stats' => '$this->stats();',
  340. 'printstats' => '$this->stats();');
  341. $restricted = array('firstdown' => '$this->activity->move_iassign("firstdown", $this->iassign_down, $this->return);',
  342. 'up' => '$this->activity->move_iassign("up", $this->iassign_up, $this->return);',
  343. 'down' => '$this->activity->move_iassign("down", $this->iassign_down, $this->return);',
  344. 'visible' => '$this->activity->visible_iassign($this->return);',
  345. 'delete' => '$this->activity->delete($this->return);',
  346. 'deleteno' => '$this->return_home_course("confirm_not_delete_iassign");',
  347. 'deleteyes' => '$this->activity->deleteyes($this->return, $this);',
  348. 'add' => '$this->add_edit_iassign();',
  349. 'edit' => '$this->add_edit_iassign();',
  350. 'get_answer' => '$this->get_answer();',
  351. 'duplicate_activity' => '$this->duplicate_activity();',
  352. 'move_activity' => '$this->move_activity();',
  353. 'auto_evaluate' => '$this->auto_evaluate();',
  354. 'all_submissions' => '$this->all_submissions();',
  355. 'delete_submissions' => '$this->delete_submissions();',
  356. 'download_submissions' => '$this->download_submissions();',
  357. 'open_individual_submission' => '$this->open_individual_submission();',
  358. 'get_individual_submission' => '$this->get_individual_submission();',
  359. 'get_student_submission' => '$this->get_student_submission();',
  360. 'get_teacher_exercise' => '$this->get_teacher_exercise();',
  361. 'post_auto_eval_result' => '$this->post_auto_eval_result();',
  362. 'import_activity' => '$this->import_activity();',
  363. 'import_activity_confirm' => '$this->import_activity_confirm();'
  364. );
  365. $action_iassign_restricted = array_merge($restricted, $action_iassign_limit, $action_iassign);
  366. $action = $this->action;
  367. $array_teacher = array("edit", "delete", "visible", "firstdown", "up", "down", "duplicate_activity");
  368. if (in_array($action, $array_teacher) && !has_capability('mod/iassign:editiassign', $this->context, $USER->id)) { //TODO block when editing (probably block must be in "add_edit_iassign()"
  369. // Invalid access!!! Perhaps the message 'invalidsection'='Invalid section.' is more explainable?
  370. // Capabilities 'mod/iassign:editiassign' and 'mod/iassign:editiassign' are similar, the first one is more permissive
  371. // allowing 'teacher' (the second starts with 'editingteacher').
  372. // redirect(new moodle_url('/admin/index.php'));
  373. print_error('accessdenied', 'admin', $CFG->wwwroot . "/" . $CFG->admin); // ./lang/en/admin.php: "Access denied"
  374. exit; // additional security...
  375. // $this->userid_iassign = $USER->id;
  376. }
  377. // On depends the user's capability: precedence to edit power ('editiassign'); after to analyse ('evaluateiassign'); otherwise only view (any other).
  378. if (has_capability('mod/iassign:editiassign', $this->context, $USER->id)) {
  379. // When teacher is seeing student solution. By 'viewsubmission' goes to the function 'view_iassign_current()'
  380. eval($action_iassign_restricted[$this->action]); // Now load 'view_iassign_current()' with 'viewsubmission' or 'new_iassign($param)' by '$this->add_edit_iassign()'
  381. }
  382. elseif (has_capability('mod/iassign:evaluateiassign', $this->context, $USER->id)) {
  383. //D echo "locallib.php: this->action=" . $this->action . "<br/>action_iassign_limit[]="; print_r($action_iassign_limit); echo "<br/>";
  384. if (isset($action_iassign_limit[$this->action]))
  385. eval($action_iassign_limit[$this->action]); // if the permission 'stats_student' is missing in $action_iassign_limit = array(...) this helps
  386. }
  387. else { // Student reaches this point
  388. // When student do/redo activity: do => action = "view"; redo => action = "repeat"
  389. // Arrive here with: "get_answer"; "view"
  390. if (isset($action_iassign[$this->action])) // avoid error if non capable user try direct acess
  391. eval($action_iassign[$this->action]); // Now load 'view_iassign_current()' with 'view'
  392. }
  393. } // function action()
  394. //2022: ---------------------------------------------------------------------------------------------------------------------------------
  395. //2022: Recuperacao {files} {iassign_statement}
  396. //2022/02 see_all_files_context($context, $DB)
  397. /// Temporary: fix 'iassign_statement.filesid' and 'files.itemid' (iassign_statement.filesid <- files.id; files.itemid <- iassign_statement.id)
  398. // First version of iAssign used a random value to associate 'files.itemid' with 'iassign_statement.file'
  399. // @calledby locallib.php ! get_update_files($context_id, $component, $filearea, $iassign_statement)
  400. static function update_iassign_statement_files ($iassign_statement_id, $context_id, $component, $filearea, $files_itemid) {
  401. global $DB; //2022: echo "update_iassign_statement_files:<br/>";//D2022
  402. // $files = $DB->get_records("files", array("component" => "mod_iassign", "filearea" => "exercise", "contextid" => $context_id, "itemid" => $files_itemid)); // contextid=1390 ; itemid=400 ;
  403. // $sql_query = "SELECT * FROM {files} WHERE component='mod_iassign' AND filearea='exercise' AND contextid=" . $context_id . " AND filename<>'.' AND itemid=" . $files_itemid;
  404. $sql_query = "SELECT * FROM {files} WHERE component='mod_iassign' AND filearea='exercise' AND contextid=" . $context_id . " AND itemid=" . $files_itemid;
  405. $elem_in_files = $DB->get_records_sql($sql_query);
  406. if (!$elem_in_files) {
  407. print "Vazio! <br/>"; //TODO: se mantiver, internacionalizar!
  408. return NULL;
  409. }
  410. // if (count($elem_in_files)>1) { echo "Problema! Mais de um arquivo!<br/>"; return NULL; }
  411. //2022: echo "#files=" . count($elem_in_files) . ": "; // print_r($elem_in_files); echo "<br/>";//D2022
  412. foreach ($elem_in_files as $element_file) { // since ID is greater than 0, only "$file1 = $elem_in_files[0]" will not work!
  413. if ($element_file->filename != '.') { // If not '.' is the actual file
  414. $file1 = $element_file; // this '$file1' has the 'hashcontent' of the real file content, then it will be used to update 'iassign_statement.filesid' <- 'files.id'
  415. }
  416. // Update 'files.itemid' with 'iassign_statement.id'
  417. $newentry = new stdClass();
  418. $newentry->id = $element_file->id; $newentry->itemid = $iassign_statement_id;
  419. //2022: if (!$DB->update_record('files', $newentry)) print_error('error_update', 'iassign');
  420. }
  421. // Update 'iassign_statement.filesid' with 'files.id'
  422. $newentry = new stdClass();
  423. $newentry->id = $iassign_statement_id; $newentry->filesid = $file1->id;
  424. //2022: if (!$DB->update_record('iassign_statement', $newentry)) print_error('error_update', 'iassign');
  425. } // static function update_iassign_statement_files($iassign_statement_id, $context_id, $component, $filearea, $files_itemid)
  426. //TODO
  427. //TODO Remover funcao 'get_update_files(.)' em favor do 'files_functions.php!update_iassignstatement_files(.)' ???????????
  428. //TODO
  429. /// Get file from table 'files', if 'files.itemid != iassign_statement.id' then update 'files.itemid' with 'iassign_statement.id'
  430. // Usually 'context.instanceid' is 'course_modules.id' and 'course_modules.instance = iassign.id'
  431. // However since iAssign is a "block of actual activities" from "iassign_statement", we store in 'files.itemid' the 'iassign_statement.id'
  432. // @calledby function add_edit_iassign(): if ($this->action == 'edit'): to edit iAssign activity with "$files_list = iassign::get_update_files(...)" [2721/9504]
  433. static function get_update_files ($context_id, $component, $filearea, $iassign_statement, $fs) {
  434. // $fs is the reference to all files in Moodle data
  435. //leo //REMOVE
  436. echo "<br/><br/><br/><br/><br/>get_update_files: iassign_statement: " . $context_id . ", " . $component . ", " . $filearea . ", " .
  437. "id=" . $iassign_statement->id . ", file=" . $iassign_statement->file . ", filesid=" . $iassign_statement->filesid .
  438. ", iassign_ilmid =" . $iassign_statement->iassign_ilmid . ",name=" . $iassign_statement->name . "<br/>\n";//D2022
  439. $msgAux = "";
  440. $must_update_files_itemid = 0; // if changed to 1 => must update {files}.itemid; depois muda para $iassign_statement->filesid
  441. // To consider old versions with improper association 'files.itemid': try 'iassign_statement.id' (correct one), 'iassign_statement.file' and 'iassign_statement.filesid'
  442. // Correct association: {files}->itemid = $iassign_statement->id and {files}->id=$iassign_statement->fileid;
  443. $files = $fs->get_area_files($context_id, $component, $filearea, $iassign_statement->id); // try: 'iassign_statement.id'
  444. if (!$files) { // Could not find with 'iassign_statement.id'
  445. $msgAux .= "- Nao com 'iassign_statement.id'<br/>\n";
  446. $must_update_files_itemid = 1; // {files}.itemid must receive {iassign_statement}.id
  447. $files = $fs->get_area_files($context_id, $component, $filearea, $iassign_statement->file); // try with 'iassign_statement.file'
  448. if (!$files) { // Could not find with 'iassign_statement.file'
  449. $msgAux .= "- Nao com 'iassign_statement.file'<br/>\n";
  450. $files = $fs->get_area_files($context_id, $component, $filearea, $iassign_statement->filesid); // try with 'iassign_statement.filesid'
  451. if (!$files) { // Could not find with 'iassign_statement.file'
  452. print "ERROR {files} not found: " . $msgAux; //D2022
  453. return NULL;
  454. }
  455. else { $must_update_files_itemid = $iassign_statement->filesid; echo "OK 3, nao com 'id', nem com 'file', mas pegou com 'iassign_statement.filesid'=" . $iassign_statement->filesid . "<br/>\n"; }
  456. }
  457. else { $must_update_files_itemid = $iassign_statement->file; echo "OK 2, nao com 'id', pegou com 'iassign_statement.file'=" . $iassign_statement->file . "<br/>\n"; }
  458. }
  459. else echo "OK 1, pegou com 'iassign_statement.id'=" . $iassign_statement->id . "<br/>\n";//D2022
  460. if ($must_update_files_itemid!=0) {
  461. $res_update = iassign::update_iassign_statement_files($iassign_statement->id, $context_id, $component, $filearea, $must_update_files_itemid);
  462. }
  463. foreach ($files as $file) { if ($file->get_filename() != '.') break; }
  464. //$files_itemid = $file->get_itemid(); // /var/www/html/saw2021_1/lib/filestorage/stored_file.php: devolve 'files.itemid'
  465. //$files_id = $file->get_id(); // file_record
  466. echo "files[.]: id=" . $files_id . ", itemid=" . $files_itemid . "<br/>\n"; //D2022
  467. return $file;
  468. } // function get_update_files($context_id, $component, $filearea, $iassign_statement)
  469. //2022: Recuperacao {files} {iassign_statement}
  470. //2022: ---------------------------------------------------------------------------------------------------------------------------------
  471. /// This method prints the student submission to a specific exercise
  472. function get_student_submission () {
  473. global $DB;
  474. $student = optional_param('iassign_student', NULL, PARAM_TEXT);
  475. $exercise = optional_param('iassign_exercise', NULL, PARAM_TEXT);
  476. if (!$student || !$exercise) exit;
  477. $iassign_submission = $DB->get_record("iassign_submission", array("iassign_statementid" => $exercise, "userid" => $student));
  478. if ($iassign_submission)
  479. print($iassign_submission->answer);
  480. die();
  481. }
  482. /// This method prints the teacher exercise
  483. // @calledby AJAX trhough "function action()" that calls "function auto_evaluate()"
  484. function get_teacher_exercise () {
  485. global $DB, $COURSE;
  486. $ilmid = optional_param('iassign_exercise', NULL, PARAM_INT);
  487. $iassign_statement = $DB->get_record("iassign_statement", array("id" => $ilmid));
  488. $fs = get_file_storage(); // Get reference to all files in Moodle data
  489. //2021/12: $files = $fs->get_area_files(context_module::instance($this->cm->id)->id, 'mod_iassign', 'exercise', $iassign_statement->filesid);
  490. //TODO 2022: trocar "$iassign_statement->id" por "$iassign_statement->file"???
  491. // ./lib/filestorage/file_storage.php: public function get_area_files($contextid, $component, $filearea, $itemid = false,...)
  492. $files = $fs->get_area_files(context_module::instance($this->cm->id)->id, 'mod_iassign', 'exercise', $iassign_statement->id); // use itemid=$iassign_statement->id
  493. if (!$files) { // try with 'iassign_statement->filesid'
  494. $files = $fs->get_area_files(context_module::instance($this->cm->id)->id, 'mod_iassign', 'exercise', $iassign_statement->filesid);
  495. }
  496. foreach ($files as $file) {
  497. if ($file->get_filename() != ".") {
  498. print($file->get_content());
  499. }
  500. }
  501. die();
  502. }
  503. /// This method receives the data from auto_eval event
  504. /// @see auto_evaluate()
  505. function post_auto_eval_result () {
  506. global $COURSE, $CFG, $USER, $DB;
  507. if (!has_capability('mod/iassign:runautoevaluate', $USER->context, $USER->id))
  508. exit;
  509. //$exercise="+exercises[actual_exercise]+"&student="+students[actual_student]+"&grade="+(grade * actual_value),
  510. $exercise = optional_param('iassign_exercise', -1, PARAM_INT);
  511. $student = optional_param('student', -1, PARAM_INT);
  512. $grade = optional_param('grade', 0, PARAM_FLOAT);
  513. // 1: Recuperar a nota atual:
  514. $actual = $DB->get_record('iassign_submission', array('iassign_statementid' => $exercise, 'userid' => $student));
  515. // 2: Atualizar a tabela:
  516. $update_entry = new stdClass();
  517. $update_entry->id = $actual->id;
  518. $update_entry->previous_grade = $actual->grade;
  519. $update_entry->grade = $grade;
  520. // Update the iassign_submission
  521. $DB->update_record("iassign_submission", $update_entry);
  522. }
  523. /// Function to help download all_submissions files
  524. function download_submissions_help ($list) {
  525. global $DB, $CFG, $OUTPUT;
  526. // Get list of iLMs
  527. $ilm_list = $DB->get_records("iassign_ilm");
  528. // Get list of enrolled students:
  529. $params_temp = array('shortname' => 'student');
  530. $role = $DB->get_record_sql("SELECT s.id, s.shortname FROM {role} s WHERE s.shortname = :shortname", $params_temp);
  531. $context = context_course::instance($this->course->id);
  532. $params_temp = array('contextid' => $context->id, 'roleid' => $role->id);
  533. $students_list = $DB->get_records_sql(
  534. "SELECT s.userid, a.firstname, a.lastname FROM {role_assignments} s, {user} a WHERE s.contextid = :contextid AND s.userid = a.id AND s.roleid = :roleid ORDER BY a.firstname ASC,a.lastname ASC", $params_temp);
  535. $json_obj = new stdclass();
  536. $json_obj->submissions = [];
  537. $csvfields = [];
  538. $csvfields[] = 'submissionid';
  539. $csvfields[] = 'file';
  540. $csvfields[] = 'filesize';
  541. $csvfields[] = 'timestamp';
  542. $csvfields[] = 'humandate';
  543. $csvfields[] = 'grade';
  544. $csvfields[] = 'userid';
  545. $csvfields[] = 'exerciseid';
  546. $json_obj->submissions[] = $csvfields;
  547. $exercise_dir = [];
  548. $exercises_list = [];
  549. foreach ($list as $item) {
  550. if (!in_array($item->iassign_statementid, $exercise_dir)) {
  551. $temp = $DB->get_record("iassign_statement", array("id" => $item->iassign_statementid));
  552. $exercises_list[] = $temp;
  553. $exercise_dir[] = $temp->id;
  554. }
  555. }
  556. $student_dir = [];
  557. $students_list = [];
  558. foreach ($list as $item) {
  559. if (!in_array($item->userid, $student_dir)) {
  560. $temp = $DB->get_record("user", array("id" => $item->userid));
  561. $students_list[] = $temp;
  562. $student_dir[] = $temp->id;
  563. }
  564. }
  565. //2021 $directory = $CFG->dataroot . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR;
  566. //TODO: tem que criar moodledata/temp/iassign_files
  567. // Create $CFG->dataroot/temp/iassign_files
  568. $tempfilespath = $CFG->dataroot . DIRECTORY_SEPARATOR . 'temp';
  569. if (!file_exists($tempfilespath)) {
  570. mkdir($tempfilespath, 0777, true);
  571. }
  572. $iassignfilespath = $tempfilespath . DIRECTORY_SEPARATOR . 'iassign_files';
  573. if (!file_exists($iassignfilespath)) {
  574. mkdir($iassignfilespath, 0777, true);
  575. }
  576. $directory = $CFG->dataroot . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'iassign_files' . DIRECTORY_SEPARATOR;
  577. if (!file_exists($directory)) {
  578. // print 'Não foi possível criar o diretório para preparar o arquivo com as submissões. Possivelmente o caminho está bloqueado para escrita.';
  579. print 'It was not possible to create the directory to receive the students submission files. Possibly the path has no write permission.';
  580. exit;
  581. }
  582. $zip_filename = $directory . 'package_iassign_' . time() . '.zip';
  583. $zip = new zip_archive(); // create ZIP
  584. if (!$zip->open($zip_filename)) {
  585. // print 'Não foi possível criar o arquivo zip com as submissões dos alunos, pois o diretório no servidor está bloqueado para escrita: ' . $directory;
  586. print 'It was not possible to create the ZIP file with the students submission files. Possibly the server directory has no write permission: ' . $directory;
  587. exit;
  588. }
  589. foreach ($exercises_list as $exercise) {
  590. $dir_ex = iassign_utils::format_filename($exercise->name);
  591. $zip->add_directory($dir_ex);
  592. $extension_ilm = '';
  593. foreach ($ilm_list as $ilm) {
  594. if ($exercise->iassign_ilmid == $ilm->id) {
  595. $extension_ilm = $ilm->extension;
  596. break;
  597. }
  598. }
  599. foreach ($students_list as $student) {
  600. $individual_dir =
  601. $dir_ex . DIRECTORY_SEPARATOR .
  602. iassign_utils::format_filename($student->firstname . ' ' . $student->lastname);
  603. $zip->add_directory($individual_dir);
  604. $i = 1;
  605. foreach ($list as $item) {
  606. if ($item->userid == $student->id && $item->iassign_statementid == $exercise->id) {
  607. $date = new DateTime();
  608. $date->setTimestamp($item->timecreated);
  609. $new_file = $individual_dir . DIRECTORY_SEPARATOR . iassign_utils::format_filename('submission_' . ($i++) . '_' . date_format($date, 'Y-m-d_H-i-s')) . '.' . $extension_ilm;
  610. $zip->add_file_from_string($new_file, $item->answer);
  611. $temp_json = [];
  612. $temp_json[] = intval($item->id);
  613. $temp_json[] = $new_file;
  614. $temp_json[] = strlen($new_file);
  615. $temp_json[] = intval($item->timecreated);
  616. $temp_json[] = date_format($date, 'Y-m-d H-i-s');
  617. $temp_json[] = doubleval($item->grade);
  618. $temp_json[] = intval($item->userid);
  619. $temp_json[] = intval($exercise->id);
  620. $json_obj->submissions[] = $temp_json;
  621. }
  622. }
  623. }
  624. }
  625. $csv_str = "";
  626. foreach ($json_obj->submissions as $sub) {
  627. $csv_str .= $this->csvstr($sub) . "\n";
  628. }
  629. $zip->add_file_from_string('index.csv', $csv_str);
  630. $zip->close();
  631. iassign_utils::download_file($zip_filename);
  632. exit;
  633. }
  634. function csvstr (array $fields) : string {
  635. $f = fopen('php://memory', 'r+');
  636. if (fputcsv($f, $fields) === false) {
  637. return false;
  638. }
  639. rewind($f);
  640. $csv_line = stream_get_contents($f);
  641. return rtrim($csv_line);
  642. }
  643. /// Download submissions from table iassign_allsubmissions
  644. function download_submissions () {
  645. global $COURSE, $CFG, $USER, $DB, $OUTPUT, $PAGE;
  646. if (!has_capability('mod/iassign:runautoevaluate', $this->context, $USER->id))
  647. exit;
  648. $type = optional_param('type', NULL, PARAM_TEXT);
  649. $item = optional_param('item', NULL, PARAM_TEXT);
  650. $exercise = optional_param('exercise', NULL, PARAM_TEXT);
  651. $iassign_id = optional_param('iassign', NULL, PARAM_TEXT);
  652. $sub_list = [];
  653. if ($type == 'individual') {
  654. $sub_list[] = $DB->get_record(
  655. 'iassign_allsubmissions', array('id' => $item));
  656. } elseif ($type == 'student') {
  657. $sub_list = $DB->get_records(
  658. 'iassign_allsubmissions', array('userid' => $item, 'iassign_statementid' => $exercise));
  659. } elseif ($type == 'exercise') {
  660. $sub_list = $DB->get_records(
  661. 'iassign_allsubmissions', array('iassign_statementid' => $item));
  662. } elseif ($type == 'block') {
  663. $exercises_in_block = $DB->get_records(
  664. 'iassign_statement', array('iassignid' => $iassign_id));
  665. foreach ($exercises_in_block as $exercise) {
  666. $temp = $DB->get_records(
  667. 'iassign_allsubmissions', array('iassign_statementid' => $exercise->id));
  668. $sub_list = array_merge($sub_list, $temp);
  669. }
  670. }
  671. $this->download_submissions_help($sub_list);
  672. }
  673. /// Delete submissions in table iassign_allsubmissions
  674. function delete_submissions () {
  675. global $COURSE, $CFG, $USER, $DB, $OUTPUT, $PAGE;
  676. if (!has_capability('mod/iassign:runautoevaluate', $this->context, $USER->id))
  677. exit;
  678. $type = optional_param('type', NULL, PARAM_TEXT);
  679. $item = optional_param('item', NULL, PARAM_TEXT);
  680. $exercise = optional_param('exercise', NULL, PARAM_TEXT);
  681. if ($type == 'individual') {
  682. $DB->delete_records('iassign_allsubmissions', array('id' => $item));
  683. } elseif ($type == 'student') {
  684. $DB->delete_records('iassign_allsubmissions', array('userid' => $item, 'iassign_statementid' => $exercise));
  685. } elseif ($type == 'exercise') {
  686. $DB->delete_records('iassign_allsubmissions', array('iassign_statementid' => $item));
  687. }
  688. }
  689. function get_individual_submission () {
  690. global $DB, $USER;
  691. if (!has_capability('mod/iassign:runautoevaluate', $this->context, $USER->id))
  692. exit;
  693. $submissionid = optional_param('submissionid', NULL, PARAM_TEXT);
  694. $submission = $DB->get_record("iassign_allsubmissions", array("id" => $submissionid));
  695. print $submission->answer;
  696. }
  697. /// Open individual submission
  698. function open_individual_submission () {
  699. global $DB, $USER, $CFG;
  700. if (!has_capability('mod/iassign:runautoevaluate', $this->context, $USER->id))
  701. exit;
  702. $submissionid = optional_param('item', NULL, PARAM_TEXT);
  703. $id = $this->cm->id;
  704. $param_aux = "id=" . $id . "&userid_iassign=" . $USER->id;
  705. $url = "" . $CFG->wwwroot . "/mod/iassign/view.php?action=get_individual_submission&" . $param_aux . "&submissionid=" . $submissionid;
  706. $atual_submission = $DB->get_record('iassign_allsubmissions', array('id' => $submissionid));
  707. $atual_statement = $DB->get_record("iassign_statement", array("id" => $atual_submission->iassign_statementid));
  708. $all_submissions = $DB->get_records('iassign_allsubmissions', array('iassign_statementid' => $atual_submission->iassign_statementid, 'userid' => $atual_submission->userid));
  709. $user = $DB->get_record('user', array('id' => $atual_submission->userid));
  710. $previous = null;
  711. $next = null;
  712. $i = 0;
  713. $found = false;
  714. foreach($all_submissions as $individual) {
  715. if (!$found && $individual->id != $submissionid) {
  716. $previous = $individual;
  717. }
  718. if ($found) {
  719. $next = $individual;
  720. break;
  721. }
  722. if ($individual->id === $submissionid) {
  723. $found = true;
  724. }
  725. $i++;
  726. }
  727. print '<center>' . "\n";
  728. print '<table cellpading=5 cellspacing=5 style="padding-bottom: 2rem;font-size: 1.1em;"><tr><td>' . get_string('exercise', 'iassign') . ':</td><td>' . $atual_statement->name . '</td></tr>' . "\n";
  729. print '<tr><td>' . get_string('student', 'iassign') . '</td><td>' . $user->firstname . ' ' . $user->lastname . '</td></tr>' . "\n";
  730. print '<tr><td>' . get_string('grade_student', 'iassign') . ':</td><td>' . $atual_submission->grade . '</td></tr>' . "\n";
  731. print '<tr><td>' . get_string('all_submissions_manager_date', 'iassign') . ':</td><td>' . userdate($atual_submission->timecreated) . '</td></tr>' . "\n";
  732. print '</table>' . "\n";
  733. if ($previous) {
  734. $url_prev = "" . $CFG->wwwroot . "/mod/iassign/view.php?action=open_individual_submission&" . $param_aux . "&item=" . $previous->id;
  735. print '<a href="' . $url_prev . '" style="color: blue; text-decoration: none;margin: 1rem;"><span>&#9664;</span> ' . get_string('all_submissions_manager_previous', 'iassign') . '</a> ';
  736. } else {
  737. print '<a href="#" style="color: gray; text-decoration: none;margin: 1rem;"><span>&#9664;</span> ' . get_string('all_submissions_manager_previous', 'iassign') . '</a> ';
  738. }
  739. print ' (' . $i . '/' . count($all_submissions) . ')';
  740. if ($next) {
  741. $url_next = "" . $CFG->wwwroot . "/mod/iassign/view.php?action=open_individual_submission&" . $param_aux . "&item=" . $next->id;
  742. print '<a href="' . $url_next . '" style="color: blue; text-decoration: none;margin: 1rem;">' . get_string('all_submissions_manager_next', 'iassign') . ' <span>&#9654;</span></a>' . "\n";
  743. } else {
  744. print '<a href="#" style="color: gray; text-decoration: none;margin: 1rem;">' . get_string('all_submissions_manager_next', 'iassign') . ' <span>&#9654;</span></a>' . "\n";
  745. }
  746. $statement = $DB->get_record('iassign_statement', array('id' => $atual_submission->iassign_statementid));
  747. //array("type" => "editor_new", "notSEND" => "true")
  748. print ilm_settings::build_ilm_tags($statement->iassign_ilmid, ['type' => "activity", 'Proposition' => $url, "notSEND" => "false"]);
  749. print '</center>' . "\n";
  750. } // function open_individual_submission()
  751. //2021/12 ---------------------------------------------- start 1 (to import activities)
  752. // To import activities
  753. function import_activity_confirm () {
  754. global $USER, $DB;
  755. if (!has_capability('mod/iassign:runautoevaluate', $this->context, $USER->id)) // security
  756. exit;
  757. if (!isset($_REQUEST['statements'])) // security
  758. exit;
  759. $iassign_destiny = $this->iassign->id;
  760. $activities_list = $_REQUEST['statements'];
  761. foreach ($activities_list as $activity) {
  762. $activity_source = $DB->get_record('iassign_statement', array('id' => $activity));
  763. $activity_source->id = 0;
  764. $iassign_id_from = $activity_source->iassignid;
  765. $filesid_from = $activity_source->filesid;
  766. $activity_source->iassignid = $iassign_destiny;
  767. $author = $USER; //x $DB->get_record("user", array("id" => $USER->id)); --- useless since we already have {user} info.!!!
  768. $activity_source->author_modified_name = $author->firstname . ' ' . $author->lastname . ' (' . $author->id . ')';
  769. $activity_source->author_modified = $activity_source->author_modified_name;
  770. if ($id_ = $DB->insert_record("iassign_statement", $activity_source)) {
  771. $fs = get_file_storage();
  772. // Duplicate activity file
  773. $files_ids = $DB->get_records_sql('SELECT id FROM {files} WHERE component="mod_iassign" AND itemid=' . $filesid_from);
  774. print("Files to be copied (ID): "); //TODO: universalizar 'Arquivos a serem copiados: '
  775. print_r($files_ids); //
  776. foreach ($files_ids as $fileid) {
  777. $source_file = $fs->get_file_by_id($fileid->id);
  778. // ./lib/filestorage/file_storage.php : "public function create_file_from_storedfile($filerecord, $fileorid)" 1051/2412 on Moodle 2018120302.08
  779. $newfile = $fs->create_file_from_storedfile(array('contextid' => $this->context->id, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $id_), $source_file);
  780. $updateentry = new stdClass();
  781. $updateentry->id = $id_;
  782. $updateentry->filesid = $id_;
  783. // Update the duplicated iLM iAssign with new file id
  784. $DB->update_record("iassign_statement", $updateentry);
  785. }
  786. }
  787. } // foreach ($activities_list as $activity)
  788. $destiny_url = new moodle_url('/mod/iassign/view.php', array('id' => $this->context->id));
  789. $str_index = count($activities_list) > 1 ? 'import_activities_success' : 'import_activity_success';
  790. redirect($this->return, get_string($str_index, 'iassign'), null, \core\output\notification::NOTIFY_SUCCESS);
  791. exit;
  792. } // function import_activity_confirm()
  793. function import_activity () {
  794. global $COURSE, $CFG, $USER, $DB, $OUTPUT, $PAGE;
  795. if (!has_capability('mod/iassign:runautoevaluate', $this->context, $USER->id))
  796. exit;
  797. print '<script>
  798. function verify_selection () {
  799. document.getElementById("msg-error").classList.add("d-none");
  800. var checkboxes = document.getElementsByName("statements[]");
  801. for (var i = 0; i < checkboxes.length; i++)
  802. if (checkboxes[i].checked) return true;
  803. document.getElementById("msg-error").classList.remove("d-none");
  804. return false;
  805. }
  806. </script>'; // "
  807. $destiny_url = new moodle_url('/mod/iassign/view.php', array('id' => $this->cm->id, 'action' => 'import_activity_confirm'));
  808. print $OUTPUT->header();
  809. $this->print_main_menu();
  810. print '<h2 class="m-b-2 mt-4">' . get_string('import_activity', 'iassign') . '</h2>' . "\n";
  811. print '<form action="' . $destiny_url . '" method="post" onsubmit="return verify_selection()">' . "\n";
  812. $courses_as_teacher = $this->get_courses_enroled_as_teacher($USER->id);
  813. foreach ($courses_as_teacher as $course) {
  814. print '<a href="#item-' . $course->id . '" class="list-group-item list-group-item-action h5" style="margin-top: 1rem;" data-toggle="collapse" onclick=\'$(".fa-caret-right, .fa-caret-down", this).toggleClass("fa-caret-right").toggleClass("fa-caret-down");\' > <i class="fa fa-caret-right"></i> ' . $course->fullname . ' </a>' . "\n";
  815. print '<div class="collapse" style="padding: 0 1.5rem;" id="item-' . $course->id . '">' . "\n";
  816. $iassigns = $this->get_iassign_items_by_course($course->id);
  817. if ($iassigns) {
  818. foreach ($iassigns as $iassign) {
  819. print '<a href="#item-' . $course->id . '-' . $iassign->id . '" class="list-group-item list-group-item-action" data-toggle="collapse" onclick=\'$(".fa-caret-right, .fa-caret-down", this).toggleClass("fa-caret-right").toggleClass("fa-caret-down");\'><i class="fa fa-caret-right"></i> ' . $iassign->name . '</a>' . "\n";
  820. print '<div class="collapse" style="padding: 0 1.5rem;" id="item-' . $course->id . '-' . $iassign->id . '">' . "\n";
  821. $statements = $this->get_iassign_statement_by_iassign($iassign->id);
  822. print '<table class="table table-hover">' . "\n";
  823. if ($statements) {
  824. foreach ($statements as $statement) {
  825. print '<tr><td> <div class="form-check"> <input class="form-check-input" name="statements[]" type="checkbox" value="' . $statement->id . '" id="statement-' . $statement->id . '">' . "\n";
  826. print '<label class="form-check-label w-100" for="statement-' . $statement->id . '">' . $statement->name;
  827. print '</label></div></td></tr>' . "\n";
  828. }
  829. }
  830. else {
  831. print '<tr><td>' . get_string('no_activity', 'iassign') . '</td></tr>' . "\n";
  832. }
  833. print '</table></div>' . "\n";
  834. }
  835. }
  836. else {
  837. print '<div class="p-2">' . get_string('import_statement_empty', 'iassign') . '</div>' . "\n";
  838. }
  839. print '</div>' . "\n";
  840. }
  841. print '<div class="text-center"><button type="submit" class="btn btn-primary mt-2">Confirmar</button></div>' . "\n";
  842. print '<div class="text-center text-danger d-none" id="msg-error">' . get_string('import_statement_no_exercise', 'iassign') . '</div>' . "\n";
  843. print "</form>\n";
  844. print $OUTPUT->footer();
  845. } // function post_auto_eval_result()
  846. function get_iassign_items_by_course ($courseid) {
  847. global $DB;
  848. return $DB->get_records("iassign", array("course" => $courseid));
  849. }
  850. function get_iassign_statement_by_iassign ($iassignid) {
  851. global $DB;
  852. return $DB->get_records("iassign_statement", array("iassignid" => $iassignid));
  853. }
  854. function get_courses_enroled_as_teacher ($userid) {
  855. $all_enroled = enrol_get_users_courses($userid);
  856. $return = array();
  857. foreach ($all_enroled as $enroled) {
  858. $context_course = context_course::instance($enroled->id);
  859. if (has_capability('mod/iassign:runautoevaluate', $context_course, $userid, false) ) {
  860. array_push($return, $enroled);
  861. }
  862. }
  863. return $return;
  864. }
  865. //2021/12 ---------------------------------------------- end 1 (to import activities)
  866. /// Manage all submissions in exercises to table iassign_allsubmissions
  867. function all_submissions () {
  868. global $COURSE, $CFG, $USER, $DB, $OUTPUT, $PAGE;
  869. if (!has_capability('mod/iassign:runautoevaluate', $this->context, $USER->id))
  870. exit;
  871. print $OUTPUT->header();
  872. $this->print_main_menu();
  873. $id = $this->cm->id;
  874. $param_aux = "id=" . $id . "&userid_iassign=" . $USER->id;
  875. $enderecoPOST = "" . $CFG->wwwroot . "/mod/iassign/view.php?" . $param_aux;
  876. print '
  877. <div class="modal" id="modalConfirm" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  878. <div class="modal-dialog modal-dialog-centered" role="document">
  879. <div class="modal-content">
  880. <div class="modal-header">
  881. <h5 class="modal-title" id="exampleModalLongTitle">' . get_string('delete', 'iassign') . '</h5>
  882. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  883. <span aria-hidden="true">&times;</span>
  884. </button>
  885. </div>
  886. <div class="modal-body">' . get_string('all_submissions_manager_confirm', 'iassign') . '</div>
  887. <div class="modal-footer">
  888. <button type="button" onclick="confirm_delete()" class="btn btn-primary">'.get_string('confirm', 'iassign').'</button>
  889. <button type="button" class="btn btn-secondary" data-dismiss="modal">'.get_string('cancel', 'iassign').'</button>
  890. </div>
  891. </div>
  892. </div>
  893. </div>'; // "
  894. print '
  895. <script>
  896. var sub_type;
  897. var sub_item;
  898. var sub_el;
  899. var sub_exercise;
  900. function delete_submission (type, item, el, exercise = 0) {
  901. sub_type = type;
  902. sub_item = item;
  903. sub_exercise = exercise;
  904. sub_el = el.closest("tr");
  905. if (!sub_el) sub_el = el.closest("a");
  906. $("#modalConfirm").modal();
  907. }
  908. function confirm_delete () {
  909. var url = "' . $enderecoPOST . '&action=delete_submissions";
  910. var xhttp = new XMLHttpRequest();
  911. xhttp.open("GET", url + "&type=" + sub_type + "&item=" + sub_item + "&exercise=" + sub_exercise, true);
  912. xhttp.send();
  913. $("#modalConfirm").modal("hide");
  914. if ($(sub_el).attr("href"))
  915. $($(sub_el).attr("href")).remove();
  916. if (sub_el) sub_el.remove();
  917. }
  918. function download_submission (type, item, exercise = 0, iassign = 0) {
  919. var url = "' . $enderecoPOST . '&action=download_submissions";
  920. url += "&type=" + type + "&item=" + item + "&exercise=" + exercise + "&iassign=" + iassign;
  921. console.log("download_submission(.): url=" + url);
  922. document.location = url;
  923. }
  924. function open_submission (item) {
  925. var url = "' . $enderecoPOST . '&action=open_individual_submission&item=" + item;
  926. var poupup = window.open(url, "popup", "width=900,height=900");
  927. }
  928. </script>' . chr(13); // ' end of JavaScript code
  929. // Get list of exercises in the instance:
  930. $iassignid = optional_param('iassignid', NULL, PARAM_TEXT);
  931. $exercises_list = $DB->get_records("iassign_statement", array("iassignid" => $iassignid));
  932. print '<h2 class="m-b-2 mt-5">' . get_string('all_submissions_manager', 'iassign') . '';
  933. print '<i onclick="download_submission(\'block\', 0, 0, ' . $iassignid . ')" class="fa fa-download" style="float: right; color: #1fa67a; font-size: 1.5rem; cursor: pointer; margin-left: 1rem; margin-top: .2rem;" aria-hidden="true"></i> </h2>' . "\n";
  934. // Get list of enrolled students:
  935. $params_temp = array('shortname' => 'student');
  936. $role = $DB->get_record_sql("SELECT s.id, s.shortname FROM {role} s WHERE s.shortname = :shortname", $params_temp);
  937. $context = context_course::instance($this->course->id);
  938. $params_temp = array('contextid' => $context->id, 'roleid' => $role->id);
  939. $students_list = $DB->get_records_sql(
  940. "SELECT s.userid, a.firstname, a.lastname FROM {role_assignments} s, {user} a " . " WHERE s.contextid = :contextid AND s.userid = a.id AND s.roleid = :roleid " . " ORDER BY a.firstname ASC,a.lastname ASC", $params_temp);
  941. print '<div>';
  942. foreach ($exercises_list as $exercise) {
  943. $submissions_list = $DB->get_records("iassign_allsubmissions", array("iassign_statementid" => $exercise->id));
  944. $iassign_ilm = $DB->get_record("iassign_ilm", array("id" => $exercise->iassign_ilmid));
  945. print "<a href='#item-" . $exercise->id . "' class='list-group-item list-group-item-action h4' style='margin-top: 1rem;' data-toggle='collapse' onclick=\'$('.fa-caret-right, .fa-caret-down', this).toggleClass('fa-caret-right').toggleClass('fa-caret-down');\' >\n"
  946. . " <i class='fa fa-caret-right'></i>\n" . $exercise->name . ' - ' . $iassign_ilm->name
  947. . "<i class='fa fa-times' onclick='event.stopPropagation(); delete_submission(\'exercise\', " . $exercise->id . ", this)' "
  948. . " style='color:#C62828; cursor: pointer; font-size: 1.5rem; float: right; margin-left: .5rem;' aria-hidden='true'></i>\n"
  949. . "<i onclick='event.stopPropagation(); download_submission(\'exercise\', " . $exercise->id . ")' class='fa fa-download' style='float: right; color: #1fa67a; font-size: 1.2rem; cursor: pointer; margin-left: 1rem; margin-top: .2rem;' aria-hidden='true'></i> \n"
  950. . "<span class='badge badge-primary badge-pill' style='float: right;'>" . count($submissions_list) . "</span></a>\n";
  951. print "<div class='collapse' style='padding: 0 1.5rem;' id='item-" . $exercise->id . "'>\n";
  952. foreach ($students_list as $student) { // total of student submission to this block of activities
  953. $total = 0;
  954. $student_submissions = [];
  955. foreach ($submissions_list as $submission) {
  956. if ($submission->userid == $student->userid) {
  957. $student_submissions[] = $submission;
  958. $total++;
  959. }
  960. }
  961. print "<a href='#item-" . $exercise->id . "-" . $student->userid . "' class='list-group-item list-group-item-action' data-toggle='collapse' onclick=\'$('.fa-caret-right, .fa-caret-down', this).toggleClass('fa-caret-right').toggleClass('fa-caret-down');\'>\n"
  962. . " <i class='fa fa-caret-right'></i> " . $student->firstname . " " . $student->lastname
  963. . "<i class='fa fa-times' onclick='event.stopPropagation(); delete_submission(\'student\', " . $student->userid . ", this, " . $exercise->id . ")' "
  964. . " style='color:#C62828; cursor: pointer; font-size: 1.5rem; float: right; margin-left: .5rem;' aria-hidden='true'></i>\n"
  965. . "<i onclick='event.stopPropagation(); download_submission(\'student\', " . $student->userid . ", " . $exercise->id . ")' class='fa fa-download' "
  966. . " style='float: right; color: #1fa67a; font-size: 1.3rem; cursor: pointer; margin-left: 1rem; margin-top: .1rem;' aria-hidden='true'></i> "
  967. . "<span class='badge badge-primary badge-pill' style='float: right;'>" . $total . "</span></a>\n";
  968. print "<div class='collapse' style='padding-left: 5rem;padding-right: 5rem;' id='item-" . $exercise->id . "-" . $student->userid . "'>\n";
  969. print "<table class='table table-striped table-hover'><thead>
  970. <tr>
  971. <th scope='col'>#</th>
  972. <th scope='col'>" . get_string('all_submissions_manager_date', 'iassign') . "</th>
  973. <th scope='col'>" . get_string('grade_student', 'iassign') . "</th>
  974. <th scope='col'>" . get_string('config_param_actions', 'iassign') . "</th>
  975. </tr>
  976. </thead><tbody>\n";
  977. $i = 1;
  978. foreach ($student_submissions as $individual) {
  979. $date = new DateTime();
  980. $date->setTimestamp($individual->timecreated);
  981. print '<tr>';
  982. print '<td>' . ($i++) . '</td>';
  983. print '<td>' . date_format($date, 'd/m/Y H:i:s') . '</td>';
  984. print '<td>' . $individual->grade . '</td>';
  985. print "<td> <i onclick='open_submission(" . $individual->id . ")' class='fa fa-external-link' style='color:#1177d1; cursor: pointer; font-size: 1.2rem; margin-right: .5rem;' aria-hidden='true'></i>\n";
  986. print " <i onclick='download_submission(\"individual\", " . $individual->id . ", " . $exercise->id . ", 0)' \n";
  987. print " class='fa fa-download' style='color: #1fa67a; font-size: 1.2rem; cursor: pointer; margin-right: .5rem;' aria-hidden='true'></i> \n";
  988. print " <i class='fa fa-times' onclick='delete_submission(\"individual\", " . $individual->id . ", this, " . $exercise->id . ")' \n";
  989. print " style='color:#C62828; cursor: pointer; font-size: 1.5rem;' aria-hidden='true'></i></td>\n";
  990. print " </tr>\n";
  991. }
  992. print "</tbody></table>\n";
  993. //print '<a href="#" class="list-group-item list-group-item-action">Item 1.1.1</a>';
  994. print "</div>\n";
  995. }
  996. print "</div>\n";
  997. }
  998. print "</div>\n";
  999. print $OUTPUT->footer();
  1000. } // function all_submissions()
  1001. /// This method runs activities auto evaluation in teacher area
  1002. function auto_evaluate () {
  1003. global $COURSE, $CFG, $USER, $DB, $OUTPUT, $PAGE;
  1004. if (!has_capability('mod/iassign:runautoevaluate', $USER->context, $USER->id))
  1005. exit;
  1006. $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  1007. $PAGE->navbar->add(get_string('auto_evaluate_title', 'iassign'), $actual_link);
  1008. // Get list of exercises in the instance:
  1009. $iassignid = optional_param('iassignid', NULL, PARAM_TEXT);
  1010. $exercises_list = $DB->get_records("iassign_statement", array("iassignid" => $iassignid));
  1011. // Get list of enrolled students:
  1012. $params_temp = array('shortname' => 'student');
  1013. $role = $DB->get_record_sql("SELECT s.id, s.shortname FROM {role} s WHERE s.shortname = :shortname", $params_temp);
  1014. $context = context_course::instance($this->course->id);
  1015. $params_temp = array('contextid' => $context->id, 'roleid' => $role->id);
  1016. $students_list = $DB->get_records_sql(
  1017. "SELECT s.userid, a.firstname, a.lastname FROM {role_assignments} s, {user} a " .
  1018. " WHERE s.contextid = :contextid AND s.userid = a.id AND s.roleid = :roleid " .
  1019. " ORDER BY a.firstname ASC,a.lastname ASC", $params_temp);
  1020. print $OUTPUT->header();
  1021. $this->print_main_menu();
  1022. print '<div class="mt-5 mb-4"><div>' . get_string('auto_evaluate_select', 'iassign') . '</div>';
  1023. print '<ul style="list-style-type: none; margin: 1em;">';
  1024. foreach ($exercises_list as $exercise) {
  1025. $matches = [];
  1026. preg_match('/[0-9]*\.?[0-9]*/', $exercise->name, $matches, PREG_OFFSET_CAPTURE);
  1027. $iassign_ilm = $DB->get_record("iassign_ilm", array("id" => $exercise->iassign_ilmid));
  1028. if ($iassign_ilm && $iassign_ilm->reevaluate == 1) {
  1029. if ($matches[0][0]) {
  1030. print '<li id="li-item-'.$exercise->id.'"><input data-iditem="'.$matches[0][0].'" data-exercise="'.$exercise->id.'" class="form-check-input" type="checkbox" id="ex_'.$exercise->id.'"><label id="label_ex_'.$exercise->id.'" for="ex_'.$exercise->id.'">' . $exercise->name . '</label></li>';
  1031. } else {
  1032. print '<li id="li-item-'.$exercise->id.'"><input data-iditem="('.$exercise->id.')" data-exercise="'.$exercise->id.'" class="form-check-input" type="checkbox" id="ex_'.$exercise->id.'"><label id="label_ex_'.$exercise->id.'" for="ex_'.$exercise->id.'">(' . $exercise->id . ') - ' . $exercise->name . '</label></li>';
  1033. }
  1034. } else {
  1035. if ($matches[0][0]) {
  1036. print '<li id="li-item-'.$exercise->id.'"> <i onmouseenter="$(\'#li-item-'.$exercise->id.'\').find(\'i\').tooltip(\'show\');" class="fa fa-times without-reevaluator" style="font-size: 1.1em;color: red;margin-bottom: .7em;margin-left: -1.3em;margin-right: .58em;" data-toggle="tooltip" data-original-title="'.get_string('auto_evaluate_missing', 'iassign').'"></i>' . $exercise->name . '</li>';
  1037. } else {
  1038. print '<li id="li-item-'.$exercise->id.'"> <i onmouseenter="$(\'#li-item-'.$exercise->id.'\').find(\'i\').tooltip(\'show\');" class="fa fa-times without-reevaluator" style="font-size: 1.1em;color: red;margin-bottom: .7em;margin-left: -1.3em;margin-right: .58em;" data-toggle="tooltip" data-original-title="'.get_string('auto_evaluate_missing', 'iassign').'"></i>(' . $exercise->id . ') - ' . $exercise->name . '</li>';
  1039. }
  1040. }
  1041. }
  1042. print '</ul>
  1043. <center>
  1044. <button class="btn btn-primary" onclick="start_evaluation()" id="button_start">'.get_string('auto_evaluate_start', 'iassign').'</button>
  1045. <button class="btn btn-danger" disabled="disabled" onclick="stop_evaluation()" id="button_stop">'.get_string('auto_evaluate_stop', 'iassign').'</button>
  1046. <div id="evaluate_message" style="color: red"></div>
  1047. <div id="evaluate_alerts"></div>
  1048. <div>
  1049. <div id="progress_bar" class="progress" style="width: 40em; height: 30px; margin-top: 1em; display: none; text-align: left;">
  1050. <div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" style="width: 1%; height: 100%;" aria-valuenow="0" aria-valuemin="1" aria-valuemax="100">1%</div>
  1051. </div>
  1052. </center>'; // close "
  1053. print ' </div>' . "\n";
  1054. print ' <script>' . "\n";
  1055. print '// #submissions = ' . count($exercises_list) . "\n"; //Debug
  1056. print ' var submissions = [';
  1057. // Get list of student id have submit for each exercise:
  1058. foreach ($exercises_list as $exercise) {
  1059. $iassign_ilm = $DB->get_record("iassign_ilm", array("id" => $exercise->iassign_ilmid));
  1060. print '
  1061. {
  1062. "exerciseid": "' . $exercise->id . '",
  1063. "grade": ' . $exercise->grade . ',
  1064. "ilm": "' . $CFG->wwwroot . '/mod/iassign/' . $iassign_ilm->file_jar . $iassign_ilm->file_class . '",
  1065. "submited": ['; // "
  1066. $params_temp = array('iassign_statementid' => $exercise->id);
  1067. $submited = $DB->get_records_sql( // Get userid and his grade
  1068. "SELECT s.userid, s.grade FROM {iassign_submission} s " .
  1069. " WHERE s.iassign_statementid = :iassign_statementid", $params_temp);
  1070. foreach ($submited as $ss) {
  1071. print($ss->userid . ',');
  1072. }
  1073. print '],';
  1074. print '"allgrades": [';
  1075. foreach ($submited as $ss) {
  1076. print( $ss->grade . ',');
  1077. }
  1078. print ']
  1079. },'; // close "
  1080. } // foreach ($exercises_list as $exercise)
  1081. // Start of JavaScript code to auto-evaluation
  1082. print " ];\n";
  1083. print '
  1084. var total_submissions = 0;
  1085. var selected_exercises;
  1086. var count_steps;
  1087. var value_by_step;
  1088. var stoped = false;
  1089. var time_interval = 5000;
  1090. var var_interval;
  1091. function add_hover_th (id_th, id_li) { //AJAX
  1092. $("#" + id_th).hover(function() {
  1093. $("#" + id_li).css("background-color", "#d9d8d8");
  1094. $(this).css("background-color", "#d9d8d8");
  1095. }, function() {
  1096. $("#" + id_li).css("background-color", "");
  1097. $(this).css("background-color", "");
  1098. });
  1099. }
  1100. function start_evaluation () {
  1101. stoped = false;
  1102. //AJAX
  1103. $("#progress_bar").css("display", "none");
  1104. $("#evaluate_message").text("");
  1105. $("#evaluate_alerts").text("");
  1106. $(".added-col").remove();
  1107. $(".progress-bar").addClass("progress-bar-animated progress-bar-striped").text("0%").css("width", "1%");
  1108. selected_exercises = [];
  1109. var c = 0;
  1110. for (var i = 0; i < exercises.length; i++) {
  1111. if ($("#ex_"+exercises[i]).is(":checked") == true) {
  1112. selected_exercises[c++] = exercises[i];
  1113. }
  1114. }
  1115. if (selected_exercises.length == 0) {
  1116. $("#evaluate_message").text("'.get_string('auto_evaluate_select_msg', 'iassign').'");
  1117. return;
  1118. } else {
  1119. $("input:checkbox").attr("disabled", true); //AJAX
  1120. for (var i = 0; i < selected_exercises.length; i++) {
  1121. var desc = $("#ex_" + selected_exercises[i]).data("iditem");
  1122. //AJAX
  1123. $("#cols-exercises").append( $("<th />", { class : "added-col text-center", text : desc, id : "th-item-"+selected_exercises[i] }));
  1124. $("#table-results tbody tr").each(function(e) {
  1125. var id_t = "std_" + $(this).data("student") + "_ex_" + selected_exercises[i];
  1126. $(this).append( $("<td />", {class : "added-col text-center", id : id_t, text : "", data : {"student": $(this).data("student"), "exercise": selected_exercises[i]} }));
  1127. });
  1128. add_hover_th("th-item-"+selected_exercises[i], "li-item-"+selected_exercises[i]);
  1129. }
  1130. //AJAX
  1131. $("#button_start").prop("disabled", true);
  1132. $("#button_stop").prop("disabled", false);
  1133. $("#progress_bar").css("display", "block");
  1134. }
  1135. total_submissions = selected_exercises.length * students.length;
  1136. count_steps = 0;
  1137. value_by_step = 100/total_submissions;
  1138. actual_exercise = 0;
  1139. actual_student = 0;
  1140. actual_submission = 0;
  1141. get_original_propositions();
  1142. var_interval = setInterval(verifyBreak, time_interval);
  1143. } // function start_evaluation()
  1144. var actual_exercise;
  1145. var actual_student;
  1146. var actual_submission;
  1147. var actual_answer_url; // to provide the current student answer (to "iassing_integration_functios.js" of iLM
  1148. function run_individual_evaluation () {
  1149. if (stoped) return;
  1150. // Verify if student really sent his answer
  1151. var found = false;
  1152. for (var j = 0; j < submissions.length; j++) {
  1153. if (submissions[j].exerciseid == selected_exercises[actual_exercise]) {
  1154. for (var k = 0; k < submissions[j].submited.length; k++) {
  1155. if (students[actual_student] == submissions[j].submited[k]) {
  1156. found = true;
  1157. break;
  1158. }
  1159. }
  1160. break;
  1161. }
  1162. }
  1163. if (found == false) {
  1164. postResultAutoEval();
  1165. } else {
  1166. // If it was sent, search it...
  1167. var final_url = "";
  1168. var student_answer = "' . $CFG->wwwroot . "/mod/iassign/view.php?action=get_student_submission&id=" . $this->cm->id . '" + "&iassign_student=" + students[actual_student] + "&iassign_exercise=" + selected_exercises[actual_exercise];
  1169. for (var j = 0; j < submissions.length; j++) {
  1170. if (submissions[j].exerciseid == selected_exercises[actual_exercise]) {
  1171. final_url = submissions[j].ilm;
  1172. break;
  1173. }
  1174. }
  1175. final_url += "?iLM_PARAM_AssignmentURL=true&iLM_PARAM_Assignment=" + encodeURIComponent(student_answer) + "&iLM_PARAM_TeacherAutoEval=true";
  1176. actual_answer_url = student_answer; // to provide the current student answer (to "iassing_integration_functios.js" of iLM)
  1177. window.frames["ifrm"].location=final_url;
  1178. }
  1179. } // function run_individual_evaluation()
  1180. var break_student = -1;
  1181. var break_exercise = -1;
  1182. function verifyBreak () {
  1183. if (actual_student == break_student && actual_exercise == break_exercise) {
  1184. postResultAutoEval(-6);
  1185. break_student = -1;
  1186. break_exercise = -1;
  1187. return;
  1188. } else {
  1189. break_student = actual_student;
  1190. break_exercise = actual_exercise;
  1191. }
  1192. }
  1193. // This function is called by the iLM to get the student s answer (to compare with re-evaluation)
  1194. function getStudentAnswerURL () {
  1195. return actual_answer_url; // to provide the current student answer (to "iassing_integration_functios.js" of iLM)
  1196. }
  1197. // This function is called by the iLM!!!
  1198. function getAutoEvalOriginalData () {
  1199. return original_propositions[actual_exercise];
  1200. }
  1201. function updateProgressBar () {
  1202. if (stoped) return;
  1203. var temp = (count_steps * value_by_step)|0;
  1204. count_steps++;
  1205. temp = temp > 100 ? 100 : temp;
  1206. $(".progress-bar").css("width", temp + "%").text(temp + "%");
  1207. if (temp >= 100) {
  1208. $(".progress-bar").removeClass("progress-bar-animated progress-bar-striped");
  1209. $("#button_start").prop("disabled", false);
  1210. $("#button_stop").prop("disabled", true);
  1211. $("input:checkbox").attr("disabled", false);
  1212. clearInterval(var_interval);
  1213. }
  1214. }
  1215. // Process the re-evaluated "grade" - DEVELOPER: here, do NOT use apostrophe!
  1216. // The iLM method "getiLMContent()" must, after re-evaluate getting "new_grade", call this with "postResultAutoEval(new_grade)"
  1217. function postResultAutoEval (grade = null) {
  1218. var id_t = "std_" + students[actual_student] + "_ex_" + selected_exercises[actual_exercise];
  1219. if (grade == null) {
  1220. $("#"+id_t).html(\'<i class="fa fa-circle-o text-muted" aria-hidden="true" style="font-size: 2em;" data-toggle="tooltip" data-original-title="'.get_string('not_post', 'iassign').'"></i>\');
  1221. $("#"+id_t).find(\'[data-toggle="tooltip"]\').tooltip();
  1222. } else {
  1223. // Verify if the re-evaluation grade is the of that one registered:
  1224. var actual_value = 0;
  1225. var actual_student_grade = 0;
  1226. for (var k = 0; k < submissions.length; k++) {
  1227. if (submissions[k].exerciseid == selected_exercises[actual_exercise]) {
  1228. actual_value = submissions[k].grade;
  1229. for (var h = 0; h < submissions[k].submited.length; h++) {
  1230. if (submissions[k].submited[h] == students[actual_student]) {
  1231. actual_student_grade = submissions[k].allgrades[h];
  1232. break;
  1233. }
  1234. }
  1235. break;
  1236. }
  1237. }
  1238. // @FeedbackConvention
  1239. if (grade < 0) {
  1240. switch (grade) {
  1241. case -1:
  1242. // Test-cases are empty:
  1243. $("#"+id_t).html(\'<i data-toggle="modal" class="details-button" data-target="#exampleModalCenter"><i class="fa fa-times-circle-o fa-6" aria-hidden="true" style="color: #ef5151; font-size: 2em; cursor: pointer;" data-toggle="tooltip" data-original-title="'.get_string('auto_evaluate_msg_t_empty_cases', 'iassign').'"></i></i>\');
  1244. var mm = actual_exercise;
  1245. $("#"+id_t).find(".details-button").click(function(e) {
  1246. $(".modal-body").html(\'<table class="table"><tr><td style="width: 30%;">'.get_string('exercise', 'iassign').':</td><td>\'+$("#label_ex_"+selected_exercises[mm]).text()+\'</td></tr><tr><td style="width: 30%;">'.get_string('student', 'iassign').'</td><td>\'+$(this).closest("tr").find(".student-name").text()+\'</td></tr><tr><td style="width: 30%;" colspan="2"><b>'.get_string('auto_result', 'iassign').'</b><br>'.get_string('auto_evaluate_msg_empty_cases', 'iassign').'</td></tr></table>\');
  1247. });
  1248. break;
  1249. case -2:
  1250. // Test-cases were manually altered by the student (fraud!):
  1251. $("#"+id_t).html(\'<i data-toggle="modal" class="details-button" data-target="#exampleModalCenter"><i class="fa fa-times-circle-o fa-6" aria-hidden="true" style="color: #ef5151; font-size: 2em; cursor: pointer;" data-toggle="tooltip" data-original-title="'.get_string('auto_evaluate_msg_t_diff_cases', 'iassign').'"></i></i>\');
  1252. var mm = actual_exercise;
  1253. $("#"+id_t).find(".details-button").click(function(e) {
  1254. $(".modal-body").html(\'<table class="table"><tr><td style="width: 30%;">'.get_string('exercise', 'iassign').':</td><td>\'+$("#label_ex_"+selected_exercises[mm]).text()+\'</td></tr><tr><td style="width: 30%;">'.get_string('student', 'iassign').'</td><td>\'+$(this).closest("tr").find(".student-name").text()+"</td></tr><tr><td style=\'width: 30%;\' colspan=\'2\'><b>'.get_string('auto_result', 'iassign').'</b><br>'.get_string('auto_evaluate_msg_diff_cases', 'iassign').'</td></tr></table>");
  1255. });
  1256. //AJAX
  1257. $.ajax({
  1258. dataType: "html",
  1259. url: "'.$CFG->wwwroot.'/mod/iassign/view.php?action=post_auto_eval_result&id='.$this->cm->id.'&iassign_exercise="+selected_exercises[actual_exercise]+"&student="+students[actual_student]+"&grade=0",
  1260. success: function(d) {} });
  1261. break;
  1262. case -5:
  1263. // Execution failed:
  1264. $("#"+id_t).html(\'<i data-toggle="modal" class="details-button" data-target="#exampleModalCenter"><i class="fa fa-times-circle-o fa-6" aria-hidden="true" style="color: #ef5151; font-size: 2em; cursor: pointer;" data-toggle="tooltip" data-original-title="'.get_string('auto_evaluate_msg_t_fail', 'iassign').'"></i></i>\');
  1265. var mm = actual_exercise;
  1266. //AJAX
  1267. $("#"+id_t).find(".details-button").click(function(e) {
  1268. $(".modal-body").html(\'<table class="table"><tr><td style="width: 30%;">'.get_string('exercise', 'iassign').':</td><td>\'+$("#label_ex_"+selected_exercises[mm]).text()+\'</td></tr><tr><td style="width: 30%;">'.get_string('student', 'iassign').'</td><td>\'+$(this).closest("tr").find(".student-name").text()+\'</td></tr><tr><td style="width: 30%;" colspan="2"><b>'.get_string('auto_result', 'iassign').'</b><br>'.get_string('auto_evaluate_msg_fail', 'iassign').'</td></tr></table>\');
  1269. });
  1270. break;
  1271. case -6: // iLM not answering was detected in "verifyBreak()"
  1272. // The iLM are not answering:
  1273. //AJAX
  1274. $("#"+id_t).html(\'<i data-toggle="modal" class="details-button" data-target="#exampleModalCenter"><i class="fa fa-clock-o fa-6" aria-hidden="true" style="color: orange; font-size: 2em; cursor: pointer;" data-toggle="tooltip" data-original-title="'.get_string('auto_evaluate_title_time', 'iassign').'"></i></i>\');
  1275. $("#"+id_t).find(".details-button").click(function(e) {
  1276. $(".modal-body").html(\'<table class="table"><tr><td style="width: 30%;">' . get_string('auto_evaluate_msg_time', 'iassign') . '</td></tr></table>\');
  1277. });
  1278. break;
  1279. }
  1280. //AJAX
  1281. $("#"+id_t).find(\'[data-toggle="tooltip"]\').tooltip();
  1282. } else {
  1283. console.log((grade), (actual_student_grade));
  1284. if ((grade) == (actual_student_grade)) {
  1285. $("#"+id_t).html(\'<i class="fa fa-check-circle-o text-success" aria-hidden="true" style="font-size: 2em;" data-toggle="tooltip" data-original-title="'.get_string('auto_evaluate_ok', 'iassign').'"></i>\');
  1286. } else { // Notas diferem:
  1287. $("#"+id_t).html(\'<i data-toggle="modal" class="details-button" data-target="#exampleModalCenter"><i class="fa fa-exclamation-circle text-warning" aria-hidden="true" style="font-size: 2em; cursor: pointer;" data-toggle="tooltip" data-original-title="'.get_string('auto_evaluate_different_grades', 'iassign').'"></i></i>\');
  1288. var mm = actual_exercise;
  1289. //AJAX
  1290. $("#"+id_t).find(".details-button").click(function(e) {
  1291. $(".modal-body").html(\'<table class="table"><tr><td style="width: 30%;">'.get_string('exercise', 'iassign').':</td><td>\'+$("#label_ex_"+selected_exercises[mm]).text()+\'</td></tr><tr><td style="width: 30%;">'.get_string('student', 'iassign').'</td><td>\'+$(this).closest("tr").find(".student-name").text()+\'</td></tr><tr><td style="width: 30%;">'.get_string('auto_evaluate_grade_registered', 'iassign').'</td><td>\'+(actual_student_grade*grade)+\'</td></tr><tr><td style="width: 30%;"><b>'.get_string('auto_evaluate_grade_new', 'iassign').'</b></td><td>\'+(grade * actual_value)+\'</td></tr><tr><td colspan="2">'.get_string('auto_evaluate_msg_grade_update', 'iassign').'</tr></table>\');
  1292. });
  1293. //AJAX
  1294. // Send to the endpoint the post_auto_eval_result to verify:
  1295. $.ajax({
  1296. dataType: "html",
  1297. url: "' . $CFG->wwwroot . '/mod/iassign/view.php?action=post_auto_eval_result&id=' . $this->cm->id . '&iassign_exercise=" + selected_exercises[actual_exercise] + "&student=" + students[actual_student] + "&grade=" + (grade),
  1298. success: function(d) {}
  1299. });
  1300. }
  1301. //AJAX
  1302. $("#"+id_t).find(\'[data-toggle="tooltip"]\').tooltip();
  1303. }
  1304. }
  1305. updateProgressBar();
  1306. // Does it must continue in the same exercise or stop?
  1307. if (actual_student < students.length) {
  1308. actual_student++;
  1309. run_individual_evaluation();
  1310. } else {
  1311. actual_student = 0;
  1312. // Does it go to next exercise or is it finished?
  1313. if (actual_exercise < selected_exercises.length) {
  1314. actual_exercise++;
  1315. run_individual_evaluation();
  1316. } else {
  1317. //AJAX
  1318. $("#evaluate_alerts").text("' . get_string('auto_evaluate_finished', 'iassign') . '");
  1319. }
  1320. }
  1321. }
  1322. var original_propositions;
  1323. function get_original_propositions () {
  1324. original_propositions = [];
  1325. var count = 0;
  1326. for (var i = 0; i < exercises.length; i++) {
  1327. if ($("#ex_"+exercises[i]).is(":checked") == true) {
  1328. //AJAX
  1329. $.ajax({
  1330. dataType: "html",
  1331. url: "' . $CFG->wwwroot . '/mod/iassign/view.php?action=get_teacher_exercise&id=' . $this->cm->id . '&iassign_exercise="+exercises[i],
  1332. success: function(d) { // variable "d" has the iLM content
  1333. original_propositions[count] = d; // exercise original content
  1334. count++;
  1335. if (count == selected_exercises.length) {
  1336. run_individual_evaluation();
  1337. }
  1338. }
  1339. });
  1340. }
  1341. }
  1342. }
  1343. function stop_evaluation () {
  1344. stoped = true;
  1345. //AJAX
  1346. $("#button_start").prop("disabled", false);
  1347. $("#button_stop").prop("disabled", true);
  1348. $(".progress-bar").removeClass("progress-bar-animated progress-bar-striped");
  1349. $("input:checkbox").attr("disabled", false);
  1350. $("#evaluate_alerts").text("' . get_string('auto_evaluate_stopped', 'iassign') . '");
  1351. clearInterval(var_interval);
  1352. }
  1353. var exercises = [ '; // '
  1354. foreach ($exercises_list as $exercise) {
  1355. print $exercise->id . ',';
  1356. }
  1357. print ' ];';
  1358. print ' var students = [ ';
  1359. foreach ($students_list as $student) {
  1360. print $student->userid . ',';
  1361. }
  1362. print '];';
  1363. print ' var ilms = [';
  1364. foreach ($exercises_list as $exercise) {
  1365. $iassign_ilm = $DB->get_record("iassign_ilm", array("id" => $exercise->iassign_ilmid));
  1366. print "'" . $CFG->wwwroot . "/mod/iassign/" . $iassign_ilm->file_jar . "/" . $iassign_ilm->file_class . "',";
  1367. }
  1368. print ']';
  1369. print '</script>';
  1370. print '
  1371. <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  1372. <div class="modal-dialog modal-dialog-centered" role="document">
  1373. <div class="modal-content">
  1374. <div class="modal-header">
  1375. <h5 class="modal-title" id="exampleModalLongTitle">' . get_string('auto_evaluate_details', 'iassign') . '</h5>
  1376. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  1377. <span aria-hidden="true">&times;</span>
  1378. </button>
  1379. </div>
  1380. <div class="modal-body">
  1381. ...
  1382. </div>
  1383. <div class="modal-footer">
  1384. <button type="button" class="btn btn-secondary" data-dismiss="modal">' . get_string('close', 'iassign') . '</button>
  1385. </div>
  1386. </div>
  1387. </div>
  1388. </div>'; //
  1389. print '<iframe name="ifrm" style="position: absolute; top: -1000px;"></iframe>';
  1390. print '<table id="table-results" class="table table-hover m-t-1">
  1391. <thead>
  1392. <tr id="cols-exercises">
  1393. <th scope="col" style="width: 30rem;">'.get_string('students', 'iassign').'</th>
  1394. </tr>
  1395. </thead>
  1396. <tbody>'; // "
  1397. // End of JavaScript code to auto-evaluation
  1398. foreach ($students_list as $student) {
  1399. $user = $DB->get_record('user', array('id' => $student->userid));
  1400. $avatar = new user_picture($user);
  1401. $avatar->courseid = $COURSE->id;
  1402. $avatar->link = true;
  1403. print ' <tr data-student="' . $student->userid . '">
  1404. <td>' . $OUTPUT->render($avatar) . ' <span class="student-name">' . $user->firstname . ' ' . $user->lastname . '</span></td>
  1405. </tr>' . "\n"; // "
  1406. }
  1407. print " </tbody>
  1408. </table>\n";
  1409. print $OUTPUT->footer();
  1410. } // function auto_evaluate()
  1411. /// This method moves an iAssign activity
  1412. function move_activity () {
  1413. global $USER, $CFG, $DB, $COURSE;
  1414. $iassign_statement_id = optional_param('iassign_current', NULL, PARAM_TEXT);
  1415. $iassignid_destiny = optional_param('iassign_destiny', NULL, PARAM_TEXT);
  1416. $update_entry_new_iassign = new stdClass();
  1417. $update_entry_new_iassign->id = $iassign_statement_id;
  1418. $update_entry_new_iassign->iassignid = $iassignid_destiny;
  1419. // Update the iassign_statement
  1420. $DB->update_record("iassign_statement", $update_entry_new_iassign);
  1421. // Update file context
  1422. $c_i = context_module::instance($this->cm->id);
  1423. $ret = get_coursemodule_from_id("iassign", $c_i->instanceid);
  1424. $course_module_info = $DB->get_record("course_modules", array("course" => $COURSE->id, "module" => $ret->module, "instance" => $iassignid_destiny));
  1425. $path_module = context_course::instance($COURSE->id);
  1426. $context_file = $DB->get_record_sql('SELECT * FROM {context} WHERE instanceid="' . $course_module_info->id . '" AND path like "'.$path_module->path.'%"');
  1427. $course_module = get_coursemodule_from_instance('iassign', $iassignid_destiny); //2021/12
  1428. if ($context_file) {
  1429. $id_context = $context_file->id;
  1430. $iassign_statement = $DB->get_record("iassign_statement", array("id" => $iassign_statement_id));
  1431. $fs = get_file_storage(); // Get reference to all files in Moodle data
  1432. $context = context_module::instance($USER->cm); //2021/12
  1433. //2021/12 $files = $fs->get_area_files(context_module::instance($this->cm->id)->id, 'mod_iassign', 'exercise', $iassign_statement->filesid);
  1434. //TODO 2022: trocar "$iassign_statement->id" por "$iassign_statement->file"???
  1435. $files = $fs->get_area_files($context->id, 'mod_iassign', 'exercise', $iassign_statement->id);
  1436. foreach ($files as $value) {
  1437. if ($value->get_filename() != ".") {
  1438. //2021/12 $last_id = $DB->get_record_sql('SELECT itemid FROM {files} WHERE component="mod_iassign" ORDER BY itemid DESC LIMIT 1')->itemid;
  1439. //2021/12 $newfile = $fs->create_file_from_storedfile(array('contextid' => $id_context, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement_id), $value);
  1440. $fs->delete_area_files(context_module::instance($this->cm->id)->id, 'mod_iassign', 'exercise', $value->get_itemid());
  1441. $destiny_context = context_module::instance($course_module->id);
  1442. // ./lib/filestorage/file_storage.php : "public function create_file_from_string($filerecord, $content)" 1347/2412 on Moodle 2018120302.08
  1443. $newfile = $fs->create_file_from_string(array('contextid' => $destiny_context->id, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement_id, 'filepath' => '/', 'filename' => $value->get_filename()), $value->get_content());
  1444. $updateentry = new stdClass();
  1445. $updateentry->id = $iassign_statement_id;
  1446. // Correct association: {files}.itemid = {iassign_statement}.id and {iassign_statement}.filesid={files}.id
  1447. $updateentry->filesid = $newfile->get_id(); // 2022/10/10: was "$updateentry->filesid = $iassign_statement_id;"
  1448. $updateentry->file = $updateentry->filesid; // keep the old field
  1449. $DB->update_record("iassign_statement", $updateentry);
  1450. //2021/12 $fs->delete_area_files(context_module::instance($this->cm->id)->id, 'mod_iassign', 'exercise', $value->get_itemid());
  1451. break;
  1452. }
  1453. }
  1454. }
  1455. //2021/12 $this->return_home_course('moved_activity');
  1456. $destiny_url = new moodle_url('/mod/iassign/view.php', array('id' => $course_module->id));
  1457. redirect($this->return, get_string('moved_activity', 'iassign').' <a href="'.$destiny_url.'">' . get_string('move_activity_access', 'iassign') . '\'' . $course_module->name . '\'</a>', null, \core\output\notification::NOTIFY_SUCCESS);
  1458. exit;
  1459. } // function move_activity()
  1460. /// This method duplicates an iAssign activity
  1461. // To duplicate one {iassign_statement}: "click" over the activity "duplicate" button
  1462. // with 'http...mod/iassign/view.php?action=duplicate_activity&id=ID1&iassign_current=ID2&iassign_up=IDu&iassign_down=IDd'
  1463. // @param GET ID1 = {course_modules}.id
  1464. // @param GET ID2 = {iassign_statement}.id
  1465. // @param GET IDu = {iassign_statement}.id of the activity above of ID2
  1466. // @param GET IDd = {iassign_statement}.id of the activity bellow of ID2
  1467. // @calledby locallib.php : function action(): "$restricted = array(... 'duplicate_activity' => '$this->duplicate_activity();'," ...
  1468. // @calledby locallib.php : function action(): 'import_activity_confirm' => '$this->import_activity_confirm();'
  1469. // @calledby locallib.php : function import_activity_confirm(): ''
  1470. function duplicate_activity () {
  1471. global $USER, $CFG, $COURSE, $DB, $OUTPUT;
  1472. // Not used: $id = $this->cm->id; // Get {iassign}.id from {course_module}: {iassign_statement}.iassignid = {course_module}.id = $id
  1473. $iassignid = optional_param('iassign_current', NULL, PARAM_TEXT);
  1474. $iassign_up = optional_param('iassign_up', NULL, PARAM_TEXT);
  1475. $iassign_down = optional_param('iassign_down', NULL, PARAM_TEXT);
  1476. $context = context_module::instance($this->cm->id);
  1477. // Not used: $contextuser = context_user::instance($USER->id);
  1478. // Get the the iAssign acitivity to be duplicated
  1479. $original_iassign_statement = $DB->get_record("iassign_statement", array("id" => $iassignid));
  1480. // Make a copy of $original_iassign_statement (after we will change fields 'id, name, author_modified, author_modified_name, timemodified, file, filesid')
  1481. $original_iassign_statement_id = $original_iassign_statement->id;
  1482. $new_iassign_statement = new stdClass(); // build record to the new {iassign_statement}
  1483. foreach ($original_iassign_statement as $field_name => $value) {
  1484. // id name iassignid type_iassign proposition author_name author_modified_name iassign_ilmid file grade timemodified timecreated timeavailable timedue preventlate test special_param1 position visible max_experiment dependency automatic_evaluate show_answer filesid store_all_submissions
  1485. $new_iassign_statement->$field_name = $value; // copy the original value
  1486. }// $new_iassign_statement->id = 0; // will receive the new {iassign_statement}.id - below "$updateentry->id = $id"
  1487. // Get the information about current author, and add this information in author_modified field
  1488. $author = $USER; //x $DB->get_record("user", array("id" => $USER->id)); --- useless since we already have {user}=id; auth; ... username; password; idnumber; firstname; lastname; email; ... description ...
  1489. // Change the field 'name' using the prefix "Copy of ", 'author_modified_name' and 'author_modified' to the current $USER
  1490. $new_iassign_statement->name = get_string('duplicate_iassign_prefix', 'iassign') . ' ' . $original_iassign_statement->name; // new name to {iassign_statement}.name
  1491. $new_iassign_statement->author_modified_name = $author->firstname . ' ' . $author->lastname . ' (' . $author->id . ')'; // use the current user as author
  1492. $new_iassign_statement->author_modified = $new_iassign_statement->author_modified_name ; // original_iassign_statement->author_modified_name;
  1493. $new_iassign_statement->timemodified = time(); // current time
  1494. // Store the activity in the table
  1495. if ($new_iassign_statement_id = $DB->insert_record("iassign_statement", $new_iassign_statement)) {
  1496. $new_iassign_statement->id = $new_iassign_statement_id; // IMPORTANT: update the {iassign_statement}.id to the correct new value!
  1497. // ./lib/filestorage/file_storage.php: public function get_area_files($contextid, $component, $filearea, $itemid = false,...)
  1498. $fs = get_file_storage(); // Get reference to all files in Moodle data (with entry in {files})
  1499. // Try to {files} through get_from_files($ia_id, $ia_file, $ia_filesid, $fs, $contextid, $component, $filearea).
  1500. // Let A={files}.itemid and B={iassign_statement}, they try in this order: A=B.id ; A=B.file ; A=B.filesid
  1501. require_once('files_functions.php'); // Get auxiliary functions to manage {iassign_statement}/{files}
  1502. $answer_array = get_from_files($original_iassign_statement_id, $original_iassign_statement->file, $original_iassign_statement->filesid, $fs, $context->id, 'mod_iassign', 'exercise');
  1503. if ($answer_array==NULL) {
  1504. //x print_error('invalidid', 'iassign'); // $string['invalidid'] = 'Invalid ID';
  1505. // print($OUTPUT->notification(get_string('error_view_ilm', 'iassign'), 'notifyproblem')); --- other option, to present msg a frame (on the same page)
  1506. print_error('error_view_ilm', 'iassign'); // Error: exercise file not found. Inform the teacher of the course.
  1507. exit;
  1508. }
  1509. $f1_obj = $answer_array[0]; // regular 'filename'
  1510. $f2_obj = $answer_array[1]; // directory (filename == '.') - do not used it
  1511. if ($f1_obj!=null) { // duplicate this entry from {files} creating a new file in Moodle Data
  1512. // @seeurl https://docs.moodle.org/dev/File_API : difference between $fs->create_file_from_string($fileinfo, 'hello world');
  1513. // ./lib/filestorage/file_storage.php : "public function create_file_from_string($filerecord, $content)" 1347/2412 on Moodle 2018120302.08
  1514. // ./lib/filestorage/file_storage.php : "public function create_file_from_storedfile($filerecord, $fileorid)" 1051/2412 on Moodle 2018120302.08
  1515. // Version 1: use one preexisting file - this version is better since we are duplicating a previous files
  1516. // ./lib/filestorage/file_storage.php : "public function create_file_from_storedfile($filerecord, $fileorid)" 1051/2412 on Moodle 2018120302.08
  1517. // ./lib/filestorage/file_storage.php: '$new_files' is protected
  1518. $array_config = array('contextid' => $context->id, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $new_iassign_statement_id); // Version 1
  1519. $new_files = $fs->create_file_from_storedfile($array_config, $f1_obj); // create entry in {files} and a new file in Moodle Data // Version 1
  1520. //2 Version 2: create NEW file by contents, add the prefix 'new_' followed to the original name
  1521. //2 $array_param = array('contextid' => $context->id, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $new_iassign_statement_id,
  1522. //2 'userid' => $USER->id, 'author' => $new_iassign_statement->author_modified_name, 'license' => 'allrightsreserved',
  1523. //2 'filepath' => '/', 'filename' => 'new_' . $f1_obj->get_filename());
  1524. //2 // The './lib/file_storage.php!get_pathname_hash(.)' computed path name with: sha1("/".$contextid."/".$component."/".$filearea."/".$itemid.$filepath.$filename);
  1525. //2 $new_files = $fs->create_file_from_string($array_param, $f1_obj->get_content());
  1526. //D echo " * ./lib/file_storage.php!get_pathname_hash(.) = " . file_storage::get_pathname_hash($context->id,'mod_iassign','exercise',$new_iassign_statement_id,'/','new_' . $f1_obj->get_filename()) . "<br/>";
  1527. //$f1_obj->author = $USER->id; $f1_obj->license = 'allrightsreserved';
  1528. // Now update {iassign_statement}.filesid with '$new_files->id': {iassign_statement}.filesid = {files}.id
  1529. //2021/12 $updateentry->filesid = $new_files->get_itemid();
  1530. $new_iassign_statement->filesid = $new_files->get_id(); // points {iassign_statement}.filesid to the new {files}: {iassign_statement}.filesid = {files}.id
  1531. $new_iassign_statement->file = $new_iassign_statement->filesid; // old field
  1532. // Update the duplicated {iassign_statement} with references to the new {files} (and its associated file in Moodle Data)
  1533. if (!$DB->update_record("iassign_statement", $new_iassign_statement)) {
  1534. // print_error('error_update_move_iassign', 'iassign');
  1535. // print($OUTPUT->notification(get_string('error_add_ilm_zip', 'iassign'), 'notifyproblem'));
  1536. print_error('error_file_dupl_1', 'iassign'); // Error: it was not possible to duplicate this file, perhaps it already exists!
  1537. }
  1538. } // if ($f1_obj!=null) - f1_obj in {files}
  1539. else { //DEBUG
  1540. print_error('error_file_not_exists', 'iassign'); // '<font color="red">Error: file not existing!!</font>'
  1541. }
  1542. } // if ($new_iassign_statement_id = $DB->insert_record("iassign_statement", $new_iassign_statement))
  1543. // log event --------------------------------------------------------------------------------------
  1544. iassign_log::add_log('duplicate_iassign_exercise', 'name: ' . $author->firstname, $new_iassign_statement_id, $this->cm->id);
  1545. $this->return_home_course('duplicated_activity');
  1546. exit;
  1547. } // function duplicate_activity()
  1548. // / This method gets the content from comment and register it
  1549. function add_comment () {
  1550. global $USER, $DB;
  1551. $submission_comment = optional_param('submission_comment', NULL, PARAM_TEXT);
  1552. $comment = false;
  1553. if ($submission_comment)
  1554. $comment = $this->write_comment_submission();
  1555. $id_submission = optional_param('iassign_submission_current', NULL, PARAM_TEXT);
  1556. if (!$id_submission) {
  1557. $iassign_submission = $DB->get_record("iassign_submission", array("iassign_statementid" => $this->activity->get_activity()->id, "userid" => $USER->id));
  1558. if ($iassign_submission) {
  1559. $id_submission = $iassign_submission->id;
  1560. }
  1561. }
  1562. //_//REMOVE 2026/04 *****
  1563. //_//DEBUG To debug JavaScript calling, activate this code
  1564. //_//leo REMOVER! xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  1565. //_// function add_comment(): 1776/9737
  1566. //_require_once("ilm_debug/escreva.php");
  1567. //_$userid = $USER->id;
  1568. //_$data = date('Y_m_d_h_s');
  1569. //_$contexto = $this->generateCallTrace(); // debug_backtrace()
  1570. //_$texto = " userid=" . $userid . ", data=" . $data . ", iassign_statementid=" . $iassign_statementid . " - antes de require_capability('mod/iassign:view')\n";
  1571. //_$texto .= " Entrou no 'add_comment()\n'. this->context->id=" . $this->context->id;
  1572. //_//$resp = writeContent("", "", "comentario_view_leo.txt", $texto);
  1573. //_$resp = writeContent("", "", "comentario_view_felipe.txt", $contexto . "\n" . $texto);
  1574. //_//leo REMOVER! xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  1575. //_//REMOVE 2026/04 *****
  1576. $content = $this->search_comment_submission($id_submission);
  1577. ob_end_clean();
  1578. header("Connection: close\r\n");
  1579. header("Content-Encoding: none\r\n");
  1580. header('Content-Type: text/html');
  1581. ob_start();
  1582. print $this->search_comment_submission($id_submission);
  1583. $size = ob_get_length();
  1584. header("Content-Length: $size");
  1585. ob_end_flush();
  1586. flush();
  1587. ob_end_clean();
  1588. // Send email to users:
  1589. $iassign_submission = $DB->get_record("iassign_submission", array("id" => $id_submission));
  1590. if ($comment) // has comment
  1591. $this->send_alert_new_comment($submission_comment, $iassign_submission->userid);
  1592. else $this->send_alert_new_comment("Error: comment not sent!", $iassign_submission->userid);
  1593. } // function add_comment()
  1594. /// This method alerts envolved to new comment sent
  1595. function send_alert_new_comment ($comment, $student_id = -1) {
  1596. global $USER, $DB, $COURSE, $SITE;
  1597. $subject = $COURSE->shortname . ": " . get_string('new_comment_mail_subject', 'iassign') . " ";
  1598. $iassign_ilm = $DB->get_record("iassign_ilm", array("id" => $this->activity->get_activity()->iassign_ilmid)); //leo 2021/12
  1599. $subject .= $iassign_ilm->name . ' ' . get_string('new_comment_mail_at', 'iassign') . ' ' . get_string("iassign", "iassign") . '/Moodle'; //leo 2021/12
  1600. // $url_curso; $url_submissao;
  1601. $text = "<p style='margin-top: 1rem;'>\n";
  1602. $text .= "<a href=\"" . new moodle_url('/course/view.php', array('id' => $COURSE->id)) . "\">" . $COURSE->shortname . "</a> " . " &#8811; \n";
  1603. $modinfo = get_fast_modinfo($COURSE);
  1604. for ($i = 0; $i < count($modinfo->sections); $i++) {
  1605. if ($modinfo->get_section_info($i) && $modinfo->get_section_info($i)->id == $this->cm->section) {
  1606. $url_curso = new moodle_url('/course/view.php', array('id' => $COURSE->id));
  1607. $text .= "<a href=\"" . $url_curso . "#section-" . $modinfo->get_section_info($i)->section . "\">" . get_section_name($COURSE, $modinfo->get_section_info($i)->section) . "</a> &#8811; \n";
  1608. break;
  1609. }
  1610. }
  1611. $iassign = $DB->get_record('iassign', array('id' => $this->activity->get_activity()->iassignid));
  1612. $text .= "<a href=\"" . new moodle_url('/mod/iassign/view.php', array('id' => $this->cm->id)) . "\">" . $iassign->name . "</a> &#8811; \n";
  1613. $url_submissao = new moodle_url('/mod/iassign/view.php',
  1614. array(
  1615. 'id' => $this->cm->id,
  1616. 'action' => 'viewsubmission',
  1617. 'iassign_current' => $this->activity->get_activity()->id,
  1618. 'userid_iassign' => $USER->id));
  1619. $text .= "<a href=\"" . $url_submissao . "\">" . $this->activity->get_activity()->name . "</a></p>\n";
  1620. $text .= "<div style='margin-left: .5rem;'>\n";
  1621. $text .= get_string('new_comment_mail_course', 'iassign') . ": <a href=\"" . $url_curso . "\">" . $COURSE->fullname . "</a>\n";
  1622. $text .= "</div><div style='margin-top: .3rem; margin-left: .5rem;'>\n";
  1623. $text .= get_string('exercise', 'iassign') . ": <a href=\"" . $url_submissao . "\">" . $this->activity->get_activity()->name . "</a>\n";
  1624. $text .= "</div><div style='margin-top: .3rem; margin-left: .5rem;'>\n";
  1625. $text .= get_string('new_comment_mail_sent_by', 'iassign') . ': ' . "<a href=\"" .
  1626. new moodle_url('/user/view.php', array('id' => $USER->id, 'course' => $COURSE->id)) . "\">" . $USER->firstname . " " . $USER->lastname . "</a>\n";
  1627. $text .= " - " . userdate(time());
  1628. $text .= "</div><div style='margin: 2rem;'>\n";
  1629. $text .= $comment;
  1630. $text .= "</div><div>\n";
  1631. $text .= "<a href=\"" . $url_submissao . "#comments\">" . get_string('new_comment_mail_open_exercise', 'iassign') . "</a> | \n";
  1632. $text .= "<a href=\"" . $url_submissao . "#comments\">" . get_string('new_comment_mail_answer', 'iassign') . "</a>\n";
  1633. $text .= "</div>\n";
  1634. // Who has to receive?
  1635. $teachers = get_users_by_capability($this->context, 'mod/iassign:evaluateiassign');
  1636. $userfrom = \core_user::get_noreply_user();
  1637. if (has_capability('mod/iassign:evaluateiassign', $this->context, $USER->id)) { // a teacher has sent the comment:
  1638. // other teachers receive also: (excepts the sender)
  1639. foreach ($teachers as $teacher) {
  1640. if ($teacher->id != $USER->id) {
  1641. email_to_user(
  1642. $teacher,
  1643. $USER->firstname . " " . $USER->lastname . " (via " . $SITE->shortname . ")",
  1644. $subject,
  1645. "",
  1646. $text,
  1647. '',
  1648. '',
  1649. false,
  1650. $userfrom->email,
  1651. get_string('comment_not_answer_this', 'iassign') // 'Do not answer this message'
  1652. );
  1653. $message = new \core\message\message();
  1654. $message->component = 'mod_iassign';
  1655. $message->name = 'message';
  1656. $message->userfrom = core_user::get_noreply_user();
  1657. $message->userto = $teacher;
  1658. $message->subject = $subject;
  1659. $message->fullmessage = $text;
  1660. $message->fullmessageformat = FORMAT_MARKDOWN;
  1661. $message->fullmessagehtml = $text;
  1662. $message->smallmessage = $subject;
  1663. $message->notification = 1;
  1664. $message->contexturl = $url_submissao . '#comments';
  1665. $message->contexturlname = $this->activity->get_activity()->name;
  1666. $messageid = message_send($message);
  1667. }
  1668. } // foreach ($teachers as $teacher)
  1669. // and student:
  1670. $student = $DB->get_record('user', array('id' => $student_id));
  1671. $formated_text = str_replace("viewsubmission", "view", $text);
  1672. email_to_user(
  1673. $student,
  1674. $USER->firstname . " " . $USER->lastname . " (via " . $SITE->shortname . ")",
  1675. $subject,
  1676. "",
  1677. $formated_text,
  1678. '',
  1679. '',
  1680. false,
  1681. $userfrom->email,
  1682. get_string('comment_not_answer_this', 'iassign') // 'Do not answer this message'
  1683. );
  1684. $url_student = new moodle_url('/mod/iassign/view.php',
  1685. array(
  1686. 'id' => $this->cm->id,
  1687. 'action' => 'view',
  1688. 'iassign_current' => $this->activity->get_activity()->id));
  1689. $message = new \core\message\message();
  1690. $message->component = 'mod_iassign';
  1691. $message->name = 'message';
  1692. $message->userfrom = core_user::get_noreply_user();
  1693. $message->userto = $student;
  1694. $message->subject = $subject;
  1695. $message->fullmessage = $formated_text;
  1696. $message->fullmessageformat = FORMAT_MARKDOWN;
  1697. $message->fullmessagehtml = $formated_text;
  1698. $message->smallmessage = $subject;
  1699. $message->notification = 1;
  1700. $message->contexturl = $url_student . '#comments';
  1701. $message->contexturlname = $this->activity->get_activity()->name;
  1702. $messageid = message_send($message);
  1703. } // if (has_capability('mod/iassign:evaluateiassign', $this->context, $USER->id))
  1704. else { // a student has sent the comment:
  1705. foreach ($teachers as $teacher) {
  1706. email_to_user(
  1707. $teacher,
  1708. $USER->firstname . " " . $USER->lastname . " (via " . $SITE->shortname . ")",
  1709. $subject,
  1710. "",
  1711. $text,
  1712. '',
  1713. '',
  1714. false,
  1715. $userfrom->email,
  1716. get_string('comment_not_answer_this', 'iassign') // 'Do not answer this message'
  1717. );
  1718. $message = new \core\message\message();
  1719. $message->component = 'mod_iassign';
  1720. $message->name = 'message';
  1721. $message->userfrom = core_user::get_noreply_user();
  1722. $message->userto = $teacher;
  1723. $message->subject = $subject;
  1724. $message->fullmessage = $text;
  1725. $message->fullmessageformat = FORMAT_MARKDOWN;
  1726. $message->fullmessagehtml = $text;
  1727. $message->smallmessage = $subject;
  1728. $message->notification = 1;
  1729. $message->contexturl = $url_submissao . '#comments';
  1730. $message->contexturlname = $this->activity->get_activity()->name;
  1731. $messageid = message_send($message);
  1732. } // foreach ($teachers as $teacher)
  1733. }
  1734. } // function send_alert_new_comment($comment, $student_id = -1)
  1735. /// This method gets the content from the iLM and register it
  1736. // It could be the "exercise template" (teacher) or an answer (student)
  1737. // If student answer: the answer must be registered in table {iassign_submission} (perhaps also in {submission_comment}, {iassign_allsubmissions})
  1738. // In 2022/10/05 was fixed to allow the registration of new solutions send by the student (even when the grade is reduced)
  1739. function get_answer () {
  1740. global $USER, $CFG, $DB, $OUTPUT;
  1741. $id = $this->cm->id;
  1742. $submission_comment = optional_param('submission_comment', NULL, PARAM_TEXT);
  1743. $submission_ajax = optional_param('iLM_ajax', NULL, PARAM_TEXT);
  1744. if ($submission_ajax == 1) ob_start(); // submission through JavaScript => do not present echoes
  1745. $comment = false;
  1746. if ($submission_comment)
  1747. $comment = $this->write_comment_submission();
  1748. // receives data of iLM using the current activity
  1749. $iassign_ilm = $DB->get_record("iassign_ilm", array("id" => $this->activity->get_activity()->iassign_ilmid)); // has automatic evaluation?
  1750. $iassign = $DB->get_record("iassign", array("id" => $this->activity->get_activity()->iassignid)); // activity
  1751. // Receives data of submission of current activity: the field 'activity' of class 'activity' has all fields of {iassign_statement}
  1752. $iassign_submission = $DB->get_record("iassign_submission", array("iassign_statementid" => $this->activity->get_activity()->id, "userid" => $this->userid_iassign)); // data about student solution
  1753. // receives post and get
  1754. $iLM_PARAM_ActivityEvaluation = optional_param('iLM_PARAM_ActivityEvaluation', 0, PARAM_INT); // 1 - activity evaluated as correct ; 0 - activity evaluated as incorrect
  1755. $iLM_PARAM_RealGrade = optional_param('iLM_PARAM_RealGrade', 0, PARAM_FLOAT); // the grade send by the iLM automatic evaluator (if it has one): a value between 0 and 1
  1756. //2016/02/16: IMPORTANTE trocar formatador para "nao formatado", pois esta destruindo o conteudo do arquivo
  1757. $iLM_PARAM_ArchiveContent = optional_param('iLM_PARAM_ArchiveContent', NULL, PARAM_RAW); // answer file (ATTENTION: do not change format, use RAW in order to ensure the correct content)
  1758. $MA_POST_Info = optional_param('MA_POST_Info', NULL, PARAM_FORMAT);
  1759. $MA_POST_SystemData = optional_param('MA_POST_SystemData', NULL, PARAM_FORMAT);
  1760. //TODO: With $return_get_answer == 1 implies that the new submission will over-write the old one, even with a smaller grade
  1761. //TODO: To be implemented: we can use JS functions 'overwrite()' and 'nooverwrite()' (bellow) to the student to choose over-write or not
  1762. // $return_get_answer = optional_param('return_get_answer', 0, PARAM_INT);
  1763. $return_get_answer = optional_param('return_get_answer', 1, PARAM_INT); // if 1 then register new submission even if the grade is reduced from 1.0 to any lower value
  1764. $msg = '';
  1765. // If the activity is defined to store all submissions
  1766. if ($this->activity->get_activity()->store_all_submissions == 1) {
  1767. $activity_scale = $this->activity->get_activity()->grade; // get {iassign_statement}.grade (to be used to weighted the student grade from [0,1])
  1768. $newentry = new stdClass();
  1769. $newentry->iassign_statementid = $this->activity->get_activity()->id;
  1770. $newentry->userid = $this->userid_iassign;
  1771. $newentry->timecreated = time();
  1772. //2023/05/25 $newentry->grade = round($iLM_PARAM_RealGrade, 2);
  1773. $newentry->grade = round($iLM_PARAM_RealGrade * $activity_scale, 2); // iLM must return through 'getEvaluation()' a value between 0 and 1
  1774. $newentry->answer = $iLM_PARAM_ArchiveContent;
  1775. $DB->insert_record("iassign_allsubmissions", $newentry);
  1776. }
  1777. // Feedback
  1778. // Activity status: 0 => not post 1; => post; 2 => evaluated as incorrect; 3 => evaluated as correct
  1779. $str_action = "view"; // repeat
  1780. if (strtolower($iassign_ilm->name) == "igeom") {
  1781. }
  1782. $title = get_string('evaluate_iassign', 'iassign');
  1783. print $OUTPUT->header();
  1784. print $OUTPUT->box_start();
  1785. // Action = { view ; repeat ; viewsubmission }
  1786. // * 'view' => it is impossible to re-send answer (this is correct with iGeom because its model to ensure security - do not allow the learner's access to the "answer model")
  1787. // * 'repeat' => student explicitly requested to redo the activiy or enter in the last submission under iLM other then iGeom
  1788. // * 'viewsubmission' => teacher/non editing teacher seeing the learner's activity
  1789. $return = $CFG->wwwroot . "/mod/iassign/view.php?action=view&id=" . $id . "&iassign_submission_current=" . $this->iassign_submission_current . "&userid_iassign=" . $this->userid_iassign . "&iassign_current=" . $this->activity->get_activity()->id;
  1790. $return_last = " <a href='" . $return . "'>" . iassign_icons::insert('return_home') . ' ' . get_string('return_iassign', 'iassign') . "</a>\n";
  1791. $link_return = " <a href='" . $this->return . "'>" . iassign_icons::insert('home') . ' ' . get_string('activities_page', 'iassign') . "</a>\n";
  1792. print "<table width=100% >\n";
  1793. if ($iLM_PARAM_ArchiveContent == - 1 || empty($iLM_PARAM_ArchiveContent)) { // if ($iLM_PARAM_ActivityEvaluation == -1)
  1794. //TODO Does it need review?
  1795. $this->write_solution = 0; // necessary in order to take note in Moodle 'grade' system
  1796. // empty_answer_post = No solution was posted.
  1797. if ($comment)
  1798. print '<tr><td colspan=2><br>' . get_string('empty_answer_post', 'iassign') . '</br>' . get_string('confirm_add_comment', 'iassign') . '</td>' . "\n";
  1799. else
  1800. print '<tr><td colspan=2><br>' . get_string('empty_answer_post', 'iassign') . '</td>' . "\n";
  1801. print '<tr><td width=40% align=right>' . $return_last . ' ' . $link_return . '</td></tr>' . "\n";
  1802. }
  1803. else { // if ($iLM_PARAM_ArchiveContent == - 1 || empty($iLM_PARAM_ArchiveContent))
  1804. $this->write_solution = 1; // indicate that this answer must overwrite the old one
  1805. if ($iassign_ilm->evaluate == 1 && $this->activity->get_activity()->automatic_evaluate == 1) { // iLM with automatic evaluator
  1806. //TODO Do compare with previous soluction to prevent update to one answer with a smaller grade?
  1807. // The student grade must be a value between 0 and 1: 0 => complete wrong ; 1 => 100% correct!
  1808. if (intval($iLM_PARAM_ActivityEvaluation) == 1) {
  1809. // if (true) { // after 2022/10/04: accept any new submission (overwrite, even when the grade is reduced!)
  1810. // intval($iLM_PARAM_ActivityEvaluation) == 1 => correct answer!!
  1811. //TODO Perhaps, must we use 'write_solution=0'?
  1812. // $this->write_solution = 1; // indicate that this answer must overwrite the old one
  1813. $status = 3;
  1814. $grade_student = $iLM_PARAM_RealGrade; // evaluated as correct solution submitted is assigned the note pattern of activity
  1815. $msg = '<tr><td colspan=2>' . iassign_icons::insert('feedback_correct') . '<br>' . get_string('get_answer_correct', 'iassign') . '</td>';
  1816. // log record
  1817. $info = $iassign->name . "&nbsp;-&nbsp;" . $this->activity->get_activity()->name . "&nbsp;-&nbsp;" . get_string('feedback_correct', 'iassign') . "&nbsp;-&nbsp;" . get_string('grade_iassign', 'iassign') . ":" . $grade_student;
  1818. // Trigger module viewed event.
  1819. $event = \mod_iassign\event\submission_created::create(array(
  1820. 'objectid' => $this->iassign->id,
  1821. 'context' => $this->context,
  1822. 'other' => $info
  1823. ));
  1824. $event->add_record_snapshot('course', $this->course);
  1825. $event->trigger();
  1826. }
  1827. else { // else if (intval($iLM_PARAM_ActivityEvaluation) == 1)
  1828. // Wrong answer...: get_answer_incorrect
  1829. $status = 2;
  1830. $grade_student = $iLM_PARAM_RealGrade; // evaluated as incorrect solution
  1831. $msg = '<tr><td colspan=2>' . iassign_icons::insert('feedback_incorrect') . '<br>' . get_string('get_answer_incorrect', 'iassign') . '</td>' . "\n";
  1832. // log record
  1833. $info = $iassign->name . " - " . $this->activity->get_activity()->name . " - " . get_string('feedback_incorrect', 'iassign') .
  1834. " - " . get_string('grade_iassign', 'iassign') . $grade_student;
  1835. // Trigger module viewed event.
  1836. $event = \mod_iassign\event\submission_created::create(array(
  1837. 'objectid' => $this->iassign->id,
  1838. 'context' => $this->context,
  1839. 'other' => $info
  1840. ));
  1841. $event->add_record_snapshot('course', $this->course);
  1842. $event->trigger();
  1843. } // else if (intval($iLM_PARAM_ActivityEvaluation) == 1)
  1844. // Presents to the learner the result of the automatic evaluate?
  1845. if ($this->activity->get_activity()->show_answer == 0) { // no...
  1846. print '<tr><td width=60% ><strong>' . iassign_icons::insert('post') . get_string('get_answer', 'iassign') . '</strong></td>';
  1847. print '<tr><td width=40% align=right>' . $return_last . '&nbsp;' . $link_return . '</td></tr>';
  1848. print '<tr>';
  1849. // log record
  1850. $info = $iassign->name . "&nbsp;-&nbsp;" . $this->activity->get_activity()->name . "&nbsp;-&nbsp;" . get_string('get_answer', 'iassign');
  1851. // Trigger module viewed event.
  1852. $event = \mod_iassign\event\submission_created::create(array(
  1853. 'objectid' => $this->iassign->id,
  1854. 'context' => $this->context,
  1855. 'other' => $info
  1856. ));
  1857. $event->add_record_snapshot('course', $this->course);
  1858. $event->trigger();
  1859. } else { // yes!!!
  1860. print '<tr><td width=60% ><strong>' . get_string('auto_result', 'iassign') . '</strong></td>';
  1861. print '<td width=40% align=right>' . $return_last . '&nbsp;' . $link_return . '</td></tr>';
  1862. print '<tr>';
  1863. print $msg;
  1864. }
  1865. }
  1866. else { // if ($iassign_ilm->evaluate == 1 && $this->activity->get_activity()->automatic_evaluate == 1)
  1867. $status = 1;
  1868. $grade_student = $iLM_PARAM_RealGrade; // iLM not have automatic evaluator
  1869. print '<tr><td colspan=2>' . iassign_icons::insert('post') . get_string('get_answer_post', 'iassign') . '</td>';
  1870. print '<tr><td width=40% align=right>' . $return_last . '&nbsp;' . $link_return . '</td></tr>';
  1871. print '<tr>';
  1872. // log record
  1873. $info = $iassign->name . "&nbsp;-&nbsp;" . $this->activity->get_activity()->name . "&nbsp;-&nbsp;" . get_string('get_answer_post', 'iassign');
  1874. $event = \mod_iassign\event\submission_created::create(array(
  1875. 'objectid' => $this->iassign->id,
  1876. 'context' => $this->context,
  1877. 'other' => $info
  1878. ));
  1879. $event->add_record_snapshot('course', $this->course);
  1880. $event->trigger();
  1881. } // if ($iassign_ilm->evaluate == 1)
  1882. } // else of if ($iLM_PARAM_ArchiveContent == - 1 || empty($iLM_PARAM_ArchiveContent))
  1883. print "</tr></table>\n";
  1884. print $OUTPUT->box_end();
  1885. // Insert new entry to {iassign_statement} or update it
  1886. if ($this->write_solution == 1) {
  1887. $timenow = time();
  1888. // New record to table {iassign_submission}
  1889. if (!$iassign_submission) {
  1890. $activity_scale = $this->activity->get_activity()->grade; // get {iassign_statement}.grade (to be used to weighted the student grade from [0,1])
  1891. $newentry = new stdClass();
  1892. $newentry->userid = $this->userid_iassign;
  1893. // $newentry->userid = $USER->id;
  1894. $newentry->iassign_statementid = $this->activity->get_activity()->id;
  1895. $newentry->timecreated = $timenow;
  1896. $newentry->timemodified = $timenow;
  1897. $newentry->answer = $iLM_PARAM_ArchiveContent;
  1898. //2023/05/25 $newentry->grade = round($grade_student, 2);
  1899. $newentry->grade = round($iLM_PARAM_RealGrade * $activity_scale, 2); // iLM must return through 'getEvaluation()' a value between 0 and 1
  1900. $newentry->status = $status;
  1901. $newentry->experiment = 1;
  1902. if (!$newentry->id = $DB->insert_record("iassign_submission", $newentry)) {
  1903. print_error('error_insert', 'iassign');
  1904. } else {
  1905. // Trigger module viewed event.
  1906. $event = \mod_iassign\event\submission_created::create(array(
  1907. 'objectid' => $this->iassign->id,
  1908. 'context' => $this->context
  1909. ));
  1910. $event->add_record_snapshot('course', $this->course);
  1911. $event->trigger();
  1912. $this->update_grade_student($newentry->userid, $newentry->iassign_statementid, $this->iassign->id);
  1913. }
  1914. } elseif ($iassign_submission->status != 3) { // last submission is not correct
  1915. $activity_scale = $this->activity->get_activity()->grade; // get {iassign_statement}.grade (to be used to weighted the student grade from [0,1])
  1916. $newentry = new stdClass();
  1917. $newentry->id = $iassign_submission->id;
  1918. $newentry->iassign_statementid = $iassign_submission->iassign_statementid;
  1919. $newentry->userid = $iassign_submission->userid;
  1920. $newentry->timecreated = $iassign_submission->timecreated;
  1921. $newentry->timemodified = $timenow;
  1922. $newentry->answer = $iLM_PARAM_ArchiveContent;
  1923. //2023/05/25 $newentry->grade = round($grade_student, 2);
  1924. $newentry->grade = round($iLM_PARAM_RealGrade * $activity_scale, 2); // iLM must return through 'getEvaluation()' a value between 0 and 1
  1925. $newentry->status = $status;
  1926. $newentry->experiment = $iassign_submission->experiment + 1;
  1927. if (!$DB->update_record("iassign_submission", $newentry)) {
  1928. print_error('error_update', 'iassign');
  1929. //D depurar...
  1930. //D $stringAux = "ia.class.php: ".$iLM_PARAM_ArchiveContent."<br/> ".utf8_encode($iLM_PARAM_ArchiveContent)."<br/>".utf8_encode(utf8_encode($iLM_PARAM_ArchiveContent))."<br/>";
  1931. //D $fp = fopen("teste1.txt","w");
  1932. //D fwrite($fp,$stringAux);
  1933. } else {
  1934. // Trigger module viewed event.
  1935. $event = \mod_iassign\event\submission_updated::create(array(
  1936. 'objectid' => $this->iassign->id,
  1937. 'context' => $this->context
  1938. ));
  1939. $event->add_record_snapshot('course', $this->course);
  1940. $this->update_grade_student($newentry->userid, $newentry->iassign_statementid, $this->iassign->id);
  1941. }
  1942. } else { // elseif ($iassign_submission->status != 3)
  1943. // last submission is correct
  1944. if ($return_get_answer == 1) { // register the new solution, even if it is wrong!
  1945. $activity_scale = $this->activity->get_activity()->grade; // get {iassign_statement}.grade (to be used to weighted the student grade from [0,1])
  1946. $newentry = new stdClass();
  1947. $newentry->id = $iassign_submission->id;
  1948. $newentry->iassign_statementid = $iassign_submission->iassign_statementid;
  1949. $newentry->userid = $iassign_submission->userid;
  1950. $newentry->timecreated = $iassign_submission->timecreated;
  1951. $newentry->timemodified = $timenow;
  1952. $newentry->answer = $iLM_PARAM_ArchiveContent;
  1953. //2023/05/25 $newentry->grade = round($grade_student, 2);
  1954. $newentry->grade = round($iLM_PARAM_RealGrade * $activity_scale, 2); // iLM must return through 'getEvaluation()' a value between 0 and 1
  1955. $newentry->status = $status;
  1956. $newentry->experiment = $iassign_submission->experiment + 1;
  1957. if (!$DB->update_record("iassign_submission", $newentry))
  1958. print_error('error_update', 'iassign');
  1959. else {
  1960. $event = \mod_iassign\event\submission_updated::create(array(
  1961. 'objectid' => $this->iassign->id,
  1962. 'context' => $this->context
  1963. ));
  1964. $event->add_record_snapshot('course', $this->course);
  1965. $event->trigger();
  1966. $this->update_grade_student($newentry->userid, $newentry->iassign_statementid, $this->iassign->id);
  1967. print $OUTPUT->box_start();
  1968. print "<p>" . get_string('iassign_update', 'iassign') . "</p>";
  1969. print $OUTPUT->box_end();
  1970. }
  1971. } elseif ($return_get_answer == 2) {
  1972. print $OUTPUT->box_start();
  1973. print "<p>" . get_string('iassign_cancel', 'iassign') . "</p>";
  1974. print $OUTPUT->box_end();
  1975. } else {
  1976. print $OUTPUT->box_start();
  1977. print "
  1978. <script type='text/javascript'>
  1979. //<![CDATA[
  1980. function overwrite () {
  1981. document.formEnvio.return_get_answer.value = 1;
  1982. document.formEnvio.submit();
  1983. }
  1984. function nooverwrite () {
  1985. document.formEnvio.return_get_answer.value = 2;
  1986. document.formEnvio.submit();
  1987. }
  1988. //]]>
  1989. </script>";
  1990. $param_aux = "action=overwrite&iassign_submission_current=" . $iassign_submission->id . "&id=" . $id . "&iassign_current=" . $this->activity->get_activity()->id . "&write_solution=" . $this->write_solution . "&userid_iassign=" . $USER->id;
  1991. $get_answer_overwrite = $CFG->wwwroot . "/mod/iassign/view.php?" . $param_aux;
  1992. print "<form name='formEnvio' method='post' action='" . $get_answer_overwrite . "' enctype='multipart/form-data'>";
  1993. print "<p>" . get_string('last_iassign_correct', 'iassign') . "</p>";
  1994. print "<p>" . get_string('update_iassign', 'iassign') . "</p>";
  1995. print "<input type='hidden' name='iLM_PARAM_ArchiveContent' value='" . $iLM_PARAM_ArchiveContent . "'/>
  1996. <input type='hidden' name='iLM_PARAM_ActivityEvaluation' value='$iLM_PARAM_ActivityEvaluation'/>
  1997. <input type='hidden' name='MA_POST_Info' value='" . $MA_POST_Info . "'/>
  1998. <input type='hidden' name='MA_POST_SystemData' value='" . $MA_POST_SystemData . "'/>
  1999. <input type='hidden' name='return_get_answer'/> ";
  2000. print "<input type=button value='" . get_string('yes', 'iassign') . "' onClick = 'overwrite()'
  2001. title='" . get_string('message_update_iassign', 'iassign') . "'/>\n";
  2002. print "<input type=button value='" . get_string('no', 'iassign') . "' onClick = 'nooverwrite()'
  2003. title='" . get_string('message_no_update_iassign', 'iassign') . "'/>\n";
  2004. print " </form>\n";
  2005. print $OUTPUT->box_end();
  2006. }
  2007. }
  2008. } // if ($this->write_solution == 1)
  2009. print $OUTPUT->footer();
  2010. if ($submission_ajax == 1) {
  2011. ob_end_clean();
  2012. print 'OK';
  2013. }
  2014. die();
  2015. } // function get_answer()
  2016. /// Export in file the answer of student.
  2017. function export_file_answer () {
  2018. global $DB;
  2019. $iassign_submission_id = optional_param('iassign_submission_id', NULL, PARAM_INT);
  2020. $iassign_submission = $DB->get_record("iassign_submission", array("id" => $iassign_submission_id));
  2021. $iassign_statement = $DB->get_record("iassign_statement", array("id" => $iassign_submission->iassign_statementid));
  2022. $name = iassign_utils::format_filename(strip_tags($iassign_statement->name));
  2023. $iassign_ilm = $DB->get_record("iassign_ilm", array("id" => $iassign_statement->iassign_ilmid));
  2024. $extensions = explode(",", $iassign_ilm->extension);
  2025. $iassign_user = $DB->get_record("user", array("id" => $iassign_submission->userid));
  2026. $username = iassign_utils::format_filename($iassign_user->firstname . ' ' . $iassign_user->lastname);
  2027. $name_answer = $username . '-' . $name . '-' . userdate($iassign_submission->timemodified, '%Y%m%d-%H%M') . '.' . $extensions[0];
  2028. header("Pragma: public");
  2029. header("Expires: 0");
  2030. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  2031. header("Cache-Control: private", false);
  2032. header("Content-Type: document/unknown");
  2033. header("Content-Disposition: attachment; filename=\"" . $name_answer . "\";");
  2034. set_time_limit(0);
  2035. print($iassign_submission->answer);
  2036. exit;
  2037. } // function export_file_answer()
  2038. /// Prepare data to static exportation (with course name, iAsssing block name, and exercises names
  2039. // @calledby $this->export_package_answer()
  2040. //TODO Precisa terminar! //TODO_HTML
  2041. function htmlcode_2_export ($course_fullname, $course_id, $iassign_block_id, $iassign_block_name, $array_iassign_name, $array_iassign_id, $userid, $username) {
  2042. $str_html = "<html dir='ltr' lang='pt-br' xml:lang='pt-br'>
  2043. <head>
  2044. <title>" . get_string('pluginname', 'iassign') . "</title>\n"; // 'iAssign: interactive Learning Activities'
  2045. $str_html .= " </head>
  2046. <body>\n";
  2047. $tam = sizeof($array_iassign_name);
  2048. for ($ii=0; $ii<$tam; $ii++) {
  2049. //TODO_HTML completar construir HTML com titulo do exercicio e talvez legar com HTML para abri-lo com o iMA
  2050. }
  2051. $str_html .= "
  2052. </body>\n</html>";
  2053. return $str_html;
  2054. }
  2055. /// Export an package (zip) with all answer of students
  2056. // @calledby $this->action()
  2057. function export_package_answer () {
  2058. global $DB, $CFG, $OUTPUT, $USER;
  2059. $iassign_id = optional_param('iassign_id', NULL, PARAM_INT); // ID of iAssign block of activities
  2060. if (!$iassign_id) // allow direct access through http?://.../moodle/mod/iassign/view.php?action=download_all_answer&id=CMID&iassignid=IASSIGNID
  2061. $iassign_id = optional_param('iassignid', NULL, PARAM_INT);
  2062. $iassign = $DB->get_record("iassign", array("id" => $iassign_id)); // all data from this block of iAssign activities (table '*_iassign')
  2063. $iassign_block_name = iassign_utils::format_filename($iassign->name);
  2064. $userid = optional_param('userid', NULL, PARAM_INT);
  2065. $package_all_students = false;
  2066. if (!$userid) { // allow direct access through http?://.../moodle/mod/iassign/view.php?action=download_all_answer&id=CMID&iassignid=IASSIGNID
  2067. if (has_capability('mod/iassign:evaluateiassign', $USER->context, $USER->id)) { // Teacher (or above) access, so can export all users answers
  2068. $userid = $USER->id;
  2069. $package_all_students = true;
  2070. }
  2071. }
  2072. $iassign_user = $DB->get_record("user", array("id" => $userid));
  2073. $username = ""; // if all student use empty name
  2074. if (!$package_all_students) {
  2075. if (isset($iassign_user->firstname)) {
  2076. $firstname = strtolower($iassign_user->firstname);
  2077. $sep_aux = "_";
  2078. }
  2079. if (isset($iassign_user->lastname)) {
  2080. $lastname = strtolower($iassign_user->lastname);
  2081. $sep_aux = "_";
  2082. }
  2083. $username = iassign_utils::format_filename($firstname . $sep_aux . $lastname) . '_'; // user information
  2084. }
  2085. $current_date = date('Y_m_d_H');
  2086. $directory = $CFG->dataroot . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR; // /var/<moodledata>/temp/files/
  2087. // If temporary directory under Moodle data does not exists => create it
  2088. if (!is_dir($directory)) {
  2089. mkdir($directory, 0755, true); // permissions: drwxr-xr-x
  2090. // created directory with permissions to be seen by everyone
  2091. }
  2092. $zip_filename = $directory . 'package_iassign_' . $username . strtolower($iassign_block_name) . '_' . $current_date . '.zip';
  2093. if (!is_writable($directory)) { // send a highlighted message!
  2094. print_error('error_answer_export_dir', 'iassign'); // Error: the target directory has no permission! Please, send this message to the Moodle admin!
  2095. exit; // just in case
  2096. }
  2097. //D echo "locallib.php!export_package_answer(): iassign_id=" . $iassign_id . ", iassign_block_name=" . $iassign_block_name . ", userid=" . $userid . ", directory=" . $directory;
  2098. //D echo " - zip_filename=" . $zip_filename . "<br/>";
  2099. //D echo "<br/>"; exit;
  2100. $zip = new zip_archive(); // create ZIP
  2101. $zip->open($zip_filename); // open ZIP using the temporary file/directory
  2102. $iassign_statements = $DB->get_records("iassign_statement", array("iassignid" => $iassign_id));
  2103. //TODO $vet_exerc_title = array(); //TODO to be used with 'htmlcode_2_export(...)' - missing: build the HTML and directories linking files
  2104. $vet_student_filename = array();
  2105. $vet_student_content = array();
  2106. foreach ($iassign_statements as $iassign_statement) {
  2107. $activity_name = iassign_utils::format_filename(strip_tags($iassign_statement->name));
  2108. $iassign_ilm = $DB->get_record("iassign_ilm", array("id" => $iassign_statement->iassign_ilmid));
  2109. //TODO_HTML $vet_exerc_title[] = $iassign_statement->name;
  2110. $extensions = explode(",", $iassign_ilm->extension);
  2111. if ($package_all_students)
  2112. $array_iassign_submissions = $DB->get_records("iassign_submission", array("iassign_statementid" => $iassign_statement->id)); // get ALL students
  2113. else {
  2114. $iassign_submission = $DB->get_record("iassign_submission", array("iassign_statementid" => $iassign_statement->id, "userid" => $userid));
  2115. $array_iassign_submissions = array($iassign_submission); // only one student (but put in array to uniform treatment
  2116. }
  2117. foreach ($array_iassign_submissions as $iassign_submission) { // to all students
  2118. if ($iassign_submission) { // student has submission to this activity
  2119. $appendix_student_filename = "";
  2120. if ($package_all_students)
  2121. $appendix_student_filename = "userid_" . $iassign_submission->userid . "_"; // use the student ID
  2122. $timemodified = '-' . userdate($iassign_submission->timemodified, '%Y_%m_%d_%H_%M');
  2123. $student_content = $iassign_submission->answer;
  2124. $student_filename = $appendix_student_filename . iassign_utils::format_filename(strtolower($activity_name)) . $timemodified . '.' . $extensions[0];
  2125. $resp = $zip->add_file_from_string($student_filename, $student_content); // add file to ZIP
  2126. //$resp = $zip->add_file_from_string($path_dir . $one_file, $destination . $ds . $one_file); //2 Esta versao funciona!
  2127. if ($resp) {
  2128. $vet_student_filename[] = $student_filename;
  2129. $vet_student_content[] = $student_content;
  2130. }
  2131. else { // send a highlighted message!
  2132. print_error('error_answer_export_file', 'iassign'); // Error: fail to insert content to the file
  2133. exit; // just in case
  2134. }
  2135. }
  2136. } // foreach ($array_iassign_submissions as $iassign_submission) // to all students
  2137. } // foreach ($iassign_statements as $iassign_statement)
  2138. $zip->close(); // close the ZIP file
  2139. if (count($vet_student_filename)>0) {
  2140. $vet_result = iassign_utils::register_temporary_file($zip_filename, $vet_student_filename, $vet_student_content);
  2141. $result_tf = $vet_result[0];
  2142. $filenameZip = $vet_result[1]->filename;
  2143. }
  2144. iassign_utils::download_file($zip_filename); // download the ZIP file
  2145. exit;
  2146. } // function export_package_answer()
  2147. /// Editing status of interactive activities
  2148. function edit_status () {
  2149. global $USER, $DB, $OUTPUT;
  2150. $newentry = new stdClass();
  2151. $newentry->id = $this->iassign_submission_current;
  2152. $newentry->status = optional_param('return_status', 0, PARAM_INT);
  2153. $iassign_submission = $DB->get_record('iassign_submission', array('id' => $this->iassign_submission_current));
  2154. if ($iassign_submission->status != 0 && $newentry->status == 0)
  2155. $newentry->status = $iassign_submission->status;
  2156. $newentry->teacher = $USER->id;
  2157. if (!$DB->update_record('iassign_submission', $newentry))
  2158. print_error('error_update', 'iassign');
  2159. else {
  2160. // Trigger module viewed event.
  2161. $event = \mod_iassign\event\submission_updated::create(array(
  2162. 'objectid' => $this->iassign->id,
  2163. 'context' => $this->context
  2164. ));
  2165. $event->add_record_snapshot('course', $this->course);
  2166. $event->trigger();
  2167. $this->action = 'viewsubmission';
  2168. $this->view_iassign_current();
  2169. } // if (!$DB->update_record('iassign_submission', $newentry))
  2170. } // function edit_status()
  2171. /// Editing grade of interactive activities
  2172. function edit_grade () {
  2173. global $USER, $DB, $OUTPUT;
  2174. $newgrade = optional_param('return_grade', 0, PARAM_INT);
  2175. if ($newgrade && $newgrade >= 0) {
  2176. $newentry = new stdClass();
  2177. $newentry->id = $this->iassign_submission_current;
  2178. $newentry->grade = optional_param('return_grade', 0, PARAM_INT);
  2179. $newentry->teacher = $USER->id;
  2180. if (!$DB->update_record('iassign_submission', $newentry))
  2181. print_error('error_update', 'iassign');
  2182. else {
  2183. // Trigger module viewed event.
  2184. $event = \mod_iassign\event\submission_updated::create(array(
  2185. 'objectid' => $this->iassign->id,
  2186. 'context' => $this->context
  2187. ));
  2188. $event->add_record_snapshot('course', $this->course);
  2189. $event->trigger();
  2190. }
  2191. } // if ($newgrade >= 0)
  2192. $this->action = 'viewsubmission';
  2193. $this->view_iassign_current();
  2194. } // function edit_grade()
  2195. /// Add or Edit interactive activities
  2196. // Get here when editing form and and submitted form data
  2197. // To each activity, we have an associated file in Moodle Data (registered in {files}) with: {files}.itemid := {iassign_statement}.id and {iassign_statement}.filesid := {files}.id
  2198. // Given a {iassign_statement}.id it is possible to recover its associated {files} using this (e.g. {iassign_statement}.id=10):
  2199. // SELECT cm.course, cm.id, cm.instance, c.instanceid, c.id AS contextid, f.id AS files_id, f.itemid, f.author, f.timecreated, f.filename,
  2200. // ia.id AS iaid, ias.file, ia.name AS ianame, ias.file AS iasfile, ias.id AS iasid, ias.name AS iasname, ias.timecreated
  2201. // FROM {modules} AS m, {course_modules} AS cm, {context} AS c, {files} AS f, {iassign} AS ia, {iassign_statement} AS ias
  2202. // WHERE m.name='iassign' AND m.id=cm.module AND cm.id = c.instanceid AND cm.instance = ia.id AND c.id=f.contextid AND f.component='mod_iassign'
  2203. // AND ia.id=ias.iassignid AND (f.itemid=ias.file OR f.itemid=ias.id) AND ias.id=10
  2204. // @calledby $this->action(): with "'add' => '$this->add_edit_iassign();'," or "'edit' => '$this->add_edit_iassign();',"
  2205. function add_edit_iassign () {
  2206. global $USER, $CFG, $COURSE, $DB, $OUTPUT;
  2207. require_once('iassign_form.php');
  2208. $id = $this->cm->id; // All iAssign statement => iassign_statement.iassignid
  2209. $iassignid = $this->iassign->id;
  2210. $param = new stdClass(); // parameters to build data to the form - see bellow "$mform = new mod_iassign_form(...); $mform->set_data($param);
  2211. $param->action = $this->action; // hidden
  2212. $param->id = $id; // hidden
  2213. $param->special_param1 = 0; // 'special_param1 == 1' => script of iGeom
  2214. // Trick to pass value to the Moodle form that is loaded bellow (with "$mform = new mod_iassign_form(null,...)")
  2215. // the last 2 are used in "iassign_form.php!definition()"
  2216. //x $COURSE->cm = $id; // "$id = $COURSE->cm;" but not used...
  2217. $COURSE->iassignid = $iassignid; // "$iassigns = $DB->get_records('iassign_statement', array('iassignid' => $COURSE->iassignid));"
  2218. $COURSE->iassign_file_id = NULL; // "if (!is_null($COURSE->iassign_file_id))"
  2219. // Associated {files} are registered with contextid from {context}.contextlevel = CONTEXT_MODULE
  2220. // ./lib/accesslib.php : CONTEXT_MODULE = 70 (CONTEXT_COURSE = 50)
  2221. $context = context_module::instance($this->cm->id); // use context from this course
  2222. $component = 'mod_iassign'; // to {files}
  2223. $filearea = 'exercise';
  2224. //x TODO remove this after a while (2023/04/19) - here $this->iassign_current is empty and $COURSE->iassign_id is never used!
  2225. if (!empty($this->iassign_current)) $COURSE->iassign_id = $this->iassign_current;
  2226. else $COURSE->iassign_id = 0;
  2227. $total_of_activities = 0; // get the total number of activities in this iAssign block
  2228. if ($this->action == 'add') {
  2229. $iassign_data = $DB->get_record("iassign", array('id' => $iassignid));
  2230. $params_temp = array('iassignid' => $iassignid);
  2231. $query_str = "SELECT s.id, s.name, s.dependency FROM {iassign_statement} s WHERE s.iassignid = :iassignid ORDER BY s.position ASC";
  2232. $iassign_statement = $DB->get_records_sql($query_str, $params_temp); //
  2233. $total_of_activities = count($iassign_statement);
  2234. $param->iassignid = $iassignid;
  2235. $param->name = "";
  2236. $param->oldname = "";
  2237. $param->type_iassign = 3;
  2238. $param->proposition = "";
  2239. $author = $USER; //x $DB->get_record("user", array("id" => $USER->id)); --- useless since we already have {user} info.!!!
  2240. $param->author_name = $author->firstname . ' ' . $author->lastname . ' (' . $author->id . ')';
  2241. $param->author_modified_name = $author->firstname . ' ' . $author->lastname . ' (' . $author->id . ')';
  2242. $param->author = $param->author_name;
  2243. $param->author_modified = $param->author_modified_name;
  2244. $COURSE->iassign_list = array();
  2245. $param->iassign_list = array();
  2246. if ($iassign_statement) {
  2247. foreach ($iassign_statement as $iassign) {
  2248. $iassignid = $iassign->id;
  2249. $param->iassign_list[$iassignid] = 0;
  2250. $COURSE->iassign_list[$iassignid] = new stdClass();
  2251. $COURSE->iassign_list[$iassignid]->id = $iassignid;
  2252. $COURSE->iassign_list[$iassignid]->name = $iassign->name;
  2253. $COURSE->iassign_list[$iassignid]->enable = 1;
  2254. } // foreach ($iassign_statement as $iassign)
  2255. }
  2256. $param->iassign_ilmid = 0;
  2257. $param->file = 0;
  2258. $param->fileid = 0; // to receive {files}.id
  2259. $param->fileold = 0;
  2260. $param->filename = "";
  2261. $param->grade = $iassign_data->grade;
  2262. $param->timemodified = time();
  2263. $param->timecreated = time();
  2264. $param->timeavailable = $iassign_data->timeavailable;
  2265. $param->timedue = $iassign_data->timedue;
  2266. $param->preventlate = $iassign_data->preventlate;
  2267. $param->test = $iassign_data->test;
  2268. $param->special_param1 = 0;
  2269. $param->visible = 1;
  2270. $param->max_experiment = $iassign_data->max_experiment;
  2271. $param->dependency = 0;
  2272. $param->automatic_evaluate = 1;
  2273. $param->show_answer = 1;
  2274. } // if ($this->action == 'add')
  2275. elseif ($this->action == 'edit') {
  2276. // First form load
  2277. // The update is effectively registered in {iassign_statement} and {files} bellow,
  2278. // in the second "elseif ($this->action == 'edit')": $iassignid = $this->activity->update_iassign($result);
  2279. $COURSE->iassign_list = array();
  2280. $this_activity = $this->activity->get_activity();
  2281. if ($this_activity != null) { // first enter here: fill form data
  2282. $iassign_statement_current = $this_activity; // $DB->get_record("iassign_statement", array("id" => $this_activity->id));
  2283. $param->iassign_id = $iassign_statement_current->id; // oculto
  2284. $param->iassignid = $iassign_statement_current->iassignid; // oculto
  2285. $param->name = $iassign_statement_current->name;
  2286. $param->oldname = $iassign_statement_current->name;
  2287. $param->type_iassign = $iassign_statement_current->type_iassign;
  2288. $param->store_all_submissions = $iassign_statement_current->store_all_submissions;
  2289. $param->proposition = array('text' => $iassign_statement_current->proposition, ''); //'format' => $instance->introformat
  2290. $param->author_name = $iassign_statement_current->author_name; // hidden field
  2291. $param->author = $iassign_statement_current->author_name;
  2292. $author = $USER; //x $DB->get_record("user", array('id' => $USER->id)); --- useless since we already have {user} info.!!!
  2293. $param->author_modified_name = $author->firstname . ' ' . $author->lastname . ' (' . $author->id . ')';
  2294. $param->author_modified = $param->author_modified_name;
  2295. $dependency = explode(';', $iassign_statement_current->dependency);
  2296. $param->iassign_list = array();
  2297. // Select all other iAssign activities inside this block with dependency
  2298. $str_query = "SELECT * FROM {iassign_statement} WHERE iassignid = '" . $iassignid . "' AND id!='" . $iassign_statement_current->id . "' AND dependency!=0";
  2299. $iassign_statement_dependency = $DB->get_records_sql($str_query);
  2300. $array_dependency = array();
  2301. $subdependency = "";
  2302. $sub_subdependency = "";
  2303. // Dependents on this exercise
  2304. if ($iassign_statement_dependency) {
  2305. $subdependency .= $this->search_dependency($iassign_statement_current->id, $iassign_statement_dependency);
  2306. // to whom this exercise depends
  2307. foreach ($subdependency as $tmp)
  2308. $sub_subdependency .= $this->search_sub_dependency($tmp);
  2309. $list_dependency = $subdependency . $sub_subdependency;
  2310. $array_dependency = explode(";", $list_dependency);
  2311. }
  2312. // Get all other activities in the same iAssigment block:
  2313. $str_query = "SELECT id, name FROM {iassign_statement} WHERE iassignid = '" . $iassignid . "' AND id!='" . $iassign_statement_current->id . "' ORDER BY position ASC";
  2314. $all_others_iassign_statement = $DB->get_records_sql($str_query);
  2315. $total_of_activities = count($all_others_iassign_statement) + 1;
  2316. $inter = array();
  2317. if ($all_others_iassign_statement) {
  2318. foreach ($all_others_iassign_statement as $iassign)
  2319. if (in_array($iassign->id, $dependency))
  2320. $inter[] = $iassign->id;
  2321. }
  2322. if ($all_others_iassign_statement) {
  2323. foreach ($all_others_iassign_statement as $iassign) {
  2324. $iassignid = $iassign->id;
  2325. $COURSE->iassign_list[$iassignid] = new stdClass();
  2326. $COURSE->iassign_list[$iassignid]->name = $iassign->name;
  2327. $COURSE->iassign_list[$iassignid]->id = $iassignid;
  2328. if (in_array($iassignid, $dependency))
  2329. $param->iassign_list[$iassignid] = 1;
  2330. else
  2331. $param->iassign_list[$iassignid] = 0;
  2332. if (in_array($iassignid, $array_dependency))
  2333. $COURSE->iassign_list[$iassignid]->enable = 0;
  2334. else
  2335. $COURSE->iassign_list[$iassignid]->enable = 1;
  2336. } // foreach ($all_others_iassign_statement as $iassign)
  2337. } // if ($all_others_iassign_statement)
  2338. $param->iassign_ilmid = $iassign_statement_current->iassign_ilmid; // define iLM ID to be used to define 'select' on 'iassign_form.php'
  2339. $param->fileold = 0;
  2340. $param->filesid = 0;
  2341. $param->file = $iassign_statement_current->file; // old version of {iassign_statement}/{files} association
  2342. $param->filesid = 0; // new version of {iassign_statement}/{files} association: {iassign_statement}.filesid := {files}.id and {files}.itemid := {iassign_statement}.id
  2343. $param->filename = '';
  2344. $param->grade = $iassign_statement_current->grade;
  2345. $param->timecreated = $iassign_statement_current->timecreated; // hidden
  2346. $param->timeavailable = $iassign_statement_current->timeavailable;
  2347. $param->timedue = $iassign_statement_current->timedue;
  2348. $param->preventlate = $iassign_statement_current->preventlate;
  2349. $param->test = $iassign_statement_current->test;
  2350. $param->special_param1 = $iassign_statement_current->special_param1; // 'special_param1 == 1' => script of iGeom
  2351. $param->position = $iassign_statement_current->position; // hidden
  2352. $param->visible = $iassign_statement_current->visible;
  2353. $param->max_experiment = $iassign_statement_current->max_experiment;
  2354. $param->automatic_evaluate = $iassign_statement_current->automatic_evaluate;
  2355. $param->show_answer = $iassign_statement_current->show_answer;
  2356. // Association {iassign_statement} with {files}
  2357. // Attention to the {files}.contextid and {files}.itemid used: "{files}.itemid" must be "{iassign_statement}.id" (and {iassign_statement}.filesid={files}.id)
  2358. // Old version used: {iassign_statement}.files = {files}.itemid with a randam value...
  2359. // ./lib/filestorage/file_storage.php: public function get_area_files($contextid, $component, $filearea, $itemid = false,...)
  2360. $fs = get_file_storage(); // Get reference to all files in Moodle data
  2361. //2021/12 $files_list = $fs->get_area_files($context->id, $component, $filearea, $iassign_statement_current->filesid);
  2362. //2022/02 $files_list = $fs->get_area_files($context->id, $component, $filearea, $iassign_statement_current->id); // em alguns "_files.id" tem 'file' NAO 'id'
  2363. $iassign_statement_id = $iassign_statement_current->id;
  2364. $iassign_statement_file = $iassign_statement_current->file;
  2365. $iassign_statement_filesid = $iassign_statement_current->filesid;
  2366. //D if ($USER->id == 3) // Here: function add_edit_iassign() : elseif ($this->action == 'edit') : if ($this_activity != null)
  2367. //D echo "<br/><br/><br/><br/>locallib.php: add_edit_iassign(): user.id=3: {ia}.id=" . $iassign_statement_id . ", contextid=" . $context->id . "<br/>\n"; //leo
  2368. // Get the element associated to {iassign_statement} in {files} using 'files_functions.php!get_from_files($ia_id, $ia_file, $ia_filesid, $fs, $contextid, $component, $filearea)'
  2369. require_once('files_functions.php'); // Get auxiliary functions to manage {iassign_statement}/{files}
  2370. $answer_array = get_from_files($iassign_statement_id, $iassign_statement_file, $iassign_statement_filesid, $fs, $context->id, $component, $filearea);
  2371. // if $f2_obj!=NULL but $f2_obj==NULL, then probably $f1_obj was recovered by 'files_functions.php!get_from_filesAll(.)' in the previous edition
  2372. if ($answer_array==NULL) {
  2373. if ($USER->id == 3) { //D Debug study - only {user}.id==3 sees it
  2374. echo " x locallib.php!add_edit_iassign(.): ERRO! {iassign_statement}.id=" . $iassign_statement_id . ", contextid=" . $context->id . " NAO esta' com {files} associado!<br/>\n"; //D
  2375. echo "Error: answer_array==NULL<br/>"; //D
  2376. echo "invalidid = " . get_string('invalidid', 'iassign') . "<br/>\n"; //D
  2377. // {files} : id contenthash pathnamehash contextid component filearea itemid filepath filename userid filesize mimetype status source author license timecreated
  2378. }
  2379. // Try to recover file in the same context with the smallest Levenstein distance from the activity name:
  2380. // levenshtein($str1,$str2) is the minimal number of characters you have to replace, insert or delete to transform $str1 into $str2
  2381. $ia_name = $iassign_statement_current->name;
  2382. $mindist_levenshtein = strlen($ia_name);
  2383. $mindist_component = '';
  2384. $answer_array = get_from_filesAll($fs, $context->id, 'exercise', $filearea); // $component='exercise'
  2385. if ($USER->id == 3) { //D Debug study - only {user}.id==3 sees it
  2386. if (is_null($answer_array) || !is_array($answer_array)) $count_arr = 0; //D
  2387. else $count_arr = count($answer_array); //D
  2388. }
  2389. foreach ($answer_array as $one_files)
  2390. if ($one_files->filename!='.') {
  2391. $dist_levenshtein = levenshtein($one_files->filename,$ia_name);
  2392. if ($dist_levenshtein <= $mindist_levenshtein) { // smaller distance (get always the last one - more recent one)
  2393. $mindist_levenshtein = $dist_levenshtein;
  2394. $mindist_component = 'exercise';
  2395. $f1_obj = $one_files;
  2396. }
  2397. } // if ($one_files->filename!='.')
  2398. $answer_array = get_from_filesAll($fs, $context->id, 'activity', $filearea); // $component='activity'
  2399. foreach ($answer_array as $one_files)
  2400. if ($one_files->filename!='.') {
  2401. $dist_levenshtein = levenshtein($one_files->filename,$ia_name);
  2402. if ($dist_levenshtein <= $mindist_levenshtein) { // smaller distance (get always the last one - more recent one)
  2403. $mindist_levenshtein = $dist_levenshtein;
  2404. $mindist_component = 'activity';
  2405. $f1_obj = $one_files;
  2406. }
  2407. } // if ($one_files->filename!='.')
  2408. // Now get as a 'file_storage' object (not a generic one returned fron $DB->get_records('files',...)
  2409. //./lib/filestorage/file_storage.php: public function get_file_instance(stdClass $filerecord)
  2410. if ($f1_obj != NULL) {
  2411. $f1_obj = $fs->get_file_instance($f1_obj);
  2412. }
  2413. $f2_obj = NULL;
  2414. //D print_error('invalidid', 'iassign'); // $string['invalidid'] = 'Invalid ID';
  2415. //D exit; // do not need 'print_error(.)' will stop execution
  2416. }
  2417. else { //DEBUG if ($answer_array==NULL)
  2418. //DEBUG evitar mensagens de depuracao no caso de erro de 'files_functions.php!get_from_files(.)' devolver vazio
  2419. $f1_obj = $answer_array[0]; // regular 'filename'
  2420. $f2_obj = $answer_array[1]; // directory (filename == '.')
  2421. } //DEBUG REMOVER if ($answer_array==NULL)
  2422. $ia_obj = $iassign_statement_current;
  2423. // 2022/10/07 Keep this until next iAssign version update
  2424. // ATTENTION: Some iAssign activity could have only one entry in {files} => came from duplication (using the same Moodle Data file)
  2425. // TODO: every duplication of activity must generate a new copy of actual file in Moodle Data
  2426. //TOD: Quem nao esta' duplicando {files}: "duplicar" ou "backup"?
  2427. // Ver: /home/leo/projetos/iMA/lms/itarefa/novo/files_iassign_statement/sobre_recuperacao_ia_files.txt
  2428. // It must have: {files}->id=$iassign_statement_current->file
  2429. if ($f1_obj != NULL) {
  2430. // ---------------------------------------------------------------------------------------------------------------------------------
  2431. // Try to recover association {iassign_statement}/{files}
  2432. // It uses "files_functions.php ! update_iassignstatement_files(...)" to (eventually) reconstruct relation
  2433. $result = update_iassignstatement_files($DB, $ia_obj->id, $f1_obj->get_id(), $ia_obj, $f1_obj, $f2_obj, $context); // ./files_functions.php
  2434. if (!$result) print "Could not change {iassign_statement}.filesid=" . $ia_obj->id . " or {files}.itemid=" . $f1_obj->itemid . "<br/>";
  2435. // $files_list = iassign::get_update_files($context->id, $component, $filearea, $iassign_statement_current, $fs); //2022: tentativa de recupearar arquivos se perdido
  2436. if ($f1_obj->get_filename() != '.') { // additional security: ensure the first {files} has real "filename"
  2437. $param->filename = $f1_obj->get_filename();
  2438. $param->file = $f1_obj->get_id();
  2439. $param->fileold = $f1_obj->get_id();
  2440. $COURSE->iassign_file_id = $f1_obj->get_id(); // necessary to "iassign_form.php" (loaded bellow with "$mform = new mod_iassign_form(...)")
  2441. }
  2442. } // if ($f1_obj != NULL)
  2443. //TODO //MOOC2014 -- get special parameters?
  2444. //D $iassign_ilm_configs = $DB->get_records('iassign_statement_config', array('iassign_statementid' => $iassign_statement_current->id));
  2445. //D if ($iassign_ilm_configs) {
  2446. //D foreach ($iassign_ilm_configs as $iassign_ilm_config)
  2447. //D $param->{'param_'.$iassign_ilm_config->iassign_ilm_configid} = $iassign_ilm_config->param_value;
  2448. //D } //MOOC2014 --
  2449. } // if ($this_activity != null)
  2450. } // elseif ($this->action == 'edit')
  2451. //D Get all iActivity in the iAssign block with id = $this->iassign->id ($this->iassign has data from the form)
  2452. //D It could be used to define dependency of the current activity (considering those list $iassign_list)
  2453. //D $iassign_list = $DB->get_records_list('iassign_statement', 'iassignid', array($this->iassign->id), 'position ASC'); //2022: not used any more
  2454. //D echo "locallib.php: add_edit_iassign(): this->iassign->id=" . $this->iassign->id . "<br/>";
  2455. //D foreach ($iassign_list as $item) echo " - id=" . $item->id . ", name=" . $item->name . ", iassignid=" . $item->iassignid . ", type_iassign=" . $item->type_iassign . "<br/>";
  2456. //D if ($iassign_list) { $end_list = array_pop($iassign_list); $param->position = $end_list->position + 1; } else $param->position = 1;
  2457. $param->position = $total_of_activities; // $total_of_activities = count($all_others_iassign_statement) + 1;
  2458. // Create form and associate its data that will be finally processed by 'locallib.php!update_iassign($param)'
  2459. // If {iassign_ilm}.enable == 0, then iLM remains undefined (does not appear in iLM selection)
  2460. // 'mform1' is used in: ./mod/iassign/iassign_form.php, ./mod/iassign/settings_form.php
  2461. // new mod_iassign_form(...) in ./mod/iassign/iassign_form.php extends /lib/formslib.php:
  2462. // ./lib/formslib.php: __construct($action=null, $customdata=null, $method='post', $target='', $attributes=null, $editable=true, $ajaxformdata=null)
  2463. if (isset($param->iassign_ilmid)) {
  2464. // array is additional atributes to 'form'
  2465. // 'special_param1 == 1' => script of iGeom : <select class="custom-select" name="special_param1" id="id_special_param1"> "Assignment with script(algorithm)? (Applicable only to iGeom iLM)"
  2466. $mform = new mod_iassign_form(null, array('special_param1'=>$param->special_param1),
  2467. 'post', null, array('id'=>'mform1', 'iassign_ilmid'=>$param->iassign_ilmid, 'author'=>$param->author, 'author_name'=>$param->author), true, null);
  2468. }
  2469. else {
  2470. $mform = new mod_iassign_form(null, null, null, null, array('id'=>'mform1')); // ./mod/iassign/iassign_form.php
  2471. }
  2472. // After "submit" button is pushed '$mform->get_data()' is sent to 'function update_iassign($param)'
  2473. $mform->set_data($param); // define data in the form
  2474. if ($mform->is_cancelled()) {
  2475. $this->return_home_course('iassign_cancel');
  2476. exit;
  2477. }
  2478. else { // else if ($mform->is_cancelled())
  2479. $result = $mform->get_data();
  2480. if ($result) { // from: $result = $mform->get_data();
  2481. $result->context = $context;
  2482. if ($result->type_iassign == 1 || $result->type_iassign == 2)
  2483. $result->grade = 0;
  2484. if ($result->type_iassign == 1) {
  2485. $result->automatic_evaluate = 0;
  2486. $result->show_answer = 0;
  2487. }
  2488. elseif ($result->automatic_evaluate == 0)
  2489. $result->show_answer = 0;
  2490. // $_POST['iassign_list']
  2491. $result->iassign_list = optional_param_array('iassign_list', array(), PARAM_RAW);
  2492. if ($result->iassign_list) {
  2493. foreach ($result->iassign_list as $key => $value)
  2494. $result->dependency .= $key . ';';
  2495. }
  2496. else
  2497. $result->dependency = 0;
  2498. //TODO I used in 'iassign_form.php' '<select name='iassign_ilmid'...>' with 'optgroup' by hand! But MoodleForm clear/do not register the 'iassign_ilmid'
  2499. //TODO Then get it directly from the form data $_POST!
  2500. //TODO See: 'iassign_form.php' field 'iassign_ilmid' and here 'function new_iassign($param)'
  2501. if (!isset($result->iassign_ilmid) && isset($_POST['iassign_ilmid']))
  2502. $result->iassign_ilmid = $_POST['iassign_ilmid'];
  2503. // $iassign_ilm = $DB->get_record("iassign_ilm", array("id" => $result->iassign_ilmid)); //2022: not used any more
  2504. if ($this->action == 'add') {
  2505. $iassign_statement_name = $DB->get_records('iassign_statement', array('iassignid' => $result->iassignid, 'name' => $result->name));
  2506. if ($iassign_statement_name) { // if this name already exist (in the same iAssign block) launch message error!
  2507. $this->return_home_course('error_iassign_name');
  2508. die();
  2509. }
  2510. // Really insert the new iActivity in table {iassign_statement}
  2511. // The form of 'ilm_manager.php' provides the {files} using its id (in GET variavel 'fileid') - copy it in {iassign_security}
  2512. // $file = $mform->save_stored_file(...)
  2513. $iassignid = $this->activity->new_iassign($result); // class activity : function new_iassign($param)
  2514. $this->activity->add_calendar($iassignid); // here calls static function add_calendar($iassignid): calendar_event::create($event); [5885/9304]
  2515. // Trigger module viewed event.
  2516. $event = \mod_iassign\event\iassign_created::create(array(
  2517. 'objectid' => $iassignid,
  2518. 'context' => $context
  2519. ));
  2520. $event->add_record_snapshot('course', $this->course);
  2521. $event->trigger();
  2522. $this->return_home_course('iassign_add');
  2523. } // if ($this->action == 'add')
  2524. elseif ($this->action == 'edit') {
  2525. // Here is the real registration in {iassign_statement} and {files}
  2526. // Above: $result = $mform->get_data();
  2527. $iassignid = $this->activity->update_iassign($result); // important: $result->file has the {files}.id selected
  2528. $this->activity->update_calendar($iassignid, $result->oldname);
  2529. // Trigger module viewed event.
  2530. $event = \mod_iassign\event\iassign_updated::create(array(
  2531. 'objectid' => $iassignid,
  2532. 'context' => $context
  2533. ));
  2534. $event->add_record_snapshot('course', $this->course);
  2535. $event->trigger();
  2536. $this->return_home_course('iassign_update');
  2537. } // elseif ($this->action == 'edit')
  2538. die();
  2539. } // if ($result): from $result = $mform->get_data();
  2540. } // else if ($mform->is_cancelled())
  2541. print $OUTPUT->header();
  2542. $mform->display();
  2543. print $OUTPUT->footer();
  2544. die();
  2545. } // function add_edit_iassign()
  2546. /// Search for dependencies
  2547. function search_dependency ($search_iassing_id, $iassign_statement) {
  2548. global $DB, $OUTPUT;
  2549. $dependency = "";
  2550. if ($iassign_statement)
  2551. foreach ($iassign_statement as $iassign) {
  2552. $inter_dependency = explode(';', $iassign->dependency);
  2553. if (in_array($search_iassing_id, $inter_dependency)) {
  2554. $dependency .= $iassign->id . ";";
  2555. $dependency .= $this->search_dependency($iassign->id, $iassign_statement);
  2556. } // if (in_array($search_iassing_id, $inter_dependency))
  2557. } // foreach ($iassign_statement as $iassign)
  2558. return $dependency;
  2559. }
  2560. /// Search for "sub"dependency
  2561. function search_sub_dependency ($search_iassing_id) {
  2562. global $DB, $OUTPUT;
  2563. $iassign_statement = $DB->get_record("iassign_statement", array("id" => $search_iassing_id));
  2564. $dependency = "";
  2565. if ($iassign_statement) {
  2566. $inter_dependency = explode(';', $iassign_statement->dependency);
  2567. foreach ($inter_dependency as $tmp) {
  2568. if ($tmp != 0)
  2569. $dependency .= $tmp . ";";
  2570. $dependency .= $this->search_sub_dependency($tmp);
  2571. } // foreach ($inter_dependency as $tmp)
  2572. } // if ($iassign_statement)
  2573. return $dependency;
  2574. } // function search_sub_dependency($search_iassing_id)
  2575. // Warning message
  2576. static function warning_message_iassign ($strcode) {
  2577. return "<div class='warning' style='display:inline; font-weight: bold; color:#a00'>" . get_string($strcode, 'iassign') . "</div>\n";
  2578. }
  2579. /// Update grade of iAssign
  2580. // Called always any iAssign activity is created
  2581. // @see /mod/iassign/view.php: call to iassign->iassign(): starting point
  2582. static function update_grade_iassign ($iassignid) {
  2583. global $USER, $CFG, $COURSE, $DB, $OUTPUT;
  2584. require_once($CFG->libdir . '/gradelib.php');
  2585. //D $sum_grade = $DB->get_records_sql ( "SELECT SUM(grade) as total
  2586. //D FROM {$CFG->prefix}iassign_statement s WHERE s.iassignid = '$iassignid' AND s.type_iassign=3" );
  2587. //TODO: REVIEW: wich one is more efficienty, '$DB->get_records' geting objects or '$DB->get_records' with 'foreach'?
  2588. // Each iAssign item is associated with one item on the "gradebook"
  2589. // Sum all '*_iassign_statement' associated with one item in '*_grade_items': iassignid AND type_iassign=3
  2590. //$sum_grade = 0;
  2591. //$grade = $DB->get_records('iassign_statement', array('iassignid' => $iassignid, 'type_iassign' => 3));
  2592. //foreach($grade as $tmp) {
  2593. // $sum_grade += $tmp->grade;
  2594. // }
  2595. //1 Solution 1
  2596. //1 $grade = $DB->get_records('iassign_statement', array('iassignid' => $iassignid, 'type_iassign' => 3)); //1
  2597. //1 $sum_grade = 0; //1
  2598. //1 foreach ($grade as $tmp) { $sum_grade += $tmp->grade; } //1
  2599. //2 Solution 2
  2600. $array_sum_grade = $DB->get_records_sql("SELECT SUM(grade) as total FROM {iassign_statement} s WHERE s.iassignid = '$iassignid' AND s.type_iassign=3"); //2
  2601. //2 foreach ($array_sum_grade as $array_item) { $sum_grade = $array_item->total; break; } // nao necessario, basta 'key(...)' abaixo
  2602. if (key($array_sum_grade))
  2603. $sum_grade = key($array_sum_grade); //2
  2604. else
  2605. $sum_grade = 0; //2
  2606. $grade_iassign = $DB->get_record("iassign", array("id" => $iassignid));
  2607. $grades = NULL;
  2608. $params_temp = array('itemname' => $grade_iassign->name);
  2609. $params_temp['iteminstance'] = $iassignid;
  2610. $params_temp['gradetype'] = GRADE_TYPE_VALUE;
  2611. //2016 if ($sum_grade != 0) {
  2612. $params_temp['grademax'] = $sum_grade;
  2613. $params_temp['rawgrademax'] = $sum_grade;
  2614. //2016 } else { $params_temp['grademax'] = 0; $params_temp['rawgrademax'] = 0; }
  2615. $params_temp['grademin'] = 0;
  2616. // @calls /lib/gradelib.php: function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grades=null, $itemdetails=null, $isbulkupdate=false)
  2617. // It uses the table {grade_items}
  2618. //TODO: is there any error here in Moodle version 3.0?
  2619. //TODO: Incorrect property 'grademax' found when inserting grade object
  2620. //TODO: line 899 of /mod/iassign/locallib.php: call to grade_update()
  2621. grade_update('mod/iassign', $grade_iassign->course, 'mod', 'iassign', $iassignid, 0, $grades, $params_temp);
  2622. }
  2623. /// Update grade of student
  2624. // {iassign_statement}.grade = maximum grade to this activity
  2625. // {iassign_submission}.grade = full grade in this submission (considering [0,1]*{iassign_statement}.grade)
  2626. function update_grade_student ($userid, $iassign_statementid, $iassignid) {
  2627. global $CFG, $DB, $OUTPUT;
  2628. require_once($CFG->libdir . '/gradelib.php');
  2629. $grade_iassign = $DB->get_record('iassign', array('id' => $iassignid));
  2630. // Review all the student submission for this iAssign activity
  2631. $grade_iassign_statements = $DB->get_records('iassign_statement', array('iassignid' => $iassignid));
  2632. $total_grade = 0;
  2633. foreach ($grade_iassign_statements as $grade_iassign_statement) {
  2634. $iassign_submission = $DB->get_record('iassign_submission', array('iassign_statementid' => $grade_iassign_statement->id, 'userid' => $userid));
  2635. if ($iassign_submission) // sum all {iassign_submission}.grade
  2636. $total_grade += $iassign_submission->grade;
  2637. } // foreach ($grade_iassign_statements as $grade_iassign_statement)
  2638. //D $sum_grade = $DB->get_records_sql("SELECT SUM(grade) as total
  2639. //D FROM {$CFG->prefix}iassign_statement s WHERE s.iassignid = '$iassignid' AND s.type_iassign=3" );
  2640. //TODO: REVIEW: wich one is more efficienty, '$DB->get_records' geting objects or '$DB->get_records' with 'foreach'?
  2641. //1 $sum_grade = 0; $grade = $DB->get_records('iassign_statement', array('iassignid' => $iassignid, 'type_iassign' => 3));
  2642. //1 foreach ($grade as $tmp) { $sum_grade += $tmp->grade; }
  2643. // For each {iassign_statement} in the block {iassign}, sum their max grade (field {grade})
  2644. $array_sum_grade = $DB->get_records_sql("SELECT SUM(grade) as total FROM {iassign_statement} s WHERE s.iassignid = '$iassignid' AND s.type_iassign=3"); //2
  2645. if (key($array_sum_grade))
  2646. $sum_grade = key($array_sum_grade); //2
  2647. else
  2648. $sum_grade = 0; //2
  2649. $grades['userid'] = $userid;
  2650. $grades['rawgrade'] = $total_grade; // sum of all submissions for this iAssign activity
  2651. $params_temp = array('itemname' => $grade_iassign->name);
  2652. $params_temp['iteminstance'] = $iassignid;
  2653. $params_temp['gradetype'] = GRADE_TYPE_VALUE;
  2654. //2016 if ($sum_grade != 0) { // depois eliminar comentario
  2655. $params_temp['grademax'] = $sum_grade;
  2656. $params_temp['rawgrademax'] = $sum_grade;
  2657. //2016 } else { $params_temp['grademax'] = 0; $params_temp['rawgrademax'] = 0; }
  2658. // Now write the grade of iAssign activity (sum of all of its {iassign_statement}.grade) to the Moodle grade book
  2659. // @calls /lib/gradelib.php: function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grades=null, $itemdetails=null, $isbulkupdate=false)
  2660. // It uses the table {grade_items}
  2661. grade_update('mod/iassign', $grade_iassign->course, 'mod', 'iassign', $iassignid, 0, $grades, $params_temp);
  2662. } // function update_grade_student($userid, $iassign_statementid, $iassignid)
  2663. /// Display caption of icons - to the bottom page of teacher's report
  2664. function view_legend_icons () {
  2665. global $USER, $CFG, $DB, $OUTPUT;
  2666. $id = $this->cm->id;
  2667. if ($this->action == 'print')
  2668. print '<table border=1 width=100%><tr>';
  2669. else
  2670. print '<table width=100%><tr>';
  2671. print '<td >';
  2672. if (has_capability('mod/iassign:viewreport', $this->context, $USER->id) && $this->action == 'report') {
  2673. if ($this->action != 'print') {
  2674. $link_export = "<a href='" . $CFG->wwwroot . "/mod/iassign/view.php?id=" . $id . "&action=export_csv&iassignid=" . $this->iassign->id .
  2675. "' title='" . get_string('export_csv_help', 'iassign') . "'>" . iassign_icons::insert('export_ilm') . '&nbsp;' . get_string('export_csv', 'iassign') . "</a>\n";
  2676. $link_all_solutions = "<a href='" . $CFG->wwwroot . "/mod/iassign/view.php?id=" . $id . "&action=download_all_answer&iassignid=" . $this->iassign->id .
  2677. "' title='" . get_string('export_all_submissions_help', 'iassign') . "'>" . iassign_icons::insert('export_ilm') . '&nbsp;' . get_string('export_all_submissions', 'iassign') . "</a>\n";
  2678. $link_print = "<a href='" . $CFG->wwwroot . "/mod/iassign/view.php?id=" . $id . "&action=print&iassignid=" . $this->iassign->id .
  2679. "' title='" . get_string('print_help', 'iassign') . "'>" . iassign_icons::insert('print') . '&nbsp;' . get_string('print', 'iassign') . "</a>\n";
  2680. print '<td width=10% align="right">' . $link_export . '</td>' . "\n";
  2681. print '<td width=10% align="right">' . $link_all_solutions . '</td>' . "\n"; // export a package with ALL the submissions to ALL students to each activities
  2682. print '<td width=15% align="right">' . $link_print . '</td>' . "\n";
  2683. } // if ($this->action != 'print')
  2684. print '</tr></table>' . "\n";
  2685. } // if (has_capability('mod/iassign:viewreport', $this->context, $USER->id) && $this->action == 'report')
  2686. print '</td></tr></table>' . "\n";
  2687. } // function view_legend_icons()
  2688. /// Display activity current
  2689. // @calledby view() -> action() : when student do/redo activity and teacher see student answer
  2690. function view_iassign_current () {
  2691. global $USER, $CFG, $COURSE, $DB, $OUTPUT, $PAGE;
  2692. $id = $this->cm->id;
  2693. $iassignid = $this->iassign->id;
  2694. $only_one_send_button = 0; // to avoid to put 2 copies of comments (area to send and see comments between teacher and student)
  2695. $iassign = $DB->get_record("iassign", array("id" => $iassignid)); // from table '*_iassign': id course name intro introformat activity_group grade timeavailable timedue preventlate test max_experiment
  2696. // Get data of current activity : table '*_iassign_statement' = id name iassignid type_iassign proposition author_name author_modified_name iassign_ilmid file grade timemodified timecreated ...
  2697. $iassign_statement_activity_item = $this->activity->get_activity(); // get all field to the current activity: the teacher activity model in {iassign_statement}
  2698. if (!$iassign_statement_activity_item) {
  2699. print $OUTPUT->header();
  2700. \core\notification::error(get_string('activity_not_found', 'iassign'));
  2701. print $OUTPUT->footer();
  2702. exit;
  2703. }
  2704. $ilm = new ilm($iassign_statement_activity_item->iassign_ilmid);
  2705. $ilm_name = '';
  2706. if ($ilm->ilm)
  2707. $ilm_name = strtolower($ilm->ilm->name); // class ilm has a unique property ('ilm'), get the iLM name
  2708. // Do not allow the learner resent his solution only is iGeom iLM ("igeom") - why? iGeom acitivity "model answer" is not sent with the learner solution
  2709. $allow_resubmission = (substr($ilm_name, 0, 5) != "igeom" ? 1 : 0);
  2710. // log record
  2711. $info = $iassign->name . ":" . $iassign_statement_activity_item->name;
  2712. //Trigger module viewed event.
  2713. $event = \mod_iassign\event\submission_viewed::create(array(
  2714. 'objectid' => $iassign->id,
  2715. 'context' => $this->context,
  2716. 'other' => $info
  2717. ));
  2718. $event->add_record_snapshot('course', $this->course);
  2719. $event->trigger();
  2720. // Search of iLM data used in the current activity
  2721. $iassign_ilm = $DB->get_record("iassign_ilm", array("id" => $iassign_statement_activity_item->iassign_ilmid));
  2722. // Add actual activity to navbar:
  2723. $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  2724. $PAGE->navbar->add($iassign_statement_activity_item->name, $actual_link);
  2725. print $OUTPUT->header();
  2726. if ($this->action == 'viewsubmission') {
  2727. if (!empty($this->iassign_submission_current) || $this->iassign_submission_current != 0)
  2728. $iassign_submission = $DB->get_record("iassign_submission", array("id" => $this->iassign_submission_current)); // data about activity current
  2729. else
  2730. $iassign_submission = $DB->get_record("iassign_submission", array("iassign_statementid" => $this->activity->get_activity()->id, "userid" => $this->userid_iassign)); // data about student solution
  2731. }
  2732. else {
  2733. $iassign_submission = $DB->get_record("iassign_submission", array("iassign_statementid" => $this->activity->get_activity()->id, "userid" => $this->userid_iassign)); // data about student solution
  2734. }
  2735. if ($iassign_submission)
  2736. $this->update_comment($iassign_submission->id);
  2737. //2021/12 $file = $iassign_statement_activity_item->filesid;
  2738. // $file = $iassign_statement_activity_item->id; - nao usado aqui!
  2739. // 1 when open previous file; 2 when the activity is redone!; 3 when the teacher enter in the activity
  2740. // 1 => locallib.php: view_iassign_current(): action=view will set write_solution=0!!!!!
  2741. // 2 => locallib.php: view_iassign_current(): action=repeat will set write_solution=0!!!!!
  2742. // 3 => locallib.php: view_iassign_current(): action=viewsubmission will set write_solution=0!!!!!
  2743. $this->allowPostButton = 0; // hide submit button
  2744. //xxx $this->write_solution = 0; // disable recording solution (however, iVProg allow the learner to edit previou solution)
  2745. $this->view_iassign = false; // disable visualization of activity
  2746. $repeat = "";
  2747. $last_iassign = "";
  2748. $student_answer = "";
  2749. $comment = "";
  2750. // *** Teacher access (view learner's submission) or view the exercise
  2751. if (($this->action != 'viewsubmission') && has_capability('mod/iassign:evaluateiassign', $USER->context, $USER->id)) {
  2752. //TODO leo Verificar se o correto eh '$this->context' ou '$USER->context' como deixei
  2753. // It is not 'viewsubmission' and it is teacher or 'non editing teacher'?
  2754. // ---> access teacher for test
  2755. if ($iassign_statement_activity_item->type_iassign != 1) // type_iassign=1 => activity of type "example" - usually does not allow submit button
  2756. $this->allowPostButton = 1;
  2757. print $OUTPUT->box('<p><strong>' . get_string('area_specific_teacher', 'iassign') . '</strong></p>');
  2758. $this->activity->view_dates();
  2759. $USER->iassignEdit = $this->allowPostButton; // To be used by "./mod/iassign/ilm_handers/*.php"
  2760. $this->activity->show_info_iassign();
  2761. // Prepare variable "$enderecoPOST" that is used in the JS code in /mod/iassign/ilm_handlers/html5.php
  2762. if ($iassign_submission) {
  2763. //xxx Removed tag "write_solution": . "&write_solution=" . $this->write_solution .
  2764. $param_aux = "action=get_answer&iassign_submission_current=" . $iassign_submission->id . "&id=" . $id . "&iassign_current=" .
  2765. $this->activity->get_activity()->id . "&userid_iassign=" . $USER->id;
  2766. } else {
  2767. //xxx Removed tag "write_solution": . "&write_solution=" . $this->write_solution .
  2768. $param_aux = "action=get_answer&id=" . $id . "&iassign_current=" . $this->activity->get_activity()->id . "&userid_iassign=" . $USER->id;
  2769. }
  2770. $enderecoPOST = "" . $CFG->wwwroot . "/mod/iassign/view.php?" . $param_aux;
  2771. // Prepare tags to present the iLM
  2772. print $OUTPUT->box($ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, true));
  2773. } // if (($this->action != 'viewsubmission') && has_capability('mod/iassign:evaluateiassign', $USER->context, $USER->id))
  2774. // *** (end) Teacher access (view the activity)
  2775. // *** Teacher access (view learner's submission to the activity)
  2776. elseif (($this->action == 'viewsubmission') && has_capability('mod/iassign:evaluateiassign', $USER->context, $USER->id)) {
  2777. // It is teacher or 'nonediting teacher' that can evaluate
  2778. // ----> area teacher evaluate
  2779. $row = optional_param('row', 0, PARAM_INT);
  2780. $column = optional_param('column', 0, PARAM_INT);
  2781. $link_next = iassign_icons::insert('right_disable');
  2782. $link_previous = iassign_icons::insert('left_disable');
  2783. $link_up = iassign_icons::insert('up_disable');
  2784. $link_down = iassign_icons::insert('down_disable');
  2785. if (isset($USER->matrix_iassign[$row][$column])) {
  2786. $has_row_column = True;
  2787. $element_matrix_iassign = $USER->matrix_iassign[$row][$column];
  2788. }
  2789. else {
  2790. $has_row_column = False;
  2791. $element_matrix_iassign = $USER->matrix_iassign[$row][$column];
  2792. }
  2793. $msgViewAction = "view.php?action=viewsubmission&id=$id&iassign_submission_current=";
  2794. $msgUserId_current = "&userid_iassign=" . $this->userid_iassign . "&iassign_current=";
  2795. // next_activity
  2796. if ($has_row_column && $element_matrix_iassign->iassign_next != - 1) {
  2797. $url_next = $msgViewAction . $USER->matrix_iassign[$row][$column + 1]->iassign_submission_current . $msgUserId_current . $element_matrix_iassign->iassign_next . "&view_iassign=report&row=" . ($row) . "&column=" . ($column + 1);
  2798. $link_next = "<a href='" . $url_next . "'>" . (iassign_icons::insert('next_activity')) . "</a>";
  2799. }
  2800. // previous_activity
  2801. if ($has_row_column && $element_matrix_iassign->iassign_previous != - 1) {
  2802. $url_previous = $msgViewAction . $USER->matrix_iassign[$row][$column - 1]->iassign_submission_current . $msgUserId_current . $element_matrix_iassign->iassign_previous . "&view_iassign=report&row=" . ($row) . "&column=" . ($column - 1);
  2803. $link_previous = "<a href='" . $url_previous . "'>" . (iassign_icons::insert('previous_activity')) . "</a>";
  2804. }
  2805. // previous_student
  2806. if ($has_row_column && $element_matrix_iassign->user_next != - 1) {
  2807. $url_down = $msgViewAction . $USER->matrix_iassign[$row + 1][$column]->iassign_submission_current . "&userid_iassign=" . $element_matrix_iassign->user_next . "&iassign_current=" . $this->activity->get_activity()->id . "&view_iassign=report&row=" . ($row + 1) . "&column=" . ($column);
  2808. $link_down = "<a href='" . $url_down . "'>" . (iassign_icons::insert('previous_student')) . "</a>";
  2809. }
  2810. // next_student
  2811. if ($has_row_column && $element_matrix_iassign->user_previous != - 1) {
  2812. $url_up = $msgViewAction . $USER->matrix_iassign[$row - 1][$column]->iassign_submission_current . "&userid_iassign=" . $element_matrix_iassign->user_previous . "&iassign_current=" . $this->activity->get_activity()->id . "&view_iassign=report&row=" . ($row - 1) . "&column=" . ($column);
  2813. $link_up = "<a href='" . $url_up . "'>" . (iassign_icons::insert('next_student')) . "</a>";
  2814. }
  2815. if ($iassign_submission) {
  2816. $student_answer = $iassign_submission->answer;
  2817. }
  2818. $last_iassign = get_string('last_iassign', 'iassign');
  2819. $user_data = $DB->get_record("user", array('id' => $this->userid_iassign));
  2820. // Messages related to due date (and user role)
  2821. $this->activity->view_dates();
  2822. print $OUTPUT->box_start();
  2823. print '<table width=100% border=0 valign="top"><tr>' . "\n";
  2824. print '<td width=80%><font color="blue"><strong>' . get_string('area_available', 'iassign') . '</strong></font><br>' . "\n";
  2825. if ($user_data) // additional security (if occurs programming errors in $url_next, $url_previous...)
  2826. print $OUTPUT->user_picture($user_data);
  2827. print ' ' . $user_data->firstname . ' ' . $user_data->lastname;
  2828. print '</td>' . "\n";
  2829. print '<td width=20% align=right>' . "\n";
  2830. print '<table width=50 cellpadding="0">';
  2831. print '<tr><td colspan=2 align=center>' . $link_up . '</td></tr>' . "\n";
  2832. print '<tr><td align=center>' . $link_previous . '</td>' . "\n";
  2833. print '<td align=center>' . $link_next . '</td></tr>' . "\n";
  2834. print '<td colspan=2 align=center>' . $link_down . '</td></tr>' . "\n";
  2835. print '</table>' . "\n";
  2836. print '</td></tr></table>' . "\n";
  2837. print $OUTPUT->box_end();
  2838. print $OUTPUT->box_start();
  2839. print '<table width=100% border=0 valign="top"><tr>' . "\n";
  2840. print '<td width=60% valign="top">' . "\n";
  2841. print '<p><strong>' . get_string('proposition', 'iassign') . ':</strong></p>' . "\n";
  2842. print '<div class="proposition">' . $iassign_statement_activity_item->proposition . '</div>' . "\n";
  2843. if ($iassign_statement_activity_item->automatic_evaluate == 1)
  2844. $resp = get_string('yes');
  2845. else
  2846. $resp = get_string('no');
  2847. print '<p>' . get_string('automatic_evaluate', 'iassign') . '&nbsp;' . $resp . '</p>' . "\n";
  2848. if ($iassign_statement_activity_item->show_answer == 1)
  2849. $resp = get_string('yes');
  2850. else
  2851. $resp = get_string('no');
  2852. print '<p>' . get_string('show_answer', 'iassign') . '&nbsp;' . $resp . '</p>' . "\n";
  2853. print '</td>';
  2854. if ($iassign_statement_activity_item->type_iassign == 3) { // type_iassign=3 => activity of type "exercise" - submit button and automatic evaluation
  2855. print '<td width=40% valign="top" align="left">';
  2856. print '<strong>' . get_string('status', 'iassign') . '</strong>' . "\n";
  2857. // check status of solution sent by the student
  2858. if ($iassign_submission) {
  2859. switch ($iassign_submission->status) {
  2860. case 3 :
  2861. print iassign_icons::insert('correct') . '&nbsp;' . get_string('correct', 'iassign') . '&nbsp;' . $comment;
  2862. break;
  2863. case 2 :
  2864. print iassign_icons::insert('incorrect') . '&nbsp;' . get_string('incorrect', 'iassign') . '&nbsp;' . $comment;
  2865. break;
  2866. case 1 :
  2867. print iassign_icons::insert('post') . '&nbsp;' . get_string('post', 'iassign') . '&nbsp;' . $comment;
  2868. break;
  2869. default :
  2870. print iassign_icons::insert('not_post') . '&nbsp;' . get_string('not_post', 'iassign') . '&nbsp;' . $comment;
  2871. $last_iassign = get_string('no_iLM_PARAM_ArchiveContent', 'iassign');
  2872. } // switch ($iassign_submission->status)
  2873. }
  2874. else { // if ($iassign_submission)
  2875. print iassign_icons::insert('not_post') . '&nbsp;' . get_string('not_post', 'iassign') . '&nbsp;' . $comment;
  2876. $last_iassign = get_string('no_iLM_PARAM_ArchiveContent', 'iassign');
  2877. }
  2878. // update_status
  2879. if ($iassign_submission && $iassign_submission->experiment > 0) {
  2880. $edit_status = $CFG->wwwroot . "/mod/iassign/view.php?action=edit_status&id=" . $id . "&userid_iassign=" . $this->userid_iassign . "&iassign_current=" . $this->activity->get_activity()->id . "&iassign_submission_current=" . $this->iassign_submission_current . "&row=" . ($row) . "&column=" . ($column);
  2881. print " <script type='text/javascript'>
  2882. //<![CDATA[
  2883. function overwriteStatus (newstatus) {
  2884. if (confirm('" . get_string('confirm_change_situation', 'iassign') . "')) {
  2885. document.formEditStatus.return_status.value=newstatus;
  2886. document.formEditStatus.submit();
  2887. }
  2888. else
  2889. document.formEditStatus.return_status.value=-1;
  2890. }
  2891. //]]>
  2892. </script>";
  2893. print "<form name='formEditStatus' method='post' action='$edit_status' enctype='multipart/form-data'>\n";
  2894. print ' <font color="blue"><strong>' . get_string('changeto', 'iassign') . "</strong></font>\n";
  2895. print " <select name='status' onchange= 'overwriteStatus(this.value)'>\n" . " <option value=\"3\">" . get_string('correct', 'iassign') . "</option>\n" . " <option value=\"2\">" . get_string('incorrect', 'iassign') . "</option>\n" . " <option value=\"1\">" . get_string('post', 'iassign') . "</option>\n" . " <option value=\"0\">" . get_string('not_post', 'iassign') . "</option>\n" . " <option value=\"-1\" selected>" . get_string('newsituation', 'iassign') . "</option>\n" . " </select>\n";
  2896. print " <input type='hidden' name='return_status'>\n";
  2897. print "</form>\n";
  2898. print '<p><strong>' . get_string('grade_student', 'iassign') . '</strong> ' . $iassign_submission->grade . "</p>\n";
  2899. print '<p><strong>' . get_string('grade_iassign', 'iassign') . '</strong> ' . $iassign_statement_activity_item->grade . "</p>\n";
  2900. $edit_grade = $CFG->wwwroot . "/mod/iassign/view.php?action=edit_grade&id=" . $id . "&userid_iassign=" . $this->userid_iassign . "&iassign_current=" . $this->activity->get_activity()->id . "&iassign_submission_current=" . $this->iassign_submission_current . "&row=" . ($row) . "&column=" . ($column);
  2901. print "
  2902. <script type='text/javascript'>
  2903. //<![CDATA[
  2904. function overwriteGrade (newgrade,maxgrade) {
  2905. if (newgrade<0 || newgrade>maxgrade) {
  2906. alert('" . get_string('erro_grade', 'iassign') . " '+maxgrade)
  2907. document.formEditGrade.return_grade.value=-1;
  2908. document.formEditGrade.submit();
  2909. }
  2910. else {
  2911. document.formEditGrade.return_grade.value=newgrade;
  2912. document.formEditGrade.submit();
  2913. }
  2914. }
  2915. //]]>
  2916. </script>";
  2917. print "<form name='formEditGrade' method='post' action='$edit_grade' enctype='multipart/form-data'>\n";
  2918. print ' <font color="blue"><strong>' . get_string('changeto', 'iassign') . "</strong></font>" . "\n";
  2919. print " <input type='text' name='grade' size='6'>";
  2920. print " <input type='hidden' name='return_grade'> ";
  2921. print " <input type=button value='" . get_string('confirm', 'iassign') . "' onClick = 'overwriteGrade(grade.value," . $iassign_statement_activity_item->grade . ")' " . " title='" . get_string('confirm_new_grade', 'iassign') . "'>\n";
  2922. print "</form>";
  2923. $url_answer = "" . $CFG->wwwroot . "/mod/iassign/view.php?" . "action=download_answer&iassign_submission_id=" . $iassign_submission->id . "&id=" . $id;
  2924. print '<p><strong>' . get_string('experiment', 'iassign') . '</strong> ' . $iassign_submission->experiment . ' <a href="' . $url_answer . '">' . iassign_icons::insert('download_assign') . '</a></p>';
  2925. print '<p><strong>' . get_string('timemodified', 'iassign') . '</strong> ' . userdate($iassign_submission->timemodified) . '</p>';
  2926. $teacher = $DB->get_record("user", array('id' => $iassign_submission->teacher));
  2927. if ($teacher)
  2928. print '<p><strong>' . get_string('last_modification', 'iassign') . '</strong> ' . $teacher->firstname . '</p>' . "\n";
  2929. } // if ($iassign_submission->experiment > 0)
  2930. print '</td>';
  2931. } // if ($iassign_statement_activity_item->type_iassign == 3)
  2932. print '</tr></table>';
  2933. print $OUTPUT->box_end();
  2934. //TODO It can be removed, since now we have the field {iassign_statement}.editingbehavior?
  2935. $USER->iassignEdit = $this->allowPostButton; // To be used by "./mod/iassign/ilm_handers/*.php"
  2936. print activity::toggle_columns_script();
  2937. if ($iassign_submission && $allow_resubmission) {
  2938. // Put the iLM to
  2939. print $OUTPUT->box_start();
  2940. print '<p><strong>' . $last_iassign . '</strong></p>';
  2941. $enderecoPOST = "";
  2942. // Prepare tags to present the iLM
  2943. print $ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, false);
  2944. print $OUTPUT->box_end();
  2945. } // if ($iassign_submission && $allow_resubmission)
  2946. else { // if ($iassign_submission && $allow_resubmission) - techer view student answer
  2947. // If '$student_answer' is empty, then the student does not submit any solution to the exercise
  2948. // ATTENTION: exception used by iGeom (exercise with "script")
  2949. $loadTeacherActivity = false; // use 'true' whenever 'special_param1 == 1'
  2950. if (substr($ilm_name, 0, 5)=="igeom") {
  2951. if ($iassign_statement_activity_item->special_param1 == 1) // if 1 => use the teacher activity with some complement from the student (in iGeom = GEO + SCR)
  2952. $loadTeacherActivity = true;
  2953. }
  2954. else
  2955. if (!$student_answer) { //2022/01 this student does not submit any soluction => load the teacher content
  2956. $loadTeacherActivity = true;
  2957. }
  2958. print $OUTPUT->box('<p><strong>' . $last_iassign . '</strong></p>' . "\n");
  2959. if (!isset($enderecoPOST)) $enderecoPOST = "";
  2960. print activity::toggle_columns_script();
  2961. // Prepare tags to present the iLM
  2962. print $OUTPUT->box($ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, $loadTeacherActivity));
  2963. }
  2964. //2020 Eliminei daqui o codigo para colocar o quadro com area para enviar e ler comentarios
  2965. //2020 Estava subordinado ao: elseif (($this->action == 'viewsubmission') && has_capability('mod/iassign:evaluateiassign', $USER->context, $USER->id))
  2966. //2020 e com isso NAO permitia aluno ter o quadro para envio!
  2967. //2020 Foi para final dessa funcao 'view_iassign_current()'
  2968. } // elseif (($this->action == 'viewsubmission') && has_capability('mod/iassign:evaluateiassign', $USER->context, $USER->id))
  2969. // *** (end) Teacher access (view learner's submission to the activity)
  2970. // *** Student access (view the activity)
  2971. elseif (has_capability('mod/iassign:submitiassign', $USER->context, $USER->id)) {
  2972. // It could be the learner (he could send or resend the activity)
  2973. // ---> access student
  2974. $time_now = time();
  2975. if ($iassign_statement_activity_item->type_iassign == 1) { // type_iassign=1 => activity of type "exemple" - no submit button
  2976. // activity of type example - not submit button for submission
  2977. $this->view_iassign = true;
  2978. //TODO rever esta condicao para iMA que nao fazem autoavaliacao
  2979. }
  2980. elseif ($iassign_statement_activity_item->type_iassign == 2 && $iassign_ilm->evaluate == 1) {
  2981. // activity of type test - iLM automatic evaluator - submit button for submission
  2982. if ($iassign_statement_activity_item->timeavailable < $time_now && $iassign_statement_activity_item->timedue > $time_now) { // activity within of deadline
  2983. $this->allowPostButton = 1;
  2984. $this->view_iassign = true;
  2985. }
  2986. else {
  2987. $this->allowPostButton = 0;
  2988. $this->view_iassign = false;
  2989. }
  2990. }
  2991. elseif ($iassign_statement_activity_item->type_iassign == 3) { // type_iassign=3 => activity of type "exercise" - submit button and automatic evaluation
  2992. // Activity of type exercise (learner can send his answer, if yet open...)
  2993. // $iassign_statement_activity_item has {iassign_statement} = id name iassignid type_iassign ... timedue preventlate test
  2994. $this->view_iassign = true;
  2995. if ($iassign_statement_activity_item->timeavailable > $time_now) { // activity not yet available
  2996. $this->view_iassign = false;
  2997. }
  2998. elseif ($iassign_statement_activity_item->timedue > $time_now || $iassign_statement_activity_item->preventlate == 1) {
  2999. // Activity open: due date not expired
  3000. $this->allowPostButton = 1; // allow the submit button
  3001. // Look at table 'iassign_submission' ('iassign_submission.experiment' is the number of submissions)
  3002. // action=repeat will set write_solution=0 => the iLM allow to edit the "old" solution
  3003. $repeat_title = ' title="' . get_string('repeat_alt', 'iassign') . '" '; // Use this button to 'redo the activity'
  3004. if (!$iassign_submission || $this->action == 'repeat' || ($iassign_submission && $allow_resubmission)) {
  3005. // No submission OR action repeat (from scratch) OR (has submission and is allowed to edit it)
  3006. if (!$iassign_submission) {
  3007. $str_iassign_submission_id = '';
  3008. $str_iassign_submission_answer = '';
  3009. }
  3010. else {
  3011. $str_iassign_submission_id = $iassign_submission->id;
  3012. $str_iassign_submission_answer = $iassign_submission->answer;
  3013. }
  3014. if ($this->action != 'repeat') {
  3015. $repeat = "<a href='view.php?action=repeat&id=" . $id . "&userid_iassign=" . $USER->id . "&iassign_current="
  3016. . $this->activity->get_activity()->id . "&iassign_submission_current=" . $str_iassign_submission_id
  3017. . "'" . $repeat_title . ">" . iassign_icons::insert('repeat') . ' ' . get_string('repeat', 'iassign') . "</a>\n";
  3018. }
  3019. $this->allowPostButton = 1;
  3020. $this->write_solution = 1; // can register his submission
  3021. $student_answer = $str_iassign_submission_answer;
  3022. }
  3023. else { // if (!$iassign_submission || $this->action == 'repeat' || ($iassign_submission && $allow_resubmission))
  3024. // In 'class ilm : view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, $view)'
  3025. $last_iassign = get_string('last_iassign', 'iassign');
  3026. if ($iassign_submission) {
  3027. $repeat = "<a href='view.php?action=repeat&id=" . $id . "&userid_iassign=$USER->id&iassign_current=" . $this->activity->get_activity()->id .
  3028. "&iassign_submission_current=" . $iassign_submission->id . "'" . $repeat_title . ">" . iassign_icons::insert('repeat') . ' ' . get_string('repeat', 'iassign') . "</a>\n";
  3029. $student_answer = $iassign_submission->answer;
  3030. } else {
  3031. $repeat = "<a href='view.php?action=repeat&id=" . $id . "&userid_iassign=$USER->id&iassign_current=" . $this->activity->get_activity()->id .
  3032. "'" . $repeat_title . ">" . iassign_icons::insert('repeat') . ' ' . get_string('repeat', 'iassign') . "</a>\n";
  3033. }
  3034. }
  3035. } // elseif ($iassign_statement_activity_item->timedue > $time_now || $iassign_statement_activity_item->preventlate == 1)
  3036. elseif ($iassign_statement_activity_item->test == 1) { // allowed to test after expired due date
  3037. if ($this->action == 'repeat' || ($iassign_submission && $iassign_submission->experiment < 1)) {
  3038. $this->allowPostButton = 1;
  3039. $this->write_solution = 0; // if iVProg it is valid to the learner to edit previous solution
  3040. } else {
  3041. $last_iassign = get_string('last_iassign', 'iassign');
  3042. if ($iassign_submission) {
  3043. $repeat = "<a href='view.php?action=repeat&id=" . $id . "&userid_iassign=$USER->id&iassign_current=" . $this->activity->get_activity()->id .
  3044. "&iassign_submission_current=" . $iassign_submission->id . "'" . $repeat_title . ">" . iassign_icons::insert('repeat') . ' ' . get_string('repeat', 'iassign') . "</a>\n";
  3045. $student_answer = $iassign_submission->answer;
  3046. } else {
  3047. // Symbol of "redo activity"
  3048. $repeat = "<a href='view.php?action=repeat&id=" . $id . "&userid_iassign=$USER->id&iassign_current=" . $this->activity->get_activity()->id .
  3049. "'" . $repeat_title . ">" . iassign_icons::insert('repeat') . ' ' . get_string('repeat', 'iassign') . "</a>\n";
  3050. }
  3051. }
  3052. } // elseif ($iassign_statement_activity_item->test == 1)
  3053. elseif ($iassign_statement_activity_item->test == 0) {
  3054. $this->view_iassign = false;
  3055. }
  3056. } // elseif ($iassign_statement_activity_item->type_iassign == 3)
  3057. if ($iassign_statement_activity_item->timeavailable <= $time_now &&
  3058. ($iassign_statement_activity_item->timedue > $time_now || $iassign_statement_activity_item->preventlate==1)
  3059. ) { // 2023/05/30
  3060. // Enter if, and only if, exercise is AVAILABLE and ( ON TIME OR allow submission after due date )
  3061. // $iassign_statement_activity_item->preventlate == 0
  3062. // {iassign_statement} = id name iassignid type_iassign ... timedue preventlate test: preventlate==0 <=> do NOT allow submission after due date
  3063. // Prepare variable "$enderecoPOST" that is used in the JS code in /mod/iassign/ilm_handlers/html5.php
  3064. if ($iassign_submission)
  3065. $param_aux = "action=get_answer&iassign_submission_current=" . $iassign_submission->id . "&id=" . $id . "&iassign_current=" . $this->activity->get_activity()->id .
  3066. "&write_solution=" . $this->write_solution . "&userid_iassign=" . $USER->id;
  3067. else
  3068. $param_aux = "action=get_answer&id=" . $id . "&iassign_current=" . $this->activity->get_activity()->id . "&write_solution=" . $this->write_solution . "&userid_iassign=" . $USER->id;
  3069. // iLM: to be used to the iLM to where it must send the student answer
  3070. $enderecoPOST = "" . $CFG->wwwroot . "/mod/iassign/view.php?" . $param_aux;
  3071. }
  3072. else { // Otherwise, set address to process answer as empty
  3073. // ilm_handlers/html5.php!show_activity_in_ilm(... $enderecoPOST ...): will check this, if empty do NOT present "submission button" nor allow sending
  3074. $enderecoPOST = ""; // do NOT allow send if after due date
  3075. }
  3076. $this->activity->view_dates(); // prints "Type of activity:", "Available from:"
  3077. if ($this->view_iassign) { // Box 5 to 'proposition' - open
  3078. print $OUTPUT->box_start();
  3079. print '<table width=100% border=0 valign="top">' . "\n";
  3080. print '<tr><td width=60% valign="top">' . "\n";
  3081. $flag_dependency = true;
  3082. if ($iassign_statement_activity_item->type_iassign == 3) {
  3083. if ($iassign_statement_activity_item->dependency == 0) {
  3084. print '<strong>' . get_string('independent_activity', 'iassign') . '</strong>' . "\n";
  3085. } else {
  3086. $dependencys = explode(';', $iassign_statement_activity_item->dependency);
  3087. print '<p><strong>' . get_string('dependency', 'iassign') . '</strong></p>' . "\n";
  3088. foreach ($dependencys as $dependency) {
  3089. if ($dependency) {
  3090. $dependencyiassign = $DB->get_record("iassign_statement", array("id" => $dependency));
  3091. $dependencysubmissions = $DB->get_record("iassign_submission", array("iassign_statementid" => $dependencyiassign->id, 'userid' => $USER->id));
  3092. if ($dependencysubmissions) {
  3093. if ($dependencysubmissions->status == 3)
  3094. $icon = iassign_icons::insert('correct');
  3095. elseif ($dependencysubmissions->status == 2) {
  3096. $icon = iassign_icons::insert('incorrect');
  3097. $flag_dependency = false;
  3098. } elseif ($dependencysubmissions->status == 1) {
  3099. $icon = iassign_icons::insert('post');
  3100. $flag_dependency = false;
  3101. } elseif ($dependencysubmissions->status == 0) {
  3102. $icon = iassign_icons::insert('not_post');
  3103. $flag_dependency = false;
  3104. }
  3105. } else {
  3106. $icon = iassign_icons::insert('not_post');
  3107. $flag_dependency = false;
  3108. } // if ($dependencysubmissions)
  3109. print '<p> ' . $icon . $dependencyiassign->name . '</p>' . "\n";
  3110. } // if ($dependency)
  3111. } // foreach ($dependencys as $dependency)
  3112. } // if ($iassign_statement_activity_item->dependency == 0)
  3113. } // if ($iassign_statement_activity_item->type_iassign == 3)
  3114. if ($flag_dependency == false) {
  3115. print '<strong>' . get_string('message_dependency', 'iassign') . '</strong>.' . "\n";
  3116. $this->view_iassign = false;
  3117. print '</tr></table>' . "\n";
  3118. }
  3119. else {
  3120. $this->view_iassign = true;
  3121. print '</td>' . "\n";
  3122. } // if ($flag_dependency == false)
  3123. if ($this->view_iassign) { // it is already inside 'if ($this->view_iassign)' but the dependency check could had change its value (above)
  3124. if ($iassign_statement_activity_item->type_iassign == 3) { // activity is present only if exercise
  3125. // receiver=1 - message to teacher
  3126. // receiver=2 - message to student
  3127. if ($iassign_submission) {
  3128. $verify_message = $DB->get_record('iassign_submission_comment', array('iassign_submissionid' => $iassign_submission->id, 'return_status' => 0, 'receiver' => 2));
  3129. if ($verify_message)
  3130. $comment = iassign_icons::insert('comment_unread');
  3131. }
  3132. print '<td width=40% valign="top" align="left">';
  3133. print '<strong>' . get_string('status', 'iassign') . '</strong>' . "\n";
  3134. if ($iassign_statement_activity_item->show_answer == 1) {
  3135. // Check status of solution sent by the student:
  3136. if ($iassign_submission) {
  3137. switch ($iassign_submission->status) { // {iassign_submission}.status = 3 => 'correct'; 2 => 'incorrect'; 1 => 'post'; 0 => 'not_post'
  3138. case 3 :
  3139. print iassign_icons::insert('correct') . ' ' . get_string('correct', 'iassign') . ' ' . $comment;
  3140. break;
  3141. case 2 :
  3142. print iassign_icons::insert('incorrect') . ' ' . get_string('incorrect', 'iassign') . ' ' . $comment;
  3143. break;
  3144. case 1 :
  3145. print iassign_icons::insert('post') . ' ' . get_string('post', 'iassign') . ' ' . $comment;
  3146. break;
  3147. default :
  3148. print iassign_icons::insert('not_post') . ' ' . get_string('not_post', 'iassign') . ' ' . $comment;
  3149. $repeat = "";
  3150. $last_iassign = "";
  3151. } // switch ($iassign_submission->status)
  3152. } else {
  3153. print iassign_icons::insert('not_post') . ' ' . get_string('not_post', 'iassign') . ' ' . $comment;
  3154. $repeat = "";
  3155. $last_iassign = "";
  3156. }
  3157. if ($iassign_submission && $iassign_submission->experiment > 0) {
  3158. // {iassign_statement}.grade = float representing the maximum score in the activity
  3159. // {iassign_submission}.grade = float representing the student real grade (must be <= {iassign_statement}.grade)
  3160. // {iassign_submission}.grade = grade_from_iLM*{iassign_statement}.grade (grade_from_iLM must be a float value in [0,1])
  3161. // Messages: grade_max='Maximum score' ; grade='Grade activity'
  3162. if ($iassign_submission->grade)
  3163. $student_grade = $iassign_submission->grade;
  3164. else
  3165. $student_grade = 0; // security
  3166. if ($iassign_statement_activity_item->grade) // prevent max grade indefined => zero
  3167. $ia_max_grade = $iassign_statement_activity_item->grade; // get max grade in {iassign_statement}
  3168. else // If 'else' is executed, the {iassign_statement}.grade must be fixed by the teacher!
  3169. $ia_max_grade = 10; // security: if {iassign_statement}.grade empty, set the default 10...
  3170. //D echo "locallib.php!view_iassign_current(): ia_max_grade=" . $ia_max_grade . ", student_grade=" . $student_grade . "<br/>";
  3171. // To prevent changes provoking errors: probably iLM send wrong value (by evaluation button?)...
  3172. if ($student_grade > $ia_max_grade) $msg_grade = $ia_max_grade;
  3173. else $msg_grade = $student_grade;
  3174. print '<p><strong>' . get_string('grade', 'iassign') . '</strong> ' . $msg_grade; // "Grade activity:"
  3175. print ' (' . get_string('grade_max', 'iassign') . ': ' . $iassign_statement_activity_item->grade . ')</p>' . "\n";
  3176. print '<p><strong>' . get_string('experiment_student', 'iassign') . '</strong> ' . $iassign_submission->experiment;
  3177. if ($iassign_statement_activity_item->max_experiment == 0)
  3178. print ' (' . get_string('experiment_iassign', 'iassign') . ' ' . get_string('ilimit', 'iassign') . ')</p>' . "\n";
  3179. else {
  3180. print ' (' . get_string('experiment_iassign', 'iassign') . ' ' . $iassign_statement_activity_item->max_experiment . ')</p>' . "\n";
  3181. if ($iassign_submission->experiment >= $iassign_statement_activity_item->max_experiment) {
  3182. $repeat = "";
  3183. $last_iassign .= " <font color=red>" . get_string('attempts_exhausted', 'iassign') . '</font>' . "\n";
  3184. $this->allowPostButton = 0;
  3185. $this->write_solution = 0;
  3186. }
  3187. } // else if ($iassign_statement_activity_item->max_experiment == 0)
  3188. print '<p><strong>' . get_string('timemodified', 'iassign') . '</strong> ' . userdate($iassign_submission->timemodified) . '</p>' . "\n";
  3189. $teacher = $DB->get_record("user", array('id' => $iassign_submission->teacher));
  3190. if ($teacher)
  3191. print '<p><strong>' . get_string('last_modification', 'iassign') . '</strong> ' . $teacher->firstname . '</p>' . "\n";
  3192. } // if ($iassign_submission && $iassign_submission->experiment > 0)
  3193. } // if ($iassign_statement_activity_item->show_answer==1)
  3194. else {
  3195. if (!isset($iassign_submission) || $iassign_submission->status == 0) {
  3196. print iassign_icons::insert('not_post') . ' ' . get_string('not_post', 'iassign') . ' ' . $comment;
  3197. $repeat = "";
  3198. $last_iassign = "";
  3199. } elseif ($iassign_submission->status == 1) {
  3200. print iassign_icons::insert('post') . ' ' . get_string('post', 'iassign') . ' ' . $comment;
  3201. }
  3202. }
  3203. print '</td>';
  3204. } // if ($iassign_statement_activity_item->type_iassign == 3)
  3205. print '</tr></table>' . "\n";
  3206. // Presents the iLM
  3207. print '<table width=100% border=0 valign="top">' . "\n";
  3208. print '<td width=80% align="left">';
  3209. print '<strong>' . $last_iassign . ' ' . get_string('repeat_msg', 'iassign') . '</strong></td>' . "\n"; // If you want to do this activity from the beginning again, use the \"Redo button\".
  3210. print '<td width=20% align="rigth">';
  3211. //D $ilm_name = strtolower($this->ilm->name); //if (substr($ilm_name, 0, 5) == "igeom") ; // is iGeom exercise
  3212. print $repeat; // symbol of "redo activity"
  3213. print '</td></tr></table>' . "\n";
  3214. //2019 print $OUTPUT->box_end(); // closing Box 5 but sometimes it does reach this point - see it at the end of 'if ($this->view_iassign)'
  3215. print activity::toggle_columns_script();
  3216. print '<p class="mt-5"><strong>' . get_string('proposition', 'iassign') . '</strong></p>' . "\n";
  3217. print '<div class="proposition">' . $iassign_statement_activity_item->proposition . '</div>' . "\n";
  3218. $output = $OUTPUT->box_start();
  3219. $USER->iassignEdit = $this->allowPostButton; // To be used by "./mod/iassign/ilm_handers/*.php"
  3220. //leo Re-submission
  3221. //D echo " * locallib.php: view_iassign_current(): !iassign_submission : action=" . $this->action . ", answer=" . $iassign_submission->answer . "<br/>\n"; //leo
  3222. // view_iLM: $retorno = $typec::show_activity_in_ilm($iassign_statement_activity_item, $student_answer, $enderecoPOST, $view_teacherfileversion);
  3223. // ---
  3224. // Presents the iLM
  3225. // Prepare tags to present the iLM: it use 'ilm_handlers/<{iassign_statement}.type>.php' typec::show_activity_in_ilm(.)
  3226. if (!$iassign_submission || $this->action == 'repeat') { // or $iassign_submission->answer==0
  3227. $student_answer = ""; // when student never answer this exercise reaches this point
  3228. $output .= $ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, true); // presents iLM and true => see the teacher file
  3229. } elseif ($iassign_submission && $iassign_submission->answer == '0') {
  3230. $student_answer = "";
  3231. $output .= $ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, true); // presents iLM
  3232. } else {
  3233. // When student is redoing his activity
  3234. // ATTENTION: exception used by iGeom (exercise with "script")
  3235. $loadTeacherActivity = false; // trocar para 'true' se 'special_param1 == 1'
  3236. if (substr($ilm_name, 0, 5)=="igeom") {
  3237. if ($iassign_statement_activity_item->special_param1 == 1) // if 1 => use the teacher activity with some complement from the student (in iGeom = GEO + SCR)
  3238. $loadTeacherActivity = true;
  3239. }
  3240. // Will calls "./ilm_handlers/html5.php!show_activity_in_ilm(.)"
  3241. $output .= $ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, $loadTeacherActivity); // presents iLM
  3242. }
  3243. //1 Desse modo entre area para comentario, mas nao seu botao separado - deixar para o final com botao!
  3244. //1 if ($iassign_statement_activity_item->type_iassign == 3) {
  3245. //1 $history_comment = '';
  3246. //1 if ($iassign_submission) { $history_comment = $this->search_comment_submission($iassign_submission->id); }
  3247. //1 if (!empty($history_comment))
  3248. //1 $output .= "\n <table id='outlinetable' class='generaltable boxaligncenter' cellpadding='5' width='100%'>\n" .
  3249. //1 " <tr><th>" . get_string('history_comments', 'iassign') . "</th></tr>\n" . $history_comment . "</table>\n";
  3250. //1 $output .= "</form></center>\n";
  3251. //1 $only_one_send_button = 1; // avoid put comment frame again (bellow)
  3252. //1 }
  3253. $output .= $OUTPUT->box_end();
  3254. print $output;
  3255. } // if ($this->view_iassign)
  3256. print $OUTPUT->box_end(); // Box 5 to 'proposition' - close
  3257. } // if ($this->view_iassign)
  3258. } // elseif (has_capability('mod/iassign:submitiassign', $USER->context, $USER->id))
  3259. else
  3260. if (isguestuser()) { // else of elseif (has_capability('mod/iassign:submitiassign', $USER->context, $USER->id))
  3261. print($OUTPUT->notification(get_string('no_permission_iassign', 'iassign'), 'notifyproblem'));
  3262. print '<table width=100% border=0 valign="top">' . "\n";
  3263. print '<tr><td width=60% valign="top">' . "\n";
  3264. print '<strong>' . get_string('proposition', 'iassign') . '</strong>' . "\n";
  3265. print '<p>' . $iassign_statement_activity_item->proposition . '</p>' . "\n";
  3266. print '</tr></table>' . "\n";
  3267. $student_answer = "";
  3268. $enderecoPOST = "";
  3269. // Prepare tags to present the iLM
  3270. $output = $ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, true);
  3271. print $output;
  3272. }
  3273. //2020 Frame with area to send and read comments (in this point to allow everyone read)
  3274. // $only_one_send_button is to avoid to put 2 copies of comments (area to send and see comments between teacher and student)
  3275. if ($only_one_send_button==0 && $iassign_statement_activity_item->type_iassign == 3) { // type_iassign=3 => activity of type "exercise" - submit button and automatic evaluation
  3276. // Put a block with area to sent/answer comments
  3277. $output = '';
  3278. $history_comment = '';
  3279. $row = optional_param('row', 0, PARAM_INT);
  3280. $column = optional_param('column', 0, PARAM_INT);
  3281. if ($iassign_submission) { // @calls function action(): with
  3282. $enderecoPOSTcomment = "" . $CFG->wwwroot . "/mod/iassign/view.php?id=" . $id . "&action=newcomment&iassign_current=" . $this->activity->get_activity()->id .
  3283. "&iassign_submission_current=" . $iassign_submission->id . "&userid_iassign=" . $this->userid_iassign . "&row=" . ($row) . "&column=" . ($column);
  3284. $history_comment = $this->search_comment_submission($iassign_submission->id);
  3285. }
  3286. else {
  3287. $enderecoPOSTcomment = "" . $CFG->wwwroot . "/mod/iassign/view.php?id=" . $id . "&action=newcomment&iassign_current=" . $this->activity->get_activity()->id .
  3288. "&userid_iassign=" . $this->userid_iassign . "&row=" . ($row) . "&column=" . ($column);
  3289. }
  3290. $output .= $OUTPUT->box_start();
  3291. //if (!empty($history_comment)) {
  3292. $output .= "<a id='comments'></a>";
  3293. $output .= " <table id='outlinetable' class='generaltable boxaligncenter' cellpadding='5' width='100%'> \n";
  3294. $output .= " <tr><th><i style='font-size: 20px' class='icon fa fa-comments-o'></i>" . get_string('history_comments', 'iassign') . "</th></tr>";
  3295. $output .= "</table>";
  3296. print " <script type='text/javascript'>
  3297. //<![CDATA[
  3298. function submit_comment (url) {
  3299. console.log('submit_comment(url): url=' + url); //DEBUG
  3300. if (document.formEnvioComment.submission_comment.value.length < 1) {
  3301. console.log('submit_comment(url): Error 1 - document.formEnvioComment.submission_comment.value.length<1');
  3302. return;
  3303. }
  3304. var formData = new FormData();
  3305. formData.append('submission_comment', document.formEnvioComment.submission_comment.value);
  3306. var request = new XMLHttpRequest();
  3307. request.open('POST', url);
  3308. document.getElementById('comments_history').innerHTML = '';
  3309. request.onload = function (e) {
  3310. if (request.readyState === 4) {
  3311. if (request.status === 200) {
  3312. document.getElementById('comments_history').innerHTML = request.responseText;
  3313. document.getElementById('check-message-success').style.visibility = 'visible';
  3314. setTimeout(function(){
  3315. document.getElementById('check-message-success').style.visibility = 'hidden';
  3316. }, 3000);
  3317. }
  3318. }
  3319. };
  3320. document.formEnvioComment.submission_comment.value = '';
  3321. request.send(formData);
  3322. }
  3323. function event_text_area (event) {
  3324. console.log('event_text_area(event): enderecoPOSTcomment= + " . $enderecoPOSTcomment . "'); //DEBUG
  3325. if (event.ctrlKey && event.keyCode == 13)
  3326. submit_comment('" . $enderecoPOSTcomment . "');
  3327. }
  3328. //]]>
  3329. </script>";
  3330. $output .= "<center><form name='formEnvioComment' id='formEnvioComment1' enctype='multipart/form-data'>\n";
  3331. $output .= "<br><p><textarea class='form-control w-50' rows='2' cols='60' name='submission_comment' onkeypress='event_text_area(event)'></textarea></p>\n";
  3332. $output .= "<p><button class='btn btn-primary' onclick=\"submit_comment('" . $enderecoPOSTcomment . "')\" type=button value='" . get_string('submit_comment', 'iassign') . "'>".get_string('submit_comment', 'iassign')."</button> <i id='check-message-success' class='fa fa-check' aria-hidden='true' style='position: absolute; color: green; font-size: 2.0rem; margin-left: 1rem; visibility: hidden;'></i> </p> \n";
  3333. $output .= "</form> </center>\n";
  3334. $output .= "<table id='comments_history' class='generaltable boxaligncenter' cellpadding='5' width='100%'>".$history_comment."</table>";
  3335. // }
  3336. $output .= $OUTPUT->box_end();
  3337. print $output;
  3338. } // if ($iassign_statement_activity_item->type_iassign == 3)
  3339. // final block 'studant'
  3340. print $OUTPUT->footer();
  3341. die();
  3342. } // function view_iassign_current()
  3343. /// Export a survey of all the student submissions in the current course into CSV file format
  3344. // Each student will have one line with:
  3345. // student_id, student_name, [followed by 7 columns to each student submissions]
  3346. // The 7 columns are: activity_id_i, activity_name_i, total_submissions_activity_i, grade_activity_i, status_activity_i, ilm_id_activity_i, ilm_name_activity_i
  3347. function export_csv () {
  3348. global $USER, $CFG, $DB, $OUTPUT;
  3349. $SEP = ';'; // CSV separator
  3350. $str_csv_content = // string to be used with CSV text
  3351. $SEP . "Attention field 'answer' is encoded with PHP function 'base64_encode(.)'.\n" .
  3352. $SEP . "To process it by Python, you can use package 'base64' and function 'base64.b64decode(your_string)'.\n";
  3353. // Finding all {iassign_statement} for the current iAssign block {iassign}:
  3354. $fields = "student_id" . $SEP . "student_name";
  3355. $id = $this->cm->id;
  3356. $iassign_list = $DB->get_records_list('iassign_statement', 'iassignid', array('iassignid' => $this->iassign->id), "position ASC");
  3357. $c = 1;
  3358. foreach ($iassign_list as $iassign) {
  3359. $fields .= $SEP . " activity_id_" . $c . $SEP . "activity_name_" . $c . $SEP . "total_submissions_activity_" . $c .
  3360. $SEP . "grade_activity_" . $c . "" . $SEP . "status_activity_" . $c . $SEP . "ilm_id_activity_" . $c .
  3361. $SEP . "ilm_name_activity_" . $c . $SEP . "timecreated_" . $c . $SEP . "timemodified_" . $c . $SEP . "answer_" . $c;
  3362. $c++;
  3363. }
  3364. $str_csv_content .= $fields . "\n";
  3365. // Find all enrolled students:
  3366. $params_temp = array('shortname' => 'student');
  3367. $role = $DB->get_record_sql("SELECT s.id, s.shortname FROM {role} s WHERE s.shortname = :shortname", $params_temp);
  3368. $context = context_course::instance($this->course->id);
  3369. $params_temp = array('contextid' => $context->id, 'roleid' => $role->id);
  3370. $sql_query = "SELECT s.userid, a.firstname, a.lastname FROM {role_assignments} s, {user} a " .
  3371. " WHERE s.contextid = :contextid AND s.userid = a.id AND s.roleid = :roleid " .
  3372. " ORDER BY a.firstname ASC,a.lastname ASC";
  3373. $students = $DB->get_records_sql($sql_query, $params_temp);
  3374. // Now is exported each answer submited by each student
  3375. foreach ($students as $student) { // Consider each student $student in the course
  3376. $str_csv_content .= $student->userid . $SEP;
  3377. $str_csv_content .= '"' . $student->firstname . ' ' . $student->lastname . '"' . $SEP;
  3378. foreach ($iassign_list as $iassign) { // Consider each submission to this student $student->userid
  3379. $str_csv_content .= $iassign->id . $SEP;
  3380. $str_csv_content .= '"' . $iassign->name . '"' . $SEP;
  3381. $student_submissions = $DB->get_record("iassign_submission", array('iassign_statementid' => $iassign->id, 'userid' => $student->userid));
  3382. //D if ($student->userid==52) { echo "Leonardo: "; print_r($student_submissions); echo "<br/>"; }
  3383. if ($student_submissions) {
  3384. $str_csv_content .= $student_submissions->experiment . $SEP;
  3385. $str_csv_content .= $student_submissions->grade . $SEP;
  3386. switch ($student_submissions->status) { // {iassign_submission}.status = 3 => 'correct'; 2 => 'incorrect'; 1 => 'post'; 0 => 'not_post'
  3387. case 3:
  3388. $str_csv_content .= '"correct"' . $SEP;
  3389. break;
  3390. case 2:
  3391. $str_csv_content .= '"incorrect"' . $SEP;
  3392. break;
  3393. case 1:
  3394. $str_csv_content .= '"post"' . $SEP;
  3395. break;
  3396. case 0:
  3397. $str_csv_content .= '"not_post"' . $SEP;
  3398. break;
  3399. }
  3400. }
  3401. else {
  3402. $str_csv_content .= $SEP . $SEP . '"not_post"' . $SEP;
  3403. }
  3404. $str_csv_content .= $iassign->iassign_ilmid . $SEP;
  3405. $ilm_activity = $DB->get_record("iassign_ilm", array('id' => $iassign->iassign_ilmid));
  3406. if (is_null($ilm_activity) || !isset($ilm_activity->name)) ; //leo $str_csv_content .= '{iassign_ilm}.id=' . $iassign->iassign_ilmid . ' empty!';
  3407. else $str_csv_content .= '"' . $ilm_activity->name . '"';
  3408. if ($student_submissions) { // Also register fields: timecreated, timemodified, answer
  3409. $str_csv_content .= $SEP . $student_submissions->timecreated . $SEP . $student_submissions->timemodified . $SEP .
  3410. base64_encode($student_submissions->answer) . $SEP;
  3411. }
  3412. else $str_csv_content .= $SEP . $SEP . $SEP . $SEP;
  3413. } // foreach ($iassign_list as $iassign)
  3414. $str_csv_content .= "\n";
  3415. }
  3416. header("Content-type: text/csv");
  3417. header("Content-disposition: attachment; filename=report.csv");
  3418. // header("Content-type: text"); //D export as text
  3419. // header("Content-disposition: attachment; filename=report.txt"); //D export as text
  3420. header("Pragma: no-cache");
  3421. header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
  3422. header('Content-Length: ' . strlen($str_csv_content));
  3423. header('Connection: close');
  3424. print $str_csv_content;
  3425. flush();
  3426. } // function export_csv()
  3427. /// Print the teachers' report (of one iAssign block {iassign}), when selected: report='Reports' | home='Home'
  3428. // Page with all activities {iassign_statement} in iAssign block {iassign}: {iassign_submission} to each student
  3429. function report () {
  3430. global $USER, $CFG, $DB, $OUTPUT;
  3431. $id = $this->cm->id;
  3432. // Get all activities in this block
  3433. // Table 'iassign_statement' with: id; name; iassignid; type_iassign; proposition; author_name; author_modified_name; iassign_ilmid; file;
  3434. // grade; timemodified; timecreated; timeavailable; timedue; preventlate; test; special_param1; position; visible; max_experiment;
  3435. // dependency; automatic_evaluate; show_answer; filesid; store_all_submissions
  3436. // $iassign_list[i]->type_iassign = 1 => example (not even register student's submission); 2 => test (register); 3 => (register and evaluate)
  3437. $temporary_iassign_list = $DB->get_records_list('iassign_statement', 'iassignid', array('iassignid' => $this->iassign->id), "position ASC");
  3438. // Adjust $one_iassign_statement->position setting type_iassign==2 in the last position
  3439. $list_exerc = array(); // temporary list to exercises (type_iassign==3)
  3440. $list_test = array(); // temporary list to tests (type_iassign==2)
  3441. foreach ($temporary_iassign_list as $one_iassign_statement) { // count exercises and tests
  3442. if ($one_iassign_statement->type_iassign==2) $list_test[] = $one_iassign_statement;
  3443. else
  3444. if ($one_iassign_statement->type_iassign==3) $list_exerc[] = $one_iassign_statement;
  3445. }
  3446. $iassign_list = array();
  3447. foreach ($list_exerc as $one_iassign_statement) { // start with exercises
  3448. $iassign_list[] = $one_iassign_statement;
  3449. }
  3450. foreach ($list_test as $one_iassign_statement) { // finish with tests
  3451. $iassign_list[] = $one_iassign_statement;
  3452. }
  3453. if ($this->action != 'print') {
  3454. $title = get_string('report', 'iassign');
  3455. print $OUTPUT->header();
  3456. } // if ($this->action != 'print')
  3457. $this->print_main_menu(); // line with other options: activities_page ; help_ilm ; report ; results ; auto_evaluate_name_config ; notifications ; add_iassign
  3458. print $OUTPUT->box_start();
  3459. $this->view_legend_icons();
  3460. print $OUTPUT->box_end();
  3461. if ($this->action == 'print')
  3462. print '<table border=1 width="100%">' . "\n";
  3463. else
  3464. print '<table id="outlinetable" class="generaltable boxaligncenter table table-hover table-striped mt-5" width="100%">' . "\n";
  3465. print '<tr><th colspan=3 class="header c1">' . iassign_utils::remove_code_message($this->iassign->name) . '</th></tr>' . "\n";
  3466. // Prepare the list of all {iassign_statement} in this iAssign block {iassign}: number; due date; exercise statement ({iassign_statement}.name)
  3467. $i = 1; // to exercises 'students_submissions[]'
  3468. $students_submissions = array(); // array indexed by student index with all submissions (to all activities)
  3469. $array_type_iassign = array(); // array with all exercises and tests: $array_type_iassign[$i]==true => type_iassign=3
  3470. $stylebutton = "background: none; -webkit-box-shadow: 2px 2px 5px 0px rgba(0,0,0,0.75);-moz-box-shadow: 2px 2px 5px 0px rgba(0,0,0,0.75);box-shadow: 2px 2px 5px 0px rgba(0,0,0,0.75); min-width: 3.5rem;";
  3471. foreach ($iassign_list as $one_iassign_statement) { // for all exercise in this block, print its information: number; due date; exercise statement
  3472. $test_exercise = "";
  3473. // Get all submissions to this activity (all students)
  3474. $iassign_submission = $DB->get_records("iassign_submission", array("iassign_statementid" => $one_iassign_statement->id));
  3475. if (($iassign_submission) && $one_iassign_statement->type_iassign < 3) { // Activity type not any more 'exercise' (however has submission)
  3476. // Since 2025/06/27 we also presents type_iassign=2 (tests), so this message dos not make sense any more
  3477. // $test_exercise = " <b>(" . get_string('iassign_exercise', 'iassign') . ")</b>"; // 'Activity has been exercise'
  3478. }
  3479. if ($one_iassign_statement->type_iassign > 1) { // 3: is 'exercise' => has evaluation grade
  3480. $students_submissions[$i] = new stdClass();
  3481. $students_submissions[$i]->name = $one_iassign_statement->name;
  3482. $students_submissions[$i]->id = $i;
  3483. $students_submissions[$i]->iassignid = $one_iassign_statement->id;
  3484. $students_submissions[$i]->timedue = $one_iassign_statement->timedue;
  3485. if ($one_iassign_statement->type_iassign == 3) { // 3: is 'exercise' => has evaluation grade
  3486. $students_submissions[$i]->grade = $one_iassign_statement->grade;
  3487. $array_type_iassign[$i] = true; // $array_type_iassign[$i]==true => type_iassign=3
  3488. $msg_grade = "max grade=" . $one_iassign_statement->grade;
  3489. }
  3490. else {
  3491. $array_type_iassign[$i] = false; // $array_type_iassign[$i]==false => type_iassign=2, is test
  3492. $msg_grade = "test has no grade";
  3493. }
  3494. $students_submissions[$i]->total_answers = 0; // initialize the total of answers to this activity
  3495. print ' <tr >' . "\n";
  3496. print " <td class='cell c1 numviews' width='10px' align='center'><strong>" . $students_submissions[$i]->id . "</strong></td>\n";
  3497. print " <td class='cell c1' width='180px' title='" . get_string('duedate', 'iassign') . "'> " . date("d/m/Y H:i",$students_submissions[$i]->timedue) . " </td>\n";
  3498. print " <td class='cell c0 actvity' title='" . $msg_grade . "'> " . $students_submissions[$i]->name . ' ' . $test_exercise . " </td>\n";
  3499. print ' </tr>' . "\n";
  3500. $i++;
  3501. } // if ($one_iassign_statement->type_iassign == 3)
  3502. } // foreach ($iassign_list as $one_iassign_statement)
  3503. $num_exerc = $i; // total of exercises + tests + 1
  3504. print "</table>\n";
  3505. print "<p></p>\n";
  3506. // Reduction over 'btn' in Boost ./theme/boost/style/moodle.css:btn
  3507. print "<style>.btnLInE {width: 75px;line-height: 1.5; padding: 0.275rem 0.35rem; border-radius: 0.5rem; ";
  3508. print "transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;}</style>\n";
  3509. if ($this->action == 'print')
  3510. print '<table border=1 width="100%">' . "\n";
  3511. else // ./theme/boost/style/moodle.css: .generaltable
  3512. print '<table id="outlinetable" class="generaltable boxaligncenter table table-hover table-striped table-bordered" cellpadding="5" width="100%">' . "\n";
  3513. $context = context_course::instance($this->course->id);
  3514. if ($num_exerc > 1) {
  3515. // $role = $DB->get_record_sql("SELECT s.id, s.shortname FROM {$CFG->prefix}role s WHERE s.shortname = 'student'");
  3516. $params_temp = array('shortname' => 'student');
  3517. $role = $DB->get_record_sql("SELECT s.id, s.shortname FROM {role} s WHERE s.shortname = :shortname", $params_temp);
  3518. // $students = $DB->get_records_sql("SELECT s.userid, a.firstname, a.lastname FROM {$CFG->prefix}role_assignments s, {$CFG->prefix}user a WHERE s.contextid = '$context->id' AND s.userid = a.id AND s.roleid = '$role->id' ORDER BY a.firstname ASC,a.lastname ASC");
  3519. $params_temp = array('contextid' => $context->id, 'roleid' => $role->id);
  3520. $students = $DB->get_records_sql(
  3521. "SELECT s.userid, a.firstname, a.lastname FROM {role_assignments} s, {user} a " .
  3522. " WHERE s.contextid = :contextid AND s.userid = a.id AND s.roleid = :roleid " .
  3523. " ORDER BY a.firstname ASC,a.lastname ASC", $params_temp);
  3524. print '<tr><th class="header c1">' . get_string('students', 'iassign') . '</th>' . "\n";
  3525. $width = 85/$num_exerc; // width to each column
  3526. for ($j=1; $j<$num_exerc; $j++) { // all exercises on this block: initialize the counter of correct answers
  3527. $sum_iassign_correct[$j] = 0;
  3528. print '<th class="header c1" scope="col" style="width:' . $width . '%; text-align: center;">' . $students_submissions[$j]->id . '</th>' . "\n";
  3529. // <th class="header c1" scope="col">
  3530. }
  3531. print '<th class="header c1" width=5%> ' . get_string('functions', 'iassign') . '</th>';
  3532. $sum_iassign = $j - 1;
  3533. print '</tr>' . "\n";
  3534. $total = 0;
  3535. $sum_student = 0;
  3536. $comment = iassign_icons::insert('comment_read');
  3537. $sum_comment = 0;
  3538. $sum_correct_iassign = array(); //NOT used
  3539. $sum_correct_student = array(); //NOT used
  3540. $USER->matrix_iassign = array();
  3541. if ($students) {
  3542. $total_students = 0;
  3543. foreach ($students as $tmp) { // count students
  3544. $users_array[$total_students] = $tmp;
  3545. $total_students++;
  3546. }
  3547. print "<strong>" . get_string('report_total_students', 'iassign') . "</strong>: " . $total_students . "<br/>"; // 'Total of students in class'
  3548. for ($student_index=0; $student_index<$total_students; $student_index++) { // for all students
  3549. print '<tr>' . "\n";
  3550. $sum_student++;
  3551. $name = $users_array[$student_index]->firstname . ' ' . $users_array[$student_index]->lastname;
  3552. print ' <td style="vertical-align: middle;">' . $name . '</td>' . "\n";
  3553. $total_in_activity = 0;
  3554. $tentativas = 0;
  3555. for ($j=1; $j<$num_exerc; $j++) { // for all exercise
  3556. $sum_comment = $sum_verify_message = 0;
  3557. // Now the last submission to this activity of this student
  3558. $student_submissions = $DB->get_record("iassign_submission", array('iassign_statementid' => $students_submissions[$j]->iassignid, 'userid' => $users_array[$student_index]->userid)); // data about student solution
  3559. if ($student_submissions) {
  3560. $students_submissions[$j]->total_answers += 1; // one more submission to this activity
  3561. $msg_grade = $student_submissions->grade . " &Element; [0,1]"; // &Element; = bellong symbol
  3562. if ($array_type_iassign[$j] && $students_submissions[$j]->grade) // activity has grade associated
  3563. $msg_grade .= " => " . $students_submissions[$j]->grade * $student_submissions->grade;
  3564. $last_solution_submission = " title='" . userdate($student_submissions->timemodified) . ", " . $msg_grade . "' "; // timemodified: time of the last student solution
  3565. $tentativas = $student_submissions->experiment;
  3566. // Get student comments
  3567. // $student_submissions_comment = $DB->get_record_sql("SELECT COUNT(iassign_submissionid) FROM {$CFG->prefix}ia_assign_submissions_comment WHERE iassign_submissionid = '$student_submissions->id'");
  3568. $params_temp = array('iassign_submissionid' => $student_submissions->id);
  3569. $student_submissions_comment = $DB->get_record_sql(
  3570. "SELECT COUNT(iassign_submissionid) FROM {iassign_submission_comment} " .
  3571. "WHERE iassign_submissionid = :iassign_submissionid", $params_temp);
  3572. if ($student_submissions_comment) // student has comment
  3573. foreach ($student_submissions_comment as $tmp)
  3574. $sum_comment = $tmp;
  3575. // Informations to allow teacher to browse previous activities (after enter in one specific student solution)
  3576. if ($j - 1 < 1 || $j == $num_exerc) $iassign_previous = "-1";
  3577. else $iassign_previous = $students_submissions[$j - 1]->iassignid;
  3578. if ($student_index - 1 < 0 || $student_index == $total_students) $user_previous = "-1";
  3579. else $user_previous = $users_array[$student_index - 1]->userid;
  3580. // next
  3581. if ($i - 1 > $j) $iassign_next = $students_submissions[$j + 1]->iassignid;
  3582. else $iassign_next = "-1";
  3583. if ($total_students - 1 > $student_index) $user_next = $users_array[$student_index + 1]->userid;
  3584. else $user_next = "-1";
  3585. $url = "" . $CFG->wwwroot . "/mod/iassign/view.php?action=viewsubmission&id=" . $id . "&iassign_submission_current=" . $student_submissions->id . "&userid_iassign=" .
  3586. $users_array[$student_index]->userid . "&iassign_current=" . $students_submissions[$j]->iassignid . "&view_iassign=" . $this->view_iassign . "&row=" . $student_index . "&column=" . $j;
  3587. // receiver=1 - message to teacher
  3588. // receiver=2 - message to student
  3589. // $verify_message = $DB->get_record_sql("SELECT COUNT(iassign_submissionid) FROM {$CFG->prefix}ia_assign_submissions_comment " .
  3590. // "WHERE iassign_submissionid = '$student_submissions->id' AND return_status='0' AND receiver='1'");
  3591. $params_temp = array('iassign_submissionid' => $student_submissions->id, 'return_status' => '0', 'receiver' => '1');
  3592. $verify_message = $DB->get_record_sql(
  3593. "SELECT COUNT(iassign_submissionid) FROM {iassign_submission_comment} WHERE " .
  3594. "iassign_submissionid = :iassign_submissionid AND return_status= :return_status AND receiver= :receiver", $params_temp);
  3595. if ($verify_message) { foreach ($verify_message as $tmp) $sum_verify_message = $tmp; }
  3596. if ($sum_verify_message > 0) $comment = '<i class="fa fa-comments" style="color: #658af7; font-size: 1.4rem;" aria-hidden="true"></i>';
  3597. else $comment = '<i class="fa fa-comments" style="color: gray; font-size: 1.3rem;" aria-hidden="true"></i>';
  3598. if ($student_submissions->status == 3) { // {iassign_submission}.status = 3 => 'correct'; 2 => 'incorrect'; 1 => 'post'; 0 => 'not_post'
  3599. $sum_iassign_correct[$j]++;
  3600. $total_in_activity++;
  3601. $feedback = '<i class="fa fa-check" style="color: green; font-size: 1.2rem;" aria-hidden="true""></i>';
  3602. }
  3603. elseif ($student_submissions->status == 2) {
  3604. $feedback = '<i class="fa fa-times" style="color: red; font-size: 1.2rem;" aria-hidden="true"></i>';
  3605. }
  3606. elseif ($student_submissions->status == 1) {
  3607. $feedback = '<i class="fa fa-question" style="color: #77229c; font-size: 1.2rem;" aria-hidden="true"></i>';
  3608. }
  3609. elseif ($student_submissions->status == 0) {
  3610. $feedback = '<i class="fa fa-question" style="color: #77229c; font-size: 1.2rem;" aria-hidden="true"></i>';
  3611. }
  3612. // Using theme "boost": 'btn-secondary' is here "/theme/boost/style/moodle.css"
  3613. if ($tentativas > 0)
  3614. print '<td style="text-align: center;"> <a href="' . $url . '" ' . $last_solution_submission . '>'
  3615. . '<div type="button1" class="btnLInE btn-secondary" style="' . $stylebutton . '">' . $feedback
  3616. . '<span class="badge badge-light" style="font-size: 1rem; margin-left: .5rem;"> '. $tentativas
  3617. . '</span> </div>'. '</a>' . "\n"; //rr
  3618. else
  3619. print '<td style="text-align: center;"> <a href="' . $url . '" ' . $last_solution_submission . '>'
  3620. . '<div type="button1" class="btnLInE btn-secondary" style="'.$stylebutton.'">' . $feedback
  3621. . '</div>' . '</a>' . "\n";
  3622. if ($this->action != 'print') { // do not presents comments if "print" mode
  3623. if ($sum_comment > 0 && $sum_verify_message > 0)
  3624. print '<a href="' . $url . '"> '
  3625. . '<div type="button1" class="btnLInE btn-secondary" style="'.$stylebutton.'">' . $comment
  3626. . '<span class="badge badge-light" style="font-size: 1rem; margin-left: .5rem;">' . $sum_verify_message . '/' . $sum_comment
  3627. . '</span> </div>' . '</a>';
  3628. else if ($sum_comment > 0)
  3629. print '<a href="' . $url . '"> '
  3630. . '<div type="button1" class="btnLInE btn-secondary" style="'.$stylebutton.'"> ' . $comment
  3631. . '<span class="badge badge-light" style="font-size: 1rem; margin-left: .5rem;">' . $sum_comment
  3632. . '</span> </div>' . '</a>';
  3633. }
  3634. print '</td>' . "\n";
  3635. // if ($this->action == 'print') print $feedback . ' (' . $tentativas . ')<br>' . $comment . ' (' . $sum_comment . ') ' . "\n";
  3636. } // if ($student_submissions)
  3637. else { // if ($student_submissions)
  3638. // Student does not have any submission
  3639. // Informations to allow teacher to browse previous activities (after enter in one specific student solution)
  3640. if ($j - 1 < 1 || $j == $i) $iassign_previous = "-1";
  3641. else $iassign_previous = $students_submissions[$j - 1]->iassignid;
  3642. if ($student_index - 1 < 0 || $student_index == $total_students) $user_previous = "-1";
  3643. else $user_previous = $users_array[$student_index - 1]->userid;
  3644. // next
  3645. if ($i - 1 > $j) $iassign_next = $students_submissions[$j + 1]->iassignid;
  3646. else $iassign_next = "-1";
  3647. if ($total_students - 1 > $student_index) $user_next = $users_array[$student_index + 1]->userid;
  3648. else $user_next = "-1";
  3649. $url = $CFG->wwwroot . "/mod/iassign/view.php?action=viewsubmission&id=" . $id . "&userid_iassign=" . $users_array[$student_index]->userid .
  3650. "&iassign_current=" . $students_submissions[$j]->iassignid . "&view_iassign=" . $this->view_iassign . "&row=" . $student_index . "&column=" . $j;
  3651. $feedback = '<i class="fa fa-question" style="color: #77229c; font-size: 1.2rem;" aria-hidden="true"></i>';
  3652. // if ($this->action == 'print') print $feedback . ' (0)<br>' . $comment . ' (' . $sum_comment . ') ' . "\n";
  3653. // else {
  3654. print '<td style="text-align: center;"> <a href="' . $url . '">'
  3655. . '<div type="button1" class="btnLInE btn-secondary" style="' . $stylebutton . '">' . $feedback
  3656. . '</div>' . '</a>' . "\n";
  3657. if ($sum_comment > 0)
  3658. print '<a href="' . $url . '">' . $comment . '</a> (' . $sum_comment . ') ' . "\n";
  3659. print '</td>' . "\n";
  3660. // }
  3661. } // else if ($student_submissions)
  3662. $USER->matrix_iassign[$student_index][$j] = new stdClass();
  3663. $USER->matrix_iassign[$student_index][$j]->iassign_previous = $iassign_previous;
  3664. $USER->matrix_iassign[$student_index][$j]->user_previous = $user_previous;
  3665. $USER->matrix_iassign[$student_index][$j]->iassign_next = $iassign_next;
  3666. $USER->matrix_iassign[$student_index][$j]->user_next = $user_next;
  3667. if ($student_submissions)
  3668. $USER->matrix_iassign[$student_index][$j]->iassign_submission_current = $student_submissions->id;
  3669. else
  3670. $USER->matrix_iassign[$student_index][$j]->iassign_submission_current = 0;
  3671. //print '</td>' . "\n";
  3672. } // for ($j=1; $j<$students_submissions_exerc; $j++)
  3673. $total = $total + $total_in_activity;
  3674. $porcentagem = ($total_in_activity / ($j - 1)) * 100; //Attention: final grade
  3675. if ($tentativas != 0 && $tentativas != null) {
  3676. $url_answer = "" . $CFG->wwwroot . "/mod/iassign/view.php?" . "action=download_all_answer&iassign_id=" . $this->iassign->id . "&userid=" . $users_array[$student_index]->userid . "&id=" . $id;
  3677. print ' <td align="center"><a href="' . $url_answer . '">' . iassign_icons::insert('download_all_assign') . '</a></td>' . "\n";
  3678. }
  3679. else {
  3680. print ' <td align="center">' . iassign_icons::insert('download_all_assign_disabled') . '</td>' . "\n";
  3681. }
  3682. print '</tr>' . "\n";
  3683. //NOTused $sum_correct_student[$sum_student] = new stdClass();
  3684. //NOTused $sum_correct_student[$sum_student]->name = $name;
  3685. //NOTused $sum_correct_student[$sum_student]->sum = $total_in_activity;
  3686. } // for ($student_index = 0; $student_index < $total_students; $student_index++)
  3687. //NOTused for ($ii = 1; $ii < $num_exerc; $ii++) {
  3688. //NOTused if (is_null($sum_iassign_correct[$ii]))
  3689. //NOTused $sum_iassign_correct[$ii] = 0;
  3690. //NOTused $sum_correct_iassign[$ii] = new stdClass();
  3691. //NOTused $sum_correct_iassign[$ii]->sum = $sum_iassign_correct[$i];
  3692. //NOTused $sum_correct_iassign[$ii]->name = $students_submissions[$i]->name;
  3693. //NOTused }
  3694. // Print total of submissions per activity: last line of table
  3695. print " <tr><td><strong>" . get_string('report_total_submissions', 'iassign') . "</strong></td>\n"; // 'Total of submissions'
  3696. for ($ii = 1; $ii < $num_exerc; $ii++) {
  3697. print " <td style='text-align: center;'>" . $students_submissions[$ii]->total_answers . "</td> ";
  3698. }
  3699. print " </tr>\n";
  3700. } // if ($students)
  3701. } // if ($num_exerc>1)
  3702. else {
  3703. print_string('no_activity', 'iassign');
  3704. }
  3705. print "</table>\n";
  3706. print '<p class="mt-5">' . get_string('ps_experiment', 'iassign') . "</p>\n"; // the number of atemps is...
  3707. print '<p>' . get_string('ps_comment', 'iassign') . "</p>\n"; // the total number of messages is...
  3708. if ($this->action != 'print')
  3709. print $OUTPUT->footer();
  3710. die();
  3711. } // function report()
  3712. /// Display graphics of performance
  3713. // Used by teacher's interface: Reports | Graphics
  3714. function stats () {
  3715. global $USER, $CFG, $DB, $OUTPUT;
  3716. $id = $this->cm->id;
  3717. $iassign_statement_list = $DB->get_records_list('iassign_statement', 'iassignid', array('iassignid' => $this->iassign->id), "position ASC");
  3718. if ($this->action != 'printstats')
  3719. $title = get_string('graphic', 'iassign');
  3720. $num = array();
  3721. $ii = 1;
  3722. $total_iassign_statement = 0; // total of exercises in this iAssign block
  3723. foreach ($iassign_statement_list as $one_iassign_statement) {
  3724. //REMOVE: useless here: $iassign_submission = $DB->get_records("iassign_submission", array("iassign_statementid" => $one_iassign_statement->id));
  3725. if ($one_iassign_statement->type_iassign == 3) { // {iassign_statement}.type_iassign == 3 <=> is exercise
  3726. $total_iassign_statement++; // one more exercise
  3727. $sum_iassign_correct[$ii] = 0;
  3728. $num[$ii] = new stdClass();
  3729. $num[$ii]->name = $one_iassign_statement->name;
  3730. $num[$ii]->id = $ii; // register index by {iassign_statement}.id
  3731. $num[$ii]->iassignid = $one_iassign_statement->id; // {iassign_submission}.id xxxxxxxxxxxxx remover!!!
  3732. $num[$ii]->iassign_statementid = $one_iassign_statement->id; // {iassign_submission}.iassign_statementid = {iassign_statement}.id
  3733. $ii++;
  3734. } // if ($one_iassign_statement->type_iassign == 3)
  3735. } // foreach ($iassign_statement_list as $one_iassign_statement)
  3736. $context = context_course::instance($this->course->id);
  3737. if ($total_iassign_statement > 0) { // it has any exercise?
  3738. // $role = $DB->get_record_sql("SELECT s.id, s.shortname FROM {$CFG->prefix}role s WHERE s.shortname = 'student'");
  3739. $params_temp = array('shortname' => 'student');
  3740. $role = $DB->get_record_sql(
  3741. "SELECT s.id, s.shortname FROM {role} s " .
  3742. " WHERE s.shortname = :shortname", $params_temp);
  3743. // $students = $DB->get_records_sql("SELECT s.userid, a.firstname, a.lastname FROM {$CFG->prefix}role_assignments s, {$CFG->prefix}user a WHERE s.contextid = '$context->id' AND s.userid = a.id AND s.roleid = '$role->id' ORDER BY a.firstname ASC,a.lastname ASC");
  3744. $params_temp = array('contextid' => $context->id, 'roleid' => $role->id);
  3745. $students = $DB->get_records_sql(
  3746. "SELECT s.userid, a.firstname, a.lastname FROM {role_assignments} s, {user} a " .
  3747. " WHERE s.contextid = :contextid AND s.userid = a.id AND s.roleid = :roleid " .
  3748. " ORDER BY a.firstname ASC,a.lastname ASC", $params_temp);
  3749. $j = 0;
  3750. $total = 0;
  3751. $total_students_class = 0; // will receive the total of students in class
  3752. $aux_total_students_class = 0; // will count students in class, to define '$total_students_class'
  3753. $sum_correct_iassign = array();
  3754. $sum_correct_student = array();
  3755. $sum_experiment = array();
  3756. $total_at_least_one_submission = array(); // array to register the total number of exercises with at least one submission (per exercicse)
  3757. for ($jj = 1; $jj <= $total_iassign_statement; $jj++) {
  3758. $sum_experiment[$jj] = 0;
  3759. $total_at_least_one_submission[$jj] = 0;
  3760. }
  3761. if ($students) { // $students has {role_assignments}.userid, {user}.firstname, {user}.lastname
  3762. foreach ($students as $one_user) { // For each student
  3763. $aux_total_students_class++;
  3764. $name = $one_user->firstname . ' ' . $one_user->lastname;
  3765. // rows
  3766. $count_student_correct_answer = 0;
  3767. for ($jj = 1; $jj <= $total_iassign_statement; $jj++) { // {iassign_submission}.iassign_statementid = {iassign_statement}.id
  3768. $student_submissions = $DB->get_record("iassign_submission", array('iassign_statementid' => $num[$jj]->iassignid, 'userid' => $one_user->userid)); // data about student solution
  3769. if ($student_submissions) {
  3770. if ($student_submissions->status == 3) {
  3771. $sum_iassign_correct[$jj]++;
  3772. $count_student_correct_answer++;
  3773. } // if ($student_submissions->status == 3)
  3774. // Sum the total number of "experiment" (if set, all of them is registerede in {iassign_allsubmissions})
  3775. // {iassign_submission}.experiment is the number of time student "click" in the evaluation button of the iLM
  3776. $total_experiment = $student_submissions->experiment;
  3777. $sum_experiment[$jj] += $student_submissions->experiment; // to be used in 'Total Attempts' and 'Average attempts'
  3778. $total_at_least_one_submission[$jj]++; // one more student with submission to this exercise - used in 'Average attempts'
  3779. }
  3780. } // for ($jj=1; $jj<$total_iassign_statement; $jj++)
  3781. $total = $total + $count_student_correct_answer;
  3782. $sum_correct_student[$aux_total_students_class] = new stdClass();
  3783. $sum_correct_student[$aux_total_students_class]->name = $name;
  3784. $sum_correct_student[$aux_total_students_class]->sum = $count_student_correct_answer; // total of correct exercises of this student
  3785. } // foreach ($students as $one_user)
  3786. $total_students_class = $aux_total_students_class;
  3787. }
  3788. for ($ii = 1; $ii < $total_iassign_statement; $ii++) {
  3789. if (is_null($sum_iassign_correct[$ii])) {
  3790. $sum_iassign_correct[$ii] = 0;
  3791. }
  3792. $sum_correct_iassign[$ii] = new stdClass();
  3793. $sum_correct_iassign[$ii]->sum = $sum_iassign_correct[$ii];
  3794. $sum_correct_iassign[$ii]->name = $num[$ii]->name;
  3795. $sum_correct_iassign[$ii]->experiment = $sum_experiment[$ii]; // {iassign_submission}.experiment : total
  3796. } // for ($ii = 1; $ii < $total_iassign_statement; $ii++)
  3797. } // if ($total_iassign_statement > 1)
  3798. print $OUTPUT->header();
  3799. $this->print_main_menu();
  3800. if ($this->action != 'printstats') {
  3801. $title = get_string('graphic', 'iassign');
  3802. $link_report = "<a href='" . $CFG->wwwroot . "/mod/iassign/view.php?id=" . $id . "&action=report&iassignid=" . $this->iassign->id . "'>" . iassign_icons::insert('view_report') . ' ' . get_string('report', 'iassign') . "</a>";
  3803. $link_print_stats = "<a href='" . $CFG->wwwroot . "/mod/iassign/view.php?id=" . $id . "&action=printstats&&iassignid=" . $this->iassign->id . "'>" . iassign_icons::insert('print') . ' ' . get_string('print', 'iassign') . "</a>";
  3804. print '<table width=100%><tr>';
  3805. print '<td align="right">' . $link_print_stats . '</td>' . "\n";
  3806. print '<td width=15% align="right">' . $link_report . '</td>';
  3807. print '</td></tr></table>' . "\n";
  3808. print "<br><br>";
  3809. print '<table id="outlinetable" class="generaltable boxaligncenter" cellpadding="5" width="100%">' . "\n";
  3810. print '<tr><th colspan=5 class="header c1">' . get_string('distribution_activity', 'iassign') . '</th></tr>' . "\n"; // 'Distribution of hits per activity'
  3811. print '<tr><td class=\'cell c0 actvity\' width=35%><strong>' . iassign_utils::remove_code_message($this->iassign->name) . '</strong></td>' . "\n";
  3812. print '<td class=\'cell c0 actvity\' width=35%><strong>' . get_string('percentage_correct', 'iassign') . '</strong></td>' . "\n"; // 'Percentage of correct'
  3813. print '<td class=\'cell c0 actvity\' width=10% align="right"><strong>' . get_string('proportion_correct', 'iassign') . '</strong>' . "\n"; // 'Percentage of correct'
  3814. print '<td class=\'cell c0 actvity\' width=10% align="right"><strong>' . get_string('sum_experiment', 'iassign') . '</strong></td>' . "\n"; // 'Total Attempts'
  3815. print '<td class=\'cell c0 actvity\' width=10% align="right"><strong>' . get_string('avg_experiment', 'iassign') . '</strong></td>' . "\n"; // 'Average attempts'
  3816. print '</tr>' . "\n";
  3817. $sum_correct = 0;
  3818. //D echo "total_students_class=" . $total_students_class . "<br/>\n"; //D 2023/09/02
  3819. if ($sum_correct_iassign) { // is any correct answer in the array?
  3820. $ii = 1; // exercise number
  3821. foreach ($sum_correct_iassign as $each_correct_answer) { // element of array with: sum, name, experiment
  3822. if (is_null($each_correct_answer->experiment))
  3823. $each_correct_answer->experiment = 0;
  3824. $bar = "";
  3825. $sum = $each_correct_answer->sum;
  3826. $percent = ($sum / $total_students_class) * 100; //Attention: final grade
  3827. $text = number_format($percent, 1) . '%';
  3828. $sum_correct += $sum;
  3829. if ($sum > 0) {
  3830. for ($i = 1; $i < $percent * 2; $i++)
  3831. $bar .= iassign_icons::insert('hbar_blue');
  3832. $bar .= iassign_icons::insert('hbar_blue_r');
  3833. } // if ($sum > 0)
  3834. print '<tr ><td class=\'cell c0 actvity\'width=35%>' . $each_correct_answer->name . '</td>' . "\n";
  3835. print ' <td class=\'cell c0 actvity\' width=35%>' . $bar . ' ' . $text . '</td>' . "\n"; // 'Percentage of correct' answers
  3836. print ' <td class=\'cell c0 actvity\' width=10% align="right">' . $sum . '/' . $total_students_class . '</td>' . "\n"; // 'Percentage of correct'
  3837. print ' <td class=\'cell c0 actvity\' width=10% align="right">' . $each_correct_answer->experiment . '</td>' . "\n"; // 'Total Attempts'
  3838. //Attention: final grade
  3839. print ' <td class=\'cell c0 actvity\' width=10% align="right">' . number_format($each_correct_answer->experiment / $total_at_least_one_submission[$ii], 1) . '</td>' . "\n"; // 'Average attempts'
  3840. print '</tr>' . "\n";
  3841. $ii++;
  3842. } // foreach ($sum_correct_iassign as $each_correct_answer)
  3843. }
  3844. print "</table>";
  3845. print "<br><br>";
  3846. print '<table id="outlinetable" class="generaltable boxaligncenter" cellpadding="5" width="100%">' . "\n";
  3847. print '<tr><th colspan=3 class="header c1">' . get_string('distribution_student', 'iassign') . '</th></tr>' . "\n"; // 'Distribution of hits per studant'
  3848. print '<tr><td class=\'cell c0 actvity\' width=50%><strong>' . iassign_utils::remove_code_message($this->iassign->name) . '</strong></td>' . "\n";
  3849. print ' <td class=\'cell c0 actvity\' width=40%><strong>' . get_string('percentage_correct', 'iassign') . '</strong></td>'; // 'Percentage of correct'
  3850. print ' <td class=\'cell c0 actvity\' width=10% align="right"><strong>' . get_string('sum_correct', 'iassign') . '</strong></td>';
  3851. print '</tr>' . "\n";
  3852. $sum_correct = 0;
  3853. foreach ($sum_correct_student as $student) {
  3854. $bar = "";
  3855. $sum = $student->sum;
  3856. $percent = ($sum / $total_iassign_statement) * 100; //Attention: final grade
  3857. $text = number_format($percent, 1) . '%';
  3858. $sum_correct += $sum;
  3859. if ($sum > 0) {
  3860. for ($i = 1; $i < $percent * 2; $i++)
  3861. $bar .= iassign_icons::insert('hbar_blue');
  3862. $bar .= iassign_icons::insert('hbar_blue_r');
  3863. } // if ($sum > 0)
  3864. print '<tr ><td class=\'cell c0 actvity\'width=50%>' . $student->name . '</td>' . "\n";
  3865. print '<td class=\'cell c0 actvity\' width=40%>' . $bar . ' ' . $text . '</td>' . "\n";
  3866. print '<td class=\'cell c0 actvity\' width=10% align="right">' . $sum . '/' . $total_iassign_statement . '</td>' . "\n";
  3867. print '</tr>' . "\n";
  3868. } // foreach ($sum_correct_student as $student)
  3869. print "</table>\n";
  3870. print "<br><br>\n";
  3871. $var1 = 0;
  3872. $cv = 0;
  3873. $dv = 0;
  3874. $avg = 0;
  3875. if ($sum_correct_student) {
  3876. $avg = $sum_correct / $total_students_class; //Attention: final grade
  3877. if ($avg > 0) {
  3878. foreach ($sum_correct_student as $student) {
  3879. $aux = $student->sum - $avg;
  3880. $var1 += $aux * $aux; // pow($student->sum - $avg, 2);
  3881. }
  3882. $var1 = $var1 / $total_students_class; //Attention: final grade
  3883. $dv = sqrt($var1); // population variation
  3884. $cv = ($dv / $avg) * 100; // population standart deviation //Attention: final grade
  3885. }
  3886. }
  3887. $msg_sum_activity = ' title="' . get_string('sum_activity_title', 'iassign') . '"'; // 'The number of exercises in this iAssign block of activities (NE)'
  3888. $msg_sum_student = ' title="' . get_string('sum_student_title', 'iassign') . '"'; // 'The number of students in this course/class (NS)'
  3889. $msg_mean_score = ' title="' . get_string('mean_score_title', 'iassign') . '"'; // 'The ration total number of students with correct answers divided by the number of students (M)'
  3890. $msg_standard_deviation = ' title="' . get_string('standard_deviation_title', 'iassign') . '"'; // 'Population variation (PV) - over NS'
  3891. $msg_coefficient_variation = ' title="' . get_string('coefficient_variation_title', 'iassign') . '"'; // 'Population standard deviation: square root of PV'
  3892. print '<table id="outlinetable" class="generaltable boxaligncenter" cellpadding="5" width="100%">' . "\n";
  3893. print '<tr><th colspan=5 class="header c1">' . get_string('statistics', 'iassign') . '</th></tr>' . "\n";
  3894. print '<tr><td class=\'cell c0 actvity\' width=20% align="center"' . $msg_sum_activity . '><strong>' . get_string('sum_activity', 'iassign') . '</strong></td>' . "\n"; // 'Total activity'
  3895. print ' <td class=\'cell c0 actvity\' width=20% align="center"' . $msg_sum_student . '><strong>' . get_string('sum_student', 'iassign') . '</strong></td>' . "\n"; // 'Total students'
  3896. print ' <td class=\'cell c0 actvity\' width=20% align="center"' . $msg_mean_score . '><strong>' . get_string('mean_score', 'iassign') . '</strong></td>' . "\n"; // 'Mean scores'
  3897. print ' <td class=\'cell c0 actvity\' width=20% align="center"' . $msg_standard_deviation . '><strong>' . get_string('standard_deviation', 'iassign') . '</strong></td>' . "\n"; // 'Standard deviation'
  3898. print ' <td class=\'cell c0 actvity\' width=20% align="center"' . $msg_coefficient_variation . '><strong>' . get_string('coefficient_variation', 'iassign') . '</strong></td></tr>' . "\n"; // 'Coefficient of variation'
  3899. print '<tr><td class=\'cell c0 actvity\' width=20% align="center">' . $total_iassign_statement . '</td>' . "\n";
  3900. print ' <td class=\'cell c0 actvity\' width=20% align="center">' . $total_students_class . '</td>' . "\n";
  3901. print ' <td class=\'cell c0 actvity\' width=20% align="center">' . number_format($avg, 1) . '</td>' . "\n"; // 'Mean scores'
  3902. print ' <td class=\'cell c0 actvity\' width=20% align="center">' . number_format($dv, 1) . '</td>' . "\n";
  3903. print ' <td class=\'cell c0 actvity\' width=20% align="center">' . number_format($cv, 1) . '%</td></tr>' . "\n";
  3904. print "</table>\n";
  3905. print $OUTPUT->footer();
  3906. } else {
  3907. print "<STYLE TYPE='text/css'>
  3908. <!--
  3909. .boldtable {
  3910. font-family:sans-serif;
  3911. font-size:10pt;
  3912. }
  3913. -->
  3914. </STYLE>\n";
  3915. print '<table border=1 width=100%>' . "\n";
  3916. print '<tr><td colspan=3 align="center"><strong>' . get_string('distribution_activity', 'iassign') . '</strong></td></tr>' . "\n"; // 'Distribution of hits per activity'
  3917. print '<tr><td width=50%><strong>' . iassign_utils::remove_code_message($this->iassign->name) . '</strong></td>' . "\n";
  3918. print '<td width=40%><strong>' . get_string('percentage_correct', 'iassign') . '</strong></td>';// 'Percentage of correct'
  3919. print '<td width=10% align="right"><strong>' . get_string('sum_correct', 'iassign') . '</strong></td>';
  3920. print '</tr>' . "\n";
  3921. $sum_correct = 0;
  3922. foreach ($sum_correct_iassign as $each_correct_answer) {
  3923. $bar = "";
  3924. $sum = $each_correct_answer->sum;
  3925. $percent = ($sum / $total_students_class) * 100; //Attention: final grade
  3926. $text = number_format($percent, 1) . '%';
  3927. $sum_correct += $sum;
  3928. if ($sum > 0) {
  3929. for ($i = 1; $i < $percent * 2; $i++)
  3930. $bar .= iassign_icons::insert('hbar_blue');
  3931. $bar .= iassign_icons::insert('hbar_blue_r');
  3932. } // if ($sum > 0)
  3933. print '<tr><td width=50%>' . $each_correct_answer->name . '</td>' . "\n";
  3934. print '<td width=40%>' . $bar . ' ' . $text . '</td>';
  3935. print '<td width=10% align="right">' . $sum . '/' . $total_students_class . '</td>';
  3936. print '</tr>' . "\n";
  3937. } // foreach ($sum_correct_iassign as $each_correct_answer)
  3938. print "</table>";
  3939. print "<br><br>";
  3940. print '<table border=1 class="boldtable" width=100%>' . "\n";
  3941. print '<tr><td colspan=3 align="center" ><strong>' . get_string('distribution_student', 'iassign') . '</strong></td></tr>' . "\n"; // 'Distribution of hits per studant'
  3942. print '<tr><td width=50%><strong>' . iassign_utils::remove_code_message($this->iassign->name) . '</strong></td>' . "\n";
  3943. print '<td width=40%><strong>' . get_string('percentage_correct', 'iassign') . '</strong></td>'; // 'Percentage of correct'
  3944. print '<td width=10% align="right"><strong>' . get_string('sum_correct', 'iassign') . '</strong></td>';
  3945. print '</tr>' . "\n";
  3946. $sum_correct = 0;
  3947. foreach ($sum_correct_student as $student) {
  3948. $bar = "";
  3949. $sum = $student->sum;
  3950. $percent = ($sum / $total_iassign_statement) * 100; //Attention: final grade
  3951. $text = number_format($percent, 1) . '%';
  3952. $sum_correct += $sum;
  3953. if ($sum > 0) {
  3954. for ($i = 1; $i < $percent * 2; $i++)
  3955. $bar .= iassign_icons::insert('hbar_blue');
  3956. $bar .= iassign_icons::insert('hbar_blue_r');
  3957. } // if ($sum > 0)
  3958. print "<tr><td width=50%>" . $student->name . "</td>\n";
  3959. print ' <td width=40%>' . $bar . ' ' . $text . '</td>' . "\n";
  3960. print ' <td width=10% align="right">' . $sum . '/' . $total_iassign_statement . '</td>' . "\n";
  3961. print '</tr>' . "\n";
  3962. } // foreach ($sum_correct_student as $student)
  3963. print "</table>\n";
  3964. print "<br/><br/>\n";
  3965. $var1 = 0;
  3966. $cv = 0;
  3967. $dv = 0;
  3968. $avg = 0;
  3969. if ($sum_correct_student) {
  3970. $avg = $sum_correct / $total_students_class; //Attention: final grade
  3971. if ($avg > 0) {
  3972. foreach ($sum_correct_student as $student)
  3973. $var1 += pow($student->sum - $avg, 2);
  3974. $var1 = $var1 / $total_students_class;
  3975. $dv = sqrt($var1);
  3976. $cv = ($dv / $avg) * 100;
  3977. }
  3978. }
  3979. print '<table border=1 class="boldtable" width=100%>' . "\n";
  3980. print '<tr><td colspan=5 align="center"><strong>' . get_string('statistics', 'iassign') . '</strong></th></tr>' . "\n";
  3981. print '<tr><td width=20% align="center"><strong>' . get_string('sum_activity', 'iassign') . '</strong></td>' . "\n";
  3982. print '<td width=20% align="center"><strong>' . get_string('sum_student', 'iassign') . '</strong></td>' . "\n";
  3983. print '<td width=20% align="center"><strong>' . get_string('mean_score', 'iassign') . '</strong></td>' . "\n";
  3984. print '<td width=20% align="center"><strong>' . get_string('standard_deviation', 'iassign') . '</strong></td>' . "\n";
  3985. print '<td width=20% align="center"><strong>' . get_string('coefficient_variation', 'iassign') . '</strong></td></tr>' . "\n";
  3986. print '<tr><td width=20% align="center">' . $total_iassign_statement . '</td>' . "\n";
  3987. print '<td width=20% align="center">' . $total_students_class . '</td>' . "\n";
  3988. print '<td width=20% align="center">' . number_format($avg, 1) . '</td>' . "\n";
  3989. print '<td width=20% align="center">' . number_format($dv, 1) . '</td>' . "\n";
  3990. print '<td width=20% align="center">' . number_format($cv, 1) . '%</td></tr>' . "\n";
  3991. print "</table>\n";
  3992. } // if ($this->action != 'printstats')
  3993. die();
  3994. } // function stats()
  3995. /// Display a survey (an a graphic) of the student situation
  3996. // The survey is to the current student
  3997. function stats_students () {
  3998. global $USER, $CFG, $DB, $OUTPUT;
  3999. $id = $this->cm->id;
  4000. $sql_str = "SELECT * FROM {iassign_statement} s WHERE s.iassignid = '{$this->iassign->id}' AND s.type_iassign=3 ORDER BY s.position";
  4001. //D echo "<br/><br/><br/><br/><br/>locallib.php!stats_students(): this->iassign->id=" . $this->iassign->id . "<br/>" . $sql_str . "<br/>"; //exit;
  4002. $iassign_statement_list = $DB->get_records_sql($sql_str); // get all the iActivities in this block
  4003. $title = get_string('results', 'iassign');
  4004. $sum_correct = 0;
  4005. $sum_incorrect = 0;
  4006. $sum_post = 0;
  4007. $sum_nopost = 0;
  4008. $sum_iassign_st = count($iassign_statement_list);
  4009. $bar_nopost = "";
  4010. $bar_correct = "";
  4011. $bar_incorrect = "";
  4012. $bar_post = "";
  4013. $text_nopost = "";
  4014. $text_correct = "";
  4015. $text_incorrect = "";
  4016. foreach ($iassign_statement_list as $iassign_statement_activity_item) { // get all the submissions to each iActivity considering the current user
  4017. $iassign_submission = $DB->get_record("iassign_submission", array('iassign_statementid' => $iassign_statement_activity_item->id, 'userid' => $USER->id)); // data about student solution
  4018. if ($iassign_submission) {
  4019. if ($iassign_submission->status == 3)
  4020. $sum_correct++;
  4021. elseif ($iassign_submission->status == 2)
  4022. $sum_incorrect++;
  4023. elseif ($iassign_submission->status == 1)
  4024. $sum_post++;
  4025. elseif ($iassign_submission->status == 0 || !$iassign_submission)
  4026. $sum_nopost++;
  4027. } // if ($iassign_submission)
  4028. } // foreach ($iassign_statement_list as $iassign_statement_activity_item)
  4029. if ($sum_iassign_st > 0) {
  4030. $percent_correct = ($sum_correct / $sum_iassign_st) * 100; //Attention: final grade
  4031. $text_correct = number_format($percent_correct, 1) . '%';
  4032. }
  4033. if ($sum_correct > 0) {
  4034. for ($i = 1; $i < $percent_correct * 2; $i++)
  4035. $bar_correct .= iassign_icons::insert('hbar_green');
  4036. $bar_correct .= iassign_icons::insert('hbar_green_r');
  4037. } // if ($sum_correct > 0)
  4038. if ($sum_iassign_st > 0) {
  4039. $percent_incorrect = ($sum_incorrect / $sum_iassign_st) * 100;
  4040. $text_incorrect = number_format($percent_incorrect, 1) . '%';
  4041. }
  4042. if ($sum_incorrect > 0) {
  4043. for ($i = 1; $i < $percent_incorrect * 2; $i++)
  4044. $bar_incorrect .= iassign_icons::insert('hbar_red');
  4045. $bar_incorrect .= iassign_icons::insert('hbar_red_r');
  4046. } // if ($sum_incorrect > 0)
  4047. if ($sum_iassign_st > 0) {
  4048. $percent_post = ($sum_post / $sum_iassign_st) * 100;
  4049. $text_post = number_format($percent_post, 1) . '%';
  4050. }
  4051. if ($sum_post > 0) {
  4052. for ($i = 1; $i < $percent_post * 2; $i++)
  4053. $bar_post .= iassign_icons::insert('hbar_blue');
  4054. $bar_post .= iassign_icons::insert('hbar_blue_r');
  4055. } // if ($sum_post > 0)
  4056. if ($sum_iassign_st > 0) {
  4057. $percent_nopost = ($sum_nopost / $sum_iassign_st) * 100;
  4058. $text_nopost = number_format($percent_nopost, 1) . '%';
  4059. }
  4060. if ($sum_nopost > 0) {
  4061. for ($i = 1; $i < $percent_nopost * 2; $i++)
  4062. $bar_nopost .= iassign_icons::insert('hbar_orange');
  4063. $bar_nopost .= iassign_icons::insert('hbar_orange_r');
  4064. } // if ($sum_nopost > 0)
  4065. print $OUTPUT->header();
  4066. //2021/12 $link_return = " <a href='" . $this->return . "'>" . iassign_icons::insert('home') . get_string('activities_page', 'iassign') . "</a>";
  4067. //2021/12 print '<table width=100%><tr>';
  4068. //2021/12 print '<td align="right">' . $link_return . '</td>' . "\n";
  4069. //2021/12 print '</td></tr></table>' . "\n";
  4070. print "<br/><br/>\n";
  4071. print '<table id="outlinetable" class="generaltable box aligncenter mt-5" cellpadding="5" width="100%">' . "\n";
  4072. print '<tr><th colspan=3 class="header c1">' . "\n";
  4073. // helpbutton('legend', get_string('legend', 'iassign'), 'iassign', $image = true, $linktext = false, $text = '', $return = false,
  4074. // $imagetext = '');
  4075. print iassign_utils::remove_code_message($this->iassign->name) . '</th></tr>' . "\n";
  4076. print '<tr ><td class=\'cell c0 actvity\'width=50%>' . get_string('correct', 'iassign') . '</td>' . "\n";
  4077. print '<td class=\'cell c0 actvity\' width=40%>' . $bar_correct . ' ' . $text_correct . '</td>';
  4078. print '<td class=\'cell c0 actvity\' width=10% align="right">' . $sum_correct . '/' . $sum_iassign_st . '</td>';
  4079. print '</tr>' . "\n";
  4080. print '<tr ><td class=\'cell c0 actvity\'width=50%>' . get_string('incorrect', 'iassign') . '</td>' . "\n";
  4081. print '<td class=\'cell c0 actvity\' width=40%>' . $bar_incorrect . ' ' . $text_incorrect . '</td>';
  4082. print '<td class=\'cell c0 actvity\' width=10% align="right">' . $sum_incorrect . '/' . $sum_iassign_st . '</td>';
  4083. print '</tr>' . "\n";
  4084. if ($sum_post) {
  4085. print '<tr ><td class=\'cell c0 actvity\'width=50%>' . get_string('post', 'iassign') . '</td>' . "\n";
  4086. print '<td class=\'cell c0 actvity\' width=40%>' . $bar_post . ' ' . $text_post . '</td>';
  4087. print '<td class=\'cell c0 actvity\' width=10% align="right">' . $sum_post . '/' . $sum_iassign_st . '</td>';
  4088. print '</tr>' . "\n";
  4089. } // if ($sum_post)
  4090. print '<tr ><td class=\'cell c0 actvity\'width=50%>' . get_string('not_post', 'iassign') . '</td>' . "\n";
  4091. print '<td class=\'cell c0 actvity\' width=40%>' . $bar_nopost . ' ' . $text_nopost . '</td>';
  4092. print '<td class=\'cell c0 actvity\' width=10% align="right">' . $sum_nopost . '/' . $sum_iassign_st . '</td>';
  4093. print '</tr>' . "\n";
  4094. print "</table>";
  4095. print "<br><br>";
  4096. print '<table id="outlinetable" class="generaltable boxaligncenter" cellpadding="5" width="100%">' . "\n";
  4097. print '<tr><th colspan=3 class="header c1">' . get_string('grades', 'iassign') . '</th></tr>' . "\n";
  4098. print '<tr><td class=\'cell c0 actvity\' width=50%><strong>' . iassign_utils::remove_code_message($this->iassign->name) . '</strong></td>' . "\n";
  4099. print '<td class=\'cell c0 actvity\' width=25% align=right><strong>' . get_string('grade_student', 'iassign') . '</strong></td>' . "\n";
  4100. print '<td class=\'cell c0 actvity\' width=25% align=right><strong>' . get_string('grade_iassign', 'iassign') . '</strong></tr>' . "\n";
  4101. $sum_grade = 0;
  4102. $sum_grade_student = 0;
  4103. $avg = 0;
  4104. foreach ($iassign_statement_list as $iassign_statement_activity_item) {
  4105. $iassign_submission = $DB->get_record("iassign_submission", array('iassign_statementid' => $iassign_statement_activity_item->id, 'userid' => $USER->id));
  4106. if (!$iassign_submission) {
  4107. $iassign_submission = new stdClass();
  4108. $iassign_submission->grade = 0;
  4109. }
  4110. print '<tr ><td class=\'cell c0 actvity\'width=50%>' . $iassign_statement_activity_item->name . '</td>' . "\n";
  4111. print '<td class=\'cell c0 actvity\' width=25% align=right>' . $iassign_submission->grade . '</td>';
  4112. print '<td class=\'cell c0 actvity\' width=25% align=right>' . $iassign_statement_activity_item->grade . '</td>';
  4113. print '</tr>' . "\n";
  4114. $sum_grade += $iassign_statement_activity_item->grade;
  4115. $sum_grade_student += $iassign_submission->grade;
  4116. } // foreach ($iassign_statement_list as $iassign_statement_activity_item)
  4117. if ($sum_grade > 0)
  4118. $avg = $sum_grade_student / $sum_grade * 100;
  4119. print '<tr><td class=\'cell c0 actvity\' width=50%><strong>' . get_string('total', 'iassign') . '</strong></td>' . "\n";
  4120. print '<td class=\'cell c0 actvity\' width=25% align=right><strong>' . $sum_grade_student . '</strong></td>' . "\n";
  4121. print '<td class=\'cell c0 actvity\' width=25% align=right><strong>' . $sum_grade . '</strong></tr>' . "\n";
  4122. print '<tr><td class=\'cell c0 actvity\' width=25% align=left><strong>' . get_string('percentage_correct', 'iassign') . '</strong></td>' . "\n"; // 'Percentage of correct'
  4123. print '<td colspan=2 class=\'cell c0 actvity\' align=right><strong>' . number_format($avg, 1) . '%</strong></tr>' . "\n";
  4124. print "</table>";
  4125. print $OUTPUT->footer();
  4126. die();
  4127. } // function stats_students()
  4128. function get_total_notifications () {
  4129. global $DB, $USER, $COURSE;
  4130. $total_messages = 0;
  4131. if (has_capability('mod/iassign:editiassign', $this->context, $USER->id)) {
  4132. $params = array('userid' => $USER->id, 'course' => $COURSE->id, 'receiver' => '1');
  4133. $total_messages = $DB->get_record_sql("SELECT COUNT(*) AS total FROM {iassign_submission_comment} AS c, {iassign_submission} AS s, {iassign_statement} AS st, {iassign} AS ias "
  4134. . " WHERE c.return_status=0 AND c.receiver=:receiver AND c.iassign_submissionid=s.id AND s.iassign_statementid=st.id AND st.iassignid=ias.id AND ias.course=:course", $params);
  4135. }
  4136. else {
  4137. $params = array('userid' => $USER->id, 'course' => $COURSE->id, 'receiver' => '2');
  4138. $total_messages = $DB->get_record_sql("SELECT COUNT(*) AS total FROM {iassign_submission_comment} AS c, {iassign_submission} AS s, {iassign_statement} AS st, {iassign} AS ias "
  4139. . " WHERE c.return_status=0 AND c.receiver=:receiver AND c.iassign_submissionid=s.id AND s.userid=:userid AND s.iassign_statementid=st.id AND st.iassignid=ias.id AND ias.course=:course", $params);
  4140. }
  4141. return $total_messages->total;
  4142. }
  4143. function get_iassign_statement_messages () {
  4144. global $DB, $USER, $COURSE;
  4145. if (has_capability('mod/iassign:editiassign', $this->context, $USER->id)) {
  4146. $params = array('userid' => $USER->id, 'course' => $COURSE->id, 'receiver' => '1');
  4147. $totais = array();
  4148. $messages = $DB->get_records_sql("SELECT c.id AS commentid, st.id AS stid, s.userid AS user, ias.id AS iasid, s.id AS subid, st.name AS stname, ias.name AS iasname FROM {iassign_submission_comment} AS c, {iassign_submission} AS s, {iassign_statement} AS st, {iassign} AS ias "
  4149. . " WHERE c.return_status=0 AND c.receiver=:receiver AND c.iassign_submissionid=s.id AND s.iassign_statementid=st.id AND st.iassignid=ias.id AND ias.course=:course", $params);
  4150. foreach($messages as $item) {
  4151. if (isset($totais[$item->subid])) {
  4152. $totais[$item->subid]->total++;
  4153. }
  4154. else {
  4155. $totais[$item->subid] = new stdclass();
  4156. $item->total = 1;
  4157. $totais[$item->subid] = $item;
  4158. }
  4159. }
  4160. return $totais;
  4161. }
  4162. else {
  4163. $params = array('userid' => $USER->id, 'course' => $COURSE->id, 'receiver' => '2');
  4164. $totais = array();
  4165. $messages = $DB->get_records_sql("SELECT c.id as commentid, s.id AS subid, st.id AS stid, ias.id AS iasid, s.userid AS userid, st.name AS stname, ias.name AS iasname FROM {iassign_submission_comment} AS c, {iassign_submission} AS s, {iassign_statement} AS st, {iassign} AS ias "
  4166. . " WHERE c.return_status=0 AND c.receiver=:receiver AND c.iassign_submissionid=s.id AND s.userid=:userid AND s.iassign_statementid=st.id AND st.iassignid=ias.id AND ias.course=:course", $params);
  4167. foreach ($messages as $item) {
  4168. if (isset($totais[$item->subid])) {
  4169. $totais[$item->subid]->total++;
  4170. }
  4171. else {
  4172. $totais[$item->subid] = new stdclass();
  4173. $item->total = 1;
  4174. $totais[$item->subid] = $item;
  4175. }
  4176. }
  4177. return $totais;
  4178. }
  4179. }
  4180. // It shows to the teacher all iAssign notifications (all new students' comments)
  4181. function view_notifications_teachers () {
  4182. global $OUTPUT, $USER, $DB;
  4183. $messages = $this->get_iassign_statement_messages();
  4184. print '<table class="table table-hover ml-4 mb-4 w-75 mt-3" style="border-bottom: 1px solid #dee2e6;">';
  4185. // 1. list all messages received in the current block:
  4186. $actual_block = $this->iassign->id;
  4187. $id = $this->cm->id;
  4188. $total_include = 0;
  4189. $activities_actual_block = array();
  4190. if ($messages) {
  4191. foreach ($messages as $item)
  4192. if ($item->iasid == $actual_block)
  4193. if (!in_array($item->stid, $activities_actual_block))
  4194. array_push($activities_actual_block, $item->stid);
  4195. }
  4196. if ($activities_actual_block) {
  4197. foreach ($activities_actual_block as $activity_id) {
  4198. $printed = false;
  4199. foreach ($messages as $item) {
  4200. if ($item->stid == $activity_id) {
  4201. if (!$printed) {
  4202. $student_url = "view.php?id=$id&userid_iassign=$USER->id&action=view&iassign_current=$item->stid";
  4203. $link_student = "<a href='$student_url'>" . $item->stname . "</a>";
  4204. print '<tr><td style="white-space: nowrap; width: 1%; padding-right: 2rem;" class="align-middle">' . $link_student . '';
  4205. print '<table class="table table-hover ml-4 mb-4 w-75 mt-3" style="border-bottom: 1px solid #dee2e6;">';
  4206. $printed = true;
  4207. }
  4208. $submission_url = "view.php?id=$id&userid_iassign=$item->user&action=viewsubmission&iassign_current=$item->stid";
  4209. $user = $DB->get_record('user', array('id' => $item->user));
  4210. $label = $item->total > 1 ? get_string('comment_unread', 'iassign') : get_string('comment_unread_one', 'iassign');
  4211. print '<tr><td style="white-space: nowrap; width: 1%; padding-right: 2rem;" class="align-middle"><i class="fa fa-user" aria-hidden="true"></i> ' . $user->firstname . ' '
  4212. . $user->lastname . ' </td> <td class="align-middle"> <a href="' . $submission_url . '#comments"><span class="badge bg-danger" style="color:white; font-size: .9em;">'
  4213. . '<i class="fa fa-envelope" aria-hidden="true"></i> ' . $label . ' <span class="badge rounded-pill bg-light text-dark" style="font-size: .8em;">'.$item->total.'</span></span></a></td>'
  4214. . "</tr>\n";
  4215. $total_include++;
  4216. }
  4217. }
  4218. print "</td></tr></table>\n";
  4219. }
  4220. }
  4221. print "</table>\n";
  4222. if (count($activities_actual_block) == 0) {
  4223. print '<h6 class="ml-5">' . get_string('empty_new_messages_block', 'iassign') . "</h6>\n";
  4224. print '<hr class="my-4">' . "\n";
  4225. }
  4226. if ($messages && $total_include == count($messages)) {
  4227. print $OUTPUT->footer();
  4228. return;
  4229. }
  4230. print '<h4 class="mt-4 mb-3 ml-2"><i class="fa fa-comments-o" aria-hidden="true"></i> ' . get_string('new_messages_other_blocks', 'iassign') . "</h4>\n";
  4231. $iassign_ids = array();
  4232. if ($messages) {
  4233. foreach ($messages as $item)
  4234. if (!in_array($item->iasid, $iassign_ids) && $item->iasid != $actual_block)
  4235. array_push($iassign_ids, $item->iasid);
  4236. }
  4237. foreach ($iassign_ids as $id_iassign) {
  4238. $printed = false;
  4239. $activities_in_block = array();
  4240. foreach ($messages as $subitem) {
  4241. if ($subitem->iasid == $id_iassign)
  4242. if (!in_array($item->stid, $activities_in_block))
  4243. array_push($activities_in_block, $item->stid);
  4244. }
  4245. foreach ($messages as $item) {
  4246. if ($item->iasid == $id_iassign) {
  4247. if (!$printed) {
  4248. $printed = true;
  4249. print '<i class="fa fa-th-large ml-4 mb-3 mt-2" aria-hidden="true"></i> ' . $item->iasname . "<br>\n";
  4250. print '<table class="table table-hover ml-5 w-75" style="border-bottom: 1px solid #dee2e6;">' . "\n";
  4251. }
  4252. foreach ($activities_in_block as $id_activity) {
  4253. $print = false;
  4254. foreach ($messages as $subitem) {
  4255. if ($subitem->stid == $id_activity) {
  4256. if (!$print) {
  4257. $student_url = "view.php?id=$id&userid_iassign=$USER->id&action=view&iassign_current=$subitem->stid";
  4258. $link_student = "<a href='$student_url'>" . $subitem->stname . "</a>";
  4259. print '<tr><td style="white-space: nowrap; width: 1%; padding-right: 2rem;" class="align-middle">' . $link_student . '';
  4260. print '<table class="table table-hover ml-4 mb-4 w-75 mt-3" style="border-bottom: 1px solid #dee2e6;">';
  4261. $print = true;
  4262. }
  4263. foreach ($messages as $comment_student) {
  4264. if ($comment_student->stid == $id_activity) {
  4265. $submission_url = "view.php?id=$id&userid_iassign=$comment_student->user&action=viewsubmission&iassign_current=$comment_student->stid";
  4266. $user = $DB->get_record('user', array('id' => $comment_student->user));
  4267. $label = $comment_student->total > 1 ? get_string('comment_unread', 'iassign') : get_string('comment_unread_one', 'iassign');
  4268. print '<tr><td style="white-space: nowrap; width: 1%; padding-right: 2rem;" class="align-middle"><i class="fa fa-user" aria-hidden="true"></i> ' . $user->firstname . ' ' . $user->lastname . ' </td>'
  4269. . '<td class="align-middle"> <a href="' . $submission_url . '#comments"><span class="badge bg-danger" style="color:white; font-size: .9em;"><i class="fa fa-envelope" aria-hidden="true"></i> '
  4270. . $label . ' <span class="badge rounded-pill bg-light text-dark" style="font-size: .8em;">'.$comment_student->total.'</span> </span></a></td>' . "</tr>\n";
  4271. }
  4272. }
  4273. break;
  4274. }
  4275. }
  4276. } // foreach ($activities_in_block as $id_activity)
  4277. print '</table></td></tr></table>';
  4278. break;
  4279. } // if ($item->iasid == $id_iassign)
  4280. } // foreach ($messages as $item)
  4281. } // foreach ($iassign_ids as $id_iassign)
  4282. print $OUTPUT->footer();
  4283. } // function view_notifications_teachers()
  4284. // Teachers' page with notifications (student messagens associated to each {iassign_statement})
  4285. function view_notifications () {
  4286. global $OUTPUT, $USER;
  4287. print $OUTPUT->header();
  4288. $this->print_main_menu();
  4289. print '<h4 class="mt-4 ml-2"><i class="fa fa-comments-o" aria-hidden="true"></i> ' . get_string('new_messages', 'iassign') . "</h4>\n";
  4290. if ($this->get_total_notifications() == 0) {
  4291. print '<h6 class="mt-3 ml-5">' . get_string('empty_new_messages', 'iassign') . "</h6>\n";
  4292. print '<hr class="my-4" />';
  4293. print $OUTPUT->footer();
  4294. return;
  4295. }
  4296. if (has_capability('mod/iassign:editiassign', $this->context, $USER->id)) {
  4297. $this->view_notifications_teachers();
  4298. return;
  4299. }
  4300. $messages = $this->get_iassign_statement_messages();
  4301. print '<table class="table table-hover ml-4 mb-4 w-75 mt-3" style="border-bottom: 1px solid #dee2e6;">' . "\n";
  4302. // 1. list all messages received in the current block:
  4303. $actual_block = $this->iassign->id;
  4304. $id = $this->cm->id;
  4305. $total_include = 0;
  4306. if ($messages)
  4307. foreach ($messages as $item) {
  4308. if ($item->iasid == $actual_block) {
  4309. $student_url = "view.php?id=" . $id . "&userid_iassign=" . $USER->id . "&action=view&iassign_current=" . $item->stid . "#comments";
  4310. $link_student = "<a href='$student_url'>" . $item->stname . "</a>";
  4311. $label = $item->total > 1 ? get_string('comment_unread', 'iassign') : get_string('comment_unread_one', 'iassign');
  4312. print '<tr><td style="white-space: nowrap; width: 1%; padding-right: 2rem;" class="align-middle">' . $link_student . '</td><td class="align-middle"> <a href="'.$student_url.'"><span class="badge bg-danger" style="color:white; font-size: .9em;"><i class="fa fa-envelope" aria-hidden="true"></i> ' . $label . ' <span class="badge rounded-pill bg-light text-dark" style="font-size: .8em;">'.$item->total.'</span> </span></a></td></tr>';
  4313. $total_include++;
  4314. }
  4315. }
  4316. print "</table>\n";
  4317. if ($total_include == 0) {
  4318. print '<h6 class="ml-5">'.get_string('empty_new_messages_block', 'iassign') . "</h6>\n";
  4319. print '<hr class="my-4">';
  4320. }
  4321. if ($messages && $total_include == count($messages)) {
  4322. print $OUTPUT->footer();
  4323. return;
  4324. }
  4325. print '<h4 class="mt-4 mb-3 ml-2"><i class="fa fa-comments-o" aria-hidden="true"></i> '.get_string('new_messages_other_blocks', 'iassign').'</h4>';
  4326. // 2. List all the messages received from another blocks:
  4327. $iassign_ids = array();
  4328. if ($messages) {
  4329. foreach ($messages as $item)
  4330. if (!in_array($item->iasid, $iassign_ids) && $item->iasid != $actual_block)
  4331. array_push($iassign_ids, $item->iasid);
  4332. }
  4333. foreach ($iassign_ids as $id_iassign) {
  4334. $printed = false;
  4335. foreach ($messages as $item) {
  4336. if ($item->iasid == $id_iassign) {
  4337. if (!$printed) {
  4338. $printed = true;
  4339. print '<i class="fa fa-th-large ml-4 mb-3 mt-2" aria-hidden="true"></i> ' . $item->iasname . "<br>\n";
  4340. print '<table class="table table-hover ml-5 w-75" style="border-bottom: 1px solid #dee2e6;">';
  4341. }
  4342. $student_url = "view.php?id=" . $id . "&userid_iassign=" . $USER->id . "&action=view&iassign_current=" . $item->stid . "#comments";
  4343. $link_student = "<a href='$student_url'>" . $item->stname . "</a>";
  4344. $label = $item->total > 1 ? get_string('comment_unread', 'iassign') : get_string('comment_unread_one', 'iassign');
  4345. print '<tr><td style="white-space: nowrap; width: 1%; padding-right: 2rem;" class="align-middle">' . $link_student . '</td><td class="align-middle">' .
  4346. '<a href="' . $student_url . '"><span class="badge bg-danger" style="color:white; font-size: .9em;"><i class="fa fa-envelope" aria-hidden="true"></i> ' . $label .
  4347. ' <span class="badge rounded-pill bg-light text-dark" style="font-size: .8em;">' . $item->total . "</span></span></a> </td></tr>\n";
  4348. }
  4349. }
  4350. print "</table>\n";
  4351. }
  4352. print $OUTPUT->footer();
  4353. } // function view_notifications()
  4354. // Print the first line with options to teachers' report (of one iAssign block {iassign}): report='Reports' | home='Home'
  4355. // Options: activities_page ; help_ilm ; report ; results ; auto_evaluate_name_config ; notifications ; add_iassign
  4356. // Activities Page | About iLM | Reports | Results | Auto reevaluation | Notifications | Add activity
  4357. function print_main_menu () {
  4358. global $USER;
  4359. $id = $this->cm->id;
  4360. $actual_action = optional_param('action', '', PARAM_TEXT);
  4361. $url_help = new moodle_url('/mod/iassign/settings_ilm.php', array('action' => 'list', 'ilm_id' => 1));
  4362. $url_report = new moodle_url('/mod/iassign/view.php', array('action' => 'report', 'id' => $id, 'iassignid' => $this->iassign->id));
  4363. $url_add = new moodle_url('/mod/iassign/view.php', array('id' => $id, 'action' => 'add', 'iassignid' => $this->iassign->id));
  4364. $url_stats = new moodle_url('/mod/iassign/view.php', array('id' => $id, 'action' => 'stats_student', 'iassignid' => $this->iassign->id));
  4365. $url_home = new moodle_url('/mod/iassign/view.php', array('id' => $id));
  4366. $url_notifications = new moodle_url('/mod/iassign/view.php', array('id' => $id, 'action' => 'notifications', 'iassignid' => $this->iassign->id));
  4367. $url_auto_evaluate = new moodle_url('/mod/iassign/view.php', array('id' => $id, 'action' => 'auto_evaluate', 'iassignid' => $this->iassign->id));
  4368. $url_sub_stats = new moodle_url('/mod/iassign/view.php', array('id' => $id, 'action' => 'stats', 'iassignid' => $this->iassign->id));
  4369. $url_all_submissions = new moodle_url('/mod/iassign/view.php', array('id' => $id, 'action' => 'all_submissions', 'iassignid' => $this->iassign->id));
  4370. $url_import_activity = new moodle_url('/mod/iassign/view.php', array('id' => $id, 'action' => 'import_activity', 'iassignid' => $this->iassign->id));
  4371. print '<div class="btn-group" role="group" aria-label="">' . "\n";
  4372. print '<a href="' . $url_home . '" class="btn btn-outline-primary ' . ($actual_action == '' ? 'active' : '') . '" title="' . get_string('activities_page_help', 'iassign') . '">' . "\n";
  4373. print ' <i class="fa fa-home" aria-hidden="true"></i> ' . get_string('activities_page', 'iassign') . "</a>\n";
  4374. if (has_capability('mod/iassign:viewiassignall', $this->context, $USER->id)) {
  4375. // could be "has_capability('mod/iassign:viewiassignall', $this->context, $USER->id)"
  4376. // Has capability to see "report": teacher or up
  4377. // Menu items: "Activities Page", "About iLM", "Reports", "Results", "", "Auto reevaluation", "Notifications", "Add activity", "\/" (import activity)
  4378. print '<a href="' . $url_help . '" class="btn btn-outline-primary" title="' . get_string('help_ilm_help', 'iassign') . '">' .
  4379. '<i class="fa fa-question-circle" aria-hidden="true"></i> ' . get_string('help_ilm', 'iassign') . "</a>\n" . // Menu: About iLM
  4380. '<div class="btn-group">'. "\n";
  4381. print '<a href="' . $url_report . '" class="btn btn-outline-primary '. ($actual_action == 'report' || $actual_action == 'stats' || $actual_action == 'all_submissions' ? 'active' : '') .
  4382. ' dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" title="' . get_string('report_menu_help', 'iassign') . '">' . "\n" .
  4383. '<i class="fa fa-bar-chart" aria-hidden="true"></i> ' . get_string('report', 'iassign') . "</a>\n"; // Menu: Report | with sub-menus: Home ; Graphics ; Manage all iLM
  4384. print ' <div class="dropdown-menu"> ' . "\n";
  4385. print '<a href="' . $url_report . '" class=" '. ($actual_action == 'report' ? 'active' : '') . ' dropdown-item" title="' . get_string('report_help', 'iassign') . '">'.
  4386. '<i class="fa fa-bar-chart" aria-hidden="true"></i> ' . get_string('home', 'iassign') . '</a>' . "\n" . ' <div class="dropdown-divider"></div>' . "\n"; // Sub-menu: Home (list all)
  4387. print '<a href="' . $url_sub_stats . '" class="dropdown-item ' . ($actual_action == 'stats' ? 'active' : '') . '" title="' . get_string('graphic_help', 'iassign') . '">'.
  4388. '<i class="fa fa-pie-chart" aria-hidden="true"></i> ' . get_string('graphic', 'iassign') . "</a>\n" . // Sub-menu: Graphics
  4389. ' <div class="dropdown-divider"></div>' . "\n";
  4390. print '<a href="' . $url_all_submissions . '" class="dropdown-item ' . ($actual_action == 'all_submissions' ? 'active' : '') . '" title="' . get_string('all_submissions_manager_help', 'iassign') . '">' .
  4391. '<i class="fa fa-files-o" aria-hidden="true"></i> ' . get_string('all_submissions_manager', 'iassign') . " </a>" .
  4392. "</div><!-- div btn-group -->\n"; // Sub-menu: Manage all submissions
  4393. print '<a href="' . $url_stats . '" class="btn btn-outline-primary ' . ($actual_action == 'stats_student' ? 'active' : '') . '" title="' . get_string('results_help', 'iassign') . '">'.
  4394. '<i class="fa fa-list" aria-hidden="true"></i> ' . get_string('results', 'iassign') . "</a>\n"; // Menu: Results
  4395. print '<a href="' . $url_auto_evaluate . '" class="btn btn-outline-primary ' . ($actual_action == 'auto_evaluate' ? 'active' : '') . '" title="' . get_string('auto_evaluate_name_config_help', 'iassign') .
  4396. '"> <i class="fa fa-check-square-o" aria-hidden="true"></i> ' . get_string('auto_evaluate_name_config', 'iassign') . "</a>\n"; // Menu: Auto reevaluation
  4397. }
  4398. $notifications = $this->get_total_notifications();
  4399. print '<a href="' . $url_notifications . '" class="btn btn-outline-primary ' . ($actual_action == 'notifications' ? 'active' : '') . '" title="' . get_string('notifications_help', 'iassign') . '">' . "\n";
  4400. print ' <i class="fa fa-bell" aria-hidden="true"></i> ' . get_string('notifications', 'iassign'); // Notifications
  4401. if ($notifications > 0)
  4402. print ' <span class="badge bg-danger rounded-pill" style="color: white; font-size: .9rem;">' . $notifications . "</span>\n";
  4403. print '</a></div><div class="btn-group">';
  4404. if (has_capability('mod/iassign:editiassign', $this->context, $USER->id)) {
  4405. print '<a href="' . $url_add . '" class="btn btn-success"><i class="fa fa-plus" aria-hidden="true"></i> ' . get_string('add_iassign', 'iassign') . "</a>\n";
  4406. print ' <button type="button" class="btn btn-success dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> '.
  4407. ' </button> <div class="dropdown-menu"> <a class="dropdown-item" href="' . $url_import_activity . '">' . get_string('import_activity', 'iassign') .
  4408. " </a></div>\n";
  4409. } // if (has_capability('mod/iassign:editiassign', $this->context, $USER->id))
  4410. print '</div></div>' . "\n";
  4411. } // function print_main_menu()
  4412. /// Display page of iAssign's block of activities
  4413. function view_iassigns () {
  4414. global $USER, $CFG, $COURSE, $DB, $OUTPUT;
  4415. $id = $this->cm->id;
  4416. $iassign_statement_list = $DB->get_records_list('iassign_statement', 'iassignid', array('iassignid' => $this->iassign->id), 'position ASC');
  4417. $notice = optional_param('notice', '', PARAM_TEXT);
  4418. if (strpos($notice, 'error'))
  4419. print($OUTPUT->notification(get_string($notice, 'iassign'), 'notifyproblem'));
  4420. else if ($notice != '')
  4421. print($OUTPUT->notification(get_string($notice, 'iassign'), 'notifysuccess'));
  4422. $this->print_main_menu(); // teachers' menu: Page of activities; About iLM; Report; Results; Reevaluation; Notifications
  4423. print $OUTPUT->box_start();
  4424. // Put the "Enable editing" or "Disable editing" button - if teacher on
  4425. if (has_capability('mod/iassign:editiassign', $this->context, $USER->id)) {
  4426. print '<div class="border float-right">' . "\n";
  4427. if ($iassign_statement_list) {
  4428. // $USER->iassignEdit == 0 view 'Turn editing off'
  4429. // $USER->iassignEdit == 1 view 'Turn editing on'
  4430. if (!isset($USER->iassignEdit)) // To be used by "./mod/iassign/ilm_handers/*.php"
  4431. $USER->iassignEdit = 0;
  4432. if ($USER->iassignEdit == 0) {
  4433. $bottonEdit_message = get_string('turneditingon', 'iassign');
  4434. $botton = 1;
  4435. }
  4436. elseif ($USER->iassignEdit == 1) {
  4437. $bottonEdit_message = get_string('turneditingoff', 'iassign');
  4438. $botton = 0;
  4439. }
  4440. $editPost = "" . $CFG->wwwroot . "/mod/iassign/view.php?id=" . $id . "&botton=" . $botton;
  4441. print "\n<form name='formEditPost' id='formEditPost' method='post' action='" . $editPost . "' enctype='multipart/form-data'>\n";
  4442. print " <input class='btn btn-primary' type=submit value='" . $bottonEdit_message . "'/>\n";
  4443. print "</form>\n";
  4444. } // if ($iassign_statement_list)
  4445. print "</div>\n";
  4446. }
  4447. // if (has_capability('mod/iassign:submitiassign', $this->context, $USER->id))
  4448. // $this->view_legend_icons();
  4449. print $OUTPUT->box_end();
  4450. $iassign_array_exercise = array();
  4451. $i_exercise = 0; // count total number of activities of type 'exercise' inside the block
  4452. $iassign_array_test = array();
  4453. $i_test = 0; // count total number of activities of type 'test' inside the block
  4454. $iassign_array_example = array();
  4455. $i_example = 0; // count total number of activities of type 'example' inside the block
  4456. $iassign_array_general = array();
  4457. $i_general = 0;
  4458. if ($iassign_statement_list) { // This iAssign block has activities
  4459. if ($this->iassign->activity_group == 0) {
  4460. foreach ($iassign_statement_list as $one_iassign) {
  4461. $iassign_array_general[$i_general] = $one_iassign;
  4462. $i_general++;
  4463. }
  4464. } // if ($this->iassign->activity_group == 0)
  4465. else {
  4466. foreach ($iassign_statement_list as $one_iassign) {
  4467. if ($one_iassign->type_iassign == 3) {
  4468. $iassign_array_exercise[$i_exercise] = $one_iassign;
  4469. $i_exercise++;
  4470. } // if ($one_iassign->type_iassign == 3)
  4471. if ($one_iassign->type_iassign == 2) {
  4472. $iassign_array_test[$i_test] = $one_iassign;
  4473. $i_test++;
  4474. } // if ($one_iassign->type_iassign == 2)
  4475. if ($one_iassign->type_iassign == 1) {
  4476. $iassign_array_example[$i_example] = $one_iassign;
  4477. $i_example++;
  4478. } // if ($one_iassign->type_iassign == 1)
  4479. }
  4480. }
  4481. // Now insert HTML with iAssign statements and their URL
  4482. if ($iassign_array_exercise) {
  4483. $title = get_string('exercise', 'iassign').(count($iassign_array_exercise)>1?'s':'');
  4484. $this->show_iassign($title, $iassign_array_exercise, $i_exercise);
  4485. }
  4486. if ($iassign_array_test) {
  4487. $title = get_string('test', 'iassign').(count($iassign_array_test)>1?'s':'');
  4488. $this->show_iassign($title, $iassign_array_test, $i_test);
  4489. }
  4490. if ($iassign_array_example) {
  4491. $title = get_string('example', 'iassign').(count($iassign_array_example)>1?'s':'');
  4492. $this->show_iassign($title, $iassign_array_example, $i_example);
  4493. }
  4494. if ($iassign_array_general) {
  4495. $title = "";
  4496. $this->show_iassign($title, $iassign_array_general, $i_general);
  4497. }
  4498. } else { // if ($iassign_statement_list)
  4499. print $OUTPUT->notification(get_string('no_activity', 'iassign'), 'notifysuccess');
  4500. }
  4501. if (count($iassign_statement_list) > 5 && !(has_capability('mod/iassign:submitiassign', $this->context, $USER->id))) {
  4502. if (has_capability('mod/iassign:viewiassignall', $this->context, $USER->id)) {
  4503. print $OUTPUT->box_start();
  4504. print '<table width=100% border=0><tr>' . "\n";
  4505. print '<td width=10% align="left">' . "\n";
  4506. print $link_report;
  4507. print '</td>' . "\n";
  4508. print '</tr></table>' . "\n";
  4509. print $OUTPUT->box_end();
  4510. } // if (has_capability('mod/iassign:viewiassignall', $this->context, $USER->id))
  4511. if (has_capability('mod/iassign:editiassign', $this->context, $USER->id)) {
  4512. print $OUTPUT->box_start();
  4513. print '<table width=100% border=0><tr>' . "\n";
  4514. print '<td align="left">' . "\n";
  4515. print $link_add;
  4516. print '</td>' . "\n";
  4517. print '</tr></table>' . "\n";
  4518. print $OUTPUT->box_end();
  4519. } // if (has_capability('mod/iassign:editiassign', $this->context, $USER->id))
  4520. } // if (count($iassign_statement_list) > 5 && !(has_capability('mod/iassign:submitiassign', $this->context, $USER->id)))
  4521. // if (has_capability('mod/iassign:submitiassign', $this->context, $USER->id))
  4522. // $this->view_legend_icons();
  4523. } // function view_iassigns()
  4524. /// Display all iAssigns: the list of activities inside the iAssign block
  4525. // @calledby this->view_iassigns(): $this->show_iassign($title, $iassign_array_exercise, $i_exercise);
  4526. // HTTP: ./mod/iassign/view.php?id= <$id>
  4527. function show_iassign ($title, $iassign_array, $total_of_activities) {
  4528. global $USER, $CFG, $DB, $OUTPUT, $PAGE;
  4529. $id = $this->cm->id; // ID of iAssign block (accessed throgh table "couse_module")
  4530. print $OUTPUT->box_start();
  4531. print " <script type='text/javascript'>
  4532. //<![CDATA[
  4533. function validate_move () {
  4534. var radios = document.getElementsByName('iassign_destiny');
  4535. var formValid = false;
  4536. //alert('aqui validate_move()');
  4537. var i = 0;
  4538. while (!formValid && i < radios.length) {
  4539. if (radios[i].checked) formValid = true;
  4540. i++;
  4541. }
  4542. if (!formValid) {
  4543. document.getElementById('move_dest').classList.add('alert-danger');
  4544. }
  4545. return formValid;
  4546. }
  4547. var modal;
  4548. function load_move(id_ias) {
  4549. document.form_move_activity.iassign_current.value = id_ias;
  4550. modal.style.display = 'block';
  4551. var radios = document.getElementsByName('iassign_destiny');
  4552. var i = 0;
  4553. while (i < radios.length) {
  4554. radios[i].checked = false;
  4555. i++;
  4556. }
  4557. document.getElementById('move_dest').classList.remove('alert-danger');
  4558. }
  4559. function closeModal () {
  4560. modal.style.display = 'none';
  4561. }
  4562. function load_modal_elements () {
  4563. modal = document.getElementById('myModal');
  4564. var span = document.getElementById('close_modal');
  4565. span.onclick = function() {
  4566. modal.style.display = 'none';
  4567. }
  4568. window.onclick = function(event) {
  4569. if (event.target == modal) {
  4570. modal.style.display = 'none';
  4571. }
  4572. }
  4573. }
  4574. window.onload = load_modal_elements;
  4575. //]]>
  4576. </script>\n";
  4577. print " <div id='myModal' class='modal'>\n <div class='modal-content'>\n <span id='close_modal'>&times;</span>\n" .
  4578. " <div id='modal_title' class='moodle-dialogue-hd yui3-widget-hd'>" . get_string('move_activity', 'iassign') . "</div>\n" .
  4579. " <div style='padding: 10px; padding-left: 20px;' id='move_dest'>" . get_string('move_destination', 'iassign') . ":\n <div style='margin: 5px;'>";
  4580. $ccm = get_coursemodule_from_id('iassign', optional_param('id', 0, PARAM_INT));
  4581. $radio_buttons = " <form name='form_move_activity'><input type='hidden' name='action' value='move_activity' />
  4582. <input type='hidden' name='id' value='" . optional_param('id', NULL, PARAM_TEXT) . "' />
  4583. <input type='hidden' name='iassign_current' value='' />";
  4584. $course_sections = get_fast_modinfo($PAGE->course->id)->get_section_info_all();
  4585. $section = $course_sections[0];
  4586. $ids_sections = array_keys($section->__get('modinfo')->get_sections());
  4587. for ($k = 0; $k < count($ids_sections); $k++) {
  4588. $radio_buttons .= get_section_name($PAGE->course->id, $ids_sections[$k]). "<ul style='list-style-type: none'>";
  4589. for ($j = 0; $j < count($section->__get('modinfo')->get_sections()[$ids_sections[$k]]); $j++) {
  4590. $c_cm = get_coursemodule_from_id('iassign', $section->__get('modinfo')->get_sections()[$ids_sections[$k]][$j]);
  4591. if ($c_cm) {
  4592. if ($ccm->instance == $c_cm->instance) {
  4593. $radio_buttons .= " <li><input type='radio' disabled='disabled' id='radio_" . $c_cm->instance . "' name='iassign_destiny' value='" . $c_cm->instance . "'>
  4594. <label for='radio_" . $c_cm->instance . "'>" . $c_cm->name . "</label></li>\n";
  4595. }
  4596. else {
  4597. $radio_buttons .= " <li><input type='radio' id='radio_" . $c_cm->instance . "' name='iassign_destiny' value='" . $c_cm->instance . "'>
  4598. <label class='input_move' for='radio_" . $c_cm->instance . "'>" . $c_cm->name . "</label></li>\n";
  4599. }
  4600. }
  4601. }
  4602. $radio_buttons .= "</ul>";
  4603. }
  4604. print $radio_buttons . " <center><input type='submit' value='OK' onClick='return validate_move();' />\n
  4605. <input type='button' value='" . get_string('cancel', 'iassign') . "' onClick='closeModal()' data-bs-dismiss='modal' /></center>
  4606. </form>
  4607. </div></div> </div></div>\n";
  4608. print " <style>.modal {
  4609. display: none;
  4610. position: fixed;
  4611. z-index: 1;
  4612. left: 0;
  4613. top: 0;
  4614. width: 100%;
  4615. height: 100%;
  4616. overflow: scroll;
  4617. background-color: rgb(0,0,0);
  4618. background-color: rgba(0,0,0,0.4);
  4619. }
  4620. .modal-content {
  4621. background-color: #fefefe;
  4622. margin: 15% auto;
  4623. padding: 10px 10px;
  4624. border: 1px solid #888;
  4625. width: 50%;
  4626. }
  4627. #close_modal {
  4628. color: #aaa;
  4629. float: right;
  4630. font-size: 28px;
  4631. font-weight: bold;
  4632. margin-top: -15px;
  4633. margin-right: -3px;
  4634. }
  4635. #close_modal:hover,
  4636. #close_modal:focus {
  4637. color: black;
  4638. text-decoration: none;
  4639. cursor: pointer;
  4640. }
  4641. #modal_title {
  4642. min-height: 3rem;
  4643. color: initial;
  4644. background: initial;
  4645. font-size: 1.5rem;
  4646. line-height: 1.5;
  4647. padding: 15px;
  4648. border-bottom: 1px solid #e5e5e5;
  4649. }
  4650. .input_move {
  4651. width: 90%;
  4652. padding: 2px;
  4653. }
  4654. .input_move:hover {
  4655. background: #eff0ef;
  4656. }
  4657. </style>";
  4658. $action_links = "";
  4659. if (has_capability('mod/iassign:viewiassignall', $this->context, $USER->id)) {
  4660. print '<h5 class="fw-bold" style="font-weight: 400; margin: -1rem 0 1.5rem 0;">' . $title . "</h5>\n";
  4661. print '<table class="table table-hover" style="border-bottom: 1px solid #dee2e6;">' . "\n";
  4662. for ($j=0; $j<$total_of_activities; $j++) {
  4663. $iassign_current = $iassign_array[$j]->id;
  4664. // receiver=1 - message to teacher
  4665. // receiver=2 - message to student
  4666. $sum_comment = 0;
  4667. $iassign_submissions = $DB->get_records('iassign_submission', array('iassign_statementid' => $iassign_current));
  4668. foreach ($iassign_submissions as $iassign_submission) {
  4669. $params_temp = array('iassign_submissionid' => $iassign_submission->id, 'return_status' => '0', 'receiver' => '1');
  4670. $verify_message = $DB->get_record_sql("SELECT COUNT(iassign_submissionid) FROM {iassign_submission_comment} " .
  4671. " WHERE iassign_submissionid = :iassign_submissionid AND return_status= :return_status AND receiver= :receiver", $params_temp);
  4672. if ($verify_message)
  4673. foreach ($verify_message as $tmp)
  4674. $sum_comment += $tmp;
  4675. } // foreach ($iassign_submissions as $iassign_submission)
  4676. if ($sum_comment == 0)
  4677. $comment_unread = "";
  4678. else {
  4679. $comment_unread_message = get_string('comment_unread', 'iassign');
  4680. if ($sum_comment == 1)
  4681. $comment_unread_message = get_string('comment_unread_one', 'iassign');
  4682. $comment_unread = ' <a href="' . $CFG->wwwroot . '/mod/iassign/view.php?id=' . $id . '&action=report&iassignid=' . $this->iassign->id . '">' .
  4683. ' <span class="badge bg-danger ml-2" style="color:white; font-size: .9em;"><i class="fa fa-envelope" aria-hidden="true"></i> ' . $comment_unread_message .
  4684. ' <span class="badge rounded-pill bg-light text-dark" style="font-size: .8em;">' . $sum_comment . '</span> </span></a>' . "\n";
  4685. }
  4686. if ($j == $total_of_activities - 1)
  4687. $iassign_down = $iassign_array[$j]->id;
  4688. else
  4689. $iassign_down = $iassign_array[$j + 1]->id;
  4690. if ($j > 0)
  4691. $iassign_up = $iassign_array[$j - 1]->id;
  4692. else
  4693. $iassign_up = $iassign_array[$j]->id;
  4694. if ($iassign_array[$j]->visible == 0)
  4695. $links = " <a href='view.php?id=" . $id . "&userid_iassign=" . $USER->id . "&action=view&iassign_current=" . $iassign_current . "'><font color='#bbbbbb'>" . $iassign_array[$j]->name . "</font></a>\n";
  4696. else
  4697. $links = " <a href='view.php?id=" . $id . "&userid_iassign=" . $USER->id . "&action=view&iassign_current=" . $iassign_current . "'>" . $iassign_array[$j]->name . "</a>\n";
  4698. $links .= $comment_unread;
  4699. if (has_capability('mod/iassign:editiassign', $this->context, $USER->id) && $USER->iassignEdit == 1) {
  4700. $action_links = "<nav aria-label='Action links'><ul class='pagination pagination-sm mb-0 my-1 text-success'>\n";
  4701. $str_aux = "&id=" . $id . "&iassign_current=" . $iassign_current . "&iassign_up=" . $iassign_up . "&iassign_down=" . $iassign_down;
  4702. $link_up = "<li class='page-item'><a href='view.php?action=up" . $str_aux . "' class='page-link' title='" . get_string('move_up_iassign', 'iassign') . "'><i class='fa fa-arrow-up' aria-hidden='true'></i></a></li>\n";
  4703. if ($j==0) // first activity
  4704. $link_down = "<li class='page-item'><a href='view.php?action=firstdown" . $str_aux . "' class='page-link' title='" . get_string('move_down_iassign', 'iassign') . "'><i class='fa fa-arrow-down' aria-hidden='true'></i></a></li>\n";
  4705. else
  4706. $link_down = "<li class='page-item'><a href='view.php?action=down" . $str_aux . "' class='page-link' title='" . get_string('move_up_iassign', 'iassign') . "'><i class='fa fa-arrow-down' aria-hidden='true'></i></a></li>\n";
  4707. $link_delete = "<li class='page-item'><a href='view.php?action=delete" . $str_aux . "' class='page-link' title='" . get_string('delete_iassign', 'iassign') . "'><i class='fa fa-trash-o' aria-hidden='true'></i></a></li>\n";
  4708. $link_visible_hide = "<li class='page-item'><a href='view.php?action=visible" . $str_aux . "' class='page-link' title='" . get_string('hide_iassign', 'iassign') . "'><i class='fa fa-eye' aria-hidden='true'></i></a></li>\n";
  4709. $link_visible_show = "<li class='page-item'><a href='view.php?action=visible" . $str_aux . "' class='page-link' title='" . get_string('show_iassign', 'iassign') . "'><i class='fa fa-eye-slash' aria-hidden='true'></i></a></li>\n";
  4710. $link_edit = "<li class='page-item'><a href='view.php?action=edit" . $str_aux . "' class='page-link' title='" . get_string('edit_iassign', 'iassign') . "'><i class='fa fa-pencil-square-o' aria-hidden='true'></i></a></li>\n";
  4711. $link_duplicate_activity = "<li class='page-item'><a href='view.php?action=duplicate_activity" . $str_aux . "' class='page-link' title='" . get_string('duplicate_iassign', 'iassign') . "'><i class='fa fa-clone' aria-hidden='true'></i></a></li>\n";
  4712. $link_move_activity = "<li class='page-item'><a href='#' onclick='load_move(" . $iassign_current . "); return false;' class='page-link' title='" . get_string('move_activity', 'iassign') . "'><i class='fa fa-sign-out' aria-hidden='true'></i></a></li>\n";
  4713. if (count($iassign_array) > 1) {
  4714. if ($j == 0)
  4715. $action_links .= $link_down;
  4716. elseif ($j == $total_of_activities - 1)
  4717. $action_links .= $link_up;
  4718. else
  4719. $action_links .= $link_up . $link_down;
  4720. } // if (count($iassign_array) > 1)
  4721. $action_links .= $link_edit . $link_delete;
  4722. if ($iassign_array[$j]->visible == 0)
  4723. $action_links .= $link_visible_show;
  4724. else
  4725. $action_links .= $link_visible_hide;
  4726. $action_links .= $link_duplicate_activity . $link_move_activity;
  4727. } // if ($USER->iassignEdit == 1 && has_capability('mod/iassign:editiassign', $this->context, $USER->id))
  4728. $action_links .= "</ul></nav>\n";
  4729. print '<tr><td style="white-space: nowrap; width: 1%; padding-right: 2rem;" class="align-middle">' . $links . '</td><td class="align-middle">' . $action_links . "</td></tr>\n";
  4730. } // for ($j=0; $j<$total_of_activities; $j++)
  4731. //D echo "locallib.php: show_iassign():<br/>";
  4732. //EXTRA: This for is to correct error in ordem inside the block, to detect (and fix it) 'iassign_statement.position' errors
  4733. $vector_final_order = array(); // get the final order to be used to correct existent one
  4734. for ($j=0; $j<$total_of_activities; $j++) { // List all iAssign activity inside this block
  4735. //D echo $j . ": id=" . $iassign_array[$j]->id . ", position=" . $iassign_array[$j]->position . "<br/>";
  4736. if ($iassign_array[$j]->position!=($j+1)) {
  4737. activity::move_change_activity_position($DB, $iassign_array[$j]->id, $iassign_array[$j]->id, $j+1, $j+1);
  4738. }
  4739. }
  4740. print "</tbody></table>\n";
  4741. } // if (has_capability('mod/iassign:viewiassignall', $this->context, $USER->id))
  4742. elseif (has_capability('mod/iassign:submitiassign', $this->context, $USER->id)) { // student
  4743. print '<h5 class="fw-bold" style="font-weight: 400; margin: -1rem 0 1.5rem 0;">' . $title . "</h5>\n";
  4744. print '<table class="table table-hover" style="border-bottom: 1px solid #dee2e6;">' . "\n";
  4745. for ($j=0; $j<$total_of_activities; $j++) {
  4746. $icon_status = "";
  4747. $icon_comment = "";
  4748. if ($iassign_array[$j]->visible == 1) {
  4749. $iassign_current = $iassign_array[$j]->id;
  4750. $iassign_submission = $DB->get_record('iassign_submission', array('iassign_statementid' => $iassign_current, 'userid' => $USER->id));
  4751. $links = " <a href='view.php?id=" . $id . "&userid_iassign=" . $USER->id . "&action=view&iassign_current=" . $iassign_current . "'>" . $iassign_array[$j]->name . "</a>\n";
  4752. $icon_status = "";
  4753. $icon_comment = "";
  4754. if ($iassign_submission) {
  4755. // receiver=1 - message to teacher
  4756. // receiver=2 - message to student
  4757. // $verify_message = $DB->get_record_sql("SELECT COUNT(iassign_submissionid) FROM {$CFG->prefix}ia_assign_submissions_comment WHERE iassign_submissionid = '$iassign_submission->id' and return_status= 0 and receiver=2");
  4758. $params_temp = array('iassign_submissionid' => $iassign_submission->id, 'return_status' => '0', 'receiver' => '2');
  4759. $verify_message = $DB->get_record_sql(
  4760. "SELECT COUNT(iassign_submissionid) FROM {iassign_submission_comment} " .
  4761. " WHERE iassign_submissionid = :iassign_submissionid AND return_status= :return_status AND receiver= :receiver", $params_temp);
  4762. if ($verify_message)
  4763. foreach ($verify_message as $tmp)
  4764. $sum_comment = $tmp;
  4765. if ($sum_comment > 0) {
  4766. $comment_unread_message = get_string('comment_unread', 'iassign');
  4767. if ($sum_comment == 1)
  4768. $comment_unread_message = get_string('comment_unread_one', 'iassign');
  4769. $icon_comment = "<a href='view.php?id=" . $id . "&userid_iassign=" . $USER->id . "&action=view&iassign_current=" . $iassign_current . "#comments'>\n" .
  4770. ' <span class="badge bg-danger" style="color:white; font-size: .9em;"><i class="fa fa-envelope" aria-hidden="true"></i> ' . $comment_unread_message . "\n" .
  4771. ' <span class="badge rounded-pill bg-light text-dark" style="font-size: .8em;">' . $sum_comment . "</span> </span></a>\n";
  4772. }
  4773. // $icon_comment = iassign_icons::insert('comment_unread');
  4774. if ($iassign_array[$j]->type_iassign == 3) {
  4775. if ($iassign_array[$j]->show_answer == 1) {
  4776. if ($iassign_submission->status == 3)
  4777. $icon_status = '<i class="fa fa-check" style="color: green; font-size: 1.2rem;" aria-hidden="true" title="' . get_string('correct', 'iassign') . '"></i>' . "\n";
  4778. elseif ($iassign_submission->status == 2)
  4779. $icon_status = '<i class="fa fa-times" style="color: red; font-size: 1.2rem;" aria-hidden="true" title="' . get_string('incorrect', 'iassign') . '"></i>' . "\n";
  4780. elseif ($iassign_submission->status == 1)
  4781. $icon_status = '<i class="fa fa-question" style="color: #77229; font-size: 1.2rem;" aria-hidden="true" title="' . get_string('post', 'iassign') . '"></i>' . "\n";
  4782. //2021/12 $icon_status = iassign_icons::insert('post');
  4783. elseif ($iassign_submission->status == 0)
  4784. $icon_status = '<i class="fa fa-question" style="color: red; font-size: 1.2rem;" aria-hidden="true" title="' . get_string('not_post', 'iassign') . '"></i>' . "\n";
  4785. } // if ($iassign_array[$j]->show_answer==1)
  4786. else {
  4787. if ($iassign_submission->status == 0) {
  4788. //TODO: $icon_status = '<i class="fa fa-question" style="color: #77229c; font-size: 1.2rem;" aria-hidden="true" title="' . get_string('not_post', 'iassign') . '"></i>' . "\n";
  4789. $icon_status = iassign_icons::insert('not_post');
  4790. }
  4791. else {
  4792. //TODO: $icon_status = '<i class="fa fa-question" style="color: #77229c; font-size: 1.2rem;" aria-hidden="true" title="' . get_string('post', 'iassign') . '"></i>' . "\n";
  4793. $icon_status = iassign_icons::insert('post');
  4794. }
  4795. }
  4796. } // if ($iassign_array[$j]->type_iassign == 3)
  4797. } // if ($iassign_submission)
  4798. elseif ($iassign_array[$j]->type_iassign == 3) {
  4799. $icon_status = '<i class="fa fa-question" style="color: #77229c; font-size: 1.2rem;" aria-hidden="true" title="' . get_string('not_post', 'iassign') . '"></i>' . "\n";
  4800. //2021/12 $icon_status = iassign_icons::insert('not_post');
  4801. } // if ($iassign_array[$j]->type_iassign == 3)
  4802. print '<tr><td style="white-space: nowrap; width: 1%; padding-right: 0; text-align: center;">' . $icon_status . '</td><td style="white-space: nowrap; width: 1%; padding-right: 2rem; padding-left: 0.2rem;">'
  4803. . $links . '</td><td>' . $icon_comment . '</td></tr>' . "\n";
  4804. //print '<p>' . $icon_status . ' ' . $links . ' ' . $icon_comment . '</p>' . "\n";
  4805. } // if ($iassign_array[$j]->visible == 1)
  4806. } // for ($j=0; $j<$total_of_activities; $j++)
  4807. print "</table>\n";
  4808. } // elseif (has_capability('mod/iassign:submitiassign', $this->context, $USER->id))
  4809. else if (isguestuser()) {
  4810. print($OUTPUT->notification(get_string('no_permission_iassign', 'iassign'), 'notifyproblem'));
  4811. print '<table width=100% ><tr>' . "\n";
  4812. print "<td width=70% align='left'><font color='#0000aa'><strong>" . $title . "</strong></font></td>" . "\n";
  4813. print '</tr></table>' . "\n";
  4814. for ($j=0; $j<$total_of_activities; $j++) {
  4815. $icon_status = "";
  4816. $icon_comment = "";
  4817. if ($iassign_array[$j]->visible == 1) {
  4818. $iassign_current = $iassign_array[$j]->id;
  4819. $links = " <a href='view.php?id=" . $id . "&userid_iassign=" . $USER->id . "&action=view&iassign_current=" . $iassign_current . "'>" . $iassign_array[$j]->name . "</a>\n";
  4820. print '<p>' . $links . '</p>' . "\n";
  4821. } // if ($iassign_array[$j]->visible == 1)
  4822. }
  4823. }
  4824. print $OUTPUT->box_end();
  4825. } // function show_iassign($title, $iassign_array, $total_of_activities)
  4826. /// Show message of return
  4827. function return_home_course ($message) {
  4828. //D global $DB, $OUTPUT;
  4829. //D $link_return = " <a href='" . $this->return . "'>" . iassign_icons::insert('home') . get_string('activities_page', 'iassign') . "</a>";
  4830. //D echo $OUTPUT->box_start();
  4831. //D echo '<table width=100% border=0 valign="top">' . "\n";
  4832. //D echo '<tr><td align="left"><strong>' . "\n";
  4833. //D print_string($message, 'iassign');
  4834. //D echo '</strong></td>' . "\n";
  4835. //D echo '<td width=20% align="right">' . "\n";
  4836. //D echo $link_return;
  4837. //D echo '</td></tr></table>' . "\n";
  4838. //D echo $OUTPUT->box_end();
  4839. //D // echo $OUTPUT->footer();
  4840. redirect(new moodle_url($this->return . '&notice=' . $message));
  4841. exit;
  4842. }
  4843. /// Search comment of activity
  4844. function search_comment_submission ($iassign_submissionid) {
  4845. global $USER, $DB, $OUTPUT, $COURSE;
  4846. // $context = context_course::instance($COURSE->id);
  4847. $colorEdit1 = "#b7ceee"; // "#dce7ec"
  4848. $colorEdit2 = "#d2e5fc";
  4849. $has_capability = 0;
  4850. $comments = $DB->get_records_list('iassign_submission_comment', 'iassign_submissionid', array('iassign_submissionid' => $iassign_submissionid), 'timecreated DESC'); // 'ORDER BY "timecreated" ASC'
  4851. $text = "";
  4852. if ($comments) {
  4853. $even = 1;
  4854. foreach ($comments as $tmp) {
  4855. $user_data = $DB->get_record("user", array('id' => $tmp->comment_authorid));
  4856. // if (has_capability('mod/iassign:editiassign', $context, $tmp->comment_authorid)) {
  4857. // $text .= "<tr><td bgcolor='#fee7ae'><b> $user_data->firstname</b> (" . userdate($tmp->timecreated) . ")</br>\n";
  4858. // $text .= $tmp->comment . "</td></tr>";
  4859. //} else {
  4860. // $text .= "<tr><td bgcolor='#dce7ec'>&raquo;<b>" . $user_data->firstname . "</b> (" . userdate($tmp->timecreated) . "</br>\n";
  4861. $avatar = new user_picture($user_data);
  4862. $avatar->courseid = $COURSE->id;
  4863. $avatar->link = true;
  4864. $user_picture = $OUTPUT->render($avatar);
  4865. $user_picture = str_replace('"35"', '"45"', $user_picture);
  4866. if ($even) {
  4867. $text .= "<tr><td style='vertical-align: middle; width: 55px !important;' bgcolor='" . $colorEdit1 . "'>$user_picture</td><td bgcolor='" . $colorEdit1 . "' style='padding-left: 0px !important;'><b style='font-size: 1.1rem;'>" . $user_data->firstname . "</b> <span style='color: #686969; font-size: 90%'><i class='fa fa-clock-o' aria-hidden='true' style='margin-left: 2rem; margin-right: .1rem;'></i> " . userdate($tmp->timecreated) . "</span></br>\n";
  4868. $even = 0;
  4869. } else {
  4870. $text .= "<tr><td style='vertical-align: middle; width: 55px !important;' bgcolor='" . $colorEdit2 . "'>$user_picture</td><td bgcolor='" . $colorEdit2 . "' style='padding-left: 0px !important;'><b style='font-size: 1.1rem;'>" . $user_data->firstname . "</b> <span style='color: #686969; font-size: 90%'><i class='fa fa-clock-o' aria-hidden='true' style='margin-left: 2rem; margin-right: .1rem;'></i> " . userdate($tmp->timecreated) . "</span></br>\n";
  4871. $even = 1;
  4872. }
  4873. $text .= "<div style='padding-left: 0rem; padding-top: .2rem;'>" . $tmp->comment . "</div></td></tr>\n";
  4874. } // foreach ($comments as $tmp)
  4875. }
  4876. return $text;
  4877. }
  4878. /// Update comment of activity
  4879. function update_comment ($iassign_submissionid) {
  4880. global $USER, $DB, $OUTPUT;
  4881. if (!has_capability('mod/iassign:submitiassign', $this->context, $USER->id) || is_siteadmin())
  4882. $receiver = 1; // student message to teacher
  4883. else
  4884. $receiver = 2; // teacher message to student
  4885. $verify_message = $DB->get_records('iassign_submission_comment', array('iassign_submissionid' => $iassign_submissionid)); //
  4886. if ($verify_message) {
  4887. foreach ($verify_message as $message) {
  4888. if ($message->receiver == $receiver) {
  4889. $newentry = new stdClass();
  4890. $newentry->id = $message->id;
  4891. $newentry->return_status = 1;
  4892. if (!$DB->update_record('iassign_submission_comment', $newentry)) {
  4893. print_error('error_update', 'iassign');
  4894. } // if (!$DB->update_record('iassign_submission_comment', $newentry))
  4895. } // if ($message->receiver == $receiver)
  4896. // Trigger module viewed event.
  4897. $event = \mod_iassign\event\submission_comment_updated::create(array(
  4898. 'objectid' => $this->iassign->id,
  4899. 'context' => $this->context
  4900. ));
  4901. $event->add_record_snapshot('course', $this->course);
  4902. $event->trigger();
  4903. } // foreach ($verify_message as $message)
  4904. }
  4905. } // function update_comment($iassign_submissionid)
  4906. /// Record comment of activity
  4907. // @calledby locallib.php!view(): $this->action(); // calls '$this->view_iLM();'
  4908. // @calledby __construct(.): $this->view();
  4909. // @calledby referer: http://.../mod/iassign/view.php?action=viewsubmission&id=26981&iassign_submission_current=35665&userid_iassign=10823&iassign_current=6848&view_iassign=1&row=6&column=2
  4910. function write_comment_submission () {
  4911. global $USER, $CFG, $DB;
  4912. $id = $this->cm->id;
  4913. $submission_comment = optional_param('submission_comment', NULL, PARAM_TEXT);
  4914. $row = optional_param('row', 0, PARAM_INT);
  4915. $column = optional_param('column', 0, PARAM_INT);
  4916. $sum_comment = 0;
  4917. $return = "" . $CFG->wwwroot . "/mod/iassign/view.php?action=viewsubmission&id=" . $id . "&iassign_submission_current=" . $this->iassign_submission_current . "&userid_iassign=" . $this->userid_iassign . "&iassign_current=" . $this->activity->get_activity()->id . "&row=" . ($row) . "&column=" . ($column);
  4918. $link_return = " <a href='" . $return . "'>" . iassign_icons::insert('return_home') . get_string('return', 'iassign') . "</a>";
  4919. $str1 = trim($submission_comment);
  4920. $str2 = trim(get_string('box_comment_message', 'iassign'));
  4921. if (!empty($submission_comment) && (strcmp($str1, $str2) != 0)) { // there is comment and it is different from "previous"
  4922. if (has_capability('mod/iassign:submitiassign', $this->context, $USER->id) && !is_siteadmin()) { //MOOC '&& !is_siteadmin()'
  4923. $receiver = 1; // student message to teacher
  4924. $this->action = 'view';
  4925. }
  4926. else {
  4927. $receiver = 2; // teacher/"non-editing teacher" message to student
  4928. $this->action = 'viewsubmission';
  4929. }
  4930. // Get record from 'iassign_submission'
  4931. $iassign_submission = $DB->get_record("iassign_submission", array("id" => $this->iassign_submission_current));
  4932. // Look if the student has previou submission and inform him
  4933. if (!$iassign_submission) {
  4934. $iassign_submission = $DB->get_record("iassign_submission", array("iassign_statementid" => $this->activity->get_activity()->id, "userid" => $USER->id));
  4935. }
  4936. if (!$iassign_submission) { // the student has none submission to this activity, creates an empty one
  4937. $iassign_statement_activity_item = $DB->get_record("iassign_statement", array("id" => $this->activity->get_activity()->id));
  4938. $id_submission = $this->empty_submission($iassign_statement_activity_item->id, $this->userid_iassign, $receiver);
  4939. $this->iassign_submission_current = $id_submission;
  4940. }
  4941. else {
  4942. $id_submission = $iassign_submission->id;
  4943. } // if (!$iassign_submission)
  4944. // $comments = $DB->get_record_sql("SELECT COUNT(iassign_submissionid) FROM {$CFG->prefix}ia_assign_submissions_comment
  4945. // WHERE iassign_submissionid = '$id_submission' and comment='$submission_comment' and comment_authorid='$USER->id'"); //
  4946. // Attention: this Moodle function 'get_record_sql' makes a replace in ':comment'
  4947. $params_temp = array("iassign_submissionid" => $id_submission, "comment" => $submission_comment, "comment_authorid" => $USER->id);
  4948. $comments = $DB->get_record_sql(
  4949. "SELECT COUNT(iassign_submissionid) FROM {iassign_submission_comment} " .
  4950. " WHERE iassign_submissionid = :iassign_submissionid AND comment= :comment AND comment_authorid= :comment_authorid", $params_temp);
  4951. if ($comments)
  4952. foreach ($comments as $tmp)
  4953. $sum_comment = $tmp;
  4954. //DEBUG To debug JavaScript calling, copy here the code in code_to_write_file_through_javascript_calling()
  4955. //DEBUG require_once("ilm_debug/escreva.php"); ... $resp = writeContent("", "", "write_comment_submission_comment.txt", $contexto . "\n" . $texto);
  4956. if ($sum_comment == 0) {
  4957. $newentry = new stdClass();
  4958. $newentry->iassign_submissionid = $id_submission;
  4959. $newentry->comment_authorid = $USER->id;
  4960. $newentry->timecreated = time();
  4961. $newentry->comment = $submission_comment;
  4962. $newentry->receiver = $receiver;
  4963. $ia_assign_submissions_comment_id = $DB->insert_record('iassign_submission_comment', $newentry);
  4964. // Trigger module viewed event
  4965. $event = \mod_iassign\event\submission_comment_created::create(array(
  4966. 'objectid' => $this->iassign->id,
  4967. 'context' => $this->context
  4968. ));
  4969. $event->add_record_snapshot('course', $this->course);
  4970. $event->trigger();
  4971. }
  4972. } // if (!empty($submission_comment) && (strcmp($str1, $str2) != 0))
  4973. // if ($this->action=='viewsubmission') {
  4974. // echo $OUTPUT->header();
  4975. // $this->return_last('confirm_add_comment', $link_return);
  4976. // die;
  4977. // }
  4978. return true;
  4979. } // function write_comment_submission()
  4980. /// Writes one artificial submission, to be associated with {iassign_submission_comment}
  4981. // since {iassign_submission_comment}.iassign_submissionid = {iassign_submission}.id
  4982. // A real register to {iassign_submission} is performed by 'this->get_answer()'
  4983. // @calledby class iassign : function write_comment_submission(): $id_submission = $this->empty_submission($iassign_statement_activity_item->id, $this->userid_iassign, $receiver);
  4984. function empty_submission ($iassignid, $id_user, $receiver) {
  4985. global $USER, $DB, $OUTPUT;
  4986. $newentry = new stdClass();
  4987. $newentry->iassign_statementid = $iassignid;
  4988. $newentry->userid = $id_user;
  4989. $newentry->timecreated = time();
  4990. $newentry->timemodified = time();
  4991. $newentry->answer = 0; // student only submited a message
  4992. if ($receiver == 2) // teacher message to student (write id teacher)
  4993. $newentry->teacher = $USER->id;
  4994. if (!$newentry->id = $DB->insert_record("iassign_submission", $newentry))
  4995. return_home_course('error_insert_submissions');
  4996. else {
  4997. // Trigger module view event
  4998. $event = \mod_iassign\event\submission_created::create(array(
  4999. 'objectid' => $this->iassign->id,
  5000. 'context' => $this->context
  5001. ));
  5002. $event->add_record_snapshot('course', $this->course);
  5003. $event->trigger();
  5004. }
  5005. return $newentry->id;
  5006. }
  5007. /// Return to a specific address of page
  5008. function return_last ($message, $link_return) {
  5009. global $DB, $OUTPUT;
  5010. print $OUTPUT->box_start();
  5011. print '<table width=100% border=0 valign="top">' . "\n";
  5012. print '<tr><td align="left"><strong>' . "\n";
  5013. print_string($message, 'iassign');
  5014. print '</strong></td>' . "\n";
  5015. print '<td width=20% align="right">' . "\n";
  5016. print $link_return;
  5017. print '</td></tr></table>' . "\n";
  5018. print $OUTPUT->box_end();
  5019. print $OUTPUT->footer();
  5020. die();
  5021. } // function return_last($message, $link_return)
  5022. } // class iassign
  5023. /// Class for manage activities
  5024. class activity {
  5025. var $activity;
  5026. /// Constructor of class.
  5027. // @param int $id Id of activity
  5028. // 3.1 update PHP 7.0 compatibility for all moodle versions
  5029. // public function activity($id) { self::__construct($id); }
  5030. function __construct ($id) {
  5031. global $DB;
  5032. $this->activity = $DB->get_record("iassign_statement", array("id" => $id));
  5033. if (empty($this->activity))
  5034. $this->activity = null;
  5035. }
  5036. /// Get an activity
  5037. // @return NULL
  5038. function get_activity () {
  5039. if ($this->activity != null)
  5040. return $this->activity;
  5041. else
  5042. return null;
  5043. }
  5044. /// Delete interactive activities
  5045. function delete ($return) {
  5046. global $USER, $CFG, $DB, $OUTPUT;
  5047. $iassign_submission_currents = $DB->get_records("iassign_submission", array("iassign_statementid" => $this->activity->id));
  5048. print $OUTPUT->header();
  5049. $output .= $OUTPUT->box_start();
  5050. $output .= "<p>" . get_string('delete_activity', 'iassign') . " <strong>" . $this->activity->name . "</strong></p>";
  5051. if ($iassign_submission_currents) {
  5052. $output .= "<p>" . get_string('number_submissions', 'iassign') . " <strong>" . count($iassign_submission_currents) . "</strong></p>";
  5053. if (!has_capability('mod/iassign:deleteiassignnotnull', $USER->context, $USER->id)) {
  5054. $output .= $OUTPUT->heading(get_string('delete_activity_permission_adm', 'iassign'));
  5055. $output .= $OUTPUT->single_button($return, get_string('return', 'iassign'), 'get');
  5056. $output .= $OUTPUT->box_end();
  5057. $output .= $OUTPUT->footer();
  5058. print $output;
  5059. die();
  5060. } // if (!has_capability('mod/iassign:deleteiassignnotnull', $this->context, $USER->id))
  5061. } // if ($iassign_submission_currents)
  5062. else
  5063. $output .= "<p>" . get_string('not_submissions_activity', 'iassign') . "</p>\n";
  5064. $output .= '<table width=50% border=0>';
  5065. $output .= '<tr valign="top"><td>';
  5066. $output .= "<p>" . get_string('what_do', 'iassign') . "</p>\n";
  5067. $output .= '</td><td>';
  5068. $bottonDelete_yes = get_string('delete_iassign', 'iassign');
  5069. $deleteiassignyes = $CFG->wwwroot . "/mod/iassign/view.php?id=" . $USER->cm . "&action=deleteyes&iassign_current=" . $this->activity->id;
  5070. $output .= "<form name='formDelete' id='formDelete' method='post' action='" . $deleteiassignyes . "' enctype='multipart/form-data'>\n";
  5071. $output .= " <input type=submit value='" . $bottonDelete_yes . "'/>\n";
  5072. $output .= "</form>\n";
  5073. $output .= '</td><td>';
  5074. $bottonDelete_no = get_string('delete_cancel', 'iassign');
  5075. $deleteiassignno = $CFG->wwwroot . "/mod/iassign/view.php?id=" . $USER->cm . "&action=deleteno&iassign_current=" . $this->activity->id;
  5076. $output .= "<form name='formDelete' id='formDelete' method='post' action='" . $deleteiassignno . "' enctype='multipart/form-data'>\n";
  5077. $output .= " <p><input type=submit value='" . $bottonDelete_no . "'/></p>\n";
  5078. $output .= "</form>\n";
  5079. $output .= '</td></tr></table>' . "\n";
  5080. $output .= $OUTPUT->box_end();
  5081. $output .= $OUTPUT->footer();
  5082. print $output;
  5083. } // function delete($return)
  5084. /// Function for confirm the delete of activity
  5085. // @param String $return Url of return
  5086. // @param Object $iassign Object content an activity
  5087. function deleteyes ($return, $iassign) {
  5088. global $USER, $CFG, $DB, $OUTPUT, $COURSE;
  5089. $msg = '';
  5090. if (!empty($this->activity->id)) {
  5091. $iassign_submission_currents = $DB->get_records("iassign_submission", array("iassign_statementid" => $this->activity->id));
  5092. if ($iassign_submission_currents) {
  5093. if (has_capability('mod/iassign:deleteassignnull', $USER->context, $USER->id)) {
  5094. foreach ($iassign_submission_currents as $iassign_submission)
  5095. $DB->delete_records('iassign_submission_comment', array('iassign_submissionid' => $iassign_submission->id));
  5096. $delete_iassign_submission_currents = $DB->delete_records("iassign_submission ", array("iassign_statementid" => $this->activity->id));
  5097. } // if ($iassign_submission_currents)
  5098. }
  5099. //$delete_iassign_statement_config = $DB->delete_records('iassign_statement_config', array('iassign_statementid' => $this->activity->id)); //MOOC 2016
  5100. // Remove all files associated to this activity:
  5101. $fs = get_file_storage();
  5102. //TODO 2022: nao deveria ser "$this->activity->file" no lugar de "$this->activity->id"?
  5103. $fs->delete_area_files($iassign->context->id, 'mod_iassign', 'exercise', $this->activity->id);
  5104. $this->delete_calendar($this->activity->id);
  5105. $delete_iassign_current = $DB->delete_records('iassign_statement', array('id' => $this->activity->id));
  5106. iassign::update_grade_iassign($this->activity->iassignid);
  5107. if ($delete_iassign_current) {
  5108. //x $iassign->return_home_course('confirm_del_iassign');
  5109. $iassign->return_home_course(get_string('confirm_del_iassign', 'iassign')); // Activity successfully removed.
  5110. //$msg = get_string('confirm_del_iassign', 'iassign');
  5111. }
  5112. else {
  5113. //x $iassign->return_home_course('error_confirm_del_iassign');
  5114. $iassign->return_home_course(get_string('error_confirm_del_iassign', 'iassign'));
  5115. //$msg = get_string('error_confirm_del_iassign', 'iassign');
  5116. }
  5117. // if (($this->action == 'deleteyes') && (has_capability('mod/iassign:deleteassignnull', $this->context, $USER->id)))
  5118. }
  5119. } // function deleteyes($return, $iassign)
  5120. /// Change fields 'iassign_statement.position' between 2 activities
  5121. // @calledby : class this->move_iassign($move_action, $target, $return)
  5122. // @calledby : class iassign show_iassign($title, $iassign_array, $total_of_activities) - this is used only to fix 'position' erros (e.g. {1,2,2,3} to {1,2,3,4})
  5123. static function move_change_activity_position ($DB, $idFrom, $idTo, $posFrom, $posTo) { //1
  5124. $newentry = new stdClass(); // define a new object (activity) to receive only 2 fields: 'id' and 'position'
  5125. $newentry->id = $idFrom; // ID of the "source activity" //1 $this->activity->id -> $idFrom
  5126. $newentry->position = $posFrom; // final position to source //1 $final_position_source -> $posFrom
  5127. if (!$DB->update_record('iassign_statement', $newentry)) { // modify 'position' of the activity with 'id'=$target
  5128. print_error('error_update_move_iassign', 'iassign');
  5129. }
  5130. // If $idFrom == $idTo then came here from 'this->show_iassign(.)' above, to correct order!
  5131. if ($idFrom != $idTo) { // if $idFrom != $idTo then must update also the targe position
  5132. $newentry->id = $idTo; //1 $iassign_target->id -> $idTo
  5133. $newentry->position = $posTo; //1 $final_position_target -> $posTo
  5134. if (!$DB->update_record('iassign_statement', $newentry))
  5135. print_error('error_update_move_iassign', 'iassign');
  5136. }
  5137. }
  5138. /// Changes position of activities (between those inside a group of interactive activities)
  5139. // @param $move_action = must be "up" or "down"
  5140. // @param $target = if 'down' => is 'iassign_statement.id' of the activity bellow the current one; if 'up' => is activity above the current one
  5141. function move_iassign ($move_action, $target, $return) {
  5142. global $DB, $OUTPUT;
  5143. $initial_position_source = $this->activity->position; // the position of the "source activity" (that one to be moved)
  5144. $iassign_target = $DB->get_record("iassign_statement", array("id" => $target)); // "target activity" = the activity in the position the receive "source activity"
  5145. if ($move_action=="firstdown") {
  5146. $final_position_source = 2;
  5147. $final_position_target = 1;
  5148. }
  5149. else
  5150. if ($move_action=="down") {
  5151. $final_position_target = $this->activity->position;
  5152. $final_position_source = $final_position_target + 1;
  5153. }
  5154. else
  5155. if ($move_action=="up") {
  5156. $final_position_target = $this->activity->position;
  5157. $final_position_source = $final_position_target - 1;
  5158. }
  5159. else { // error:
  5160. print_error('error_update_move_iassign', 'iassign');
  5161. exit;
  5162. }
  5163. activity::move_change_activity_position($DB, $this->activity->id, $iassign_target->id, $final_position_source, $final_position_target); // efectively exchange 'position'
  5164. redirect($return);
  5165. } // function move_iassign($move_action, $target, $return)
  5166. /// Enable or disable the display of interactive activities
  5167. function visible_iassign ($return) {
  5168. global $DB;
  5169. $newentry = new stdClass();
  5170. $newentry->id = $this->activity->id;
  5171. $newentry->visible = $this->activity->visible == 0 ? 1 : 0;
  5172. if (!$DB->update_record('iassign_statement', $newentry))
  5173. print_error('error_update_visible_iassign', 'iassign');
  5174. redirect($return);
  5175. }
  5176. /// Add new interactive activities (register in table 'iassign_statement')
  5177. // After 'add_edit_iassign()' built form (using 'iassign_form.php'), 'new_iassign(.)'
  5178. // is used to process new activity (register im {iassign_statement} and in {files}.
  5179. // The file content arrives here through form, temporaly stored in MoodleData "filedir"
  5180. // @calledby this->add_edit_iassign() : $iassignid = $this->activity->new_iassign($result);
  5181. // @see ilm_manager.php: $ilm_manager_instance->ilm_editor_new();
  5182. function new_iassign ($param) {
  5183. global $DB;
  5184. //D echo "locallib.php:new_iassign(): param->file=" . $param->file . "<br/>";
  5185. $newentry = new stdClass();
  5186. $newentry->iassignid = $param->iassignid;
  5187. $newentry->name = $param->name;
  5188. $newentry->type_iassign = $param->type_iassign;
  5189. $newentry->store_all_submissions = $param->store_all_submissions;
  5190. // $newentry->proposition = $param->proposition; // 2019/02/13 - With the use of 'editor' in 'mform', field 'proposition' is Array ( [text] => <DESCRIPTION> [format] => 1 )
  5191. $proposition = $param->proposition;
  5192. if (is_array($proposition)) $proposition_text = $proposition['text'];
  5193. else $proposition_text = $proposition;
  5194. $newentry->proposition = $proposition_text; // Notice: Trying to get property of non-object
  5195. //TODO I used in 'iassign_form.php' '<select name='iassign_ilmid'...>' with 'optgroup' by hand! But MoodleForm clear/do not register the 'iassign_ilmid'
  5196. //TODO Then get it directly from the form data $_POST!
  5197. //TODO See: 'iassign_form.php' field 'iassign_ilmid' and here (above) 'function add_edit_iassign()'
  5198. if (!isset($param->iassign_ilmid) && isset($_POST['iassign_ilmid']))
  5199. $param->iassign_ilmid = $_POST['iassign_ilmid'];
  5200. $newentry->author_name = $param->author_name;
  5201. $newentry->author_modified_name = $param->author_modified_name;
  5202. $newentry->iassign_ilmid = $param->iassign_ilmid;
  5203. // $param->file is the {files}.id: it came from the attached file (not from {iassign_statement}.file)
  5204. $newentry->file = $param->file; // came from a file from 'get_file_storage()' (it is its ID)
  5205. $newentry->filesid = $param->file; // came from a file from 'get_file_storage()' (it is its ID)
  5206. //TODO criar o campo 'iassign_statement.filesid' e usar: $newentry->filesid = $param->file; // register 'files.id' here in 'iassign_statement.filesid'
  5207. $time_now = time();
  5208. $newentry->grade = $param->grade;
  5209. $newentry->timemodified = $time_now;
  5210. $newentry->timecreated = $time_now;
  5211. if ($param->type_iassign == 1) {
  5212. $newentry->timedue = 0;
  5213. $newentry->timeavailable = 0;
  5214. }
  5215. else {
  5216. $newentry->timedue = $param->timedue;
  5217. $newentry->timeavailable = $param->timeavailable;
  5218. }
  5219. $newentry->preventlate = $param->preventlate;
  5220. $newentry->test = $param->test;
  5221. $newentry->special_param1 = $param->special_param1;
  5222. $newentry->visible = $param->visible;
  5223. $newentry->position = $param->position;
  5224. $newentry->max_experiment = $param->max_experiment;
  5225. $newentry->dependency = $param->dependency;
  5226. $newentry->automatic_evaluate = $param->automatic_evaluate;
  5227. $newentry->show_answer = $param->show_answer;
  5228. // ./lib/dml/moodle_database.php: public abstract function insert_record($table, $dataobject, $returnid=true, $bulk=false)
  5229. // ./lib/dml/mysqli_native_moodle_database.php: public function insert_record($table, $dataobject, $returnid=true, $bulk=false): return $this->insert_record_raw($table, $cleaned, $returnid, $bulk);
  5230. $new_iassign_statement_id = $DB->insert_record("iassign_statement", $newentry); // get the new {iassign_statement}.id
  5231. if ($new_iassign_statement_id) { // now, let's create the reference in {files} to the file in MoodleData
  5232. $component = 'mod_iassign';
  5233. $filearea = 'exercise'; // the associated {files} has field 'filearea="activity"'
  5234. //HISTORY: old version of iAssing (at least since 2012), here was used {files}.filearea = 'activity'
  5235. // Remember: $param->file is the {files}.id (of a temporary file in MoodleData) selected to this activity
  5236. $fs = get_file_storage(); // Get reference to all files in Moodle data
  5237. $new_file_fs = $fs->get_file_by_id($param->file); // remember: $param->file is the {files}.id selected to this activity
  5238. // Attention: $fs->get_file_by_id(.) return is different of $fs->get_area_files(.) that return "./lib/filestorage/file_storage.php" object
  5239. // ATTENTION: {files}.itemid must be equal to {iassign_statement}.id and {iassign_statement}.filesid = {files}.id
  5240. $itemid = $new_iassign_statement_id; // remember: {files}.itemid = {iassign_statement}.id
  5241. //D //TODO Verificar: melhor deixar "$itemid = $new_file_fs->get_itemid()" pois assim 'files.itemid' guarda 'iassign_statement.id'???
  5242. //D echo "new_iassign_statement_id=" . $new_iassign_statement_id . "<br/>\n";
  5243. //D //echo "new_file_fs: id=" . $new_file_fs->get_id() . ", filename=" . $new_file_fs->get_filename() . ", contextid=" . $new_file_fs->get_contextid() .
  5244. //D //", filearea=" . $new_file_fs->get_filearea() . ", itemid=" . $new_file_fs->get_itemid() . ", pathnamehash=" . $new_file_fs->get_pathnamehash() . "<br/>";
  5245. //D echo "new_file_fs: "; print_r($new_file_fs); echo "<br/>";
  5246. //D echo "contextid=" . $param->context->id . ", component=" . $component . ", filearea=" . $filearea . ", itemid=" . $itemid . "<br/>";
  5247. //D //exit;
  5248. // Actual file already inserted in other context (and with filearea='activity') with the iAssign file selector!
  5249. // Make a copy of 'new_file_fs' now with {iassign_statement} context_id and with filearea='exercise'
  5250. // ./lib/filestorage/file_storage.php: class file_storage: create_file_from_storedfile(.): always create 2 entries in {files} (one to directory with filename=".")
  5251. //TODO Verificar se apenas esta' inserindo nova referencia em 'files' (NAO deve duplicar no Moodle Data)
  5252. $newfile_fs = $fs->create_file_from_storedfile(array('contextid' => $param->context->id, 'component' => $component, 'filearea' => $filearea, 'itemid' => $itemid), $new_file_fs);
  5253. //D echo "newfile_fs: id=" . $newfile_fs->get_id() . ", filearea=" . $newfile_fs->get_filearea() . ", itemid=" . $newfile_fs->get_itemid() . "<br/>\n";
  5254. //D //exit;
  5255. $updateentry = new stdClass();
  5256. $updateentry->id = $new_iassign_statement_id;
  5257. $updateentry->filesid = $newfile_fs->get_id(); // {iassign_statement}.filesid must have {files}.id
  5258. if (!$DB->update_record("iassign_statement", $updateentry))
  5259. print_error('error_add', 'iassign');
  5260. if ($param->type_iassign == 3)
  5261. iassign::update_grade_iassign($param->iassignid);
  5262. //TODO iLM_HTML5 :: //MOOC2014
  5263. //D $iassign_ilm_configs = $DB->get_records('iassign_ilm_config', array('iassign_ilmid' => $param->iassign_ilmid, 'visible' => '1'));
  5264. //D if ($iassign_ilm_configs) {
  5265. //D foreach ($iassign_ilm_configs as $iassign_ilm_config) {
  5266. //D if ($iassign_ilm_config->param_type != 'static') {
  5267. //D $newentry = new stdClass();
  5268. //D $newentry->iassign_statementid = $new_iassign_statement_id;
  5269. //D $newentry->iassign_ilm_configid = $iassign_ilm_config->id;
  5270. //D $newentry->param_name = $iassign_ilm_config->param_name;
  5271. //D $newentry->param_value =(is_array($param->{'param_'.$iassign_ilm_config->id}) ? implode(",", $param->{'param_'.$iassign_ilm_config->id}) : $param->{'param_'.$iassign_ilm_config->id});
  5272. //D if (!$DB->insert_record("iassign_statement_config", $newentry))
  5273. //D print_error('error_add_param', 'iassign');
  5274. //D }
  5275. //D }
  5276. //D }
  5277. // log event --------------------------------------------------------------------------------------
  5278. iassign_log::add_log('add_iassign_exercise', 'name: ' . $param->name, $new_iassign_statement_id, $param->iassign_ilmid);
  5279. // log event --------------------------------------------------------------------------------------
  5280. return $new_iassign_statement_id;
  5281. }
  5282. else
  5283. print_error('error_add', 'iassign');
  5284. } // function new_iassign($param)
  5285. /// Add the calendar entries for this iassign
  5286. // @param int $coursemoduleid - Required to pass this in because it might not exist in the database yet
  5287. // @return bool
  5288. static function add_calendar ($iassignid) {
  5289. global $DB, $CFG;
  5290. require_once($CFG->dirroot . '/calendar/lib.php');
  5291. $iassign_statement_activity_item = $DB->get_record("iassign_statement", array("id" => $iassignid));
  5292. $iassign = $DB->get_record("iassign", array("id" => $iassign_statement_activity_item->iassignid));
  5293. $event = new stdClass();
  5294. $event->name = $iassign->name . ' - ' . $iassign_statement_activity_item->name;
  5295. $event->description = $iassign_statement_activity_item->name;
  5296. $event->courseid = $iassign->course;
  5297. $event->groupid = 0;
  5298. $event->userid = 0;
  5299. $event->modulename = 'iassign';
  5300. $event->instance = $iassign->id;
  5301. $event->eventtype = 'due';
  5302. $event->timestart = $iassign_statement_activity_item->timeavailable;
  5303. $event->timeduration = ($iassign_statement_activity_item->timedue - $iassign_statement_activity_item->timeavailable);
  5304. calendar_event::create($event);
  5305. } // static function add_calendar($iassignid)
  5306. /// Update the calendar entries for this iassign
  5307. // @param int $coursemoduleid - Required to pass this in because it might not exist in the database yet
  5308. // @return bool
  5309. function update_calendar ($iassignid, $olddescription) {
  5310. global $DB, $CFG;
  5311. require_once($CFG->dirroot . '/calendar/lib.php');
  5312. $iassign_statement_activity_item = $DB->get_record("iassign_statement", array("id" => $iassignid));
  5313. $iassign = $DB->get_record("iassign", array("id" => $iassign_statement_activity_item->iassignid));
  5314. $event = new stdClass();
  5315. $event->id = 0;
  5316. $events = $DB->get_records('event', array('modulename' => 'iassign', 'instance' => $iassign->id));
  5317. if ($events) {
  5318. foreach ($events as $value) {
  5319. if ($value->description == $olddescription) {
  5320. $event->id = $value->id;
  5321. }
  5322. }
  5323. }
  5324. if ($event->id != 0) {
  5325. $event->name = $iassign->name . ' - ' . $iassign_statement_activity_item->name;
  5326. $event->description = $iassign_statement_activity_item->name;
  5327. $event->timestart = $iassign_statement_activity_item->timeavailable;
  5328. $event->timeduration = ($iassign_statement_activity_item->timedue - $iassign_statement_activity_item->timeavailable);
  5329. $calendarevent = calendar_event::load($event->id);
  5330. $calendarevent->update($event);
  5331. } else
  5332. $this->add_calendar($iassignid);
  5333. }
  5334. /// Update the calendar entries for this iassign
  5335. // @param int $coursemoduleid - Required to pass this in because it might not exist in the database yet
  5336. // @return bool
  5337. function delete_calendar ($iassignid) {
  5338. global $DB, $CFG;
  5339. require_once($CFG->dirroot . '/calendar/lib.php');
  5340. $iassign_statement_activity_item = $DB->get_record("iassign_statement", array("id" => $iassignid));
  5341. $iassign = $DB->get_record("iassign", array("id" => $iassign_statement_activity_item->iassignid));
  5342. $events = $DB->get_records('event', array('modulename' => 'iassign', 'instance' => $iassign->id));
  5343. if ($events) {
  5344. foreach ($events as $value) {
  5345. if ($value->description == $iassign_statement_activity_item->name) {
  5346. $DB->delete_records('event', array('id' => $value->id));
  5347. }
  5348. }
  5349. }
  5350. }
  5351. /// Update interactive activities
  5352. // Reaches this point after teacher "clicks" the "update button" (sending all of his fields update)
  5353. // Here is registered update in {iassign_statement} and {files}
  5354. // @calledby this->activity->add_edit_iassign(): $iassignid = $this->activity->update_iassign($result);
  5355. function update_iassign ($param) {
  5356. global $DB, $USER;
  5357. $component = 'mod_iassign';
  5358. $filearea = 'exercise';
  5359. $fs = get_file_storage(); // Get reference to all files in Moodle data
  5360. $one_file_fs = $fs->get_file_by_id($param->file); // important: $param->file has the {files}.id selected
  5361. $one_file_fsold = $fs->get_file_by_id($param->fileold);
  5362. //DEBUG //REMOVER
  5363. if (!isset($param->fileid)) $param->fileid = NULL;
  5364. $aux_fs_id = "[erro]"; $aux_fs_itemid = "[erro]"; // function update_iassign($param)
  5365. $aux_fs_filename = "[erro]";
  5366. $aux_fs_old_id = "[erro]";
  5367. $aux_fs_old_itemid = "[erro]";
  5368. $aux_fs_old_Filename = "[erro]";
  5369. try {
  5370. if ($one_file_fs) {
  5371. if (!empty($one_file_fs)) $aux_fs_id = $one_file_fs->get_id();
  5372. if (!empty($one_file_fs)) $aux_fs_itemid = $one_file_fs->get_itemid();
  5373. if (!empty($one_file_fs)) $aux_fs_filename = $one_file_fs->get_filename();
  5374. if (!empty($one_file_fsold)) $aux_fs_old_id = $one_file_fsold->get_id();
  5375. if (!empty($one_file_fsold)) $aux_fs_old_itemid = $one_file_fsold->get_itemid();
  5376. if (!empty($one_file_fsold)) $aux_fs_old_Filename = $one_file_fsold->get_filename();
  5377. }
  5378. } catch (Exception $e) {
  5379. echo 'Caught exception: ', $e->getMessage(), "\n";
  5380. }
  5381. if ($USER->id==3) { //D REMOVER
  5382. echo "x locallib.php:update_iassign(.): param->id=" . $param->id, ", param->file=" . $param->file . ", param->filesid=" . $param->fileid . ", param->fileold=" . $param->fileold . "<br/>";
  5383. echo "x one_file_fs : id=" . $aux_fs_id . ", itemid=" . $aux_fs_itemid . ", filename=" . $aux_fs_filename . "<br/>";
  5384. echo "x one_file_fsold : id=" . $aux_fs_old_id . ", itemid=" . $aux_fs_old_itemid . ", filename=" . $aux_fs_old_Filename . "<br/>";
  5385. } //D REMOVER
  5386. //D echo "-------------------------------------------------------------------------------<br/>"; //D //exit;
  5387. if ($param->file != $param->fileold) { // changed the content file: $param->file is the new {files}.id and $param->fileold is the old {files}.id
  5388. if ($one_file_fsold) {
  5389. // Remove old file content in {files}: remove its 2 references in {files} (beside the {files}.id, the other associated with {files}.filename='.'
  5390. $fileoldarea = $fs->get_area_files($one_file_fsold->get_contextid(), $one_file_fsold->get_component(), $one_file_fsold->get_filearea(), $one_file_fsold->get_itemid());
  5391. foreach ($fileoldarea as $one_file_from_fs) {
  5392. if ($USER->id==3) echo " (a) delete(): one_file_from_fs : id=" . $one_file_from_fs->get_id() . ", itemid=" . $one_file_from_fs->get_itemid() . ", filename=" . $one_file_from_fs->get_filename() . "<br/>";
  5393. $one_file_from_fs->delete(); // remove old file from {files} (but keep real content in MoodleData)
  5394. }
  5395. }
  5396. // To get the file to be associated with {iassign_statement}.id = $param->iassign_id
  5397. // $newfile_onefile = NULL;
  5398. $newfile_onefile_final = NULL; // to get {files} with actual file content (not the directory {files}.filename='.')
  5399. $itemid = $param->iassign_id; // {files}.itemid = {iassign_statement}.id
  5400. // If the file was send with $one_file_fs->get_itemid()==0, better use '$param->id'
  5401. // ./lib/filestorage/stored_file.php: public function file_exists($contextid, $component, $filearea, $itemid, $filepath, $filename)
  5402. // if (!$fs->file_exists($param->context->id, $component, $filearea, $one_file_fs->get_itemid(), $one_file_fs->get_filepath(), $one_file_fs->get_filename()))
  5403. // if (!$fs->file_exists($param->context->id, $component, $filearea, $param->id, $one_file_fs->get_filepath(), $one_file_fs->get_filename()))
  5404. if (!$fs->file_exists($param->context->id, $component, $filearea, $itemid, $one_file_fs->get_filepath(), $one_file_fs->get_filename())) {
  5405. // The file is new in {files} (perhaps it is not new in Moodle Data)
  5406. if ($USER->id==3) { //D REMOVER
  5407. echo "x USER==3: entrou em !fs->file_exists: filearea=" . $filearea . ", get_itemid=" . $aux_fs_itemid . ", get_filepath=" . $one_file_fs->get_filepath() . "<br/>";
  5408. echo "x * Acabou de remover 2 referencias velhas para {files}, agora deve criar 2 novas<br/>";
  5409. echo "x one_file_fs->get_id()=" . $aux_fs_id . ", one_file_fs->get_itemid()=" . $aux_fs_itemid . ", one_file_fs->get_filename()=" . $aux_fs_filename . "<br/>";
  5410. echo "x param->file=" . $param->file . ", param->fileold=" . $param->fileold . "<br/>";
  5411. echo "x itemid=" . $itemid . " <- param->iassign_id=" . $param->iassign_id . ", param->id=" . $param->id . "<br/>";
  5412. echo "x new_iassign_statement_id = " . $new_iassign_statement_id . "<br/>";
  5413. //exit;
  5414. } //D
  5415. //exit; //REMOVER exit
  5416. //D $itemid = $one_file_fs->get_itemid() + $param->iassign_id; //REVER!!!! $param->iassign_id tem o valor do {iassign_statement}.id
  5417. //2022/09/27 $itemid = $param->id; // {files}.itemid = {iassign_statement}.id
  5418. //D moved up: $itemid = $param->iassign_id; // {files}.itemid = {iassign_statement}.id
  5419. // ./lib/filestorage/file_storage.php : "public function create_file_from_storedfile($filerecord, $fileorid)" 1051/2412 on Moodle 2018120302.08
  5420. $newfile_fs = $fs->create_file_from_storedfile(array('contextid' => $param->context->id, 'component' => $component, 'filearea' => $filearea, 'itemid' => $itemid), $one_file_fs);
  5421. // Exception - sizeof(): Argument #1 ($value) must be of type Countable|array, stored_file given
  5422. // Exception - sizeof(): Argument #1 ($value) must be of type Countable|array, stored_file given
  5423. if ($USER->id==3)
  5424. echo "x USER.id=3: Criado novo arquivo? com contextid=".$param->context->id.", itemid=".$itemid." => #newfile_fs=" .
  5425. ((is_null($newfile_fs) || !is_array($newfile_fs)) ? 0 : sizeof($newfile_fs)) . "<br/>";
  5426. if (is_array($newfile_fs)) {
  5427. // Aditional security: "./lib/filestorage/stored_file.php : create_file_from_storedfile(.)" return only one file
  5428. foreach ($newfile_fs as $newfile_onefile) { // trocar as referencias {files}.itemid por {iassign_statement}.id
  5429. //if ($newfile_onefile->get_filename()!=".") { break; } }
  5430. if ($newfile_onefile->get_itemid()!= $param->iassign_id) {
  5431. if ($USER->id==3) { echo "x Entrou na troca de temp_file.itemid=" . $newfile_onefile->get_itemid() . " <- " . $param->iassign_id . "<br/>"; } //D
  5432. echo "x Cria FS em foreach: temp_file.itemid=" . $newfile_onefile->get_itemid() . ", get_filepath=" . $newfile_onefile->get_filepath() . "<br/>";
  5433. echo "x temp_file->get_id()=" . $newfile_onefile->get_id() . ", temp_file->get_itemid()=" . $newfile_onefile->get_itemid() . ", temp_file->get_filename()=" . $newfile_onefile->get_filename() . "<br/>";
  5434. // Change the {files}.itemid to the current {iassign_statement}.id
  5435. require_once 'files_functions.php';
  5436. $error = update_files($DB, $newfile_onefile->get_id(), $param->iassign_id);
  5437. }
  5438. if ($newfile_onefile->get_filename()!='.')
  5439. $newfile_onefile_final = $newfile_onefile; // file with the content
  5440. } // foreach
  5441. } // if (is_array($newfile_fs))
  5442. else {
  5443. // Since "./lib/filestorage/stored_file.php : create_file_from_storedfile(.)" return only one file use this "else"
  5444. if ($USER->id==3) echo "x USER.id=3: Cria FS fora de laco! newfile_onefile_final: id=". $newfile_fs->get_id() . ", filename=" . $newfile_fs->get_filename() . "<br/>";
  5445. $newfile_onefile_final = $newfile_fs;
  5446. }
  5447. if ($USER->id==3) { //D REMOVER!
  5448. if ($newfile_onefile_final==NULL) {
  5449. echo "x xxxx Erro: newfile_onefile_final == NULL<br/>Remove {files}: "; //D
  5450. if (is_array($newfile_fs)) { //D
  5451. foreach ($newfile_fs as $newfile_onefile) { echo "x id=" . $newfile_onefile->get_id() . ", itemid=" . $newfile_onefile->get_itemid() . ", "; $newfile_onefile->delete(); }
  5452. } //D
  5453. else { echo "x Remove apenas 'newfile_fs': id=" . $newfile_fs->get_id() . "<br/>"; $newfile_fs->delete(); } //D
  5454. } //D
  5455. }
  5456. //D $param->file = $newfile_fs->get_itemid();
  5457. $param->file = $newfile_onefile_final->get_id(); // prepare to update {iassign_statement}.filesid = {files}.id
  5458. $param->filesid = $param->file;
  5459. if ($USER->id==3) { //D REMOVER!
  5460. echo "x * atualiza 1 {iassign_statement}.file <- " . $param->file . "<br/>\n";
  5461. echo "x * atualiza 1 {iassign_statement}.filesid <- " . $param->filesid . "<br/>\n";
  5462. }
  5463. }
  5464. else { // if (!$fs->file_exists($param->context->id, $component, $filearea, $param->id, $one_file_fs->get_filepath(), $one_file_fs->get_filename()))
  5465. // There exist one {files} in this context, with this name, is there an ERROR?
  5466. // Try to recover with this previous {files}
  5467. if ($USER->id==3) { //D
  5468. echo "x locallib.php:update_iassign(.): *** USER==3: entrou em fs->file_exists: provavel ERRO de colisao de arquivos!<br/>";
  5469. echo "x filearea=" . $filearea . ", param->iassign_id=" . $param->iassign_id . "";
  5470. }
  5471. $newfile_fs = $fs->get_area_files($param->context->id, $component, $filearea, $param->id);
  5472. foreach ($newfile_fs as $temp_file) { // trocar as referencias {files}.itemid por {iassign_statement}.id
  5473. //if ($temp_file->get_filename()!=".") { break; } }
  5474. if ($USER->id==3) { //REMOVER
  5475. echo "x get_itemid=" . $aux_fs_itemid . ", get_filepath=" . $one_file_fs->get_filepath() . "<br/>";
  5476. echo "x one_file_fs->get_id()=" . $one_file_fs->get_id() . ", one_file_fs->get_itemid()=" . $aux_fs_itemid . ", one_file_fs->get_filename()=" . $aux_fs_filename . "<br/>";
  5477. // echo "x param="; print_r($param); echo "x <br/>param->name=" . $param->name . "<br/>";
  5478. echo "x param->id=" . $param->id . ", param->name=" . $param->name . "<br/>";
  5479. echo "x param->file=" . $param->file . ", param->fileold=" . $param->fileold . "<br/>";
  5480. echo "x itemid=" . $param->id . "<br/>";
  5481. echo "x newfile_fs->get_id=" . $temp_file->get_id() . "<br/>";
  5482. echo "x newfile_fs->get_itemid=" . $temp_file->get_itemid() . "<br/>";
  5483. echo "x newfile_fs->get_filename=" . $temp_file->get_filename() . "<br/>";
  5484. } //REMOVER
  5485. if ($temp_file->get_itemid() != $param->iassign_id) {
  5486. if ($USER->id==3) { echo "x Entrou na troca de temp_file.itemid=" . $temp_file->get_itemid() . " <- " . $param->iassign_id . "<br/>"; } //D
  5487. // Change the {files}.itemid to the current {iassign_statement}.id
  5488. require_once 'files_functions.php';
  5489. $error = update_files($DB, $temp_file->get_id(), $param->iassign_id);
  5490. }
  5491. if ($temp_file->get_filename()!='.')
  5492. $newfile_onefile_finel = $temp_file; // file with the content
  5493. }
  5494. } // else if (!$fs->file_exists($param->context->id, $component, $filearea, $param->id, $one_file_fs->get_filepath(), $one_file_fs->get_filename()))
  5495. //exit; //REMOVER exit
  5496. if ($newfile_onefile_final) { // additional security...
  5497. // $param->file = $newfile_fs->get_id();
  5498. $param->file = $newfile_onefile_final->get_id(); // prepare to update {iassign_statement}.filesid = {files}.id
  5499. $param->filesid = $param->file;
  5500. if ($USER->id==3) {
  5501. echo "x * atualiza 2 {iassign_statement}.file <- " . $param->file . "<br/>\n";
  5502. echo "x * atualiza 2 {iassign_statement}.filesid <- " . $param->filesid . "<br/>\n";
  5503. }
  5504. }
  5505. else {
  5506. print("update_iassign: error, 'newfile_onefile_final' empty!<br/>"); //TODO Remove this, use 'thrown'
  5507. // print_error('error_security', 'iassign'); // ./lib/setuplib.php: moodle_exception thrown
  5508. }
  5509. }
  5510. else {
  5511. // one_file_fs=stored_file Object ( [fs:stored_file:private] => file_storage Object ( [tempdir:file_storage:private] => ... ) [file_record:stored_file:private] => stdClass Object ( [id] => 8 [contenthash] => ... ) ... )
  5512. $param->file = $one_file_fs->get_id();
  5513. $param->filesid = $param->file;
  5514. if ($USER->id==3) { // - only {user}.id==3 sees it
  5515. echo "x * atualiza 3 {iassign_statement}.file <- " . $param->file . "<br/>\n";
  5516. echo "x * atualiza 3 {iassign_statement}.filesid <- " . $param->filesid . "<br/>\n";
  5517. }
  5518. }
  5519. $newentry = new stdClass();
  5520. $newentry->id = $param->iassign_id;
  5521. $newentry->name = $param->name;
  5522. $newentry->type_iassign = $param->type_iassign;
  5523. $newentry->store_all_submissions = $param->store_all_submissions;
  5524. //2019 $newentry->proposition = $param->proposition;
  5525. $proposition = $param->proposition;
  5526. if (is_array($proposition))
  5527. $proposition_text = $proposition['text'];
  5528. else
  5529. $proposition_text = $proposition;
  5530. $newentry->proposition = $proposition_text;
  5531. $newentry->iassign_ilmid = $param->iassign_ilmid;
  5532. $newentry->file = $param->file; // old field of {iassign_statement} to stablish relation with {files}.itemid (now {iassign_statement}.filesid={files}.id
  5533. if (!$param->filesid) // additional security: $param->filesid is missing, but it must have the {files}.id
  5534. $newentry->filesid = $param->file;
  5535. else
  5536. $newentry->filesid = $param->filesid; // already defined
  5537. $newentry->grade = $param->grade;
  5538. if (isset($param->author_name)) // 2023: every time edit activity change old author "firstname&nbsp;lastname" to "firstname lastname ({user}.id)"
  5539. $newentry->author_name = $param->author_name;
  5540. $newentry->author_modified_name = $param->author_modified_name;
  5541. $newentry->timemodified = time();
  5542. if ($param->type_iassign == 1) {
  5543. $newentry->timedue = 0;
  5544. $newentry->timeavailable = 0;
  5545. } // if ($param->type_iassign == 1)
  5546. else {
  5547. $newentry->timedue = $param->timedue;
  5548. $newentry->timeavailable = $param->timeavailable;
  5549. }
  5550. $newentry->preventlate = $param->preventlate;
  5551. $newentry->test = $param->test;
  5552. if (isset($param->special_param1))
  5553. $newentry->special_param1 = $param->special_param1;
  5554. else
  5555. $newentry->special_param1 = NULL;
  5556. $newentry->visible = $param->visible;
  5557. $newentry->max_experiment = $param->max_experiment;
  5558. $newentry->dependency = $param->dependency;
  5559. $newentry->automatic_evaluate = $param->automatic_evaluate;
  5560. $newentry->show_answer = $param->show_answer;
  5561. if (!$DB->update_record("iassign_statement", $newentry))
  5562. print_error('error_update', 'iassign');
  5563. if ($param->type_iassign == 3) {
  5564. iassign::update_grade_iassign($param->iassignid);
  5565. }
  5566. //$id = $newentry->id; // MOOC 2016 --- inicio
  5567. //$iassign_activity_item_configs = $DB->get_records('iassign_statement_config', array('iassign_statementid' => $newentry->id));
  5568. //if ($iassign_activity_item_configs) {
  5569. //foreach ($iassign_activity_item_configs as $iassign_activity_item_config) {
  5570. // $newentry = new stdClass();
  5571. // $newentry->id = $iassign_activity_item_config->id;
  5572. // $newentry->param_value =(is_array($param->{'param_'.$iassign_activity_item_config->iassign_ilm_configid}) ? implode(",",
  5573. // $newentry->param_value =(is_array($param->{'param_' . $iassign_activity_item_config->iassign_ilm_configid}) ? implode(",",
  5574. // $param->{'param_' . $iassign_activity_item_config->iassign_ilm_configid}) : $param->{'param_' . $iassign_activity_item_config->iassign_ilm_configid});
  5575. // if (!$DB->update_record("iassign_statement_config", $newentry))
  5576. // print_error('error_edit_param', 'iassign');
  5577. // }
  5578. // } // MOOC 2016 --- final
  5579. // log event --------------------------------------------------------------------------------------
  5580. iassign_log::add_log('update_iassign_exercise', 'name: ' . $param->name, $param->iassign_id, $param->iassign_ilmid);
  5581. // log event --------------------------------------------------------------------------------------
  5582. // remove old file from {files} (and from MoodleData?)
  5583. // remove old file from {files} (but keep real content in MoodleData)
  5584. //D REMOVER
  5585. // - only {user}.id==3 sees it
  5586. if ($USER->id==3) echo "x * Acabou de atualizar {iassign_statement}.id=" . $newentry->id . "=" . $param->iassign_id . " com {ia}.filesid=" . $newentry->filesid . " e {files}.itemid=" . $newentry->id . " (espero)<br/>";
  5587. // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  5588. return $newentry->id; // to be used by this->add_edit_iassign() to update calendar
  5589. // if ($param->type_iassign==3)
  5590. // $this->update_grade_iassign($param->iassignid);
  5591. } // function update_iassign($param)
  5592. /// Show information of activity
  5593. function show_info_iassign () {
  5594. global $DB, $OUTPUT;
  5595. $output = '';
  5596. if ($this->activity->type_iassign == 3) {
  5597. if ($this->activity->dependency == 0) {
  5598. $output .= '<p style="margin-top: -1rem;"><strong>' . get_string('independent_activity', 'iassign') . '</strong>' . "\n";
  5599. }
  5600. else {
  5601. $dependencys = explode(';', $this->activity->dependency);
  5602. $output .= '<p style="margin-top: -1rem;"><strong>' . get_string('dependency', 'iassign') . '</strong>';
  5603. foreach ($dependencys as $dependency) {
  5604. $dependencyiassign = $DB->get_record("iassign_statement", array("id" => $dependency));
  5605. if ($dependencyiassign)
  5606. $output .= '<p>' . $dependencyiassign->name . '</p>' . "\n";
  5607. } // foreach ($dependencys as $dependency)
  5608. } // if ($iassign_statement_activity_item->dependency == 0)
  5609. if ($this->activity->max_experiment == 0)
  5610. $output .= '<strong style="margin-left: 3rem;">' . get_string('experiment', 'iassign') . '</strong> ' . get_string('ilimit', 'iassign');
  5611. else
  5612. $output .= '<strong style="margin-left: 3rem;">' . get_string('experiment_iassign', 'iassign') . '</strong> ' . $this->activity->max_experiment . "\n";
  5613. $output .= '<strong style="margin-left: 3rem;">' . get_string('grade_iassign', 'iassign') . ':</strong> ' . $this->activity->grade . '</p>' . "\n";
  5614. } // if ($iassign_statement_activity_item->type_iassign == 3)
  5615. $output .= $this->toggle_columns_script();
  5616. $output .= '<p><strong>' . get_string('proposition', 'iassign') . ':</strong></p> <div class="proposition">' . $this->activity->proposition . '</div>' . "\n";
  5617. print $OUTPUT->box($output);
  5618. } // function show_info_iassign()
  5619. /// Provide script to toggle columns
  5620. static function toggle_columns_script () {
  5621. $output = "";
  5622. $output .= '<script>
  5623. function toLeft () {
  5624. document.getElementsByClassName("proposition")[0].style.cssFloat = "left";
  5625. document.getElementsByClassName("proposition")[0].style.width = "40%";
  5626. document.getElementById("fullscreen").style.marginTop = "-3rem";
  5627. document.getElementById("columnsbutton").style.color = "#1da075";
  5628. }
  5629. function toDefault () {
  5630. document.getElementsByClassName("proposition")[0].style.cssFloat = "none";
  5631. document.getElementsByClassName("proposition")[0].style.width = "100%";
  5632. document.getElementById("fullscreen").style.marginTop = "0";
  5633. document.getElementById("columnsbutton").style.color = "#343a40";
  5634. }
  5635. var activate = false;
  5636. function toggleColumns () {
  5637. if (activate) {
  5638. activate = false;
  5639. toDefault();
  5640. } else {
  5641. activate = true;
  5642. toLeft();
  5643. }
  5644. }
  5645. </script>'; // '
  5646. $output .= '<span tabindex="0" data-toggle="tooltip" title="' . get_string('enable_disable_columns', 'iassign')
  5647. . '" style="float: right"> <i id="columnsbutton" onclick="toggleColumns()" class="fa fa-columns" style="font-size: 1.5rem;" aria-hidden="true" role="button"></i></span>' . "\n";
  5648. return $output;
  5649. } // static function toggle_columns_script()
  5650. static function get_latest_array_item ($DB, $iassignid, $position) { //TODO: may be search for the greatest grade?
  5651. $iassign_statement = $DB->get_records('iassign_statement', array('iassignid' => $iassignid, 'position' => $position));
  5652. $item = NULL;
  5653. foreach ($iassign_statement as $item) ; // get the last item!
  5654. if ($item) return $item; // return the last one
  5655. return NULL;
  5656. }
  5657. /// Shows date of opening and closing activities
  5658. function view_dates () {
  5659. global $USER, $CFG, $DB, $OUTPUT;
  5660. $return = $CFG->wwwroot . "/mod/iassign/view.php?id=" . $USER->cm;
  5661. $link_return = " <a href='" . $return . "'>" . iassign_icons::insert('home') . get_string('activities_page', 'iassign') . "</a>";
  5662. $status_iassign = "";
  5663. $status_iassign1 = "";
  5664. $status_iassign2 = "";
  5665. if ($this->activity->type_iassign == 1) // activity of type example
  5666. $type_iassign = get_string('example_iassign', 'iassign');
  5667. elseif ($this->activity->type_iassign == 2) { // activity of type test
  5668. $type_iassign = get_string('test_iassign', 'iassign');
  5669. if ($this->activity->timeavailable > time()) {
  5670. $status_iassign = get_string('previous_timeavailable', 'iassign');
  5671. } elseif ($this->activity->timedue < time()) {
  5672. $status_iassign = get_string('last_timedue', 'iassign');
  5673. }
  5674. } elseif ($this->activity->type_iassign == 3) { // activity of type exercise
  5675. $type_iassign = get_string('exercise_iassign', 'iassign');
  5676. if ($this->activity->timeavailable > time()) {
  5677. $status_iassign = get_string('previous_timeavailable', 'iassign'); // before of deadline
  5678. } elseif ($this->activity->timedue < time()) { // after delivery
  5679. $status_iassign = get_string('last_timedue', 'iassign');
  5680. if ($this->activity->preventlate == 1) // permitted to submit after the deadline
  5681. $status_iassign1 = get_string('duedate_preventlate_enable', 'iassign');
  5682. elseif ($this->activity->preventlate == 0) { // not permitted to submit after the deadline
  5683. $status_iassign1 = get_string('duedate_preventlate_desable', 'iassign');
  5684. if ($this->activity->test == 1) // allowed to test after of deadline
  5685. $status_iassign2 = get_string('test_preventlate', 'iassign');
  5686. elseif ($this->activity->test == 0) { // not allowed to test after of deadline
  5687. $status_iassign2 = get_string('test_preventlate_no', 'iassign');
  5688. } // elseif ($iassign_statement_activity_item->test == 0)
  5689. } // elseif ($iassign_statement_activity_item->preventlate == 0)
  5690. } // elseif ($iassign_statement_activity_item->timedue < time())
  5691. } // elseif ($iassign_statement_activity_item->type_iassign == 3)
  5692. $output = '<table width=100% >' . "\n";
  5693. $output .= '<tr><td colspan=2><h4>' . $this->activity->name . '</h4></td></tr>' . "\n";
  5694. $output .= '<tr>' . "\n";
  5695. //leo testes para passar por cima com 'has_capability('mod/iassign:...', $this->context, $USER->id)
  5696. $output .= '<td width=60%>' . $type_iassign . '</td>' . "\n";
  5697. //leo $output .= '<td width=80%>' . $type_iassign;
  5698. // $output .= $auxStr . " - status_assign=$status_iassign - this->activity->type_iassign=" . $this->activity->type_iassign. "<br/>"; // Period ended.
  5699. // $output .= '</td>' . "\n";
  5700. if (has_capability('mod/iassign:viewiassignall', $USER->context, $USER->id) && ($this->activity->type_iassign == 3)) {
  5701. // Link (with icon) to report survey (to present all interactivy exercises inside this block)
  5702. $link_report = "<a href='" . $CFG->wwwroot . "/mod/iassign/view.php?id=" . $USER->cm . "&action=report&iassignid=" . $this->activity->iassign_ilmid . "'>" . iassign_icons::insert('view_report') . ' ' . get_string('report', 'iassign') . "</a>";
  5703. $output .= '<td width=40% align="right">' . ' ' . $link_report . '</td>' . "\n";
  5704. }
  5705. else {
  5706. // Get all previous/next solution sent by this student (present link to his last answer)
  5707. // $iassign_previous = $DB->get_record('iassign_statement', array('iassignid' => $this->activity->iassignid, 'position' => $this->activity->position - 1));
  5708. $iassign_previous = activity::get_latest_array_item($DB, $this->activity->iassignid, $this->activity->position-1); // previous activity of this student inside this block
  5709. // $iassign_next = $DB->get_record('iassign_statement', array('iassignid' => $this->activity->iassignid, 'position' => $this->activity->position + 1));
  5710. $iassign_next = activity::get_latest_array_item($DB, $this->activity->iassignid, $this->activity->position+1); // next activity of this student inside this block
  5711. $link_next = "";
  5712. $link_previous = "";
  5713. if ($iassign_previous) { // previous_activity
  5714. $url_previous = "view.php?id=" . $USER->cm . "&userid_iassign=" . $USER->id . "&action=view&iassign_current=" . $iassign_previous->id;
  5715. $link_previous = "<a href='" . $url_previous . "'>" . (iassign_icons::insert('previous_student_activity')) . "</a>\n";
  5716. }
  5717. if ($iassign_next) { // next_activity
  5718. $url_next = "view.php?id=" . $USER->cm . "&userid_iassign=" . $USER->id . "&action=view&iassign_current=" . $iassign_next->id;
  5719. $link_next = "<a href='" . $url_next . "'>" . (iassign_icons::insert('next_student_activity')) . "</a>\n";
  5720. }
  5721. $output .= '<td width=40% align="right">' . $link_previous . ' &nbsp; ' . $link_return . ' &nbsp; ' . $link_next . '</td>' . "\n";
  5722. } // if (has_capability('mod/iassign:viewiassignall', $this->context, $USER->id) && ($iassign_statement_activity_item->type_iassign == 3))
  5723. $output .= '</tr></table>' . "\n";
  5724. $output .= '<table width=100% >' . "\n";
  5725. if ($this->activity->type_iassign > 1) {
  5726. if ($this->activity->timeavailable)
  5727. $output .= '<tr><td width=50% align="left"> <strong>' . get_string('availabledate', 'iassign') . ':</strong> ' . userdate($this->activity->timeavailable) . '</td>' . "\n";
  5728. if ($this->activity->timedue)
  5729. $output .= '<td width=50% align="left"><strong>' . get_string('duedate', 'iassign') . ':</strong> ' . userdate($this->activity->timedue) . '</td>' . "\n";
  5730. } // if ($iassign_statement_activity_item->type_iassign > 1)
  5731. if ($status_iassign != "" && $status_iassign1 != "" && $status_iassign2 != "")
  5732. $output .= '<tr><td><font color="red">' . $status_iassign . ' ' . $status_iassign1 . ' ' . $status_iassign2 . '</font></td></tr>' . "\n";
  5733. $output .= '</table>' . "\n";
  5734. print $OUTPUT->box($output);
  5735. } // function view_dates()
  5736. } // class activity
  5737. /// Class to manage Interactive Learning Module (iLM)
  5738. class ilm {
  5739. var $ilm;
  5740. /// Constructor of class
  5741. // @param int $id Id of iLM
  5742. //D 3.1 update PHP 7.0 compatibility for all moodle versions
  5743. //D public function ilm($id) { self::__construct($iassign, $cm, $course); }
  5744. function __construct ($id) {
  5745. global $DB;
  5746. $this->ilm = $DB->get_record("iassign_ilm", array("id" => $id));
  5747. if (empty($this->ilm))
  5748. $this->ilm = null;
  5749. }
  5750. /// Shows activity in iLM ($iassign_statement_activity_item{author_name, iassign_ilmid, file, ...})
  5751. // @calledby this->view_iassign_current() from 7 points, all calling ./ilm_handlers/html5.php:show_activity_in_ilm(.)
  5752. // @calledby view_iassign_current() : print $OUTPUT->box($ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, true));
  5753. // @calledby view_iassign_current() : print $ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, false);
  5754. // @calledby view_iassign_current() : print $OUTPUT->box($ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, $loadTeacherActivity));
  5755. // @calledby view_iassign_current() : $output .= $ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, true);
  5756. // @calledby view_iassign_current() : $output .= $ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, true);
  5757. // @calledby view_iassign_current() : $output .= $ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, $loadTeacherActivity);
  5758. // @calledby view_iassign_current() : $output = $ilm->view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, true);
  5759. function view_iLM ($iassign_statement_activity_item, $student_answer, $enderecoPOST, $view_teacherfileversion) {
  5760. global $USER, $CFG, $DB;
  5761. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $iassign_statement_activity_item->iassign_ilmid));
  5762. // $iassign_ilm = $DB->get_records_sql("SELECT * FROM {iassign_ilm} WHERE id=" . $iassign_statement_activity_item->iassign_ilmid);
  5763. if (!$iassign_ilm) {
  5764. // Currently, the backup store the original iLM in table {iassign_ilm} what could result error in restored Moodle.
  5765. // In backup/restore, if the iLM does not exist in the restored Moodle, it is impossible to present the activity.
  5766. // The teacher could to associate the activity to another compatible iLM (other version).
  5767. print "The iLM (with id indicated) was not found! (" . $iassign_statement_activity_item->iassign_ilmid . ")<br/>\n";
  5768. print "Perhaps you could edit this activity associating it to another compatible iLM<br/>\n";
  5769. return "";
  5770. }
  5771. // Get the iLM type (Java or HTML) to provide a correct caller
  5772. $typec = strtolower($iassign_ilm->type);
  5773. require_once 'ilm_handlers/' . $typec . '.php';
  5774. // ilm_handlers/html5.php (or ilm_handlers/java.php)
  5775. $retorno = $typec::show_activity_in_ilm($iassign_statement_activity_item, $student_answer, $enderecoPOST, $view_teacherfileversion);
  5776. return $retorno;
  5777. } // function view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, $view_teacherfileversion)
  5778. /// Function to give a single access to an iLM content avoi (after used, 'view()', after 'view_iLM(...)', will erase the entry)
  5779. // @calledby view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, $view_teacherfileversion) : $id_iLM_security=$this->write_iLM_security($iassign_statement_activity_item->id,$content_or_id_from_ilm_security);
  5780. // @param int $iassign_statement_activity_itemid Id of iassign statement
  5781. // @param Object $file File in use in activity
  5782. // @return int Return the id of log
  5783. function write_iLM_security ($iassign_activity_itemid, $content_or_id_from_ilm_security) {
  5784. global $CFG, $USER, $COURSE, $DB, $OUTPUT;
  5785. $newentry = new stdClass();
  5786. $newentry->iassign_statementid = $iassign_activity_itemid;
  5787. $newentry->userid = $USER->id;
  5788. $newentry->filesid = $content_or_id_from_ilm_security;
  5789. $newentry->timecreated = time();
  5790. $newentry->view = 1;
  5791. $id_iLM_security = $DB->insert_record("iassign_security", $newentry);
  5792. if (!$id_iLM_security) {
  5793. print_error('error_security', 'iassign'); // ./lib/setuplib.php: moodle_exception thrown
  5794. } // from (!$DB->insert_record("iassign_security", $newentry))
  5795. return $id_iLM_security;
  5796. }
  5797. /// Function to avoid that erros in remotion of entries in table 'iassign_security' allow future access to this contents
  5798. // @calledby view_iLM($iassign_statement_activity_item, $student_answer, $enderecoPOST, $view_teacherfileversion) : $this->remove_old_iLM_security_entries($USER->id, $iassign_statement_activity_item->id);
  5799. // @param int $userid
  5800. // @param int $iassign_activity_itemid Id of iassign statement
  5801. function remove_old_iLM_security_entries ($userid, $iassign_activity_itemid) {
  5802. global $DB;
  5803. // This is an additional security: erase eventually old entries in 'iassign_security' table (do not remove '$iassign_activity_itemid' since it is going to be used "now")
  5804. $result = $DB->delete_records_select("iassign_security", "userid=" . $userid . " AND iassign_statementid<>" . $iassign_activity_itemid, null);
  5805. }
  5806. } // class ilm
  5807. /// Class to manage settings of iLM.
  5808. class ilm_settings {
  5809. /// Function to prepare tag to load iLM (that is stored in Moodle file system - usually /var/moodledata/filedir/).
  5810. // In case of JAR it will prepare the tag "applet". In case of HTML5 will prepare an "iframe".
  5811. // @param int $ilm_id Id of iLM
  5812. // @param array $options An array with options for create dynamic tag html APPLET
  5813. // @return string Return with a tag html to JavaScript (or Applet)
  5814. static function build_ilm_tags ($ilm_id, $options = array()) {
  5815. global $DB;
  5816. if (!isset($ilm_id) || $ilm_id<0) // security to avoid error: Exception - Attempt to assign property "type" on bool
  5817. return "";
  5818. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  5819. // Read the iLM type: 'html5' or 'java'
  5820. if (!isset($iassign_ilm->type)) // avoid error "Deprecated: strtolower(): Passing null to parameter #1"
  5821. $iassign_ilm->type = "html5"; // default is HTML/JavaScript
  5822. $typec = strtolower($iassign_ilm->type);
  5823. require_once 'ilm_handlers/' . $typec . '.php';
  5824. $retorno = $typec::build_ilm_tags($ilm_id, $options); // In 'ilm_handlers/html5.php' or 'ilm_handlers/java.php'
  5825. return $retorno;
  5826. } // static function build_ilm_tags($ilm_id, $options = array())
  5827. /// Function for get modified date of iLM file
  5828. // @param string $file_jar String with Ids of iLM files
  5829. // @return string Return with the filenames and modified date
  5830. static function applet_filetime ($file_jar) {
  5831. $filetime = "";
  5832. $fs = get_file_storage(); // Get reference to all files in Moodle data
  5833. $files_jar = explode(",", $file_jar);
  5834. foreach ($files_jar as $one_file) {
  5835. $file = $fs->get_file_by_id($one_file);
  5836. if ($file)
  5837. $filetime .= "\n" . $file->get_filename() . ' (' . userdate($file->get_timemodified()) . ')' . '</br>';
  5838. }
  5839. return $filetime;
  5840. }
  5841. /// Function for verify an default applet
  5842. // @param String $file_jar String containing an list de ids of applet files
  5843. // @return boolean Return true or fale if applet is default
  5844. static function applet_default ($file_jar) {
  5845. $is_default = true;
  5846. $fs = get_file_storage(); // Get reference to all files in Moodle data
  5847. $files_jar = explode(",", $file_jar);
  5848. foreach ($files_jar as $one_file) {
  5849. $file = $fs->get_file_by_id($one_file);
  5850. if ($file)
  5851. $is_default &= ($file->get_itemid() == 0);
  5852. }
  5853. return $is_default;
  5854. }
  5855. /// Function for get form variables for add, edit, or copy iLM
  5856. // This function is called to fill in the iLM data to the form
  5857. // The function 'edit_ilm(.)' bellow is responsible to process edited data
  5858. // @calledby settings_ilm.php: On the first call $ilm_id is defined: it is used to fill in with parent iLM data {iassign_ilm}
  5859. // @calledby settings_ilm.php: On the second call $ilm_id empty (however must be used)
  5860. // @see settings_form.php
  5861. // @see locallib.php : static function copy_new_version_ilm($param) : perform the data register (files and database)
  5862. // @see locallib.php : static function edit_ilm($param,$itemid): perform the data register (files and database) in edition process
  5863. // @param int $ilm_id Id of iLM
  5864. // @param string $action String with the action
  5865. // @return object Return an object with forms variables
  5866. static function add_edit_copy_ilm ($ilm_id, $action) {
  5867. global $USER, $DB, $CFG;
  5868. require_once('settings_form.php'); // put the form
  5869. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  5870. $param = new stdClass();
  5871. $param->action = $action;
  5872. $param->ilm_id = $ilm_id;
  5873. $CFG->action_ilm = $action;
  5874. $CFG->ilm_id = $ilm_id;
  5875. if ($action == 'add') {
  5876. $param->title = get_string('add_ilm', 'iassign');
  5877. $param->name = "";
  5878. $param->version = "";
  5879. $param->url = "";
  5880. $param->description = "";
  5881. $param->extension = "";
  5882. $param->author = $USER->id;
  5883. $param->file_jar = "";
  5884. $param->file_jar_static = "";
  5885. $param->file_class = "";
  5886. $param->width = 800;
  5887. $param->height = 600;
  5888. $param->enable = 0; // 0 - hide / 1 - show
  5889. $param->timecreated = time();
  5890. $param->timemodified = time();
  5891. $param->evaluate = 0; // evaluate = 1 => iLM has auto-evaluation method
  5892. $param->reevaluate = 0; // reevaluate = 1 => allows to the teacher calls method to re-evaluate (does it in batch, to all students)
  5893. $param->editingbehavior = 0; // editingbehavior = 1 => iLM auto-evaluation remains working over a solution sent by the student (iGeom does not work, iVProg does)
  5894. $param->submissionbehavior = 0; // 0 => 'After submission, this iLM remains on the same page'; 1 => 'After submission, this iLM changes the current page'
  5895. $param->parent = 0;
  5896. }
  5897. elseif ($action == 'edit') { // static function add_edit_copy_ilm($ilm_id, $action)
  5898. if ($iassign_ilm) {
  5899. $description = json_decode($iassign_ilm->description);
  5900. $param->title = get_string('edit_ilm', 'iassign');
  5901. $param->id = $iassign_ilm->id;
  5902. $param->name_ilm = $iassign_ilm->name;
  5903. $param->name = $iassign_ilm->name;
  5904. $param->version = $iassign_ilm->version;
  5905. $param->ilm_type = $iassign_ilm->type;
  5906. $param->type = $iassign_ilm->type; // using as 'type' in ilm_handlers/html5.php
  5907. $param->url = $iassign_ilm->url; // iLM official URL
  5908. // 'iassign_ilm.submissionbehavior'=1 => this iLM does not have submission button
  5909. $param->submissionbehavior = $iassign_ilm->submissionbehavior;
  5910. $param->editingbehavior = $iassign_ilm->editingbehavior;
  5911. // If the iLM does not present curret language => Notice: Undefined property: stdClass::$en_us in...
  5912. //D try {
  5913. if (!isset($description->{current_language()})) {
  5914. if (isset($description->{"en_us"})) $param->description = $description->{"en_us"};
  5915. else
  5916. if (isset($description->{"pt_br"})) $param->description = $description->{"pt_br"};
  5917. else $param->description = ""; //TODO Providenciar uma mensagem!? Pegar alguma lingua existente?
  5918. }
  5919. else
  5920. $param->description = $description->{current_language()};
  5921. //D } catch (Exception $e) { $param->description = ""; } // avoid error in /lib/external/classes/external_api.php: invalid_parameter_exception thrown ?
  5922. $param->description_lang = $iassign_ilm->description;
  5923. $param->extension = $iassign_ilm->extension;
  5924. $param->author = $iassign_ilm->author;
  5925. $param->file_jar = $iassign_ilm->file_jar;
  5926. $param->file_jar_static = ilm_settings::applet_filetime($iassign_ilm->file_jar);
  5927. $param->file_class = $iassign_ilm->file_class;
  5928. $param->width = $iassign_ilm->width;
  5929. $param->height = $iassign_ilm->height;
  5930. $param->enable = $iassign_ilm->enable;
  5931. $param->timecreated = $iassign_ilm->timecreated;
  5932. $param->timemodified = time();
  5933. $param->evaluate = $iassign_ilm->evaluate; // evaluate = 1 => iLM has auto-evaluation method
  5934. $param->reevaluate = $iassign_ilm->reevaluate; // reevaluate = 1 => allows to the teacher calls method to re-evaluate (does it in batch, to all students)
  5935. $param->editingbehavior = $iassign_ilm->editingbehavior; // editingbehavior = 1 => iLM auto-evaluation remains working over a solution sent by the student (iGeom does not work, iVProg does)
  5936. $param->submissionbehavior = $iassign_ilm->submissionbehavior; // 0 => 'After submission, this iLM remains on the same page'; 1 => 'After submission, this iLM changes the current page'
  5937. $param->parent = $iassign_ilm->parent;
  5938. } // if ($iassign_ilm)
  5939. } // elseif ($action == 'edit')
  5940. elseif ($action == 'new_version') { // Reaches this point from the form to add new iLM
  5941. // @see ./settings_ilm.php: $param = ilm_settings::add_edit_copy_ilm($ilm_id, $action);
  5942. // $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  5943. if ($ilm_id==0 && !isset($mform)) { // The second turn (form sended) reaches this point - Just in case (if the admin use back and forward in the browser)
  5944. // Not necessary message: when data is sent, reaches this point with 0 and $mform null..., just return and let 'ilm_handlers/html5.php' process
  5945. // // if ($iassign_ilm)
  5946. // print get_string('empty_file', 'iassign') . "?<br/>\n"; //TODO warning not to use back?
  5947. return;
  5948. }
  5949. $description = json_decode($iassign_ilm->description);
  5950. if ($iassign_ilm) {
  5951. if ($iassign_ilm->parent == 0)
  5952. $iassign_ilm->parent = $ilm_id;
  5953. $param->ilm_id = $ilm_id; //2019 after 'settings_ilm.php' it reaches this point again, better to define 'ilm_id'
  5954. $param->title = get_string('new_version_ilm', 'iassign');
  5955. $param->name_ilm = $iassign_ilm->name;
  5956. $param->name = $iassign_ilm->name;
  5957. $param->version = ""; // erase field 'version' - in order to present this field empty on the form (in 'settings_form.php')
  5958. $param->version_last = $iassign_ilm->version; //TODO not yet used!
  5959. $param->version = $iassign_ilm->version; // do not erase field 'version'
  5960. $param->ilm_type = $iassign_ilm->type;
  5961. $param->url = $iassign_ilm->url;
  5962. $param->description = $description->{current_language()};
  5963. $param->description_lang = $iassign_ilm->description; // JSON format: {"en":"Visual Interactive Programming on the Internet HTML5","pt_br":"Programac,a~o visual interativa na Internet"}
  5964. $param->extension = $iassign_ilm->extension;
  5965. $param->author = $USER->id;
  5966. //D $param->file_jar = ''; // do not erase field 'file_jar'!
  5967. $param->file_jar = $iassign_ilm->file_jar; // do not erase field 'file_jar'!
  5968. $param->file_jar_static = '';
  5969. $param->file_class = $iassign_ilm->file_class;
  5970. $param->width = $iassign_ilm->width;
  5971. $param->height = $iassign_ilm->height;
  5972. $param->enable = 0;
  5973. $param->timecreated = time();
  5974. $param->timemodified = time();
  5975. $param->evaluate = $iassign_ilm->evaluate;
  5976. $param->reevaluate = $iassign_ilm->reevaluate; // reevaluate = 1 => allows to the teacher calls method to re-evaluate (does it in batch, to all students)
  5977. $param->editingbehavior = $iassign_ilm->editingbehavior; // editingbehavior = 1 => iLM auto-evaluation remains working over a solution sent by the student (iGeom does not work, iVProg does)
  5978. $param->submissionbehavior = $iassign_ilm->submissionbehavior; // 0 => 'After submission, this iLM remains on the same page'; 1 => 'After submission, this iLM changes the current page'
  5979. $param->parent = $iassign_ilm->parent;
  5980. } // if ($iassign_ilm)
  5981. } // elseif ($action == 'new_version')
  5982. elseif ($action == 'copy') {
  5983. $description = json_decode($iassign_ilm->description);
  5984. if ($iassign_ilm) {
  5985. if ($iassign_ilm->parent == 0)
  5986. $iassign_ilm->parent = $ilm_id;
  5987. $param->title = get_string('copy_ilm', 'iassign');
  5988. $param->id = $iassign_ilm->id;
  5989. $param->name_ilm = $iassign_ilm->name;
  5990. $param->name = $iassign_ilm->name;
  5991. $param->version = "";
  5992. $param->ilm_type = $iassign_ilm->type;
  5993. $param->type = $iassign_ilm->type; // using as 'type' in ilm_handlers/html5.php
  5994. $param->url = $iassign_ilm->url;
  5995. $param->description = $description->{current_language()};
  5996. $param->description_lang = $iassign_ilm->description;
  5997. $param->extension = $iassign_ilm->extension;
  5998. $param->author = $USER->id;
  5999. $param->file_jar = '';
  6000. $param->file_jar_static = '';
  6001. $param->file_class = $iassign_ilm->file_class;
  6002. $param->width = $iassign_ilm->width;
  6003. $param->height = $iassign_ilm->height;
  6004. $param->enable = 0;
  6005. $param->timecreated = time();
  6006. $param->timemodified = time();
  6007. $param->evaluate = $iassign_ilm->evaluate;
  6008. $param->reevaluate = $iassign_ilm->reevaluate; // reevaluate = 1 => allows to the teacher calls method to re-evaluate (does it in batch, to all students)
  6009. $param->editingbehavior = $iassign_ilm->editingbehavior; // editingbehavior = 1 => iLM auto-evaluation remains working over a solution sent by the student (iGeom does not work, iVProg does)
  6010. $param->submissionbehavior = $iassign_ilm->submissionbehavior; // 0 => 'After submission, this iLM remains on the same page'; 1 => 'After submission, this iLM changes the current page'
  6011. $param->parent = $iassign_ilm->parent;
  6012. } // if ($iassign_ilm)
  6013. } // elseif ($action == 'copy'
  6014. //D debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  6015. //D echo "Fim: action=" . $param->action . "<br/>---------------------------------<br/>";
  6016. //D var_dump(debug_backtrace());
  6017. //D print_r(debug_backtrace()); echo "param="; print_r($param); echo "<br/>\n";
  6018. //D echo "<br/>---------------------------------<br/>"; exit;
  6019. return $param;
  6020. } // static function add_edit_copy_ilm($ilm_id, $action)
  6021. //2023/05/30
  6022. // Removi funcao NAO mais usada: static function new_file_ilm ($itemid, $iassign_ilm)
  6023. // Tambem removi homonima de './iassign/ilm_handlers/html5.php': static function new_file_ilm ($itemid, $fs, $contextuser, $contextsystem, $files_ilm) : 831/1172
  6024. // Para : /home/leo/projetos/iMA/lms/itarefa/novo/removidos/locallib_html_2023_05_30.php
  6025. /// Function insert new iLM: it creates directory "mod/iassign/ilm/<iLM name>/<iLM version>" and register in {iassign_ilm}
  6026. // Admin user in iLM manager interface (iassign/settings_ilm.php) clicks on the button "Add iLM"
  6027. // fill the form (from 'settings_ilm.php') and send a ZIP file with the complete HTML5 package
  6028. // @calledby ./iassign/settings_ilm.php: if ($action == 'add'): $retorno = ilm_settings::new_ilm($formdata->file);
  6029. // @param object $param An object with iLM params
  6030. static function new_ilm ($formdata) {
  6031. global $CFG, $USER, $OUTPUT;
  6032. //D Check if there is any previous ZIP file in Moodle data "temp" (if positive, remove it): MoodleData/temp
  6033. //D if (file_exists($CFG->dataroot . '/temp/' . 'ilm-application.xml')) { unlink($CFG->dataroot . '/temp/' . 'ilm-application.xml'); }
  6034. $file_draft = $formdata->file;
  6035. $pathtemp = $CFG->dataroot . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR;
  6036. $contextuser = context_user::instance($USER->id);
  6037. $fs = get_file_storage(); // Get reference to all files in Moodle data
  6038. $zip = new zip_packer();
  6039. $files = $fs->get_directory_files($contextuser->id, 'user', 'draft', $file_draft, '/');
  6040. foreach ($files as $file) {
  6041. if (!$file->is_directory())
  6042. $files_extract = $zip->extract_to_pathname($file, $pathtemp);
  6043. }
  6044. // From './iassign/settings_ilm.php': $formdata = $mform->get_submitted_data();
  6045. $iassign_ilm_instance = $formdata;
  6046. // {iassign_ilm}.typ: $formdata->type : 0 = package JavaScript; 1 = package Java (JAR)
  6047. if ($formdata->type==1) $formdata->type = "Java";
  6048. else $formdata->type = "HTML5"; // default is "HTML5"
  6049. // Check if form sent all the necessary data to insert into table {iassign_ilm}
  6050. if (!$iassign_ilm_instance) {
  6051. // print($OUTPUT->notification(get_string('error_xml_ilm', 'iassign'), 'notifyproblem'));
  6052. print($OUTPUT->notification(get_string('error_add_ilm_zip', 'iassign'), 'notifyproblem')); // Error while extracting the content from iLM package file
  6053. return;
  6054. }
  6055. else {
  6056. $missing = "";
  6057. if (!isset($iassign_ilm_instance->name)) { $missing .= "name"; }
  6058. if (!isset($iassign_ilm_instance->version)) { $missing .= ", version"; }
  6059. if (!isset($iassign_ilm_instance->type)) { $missing .= ", type"; }
  6060. if (!isset($iassign_ilm_instance->extension)) { $missing .= ", extension"; }
  6061. if (!isset($iassign_ilm_instance->file_jar)) { $missing .= ", file_jar"; }
  6062. if (!isset($iassign_ilm_instance->file_class)) { $missing .= ", file_class"; }
  6063. if (!isset($iassign_ilm_instance->width)) { $missing .= ", width"; }
  6064. if (!isset($iassign_ilm_instance->height)) { $missing .= ", height"; }
  6065. if (!isset($iassign_ilm_instance->evaluate)) { $missing .= ", evaluate"; }
  6066. if (strlen($missing) > 2) {
  6067. // print($OUTPUT->notification(get_string('error_xml_missing', 'iassign') . $missing . ".", 'notifyproblem'));
  6068. // ERROR: in selecting compacted file (zip) to be imported!
  6069. print($OUTPUT->notification(get_string('er_file_not_selected_to_import', 'iassign') . $missing . ".", 'notifyproblem'));
  6070. return;
  6071. }
  6072. }
  6073. // Process form data using ./ilm_handlers/html5.php (or ./ilm_handlers/java.php):
  6074. $typec = strtolower($iassign_ilm_instance->type);
  6075. require_once 'ilm_handlers' . DIRECTORY_SEPARATOR . $typec . '.php';
  6076. $retorno = $typec::new_ilm($file_draft, $files_extract, $iassign_ilm_instance, $contextuser, $fs); // ./mod/iassign/ilm_handlers/html5.php
  6077. return $retorno;
  6078. } // static function new_ilm($file_draft, $formdata)
  6079. /// Function for save in database an iLM edit
  6080. // The above function 'add_edit_copy_ilm(.)' is responsible to provide the iLM data in this form
  6081. // The data stored through 'ilm_handlers/html5.php' (or 'java.php')
  6082. // @param object $param An object with iLM params
  6083. // @calledby settings_ilm.php: ilm_settings::edit_ilm($formdata, $formdata->file);
  6084. static function edit_ilm ($param, $itemid) {
  6085. // Descobrir o tipo de iLM:
  6086. global $DB, $USER, $CFG;
  6087. //D debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); //exit;
  6088. $iassign_t = $DB->get_record('iassign_ilm', array('id' => $param->id));
  6089. $pathtemp = $CFG->dataroot . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR;
  6090. $contextuser = context_user::instance($USER->id);
  6091. $fs = get_file_storage(); // Get reference to all files in Moodle data
  6092. $zip = new zip_packer();
  6093. $files = $fs->get_directory_files($contextuser->id, 'user', 'draft', $itemid, '/');
  6094. $files_extract = null;
  6095. foreach ($files as $file) {
  6096. if (!$file->is_directory())
  6097. $files_extract = $zip->extract_to_pathname($file, $pathtemp);
  6098. }
  6099. // Get the iLM type (HTML5 or Java) to call the correspondent updater
  6100. $typec = strtolower($iassign_t->type);
  6101. require_once 'ilm_handlers' . DIRECTORY_SEPARATOR . $typec . '.php';
  6102. $typec::edit_ilm($param, $itemid, $files_extract, $contextuser); // ilm_handlers/html5.php or ilm_handlers/java.php
  6103. } // static function edit_ilm($param, $itemid)
  6104. /// Function for register the new iLM (in database and in the directory)
  6105. // Came from 'settings_ilm.php' with $action=='new_version' or $action == 'copy'
  6106. // Interface: Administration > plugins > iAssign : after select the iLM and the option 'Add new iLM version'
  6107. // @see settings_form.php is extended by 'settings_ilm.php'
  6108. // @calledby settings_ilm.php!ilm_settings::copy_new_version_ilm($formdata)
  6109. // @param object $param An object with iLM params
  6110. static function copy_new_version_ilm ($param) {
  6111. global $DB, $CFG, $USER;
  6112. $itemid = $param->file;
  6113. $pathtemp = $CFG->dataroot . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR; // in Moodle Data
  6114. //D echo "locallib.php: copy_new_version_ilm(...): param->description=" . $param->description . ", param->description_lang=". $param->description_lang . "<br/>";
  6115. //D echo "pathtemp=$pathtemp<br/>";
  6116. $contextuser = context_user::instance($USER->id);
  6117. $fs = get_file_storage(); // Get reference to all files in Moodle data
  6118. $zip = new zip_packer();
  6119. $files = $fs->get_directory_files($contextuser->id, 'user', 'draft', $itemid, '/');
  6120. $files_extract = null;
  6121. foreach ($files as $one_file) {
  6122. if (!$one_file->is_directory())
  6123. $files_extract = $zip->extract_to_pathname($one_file, $pathtemp);
  6124. }
  6125. //D echo "locallib.php!copy_new_version_ilm(.): itemid=" . $itemid . ", ilm_id=" . $param->ilm_id . ", parent=" . $param->parent . "<br/>"; // . $type_ipz_zip . "<br/>";
  6126. // Get information of iLM parent
  6127. // $param->parent must be set!
  6128. $iassign_t = $DB->get_record('iassign_ilm', array('id' => $param->parent));
  6129. $param->ilm_id = $param->parent; // original {iassign_ilm}.id
  6130. $typec = strtolower($iassign_t->type);
  6131. require_once 'ilm_handlers' . DIRECTORY_SEPARATOR . $typec . '.php';
  6132. $typec::copy_new_version_ilm($param, $files_extract); // ./mod/iassign/ilm_handlers/html5.php: in fact, here the new iLM is inserted
  6133. } // static function copy_new_version_ilm($param)
  6134. //VER::
  6135. //Notice: Undefined variable: output_ilm in ./mod/iassign/locallib.php on line 5054
  6136. //Notice: Trying to get property of non-object in ./mod/iassign/locallib.php on line 192
  6137. //Notice: Trying to get property of non-object in ./mod/iassign/locallib.php on line 194
  6138. /// Function for change visibility of iLM
  6139. // @param int $ilm_id Id of iLM
  6140. // @param int $status Indicator of change vibility (0 = hide, 1 = show)
  6141. static function visible_ilm ($ilm_id, $status) {
  6142. global $DB;
  6143. if ($status == 0)
  6144. $visible = 1;
  6145. else
  6146. $visible = 0;
  6147. $newentry = new stdClass();
  6148. $newentry->id = $ilm_id;
  6149. $newentry->enable = $visible;
  6150. if (!$DB->update_record("iassign_ilm", $newentry))
  6151. error(get_string('error_edit_ilm', 'iassign'));
  6152. }
  6153. /// Function for confirm change default iLM
  6154. // @param int $ilm_id Id of iLM
  6155. // @param int $ilm_parent Id of parent iLM
  6156. // @return string Return with an string for create default page confirmation
  6157. static function confirm_default_ilm ($ilm_id, $ilm_parent) {
  6158. global $OUTPUT, $DB;
  6159. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  6160. $optionsno = new moodle_url('/admin/settings.php', array('section' => 'modsettingiassign', 'action' => 'config', 'ilm_id' => $ilm_parent));
  6161. $optionsyes = new moodle_url('/mod/iassign/settings_ilm.php', array('action' => 'default', 'ilm_id' => $ilm_id, 'ilm_parent' => $ilm_parent));
  6162. $return = $OUTPUT->heading(get_string('confirm_default', 'iassign') . ': ' . $iassign_ilm->name);
  6163. $return .= $OUTPUT->confirm(get_string('confirm_default_ilm', 'iassign') . $OUTPUT->help_icon('confirm_default_ilm', 'iassign'), $optionsyes, $optionsno);
  6164. return $return;
  6165. }
  6166. /// Function for change the default iLM
  6167. // @see settings_ilm.php
  6168. // @param int $ilm_id Id of iLM
  6169. // @return int Return Id of default iLM
  6170. static function default_ilm ($ilm_id) {
  6171. global $DB;
  6172. $iassign_ilm_default = $DB->get_record("iassign_ilm", array('id' => $ilm_id));
  6173. $iassign_ilm = $DB->get_record("iassign_ilm", array('id' => $iassign_ilm_default->parent));
  6174. $DB->delete_records("iassign_ilm", array('id' => $iassign_ilm_default->id));
  6175. $iassign_ilm_default->id = $iassign_ilm->id;
  6176. $iassign_ilm_default->parent = 0;
  6177. $iassign_ilm->parent = $iassign_ilm_default->id;
  6178. $iassign_ilm->id = 0;
  6179. $iassign_ilm_default->enable = 1;
  6180. if (!$DB->update_record("iassign_ilm", $iassign_ilm_default)) {
  6181. print_error('error_edit_ilm', 'iassign');
  6182. }
  6183. $inserted = $DB->insert_record("iassign_ilm", $iassign_ilm);
  6184. if (!$inserted) {
  6185. $msg_error = get_string('error_add_ilm', 'iassign') . "<br/>In default_ilm(" . $ilm_id . ")<br/>\n";
  6186. print_error($msg_error);
  6187. //xx print_error('error_add_ilm', 'iassign');
  6188. }
  6189. return $iassign_ilm_default->id;
  6190. } // static function default_ilm ($ilm_id)
  6191. /// Function to present to the user (tipically the Moodle Admin) the frame to confirm the intention of iLM remotion
  6192. // @see settings_ilm.php!$action=='delete' : after the Admin confirmation, 'settings_ilm.php' will be called again with 'delete' option (really erase iLM)
  6193. // @see locallib.php!delete_ilm($ilm_id) : called by "settings_ilm.php!$action=='delete'" that calls "ilm_handlers/html5.php!delete_ilm($ilm_id)"
  6194. // @see ilm_handlers/html5.php!delete_ilm($ilm_id) : called by "settings_ilm.php!$action=='delete'" to really erase the iLM
  6195. // @calledby settings_ilm.php!$action=='confirm_delete_ilm' : $delete_ilm = ilm_settings::confirm_delete_ilm($ilm_id, $ilm_parent);
  6196. // @param int $ilm_id Id of iLM
  6197. // @param int $ilm_parent Id of parent iLM
  6198. // @return string Return with an string to create delete page confirmation
  6199. static function confirm_delete_ilm ($ilm_id, $ilm_parent) {
  6200. global $OUTPUT, $DB, $USER;
  6201. //D echo "locallib.php!confirm_delete_ilm(.): (1) ilm_id=" . $ilm_id . ", ilm_parent="; print_r($ilm_parent); echo "<br/>\n"; //exit; //2025/04/11 leo
  6202. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  6203. $msg = "";
  6204. // Verify if there are {iassign_statement} or {iassign_submission} associated to this iLM
  6205. // in this case, launch a message to inform Admin of this problem
  6206. if ($iassign_ilm) { // there is this iLM with ID=$ilm_id
  6207. //D echo "locallib.php!confirm_delete_ilm(.): (2) ilm_id=" . $ilm_id . ", existe este iLM<br/>\n"; // exit; //2025/04/11 leo
  6208. // Attention, "./lib/dml/moodle_database.php ! public abstract function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0)"
  6209. // return a single element considering the first field, so we need to use {iassign_submission}.id (not {iassign_statement}.id))
  6210. $str_query = "SELECT iasubm.id AS iasub_id, iastat.id FROM {iassign_statement} AS iastat, {iassign_submission} AS iasubm" .
  6211. " WHERE iastat.iassign_ilmid=" . $ilm_id . " AND iastat.id=iasubm.iassign_statementid"; //
  6212. $tmp_list_all_iassign_statement_subm = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  6213. $num_is = count($tmp_list_all_iassign_statement_subm);
  6214. if ($num_is>0) {
  6215. $param1 = new stdClass(); $param1->num = $num_is; $param1->id = $ilm_id;
  6216. // If the Admin remove this iLM its activities will became orphans!
  6217. $msg .= get_string('confirm_del_ilm_sure1', 'iassign', $param1); // This iLM has {$num_is} submissions to its activities! See its ID and number of submissions bellow:
  6218. $last_iassign_id = -1; // to find different {iassign}
  6219. $count_ia = 0;
  6220. $count_subm = 0;
  6221. foreach ($tmp_list_all_iassign_statement_subm as $item) {
  6222. $count_subm++; // new submission to the last {iassign}.id
  6223. $iassign_id = $item->id; // {iassign}.id
  6224. if ($iassign_id != $last_iassign_id) { // new {iassign}.id, close the last one
  6225. if ($last_iassign_id!=-1) { // it is not the first
  6226. $url = "view.php?userid_iassign=" . $USER->id . "&action=view&iassign_current=" . $iassign_id;
  6227. // Click to check this activity (id,#submissions) - you will need to change its iLM
  6228. $msg .= "<a href='" . $url . "' title='" . get_string('confirm_del_ilm_sure2', 'iassign') . "' target='_blank'>" .
  6229. $iassign_id . " (" . $count_subm . ")</a> ";
  6230. }
  6231. $count_ia++;
  6232. if ($count_ia>20) {
  6233. break; // present at most 20
  6234. }
  6235. $last_iassign_id = $iassign_id; // the new identified activity
  6236. $count_subm = 1; // first submission to the new identified activity
  6237. }
  6238. }
  6239. // the last {iassign}.id was not registered
  6240. $url = "view.php?userid_iassign=" . $USER->id . "&action=view&iassign_current=" . $iassign_id;
  6241. $msg .= "<a href='" . $url . "' title='" . get_string('confirm_del_ilm_sure2', 'iassign') . "' target='_blank'>" .
  6242. $iassign_id . " (" . $count_subm . ")</a> ";
  6243. if ($count_ia>20) $msg .= "...";
  6244. $msg .= "<br/>\n";
  6245. } // if ($num_is>0)
  6246. else { // Despite the existence of activities with this iLM they haven't any associated submission!
  6247. //D echo "locallib.php!confirm_delete_ilm(.): (3) ilm_id=" . $ilm_id . ", OK, NAO existe submissoes com este iLM<br/>ilm_parent=" . $ilm_parent . "<br/>\n"; //exit; //2025/04/11 leo
  6248. $str_query = "SELECT id,name FROM {iassign_statement} WHERE iassign_ilmid=" . $ilm_id . ""; //
  6249. $tmp_list_all_iassign_statement = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  6250. $num_is = count($tmp_list_all_iassign_statement);
  6251. $param1 = new stdClass(); $param1->num = $num_is; $param1->id = $ilm_id;
  6252. // 'confirm_del_ilm_sure3' = 'This iLM has {$a} activities! Before decide to remove it, register their ID:'
  6253. $msg .= get_string('confirm_del_ilm_sure3', 'iassign', $param1);
  6254. foreach ($tmp_list_all_iassign_statement as $item) {
  6255. $url = "view.php?userid_iassign=" . $USER->id . "&action=view&iassign_current=" . $item->id;
  6256. $msg .= "<a href='" . $url . "' title='Click to check this activity (id) - you will need to change its iLM' target='_blank'>" . $item->id . " </a> ";
  6257. }
  6258. }
  6259. } // if ($iassign_ilm) // there is this iLM with ID=$ilm_id
  6260. else { // security
  6261. // $string['error_confirms_ilm'] = 'Error: iLM not registered in the database. Inform the system administrator.';
  6262. print($OUTPUT->notification(get_string('error_remove_ilm_inexist', 'iassign'), 'notifyproblem')); // Error: the given iLM (ID) does not exist
  6263. return;
  6264. }
  6265. $optionsno = new moodle_url('/admin/settings.php', array('section' => 'modsettingiassign', 'action' => 'config', 'ilm_id' => $ilm_parent));
  6266. // $optionsyes = new moodle_url('/mod/iassign/settings_ilm.php', array('action' => 'delete', 'ilm_id' => $ilm_id, 'ilm_parent' => $ilm_parent));
  6267. $optionsyes = new moodle_url('/mod/iassign/settings_ilm.php', array('action' => 'delete', 'ilm_id' => $ilm_id, 'ilm_parent' => $ilm_parent));
  6268. $msg .= "<br/>" . get_string('confirm_del_ilm', 'iassign', $iassign_ilm->name . ' ' . $iassign_ilm->version);
  6269. $answer = $OUTPUT->confirm($msg, $optionsyes, $optionsno); // return to "settings_ilm.php!$action=='confirm_delete_ilm'" message
  6270. //D echo "locallib.php!confirm_delete_ilm(.): ilm_id=" . $ilm_id . ", answer="; print_r($answer); echo "<br/>\n"; //exit; //2025/04/11 leo
  6271. return $answer;
  6272. } // static function confirm_delete_ilm($ilm_id, $ilm_parent)
  6273. /// Function for delete directory where the iLM is allocated.
  6274. // @param string $dirPath
  6275. // @throws InvalidArgumentException
  6276. public static function delete_dir ($dirPath) {
  6277. if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
  6278. $dirPath .= '/';
  6279. }
  6280. $files = glob($dirPath . '*', GLOB_MARK);
  6281. foreach ($files as $one_file) {
  6282. if (is_dir($one_file)) {
  6283. ilm_settings::delete_dir($one_file);
  6284. } else {
  6285. unlink($one_file);
  6286. }
  6287. }
  6288. rmdir($dirPath);
  6289. }
  6290. /// Function to provide the iLM removal
  6291. /// After Admin confirm his intention to remove the iLM, reaches this function
  6292. // @see ilm_handlers/html5.php!delete_ilm($ilm_id) : to really erase the iLM
  6293. // @calledy settings_ilm.php!$action=='delete' : $parent = ilm_settings::delete_ilm($ilm_id);
  6294. // @param int $ilm_id is the iLM ID ({iassign_ilm}.id)
  6295. // @return int Return ID of parent iLM ({iassign_ilm}.id)
  6296. static function delete_ilm ($ilm_id) {
  6297. global $DB;
  6298. //D echo "locallib.php!delete_ilm(.): ilm_id=" . $ilm_id . "<br/>\n"; //exit; //2025/04/11 leo
  6299. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  6300. //D echo "locallib.php!delete_ilm(.): ilm_id=" . $ilm_id . ", type=" . $iassign_ilm->type . "<br/>\n"; //exit; //2025/04/11 leo
  6301. // Get the iLM type ({iassign_ilm}.type) to call the correct function that really erase the iLM (from DataBase and from ./mod/iassin/ilm/ directory)
  6302. $typec = strtolower($iassign_ilm->type);
  6303. require_once 'ilm_handlers' . DIRECTORY_SEPARATOR . $typec . '.php';
  6304. $return_ilm_parent_id = $typec::delete_ilm($ilm_id); // $typec=="html5" => ilm_handlers/html5.php
  6305. return $return_ilm_parent_id;
  6306. }
  6307. /// Function to export iLM package (ZIP file), eventually to install in other iAssign/Moodle
  6308. // @calledby settings.php : if ($action == 'export') ilm_settings::export_ilm($ilm_id);
  6309. // @param int $ilm_id Id of iLM
  6310. static function export_ilm ($ilm_id) {
  6311. global $DB;
  6312. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  6313. if (!$iassign_ilm) return; // security debug...
  6314. // Get the iLM type ("HTML5" or "Java") and call the correspondent function in 'ilm_handlers'
  6315. $typec = strtolower($iassign_ilm->type);
  6316. if (!$typec) $typec = "html5"; // security debug...
  6317. require_once 'ilm_handlers' . DIRECTORY_SEPARATOR . $typec . '.php'; // prepare correct handler (now Java or HTML5)
  6318. $typec::export_ilm($ilm_id);
  6319. } // static function export_ilm($ilm_id)
  6320. // REMOVER NAO e' chamada! ilm_settings::export_update_ilm(.)
  6321. //TODO iLM_HTML5 :: //MOOC 2016
  6322. // Function to export iLM package descriptor (it allows online update)
  6323. // @param int $ilm_id Id of iLM
  6324. static function export_update_ilm ($ilm_id) {
  6325. global $DB, $CFG;
  6326. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  6327. $xml_filename = $CFG->dataroot . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'ilm-upgrade_' . iassign_utils::format_pathname($iassign_ilm->name) . '.xml';
  6328. $zip_filename = 'ilm-' . iassign_utils::format_pathname($iassign_ilm->name . '-v' . $iassign_ilm->version) . '.ipz';
  6329. $upgrade_descriptor = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
  6330. $upgrade_descriptor .= '<upgrade xmlns="http://line.ime.usp.br/application/1.5">' . "\n";
  6331. $upgrade_descriptor .= ' <version>' . $iassign_ilm->version . '</version>' . "\n";
  6332. $upgrade_descriptor .= ' <file>' . $zip_filename . '</file>' . "\n";
  6333. $upgrade_descriptor .= ' <description>' . iassign_language::json_to_xml($iassign_ilm->description) . "\n " . '</description>' . "\n";
  6334. $upgrade_descriptor .= '</upgrade>' . "\n";
  6335. file_put_contents($xml_filename, $upgrade_descriptor);
  6336. header("Pragma: public");
  6337. header("Expires: 0");
  6338. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  6339. header("Cache-Control: private", false);
  6340. header('Content-Type: application/xml; charset=utf-8');
  6341. header("Content-Disposition: attachment; filename=\"" . basename($xml_filename) . "\";");
  6342. header("Content-Length: " . @filesize($xml_filename));
  6343. set_time_limit(0);
  6344. @readfile("$xml_filename") || die("File not found.");
  6345. unlink($xml_filename);
  6346. exit;
  6347. } // static function export_update_ilm($ilm_id) //MOOC 2016
  6348. // REMOVER?
  6349. // ilm_settings::save_ilm_by_xml(.)
  6350. // Codigo equivalente em: ./mod/iassign/ilm_handlers/html5.php!save_ilm_by_xml($str_from, $application_xml, $files_extract)
  6351. //
  6352. //TODO Perhaps add param. "$ilm_id" to {files}.itemid <- $ilm_id ???
  6353. /// Function for save iLM from XML descriptor
  6354. // Inside: class ilm_settings
  6355. // @calledby static function upgrade_ilm($ilm_id) : $file_jar = self::save_ilm_by_xml($ilm_id, $application_xml, $files_extract);
  6356. // @param array $application_xml Data of XML descriptor
  6357. // @param array $files_extract Filenames of extract files
  6358. // @return array Return an array content id of JAR files
  6359. static function save_ilm_by_xml ($parent_ilm_id, $application_xml, $files_extract) {
  6360. global $CFG, $USER;
  6361. // Tratamento diferenciado se for do tipo HTML5:
  6362. $source = "";
  6363. $directory = "";
  6364. $df = DIRECTORY_SEPARATOR;
  6365. if (strtolower($application_xml->type) == 'html5') {
  6366. $i = 0;
  6367. foreach ($files_extract as $key => $value) {
  6368. $file = $CFG->dataroot . $df . 'temp' . $df . $key;
  6369. // Check the previous existence of the directory (to iLM):
  6370. if ($i == 0) {
  6371. $source = $file;
  6372. if (file_exists("ilm" . $df . basename($file))) {
  6373. $j = 1;
  6374. while (file_exists('ilm' . $df . basename($file) . "_" . $j)) {
  6375. $j++;
  6376. }
  6377. $directory = 'ilm' . $df . basename($file) . "_" . $j;
  6378. // mkdir($directory, 0777, true);
  6379. mkdir($directory, 0755, true); // permissions: drwxr-xr-x
  6380. }
  6381. else {
  6382. $directory = 'ilm' . $df . basename($file);
  6383. // mkdir($directory, 0777, true);
  6384. mkdir($directory, 0755, true);
  6385. }
  6386. break;
  6387. }
  6388. $i++;
  6389. }
  6390. foreach ($iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST) as $item) {
  6391. if ($item->isDir()) {
  6392. mkdir($directory . $df . $iterator->getSubPathName(), 0755, true);
  6393. }
  6394. else {
  6395. copy($item, $directory . $df . $iterator->getSubPathName());
  6396. }
  6397. }
  6398. ilm_settings::delete_dir($source);
  6399. return "." . $df . $directory;
  6400. }
  6401. $fs = get_file_storage(); // Get reference to all files in Moodle data
  6402. $file_jar = array();
  6403. $files_ilm = explode(",", $application_xml->file_jar);
  6404. $contextsystem = context_system::instance();
  6405. foreach ($files_ilm as $value) {
  6406. //TODO Sobre {files}.itemid: NAO existe no {iassign_ilm} campo para guardar {files}.itemid, entao usar 'itemid = {iassign_ilm}.id'
  6407. $file_ilm = array(
  6408. 'userid' => $USER->id,
  6409. 'contextid' => $contextsystem->id,
  6410. 'component' => 'mod_iassign',
  6411. 'filearea' => 'ilm',
  6412. 'itemid' => $parent_ilm_id, //2023 rand(1, 999999999), //2023 added param. "$ilm_id" to {files}.itemid <- $ilm_id
  6413. 'filepath' => $df . 'iassign' . $df . 'ilm' . $df . iassign_utils::format_pathname($application_xml->name) . $df . iassign_utils::format_pathname($application_xml->version) . $df,
  6414. 'filename' => $value);
  6415. $file_ilm = $fs->create_file_from_pathname($file_ilm, $CFG->dataroot . $df . 'temp' . $df . $value);
  6416. array_push($file_jar, $file_ilm->get_id()); //TODO: must be reviewed! {iassign_ilm}.file_jar as array() of int???
  6417. }
  6418. foreach ($files_extract as $key => $value) {
  6419. $file = $CFG->dataroot . $df . 'temp' . $df . $key; // in Moodle Data
  6420. if (file_exists($file))
  6421. unlink($file);
  6422. }
  6423. return $file_jar; // return array() with {files}.id of each created file in Moodle Data
  6424. } // static function save_ilm_by_xml($parent_ilm_id, $application_xml, $files_extract)
  6425. /// Function to import the iLM from a package IPZ
  6426. // @see https://www.matematica.br/ia/about_ilm-application.html
  6427. // @calledby iassign/settings_ilm.php: if ($action == 'import'): $retorno = ilm_settings::import_ilm($formdata->file);
  6428. // @param int $itemid Itemid of zip file
  6429. static function import_ilm ($itemid) {
  6430. global $CFG, $USER, $OUTPUT;
  6431. $df = DIRECTORY_SEPARATOR; // universal separator
  6432. // Verify if is remaining an old 'ilm-application.xml' in temp directory, in this case remove it
  6433. if (file_exists($CFG->dataroot . $df . 'temp' . $df . 'ilm-application.xml')) {
  6434. unlink($CFG->dataroot . $df . 'temp' . $df . 'ilm-application.xml');
  6435. }
  6436. $pathtemp = $CFG->dataroot . $df . 'temp' . $df;
  6437. $contextuser = context_user::instance($USER->id);
  6438. $fs = get_file_storage(); // Get reference to all files in Moodle data
  6439. $zip = new zip_packer();
  6440. $files = $fs->get_directory_files($contextuser->id, 'user', 'draft', $itemid, $df);
  6441. foreach ($files as $one_file) {
  6442. if (!$one_file->is_directory()) {
  6443. $files_extract = $zip->extract_to_pathname($one_file, $pathtemp);
  6444. break;
  6445. }
  6446. }
  6447. //D $end_arquivo = $CFG->dataroot . '/temp/' . 'ilm-application.xml';
  6448. //D echo "locallib.php!import_ilm(.): itemid=" . $itemid . "<br/>end_arquivo=" . $end_arquivo . "<br/>\n";
  6449. //D $myfile = fopen($end_arquivo, "r") or die("Unable to open file!");
  6450. //D echo "<pre>" . fread($myfile,filesize($end_arquivo)) . "</pre><br\>\n";
  6451. //D fclose($myfile);
  6452. // /var/data/saw_moodle-data/temp/...
  6453. $application_xml = @simplexml_load_file($CFG->dataroot . $df . 'temp' . $df . 'ilm-application.xml', null, LIBXML_NOCDATA); // PHP 'simplexml_load_file(.)' interprets an XML file into an object
  6454. //D echo "locallib.php!import_ilm(itemid): itemid=" . $itemid . "<br/>\napplication_xml="; print_r($application_xml); echo "<br/>-----<br/>\n"; //2023/05/08
  6455. // $application_xml->description->asXML()
  6456. // Verifica se o pacote possui o XML
  6457. if (!$application_xml) {
  6458. //D- echo "locallib.php!import_ilm(itemid): erro !application_xml<br/>\n"; //D-
  6459. print($OUTPUT->notification(get_string('error_xml_ilm', 'iassign'), 'notifyproblem'));
  6460. return;
  6461. } else { // if (!$application_xml)
  6462. //D- echo "locallib.php!import_ilm(itemid): NAO entrou no !application_xml<br/>\n"; //D-
  6463. $missing = "";
  6464. if (!isset($application_xml->name)) {
  6465. $missing .= "name"; // {iassign_ilm}.name has at most 20 characters
  6466. }
  6467. if (!isset($application_xml->version)) {
  6468. $missing .= ", version";
  6469. }
  6470. if (!isset($application_xml->type)) {
  6471. $missing .= ", type"; // {iassign_ilm}.type must be "Java" or "HTML5"
  6472. }
  6473. if (!isset($application_xml->extension)) {
  6474. $missing .= ", extension";
  6475. }
  6476. if (!isset($application_xml->file_jar)) {
  6477. $missing .= ", file_jar"; // {iassign_ilm}.file_jar the base name directory under ./mod/iassign/ilm/<iLM name>/<iLM version>/<base name dir>
  6478. }
  6479. if (!isset($application_xml->file_class)) {
  6480. $missing .= ", file_class"; // {iassign_ilm}.file_class is the iLM starting point (to JS could be "index.html", to JAR like "IGeomApplet.class")
  6481. }
  6482. if (!isset($application_xml->width)) {
  6483. $missing .= ", width";
  6484. }
  6485. if (!isset($application_xml->height)) {
  6486. $missing .= ", height";
  6487. }
  6488. if (!isset($application_xml->evaluate)) {
  6489. $missing .= ", evaluate";
  6490. }
  6491. if (strlen($missing) > 2) {
  6492. print($OUTPUT->notification(get_string('error_xml_missing', 'iassign') . $missing . ".", 'notifyproblem'));
  6493. return;
  6494. }
  6495. } // else if (!$application_xml)
  6496. // Get the iLM type and calls the correct handler in ./mod/iassign/ilm_handlers/
  6497. // From the IPZ package, 'import_ilm(.)' will create the directory ./mod/iassign/ilm/<iLM name>/<iLM version>/<base_name>
  6498. // and using files and meta-data in 'ilm-application.xml' will insert iLM data into {iassign_ilm} table
  6499. $typec = strtolower($application_xml->type);
  6500. //D- echo "locallib.php!import_ilm(itemid): typec=" . $typec . "<br/>\napplication_xml=|"; print_r($application_xml); echo "|<br/>\n"; exit;
  6501. require_once 'ilm_handlers' . $df . $typec . '.php';
  6502. $typec::import_ilm($itemid, $files_extract, $application_xml, $contextuser, $fs); // ./mod/iassign/ilm_handlers/html5.php: insert in table {iassign_ilm}
  6503. } // static function import_ilm($itemid)
  6504. /// Function for list iLM defaults
  6505. // @return string Return an string with a table of iLM
  6506. static function list_ilm () {
  6507. global $DB, $OUTPUT;
  6508. $iassign_ilm = $DB->get_records('iassign_ilm', array("enable" => 1));
  6509. $str = "\n";
  6510. $str .= '<table id="outlinetable" cellpadding="5" width="100%" >' . "\n";
  6511. $str .= '<tr><td align=right><input type=button value="' . get_string('close', 'iassign') . '" onclick="javascript:window.close ();"></td></tr>' . "\n";
  6512. if ($iassign_ilm) {
  6513. foreach ($iassign_ilm as $ilm) {
  6514. $url_view = new moodle_url('/mod/iassign/settings_ilm.php', array('action' => 'view', 'ilm_id' => $ilm->id));
  6515. $link_view = $OUTPUT->action_link($url_view, iassign_icons::insert('view_ilm') . ' ' . get_string('read_more', 'iassign'));
  6516. $str .= '<tr><td>';
  6517. $str .= '<table class="generaltable boxaligncenter" width="100%">';
  6518. $str .= '<tr>';
  6519. $str .= '<td class=\'cell c0 actvity\' width=40%><strong>' . get_string('name_ilm', 'iassign') . ':</strong> ' . $ilm->name . '</td>' . "\n";
  6520. $str .= '<td><strong>' . get_string('version_ilm', 'iassign') . ':</strong> ' . $ilm->version . '</td>' . "\n";
  6521. $str .= '<td align=right>' . $link_view . '</td>' . "\n";
  6522. $str .= '</tr>';
  6523. $str .= '<tr><td colspan=3>' . iassign_language::get_description_lang(current_language(), $ilm->description) . '</td></tr>';
  6524. $str .= '<tr><td colspan=3><a href="' . $ilm->url . '">' . $ilm->url . '</a></td></tr>';
  6525. $str .= "</table>\n";
  6526. $str .= "</td></tr>\n";
  6527. }
  6528. }
  6529. $str .= "</table>\n";
  6530. return $str;
  6531. }
  6532. /// Function to download and install an upgrade of some iLM
  6533. // @calledby settings_ilm.php: $ilm = ilm_settings::upgrade_ilm($ilm_id);
  6534. // @param int $ilm_id Id of iLM
  6535. static function upgrade_ilm ($ilm_id) {
  6536. global $DB, $CFG, $USER;
  6537. $df = DIRECTORY_SEPARATOR; // universal separator
  6538. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  6539. $upgrade_file = $iassign_ilm->url . 'ilm-upgrade_' . strtolower($iassign_ilm->name) . '.xml';
  6540. $update_xml = @simplexml_load_file($upgrade_file, null, LIBXML_NOCDATA);
  6541. $result = file_put_contents($CFG->dataroot . $df . 'temp' . $df . $update_xml->file, fopen($iassign_ilm->url . $update_xml->file, 'r'));
  6542. //D echo "locallib.php!upgrade_ilm(" . $ilm_id . "): aqui!"; // exit; //2023
  6543. // class ilm_settings : function build_ilm_tags ($ilm_id, $options=array()): 6545/9615 ; $typec::build_ilm_tags(...)
  6544. if (!$result)
  6545. print_error('error_upgrade_ilm', 'iassign');
  6546. else {
  6547. $zip_filename = $CFG->dataroot . $df . 'temp' . $df . $update_xml->file;
  6548. $extension = explode(".", $zip_filename);
  6549. if ($extension[count($extension) - 1] != 'ipz') {
  6550. print($OUTPUT->notification(get_string('error_upload_ilm', 'iassign'), 'notifyproblem'));
  6551. die;
  6552. }
  6553. $zip = new zip_packer();
  6554. $fs = get_file_storage(); // Get reference to all files in Moodle data
  6555. $contextuser = context_user::instance($USER->id);
  6556. $files_extract = $zip->extract_to_pathname($zip_filename, $CFG->dataroot . '/temp/');
  6557. $application_xml = @simplexml_load_file($CFG->dataroot . $df . 'temp' . $df . 'ilm-application.xml', null, LIBXML_NOCDATA);
  6558. $description_str = htmlentities(str_replace(array('<description>', '</description>'), array('', ''), $application_xml->description->asXML()));
  6559. // Attention: we have another function with the same name in "ilm_handers/", there
  6560. // to create directory "./mod/iassign/ilm/<iLM name>/<iLM version>/<base_name>"
  6561. // The return is array() with {files}.id of each created file
  6562. $file_jar = self::save_ilm_by_xml($ilm_id, $application_xml, $files_extract); //2023 added param. "$ilm_id" to {files}.itemid <- $ilm_id
  6563. if (file_exists($zip_filename)) // unzip the IPZ package
  6564. unlink($zip_filename);
  6565. if (empty($file_jar)) {
  6566. $msg_error = get_string('error_add_ilm', 'iassign') . "<br/>In upgrade_ilm(" . $ilm_id . ")<br/>\n";
  6567. print_error($msg_error);
  6568. //xx print_error('error_add_ilm', 'iassign');
  6569. }
  6570. else {
  6571. //TODO: where is the update('iassign_ilm', $newentry)???
  6572. $time_now = time();
  6573. $newentry = new stdClass();
  6574. $newentry->name = (String) $application_xml->name;
  6575. $newentry->version = (String) $application_xml->version;
  6576. $newentry->url = (String) $application_xml->url;
  6577. $newentry->description = $description_str;
  6578. $newentry->extension = strtolower((String) $application_xml->extension);
  6579. $newentry->file_jar = implode(",", $file_jar); //TODO Must be reviewed, {iassign_ilm}.file_jar must be directory name
  6580. $newentry->file_class = (String) $application_xml->file_class;
  6581. $newentry->width = (String) $application_xml->width;
  6582. $newentry->height = (String) $application_xml->height;
  6583. $newentry->enable = 0;
  6584. $newentry->timemodified = $time_now;
  6585. $newentry->author = $USER->id;
  6586. $newentry->timecreated = $time_now;
  6587. $newentry->evaluate = (String) $application_xml->evaluate;
  6588. $newentry->parent = $ilm_id;
  6589. }
  6590. }
  6591. return $iassign_ilm->id;
  6592. } // static function upgrade_ilm($ilm_id)
  6593. //MOOC2014 -- inicio
  6594. //TODO: REVIEW
  6595. // static function confirm_move_iassign($ilmid, $ilm_parent)
  6596. // static function move_iassign($ilm_id)
  6597. //MOOC2014 -- final
  6598. /// Function for list iLM versions with all informations, under "admin" plugin view: .../mod/iassign/settings_ilm.php?action=view&ilm_id=".$ilmid."&from=admin
  6599. // @return string Return an string with a table of iLM
  6600. static function view_ilm ($ilmid, $from) {
  6601. global $DB;
  6602. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilmid));
  6603. // Get the iLM type and calls its correspondent handler ('Java' or 'HTML5')
  6604. $typec = strtolower($iassign_ilm->type);
  6605. require_once 'ilm_handlers' . DIRECTORY_SEPARATOR . $typec . '.php';
  6606. $return_html_str = $typec::view_ilm($ilmid, $from); // get the string with HTML code to present the iLM
  6607. return $return_html_str;
  6608. }
  6609. /// Function for get form variables for add, edit, or copy iLM params
  6610. // @param int $ilm_param_id Id of iLM param
  6611. // @param string $action String with the action
  6612. // @return object Return an object with forms variables
  6613. static function add_edit_copy_param ($ilm_param_id, $action) {
  6614. global $DB;
  6615. require_once('params_form.php');
  6616. $iassign_ilm_config = $DB->get_record('iassign_ilm_config', array('id' => $ilm_param_id));
  6617. $param = new stdClass();
  6618. $param->action = $action;
  6619. $param->ilm_param_id = $ilm_param_id;
  6620. $type = optional_param('type', NULL, PARAM_TEXT); //MOOC2014
  6621. if ($type == NULL && $iassign_ilm_config) //MOOC2014
  6622. $type = $iassign_ilm_config->param_type;
  6623. if ($action == 'add') {
  6624. $param->title = get_string('add_ilm', 'iassign');
  6625. $param->iassign_ilmid = $ilm_param_id;
  6626. $param->param_name = "";
  6627. $param->param_value = "";
  6628. $param->description = "";
  6629. $param->visible = 1;
  6630. }
  6631. elseif ($action == 'edit') {
  6632. if ($iassign_ilm_config) {
  6633. $param->title = get_string('edit_ilm', 'iassign');
  6634. $param->id = $iassign_ilm_config->id;
  6635. $param->iassign_ilmid = $iassign_ilm_config->iassign_ilmid;
  6636. $param->param_type = $type; //MOOC2014
  6637. $param->param_name = $iassign_ilm_config->param_name;
  6638. if ($type != 'choice' && $type != 'multiple') //MOOC2014
  6639. $param->param_value = $iassign_ilm_config->param_value;
  6640. else //MOOC2014
  6641. $param->param_value = str_replace(", ", "\n", $iassign_ilm_config->param_value); //MOOC2014
  6642. $param->param_value = $iassign_ilm_config->param_value;
  6643. $param->description = $iassign_ilm_config->description;
  6644. $param->visible = $iassign_ilm_config->visible;
  6645. }
  6646. }
  6647. elseif ($action == 'copy') {
  6648. if ($iassign_ilm_config) {
  6649. $param->title = get_string('copy_ilm', 'iassign');
  6650. $param->iassign_ilmid = $iassign_ilm_config->iassign_ilmid;
  6651. $param->param_type = $type; //MOOC2014
  6652. $param->param_name = $iassign_ilm_config->param_name;
  6653. if ($type != 'choice' && $type != 'multiple') //MOOC2014
  6654. $param->param_value = $iassign_ilm_config->param_value;
  6655. else //MOOC2014
  6656. $param->param_value = str_replace(", ", "\n", $iassign_ilm_config->param_value); //MOOC2014
  6657. $param->description = $iassign_ilm_config->description;
  6658. $param->visible = $iassign_ilm_config->visible;
  6659. }
  6660. }
  6661. return $param;
  6662. } // static function add_edit_copy_param($ilm_param_id, $action)
  6663. /// Function for change visibility of iLM param
  6664. // @param int $ilm_param_id Id of iLM param
  6665. // @param int $status Indicator of change vibility (0 = hide, 1 = show)
  6666. static function visible_param ($ilm_param_id, $status) {
  6667. global $DB, $CFG;
  6668. if ($status == 0)
  6669. $visible = 1;
  6670. else
  6671. $visible = 0;
  6672. $newentry = new stdClass();
  6673. $newentry->id = $ilm_param_id;
  6674. $newentry->visible = $visible;
  6675. if (!$DB->update_record("iassign_ilm_config", $newentry))
  6676. error(get_string('error_edit_param', 'iassign'));
  6677. }
  6678. /// Function for save in database an new iLM param
  6679. // @param object $param An object with iLM params
  6680. static function add_param ($param) {
  6681. global $DB;
  6682. $newentry = new stdClass();
  6683. $newentry->iassign_ilmid = $param->iassign_ilmid;
  6684. //MOOC2014 $newentry->param_name = $param->param_name;
  6685. $newentry->param_type = $param->param_type; //MOOC2014
  6686. $newentry->param_name = iassign_utils::format_filename($param->param_name); //MOOC2014
  6687. if ($newentry->param_type != 'choice' && $newentry->param_type != 'multiple') //MOOC2014
  6688. $newentry->param_value = $param->param_value;
  6689. else //MOOC2014
  6690. $newentry->param_value = str_replace("\r\n", ", ", $param->param_value); //MOOC2014
  6691. $newentry->description = $param->description;
  6692. $newentry->visible = $param->visible;
  6693. $newentry->id = $DB->insert_record("iassign_ilm_config", $newentry);
  6694. if (!$newentry->id) {
  6695. print_error('error_add_param', 'iassign');
  6696. }
  6697. }
  6698. /// Function for save in database a iLM param edit
  6699. // @param object $param An object with iLM params
  6700. static function edit_param ($param) {
  6701. global $DB;
  6702. $updentry = new stdClass();
  6703. $updentry->id = $param->id;
  6704. $updentry->iassign_ilmid = $param->iassign_ilmid;
  6705. $updentry->param_type = $param->param_type; //MOOC2014
  6706. // $updentry->param_name = $param->param_name;
  6707. $newentry->param_name = iassign_utils::format_filename($param->param_name); //MOOC2014
  6708. if ($updentry->param_type != 'choice' && $updentry->param_type != 'multiple') //MOOC2014
  6709. $updentry->param_value = $param->param_value;
  6710. else //MOOC2014
  6711. $updentry->param_value = str_replace("\r\n", ", ", $param->param_value); //MOOC2014
  6712. $updentry->description = $param->description;
  6713. $updentry->visible = $param->visible;
  6714. if (!$DB->update_record("iassign_ilm_config", $updentry)) {
  6715. error(get_string('error_edit_param', 'iassign'));
  6716. }
  6717. }
  6718. /// Function for save in database a iLM param copy
  6719. // @param object $param An object with iLM params
  6720. static function copy_param ($param) {
  6721. global $DB;
  6722. $newentry = new stdClass();
  6723. $newentry->iassign_ilmid = $param->iassign_ilmid;
  6724. //$newentry->param_name = $param->param_name;
  6725. $newentry->param_name = iassign_utils::format_filename($param->param_name); //MOOC2014
  6726. if ($newentry->param_type != 'choice' && $newentry->param_type != 'multiple') //MOOC2014
  6727. $newentry->param_value = $param->param_value;
  6728. else //MOOC2014
  6729. $newentry->param_value = str_replace("\r\n", ", ", $param->param_value); //MOOC2014
  6730. $newentry->description = $param->description;
  6731. $newentry->visible = $param->visible;
  6732. $newentry->id = $DB->insert_record("iassign_ilm_config", $newentry);
  6733. if (!$newentry->id) {
  6734. print_error('error_add_param', 'iassign');
  6735. }
  6736. }
  6737. /// Function for delete iLM param of database
  6738. // @param int $param_id Id of iLM param
  6739. static function delete_param ($param_id) {
  6740. global $DB;
  6741. if (!$DB->delete_records("iassign_ilm_config", array('id' => $param_id))) {
  6742. print_error('error_delete_param', 'iassign');
  6743. }
  6744. }
  6745. } // class ilm_settings
  6746. /// Class for manage iLM files (editor).
  6747. class ilm_manager {
  6748. var $id; // course id
  6749. var $url;
  6750. var $from;
  6751. /// Constructor for the base ilm_manager class
  6752. // 3.1 update PHP 7.0 compatibility for all moodle versions
  6753. //D public function ilm_manager($id, $url, $from) { self::__construct($iassign, $cm, $course); }
  6754. function __construct ($id, $url, $from) {
  6755. $this->id = $id; // course id
  6756. $this->url = $url;
  6757. $this->from = $from;
  6758. }
  6759. /// Function to get iAssign content file in Moodle data (exercise)
  6760. // @calledby x function preview_ilm($iassign_ilm) :
  6761. // @calledby ilm_manager.php : with 'action=get' in '$ilm_manager_instance->get_file_ilm($ilmid, $fileid)'
  6762. function get_file_ilm () {
  6763. $ilmid = optional_param('ilmid', NULL, PARAM_INT);
  6764. $fileid = optional_param('fileid', NULL, PARAM_INT);
  6765. //$filename = optional_param('filename', NULL, PARAM_TEXT);
  6766. $fs = get_file_storage(); // Get reference to all files in Moodle data
  6767. $md_file = $fs->get_file_by_id($fileid);
  6768. $ilm_content_file = $md_file->get_content();
  6769. return $ilm_content_file;
  6770. }
  6771. /// Function for creating a new file from the iLM online editor
  6772. // @calledby ilm_manager.php: case 'new': $ilm_manager_instance->ilm_editor_new();
  6773. function ilm_editor_new () {
  6774. global $CFG, $DB, $OUTPUT, $PAGE;
  6775. $ilmid = optional_param('ilmid', NULL, PARAM_INT); // iAssign ID
  6776. $dirid = optional_param('dirid', NULL, PARAM_INT);
  6777. $iassign = $DB->get_record("iassign_ilm", array("id" => $ilmid));
  6778. $context = context_course::instance($this->id);
  6779. $returnurl = $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=$this->id&dirid=$dirid&ilmid=$ilmid";
  6780. // verify if the JAR file $PAGE->set_course($course); is registered in DB (table '*_iassign_ilm')
  6781. if (!$iassign) {
  6782. print $OUTPUT->notification(get_string('error_confirms_ilm', 'iassign'), 'notifysuccess');
  6783. die();
  6784. }
  6785. $temp = explode(",", $iassign->extension);
  6786. $extension = $temp[0]; // default extension for this iLM
  6787. //2016/02/16: IMPORTANTE trocar formatador para "nao formatado", pois esta destruindo o nome do arquivo (e.g. limpa brancos)
  6788. //2016/02/16: $stringArchiveContent = optional_param('iLM_PARAM_ArchiveContent', NULL, PARAM_ALPHANUMEXT);
  6789. $stringArchiveContent = optional_param('iLM_PARAM_ArchiveContent', NULL, PARAM_RAW); // iLM 2
  6790. // $stringArchiveContent = optional_param('iLM_PARAM_ArchiveContent', NULL, PARAM_RAW); // iLM 1
  6791. if ($stringArchiveContent != NULL) {
  6792. // $stringArchiveContent = $_POST['iLM_PARAM_ArchiveContent'];
  6793. //2016/02/16: IMPORTANTE trocar formatador para "nao formatado", pois esta destruindo o conteudo do arquivo
  6794. //2016/02/16: $filename = optional_param('filename', NULL, PARAM_ALPHANUMEXT);
  6795. $filename = optional_param('filename', NULL, PARAM_RAW);
  6796. //Leo $filename = iassign_utils::format_filename($filename); //
  6797. $filename = iassign_utils::filter_filename_extension($filename, $extension); // here: class iassign_utils
  6798. //2019/02 It is not necessary, since 'iassign_utils::filter_filename_extension(...)' does all the work!
  6799. //2019/02 $arrayfilename = explode(".", $filename);
  6800. //2019/02 // if (count($arrayfilename) == 1) $filename = $arrayfilename[0] . '.' . $extension;
  6801. //2019/02 $count_dots = count($arrayfilename);
  6802. //2019/02 if ($count_dots > 0) { // at least one dot mark
  6803. //2019/02 $last_name = $arrayfilename[$count_dots - 1];
  6804. //2019/02 if ($last_name != $extension)
  6805. //2019/02 $filename = $filename . '.' . $extension;
  6806. //2019/02 }
  6807. //2019/02 else { // no extension...
  6808. //2019/02 $filename = $filename . '.' . $extension;
  6809. //2019/02 }
  6810. // Store the name of recent created file
  6811. $_SESSION['file_name'] = $filename;
  6812. //$this->write_file_iassign($string, $filename);
  6813. // Register and (by JS.alet(.)) inform the user about the result (and the final name of the file)
  6814. $this->write_file_iassign($stringArchiveContent, $filename); // class ilm_manager - inform the user about the file insertion result
  6815. die();
  6816. }
  6817. else { // if ($stringArchiveContent != NULL)
  6818. // iLM On-line editor
  6819. if ($extension == "html" || $extension == "ivph" || strtolower($iassign->type) == 'html5') { // if iLM is HTML5 is inside a frame
  6820. $str_get_iLM = "window.frames.iLM";
  6821. $str_submitbutton_name = "javascript:window.submit_iLM_Answer()";
  6822. }
  6823. else { // otherwise it is JAR named 'iLM'
  6824. $str_get_iLM = "document.iLM";
  6825. $str_submitbutton_name = "submit_iLM_Answer()"; // to call 'submit_iLM_Answer()'
  6826. }
  6827. $fs = get_file_storage(); // Get reference to all files in Moodle data
  6828. $files = $fs->get_area_files($context->id, 'mod_iassign', 'activity');
  6829. $files_array = '';
  6830. foreach ($files as $value) {
  6831. if ($value->get_filename() != ".")
  6832. $files_array .= "'" . $value->get_filename() . "',";
  6833. }
  6834. $files_array .= "''";
  6835. $file = null;
  6836. $ia_content = "";
  6837. $filename = "";
  6838. $error_files_exists = get_string('error_file_exists', 'iassign');
  6839. $output = "<script type='text/javascript'>
  6840. //<![CDATA[
  6841. //D alert('locallib.php: ilm_editor_new');
  6842. function submit_iLM_Answer () {
  6843. var docFormOnLineEditor = document.formEnvio;
  6844. var resposta_exerc = new Array(3);
  6845. var valor_resposta = new Array(3);
  6846. var sessao = new Array(3);
  6847. var doc_iLM = " . $str_get_iLM . "; // 'window.frames.iLM' or 'document.iLM'
  6848. resposta_exerc[0] = doc_iLM.getAnswer();
  6849. valor_resposta[0] = doc_iLM.getEvaluation();
  6850. docFormOnLineEditor.iLM_PARAM_ActivityEvaluation.value = valor_resposta[0];
  6851. docFormOnLineEditor.iLM_PARAM_ArchiveContent.value = resposta_exerc[0];
  6852. var files = new Array(" . $files_array . ");
  6853. var filename = docFormOnLineEditor.filename.value+'.'+'" . $extension . "';
  6854. if (docFormOnLineEditor.filename.value=='') {
  6855. // ERROR: the file name is empty
  6856. alert('" . get_string('error_file_null_iassign', 'iassign') . "');
  6857. return false;
  6858. }
  6859. // }
  6860. for (i=0; i<files.length; i++) {
  6861. if (files[i]==docFormOnLineEditor.filename.value || files[i]==filename) {
  6862. alert('" . $error_files_exists . "');
  6863. return false;
  6864. }
  6865. }
  6866. docFormOnLineEditor.submit();
  6867. return true;
  6868. }
  6869. //]]>
  6870. </script>\n";
  6871. $output .= "
  6872. <form name='formEnvio' id='formEnvio' method='post' enctype='multipart/form-data'>\n";
  6873. $output .= $OUTPUT->box_start();
  6874. // Put text "File name" and the corresponding "input" to enter the file nama: "File name: [ ]"
  6875. $output .= "
  6876. <table width='100%' cellpadding='20'>
  6877. <tr><td>" . get_string('label_file_iassign', 'iassign') . " <input type='text' name='filename' size=50/>
  6878. <input type=button value='" . get_string('label_write_iassign', 'iassign') . "' title='' onclick='" . $str_submitbutton_name . ";'/></td>
  6879. </tr>
  6880. </table>\n";
  6881. $output .= $OUTPUT->box_end();
  6882. $output .= "<center>\n";
  6883. // Prepare tag to load the iLM. In case of JAR it will prepare the tag "applet". In case of HTML5 will prepare an "iframe".
  6884. // Since it is the activity file, it is not necessary to use 'ilm_security'
  6885. $output_ilm = ilm_settings::build_ilm_tags($ilmid, array("type" => "editor_new", "notSEND" => "true"));
  6886. $output .= $output_ilm;
  6887. $output .= " <input type='hidden' name='iLM_PARAM_ArchiveContent' value='" . $ia_content . "'>
  6888. <input type='hidden' name='iLM_PARAM_ActivityEvaluation'>
  6889. </center>
  6890. </form>\n";
  6891. $title = get_string('title_editor_iassign', 'iassign') . " - " . $iassign->name . " " . $iassign->version; //MOOC2014
  6892. $PAGE->navbar->add($title);
  6893. $PAGE->set_title($title);
  6894. $PAGE->set_heading($title); // insert title above the navigation bar
  6895. print $OUTPUT->header();
  6896. //print $OUTPUT->heading(" " . $title); // insert title below the navigation bar
  6897. print $output;
  6898. print $OUTPUT->footer();
  6899. }
  6900. die();
  6901. } // function ilm_editor_new()
  6902. /// Function for editing an file in online editor
  6903. // @calledby ilm_manager.php: case 'update': $ilm_manager_instance->ilm_editor_update($security['content'], $security['token'], $security['secure_id']);
  6904. function ilm_editor_update ($filename, $content_file, $token, $secure_id) {
  6905. global $CFG, $DB, $OUTPUT, $PAGE;
  6906. $ilmid = optional_param('ilmid', NULL, PARAM_INT); // iAssign ID
  6907. $dirid = optional_param('dirid', NULL, PARAM_INT);
  6908. $fileid = optional_param('fileid', NULL, PARAM_TEXT);
  6909. $iassign = $DB->get_record("iassign_ilm", array("id" => $ilmid));
  6910. $returnurl = $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=$this->id&dirid=$dirid&ilmid=$ilmid";
  6911. // If iLM is not registered, error!
  6912. if (!$iassign) {
  6913. print $OUTPUT->notification(get_string('error_confirms_ilm', 'iassign'), 'notifyproblem');
  6914. die;
  6915. }
  6916. //2017/03/12 //QUARANTINE agora vindo de 'ilm_manager.php' via os 3 parametros
  6917. //2017/03/12 $fs = get_file_storage(); // Get reference to all files in Moodle data // from Moodle data
  6918. //2017/03/12 $filename = ''; $end_file = ''; $file = $fs->get_file_by_id($fileid); if ($file) $filename = iassign_utils::format_filename($file->get_filename());
  6919. //D echo "locallib.php: ilm_editor_update(): filename=$filename<br/>\n";
  6920. //2016/02/16: IMPORTANTE trocar formatador para "nao formatado", pois esta destruindo o conteudo do arquivo
  6921. //2016/02/16: $stringArchiveContent = optional_param('iLM_PARAM_ArchiveContent', NULL, PARAM_ALPHANUMEXT);
  6922. $stringArchiveContent = optional_param('iLM_PARAM_ArchiveContent', NULL, PARAM_RAW);
  6923. //D echo "stringArchiveContent:$stringArchiveContent<br/>\n";
  6924. if ($stringArchiveContent != NULL) {
  6925. $this->update_file_iassign($stringArchiveContent, $filename, $fileid);
  6926. die();
  6927. } else { // if ($stringArchiveContent != NULL)
  6928. $end_file = '';
  6929. if ($content_file) {
  6930. // 2017/03/12 $token=''; $view=-1; $end_file = $CFG->wwwroot . '/mod/iassign/ilm_security.php?id=' . $fileid . '&token=' . $token . '&view=' . $view; // need full path...
  6931. $end_file = $CFG->wwwroot . '/mod/iassign/ilm_security.php?id=' . $secure_id . '&action=update&token=' . $token . '&view=0'; // need full path...
  6932. }
  6933. $temp = explode(",", $iassign->extension);
  6934. $extension = $temp[0]; // default extension for this iLM
  6935. if ($extension == "html" || $extension == "ivph" || strtolower($iassign->type) == 'html5') { // if iLM is HTML5 is inside a frame
  6936. $str_get_iLM = "window.frames.iLM";
  6937. $str_submitbutton_name = "javascript:window.submit_iLM_Answer()";
  6938. } else { // otherwise it is JAR named 'iLM'
  6939. $str_get_iLM = "document.iLM";
  6940. $str_submitbutton_name = "submit_iLM_Answer()"; // to call 'submit_iLM_Answer()'
  6941. }
  6942. $output = "<script type='text/javascript'>
  6943. //<![CDATA[
  6944. function submit_iLM_Answer () {
  6945. var docFormEditor = document.formEnvio;
  6946. var activityAnswer; // to get activity answer (text)
  6947. var activityValue; // to get activity answer value (float)
  6948. var doc_iLM = " . $str_get_iLM . "; // 'window.frames.iLM' or 'document.iLM'
  6949. //activityAnswer = doc_iLM.getAnswer();
  6950. //activityValue = doc_iLM.getEvaluation();
  6951. activityAnswer = " . $str_get_iLM . ".getAnswer();
  6952. activityValue = " . $str_get_iLM . ".getEvaluation();
  6953. docFormEditor.iLM_PARAM_ActivityEvaluation.value = activityValue;
  6954. docFormEditor.iLM_PARAM_ArchiveContent.value = activityAnswer;
  6955. docFormEditor.submit();
  6956. return true;
  6957. }
  6958. //]]>
  6959. </script>\n";
  6960. // 2016/02/16: NOT necessary, since it is teacher editing (perhaps he only make an example as exercise)
  6961. // if (activityAnswer == -1) { // E.g. in iGeom it is possible to turn an example in exercise
  6962. // alert('" . get_string('error_null_iassign', 'iassign') . "'); // ERRO: O exercício esta vazio ou não foi alterado
  6963. // return true;
  6964. // }
  6965. $output .= "
  6966. <form name='formEnvio' id='formEnvio' method='post' enctype='multipart/form-data'>\n";
  6967. $output .= $OUTPUT->box_start();
  6968. $output .= "
  6969. <table width='100%' cellpadding='20'>
  6970. <tr><td width='75%'>" . get_string('label_file_iassign', 'iassign') . "<b>$filename</b></td>
  6971. <td align='right' width='25%'><input type=button value='" . get_string('label_write_iassign', 'iassign') . "' title='' onclick='submit_iLM_Answer();'/>
  6972. <input type='hidden' name='filename' value='$filename'/></td>
  6973. <td>
  6974. <input type=button value='" . get_string('close', 'iassign') . "' title='' onclick='javascript:window.location = \"$returnurl\";'/></td>
  6975. </tr>
  6976. </table>\n";
  6977. $output .= $OUTPUT->box_end();
  6978. $output .= " <center>\n";
  6979. // Since it is the activity file, it is not necessary to use 'ilm_security' (id_iLM_security)
  6980. $output .= ilm_settings::build_ilm_tags($ilmid, array("type" => "editor_update", "notSEND" => "false", "Proposition" => $end_file));
  6981. $output .= " <input type='hidden' name='iLM_PARAM_ArchiveContent' value=''>
  6982. <input type='hidden' name='iLM_PARAM_ActivityEvaluation' value=''>
  6983. </center>
  6984. </form>\n";
  6985. $title = get_string('title_editor_iassign', 'iassign') . " - " . $iassign->name . " " . $iassign->version; //MOOC2014
  6986. $PAGE->navbar->add($title);
  6987. $PAGE->set_title($title);
  6988. $PAGE->set_heading($title); // insert title above the navigation bar
  6989. print $OUTPUT->header();
  6990. //print $OUTPUT->heading(" " . $title); // insert title below the navigation bar
  6991. print $output;
  6992. print $OUTPUT->footer();
  6993. } // else if ($stringArchiveContent != NULL)
  6994. die();
  6995. } // function ilm_editor_update()
  6996. /// Function for write iAssign file in Moodle data (exercise)
  6997. // @calledby ilm_editor_new(): $this->write_file_iassign($stringArchiveContent, $filename);
  6998. // @param string $stringArchiveContent Content of iassign file
  6999. // @param string $filename Filename of iassign file
  7000. function write_file_iassign ($stringArchiveContent, $filename) {
  7001. global $USER;
  7002. $ilmid = optional_param('ilmid', NULL, PARAM_INT);
  7003. $context = context_course::instance($this->id);
  7004. $fs = get_file_storage(); // Get reference to all files in Moodle data
  7005. $dirid = $this->get_dir_ilm('dirid');
  7006. $dir = $fs->get_file_by_id($dirid);
  7007. $fileinfo = array('contextid' => $context->id, // ID of course
  7008. 'component' => 'mod_iassign', // usually = table name
  7009. 'filearea' => 'activity', // usually = table name
  7010. 'itemid' => 0, // usually = ID of row in table
  7011. 'filepath' => '/', // any path beginning and ending with '/'
  7012. 'userid' => $USER->id,
  7013. 'author' => $USER->firstname . ' ' . $USER->lastname, 'license' => 'allrightsreserved', // allrightsreserved
  7014. 'filename' => $filename); // any filename
  7015. // Create file containing text. '$stringArchiveContent'
  7016. //TODO Verificar se apenas esta' inserindo nova referencia em 'files' (NAO deve duplicar no Moodle Data)
  7017. $file_course = $fs->create_file_from_string($fileinfo, $stringArchiveContent);
  7018. // sucess_write -> 'Interactive assignment successfully registered under the name: '
  7019. // 'Tarea interactiva registrada exitosamente bajo el nombre:'
  7020. // 'Activité interactive enregistrée avec succès sous le nom:';
  7021. // 'Atividade interativa gravada com sucesso sob o nome:'
  7022. $output = "
  7023. <script type='text/javascript'>
  7024. //<![CDATA[
  7025. alert('" . get_string('sucess_write', 'iassign') . " " . $filename . "');
  7026. window.location='" . $this->url . "&dirid=$dirid&ilmid=$ilmid';
  7027. //]]>
  7028. </script>";
  7029. print $output;
  7030. die();
  7031. }
  7032. /// Function for write iAssign file in Moodle data (exercise)
  7033. // @param string $stringArchiveContent Content of iassign file
  7034. // @param string $filename Filename of iassign file
  7035. // @param int $itemid Itemid of iassign file
  7036. function update_file_iassign ($stringArchiveContent, $filename, $fileid) {
  7037. global $OUTPUT, $USER;
  7038. $ilmid = optional_param('ilmid', NULL, PARAM_INT);
  7039. $context = context_course::instance($this->id);
  7040. $dirid = $this->get_dir_ilm('dirid');
  7041. if ($stringArchiveContent != (-1)) {
  7042. $fs = get_file_storage(); // Get reference to all files in Moodle data
  7043. $file = $fs->get_file_by_id($fileid);
  7044. if (!$file) {
  7045. print $OUTPUT->notification(get_string('error_view_ilm', 'iassign'), 'notifyproblem');
  7046. die;
  7047. }
  7048. $fileinfo = array('contextid' => $context->id, // ID of context
  7049. 'component' => 'mod_iassign', // usually = table name
  7050. 'filearea' => 'activity', // usually = table name
  7051. 'itemid' => 0, // usually = ID of row in table
  7052. 'filepath' => $file->get_filepath(), // any path beginning and ending with '/'
  7053. 'userid' => $USER->id,
  7054. 'author' => $USER->firstname . ' ' . $USER->lastname,
  7055. 'license' => 'allrightsreserved', // allrightsreserved
  7056. 'timecreated' => $file->get_timecreated(),
  7057. 'filename' => $file->get_filename()); // any filename
  7058. $file->delete();
  7059. //TODO Verificar se apenas esta' inserindo nova referencia em 'files' (NAO deve duplicar no Moodle Data)
  7060. $file_course = $fs->create_file_from_string($fileinfo, $stringArchiveContent); //$string
  7061. }
  7062. $output = "<script type='text/javascript'>
  7063. //<![CDATA[
  7064. alert('" . get_string('sucess_update', 'iassign') . "');\n
  7065. window.location='" . $this->url . "&dirid=$dirid&ilmid=$ilmid';
  7066. //]]>
  7067. </script>";
  7068. print $output;
  7069. die();
  7070. }
  7071. /// Function for create an tag for iAssign filter
  7072. // @calledby function preview_ilm($iassign_ilm) : $tag_filter = $this->tag_ilm($fileid);
  7073. // @calledby function tinymce_ilm($fileid) : $tag_filter = $this->tag_ilm($fileid);
  7074. // @calledby function editor_ilm($fileid, $editor) : $tag_filter = $this->tag_ilm($fileid);
  7075. // @calledby function atto_ilm ($fileid) : $tag_filter = $this->tag_ilm($fileid);
  7076. // @param int $fileid Id of file
  7077. // @return string Return an string with an tag of iassign filter
  7078. function tag_ilm ($fileid) {
  7079. global $DB;
  7080. $fs = get_file_storage(); // Get reference to all files in Moodle data
  7081. $width = '600';
  7082. $height = '400';
  7083. $file = $fs->get_file_by_id($fileid);
  7084. $filetype = explode(".", $file->get_filename());
  7085. $iassign_ilm = $DB->get_records('iassign_ilm', array("enable" => 1, "parent" => 0));
  7086. foreach ($iassign_ilm as $value) {
  7087. $extensions = explode(",", $value->extension);
  7088. if (in_array($filetype[1], $extensions)) {
  7089. $width = $value->width;
  7090. $height = $value->height;
  7091. }
  7092. }
  7093. return("<p>&lt;ia toolbar=disable width=$width height=$height &gt;$fileid&lt;/ia&gt;</p>");
  7094. }
  7095. /// Function for delete iAssign file in Moodle data (exercise)
  7096. function delete_file_ilm () {
  7097. $ilmid = optional_param('ilmid', NULL, PARAM_INT);
  7098. $fs = get_file_storage(); // Get reference to all files in Moodle data
  7099. $fileid = optional_param('fileid', NULL, PARAM_RAW);
  7100. $file = $fs->get_file_by_id($fileid);
  7101. if ($file)
  7102. $file->delete();
  7103. redirect(new moodle_url($this->url . '&dirid=' . $this->get_dir_ilm('dirid') . '&ilmid=' . $ilmid));
  7104. die();
  7105. }
  7106. /// Function for delete selected iAssign file in Moodle data (exercise)
  7107. function delete_selected_ilm () {
  7108. $ilmid = optional_param('ilmid', NULL, PARAM_INT);
  7109. $fs = get_file_storage(); // Get reference to all files in Moodle data
  7110. $context = context_course::instance($this->id);
  7111. $files_id = explode(",", optional_param('files_id', '', PARAM_TEXT));
  7112. $dirid = $this->get_dir_ilm('dirid');
  7113. foreach ($files_id as $file_id) {
  7114. $file = $fs->get_file_by_id($file_id);
  7115. if ($file) {
  7116. if (!$file->is_directory())
  7117. $file->delete();
  7118. else {
  7119. $files_delete = $fs->get_directory_files($context->id, 'mod_iassign', 'activity', 0, $file->get_filepath(), true, true);
  7120. foreach ($files_delete as $value)
  7121. $value->delete();
  7122. $file->delete();
  7123. }
  7124. }
  7125. }
  7126. redirect(new moodle_url($this->url . '&dirid=' . $dirid . '&ilmid=' . $ilmid));
  7127. die();
  7128. }
  7129. /// Function for duplicate iAssign file from "online" edition
  7130. // @callby JavaScript function 'duplicate_ilm(ilmid, filename, fileid)' bellow
  7131. function duplicate_file_ilm () {
  7132. global $USER, $COURSE;
  7133. $ilmid = optional_param('ilmid', NULL, PARAM_INT);
  7134. $fs = get_file_storage(); // Get reference to all files in Moodle data
  7135. $fileid = optional_param('fileid', NULL, PARAM_INT);
  7136. $filename = optional_param('filename', NULL, PARAM_RAW);
  7137. $file = $fs->get_file_by_id($fileid);
  7138. $context = context_course::instance($this->id);
  7139. $fileinfo = array(
  7140. 'contextid' => $context->id, // ID of context
  7141. 'component' => 'mod_iassign', // usually = table name
  7142. 'filearea' => 'activity', // usually = table name
  7143. 'itemid' => 0, // usually = ID of row in table
  7144. 'filepath' => $this->get_dir_ilm('dir_base'), // any path beginning and ending in /
  7145. 'userid' => $USER->id,
  7146. 'author' => $USER->firstname . ' ' . $USER->lastname, 'license' => 'allrightsreserved', // allrightsreserved
  7147. 'timecreated' => $file->get_timecreated(), 'filename' => $filename); // any filename
  7148. $newfile_fs = $fs->create_file_from_string($fileinfo, $file->get_content());
  7149. redirect(new moodle_url($this->url . "&dirid=" . $this->get_dir_ilm('dirid') . "&ilmid=" . $ilmid));
  7150. die();
  7151. }
  7152. /// Function for rename iAssign file
  7153. function rename_file_ilm () {
  7154. $ilmid = optional_param('ilmid', NULL, PARAM_INT);
  7155. $fs = get_file_storage(); // Get reference to all files in Moodle data
  7156. $fileid = optional_param('fileid', NULL, PARAM_INT);
  7157. $filename = optional_param('filename', NULL, PARAM_TEXT);
  7158. $file = $fs->get_file_by_id($fileid);
  7159. $file->rename($this->get_dir_ilm('dir_base'), $filename);
  7160. //MOOC2014 redirect(new moodle_url($this->url . "&dirid=" . $dir_parent . "&ilmid=$ilmid"));
  7161. redirect(new moodle_url($this->url . '&dirid=' . $this->get_dir_ilm('dirid') . '&ilmid=' . $ilmid));
  7162. die();
  7163. }
  7164. /// Function for get iassign file for iassign form
  7165. function add_ilm () {
  7166. $fileid = optional_param('fileid', NULL, PARAM_INT);
  7167. $filename = optional_param('filename', NULL, PARAM_TEXT);
  7168. $output = "
  7169. <script type='text/javascript'>
  7170. //<![CDATA[
  7171. var iassign_file_link = window.opener.document.getElementById('iassign_file_link');
  7172. iassign_file_link.innerHTML = '$filename';
  7173. window.opener.document.forms['mform1'].file.value='$fileid';
  7174. window.opener.document.forms['mform1'].filename.value='$filename';
  7175. window.close();
  7176. //]]>
  7177. </script>";
  7178. print $output;
  7179. die();
  7180. }
  7181. /// Function for preview iAssign file from iAssign Repository (it uses iAssign filter)
  7182. // @see /iassign_filter/filter.php : function 'filter($text, array $options = array())' and exit (do not continue bellow)
  7183. // @see ilm_manager.php : $ilm_manager_instance->preview_ilm();
  7184. function preview_ilm ($courseid, $iassign_ilm) {
  7185. global $OUTPUT, $CFG, $USER;
  7186. $fileid = optional_param('fileid', NULL, PARAM_TEXT);
  7187. $title = get_string('modulename', 'iassign'); // iAssign
  7188. print $OUTPUT->header();
  7189. print $OUTPUT->box_start();
  7190. $javascript = "
  7191. <!-- iAssign preview iLM content / LInE - http://line.ime.usp.br -->
  7192. <script type='text/javascript'>
  7193. //<![CDATA[
  7194. function submit_close () {
  7195. window.opener.location.reload();
  7196. window.close();
  7197. }
  7198. //]]>
  7199. </script>\n";
  7200. // Use iAssin filter to change "&lt;ia toolbar=disable width=800 height=600 &gt;45&lt;/ia&gt;"
  7201. // to the complete "applet" tag
  7202. // Version previous to: 2016/02/16: do not present menus' options - similar to the filter
  7203. //TODO: E' melhor apresentar o iMA completo, sem usar o filtro com opcao "<param name='SOH_ADD' value='ADD'>"
  7204. //TODO: Talvez seja melhor colocar opcao no filtro para evitar opcao 'ADD'!
  7205. //TODO: Here reaches '/iassign_filter/filter.php : function 'filter($text, array $options = array())' with parameter 'originalformat=0' (in glossary is 1)
  7206. //TODO: then, I changed '/iassign_filter/filter.php' (functions 'filter(...) to avoid 'SOH_ADD' if 'originalformat=0'
  7207. $ilm_name = substr(strtolower($iassign_ilm->name), 0, 6);
  7208. require_once($CFG->dirroot . '/mod/iassign/ilm_security.php');
  7209. $content_or_id_from_ilm_security = $this->get_file_ilm();
  7210. $timecreated = time();
  7211. $token = md5($timecreated);
  7212. $id_iLM_security = ilm_security::write_iLM_security($USER->id, $timecreated, -1, $content_or_id_from_ilm_security); // ./mod/iassign/ilm_security.php: insert in 'iassign_security'
  7213. if (!isset($iassign_ilm->id))
  7214. $iassign_ilm->id = -1; // avoid warning "Warning: Undefined property: stdClass::$id"
  7215. $param = 'id=' . $id_iLM_security . '&ilmid=' . $iassign_ilm->id . '&token=' . $token . '&view=0';
  7216. $url_file = $CFG->wwwroot . '/mod/iassign/ilm_security.php?action=preview&' . $param;
  7217. $tag_filter_filtered = ilm_settings::build_ilm_tags($iassign_ilm->id, array("type" => "view", "notSEND" => "true", "Proposition" => $url_file)); // buil iLM tag (JAR or HTML5)
  7218. $html = "
  7219. <form name='formEnvio' id='formEnvio' method='post' enctype='multipart/form-data'>
  7220. <table border='1'>
  7221. <tr><td>
  7222. <!-- iLM calls : begin -->\n" . $tag_filter_filtered . "\n
  7223. <!-- iLM calls : end -->
  7224. </td></tr>
  7225. </table>
  7226. <table>
  7227. <tr>
  7228. <td align='center'>
  7229. <input type=button value='" . get_string('close', 'iassign') . "' title='' onclick='submit_close();' />
  7230. </td></tr>
  7231. </table>
  7232. </form>\n";
  7233. print $javascript . $html;
  7234. print $OUTPUT->box_end();
  7235. //NAO echo format_string($html); // Moodle 3.X
  7236. die();
  7237. } // function preview_ilm($courseid, $iassign_ilm)
  7238. //r_ function preview_ilm ($iassign_ilm)
  7239. //r_ /// Function for preview iassign file from iassign filter.
  7240. //r_ function preview_ilm() {
  7241. //r_ $fileid = optional_param('fileid', NULL, PARAM_TEXT);
  7242. //r_ $tag_filter = $this->tag_ilm($fileid);
  7243. //r_ $javascript = "<script type='text/javascript'>
  7244. //r_ //<![CDATA[
  7245. //r_ function submit_close() { window.opener.location.reload(); window.close(); }
  7246. //r_ //]]>
  7247. //r_ </script>";
  7248. //r_ $html = "<html><head></head><body><form name='formEnvio' id='formEnvio' method='post' enctype='multipart/form-data'>
  7249. //r_ <table border='1'><tr><td>$tag_filter</td></tr></table><table><tr><td align='center'><input type=button value='" . get_string('close', 'iassign') . "' title='' onclick='submit_close();'/></td></tr>
  7250. //r_ </table></form></body></html>";
  7251. //r_ echo $javascript . format_text($html);
  7252. //r_ die;
  7253. //r_ }
  7254. /// Function for export an package (zip) of iassign files
  7255. function export_files_ilm () {
  7256. global $CFG;
  7257. $context = context_course::instance($this->id);
  7258. $files_id = explode(",", optional_param('files_id', '', PARAM_TEXT));
  7259. $zip_filename = $CFG->dataroot . '/temp/backup-iassign-files-' . date("Ymd-Hi") . '.zip';
  7260. $zip = new zip_archive();
  7261. $zip->open($zip_filename);
  7262. $fs = get_file_storage(); // Get reference to all files in Moodle data
  7263. foreach ($files_id as $file_id) {
  7264. $file = $fs->get_file_by_id($file_id);
  7265. if (!$file->is_directory())
  7266. $zip->add_file_from_string($file->get_filename(), $file->get_content());
  7267. else {
  7268. $zip->add_directory($file->get_filepath());
  7269. $files_zip = $fs->get_directory_files($context->id, 'mod_iassign', 'activity', 0, $file->get_filepath(), true, true);
  7270. foreach ($files_zip as $value) {
  7271. if (!$value->is_directory())
  7272. $zip->add_file_from_string($value->get_filepath() . $value->get_filename(), $value->get_content());
  7273. else
  7274. $zip->add_directory($value->get_filepath());
  7275. }
  7276. }
  7277. }
  7278. $zip->close();
  7279. header("Pragma: public");
  7280. header("Expires: 0");
  7281. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  7282. header("Cache-Control: private", false);
  7283. header("Content-Type: application/zip");
  7284. header("Content-Disposition: attachment; filename=\"" . basename($zip_filename) . "\";");
  7285. header("Content-Transfer-Encoding: binary");
  7286. header("Content-Length: " . @filesize($zip_filename));
  7287. set_time_limit(0);
  7288. @readfile("$zip_filename") || die("File not found.");
  7289. unlink($zip_filename);
  7290. exit;
  7291. }
  7292. /// Function of execute a command in button editor tinymce
  7293. // @param int $fileid Id of file
  7294. function tinymce_ilm ($fileid) {
  7295. $tag_filter = $this->tag_ilm($fileid);
  7296. $output = "<script type='text/javascript'>
  7297. //<![CDATA[
  7298. var tag_filter = '$tag_filter';
  7299. if (window.opener.tinyMCE.execCommand('mceiAssignReturn', tag_filter)) {
  7300. // all right, insert it
  7301. } else {
  7302. alert('Error trying to insert iLM content (tinymce_ilm " + $fileid + ")');
  7303. }
  7304. // window.close();
  7305. //]]>
  7306. </script>\n";
  7307. print $output;
  7308. die();
  7309. }
  7310. //TODO sugestao para uso de apenas uma funcao para chamar os editores, cortando 17 linhas de codigo
  7311. /// Function to add content to the Editor Window
  7312. // Updated: Marcio Passos - marciopassosbel[at]gmail[dot]com :: 07 / Jul / 2016
  7313. // @param int $fileid Id of file
  7314. function editor_ilm ($fileid, $editor) {
  7315. $tag_filter = $this->tag_ilm($fileid); // Prepare tag like: <ia toolbar=disable width=800 height=600>ID</ia>
  7316. $output1 = "<script type='text/javascript'>
  7317. //<![CDATA[
  7318. var tag_filter = '$tag_filter';\n";
  7319. if ($editor == 'atto') {
  7320. $output2 = " if (window.opener.document.execCommand('insertHTML', false, tag_filter)) { } // all right, insert it";
  7321. } elseif ($editor == 'tinyMCE') {
  7322. $output2 = " if (window.opener.tinyMCE.execCommand('mceiAssignReturn', tag_filter)) { } // all right, insert it";
  7323. } else
  7324. $output2 = "";
  7325. $output3 = " else { console.log('Error trying to insert iLM content (atto_ilm " . $fileid . ")'); }
  7326. window.close();
  7327. //]]>
  7328. </script>\n";
  7329. //D var_dump($output);
  7330. print $output1 . $output2 . $output3;
  7331. die();
  7332. }
  7333. /// Function to add content to the Atto Editor Window
  7334. // Updated: Marcio Passos - marciopassosbel[at]gmail[dot]com :: 07 / Jul / 2016
  7335. // @param int $fileid Id of file
  7336. function atto_ilm ($fileid) {
  7337. global $CFG, $DB;
  7338. $tag_filter = $this->tag_ilm($fileid);
  7339. $output = "<script type='text/javascript'>
  7340. //<![CDATA[
  7341. var tag_filter = '$tag_filter';
  7342. if (window.opener.document.execCommand('insertHTML', false, tag_filter)) {
  7343. // all right, insert iLM tag to this editor
  7344. } else {
  7345. // error trying to insert
  7346. alert('Error trying to insert iLM content (atto_ilm " + $fileid + ")');
  7347. }
  7348. //window.close();
  7349. //]]>
  7350. </script>\n";
  7351. print $output;
  7352. die();
  7353. }
  7354. /// Function for get path and info of directories: dirid, dir_base, dir_parent, dir_home
  7355. // @param string $key Key for return information
  7356. // @return Ambigous <unknown, number, string, NULL> Return an information requested
  7357. function get_dir_ilm ($key) {
  7358. $fs = get_file_storage(); // Get reference to all files in Moodle data
  7359. $context = context_course::instance($this->id);
  7360. $dirid = optional_param('dirid', 0, PARAM_INT);
  7361. $dir_home = $fs->get_file($context->id, 'mod_iassign', 'activity', 0, $dir_base = '/', '.');
  7362. if ($dirid == 0) {
  7363. $dir = ($dir_home = $fs->create_directory($context->id, 'mod_iassign', 'activity', 0, $dir_base));
  7364. $dirid = $dir->get_id();
  7365. }
  7366. else {
  7367. $dir = $fs->get_file_by_id($dirid);
  7368. $dir_base = $dir->get_filepath();
  7369. }
  7370. $dir_parent = $dir->get_parent_directory();
  7371. $data = array('dirid' => $dirid, 'dir_base' => $dir_base, 'dir_parent' => ($dir_parent == NULL ? 0 : $dir_parent->get_id()), 'dir_home' => $dir_home->get_id());
  7372. return $data[$key];
  7373. }
  7374. /// Function for create an new dir
  7375. function new_dir_ilm () {
  7376. global $USER;
  7377. $ilmid = optional_param('ilmid', NULL, PARAM_INT);
  7378. $dirname = optional_param('dirname', NULL, PARAM_TEXT);
  7379. $dir_base = $this->get_dir_ilm('dir_base');
  7380. $context = context_course::instance($this->id);
  7381. $fs = get_file_storage(); // Get reference to all files in Moodle data
  7382. $fs->create_directory($context->id, 'mod_iassign', 'activity', 0, $dir_base . $dirname . "/", $USER->id);
  7383. $dir_base = $fs->get_file($context->id, 'mod_iassign', 'activity', 0, $dir_base . $dirname . "/", '.');
  7384. $dir_base->set_author($USER->firstname . ' ' . $USER->lastname);
  7385. redirect(new moodle_url($this->url . '&dirid=' . $this->get_dir_ilm('dirid') . '&ilmid=' . $ilmid));
  7386. }
  7387. /// Function for delete an dir
  7388. function delete_dir_ilm () { //
  7389. $fs = get_file_storage(); // Get reference to all files in Moodle data
  7390. $ilmid = optional_param('ilmid', NULL, PARAM_INT);
  7391. $context = context_course::instance($this->id);
  7392. $dir = $fs->get_file_by_id($this->get_dir_ilm('dirid'));
  7393. $dir_parent = $this->get_dir_ilm('dir_parent');
  7394. if ($dir) {
  7395. if ($dir->is_directory()) {
  7396. $files_delete = $fs->get_directory_files($context->id, 'mod_iassign', 'activity', 0, $dir->get_filepath(), true, true);
  7397. foreach ($files_delete as $value)
  7398. $value->delete();
  7399. $dir->delete();
  7400. }
  7401. }
  7402. redirect(new moodle_url($this->url . '&dirid=' . $dir_parent . '&ilmid=' . $ilmid));
  7403. die();
  7404. }
  7405. /// Function for rename an dir
  7406. function rename_dir_ilm () {
  7407. $fs = get_file_storage(); // Get reference to all files in Moodle data
  7408. $ilmid = optional_param('ilmid', NULL, PARAM_INT);
  7409. $context = context_course::instance($this->id);
  7410. $dir = $fs->get_file_by_id($this->get_dir_ilm('dirid'));
  7411. $dir_parent = $this->get_dir_ilm('dir_parent');
  7412. $dirname = optional_param('dirname', NULL, PARAM_TEXT);
  7413. $pathname = explode("/", substr($dir->get_filepath(), 0, strlen($dir->get_filepath()) - 1));
  7414. if ($dir->is_directory()) {
  7415. $files_rename_path = $fs->get_directory_files($context->id, 'mod_iassign', 'activity', 0, $dir->get_filepath(), true, true);
  7416. foreach ($files_rename_path as $value)
  7417. $value->rename(str_replace($pathname[count($pathname) - 1], $dirname, $value->get_filepath()), $value->get_filename());
  7418. $dir->rename(str_replace($pathname[count($pathname) - 1], $dirname, $dir->get_filepath()), $dir->get_filename());
  7419. }
  7420. redirect(new moodle_url($this->url . '&dirid=' . $dir_parent . '&ilmid=' . $ilmid));
  7421. die();
  7422. }
  7423. /// Function for move an dir and your content for other dir
  7424. function selected_move_ilm () {
  7425. global $PAGE, $OUTPUT, $CFG;
  7426. $fs = get_file_storage(); // Get reference to all files in Moodle data
  7427. $context = context_course::instance($this->id);
  7428. $ilmid = optional_param('ilmid', NULL, PARAM_INT);
  7429. $dirid = $this->get_dir_ilm('dirid');
  7430. $dir_base = $this->get_dir_ilm('dir_base');
  7431. $files_id = explode(",", optional_param('files_id', '', PARAM_TEXT));
  7432. $code_javascript_ilm = "
  7433. <script type='text/javascript'>
  7434. //<![CDATA[
  7435. function getRadiobutton () {
  7436. var radioButtons = document.getElementsByTagName('input');
  7437. var param = '';
  7438. for (var counter=0; counter < radioButtons.length; counter++) {
  7439. if (radioButtons[counter].type.toUpperCase()=='RADIO' && radioButtons[counter].checked == true && radioButtons[counter].name == 'selected_dir')
  7440. param = radioButtons[counter].value;
  7441. }
  7442. return param;
  7443. }
  7444. function move_selected_ilm () {
  7445. var msgAnswer;
  7446. if (getRadiobutton() != '') {
  7447. msgAnswer = confirm('" . get_string('question_move_dir', 'iassign') . "');
  7448. if (msgAnswer)
  7449. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=move&ilmid=" . $ilmid . "&dirid=" . $dirid . "&files_id=" .
  7450. optional_param('files_id', '', PARAM_TEXT) . "&dir_move='+getRadiobutton();
  7451. }
  7452. else
  7453. alert('" . get_string('error_dir_not_selected_to_move', 'iassign') . "');
  7454. }
  7455. function cancel_selected_ilm () {
  7456. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&ilmid=" . $ilmid . "&dirid=" . $dirid . "';
  7457. }
  7458. //]]>
  7459. </script>\n";
  7460. $title = get_string('move_files', 'iassign');
  7461. $PAGE->set_title($title);
  7462. $PAGE->set_pagelayout('base');
  7463. print $OUTPUT->header();
  7464. print $OUTPUT->heading($title);
  7465. $dir_paths = array();
  7466. print $OUTPUT->box_start();
  7467. print "<center>";
  7468. foreach ($files_id as $file_id) {
  7469. $file = $fs->get_file_by_id($file_id);
  7470. if ($file) {
  7471. if (!$file->is_directory())
  7472. print "<p>" . iassign_icons::insert('file') . " " . $file->get_filepath() . $file->get_filename() . "</p>";
  7473. else {
  7474. print "<p>" . iassign_icons::insert('dir') . " " . $file->get_filepath() . "</p>";
  7475. array_push($dir_paths, $file->get_filepath());
  7476. }
  7477. }
  7478. }
  7479. print $OUTPUT->heading(get_string('select_move_ilm', 'iassign'), 3, 'move', 'move_files');
  7480. if ($dir_base != '/') {
  7481. $check_select = "<input name='selected_dir' type='radio' value='" . $this->get_dir_ilm('dir_home') . "'/>";
  7482. print $check_select . " " . iassign_icons::insert('dir') . " /<br>";
  7483. }
  7484. $files_tree = $fs->get_directory_files($context->id, 'mod_iassign', 'activity', 0, '/', true, true, 'filepath');
  7485. foreach ($files_tree as $file) {
  7486. if ($file->is_directory() && $file->get_filepath() != $dir_base) {
  7487. $is_parent = false;
  7488. foreach ($dir_paths as $dir) {
  7489. $path = explode("/", $dir);
  7490. array_pop($path);
  7491. $path[count($path) - 1] = '';
  7492. $path = implode("/", $path);
  7493. $is_parent |= (strpos($file->get_filepath(), $dir) === false ? false : true);
  7494. $is_parent |= ($file->get_filepath() != $path ? false : true);
  7495. }
  7496. if ($is_parent == false) {
  7497. $check_select = "<input name='selected_dir' type='radio' value='" . $file->get_id() . "'/>";
  7498. print "<p>" . $check_select . " " . iassign_icons::insert('dir') . " " . $file->get_filepath() . "</p>";
  7499. }
  7500. }
  7501. }
  7502. print "<p><input type='button' value='" . get_string('ok') . "' onclick='move_selected_ilm();'/> &nbsp; &nbsp;";
  7503. print "<input type='button' value='" . get_string('cancel') . "' onclick='cancel_selected_ilm();'/></p>";
  7504. print "</center>";
  7505. print $OUTPUT->box_end();
  7506. print $OUTPUT->footer();
  7507. print $code_javascript_ilm;
  7508. die;
  7509. }
  7510. /// Function for move files for an dir
  7511. function move_files_ilm () {
  7512. $fs = get_file_storage(); // Get reference to all files in Moodle data
  7513. $context = context_course::instance($this->id);
  7514. $ilmid = optional_param('ilmid', NULL, PARAM_INT);
  7515. $dirid = $this->get_dir_ilm('dirid');
  7516. $dir_move = $fs->get_file_by_id(optional_param('dir_move', 0, PARAM_INT));
  7517. $files_id = explode(",", optional_param('files_id', '', PARAM_TEXT));
  7518. foreach ($files_id as $file_id) {
  7519. $file = $fs->get_file_by_id($file_id);
  7520. if ($file) {
  7521. if ($file->is_directory()) {
  7522. $pathname = explode("/", $file->get_filepath());
  7523. $files_move_path = $fs->get_directory_files($context->id, 'mod_iassign', 'activity', 0, $file->get_filepath(), true, true);
  7524. foreach ($files_move_path as $value) {
  7525. $path_move = $dir_move->get_filepath() . $pathname[count($pathname) - 2] . '/' . str_replace($file->get_filepath(), '', $value->get_filepath());
  7526. $value->rename($path_move, $value->get_filename());
  7527. //echo($value->get_filepath().$value->get_filename()." - $path_move".$value->get_filename()."<br>");
  7528. }
  7529. $path_move = $dir_move->get_filepath() . $pathname[count($pathname) - 2] . '/';
  7530. //echo($file->get_filepath().$file->get_filename()." - $path_move".$file->get_filename()."<br>");
  7531. $file->rename($path_move, $file->get_filename());
  7532. } else {
  7533. //echo($file->get_filepath().$file->get_filename()." -> ".$dir_move->get_filepath().$file->get_filename()."<br>");
  7534. $file->rename($dir_move->get_filepath(), $file->get_filename());
  7535. }
  7536. }
  7537. }
  7538. //die;
  7539. redirect(new moodle_url($this->url . '&ilmid=' . $ilmid . '&dirid=' . $dirid));
  7540. die();
  7541. } // function move_files_ilm()
  7542. /// Function for recover files in use on all activities of a course
  7543. function recover_files_ilm () {
  7544. global $DB, $USER;
  7545. $fs = get_file_storage(); // Get reference to all files in Moodle data
  7546. $courseid = optional_param('id', NULL, PARAM_INT);
  7547. $dirid = $this->get_dir_ilm('dirid');
  7548. $ilmid = optional_param('ilmid', NULL, PARAM_INT);
  7549. $contextfile = context_course::instance($this->id);
  7550. $iassigns = $DB->get_records("iassign", array("course" => $courseid));
  7551. foreach ($iassigns as $iassign) {
  7552. $iassign_statement_activity_list = $DB->get_records("iassign_statement", array("iassignid" => $iassign->id));
  7553. foreach ($iassign_statement_activity_list as $iassign_statement_activity_item) {
  7554. $cm = get_coursemodule_from_instance("iassign", $iassign->id, $courseid);
  7555. $context = context_module::instance($cm->id);
  7556. // Before associtaion 'files.id' with 'iassign_statement.iassign_ilmid': $files_fs = $fs->get_area_files($context->id, 'mod_iassign', 'exercise', $iassign_statement_activity_item->filesid);
  7557. $files_fs = $fs->get_area_files($context->id, 'mod_iassign', 'exercise', $iassign_statement_activity_item->id);
  7558. if ($files_fs) {
  7559. foreach ($files_fs as $value) {
  7560. $extension = explode(".", $value->get_filename());
  7561. if (!$value->is_directory()) {
  7562. $fileinfo = array('contextid' => $contextfile->id,
  7563. 'component' => 'mod_iassign',
  7564. 'filearea' => 'activity',
  7565. 'itemid' => 0,
  7566. 'filepath' => $this->get_dir_ilm('dir_base'),
  7567. 'userid' => $USER->id,
  7568. 'author' => $USER->firstname . ' ' . $USER->lastname,
  7569. 'license' => 'allrightsreserved',
  7570. 'timecreated' => time(),
  7571. 'filename' => $iassign_statement_activity_item->name . "." . $extension[1]); // any filename
  7572. $newfile_fs = $fs->create_file_from_string($fileinfo, $value->get_content());
  7573. }
  7574. }
  7575. } // if ($files_fs)
  7576. } // foreach ($iassign_statement_activity_list as $iassign_statement_activity_item)
  7577. } // foreach ($iassigns as $iassign)
  7578. redirect(new moodle_url($this->url . '&dirid=' . $dirid . '&ilmid=' . $ilmid));
  7579. die();
  7580. } // function recover_files_ilm()
  7581. /// Auxiliary function the get all files in a given context
  7582. // @calledby view_files_ilm($iassign_ilm_class, $extension, $start = 0)
  7583. // @return [array_contextid_files_thisauthor[], array_contextid_files_otherauthors[], array_contextid_files_otherauthors_userid[]]
  7584. //TODO Falta separar os arquivos listados, apresentar primeiro aqueles do autor e depois os demais.
  7585. //TODO Nao devemos listar aqueles que estao sem permissao (license == 'allrightsreserved')
  7586. function get_files_in_context (&$filesfrommine, &$filesfromothers, $contextid, $extension, $userid) {
  7587. // s_iassign_statement: id name iassignid type_iassign proposition author_name author_modified_name iassign_ilmid
  7588. // (NAO s_iassign_statement.file = s_files.id)
  7589. $fs = get_file_storage(); // Get reference to all files in Moodle data
  7590. $dir_base = $this->get_dir_ilm('dir_base');
  7591. $files_course = $fs->get_directory_files($contextid, 'mod_iassign', 'activity', 0, $dir_base, false, true, 'filename');
  7592. $countf = 0;
  7593. $array_files_id = array(); // all files from this context
  7594. foreach ($files_course as $item_fc) { // license = not "allrightsreserved" AND is not from this author => present it
  7595. //$filename = $item_fc->get_filename();
  7596. //$filepath = $item_fc->get_filepath();
  7597. //$fileid = $item_fc->get_id(); // get file ID (in *_iassign_statement table)
  7598. $fileuserid = $item_fc->get_userid(); // get user.id of the author
  7599. // $array_files_aux[] = $item_fc;
  7600. $itemid = $item_fc->get_id();
  7601. if (!in_array($itemid, $array_files_id)) {
  7602. //TODO Avoid to insert twice - necessary since it has been inserting 2 copies in iassign_statement
  7603. if ($fileuserid == $userid)
  7604. $filesfrommine[] = $item_fc;
  7605. else
  7606. $filesfromothers[] = $item_fc;
  7607. $array_files_id[] = $itemid;
  7608. $countf++;
  7609. }
  7610. }
  7611. return $countf;
  7612. } // function get_files_in_context(&$filesfrommine, &$filesfromothers, $contextid, $extension, $userid)
  7613. // Function to get all iLM files from the same "file type": (usually) files under the same directory
  7614. // @calledby view_files_ilm($iassign_ilm_class, $extension, $start = 0)
  7615. function get_all_ilm_files_by_fileid ($fileid) {
  7616. global $DB, $USER;
  7617. // $iassign_statement_activity_list = $DB->get_records("iassign_statement", array("file" => $fileid));
  7618. //TODO Criar coluna 'authorid'
  7619. $str_query = "SELECT * FROM {iassign_statement} WHERE file = " . $fileid . " ORDER BY author_name, timecreated DESC";
  7620. $array_iassign_ilm = $DB->get_records_sql($str_query);
  7621. return $array_iassign_ilm;
  7622. }
  7623. //NN
  7624. //NN
  7625. // Build string with JavaScript code with function to edit/remove/double iLM files
  7626. // @calledby view_files_ilm($iassign_ilm_class, $extension, $start = 0)
  7627. function get_string_JavaScript_functions ($dirid, $ilmid, $files_array, $dirs_array) {
  7628. global $CFG;
  7629. $error_files_exists = get_string('error_file_exists', 'iassign');
  7630. $error_dir_exists = get_string('error_dir_exists', 'iassign');
  7631. $code_javascript_ilm = "
  7632. <script type='text/javascript'>
  7633. //<![CDATA[
  7634. function preview_ilm (fileid, ilmid) { //JavaScript function
  7635. var param = '" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=preview&fileid='+fileid+'&ilmid='+ilmid;
  7636. var preview_ilm=window.open(param,'','menubar=0,location=0,scrollbars,status,resizable,width=900 height=700');
  7637. }
  7638. function update_ilm (ilmid, fileid) { //JavaScript function
  7639. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=update&ilmid='+ilmid+'&dirid=" . $dirid . "&fileid='+fileid;
  7640. }
  7641. function delete_ilm (ilmid, fileid) { //JavaScript function
  7642. var msgAnswer;
  7643. msgAnswer = confirm('" . get_string('delete_file', 'iassign') . "');
  7644. if (msgAnswer) {
  7645. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=delete&ilmid='+ilmid+'&dirid=" . $dirid . "&fileid='+fileid;
  7646. }
  7647. }
  7648. function delete_selected_ilm () { //JavaScript function
  7649. var msgAnswer;
  7650. var param = getCheckbox();
  7651. if (param.join() != '') {
  7652. msgAnswer = confirm('" . get_string('delete_files', 'iassign') . "');
  7653. if (msgAnswer)
  7654. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=selected_delete&dirid=" . $dirid . "&files_id='+param.join();
  7655. }
  7656. else
  7657. alert('" . get_string('er_file_not_selected_to_delete', 'iassign') . "');
  7658. }
  7659. function add_ilm_iassign (ilmid, filename, fileid) { //JavaScript function
  7660. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=addilm&ilmid='+ilmid+'&fileid='+fileid+'&filename='+filename;
  7661. }
  7662. function duplicate_ilm (ilmid, filename, fileid) { //JavaScript function
  7663. var filenamecopy;
  7664. var i;
  7665. var files = new Array(" . $files_array . ");
  7666. do {
  7667. filenamecopy = prompt ('" . get_string('duplicate_file', 'iassign') . "',filename);
  7668. } while (filenamecopy == '');
  7669. if (filenamecopy == null)
  7670. return false;\n
  7671. else {
  7672. for (i=0;i<files.length;i++) {
  7673. if (files[i]==filenamecopy) {
  7674. alert('" . $error_files_exists . "');
  7675. return false;\n
  7676. }
  7677. }
  7678. // @see: PHP 'function duplicate_file_ilm()' above (line 8083/9608)
  7679. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?" .
  7680. "from=" . $this->from . "&id=" . $this->id . "&action=duplicate&ilmid=' + ilmid + '&dirid=" . $dirid . "&fileid=' + fileid + '&filename=' + filenamecopy;
  7681. } // else
  7682. }
  7683. function rename_ilm (ilmid, filename, fileid) { //JavaScript function
  7684. var filenamecopy;
  7685. var i;
  7686. var files = new Array($files_array);
  7687. do {
  7688. filenamecopy = prompt('" . get_string('rename_file', 'iassign') . "',filename);
  7689. } while (filenamecopy == '');
  7690. if (filenamecopy == null)
  7691. return false;
  7692. else {
  7693. for (i=0;i<files.length;i++) {
  7694. if (files[i]==filenamecopy) {
  7695. alert('" . $error_files_exists . "');
  7696. return false;
  7697. }
  7698. }
  7699. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=rename&ilmid='+ilmid+'&dirid=" . $dirid . "&fileid='+fileid+'&filename='+filenamecopy;
  7700. }
  7701. }
  7702. function export_files_ilm () { //JavaScript function
  7703. var param = getCheckbox();
  7704. if (param.join() != '')
  7705. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=export&dirid=" . $dirid . "&files_id='+param.join();
  7706. else
  7707. alert('" . get_string('er_file_not_selected_to_export', 'iassign') . "');
  7708. }
  7709. function select_all_ilm () { //JavaScript function
  7710. var checkBoxes = document.getElementsByTagName('input');
  7711. var selectAll = document.getElementById('select_all');
  7712. for (var counter=0; counter < checkBoxes.length; counter++) {
  7713. if (checkBoxes[counter].type.toUpperCase()=='CHECKBOX' && checkBoxes[counter].name == 'selected_file')
  7714. checkBoxes[counter].checked = selectAll.checked;
  7715. }
  7716. }
  7717. function getCheckbox () { //JavaScript function
  7718. var checkBoxes = document.getElementsByTagName('input');
  7719. var param = new Array();
  7720. for (var counter=0; counter < checkBoxes.length; counter++) {
  7721. if (checkBoxes[counter].type.toUpperCase()=='CHECKBOX' && checkBoxes[counter].checked == true && checkBoxes[counter].name == 'selected_file')
  7722. param.push(checkBoxes[counter].value);
  7723. }
  7724. return param;
  7725. }
  7726. function new_dir_ilm () { //JavaScript function
  7727. var dirname = '';
  7728. var i;
  7729. var dirs = new Array(" . $dirs_array . ");
  7730. do {
  7731. var dirname = prompt ('" . get_string('question_new_dir', 'iassign') . "', '');
  7732. } while (dirname == '');
  7733. if (dirname == null)
  7734. return false;\n
  7735. else {
  7736. for (i=0;i<dirs.length;i++) {
  7737. if (dirs[i]==dirname) {
  7738. alert('" . $error_dir_exists . "');
  7739. return false;\n
  7740. }
  7741. }
  7742. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=new_dir&ilmid=" . $ilmid . "&dirid=" . $dirid . "&dirname='+dirname;
  7743. }
  7744. }
  7745. function delete_dir_ilm (ilmid, dirid) { //JavaScript function
  7746. var msgAnswer;
  7747. msgAnswer = confirm('" . get_string('question_delete_dir', 'iassign') . "');
  7748. if (msgAnswer) {
  7749. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=delete_dir&ilmid='+ilmid+'&dirid='+dirid;
  7750. }
  7751. }
  7752. function rename_dir_ilm (ilmid, dirname, dirid) { //JavaScript function
  7753. var dirnamecopy;
  7754. var i;
  7755. var dirs = new Array($dirs_array);
  7756. do {
  7757. dirnamecopy = prompt ('" . get_string('question_rename_dir', 'iassign') . "',dirname);
  7758. } while (dirnamecopy == '');
  7759. if (dirnamecopy == null)
  7760. return false;\n
  7761. else {
  7762. for (i=0;i<dirs.length;i++) {
  7763. if (dirs[i]==dirnamecopy) {
  7764. alert('" . $error_dir_exists . "');
  7765. return false;\n
  7766. }
  7767. }
  7768. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=rename_dir&ilmid='+ilmid+'&dirid='+dirid+'&dirname='+dirnamecopy;
  7769. }
  7770. }
  7771. function move_selected_ilm (ilmid) { //JavaScript function
  7772. var param = getCheckbox();
  7773. if (param.join() != '')
  7774. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=selected_move&ilmid='+ilmid+'&dirid=" . $dirid . "&files_id='+param.join();
  7775. else
  7776. alert('" . get_string('er_file_not_selected_to_move', 'iassign') . "');
  7777. }
  7778. function recover_files_ilm () { //JavaScript function
  7779. var msgAnswer;
  7780. msgAnswer = confirm('" . get_string('question_recover_files', 'iassign') . "');
  7781. if (msgAnswer) {
  7782. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=recover&ilmid=" . $ilmid . "&dirid=" . $dirid . "';
  7783. }
  7784. }
  7785. window.onload = function() { //JavaScript function
  7786. var xPosition = 0;
  7787. var yPosition = 0;
  7788. element = document.getElementById('new_file');
  7789. if (element && element!=null) {
  7790. while (element) {
  7791. xPosition += (element.offsetLeft - element.scrollLeft + element.clientLeft);
  7792. yPosition += (element.offsetTop - element.scrollTop + element.clientTop);
  7793. element = element.offsetParent;
  7794. }
  7795. document.getElementById('new_file').style.top = '' + (yPosition - 400) + 'px';
  7796. document.getElementById('new_file').style.right = '' + 0 + 'px';
  7797. }
  7798. location.hash = '#new_file';
  7799. };
  7800. //]]>
  7801. </script>\n"; // end of $code_javascript_ilm
  7802. return $code_javascript_ilm;
  7803. } // function get_string_JavaScript_functions($dirid, $ilmid, $files_array, $dirs_array)
  7804. /// List iassign files from course directory
  7805. // User (teacher or above it) can access all the iLM files under the current course
  7806. // @calledby ilm_manager.php : $ilm_manager_instance->view_files_ilm($iassign_ilm->extension);
  7807. function view_files_ilm ($iassign_ilm_class, $extension) {
  7808. global $CFG, $DB, $USER, $OUTPUT; // class iassign : var $cm / class ilm_manager
  7809. $fs = get_file_storage();
  7810. $context = context_course::instance($this->id);
  7811. //D echo "<br/><br/><br/><br/><br/>locallib.php!view_files_ilm(.): id=" . $this->id . "<br/>";
  7812. $ilmid = optional_param('ilmid', NULL, PARAM_INT);
  7813. $dirid = $this->get_dir_ilm('dirid');
  7814. $dir_base = $this->get_dir_ilm('dir_base');
  7815. // $context->id must be the same as context_module::instance($USER->cm)->id
  7816. // {files}.filearea='activity' => it is the file ('exercise'=>the correspondent entry associated to {iassign_statement}
  7817. $files_all = $fs->get_directory_files($context->id, 'mod_iassign', 'activity', 0, $dir_base, false, true, 'filename');
  7818. $files_filename = array(); // auxiliary to avoid duplicit ({files}.filearea = 'activity' and the same in {files}.filearea = 'exercise')
  7819. $files_contenthash = array(); // auxiliary to avoid duplicit ({files}.filearea = 'activity' and the same in {files}.filearea = 'exercise')
  7820. $is_activity = array(); // $is_activity => use {files}.filearea = 'activity', otherwise {files}.filearea = 'exercise'
  7821. foreach ($files_all as $one_files) {
  7822. $is_activity[] = true;
  7823. $files_filename[] = $one_files->get_filename();
  7824. $files_contenthash[] = $one_files->get_contenthash();
  7825. //D if ($one_files->get_filename()=="exd--desafio.ivph") echo " * exd--desafio.ivph : index=" . (count($files_filename)-1) . ": |" . $one_files->get_contenthash() . "|<br/>";
  7826. }
  7827. // https://moodledev.io/docs/4.5/apis/subsystems/form/usage/files
  7828. // Get {files} from another contexts in this course
  7829. require_once 'files_functions.php';
  7830. $files_exercise = get_all_files_course($DB, $this->id, $fs); // $this->id = {course}.id
  7831. // $files_all = array_merge($files_exercise, $files_all); // append new elements
  7832. if (count($files_exercise)>0) {
  7833. foreach ($files_exercise as $item_files_exercise) {
  7834. $not_existent = true;
  7835. $index = array_search($item_files_exercise->get_filename(), $files_filename);
  7836. if ($index!="") { // not empyt means existent
  7837. if ($files_contenthash[$index]==$item_files_exercise->get_contenthash())
  7838. $not_existent = false; // the file is already existent in {files}.filearea='activity'
  7839. //D else { echo " - nao : sem index : " . $item_files_exercise->get_filename() . " - |" . $item_files_exercise->get_contenthash() . "|<br/>"; }
  7840. }
  7841. //D else { echo " - nao : index=" . $index . " : " . $item_files_exercise->get_filename() . " - |" . $item_files_exercise->get_contenthash() . "|<br/>"; }
  7842. if ($not_existent) { // empyt means inexistent
  7843. $files_all[] = $item_files_exercise; // append the {files}.filearea='exercise' to $files_all[]
  7844. $is_activity[] = false; // register it has {files}.filearea= 'exercise'
  7845. $files_filename[] = $item_files_exercise->get_filename();
  7846. $files_contenthash[] = $item_files_exercise->get_contenthash();
  7847. }
  7848. //D else {
  7849. //D echo " - SIM : index=" . $index . " : " . $item_files_exercise->get_filename() . " - |" . $item_files_exercise->get_contenthash() . "|=|" . $files_contenthash[$index] . "|=|" . $item_files_exercise->get_contenthash() . "|<br/>";
  7850. //D }
  7851. }
  7852. } // if (count($files_exercise)>0)
  7853. $files_array = '';
  7854. foreach ($files_all as $value) {
  7855. if (!$value->is_directory())
  7856. $files_array .= "'" . $value->get_filename() . "',";
  7857. }
  7858. $files_array .= "''";
  7859. $error_files_exists = get_string('error_file_exists', 'iassign');
  7860. $dirs_array = '';
  7861. foreach ($files_all as $value) {
  7862. if ($value->is_directory()) {
  7863. $pathname = explode("/", substr($value->get_filepath(), 0, strlen($value->get_filepath()) - 1));
  7864. $dirs_array .= "'" . $pathname[count($pathname) - 1] . "',";
  7865. }
  7866. }
  7867. $dirs_array .= "''";
  7868. $error_dir_exists = get_string('error_dir_exists', 'iassign');
  7869. //TODO: Rever o 'preview', pois so' apresenta uma vez!
  7870. $code_javascript_ilm = "
  7871. <script type='text/javascript'>
  7872. //<![CDATA[
  7873. function preview_ilm (fileid, ilmid) {
  7874. var param = '" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=preview&fileid='+fileid+'&ilmid='+ilmid;
  7875. var preview_ilm=window.open(param,'','menubar=0,location=0,scrollbars,status,resizable,width=900 height=700');
  7876. }
  7877. function update_ilm (ilmid, fileid) {
  7878. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=update&ilmid='+ilmid+'&dirid=" . $dirid . "&fileid='+fileid;
  7879. }
  7880. function delete_ilm (ilmid, fileid) {
  7881. var msgAnswer;
  7882. msgAnswer = confirm('" . get_string('delete_file', 'iassign') . "');
  7883. if (msgAnswer) {
  7884. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=delete&ilmid='+ilmid+'&dirid=" . $dirid . "&fileid='+fileid;
  7885. }
  7886. }
  7887. function delete_selected_ilm () {
  7888. var msgAnswer;
  7889. var param = getCheckbox();
  7890. if (param.join() != '') {
  7891. msgAnswer = confirm('" . get_string('delete_files', 'iassign') . "');
  7892. if (msgAnswer)
  7893. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=selected_delete&dirid=" . $dirid . "&files_id='+param.join();
  7894. }
  7895. else
  7896. alert('" . get_string('er_file_not_selected_to_delete', 'iassign') . "');
  7897. }
  7898. function add_ilm_iassign (ilmid, filename, fileid) {
  7899. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=addilm&ilmid='+ilmid+'&fileid='+fileid+'&filename='+filename;
  7900. }
  7901. function duplicate_ilm (ilmid, filename, fileid) {
  7902. var filenamecopy;
  7903. var i;
  7904. var files = new Array(" . $files_array . ");
  7905. do {
  7906. filenamecopy = prompt('" . get_string('duplicate_file', 'iassign') . "',filename);
  7907. } while (filenamecopy == '');
  7908. if (filenamecopy == null)
  7909. return false;
  7910. else {
  7911. for (i=0;i<files.length;i++) {
  7912. if (files[i]==filenamecopy) {
  7913. alert('" . $error_files_exists . "');
  7914. return false;
  7915. }
  7916. }
  7917. // @see: PHP 'function duplicate_file_ilm()' above (line 8083/9608)
  7918. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?" .
  7919. "from=" . $this->from . "&id=" . $this->id . "&action=duplicate&ilmid=' + ilmid + '&dirid=" . $dirid . "&fileid=' + fileid + '&filename=' + filenamecopy;
  7920. } // else
  7921. }
  7922. function rename_ilm (ilmid, filename, fileid) {
  7923. var filenamecopy;
  7924. var i;
  7925. var files = new Array($files_array);
  7926. do {
  7927. filenamecopy = prompt('" . get_string('rename_file', 'iassign') . "',filename);
  7928. } while (filenamecopy == '');
  7929. if (filenamecopy == null)
  7930. return false;
  7931. else {
  7932. for (i=0;i<files.length;i++) {
  7933. if (files[i]==filenamecopy) {
  7934. alert('" . $error_files_exists . "');
  7935. return false;
  7936. }
  7937. }
  7938. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=rename&ilmid='+ilmid+'&dirid=" . $dirid . "&fileid='+fileid+'&filename='+filenamecopy;
  7939. }
  7940. }
  7941. function export_files_ilm () {
  7942. var param = getCheckbox();
  7943. if (param.join() != '')
  7944. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=export&dirid=" . $dirid . "&files_id='+param.join();
  7945. else
  7946. alert('" . get_string('er_file_not_selected_to_export', 'iassign') . "');
  7947. }
  7948. function select_all_ilm () {
  7949. var checkBoxes = document.getElementsByTagName('input');
  7950. var selectAll = document.getElementById('select_all');
  7951. for (var counter=0; counter < checkBoxes.length; counter++) {
  7952. if (checkBoxes[counter].type.toUpperCase()=='CHECKBOX' && checkBoxes[counter].name == 'selected_file')
  7953. checkBoxes[counter].checked = selectAll.checked;
  7954. }
  7955. }
  7956. function getCheckbox () {
  7957. var checkBoxes = document.getElementsByTagName('input');
  7958. var param = new Array();
  7959. for (var counter=0; counter < checkBoxes.length; counter++) {
  7960. if (checkBoxes[counter].type.toUpperCase()=='CHECKBOX' && checkBoxes[counter].checked == true && checkBoxes[counter].name == 'selected_file')
  7961. param.push(checkBoxes[counter].value);
  7962. }
  7963. return param;
  7964. }
  7965. function new_dir_ilm () {
  7966. var dirname = '';
  7967. var i;
  7968. var dirs = new Array(" . $dirs_array . ");
  7969. do {
  7970. var dirname = prompt('" . get_string('question_new_dir', 'iassign') . "', '');
  7971. } while (dirname == '');
  7972. if (dirname == null)
  7973. return false;
  7974. else {
  7975. for (i=0;i<dirs.length;i++) {
  7976. if (dirs[i]==dirname) {
  7977. alert('" . $error_dir_exists . "');
  7978. return false;
  7979. }
  7980. }
  7981. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=new_dir&ilmid=" . $ilmid . "&dirid=" . $dirid . "&dirname='+dirname;
  7982. }
  7983. }
  7984. function delete_dir_ilm (ilmid, dirid) {
  7985. var msgAnswer;
  7986. msgAnswer = confirm('" . get_string('question_delete_dir', 'iassign') . "');
  7987. if (msgAnswer) {
  7988. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=delete_dir&ilmid='+ilmid+'&dirid='+dirid;
  7989. }
  7990. }
  7991. function rename_dir_ilm (ilmid, dirname, dirid) {
  7992. var dirnamecopy;
  7993. var i;
  7994. var dirs = new Array(" . $dirs_array . ");
  7995. do {
  7996. dirnamecopy = prompt('" . get_string('question_rename_dir', 'iassign') . "',dirname);
  7997. } while (dirnamecopy == '');
  7998. if (dirnamecopy == null)
  7999. return false;
  8000. else {
  8001. for (i=0;i<dirs.length;i++) {
  8002. if (dirs[i]==dirnamecopy) {
  8003. alert('" . $error_dir_exists . "');
  8004. return false;
  8005. }
  8006. }
  8007. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=rename_dir&ilmid='+ilmid+'&dirid='+dirid+'&dirname='+dirnamecopy;
  8008. }
  8009. }
  8010. function move_selected_ilm (ilmid) {
  8011. var param = getCheckbox();
  8012. if (param.join() != '')
  8013. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=selected_move&ilmid='+ilmid+'&dirid=" . $dirid . "&files_id='+param.join();
  8014. else
  8015. alert('" . get_string('er_file_not_selected_to_move', 'iassign') . "');
  8016. }
  8017. function recover_files_ilm () {
  8018. var msgAnswer;
  8019. msgAnswer = confirm('" . get_string('question_recover_files', 'iassign') . "');
  8020. if (msgAnswer) {
  8021. window.location='" . $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&action=recover&ilmid=" . $ilmid . "&dirid=" . $dirid . "';
  8022. }
  8023. }
  8024. window.onload = function () {
  8025. var xPosition = 0;
  8026. var yPosition = 0;
  8027. element = document.getElementById('new_file');
  8028. if (element && element!=null) {
  8029. while (element) {
  8030. xPosition += (element.offsetLeft - element.scrollLeft + element.clientLeft);
  8031. yPosition += (element.offsetTop - element.scrollTop + element.clientTop);
  8032. element = element.offsetParent;
  8033. }
  8034. document.getElementById('new_file').style.top = '' + (yPosition - 400) + 'px';
  8035. document.getElementById('new_file').style.right = '' + 0 + 'px';
  8036. }
  8037. location.hash = '#new_file';
  8038. };
  8039. //]]>
  8040. </script>\n";
  8041. $output = "";
  8042. $select_all = "";
  8043. $count_files = 0;
  8044. $extensions_allow = array();
  8045. $iassign_ilm = $DB->get_records('iassign_ilm', array("enable" => 1)); // extensions for all iLM...
  8046. foreach ($iassign_ilm as $item_iassign_ilm)
  8047. $extensions_allow = array_merge($extensions_allow, explode(",", $item_iassign_ilm->extension));
  8048. $ii = 0; // get $is_activity[$ii] associated to $one_files
  8049. foreach ($files_all as $one_files) {
  8050. $filename = $one_files->get_filename();
  8051. $filepath = $one_files->get_filepath();
  8052. $pathname = explode("/", substr($filepath, 0, strlen($filepath) - 1));
  8053. $pathname = $pathname[count($pathname) - 1];
  8054. $fileid = $one_files->get_id();
  8055. $tmp = explode(".", $filename);
  8056. if (count($tmp)>1) // avoid error if the file has no extension
  8057. $filetype = $tmp[1];
  8058. else // probably error...
  8059. $filetype = '';
  8060. $author = $one_files->get_author();
  8061. $timemodified = date("d/m/Y H:i:s", $one_files->get_timemodified());
  8062. $timecreated = date("d/m/Y H:i:s", $one_files->get_timecreated());
  8063. $extensions = explode(",", $extension);
  8064. if (in_array(strtolower($filetype), $extensions) || $one_files->is_directory() || $this->from == 'block' || $this->from == 'tinymce' || $this->from == 'atto') {
  8065. $count_files++;
  8066. // buscar fileid nas tabelas do iassign
  8067. $list_filein_use = "";
  8068. $iassign_statement_activity_list = $DB->get_records("iassign_statement", array("file" => $fileid));
  8069. if ($iassign_statement_activity_list) {
  8070. foreach ($iassign_statement_activity_list as $iassign_statement_activity_item) {
  8071. $list_filein_use .= $iassign_statement_activity_item->name . "</br>\n";
  8072. }
  8073. }
  8074. $iassign_ilm = $DB->get_record("iassign_ilm", array('extension' => $filetype, 'id' => $ilmid, 'enable' => '1'));
  8075. if (!$iassign_ilm) {
  8076. $iassign_ilm = new stdClass();
  8077. $iassign_ilm->id = $ilmid;
  8078. }
  8079. // Do not use $var1 inside "..." - problem with some "unusual" operational system
  8080. if ($is_activity[$ii]) // use {files}.filearea='activity'
  8081. $url = $CFG->wwwroot . "/pluginfile.php/" . $one_files->get_contextid() . "/mod_iassign/activity";
  8082. else // use {files}.filearea='exercise'
  8083. $url = $CFG->wwwroot . "/pluginfile.php/" . $one_files->get_contextid() . "/mod_iassign/exercise";
  8084. $ii++; // get the next element of $is_activity[]/$files_all[]
  8085. $fileurl = $url . '/' . $one_files->get_itemid() . $filepath . $filename;
  8086. $dirurl = new moodle_url($this->url) . '&ilmid=' . $iassign_ilm->id . '&dirid=' . $fileid;
  8087. $straux = $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=" . $this->from . "&id=" . $this->id . "&fileid=" . $fileid . "&";
  8088. $link_add_ilm_iassign = " <a href='" . $straux . "action=addilm&filename=" . $filename . "'>" . iassign_icons::insert('add_ilm_iassign') . "</a>\n";
  8089. $link_add_ilm_tinymce = " <a href='" . $straux . "action=tinymceilm'>" . iassign_icons::insert('add_ilm_iassign') . "</a>\n";
  8090. $link_add_ilm_atto = " <a href='" . $straux . "action=attoilm'>" . iassign_icons::insert('add_ilm_iassign') . "</a>\n";
  8091. $check_select = "";
  8092. $link_rename = "";
  8093. $link_delete = "";
  8094. $link_duplicate = " <a href='#' onclick=\"duplicate_ilm('" . $iassign_ilm->id . "', '". $filename . "'," . $fileid . ");\">" . iassign_icons::insert('duplicate_iassign') . "</a>\n";
  8095. $link_edit = " " . iassign_icons::insert('no_edit_iassign');
  8096. $link_filter = " <a href='#' onclick=\"preview_ilm(" . $fileid . "," . $ilmid . ");\">" . iassign_icons::insert('preview_iassign') . "</a>\n";
  8097. $link_duplicate = " <a href='#' onclick=\"duplicate_ilm('" . $iassign_ilm->id . "', '" . $filename . "'," . $fileid . ");\">" .
  8098. iassign_icons::insert('duplicate_iassign') . "</a>\n";
  8099. $link_edit = " " . iassign_icons::insert('no_edit_iassign');
  8100. $link_filter = " <a href='#' onclick=\"preview_ilm(" . $fileid . "," . $ilmid . ");\">" . iassign_icons::insert('preview_iassign') . "</a>\n";
  8101. if ($one_files->get_userid() == $USER->id) {
  8102. if ($iassign_statement_activity_list) {
  8103. $check_select = "";
  8104. $link_edit = iassign_icons::insert('edit_iassign_disable');
  8105. $link_delete = " " . iassign_icons::insert('delete_iassign_disable');
  8106. $link_rename = "";
  8107. } else {
  8108. $check_select = "<input name='selected_file' type='checkbox' value='" . $fileid . "'/>\n";
  8109. $link_edit = " <a href='#' onclick=\"update_ilm('" . $iassign_ilm->id . "', " . $fileid . ")\">" . iassign_icons::insert('edit_iassign') . "</a>\n";
  8110. $link_delete = " <a href='#' onclick=\"delete_ilm('" . $iassign_ilm->id . "', " . $fileid . ");\">" . iassign_icons::insert('delete_iassign') . "</a>\n";
  8111. $link_rename = " <a href='#' onclick=\"rename_ilm('" . $iassign_ilm->id . "', '" . $filename . "'," . $fileid . ");\">" . iassign_icons::insert('rename_iassign') . "</a>\n";
  8112. }
  8113. }
  8114. if (!in_array($filetype, $extensions_allow)) {
  8115. $link_edit = "";
  8116. $link_add_ilm_iassign = "";
  8117. $link_add_ilm_tinymce = "";
  8118. $link_add_ilm_atto = "";
  8119. $link_filter = "";
  8120. }
  8121. if ($one_files->is_directory()) {
  8122. $link_delete = " <a href='#' onclick=\"delete_dir_ilm('" . $iassign_ilm->id . "', " . $fileid . ");\">" . iassign_icons::insert('delete_dir') . "</a>\n";
  8123. $link_rename = " <a href='#' onclick=\"rename_dir_ilm('" . $iassign_ilm->id . "', '" . $pathname . "'," . $fileid . ");\">" . iassign_icons::insert('rename_dir') . "</a>\n";
  8124. $output .= "<tr><td>" . $check_select . $link_rename . $link_delete . "</td>
  8125. <td><a href='" . $dirurl . "' title='" . get_string('dir', 'iassign') . $pathname . "'>" . iassign_icons::insert('dir') . " " . $pathname . "</a></td>
  8126. <td><center>" . $author . "</center></td>
  8127. <td><center>" . $timecreated . "</center></td>
  8128. <td><center>" . $timemodified . "</center></td></tr>\n";
  8129. }
  8130. else if ($this->from == 'iassign') {
  8131. $new_id = "";
  8132. $new_class = "";
  8133. if (isset($_SESSION['file_name']) && $filename == $_SESSION['file_name']) {
  8134. $new_class = "<div id='new_file' style='position: absolute;'></div>"; unset($_SESSION['file_name']);
  8135. $new_id = "id='id_new_blink' style='background-color: hsl(244,61%,90%);'";
  8136. }
  8137. $output .= "<tr $new_id><td>" . $new_class . " " . $check_select . $link_rename . $link_delete . $link_duplicate . $link_edit . $link_filter . $link_add_ilm_iassign . "</td>
  8138. <td><a href='" . $fileurl . "' title='" . get_string('download_file', 'iassign') . " " . $filename . "'>" . $filename . "</a></td>
  8139. <td><center>" . $author . "</center></td>
  8140. <td><center>" . $timecreated . "</center></td>
  8141. <td><center>" . $timemodified . "</center></td></tr>\n";
  8142. }
  8143. else if ($this->from == 'block') {
  8144. $output .= "<tr><td>" . $check_select . $link_rename . $link_delete . $link_duplicate . $link_edit . $link_filter . "</td>
  8145. <td><a href='" . $fileurl . "' title='" . get_string('download_file', 'iassign') . " " . $filename . "'>" . $filename . "</a></td>
  8146. <td><center>" . $author . "</center></td>
  8147. <td><center>" . $timecreated . "</center></td>
  8148. <td><center>" . $timemodified . "</center></td></tr>\n";
  8149. }
  8150. else if ($this->from == 'tinymce') {
  8151. $output .= "<tr><td>" . $check_select . $link_rename . $link_delete . $link_duplicate . $link_edit . $link_filter . $link_add_ilm_tinymce . "</td>
  8152. <td><a href='" . $fileurl . "' title='" . get_string('download_file', 'iassign') . " " . $filename . "'>" . $filename . "</a></td>
  8153. <td><center>" . $author . "</center></td>
  8154. <td><center>" . $timecreated . "</center></td>
  8155. <td><center>" . $timemodified . "</center></td></tr>\n";
  8156. }
  8157. else if ($this->from == 'atto') {
  8158. $output .= "<tr><td>" . $check_select . $link_rename . $link_delete . $link_duplicate . $link_edit . $link_filter . $link_add_ilm_atto . "</td>
  8159. <td><a href='" . $fileurl . "' title='" . get_string('download_file', 'iassign') . " " . $filename . "'>" . $filename . "</a></td>
  8160. <td><center>" . $author . "</center></td>
  8161. <td><center>" . $timecreated . "</center></td>
  8162. <td><center>" . $timemodified . "</center></td></tr>\n";
  8163. }
  8164. }
  8165. }
  8166. $basename = explode("/", substr($dir_base, 0, strlen($dir_base) - 1));
  8167. $dir_base = "";
  8168. $header = "";
  8169. // Get all files in the course context with the associated iLM extension (e.g., iLM=iVProgH => extension "*ivph")
  8170. foreach ($basename as $value) {
  8171. $dir_base .= $value . "/";
  8172. $dir_id = $fs->get_file($context->id, 'mod_iassign', 'activity', 0, $dir_base, '.');
  8173. if ($dir_id) {
  8174. if ($value == "") {
  8175. $fileurl = new moodle_url($this->url) . '&dirid=' . $dir_id->get_id() . '&ilmid=' . $ilmid;
  8176. $header .= " <a href='$fileurl' title='" . get_string('dir', 'iassign') . "Home'>Home</a>\n";
  8177. } else {
  8178. $fileurl = new moodle_url($this->url) . '&dirid=' . $dir_id->get_id() . '&ilmid=' . $ilmid;
  8179. $header .= " " . $OUTPUT->rarrow() . " <a href='$fileurl' title='" . get_string('dir', 'iassign') . "$dir_base'>$value</a>\n";
  8180. }
  8181. }
  8182. } // foreach ($basename as $value)
  8183. $html = $OUTPUT->heading(iassign_icons::insert('open_dir') . $header, 2, 'dirtitle', 'iassign');
  8184. $select_all = "<input id='select_all' type='checkbox' onclick='select_all_ilm();'/>\n";
  8185. $html .= "
  8186. <table id='outlinetable' class='generaltable boxaligncenter' cellpadding='5' width='100%'>
  8187. <tr><th class='header c1' width='20%'>$select_all " . get_string('functions', 'iassign') . "</th>
  8188. <th class='header c1' width='*'>" . get_string('file', 'iassign') . "</th>
  8189. <th class='header c1' width='10%'>" . get_string('author', 'iassign') . "</th>
  8190. <th class='header c1' width='10%'>" . get_string('file_created', 'iassign') . "</th>
  8191. <th class='header c1' width='10%'>" . get_string('file_modified', 'iassign') . "</th>\n " . $output . "
  8192. </table>\n";
  8193. $html .= "<form id='form_buttons' method='post' enctype='multipart/form-data'>\n";
  8194. $html .= "<table width='100%'><tr>\n";
  8195. $html .= "<td width='80%'><input type='button' value='" . get_string('new_dir', 'iassign') . "' onClick='new_dir_ilm();'>\n";
  8196. if ($count_files != 0) {
  8197. $html .= " &nbsp; | &nbsp; <input type='button' value='" . get_string('file_ilm_move', 'iassign') . "' onClick=\"move_selected_ilm('" . $iassign_ilm->id . "');\"/> &nbsp; | &nbsp; \n";
  8198. $html .= "<input type='button' value='" . get_string('file_ilm_delete', 'iassign') . "' onClick=\"delete_selected_ilm('" . $iassign_ilm->id . "');\"> &nbsp; | &nbsp; \n";
  8199. $html .= "<input type='button' value='" . get_string('file_ilm_export', 'iassign') . "' onClick='export_files_ilm();'>\n";
  8200. }
  8201. $html .= "</td><td width='100%' align='right'><input type='button' value='" . get_string('file_ilm_recover', 'iassign') . "' onClick='recover_files_ilm();'>\n";
  8202. $html .= $OUTPUT->help_icon('file_ilm_recover', 'iassign') . "</td></tr></table>\n";
  8203. $html .= "</form>\n";
  8204. print $code_javascript_ilm;
  8205. print $html;
  8206. } // function view_files_ilm($iassign_ilm_class, $extension)
  8207. } // class ilm_manager
  8208. /// Class to insert of icons
  8209. class iassign_icons {
  8210. static function insert ($icon) {
  8211. global $CFG;
  8212. $string = '<img src="' . $CFG->wwwroot . '/mod/iassign/icon/' . $icon . '.gif" title="' . get_string($icon, 'iassign') . '" alt="' . get_string($icon, 'iassign') . '"/>'; // "\n"
  8213. return $string;
  8214. }
  8215. static function insertIT ($icon, $title, $text_alt) {
  8216. global $CFG;
  8217. $string = '<img src="' . $CFG->wwwroot . '/mod/iassign/icon/' . $icon . '.gif" title="' . get_string($title, 'iassign') . '" alt="' . get_string($text_alt, 'iassign') . '"/>'; // "\n"
  8218. return $string;
  8219. }
  8220. }
  8221. /// Class with util functions for plugin manage
  8222. class iassign_utils {
  8223. /// Function to return the filename extension
  8224. // @param string The $filename as an string
  8225. // @return string Return an string (last group after '.')
  8226. static function filename_extension ($filename) {
  8227. if ($filename == null || $filename == '') {
  8228. return null;
  8229. }
  8230. $itens = explode('.', $filename);
  8231. $num = count($itens);
  8232. if ($num < 2) {
  8233. return null;
  8234. }
  8235. return $itens[$num - 1];
  8236. }
  8237. /// Use the PHP class/package ZipArchive to create a Zip file
  8238. // The version 1 with ZipArchive (with '$zip->addFile(...)') is NOT working:
  8239. // Warning: ZipArchive::close(): Failure to create temporary file: No such file or directory in /var/www/html/.../lib/filestorage/zip_archive.php on line 224
  8240. // @param $full_path_zip_filename, $vet_student_filename, $vet_student_content
  8241. static function register_temporary_file ($full_path_zip_filename, $vet_student_filename, $vet_student_content) {
  8242. //1 $zip = new ZipArchive(); //TODO this version uses '$zip->addFile(...)' and it is NOT working!
  8243. $zip = new zip_archive(); //2
  8244. $res = $zip->open($full_path_zip_filename);
  8245. $total_vet = count($vet_student_filename);
  8246. for ($ii=0; $ii<$total_vet; $ii++) {
  8247. // $zip->addFile($vet_student_filename[$ii], $vet_student_content[$ii]); //1 to ZipArchive
  8248. $zip->add_file_from_string($vet_student_filename[$ii], $vet_student_content[$ii]); //2 to zip_archive
  8249. }
  8250. $ret = $zip->close();
  8251. iassign_utils::download_file($full_path_zip_filename);
  8252. $filenameZip = $zip->filename;
  8253. return array($ret, $zip);
  8254. }
  8255. static function download_file ($zip_filename) {
  8256. header("Pragma: public");
  8257. header("Expires: 0");
  8258. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  8259. header("Cache-Control: private", false);
  8260. header("Content-Type: application/zip");
  8261. header("Content-Disposition: attachment; filename=\"" . basename($zip_filename) . "\";");
  8262. header("Content-Transfer-Encoding: binary");
  8263. header("Content-Length: " . @filesize($zip_filename));
  8264. set_time_limit(0);
  8265. @readfile("$zip_filename") || die("File not found.");
  8266. unlink($zip_filename);
  8267. }
  8268. /// Function for format filename remove special caracters
  8269. // @param string $text String for clean
  8270. // @param boolean $is_lowercase Boolean for apply lowercase in filename
  8271. // @return string Return an string in clean format
  8272. static function format_filename ($text) {
  8273. if ($text != '.') {
  8274. //D $text = htmlspecialchars(urldecode($text));
  8275. //D echo "locallib.php: format_filename: $text<br/>";
  8276. if (is_object($text)) // with iAssign did not filtered text from JSON format
  8277. $text = json_decode($text)->{current_language()};
  8278. // $text = htmlspecialchars($text); // not good ideia, it results strange at 'iassign_ilm' field
  8279. $array1 = array("(", ")", ",", "/", "\\", "!", "@", "#", "$", "&", "*", "+", "!", "?", ".", " ", ":",
  8280. "á", "à", "â", "ã", "ä", "é", "è", "ê", "ë", "ẽ", "í", "ì", "î", "ï", "ó", "ò", "ô", "õ", "ö", "ú", "ù", "û", "ü",
  8281. "ç", "Á", "À", "Â", "Ã", "Ä", "É", "È", "Ê", "Ë", "Í", "Ì", "Î", "Ï", "Ó", "Ò", "Ô", "Õ", "Ö", "Ú", "Ù", "Û", "Ü", "Ç");
  8282. $array2 = array("", "", "_", "_", "_", "", "", "", "", "", "", "", "", "", "_", "_", "-",
  8283. "a", "a", "a", "a", "a", "e", "e", "e", "e", "e", "i", "i", "i", "i", "o", "o", "o", "o", "o", "u", "u", "u", "u",
  8284. "c", "A", "A", "A", "A", "A", "E", "E", "E", "E", "I", "I", "I", "I", "O", "O", "O", "O", "O", "U", "U", "U", "U", "C");
  8285. // E.g. text = "exerc_midpoint.course.geo"
  8286. // 0123456789012345678901234
  8287. $index_lastposition = strrpos($text, "."); // Find the position of the last occurrence of a substring in a string (21)
  8288. $text2 = substr($text, 0, $index_lastposition); // Erase the final point and its extension ("exerc_midpoint.course")
  8289. $ext = substr($text, $index_lastposition); // Point with extention (".geo")
  8290. $text = str_replace(".", "", $text2) . $ext; // Clear any other point and redefine the name ("exerc_midpoint.course.geo")
  8291. $text = str_replace($array1, $array2, $text); // Replace any letter with accent mark under the UTF8 format
  8292. $text = str_replace("-_", "_", $text); // avoid "-_"
  8293. $text = str_replace("__", "_", $text); // avoid "__"
  8294. }
  8295. return $text;
  8296. }
  8297. /// Function to filter characters to the file name to iLM content
  8298. // @param string $text String for clean
  8299. // @param string $ext String ot the extension of the iLM
  8300. // @return string Return an string in clean format
  8301. static function filter_filename_extension ($filename, $ext) {
  8302. if ($filename != '.') {
  8303. $arrFrom = array("=","~","`","!","@","$","%","^","&","*","(",")");
  8304. $arrTo = array("_","","","","","","","","","","","","");
  8305. $filename = str_replace($arrFrom, $arrTo, $filename);
  8306. $filenameF = preg_replace('/[^\x20-\x7E]/','', $filename); // only ASCII - https://stackoverflow.com/questions/8781911/remove-non-ascii-characters-from-string
  8307. $index_lastposition = strrpos($filenameF, "."); // find the position of the last occurrence of a substring in a string (21)
  8308. if (!isset($index_lastposition) || $index_lastposition=="" || $index_lastposition<0) // Just in case (logic of function 'strrpos(...)' change, ensure to preserve name without '.'
  8309. $index_lastposition = strlen($filenameF);
  8310. $text = substr($filenameF, 0, $index_lastposition); // erase the final point and its extension ("exerc_midpoint.course")
  8311. $text = str_replace(".", "_", $text); // erase all other "."
  8312. $existext = substr($filenameF, $index_lastposition); // get the possibly extension
  8313. $filenameF = $text . "." . $ext; // no matter, use the prefiz with oficial extension
  8314. }
  8315. else
  8316. $filenameF = 'file_without_name.' . $ext; // Just in case... if the use do not enter a valid name, only entered '.'!
  8317. return $filenameF;
  8318. }
  8319. /// Function for format pathname remove special caracters
  8320. // @param string $text String for clean
  8321. // @param boolean $is_lowercase Boolean for apply lowercase in pathname
  8322. // @return string Return an string in clean format
  8323. static function format_pathname ($text, $is_lowercase = true) {
  8324. $array1 = array("á", "à", "â", "ã", "ä", "é", "è", "ê", "ë", "í", "ì", "î", "ï", "ó", "ò", "ô", "õ", "ö", "ú", "ù", "û", "ü", "ç", "Á", "À", "Â", "Ã", "Ä", "É", "È", "Ê", "Ë", "Í", "Ì", "Î", "Ï", "Ó", "Ò", "Ô", "Õ", "Ö", "Ú", "Ù", "Û", "Ü", "Ç", "@", " ", "!", "?", ".");
  8325. $array2 = array("a", "a", "a", "a", "a", "e", "e", "e", "e", "i", "i", "i", "i", "o", "o", "o", "o", "o", "u", "u", "u", "u", "c", "A", "A", "A", "A", "A", "E", "E", "E", "E", "I", "I", "I", "I", "O", "O", "O", "O", "O", "U", "U", "U", "U", "C", "-", "_", "", "", "");
  8326. $text = str_replace($array1, $array2, $text);
  8327. if ($is_lowercase)
  8328. $text = strtolower($text);
  8329. return $text;
  8330. }
  8331. /// Function to standardize file name from iLM original name (<iLM name>) and "base_name" ({iassign_ilm}.file_jar)
  8332. /// Will be created the directory: ./mod/iassign/ilm/<iLM name>/<iLM version>/<base_name>
  8333. // @calledby ./mod/iassign/ilm_handlers/html5.php!save_ilm_by_xml($application_xml,$files_extract): twice times, first with 'name', second with 'file_jar'
  8334. // $application_file_name = iassign_utils::filename_from_iLM_name($application_xml->name,false);
  8335. // $file_jar = iassign_utils::filename_from_iLM_name($application_xml->file_jar, false); // in "locallib.php"
  8336. // @param string $text String to build the file neam
  8337. // @param boolean $is_lowercase Boolean for apply lowercase in pathname
  8338. // @return string Return an string in clean format
  8339. static function filename_from_iLM_name ($ilm_name, $is_lowercase = true) {
  8340. // Change special UTF-8 characters to the corresponding ASCII
  8341. $array1 = array("á", "à", "â", "ã", "ä", "é", "è", "ê", "ë", "í", "ì", "î", "ï", "ó", "ò", "ô", "õ", "ö",
  8342. "ú", "ù", "û", "ü", "ç", "Á", "À", "Â", "Ã", "Ä", "É", "È", "Ê", "Ë", "Í", "Ì", "Î", "Ï",
  8343. "Ó", "Ò", "Ô", "Õ", "Ö", "Ú", "Ù", "Û", "Ü", "Ç", "@", " ", "!", "?"); // in 2025/04 removed last item, dot: , "."
  8344. $array2 = array("a", "a", "a", "a", "a", "e", "e", "e", "e", "i", "i", "i", "i", "o", "o", "o", "o", "o",
  8345. "u", "u", "u", "u", "c", "A", "A", "A", "A", "A", "E", "E", "E", "E", "I", "I", "I", "I",
  8346. "O", "O", "O", "O", "O", "U", "U", "U", "U", "C", "-", "_", "", ""); // in 2025/04 removed last item, dot: , ""
  8347. $index_firstposition_blank = strpos($ilm_name, " ");
  8348. if ($index_firstposition_blank) { // truncate the name!
  8349. $ilm_name = substr($ilm_name, 0, $index_firstposition_blank);
  8350. }
  8351. $index_firstposition_dots = strpos($ilm_name, ":");
  8352. if ($index_firstposition_dots) { // truncate the name!
  8353. $ilm_name = substr($ilm_name, 0, $index_firstposition_dots);
  8354. }
  8355. $namefiltered = str_replace($array1, $array2, $ilm_name); // Erase UTF-8 specials to the simple ASCII
  8356. if ($is_lowercase)
  8357. $namefiltered = strtolower($namefiltered);
  8358. return $namefiltered;
  8359. }
  8360. /// Function for insert version in the filename
  8361. // @param string $filename Name of file
  8362. // @return string Return the filename with version
  8363. static function version_filename ($filename) {
  8364. $array_filename = explode('.', $filename);
  8365. if (count($array_filename) > 1)
  8366. $filename = $array_filename[0] . '-' . date("Ymd-His") . '.' . $array_filename[1];
  8367. else
  8368. $filename = $array_filename[0] . '-' . date("Ymd-His");
  8369. return $filename;
  8370. }
  8371. //TODO Retirar quando atualizar todo os iassign que estão com a tag &lt;ia_uc&gt;
  8372. static function remove_code_message ($string) {
  8373. $array = explode("&lt;ia_uc&gt;", $string);
  8374. return $array[0];
  8375. }
  8376. }
  8377. // class iassign_utils
  8378. /// Class with language functions for plugin manage
  8379. class iassign_language {
  8380. /// Function for return text in language or get default language (en)
  8381. // @param string $lang Code of language
  8382. // @param string $description JSON text content all languages
  8383. // @return string Return an string in the language selected
  8384. static function get_description_lang ($lang, $descriptions) {
  8385. $description_lang = "";
  8386. $description = json_decode($descriptions);
  8387. if ($description == null) {
  8388. $description_lang = $descriptions;
  8389. }
  8390. else {
  8391. if (isset($description->{$lang}))
  8392. $description_lang = $description->{$lang};
  8393. else
  8394. if (isset($description->en)) //MOOC2014
  8395. $description_lang = $description->en;
  8396. else //MOOC2014
  8397. $description_lang = "en"; //MOOC2014
  8398. }
  8399. return $description_lang;
  8400. }
  8401. /// Function for return all language supported by iLM
  8402. // @param string $descriptions JSON text content all languages
  8403. // @return string Return as string with all languages
  8404. static function get_all_lang ($descriptions) {
  8405. $langs = "";
  8406. $description = json_decode($descriptions);
  8407. if ($description) {
  8408. foreach ($description as $key => $value) {
  8409. $langs .= $key . " ; ";
  8410. }
  8411. $langs = substr($langs, 0, strlen($langs) - 3);
  8412. }
  8413. return $langs;
  8414. }
  8415. /// Function for convert json in xml //MOOC2014
  8416. // @param string $json JSON text
  8417. // @return string Return as string with xml tags
  8418. static function json_to_xml ($json) {
  8419. $xml = "";
  8420. $json = json_decode($json);
  8421. foreach ($json as $key => $value) {
  8422. $xml .= "\n <" . $key . ">" . $value . "</" . $key . ">";
  8423. }
  8424. return $xml;
  8425. }
  8426. }
  8427. // class iassign_language
  8428. /// Class with log functions for plugin manage.
  8429. class iassign_log {
  8430. /// Function for insert log event
  8431. // @param string $action Code action of event
  8432. // @param string $information Text for describe action of event
  8433. // @param int $cmid Id of context module
  8434. // @param int $ilmid Id of iLM
  8435. static function add_log ($action, $information = "", $cmid = 0, $ilmid = 0) {
  8436. global $COURSE, $CFG, $USER, $DB;
  8437. $newentry = new stdClass();
  8438. $newentry->time = time();
  8439. $newentry->userid = $USER->id;
  8440. $newentry->ip = $_SERVER['REMOTE_ADDR'];
  8441. $newentry->course = $COURSE->id;
  8442. $newentry->cmid = $cmid;
  8443. $newentry->ilmid = $ilmid;
  8444. $newentry->action = $action;
  8445. $newentry->info = $information;
  8446. $newentry->language = current_language();
  8447. $newentry->user_agent = $_SERVER['HTTP_USER_AGENT'];
  8448. if (ini_get("browscap") && function_exists('get_browse')) {
  8449. $browser = get_browse(null, true);
  8450. $newentry->javascript = $browser['javascript'];
  8451. $newentry->java = $browser['javaapplets'];
  8452. }
  8453. if (!$newentry->id = $DB->insert_record("iassign_log", $newentry))
  8454. print_error('error_add_log', 'iassign');
  8455. } // static function add_log($action, $information = "", $cmid = 0, $ilmid = 0)
  8456. } // class iassign_log