wiki.inc.php 238 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Component\Editor\Connector;
  4. use Chamilo\CoreBundle\Component\Filesystem\Data;
  5. use ChamiloSession as Session;
  6. use MediaAlchemyst\Alchemyst;
  7. use MediaAlchemyst\DriversContainer;
  8. use Neutron\TemporaryFilesystem\Manager;
  9. use Neutron\TemporaryFilesystem\TemporaryFilesystem;
  10. use Symfony\Component\Filesystem\Filesystem;
  11. /**
  12. * Class Wiki
  13. * Functions library for the wiki tool
  14. * @author Juan Carlos Raña <herodoto@telefonica.net>
  15. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  16. * @author Julio Montoya <gugli100@gmail.com> using the pdf.lib.php library
  17. *
  18. * @package chamilo.wiki
  19. */
  20. class Wiki
  21. {
  22. public $tbl_wiki;
  23. public $tbl_wiki_discuss;
  24. public $tbl_wiki_mailcue;
  25. public $tbl_wiki_conf;
  26. public $session_id = null;
  27. public $course_id = null;
  28. public $condition_session = null;
  29. public $group_id;
  30. public $assig_user_id;
  31. public $groupfilter = 'group_id=0';
  32. public $courseInfo;
  33. public $charset;
  34. public $page;
  35. public $action;
  36. public $wikiData = array();
  37. public $url;
  38. /**
  39. * Constructor
  40. */
  41. public function __construct()
  42. {
  43. // Database table definition
  44. $this->tbl_wiki = Database::get_course_table(TABLE_WIKI);
  45. $this->tbl_wiki_discuss = Database::get_course_table(TABLE_WIKI_DISCUSS);
  46. $this->tbl_wiki_mailcue = Database::get_course_table(TABLE_WIKI_MAILCUE);
  47. $this->tbl_wiki_conf = Database::get_course_table(TABLE_WIKI_CONF);
  48. $this->session_id = api_get_session_id();
  49. $this->condition_session = api_get_session_condition($this->session_id);
  50. $this->course_id = api_get_course_int_id();
  51. $this->group_id = api_get_group_id();
  52. if (!empty($this->group_id)) {
  53. $this->groupfilter = ' group_id="'.$this->group_id.'"';
  54. }
  55. $this->courseInfo = api_get_course_info();
  56. $this->url = api_get_path(WEB_CODE_PATH).'wiki/index.php?'.api_get_cidreq();
  57. }
  58. /**
  59. * Check whether this title is already used
  60. * @param string $link
  61. *
  62. *
  63. * @return bool False if title is already taken
  64. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  65. **/
  66. public function checktitle($link)
  67. {
  68. $tbl_wiki = $this->tbl_wiki;
  69. $condition_session = $this->condition_session;
  70. $course_id = $this->course_id;
  71. $groupfilter = $this->groupfilter;
  72. $sql = 'SELECT * FROM '.$tbl_wiki.'
  73. WHERE
  74. c_id = '.$course_id.' AND
  75. reflink="'.Database::escape_string($link).'" AND
  76. '.$groupfilter.$condition_session.'';
  77. $result = Database::query($sql);
  78. $numberofresults = Database::num_rows($result);
  79. // the value has not been found and is this available
  80. if ($numberofresults == 0) {
  81. return true;
  82. } else {
  83. // the value has been found
  84. return false;
  85. }
  86. }
  87. /**
  88. * check wikilinks that has a page
  89. * @author Juan Carlos Raña <herodoto@telefonica.net>
  90. * @param string $input
  91. **/
  92. public function links_to($input)
  93. {
  94. $input_array = preg_split("/(\[\[|\]\])/",$input,-1, PREG_SPLIT_DELIM_CAPTURE);
  95. $all_links = array();
  96. foreach ($input_array as $key=>$value) {
  97. if (isset($input_array[$key-1]) && $input_array[$key-1] == '[[' &&
  98. isset($input_array[$key+1]) && $input_array[$key+1] == ']]'
  99. ) {
  100. if (api_strpos($value, "|") !== false) {
  101. $full_link_array = explode("|", $value);
  102. $link = trim($full_link_array[0]);
  103. $title = trim($full_link_array[1]);
  104. } else {
  105. $link = trim($value);
  106. $title = trim($value);
  107. }
  108. unset($input_array[$key-1]);
  109. unset($input_array[$key+1]);
  110. //replace blank spaces by _ within the links. But to remove links at the end add a blank space
  111. $all_links[]= Database::escape_string(str_replace(' ','_',$link)).' ';
  112. }
  113. }
  114. $output = implode($all_links);
  115. return $output;
  116. }
  117. /**
  118. * detect and add style to external links
  119. * @author Juan Carlos Raña Trabado
  120. **/
  121. public function detect_external_link($input)
  122. {
  123. $exlink = 'href=';
  124. $exlinkStyle = 'class="wiki_link_ext" href=';
  125. $output = str_replace($exlink, $exlinkStyle, $input);
  126. return $output;
  127. }
  128. /**
  129. * detect and add style to anchor links
  130. * @author Juan Carlos Raña Trabado
  131. **/
  132. public function detect_anchor_link($input)
  133. {
  134. $anchorlink = 'href="#';
  135. $anchorlinkStyle = 'class="wiki_anchor_link" href="#';
  136. $output = str_replace($anchorlink, $anchorlinkStyle, $input);
  137. return $output;
  138. }
  139. /**
  140. * detect and add style to mail links
  141. * author Juan Carlos Raña Trabado
  142. **/
  143. public function detect_mail_link($input)
  144. {
  145. $maillink = 'href="mailto';
  146. $maillinkStyle = 'class="wiki_mail_link" href="mailto';
  147. $output = str_replace($maillink, $maillinkStyle, $input);
  148. return $output;
  149. }
  150. /**
  151. * detect and add style to ftp links
  152. * @author Juan Carlos Raña Trabado
  153. **/
  154. public function detect_ftp_link($input)
  155. {
  156. $ftplink='href="ftp';
  157. $ftplinkStyle = 'class="wiki_ftp_link" href="ftp';
  158. $output = str_replace($ftplink, $ftplinkStyle, $input);
  159. return $output;
  160. }
  161. /**
  162. * detect and add style to news links
  163. * @author Juan Carlos Raña Trabado
  164. **/
  165. public function detect_news_link($input)
  166. {
  167. $newslink = 'href="news';
  168. $newslinkStyle = 'class="wiki_news_link" href="news';
  169. $output = str_replace($newslink, $newslinkStyle, $input);
  170. return $output;
  171. }
  172. /**
  173. * detect and add style to irc links
  174. * @author Juan Carlos Raña Trabado
  175. **/
  176. public function detect_irc_link($input)
  177. {
  178. $irclink = 'href="irc';
  179. $irclinkStyle = 'class="wiki_irc_link" href="irc';
  180. $output = str_replace($irclink, $irclinkStyle, $input);
  181. return $output;
  182. }
  183. /**
  184. * This function allows users to have [link to a title]-style links like in most regular wikis.
  185. * It is true that the adding of links is probably the most anoying part of Wiki for the people
  186. * who know something about the wiki syntax.
  187. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  188. * Improvements [[]] and [[ | ]]by Juan Carlos Raña
  189. * Improvements internal wiki style and mark group by Juan Carlos Raña
  190. **/
  191. public function make_wiki_link_clickable($input)
  192. {
  193. $groupId = api_get_group_id();
  194. //now doubles brackets
  195. $input_array = preg_split("/(\[\[|\]\])/",$input,-1, PREG_SPLIT_DELIM_CAPTURE);
  196. foreach ($input_array as $key => $value) {
  197. //now doubles brackets
  198. if (isset($input_array[$key-1]) && $input_array[$key-1] == '[[' AND
  199. $input_array[$key+1] == ']]'
  200. ) {
  201. // now full wikilink
  202. if (api_strpos($value, "|") !== false){
  203. $full_link_array=explode("|", $value);
  204. $link=trim(strip_tags($full_link_array[0]));
  205. $title=trim($full_link_array[1]);
  206. } else {
  207. $link=trim(strip_tags($value));
  208. $title=trim($value);
  209. }
  210. //if wikilink is homepage
  211. if ($link == 'index') {
  212. $title = get_lang('DefaultTitle');
  213. }
  214. if ($link == get_lang('DefaultTitle')) {
  215. $link = 'index';
  216. }
  217. // note: checkreflink checks if the link is still free. If it is not used then it returns true, if it is used, then it returns false. Now the title may be different
  218. if (self::checktitle(strtolower(str_replace(' ','_',$link)))) {
  219. $link = api_html_entity_decode($link);
  220. $input_array[$key]='<a href="'.api_get_path(WEB_PATH).'main/wiki/index.php?'.api_get_cidreq().'&action=addnew&amp;title='.Security::remove_XSS($link).'&group_id='.$groupId.'" class="new_wiki_link">'.$title.'</a>';
  221. } else {
  222. $input_array[$key]='<a href="'.api_get_path(WEB_PATH).'main/wiki/index.php?'.api_get_cidreq().'&action=showpage&amp;title='.urlencode(strtolower(str_replace(' ','_',$link))).'&group_id='.$groupId.'" class="wiki_link">'.$title.'</a>';
  223. }
  224. unset($input_array[$key-1]);
  225. unset($input_array[$key+1]);
  226. }
  227. }
  228. $output = implode('', $input_array);
  229. return $output;
  230. }
  231. /**
  232. * This function saves a change in a wiki page
  233. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  234. * @param array $values
  235. * @return language string saying that the changes are stored
  236. **/
  237. public function save_wiki($values)
  238. {
  239. $tbl_wiki = $this->tbl_wiki;
  240. $tbl_wiki_conf = $this->tbl_wiki_conf;
  241. $_course = $this->courseInfo;
  242. $time = api_get_utc_datetime();
  243. $session_id = api_get_session_id();
  244. $groupId = api_get_group_id();
  245. $userId = api_get_user_id();
  246. $course_id = api_get_course_int_id();
  247. $_clean = array(
  248. 'task' => '',
  249. 'feedback1' => '',
  250. 'feedback2' => '',
  251. 'feedback3' => '',
  252. 'fprogress1' => '',
  253. 'fprogress2' => '',
  254. 'fprogress3' => '',
  255. 'max_text' => 0,
  256. 'max_version' => 0,
  257. 'delayedsubmit' => '',
  258. 'assignment' => 0
  259. );
  260. $pageId = intval($values['page_id']);
  261. // NOTE: visibility, visibility_disc and ratinglock_disc changes
  262. // are not made here, but through the interce buttons
  263. // cleaning the variables
  264. if (api_get_setting('htmlpurifier_wiki') == 'true'){
  265. //$purifier = new HTMLPurifier();
  266. $values['content'] = Security::remove_XSS($values['content']);
  267. }
  268. $version = intval($values['version']) + 1 ;
  269. $linkTo = self::links_to($values['content']); //and check links content
  270. //cleaning config variables
  271. if (!empty($values['task'])) {
  272. $_clean['task'] = $values['task'];
  273. }
  274. if (!empty($values['feedback1']) || !empty($values['feedback2']) || !empty($values['feedback3'])) {
  275. $_clean['feedback1'] = $values['feedback1'];
  276. $_clean['feedback2'] = $values['feedback2'];
  277. $_clean['feedback3'] = $values['feedback3'];
  278. $_clean['fprogress1'] = $values['fprogress1'];
  279. $_clean['fprogress2'] = $values['fprogress2'];
  280. $_clean['fprogress3'] = $values['fprogress3'];
  281. }
  282. if (isset($values['initstartdate']) && $values['initstartdate'] == 1) {
  283. $_clean['startdate_assig'] = $values['startdate_assig'];
  284. } else {
  285. $_clean['startdate_assig'] = null;
  286. }
  287. if (isset($values['initenddate']) && $values['initenddate']==1) {
  288. $_clean['enddate_assig'] = $values['enddate_assig'];
  289. } else {
  290. $_clean['enddate_assig'] = null;
  291. }
  292. if (isset($values['delayedsubmit'])) {
  293. $_clean['delayedsubmit'] = $values['delayedsubmit'];
  294. }
  295. if (!empty($values['max_text']) || !empty($values['max_version'])) {
  296. $_clean['max_text'] = $values['max_text'];
  297. $_clean['max_version'] = $values['max_version'];
  298. }
  299. $values['assignment'] = isset($values['assignment']) ? $values['assignment'] : 0;
  300. $values['page_id'] = isset($values['page_id']) ? $values['page_id'] : 0;
  301. $params = [
  302. 'c_id' => $course_id,
  303. 'addlock' => 1,
  304. 'visibility' => 1,
  305. 'visibility_disc' => 1,
  306. 'addlock_disc' => 1,
  307. 'ratinglock_disc' => 1,
  308. 'page_id' => $pageId,
  309. 'reflink' => trim($values['reflink']),
  310. 'title' => trim($values['title']),
  311. 'content' => $values['content'],
  312. 'user_id' => $userId,
  313. 'group_id' => $groupId,
  314. 'dtime' => $time,
  315. 'assignment' => $values['assignment'],
  316. 'comment' => $values['comment'],
  317. 'progress' => $values['progress'],
  318. 'version' => $version,
  319. 'linksto' => $linkTo,
  320. 'user_ip' => $_SERVER['REMOTE_ADDR'],
  321. 'session_id' => $session_id,
  322. 'page_id' => $values['page_id'],
  323. 'editlock' => 0,
  324. 'is_editing' => 0,
  325. 'time_edit' => $time,
  326. 'tag' => ''
  327. ];
  328. $id = Database::insert($tbl_wiki, $params);
  329. if ($id > 0) {
  330. $sql = "UPDATE $tbl_wiki SET id = iid WHERE iid = $id";
  331. Database::query($sql);
  332. // insert into item_property
  333. api_item_property_update(
  334. $_course,
  335. TOOL_WIKI,
  336. $id,
  337. 'WikiAdded',
  338. $userId,
  339. $groupId
  340. );
  341. if ($values['page_id']== 0) {
  342. $sql = 'UPDATE '.$tbl_wiki.' SET page_id="'.$id.'"
  343. WHERE c_id = '.$course_id.' AND iid ="'.$id.'"';
  344. Database::query($sql);
  345. }
  346. }
  347. // Update wiki config
  348. if ($values['reflink'] == 'index' && $version == 1) {
  349. $params = [
  350. 'c_id' => $course_id,
  351. 'page_id' => $id,
  352. 'task' => $_clean['task'],
  353. 'feedback1' => $_clean['feedback1'],
  354. 'feedback2' => $_clean['feedback2'],
  355. 'feedback3' => $_clean['feedback3'],
  356. 'fprogress1' => $_clean['fprogress1'],
  357. 'fprogress2' => $_clean['fprogress2'],
  358. 'fprogress3' => $_clean['fprogress3'],
  359. 'max_text' => intval($_clean['max_text']),
  360. 'max_version' => intval($_clean['max_version']),
  361. 'startdate_assig' => $_clean['startdate_assig'],
  362. 'enddate_assig' => $_clean['enddate_assig'],
  363. 'delayedsubmit' => $_clean['delayedsubmit']
  364. ];
  365. Database::insert($tbl_wiki_conf, $params);
  366. } else {
  367. $params = [
  368. 'task' => $_clean['task'],
  369. 'feedback1' => $_clean['feedback1'],
  370. 'feedback2' => $_clean['feedback2'],
  371. 'feedback3' => $_clean['feedback3'],
  372. 'fprogress1' => $_clean['fprogress1'],
  373. 'fprogress2' => $_clean['fprogress2'],
  374. 'fprogress3' => $_clean['fprogress3'],
  375. 'max_text' => intval($_clean['max_text']),
  376. 'max_version' => intval($_clean['max_version']),
  377. 'startdate_assig' => $_clean['startdate_assig'],
  378. 'enddate_assig' => $_clean['enddate_assig'],
  379. 'delayedsubmit' => $_clean['delayedsubmit']
  380. ];
  381. Database::update(
  382. $tbl_wiki_conf,
  383. $params,
  384. ['page_id = ? AND c_id = ?' => [$pageId, $course_id]]
  385. );
  386. }
  387. api_item_property_update(
  388. $_course,
  389. 'wiki',
  390. $id,
  391. 'WikiAdded',
  392. $userId,
  393. $groupId
  394. );
  395. self::check_emailcue($_clean['reflink'], 'P', $time, $userId);
  396. $this->setWikiData($id);
  397. return get_lang('Saved');
  398. }
  399. /**
  400. * This function restore a wikipage
  401. * @author Juan Carlos Raña <herodoto@telefonica.net>
  402. * @return string Message of success (to be printed on screen)
  403. **/
  404. public function restore_wikipage(
  405. $r_page_id,
  406. $r_reflink,
  407. $r_title,
  408. $r_content,
  409. $r_group_id,
  410. $r_assignment,
  411. $r_progress,
  412. $c_version,
  413. $r_version,
  414. $r_linksto
  415. ) {
  416. $tbl_wiki = $this->tbl_wiki;
  417. $_course = $this->courseInfo;
  418. $r_user_id = api_get_user_id();
  419. $r_dtime = api_get_utc_datetime();
  420. $r_version = $r_version+1;
  421. $r_comment = get_lang('RestoredFromVersion').': '.$c_version;
  422. $session_id = api_get_session_id();
  423. $course_id = api_get_course_int_id();
  424. $params = [
  425. 'c_id' => $course_id,
  426. 'page_id' => $r_page_id,
  427. 'reflink' => $r_reflink,
  428. 'title' => $r_title,
  429. 'content' => $r_content,
  430. 'user_id' => $r_user_id,
  431. 'group_id' => $r_group_id,
  432. 'dtime' => $r_dtime,
  433. 'assignment' => $r_assignment,
  434. 'comment' => $r_comment,
  435. 'progress' => $r_progress,
  436. 'version' => $r_version,
  437. 'linksto' => $r_linksto,
  438. 'user_ip' => $_SERVER['REMOTE_ADDR'],
  439. 'session_id' => $session_id,
  440. ];
  441. $id = Database::insert($tbl_wiki, $params);
  442. if ($id) {
  443. $sql = "UPDATE $tbl_wiki SET id = iid WHERE iid = $id";
  444. Database::query($sql);
  445. api_item_property_update(
  446. $_course,
  447. 'wiki',
  448. $id,
  449. 'WikiAdded',
  450. api_get_user_id(),
  451. $r_group_id
  452. );
  453. self::check_emailcue($r_reflink, 'P', $r_dtime, $r_user_id);
  454. }
  455. return get_lang('PageRestored');
  456. }
  457. /**
  458. * This function delete a wiki
  459. * @author Juan Carlos Raña <herodoto@telefonica.net>
  460. * @return string Message of success (to be printed)
  461. **/
  462. public function delete_wiki()
  463. {
  464. $tbl_wiki = $this->tbl_wiki;
  465. $tbl_wiki_discuss = $this->tbl_wiki_discuss;
  466. $tbl_wiki_mailcue = $this->tbl_wiki_mailcue;
  467. $tbl_wiki_conf = $this->tbl_wiki_conf;
  468. $conditionSession = $this->condition_session;
  469. $groupFilter = $this->groupfilter;
  470. $course_id = $this->course_id;
  471. $sql = "SELECT page_id FROM $tbl_wiki
  472. WHERE c_id = $course_id AND $groupFilter $conditionSession
  473. ORDER BY id DESC";
  474. $sql = "DELETE FROM $tbl_wiki_conf
  475. WHERE c_id = $course_id AND page_id IN ($sql)";
  476. Database::query($sql);
  477. $sql = "DELETE FROM $tbl_wiki_discuss
  478. WHERE c_id = $course_id AND publication_id IN ($sql)";
  479. Database::query($sql);
  480. $sql = "DELETE FROM $tbl_wiki_mailcue
  481. WHERE c_id = $course_id AND $groupFilter $conditionSession ";
  482. Database::query($sql);
  483. $sql = "DELETE FROM $tbl_wiki
  484. WHERE c_id = $course_id AND $groupFilter $conditionSession ";
  485. Database::query($sql);
  486. return get_lang('WikiDeleted');
  487. }
  488. /**
  489. * This function saves a new wiki page.
  490. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  491. * @todo consider merging this with the function save_wiki into one single function.
  492. * @return string Message of success
  493. **/
  494. public function save_new_wiki($values)
  495. {
  496. $tbl_wiki = $this->tbl_wiki;
  497. $tbl_wiki_conf = $this->tbl_wiki_conf;
  498. $assig_user_id = $this->assig_user_id;
  499. $_clean = array();
  500. // cleaning the variables
  501. $_clean['assignment'] = '';
  502. if (isset($values['assignment'])) {
  503. $_clean['assignment'] = $values['assignment'];
  504. }
  505. // session_id
  506. $session_id = api_get_session_id();
  507. // Unlike ordinary pages of pages of assignments.
  508. // Allow create a ordinary page although there is a assignment with the same name
  509. if ($_clean['assignment'] == 2 || $_clean['assignment'] == 1) {
  510. $page = str_replace(' ','_',$values['title']."_uass".$assig_user_id);
  511. } else {
  512. $page = str_replace(' ','_',$values['title']);
  513. }
  514. $_clean['reflink'] = $page;
  515. $_clean['title'] = trim($values['title']);
  516. $_clean['content'] = $values['content'];
  517. if (api_get_setting('htmlpurifier_wiki') === 'true') {
  518. $purifier = new HTMLPurifier();
  519. $_clean['content'] = $purifier->purify($_clean['content']);
  520. }
  521. //re-check after strip_tags if the title is empty
  522. if (empty($_clean['title']) || empty($_clean['reflink'])) {
  523. return false;
  524. }
  525. if ($_clean['assignment'] == 2) {
  526. //config by default for individual assignment (students)
  527. //Identifies the user as a creator, not the teacher who created
  528. $_clean['user_id'] = intval($assig_user_id);
  529. $_clean['visibility'] = 0;
  530. $_clean['visibility_disc'] = 0;
  531. $_clean['ratinglock_disc'] = 0;
  532. } else {
  533. $_clean['user_id'] = api_get_user_id();
  534. $_clean['visibility'] = 1;
  535. $_clean['visibility_disc'] = 1;
  536. $_clean['ratinglock_disc'] = 1;
  537. }
  538. $_clean['comment'] = $values['comment'];
  539. $_clean['progress'] = $values['progress'];
  540. $_clean['version'] = 1;
  541. $groupId = api_get_group_id();
  542. //check wikilinks
  543. $_clean['linksto'] = self::links_to($_clean['content']);
  544. // cleaning config variables
  545. $_clean['task'] = isset($values['task']) ? $values['task'] : '';
  546. $_clean['feedback1'] = isset($values['feedback1']) ? $values['feedback1'] : '';
  547. $_clean['feedback2'] = isset($values['feedback2']) ? $values['feedback2'] : '';
  548. $_clean['feedback3'] = isset($values['feedback3']) ? $values['feedback3'] : '';
  549. $_clean['fprogress1'] = isset($values['fprogress1']) ? $values['fprogress1'] : '';
  550. $_clean['fprogress2'] = isset($values['fprogress2']) ? $values['fprogress2'] : '';
  551. $_clean['fprogress3'] = isset($values['fprogress3']) ? $values['fprogress3'] : '';
  552. if (isset($values['initstartdate']) && $values['initstartdate'] == 1) {
  553. $_clean['startdate_assig'] = $values['startdate_assig'];
  554. } else {
  555. $_clean['startdate_assig'] = null;
  556. }
  557. if (isset($values['initenddate']) && $values['initenddate'] == 1) {
  558. $_clean['enddate_assig'] = $values['enddate_assig'];
  559. } else {
  560. $_clean['enddate_assig'] = null;
  561. }
  562. $_clean['delayedsubmit'] = isset($values['delayedsubmit']) ? $values['delayedsubmit'] : '';
  563. $_clean['max_text'] = isset($values['max_text']) ? $values['max_text'] : '';
  564. $_clean['max_version'] = isset($values['max_version']) ? $values['max_version'] : '';
  565. $course_id = api_get_course_int_id();
  566. // Filter no _uass
  567. if (api_strtoupper(trim($values['title'])) === 'INDEX') {
  568. Display::addFlash(Display::display_warning_message(get_lang('GoAndEditMainPage'), false, true));
  569. } else {
  570. $var = $_clean['reflink'];
  571. $group_id = intval($_GET['group_id']);
  572. if (!self::checktitle($var)) {
  573. return get_lang('WikiPageTitleExist').
  574. '<a href="index.php?action=edit&amp;title='.$var.'&group_id='.$group_id.'">'.
  575. $values['title'].'</a>';
  576. } else {
  577. $dtime = api_get_utc_datetime();
  578. $params = [
  579. 'c_id' => $course_id,
  580. 'reflink' => $_clean['reflink'],
  581. 'title' => $_clean['title'],
  582. 'content' => $_clean['content'],
  583. 'user_id' => $_clean['user_id'],
  584. 'group_id' => $groupId,
  585. 'dtime' => $dtime,
  586. 'visibility' => $_clean['visibility'],
  587. 'visibility_disc' => $_clean['visibility_disc'],
  588. 'ratinglock_disc' => $_clean['ratinglock_disc'],
  589. 'assignment' => $_clean['assignment'],
  590. 'comment' => $_clean['comment'],
  591. 'progress' => $_clean['progress'],
  592. 'version' => $_clean['version'],
  593. 'linksto' => $_clean['linksto'],
  594. 'user_ip' => $_SERVER['REMOTE_ADDR'],
  595. 'session_id' => $session_id,
  596. ];
  597. $id = Database::insert($tbl_wiki, $params);
  598. if ($id > 0) {
  599. $sql = "UPDATE $tbl_wiki SET id = iid WHERE iid = $id";
  600. Database::query($sql);
  601. //insert into item_property
  602. api_item_property_update(
  603. api_get_course_info(),
  604. TOOL_WIKI,
  605. $id,
  606. 'WikiAdded',
  607. api_get_user_id(),
  608. $groupId
  609. );
  610. $sql = 'UPDATE '.$tbl_wiki.' SET page_id="'.$id.'"
  611. WHERE c_id = '.$course_id.' AND id = "'.$id.'"';
  612. Database::query($sql);
  613. // insert wiki config
  614. $params = [
  615. 'c_id' => $course_id,
  616. 'page_id' => $id,
  617. 'task' => $_clean['task'],
  618. 'feedback1' => $_clean['feedback1'],
  619. 'feedback2' => $_clean['feedback2'],
  620. 'feedback3' => $_clean['feedback3'],
  621. 'fprogress1' => $_clean['fprogress1'],
  622. 'fprogress2' => $_clean['fprogress2'],
  623. 'fprogress3' => $_clean['fprogress3'],
  624. 'max_text' => $_clean['max_text'],
  625. 'max_version' => $_clean['max_version'],
  626. 'startdate_assig' => $_clean['startdate_assig'],
  627. 'enddate_assig' => $_clean['enddate_assig'],
  628. 'delayedsubmit' => $_clean['delayedsubmit']
  629. ];
  630. Database::insert($tbl_wiki_conf, $params);
  631. $this->setWikiData($id);
  632. self::check_emailcue(0, 'A');
  633. return get_lang('NewWikiSaved');
  634. }
  635. }
  636. }
  637. }
  638. /**
  639. * @param FormValidator $form
  640. * @param array $row
  641. */
  642. public function setForm($form, $row = array())
  643. {
  644. $toolBar = api_is_allowed_to_edit(null,true)
  645. ? array('ToolbarSet' => 'Wiki', 'Width' => '100%', 'Height' => '400')
  646. : array('ToolbarSet' => 'WikiStudent', 'Width' => '100%', 'Height' => '400', 'UserStatus' => 'student');
  647. $form->addHtmlEditor('content', get_lang('Content'), false, false, $toolBar);
  648. //$content
  649. $form->addElement('text', 'comment', get_lang('Comments'));
  650. $progress = array('', 10, 20, 30, 40, 50, 60, 70, 80, 90, 100);
  651. $form->addElement('select', 'progress', get_lang('Progress'), $progress);
  652. if ((api_is_allowed_to_edit(false,true) ||
  653. api_is_platform_admin()) &&
  654. isset($row['reflink']) && $row['reflink'] != 'index'
  655. ) {
  656. $form->addElement('advanced_settings', 'advanced_params', get_lang('AdvancedParameters'));
  657. $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
  658. $form->addHtmlEditor(
  659. 'task',
  660. get_lang('DescriptionOfTheTask'),
  661. false,
  662. false,
  663. array(
  664. 'ToolbarSet' => 'wiki_task',
  665. 'Width' => '100%',
  666. 'Height' => '200',
  667. )
  668. );
  669. $form->addElement('label', null, get_lang('AddFeedback'));
  670. $form->addElement('textarea', 'feedback1', get_lang('Feedback1'));
  671. $form->addElement('select', 'fprogress1', get_lang('FProgress'), $progress);
  672. $form->addElement('textarea', 'feedback2', get_lang('Feedback2'));
  673. $form->addElement('select', 'fprogress2', get_lang('FProgress'), $progress);
  674. $form->addElement('textarea', 'feedback3', get_lang('Feedback3'));
  675. $form->addElement('select', 'fprogress3', get_lang('FProgress'), $progress);
  676. $form->addElement('checkbox', 'initstartdate', null, get_lang('StartDate'), array('id' => 'start_date_toggle'));
  677. $style = "display:block";
  678. $row['initstartdate'] = 1;
  679. if (empty($row['startdate_assig'])) {
  680. $style = "display:none";
  681. $row['initstartdate'] = null;
  682. }
  683. $form->addElement('html', '<div id="start_date" style="'.$style.'">');
  684. $form->addDatePicker('startdate_assig', '');
  685. $form->addElement('html', '</div>');
  686. $form->addElement('checkbox', 'initenddate', null, get_lang('EndDate'), array('id' => 'end_date_toggle'));
  687. $style = "display:block";
  688. $row['initenddate'] = 1;
  689. if (empty($row['enddate_assig'])) {
  690. $style = "display:none";
  691. $row['initenddate'] = null;
  692. }
  693. $form->addElement('html', '<div id="end_date" style="'.$style.'">');
  694. $form->addDatePicker('enddate_assig', '');
  695. $form->addElement('html', '</div>');
  696. $form->addElement('checkbox', 'delayedsubmit', null, get_lang('AllowLaterSends'));
  697. $form->addElement('text', 'max_text', get_lang('NMaxWords'));
  698. $form->addElement('text', 'max_version', get_lang('NMaxVersion'));
  699. $form->addElement('checkbox', 'assignment', null, get_lang('CreateAssignmentPage'));
  700. $form->addElement('html', '</div>');
  701. }
  702. $form->addElement('hidden', 'page_id');
  703. $form->addElement('hidden', 'reflink');
  704. // $form->addElement('hidden', 'assignment');
  705. $form->addElement('hidden', 'version');
  706. $form->addElement('hidden', 'wpost_id', api_get_unique_id());
  707. }
  708. /**
  709. * This function displays the form for adding a new wiki page.
  710. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  711. * @return html code
  712. **/
  713. public function display_new_wiki_form()
  714. {
  715. $url = api_get_self().'?'.api_get_cidreq().'&action=addnew&group_id='.api_get_group_id();
  716. $form = new FormValidator('wiki_new', 'post', $url);
  717. $form->addElement('text', 'title', get_lang('Title'));
  718. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  719. self::setForm($form);
  720. $form->addElement('button', 'SaveWikiNew', get_lang('Save'));
  721. $form->display();
  722. if ($form->validate()) {
  723. $values = $form->exportValues();
  724. if (isset($values['startdate_assig']) &&
  725. isset($values['enddate_assig']) &&
  726. strtotime($values['startdate_assig']) > strtotime($values['enddate_assig'])
  727. ) {
  728. Display::addFlash(
  729. Display::display_error_message(
  730. get_lang("EndDateCannotBeBeforeTheStartDate"),
  731. false,
  732. true
  733. )
  734. );
  735. } elseif (!self::double_post($_POST['wpost_id'])) {
  736. //double post
  737. } else {
  738. if (isset($values['assignment']) && $values['assignment'] == 1) {
  739. self::auto_add_page_users($values);
  740. }
  741. $return_message = self::save_new_wiki($values);
  742. if ($return_message == false) {
  743. Display::addFlash(Display::display_error_message(get_lang('NoWikiPageTitle'), false, true));
  744. } else {
  745. Display::addFlash(Display::display_confirmation_message($return_message, false, true));
  746. }
  747. $wikiData = self::getWikiData();
  748. $redirectUrl = $this->url.'&action=showpage&title='.$wikiData['reflink'].'&'.api_get_cidreq();
  749. header('Location: '.$redirectUrl);
  750. exit;
  751. }
  752. }
  753. }
  754. /**
  755. * This function displays a wiki entry
  756. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  757. * @author Juan Carlos Raña Trabado
  758. * @param string $newtitle
  759. * @return string html code
  760. **/
  761. public function display_wiki_entry($newtitle)
  762. {
  763. $tbl_wiki = $this->tbl_wiki;
  764. $tbl_wiki_conf = $this->tbl_wiki_conf;
  765. $condition_session = $this->condition_session;
  766. $groupfilter = $this->groupfilter;
  767. $page = $this->page;
  768. $session_id = api_get_session_id();
  769. $course_id = api_get_course_int_id();
  770. if ($newtitle) {
  771. $pageMIX = $newtitle; //display the page after it is created
  772. } else {
  773. $pageMIX = $page;//display current page
  774. }
  775. $filter = null;
  776. if (isset($_GET['view']) && $_GET['view']) {
  777. $_clean['view'] = Database::escape_string($_GET['view']);
  778. $filter =' AND w.id="'.$_clean['view'].'"';
  779. }
  780. // First, check page visibility in the first page version
  781. $sql = 'SELECT * FROM '.$tbl_wiki.'
  782. WHERE
  783. c_id = '.$course_id.' AND
  784. reflink="'.Database::escape_string($pageMIX).'" AND
  785. '.$groupfilter.$condition_session.'
  786. ORDER BY id ASC';
  787. $result = Database::query($sql);
  788. $row = Database::fetch_array($result, 'ASSOC');
  789. $KeyVisibility = $row['visibility'];
  790. // second, show the last version
  791. $sql = 'SELECT * FROM '.$tbl_wiki.' w
  792. INNER JOIN '.$tbl_wiki_conf.' wc
  793. ON (wc.page_id = w.page_id AND wc.c_id = w.c_id)
  794. WHERE
  795. w.c_id = '.$course_id.' AND
  796. w.reflink = "'.Database::escape_string($pageMIX).'" AND
  797. w.session_id = '.$session_id.' AND
  798. w.'.$groupfilter.' '.$filter.'
  799. ORDER BY id DESC';
  800. $result = Database::query($sql);
  801. // we do not need a while loop since we are always displaying the last version
  802. $row = Database::fetch_array($result, 'ASSOC');
  803. //log users access to wiki (page_id)
  804. if (!empty($row['page_id'])) {
  805. Event::addEvent(LOG_WIKI_ACCESS, LOG_WIKI_PAGE_ID, $row['page_id']);
  806. }
  807. //update visits
  808. if ($row['id']) {
  809. $sql = 'UPDATE '.$tbl_wiki.' SET hits=(hits+1)
  810. WHERE c_id = '.$course_id.' AND id='.$row['id'].'';
  811. Database::query($sql);
  812. }
  813. // if both are empty and we are displaying the index page then we display the default text.
  814. if ($row['content']=='' && $row['title']=='' && $page=='index') {
  815. if (api_is_allowed_to_edit(false, true) ||
  816. api_is_platform_admin() ||
  817. GroupManager::is_user_in_group(api_get_user_id(), api_get_group_id()) ||
  818. api_is_allowed_in_course()
  819. ) {
  820. //Table structure for better export to pdf
  821. $default_table_for_content_Start='<table align="center" border="0"><tr><td align="center">';
  822. $default_table_for_content_End='</td></tr></table>';
  823. $content = $default_table_for_content_Start.
  824. sprintf(get_lang('DefaultContent'), api_get_path(WEB_IMG_PATH)).
  825. $default_table_for_content_End;
  826. $title = get_lang('DefaultTitle');
  827. } else {
  828. return Display::addFlash(Display::display_normal_message(get_lang('WikiStandBy'), false, true));
  829. }
  830. } else {
  831. $content = Security::remove_XSS($row['content']);
  832. $title = Security::remove_XSS($row['title']);
  833. }
  834. //assignment mode: identify page type
  835. $icon_assignment = null;
  836. if ($row['assignment']==1) {
  837. $icon_assignment = Display::return_icon('wiki_assignment.png', get_lang('AssignmentDescExtra'),'',ICON_SIZE_SMALL);
  838. } elseif($row['assignment']==2) {
  839. $icon_assignment = Display::return_icon('wiki_work.png', get_lang('AssignmentWork'),'',ICON_SIZE_SMALL);
  840. }
  841. //task mode
  842. $icon_task = null;
  843. if (!empty($row['task'])) {
  844. $icon_task=Display::return_icon('wiki_task.png', get_lang('StandardTask'),'',ICON_SIZE_SMALL);
  845. }
  846. // Show page. Show page to all users if isn't hide page. Mode assignments: if student is the author, can view
  847. if ($KeyVisibility == "1" ||
  848. api_is_allowed_to_edit(false, true) ||
  849. api_is_platform_admin() ||
  850. ($row['assignment'] == 2 && $KeyVisibility=="0" && (api_get_user_id() == $row['user_id'])) ||
  851. api_is_allowed_in_course()
  852. ) {
  853. $actionsLeft = '';
  854. // menu edit page
  855. $editLink = '<a href="index.php?'.api_get_cidreq().'&action=edit&title='.api_htmlentities(urlencode($page)).'"'.self::is_active_navigation_tab('edit').'>'.
  856. Display::return_icon('edit.png', get_lang('EditThisPage'), '', ICON_SIZE_MEDIUM).'</a>';
  857. if (api_is_allowed_to_edit(false, true)) {
  858. $actionsLeft .= $editLink;
  859. } else {
  860. if ((api_is_allowed_in_course() ||
  861. GroupManager::is_user_in_group(api_get_user_id(), api_get_group_id()))
  862. ) {
  863. $actionsLeft .= $editLink;
  864. } else {
  865. $actionsLeft .= '';
  866. }
  867. }
  868. $actionsRight = '';
  869. $protect_page = null;
  870. $lock_unlock_protect = null;
  871. // page action: protecting (locking) the page
  872. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  873. if (self::check_protect_page()==1) {
  874. $protect_page = Display::return_icon('lock.png', get_lang('PageLockedExtra'), '', ICON_SIZE_MEDIUM);
  875. $lock_unlock_protect='unlock';
  876. } else {
  877. $protect_page = Display::return_icon('unlock.png', get_lang('PageUnlockedExtra'), '', ICON_SIZE_MEDIUM);
  878. $lock_unlock_protect='lock';
  879. }
  880. }
  881. if ($row['id']) {
  882. $actionsRight .= '<a href="index.php?'.api_get_cidreq().'&action=showpage&actionpage='.$lock_unlock_protect.'&title='.api_htmlentities(urlencode($page)).'">'.
  883. $protect_page.'</a>';
  884. }
  885. $visibility_page = null;
  886. $lock_unlock_visibility = null;
  887. //page action: visibility
  888. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  889. if (self::check_visibility_page() == 1) {
  890. $visibility_page = Display::return_icon('visible.png', get_lang('ShowPageExtra'),'', ICON_SIZE_MEDIUM);
  891. $lock_unlock_visibility = 'invisible';
  892. } else {
  893. $visibility_page = Display::return_icon('invisible.png', get_lang('HidePageExtra'),'', ICON_SIZE_MEDIUM);
  894. $lock_unlock_visibility = 'visible';
  895. }
  896. }
  897. if ($row['id']) {
  898. $actionsRight .= '<a href="index.php?'.api_get_cidreq().'&action=showpage&actionpage='.$lock_unlock_visibility.'&title='.api_htmlentities(urlencode($page)).'">'.
  899. $visibility_page.'</a>';
  900. }
  901. //page action: notification
  902. if (api_is_allowed_to_session_edit()) {
  903. if (self::check_notify_page($page)==1) {
  904. $notify_page = Display::return_icon('messagebox_info.png', get_lang('NotifyByEmail'),'',ICON_SIZE_MEDIUM);
  905. $lock_unlock_notify_page = 'unlocknotify';
  906. } else {
  907. $notify_page = Display::return_icon('mail.png', get_lang('CancelNotifyByEmail'),'',ICON_SIZE_MEDIUM);
  908. $lock_unlock_notify_page = 'locknotify';
  909. }
  910. }
  911. // Only available if row['id'] is set
  912. if ($row['id']) {
  913. if (api_is_allowed_to_session_edit(false, true) && api_is_allowed_to_edit() ||
  914. GroupManager::is_user_in_group(api_get_user_id(), api_get_group_id())
  915. ) {
  916. // menu discuss page
  917. $actionsRight .= '<a href="index.php?'.api_get_cidreq().'&action=discuss&title='.api_htmlentities(urlencode($page)).'" '.self::is_active_navigation_tab('discuss').'>'.
  918. Display::return_icon('discuss.png',get_lang('DiscussThisPage'),'',ICON_SIZE_MEDIUM).'</a>';
  919. }
  920. //menu history
  921. $actionsRight .= '<a href="index.php?'.api_get_cidreq().'&action=history&title='.api_htmlentities(urlencode($page)).'" '.self::is_active_navigation_tab('history').'>'.
  922. Display::return_icon('history.png',get_lang('ShowPageHistory'),'',ICON_SIZE_MEDIUM).'</a>';
  923. //menu linkspages
  924. $actionsRight .= '<a href="index.php?'.api_get_cidreq().'action=links&title='.api_htmlentities(urlencode($page)).'" '.self::is_active_navigation_tab('links').'>'.
  925. Display::return_icon('what_link_here.png',get_lang('LinksPages'),'',ICON_SIZE_MEDIUM).'</a>';
  926. //menu delete wikipage
  927. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  928. $actionsRight .= '<a href="index.php?action=delete&'.api_get_cidreq().'&title='.api_htmlentities(urlencode($page)).'"'.self::is_active_navigation_tab('delete').'>'.
  929. Display::return_icon('delete.png',get_lang('DeleteThisPage'),'',ICON_SIZE_MEDIUM).'</a>';
  930. }
  931. $actionsRight .= '<a href="index.php?'.api_get_cidreq().'&action=showpage&actionpage='.$lock_unlock_notify_page.'&title='.api_htmlentities(urlencode($page)).'">'.
  932. $notify_page.'</a>';
  933. // Page action: copy last version to doc area
  934. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  935. $actionsRight .= '<a href="index.php?'.api_get_cidreq().'&action=export2doc&wiki_id='.$row['id'].'">'.
  936. Display::return_icon('export_to_documents.png', get_lang('ExportToDocArea'), '', ICON_SIZE_MEDIUM).'</a>';
  937. }
  938. $actionsRight .= '<a href="index.php?'.api_get_cidreq().'&action=export_to_pdf&wiki_id='.$row['id'].'">'.
  939. Display::return_icon('pdf.png',get_lang('ExportToPDF'),'',ICON_SIZE_MEDIUM).'</a>';
  940. $unoconv = api_get_configuration_value('unoconv.binaries');
  941. if ($unoconv) {
  942. $actionsRight .= '<a href="'.api_get_path(WEB_CODE_PATH).'wiki/index.php?action=export_to_doc_file&id='.$row['id'].'&'.api_get_cidreq().'">'.
  943. Display::return_icon('export_doc.png', get_lang('ExportToDoc'), array(), ICON_SIZE_MEDIUM).'</a>';
  944. }
  945. //export to print
  946. ?>
  947. <script>
  948. function goprint() {
  949. var a = window.open('','','width=800,height=600');
  950. a.document.open("text/html");
  951. a.document.write(document.getElementById('wikicontent').innerHTML);
  952. a.document.close();
  953. a.print();
  954. }
  955. </script>
  956. <?php
  957. $actionsRight .= Display::url(
  958. Display::return_icon('printer.png', get_lang('Print'), '', ICON_SIZE_MEDIUM),
  959. '#',
  960. array('onclick' => "javascript: goprint();")
  961. );
  962. }
  963. echo Display::toolbarAction('toolbar-wikistudent', [$actionsLeft, $actionsRight]);
  964. if (empty($title)) {
  965. $pageTitle = get_lang('DefaultTitle');
  966. }
  967. if (self::wiki_exist($title)) {
  968. $pageTitle = $icon_assignment.'&nbsp;'.$icon_task.'&nbsp;'.api_htmlentities($title);
  969. } else {
  970. $pageTitle = api_htmlentities($title);
  971. }
  972. $pageWiki = self::make_wiki_link_clickable(
  973. self::detect_external_link(
  974. self::detect_anchor_link(
  975. self::detect_mail_link(
  976. self::detect_ftp_link(
  977. self::detect_irc_link(
  978. self::detect_news_link($content)
  979. )
  980. )
  981. )
  982. )
  983. )
  984. );
  985. $footerWiki = '<div id="wikifooter">'.get_lang('Progress').': '.($row['progress']*10).'%&nbsp;&nbsp;&nbsp;'.get_lang('Rating').': '.$row['score'].'&nbsp;&nbsp;&nbsp;'.get_lang('Words').': '.self::word_count($content).'</div>';
  986. echo Display::panel($pageWiki, $pageTitle, $footerWiki);
  987. } //end filter visibility
  988. }
  989. /**
  990. * This function counted the words in a document. Thanks Adeel Khan
  991. * @param string Document's text
  992. * @return int Number of words
  993. */
  994. public function word_count($document)
  995. {
  996. $search = array(
  997. '@<script[^>]*?>.*?</script>@si',
  998. '@<style[^>]*?>.*?</style>@siU',
  999. '@<div id="player.[^>]*?>.*?</div>@',
  1000. '@<![\s\S]*?--[ \t\n\r]*>@'
  1001. );
  1002. $document = preg_replace($search, '', $document);
  1003. # strip all html tags
  1004. $wc = strip_tags($document);
  1005. $wc = html_entity_decode($wc, ENT_NOQUOTES, 'UTF-8');// TODO:test also old html_entity_decode(utf8_encode($wc))
  1006. # remove 'words' that don't consist of alphanumerical characters or punctuation. And fix accents and some letters
  1007. $pattern = "#[^(\w|\d|\'|\"|\.|\!|\?|;|,|\\|\/|\-|:|\&|@|á|é|í|ó|ú|à|è|ì|ò|ù|ä|ë|ï|ö|ü|Á|É|Í|Ó|Ú|À|È|Ò|Ù|Ä|Ë|Ï|Ö|Ü|â|ê|î|ô|û|Â|Ê|Î|Ô|Û|ñ|Ñ|ç|Ç)]+#";
  1008. $wc = trim(preg_replace($pattern, " ", $wc));
  1009. # remove one-letter 'words' that consist only of punctuation
  1010. $wc = trim(preg_replace("#\s*[(\'|\"|\.|\!|\?|;|,|\\|\/|\-|:|\&|@)]\s*#", " ", $wc));
  1011. # remove superfluous whitespace
  1012. $wc = preg_replace("/\s\s+/", " ", $wc);
  1013. # split string into an array of words
  1014. $wc = explode(" ", $wc);
  1015. # remove empty elements
  1016. $wc = array_filter($wc);
  1017. # return the number of words
  1018. return count($wc);
  1019. }
  1020. /**
  1021. * This function checks if wiki title exist
  1022. */
  1023. public function wiki_exist($title)
  1024. {
  1025. $tbl_wiki = $this->tbl_wiki;
  1026. $groupfilter = $this->groupfilter;
  1027. $condition_session = $this->condition_session;
  1028. $course_id = api_get_course_int_id();
  1029. $sql='SELECT id FROM '.$tbl_wiki.'
  1030. WHERE
  1031. c_id = '.$course_id.' AND
  1032. title="'.Database::escape_string($title).'" AND
  1033. '.$groupfilter.$condition_session.'
  1034. ORDER BY id ASC';
  1035. $result=Database::query($sql);
  1036. $cant=Database::num_rows($result);
  1037. if ($cant>0) {
  1038. return true;
  1039. } else {
  1040. return false;
  1041. }
  1042. }
  1043. /**
  1044. * Checks if this navigation tab has to be set to active
  1045. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  1046. *
  1047. * @return string html code
  1048. */
  1049. public function is_active_navigation_tab($paramwk)
  1050. {
  1051. if (isset($_GET['action']) && $_GET['action'] == $paramwk) {
  1052. return ' class="active"';
  1053. }
  1054. }
  1055. /**
  1056. * Lock add pages
  1057. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1058. * return current database status of protect page and change it if get action
  1059. */
  1060. public function check_addnewpagelock()
  1061. {
  1062. $tbl_wiki = $this->tbl_wiki;
  1063. $condition_session = $this->condition_session;
  1064. $groupfilter = $this->groupfilter;
  1065. $course_id = api_get_course_int_id();
  1066. $sql = 'SELECT *
  1067. FROM '.$tbl_wiki.'
  1068. WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.'
  1069. ORDER BY id ASC';
  1070. $result = Database::query($sql);
  1071. $row = Database::fetch_array($result);
  1072. $status_addlock = $row['addlock'];
  1073. // Change status
  1074. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  1075. if (isset($_GET['actionpage'])) {
  1076. if ($_GET['actionpage'] == 'lockaddnew' && $status_addlock == 1) {
  1077. $status_addlock = 0;
  1078. }
  1079. if ($_GET['actionpage'] == 'unlockaddnew' && $status_addlock == 0) {
  1080. $status_addlock = 1;
  1081. }
  1082. $sql = 'UPDATE '.$tbl_wiki.' SET
  1083. addlock="'.Database::escape_string($status_addlock).'"
  1084. WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session;
  1085. Database::query($sql);
  1086. }
  1087. $sql = 'SELECT *
  1088. FROM '.$tbl_wiki.'
  1089. WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.'
  1090. ORDER BY id ASC';
  1091. $result = Database::query($sql);
  1092. $row = Database::fetch_array($result);
  1093. }
  1094. return $row['addlock'];
  1095. }
  1096. /**
  1097. * Protect page
  1098. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1099. * return current database status of protect page and change it if get action
  1100. */
  1101. public function check_protect_page()
  1102. {
  1103. $tbl_wiki = $this->tbl_wiki;
  1104. $condition_session = $this->condition_session;
  1105. $groupfilter = $this->groupfilter;
  1106. $page = $this->page;
  1107. $course_id = api_get_course_int_id();
  1108. $sql='SELECT * FROM '.$tbl_wiki.'
  1109. WHERE
  1110. c_id = '.$course_id.' AND
  1111. reflink="'.Database::escape_string($page).'" AND
  1112. '.$groupfilter.$condition_session.'
  1113. ORDER BY id ASC';
  1114. $result=Database::query($sql);
  1115. $row=Database::fetch_array($result);
  1116. $status_editlock = $row['editlock'];
  1117. $id = $row['page_id'];
  1118. ///change status
  1119. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  1120. if (isset($_GET['actionpage']) && $_GET['actionpage'] == 'lock' && $status_editlock == 0) {
  1121. $status_editlock = 1;
  1122. }
  1123. if (isset($_GET['actionpage']) && $_GET['actionpage'] == 'unlock' && $status_editlock == 1) {
  1124. $status_editlock = 0;
  1125. }
  1126. $sql = 'UPDATE '.$tbl_wiki.' SET editlock="'.Database::escape_string($status_editlock).'"
  1127. WHERE c_id = '.$course_id.' AND page_id="'.$id.'"';
  1128. Database::query($sql);
  1129. $sql='SELECT * FROM '.$tbl_wiki.'
  1130. WHERE
  1131. c_id = '.$course_id.' AND
  1132. reflink="'.Database::escape_string($page).'" AND
  1133. '.$groupfilter.$condition_session.'
  1134. ORDER BY id ASC';
  1135. $result=Database::query($sql);
  1136. $row = Database::fetch_array($result);
  1137. }
  1138. //show status
  1139. return $row['editlock'];
  1140. }
  1141. /**
  1142. * Visibility page
  1143. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1144. * return current database status of visibility and change it if get action
  1145. */
  1146. public function check_visibility_page()
  1147. {
  1148. $tbl_wiki = $this->tbl_wiki;
  1149. $page = $this->page;
  1150. $condition_session = $this->condition_session;
  1151. $groupfilter = $this->groupfilter;
  1152. $course_id = api_get_course_int_id();
  1153. $sql = 'SELECT * FROM '.$tbl_wiki.'
  1154. WHERE
  1155. c_id = '.$course_id.' AND
  1156. reflink="'.Database::escape_string($page).'" AND
  1157. '.$groupfilter.$condition_session.'
  1158. ORDER BY id ASC';
  1159. $result = Database::query($sql);
  1160. $row = Database::fetch_array($result);
  1161. $status_visibility = $row['visibility'];
  1162. //change status
  1163. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  1164. if (isset($_GET['actionpage']) && $_GET['actionpage']=='visible' && $status_visibility==0) {
  1165. $status_visibility=1;
  1166. }
  1167. if (isset($_GET['actionpage']) && $_GET['actionpage']=='invisible' && $status_visibility==1) {
  1168. $status_visibility=0;
  1169. }
  1170. $sql = 'UPDATE '.$tbl_wiki.' SET visibility="'.Database::escape_string($status_visibility).'"
  1171. WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string($page).'" AND '.$groupfilter.$condition_session;
  1172. Database::query($sql);
  1173. // Although the value now is assigned to all (not only the first),
  1174. // these three lines remain necessary.
  1175. // They do that by changing the page state is
  1176. // made when you press the button and not have to wait to change his page
  1177. $sql = 'SELECT * FROM '.$tbl_wiki.'
  1178. WHERE
  1179. c_id = '.$course_id.' AND
  1180. reflink="'.Database::escape_string($page).'" AND
  1181. '.$groupfilter.$condition_session.'
  1182. ORDER BY id ASC';
  1183. $result=Database::query($sql);
  1184. $row = Database::fetch_array($result);
  1185. }
  1186. if (empty($row['id'])) {
  1187. $row['visibility']= 1;
  1188. }
  1189. //show status
  1190. return $row['visibility'];
  1191. }
  1192. /**
  1193. * Visibility discussion
  1194. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1195. * @return int current database status of discuss visibility and change it if get action page
  1196. */
  1197. public function check_visibility_discuss()
  1198. {
  1199. $tbl_wiki = $this->tbl_wiki;
  1200. $page = $this->page;
  1201. $condition_session = $this->condition_session;
  1202. $groupfilter = $this->groupfilter;
  1203. $course_id = api_get_course_int_id();
  1204. $sql = 'SELECT * FROM '.$tbl_wiki.'
  1205. WHERE
  1206. c_id = '.$course_id.' AND
  1207. reflink="'.Database::escape_string($page).'" AND
  1208. '.$groupfilter.$condition_session.'
  1209. ORDER BY id ASC';
  1210. $result=Database::query($sql);
  1211. $row=Database::fetch_array($result);
  1212. $status_visibility_disc = $row['visibility_disc'];
  1213. //change status
  1214. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  1215. if (isset($_GET['actionpage']) && $_GET['actionpage'] =='showdisc' && $status_visibility_disc==0) {
  1216. $status_visibility_disc=1;
  1217. }
  1218. if (isset($_GET['actionpage']) && $_GET['actionpage'] =='hidedisc' && $status_visibility_disc==1) {
  1219. $status_visibility_disc=0;
  1220. }
  1221. $sql = 'UPDATE '.$tbl_wiki.' SET visibility_disc="'.Database::escape_string($status_visibility_disc).'"
  1222. WHERE
  1223. c_id = '.$course_id.' AND
  1224. reflink="'.Database::escape_string($page).'" AND
  1225. '.$groupfilter.$condition_session;
  1226. Database::query($sql);
  1227. // Although the value now is assigned to all (not only the first),
  1228. // these three lines remain necessary.
  1229. // They do that by changing the page state is made when you press
  1230. // the button and not have to wait to change his page
  1231. $sql = 'SELECT * FROM '.$tbl_wiki.'
  1232. WHERE
  1233. c_id = '.$course_id.' AND
  1234. reflink="'.Database::escape_string($page).'" AND
  1235. '.$groupfilter.$condition_session.'
  1236. ORDER BY id ASC';
  1237. $result = Database::query($sql);
  1238. $row = Database::fetch_array($result);
  1239. }
  1240. return $row['visibility_disc'];
  1241. }
  1242. /**
  1243. * Lock add discussion
  1244. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1245. * @return int current database status of lock dicuss and change if get action
  1246. */
  1247. public function check_addlock_discuss()
  1248. {
  1249. $tbl_wiki = $this->tbl_wiki;
  1250. $page = $this->page;
  1251. $condition_session = $this->condition_session;
  1252. $groupfilter = $this->groupfilter;
  1253. $course_id = api_get_course_int_id();
  1254. $sql = 'SELECT * FROM '.$tbl_wiki.'
  1255. WHERE
  1256. c_id = '.$course_id.' AND
  1257. reflink="'.Database::escape_string($page).'" AND
  1258. '.$groupfilter.$condition_session.'
  1259. ORDER BY id ASC';
  1260. $result = Database::query($sql);
  1261. $row = Database::fetch_array($result);
  1262. $status_addlock_disc=$row['addlock_disc'];
  1263. //change status
  1264. if (api_is_allowed_to_edit() || api_is_platform_admin()) {
  1265. if (isset($_GET['actionpage']) && $_GET['actionpage'] =='lockdisc' && $status_addlock_disc==0) {
  1266. $status_addlock_disc = 1;
  1267. }
  1268. if (isset($_GET['actionpage']) && $_GET['actionpage'] =='unlockdisc' && $status_addlock_disc==1) {
  1269. $status_addlock_disc = 0;
  1270. }
  1271. $sql = 'UPDATE '.$tbl_wiki.' SET
  1272. addlock_disc="'.Database::escape_string($status_addlock_disc).'"
  1273. WHERE
  1274. c_id = '.$course_id.' AND
  1275. reflink="'.Database::escape_string($page).'" AND
  1276. '.$groupfilter.$condition_session;
  1277. Database::query($sql);
  1278. // Although the value now is assigned to all (not only the first),
  1279. // these three lines remain necessary.
  1280. // They do that by changing the page state is made when you press
  1281. // the button and not have to wait to change his page
  1282. $sql = 'SELECT * FROM '.$tbl_wiki.'
  1283. WHERE
  1284. c_id = '.$course_id.' AND
  1285. reflink="'.Database::escape_string($page).'" AND
  1286. '.$groupfilter.$condition_session.'
  1287. ORDER BY id ASC';
  1288. $result=Database::query($sql);
  1289. $row=Database::fetch_array($result);
  1290. }
  1291. return $row['addlock_disc'];
  1292. }
  1293. /**
  1294. * Lock rating discussion
  1295. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1296. * @return int current database status of rating discuss and change it if get action
  1297. */
  1298. public function check_ratinglock_discuss()
  1299. {
  1300. $tbl_wiki = $this->tbl_wiki;
  1301. $page = $this->page;
  1302. $condition_session = $this->condition_session;
  1303. $groupfilter = $this->groupfilter;
  1304. $course_id = api_get_course_int_id();
  1305. $sql = 'SELECT * FROM '.$tbl_wiki.'
  1306. WHERE
  1307. c_id = '.$course_id.' AND
  1308. reflink="'.Database::escape_string($page).'" AND
  1309. '.$groupfilter.$condition_session.'
  1310. ORDER BY id ASC';
  1311. $result=Database::query($sql);
  1312. $row=Database::fetch_array($result);
  1313. $status_ratinglock_disc=$row['ratinglock_disc'];
  1314. //change status
  1315. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  1316. if (isset($_GET['actionpage']) && $_GET['actionpage'] =='lockrating' && $status_ratinglock_disc==0) {
  1317. $status_ratinglock_disc=1;
  1318. }
  1319. if (isset($_GET['actionpage']) && $_GET['actionpage'] =='unlockrating' && $status_ratinglock_disc==1) {
  1320. $status_ratinglock_disc=0;
  1321. }
  1322. $sql = 'UPDATE '.$tbl_wiki.'
  1323. SET ratinglock_disc="'.Database::escape_string($status_ratinglock_disc).'"
  1324. WHERE
  1325. c_id = '.$course_id.' AND
  1326. reflink="'.Database::escape_string($page).'" AND
  1327. '.$groupfilter.$condition_session;
  1328. //Visibility. Value to all,not only for the first
  1329. Database::query($sql);
  1330. // Although the value now is assigned to all (not only the first),
  1331. // these three lines remain necessary. They do that by changing the
  1332. // page state is made when you press the button and not have to wait
  1333. // to change his page
  1334. $sql='SELECT * FROM '.$tbl_wiki.'
  1335. WHERE
  1336. c_id = '.$course_id.' AND
  1337. reflink="'.Database::escape_string($page).'" AND
  1338. '.$groupfilter.$condition_session.'
  1339. ORDER BY id ASC';
  1340. $result=Database::query($sql);
  1341. $row=Database::fetch_array($result);
  1342. }
  1343. return $row['ratinglock_disc'];
  1344. }
  1345. /**
  1346. * Notify page changes
  1347. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1348. * @return int the current notification status
  1349. */
  1350. public function check_notify_page($reflink)
  1351. {
  1352. $tbl_wiki = $this->tbl_wiki;
  1353. $tbl_wiki_mailcue = $this->tbl_wiki_mailcue;
  1354. $condition_session = $this->condition_session;
  1355. $groupfilter = $this->groupfilter;
  1356. $groupId = api_get_group_id();
  1357. $session_id = api_get_session_id();
  1358. $course_id = api_get_course_int_id();
  1359. $userId = api_get_user_id();
  1360. $sql = 'SELECT * FROM '.$tbl_wiki.'
  1361. WHERE c_id = '.$course_id.' AND reflink="'.$reflink.'" AND '.$groupfilter.$condition_session.'
  1362. ORDER BY id ASC';
  1363. $result=Database::query($sql);
  1364. $row=Database::fetch_array($result);
  1365. $id = $row['id'];
  1366. $sql='SELECT * FROM '.$tbl_wiki_mailcue.'
  1367. WHERE c_id = '.$course_id.' AND id="'.$id.'" AND user_id="'.api_get_user_id().'" AND type="P"';
  1368. $result=Database::query($sql);
  1369. $row=Database::fetch_array($result);
  1370. $idm = $row['id'];
  1371. if (empty($idm)) {
  1372. $status_notify=0;
  1373. } else {
  1374. $status_notify=1;
  1375. }
  1376. // Change status
  1377. if (isset($_GET['actionpage']) && $_GET['actionpage'] =='locknotify' && $status_notify==0) {
  1378. $sql = "SELECT id FROM $tbl_wiki_mailcue
  1379. WHERE c_id = $course_id AND id = $id AND user_id = $userId";
  1380. $result = Database::query($sql);
  1381. $exist = false;
  1382. if (Database::num_rows($result)) {
  1383. $exist = true;
  1384. }
  1385. if ($exist == false) {
  1386. $sql="INSERT INTO ".$tbl_wiki_mailcue." (c_id, id, user_id, type, group_id, session_id) VALUES
  1387. ($course_id, '".$id."','".api_get_user_id()."','P','".$groupId."','".$session_id."')";
  1388. Database::query($sql);
  1389. }
  1390. $status_notify=1;
  1391. }
  1392. if (isset($_GET['actionpage']) && $_GET['actionpage'] =='unlocknotify' && $status_notify==1) {
  1393. $sql = 'DELETE FROM '.$tbl_wiki_mailcue.'
  1394. WHERE id="'.$id.'" AND user_id="'.api_get_user_id().'" AND type="P" AND c_id = '.$course_id;
  1395. Database::query($sql);
  1396. $status_notify=0;
  1397. }
  1398. return $status_notify;
  1399. }
  1400. /**
  1401. * Notify discussion changes
  1402. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1403. * @param string $reflink
  1404. * @return int current database status of rating discuss and change it if get action
  1405. */
  1406. public function check_notify_discuss($reflink)
  1407. {
  1408. $tbl_wiki_mailcue = $this->tbl_wiki_mailcue;
  1409. $tbl_wiki = $this->tbl_wiki;
  1410. $condition_session = $this->condition_session;
  1411. $groupfilter = $this->groupfilter;
  1412. $course_id = api_get_course_int_id();
  1413. $groupId = api_get_group_id();
  1414. $session_id = api_get_session_id();
  1415. $sql = 'SELECT * FROM '.$tbl_wiki.'
  1416. WHERE c_id = '.$course_id.' AND reflink="'.$reflink.'" AND '.$groupfilter.$condition_session.'
  1417. ORDER BY id ASC';
  1418. $result=Database::query($sql);
  1419. $row=Database::fetch_array($result);
  1420. $id=$row['id'];
  1421. $sql = 'SELECT * FROM '.$tbl_wiki_mailcue.'
  1422. WHERE c_id = '.$course_id.' AND id="'.$id.'" AND user_id="'.api_get_user_id().'" AND type="D"';
  1423. $result = Database::query($sql);
  1424. $row = Database::fetch_array($result);
  1425. $idm = $row['id'];
  1426. if (empty($idm)) {
  1427. $status_notify_disc=0;
  1428. } else {
  1429. $status_notify_disc=1;
  1430. }
  1431. //change status
  1432. if (isset($_GET['actionpage']) && $_GET['actionpage'] =='locknotifydisc' && $status_notify_disc==0) {
  1433. $sql="INSERT INTO ".$tbl_wiki_mailcue." (c_id, id, user_id, type, group_id, session_id) VALUES
  1434. ($course_id, '".$id."','".api_get_user_id()."','D','".$groupId."','".$session_id."')";
  1435. Database::query($sql);
  1436. $status_notify_disc=1;
  1437. }
  1438. if (isset($_GET['actionpage']) && $_GET['actionpage'] =='unlocknotifydisc' && $status_notify_disc==1) {
  1439. $sql = 'DELETE FROM '.$tbl_wiki_mailcue.'
  1440. WHERE c_id = '.$course_id.' AND id="'.$id.'" AND user_id="'.api_get_user_id().'" AND type="D" AND c_id = '.$course_id;
  1441. Database::query($sql);
  1442. $status_notify_disc=0;
  1443. }
  1444. return $status_notify_disc;
  1445. }
  1446. /**
  1447. * Notify all changes
  1448. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1449. */
  1450. public function check_notify_all()
  1451. {
  1452. $tbl_wiki_mailcue = $this->tbl_wiki_mailcue;
  1453. $course_id = api_get_course_int_id();
  1454. $groupId = api_get_group_id();
  1455. $session_id=api_get_session_id();
  1456. $sql = 'SELECT * FROM '.$tbl_wiki_mailcue.'
  1457. WHERE
  1458. c_id = '.$course_id.' AND
  1459. user_id="'.api_get_user_id().'" AND
  1460. type="F" AND
  1461. group_id="'.$groupId.'" AND
  1462. session_id="'.$session_id.'"';
  1463. $result=Database::query($sql);
  1464. $row=Database::fetch_array($result);
  1465. $idm=$row['user_id'];
  1466. if (empty($idm)) {
  1467. $status_notify_all=0;
  1468. } else {
  1469. $status_notify_all=1;
  1470. }
  1471. //change status
  1472. if (isset($_GET['actionpage']) && $_GET['actionpage'] =='locknotifyall' && $status_notify_all==0) {
  1473. $sql="INSERT INTO ".$tbl_wiki_mailcue." (c_id, user_id, type, group_id, session_id) VALUES
  1474. ($course_id, '".api_get_user_id()."','F','".$groupId."','".$session_id."')";
  1475. Database::query($sql);
  1476. $status_notify_all=1;
  1477. }
  1478. if (isset($_GET['actionpage']) &&
  1479. isset($_GET['actionpage']) &&
  1480. $_GET['actionpage'] =='unlocknotifyall' &&
  1481. $status_notify_all == 1
  1482. ) {
  1483. $sql ='DELETE FROM '.$tbl_wiki_mailcue.'
  1484. WHERE
  1485. c_id = '.$course_id.' AND
  1486. user_id="'.api_get_user_id().'" AND
  1487. type="F" AND
  1488. group_id="'.$groupId.'" AND
  1489. session_id="'.$session_id.'" AND
  1490. c_id = '.$course_id;
  1491. Database::query($sql);
  1492. $status_notify_all=0;
  1493. }
  1494. //show status
  1495. return $status_notify_all;
  1496. }
  1497. /**
  1498. * Sends pending e-mails
  1499. */
  1500. public function check_emailcue($id_or_ref, $type, $lastime='', $lastuser='')
  1501. {
  1502. $tbl_wiki_mailcue = $this->tbl_wiki_mailcue;
  1503. $tbl_wiki = $this->tbl_wiki;
  1504. $condition_session = $this->condition_session;
  1505. $groupfilter = $this->groupfilter;
  1506. $_course = $this->courseInfo;
  1507. $groupId = api_get_group_id();
  1508. $session_id=api_get_session_id();
  1509. $course_id = api_get_course_int_id();
  1510. $group_properties = GroupManager :: get_group_properties($groupId);
  1511. $group_name = $group_properties['name'];
  1512. $allow_send_mail = false; //define the variable to below
  1513. $email_assignment = null;
  1514. if ($type=='P') {
  1515. //if modifying a wiki page
  1516. //first, current author and time
  1517. //Who is the author?
  1518. $userinfo = api_get_user_info($lastuser);
  1519. $email_user_author = get_lang('EditedBy').': '.$userinfo['complete_name'];
  1520. //When ?
  1521. $year = substr($lastime, 0, 4);
  1522. $month = substr($lastime, 5, 2);
  1523. $day = substr($lastime, 8, 2);
  1524. $hours=substr($lastime, 11,2);
  1525. $minutes=substr($lastime, 14,2);
  1526. $seconds=substr($lastime, 17,2);
  1527. $email_date_changes=$day.' '.$month.' '.$year.' '.$hours.":".$minutes.":".$seconds;
  1528. //second, extract data from first reg
  1529. $sql = 'SELECT * FROM '.$tbl_wiki.'
  1530. WHERE c_id = '.$course_id.' AND reflink="'.$id_or_ref.'" AND '.$groupfilter.$condition_session.'
  1531. ORDER BY id ASC';
  1532. $result=Database::query($sql);
  1533. $row=Database::fetch_array($result);
  1534. $id=$row['id'];
  1535. $email_page_name=$row['title'];
  1536. if ($row['visibility']==1) {
  1537. $allow_send_mail=true; //if visibility off - notify off
  1538. $sql = 'SELECT * FROM '.$tbl_wiki_mailcue.'
  1539. WHERE
  1540. c_id = '.$course_id.' AND
  1541. id="'.$id.'" AND
  1542. type="'.$type.'" OR
  1543. type="F" AND
  1544. group_id="'.$groupId.'" AND
  1545. session_id="'.$session_id.'"';
  1546. //type: P=page, D=discuss, F=full.
  1547. $result=Database::query($sql);
  1548. $emailtext=get_lang('EmailWikipageModified').' <strong>'.$email_page_name.'</strong> '.get_lang('Wiki');
  1549. }
  1550. } elseif ($type=='D') {
  1551. //if added a post to discuss
  1552. //first, current author and time
  1553. //Who is the author of last message?
  1554. $userinfo = api_get_user_info($lastuser);
  1555. $email_user_author = get_lang('AddedBy').': '.$userinfo['complete_name'];
  1556. //When ?
  1557. $year = substr($lastime, 0, 4);
  1558. $month = substr($lastime, 5, 2);
  1559. $day = substr($lastime, 8, 2);
  1560. $hours=substr($lastime, 11,2);
  1561. $minutes=substr($lastime, 14,2);
  1562. $seconds=substr($lastime, 17,2);
  1563. $email_date_changes=$day.' '.$month.' '.$year.' '.$hours.":".$minutes.":".$seconds;
  1564. //second, extract data from first reg
  1565. $id=$id_or_ref; //$id_or_ref is id from tblwiki
  1566. $sql = 'SELECT * FROM '.$tbl_wiki.'
  1567. WHERE c_id = '.$course_id.' AND id="'.$id.'"
  1568. ORDER BY id ASC';
  1569. $result=Database::query($sql);
  1570. $row=Database::fetch_array($result);
  1571. $email_page_name=$row['title'];
  1572. if ($row['visibility_disc']==1) {
  1573. $allow_send_mail=true; //if visibility off - notify off
  1574. $sql = 'SELECT * FROM '.$tbl_wiki_mailcue.'
  1575. WHERE
  1576. c_id = '.$course_id.' AND
  1577. id="'.$id.'" AND
  1578. type="'.$type.'" OR
  1579. type="F" AND
  1580. group_id="'.$groupId.'" AND
  1581. session_id="'.$session_id.'"';
  1582. //type: P=page, D=discuss, F=full
  1583. $result = Database::query($sql);
  1584. $emailtext=get_lang('EmailWikiPageDiscAdded').' <strong>'.$email_page_name.'</strong> '.get_lang('Wiki');
  1585. }
  1586. } elseif($type=='A') {
  1587. //for added pages
  1588. $id=0; //for tbl_wiki_mailcue
  1589. $sql = 'SELECT * FROM '.$tbl_wiki.'
  1590. WHERE c_id = '.$course_id.'
  1591. ORDER BY id DESC'; //the added is always the last
  1592. $result=Database::query($sql);
  1593. $row=Database::fetch_array($result);
  1594. $email_page_name=$row['title'];
  1595. //Who is the author?
  1596. $userinfo = api_get_user_info($row['user_id']);
  1597. $email_user_author= get_lang('AddedBy').': '.$userinfo['complete_name'];
  1598. //When ?
  1599. $year = substr($row['dtime'], 0, 4);
  1600. $month = substr($row['dtime'], 5, 2);
  1601. $day = substr($row['dtime'], 8, 2);
  1602. $hours = substr($row['dtime'], 11, 2);
  1603. $minutes = substr($row['dtime'], 14, 2);
  1604. $seconds = substr($row['dtime'], 17, 2);
  1605. $email_date_changes=$day.' '.$month.' '.$year.' '.$hours.":".$minutes.":".$seconds;
  1606. if($row['assignment']==0) {
  1607. $allow_send_mail=true;
  1608. } elseif($row['assignment']==1) {
  1609. $email_assignment=get_lang('AssignmentDescExtra').' ('.get_lang('AssignmentMode').')';
  1610. $allow_send_mail=true;
  1611. } elseif($row['assignment']==2) {
  1612. $allow_send_mail=false; //Mode tasks: avoids notifications to all users about all users
  1613. }
  1614. $sql = 'SELECT * FROM '.$tbl_wiki_mailcue.'
  1615. WHERE c_id = '.$course_id.' AND id="'.$id.'" AND type="F" AND group_id="'.$groupId.'" AND session_id="'.$session_id.'"';
  1616. //type: P=page, D=discuss, F=full
  1617. $result=Database::query($sql);
  1618. $emailtext = get_lang('EmailWikiPageAdded').' <strong>'.$email_page_name.'</strong> '.get_lang('In').' '. get_lang('Wiki');
  1619. } elseif ($type=='E') {
  1620. $id=0;
  1621. $allow_send_mail=true;
  1622. //Who is the author?
  1623. $userinfo = api_get_user_info(api_get_user_id()); //current user
  1624. $email_user_author = get_lang('DeletedBy').': '.$userinfo['complete_name'];
  1625. //When ?
  1626. $today = date('r'); //current time
  1627. $email_date_changes=$today;
  1628. $sql = 'SELECT * FROM '.$tbl_wiki_mailcue.'
  1629. WHERE
  1630. c_id = '.$course_id.' AND
  1631. id="'.$id.'" AND type="F" AND
  1632. group_id="'.$groupId.'" AND
  1633. session_id="'.$session_id.'"'; //type: P=page, D=discuss, F=wiki
  1634. $result = Database::query($sql);
  1635. $emailtext = get_lang('EmailWikipageDedeleted');
  1636. }
  1637. ///make and send email
  1638. if ($allow_send_mail) {
  1639. while ($row = Database::fetch_array($result)) {
  1640. $userinfo = api_get_user_info($row['user_id']); //$row['user_id'] obtained from tbl_wiki_mailcue
  1641. $name_to = $userinfo['complete_name'];
  1642. $email_to = $userinfo['email'];
  1643. $sender_name = api_get_setting('emailAdministrator');
  1644. $sender_email = api_get_setting('emailAdministrator');
  1645. $email_subject = get_lang('EmailWikiChanges').' - '.$_course['official_code'];
  1646. $email_body = get_lang('DearUser').' '.api_get_person_name($userinfo['firstname'], $userinfo['lastname']).',<br /><br />';
  1647. if($session_id==0){
  1648. $email_body .= $emailtext.' <strong>'.$_course['name'].' - '.$group_name.'</strong><br /><br /><br />';
  1649. }else{
  1650. $email_body .= $emailtext.' <strong>'.$_course['name'].' ('.api_get_session_name(api_get_session_id()).') - '.$group_name.'</strong><br /><br /><br />';
  1651. }
  1652. $email_body .= $email_user_author.' ('.$email_date_changes.')<br /><br /><br />';
  1653. $email_body .= $email_assignment.'<br /><br /><br />';
  1654. $email_body .= '<font size="-2">'.get_lang('EmailWikiChangesExt_1').': <strong>'.get_lang('NotifyChanges').'</strong><br />';
  1655. $email_body .= get_lang('EmailWikiChangesExt_2').': <strong>'.get_lang('NotNotifyChanges').'</strong></font><br />';
  1656. @api_mail_html(
  1657. $name_to,
  1658. $email_to,
  1659. $email_subject,
  1660. $email_body,
  1661. $sender_name,
  1662. $sender_email
  1663. );
  1664. }
  1665. }
  1666. }
  1667. /**
  1668. * Function export last wiki page version to document area
  1669. * @param int $doc_id wiki page id
  1670. *
  1671. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1672. */
  1673. public function export2doc($doc_id)
  1674. {
  1675. $_course = $this->courseInfo;
  1676. $groupId = api_get_group_id();
  1677. $data = self::getWikiDataFromDb($doc_id);
  1678. if (empty($data)) {
  1679. return false;
  1680. }
  1681. $wikiTitle = $data['title'];
  1682. $wikiContents = $data['content'];
  1683. $template =
  1684. '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  1685. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="{LANGUAGE}" lang="{LANGUAGE}">
  1686. <head>
  1687. <title>{TITLE}</title>
  1688. <meta http-equiv="Content-Type" content="text/html; charset={ENCODING}" />
  1689. <style type="text/css" media="screen, projection">
  1690. /*<![CDATA[*/
  1691. {CSS}
  1692. /*]]>*/
  1693. </style>
  1694. {ASCIIMATHML_SCRIPT}</head>
  1695. <body dir="{TEXT_DIRECTION}">
  1696. {CONTENT}
  1697. </body>
  1698. </html>';
  1699. $css_file = api_get_path(SYS_CSS_PATH).'themes/'.api_get_setting('stylesheets').'/default.css';
  1700. if (file_exists($css_file)) {
  1701. $css = @file_get_contents($css_file);
  1702. } else {
  1703. $css = '';
  1704. }
  1705. // Fixing some bugs in css files.
  1706. $root_rel = api_get_path(REL_PATH);
  1707. $css_path = 'main/css/';
  1708. $theme = api_get_setting('stylesheets').'/';
  1709. $css = str_replace('behavior:url("/main/css/csshover3.htc");', '', $css);
  1710. $css = str_replace('main/', $root_rel.'main/', $css);
  1711. $css = str_replace('images/', $root_rel.$css_path.$theme.'images/', $css);
  1712. $css = str_replace('../../img/', $root_rel.'main/img/', $css);
  1713. $asciimathmal_script = (api_contains_asciimathml($wikiContents) || api_contains_asciisvg($wikiContents))
  1714. ? '<script src="'.api_get_path(WEB_CODE_PATH).'inc/lib/javascript/asciimath/ASCIIMathML.js" type="text/javascript"></script>'."\n" : '';
  1715. $template = str_replace(array('{LANGUAGE}', '{ENCODING}', '{TEXT_DIRECTION}', '{TITLE}', '{CSS}', '{ASCIIMATHML_SCRIPT}'),
  1716. array(api_get_language_isocode(), api_get_system_encoding(), api_get_text_direction(), $wikiTitle, $css, $asciimathmal_script),
  1717. $template);
  1718. if (0 != $groupId) {
  1719. $groupPart = '_group' . $groupId; // and add groupId to put the same document title in different groups
  1720. $group_properties = GroupManager :: get_group_properties($groupId);
  1721. $groupPath = $group_properties['directory'];
  1722. } else {
  1723. $groupPart = '';
  1724. $groupPath ='';
  1725. }
  1726. $exportDir = api_get_path(SYS_COURSE_PATH).api_get_course_path(). '/document'.$groupPath;
  1727. $exportFile = api_replace_dangerous_char($wikiTitle) . $groupPart;
  1728. $wikiContents = trim(preg_replace("/\[[\[]?([^\]|]*)[|]?([^|\]]*)\][\]]?/", "$1", $wikiContents));
  1729. //TODO: put link instead of title
  1730. $wikiContents = str_replace('{CONTENT}', $wikiContents, $template);
  1731. // replace relative path by absolute path for courses, so you can see items into this page wiki (images, mp3, etc..) exported in documents
  1732. if (api_strpos($wikiContents,'../..'.api_get_path(REL_COURSE_PATH)) !== false) {
  1733. $web_course_path = api_get_path(WEB_COURSE_PATH);
  1734. $wikiContents = str_replace('../..'.api_get_path(REL_COURSE_PATH), $web_course_path, $wikiContents);
  1735. }
  1736. $i = 1;
  1737. //only export last version, but in new export new version in document area
  1738. while (file_exists($exportDir.'/'.$exportFile.'_'.$i.'.html')) {
  1739. $i++;
  1740. }
  1741. $wikiFileName = $exportFile . '_' . $i . '.html';
  1742. $exportPath = $exportDir . '/' . $wikiFileName;
  1743. file_put_contents($exportPath, $wikiContents);
  1744. $doc_id = add_document(
  1745. $_course,
  1746. $groupPath.'/'.$wikiFileName,
  1747. 'file',
  1748. filesize($exportPath),
  1749. $wikiTitle
  1750. );
  1751. api_item_property_update(
  1752. $_course,
  1753. TOOL_DOCUMENT,
  1754. $doc_id,
  1755. 'DocumentAdded',
  1756. api_get_user_id(),
  1757. $groupId
  1758. );
  1759. return $doc_id;
  1760. }
  1761. /**
  1762. * Exports the wiki page to PDF
  1763. */
  1764. public function export_to_pdf($id, $course_code)
  1765. {
  1766. if (!api_is_platform_admin()) {
  1767. if (api_get_setting('students_export2pdf') !== 'true') {
  1768. Display::addFlash(
  1769. Display::display_error_message(
  1770. get_lang('PDFDownloadNotAllowedForStudents'),
  1771. false,
  1772. true
  1773. )
  1774. );
  1775. return false;
  1776. }
  1777. }
  1778. $data = self::getWikiDataFromDb($id);
  1779. $content_pdf = api_html_entity_decode($data['content'], ENT_QUOTES, api_get_system_encoding());
  1780. //clean wiki links
  1781. $content_pdf=trim(preg_replace("/\[[\[]?([^\]|]*)[|]?([^|\]]*)\][\]]?/", "$1", $content_pdf));
  1782. //TODO: It should be better to display the link insted of the tile but it is hard for [[title]] links
  1783. $title_pdf = api_html_entity_decode($data['title'], ENT_QUOTES, api_get_system_encoding());
  1784. $title_pdf = api_utf8_encode($title_pdf, api_get_system_encoding());
  1785. $content_pdf = api_utf8_encode($content_pdf, api_get_system_encoding());
  1786. $html='
  1787. <!-- defines the headers/footers - this must occur before the headers/footers are set -->
  1788. <!--mpdf
  1789. <pageheader name="odds" content-left="'.$title_pdf.'" header-style-left="color: #880000; font-style: italic;" line="1" />
  1790. <pagefooter name="odds" content-right="{PAGENO}/{nb}" line="1" />
  1791. <!-- set the headers/footers - they will occur from here on in the document -->
  1792. <!--mpdf
  1793. <setpageheader name="odds" page="odd" value="on" show-this-page="1" />
  1794. <setpagefooter name="odds" page="O" value="on" />
  1795. mpdf-->'.$content_pdf;
  1796. $css_file = api_get_path(SYS_CSS_PATH).'themes/'.api_get_setting('stylesheets').'/print.css';
  1797. if (file_exists($css_file)) {
  1798. $css = @file_get_contents($css_file);
  1799. } else {
  1800. $css = '';
  1801. }
  1802. $pdf = new PDF();
  1803. $pdf->content_to_pdf($html, $css, $title_pdf, $course_code);
  1804. exit;
  1805. }
  1806. /**
  1807. * Function prevent double post (reload or F5)
  1808. *
  1809. */
  1810. public function double_post($wpost_id)
  1811. {
  1812. $postId = Session::read('wpost_id');
  1813. if (!empty($postId)) {
  1814. if ($wpost_id == $postId) {
  1815. return false;
  1816. } else {
  1817. Session::write('wpost_id', $wpost_id);
  1818. return true;
  1819. }
  1820. } else {
  1821. Session::write('wpost_id', $wpost_id);
  1822. return true;
  1823. }
  1824. }
  1825. /**
  1826. * Function wizard individual assignment
  1827. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1828. */
  1829. public function auto_add_page_users($values)
  1830. {
  1831. $assignment_type = $values['assignment'];
  1832. $session_id = $this->session_id;
  1833. $groupId = api_get_group_id();
  1834. if ($groupId==0) {
  1835. //extract course members
  1836. if(!empty($session_id)) {
  1837. $a_users_to_add = CourseManager::get_user_list_from_course_code(api_get_course_id(), $session_id);
  1838. } else {
  1839. $a_users_to_add = CourseManager::get_user_list_from_course_code(api_get_course_id(), 0);
  1840. }
  1841. } else {
  1842. //extract group members
  1843. $subscribed_users = GroupManager :: get_subscribed_users($groupId);
  1844. $subscribed_tutors = GroupManager :: get_subscribed_tutors($groupId);
  1845. $a_users_to_add_with_duplicates = array_merge($subscribed_users, $subscribed_tutors);
  1846. //remove duplicates
  1847. $a_users_to_add = $a_users_to_add_with_duplicates;
  1848. //array_walk($a_users_to_add, create_function('&$value,$key', '$value = json_encode($value);'));
  1849. $a_users_to_add = array_unique($a_users_to_add);
  1850. //array_walk($a_users_to_add, create_function('&$value,$key', '$value = json_decode($value, true);'));
  1851. }
  1852. $all_students_pages = array();
  1853. // Data about teacher
  1854. $userId = api_get_user_id();
  1855. $userinfo = api_get_user_info($userId);
  1856. $username = api_htmlentities(sprintf(get_lang('LoginX'), $userinfo['username'], ENT_QUOTES));
  1857. $name = $userinfo['complete_name']." - ".$username;
  1858. $photo = '<img src="' . $userinfo['avatar'] . '" alt="' . $name . '" width="40" height="50" align="top" title="' . $name . '" />';
  1859. // teacher assignment title
  1860. $title_orig = $values['title'];
  1861. // teacher assignment reflink
  1862. $link2teacher = $values['title'] = $title_orig."_uass".$userId;
  1863. // first: teacher name, photo, and assignment description (original content)
  1864. $content_orig_A = '<div align="center" style="background-color: #F5F8FB; border:solid; border-color: #E6E6E6">
  1865. <table border="0">
  1866. <tr><td style="font-size:24px">'.get_lang('AssignmentDesc').'</td></tr>
  1867. <tr><td>'.$photo.'<br />'.Display::tag('span', api_get_person_name($userinfo['firstname'], $userinfo['lastname']), array('title'=>$username)).'</td></tr>
  1868. </table></div>';
  1869. $content_orig_B = '<br/><div align="center" style="font-size:24px">'.
  1870. get_lang('AssignmentDescription').': '.
  1871. $title_orig.'</div><br/>'.Security::remove_XSS($_POST['content']);
  1872. //Second: student list (names, photo and links to their works).
  1873. //Third: Create Students work pages.
  1874. foreach ($a_users_to_add as $o_user_to_add) {
  1875. if ($o_user_to_add['user_id'] != $userId) {
  1876. // except that puts the task
  1877. $assig_user_id = $o_user_to_add['user_id'];
  1878. // identifies each page as created by the student, not by teacher
  1879. $userPicture = UserManager::getUserPicture($assig_user_id);
  1880. $username = api_htmlentities(sprintf(get_lang('LoginX'), $o_user_to_add['username'], ENT_QUOTES));
  1881. $name = api_get_person_name($o_user_to_add['firstname'], $o_user_to_add['lastname'])." . ".$username;
  1882. $photo= '<img src="'.$userPicture.'" alt="'.$name.'" width="40" height="50" align="bottom" title="'.$name.'" />';
  1883. $is_tutor_of_group = GroupManager::is_tutor_of_group($assig_user_id, $groupId); //student is tutor
  1884. $is_tutor_and_member = GroupManager::is_tutor_of_group($assig_user_id, $groupId) && GroupManager::is_subscribed($assig_user_id, $groupId);
  1885. // student is tutor and member
  1886. if ($is_tutor_and_member) {
  1887. $status_in_group=get_lang('GroupTutorAndMember');
  1888. } else {
  1889. if($is_tutor_of_group) {
  1890. $status_in_group=get_lang('GroupTutor');
  1891. } else {
  1892. $status_in_group=" "; //get_lang('GroupStandardMember')
  1893. }
  1894. }
  1895. if ($assignment_type==1) {
  1896. $values['title']= $title_orig;
  1897. $values['content'] = '<div align="center" style="background-color: #F5F8FB; border:solid; border-color: #E6E6E6">
  1898. <table border="0">
  1899. <tr><td style="font-size:24px">'.get_lang('AssignmentWork').'</td></tr>
  1900. <tr><td>'.$photo.'<br />'.$name.'</td></tr></table>
  1901. </div>[['.$link2teacher.' | '.get_lang('AssignmentLinktoTeacherPage').']] ';
  1902. //If $content_orig_B is added here, the task written by the professor was copied to the page of each student. TODO: config options
  1903. // AssignmentLinktoTeacherPage
  1904. $all_students_pages[] = '<li>'.
  1905. Display::tag(
  1906. 'span',
  1907. strtoupper($o_user_to_add['lastname']).', '.$o_user_to_add['firstname'], array('title'=>$username)
  1908. ).
  1909. ' [['.Security::remove_XSS($_POST['title'])."_uass".$assig_user_id.' | '.$photo.']] '.$status_in_group.'</li>';
  1910. //don't change this line without guaranteeing that users will be ordered by last names in the following format (surname, name)
  1911. $values['assignment']=2;
  1912. }
  1913. $this->assig_user_id = $assig_user_id;
  1914. self::save_new_wiki($values);
  1915. }
  1916. }
  1917. foreach ($a_users_to_add as $o_user_to_add) {
  1918. if ($o_user_to_add['user_id'] == $userId) {
  1919. $assig_user_id=$o_user_to_add['user_id'];
  1920. if ($assignment_type == 1) {
  1921. $values['title']= $title_orig;
  1922. $values['comment']=get_lang('AssignmentDesc');
  1923. sort($all_students_pages);
  1924. $values['content']=$content_orig_A.$content_orig_B.'<br/>
  1925. <div align="center" style="font-size:18px; background-color: #F5F8FB; border:solid; border-color:#E6E6E6">
  1926. '.get_lang('AssignmentLinkstoStudentsPage').'
  1927. </div><br/>
  1928. <div style="background-color: #F5F8FB; border:solid; border-color:#E6E6E6">
  1929. <ol>'.implode($all_students_pages).'</ol>
  1930. </div>
  1931. <br/>';
  1932. $values['assignment']=1;
  1933. }
  1934. $this->assig_user_id = $assig_user_id;
  1935. self::save_new_wiki($values);
  1936. }
  1937. }
  1938. }
  1939. /**
  1940. * Displays the results of a wiki search
  1941. * @param string Search term
  1942. * @param int Whether to search the contents (1) or just the titles (0)
  1943. * @param int
  1944. */
  1945. public function display_wiki_search_results($search_term, $search_content=0, $all_vers=0)
  1946. {
  1947. $tbl_wiki = $this->tbl_wiki;
  1948. $condition_session = $this->condition_session;
  1949. $groupfilter = $this->groupfilter;
  1950. $_course = $this->courseInfo;
  1951. $course_id = api_get_course_int_id();
  1952. echo '<legend>'.get_lang('WikiSearchResults').': '.Security::remove_XSS($search_term);
  1953. echo '</legend>';
  1954. //only by professors when page is hidden
  1955. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  1956. if ($all_vers=='1') {
  1957. if ($search_content=='1') {
  1958. $sql = "SELECT * FROM ".$tbl_wiki."
  1959. WHERE
  1960. c_id = $course_id AND
  1961. title LIKE '%".Database::escape_string($search_term)."%' OR
  1962. content LIKE '%".Database::escape_string($search_term)."%' AND
  1963. ".$groupfilter.$condition_session."";
  1964. //search all pages and all versions
  1965. } else {
  1966. $sql = "SELECT * FROM ".$tbl_wiki."
  1967. WHERE
  1968. c_id = $course_id AND
  1969. title LIKE '%".Database::escape_string($search_term)."%' AND
  1970. ".$groupfilter.$condition_session."";
  1971. //search all pages and all versions
  1972. }
  1973. } else {
  1974. if ($search_content=='1') {
  1975. $sql = "SELECT * FROM ".$tbl_wiki." s1
  1976. WHERE
  1977. s1.c_id = $course_id AND
  1978. title LIKE '%".Database::escape_string($search_term)."%' OR
  1979. content LIKE '%".Database::escape_string($search_term)."%' AND
  1980. id=(
  1981. SELECT MAX(s2.id)
  1982. FROM ".$tbl_wiki." s2
  1983. WHERE
  1984. s2.c_id = $course_id AND
  1985. s1.reflink = s2.reflink AND
  1986. ".$groupfilter.$condition_session.")";
  1987. // warning don't use group by reflink because don't return the last version
  1988. }
  1989. else {
  1990. $sql = " SELECT * FROM ".$tbl_wiki." s1
  1991. WHERE
  1992. s1.c_id = $course_id AND
  1993. title LIKE '%".Database::escape_string($search_term)."%' AND
  1994. id=(
  1995. SELECT MAX(s2.id)
  1996. FROM ".$tbl_wiki." s2
  1997. WHERE
  1998. s2.c_id = $course_id AND
  1999. s1.reflink = s2.reflink AND
  2000. ".$groupfilter.$condition_session.")";
  2001. // warning don't use group by reflink because don't return the last version
  2002. }
  2003. }
  2004. } else {
  2005. if ($all_vers == '1') {
  2006. if ($search_content == '1') {
  2007. $sql = "SELECT * FROM ".$tbl_wiki."
  2008. WHERE
  2009. c_id = $course_id AND
  2010. visibility=1 AND
  2011. title LIKE '%".Database::escape_string($search_term)."%' OR
  2012. content LIKE '%".Database::escape_string($search_term)."%' AND
  2013. ".$groupfilter.$condition_session."";
  2014. //search all pages and all versions
  2015. } else {
  2016. $sql = "SELECT * FROM ".$tbl_wiki."
  2017. WHERE
  2018. c_id = $course_id AND
  2019. visibility=1 AND
  2020. title LIKE '%".Database::escape_string($search_term)."%' AND
  2021. ".$groupfilter.$condition_session."";
  2022. //search all pages and all versions
  2023. }
  2024. } else {
  2025. if($search_content=='1') {
  2026. $sql = "SELECT * FROM ".$tbl_wiki." s1
  2027. WHERE
  2028. s1.c_id = $course_id AND
  2029. visibility=1 AND
  2030. title LIKE '%".Database::escape_string($search_term)."%' OR
  2031. content LIKE '%".Database::escape_string($search_term)."%' AND
  2032. id=(
  2033. SELECT MAX(s2.id)
  2034. FROM ".$tbl_wiki." s2
  2035. WHERE s2.c_id = $course_id AND
  2036. s1.reflink = s2.reflink AND
  2037. ".$groupfilter.$condition_session.")";
  2038. // warning don't use group by reflink because don't return the last version
  2039. } else {
  2040. $sql = "SELECT * FROM ".$tbl_wiki." s1
  2041. WHERE
  2042. s1.c_id = $course_id AND
  2043. visibility=1 AND
  2044. title LIKE '%".Database::escape_string($search_term)."%' AND
  2045. id=(
  2046. SELECT MAX(s2.id) FROM ".$tbl_wiki." s2
  2047. WHERE s2.c_id = $course_id AND
  2048. s1.reflink = s2.reflink AND
  2049. ".$groupfilter.$condition_session.")";
  2050. // warning don't use group by reflink because don't return the last version
  2051. }
  2052. }
  2053. }
  2054. $result = Database::query($sql);
  2055. //show table
  2056. $rows = array();
  2057. if (Database::num_rows($result) > 0) {
  2058. while ($obj = Database::fetch_object($result)) {
  2059. //get author
  2060. $userinfo = api_get_user_info($obj->user_id);
  2061. //get time
  2062. $year = substr($obj->dtime, 0, 4);
  2063. $month = substr($obj->dtime, 5, 2);
  2064. $day = substr($obj->dtime, 8, 2);
  2065. $hours = substr($obj->dtime, 11, 2);
  2066. $minutes = substr($obj->dtime, 14, 2);
  2067. $seconds = substr($obj->dtime, 17, 2);
  2068. //get type assignment icon
  2069. if($obj->assignment==1) {
  2070. $ShowAssignment = Display::return_icon('wiki_assignment.png', get_lang('AssignmentDesc'),'',ICON_SIZE_SMALL);
  2071. } elseif ($obj->assignment==2) {
  2072. $ShowAssignment = Display::return_icon('wiki_work.png', get_lang('AssignmentWork'),'',ICON_SIZE_SMALL);
  2073. } elseif ($obj->assignment==0) {
  2074. $ShowAssignment = Display::return_icon('px_transparent.gif');
  2075. }
  2076. $row = array();
  2077. $row[] = $ShowAssignment;
  2078. if($all_vers=='1') {
  2079. $row[] = '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=showpage&title='.api_htmlentities(urlencode($obj->reflink)).'&view='.$obj->id.'&session_id='.api_htmlentities(urlencode($_GET['$session_id'])).'&group_id='.api_htmlentities(urlencode($_GET['group_id'])).'">'.
  2080. api_htmlentities($obj->title).'</a>';
  2081. } else {
  2082. $row[] = '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=showpage&title='.api_htmlentities(urlencode($obj->reflink)).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities($_GET['group_id']).'">'.
  2083. $obj->title.'</a>';
  2084. }
  2085. $row[] = ($obj->user_id != 0 && $userinfo !== false) ? UserManager::getUserProfileLink($userinfo) : get_lang('Anonymous').' ('.$obj->user_ip.')';
  2086. $row[] = $year.'-'.$month.'-'.$day.' '.$hours.":".$minutes.":".$seconds;
  2087. if ($all_vers=='1') {
  2088. $row[] = $obj->version;
  2089. } else {
  2090. $showdelete = '';
  2091. if (api_is_allowed_to_edit(false,true)|| api_is_platform_admin()) {
  2092. $showdelete=' <a href="'.api_get_self().'?'.api_get_cidreq().'&action=delete&title='.api_htmlentities(urlencode($obj->reflink)).'&group_id='.api_htmlentities($_GET['group_id']).'">'.
  2093. Display::return_icon('delete.png', get_lang('Delete'),'',ICON_SIZE_SMALL);
  2094. }
  2095. $row[] = '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=edit&title='.api_htmlentities(urlencode($obj->reflink)).'&group_id='.api_htmlentities($_GET['group_id']).'">'.
  2096. Display::return_icon('edit.png', get_lang('EditPage'),'',ICON_SIZE_SMALL).'</a>
  2097. <a href="'.api_get_self().'?cidReq='.$_course['code'].'&action=discuss&title='.api_htmlentities(urlencode($obj->reflink)).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities($_GET['group_id']).'">'.
  2098. Display::return_icon('discuss.png', get_lang('Discuss'),'',ICON_SIZE_SMALL).'</a>
  2099. <a href="'.api_get_self().'?cidReq='.$_course['code'].'&action=history&title='.api_htmlentities(urlencode($obj->reflink)).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities($_GET['group_id']).'">'.
  2100. Display::return_icon('history.png', get_lang('History'),'',ICON_SIZE_SMALL).'</a> <a href="'.api_get_self().'?cidReq='.$_course['code'].'&action=links&title='.api_htmlentities(urlencode($obj->reflink)).'&group_id='.api_htmlentities($_GET['group_id']).'">'.
  2101. Display::return_icon('what_link_here.png', get_lang('LinksPages'),'',ICON_SIZE_SMALL).'</a>'.$showdelete;
  2102. }
  2103. $rows[] = $row;
  2104. }
  2105. $table = new SortableTableFromArrayConfig(
  2106. $rows,
  2107. 1,
  2108. 10,
  2109. 'SearchPages_table',
  2110. '',
  2111. '',
  2112. 'ASC'
  2113. );
  2114. $table->set_additional_parameters(
  2115. array(
  2116. 'cidReq' => $_GET['cidReq'],
  2117. 'action' => $_GET['action'],
  2118. 'group_id' => intval($_GET['group_id']),
  2119. 'mode_table' => 'yes2',
  2120. 'search_term' => $search_term,
  2121. 'search_content' => $search_content,
  2122. 'all_vers' => $all_vers,
  2123. )
  2124. );
  2125. $table->set_header(0,get_lang('Type'), true, array ('style' => 'width:30px;'));
  2126. $table->set_header(1,get_lang('Title'), true);
  2127. if ($all_vers == '1') {
  2128. $table->set_header(2,get_lang('Author'), true);
  2129. $table->set_header(3,get_lang('Date'), true);
  2130. $table->set_header(4,get_lang('Version'), true);
  2131. } else {
  2132. $table->set_header(2,get_lang('Author').' ('.get_lang('LastVersion').')', true);
  2133. $table->set_header(3,get_lang('Date').' ('.get_lang('LastVersion').')', true);
  2134. $table->set_header(4,get_lang('Actions'), false, array ('style' => 'width:130px;'));
  2135. }
  2136. $table->display();
  2137. } else {
  2138. echo get_lang('NoSearchResults');
  2139. }
  2140. }
  2141. /**
  2142. * Returns a date picker
  2143. * @todo replace this function with the formvalidator datepicker
  2144. *
  2145. */
  2146. public function draw_date_picker($prefix,$default='')
  2147. {
  2148. if (empty($default)) {
  2149. $default = date('Y-m-d H:i:s');
  2150. }
  2151. $parts = explode(' ', $default);
  2152. list($d_year,$d_month,$d_day) = explode('-',$parts[0]);
  2153. list($d_hour,$d_minute) = explode(':',$parts[1]);
  2154. $month_list = array(
  2155. 1 => get_lang('JanuaryLong'),
  2156. 2 => get_lang('FebruaryLong'),
  2157. 3 => get_lang('MarchLong'),
  2158. 4 => get_lang('AprilLong'),
  2159. 5 => get_lang('MayLong'),
  2160. 6 => get_lang('JuneLong'),
  2161. 7 => get_lang('JulyLong'),
  2162. 8 => get_lang('AugustLong'),
  2163. 9 => get_lang('SeptemberLong'),
  2164. 10 => get_lang('OctoberLong'),
  2165. 11 => get_lang('NovemberLong'),
  2166. 12 => get_lang('DecemberLong'),
  2167. );
  2168. $minute = range(10,59);
  2169. array_unshift($minute,'00','01','02','03','04','05','06','07','08','09');
  2170. $date_form = self::make_select($prefix.'_day', array_combine(range(1,31),range(1,31)), $d_day);
  2171. $date_form .= self::make_select($prefix.'_month', $month_list, $d_month);
  2172. $date_form .= self::make_select(
  2173. $prefix.'_year',
  2174. array(
  2175. $d_year - 2 => $d_year - 2,
  2176. $d_year - 1 => $d_year - 1,
  2177. $d_year => $d_year,
  2178. $d_year + 1 => $d_year + 1,
  2179. $d_year + 2 => $d_year + 2,
  2180. ),
  2181. $d_year
  2182. ).'&nbsp;&nbsp;&nbsp;&nbsp;';
  2183. $date_form .= self::make_select($prefix.'_hour', array_combine(range(0,23),range(0,23)), $d_hour).' : ';
  2184. $date_form .= self::make_select($prefix.'_minute', $minute, $d_minute);
  2185. return $date_form;
  2186. }
  2187. /**
  2188. * Draws an HTML form select with the given options
  2189. *
  2190. */
  2191. public function make_select($name,$values,$checked='')
  2192. {
  2193. $output = '<select name="'.$name.'" id="'.$name.'">';
  2194. foreach($values as $key => $value) {
  2195. $output .= '<option value="'.$key.'" '.(($checked==$key)?'selected="selected"':'').'>'.$value.'</option>';
  2196. }
  2197. $output .= '</select>';
  2198. return $output;
  2199. }
  2200. /**
  2201. * Translates a form date into a more usable format
  2202. *
  2203. */
  2204. public function get_date_from_select($prefix)
  2205. {
  2206. return $_POST[$prefix.'_year'].'-'.
  2207. self::two_digits($_POST[$prefix.'_month']).'-'.
  2208. self::two_digits($_POST[$prefix.'_day']).' '.
  2209. self::two_digits($_POST[$prefix.'_hour']).':'.
  2210. self::two_digits($_POST[$prefix.'_minute']).':00';
  2211. }
  2212. /**
  2213. * Converts 1-9 to 01-09
  2214. */
  2215. public function two_digits($number)
  2216. {
  2217. $number = (int)$number;
  2218. return ($number < 10) ? '0'.$number : $number;
  2219. }
  2220. /**
  2221. * Get wiki information
  2222. * @param int|bool wiki id
  2223. * @return array wiki data
  2224. */
  2225. public function getWikiDataFromDb($id)
  2226. {
  2227. $tbl_wiki = $this->tbl_wiki;
  2228. $course_id = api_get_course_int_id();
  2229. if ($id === false) {
  2230. return array();
  2231. }
  2232. $id = intval($id);
  2233. $sql = 'SELECT * FROM '.$tbl_wiki.'
  2234. WHERE c_id = '.$course_id.' AND id = '.$id.' ';
  2235. $result = Database::query($sql);
  2236. $data = array();
  2237. while ($row=Database::fetch_array($result,'ASSOC')) {
  2238. $data = $row;
  2239. }
  2240. return $data;
  2241. }
  2242. /**
  2243. * @param string $refLink
  2244. * @return array
  2245. */
  2246. public function getLastWikiData($refLink)
  2247. {
  2248. $tbl_wiki = $this->tbl_wiki;
  2249. $groupfilter = $this->groupfilter;
  2250. $condition_session = $this->condition_session;
  2251. $course_id = api_get_course_int_id();
  2252. $sql = 'SELECT * FROM '.$tbl_wiki.'
  2253. WHERE
  2254. c_id = '.$course_id.' AND
  2255. reflink="'.Database::escape_string($refLink).'" AND
  2256. '.$groupfilter.$condition_session.'
  2257. ORDER BY id DESC';
  2258. $result = Database::query($sql);
  2259. return Database::fetch_array($result);
  2260. }
  2261. /**
  2262. * Get wiki information
  2263. * @param string wiki id
  2264. * @param int $courseId
  2265. * @return array wiki data
  2266. */
  2267. public function getPageByTitle($title, $courseId = null)
  2268. {
  2269. $tbl_wiki = $this->tbl_wiki;
  2270. if (empty($courseId)) {
  2271. $courseId = api_get_course_int_id();
  2272. } else {
  2273. $courseId = intval($courseId);
  2274. }
  2275. if (empty($title) || empty($courseId)) {
  2276. return array();
  2277. }
  2278. $title = Database::escape_string($title);
  2279. $sql = "SELECT * FROM $tbl_wiki
  2280. WHERE c_id = $courseId AND reflink = '$title'";
  2281. $result = Database::query($sql);
  2282. $data = array();
  2283. if (Database::num_rows($result)) {
  2284. $data = Database::fetch_array($result,'ASSOC');
  2285. }
  2286. return $data;
  2287. }
  2288. /**
  2289. * @param string $title
  2290. * @param int $courseId
  2291. * @param string
  2292. * @param string
  2293. * @return bool
  2294. */
  2295. public function deletePage($title, $courseId, $groupfilter = null, $condition_session = null)
  2296. {
  2297. $tbl_wiki = $this->tbl_wiki;
  2298. $tbl_wiki_discuss = $this->tbl_wiki_discuss;
  2299. $tbl_wiki_mailcue = $this->tbl_wiki_mailcue;
  2300. $tbl_wiki_conf = $this->tbl_wiki_conf;
  2301. $pageInfo = self::getPageByTitle($title, $courseId);
  2302. if (!empty($pageInfo)) {
  2303. $pageId = $pageInfo['id'];
  2304. $sql = "DELETE FROM $tbl_wiki_conf
  2305. WHERE c_id = $courseId AND page_id = $pageId";
  2306. Database::query($sql);
  2307. $sql = 'DELETE FROM '.$tbl_wiki_discuss.'
  2308. WHERE c_id = '.$courseId.' AND publication_id = '.$pageId;
  2309. Database::query($sql);
  2310. $sql = 'DELETE FROM '.$tbl_wiki_mailcue.'
  2311. WHERE c_id = '.$courseId.' AND id = '.$pageId.' AND '.$groupfilter.$condition_session.'';
  2312. Database::query($sql);
  2313. $sql = 'DELETE FROM '.$tbl_wiki.'
  2314. WHERE c_id = '.$courseId.' AND id = '.$pageId.' AND '.$groupfilter.$condition_session.'';
  2315. Database::query($sql);
  2316. self::check_emailcue(0, 'E');
  2317. return true;
  2318. }
  2319. return false;
  2320. }
  2321. /**
  2322. * @return array
  2323. */
  2324. public function getAllWiki()
  2325. {
  2326. $tbl_wiki = $this->tbl_wiki;
  2327. $course_id = $this->course_id;
  2328. $condition_session = $this->condition_session;
  2329. $sql = "SELECT * FROM $tbl_wiki
  2330. WHERE
  2331. c_id = $course_id AND
  2332. is_editing != '0' ".$condition_session;
  2333. $result = Database::query($sql);
  2334. return Database::store_result($result, 'ASSOC');
  2335. }
  2336. /**
  2337. * @param int $isEditing
  2338. */
  2339. public function updateWikiIsEditing($isEditing)
  2340. {
  2341. $tbl_wiki = $this->tbl_wiki;
  2342. $course_id = $this->course_id;
  2343. $condition_session = $this->condition_session;
  2344. $isEditing = Database::escape_string($isEditing);
  2345. $sql = 'UPDATE '.$tbl_wiki.' SET
  2346. is_editing = "0",
  2347. time_edit = NULL
  2348. WHERE
  2349. c_id = '.$course_id.' AND
  2350. is_editing="'.$isEditing.'" '.
  2351. $condition_session;
  2352. Database::query($sql);
  2353. }
  2354. /**
  2355. * Release of blocked pages to prevent concurrent editions
  2356. * @param int $userId
  2357. * @param string $action
  2358. */
  2359. public function blockConcurrentEditions($userId, $action = null)
  2360. {
  2361. $result = self::getAllWiki();
  2362. if (!empty($result)) {
  2363. foreach ($result as $is_editing_block) {
  2364. $max_edit_time = 1200; // 20 minutes
  2365. $timestamp_edit = strtotime($is_editing_block['time_edit']);
  2366. $time_editing = time()-$timestamp_edit;
  2367. // First prevent concurrent users and double version
  2368. if ($is_editing_block['is_editing'] == $userId) {
  2369. Session::write('_version', $is_editing_block['version']);
  2370. } else {
  2371. Session::erase('_version');
  2372. }
  2373. // Second checks if has exceeded the time that a page may be available or if a page was edited and saved by its author
  2374. if ($time_editing > $max_edit_time || ($is_editing_block['is_editing'] == $userId && $action!='edit')) {
  2375. self::updateWikiIsEditing($is_editing_block['is_editing']);
  2376. }
  2377. }
  2378. }
  2379. }
  2380. /**
  2381. * Showing wiki stats
  2382. */
  2383. public function getStats()
  2384. {
  2385. if (!api_is_allowed_to_edit(false, true)) {
  2386. return false;
  2387. }
  2388. $tbl_wiki = $this->tbl_wiki;
  2389. $course_id = $this->course_id;
  2390. $condition_session = $this->condition_session;
  2391. $groupfilter = $this->groupfilter;
  2392. $session_id = $this->session_id;
  2393. $tbl_wiki_conf = $this->tbl_wiki_conf;
  2394. echo '<div class="actions">'.get_lang('Statistics').'</div>';
  2395. // Check all versions of all pages
  2396. $total_words = 0;
  2397. $total_links = 0;
  2398. $total_links_anchors = 0;
  2399. $total_links_mail = 0;
  2400. $total_links_ftp = 0;
  2401. $total_links_irc = 0;
  2402. $total_links_news = 0;
  2403. $total_wlinks = 0;
  2404. $total_images = 0;
  2405. $clean_total_flash = 0;
  2406. $total_flash = 0;
  2407. $total_mp3 = 0;
  2408. $total_flv_p = 0;
  2409. $total_flv = 0;
  2410. $total_youtube = 0;
  2411. $total_multimedia = 0;
  2412. $total_tables = 0;
  2413. $sql = "SELECT *, COUNT(*) AS TOTAL_VERS, SUM(hits) AS TOTAL_VISITS
  2414. FROM ".$tbl_wiki."
  2415. WHERE c_id = $course_id AND ".$groupfilter.$condition_session."";
  2416. $allpages=Database::query($sql);
  2417. while ($row = Database::fetch_array($allpages)) {
  2418. $total_versions = $row['TOTAL_VERS'];
  2419. $total_visits = intval($row['TOTAL_VISITS']);
  2420. }
  2421. $sql = "SELECT * FROM ".$tbl_wiki."
  2422. WHERE c_id = $course_id AND ".$groupfilter.$condition_session."";
  2423. $allpages = Database::query($sql);
  2424. while ($row=Database::fetch_array($allpages)) {
  2425. $total_words = $total_words + self::word_count($row['content']);
  2426. $total_links = $total_links+substr_count($row['content'], "href=");
  2427. $total_links_anchors = $total_links_anchors+substr_count($row['content'], 'href="#');
  2428. $total_links_mail = $total_links_mail+substr_count($row['content'], 'href="mailto');
  2429. $total_links_ftp = $total_links_ftp+substr_count($row['content'], 'href="ftp');
  2430. $total_links_irc = $total_links_irc+substr_count($row['content'], 'href="irc');
  2431. $total_links_news = $total_links_news+substr_count($row['content'], 'href="news');
  2432. $total_wlinks = $total_wlinks+substr_count($row['content'], "[[");
  2433. $total_images = $total_images+substr_count($row['content'], "<img");
  2434. $clean_total_flash = preg_replace('/player.swf/', ' ', $row['content']);
  2435. $total_flash = $total_flash+substr_count($clean_total_flash, '.swf"');
  2436. //.swf" end quotes prevent insert swf through flvplayer (is not counted)
  2437. $total_mp3 = $total_mp3+substr_count($row['content'], ".mp3");
  2438. $total_flv_p = $total_flv_p+substr_count($row['content'], ".flv");
  2439. $total_flv = $total_flv_p/5;
  2440. $total_youtube = $total_youtube+substr_count($row['content'], "http://www.youtube.com");
  2441. $total_multimedia = $total_multimedia+substr_count($row['content'], "video/x-msvideo");
  2442. $total_tables = $total_tables+substr_count($row['content'], "<table");
  2443. }
  2444. //check only last version of all pages (current page)
  2445. $sql =' SELECT *, COUNT(*) AS TOTAL_PAGES, SUM(hits) AS TOTAL_VISITS_LV
  2446. FROM '.$tbl_wiki.' s1
  2447. WHERE s1.c_id = '.$course_id.' AND id=(
  2448. SELECT MAX(s2.id)
  2449. FROM '.$tbl_wiki.' s2
  2450. WHERE
  2451. s2.c_id = '.$course_id.' AND
  2452. s1.reflink = s2.reflink AND
  2453. '.$groupfilter.' AND
  2454. session_id='.$session_id.')';
  2455. $allpages = Database::query($sql);
  2456. while ($row=Database::fetch_array($allpages)) {
  2457. $total_pages = $row['TOTAL_PAGES'];
  2458. $total_visits_lv = intval($row['TOTAL_VISITS_LV']);
  2459. }
  2460. $total_words_lv = 0;
  2461. $total_links_lv = 0;
  2462. $total_links_anchors_lv = 0;
  2463. $total_links_mail_lv = 0;
  2464. $total_links_ftp_lv = 0;
  2465. $total_links_irc_lv = 0;
  2466. $total_links_news_lv = 0;
  2467. $total_wlinks_lv = 0;
  2468. $total_images_lv = 0;
  2469. $clean_total_flash_lv = 0;
  2470. $total_flash_lv = 0;
  2471. $total_mp3_lv = 0;
  2472. $total_flv_p_lv = 0;
  2473. $total_flv_lv = 0;
  2474. $total_youtube_lv = 0;
  2475. $total_multimedia_lv = 0;
  2476. $total_tables_lv = 0;
  2477. $sql = 'SELECT * FROM '.$tbl_wiki.' s1
  2478. WHERE s1.c_id = '.$course_id.' AND id=(
  2479. SELECT MAX(s2.id) FROM '.$tbl_wiki.' s2
  2480. WHERE
  2481. s2.c_id = '.$course_id.' AND
  2482. s1.reflink = s2.reflink AND
  2483. '.$groupfilter.' AND
  2484. session_id='.$session_id.'
  2485. )';
  2486. $allpages = Database::query($sql);
  2487. while ($row=Database::fetch_array($allpages)) {
  2488. $total_words_lv = $total_words_lv+ self::word_count($row['content']);
  2489. $total_links_lv = $total_links_lv+substr_count($row['content'], "href=");
  2490. $total_links_anchors_lv = $total_links_anchors_lv+substr_count($row['content'], 'href="#');
  2491. $total_links_mail_lv = $total_links_mail_lv+substr_count($row['content'], 'href="mailto');
  2492. $total_links_ftp_lv = $total_links_ftp_lv+substr_count($row['content'], 'href="ftp');
  2493. $total_links_irc_lv = $total_links_irc_lv+substr_count($row['content'], 'href="irc');
  2494. $total_links_news_lv = $total_links_news_lv+substr_count($row['content'], 'href="news');
  2495. $total_wlinks_lv = $total_wlinks_lv+substr_count($row['content'], "[[");
  2496. $total_images_lv = $total_images_lv+substr_count($row['content'], "<img");
  2497. $clean_total_flash_lv = preg_replace('/player.swf/', ' ', $row['content']);
  2498. $total_flash_lv = $total_flash_lv+substr_count($clean_total_flash_lv, '.swf"');
  2499. //.swf" end quotes prevent insert swf through flvplayer (is not counted)
  2500. $total_mp3_lv = $total_mp3_lv+substr_count($row['content'], ".mp3");
  2501. $total_flv_p_lv = $total_flv_p_lv+substr_count($row['content'], ".flv");
  2502. $total_flv_lv = $total_flv_p_lv/5;
  2503. $total_youtube_lv = $total_youtube_lv+substr_count($row['content'], "http://www.youtube.com");
  2504. $total_multimedia_lv = $total_multimedia_lv+substr_count($row['content'], "video/x-msvideo");
  2505. $total_tables_lv = $total_tables_lv+substr_count($row['content'], "<table");
  2506. }
  2507. //Total pages edited at this time
  2508. $total_editing_now=0;
  2509. $sql = 'SELECT *, COUNT(*) AS TOTAL_EDITING_NOW
  2510. FROM '.$tbl_wiki.' s1
  2511. WHERE is_editing!=0 AND s1.c_id = '.$course_id.' AND
  2512. id=(
  2513. SELECT MAX(s2.id)
  2514. FROM '.$tbl_wiki.' s2
  2515. WHERE
  2516. s2.c_id = '.$course_id.' AND
  2517. s1.reflink = s2.reflink AND
  2518. '.$groupfilter.' AND
  2519. session_id='.$session_id.'
  2520. )';
  2521. // Can not use group by because the mark is set in the latest version
  2522. $allpages=Database::query($sql);
  2523. while ($row=Database::fetch_array($allpages)) {
  2524. $total_editing_now = $row['TOTAL_EDITING_NOW'];
  2525. }
  2526. // Total hidden pages
  2527. $total_hidden=0;
  2528. $sql = 'SELECT * FROM '.$tbl_wiki.'
  2529. WHERE c_id = '.$course_id.' AND visibility=0 AND '.$groupfilter.$condition_session.'
  2530. GROUP BY reflink';
  2531. // or group by page_id. As the mark of hidden places it in all versions of the page, I can use group by to see the first
  2532. $allpages=Database::query($sql);
  2533. while ($row=Database::fetch_array($allpages)) {
  2534. $total_hidden = $total_hidden+1;
  2535. }
  2536. //Total protect pages
  2537. $total_protected=0;
  2538. $sql = 'SELECT * FROM '.$tbl_wiki.'
  2539. WHERE c_id = '.$course_id.' AND editlock=1 AND '.$groupfilter.$condition_session.'
  2540. GROUP BY reflink';
  2541. // or group by page_id. As the mark of protected page is the first version of the page, I can use group by
  2542. $allpages=Database::query($sql);
  2543. while ($row=Database::fetch_array($allpages)) {
  2544. $total_protected = $total_protected+1;
  2545. }
  2546. // Total empty versions.
  2547. $total_empty_content=0;
  2548. $sql = 'SELECT * FROM '.$tbl_wiki.'
  2549. WHERE
  2550. c_id = '.$course_id.' AND
  2551. content="" AND
  2552. '.$groupfilter.$condition_session.'';
  2553. $allpages = Database::query($sql);
  2554. while ($row=Database::fetch_array($allpages)) {
  2555. $total_empty_content = $total_empty_content+1;
  2556. }
  2557. //Total empty pages (last version)
  2558. $total_empty_content_lv=0;
  2559. $sql = 'SELECT * FROM '.$tbl_wiki.' s1
  2560. WHERE s1.c_id = '.$course_id.' AND content="" AND id=(
  2561. SELECT MAX(s2.id) FROM '.$tbl_wiki.' s2
  2562. WHERE s1.c_id = '.$course_id.' AND s1.reflink = s2.reflink AND '.$groupfilter.' AND session_id='.$session_id.')';
  2563. $allpages=Database::query($sql);
  2564. while ($row = Database::fetch_array($allpages)) {
  2565. $total_empty_content_lv = $total_empty_content_lv+1;
  2566. }
  2567. // Total locked discuss pages
  2568. $total_lock_disc=0;
  2569. $sql = 'SELECT * FROM '.$tbl_wiki.'
  2570. WHERE c_id = '.$course_id.' AND addlock_disc=0 AND '.$groupfilter.$condition_session.'
  2571. GROUP BY reflink';//group by because mark lock in all vers, then always is ok
  2572. $allpages=Database::query($sql);
  2573. while ($row = Database::fetch_array($allpages)) {
  2574. $total_lock_disc = $total_lock_disc+1;
  2575. }
  2576. // Total hidden discuss pages.
  2577. $total_hidden_disc = 0;
  2578. $sql = 'SELECT * FROM '.$tbl_wiki.'
  2579. WHERE c_id = '.$course_id.' AND visibility_disc=0 AND '.$groupfilter.$condition_session.'
  2580. GROUP BY reflink';
  2581. //group by because mark lock in all vers, then always is ok
  2582. $allpages = Database::query($sql);
  2583. while ($row = Database::fetch_array($allpages)) {
  2584. $total_hidden_disc = $total_hidden_disc+1;
  2585. }
  2586. //Total versions with any short comment by user or system
  2587. $total_comment_version = 0;
  2588. $sql = 'SELECT * FROM '.$tbl_wiki.'
  2589. WHERE c_id = '.$course_id.' AND comment!="" AND '.$groupfilter.$condition_session.'';
  2590. $allpages=Database::query($sql);
  2591. while ($row = Database::fetch_array($allpages)) {
  2592. $total_comment_version = $total_comment_version+1;
  2593. }
  2594. // Total pages that can only be scored by teachers.
  2595. $total_only_teachers_rating=0;
  2596. $sql = 'SELECT * FROM '.$tbl_wiki.'
  2597. WHERE c_id = '.$course_id.' AND
  2598. ratinglock_disc = 0 AND
  2599. '.$groupfilter.$condition_session.'
  2600. GROUP BY reflink';//group by because mark lock in all vers, then always is ok
  2601. $allpages=Database::query($sql);
  2602. while ($row=Database::fetch_array($allpages)) {
  2603. $total_only_teachers_rating = $total_only_teachers_rating+1;
  2604. }
  2605. // Total pages scored by peers
  2606. // put always this line alfter check num all pages and num pages rated by teachers
  2607. $total_rating_by_peers=$total_pages-$total_only_teachers_rating;
  2608. //Total pages identified as standard task
  2609. $total_task=0;
  2610. $sql='SELECT * FROM '.$tbl_wiki.', '.$tbl_wiki_conf.'
  2611. WHERE '.$tbl_wiki_conf.'.c_id = '.$course_id.' AND
  2612. '.$tbl_wiki_conf.'.task!="" AND
  2613. '.$tbl_wiki_conf.'.page_id='.$tbl_wiki.'.page_id AND
  2614. '.$tbl_wiki.'.'.$groupfilter.$condition_session;
  2615. $allpages = Database::query($sql);
  2616. while ($row=Database::fetch_array($allpages)) {
  2617. $total_task=$total_task+1;
  2618. }
  2619. //Total pages identified as teacher page (wiki portfolio mode - individual assignment)
  2620. $total_teacher_assignment=0;
  2621. $sql = 'SELECT * FROM '.$tbl_wiki.' s1
  2622. WHERE s1.c_id = '.$course_id.' AND assignment=1 AND id=(
  2623. SELECT MAX(s2.id)
  2624. FROM '.$tbl_wiki.' s2
  2625. WHERE s2.c_id = '.$course_id.' AND s1.reflink = s2.reflink AND '.$groupfilter.' AND session_id='.$session_id.'
  2626. )';
  2627. //mark all versions, but do not use group by reflink because y want the pages not versions
  2628. $allpages = Database::query($sql);
  2629. while ($row = Database::fetch_array($allpages)) {
  2630. $total_teacher_assignment=$total_teacher_assignment+1;
  2631. }
  2632. //Total pages identifies as student page (wiki portfolio mode - individual assignment)
  2633. $total_student_assignment=0;
  2634. $sql = 'SELECT * FROM '.$tbl_wiki.' s1
  2635. WHERE s1.c_id = '.$course_id.' AND assignment=2 AND
  2636. id=(SELECT MAX(s2.id) FROM '.$tbl_wiki.' s2
  2637. WHERE s2.c_id = '.$course_id.' AND s1.reflink = s2.reflink AND '.$groupfilter.' AND session_id='.$session_id.')';
  2638. //mark all versions, but do not use group by reflink because y want the pages not versions
  2639. $allpages=Database::query($sql);
  2640. while ($row=Database::fetch_array($allpages)) {
  2641. $total_student_assignment = $total_student_assignment+1;
  2642. }
  2643. //Current Wiki status add new pages
  2644. $sql = 'SELECT * FROM '.$tbl_wiki.'
  2645. WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.'
  2646. GROUP BY addlock';//group by because mark 0 in all vers, then always is ok
  2647. $allpages = Database::query($sql);
  2648. $wiki_add_lock = null;
  2649. while ($row=Database::fetch_array($allpages)) {
  2650. $wiki_add_lock=$row['addlock'];
  2651. }
  2652. if ($wiki_add_lock==1) {
  2653. $status_add_new_pag=get_lang('Yes');
  2654. } else {
  2655. $status_add_new_pag=get_lang('No');
  2656. }
  2657. //Creation date of the oldest wiki page and version
  2658. $first_wiki_date = null;
  2659. $sql = 'SELECT * FROM '.$tbl_wiki.'
  2660. WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.'
  2661. ORDER BY dtime ASC
  2662. LIMIT 1';
  2663. $allpages = Database::query($sql);
  2664. while ($row = Database::fetch_array($allpages)) {
  2665. $first_wiki_date = $row['dtime'];
  2666. }
  2667. // Date of publication of the latest wiki version.
  2668. $last_wiki_date = null;
  2669. $sql = 'SELECT * FROM '.$tbl_wiki.'
  2670. WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.'
  2671. ORDER BY dtime DESC
  2672. LIMIT 1';
  2673. $allpages = Database::query($sql);
  2674. while ($row=Database::fetch_array($allpages)) {
  2675. $last_wiki_date = $row['dtime'];
  2676. }
  2677. // Average score of all wiki pages. (If a page has not scored zero rated)
  2678. $media_score = 0;
  2679. $sql = "SELECT *, SUM(score) AS TOTAL_SCORE FROM ".$tbl_wiki."
  2680. WHERE c_id = $course_id AND ".$groupfilter.$condition_session."
  2681. GROUP BY reflink ";
  2682. //group by because mark in all versions, then always is ok.
  2683. // Do not use "count" because using "group by", would give a wrong value
  2684. $allpages = Database::query($sql);
  2685. $total_score = 0;
  2686. while ($row=Database::fetch_array($allpages)) {
  2687. $total_score = $total_score+$row['TOTAL_SCORE'];
  2688. }
  2689. if (!empty($total_pages)) {
  2690. $media_score = $total_score/$total_pages;
  2691. //put always this line alfter check num all pages
  2692. }
  2693. // Average user progress in his pages.
  2694. $media_progress=0;
  2695. $sql = 'SELECT *, SUM(progress) AS TOTAL_PROGRESS
  2696. FROM '.$tbl_wiki.' s1
  2697. WHERE s1.c_id = '.$course_id.' AND id=
  2698. (
  2699. SELECT MAX(s2.id) FROM '.$tbl_wiki.' s2
  2700. WHERE
  2701. s2.c_id = '.$course_id.' AND
  2702. s1.reflink = s2.reflink AND
  2703. '.$groupfilter.' AND
  2704. session_id='.$session_id.')';
  2705. // As the value is only the latest version I can not use group by
  2706. $allpages = Database::query($sql);
  2707. while ($row = Database::fetch_array($allpages)) {
  2708. $total_progress = $row['TOTAL_PROGRESS'];
  2709. }
  2710. if (!empty($total_pages)) {
  2711. $media_progress=$total_progress/$total_pages;
  2712. //put always this line alfter check num all pages
  2713. }
  2714. //Total users that have participated in the Wiki
  2715. $total_users=0;
  2716. $sql = 'SELECT * FROM '.$tbl_wiki.'
  2717. WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.'
  2718. GROUP BY user_id';
  2719. //as the mark of user it in all versions of the page, I can use group by to see the first
  2720. $allpages=Database::query($sql);
  2721. while ($row=Database::fetch_array($allpages)) {
  2722. $total_users = $total_users+1;
  2723. }
  2724. // Total of different IP addresses that have participated in the wiki
  2725. $total_ip = 0;
  2726. $sql = 'SELECT * FROM '.$tbl_wiki.'
  2727. WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.'
  2728. GROUP BY user_ip';
  2729. $allpages=Database::query($sql);
  2730. while ($row=Database::fetch_array($allpages)) {
  2731. $total_ip = $total_ip+1;
  2732. }
  2733. echo '<table class="data_table">';
  2734. echo '<thead>';
  2735. echo '<tr>';
  2736. echo '<th colspan="2">'.get_lang('General').'</th>';
  2737. echo '</tr>';
  2738. echo '</thead>';
  2739. echo '<tr>';
  2740. echo '<td>'.get_lang('StudentAddNewPages').'</td>';
  2741. echo '<td>'.$status_add_new_pag.'</td>';
  2742. echo '</tr>';
  2743. echo '<tr>';
  2744. echo '<td>'.get_lang('DateCreateOldestWikiPage').'</td>';
  2745. echo '<td>'.$first_wiki_date.'</td>';
  2746. echo '</tr>';
  2747. echo '<tr>';
  2748. echo '<td>'.get_lang('DateEditLatestWikiVersion').'</td>';
  2749. echo '<td>'.$last_wiki_date.'</td>';
  2750. echo '</tr>';
  2751. echo '<tr>';
  2752. echo '<td>'.get_lang('AverageScoreAllPages').'</td>';
  2753. echo '<td>'.$media_score.' %</td>';
  2754. echo '</tr>';
  2755. echo '<tr>';
  2756. echo '<td>'.get_lang('AverageMediaUserProgress').'</td>';
  2757. echo '<td>'.$media_progress.' %</td>';
  2758. echo '</tr>';
  2759. echo '<tr>';
  2760. echo '<td>'.get_lang('TotalWikiUsers').'</td>';
  2761. echo '<td>'.$total_users.'</td>';
  2762. echo '</tr>';
  2763. echo '<tr>';
  2764. echo '<td>'.get_lang('TotalIpAdress').'</td>';
  2765. echo '<td>'.$total_ip.'</td>';
  2766. echo '</tr>';
  2767. echo '</table>';
  2768. echo '<br/>';
  2769. echo '<table class="data_table">';
  2770. echo '<thead>';
  2771. echo '<tr>';
  2772. echo '<th colspan="2">'.get_lang('Pages').' '.get_lang('And').' '.get_lang('Versions').'</th>';
  2773. echo '</tr>';
  2774. echo '</thead>';
  2775. echo '<tr>';
  2776. echo '<td>'.get_lang('Pages').' - '.get_lang('NumContributions').'</td>';
  2777. echo '<td>'.$total_pages.' ('.get_lang('Versions').': '.$total_versions.')</td>';
  2778. echo '</tr>';
  2779. echo '<tr>';
  2780. echo '<td>'.get_lang('EmptyPages').'</td>';
  2781. echo '<td>'.$total_empty_content_lv.' ('.get_lang('Versions').': '.$total_empty_content.')</td>';
  2782. echo '</tr>';
  2783. echo '<tr>';
  2784. echo '<td>'.get_lang('NumAccess').'</td>';
  2785. echo '<td>'.$total_visits_lv.' ('.get_lang('Versions').': '.$total_visits.')</td>';
  2786. echo '</tr>';
  2787. echo '<tr>';
  2788. echo '<td>'.get_lang('TotalPagesEditedAtThisTime').'</td>';
  2789. echo '<td>'.$total_editing_now.'</td>';
  2790. echo '</tr>';
  2791. echo '<tr>';
  2792. echo '<td>'.get_lang('TotalHiddenPages').'</td>';
  2793. echo '<td>'.$total_hidden.'</td>';
  2794. echo '</tr>';
  2795. echo '<tr>';
  2796. echo '<td>'.get_lang('NumProtectedPages').'</td>';
  2797. echo '<td>'.$total_protected.'</td>';
  2798. echo '</tr>';
  2799. echo '<tr>';
  2800. echo '<td>'.get_lang('LockedDiscussPages').'</td>';
  2801. echo '<td>'.$total_lock_disc.'</td>';
  2802. echo '</tr>';
  2803. echo '<tr>';
  2804. echo '<td>'.get_lang('HiddenDiscussPages').'</td>';
  2805. echo '<td>'.$total_hidden_disc.'</td>';
  2806. echo '</tr>';
  2807. echo '<tr>';
  2808. echo '<td>'.get_lang('TotalComments').'</td>';
  2809. echo '<td>'.$total_comment_version.'</td>';
  2810. echo '</tr>';
  2811. echo '<tr>';
  2812. echo '<td>'.get_lang('TotalOnlyRatingByTeacher').'</td>';
  2813. echo '<td>'.$total_only_teachers_rating.'</td>';
  2814. echo '</tr>';
  2815. echo '<tr>';
  2816. echo '<td>'.get_lang('TotalRatingPeers').'</td>';
  2817. echo '<td>'.$total_rating_by_peers.'</td>';
  2818. echo '</tr>';
  2819. echo '<tr>';
  2820. echo '<td>'.get_lang('TotalTeacherAssignments').' - '.get_lang('PortfolioMode').'</td>';
  2821. echo '<td>'.$total_teacher_assignment.'</td>';
  2822. echo '</tr>';
  2823. echo '<tr>';
  2824. echo '<td>'.get_lang('TotalStudentAssignments').' - '.get_lang('PortfolioMode').'</td>';
  2825. echo '<td>'.$total_student_assignment.'</td>';
  2826. echo '</tr>';
  2827. echo '<tr>';
  2828. echo '<td>'.get_lang('TotalTask').' - '.get_lang('StandardMode').'</td>';
  2829. echo '<td>'.$total_task.'</td>';
  2830. echo '</tr>';
  2831. echo '</table>';
  2832. echo '<br/>';
  2833. echo '<table class="data_table">';
  2834. echo '<thead>';
  2835. echo '<tr>';
  2836. echo '<th colspan="3">'.get_lang('ContentPagesInfo').'</th>';
  2837. echo '</tr>';
  2838. echo '<tr>';
  2839. echo '<td></td>';
  2840. echo '<td>'.get_lang('InTheLastVersion').'</td>';
  2841. echo '<td>'.get_lang('InAllVersions').'</td>';
  2842. echo '</tr>';
  2843. echo '</thead>';
  2844. echo '<tr>';
  2845. echo '<td>'.get_lang('NumWords').'</td>';
  2846. echo '<td>'.$total_words_lv.'</td>';
  2847. echo '<td>'.$total_words.'</td>';
  2848. echo '</tr>';
  2849. echo '<tr>';
  2850. echo '<td>'.get_lang('NumlinksHtmlImagMedia').'</td>';
  2851. echo '<td>'.$total_links_lv.' ('.get_lang('Anchors').':'.$total_links_anchors_lv.', Mail:'.$total_links_mail_lv.', FTP:'.$total_links_ftp_lv.' IRC:'.$total_links_irc_lv.', News:'.$total_links_news_lv.', ... ) </td>';
  2852. echo '<td>'.$total_links.' ('.get_lang('Anchors').':'.$total_links_anchors.', Mail:'.$total_links_mail.', FTP:'.$total_links_ftp.', IRC:'.$total_links_irc.', News:'.$total_links_news.', ... ) </td>';
  2853. echo '</tr>';
  2854. echo '<tr>';
  2855. echo '<td>'.get_lang('NumWikilinks').'</td>';
  2856. echo '<td>'.$total_wlinks_lv.'</td>';
  2857. echo '<td>'.$total_wlinks.'</td>';
  2858. echo '</tr>';
  2859. echo '<tr>';
  2860. echo '<td>'.get_lang('NumImages').'</td>';
  2861. echo '<td>'.$total_images_lv.'</td>';
  2862. echo '<td>'.$total_images.'</td>';
  2863. echo '</tr>';
  2864. echo '<tr>';
  2865. echo '<td>'.get_lang('NumFlash').'</td>';
  2866. echo '<td>'.$total_flash_lv.'</td>';
  2867. echo '<td>'.$total_flash.'</td>';
  2868. echo '</tr>';
  2869. echo '<tr>';
  2870. echo '<td>'.get_lang('NumMp3').'</td>';
  2871. echo '<td>'.$total_mp3_lv.'</td>';
  2872. echo '<td>'.$total_mp3.'</td>';
  2873. echo '</tr>';
  2874. echo '<tr>';
  2875. echo '<td>'.get_lang('NumFlvVideo').'</td>';
  2876. echo '<td>'.$total_flv_lv.'</td>';
  2877. echo '<td>'.$total_flv.'</td>';
  2878. echo '</tr>';
  2879. echo '<tr>';
  2880. echo '<td>'.get_lang('NumYoutubeVideo').'</td>';
  2881. echo '<td>'.$total_youtube_lv.'</td>';
  2882. echo '<td>'.$total_youtube.'</td>';
  2883. echo '</tr>';
  2884. echo '<tr>';
  2885. echo '<td>'.get_lang('NumOtherAudioVideo').'</td>';
  2886. echo '<td>'.$total_multimedia_lv.'</td>';
  2887. echo '<td>'.$total_multimedia.'</td>';
  2888. echo '</tr>';
  2889. echo '<tr>';
  2890. echo '<td>'.get_lang('NumTables').'</td>';
  2891. echo '<td>'.$total_tables_lv.'</td>';
  2892. echo '<td>'.$total_tables.'</td>';
  2893. echo '</tr>';
  2894. echo '</table>';
  2895. }
  2896. /**
  2897. * @param string $action
  2898. */
  2899. public function getActiveUsers($action)
  2900. {
  2901. $tbl_wiki = $this->tbl_wiki;
  2902. $course_id = $this->course_id;
  2903. $condition_session = $this->condition_session;
  2904. $groupfilter = $this->groupfilter;
  2905. $_course = $this->courseInfo;
  2906. echo '<div class="actions">'.get_lang('MostActiveUsers').'</div>';
  2907. $sql = 'SELECT *, COUNT(*) AS NUM_EDIT FROM '.$tbl_wiki.'
  2908. WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.'
  2909. GROUP BY user_id';
  2910. $allpages = Database::query($sql);
  2911. //show table
  2912. if (Database::num_rows($allpages) > 0) {
  2913. while ($obj = Database::fetch_object($allpages)) {
  2914. $userinfo = api_get_user_info($obj->user_id);
  2915. $username = api_htmlentities(sprintf(get_lang('LoginX'), $userinfo['username']), ENT_QUOTES);
  2916. $row = array();
  2917. if ($obj->user_id != 0 && $userinfo !== false) {
  2918. $row[] = UserManager::getUserProfileLink($userinfo).'
  2919. <a href="'.api_get_self().'?cidReq='.$_course['code'].'&action=usercontrib&user_id='.urlencode($obj->user_id).
  2920. '&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities($_GET['group_id']).'"></a>';
  2921. } else {
  2922. $row[] = get_lang('Anonymous').' ('.$obj->user_ip.')';
  2923. }
  2924. $row[] ='<a href="'.api_get_self().'?cidReq='.$_course['code'].'&action=usercontrib&user_id='.urlencode($obj->user_id).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities($_GET['group_id']).'">'.$obj->NUM_EDIT.'</a>';
  2925. $rows[] = $row;
  2926. }
  2927. $table = new SortableTableFromArrayConfig($rows,1,10,'MostActiveUsersA_table','','','DESC');
  2928. $table->set_additional_parameters(
  2929. array(
  2930. 'cidReq' => Security::remove_XSS($_GET['cidReq']),
  2931. 'action' => Security::remove_XSS($action),
  2932. 'session_id' => Security::remove_XSS($_GET['session_id']),
  2933. 'group_id' => Security::remove_XSS($_GET['group_id'])
  2934. )
  2935. );
  2936. $table->set_header(0,get_lang('Author'), true);
  2937. $table->set_header(1,get_lang('Contributions'), true,array ('style' => 'width:30px;'));
  2938. $table->display();
  2939. }
  2940. }
  2941. /**
  2942. * @param string $page
  2943. */
  2944. public function getDiscuss($page)
  2945. {
  2946. $tbl_wiki = $this->tbl_wiki;
  2947. $course_id = $this->course_id;
  2948. $condition_session = $this->condition_session;
  2949. $groupfilter = $this->groupfilter;
  2950. $tbl_wiki_discuss = $this->tbl_wiki_discuss;
  2951. if (api_get_session_id()!=0 &&
  2952. api_is_allowed_to_session_edit(false,true)==false
  2953. ) {
  2954. api_not_allowed();
  2955. }
  2956. if (!$_GET['title']) {
  2957. Display::addFlash(Display::display_error_message(get_lang("MustSelectPage"), false, true));
  2958. return;
  2959. }
  2960. // First extract the date of last version
  2961. $sql = 'SELECT * FROM '.$tbl_wiki.'
  2962. WHERE
  2963. c_id = '.$course_id.' AND
  2964. reflink = "'.Database::escape_string($page).'" AND
  2965. '.$groupfilter.$condition_session.'
  2966. ORDER BY id DESC';
  2967. $result = Database::query($sql);
  2968. $row = Database::fetch_array($result);
  2969. $lastversiondate = api_get_local_time($row['dtime']);
  2970. $lastuserinfo = api_get_user_info($row['user_id']);
  2971. // Select page to discuss
  2972. $sql = 'SELECT * FROM '.$tbl_wiki.'
  2973. WHERE
  2974. c_id = '.$course_id.' AND
  2975. reflink="'.Database::escape_string($page).'" AND
  2976. '.$groupfilter.$condition_session.'
  2977. ORDER BY id ASC';
  2978. $result = Database::query($sql);
  2979. $row = Database::fetch_array($result);
  2980. $id = $row['id'];
  2981. $firstuserid = $row['user_id'];
  2982. if (isset($_POST['Submit']) && self::double_post($_POST['wpost_id'])) {
  2983. $dtime = api_get_utc_datetime();
  2984. $message_author = api_get_user_id();
  2985. $params = [
  2986. 'c_id' => $course_id,
  2987. 'publication_id' => $id,
  2988. 'userc_id' => $message_author,
  2989. 'comment' => $_POST['comment'],
  2990. 'p_score' => $_POST['rating'],
  2991. 'dtime' => $dtime
  2992. ];
  2993. $discussId = Database::insert($tbl_wiki_discuss, $params);
  2994. if ($discussId) {
  2995. $sql = "UPDATE $tbl_wiki_discuss SET id = iid WHERE iid = $discussId";
  2996. Database::query($sql);
  2997. }
  2998. self::check_emailcue($id, 'D', $dtime, $message_author);
  2999. header('Location: index.php?action=discuss&title='.api_htmlentities(urlencode($page)).'&'.api_get_cidreq());
  3000. exit;
  3001. }
  3002. //mode assignment: previous to show page type
  3003. $icon_assignment = null;
  3004. if ($row['assignment'] == 1) {
  3005. $icon_assignment = Display::return_icon('wiki_assignment.png', get_lang('AssignmentDescExtra'),'',ICON_SIZE_SMALL);
  3006. } elseif ($row['assignment'] == 2) {
  3007. $icon_assignment = Display::return_icon('wiki_work.png', get_lang('AssignmentWorkExtra'),'',ICON_SIZE_SMALL);
  3008. }
  3009. $countWPost = null;
  3010. $avg_WPost_score = null;
  3011. // Show title and form to discuss if page exist
  3012. if ($id != '') {
  3013. // Show discussion to students if isn't hidden.
  3014. // Show page to all teachers if is hidden.
  3015. // Mode assignments: If is hidden, show pages to student only if student is the author
  3016. if ($row['visibility_disc']==1 ||
  3017. api_is_allowed_to_edit(false,true) ||
  3018. api_is_platform_admin() ||
  3019. ($row['assignment']==2 && $row['visibility_disc']==0 && (api_get_user_id()==$row['user_id']))
  3020. ) {
  3021. echo '<div id="wikititle">';
  3022. // discussion action: protecting (locking) the discussion
  3023. $addlock_disc = null;
  3024. $lock_unlock_disc = null;
  3025. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  3026. if (self::check_addlock_discuss() == 1) {
  3027. $addlock_disc = Display::return_icon('unlock.png', get_lang('UnlockDiscussExtra'),'',ICON_SIZE_SMALL);
  3028. $lock_unlock_disc ='unlockdisc';
  3029. } else {
  3030. $addlock_disc = Display::return_icon('lock.png', get_lang('LockDiscussExtra'),'',ICON_SIZE_SMALL);
  3031. $lock_unlock_disc ='lockdisc';
  3032. }
  3033. }
  3034. echo '<span style="float:right">';
  3035. echo '<a href="index.php?action=discuss&actionpage='.$lock_unlock_disc.'&title='.api_htmlentities(urlencode($page)).'">'.$addlock_disc.'</a>';
  3036. echo '</span>';
  3037. // discussion action: visibility. Show discussion to students if isn't hidden. Show page to all teachers if is hidden.
  3038. $visibility_disc = null;
  3039. $hide_show_disc = null;
  3040. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  3041. if (self::check_visibility_discuss()==1) {
  3042. /// TODO: Fix Mode assignments: If is hidden, show discussion to student only if student is the author
  3043. $visibility_disc = Display::return_icon('visible.png', get_lang('ShowDiscussExtra'),'',ICON_SIZE_SMALL);
  3044. $hide_show_disc = 'hidedisc';
  3045. } else {
  3046. $visibility_disc = Display::return_icon('invisible.png', get_lang('HideDiscussExtra'),'',ICON_SIZE_SMALL);
  3047. $hide_show_disc = 'showdisc';
  3048. }
  3049. }
  3050. echo '<span style="float:right">';
  3051. echo '<a href="index.php?action=discuss&amp;actionpage='.$hide_show_disc.'&amp;title='.api_htmlentities(urlencode($page)).'">'.$visibility_disc.'</a>';
  3052. echo '</span>';
  3053. //discussion action: check add rating lock. Show/Hide list to rating for all student
  3054. $lock_unlock_rating_disc = null;
  3055. $ratinglock_disc = null;
  3056. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  3057. if (self::check_ratinglock_discuss() == 1) {
  3058. $ratinglock_disc = Display::return_icon('star.png', get_lang('UnlockRatingDiscussExtra'),'',ICON_SIZE_SMALL);
  3059. $lock_unlock_rating_disc = 'unlockrating';
  3060. } else {
  3061. $ratinglock_disc = Display::return_icon('star_na.png', get_lang('LockRatingDiscussExtra'),'',ICON_SIZE_SMALL);
  3062. $lock_unlock_rating_disc = 'lockrating';
  3063. }
  3064. }
  3065. echo '<span style="float:right">';
  3066. echo '<a href="index.php?action=discuss&amp;actionpage='.$lock_unlock_rating_disc.'&amp;title='.api_htmlentities(urlencode($page)).'">'.$ratinglock_disc.'</a>';
  3067. echo '</span>';
  3068. //discussion action: email notification
  3069. if (self::check_notify_discuss($page) == 1) {
  3070. $notify_disc= Display::return_icon('messagebox_info.png', get_lang('NotifyDiscussByEmail'),'',ICON_SIZE_SMALL);
  3071. $lock_unlock_notify_disc='unlocknotifydisc';
  3072. } else {
  3073. $notify_disc= Display::return_icon('mail.png', get_lang('CancelNotifyDiscussByEmail'),'',ICON_SIZE_SMALL);
  3074. $lock_unlock_notify_disc='locknotifydisc';
  3075. }
  3076. echo '<span style="float:right">';
  3077. echo '<a href="index.php?action=discuss&amp;actionpage='.$lock_unlock_notify_disc.'&amp;title='.api_htmlentities(urlencode($page)).'">'.$notify_disc.'</a>';
  3078. echo '</span>';
  3079. echo $icon_assignment.'&nbsp;&nbsp;&nbsp;'.api_htmlentities($row['title']);
  3080. if ($lastuserinfo !== false) {
  3081. echo ' (' . get_lang('MostRecentVersionBy') . ' ' . UserManager::getUserProfileLink($lastuserinfo) . ' ' . $lastversiondate . $countWPost . ')' . $avg_WPost_score . ' '; //TODO: read average score
  3082. }
  3083. echo '</div>';
  3084. if ($row['addlock_disc']==1 || api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  3085. //show comments but students can't add theirs
  3086. ?>
  3087. <form name="form1" method="post" action="">
  3088. <table>
  3089. <tr>
  3090. <td valign="top" ><?php echo get_lang('Comments');?>:</td>
  3091. <?php echo '<input type="hidden" name="wpost_id" value="'.md5(uniqid(rand(), true)).'">';//prevent double post ?>
  3092. <td><textarea name="comment" cols="80" rows="5" id="comment"></textarea></td>
  3093. </tr>
  3094. <tr>
  3095. <?php
  3096. //check if rating is allowed
  3097. if ($row['ratinglock_disc']==1 || api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  3098. ?>
  3099. <td><?php echo get_lang('Rating');?>: </td>
  3100. <td valign="top"><select name="rating" id="rating">
  3101. <option value="-" selected>-</option>
  3102. <option value="0">0</option>
  3103. <option value="1">1</option>
  3104. <option value="2">2</option>
  3105. <option value="3">3</option>
  3106. <option value="4">4</option>
  3107. <option value="5">5</option>
  3108. <option value="6">6</option>
  3109. <option value="7">7</option>
  3110. <option value="8">8</option>
  3111. <option value="9">9</option>
  3112. <option value="10">10</option>
  3113. </select></td>
  3114. <?php
  3115. } else {
  3116. echo '<input type=hidden name="rating" value="-">';// must pass a default value to avoid rate automatically
  3117. }
  3118. ?>
  3119. </tr>
  3120. <tr>
  3121. <td>&nbsp;</td>
  3122. <td> <?php echo '<button class="save" type="submit" name="Submit"> '.get_lang('Send').'</button>'; ?></td>
  3123. </tr>
  3124. </table>
  3125. </form>
  3126. <?php
  3127. }//end discuss lock
  3128. echo '<hr noshade size="1">';
  3129. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  3130. $sql = "SELECT *
  3131. FROM $tbl_wiki_discuss reviews, $user_table user
  3132. WHERE
  3133. reviews.c_id = $course_id AND
  3134. reviews.publication_id='".$id."' AND
  3135. user.user_id='".$firstuserid."'
  3136. ORDER BY reviews.id DESC";
  3137. $result = Database::query($sql);
  3138. $countWPost = Database::num_rows($result);
  3139. echo get_lang('NumComments').": ".$countWPost; //comment's numbers
  3140. $sql = "SELECT SUM(p_score) as sumWPost
  3141. FROM $tbl_wiki_discuss
  3142. WHERE c_id = $course_id AND publication_id = '".$id."' AND NOT p_score='-'
  3143. ORDER BY id DESC";
  3144. $result2 = Database::query($sql);
  3145. $row2 = Database::fetch_array($result2);
  3146. $sql = "SELECT * FROM $tbl_wiki_discuss
  3147. WHERE c_id = $course_id AND publication_id='".$id."' AND NOT p_score='-'";
  3148. $result3 = Database::query($sql);
  3149. $countWPost_score= Database::num_rows($result3);
  3150. echo ' - '.get_lang('NumCommentsScore').': '.$countWPost_score;//
  3151. if ($countWPost_score!=0) {
  3152. $avg_WPost_score = round($row2['sumWPost'] / $countWPost_score,2).' / 10';
  3153. } else {
  3154. $avg_WPost_score = $countWPost_score;
  3155. }
  3156. echo ' - '.get_lang('RatingMedia').': '.$avg_WPost_score; // average rating
  3157. $sql = 'UPDATE '.$tbl_wiki.' SET
  3158. score="'.Database::escape_string($avg_WPost_score).'"
  3159. WHERE
  3160. c_id = '.$course_id.' AND
  3161. reflink="'.Database::escape_string($page).'" AND
  3162. '.$groupfilter.$condition_session;
  3163. // check if work ok. TODO:
  3164. Database::query($sql);
  3165. echo '<hr noshade size="1">';
  3166. while ($row=Database::fetch_array($result)) {
  3167. $userinfo = api_get_user_info($row['userc_id']);
  3168. if (($userinfo['status'])=="5") {
  3169. $author_status=get_lang('Student');
  3170. } else {
  3171. $author_status=get_lang('Teacher');
  3172. }
  3173. $name = $userinfo['complete_name'];
  3174. $author_photo= '<img src="'.$userinfo['avatar'].'" alt="'.api_htmlentities($name).'" width="40" height="50" align="top" title="'.api_htmlentities($name).'" />';
  3175. //stars
  3176. $p_score = $row['p_score'];
  3177. switch ($p_score) {
  3178. case 0:
  3179. $imagerating = Display::return_icon('rating/stars_0.gif');
  3180. break;
  3181. case 1:
  3182. $imagerating = Display::return_icon('rating/stars_5.gif');
  3183. break;
  3184. case 2:
  3185. $imagerating = Display::return_icon('rating/stars_10.gif');
  3186. break;
  3187. case 3:
  3188. $imagerating = Display::return_icon('rating/stars_15.gif');
  3189. break;
  3190. case 4:
  3191. $imagerating = Display::return_icon('rating/stars_20.gif');
  3192. break;
  3193. case 5:
  3194. $imagerating = Display::return_icon('rating/stars_25.gif');
  3195. break;
  3196. case 6:
  3197. $imagerating = Display::return_icon('rating/stars_30.gif');
  3198. break;
  3199. case 7:
  3200. $imagerating = Display::return_icon('rating/stars_35.gif');
  3201. break;
  3202. case 8:
  3203. $imagerating = Display::return_icon('rating/stars_40.gif');
  3204. break;
  3205. case 9:
  3206. $imagerating = Display::return_icon('rating/stars_45.gif');
  3207. break;
  3208. case 10:
  3209. $imagerating = Display::return_icon('rating/stars_50.gif');
  3210. break;
  3211. }
  3212. echo '<p><table>';
  3213. echo '<tr>';
  3214. echo '<td rowspan="2">'.$author_photo.'</td>';
  3215. $userProfile = '';
  3216. if ($userinfo !== false) {
  3217. $userProfile = UserManager::getUserProfileLink($userinfo);
  3218. }
  3219. echo '<td style=" color:#999999">' . $userProfile . ' (' . $author_status . ') '.
  3220. api_get_local_time($row['dtime'], null, date_default_timezone_get()).
  3221. ' - '.get_lang('Rating').': '.$row['p_score'].' '.$imagerating.' </td>';
  3222. echo '</tr>';
  3223. echo '<tr>';
  3224. echo '<td>'.api_htmlentities($row['comment']).'</td>';
  3225. echo '</tr>';
  3226. echo "</table>";
  3227. }
  3228. } else {
  3229. Display::addFlash(Display::display_warning_message(get_lang('LockByTeacher'), false, true));
  3230. }
  3231. } else {
  3232. Display::addFlash(Display::display_normal_message(get_lang('DiscussNotAvailable'), false, true));
  3233. }
  3234. }
  3235. /**
  3236. * Show all pages
  3237. */
  3238. public function allPages($action)
  3239. {
  3240. $tbl_wiki = $this->tbl_wiki;
  3241. $course_id = $this->course_id;
  3242. $session_id = $this->session_id;
  3243. $groupfilter = $this->groupfilter;
  3244. $_course = $this->courseInfo;
  3245. echo '<div class="actions">'.get_lang('AllPages');
  3246. // menu delete all wiki
  3247. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  3248. echo ' <a href="index.php?action=deletewiki&'.api_get_cidreq().'">'.
  3249. Display::return_icon('delete.png', get_lang('DeleteWiki'), '', ICON_SIZE_MEDIUM).'</a>';
  3250. }
  3251. echo '</div>';
  3252. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) { //only by professors if page is hidden
  3253. $sql = 'SELECT *
  3254. FROM '.$tbl_wiki.' s1
  3255. WHERE s1.c_id = '.$course_id.' AND id=(
  3256. SELECT MAX(s2.id) FROM '.$tbl_wiki.' s2
  3257. WHERE s2.c_id = '.$course_id.' AND s1.reflink = s2.reflink AND '.$groupfilter.' AND session_id='.$session_id.')';
  3258. // warning don't use group by reflink because does not return the last version
  3259. } else {
  3260. $sql = 'SELECT * FROM '.$tbl_wiki.' s1
  3261. WHERE visibility=1 AND s1.c_id = '.$course_id.' AND id=(
  3262. SELECT MAX(s2.id) FROM '.$tbl_wiki.' s2
  3263. WHERE s2.c_id = '.$course_id.' AND s1.reflink = s2.reflink AND '.$groupfilter.' AND session_id='.$session_id.')';
  3264. // warning don't use group by reflink because does not return the last version
  3265. }
  3266. $allpages = Database::query($sql);
  3267. //show table
  3268. if (Database::num_rows($allpages) > 0) {
  3269. while ($obj = Database::fetch_object($allpages)) {
  3270. //get author
  3271. $userinfo = api_get_user_info($obj->user_id);
  3272. $username = api_htmlentities(sprintf(get_lang('LoginX'), $userinfo['username']), ENT_QUOTES);
  3273. //get type assignment icon
  3274. if ($obj->assignment==1) {
  3275. $ShowAssignment=Display::return_icon('wiki_assignment.png', get_lang('AssignmentDesc'),'',ICON_SIZE_SMALL);
  3276. } elseif ($obj->assignment==2) {
  3277. $ShowAssignment=Display::return_icon('wiki_work.png', get_lang('AssignmentWork'),'',ICON_SIZE_SMALL);
  3278. } elseif ($obj->assignment==0) {
  3279. $ShowAssignment = Display::return_icon('px_transparent.gif');
  3280. }
  3281. //get icon task
  3282. if (!empty($obj->task)) {
  3283. $icon_task = Display::return_icon('wiki_task.png', get_lang('StandardTask'), '', ICON_SIZE_SMALL);
  3284. } else {
  3285. $icon_task = Display::return_icon('px_transparent.gif');
  3286. }
  3287. $row = array();
  3288. $row[] = $ShowAssignment.$icon_task;
  3289. $row[] = '<a href="'.api_get_self().'?cidReq='.$_course['code'].'&action=showpage&title='.api_htmlentities(urlencode($obj->reflink)).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities($_GET['group_id']).'">
  3290. '.api_htmlentities($obj->title).'</a>';
  3291. if ($userinfo !== false) {
  3292. $row[] = UserManager::getUserProfileLink($userinfo);
  3293. }
  3294. else {
  3295. $row[] = get_lang('Anonymous').' ('.api_htmlentities($obj->user_ip).')';
  3296. }
  3297. $row[] = api_get_local_time($obj->dtime, null, date_default_timezone_get());
  3298. $showdelete = '';
  3299. if (api_is_allowed_to_edit(false,true)|| api_is_platform_admin()) {
  3300. $showdelete =' <a href="'.api_get_self().'?cidReq='.$_course['code'].'&action=delete&title='.api_htmlentities(urlencode($obj->reflink)).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities($_GET['group_id']).'">'.
  3301. Display::return_icon('delete.png', get_lang('Delete'),'',ICON_SIZE_SMALL);
  3302. }
  3303. if (api_is_allowed_to_session_edit(false,true) ) {
  3304. $row[] = '<a href="'.api_get_self().'?cidReq='.$_course['code'].'&action=edit&title='.api_htmlentities(urlencode($obj->reflink)).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities($_GET['group_id']).'">'.
  3305. Display::return_icon('edit.png', get_lang('EditPage'),'',ICON_SIZE_SMALL).'</a> <a href="'.api_get_self().'?cidReq='.$_course['code'].'&action=discuss&title='.api_htmlentities(urlencode($obj->reflink)).'&group_id='.api_htmlentities($_GET['group_id']).'">'.
  3306. Display::return_icon('discuss.png', get_lang('Discuss'),'',ICON_SIZE_SMALL).'</a> <a href="'.api_get_self().'?cidReq='.$_course['code'].'&action=history&title='.api_htmlentities(urlencode($obj->reflink)).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities($_GET['group_id']).'">'.
  3307. Display::return_icon('history.png', get_lang('History'),'',ICON_SIZE_SMALL).'</a>
  3308. <a href="'.api_get_self().'?cidReq='.$_course['code'].'&action=links&title='.api_htmlentities(urlencode($obj->reflink)).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities($_GET['group_id']).'">'.
  3309. Display::return_icon('what_link_here.png', get_lang('LinksPages'),'',ICON_SIZE_SMALL).'</a>'.$showdelete;
  3310. }
  3311. $rows[] = $row;
  3312. }
  3313. $table = new SortableTableFromArrayConfig($rows,1,10,'AllPages_table','','','ASC');
  3314. $table->set_additional_parameters(array('cidReq' =>Security::remove_XSS($_GET['cidReq']),'action'=>Security::remove_XSS($action),'group_id'=>Security::remove_XSS($_GET['group_id'])));
  3315. $table->set_header(0,get_lang('Type'), true, array ('style' => 'width:30px;'));
  3316. $table->set_header(1,get_lang('Title'), true);
  3317. $table->set_header(2,get_lang('Author').' ('.get_lang('LastVersion').')', true);
  3318. $table->set_header(3,get_lang('Date').' ('.get_lang('LastVersion').')', true);
  3319. if (api_is_allowed_to_session_edit(false,true) ) {
  3320. $table->set_header(4,get_lang('Actions'), true, array ('style' => 'width:130px;'));
  3321. }
  3322. $table->display();
  3323. }
  3324. }
  3325. /**
  3326. * Get recent changes
  3327. * @param string $page
  3328. * @param string $action
  3329. *
  3330. */
  3331. public function recentChanges($page, $action)
  3332. {
  3333. $tbl_wiki = $this->tbl_wiki;
  3334. $course_id = $this->course_id;
  3335. $condition_session = $this->condition_session;
  3336. $groupfilter = $this->groupfilter;
  3337. $tbl_wiki_conf = $this->tbl_wiki_conf;
  3338. if (api_is_allowed_to_session_edit(false,true) ) {
  3339. if (self::check_notify_all()==1) {
  3340. $notify_all= Display::return_icon('messagebox_info.png', get_lang('NotifyByEmail'),'',ICON_SIZE_SMALL).' '.get_lang('NotNotifyChanges');
  3341. $lock_unlock_notify_all='unlocknotifyall';
  3342. } else {
  3343. $notify_all=Display::return_icon('mail.png', get_lang('CancelNotifyByEmail'),'',ICON_SIZE_SMALL).' '.get_lang('NotifyChanges');
  3344. $lock_unlock_notify_all='locknotifyall';
  3345. }
  3346. }
  3347. echo '<div class="actions"><span style="float: right;">';
  3348. echo '<a href="index.php?action=recentchanges&amp;actionpage='.$lock_unlock_notify_all.'&amp;title='.api_htmlentities(urlencode($page)).'">'.$notify_all.'</a>';
  3349. echo '</span>'.get_lang('RecentChanges').'</div>';
  3350. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) { //only by professors if page is hidden
  3351. $sql = 'SELECT * FROM '.$tbl_wiki.', '.$tbl_wiki_conf.'
  3352. WHERE '.$tbl_wiki_conf.'.c_id= '.$course_id.' AND
  3353. '.$tbl_wiki.'.c_id= '.$course_id.' AND
  3354. '.$tbl_wiki_conf.'.page_id='.$tbl_wiki.'.page_id AND
  3355. '.$tbl_wiki.'.'.$groupfilter.$condition_session.'
  3356. ORDER BY dtime DESC'; // new version
  3357. } else {
  3358. $sql = 'SELECT *
  3359. FROM '.$tbl_wiki.'
  3360. WHERE
  3361. c_id = '.$course_id.' AND
  3362. '.$groupfilter.$condition_session.' AND
  3363. visibility=1
  3364. ORDER BY dtime DESC';
  3365. // old version TODO: Replace by the bottom line
  3366. }
  3367. $allpages = Database::query($sql);
  3368. //show table
  3369. if (Database::num_rows($allpages) > 0) {
  3370. $rows = array();
  3371. while ($obj = Database::fetch_object($allpages)) {
  3372. //get author
  3373. $userinfo = api_get_user_info($obj->user_id);
  3374. //get type assignment icon
  3375. if ($obj->assignment==1) {
  3376. $ShowAssignment=Display::return_icon('wiki_assignment.png', get_lang('AssignmentDesc'),'',ICON_SIZE_SMALL);
  3377. } elseif ($obj->assignment==2) {
  3378. $ShowAssignment=Display::return_icon('wiki_work.png', get_lang('AssignmentWork'),'',ICON_SIZE_SMALL);
  3379. } elseif ($obj->assignment==0) {
  3380. $ShowAssignment=Display::return_icon('px_transparent.gif');
  3381. }
  3382. // Get icon task
  3383. if (!empty($obj->task)) {
  3384. $icon_task=Display::return_icon('wiki_task.png', get_lang('StandardTask'),'',ICON_SIZE_SMALL);
  3385. } else {
  3386. $icon_task=Display::return_icon('px_transparent.gif');
  3387. }
  3388. $row = array();
  3389. $row[] = api_get_local_time($obj->dtime, null, date_default_timezone_get());
  3390. $row[] = $ShowAssignment.$icon_task;
  3391. $row[] = '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=showpage&title='.api_htmlentities(urlencode($obj->reflink)).'&amp;view='.$obj->id.'&session_id='.api_get_session_id().'&group_id='.api_get_group_id().'">'.
  3392. api_htmlentities($obj->title).'</a>';
  3393. $row[] = $obj->version>1 ? get_lang('EditedBy') : get_lang('AddedBy');
  3394. if ($userinfo !== false) {
  3395. $row[] = UserManager::getUserProfileLink($userinfo);
  3396. } else {
  3397. $row[] = get_lang('Anonymous').' ('.api_htmlentities($obj->user_ip).')';
  3398. }
  3399. $rows[] = $row;
  3400. }
  3401. $table = new SortableTableFromArrayConfig($rows,0,10,'RecentPages_table','','','DESC');
  3402. $table->set_additional_parameters(
  3403. array(
  3404. 'cidReq' =>api_get_course_id(),
  3405. 'action'=>Security::remove_XSS($action),
  3406. 'session_id' => api_get_session_id(),
  3407. 'group_id' => api_get_group_id()
  3408. )
  3409. );
  3410. $table->set_header(0,get_lang('Date'), true, array ('style' => 'width:200px;'));
  3411. $table->set_header(1,get_lang('Type'), true, array ('style' => 'width:30px;'));
  3412. $table->set_header(2,get_lang('Title'), true);
  3413. $table->set_header(3,get_lang('Actions'), true, array ('style' => 'width:80px;'));
  3414. $table->set_header(4,get_lang('Author'), true);
  3415. $table->display();
  3416. }
  3417. }
  3418. /**
  3419. * What links here. Show pages that have linked this page
  3420. *
  3421. * @param string $page
  3422. */
  3423. public function getLinks($page)
  3424. {
  3425. $tbl_wiki = $this->tbl_wiki;
  3426. $course_id = $this->course_id;
  3427. $condition_session = $this->condition_session;
  3428. $groupfilter = $this->groupfilter;
  3429. $_course = $this->courseInfo;
  3430. $action = $this->action;
  3431. if (!$_GET['title']) {
  3432. Display::addFlash(Display::display_error_message(get_lang("MustSelectPage"), false, true));
  3433. } else {
  3434. $sql = 'SELECT * FROM '.$tbl_wiki.'
  3435. WHERE
  3436. c_id = '.$course_id.' AND
  3437. reflink="'.Database::escape_string($page).'" AND
  3438. '.$groupfilter.$condition_session.'';
  3439. $result = Database::query($sql);
  3440. $row = Database::fetch_array($result);
  3441. //get type assignment icon
  3442. $ShowAssignment = '';
  3443. if ($row['assignment']==1) {
  3444. $ShowAssignment=Display::return_icon('wiki_assignment.png', get_lang('AssignmentDesc'),'',ICON_SIZE_SMALL);
  3445. } elseif ($row['assignment']==2) {
  3446. $ShowAssignment=Display::return_icon('wiki_work.png', get_lang('AssignmentWork'),'',ICON_SIZE_SMALL);
  3447. } elseif ($row['assignment']==0) {
  3448. $ShowAssignment=Display::return_icon('px_transparent.gif');
  3449. }
  3450. //fix Title to reflink (link Main Page)
  3451. if ($page==get_lang('DefaultTitle')) {
  3452. $page='index';
  3453. }
  3454. echo '<div id="wikititle">';
  3455. echo get_lang('LinksPagesFrom').': '.$ShowAssignment.' <a href="'.api_get_self().'?cidReq='.$_course['code'].'&action=showpage&title='.api_htmlentities(urlencode($page)).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities($_GET['group_id']).'">'.
  3456. api_htmlentities($row['title']).'</a>';
  3457. echo '</div>';
  3458. //fix index to title Main page into linksto
  3459. if ($page == 'index') {
  3460. $page = str_replace(' ','_',get_lang('DefaultTitle'));
  3461. }
  3462. //table
  3463. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  3464. //only by professors if page is hidden
  3465. $sql = "SELECT * FROM ".$tbl_wiki." s1
  3466. WHERE s1.c_id = $course_id AND linksto LIKE '%".Database::escape_string($page)."%' AND id=(
  3467. SELECT MAX(s2.id) FROM ".$tbl_wiki." s2
  3468. WHERE s2.c_id = $course_id AND s1.reflink = s2.reflink AND ".$groupfilter.$condition_session.")";
  3469. //add blank space after like '%" " %' to identify each word
  3470. } else {
  3471. $sql = "SELECT * FROM ".$tbl_wiki." s1
  3472. WHERE s1.c_id = $course_id AND visibility=1 AND linksto LIKE '%".Database::escape_string($page)."%' AND id=(
  3473. SELECT MAX(s2.id) FROM ".$tbl_wiki." s2
  3474. WHERE s2.c_id = $course_id AND s1.reflink = s2.reflink AND ".$groupfilter.$condition_session.")";
  3475. //add blank space after like '%" " %' to identify each word
  3476. }
  3477. $allpages = Database::query($sql);
  3478. //show table
  3479. if (Database::num_rows($allpages) > 0) {
  3480. $rows = array();
  3481. while ($obj = Database::fetch_object($allpages)) {
  3482. //get author
  3483. $userinfo = api_get_user_info($obj->user_id);
  3484. //get time
  3485. $year = substr($obj->dtime, 0, 4);
  3486. $month = substr($obj->dtime, 5, 2);
  3487. $day = substr($obj->dtime, 8, 2);
  3488. $hours = substr($obj->dtime, 11,2);
  3489. $minutes = substr($obj->dtime, 14,2);
  3490. $seconds = substr($obj->dtime, 17,2);
  3491. //get type assignment icon
  3492. if ($obj->assignment == 1) {
  3493. $ShowAssignment = Display::return_icon('wiki_assignment.png', get_lang('AssignmentDesc'), '', ICON_SIZE_SMALL);
  3494. } elseif ($obj->assignment == 2) {
  3495. $ShowAssignment = Display::return_icon('wiki_work.png', get_lang('AssignmentWork'), '', ICON_SIZE_SMALL);
  3496. } elseif ($obj->assignment == 0) {
  3497. $ShowAssignment = Display::return_icon('px_transparent.gif');
  3498. }
  3499. $row = array();
  3500. $row[] =$ShowAssignment;
  3501. $row[] = '<a href="'.api_get_self().'?cidReq='.$_course['code'].'&action=showpage&title='.api_htmlentities(urlencode($obj->reflink)).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities($_GET['group_id']).'">'.
  3502. api_htmlentities($obj->title).'</a>';
  3503. if ($userinfo !== false) {
  3504. $row[] = UserManager::getUserProfileLink($userinfo);
  3505. }
  3506. else {
  3507. $row[] = get_lang('Anonymous').' ('.$obj->user_ip.')';
  3508. }
  3509. $row[] = $year.'-'.$month.'-'.$day.' '.$hours.":".$minutes.":".$seconds;
  3510. $rows[] = $row;
  3511. }
  3512. $table = new SortableTableFromArrayConfig(
  3513. $rows,
  3514. 1,
  3515. 10,
  3516. 'AllPages_table',
  3517. '',
  3518. '',
  3519. 'ASC'
  3520. );
  3521. $table->set_additional_parameters(
  3522. array(
  3523. 'cidReq' => Security::remove_XSS($_GET['cidReq']),
  3524. 'action' => Security::remove_XSS($action),
  3525. 'group_id' => intval($_GET['group_id']),
  3526. )
  3527. );
  3528. $table->set_header(
  3529. 0,
  3530. get_lang('Type'),
  3531. true,
  3532. array('style' => 'width:30px;')
  3533. );
  3534. $table->set_header(1, get_lang('Title'), true);
  3535. $table->set_header(2, get_lang('Author'), true);
  3536. $table->set_header(3, get_lang('Date'), true);
  3537. $table->display();
  3538. }
  3539. }
  3540. }
  3541. /**
  3542. * @param string $action
  3543. */
  3544. public function getSearchPages($action)
  3545. {
  3546. echo '<div class="actions">'.get_lang('SearchPages').'</div>';
  3547. if (isset($_GET['mode_table'])) {
  3548. if (!isset($_GET['SearchPages_table_page_nr'])) {
  3549. $_GET['search_term'] = isset($_POST['search_term']) ? $_POST['search_term'] : '';
  3550. $_GET['search_content'] = isset($_POST['search_content']) ? $_POST['search_content'] : '';
  3551. $_GET['all_vers'] = isset($_POST['all_vers']) ? $_POST['all_vers'] : '';
  3552. }
  3553. self::display_wiki_search_results(
  3554. $_GET['search_term'],
  3555. $_GET['search_content'],
  3556. $_GET['all_vers']
  3557. );
  3558. } else {
  3559. // initiate the object
  3560. $form = new FormValidator('wiki_search',
  3561. 'post',
  3562. api_get_self().'?cidReq='.api_get_course_id().'&action='.api_htmlentities($action).'&session_id='.api_get_session_id().'&group_id='.api_get_group_id().'&mode_table=yes1'
  3563. );
  3564. // Setting the form elements
  3565. $form->addText('search_term', get_lang('SearchTerm'), true, array('autofocus' => 'autofocus'));
  3566. $form->addElement('checkbox', 'search_content', null, get_lang('AlsoSearchContent'));
  3567. $form->addElement('checkbox', 'all_vers', null, get_lang('IncludeAllVersions'));
  3568. $form->addButtonSearch(get_lang('Search'), 'SubmitWikiSearch');
  3569. // setting the rules
  3570. $form->addRule('search_term', get_lang('TooShort'), 'minlength', 3); //TODO: before fixing the pagination rules worked, not now
  3571. if ($form->validate()) {
  3572. $form->display();
  3573. $values = $form->exportValues();
  3574. self::display_wiki_search_results(
  3575. $values['search_term'],
  3576. $values['search_content'],
  3577. $values['all_vers']
  3578. );
  3579. } else {
  3580. $form->display();
  3581. }
  3582. }
  3583. }
  3584. /**
  3585. * @param int $userId
  3586. * @param string $action
  3587. */
  3588. public function getUserContributions($userId, $action)
  3589. {
  3590. $_course = $this->courseInfo;
  3591. $tbl_wiki = $this->tbl_wiki;
  3592. $course_id = $this->course_id;
  3593. $condition_session = $this->condition_session;
  3594. $groupfilter = $this->groupfilter;
  3595. $userId = intval($userId);
  3596. $userinfo = api_get_user_info($userId);
  3597. if ($userinfo !== false) {
  3598. echo '<div class="actions">' . get_lang('UserContributions') . ': ' . UserManager::getUserProfileLink($userinfo) .
  3599. '<a href="' . api_get_self() . '?cidReq=' . $_course['code'] . '&action=usercontrib&user_id=' . $userId .
  3600. '&session_id=' . $this->session_id . '&group_id=' . $this->group_id . '">' .
  3601. '</a></div>';
  3602. }
  3603. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  3604. //only by professors if page is hidden
  3605. $sql = 'SELECT * FROM '.$tbl_wiki.'
  3606. WHERE
  3607. c_id = '.$course_id.' AND
  3608. '.$groupfilter.$condition_session.' AND
  3609. user_id="'.$userId.'"';
  3610. } else {
  3611. $sql = 'SELECT * FROM '.$tbl_wiki.'
  3612. WHERE
  3613. c_id = '.$course_id.' AND
  3614. '.$groupfilter.$condition_session.' AND
  3615. user_id="'.$userId.'" AND
  3616. visibility=1';
  3617. }
  3618. $allpages = Database::query($sql);
  3619. //show table
  3620. if (Database::num_rows($allpages) > 0) {
  3621. $rows = array();
  3622. while ($obj = Database::fetch_object($allpages)) {
  3623. // Get time
  3624. $year = substr($obj->dtime, 0, 4);
  3625. $month = substr($obj->dtime, 5, 2);
  3626. $day = substr($obj->dtime, 8, 2);
  3627. $hours = substr($obj->dtime, 11,2);
  3628. $minutes = substr($obj->dtime, 14,2);
  3629. $seconds = substr($obj->dtime, 17,2);
  3630. //get type assignment icon
  3631. $ShowAssignment = '';
  3632. if ($obj->assignment==1) {
  3633. $ShowAssignment=Display::return_icon('wiki_assignment.png', get_lang('AssignmentDescExtra'),'',ICON_SIZE_SMALL);
  3634. } elseif ($obj->assignment==2) {
  3635. $ShowAssignment=Display::return_icon('wiki_work.png', get_lang('AssignmentWork'),'',ICON_SIZE_SMALL);
  3636. } elseif ($obj->assignment==0) {
  3637. $ShowAssignment= Display::return_icon('px_transparent.gif');
  3638. }
  3639. $row = array();
  3640. $row[] = $year.'-'.$month.'-'.$day.' '.$hours.":".$minutes.":".$seconds;
  3641. $row[] = $ShowAssignment;
  3642. $row[] = '<a href="'.api_get_self().'?cidReq='.$_course['code'].'&action=showpage&title='.api_htmlentities(urlencode($obj->reflink)).'&view='.$obj->id.'&session_id='.api_get_session_id().'&group_id='.api_get_group_id().'">'.
  3643. api_htmlentities($obj->title).'</a>';
  3644. $row[] = Security::remove_XSS($obj->version);
  3645. $row[] = Security::remove_XSS($obj->comment);
  3646. $row[] = Security::remove_XSS($obj->progress).' %';
  3647. $row[] = Security::remove_XSS($obj->score);
  3648. $rows[] = $row;
  3649. }
  3650. $table = new SortableTableFromArrayConfig($rows,2,10,'UsersContributions_table','','','ASC');
  3651. $table->set_additional_parameters(
  3652. array(
  3653. 'cidReq' => Security::remove_XSS($_GET['cidReq']),
  3654. 'action' => Security::remove_XSS($action),
  3655. 'user_id' => intval($userId),
  3656. 'session_id' => intval($_GET['session_id']),
  3657. 'group_id' => intval($_GET['group_id']),
  3658. )
  3659. );
  3660. $table->set_header(0,get_lang('Date'), true, array ('style' => 'width:200px;'));
  3661. $table->set_header(1,get_lang('Type'), true, array ('style' => 'width:30px;'));
  3662. $table->set_header(2,get_lang('Title'), true, array ('style' => 'width:200px;'));
  3663. $table->set_header(3,get_lang('Version'), true, array ('style' => 'width:30px;'));
  3664. $table->set_header(4,get_lang('Comment'), true, array ('style' => 'width:200px;'));
  3665. $table->set_header(5,get_lang('Progress'), true, array ('style' => 'width:30px;'));
  3666. $table->set_header(6,get_lang('Rating'), true, array ('style' => 'width:30px;'));
  3667. $table->display();
  3668. }
  3669. }
  3670. /**
  3671. * @param string $action
  3672. */
  3673. public function getMostChangedPages($action)
  3674. {
  3675. $_course = $this->courseInfo;
  3676. $tbl_wiki = $this->tbl_wiki;
  3677. $course_id = $this->course_id;
  3678. $condition_session = $this->condition_session;
  3679. $groupfilter = $this->groupfilter;
  3680. echo '<div class="actions">'.get_lang('MostChangedPages').'</div>';
  3681. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) { //only by professors if page is hidden
  3682. $sql = 'SELECT *, MAX(version) AS MAX FROM '.$tbl_wiki.'
  3683. WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.'
  3684. GROUP BY reflink';//TODO:check MAX and group by return last version
  3685. } else {
  3686. $sql = 'SELECT *, MAX(version) AS MAX FROM '.$tbl_wiki.'
  3687. WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.' AND visibility=1
  3688. GROUP BY reflink'; //TODO:check MAX and group by return last version
  3689. }
  3690. $allpages = Database::query($sql);
  3691. //show table
  3692. if (Database::num_rows($allpages) > 0) {
  3693. $rows = array();
  3694. while ($obj = Database::fetch_object($allpages)) {
  3695. //get type assignment icon
  3696. $ShowAssignment = '';
  3697. if ($obj->assignment==1) {
  3698. $ShowAssignment=Display::return_icon('wiki_assignment.png', get_lang('AssignmentDesc'),'',ICON_SIZE_SMALL);
  3699. } elseif ($obj->assignment==2) {
  3700. $ShowAssignment=Display::return_icon('wiki_work.png', get_lang('AssignmentWork'),'',ICON_SIZE_SMALL);
  3701. } elseif ($obj->assignment==0) {
  3702. $ShowAssignment= Display::return_icon('px_transparent.gif');
  3703. }
  3704. $row = array();
  3705. $row[] = $ShowAssignment;
  3706. $row[] = '<a href="'.api_get_self().'?cidReq='.$_course['code'].'&action=showpage&title='.api_htmlentities(urlencode($obj->reflink)).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities($_GET['group_id']).'">'.
  3707. api_htmlentities($obj->title).'</a>';
  3708. $row[] = $obj->MAX;
  3709. $rows[] = $row;
  3710. }
  3711. $table = new SortableTableFromArrayConfig(
  3712. $rows,
  3713. 2,
  3714. 10,
  3715. 'MostChangedPages_table',
  3716. '',
  3717. '',
  3718. 'DESC'
  3719. );
  3720. $table->set_additional_parameters(
  3721. array(
  3722. 'cidReq' => Security::remove_XSS($_GET['cidReq']),
  3723. 'action' => Security::remove_XSS($action),
  3724. 'session_id' => intval($_GET['session_id']),
  3725. 'group_id' => intval($_GET['group_id']),
  3726. )
  3727. );
  3728. $table->set_header(0,get_lang('Type'), true, array ('style' => 'width:30px;'));
  3729. $table->set_header(1,get_lang('Title'), true);
  3730. $table->set_header(2,get_lang('Changes'), true);
  3731. $table->display();
  3732. }
  3733. }
  3734. /**
  3735. * Restore page
  3736. * @return bool
  3737. */
  3738. public function restorePage()
  3739. {
  3740. $userId = api_get_user_id();
  3741. $_course = $this->courseInfo;
  3742. $current_row = $this->getWikiData();
  3743. $last_row = $this->getLastWikiData($this->page);
  3744. if (empty($last_row)) {
  3745. return false;
  3746. }
  3747. $PassEdit = false;
  3748. /* Only teachers and platform admin can edit the index page.
  3749. Only teachers and platform admin can edit an assignment teacher*/
  3750. if (($current_row['reflink'] == 'index' || $current_row['reflink'] == '' || $current_row['assignment'] == 1) &&
  3751. (!api_is_allowed_to_edit(false,true) && $this->group_id == 0)
  3752. ) {
  3753. Display::addFlash(
  3754. Display::display_normal_message(get_lang('OnlyEditPagesCourseManager'), false, true)
  3755. );
  3756. } else {
  3757. // check if is a wiki group
  3758. if ($current_row['group_id'] != 0) {
  3759. //Only teacher, platform admin and group members can edit a wiki group
  3760. if (api_is_allowed_to_edit(false,true) ||
  3761. api_is_platform_admin() ||
  3762. GroupManager :: is_user_in_group($userId, $this->group_id) ||
  3763. api_is_allowed_in_course()
  3764. ) {
  3765. $PassEdit = true;
  3766. } else {
  3767. Display::addFlash(
  3768. Display::display_normal_message(get_lang('OnlyEditPagesGroupMembers'), false, true)
  3769. );
  3770. }
  3771. } else {
  3772. $PassEdit = true;
  3773. }
  3774. // check if is an assignment
  3775. //$icon_assignment = null;
  3776. if ($current_row['assignment'] == 1) {
  3777. Display::addFlash(Display::display_normal_message(get_lang('EditAssignmentWarning'), false, true));
  3778. } elseif($current_row['assignment']==2) {
  3779. if (($userId == $current_row['user_id'])==false) {
  3780. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  3781. $PassEdit = true;
  3782. } else {
  3783. Display::addFlash(Display::display_warning_message(get_lang('LockByTeacher'), false, true));
  3784. $PassEdit = false;
  3785. }
  3786. } else {
  3787. $PassEdit = true;
  3788. }
  3789. }
  3790. //show editor if edit is allowed
  3791. if ($PassEdit) {
  3792. if ($current_row['editlock'] == 1 &&
  3793. (api_is_allowed_to_edit(false,true)==false || api_is_platform_admin()==false)
  3794. ) {
  3795. Display::addFlash(Display::display_normal_message(get_lang('PageLockedExtra'), false, true));
  3796. } else {
  3797. if ($last_row['is_editing']!=0 && $last_row['is_editing'] != $userId) {
  3798. // Checking for concurrent users
  3799. $timestamp_edit = strtotime($last_row['time_edit']);
  3800. $time_editing = time() - $timestamp_edit;
  3801. $max_edit_time = 1200; // 20 minutes
  3802. $rest_time = $max_edit_time - $time_editing;
  3803. $userinfo = api_get_user_info($last_row['is_editing']);
  3804. $is_being_edited = get_lang('ThisPageisBeginEditedBy').' <a href='.$userinfo['profile_url'].'>'.
  3805. Display::tag('span', $userinfo['complete_name_with_username']).
  3806. get_lang('ThisPageisBeginEditedTryLater').' '.date( "i",$rest_time).' '.get_lang('MinMinutes');
  3807. Display::addFlash(Display::display_normal_message($is_being_edited, false, true));
  3808. } else {
  3809. Display::addFlash(Display::display_confirmation_message(
  3810. self::restore_wikipage(
  3811. $current_row['page_id'],
  3812. $current_row['reflink'],
  3813. $current_row['title'],
  3814. $current_row['content'],
  3815. $current_row['group_id'],
  3816. $current_row['assignment'],
  3817. $current_row['progress'],
  3818. $current_row['version'],
  3819. $last_row['version'],
  3820. $current_row['linksto']
  3821. ).': <a href="index.php?cidReq='.$_course['code'].'&action=showpage&amp;title='.api_htmlentities(urlencode($last_row['reflink'])).'&session_id='.$last_row['session_id'].'&group_id='.$last_row['group_id'].'">'.
  3822. api_htmlentities($last_row['title']).'</a>',
  3823. false,
  3824. true
  3825. ));
  3826. }
  3827. }
  3828. }
  3829. }
  3830. }
  3831. /**
  3832. * @param int|bool $wikiId
  3833. */
  3834. public function setWikiData($wikiId)
  3835. {
  3836. $this->wikiData = self::getWikiDataFromDb($wikiId);
  3837. }
  3838. /**
  3839. * @return array
  3840. */
  3841. public function getWikiData()
  3842. {
  3843. return $this->wikiData;
  3844. }
  3845. /**
  3846. * Check last version
  3847. * @param int $view
  3848. */
  3849. public function checkLastVersion($view)
  3850. {
  3851. $tbl_wiki = $this->tbl_wiki;
  3852. $course_id = $this->course_id;
  3853. $condition_session = $this->condition_session;
  3854. $groupfilter = $this->groupfilter;
  3855. $page = $this->page;
  3856. $_course = $this->courseInfo;
  3857. if (empty($view)) {
  3858. return false;
  3859. }
  3860. $current_row = $this->getWikiData();
  3861. $sql = 'SELECT * FROM '.$tbl_wiki.'
  3862. WHERE
  3863. c_id = '.$course_id.' AND
  3864. reflink = "'.Database::escape_string($page).'" AND
  3865. '.$groupfilter.$condition_session.'
  3866. ORDER BY id DESC'; //last version
  3867. $result = Database::query($sql);
  3868. $last_row = Database::fetch_array($result);
  3869. if ($view < $last_row['id']) {
  3870. $message = '<center>'.get_lang('NoAreSeeingTheLastVersion').'<br />
  3871. '.get_lang("Version").' (
  3872. <a href="index.php?cidReq='.$_course['code'].'&action=showpage&amp;title='.api_htmlentities(urlencode($current_row['reflink'])).'&group_id='.$current_row['group_id'].'&session_id='.$current_row['session_id'].'&view='.api_htmlentities($_GET['view']).'" title="'.get_lang('CurrentVersion').'">
  3873. '.$current_row['version'].'
  3874. </a> /
  3875. <a href="index.php?cidReq='.$_course['code'].'&action=showpage&amp;title='.api_htmlentities(urlencode($last_row['reflink'])).'&group_id='.$last_row['group_id'].'&session_id='.$last_row['session_id'].'" title="'.get_lang('LastVersion').'">
  3876. '.$last_row['version'].'
  3877. </a>) <br />'.get_lang("ConvertToLastVersion").':
  3878. <a href="index.php?cidReq='.$_course['id'].'&action=restorepage&amp;title='.api_htmlentities(urlencode($last_row['reflink'])).'&group_id='.$last_row['group_id'].'&session_id='.$last_row['session_id'].'&view='.api_htmlentities($_GET['view']).'">'.
  3879. get_lang("Restore").'</a></center>';
  3880. Display::addFlash(Display::display_warning_message($message, false, true));
  3881. }
  3882. }
  3883. /**
  3884. * Get most linked pages
  3885. */
  3886. public function getMostLinked()
  3887. {
  3888. $tbl_wiki = $this->tbl_wiki;
  3889. $course_id = $this->course_id;
  3890. $groupfilter = $this->groupfilter;
  3891. $condition_session = $this->condition_session;
  3892. $_course = $this->courseInfo;
  3893. echo '<div class="actions">'.get_lang('MostLinkedPages').'</div>';
  3894. $pages = array();
  3895. $linked = array();
  3896. // Get name pages
  3897. $sql = 'SELECT * FROM '.$tbl_wiki.'
  3898. WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.'
  3899. GROUP BY reflink
  3900. ORDER BY reflink ASC';
  3901. $allpages = Database::query($sql);
  3902. while ($row=Database::fetch_array($allpages)) {
  3903. if ($row['reflink']=='index') {
  3904. $row['reflink']=str_replace(' ', '_', get_lang('DefaultTitle'));
  3905. }
  3906. $pages[] = $row['reflink'];
  3907. }
  3908. // Get name refs in last pages
  3909. $sql = 'SELECT *
  3910. FROM '.$tbl_wiki.' s1
  3911. WHERE s1.c_id = '.$course_id.' AND id=(
  3912. SELECT MAX(s2.id) FROM '.$tbl_wiki.' s2
  3913. WHERE
  3914. s2.c_id = '.$course_id.' AND
  3915. s1.reflink = s2.reflink AND
  3916. '.$groupfilter.$condition_session.'
  3917. )';
  3918. $allpages = Database::query($sql);
  3919. while ($row=Database::fetch_array($allpages)) {
  3920. //remove self reference
  3921. $row['linksto']= str_replace($row["reflink"], " ", trim($row["linksto"]));
  3922. $refs = explode(" ", trim($row["linksto"]));
  3923. // Find linksto into reflink. If found ->page is linked
  3924. foreach ($refs as $v) {
  3925. if (in_array($v, $pages)) {
  3926. if (trim($v)!="") {
  3927. $linked[]=$v;
  3928. }
  3929. }
  3930. }
  3931. }
  3932. $linked = array_unique($linked);
  3933. //make a unique list. TODO:delete this line and count how many for each page
  3934. //show table
  3935. $rows = array();
  3936. foreach ($linked as $linked_show) {
  3937. $row = array();
  3938. $row[] = '<a href="'.api_get_self().'?cidReq='.$_course['code'].'&action=showpage&title='.api_htmlentities(urlencode(str_replace('_',' ',$linked_show))).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities($_GET['group_id']).'">'.
  3939. str_replace('_',' ',$linked_show).'</a>';
  3940. $rows[] = $row;
  3941. }
  3942. $table = new SortableTableFromArrayConfig($rows,0,10,'LinkedPages_table','','','DESC');
  3943. $table->set_additional_parameters(
  3944. array(
  3945. 'cidReq' => Security::remove_XSS($_GET['cidReq']),
  3946. 'action' => Security::remove_XSS($this->action),
  3947. 'session_id' => intval($_GET['session_id']),
  3948. 'group_id' => intval($_GET['group_id']),
  3949. )
  3950. );
  3951. $table->set_header(0,get_lang('Title'), true);
  3952. $table->display();
  3953. }
  3954. /**
  3955. * Get orphan pages
  3956. */
  3957. public function getOrphaned()
  3958. {
  3959. $tbl_wiki = $this->tbl_wiki;
  3960. $course_id = $this->course_id;
  3961. $groupfilter = $this->groupfilter;
  3962. $condition_session = $this->condition_session;
  3963. $_course = $this->courseInfo;
  3964. echo '<div class="actions">'.get_lang('OrphanedPages').'</div>';
  3965. $pages = array();
  3966. $orphaned = array();
  3967. //get name pages
  3968. $sql = 'SELECT * FROM '.$tbl_wiki.'
  3969. WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.'
  3970. GROUP BY reflink
  3971. ORDER BY reflink ASC';
  3972. $allpages=Database::query($sql);
  3973. while ($row=Database::fetch_array($allpages)) {
  3974. $pages[] = $row['reflink'];
  3975. }
  3976. //get name refs in last pages and make a unique list
  3977. $sql = 'SELECT * FROM '.$tbl_wiki.' s1
  3978. WHERE s1.c_id = '.$course_id.' AND id=(
  3979. SELECT MAX(s2.id) FROM '.$tbl_wiki.' s2
  3980. WHERE
  3981. s2.c_id = '.$course_id.' AND
  3982. s1.reflink = s2.reflink AND
  3983. '.$groupfilter.$condition_session.'
  3984. )';
  3985. $allpages = Database::query($sql);
  3986. $array_refs_linked = array();
  3987. while ($row=Database::fetch_array($allpages)) {
  3988. $row['linksto']= str_replace($row["reflink"], " ", trim($row["linksto"])); //remove self reference
  3989. $refs = explode(" ", trim($row["linksto"]));
  3990. foreach ($refs as $ref_linked){
  3991. if ($ref_linked==str_replace(' ','_',get_lang('DefaultTitle'))) {
  3992. $ref_linked='index';
  3993. }
  3994. $array_refs_linked[]= $ref_linked;
  3995. }
  3996. }
  3997. $array_refs_linked = array_unique($array_refs_linked);
  3998. //search each name of list linksto into list reflink
  3999. foreach ($pages as $v) {
  4000. if (!in_array($v, $array_refs_linked)) {
  4001. $orphaned[] = $v;
  4002. }
  4003. }
  4004. $rows = array();
  4005. foreach ($orphaned as $orphaned_show) {
  4006. // get visibility status and title
  4007. $sql = 'SELECT *
  4008. FROM '.$tbl_wiki.'
  4009. WHERE
  4010. c_id = '.$course_id.' AND
  4011. '.$groupfilter.$condition_session.' AND
  4012. reflink="'.Database::escape_string($orphaned_show).'"
  4013. GROUP BY reflink';
  4014. $allpages=Database::query($sql);
  4015. while ($row=Database::fetch_array($allpages)) {
  4016. $orphaned_title=$row['title'];
  4017. $orphaned_visibility=$row['visibility'];
  4018. if ($row['assignment']==1) {
  4019. $ShowAssignment=Display::return_icon('wiki_assignment.png','','',ICON_SIZE_SMALL);
  4020. } elseif ($row['assignment']==2) {
  4021. $ShowAssignment=Display::return_icon('wiki_work.png','','',ICON_SIZE_SMALL);
  4022. } elseif ($row['assignment']==0) {
  4023. $ShowAssignment= Display::return_icon('px_transparent.gif');
  4024. }
  4025. }
  4026. if (!api_is_allowed_to_edit(false,true) || !api_is_platform_admin() && $orphaned_visibility==0){
  4027. continue;
  4028. }
  4029. //show table
  4030. $row = array();
  4031. $row[] = $ShowAssignment;
  4032. $row[] = '<a href="'.api_get_self().'?cidReq='.$_course['code'].'&action=showpage&title='.api_htmlentities(urlencode($orphaned_show)).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities($_GET['group_id']).'">'.
  4033. api_htmlentities($orphaned_title).'</a>';
  4034. $rows[] = $row;
  4035. }
  4036. $table = new SortableTableFromArrayConfig($rows,1, 10, 'OrphanedPages_table','','','DESC');
  4037. $table->set_additional_parameters(
  4038. array(
  4039. 'cidReq' => Security::remove_XSS($_GET['cidReq']),
  4040. 'action' => Security::remove_XSS($this->action),
  4041. 'session_id' => intval($_GET['session_id']),
  4042. 'group_id' => intval($_GET['group_id']),
  4043. )
  4044. );
  4045. $table->set_header(0,get_lang('Type'), true, array ('style' => 'width:30px;'));
  4046. $table->set_header(1,get_lang('Title'), true);
  4047. $table->display();
  4048. }
  4049. /**
  4050. * Get wanted pages
  4051. */
  4052. public function getWantedPages()
  4053. {
  4054. $tbl_wiki = $this->tbl_wiki;
  4055. $course_id = $this->course_id;
  4056. $groupfilter = $this->groupfilter;
  4057. $condition_session = $this->condition_session;
  4058. echo '<div class="actions">'.get_lang('WantedPages').'</div>';
  4059. $pages = array();
  4060. $wanted = array();
  4061. //get name pages
  4062. $sql = 'SELECT * FROM '.$tbl_wiki.'
  4063. WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.'
  4064. GROUP BY reflink
  4065. ORDER BY reflink ASC';
  4066. $allpages=Database::query($sql);
  4067. while ($row=Database::fetch_array($allpages)) {
  4068. if ($row['reflink']=='index'){
  4069. $row['reflink']=str_replace(' ','_',get_lang('DefaultTitle'));
  4070. }
  4071. $pages[] = $row['reflink'];
  4072. }
  4073. //get name refs in last pages
  4074. $sql = 'SELECT * FROM '.$tbl_wiki.' s1
  4075. WHERE s1.c_id = '.$course_id.' AND id=(
  4076. SELECT MAX(s2.id) FROM '.$tbl_wiki.' s2
  4077. WHERE s2.c_id = '.$course_id.' AND s1.reflink = s2.reflink AND '.$groupfilter.$condition_session.'
  4078. )';
  4079. $allpages = Database::query($sql);
  4080. while ($row=Database::fetch_array($allpages)) {
  4081. $refs = explode(" ", trim($row["linksto"]));
  4082. // Find linksto into reflink. If not found ->page is wanted
  4083. foreach ($refs as $v) {
  4084. if (!in_array($v, $pages)) {
  4085. if (trim($v)!="") {
  4086. $wanted[]=$v;
  4087. }
  4088. }
  4089. }
  4090. }
  4091. $wanted = array_unique($wanted);//make a unique list
  4092. //show table
  4093. $rows = array();
  4094. foreach ($wanted as $wanted_show) {
  4095. $row = array();
  4096. $wanted_show = Security::remove_XSS($wanted_show);
  4097. $row[] = '<a href="'.api_get_path(WEB_PATH).'main/wiki/index.php?cidReq=&action=addnew&title='.str_replace('_',' ',$wanted_show).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities($_GET['group_id']).'" class="new_wiki_link">'.str_replace('_',' ',$wanted_show).'</a>';//meter un remove xss en lugar de htmlentities
  4098. $rows[] = $row;
  4099. }
  4100. $table = new SortableTableFromArrayConfig($rows,0,10,'WantedPages_table','','','DESC');
  4101. $table->set_additional_parameters(
  4102. array(
  4103. 'cidReq' => Security::remove_XSS($_GET['cidReq']),
  4104. 'action' => Security::remove_XSS($this->action),
  4105. 'session_id' => intval($_GET['session_id']),
  4106. 'group_id' => intval($_GET['group_id']),
  4107. )
  4108. );
  4109. $table->set_header(0,get_lang('Title'), true);
  4110. $table->display();
  4111. }
  4112. /**
  4113. * Most visited
  4114. */
  4115. public function getMostVisited()
  4116. {
  4117. $tbl_wiki = $this->tbl_wiki;
  4118. $course_id = $this->course_id;
  4119. $groupfilter = $this->groupfilter;
  4120. $condition_session = $this->condition_session;
  4121. $_course = $this->courseInfo;
  4122. echo '<div class="actions">'.get_lang('MostVisitedPages').'</div>';
  4123. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) { //only by professors if page is hidden
  4124. $sql = 'SELECT *, SUM(hits) AS tsum FROM '.$tbl_wiki.'
  4125. WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.'
  4126. GROUP BY reflink';
  4127. } else {
  4128. $sql = 'SELECT *, SUM(hits) AS tsum FROM '.$tbl_wiki.'
  4129. WHERE
  4130. c_id = '.$course_id.' AND
  4131. '.$groupfilter.$condition_session.' AND
  4132. visibility=1
  4133. GROUP BY reflink';
  4134. }
  4135. $allpages = Database::query($sql);
  4136. //show table
  4137. if (Database::num_rows($allpages) > 0) {
  4138. $rows = array();
  4139. while ($obj = Database::fetch_object($allpages)) {
  4140. //get type assignment icon
  4141. $ShowAssignment = '';
  4142. if ($obj->assignment==1) {
  4143. $ShowAssignment=Display::return_icon('wiki_assignment.png', get_lang('AssignmentDesc'),'',ICON_SIZE_SMALL);
  4144. } elseif ($obj->assignment==2) {
  4145. $ShowAssignment=$ShowAssignment=Display::return_icon('wiki_work.png', get_lang('AssignmentWork'),'',ICON_SIZE_SMALL);
  4146. } elseif ($obj->assignment==0) {
  4147. $ShowAssignment = Display::return_icon('px_transparent.gif');
  4148. }
  4149. $row = array();
  4150. $row[] = $ShowAssignment;
  4151. $row[] = '<a href="'.api_get_self().'?cidReq='.$_course['code'].'&action=showpage&title='.api_htmlentities(urlencode($obj->reflink)).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities($_GET['group_id']).'">'.
  4152. api_htmlentities($obj->title).'</a>';
  4153. $row[] = $obj->tsum;
  4154. $rows[] = $row;
  4155. }
  4156. $table = new SortableTableFromArrayConfig(
  4157. $rows,
  4158. 2,
  4159. 10,
  4160. 'MostVisitedPages_table',
  4161. '',
  4162. '',
  4163. 'DESC'
  4164. );
  4165. $table->set_additional_parameters(
  4166. array(
  4167. 'cidReq' => Security::remove_XSS($_GET['cidReq']),
  4168. 'action' => Security::remove_XSS($this->action),
  4169. 'session_id' => intval($_GET['session_id']),
  4170. 'group_id' => intval($_GET['group_id']),
  4171. )
  4172. );
  4173. $table->set_header(0,get_lang('Type'), true, array ('style' => 'width:30px;'));
  4174. $table->set_header(1, get_lang('Title'), true);
  4175. $table->set_header(2, get_lang('Visits'), true);
  4176. $table->display();
  4177. }
  4178. }
  4179. /**
  4180. * Get actions bar
  4181. * @return string
  4182. */
  4183. public function showActionBar()
  4184. {
  4185. $_course = $this->courseInfo;
  4186. $session_id = $this->session_id;
  4187. $groupId = $this->group_id;
  4188. $page = $this->page;
  4189. $actionsLeft = '';
  4190. $actionsLeft .= '<a href="index.php?action=showpage&title=index&cidReq='.$_course['id'].'&session_id='.$session_id.'&group_id='.$groupId.'">'.
  4191. Display::return_icon('home.png', get_lang('Home'), '', ICON_SIZE_MEDIUM).'</a>';
  4192. if (api_is_allowed_to_session_edit(false, true) && api_is_allowed_to_edit()) {
  4193. // menu add page
  4194. $actionsLeft .= '<a href="index.php?cidReq=' . $_course['id'] . '&action=addnew&session_id=' . $session_id . '&group_id=' . $groupId . '"' . self::is_active_navigation_tab('addnew').'>'
  4195. . Display::return_icon('add.png', get_lang('AddNew'), '', ICON_SIZE_MEDIUM) . '</a>';
  4196. }
  4197. $lock_unlock_addnew = null;
  4198. $protect_addnewpage = null;
  4199. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  4200. // page action: enable or disable the adding of new pages
  4201. if (self::check_addnewpagelock()==0) {
  4202. $protect_addnewpage = Display::return_icon('off.png', get_lang('AddOptionProtected'));
  4203. $lock_unlock_addnew ='unlockaddnew';
  4204. } else {
  4205. $protect_addnewpage = Display::return_icon('on.png', get_lang('AddOptionUnprotected'));
  4206. $lock_unlock_addnew ='lockaddnew';
  4207. }
  4208. }
  4209. // menu find
  4210. $actionsLeft .= '<a href="index.php?cidReq='.$_course['id'].'&action=searchpages&session_id='.$session_id.'&group_id='.$groupId.'"'.self::is_active_navigation_tab('searchpages').'>'.
  4211. Display::return_icon('search.png', get_lang('SearchPages'), '', ICON_SIZE_MEDIUM).'</a></li>';
  4212. ///menu more
  4213. $actionsLeft .= '<a href="index.php?action=more&amp;title='.api_htmlentities(urlencode($page)).'"'.self::is_active_navigation_tab('more').'>'.
  4214. Display::return_icon('stats.png', get_lang('Statistics'), '', ICON_SIZE_MEDIUM).'</a></li>';
  4215. // menu all pages
  4216. $actionsLeft .= '<a class="btn btn-default" href="index.php?cidReq='.$_course['id'].'&action=allpages&session_id='.$session_id.'&group_id='.$groupId.'"'.self::is_active_navigation_tab('allpages').'>'.
  4217. get_lang('AllPages').'</a>';
  4218. // menu recent changes
  4219. $actionsLeft .= '<a class="btn btn-default" href="index.php?cidReq='.$_course['id'].'&action=recentchanges&session_id='.$session_id.'&group_id='.$groupId.'"'.self::is_active_navigation_tab('recentchanges').'>'.
  4220. get_lang('RecentChanges').'</a>';
  4221. echo Display::toolbarAction('toolbar-wiki', [$actionsLeft]);
  4222. }
  4223. /**
  4224. * Showing warning
  4225. */
  4226. public function deletePageWarning()
  4227. {
  4228. $page = $this->page;
  4229. $course_id = $this->course_id;
  4230. $groupfilter = $this->groupfilter;
  4231. $condition_session = $this->condition_session;
  4232. if (!$_GET['title']) {
  4233. Display::addFlash(Display::display_error_message(get_lang('MustSelectPage'), false, true));
  4234. return;
  4235. }
  4236. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  4237. Display::addFlash('<div id="wikititle">'.get_lang('DeletePageHistory').'</div>');
  4238. if ($page == "index") {
  4239. Display::addFlash(Display::display_warning_message(get_lang('WarningDeleteMainPage'), false, true));
  4240. }
  4241. $message = get_lang('ConfirmDeletePage')."
  4242. <a href=\"index.php?".api_get_cidreq()."\">".get_lang("No")."</a>
  4243. <a href=\"".api_get_self()."?".api_get_cidreq()."&action=delete&title=".api_htmlentities(urlencode($page))."&delete=yes\">".
  4244. get_lang("Yes")."</a>";
  4245. if (!isset($_GET['delete'])) {
  4246. Display::addFlash(Display::display_warning_message($message, false, true));
  4247. }
  4248. if (isset($_GET['delete']) && $_GET['delete'] == 'yes') {
  4249. $result = self::deletePage($page, $course_id, $groupfilter, $condition_session);
  4250. if ($result) {
  4251. Display::addFlash(Display::display_confirmation_message(get_lang('WikiPageDeleted'), false, true));
  4252. }
  4253. }
  4254. } else {
  4255. Display::addFlash(Display::display_normal_message(get_lang("OnlyAdminDeletePageWiki"), false, true));
  4256. }
  4257. }
  4258. /**
  4259. * Edit page
  4260. */
  4261. public function editPage()
  4262. {
  4263. $tbl_wiki = $this->tbl_wiki;
  4264. $tbl_wiki_conf = $this->tbl_wiki_conf;
  4265. $condition_session = $this->condition_session;
  4266. $groupfilter = $this->groupfilter;
  4267. $page = $this->page;
  4268. $course_id = $this->course_id;
  4269. $groupId = $this->group_id;
  4270. $userId = api_get_user_id();
  4271. if (api_get_session_id() != 0 &&
  4272. api_is_allowed_to_session_edit(false,true) == false
  4273. ) {
  4274. api_not_allowed();
  4275. }
  4276. $sql = 'SELECT *
  4277. FROM '.$tbl_wiki.' w INNER JOIN '.$tbl_wiki_conf.' c
  4278. ON (w.c_id = c.c_id AND w.page_id = c.page_id)
  4279. WHERE
  4280. w.c_id = '.$course_id.' AND
  4281. w.reflink= "'.Database::escape_string($page).'" AND
  4282. w.'.$groupfilter.$condition_session.'
  4283. ORDER BY id DESC';
  4284. $result = Database::query($sql);
  4285. $row = Database::fetch_array($result);
  4286. // we do not need a while loop since we are always displaying the last version
  4287. if ($row['content'] == '' && $row['title'] == '' && $page == '') {
  4288. Display::addFlash(
  4289. Display::display_error_message(get_lang('MustSelectPage'), false, true)
  4290. );
  4291. return;
  4292. } elseif ($row['content']=='' && $row['title']=='' && $page=='index') {
  4293. // Table structure for better export to pdf
  4294. $default_table_for_content_Start = '<table align="center" border="0"><tr><td align="center">';
  4295. $default_table_for_content_End = '</td></tr></table>';
  4296. $content = $default_table_for_content_Start.sprintf(get_lang('DefaultContent'),api_get_path(WEB_IMG_PATH)).$default_table_for_content_End;
  4297. $title = get_lang('DefaultTitle');
  4298. $page_id = 0;
  4299. } else {
  4300. $content = api_html_entity_decode($row['content']);
  4301. $title = api_html_entity_decode($row['title']);
  4302. $page_id = $row['page_id'];
  4303. }
  4304. // Only teachers and platform admin can edit the index page.
  4305. // Only teachers and platform admin can edit an assignment teacher.
  4306. // And users in groups
  4307. if (($row['reflink'] == 'index' || $row['reflink'] == '' || $row['assignment'] == 1) &&
  4308. (!api_is_allowed_to_edit(false, true) && $groupId == 0) && !api_is_allowed_in_course()
  4309. ) {
  4310. Display::addFlash(
  4311. Display::return_message(get_lang('OnlyEditPagesCourseManager')),
  4312. 'error'
  4313. );
  4314. } else {
  4315. $PassEdit = false;
  4316. // Check if is a wiki group
  4317. if (!empty($groupId)) {
  4318. //Only teacher, platform admin and group members can edit a wiki group
  4319. if (api_is_allowed_to_edit(false,true) ||
  4320. api_is_platform_admin() ||
  4321. GroupManager :: is_user_in_group($userId, $groupId)
  4322. ) {
  4323. $PassEdit = true;
  4324. } else {
  4325. Display::addFlash(
  4326. Display::return_message(
  4327. get_lang('OnlyEditPagesGroupMembers')
  4328. )
  4329. );
  4330. }
  4331. } else {
  4332. $PassEdit=true;
  4333. }
  4334. $icon_assignment = null;
  4335. // check if is a assignment
  4336. if ($row['assignment'] == 1) {
  4337. Display::addFlash(
  4338. Display::return_message(get_lang('EditAssignmentWarning'))
  4339. );
  4340. $icon_assignment=Display::return_icon('wiki_assignment.png', get_lang('AssignmentDescExtra'),'',ICON_SIZE_SMALL);
  4341. } elseif ($row['assignment'] == 2) {
  4342. $icon_assignment=Display::return_icon('wiki_work.png', get_lang('AssignmentWorkExtra'),'',ICON_SIZE_SMALL);
  4343. if (($userId == $row['user_id'])==false) {
  4344. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  4345. $PassEdit = true;
  4346. } else {
  4347. Display::addFlash(
  4348. Display::return_message(
  4349. get_lang('LockByTeacher'),
  4350. 'warning'
  4351. )
  4352. );
  4353. $PassEdit = false;
  4354. }
  4355. } else {
  4356. $PassEdit = true;
  4357. }
  4358. }
  4359. if ($PassEdit) {
  4360. //show editor if edit is allowed <<<<<
  4361. if ($row['editlock'] == 1 &&
  4362. (api_is_allowed_to_edit(false, true) == false || api_is_platform_admin()==false)
  4363. ) {
  4364. Display::addFlash(
  4365. Display::return_message(
  4366. get_lang('PageLockedExtra')
  4367. )
  4368. );
  4369. } else {
  4370. // Check tasks
  4371. if (!empty($row['startdate_assig']) && time() < api_strtotime($row['startdate_assig'])
  4372. ) {
  4373. $message = get_lang('TheTaskDoesNotBeginUntil').': '.api_get_local_time($row['startdate_assig']);
  4374. Display::addFlash(
  4375. Display::return_message(
  4376. $message,
  4377. 'warning'
  4378. )
  4379. );
  4380. if (!api_is_allowed_to_edit(false, true)) {
  4381. $this->redirectHome();
  4382. }
  4383. }
  4384. if (!empty($row['enddate_assig']) &&
  4385. time() > strtotime($row['enddate_assig']) &&
  4386. $row['delayedsubmit']==0
  4387. ) {
  4388. $message = get_lang('TheDeadlineHasBeenCompleted').': '.api_get_local_time($row['enddate_assig']);
  4389. Display::addFlash(
  4390. Display::return_message(
  4391. $message,
  4392. 'warning'
  4393. )
  4394. );
  4395. if (!api_is_allowed_to_edit(false,true)) {
  4396. $this->redirectHome();
  4397. }
  4398. }
  4399. if (!empty($row['max_version']) && $row['version']>=$row['max_version']) {
  4400. $message=get_lang('HasReachedMaxiNumVersions');
  4401. Display::addFlash(
  4402. Display::return_message(
  4403. $message,
  4404. 'warning'
  4405. )
  4406. );
  4407. if (!api_is_allowed_to_edit(false,true)) {
  4408. $this->redirectHome();
  4409. }
  4410. }
  4411. if (!empty($row['max_text']) && $row['max_text'] <= self::word_count($row['content'])) {
  4412. $message = get_lang('HasReachedMaxNumWords');
  4413. Display::addFlash(
  4414. Display::return_message(
  4415. $message,
  4416. 'warning'
  4417. )
  4418. );
  4419. if (!api_is_allowed_to_edit(false,true)) {
  4420. $this->redirectHome();
  4421. }
  4422. }
  4423. if (!empty($row['task'])) {
  4424. //previous change 0 by text
  4425. if (!empty($row['startdate_assig'])) {
  4426. $message_task_startdate =get_lang('No');
  4427. } else {
  4428. $message_task_startdate = api_get_local_time($row['startdate_assig']);
  4429. }
  4430. if (!empty($row['enddate_assig'])) {
  4431. $message_task_enddate = get_lang('No');
  4432. } else {
  4433. $message_task_enddate = api_get_local_time($row['enddate_assig']);
  4434. }
  4435. if ($row['delayedsubmit']==0) {
  4436. $message_task_delayedsubmit=get_lang('No');
  4437. } else {
  4438. $message_task_delayedsubmit=get_lang('Yes');
  4439. }
  4440. if ($row['max_version']==0) {
  4441. $message_task_max_version=get_lang('No');
  4442. } else {
  4443. $message_task_max_version=$row['max_version'];
  4444. }
  4445. if ($row['max_text']==0) {
  4446. $message_task_max_text=get_lang('No');
  4447. } else {
  4448. $message_task_max_text=$row['max_text'];
  4449. }
  4450. // Comp message
  4451. $message_task='<b>'.get_lang('DescriptionOfTheTask').'</b><p>'.$row['task'].'</p><hr>';
  4452. $message_task.='<p>'.get_lang('StartDate').': '.$message_task_startdate.'</p>';
  4453. $message_task.='<p>'.get_lang('EndDate').': '.$message_task_enddate;
  4454. $message_task.=' ('.get_lang('AllowLaterSends').') '.$message_task_delayedsubmit.'</p>';
  4455. $message_task.='<p>'.get_lang('OtherSettings').': '.get_lang('NMaxVersion').': '.$message_task_max_version;
  4456. $message_task.=' '.get_lang('NMaxWords').': '.$message_task_max_text;
  4457. // Display message
  4458. Display::addFlash(
  4459. Display::return_message(
  4460. $message_task
  4461. )
  4462. );
  4463. }
  4464. $feedback_message = '';
  4465. if ($row['progress'] == $row['fprogress1'] && !empty($row['fprogress1'])) {
  4466. $feedback_message='<b>'.get_lang('Feedback').'</b><p>'.api_htmlentities($row['feedback1']).'</p>';
  4467. } elseif ($row['progress'] == $row['fprogress2'] && !empty($row['fprogress2'])) {
  4468. $feedback_message='<b>'.get_lang('Feedback').'</b><p>'.api_htmlentities($row['feedback2']).'</p>';
  4469. } elseif ($row['progress'] == $row['fprogress3'] && !empty($row['fprogress3'])) {
  4470. $feedback_message='<b>'.get_lang('Feedback').'</b><p>'.api_htmlentities($row['feedback3']).'</p>';
  4471. }
  4472. if (!empty($feedback_message)) {
  4473. Display::addFlash(
  4474. Display::return_message(
  4475. $feedback_message
  4476. )
  4477. );
  4478. }
  4479. // Previous checking for concurrent editions
  4480. if ($row['is_editing'] == 0) {
  4481. Display::addFlash(
  4482. Display::return_message(
  4483. get_lang('WarningMaxEditingTime')
  4484. )
  4485. );
  4486. $time_edit = api_get_utc_datetime();
  4487. $sql = 'UPDATE '.$tbl_wiki.' SET
  4488. is_editing = "'.$userId.'",
  4489. time_edit = "'.$time_edit.'"
  4490. WHERE c_id = '.$course_id.' AND id="'.$row['id'].'"';
  4491. Database::query($sql);
  4492. } elseif ($row['is_editing'] != $userId) {
  4493. $timestamp_edit=strtotime($row['time_edit']);
  4494. $time_editing = time() - $timestamp_edit;
  4495. $max_edit_time = 1200; // 20 minutes
  4496. $rest_time = $max_edit_time - $time_editing;
  4497. $userinfo = api_get_user_info($row['is_editing']);
  4498. if ($userinfo !== false) {
  4499. $is_being_edited = get_lang('ThisPageisBeginEditedBy') . ' ' . UserManager::getUserProfileLink($userinfo) . '
  4500. ' . get_lang('ThisPageisBeginEditedTryLater') . ' ' . date("i", $rest_time) . ' ' . get_lang('MinMinutes') . '';
  4501. }
  4502. Display::addFlash(
  4503. Display::return_message(
  4504. $is_being_edited,
  4505. 'normal',
  4506. false
  4507. )
  4508. );
  4509. $this->redirectHome();
  4510. }
  4511. // Form.
  4512. $url = api_get_self().'?action=edit&title='.urlencode($page).'&session_id='.api_get_session_id().'&group_id='.api_get_group_id().'&'.api_get_cidreq();
  4513. $form = new FormValidator('wiki', 'post', $url);
  4514. $form->addElement('header', $icon_assignment.str_repeat('&nbsp;',3).api_htmlentities($title));
  4515. self::setForm($form, $row);
  4516. $form->addElement('hidden', 'title');
  4517. $form->addElement('button', 'SaveWikiChange', get_lang('Save'));
  4518. $row['title'] = $title;
  4519. $row['page_id'] = $page_id;
  4520. $row['reflink'] = $page;
  4521. $row['content'] = $content;
  4522. $form->setDefaults($row);
  4523. $form->display();
  4524. // Saving a change
  4525. if ($form->validate()) {
  4526. $versionFromSession = Session::read('_version');
  4527. if (empty($_POST['title'])) {
  4528. Display::addFlash(
  4529. Display::return_message(
  4530. get_lang("NoWikiPageTitle"),
  4531. 'error'
  4532. )
  4533. );
  4534. } elseif (!self::double_post($_POST['wpost_id'])) {
  4535. //double post
  4536. } elseif ($_POST['version'] != '' && $versionFromSession != 0 && $_POST['version'] != $versionFromSession) {
  4537. //prevent concurrent users and double version
  4538. Display::addFlash(
  4539. Display::return_message(
  4540. get_lang("EditedByAnotherUser"),
  4541. 'error'
  4542. )
  4543. );
  4544. } else {
  4545. $returnMessage = self::save_wiki($form->exportValues());
  4546. Display::addFlash(
  4547. Display::return_message(
  4548. $returnMessage,
  4549. 'confirmation'
  4550. )
  4551. );
  4552. }
  4553. $wikiData = self::getWikiData();
  4554. $redirectUrl = $this->url.'&action=showpage&title='.$wikiData['reflink'].'&'.api_get_cidreq();
  4555. header('Location: '.$redirectUrl);
  4556. exit;
  4557. }
  4558. }
  4559. }
  4560. }
  4561. }
  4562. /**
  4563. * Get history
  4564. */
  4565. public function getHistory()
  4566. {
  4567. $tbl_wiki = $this->tbl_wiki;
  4568. $condition_session = $this->condition_session;
  4569. $groupfilter = $this->groupfilter;
  4570. $page = $this->page;
  4571. $course_id = $this->course_id;
  4572. $session_id = $this->session_id;
  4573. $userId = api_get_user_id();
  4574. if (!$_GET['title']) {
  4575. Display::addFlash(Display::display_error_message(get_lang("MustSelectPage"), false, true));
  4576. return;
  4577. }
  4578. /* First, see the property visibility that is at the last register and
  4579. therefore we should select descending order.
  4580. But to give ownership to each record,
  4581. this is no longer necessary except for the title. TODO: check this*/
  4582. $sql = 'SELECT * FROM '.$tbl_wiki.'
  4583. WHERE
  4584. c_id = '.$course_id.' AND
  4585. reflink="'.Database::escape_string($page).'" AND
  4586. '.$groupfilter.$condition_session.'
  4587. ORDER BY id DESC';
  4588. $result = Database::query($sql);
  4589. $KeyVisibility = null;
  4590. $KeyAssignment = null;
  4591. $KeyTitle = null;
  4592. $KeyUserId = null;
  4593. while ($row=Database::fetch_array($result)) {
  4594. $KeyVisibility = $row['visibility'];
  4595. $KeyAssignment = $row['assignment'];
  4596. $KeyTitle = $row['title'];
  4597. $KeyUserId = $row['user_id'];
  4598. }
  4599. $icon_assignment = null;
  4600. if ($KeyAssignment == 1) {
  4601. $icon_assignment = Display::return_icon('wiki_assignment.png', get_lang('AssignmentDescExtra'), '', ICON_SIZE_SMALL);
  4602. } elseif($KeyAssignment == 2) {
  4603. $icon_assignment = Display::return_icon('wiki_work.png', get_lang('AssignmentWorkExtra'), '', ICON_SIZE_SMALL);
  4604. }
  4605. // Second, show
  4606. //if the page is hidden and is a job only sees its author and professor
  4607. if ($KeyVisibility == 1 ||
  4608. api_is_allowed_to_edit(false,true) ||
  4609. api_is_platform_admin() ||
  4610. (
  4611. $KeyAssignment==2 && $KeyVisibility==0 &&
  4612. ($userId == $KeyUserId)
  4613. )
  4614. ) {
  4615. // We show the complete history
  4616. if (!isset($_POST['HistoryDifferences']) && !isset($_POST['HistoryDifferences2'])) {
  4617. $sql = 'SELECT * FROM '.$tbl_wiki.'
  4618. WHERE
  4619. c_id = '.$course_id.' AND
  4620. reflink="'.Database::escape_string($page).'" AND
  4621. '.$groupfilter.$condition_session.'
  4622. ORDER BY id DESC';
  4623. $result = Database::query($sql);
  4624. $title = $_GET['title'];
  4625. $group_id = api_get_group_id();
  4626. echo '<div id="wikititle">';
  4627. echo $icon_assignment.'&nbsp;&nbsp;&nbsp;'.api_htmlentities($KeyTitle);
  4628. echo '</div>';
  4629. echo '<form id="differences" method="POST" action="index.php?'.api_get_cidreq().'&action=history&title='.api_htmlentities(urlencode($title)).'&session_id='.api_htmlentities($session_id).'&group_id='.api_htmlentities($group_id).'">';
  4630. echo '<ul style="list-style-type: none;">';
  4631. echo '<br/>';
  4632. echo '<button class="search" type="submit" name="HistoryDifferences" value="HistoryDifferences">'.
  4633. get_lang('ShowDifferences').' '.get_lang('LinesDiff').'</button>';
  4634. echo '<button class="search" type="submit" name="HistoryDifferences2" value="HistoryDifferences2">'.
  4635. get_lang('ShowDifferences').' '.get_lang('WordsDiff').'</button>';
  4636. echo '<br/><br/>';
  4637. $counter=0;
  4638. $total_versions = Database::num_rows($result);
  4639. while ($row = Database::fetch_array($result)) {
  4640. $userinfo = api_get_user_info($row['user_id']);
  4641. $username = api_htmlentities(sprintf(get_lang('LoginX'), $userinfo['username']), ENT_QUOTES);
  4642. echo '<li style="margin-bottom: 5px;">';
  4643. ($counter==0) ? $oldstyle='style="visibility: hidden;"':$oldstyle='';
  4644. ($counter==0) ? $newchecked=' checked': $newchecked='';
  4645. ($counter==$total_versions-1) ? $newstyle='style="visibility: hidden;"':$newstyle='';
  4646. ($counter==1) ? $oldchecked=' checked':$oldchecked='';
  4647. echo '<input name="old" value="'.$row['id'].'" type="radio" '.$oldstyle.' '.$oldchecked.'/> ';
  4648. echo '<input name="new" value="'.$row['id'].'" type="radio" '.$newstyle.' '.$newchecked.'/> ';
  4649. echo '<a href="'.api_get_self().'?action=showpage&amp;title='.api_htmlentities(urlencode($page)).'&amp;view='.$row['id'].'">';
  4650. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=showpage&title='.api_htmlentities(urlencode($page)).'&view='.$row['id'].'">';
  4651. echo api_get_local_time($row['dtime'], null, date_default_timezone_get());
  4652. echo '</a>';
  4653. echo ' ('.get_lang('Version').' '.$row['version'].')';
  4654. echo ' '.get_lang('By').' ';
  4655. if ($userinfo !== false) {
  4656. echo UserManager::getUserProfileLink($userinfo);
  4657. } else {
  4658. echo get_lang('Anonymous').' ('.api_htmlentities($row['user_ip']).')';
  4659. }
  4660. echo ' ( '.get_lang('Progress').': '.api_htmlentities($row['progress']).'%, ';
  4661. $comment = $row['comment'];
  4662. if (!empty($comment)) {
  4663. $comment = api_substr($comment, 0, 100);
  4664. if ($comment !== false) {
  4665. $comment = api_htmlentities($comment);
  4666. echo get_lang('Comments').': ' . $comment;
  4667. if (api_strlen($row['comment'])>100) {
  4668. echo '... ';
  4669. }
  4670. }
  4671. } else {
  4672. echo get_lang('Comments').': ---';
  4673. }
  4674. echo ' ) </li>';
  4675. $counter++;
  4676. } //end while
  4677. echo '<br/>';
  4678. echo '<button class="search" type="submit" name="HistoryDifferences" value="HistoryDifferences">'.get_lang('ShowDifferences').' '.get_lang('LinesDiff').'</button>';
  4679. echo '<button class="search" type="submit" name="HistoryDifferences2" value="HistoryDifferences2">'.get_lang('ShowDifferences').' '.get_lang('WordsDiff').'</button>';
  4680. echo '</ul></form>';
  4681. } else { // We show the differences between two versions
  4682. $version_old = array();
  4683. if (isset($_POST['old'])) {
  4684. $sql_old = "SELECT * FROM $tbl_wiki
  4685. WHERE c_id = $course_id AND id='".Database::escape_string($_POST['old'])."'";
  4686. $result_old = Database::query($sql_old);
  4687. $version_old = Database::fetch_array($result_old);
  4688. }
  4689. $sql_new = "SELECT * FROM $tbl_wiki
  4690. WHERE c_id = $course_id AND id='".Database::escape_string($_POST['new'])."'";
  4691. $result_new=Database::query($sql_new);
  4692. $version_new=Database::fetch_array($result_new);
  4693. $oldTime = isset($version_old['dtime']) ? $version_old['dtime'] : null;
  4694. $oldContent = isset($version_old['content']) ? $version_old['content'] : null;
  4695. if (isset($_POST['HistoryDifferences'])) {
  4696. include 'diff.inc.php';
  4697. //title
  4698. echo '<div id="wikititle">'.api_htmlentities($version_new['title']).'
  4699. <font size="-2"><i>('.get_lang('DifferencesNew').'</i>
  4700. <font style="background-color:#aaaaaa">'.$version_new['dtime'].'</font>
  4701. <i>'.get_lang('DifferencesOld').'</i>
  4702. <font style="background-color:#aaaaaa">'.$oldTime.'</font>
  4703. ) '.get_lang('Legend').': <span class="diffAdded" >'.get_lang('WikiDiffAddedLine').'</span>
  4704. <span class="diffDeleted" >'.get_lang('WikiDiffDeletedLine').'</span> <span class="diffMoved">'.get_lang('WikiDiffMovedLine').'</span></font>
  4705. </div>';
  4706. }
  4707. if (isset($_POST['HistoryDifferences2'])) {
  4708. //title
  4709. echo '<div id="wikititle">'.api_htmlentities($version_new['title']).'
  4710. <font size="-2"><i>('.get_lang('DifferencesNew').'</i> <font style="background-color:#aaaaaa">'.$version_new['dtime'].'</font>
  4711. <i>'.get_lang('DifferencesOld').'</i> <font style="background-color:#aaaaaa">'.$oldTime.'</font>)
  4712. '.get_lang('Legend').': <span class="diffAddedTex" >'.get_lang('WikiDiffAddedTex').'</span>
  4713. <span class="diffDeletedTex" >'.get_lang('WikiDiffDeletedTex').'</span></font></div>';
  4714. }
  4715. if (isset($_POST['HistoryDifferences'])) {
  4716. echo '<table>'.diff($oldContent, $version_new['content'], true, 'format_table_line' ).'</table>'; // format_line mode is better for words
  4717. echo '<br />';
  4718. echo '<strong>'.get_lang('Legend').'</strong><div class="diff">' . "\n";
  4719. echo '<table><tr>';
  4720. echo '<td>';
  4721. echo '</td><td>';
  4722. echo '<span class="diffEqual" >'.get_lang('WikiDiffUnchangedLine').'</span><br />';
  4723. echo '<span class="diffAdded" >'.get_lang('WikiDiffAddedLine').'</span><br />';
  4724. echo '<span class="diffDeleted" >'.get_lang('WikiDiffDeletedLine').'</span><br />';
  4725. echo '<span class="diffMoved" >'.get_lang('WikiDiffMovedLine').'</span><br />';
  4726. echo '</td>';
  4727. echo '</tr></table>';
  4728. }
  4729. if (isset($_POST['HistoryDifferences2'])) {
  4730. $lines1 = array(strip_tags($oldContent)); //without <> tags
  4731. $lines2 = array(strip_tags($version_new['content'])); //without <> tags
  4732. $diff = new Text_Diff($lines1, $lines2);
  4733. $renderer = new Text_Diff_Renderer_inline();
  4734. echo '<style>del{background:#fcc}ins{background:#cfc}</style>'.$renderer->render($diff); // Code inline
  4735. echo '<br />';
  4736. echo '<strong>'.get_lang('Legend').'</strong><div class="diff">' . "\n";
  4737. echo '<table><tr>';
  4738. echo '<td>';
  4739. echo '</td><td>';
  4740. echo '<span class="diffAddedTex" >'.get_lang('WikiDiffAddedTex').'</span><br />';
  4741. echo '<span class="diffDeletedTex" >'.get_lang('WikiDiffDeletedTex').'</span><br />';
  4742. echo '</td>';
  4743. echo '</tr></table>';
  4744. }
  4745. }
  4746. }
  4747. }
  4748. /**
  4749. * Get stat tables
  4750. */
  4751. public function getStatsTable()
  4752. {
  4753. $_course = $this->courseInfo;
  4754. $session_id = $this->session_id;
  4755. $groupId = $this->group_id;
  4756. echo '<div class="actions">'.get_lang('More').'</div>';
  4757. echo '<table border="0">';
  4758. echo ' <tr>';
  4759. echo ' <td>';
  4760. echo ' <ul>';
  4761. //Submenu Most active users
  4762. echo ' <li><a href="index.php?cidReq='.$_course['code'].'&action=mactiveusers&session_id='.$session_id.'&group_id='.$groupId.'">'.get_lang('MostActiveUsers').'</a></li>';
  4763. //Submenu Most visited pages
  4764. echo ' <li><a href="index.php?cidReq='.$_course['code'].'&action=mvisited&session_id='.$session_id.'&group_id='.$groupId.'">'.get_lang('MostVisitedPages').'</a></li>';
  4765. //Submenu Most changed pages
  4766. echo ' <li><a href="index.php?cidReq='.$_course['code'].'&action=mostchanged&session_id='.$session_id.'&group_id='.$groupId.'">'.get_lang('MostChangedPages').'</a></li>';
  4767. echo ' </ul>';
  4768. echo ' </td>';
  4769. echo ' <td>';
  4770. echo ' <ul>';
  4771. // Submenu Orphaned pages
  4772. echo ' <li><a href="index.php?cidReq='.$_course['code'].'&action=orphaned&session_id='.$session_id.'&group_id='.$groupId.'">'.get_lang('OrphanedPages').'</a></li>';
  4773. // Submenu Wanted pages
  4774. echo ' <li><a href="index.php?cidReq='.$_course['code'].'&action=wanted&session_id='.$session_id.'&group_id='.$groupId.'">'.get_lang('WantedPages').'</a></li>';
  4775. // Submenu Most linked pages
  4776. echo '<li><a href="index.php?cidReq='.$_course['code'].'&action=mostlinked&session_id='.$session_id.'&group_id='.$groupId.'">'.get_lang('MostLinkedPages').'</a></li>';
  4777. echo '</ul>';
  4778. echo '</td>';
  4779. echo '<td style="vertical-align:top">';
  4780. echo '<ul>';
  4781. // Submenu Statistics
  4782. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  4783. echo '<li><a href="index.php?cidReq='.$_course['id'].'&action=statistics&session_id='.$session_id.'&group_id='.$groupId.'">'.get_lang('Statistics').'</a></li>';
  4784. }
  4785. echo ' </ul>';
  4786. echo' </td>';
  4787. echo ' </tr>';
  4788. echo '</table>';
  4789. }
  4790. /**
  4791. * Kind of controller
  4792. * @param string $action
  4793. */
  4794. public function handleAction($action)
  4795. {
  4796. $page = $this->page;
  4797. switch ($action) {
  4798. case 'export_to_pdf':
  4799. if (isset($_GET['wiki_id'])) {
  4800. self::export_to_pdf($_GET['wiki_id'], api_get_course_id());
  4801. break;
  4802. }
  4803. break;
  4804. case 'export2doc':
  4805. if (isset($_GET['wiki_id'])) {
  4806. $export2doc = self::export2doc($_GET['wiki_id']);
  4807. if ($export2doc) {
  4808. Display::addFlash(
  4809. Display::display_confirmation_message(
  4810. get_lang('ThePageHasBeenExportedToDocArea'),
  4811. false,
  4812. true
  4813. )
  4814. );
  4815. }
  4816. }
  4817. break;
  4818. case 'restorepage':
  4819. self::restorePage();
  4820. break;
  4821. case 'more':
  4822. self::getStatsTable();
  4823. break;
  4824. case 'statistics':
  4825. self::getStats();
  4826. break;
  4827. case 'mactiveusers':
  4828. self::getActiveUsers($action);
  4829. break;
  4830. case 'usercontrib':
  4831. self::getUserContributions($_GET['user_id'], $action);
  4832. break;
  4833. case 'mostchanged':
  4834. $this->getMostChangedPages($action);
  4835. break;
  4836. case 'mvisited':
  4837. self::getMostVisited();
  4838. break;
  4839. case 'wanted':
  4840. $this->getWantedPages();
  4841. break;
  4842. case 'orphaned':
  4843. self::getOrphaned();
  4844. break;
  4845. case 'mostlinked':
  4846. self::getMostLinked();
  4847. break;
  4848. case 'delete':
  4849. self::deletePageWarning($page);
  4850. break;
  4851. case 'deletewiki':
  4852. $title = '<div class="actions">'.get_lang('DeleteWiki').'</div>';
  4853. if (api_is_allowed_to_edit(false,true) || api_is_platform_admin()) {
  4854. $message = get_lang('ConfirmDeleteWiki');
  4855. $message .= '<p>
  4856. <a href="index.php?'.api_get_cidreq().'">'.get_lang('No').'</a>
  4857. &nbsp;&nbsp;|&nbsp;&nbsp;
  4858. <a href="'.api_get_self().'?'.api_get_cidreq().'&action=deletewiki&delete=yes">'.
  4859. get_lang('Yes').'</a>
  4860. </p>';
  4861. if (!isset($_GET['delete'])) {
  4862. Display::addFlash($title.Display::display_warning_message($message, false, true));
  4863. }
  4864. } else {
  4865. Display::addFlash(Display::display_normal_message(get_lang("OnlyAdminDeleteWiki"), false, true));
  4866. }
  4867. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  4868. if (isset($_GET['delete']) && $_GET['delete'] == 'yes') {
  4869. $return_message = self::delete_wiki();
  4870. Display::addFlash(Display::display_confirmation_message($return_message, false, true));
  4871. $this->redirectHome();
  4872. }
  4873. }
  4874. break;
  4875. case 'searchpages':
  4876. self::getSearchPages($action);
  4877. break;
  4878. case 'links':
  4879. self::getLinks($page);
  4880. break;
  4881. case 'addnew':
  4882. if (api_get_session_id()!=0 && api_is_allowed_to_session_edit(false,true)==false) {
  4883. api_not_allowed();
  4884. }
  4885. echo '<div class="actions">'.get_lang('AddNew').'</div>';
  4886. echo '<br/>';
  4887. //first, check if page index was created. chektitle=false
  4888. if (self::checktitle('index')) {
  4889. if (api_is_allowed_to_edit(false,true) ||
  4890. api_is_platform_admin() ||
  4891. GroupManager::is_user_in_group(api_get_user_id(), api_get_group_id()) ||
  4892. api_is_allowed_in_course()
  4893. ) {
  4894. Display::addFlash(Display::display_normal_message(get_lang('GoAndEditMainPage'), false, true));
  4895. } else {
  4896. Display::addFlash(Display::display_normal_message(get_lang('WikiStandBy'), false, true));
  4897. }
  4898. } elseif (self::check_addnewpagelock()==0 && (api_is_allowed_to_edit(false, true)==false || api_is_platform_admin()==false)) {
  4899. Display::addFlash(Display::display_error_message(get_lang('AddPagesLocked'), false, true));
  4900. } else {
  4901. if (api_is_allowed_to_edit(false,true) ||
  4902. api_is_platform_admin() ||
  4903. GroupManager::is_user_in_group(api_get_user_id(), api_get_group_id()) ||
  4904. $_GET['group_id'] == 0
  4905. ) {
  4906. self::display_new_wiki_form();
  4907. } else {
  4908. Display::addFlash(Display::display_normal_message(get_lang('OnlyAddPagesGroupMembers'), false, true));
  4909. }
  4910. }
  4911. break;
  4912. case 'show':
  4913. self::display_wiki_entry($page);
  4914. break;
  4915. case 'showpage':
  4916. self::display_wiki_entry($page);
  4917. break;
  4918. case 'edit':
  4919. self::editPage();
  4920. break;
  4921. case 'history':
  4922. self::getHistory();
  4923. break;
  4924. case 'recentchanges':
  4925. self::recentChanges($page, $action);
  4926. break;
  4927. case 'allpages':
  4928. self::allPages($action);
  4929. break;
  4930. case 'discuss':
  4931. self::getDiscuss($page);
  4932. break;
  4933. case 'export_to_doc_file':
  4934. self::exportTo($_GET['id'], 'odt');
  4935. exit;
  4936. break;
  4937. }
  4938. }
  4939. /**
  4940. * Redirect to home
  4941. */
  4942. public function redirectHome()
  4943. {
  4944. $redirectUrl = $this->url.'&action=showpage&title=index';
  4945. header('Location: '.$redirectUrl.'&'.api_get_cidreq());
  4946. exit;
  4947. }
  4948. /**
  4949. * Export wiki content in a ODF
  4950. * @param int $id
  4951. * @param string int
  4952. * @return bool
  4953. */
  4954. public function exportTo($id, $format = 'doc')
  4955. {
  4956. $data = self::getWikiDataFromDb($id);
  4957. if (isset($data['content']) && !empty($data['content'])) {
  4958. Export::htmlToOdt($data['content'], $data['reflink'], $format);
  4959. }
  4960. return false;
  4961. }
  4962. }