wiki.inc.php 238 KB

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