wiki.inc.php 239 KB

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