wiki.inc.php 240 KB

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