wiki.inc.php 236 KB

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