wiki.inc.php 236 KB

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