wiki.inc.php 238 KB

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