wiki.inc.php 238 KB

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