wiki.inc.php 238 KB

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