wiki.inc.php 239 KB

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