agenda.lib.php 148 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class Agenda
  5. *
  6. * @author: Julio Montoya <gugli100@gmail.com>
  7. */
  8. class Agenda
  9. {
  10. public $events = array();
  11. /** @var string Current type */
  12. public $type = 'personal';
  13. public $types = array('personal', 'admin', 'course');
  14. public $sessionId = 0;
  15. public $senderId;
  16. /** @var array */
  17. public $course;
  18. /** @var array */
  19. private $sessionInfo;
  20. /** @var string */
  21. public $comment;
  22. /** @var bool */
  23. private $isAllowedToEdit;
  24. public $eventStudentPublicationColor;
  25. /**
  26. * Constructor
  27. * @param string $type
  28. * @param int $senderId Optional The user sender ID
  29. * @param int $courseId Opitonal. The course ID
  30. * @param int $sessionId Optional The session ID
  31. */
  32. public function __construct(
  33. $type,
  34. $senderId = 0,
  35. $courseId = 0,
  36. $sessionId = 0
  37. ) {
  38. // Table definitions
  39. $this->tbl_global_agenda = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
  40. $this->tbl_personal_agenda = Database::get_main_table(TABLE_PERSONAL_AGENDA);
  41. $this->tbl_course_agenda = Database::get_course_table(TABLE_AGENDA);
  42. $this->table_repeat = Database::get_course_table(TABLE_AGENDA_REPEAT);
  43. $this->setType($type);
  44. $this->setSenderId($senderId ?: api_get_user_id());
  45. $isAllowToEdit = false;
  46. switch ($type) {
  47. case 'course':
  48. $sessionId = $sessionId ?: api_get_session_id();
  49. $sessionInfo = api_get_session_info($sessionId);
  50. $this->setSessionId($sessionId);
  51. $this->setSessionInfo($sessionInfo);
  52. // Setting the course object if we are in a course
  53. $courseInfo = api_get_course_info_by_id($courseId);
  54. if (!empty($courseInfo)) {
  55. $this->set_course($courseInfo);
  56. }
  57. // Check if teacher
  58. if (empty($sessionId)) {
  59. $isAllowToEdit = api_is_allowed_to_edit(false, true);
  60. } else {
  61. $isAllowToEdit = api_is_allowed_to_session_edit(
  62. false,
  63. true
  64. );
  65. }
  66. // Check
  67. if (api_get_course_setting('allow_user_edit_agenda') && api_is_allowed_in_course()) {
  68. $isAllowToEdit = true;
  69. }
  70. $groupId = api_get_group_id();
  71. if (!empty($groupId)) {
  72. $groupInfo = GroupManager::get_group_properties($groupId);
  73. $isGroupAccess = GroupManager::user_has_access(
  74. api_get_user_id(),
  75. $groupInfo['iid'],
  76. GroupManager::GROUP_TOOL_CALENDAR
  77. ) &&
  78. GroupManager::is_tutor_of_group(
  79. api_get_user_id(),
  80. $groupInfo['iid']
  81. );
  82. if ($isGroupAccess) {
  83. $isAllowToEdit = true;
  84. } else {
  85. $isAllowToEdit = false;
  86. }
  87. }
  88. break;
  89. case 'admin':
  90. $isAllowToEdit = api_is_platform_admin();
  91. break;
  92. case 'personal':
  93. $isAllowToEdit = !api_is_anonymous();
  94. break;
  95. }
  96. $this->setIsAllowedToEdit($isAllowToEdit);
  97. $this->events = [];
  98. // Event colors
  99. $this->event_platform_color = 'red'; //red
  100. $this->event_course_color = '#458B00'; //green
  101. $this->event_group_color = '#A0522D'; //siena
  102. $this->event_session_color = '#00496D'; // kind of green
  103. $this->eventOtherSessionColor = '#999';
  104. $this->event_personal_color = 'steel blue'; //steel blue
  105. $this->eventStudentPublicationColor = '#FF8C00'; //DarkOrange
  106. }
  107. /**
  108. * @param int $senderId
  109. */
  110. public function setSenderId($senderId)
  111. {
  112. $this->senderId = intval($senderId);
  113. }
  114. /**
  115. * @return int
  116. */
  117. public function getSenderId()
  118. {
  119. return $this->senderId;
  120. }
  121. /**
  122. * @param string $type can be 'personal', 'admin' or 'course'
  123. */
  124. public function setType($type)
  125. {
  126. $typeList = $this->getTypes();
  127. if (in_array($type, $typeList)) {
  128. $this->type = $type;
  129. }
  130. }
  131. /**
  132. * @param int $id
  133. */
  134. public function setSessionId($id)
  135. {
  136. $this->sessionId = intval($id);
  137. }
  138. /**
  139. * @param array $sessionInfo
  140. */
  141. public function setSessionInfo($sessionInfo)
  142. {
  143. $this->sessionInfo = $sessionInfo;
  144. }
  145. /**
  146. * @return int $id
  147. */
  148. public function getSessionId()
  149. {
  150. return $this->sessionId;
  151. }
  152. /**
  153. * @param array $courseInfo
  154. */
  155. public function set_course($courseInfo)
  156. {
  157. $this->course = $courseInfo;
  158. }
  159. /**
  160. * @return array
  161. */
  162. public function getTypes()
  163. {
  164. return $this->types;
  165. }
  166. /**
  167. * Adds an event to the calendar
  168. * @param string $start datetime format: 2012-06-14 09:00:00
  169. * @param string $end datetime format: 2012-06-14 09:00:00
  170. * @param string $allDay (true, false)
  171. * @param string $title
  172. * @param string $content
  173. * @param array $usersToSend array('everyone') or a list of user/group ids
  174. * @param bool $addAsAnnouncement event as a *course* announcement
  175. * @param int $parentEventId
  176. * @param array $attachmentArray array of $_FILES['']
  177. * @param array $attachmentCommentList
  178. * @param string $eventComment
  179. * @param string $color
  180. *
  181. * @return int
  182. */
  183. public function addEvent(
  184. $start,
  185. $end,
  186. $allDay,
  187. $title,
  188. $content,
  189. $usersToSend = array(),
  190. $addAsAnnouncement = false,
  191. $parentEventId = null,
  192. $attachmentArray = array(),
  193. $attachmentCommentList = array(),
  194. $eventComment = null,
  195. $color = ''
  196. ) {
  197. $start = api_get_utc_datetime($start);
  198. $end = api_get_utc_datetime($end);
  199. $allDay = isset($allDay) && $allDay === 'true' ? 1 : 0;
  200. $id = null;
  201. switch ($this->type) {
  202. case 'personal':
  203. $attributes = array(
  204. 'user' => api_get_user_id(),
  205. 'title' => $title,
  206. 'text' => $content,
  207. 'date' => $start,
  208. 'enddate' => $end,
  209. 'all_day' => $allDay,
  210. 'color' => $color
  211. );
  212. $id = Database::insert(
  213. $this->tbl_personal_agenda,
  214. $attributes
  215. );
  216. break;
  217. case 'course':
  218. $attributes = array(
  219. 'title' => $title,
  220. 'content' => $content,
  221. 'start_date' => $start,
  222. 'end_date' => $end,
  223. 'all_day' => $allDay,
  224. 'session_id' => $this->getSessionId(),
  225. 'c_id' => $this->course['real_id'],
  226. 'comment' => $eventComment,
  227. 'color' => $color
  228. );
  229. if (!empty($parentEventId)) {
  230. $attributes['parent_event_id'] = $parentEventId;
  231. }
  232. $senderId = $this->getSenderId();
  233. $sessionId = $this->getSessionId();
  234. // Simple course event.
  235. $id = Database::insert($this->tbl_course_agenda, $attributes);
  236. if ($id) {
  237. $sql = "UPDATE ".$this->tbl_course_agenda." SET id = iid WHERE iid = $id";
  238. Database::query($sql);
  239. $groupId = api_get_group_id();
  240. $groupIid = 0;
  241. if ($groupId) {
  242. $groupInfo = GroupManager::get_group_properties(
  243. $groupId
  244. );
  245. if ($groupInfo) {
  246. $groupIid = $groupInfo['iid'];
  247. }
  248. }
  249. if (!empty($usersToSend)) {
  250. $sendTo = $this->parseSendToArray($usersToSend);
  251. if ($sendTo['everyone']) {
  252. api_item_property_update(
  253. $this->course,
  254. TOOL_CALENDAR_EVENT,
  255. $id,
  256. 'AgendaAdded',
  257. $senderId,
  258. $groupIid,
  259. '',
  260. $start,
  261. $end,
  262. $sessionId
  263. );
  264. api_item_property_update(
  265. $this->course,
  266. TOOL_CALENDAR_EVENT,
  267. $id,
  268. 'visible',
  269. $senderId,
  270. $groupIid,
  271. '',
  272. $start,
  273. $end,
  274. $sessionId
  275. );
  276. } else {
  277. // Storing the selected groups
  278. if (!empty($sendTo['groups'])) {
  279. foreach ($sendTo['groups'] as $group) {
  280. $groupIidItem = 0;
  281. if ($group) {
  282. $groupInfo = GroupManager::get_group_properties(
  283. $group
  284. );
  285. if ($groupInfo) {
  286. $groupIidItem = $groupInfo['iid'];
  287. }
  288. }
  289. api_item_property_update(
  290. $this->course,
  291. TOOL_CALENDAR_EVENT,
  292. $id,
  293. 'AgendaAdded',
  294. $senderId,
  295. $groupIidItem,
  296. 0,
  297. $start,
  298. $end,
  299. $sessionId
  300. );
  301. api_item_property_update(
  302. $this->course,
  303. TOOL_CALENDAR_EVENT,
  304. $id,
  305. 'visible',
  306. $senderId,
  307. $groupIidItem,
  308. 0,
  309. $start,
  310. $end,
  311. $sessionId
  312. );
  313. }
  314. }
  315. // storing the selected users
  316. if (!empty($sendTo['users'])) {
  317. foreach ($sendTo['users'] as $userId) {
  318. api_item_property_update(
  319. $this->course,
  320. TOOL_CALENDAR_EVENT,
  321. $id,
  322. 'AgendaAdded',
  323. $senderId,
  324. $groupIid,
  325. $userId,
  326. $start,
  327. $end,
  328. $sessionId
  329. );
  330. api_item_property_update(
  331. $this->course,
  332. TOOL_CALENDAR_EVENT,
  333. $id,
  334. 'visible',
  335. $senderId,
  336. $groupIid,
  337. $userId,
  338. $start,
  339. $end,
  340. $sessionId
  341. );
  342. }
  343. }
  344. }
  345. }
  346. // Add announcement.
  347. if ($addAsAnnouncement) {
  348. $this->storeAgendaEventAsAnnouncement(
  349. $id,
  350. $usersToSend
  351. );
  352. }
  353. // Add attachment.
  354. if (isset($attachmentArray) && !empty($attachmentArray)) {
  355. $counter = 0;
  356. foreach ($attachmentArray as $attachmentItem) {
  357. $this->addAttachment(
  358. $id,
  359. $attachmentItem,
  360. $attachmentCommentList[$counter],
  361. $this->course
  362. );
  363. $counter++;
  364. }
  365. }
  366. }
  367. break;
  368. case 'admin':
  369. if (api_is_platform_admin()) {
  370. $attributes = array(
  371. 'title' => $title,
  372. 'content' => $content,
  373. 'start_date' => $start,
  374. 'end_date' => $end,
  375. 'all_day' => $allDay,
  376. 'access_url_id' => api_get_current_access_url_id()
  377. );
  378. $id = Database::insert(
  379. $this->tbl_global_agenda,
  380. $attributes
  381. );
  382. }
  383. break;
  384. }
  385. return $id;
  386. }
  387. /**
  388. * @param int $eventId
  389. * @param int $courseId
  390. *
  391. * @return array
  392. */
  393. public function getRepeatedInfoByEvent($eventId, $courseId)
  394. {
  395. $repeatTable = Database::get_course_table(TABLE_AGENDA_REPEAT);
  396. $eventId = intval($eventId);
  397. $courseId = intval($courseId);
  398. $sql = "SELECT * FROM $repeatTable
  399. WHERE c_id = $courseId AND cal_id = $eventId";
  400. $res = Database::query($sql);
  401. $repeatInfo = array();
  402. if (Database::num_rows($res) > 0) {
  403. $repeatInfo = Database::fetch_array($res, 'ASSOC');
  404. }
  405. return $repeatInfo;
  406. }
  407. /**
  408. * @param int $eventId
  409. * @param string $type
  410. * @param string $end in local time
  411. * @param array $sentTo
  412. *
  413. * @return bool
  414. */
  415. public function addRepeatedItem($eventId, $type, $end, $sentTo = array())
  416. {
  417. $t_agenda = Database::get_course_table(TABLE_AGENDA);
  418. $t_agenda_r = Database::get_course_table(TABLE_AGENDA_REPEAT);
  419. if (empty($this->course)) {
  420. return false;
  421. }
  422. $course_id = $this->course['real_id'];
  423. $eventId = intval($eventId);
  424. $sql = "SELECT title, content, start_date, end_date, all_day
  425. FROM $t_agenda
  426. WHERE c_id = $course_id AND id = $eventId";
  427. $res = Database::query($sql);
  428. if (Database::num_rows($res) !== 1) {
  429. return false;
  430. }
  431. $row = Database::fetch_array($res);
  432. $origStartDate = api_strtotime($row['start_date'], 'UTC');
  433. $origEndDate = api_strtotime($row['end_date'], 'UTC');
  434. $diff = $origEndDate - $origStartDate;
  435. $title = $row['title'];
  436. $content = $row['content'];
  437. $allDay = $row['all_day'];
  438. $now = time();
  439. $type = Database::escape_string($type);
  440. $end = api_strtotime($end);
  441. if (1 <= $end && $end <= 500) {
  442. // We assume that, with this type of value, the user actually gives a count of repetitions
  443. //and that he wants us to calculate the end date with that (particularly in case of imports from ical)
  444. switch ($type) {
  445. case 'daily':
  446. $end = $origStartDate + (86400 * $end);
  447. break;
  448. case 'weekly':
  449. $end = $this->addWeek($origStartDate, $end);
  450. break;
  451. case 'monthlyByDate':
  452. $end = $this->addMonth($origStartDate, $end);
  453. break;
  454. case 'monthlyByDay':
  455. //TODO
  456. break;
  457. case 'monthlyByDayR':
  458. //TODO
  459. break;
  460. case 'yearly':
  461. $end = $this->addYear($origStartDate, $end);
  462. break;
  463. }
  464. }
  465. $typeList = array(
  466. 'daily',
  467. 'weekly',
  468. 'monthlyByDate',
  469. 'monthlyByDay',
  470. 'monthlyByDayR',
  471. 'yearly'
  472. );
  473. // The event has to repeat *in the future*. We don't allow repeated
  474. // events in the past
  475. if ($end > $now && in_array($type, $typeList)) {
  476. $sql = "INSERT INTO $t_agenda_r (c_id, cal_id, cal_type, cal_end)
  477. VALUES ($course_id, '$eventId', '$type', '$end')";
  478. Database::query($sql);
  479. switch ($type) {
  480. // @todo improve loop.
  481. case 'daily':
  482. for ($i = $origStartDate + 86400; $i <= $end; $i += 86400) {
  483. $start = date('Y-m-d H:i:s', $i);
  484. $repeatEnd = date('Y-m-d H:i:s', $i + $diff);
  485. $this->addEvent(
  486. $start,
  487. $repeatEnd,
  488. $allDay,
  489. $title,
  490. $content,
  491. $sentTo,
  492. false,
  493. $eventId
  494. );
  495. }
  496. break;
  497. case 'weekly':
  498. for ($i = $origStartDate + 604800; $i <= $end; $i += 604800) {
  499. $start = date('Y-m-d H:i:s', $i);
  500. $repeatEnd = date('Y-m-d H:i:s', $i + $diff);
  501. $this->addEvent(
  502. $start,
  503. $repeatEnd,
  504. $allDay,
  505. $title,
  506. $content,
  507. $sentTo,
  508. false,
  509. $eventId
  510. );
  511. }
  512. break;
  513. case 'monthlyByDate':
  514. $next_start = $this->addMonth($origStartDate);
  515. while ($next_start <= $end) {
  516. $start = date('Y-m-d H:i:s', $next_start);
  517. $repeatEnd = date('Y-m-d H:i:s', $next_start + $diff);
  518. $this->addEvent(
  519. $start,
  520. $repeatEnd,
  521. $allDay,
  522. $title,
  523. $content,
  524. $sentTo,
  525. false,
  526. $eventId
  527. );
  528. $next_start = $this->addMonth($next_start);
  529. }
  530. break;
  531. case 'monthlyByDay':
  532. //not yet implemented
  533. break;
  534. case 'monthlyByDayR':
  535. //not yet implemented
  536. break;
  537. case 'yearly':
  538. $next_start = $this->addYear($origStartDate);
  539. while ($next_start <= $end) {
  540. $start = date('Y-m-d H:i:s', $next_start);
  541. $repeatEnd = date('Y-m-d H:i:s', $next_start + $diff);
  542. $this->addEvent(
  543. $start,
  544. $repeatEnd,
  545. $allDay,
  546. $title,
  547. $content,
  548. $sentTo,
  549. false,
  550. $eventId
  551. );
  552. $next_start = $this->addYear($next_start);
  553. }
  554. break;
  555. }
  556. }
  557. return true;
  558. }
  559. /**
  560. * @param int $item_id
  561. * @param array $sentTo
  562. * @return int
  563. */
  564. public function storeAgendaEventAsAnnouncement($item_id, $sentTo = array())
  565. {
  566. $table_agenda = Database::get_course_table(TABLE_AGENDA);
  567. $course_id = api_get_course_int_id();
  568. // Check params
  569. if (empty($item_id) || $item_id != strval(intval($item_id))) {
  570. return -1;
  571. }
  572. // Get the agenda item.
  573. $item_id = intval($item_id);
  574. $sql = "SELECT * FROM $table_agenda
  575. WHERE c_id = $course_id AND id = ".$item_id;
  576. $res = Database::query($sql);
  577. if (Database::num_rows($res) > 0) {
  578. $row = Database::fetch_array($res, 'ASSOC');
  579. // Sending announcement
  580. if (!empty($sentTo)) {
  581. $id = AnnouncementManager::add_announcement(
  582. api_get_course_info(),
  583. api_get_session_id(),
  584. $row['title'],
  585. $row['content'],
  586. $sentTo,
  587. null,
  588. null,
  589. $row['end_date']
  590. );
  591. AnnouncementManager::sendEmail(
  592. api_get_course_info(),
  593. api_get_session_id(),
  594. $id
  595. );
  596. return $id;
  597. }
  598. }
  599. return -1;
  600. }
  601. /**
  602. * Edits an event
  603. *
  604. * @param int $id
  605. * @param string $start datetime format: 2012-06-14 09:00:00
  606. * @param string $end datetime format: 2012-06-14 09:00:00
  607. * @param int $allDay is all day 'true' or 'false'
  608. * @param string $title
  609. * @param string $content
  610. * @param array $usersToSend
  611. * @param array $attachmentArray
  612. * @param array $attachmentCommentList
  613. * @param string $comment
  614. * @param string $color
  615. * @param bool $addAnnouncement
  616. * @param bool $updateContent
  617. *
  618. * @return null|false
  619. */
  620. public function editEvent(
  621. $id,
  622. $start,
  623. $end,
  624. $allDay,
  625. $title,
  626. $content,
  627. $usersToSend = array(),
  628. $attachmentArray = array(),
  629. $attachmentCommentList = array(),
  630. $comment = null,
  631. $color = '',
  632. $addAnnouncement = false,
  633. $updateContent = true
  634. ) {
  635. $start = api_get_utc_datetime($start);
  636. $end = api_get_utc_datetime($end);
  637. $allDay = isset($allDay) && $allDay == 'true' ? 1 : 0;
  638. switch ($this->type) {
  639. case 'personal':
  640. $eventInfo = $this->get_event($id);
  641. if ($eventInfo['user'] != api_get_user_id()) {
  642. break;
  643. }
  644. $attributes = array(
  645. 'title' => $title,
  646. 'date' => $start,
  647. 'enddate' => $end,
  648. 'all_day' => $allDay,
  649. );
  650. if ($updateContent) {
  651. $attributes['text'] = $content;
  652. }
  653. if (!empty($color)) {
  654. $attributes['color'] = $color;
  655. }
  656. Database::update(
  657. $this->tbl_personal_agenda,
  658. $attributes,
  659. array('id = ?' => $id)
  660. );
  661. break;
  662. case 'course':
  663. $eventInfo = $this->get_event($id);
  664. if (empty($eventInfo)) {
  665. return false;
  666. }
  667. $groupId = api_get_group_id();
  668. $groupIid = 0;
  669. if ($groupId) {
  670. $groupInfo = GroupManager::get_group_properties($groupId);
  671. if ($groupInfo) {
  672. $groupIid = $groupInfo['iid'];
  673. }
  674. }
  675. $course_id = $this->course['real_id'];
  676. if (empty($course_id)) {
  677. return false;
  678. }
  679. if ($this->getIsAllowedToEdit()) {
  680. $attributes = array(
  681. 'title' => $title,
  682. 'start_date' => $start,
  683. 'end_date' => $end,
  684. 'all_day' => $allDay,
  685. 'comment' => $comment
  686. );
  687. if ($updateContent) {
  688. $attributes['content'] = $content;
  689. }
  690. if (!empty($color)) {
  691. $attributes['color'] = $color;
  692. }
  693. Database::update(
  694. $this->tbl_course_agenda,
  695. $attributes,
  696. array(
  697. 'id = ? AND c_id = ? AND session_id = ? ' => array(
  698. $id,
  699. $course_id,
  700. $this->sessionId
  701. )
  702. )
  703. );
  704. if (!empty($usersToSend)) {
  705. $sendTo = $this->parseSendToArray($usersToSend);
  706. $usersToDelete = array_diff(
  707. $eventInfo['send_to']['users'],
  708. $sendTo['users']
  709. );
  710. $usersToAdd = array_diff(
  711. $sendTo['users'],
  712. $eventInfo['send_to']['users']
  713. );
  714. $groupsToDelete = array_diff(
  715. $eventInfo['send_to']['groups'],
  716. $sendTo['groups']
  717. );
  718. $groupToAdd = array_diff(
  719. $sendTo['groups'],
  720. $eventInfo['send_to']['groups']
  721. );
  722. if ($sendTo['everyone']) {
  723. // Delete all from group
  724. if (isset($eventInfo['send_to']['groups']) &&
  725. !empty($eventInfo['send_to']['groups'])
  726. ) {
  727. foreach ($eventInfo['send_to']['groups'] as $group) {
  728. $groupIidItem = 0;
  729. if ($group) {
  730. $groupInfo = GroupManager::get_group_properties(
  731. $group
  732. );
  733. if ($groupInfo) {
  734. $groupIidItem = $groupInfo['iid'];
  735. }
  736. }
  737. api_item_property_delete(
  738. $this->course,
  739. TOOL_CALENDAR_EVENT,
  740. $id,
  741. 0,
  742. $groupIidItem,
  743. $this->sessionId
  744. );
  745. }
  746. }
  747. // Storing the selected users.
  748. if (isset($eventInfo['send_to']['users']) &&
  749. !empty($eventInfo['send_to']['users'])
  750. ) {
  751. foreach ($eventInfo['send_to']['users'] as $userId) {
  752. api_item_property_delete(
  753. $this->course,
  754. TOOL_CALENDAR_EVENT,
  755. $id,
  756. $userId,
  757. $groupIid,
  758. $this->sessionId
  759. );
  760. }
  761. }
  762. // Add to everyone only.
  763. api_item_property_update(
  764. $this->course,
  765. TOOL_CALENDAR_EVENT,
  766. $id,
  767. 'visible',
  768. api_get_user_id(),
  769. $groupIid,
  770. null,
  771. $start,
  772. $end,
  773. $this->sessionId
  774. );
  775. } else {
  776. // Delete "everyone".
  777. api_item_property_delete(
  778. $this->course,
  779. TOOL_CALENDAR_EVENT,
  780. $id,
  781. 0,
  782. 0,
  783. $this->sessionId
  784. );
  785. // Add groups
  786. if (!empty($groupToAdd)) {
  787. foreach ($groupToAdd as $group) {
  788. $groupIidItem = 0;
  789. if ($group) {
  790. $groupInfo = GroupManager::get_group_properties(
  791. $group
  792. );
  793. if ($groupInfo) {
  794. $groupIidItem = $groupInfo['iid'];
  795. }
  796. }
  797. api_item_property_update(
  798. $this->course,
  799. TOOL_CALENDAR_EVENT,
  800. $id,
  801. 'visible',
  802. api_get_user_id(),
  803. $groupIidItem,
  804. 0,
  805. $start,
  806. $end,
  807. $this->sessionId
  808. );
  809. }
  810. }
  811. // Delete groups.
  812. if (!empty($groupsToDelete)) {
  813. foreach ($groupsToDelete as $group) {
  814. $groupIidItem = 0;
  815. if ($group) {
  816. $groupInfo = GroupManager::get_group_properties(
  817. $group
  818. );
  819. if ($groupInfo) {
  820. $groupIidItem = $groupInfo['iid'];
  821. }
  822. }
  823. api_item_property_delete(
  824. $this->course,
  825. TOOL_CALENDAR_EVENT,
  826. $id,
  827. 0,
  828. $groupIidItem,
  829. $this->sessionId
  830. );
  831. }
  832. }
  833. // Add users.
  834. if (!empty($usersToAdd)) {
  835. foreach ($usersToAdd as $userId) {
  836. api_item_property_update(
  837. $this->course,
  838. TOOL_CALENDAR_EVENT,
  839. $id,
  840. 'visible',
  841. api_get_user_id(),
  842. $groupIid,
  843. $userId,
  844. $start,
  845. $end,
  846. $this->sessionId
  847. );
  848. }
  849. }
  850. // Delete users.
  851. if (!empty($usersToDelete)) {
  852. foreach ($usersToDelete as $userId) {
  853. api_item_property_delete(
  854. $this->course,
  855. TOOL_CALENDAR_EVENT,
  856. $id,
  857. $userId,
  858. $groupIid,
  859. $this->sessionId
  860. );
  861. }
  862. }
  863. }
  864. }
  865. // Add announcement.
  866. if (isset($addAnnouncement) && !empty($addAnnouncement)) {
  867. $this->storeAgendaEventAsAnnouncement(
  868. $id,
  869. $usersToSend
  870. );
  871. }
  872. // Add attachment.
  873. if (isset($attachmentArray) && !empty($attachmentArray)) {
  874. $counter = 0;
  875. foreach ($attachmentArray as $attachmentItem) {
  876. $this->updateAttachment(
  877. $attachmentItem['id'],
  878. $id,
  879. $attachmentItem,
  880. $attachmentCommentList[$counter],
  881. $this->course
  882. );
  883. $counter++;
  884. }
  885. }
  886. return true;
  887. } else {
  888. return false;
  889. }
  890. break;
  891. case 'admin':
  892. case 'platform':
  893. if (api_is_platform_admin()) {
  894. $attributes = array(
  895. 'title' => $title,
  896. 'start_date' => $start,
  897. 'end_date' => $end,
  898. 'all_day' => $allDay
  899. );
  900. if ($updateContent) {
  901. $attributes['content'] = $content;
  902. }
  903. Database::update(
  904. $this->tbl_global_agenda,
  905. $attributes,
  906. array('id = ?' => $id)
  907. );
  908. }
  909. break;
  910. }
  911. }
  912. /**
  913. * @param int $id
  914. * @param bool $deleteAllItemsFromSerie
  915. */
  916. public function deleteEvent($id, $deleteAllItemsFromSerie = false)
  917. {
  918. switch ($this->type) {
  919. case 'personal':
  920. $eventInfo = $this->get_event($id);
  921. if ($eventInfo['user'] == api_get_user_id()) {
  922. Database::delete(
  923. $this->tbl_personal_agenda,
  924. array('id = ?' => $id)
  925. );
  926. }
  927. break;
  928. case 'course':
  929. $course_id = api_get_course_int_id();
  930. if (!empty($course_id) && api_is_allowed_to_edit(null, true)) {
  931. // Delete
  932. $eventInfo = $this->get_event($id);
  933. if ($deleteAllItemsFromSerie) {
  934. /* This is one of the children.
  935. Getting siblings and delete 'Em all + the father! */
  936. if (isset($eventInfo['parent_event_id']) && !empty($eventInfo['parent_event_id'])) {
  937. // Removing items.
  938. $events = $this->getAllRepeatEvents(
  939. $eventInfo['parent_event_id']
  940. );
  941. if (!empty($events)) {
  942. foreach ($events as $event) {
  943. $this->deleteEvent($event['id']);
  944. }
  945. }
  946. // Removing parent.
  947. $this->deleteEvent($eventInfo['parent_event_id']);
  948. } else {
  949. // This is the father looking for the children.
  950. $events = $this->getAllRepeatEvents($id);
  951. if (!empty($events)) {
  952. foreach ($events as $event) {
  953. $this->deleteEvent($event['id']);
  954. }
  955. }
  956. }
  957. }
  958. // Removing from events.
  959. Database::delete(
  960. $this->tbl_course_agenda,
  961. array('id = ? AND c_id = ?' => array($id, $course_id))
  962. );
  963. api_item_property_update(
  964. $this->course,
  965. TOOL_CALENDAR_EVENT,
  966. $id,
  967. 'delete',
  968. api_get_user_id()
  969. );
  970. // Removing from series.
  971. Database::delete(
  972. $this->table_repeat,
  973. array(
  974. 'cal_id = ? AND c_id = ?' => array(
  975. $id,
  976. $course_id
  977. )
  978. )
  979. );
  980. if (isset($eventInfo['attachment']) && !empty($eventInfo['attachment'])) {
  981. foreach ($eventInfo['attachment'] as $attachment) {
  982. self::deleteAttachmentFile(
  983. $attachment['id'],
  984. $this->course
  985. );
  986. }
  987. }
  988. }
  989. break;
  990. case 'admin':
  991. if (api_is_platform_admin()) {
  992. Database::delete(
  993. $this->tbl_global_agenda,
  994. array('id = ?' => $id)
  995. );
  996. }
  997. break;
  998. }
  999. }
  1000. /**
  1001. * Get agenda events
  1002. * @param int $start
  1003. * @param int $end
  1004. * @param int $course_id
  1005. * @param int $groupId
  1006. * @param int $user_id
  1007. * @param string $format
  1008. *
  1009. * @return array|string
  1010. */
  1011. public function getEvents(
  1012. $start,
  1013. $end,
  1014. $course_id = null,
  1015. $groupId = null,
  1016. $user_id = 0,
  1017. $format = 'json'
  1018. ) {
  1019. switch ($this->type) {
  1020. case 'admin':
  1021. $this->getPlatformEvents($start, $end);
  1022. break;
  1023. case 'course':
  1024. $courseInfo = api_get_course_info_by_id($course_id);
  1025. // Session coach can see all events inside a session.
  1026. if (api_is_coach()) {
  1027. // Own course
  1028. $this->getCourseEvents(
  1029. $start,
  1030. $end,
  1031. $courseInfo,
  1032. $groupId,
  1033. $this->sessionId,
  1034. $user_id
  1035. );
  1036. // Others
  1037. $this->getSessionEvents(
  1038. $start,
  1039. $end,
  1040. $this->sessionId,
  1041. $user_id,
  1042. $this->eventOtherSessionColor
  1043. );
  1044. } else {
  1045. $this->getCourseEvents(
  1046. $start,
  1047. $end,
  1048. $courseInfo,
  1049. $groupId,
  1050. $this->sessionId,
  1051. $user_id
  1052. );
  1053. }
  1054. break;
  1055. case 'personal':
  1056. default:
  1057. $sessionFilterActive = false;
  1058. if (!empty($this->sessionId)) {
  1059. $sessionFilterActive = true;
  1060. }
  1061. if ($sessionFilterActive == false) {
  1062. // Getting personal events
  1063. $this->getPersonalEvents($start, $end);
  1064. // Getting platform/admin events
  1065. $this->getPlatformEvents($start, $end);
  1066. }
  1067. $ignoreVisibility = api_get_configuration_value('personal_agenda_show_all_session_events');
  1068. // Getting course events
  1069. $my_course_list = array();
  1070. if (!api_is_anonymous()) {
  1071. $session_list = SessionManager::get_sessions_by_user(
  1072. api_get_user_id(),
  1073. $ignoreVisibility
  1074. );
  1075. $my_course_list = CourseManager::get_courses_list_by_user_id(
  1076. api_get_user_id(),
  1077. false
  1078. );
  1079. }
  1080. if (api_is_drh()) {
  1081. if (api_drh_can_access_all_session_content()) {
  1082. $session_list = array();
  1083. $sessionList = SessionManager::get_sessions_followed_by_drh(
  1084. api_get_user_id(),
  1085. null,
  1086. null,
  1087. null,
  1088. true,
  1089. false
  1090. );
  1091. if (!empty($sessionList)) {
  1092. foreach ($sessionList as $sessionItem) {
  1093. $sessionId = $sessionItem['id'];
  1094. $courses = SessionManager::get_course_list_by_session_id(
  1095. $sessionId
  1096. );
  1097. $sessionInfo = array(
  1098. 'session_id' => $sessionId,
  1099. 'courses' => $courses
  1100. );
  1101. $session_list[] = $sessionInfo;
  1102. }
  1103. }
  1104. }
  1105. }
  1106. if (!empty($session_list)) {
  1107. foreach ($session_list as $session_item) {
  1108. if ($sessionFilterActive) {
  1109. if ($this->sessionId != $session_item['session_id']) {
  1110. continue;
  1111. }
  1112. }
  1113. $my_courses = $session_item['courses'];
  1114. $my_session_id = $session_item['session_id'];
  1115. if (!empty($my_courses)) {
  1116. foreach ($my_courses as $course_item) {
  1117. $courseInfo = api_get_course_info_by_id(
  1118. $course_item['real_id']
  1119. );
  1120. $this->getCourseEvents(
  1121. $start,
  1122. $end,
  1123. $courseInfo,
  1124. 0,
  1125. $my_session_id
  1126. );
  1127. }
  1128. }
  1129. }
  1130. }
  1131. if (!empty($my_course_list) && $sessionFilterActive == false) {
  1132. foreach ($my_course_list as $courseInfoItem) {
  1133. $courseInfo = api_get_course_info_by_id(
  1134. $courseInfoItem['real_id']
  1135. );
  1136. if (isset($course_id) && !empty($course_id)) {
  1137. if ($courseInfo['real_id'] == $course_id) {
  1138. $this->getCourseEvents(
  1139. $start,
  1140. $end,
  1141. $courseInfo
  1142. );
  1143. }
  1144. } else {
  1145. $this->getCourseEvents(
  1146. $start,
  1147. $end,
  1148. $courseInfo
  1149. );
  1150. }
  1151. }
  1152. }
  1153. break;
  1154. }
  1155. switch ($format) {
  1156. case 'json':
  1157. if (empty($this->events)) {
  1158. return '';
  1159. }
  1160. return json_encode($this->events);
  1161. break;
  1162. case 'array':
  1163. if (empty($this->events)) {
  1164. return [];
  1165. }
  1166. return $this->events;
  1167. break;
  1168. }
  1169. }
  1170. /**
  1171. * @param int $id
  1172. * @param int $minute_delta
  1173. * @return int
  1174. */
  1175. public function resizeEvent($id, $minute_delta)
  1176. {
  1177. $id = (int) $id;
  1178. $delta = intval($minute_delta);
  1179. $event = $this->get_event($id);
  1180. if (!empty($event)) {
  1181. switch ($this->type) {
  1182. case 'personal':
  1183. $sql = "UPDATE $this->tbl_personal_agenda SET
  1184. enddate = DATE_ADD(enddate, INTERVAL $delta MINUTE)
  1185. WHERE id = ".$id;
  1186. Database::query($sql);
  1187. break;
  1188. case 'course':
  1189. $sql = "UPDATE $this->tbl_course_agenda SET
  1190. end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
  1191. WHERE
  1192. c_id = ".$this->course['real_id']." AND
  1193. id = ".$id;
  1194. Database::query($sql);
  1195. break;
  1196. case 'admin':
  1197. $sql = "UPDATE $this->tbl_global_agenda SET
  1198. end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
  1199. WHERE id = ".$id;
  1200. Database::query($sql);
  1201. break;
  1202. }
  1203. }
  1204. return 1;
  1205. }
  1206. /**
  1207. * @param int $id
  1208. * @param int $minute_delta minutes
  1209. * @param int $allDay
  1210. * @return int
  1211. */
  1212. public function move_event($id, $minute_delta, $allDay)
  1213. {
  1214. $id = (int) $id;
  1215. $event = $this->get_event($id);
  1216. if (empty($event)) {
  1217. return false;
  1218. }
  1219. // we convert the hour delta into minutes and add the minute delta
  1220. $delta = intval($minute_delta);
  1221. $allDay = intval($allDay);
  1222. if (!empty($event)) {
  1223. switch ($this->type) {
  1224. case 'personal':
  1225. $sql = "UPDATE $this->tbl_personal_agenda SET
  1226. all_day = $allDay, date = DATE_ADD(date, INTERVAL $delta MINUTE),
  1227. enddate = DATE_ADD(enddate, INTERVAL $delta MINUTE)
  1228. WHERE id=".$id;
  1229. Database::query($sql);
  1230. break;
  1231. case 'course':
  1232. $sql = "UPDATE $this->tbl_course_agenda SET
  1233. all_day = $allDay,
  1234. start_date = DATE_ADD(start_date, INTERVAL $delta MINUTE),
  1235. end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
  1236. WHERE
  1237. c_id = ".$this->course['real_id']." AND
  1238. id=".$id;
  1239. Database::query($sql);
  1240. break;
  1241. case 'admin':
  1242. $sql = "UPDATE $this->tbl_global_agenda SET
  1243. all_day = $allDay,
  1244. start_date = DATE_ADD(start_date,INTERVAL $delta MINUTE),
  1245. end_date = DATE_ADD(end_date, INTERVAL $delta MINUTE)
  1246. WHERE id=".$id;
  1247. Database::query($sql);
  1248. break;
  1249. }
  1250. }
  1251. return 1;
  1252. }
  1253. /**
  1254. * Gets a single event
  1255. *
  1256. * @param int event id
  1257. * @return array
  1258. */
  1259. public function get_event($id)
  1260. {
  1261. // make sure events of the personal agenda can only be seen by the user himself
  1262. $id = intval($id);
  1263. $event = null;
  1264. switch ($this->type) {
  1265. case 'personal':
  1266. $sql = "SELECT * FROM ".$this->tbl_personal_agenda."
  1267. WHERE id = $id AND user = ".api_get_user_id();
  1268. $result = Database::query($sql);
  1269. if (Database::num_rows($result)) {
  1270. $event = Database::fetch_array($result, 'ASSOC');
  1271. $event['description'] = $event['text'];
  1272. $event['content'] = $event['text'];
  1273. $event['start_date'] = $event['date'];
  1274. $event['end_date'] = $event['enddate'];
  1275. }
  1276. break;
  1277. case 'course':
  1278. if (!empty($this->course['real_id'])) {
  1279. $sql = "SELECT * FROM ".$this->tbl_course_agenda."
  1280. WHERE c_id = ".$this->course['real_id']." AND id = ".$id;
  1281. $result = Database::query($sql);
  1282. if (Database::num_rows($result)) {
  1283. $event = Database::fetch_array($result, 'ASSOC');
  1284. $event['description'] = $event['content'];
  1285. // Getting send to array
  1286. $event['send_to'] = $this->getUsersAndGroupSubscribedToEvent(
  1287. $id,
  1288. $this->course['real_id'],
  1289. $this->sessionId
  1290. );
  1291. // Getting repeat info
  1292. $event['repeat_info'] = $this->getRepeatedInfoByEvent(
  1293. $id,
  1294. $this->course['real_id']
  1295. );
  1296. if (!empty($event['parent_event_id'])) {
  1297. $event['parent_info'] = $this->get_event(
  1298. $event['parent_event_id']
  1299. );
  1300. }
  1301. $event['attachment'] = $this->getAttachmentList(
  1302. $id,
  1303. $this->course
  1304. );
  1305. }
  1306. }
  1307. break;
  1308. case 'admin':
  1309. case 'platform':
  1310. $sql = "SELECT * FROM ".$this->tbl_global_agenda."
  1311. WHERE id = $id";
  1312. $result = Database::query($sql);
  1313. if (Database::num_rows($result)) {
  1314. $event = Database::fetch_array($result, 'ASSOC');
  1315. $event['description'] = $event['content'];
  1316. }
  1317. break;
  1318. }
  1319. return $event;
  1320. }
  1321. /**
  1322. * Gets personal events
  1323. * @param int $start
  1324. * @param int $end
  1325. * @return array
  1326. */
  1327. public function getPersonalEvents($start, $end)
  1328. {
  1329. $start = intval($start);
  1330. $end = intval($end);
  1331. $startCondition = '';
  1332. $endCondition = '';
  1333. if ($start !== 0) {
  1334. $start = api_get_utc_datetime($start);
  1335. $startCondition = "AND date >= '".$start."'";
  1336. }
  1337. if ($start !== 0) {
  1338. $end = api_get_utc_datetime($end);
  1339. $endCondition = "AND (enddate <= '".$end."' OR enddate IS NULL)";
  1340. }
  1341. $user_id = api_get_user_id();
  1342. $sql = "SELECT * FROM ".$this->tbl_personal_agenda."
  1343. WHERE user = $user_id $startCondition $endCondition";
  1344. $result = Database::query($sql);
  1345. $my_events = array();
  1346. if (Database::num_rows($result)) {
  1347. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1348. $event = array();
  1349. $event['id'] = 'personal_'.$row['id'];
  1350. $event['title'] = $row['title'];
  1351. $event['className'] = 'personal';
  1352. $event['borderColor'] = $event['backgroundColor'] = $this->event_personal_color;
  1353. $event['editable'] = true;
  1354. $event['sent_to'] = get_lang('Me');
  1355. $event['type'] = 'personal';
  1356. if (!empty($row['date'])) {
  1357. $event['start'] = $this->formatEventDate($row['date']);
  1358. $event['start_date_localtime'] = api_get_local_time(
  1359. $row['date']
  1360. );
  1361. }
  1362. if (!empty($row['enddate'])) {
  1363. $event['end'] = $this->formatEventDate($row['enddate']);
  1364. $event['end_date_localtime'] = api_get_local_time(
  1365. $row['enddate']
  1366. );
  1367. }
  1368. $event['description'] = $row['text'];
  1369. $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
  1370. $event['parent_event_id'] = 0;
  1371. $event['has_children'] = 0;
  1372. $my_events[] = $event;
  1373. $this->events[] = $event;
  1374. }
  1375. }
  1376. return $my_events;
  1377. }
  1378. /**
  1379. * Get user/group list per event.
  1380. *
  1381. * @param int $eventId
  1382. * @param int $courseId
  1383. * @param integer $sessionId
  1384. * @paraù int $sessionId
  1385. *
  1386. * @return array
  1387. */
  1388. public function getUsersAndGroupSubscribedToEvent(
  1389. $eventId,
  1390. $courseId,
  1391. $sessionId
  1392. ) {
  1393. $eventId = intval($eventId);
  1394. $courseId = intval($courseId);
  1395. $sessionId = intval($sessionId);
  1396. $sessionCondition = "ip.session_id = $sessionId";
  1397. if (empty($sessionId)) {
  1398. $sessionCondition = " (ip.session_id = 0 OR ip.session_id IS NULL) ";
  1399. }
  1400. $tlb_course_agenda = Database::get_course_table(TABLE_AGENDA);
  1401. $tbl_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1402. // Get sent_tos
  1403. $sql = "SELECT DISTINCT to_user_id, to_group_id
  1404. FROM $tbl_property ip
  1405. INNER JOIN $tlb_course_agenda agenda
  1406. ON (
  1407. ip.ref = agenda.id AND
  1408. ip.c_id = agenda.c_id AND
  1409. ip.tool = '".TOOL_CALENDAR_EVENT."'
  1410. )
  1411. WHERE
  1412. ref = $eventId AND
  1413. ip.visibility = '1' AND
  1414. ip.c_id = $courseId AND
  1415. $sessionCondition
  1416. ";
  1417. $result = Database::query($sql);
  1418. $users = array();
  1419. $groups = array();
  1420. $everyone = false;
  1421. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1422. if (!empty($row['to_group_id'])) {
  1423. $groups[] = $row['to_group_id'];
  1424. }
  1425. if (!empty($row['to_user_id'])) {
  1426. $users[] = $row['to_user_id'];
  1427. }
  1428. if (empty($groups) && empty($users)) {
  1429. if ($row['to_group_id'] == 0) {
  1430. $everyone = true;
  1431. }
  1432. }
  1433. }
  1434. return array(
  1435. 'everyone' => $everyone,
  1436. 'users' => $users,
  1437. 'groups' => $groups
  1438. );
  1439. }
  1440. /**
  1441. * @param int $start
  1442. * @param int $end
  1443. * @param int $sessionId
  1444. * @param int $userId
  1445. * @param string $color
  1446. *
  1447. * @return array
  1448. */
  1449. public function getSessionEvents(
  1450. $start,
  1451. $end,
  1452. $sessionId = 0,
  1453. $userId = 0,
  1454. $color = ''
  1455. ) {
  1456. $courses = SessionManager::get_course_list_by_session_id($sessionId);
  1457. if (!empty($courses)) {
  1458. foreach ($courses as $course) {
  1459. $this->getCourseEvents(
  1460. $start,
  1461. $end,
  1462. $course,
  1463. 0,
  1464. $sessionId,
  1465. 0,
  1466. $color
  1467. );
  1468. }
  1469. }
  1470. }
  1471. /**
  1472. * @param int $start
  1473. * @param int $end
  1474. * @param array $courseInfo
  1475. * @param int $groupId
  1476. * @param int $session_id
  1477. * @param int $user_id
  1478. * @param string $color
  1479. *
  1480. * @return array
  1481. */
  1482. public function getCourseEvents(
  1483. $start,
  1484. $end,
  1485. $courseInfo,
  1486. $groupId = 0,
  1487. $session_id = 0,
  1488. $user_id = 0,
  1489. $color = ''
  1490. ) {
  1491. $start = isset($start) && !empty($start) ? api_get_utc_datetime(intval($start)) : null;
  1492. $end = isset($end) && !empty($end) ? api_get_utc_datetime(intval($end)) : null;
  1493. if (empty($courseInfo)) {
  1494. return array();
  1495. }
  1496. $course_id = $courseInfo['real_id'];
  1497. if (empty($course_id)) {
  1498. return array();
  1499. }
  1500. $session_id = intval($session_id);
  1501. $user_id = intval($user_id);
  1502. $groupList = GroupManager::get_group_list(null, $courseInfo['code']);
  1503. $group_name_list = array();
  1504. if (!empty($groupList)) {
  1505. foreach ($groupList as $group) {
  1506. $group_name_list[$group['id']] = $group['name'];
  1507. }
  1508. }
  1509. if (!empty($groupId)) {
  1510. if (!api_is_allowed_to_edit()) {
  1511. $user_id = api_get_user_id();
  1512. $group_memberships = GroupManager::get_group_ids(
  1513. $course_id,
  1514. $user_id
  1515. );
  1516. } else {
  1517. $group_memberships = GroupManager::get_group_ids(
  1518. $course_id,
  1519. $user_id
  1520. );
  1521. }
  1522. } else {
  1523. // if no group was defined but I am a student reviewing his agenda,
  1524. // group events should show, so we should fetch those groups to which
  1525. // I belong
  1526. if (!api_is_allowed_to_edit()) {
  1527. $user_id = api_get_user_id();
  1528. $group_memberships = GroupManager::get_group_ids(
  1529. $course_id,
  1530. $user_id
  1531. );
  1532. } else {
  1533. // If no group was defined and I am a teacher/admin reviewing
  1534. // someone else's agenda, we should fetch this person's groups
  1535. $group_memberships = GroupManager::get_group_ids(
  1536. $course_id,
  1537. $user_id
  1538. );
  1539. }
  1540. }
  1541. $tlb_course_agenda = Database::get_course_table(TABLE_AGENDA);
  1542. $tbl_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1543. if (!empty($groupId)) {
  1544. $group_memberships = array($groupId);
  1545. }
  1546. if (is_array($group_memberships) && count($group_memberships) > 0) {
  1547. if (api_is_allowed_to_edit()) {
  1548. if (!empty($groupId)) {
  1549. $where_condition = "( ip.to_group_id IN (".implode(
  1550. ", ",
  1551. $group_memberships
  1552. ).") ) ";
  1553. } else {
  1554. if (!empty($user_id)) {
  1555. $where_condition = "( ip.to_user_id = $user_id OR ip.to_user_id IS NULL OR (ip.to_group_id IN (0, ".implode(
  1556. ", ",
  1557. $group_memberships
  1558. ).")) ) ";
  1559. } else {
  1560. $where_condition = "( ip.to_group_id IN (0, ".implode(
  1561. ", ",
  1562. $group_memberships
  1563. ).") ) ";
  1564. }
  1565. }
  1566. } else {
  1567. $where_condition = "( ip.to_user_id = $user_id OR ip.to_user_id IS NULL OR (ip.to_group_id IN (0, ".implode(
  1568. ", ",
  1569. $group_memberships
  1570. ).")) ) ";
  1571. }
  1572. if (empty($session_id)) {
  1573. $sessionCondition = "
  1574. (
  1575. agenda.session_id = 0 AND (ip.session_id IS NULL OR ip.session_id = 0)
  1576. ) ";
  1577. } else {
  1578. $sessionCondition = "
  1579. (
  1580. agenda.session_id = $session_id AND
  1581. ip.session_id = $session_id
  1582. ) ";
  1583. }
  1584. $sql = "SELECT DISTINCT
  1585. agenda.*,
  1586. ip.visibility,
  1587. ip.to_group_id,
  1588. ip.insert_user_id,
  1589. ip.ref,
  1590. to_user_id
  1591. FROM $tlb_course_agenda agenda
  1592. INNER JOIN $tbl_property ip
  1593. ON (
  1594. agenda.id = ip.ref AND
  1595. agenda.c_id = ip.c_id AND
  1596. ip.tool = '".TOOL_CALENDAR_EVENT."'
  1597. )
  1598. WHERE
  1599. $where_condition AND
  1600. ip.visibility = '1' AND
  1601. agenda.c_id = $course_id AND
  1602. ip.c_id = agenda.c_id AND
  1603. $sessionCondition
  1604. ";
  1605. } else {
  1606. $visibilityCondition = " ip.visibility='1' AND ";
  1607. if (api_is_allowed_to_edit()) {
  1608. if ($user_id == 0) {
  1609. $where_condition = '';
  1610. } else {
  1611. $where_condition = " (ip.to_user_id = ".$user_id." OR ip.to_user_id IS NULL) AND ip.to_group_id IS NULL AND ";
  1612. }
  1613. $visibilityCondition = " (ip.visibility IN ('1', '0')) AND ";
  1614. } else {
  1615. $where_condition = " ( (ip.to_user_id = ".api_get_user_id()." OR ip.to_user_id IS NULL) AND ip.to_group_id IS NULL) AND ";
  1616. }
  1617. if (empty($session_id)) {
  1618. $sessionCondition = "
  1619. (
  1620. agenda.session_id = 0 AND
  1621. (ip.session_id IS NULL OR ip.session_id = 0)
  1622. ) ";
  1623. } else {
  1624. $sessionCondition = "
  1625. (
  1626. agenda.session_id = $session_id AND
  1627. ip.session_id = $session_id
  1628. ) ";
  1629. }
  1630. $sql = "SELECT DISTINCT
  1631. agenda.*,
  1632. ip.visibility,
  1633. ip.to_group_id,
  1634. ip.insert_user_id,
  1635. ip.ref,
  1636. to_user_id
  1637. FROM $tlb_course_agenda agenda
  1638. INNER JOIN $tbl_property ip
  1639. ON (agenda.id = ip.ref AND agenda.c_id = ip.c_id AND ip.tool='".TOOL_CALENDAR_EVENT."' )
  1640. WHERE
  1641. $where_condition
  1642. $visibilityCondition
  1643. agenda.c_id = $course_id AND
  1644. $sessionCondition
  1645. ";
  1646. }
  1647. $dateCondition = null;
  1648. if (!empty($start) && !empty($end)) {
  1649. $dateCondition .= "AND (
  1650. agenda.start_date BETWEEN '".$start."' AND '".$end."' OR
  1651. agenda.end_date BETWEEN '".$start."' AND '".$end."' OR
  1652. (
  1653. agenda.start_date IS NOT NULL AND agenda.end_date IS NOT NULL AND
  1654. YEAR(agenda.start_date) = YEAR(agenda.end_date) AND
  1655. MONTH('$start') BETWEEN MONTH(agenda.start_date) AND MONTH(agenda.end_date)
  1656. )
  1657. )";
  1658. }
  1659. $sql .= $dateCondition;
  1660. $result = Database::query($sql);
  1661. $coachCanEdit = false;
  1662. if (!empty($session_id)) {
  1663. $coachCanEdit = api_is_coach(
  1664. $session_id,
  1665. $course_id
  1666. ) || api_is_platform_admin();
  1667. }
  1668. if (Database::num_rows($result)) {
  1669. $eventsAdded = array_column($this->events, 'unique_id');
  1670. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1671. $event = array();
  1672. $event['id'] = 'course_'.$row['id'];
  1673. $event['unique_id'] = $row['iid'];
  1674. // To avoid doubles
  1675. if (in_array($event['unique_id'], $eventsAdded)) {
  1676. continue;
  1677. }
  1678. $eventsAdded[] = $event['unique_id'];
  1679. $eventId = $row['ref'];
  1680. $items = $this->getUsersAndGroupSubscribedToEvent(
  1681. $eventId,
  1682. $course_id,
  1683. $this->sessionId
  1684. );
  1685. $group_to_array = $items['groups'];
  1686. $user_to_array = $items['users'];
  1687. $attachmentList = $this->getAttachmentList(
  1688. $row['id'],
  1689. $courseInfo
  1690. );
  1691. $event['attachment'] = '';
  1692. if (!empty($attachmentList)) {
  1693. foreach ($attachmentList as $attachment) {
  1694. $has_attachment = Display::return_icon(
  1695. 'attachment.gif',
  1696. get_lang('Attachment')
  1697. );
  1698. $user_filename = $attachment['filename'];
  1699. $url = api_get_path(WEB_CODE_PATH).'calendar/download.php?file='.$attachment['path'].'&course_id='.$course_id.'&'.api_get_cidreq();
  1700. $event['attachment'] .= $has_attachment.
  1701. Display::url(
  1702. $user_filename,
  1703. $url
  1704. ).'<br />';
  1705. }
  1706. }
  1707. $event['title'] = $row['title'];
  1708. $event['className'] = 'course';
  1709. $event['allDay'] = 'false';
  1710. $event['course_id'] = $course_id;
  1711. $event['borderColor'] = $event['backgroundColor'] = $this->event_course_color;
  1712. $sessionInfo = [];
  1713. if (isset($row['session_id']) && !empty($row['session_id'])) {
  1714. $sessionInfo = api_get_session_info($session_id);
  1715. $event['borderColor'] = $event['backgroundColor'] = $this->event_session_color;
  1716. }
  1717. $event['session_name'] = isset($sessionInfo['name']) ? $sessionInfo['name'] : '';
  1718. $event['course_name'] = isset($courseInfo['title']) ? $courseInfo['title'] : '';
  1719. if (isset($row['to_group_id']) && !empty($row['to_group_id'])) {
  1720. $event['borderColor'] = $event['backgroundColor'] = $this->event_group_color;
  1721. }
  1722. if (!empty($color)) {
  1723. $event['borderColor'] = $event['backgroundColor'] = $color;
  1724. }
  1725. if (isset($row['color']) && !empty($row['color'])) {
  1726. $event['borderColor'] = $event['backgroundColor'] = $row['color'];
  1727. }
  1728. $event['editable'] = false;
  1729. if ($this->getIsAllowedToEdit() && $this->type == 'course') {
  1730. $event['editable'] = true;
  1731. if (!empty($session_id)) {
  1732. if ($coachCanEdit == false) {
  1733. $event['editable'] = false;
  1734. }
  1735. }
  1736. }
  1737. if (!empty($row['start_date'])) {
  1738. $event['start'] = $this->formatEventDate(
  1739. $row['start_date']
  1740. );
  1741. $event['start_date_localtime'] = api_get_local_time(
  1742. $row['start_date']
  1743. );
  1744. }
  1745. if (!empty($row['end_date'])) {
  1746. $event['end'] = $this->formatEventDate($row['end_date']);
  1747. $event['end_date_localtime'] = api_get_local_time(
  1748. $row['end_date']
  1749. );
  1750. }
  1751. $event['sent_to'] = '';
  1752. $event['type'] = 'course';
  1753. if ($row['session_id'] != 0) {
  1754. $event['type'] = 'session';
  1755. }
  1756. // Event Sent to a group?
  1757. if (isset($row['to_group_id']) && !empty($row['to_group_id'])) {
  1758. $sent_to = array();
  1759. if (!empty($group_to_array)) {
  1760. foreach ($group_to_array as $group_item) {
  1761. $sent_to[] = $group_name_list[$group_item];
  1762. }
  1763. }
  1764. $sent_to = implode('@@', $sent_to);
  1765. $sent_to = str_replace(
  1766. '@@',
  1767. '</div><div class="label_tag notice">',
  1768. $sent_to
  1769. );
  1770. $event['sent_to'] = '<div class="label_tag notice">'.$sent_to.'</div>';
  1771. $event['type'] = 'group';
  1772. }
  1773. // Event sent to a user?
  1774. if (isset($row['to_user_id'])) {
  1775. $sent_to = array();
  1776. if (!empty($user_to_array)) {
  1777. foreach ($user_to_array as $item) {
  1778. $user_info = api_get_user_info($item);
  1779. // Add username as tooltip for $event['sent_to'] - ref #4226
  1780. $username = api_htmlentities(
  1781. sprintf(
  1782. get_lang('LoginX'),
  1783. $user_info['username']
  1784. ),
  1785. ENT_QUOTES
  1786. );
  1787. $sent_to[] = "<span title='".$username."'>".$user_info['complete_name']."</span>";
  1788. }
  1789. }
  1790. $sent_to = implode('@@', $sent_to);
  1791. $sent_to = str_replace(
  1792. '@@',
  1793. '</div><div class="label_tag notice">',
  1794. $sent_to
  1795. );
  1796. $event['sent_to'] = '<div class="label_tag notice">'.$sent_to.'</div>';
  1797. }
  1798. //Event sent to everyone!
  1799. if (empty($event['sent_to'])) {
  1800. $event['sent_to'] = '<div class="label_tag notice">'.get_lang(
  1801. 'Everyone'
  1802. ).'</div>';
  1803. }
  1804. $event['description'] = $row['content'];
  1805. $event['visibility'] = $row['visibility'];
  1806. $event['real_id'] = $row['id'];
  1807. $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
  1808. $event['parent_event_id'] = $row['parent_event_id'];
  1809. $event['has_children'] = $this->hasChildren(
  1810. $row['id'],
  1811. $course_id
  1812. ) ? 1 : 0;
  1813. $event['comment'] = $row['comment'];
  1814. $this->events[] = $event;
  1815. }
  1816. }
  1817. return $this->events;
  1818. }
  1819. /**
  1820. * @param int $start tms
  1821. * @param int $end tms
  1822. * @return array
  1823. */
  1824. public function getPlatformEvents($start, $end)
  1825. {
  1826. $start = isset($start) && !empty($start) ? api_get_utc_datetime(
  1827. intval($start)
  1828. ) : null;
  1829. $end = isset($end) && !empty($end) ? api_get_utc_datetime(
  1830. intval($end)
  1831. ) : null;
  1832. $dateCondition = '';
  1833. if (!empty($start) && !empty($end)) {
  1834. $dateCondition .= "AND (
  1835. start_date BETWEEN '".$start."' AND '".$end."' OR
  1836. end_date BETWEEN '".$start."' AND '".$end."' OR
  1837. (
  1838. start_date IS NOT NULL AND end_date IS NOT NULL AND
  1839. YEAR(start_date) = YEAR(end_date) AND
  1840. MONTH('$start') BETWEEN MONTH(start_date) AND MONTH(end_date)
  1841. )
  1842. )";
  1843. }
  1844. $access_url_id = api_get_current_access_url_id();
  1845. $sql = "SELECT *
  1846. FROM ".$this->tbl_global_agenda."
  1847. WHERE access_url_id = $access_url_id
  1848. $dateCondition";
  1849. $result = Database::query($sql);
  1850. $my_events = array();
  1851. if (Database::num_rows($result)) {
  1852. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1853. $event = array();
  1854. $event['id'] = 'platform_'.$row['id'];
  1855. $event['title'] = $row['title'];
  1856. $event['className'] = 'platform';
  1857. $event['allDay'] = 'false';
  1858. $event['borderColor'] = $event['backgroundColor'] = $this->event_platform_color;
  1859. $event['editable'] = false;
  1860. $event['type'] = 'admin';
  1861. if (api_is_platform_admin() && $this->type == 'admin') {
  1862. $event['editable'] = true;
  1863. }
  1864. if (!empty($row['start_date'])) {
  1865. $event['start'] = $this->formatEventDate(
  1866. $row['start_date']
  1867. );
  1868. $event['start_date_localtime'] = api_get_local_time(
  1869. $row['start_date']
  1870. );
  1871. }
  1872. if (!empty($row['end_date'])) {
  1873. $event['end'] = $this->formatEventDate($row['end_date']);
  1874. $event['end_date_localtime'] = api_get_local_time(
  1875. $row['end_date']
  1876. );
  1877. }
  1878. $event['description'] = $row['content'];
  1879. $event['allDay'] = isset($row['all_day']) && $row['all_day'] == 1 ? $row['all_day'] : 0;
  1880. $event['parent_event_id'] = 0;
  1881. $event['has_children'] = 0;
  1882. $my_events[] = $event;
  1883. $this->events[] = $event;
  1884. }
  1885. }
  1886. return $my_events;
  1887. }
  1888. /**
  1889. * Format needed for the Fullcalendar js lib
  1890. *
  1891. * @param string $utcTime
  1892. * @return bool|string
  1893. */
  1894. private function formatEventDate($utcTime)
  1895. {
  1896. $utcTimeZone = new DateTimeZone('UTC');
  1897. $platformTimeZone = new DateTimeZone(api_get_timezone());
  1898. $eventDate = new DateTime($utcTime, $utcTimeZone);
  1899. $eventDate->setTimezone($platformTimeZone);
  1900. return $eventDate->format(DateTime::ISO8601);
  1901. }
  1902. /**
  1903. * @param FormValidator $form
  1904. * @param array $groupList
  1905. * @param array $userList
  1906. * @param array $sendTo array('users' => [1, 2], 'groups' => [3, 4])
  1907. * @param array $attributes
  1908. * @param bool $addOnlyItemsInSendTo
  1909. * @param bool $required
  1910. */
  1911. public function setSendToSelect(
  1912. $form,
  1913. $groupList = [],
  1914. $userList = [],
  1915. $sendTo = [],
  1916. $attributes = [],
  1917. $addOnlyItemsInSendTo = false,
  1918. $required = false
  1919. ) {
  1920. $params = array(
  1921. 'id' => 'users_to_send_id',
  1922. 'data-placeholder' => get_lang('Select'),
  1923. 'multiple' => 'multiple',
  1924. 'class' => 'multiple-select'
  1925. );
  1926. if (!empty($attributes)) {
  1927. $params = array_merge($params, $attributes);
  1928. if (empty($params['multiple'])) {
  1929. unset($params['multiple']);
  1930. }
  1931. }
  1932. $sendToGroups = isset($sendTo['groups']) ? $sendTo['groups'] : array();
  1933. $sendToUsers = isset($sendTo['users']) ? $sendTo['users'] : array();
  1934. /** @var HTML_QuickForm_select $select */
  1935. $select = $form->addSelect(
  1936. 'users_to_send',
  1937. get_lang('To'),
  1938. null,
  1939. $params
  1940. );
  1941. if ($required) {
  1942. $form->setRequired($select);
  1943. }
  1944. $selectedEveryoneOptions = array();
  1945. if (isset($sendTo['everyone']) && $sendTo['everyone']) {
  1946. $selectedEveryoneOptions = array('selected');
  1947. $sendToUsers = array();
  1948. }
  1949. $select->addOption(
  1950. get_lang('Everyone'),
  1951. 'everyone',
  1952. $selectedEveryoneOptions
  1953. );
  1954. $options = array();
  1955. if (is_array($groupList)) {
  1956. foreach ($groupList as $group) {
  1957. $count_users = isset($group['count_users']) ? $group['count_users'] : $group['userNb'];
  1958. $count_users = " &ndash; $count_users ".get_lang('Users');
  1959. $option = array(
  1960. 'text' => $group['name'].$count_users,
  1961. 'value' => "GROUP:".$group['id']
  1962. );
  1963. $selected = in_array(
  1964. $group['id'],
  1965. $sendToGroups
  1966. ) ? true : false;
  1967. if ($selected) {
  1968. $option['selected'] = 'selected';
  1969. }
  1970. if ($addOnlyItemsInSendTo) {
  1971. if ($selected) {
  1972. $options[] = $option;
  1973. }
  1974. } else {
  1975. $options[] = $option;
  1976. }
  1977. }
  1978. $select->addOptGroup($options, get_lang('Groups'));
  1979. }
  1980. // adding the individual users to the select form
  1981. if (is_array($userList)) {
  1982. $options = array();
  1983. foreach ($userList as $user) {
  1984. if ($user['status'] == ANONYMOUS) {
  1985. continue;
  1986. }
  1987. $option = array(
  1988. 'text' => api_get_person_name(
  1989. $user['firstname'],
  1990. $user['lastname']
  1991. ).' ('.$user['username'].')',
  1992. 'value' => "USER:".$user['user_id']
  1993. );
  1994. $selected = in_array(
  1995. $user['user_id'],
  1996. $sendToUsers
  1997. ) ? true : false;
  1998. if ($selected) {
  1999. $option['selected'] = 'selected';
  2000. }
  2001. if ($addOnlyItemsInSendTo) {
  2002. if ($selected) {
  2003. $options[] = $option;
  2004. }
  2005. } else {
  2006. $options[] = $option;
  2007. }
  2008. }
  2009. $select->addOptGroup($options, get_lang('Users'));
  2010. }
  2011. }
  2012. /**
  2013. * Separates the users and groups array
  2014. * users have a value USER:XXX (with XXX the user id
  2015. * groups have a value GROUP:YYY (with YYY the group id)
  2016. * use the 'everyone' key
  2017. * @author Julio Montoya based in separate_users_groups in agenda.inc.php
  2018. * @param array $to
  2019. * @return array
  2020. */
  2021. public function parseSendToArray($to)
  2022. {
  2023. $groupList = array();
  2024. $userList = array();
  2025. $sendTo = null;
  2026. $sendTo['everyone'] = false;
  2027. if (is_array($to) && count($to) > 0) {
  2028. foreach ($to as $item) {
  2029. if ($item == 'everyone') {
  2030. $sendTo['everyone'] = true;
  2031. } else {
  2032. list($type, $id) = explode(':', $item);
  2033. switch ($type) {
  2034. case 'GROUP':
  2035. $groupList[] = $id;
  2036. break;
  2037. case 'USER':
  2038. $userList[] = $id;
  2039. break;
  2040. }
  2041. }
  2042. }
  2043. $sendTo['groups'] = $groupList;
  2044. $sendTo['users'] = $userList;
  2045. }
  2046. return $sendTo;
  2047. }
  2048. /**
  2049. * @param array $params
  2050. * @return FormValidator
  2051. */
  2052. public function getForm($params = [])
  2053. {
  2054. $action = isset($params['action']) ? Security::remove_XSS($params['action']) : null;
  2055. $id = isset($params['id']) ? intval($params['id']) : null;
  2056. if ($this->type == 'course') {
  2057. $url = api_get_self().'?'.api_get_cidreq().'&action='.$action.'&id='.$id.'&type='.$this->type;
  2058. } else {
  2059. $url = api_get_self().'?action='.$action.'&id='.$id.'&type='.$this->type;
  2060. }
  2061. $form = new FormValidator(
  2062. 'add_event',
  2063. 'post',
  2064. $url,
  2065. null,
  2066. array('enctype' => 'multipart/form-data')
  2067. );
  2068. $idAttach = isset($params['id_attach']) ? intval(
  2069. $params['id_attach']
  2070. ) : null;
  2071. $groupId = api_get_group_id();
  2072. if ($id) {
  2073. $form_title = get_lang('ModifyCalendarItem');
  2074. } else {
  2075. $form_title = get_lang('AddCalendarItem');
  2076. }
  2077. $form->addElement('header', $form_title);
  2078. $form->addElement('hidden', 'id', $id);
  2079. $form->addElement('hidden', 'action', $action);
  2080. $form->addElement('hidden', 'id_attach', $idAttach);
  2081. $isSubEventEdition = false;
  2082. $isParentFromSerie = false;
  2083. $showAttachmentForm = true;
  2084. if ($this->type == 'course') {
  2085. // Edition mode.
  2086. if (!empty($id)) {
  2087. $showAttachmentForm = false;
  2088. if (isset($params['parent_event_id']) && !empty($params['parent_event_id'])) {
  2089. $isSubEventEdition = true;
  2090. }
  2091. if (!empty($params['repeat_info'])) {
  2092. $isParentFromSerie = true;
  2093. }
  2094. }
  2095. }
  2096. if ($isSubEventEdition) {
  2097. $form->addElement(
  2098. 'label',
  2099. null,
  2100. Display::return_message(
  2101. get_lang('EditingThisEventWillRemoveItFromTheSerie'),
  2102. 'warning'
  2103. )
  2104. );
  2105. }
  2106. $form->addElement('text', 'title', get_lang('ItemTitle'));
  2107. if (isset($groupId) && !empty($groupId)) {
  2108. $form->addElement(
  2109. 'hidden',
  2110. 'selected_form[0]',
  2111. "GROUP:'.$groupId.'"
  2112. );
  2113. $form->addElement('hidden', 'to', 'true');
  2114. } else {
  2115. $sendTo = isset($params['send_to']) ? $params['send_to'] : ['everyone' => true];
  2116. if ($this->type == 'course') {
  2117. $this->showToForm($form, $sendTo, array(), false, true);
  2118. }
  2119. }
  2120. $form->addDateRangePicker(
  2121. 'date_range',
  2122. get_lang('DateRange'),
  2123. false,
  2124. array('id' => 'date_range')
  2125. );
  2126. $form->addElement('checkbox', 'all_day', null, get_lang('AllDay'));
  2127. if ($this->type == 'course') {
  2128. $repeat = $form->addElement(
  2129. 'checkbox',
  2130. 'repeat',
  2131. null,
  2132. get_lang('RepeatEvent'),
  2133. array('onclick' => 'return plus_repeated_event();')
  2134. );
  2135. $form->addElement(
  2136. 'html',
  2137. '<div id="options2" style="display:none">'
  2138. );
  2139. $form->addElement(
  2140. 'select',
  2141. 'repeat_type',
  2142. get_lang('RepeatType'),
  2143. self::getRepeatTypes()
  2144. );
  2145. $form->addElement(
  2146. 'date_picker',
  2147. 'repeat_end_day',
  2148. get_lang('RepeatEnd'),
  2149. array('id' => 'repeat_end_date_form')
  2150. );
  2151. if ($isSubEventEdition || $isParentFromSerie) {
  2152. if ($isSubEventEdition) {
  2153. $parentEvent = $params['parent_info'];
  2154. $repeatInfo = $parentEvent['repeat_info'];
  2155. } else {
  2156. $repeatInfo = $params['repeat_info'];
  2157. }
  2158. $params['repeat'] = 1;
  2159. $params['repeat_type'] = $repeatInfo['cal_type'];
  2160. $params['repeat_end_day'] = substr(
  2161. api_get_local_time($repeatInfo['cal_end']),
  2162. 0,
  2163. 10
  2164. );
  2165. $form->freeze(array('repeat_type', 'repeat_end_day'));
  2166. $repeat->_attributes['disabled'] = 'disabled';
  2167. }
  2168. $form->addElement('html', '</div>');
  2169. }
  2170. if (!empty($id)) {
  2171. if (empty($params['end_date'])) {
  2172. $params['date_range'] = $params['end_date'];
  2173. }
  2174. $params['date_range'] =
  2175. substr(api_get_local_time($params['start_date']), 0, 16).' / '.
  2176. substr(api_get_local_time($params['end_date']), 0, 16);
  2177. }
  2178. if (!api_is_allowed_to_edit(null, true)) {
  2179. $toolbar = 'AgendaStudent';
  2180. } else {
  2181. $toolbar = 'Agenda';
  2182. }
  2183. $form->addElement(
  2184. 'html_editor',
  2185. 'content',
  2186. get_lang('Description'),
  2187. null,
  2188. array(
  2189. 'ToolbarSet' => $toolbar,
  2190. 'Width' => '100%',
  2191. 'Height' => '200'
  2192. )
  2193. );
  2194. if ($this->type == 'course') {
  2195. $form->addElement('textarea', 'comment', get_lang('Comment'));
  2196. $form->addLabel(
  2197. get_lang('FilesAttachment'),
  2198. '<span id="filepaths">
  2199. <div id="filepath_1">
  2200. <input type="file" name="attach_1"/><br />
  2201. '.get_lang('Description').'&nbsp;&nbsp;<input type="text" name="legend[]" /><br /><br />
  2202. </div>
  2203. </span>'
  2204. );
  2205. $form->addLabel(
  2206. '',
  2207. '<span id="link-more-attach">
  2208. <a href="javascript://" onclick="return add_image_form()">'.
  2209. get_lang('AddOneMoreFile').'</a>
  2210. </span>&nbsp;('.sprintf(
  2211. get_lang('MaximunFileSizeX'),
  2212. format_file_size(
  2213. api_get_setting('message_max_upload_filesize')
  2214. )
  2215. ).')'
  2216. );
  2217. if (isset($params['attachment']) && !empty($params['attachment'])) {
  2218. $attachmentList = $params['attachment'];
  2219. foreach ($attachmentList as $attachment) {
  2220. $params['file_comment'] = $attachment['comment'];
  2221. if (!empty($attachment['path'])) {
  2222. $form->addElement(
  2223. 'checkbox',
  2224. 'delete_attachment['.$attachment['id'].']',
  2225. null,
  2226. get_lang(
  2227. 'DeleteAttachment'
  2228. ).': '.$attachment['filename']
  2229. );
  2230. }
  2231. }
  2232. }
  2233. $form->addElement(
  2234. 'textarea',
  2235. 'file_comment',
  2236. get_lang('FileComment')
  2237. );
  2238. }
  2239. if (empty($id)) {
  2240. $form->addElement(
  2241. 'checkbox',
  2242. 'add_announcement',
  2243. null,
  2244. get_lang('AddAnnouncement').'&nbsp('.get_lang('SendMail').')'
  2245. );
  2246. }
  2247. if ($id) {
  2248. $form->addButtonUpdate(get_lang('ModifyEvent'));
  2249. } else {
  2250. $form->addButtonSave(get_lang('AgendaAdd'));
  2251. }
  2252. $form->setDefaults($params);
  2253. $form->addRule(
  2254. 'date_range',
  2255. get_lang('ThisFieldIsRequired'),
  2256. 'required'
  2257. );
  2258. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  2259. return $form;
  2260. }
  2261. /**
  2262. * @param FormValidator $form
  2263. * @param array $sendTo array('everyone' => false, 'users' => [1, 2], 'groups' => [3, 4])
  2264. * @param array $attributes
  2265. * @param bool $addOnlyItemsInSendTo
  2266. * @param bool $required
  2267. * @return bool
  2268. */
  2269. public function showToForm(
  2270. $form,
  2271. $sendTo = array(),
  2272. $attributes = array(),
  2273. $addOnlyItemsInSendTo = false,
  2274. $required = false
  2275. ) {
  2276. if ($this->type != 'course') {
  2277. return false;
  2278. }
  2279. $order = 'lastname';
  2280. if (api_is_western_name_order()) {
  2281. $order = 'firstname';
  2282. }
  2283. $userList = CourseManager::get_user_list_from_course_code(
  2284. api_get_course_id(),
  2285. $this->sessionId,
  2286. null,
  2287. $order
  2288. );
  2289. $groupList = CourseManager::get_group_list_of_course(
  2290. api_get_course_id(),
  2291. $this->sessionId
  2292. );
  2293. $this->setSendToSelect(
  2294. $form,
  2295. $groupList,
  2296. $userList,
  2297. $sendTo,
  2298. $attributes,
  2299. $addOnlyItemsInSendTo,
  2300. $required
  2301. );
  2302. return true;
  2303. }
  2304. /**
  2305. * @param int $id
  2306. * @param int $visibility 0= invisible, 1 visible
  2307. * @param array $courseInfo
  2308. * @param int $userId
  2309. */
  2310. public static function changeVisibility(
  2311. $id,
  2312. $visibility,
  2313. $courseInfo,
  2314. $userId = null
  2315. ) {
  2316. $id = intval($id);
  2317. if (empty($userId)) {
  2318. $userId = api_get_user_id();
  2319. } else {
  2320. $userId = intval($userId);
  2321. }
  2322. if ($visibility == 0) {
  2323. api_item_property_update(
  2324. $courseInfo,
  2325. TOOL_CALENDAR_EVENT,
  2326. $id,
  2327. 'invisible',
  2328. $userId
  2329. );
  2330. } else {
  2331. api_item_property_update(
  2332. $courseInfo,
  2333. TOOL_CALENDAR_EVENT,
  2334. $id,
  2335. 'visible',
  2336. $userId
  2337. );
  2338. }
  2339. }
  2340. /**
  2341. * Get repeat types
  2342. * @return array
  2343. */
  2344. public static function getRepeatTypes()
  2345. {
  2346. return array(
  2347. 'daily' => get_lang('RepeatDaily'),
  2348. 'weekly' => get_lang('RepeatWeekly'),
  2349. 'monthlyByDate' => get_lang('RepeatMonthlyByDate'),
  2350. //monthlyByDay"> get_lang('RepeatMonthlyByDay');
  2351. //monthlyByDayR' => get_lang('RepeatMonthlyByDayR'),
  2352. 'yearly' => get_lang('RepeatYearly')
  2353. );
  2354. }
  2355. /**
  2356. * Show a list with all the attachments according to the post's id
  2357. * @param int $eventId
  2358. * @param array $courseInfo
  2359. * @return array with the post info
  2360. */
  2361. public function getAttachmentList($eventId, $courseInfo)
  2362. {
  2363. $tableAttachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
  2364. $courseId = intval($courseInfo['real_id']);
  2365. $eventId = intval($eventId);
  2366. $sql = "SELECT id, path, filename, comment
  2367. FROM $tableAttachment
  2368. WHERE
  2369. c_id = $courseId AND
  2370. agenda_id = $eventId";
  2371. $result = Database::query($sql);
  2372. $list = array();
  2373. if (Database::num_rows($result) != 0) {
  2374. $list = Database::store_result($result, 'ASSOC');
  2375. }
  2376. return $list;
  2377. }
  2378. /**
  2379. * Show a list with all the attachments according to the post's id
  2380. * @param int $attachmentId
  2381. * @param int $eventId
  2382. * @param array $courseInfo
  2383. * @return array with the post info
  2384. */
  2385. public function getAttachment($attachmentId, $eventId, $courseInfo)
  2386. {
  2387. $tableAttachment = Database::get_course_table(TABLE_AGENDA_ATTACHMENT);
  2388. $courseId = intval($courseInfo['real_id']);
  2389. $eventId = intval($eventId);
  2390. $attachmentId = intval($attachmentId);
  2391. $row = array();
  2392. $sql = "SELECT id, path, filename, comment
  2393. FROM $tableAttachment
  2394. WHERE
  2395. c_id = $courseId AND
  2396. agenda_id = $eventId AND
  2397. id = $attachmentId
  2398. ";
  2399. $result = Database::query($sql);
  2400. if (Database::num_rows($result) != 0) {
  2401. $row = Database::fetch_array($result, 'ASSOC');
  2402. }
  2403. return $row;
  2404. }
  2405. /**
  2406. * Add an attachment file into agenda
  2407. * @param int $eventId
  2408. * @param array $fileUserUpload ($_FILES['user_upload'])
  2409. * @param string $comment about file
  2410. * @param array $courseInfo
  2411. * @return string
  2412. */
  2413. public function addAttachment(
  2414. $eventId,
  2415. $fileUserUpload,
  2416. $comment,
  2417. $courseInfo
  2418. ) {
  2419. $agenda_table_attachment = Database::get_course_table(
  2420. TABLE_AGENDA_ATTACHMENT
  2421. );
  2422. $eventId = intval($eventId);
  2423. // Storing the attachments
  2424. $upload_ok = false;
  2425. if (!empty($fileUserUpload['name'])) {
  2426. $upload_ok = process_uploaded_file($fileUserUpload);
  2427. }
  2428. if (!empty($upload_ok)) {
  2429. $courseDir = $courseInfo['directory'].'/upload/calendar';
  2430. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  2431. $uploadDir = $sys_course_path.$courseDir;
  2432. // Try to add an extension to the file if it hasn't one
  2433. $new_file_name = add_ext_on_mime(
  2434. stripslashes($fileUserUpload['name']),
  2435. $fileUserUpload['type']
  2436. );
  2437. // user's file name
  2438. $file_name = $fileUserUpload['name'];
  2439. if (!filter_extension($new_file_name)) {
  2440. return Display::return_message(
  2441. get_lang('UplUnableToSaveFileFilteredExtension'),
  2442. 'error'
  2443. );
  2444. } else {
  2445. $new_file_name = uniqid('');
  2446. $new_path = $uploadDir.'/'.$new_file_name;
  2447. $result = @move_uploaded_file(
  2448. $fileUserUpload['tmp_name'],
  2449. $new_path
  2450. );
  2451. $course_id = api_get_course_int_id();
  2452. $size = intval($fileUserUpload['size']);
  2453. // Storing the attachments if any
  2454. if ($result) {
  2455. $params = [
  2456. 'c_id' => $course_id,
  2457. 'filename' => $file_name,
  2458. 'comment' => $comment,
  2459. 'path' => $new_file_name,
  2460. 'agenda_id' => $eventId,
  2461. 'size' => $size
  2462. ];
  2463. $id = Database::insert($agenda_table_attachment, $params);
  2464. if ($id) {
  2465. $sql = "UPDATE $agenda_table_attachment
  2466. SET id = iid WHERE iid = $id";
  2467. Database::query($sql);
  2468. api_item_property_update(
  2469. $courseInfo,
  2470. 'calendar_event_attachment',
  2471. $id,
  2472. 'AgendaAttachmentAdded',
  2473. api_get_user_id()
  2474. );
  2475. }
  2476. }
  2477. }
  2478. }
  2479. }
  2480. /**
  2481. * @param int $attachmentId
  2482. * @param int $eventId
  2483. * @param array $fileUserUpload
  2484. * @param string $comment
  2485. * @param array $courseInfo
  2486. */
  2487. public function updateAttachment(
  2488. $attachmentId,
  2489. $eventId,
  2490. $fileUserUpload,
  2491. $comment,
  2492. $courseInfo
  2493. ) {
  2494. $attachment = $this->getAttachment(
  2495. $attachmentId,
  2496. $eventId,
  2497. $courseInfo
  2498. );
  2499. if (!empty($attachment)) {
  2500. $this->deleteAttachmentFile($attachmentId, $courseInfo);
  2501. }
  2502. $this->addAttachment($eventId, $fileUserUpload, $comment, $courseInfo);
  2503. }
  2504. /**
  2505. * This function delete a attachment file by id
  2506. * @param int $attachmentId
  2507. * @param array $courseInfo
  2508. * @return string
  2509. */
  2510. public function deleteAttachmentFile($attachmentId, $courseInfo)
  2511. {
  2512. $agenda_table_attachment = Database::get_course_table(
  2513. TABLE_AGENDA_ATTACHMENT
  2514. );
  2515. $attachmentId = intval($attachmentId);
  2516. $courseId = $courseInfo['real_id'];
  2517. if (empty($courseId) || empty($attachmentId)) {
  2518. return false;
  2519. }
  2520. $sql = "DELETE FROM $agenda_table_attachment
  2521. WHERE c_id = $courseId AND id = ".$attachmentId;
  2522. $result = Database::query($sql);
  2523. // update item_property
  2524. api_item_property_update(
  2525. $courseInfo,
  2526. 'calendar_event_attachment',
  2527. $attachmentId,
  2528. 'AgendaAttachmentDeleted',
  2529. api_get_user_id()
  2530. );
  2531. if (!empty($result)) {
  2532. return Display::return_message(
  2533. get_lang("AttachmentFileDeleteSuccess"),
  2534. 'confirmation'
  2535. );
  2536. }
  2537. }
  2538. /**
  2539. * Adds x weeks to a UNIX timestamp
  2540. * @param int The timestamp
  2541. * @param int The number of weeks to add
  2542. * @param integer $timestamp
  2543. * @return int The new timestamp
  2544. */
  2545. public function addWeek($timestamp, $num = 1)
  2546. {
  2547. return $timestamp + $num * 604800;
  2548. }
  2549. /**
  2550. * Adds x months to a UNIX timestamp
  2551. * @param int The timestamp
  2552. * @param int The number of years to add
  2553. * @param integer $timestamp
  2554. * @return int The new timestamp
  2555. */
  2556. public function addMonth($timestamp, $num = 1)
  2557. {
  2558. list($y, $m, $d, $h, $n, $s) = split(
  2559. '/',
  2560. date('Y/m/d/h/i/s', $timestamp)
  2561. );
  2562. if ($m + $num > 12) {
  2563. $y += floor($num / 12);
  2564. $m += $num % 12;
  2565. } else {
  2566. $m += $num;
  2567. }
  2568. return mktime($h, $n, $s, $m, $d, $y);
  2569. }
  2570. /**
  2571. * Adds x years to a UNIX timestamp
  2572. * @param int The timestamp
  2573. * @param int The number of years to add
  2574. * @param integer $timestamp
  2575. * @return int The new timestamp
  2576. */
  2577. public function addYear($timestamp, $num = 1)
  2578. {
  2579. list($y, $m, $d, $h, $n, $s) = split(
  2580. '/',
  2581. date('Y/m/d/h/i/s', $timestamp)
  2582. );
  2583. return mktime($h, $n, $s, $m, $d, $y + $num);
  2584. }
  2585. /**
  2586. * @param int $eventId
  2587. * @return array
  2588. */
  2589. public function getAllRepeatEvents($eventId)
  2590. {
  2591. $events = array();
  2592. switch ($this->type) {
  2593. case 'personal':
  2594. break;
  2595. case 'course':
  2596. if (!empty($this->course['real_id'])) {
  2597. $sql = "SELECT * FROM ".$this->tbl_course_agenda."
  2598. WHERE
  2599. c_id = ".$this->course['real_id']." AND
  2600. parent_event_id = ".$eventId;
  2601. $result = Database::query($sql);
  2602. if (Database::num_rows($result)) {
  2603. while ($row = Database::fetch_array($result, 'ASSOC')) {
  2604. $events[] = $row;
  2605. }
  2606. }
  2607. }
  2608. break;
  2609. }
  2610. return $events;
  2611. }
  2612. /**
  2613. * @param int $eventId
  2614. * @param int $courseId
  2615. *
  2616. * @return bool
  2617. */
  2618. public function hasChildren($eventId, $courseId)
  2619. {
  2620. $eventId = intval($eventId);
  2621. $courseId = intval($courseId);
  2622. $sql = "SELECT count(DISTINCT(id)) as count
  2623. FROM ".$this->tbl_course_agenda."
  2624. WHERE
  2625. c_id = $courseId AND
  2626. parent_event_id = $eventId";
  2627. $result = Database::query($sql);
  2628. if (Database::num_rows($result)) {
  2629. $row = Database::fetch_array($result, 'ASSOC');
  2630. return $row['count'] > 0;
  2631. }
  2632. return false;
  2633. }
  2634. /**
  2635. * @param int $filter
  2636. * @param string $view
  2637. * @return string
  2638. */
  2639. public function displayActions($view, $filter = 0)
  2640. {
  2641. $courseInfo = api_get_course_info();
  2642. $groupInfo = GroupManager::get_group_properties(api_get_group_id());
  2643. $groupIid = isset($groupInfo['iid']) ? $groupInfo['iid'] : 0;
  2644. $courseCondition = '';
  2645. if (!empty($courseInfo)) {
  2646. $courseCondition = api_get_cidreq();
  2647. }
  2648. $actionsLeft = '';
  2649. $actionsLeft .= "<a href='".api_get_path(
  2650. WEB_CODE_PATH
  2651. )."calendar/agenda_js.php?type={$this->type}&".$courseCondition."'>".
  2652. Display::return_icon(
  2653. 'calendar.png',
  2654. get_lang('Calendar'),
  2655. '',
  2656. ICON_SIZE_MEDIUM
  2657. )."</a>";
  2658. $actionsLeft .= "<a href='".api_get_path(
  2659. WEB_CODE_PATH
  2660. )."calendar/agenda_list.php?type={$this->type}&".$courseCondition."'>".
  2661. Display::return_icon(
  2662. 'week.png',
  2663. get_lang('AgendaList'),
  2664. '',
  2665. ICON_SIZE_MEDIUM
  2666. )."</a>";
  2667. $form = '';
  2668. if (api_is_allowed_to_edit(false, true) ||
  2669. (api_get_course_setting(
  2670. 'allow_user_edit_agenda'
  2671. ) && !api_is_anonymous()) &&
  2672. api_is_allowed_to_session_edit(false, true) ||
  2673. (GroupManager::user_has_access(
  2674. api_get_user_id(),
  2675. $groupIid,
  2676. GroupManager::GROUP_TOOL_CALENDAR
  2677. ) &&
  2678. GroupManager::is_tutor_of_group(api_get_user_id(), $groupIid))
  2679. ) {
  2680. $actionsLeft .= Display::url(
  2681. Display::return_icon(
  2682. 'new_event.png',
  2683. get_lang('AgendaAdd'),
  2684. '',
  2685. ICON_SIZE_MEDIUM
  2686. ),
  2687. api_get_path(
  2688. WEB_CODE_PATH
  2689. )."calendar/agenda.php?".api_get_cidreq(
  2690. )."&action=add&type=".$this->type
  2691. );
  2692. $actionsLeft .= Display::url(
  2693. Display::return_icon(
  2694. 'import_calendar.png',
  2695. get_lang('ICalFileImport'),
  2696. '',
  2697. ICON_SIZE_MEDIUM
  2698. ),
  2699. api_get_path(
  2700. WEB_CODE_PATH
  2701. )."calendar/agenda.php?".api_get_cidreq(
  2702. )."&action=importical&type=".$this->type
  2703. );
  2704. if ($this->type === 'course') {
  2705. if (!isset($_GET['action'])) {
  2706. $form = new FormValidator(
  2707. 'form-search',
  2708. 'post',
  2709. '',
  2710. '',
  2711. array(),
  2712. FormValidator::LAYOUT_INLINE
  2713. );
  2714. $attributes = array(
  2715. 'multiple' => false,
  2716. 'id' => 'select_form_id_search'
  2717. );
  2718. $selectedValues = $this->parseAgendaFilter($filter);
  2719. $this->showToForm($form, $selectedValues, $attributes);
  2720. $form = $form->returnForm();
  2721. }
  2722. }
  2723. }
  2724. if (api_is_platform_admin() ||
  2725. api_is_teacher() ||
  2726. api_is_student_boss() ||
  2727. api_is_drh() ||
  2728. api_is_session_admin() ||
  2729. api_is_coach()
  2730. ) {
  2731. if ($this->type == 'personal') {
  2732. $form = null;
  2733. if (!isset($_GET['action'])) {
  2734. $form = new FormValidator(
  2735. 'form-search',
  2736. 'get',
  2737. api_get_self().'?type=personal&',
  2738. '',
  2739. array(),
  2740. FormValidator::LAYOUT_INLINE
  2741. );
  2742. $sessions = SessionManager::get_sessions_by_user(
  2743. api_get_user_id()
  2744. );
  2745. $form->addHidden('type', 'personal');
  2746. $sessions = array_column(
  2747. $sessions,
  2748. 'session_name',
  2749. 'session_id'
  2750. );
  2751. $sessions = ['0' => get_lang('SelectAnOption')] + $sessions;
  2752. $form->addSelect(
  2753. 'session_id',
  2754. get_lang('Session'),
  2755. $sessions,
  2756. ['id' => 'session_id', 'onchange' => 'submit();']
  2757. );
  2758. $form->addButtonReset(get_lang('Reset'));
  2759. $form = $form->returnForm();
  2760. }
  2761. }
  2762. }
  2763. $actionsRight = '';
  2764. if ($view == 'calendar') {
  2765. $actionsRight .= $form;
  2766. }
  2767. $toolbar = Display::toolbarAction(
  2768. 'toolbar-agenda',
  2769. array(0 => $actionsLeft, 1 => $actionsRight),
  2770. 2,
  2771. false
  2772. );
  2773. return $toolbar;
  2774. }
  2775. /**
  2776. * @return FormValidator
  2777. */
  2778. public function getImportCalendarForm()
  2779. {
  2780. $form = new FormValidator(
  2781. 'frm_import_ical',
  2782. 'post',
  2783. api_get_self().'?action=importical&type='.$this->type,
  2784. array('enctype' => 'multipart/form-data')
  2785. );
  2786. $form->addElement('header', get_lang('ICalFileImport'));
  2787. $form->addElement('file', 'ical_import', get_lang('ICalFileImport'));
  2788. $form->addRule(
  2789. 'ical_import',
  2790. get_lang('ThisFieldIsRequired'),
  2791. 'required'
  2792. );
  2793. $form->addButtonImport(get_lang('Import'), 'ical_submit');
  2794. return $form;
  2795. }
  2796. /**
  2797. * @param array $courseInfo
  2798. * @param $file
  2799. * @return false|string
  2800. */
  2801. public function importEventFile($courseInfo, $file)
  2802. {
  2803. $charset = api_get_system_encoding();
  2804. $filepath = api_get_path(SYS_ARCHIVE_PATH).$file['name'];
  2805. $messages = array();
  2806. if (!@move_uploaded_file($file['tmp_name'], $filepath)) {
  2807. error_log(
  2808. 'Problem moving uploaded file: '.$file['error'].' in '.__FILE__.' line '.__LINE__
  2809. );
  2810. return false;
  2811. }
  2812. $data = file_get_contents($filepath);
  2813. $trans = array(
  2814. 'DAILY' => 'daily',
  2815. 'WEEKLY' => 'weekly',
  2816. 'MONTHLY' => 'monthlyByDate',
  2817. 'YEARLY' => 'yearly'
  2818. );
  2819. $sentTo = array('everyone' => true);
  2820. $calendar = Sabre\VObject\Reader::read($data);
  2821. $currentTimeZone = api_get_timezone();
  2822. if (!empty($calendar->VEVENT)) {
  2823. foreach ($calendar->VEVENT as $event) {
  2824. $start = $event->DTSTART->getDateTime();
  2825. $end = $event->DTEND->getDateTime();
  2826. //Sabre\VObject\DateTimeParser::parseDateTime(string $dt, \Sabre\VObject\DateTimeZone $tz)
  2827. $startDateTime = api_get_local_time(
  2828. $start->format('Y-m-d H:i:s'),
  2829. $currentTimeZone,
  2830. $start->format('e')
  2831. );
  2832. $endDateTime = api_get_local_time(
  2833. $end->format('Y-m-d H:i'),
  2834. $currentTimeZone,
  2835. $end->format('e')
  2836. );
  2837. $title = api_convert_encoding(
  2838. (string)$event->summary,
  2839. $charset,
  2840. 'UTF-8'
  2841. );
  2842. $description = api_convert_encoding(
  2843. (string)$event->description,
  2844. $charset,
  2845. 'UTF-8'
  2846. );
  2847. $id = $this->addEvent(
  2848. $startDateTime,
  2849. $endDateTime,
  2850. 'false',
  2851. $title,
  2852. $description,
  2853. $sentTo
  2854. );
  2855. $messages[] = " $title - ".$startDateTime." - ".$endDateTime;
  2856. //$attendee = (string)$event->attendee;
  2857. /** @var Sabre\VObject\Property\ICalendar\Recur $repeat */
  2858. $repeat = $event->RRULE;
  2859. if ($id && !empty($repeat)) {
  2860. $repeat = $repeat->getParts();
  2861. $freq = $trans[$repeat['FREQ']];
  2862. if (isset($repeat['UNTIL']) && !empty($repeat['UNTIL'])) {
  2863. // Check if datetime or just date (strlen == 8)
  2864. if (strlen($repeat['UNTIL']) == 8) {
  2865. // Fix the datetime format to avoid exception in the next step
  2866. $repeat['UNTIL'] .= 'T000000';
  2867. }
  2868. $until = Sabre\VObject\DateTimeParser::parseDateTime(
  2869. $repeat['UNTIL'],
  2870. new DateTimeZone($currentTimeZone)
  2871. );
  2872. $until = $until->format('Y-m-d H:i');
  2873. //$res = agenda_add_repeat_item($courseInfo, $id, $freq, $until, $attendee);
  2874. $this->addRepeatedItem(
  2875. $id,
  2876. $freq,
  2877. $until,
  2878. $sentTo
  2879. );
  2880. }
  2881. if (!empty($repeat['COUNT'])) {
  2882. /*$count = $repeat['COUNT'];
  2883. $interval = $repeat['INTERVAL'];
  2884. $endDate = null;
  2885. switch($freq) {
  2886. case 'daily':
  2887. $start = api_strtotime($startDateTime);
  2888. $date = new DateTime($startDateTime);
  2889. $days = $count * $interval;
  2890. var_dump($days);
  2891. $date->add(new DateInterval("P".$days."D"));
  2892. $endDate = $date->format('Y-m-d H:i');
  2893. //$endDate = $count *
  2894. for ($i = 0; $i < $count; $i++) {
  2895. $days = 86400 * 7
  2896. }
  2897. }
  2898. }*/
  2899. //$res = agenda_add_repeat_item($courseInfo, $id, $freq, $count, $attendee);
  2900. /*$this->addRepeatedItem(
  2901. $id,
  2902. $freq,
  2903. $endDate,
  2904. $sentTo
  2905. );*/
  2906. }
  2907. }
  2908. }
  2909. }
  2910. if (!empty($messages)) {
  2911. $messages = implode('<br /> ', $messages);
  2912. } else {
  2913. $messages = get_lang('NoAgendaItems');
  2914. }
  2915. return $messages;
  2916. }
  2917. /**
  2918. * Parse filter turns USER:12 to ['users' => [12])] or G:1 ['groups' => [1]]
  2919. * @param integer $filter
  2920. * @return array
  2921. */
  2922. public function parseAgendaFilter($filter)
  2923. {
  2924. $everyone = false;
  2925. $groupId = null;
  2926. $userId = null;
  2927. if ($filter == 'everyone') {
  2928. $everyone = true;
  2929. } else {
  2930. if (substr($filter, 0, 1) == 'G') {
  2931. $groupId = str_replace('GROUP:', '', $filter);
  2932. } else {
  2933. $userId = str_replace('USER:', '', $filter);
  2934. }
  2935. }
  2936. if (empty($userId) && empty($groupId)) {
  2937. $everyone = true;
  2938. }
  2939. return array(
  2940. 'everyone' => $everyone,
  2941. 'users' => array($userId),
  2942. 'groups' => array($groupId)
  2943. );
  2944. }
  2945. /**
  2946. * This function retrieves all the agenda items of all the courses the user is subscribed to
  2947. */
  2948. public static function get_myagendaitems(
  2949. $user_id,
  2950. $courses_dbs,
  2951. $month,
  2952. $year
  2953. ) {
  2954. $user_id = intval($user_id);
  2955. $items = array();
  2956. $my_list = array();
  2957. // get agenda-items for every course
  2958. foreach ($courses_dbs as $key => $array_course_info) {
  2959. //databases of the courses
  2960. $TABLEAGENDA = Database:: get_course_table(TABLE_AGENDA);
  2961. $TABLE_ITEMPROPERTY = Database:: get_course_table(
  2962. TABLE_ITEM_PROPERTY
  2963. );
  2964. $group_memberships = GroupManager::get_group_ids(
  2965. $array_course_info['real_id'],
  2966. $user_id
  2967. );
  2968. $course_user_status = CourseManager::getUserInCourseStatus(
  2969. $user_id,
  2970. $array_course_info['real_id']
  2971. );
  2972. // if the user is administrator of that course we show all the agenda items
  2973. if ($course_user_status == '1') {
  2974. //echo "course admin";
  2975. $sqlquery = "SELECT DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  2976. FROM ".$TABLEAGENDA." agenda,
  2977. ".$TABLE_ITEMPROPERTY." ip
  2978. WHERE agenda.id = ip.ref
  2979. AND MONTH(agenda.start_date)='".$month."'
  2980. AND YEAR(agenda.start_date)='".$year."'
  2981. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  2982. AND ip.visibility='1'
  2983. GROUP BY agenda.id
  2984. ORDER BY start_date ";
  2985. } else {
  2986. // if the user is not an administrator of that course
  2987. if (is_array($group_memberships) && count(
  2988. $group_memberships
  2989. ) > 0
  2990. ) {
  2991. $sqlquery = "SELECT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  2992. FROM ".$TABLEAGENDA." agenda,
  2993. ".$TABLE_ITEMPROPERTY." ip
  2994. WHERE agenda.id = ip.ref
  2995. AND MONTH(agenda.start_date)='".$month."'
  2996. AND YEAR(agenda.start_date)='".$year."'
  2997. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  2998. AND ( ip.to_user_id='".$user_id."' OR (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(
  2999. ", ",
  3000. $group_memberships
  3001. ).")) )
  3002. AND ip.visibility='1'
  3003. ORDER BY start_date ";
  3004. } else {
  3005. $sqlquery = "SELECT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref
  3006. FROM ".$TABLEAGENDA." agenda,
  3007. ".$TABLE_ITEMPROPERTY." ip
  3008. WHERE agenda.id = ip.ref
  3009. AND MONTH(agenda.start_date)='".$month."'
  3010. AND YEAR(agenda.start_date)='".$year."'
  3011. AND ip.tool='".TOOL_CALENDAR_EVENT."'
  3012. AND ( ip.to_user_id='".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL)
  3013. AND ip.visibility='1'
  3014. ORDER BY start_date ";
  3015. }
  3016. }
  3017. $result = Database::query($sqlquery);
  3018. while ($item = Database::fetch_array($result, 'ASSOC')) {
  3019. $agendaday = -1;
  3020. if (!empty($item['start_date'])) {
  3021. $item['start_date'] = api_get_local_time(
  3022. $item['start_date']
  3023. );
  3024. $item['start_date_tms'] = api_strtotime(
  3025. $item['start_date']
  3026. );
  3027. $agendaday = date("j", $item['start_date_tms']);
  3028. }
  3029. if (!empty($item['end_date'])) {
  3030. $item['end_date'] = api_get_local_time($item['end_date']);
  3031. }
  3032. $url = api_get_path(
  3033. WEB_CODE_PATH
  3034. )."calendar/agenda.php?cidReq=".urlencode(
  3035. $array_course_info["code"]
  3036. )."&day=$agendaday&month=$month&year=$year#$agendaday";
  3037. $item['url'] = $url;
  3038. $item['course_name'] = $array_course_info['title'];
  3039. $item['calendar_type'] = 'course';
  3040. $item['course_id'] = $array_course_info['course_id'];
  3041. $my_list[$agendaday][] = $item;
  3042. }
  3043. }
  3044. // sorting by hour for every day
  3045. $agendaitems = array();
  3046. while (list ($agendaday, $tmpitems) = each($items)) {
  3047. if (!isset($agendaitems[$agendaday])) {
  3048. $agendaitems[$agendaday] = '';
  3049. }
  3050. sort($tmpitems);
  3051. while (list ($key, $val) = each($tmpitems)) {
  3052. $agendaitems[$agendaday] .= $val;
  3053. }
  3054. }
  3055. return $my_list;
  3056. }
  3057. /**
  3058. * This function retrieves one personal agenda item returns it.
  3059. * @param array The array containing existing events. We add to this array.
  3060. * @param int Day
  3061. * @param int Month
  3062. * @param int Year (4 digits)
  3063. * @param int Week number
  3064. * @param string Type of view (month_view, week_view, day_view)
  3065. * @return array The results of the database query, or null if not found
  3066. */
  3067. public static function get_global_agenda_items(
  3068. $agendaitems,
  3069. $day = "",
  3070. $month = "",
  3071. $year = "",
  3072. $week = "",
  3073. $type
  3074. ) {
  3075. $tbl_global_agenda = Database::get_main_table(
  3076. TABLE_MAIN_SYSTEM_CALENDAR
  3077. );
  3078. $month = intval($month);
  3079. $year = intval($year);
  3080. $week = intval($week);
  3081. $day = intval($day);
  3082. // 1. creating the SQL statement for getting the personal agenda items in MONTH view
  3083. $current_access_url_id = api_get_current_access_url_id();
  3084. if ($type == "month_view" or $type == "") {
  3085. // We are in month view
  3086. $sql = "SELECT * FROM ".$tbl_global_agenda." WHERE MONTH(start_date) = ".$month." AND YEAR(start_date) = ".$year." AND access_url_id = $current_access_url_id ORDER BY start_date ASC";
  3087. }
  3088. // 2. creating the SQL statement for getting the personal agenda items in WEEK view
  3089. if ($type == "week_view") { // we are in week view
  3090. $start_end_day_of_week = self::calculate_start_end_of_week(
  3091. $week,
  3092. $year
  3093. );
  3094. $start_day = $start_end_day_of_week['start']['day'];
  3095. $start_month = $start_end_day_of_week['start']['month'];
  3096. $start_year = $start_end_day_of_week['start']['year'];
  3097. $end_day = $start_end_day_of_week['end']['day'];
  3098. $end_month = $start_end_day_of_week['end']['month'];
  3099. $end_year = $start_end_day_of_week['end']['year'];
  3100. // in sql statements you have to use year-month-day for date calculations
  3101. $start_filter = $start_year."-".$start_month."-".$start_day." 00:00:00";
  3102. $start_filter = api_get_utc_datetime($start_filter);
  3103. $end_filter = $end_year."-".$end_month."-".$end_day." 23:59:59";
  3104. $end_filter = api_get_utc_datetime($end_filter);
  3105. $sql = " SELECT * FROM ".$tbl_global_agenda." WHERE start_date>='".$start_filter."' AND start_date<='".$end_filter."' AND access_url_id = $current_access_url_id ";
  3106. }
  3107. // 3. creating the SQL statement for getting the personal agenda items in DAY view
  3108. if ($type == "day_view") { // we are in day view
  3109. // we could use mysql date() function but this is only available from 4.1 and higher
  3110. $start_filter = $year."-".$month."-".$day." 00:00:00";
  3111. $start_filter = api_get_utc_datetime($start_filter);
  3112. $end_filter = $year."-".$month."-".$day." 23:59:59";
  3113. $end_filter = api_get_utc_datetime($end_filter);
  3114. $sql = " SELECT * FROM ".$tbl_global_agenda." WHERE start_date>='".$start_filter."' AND start_date<='".$end_filter."' AND access_url_id = $current_access_url_id";
  3115. }
  3116. $result = Database::query($sql);
  3117. while ($item = Database::fetch_array($result)) {
  3118. if (!empty($item['start_date'])) {
  3119. $item['start_date'] = api_get_local_time($item['start_date']);
  3120. $item['start_date_tms'] = api_strtotime($item['start_date']);
  3121. }
  3122. if (!empty($item['end_date'])) {
  3123. $item['end_date'] = api_get_local_time($item['end_date']);
  3124. }
  3125. // we break the date field in the database into a date and a time part
  3126. $agenda_db_date = explode(" ", $item['start_date']);
  3127. $date = $agenda_db_date[0];
  3128. $time = $agenda_db_date[1];
  3129. // we divide the date part into a day, a month and a year
  3130. $agendadate = explode("-", $date);
  3131. $year = intval($agendadate[0]);
  3132. $month = intval($agendadate[1]);
  3133. $day = intval($agendadate[2]);
  3134. // we divide the time part into hour, minutes, seconds
  3135. $agendatime = explode(":", $time);
  3136. $hour = $agendatime[0];
  3137. $minute = $agendatime[1];
  3138. $second = $agendatime[2];
  3139. if ($type == 'month_view') {
  3140. $item['calendar_type'] = 'global';
  3141. $agendaitems[$day][] = $item;
  3142. continue;
  3143. }
  3144. $start_time = api_format_date(
  3145. $item['start_date'],
  3146. TIME_NO_SEC_FORMAT
  3147. );
  3148. $end_time = '';
  3149. if (!empty($item['end_date'])) {
  3150. $end_time = ' - '.api_format_date(
  3151. $item['end_date'],
  3152. DATE_TIME_FORMAT_LONG
  3153. );
  3154. }
  3155. // if the student has specified a course we a add a link to that course
  3156. if ($item['course'] <> "") {
  3157. $url = api_get_path(
  3158. WEB_CODE_PATH
  3159. )."admin/agenda.php?cidReq=".urlencode(
  3160. $item['course']
  3161. )."&day=$day&month=$month&year=$year#$day"; // RH //Patrick Cool: to highlight the relevant agenda item
  3162. $course_link = "<a href=\"$url\" title=\"".$item['course']."\">".$item['course']."</a>";
  3163. } else {
  3164. $course_link = "";
  3165. }
  3166. // Creating the array that will be returned. If we have week or month view we have an array with the date as the key
  3167. // if we have a day_view we use a half hour as index => key 33 = 16h30
  3168. if ($type !== "day_view") {
  3169. // This is the array construction for the WEEK or MONTH view
  3170. //Display the Agenda global in the tab agenda (administrator)
  3171. $agendaitems[$day] .= "<i>$start_time $end_time</i>&nbsp;-&nbsp;";
  3172. $agendaitems[$day] .= "<b>".get_lang('GlobalEvent')."</b>";
  3173. $agendaitems[$day] .= "<div>".$item['title']."</div><br>";
  3174. } else {
  3175. // this is the array construction for the DAY view
  3176. $halfhour = 2 * $agendatime['0'];
  3177. if ($agendatime['1'] >= '30') {
  3178. $halfhour = $halfhour + 1;
  3179. }
  3180. if (!is_array($agendaitems[$halfhour])) {
  3181. $content = $agendaitems[$halfhour];
  3182. }
  3183. $agendaitems[$halfhour] = $content."<div><i>$hour:$minute</i> <b>".get_lang(
  3184. 'GlobalEvent'
  3185. ).": </b>".$item['title']."</div>";
  3186. }
  3187. }
  3188. return $agendaitems;
  3189. }
  3190. /**
  3191. * This function retrieves all the personal agenda items and add them to the agenda items found by the other functions.
  3192. */
  3193. public static function get_personal_agenda_items(
  3194. $user_id,
  3195. $agendaitems,
  3196. $day = "",
  3197. $month = "",
  3198. $year = "",
  3199. $week = "",
  3200. $type
  3201. ) {
  3202. $tbl_personal_agenda = Database:: get_main_table(TABLE_PERSONAL_AGENDA);
  3203. $user_id = intval($user_id);
  3204. // 1. creating the SQL statement for getting the personal agenda items in MONTH view
  3205. if ($type == "month_view" or $type == "") {
  3206. // we are in month view
  3207. $sql = "SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$user_id."' and MONTH(date)='".$month."' AND YEAR(date) = '".$year."' ORDER BY date ASC";
  3208. }
  3209. // 2. creating the SQL statement for getting the personal agenda items in WEEK view
  3210. // we are in week view
  3211. if ($type == "week_view") {
  3212. $start_end_day_of_week = self::calculate_start_end_of_week(
  3213. $week,
  3214. $year
  3215. );
  3216. $start_day = $start_end_day_of_week['start']['day'];
  3217. $start_month = $start_end_day_of_week['start']['month'];
  3218. $start_year = $start_end_day_of_week['start']['year'];
  3219. $end_day = $start_end_day_of_week['end']['day'];
  3220. $end_month = $start_end_day_of_week['end']['month'];
  3221. $end_year = $start_end_day_of_week['end']['year'];
  3222. // in sql statements you have to use year-month-day for date calculations
  3223. $start_filter = $start_year."-".$start_month."-".$start_day." 00:00:00";
  3224. $start_filter = api_get_utc_datetime($start_filter);
  3225. $end_filter = $end_year."-".$end_month."-".$end_day." 23:59:59";
  3226. $end_filter = api_get_utc_datetime($end_filter);
  3227. $sql = " SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$user_id."' AND date>='".$start_filter."' AND date<='".$end_filter."'";
  3228. }
  3229. // 3. creating the SQL statement for getting the personal agenda items in DAY view
  3230. if ($type == "day_view") {
  3231. // we are in day view
  3232. // we could use mysql date() function but this is only available from 4.1 and higher
  3233. $start_filter = $year."-".$month."-".$day." 00:00:00";
  3234. $start_filter = api_get_utc_datetime($start_filter);
  3235. $end_filter = $year."-".$month."-".$day." 23:59:59";
  3236. $end_filter = api_get_utc_datetime($end_filter);
  3237. $sql = " SELECT * FROM ".$tbl_personal_agenda." WHERE user='".$user_id."' AND date>='".$start_filter."' AND date<='".$end_filter."'";
  3238. }
  3239. $result = Database::query($sql);
  3240. while ($item = Database::fetch_array($result, 'ASSOC')) {
  3241. $time_minute = api_convert_and_format_date(
  3242. $item['date'],
  3243. TIME_NO_SEC_FORMAT
  3244. );
  3245. $item['date'] = api_get_local_time($item['date']);
  3246. $item['start_date_tms'] = api_strtotime($item['date']);
  3247. $item['content'] = $item['text'];
  3248. // we break the date field in the database into a date and a time part
  3249. $agenda_db_date = explode(" ", $item['date']);
  3250. $date = $agenda_db_date[0];
  3251. $time = $agenda_db_date[1];
  3252. // we divide the date part into a day, a month and a year
  3253. $agendadate = explode("-", $item['date']);
  3254. $year = intval($agendadate[0]);
  3255. $month = intval($agendadate[1]);
  3256. $day = intval($agendadate[2]);
  3257. // we divide the time part into hour, minutes, seconds
  3258. $agendatime = explode(":", $time);
  3259. $hour = $agendatime[0];
  3260. $minute = $agendatime[1];
  3261. $second = $agendatime[2];
  3262. if ($type == 'month_view') {
  3263. $item['calendar_type'] = 'personal';
  3264. $item['start_date'] = $item['date'];
  3265. $agendaitems[$day][] = $item;
  3266. continue;
  3267. }
  3268. // if the student has specified a course we a add a link to that course
  3269. if ($item['course'] <> "") {
  3270. $url = api_get_path(
  3271. WEB_CODE_PATH
  3272. )."calendar/agenda.php?cidReq=".urlencode(
  3273. $item['course']
  3274. )."&day=$day&month=$month&year=$year#$day"; // RH //Patrick Cool: to highlight the relevant agenda item
  3275. $course_link = "<a href=\"$url\" title=\"".$item['course']."\">".$item['course']."</a>";
  3276. } else {
  3277. $course_link = "";
  3278. }
  3279. // Creating the array that will be returned. If we have week or month view we have an array with the date as the key
  3280. // if we have a day_view we use a half hour as index => key 33 = 16h30
  3281. if ($type !== "day_view") {
  3282. // This is the array construction for the WEEK or MONTH view
  3283. //Display events in agenda
  3284. $agendaitems[$day] .= "<div><i>$time_minute</i> $course_link <a href=\"myagenda.php?action=view&view=personal&day=$day&month=$month&year=$year&id=".$item['id']."#".$item['id']."\" class=\"personal_agenda\">".$item['title']."</a></div><br />";
  3285. } else {
  3286. // this is the array construction for the DAY view
  3287. $halfhour = 2 * $agendatime['0'];
  3288. if ($agendatime['1'] >= '30') {
  3289. $halfhour = $halfhour + 1;
  3290. }
  3291. //Display events by list
  3292. $agendaitems[$halfhour] .= "<div><i>$time_minute</i> $course_link <a href=\"myagenda.php?action=view&view=personal&day=$day&month=$month&year=$year&id=".$item['id']."#".$item['id']."\" class=\"personal_agenda\">".$item['title']."</a></div>";
  3293. }
  3294. }
  3295. return $agendaitems;
  3296. }
  3297. /**
  3298. * Show the monthcalender of the given month
  3299. * @param array Agendaitems
  3300. * @param int Month number
  3301. * @param int Year number
  3302. * @param array Array of strings containing long week day names (deprecated, you can send an empty array instead)
  3303. * @param string The month name
  3304. * @return void Direct output
  3305. */
  3306. public static function display_mymonthcalendar(
  3307. $user_id,
  3308. $agendaitems,
  3309. $month,
  3310. $year,
  3311. $weekdaynames = array(),
  3312. $monthName,
  3313. $show_content = true
  3314. ) {
  3315. global $DaysShort, $course_path;
  3316. //Handle leap year
  3317. $numberofdays = array(
  3318. 0,
  3319. 31,
  3320. 28,
  3321. 31,
  3322. 30,
  3323. 31,
  3324. 30,
  3325. 31,
  3326. 31,
  3327. 30,
  3328. 31,
  3329. 30,
  3330. 31
  3331. );
  3332. if (($year % 400 == 0) or ($year % 4 == 0 and $year % 100 <> 0)) {
  3333. $numberofdays[2] = 29;
  3334. }
  3335. //Get the first day of the month
  3336. $dayone = getdate(mktime(0, 0, 0, $month, 1, $year));
  3337. //Start the week on monday
  3338. $startdayofweek = $dayone['wday'] <> 0 ? ($dayone['wday'] - 1) : 6;
  3339. $g_cc = (isset($_GET['courseCode']) ? $_GET['courseCode'] : '');
  3340. $next_month = ($month == 1 ? 12 : $month - 1);
  3341. $prev_month = ($month == 12 ? 1 : $month + 1);
  3342. $next_year = ($month == 1 ? $year - 1 : $year);
  3343. $prev_year = ($month == 12 ? $year + 1 : $year);
  3344. if ($show_content) {
  3345. $back_url = Display::url(
  3346. get_lang('Previous'),
  3347. api_get_self()."?coursePath=".urlencode(
  3348. $course_path
  3349. )."&courseCode=".Security::remove_XSS(
  3350. $g_cc
  3351. )."&action=view&view=month&month=".$next_month."&year=".$next_year
  3352. );
  3353. $next_url = Display::url(
  3354. get_lang('Next'),
  3355. api_get_self()."?coursePath=".urlencode(
  3356. $course_path
  3357. )."&courseCode=".Security::remove_XSS(
  3358. $g_cc
  3359. )."&action=view&view=month&month=".$prev_month."&year=".$prev_year
  3360. );
  3361. } else {
  3362. $back_url = Display::url(
  3363. get_lang('Previous'),
  3364. '',
  3365. array(
  3366. 'onclick' => "load_calendar('".$user_id."','".$next_month."', '".$next_year."'); ",
  3367. 'class' => 'btn ui-button ui-widget ui-state-default'
  3368. )
  3369. );
  3370. $next_url = Display::url(
  3371. get_lang('Next'),
  3372. '',
  3373. array(
  3374. 'onclick' => "load_calendar('".$user_id."','".$prev_month."', '".$prev_year."'); ",
  3375. 'class' => 'pull-right btn ui-button ui-widget ui-state-default'
  3376. )
  3377. );
  3378. }
  3379. $html = '';
  3380. $html .= '<div class="actions">';
  3381. $html .= '<div class="row">';
  3382. $html .= '<div class="col-md-4">'.$back_url.'</div>';
  3383. $html .= '<div class="col-md-4"><p class="agenda-title text-center">'.$monthName." ".$year.'</p></div>';
  3384. $html .= '<div class="col-md-4">'.$next_url.'</div>';
  3385. $html .= '</div>';
  3386. $html .= '</div>';
  3387. $html .= '<table id="agenda_list2" class="table table-bordered">';
  3388. $html .= '<tr>';
  3389. for ($ii = 1; $ii < 8; $ii++) {
  3390. $html .= '<td class="weekdays">'.$DaysShort[$ii % 7].'</td>';
  3391. }
  3392. $html .= '</tr>';
  3393. $curday = -1;
  3394. $today = getdate();
  3395. while ($curday <= $numberofdays[$month]) {
  3396. $html .= "<tr>";
  3397. for ($ii = 0; $ii < 7; $ii++) {
  3398. if (($curday == -1) && ($ii == $startdayofweek)) {
  3399. $curday = 1;
  3400. }
  3401. if (($curday > 0) && ($curday <= $numberofdays[$month])) {
  3402. $bgcolor = $class = 'class="days_week"';
  3403. $dayheader = Display::div(
  3404. $curday,
  3405. array('class' => 'agenda_day')
  3406. );
  3407. if (($curday == $today['mday']) && ($year == $today['year']) && ($month == $today['mon'])) {
  3408. $class = "class=\"days_today\" style=\"width:10%;\"";
  3409. }
  3410. $html .= "<td ".$class.">".$dayheader;
  3411. if (!empty($agendaitems[$curday])) {
  3412. $items = $agendaitems[$curday];
  3413. $items = msort($items, 'start_date_tms');
  3414. foreach ($items as $value) {
  3415. $value['title'] = Security::remove_XSS(
  3416. $value['title']
  3417. );
  3418. $start_time = api_format_date(
  3419. $value['start_date'],
  3420. TIME_NO_SEC_FORMAT
  3421. );
  3422. $end_time = '';
  3423. if (!empty($value['end_date'])) {
  3424. $end_time = '-&nbsp;<i>'.api_format_date(
  3425. $value['end_date'],
  3426. DATE_TIME_FORMAT_LONG
  3427. ).'</i>';
  3428. }
  3429. $complete_time = '<i>'.api_format_date(
  3430. $value['start_date'],
  3431. DATE_TIME_FORMAT_LONG
  3432. ).'</i>&nbsp;'.$end_time;
  3433. $time = '<i>'.$start_time.'</i>';
  3434. switch ($value['calendar_type']) {
  3435. case 'personal':
  3436. $bg_color = '#D0E7F4';
  3437. $icon = Display::return_icon(
  3438. 'user.png',
  3439. get_lang('MyAgenda'),
  3440. array(),
  3441. ICON_SIZE_SMALL
  3442. );
  3443. break;
  3444. case 'global':
  3445. $bg_color = '#FFBC89';
  3446. $icon = Display::return_icon(
  3447. 'view_remove.png',
  3448. get_lang('GlobalEvent'),
  3449. array(),
  3450. ICON_SIZE_SMALL
  3451. );
  3452. break;
  3453. case 'course':
  3454. $bg_color = '#CAFFAA';
  3455. $icon_name = 'course.png';
  3456. if (!empty($value['session_id'])) {
  3457. $icon_name = 'session.png';
  3458. }
  3459. if ($show_content) {
  3460. $icon = Display::url(
  3461. Display::return_icon(
  3462. $icon_name,
  3463. $value['course_name'].' '.get_lang(
  3464. 'Course'
  3465. ),
  3466. array(),
  3467. ICON_SIZE_SMALL
  3468. ),
  3469. $value['url']
  3470. );
  3471. } else {
  3472. $icon = Display::return_icon(
  3473. $icon_name,
  3474. $value['course_name'].' '.get_lang(
  3475. 'Course'
  3476. ),
  3477. array(),
  3478. ICON_SIZE_SMALL
  3479. );
  3480. }
  3481. break;
  3482. default:
  3483. break;
  3484. }
  3485. $result = '<div class="rounded_div_agenda" style="background-color:'.$bg_color.';">';
  3486. if ($show_content) {
  3487. //Setting a personal event to green
  3488. $icon = Display::div(
  3489. $icon,
  3490. array('style' => 'float:right')
  3491. );
  3492. $link = $value['calendar_type'].'_'.$value['id'].'_'.$value['course_id'].'_'.$value['session_id'];
  3493. //Link to bubble
  3494. $url = Display::url(
  3495. cut($value['title'], 40),
  3496. '#',
  3497. array('id' => $link, 'class' => 'opener')
  3498. );
  3499. $result .= $time.' '.$icon.' '.Display::div(
  3500. $url
  3501. );
  3502. //Hidden content
  3503. $content = Display::div(
  3504. $icon.Display::tag(
  3505. 'h2',
  3506. $value['course_name']
  3507. ).'<hr />'.Display::tag(
  3508. 'h3',
  3509. $value['title']
  3510. ).$complete_time.'<hr />'.Security::remove_XSS(
  3511. $value['content']
  3512. )
  3513. );
  3514. //Main div
  3515. $result .= Display::div(
  3516. $content,
  3517. array(
  3518. 'id' => 'main_'.$link,
  3519. 'class' => 'dialog',
  3520. 'style' => 'display:none'
  3521. )
  3522. );
  3523. $result .= '</div>';
  3524. $html .= $result;
  3525. //echo Display::div($content, array('id'=>'main_'.$value['calendar_type'].'_'.$value['id'], 'class' => 'dialog'));
  3526. } else {
  3527. $html .= $result .= $icon.'</div>';
  3528. }
  3529. }
  3530. }
  3531. $html .= "</td>";
  3532. $curday++;
  3533. } else {
  3534. $html .= "<td></td>";
  3535. }
  3536. }
  3537. $html .= "</tr>";
  3538. }
  3539. $html .= "</table>";
  3540. echo $html;
  3541. }
  3542. /**
  3543. * Get personal agenda items between two dates (=all events from all registered courses)
  3544. * @param int user ID of the user
  3545. * @param string Optional start date in datetime format (if no start date is given, uses today)
  3546. * @param string Optional end date in datetime format (if no date is given, uses one year from now)
  3547. * @param integer $user_id
  3548. * @return array Array of events ordered by start date, in
  3549. * [0]('datestart','dateend','title'),[1]('datestart','dateend','title','link','coursetitle') format,
  3550. * where datestart and dateend are in yyyyMMddhhmmss format.
  3551. * @TODO Implement really personal events (from user DB) and global events (from main DB)
  3552. */
  3553. public static function get_personal_agenda_items_between_dates(
  3554. $user_id,
  3555. $date_start = '',
  3556. $date_end = ''
  3557. ) {
  3558. $items = array();
  3559. if ($user_id != strval(intval($user_id))) {
  3560. return $items;
  3561. }
  3562. if (empty($date_start)) {
  3563. $date_start = date('Y-m-d H:i:s');
  3564. }
  3565. if (empty($date_end)) {
  3566. $date_end = date(
  3567. 'Y-m-d H:i:s',
  3568. mktime(0, 0, 0, date("m"), date("d"), date("Y") + 1)
  3569. );
  3570. }
  3571. $expr = '/\d{4}-\d{2}-\d{2}\ \d{2}:\d{2}:\d{2}/';
  3572. if (!preg_match($expr, $date_start)) {
  3573. return $items;
  3574. }
  3575. if (!preg_match($expr, $date_end)) {
  3576. return $items;
  3577. }
  3578. // get agenda-items for every course
  3579. $courses = api_get_user_courses($user_id, false);
  3580. foreach ($courses as $id => $course) {
  3581. $c = api_get_course_info_by_id($course['real_id']);
  3582. //databases of the courses
  3583. $t_a = Database:: get_course_table(TABLE_AGENDA, $course['db']);
  3584. $t_ip = Database:: get_course_table(
  3585. TABLE_ITEM_PROPERTY,
  3586. $course['db']
  3587. );
  3588. // get the groups to which the user belong
  3589. $group_memberships = GroupManager:: get_group_ids(
  3590. $course['db'],
  3591. $user_id
  3592. );
  3593. // if the user is administrator of that course we show all the agenda items
  3594. if ($course['status'] == '1') {
  3595. //echo "course admin";
  3596. $sqlquery = "SELECT ".
  3597. " DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref ".
  3598. " FROM ".$t_a." agenda, ".
  3599. $t_ip." ip ".
  3600. " WHERE agenda.id = ip.ref ".
  3601. " AND agenda.start_date>='$date_start' ".
  3602. " AND agenda.end_date<='$date_end' ".
  3603. " AND ip.tool='".TOOL_CALENDAR_EVENT."' ".
  3604. " AND ip.visibility='1' ".
  3605. " GROUP BY agenda.id ".
  3606. " ORDER BY start_date ";
  3607. } else {
  3608. // if the user is not an administrator of that course, then...
  3609. if (is_array($group_memberships) && count(
  3610. $group_memberships
  3611. ) > 0
  3612. ) {
  3613. $sqlquery = "SELECT ".
  3614. "DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref ".
  3615. " FROM ".$t_a." agenda, ".
  3616. $t_ip." ip ".
  3617. " WHERE agenda.id = ip.ref ".
  3618. " AND agenda.start_date>='$date_start' ".
  3619. " AND agenda.end_date<='$date_end' ".
  3620. " AND ip.tool='".TOOL_CALENDAR_EVENT."' ".
  3621. " AND ( ip.to_user_id='".$user_id."' OR (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(
  3622. ", ",
  3623. $group_memberships
  3624. ).")) ) ".
  3625. " AND ip.visibility='1' ".
  3626. " ORDER BY start_date ";
  3627. } else {
  3628. $sqlquery = "SELECT ".
  3629. "DISTINCT agenda.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.ref ".
  3630. " FROM ".$t_a." agenda, ".
  3631. $t_ip." ip ".
  3632. " WHERE agenda.id = ip.ref ".
  3633. " AND agenda.start_date>='$date_start' ".
  3634. " AND agenda.end_date<='$date_end' ".
  3635. " AND ip.tool='".TOOL_CALENDAR_EVENT."' ".
  3636. " AND ( ip.to_user_id='".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL) ".
  3637. " AND ip.visibility='1' ".
  3638. " ORDER BY start_date ";
  3639. }
  3640. }
  3641. $result = Database::query($sqlquery);
  3642. while ($item = Database::fetch_array($result)) {
  3643. $agendaday = date("j", strtotime($item['start_date']));
  3644. $month = date("n", strtotime($item['start_date']));
  3645. $year = date("Y", strtotime($item['start_date']));
  3646. $URL = api_get_path(
  3647. WEB_PATH
  3648. )."main/calendar/agenda.php?cidReq=".urlencode(
  3649. $course["code"]
  3650. )."&day=$agendaday&month=$month&year=$year#$agendaday";
  3651. list($year, $month, $day, $hour, $min, $sec) = split(
  3652. '[-: ]',
  3653. $item['start_date']
  3654. );
  3655. $start_date = $year.$month.$day.$hour.$min;
  3656. list($year, $month, $day, $hour, $min, $sec) = split(
  3657. '[-: ]',
  3658. $item['end_date']
  3659. );
  3660. $end_date = $year.$month.$day.$hour.$min;
  3661. $items[] = array(
  3662. 'datestart' => $start_date,
  3663. 'dateend' => $end_date,
  3664. 'title' => $item['title'],
  3665. 'link' => $URL,
  3666. 'coursetitle' => $c['name'],
  3667. );
  3668. }
  3669. }
  3670. return $items;
  3671. }
  3672. /**
  3673. * This function retrieves one personal agenda item returns it.
  3674. * @param int $id The agenda item ID
  3675. * @return array The results of the database query, or null if not found
  3676. */
  3677. public static function get_personal_agenda_item($id)
  3678. {
  3679. $tbl_personal_agenda = Database:: get_main_table(TABLE_PERSONAL_AGENDA);
  3680. $id = intval($id);
  3681. // make sure events of the personal agenda can only be seen by the user himself
  3682. $user = api_get_user_id();
  3683. $sql = " SELECT * FROM ".$tbl_personal_agenda." WHERE id=".$id." AND user = ".$user;
  3684. $result = Database::query($sql);
  3685. if (Database::num_rows($result) == 1) {
  3686. $item = Database::fetch_array($result);
  3687. } else {
  3688. $item = null;
  3689. }
  3690. return $item;
  3691. }
  3692. /**
  3693. * This function calculates the startdate of the week (monday)
  3694. * and the enddate of the week (sunday)
  3695. * and returns it as an array
  3696. */
  3697. public static function calculate_start_end_of_week($week_number, $year)
  3698. {
  3699. // determine the start and end date
  3700. // step 1: we calculate a timestamp for a day in this week
  3701. $random_day_in_week = mktime(
  3702. 0,
  3703. 0,
  3704. 0,
  3705. 1,
  3706. 1,
  3707. $year
  3708. ) + ($week_number) * (7 * 24 * 60 * 60); // we calculate a random day in this week
  3709. // step 2: we which day this is (0=sunday, 1=monday, ...)
  3710. $number_day_in_week = date('w', $random_day_in_week);
  3711. // step 3: we calculate the timestamp of the monday of the week we are in
  3712. $start_timestamp = $random_day_in_week - (($number_day_in_week - 1) * 24 * 60 * 60);
  3713. // step 4: we calculate the timestamp of the sunday of the week we are in
  3714. $end_timestamp = $random_day_in_week + ((7 - $number_day_in_week + 1) * 24 * 60 * 60) - 3600;
  3715. // step 5: calculating the start_day, end_day, start_month, end_month, start_year, end_year
  3716. $start_day = date('j', $start_timestamp);
  3717. $start_month = date('n', $start_timestamp);
  3718. $start_year = date('Y', $start_timestamp);
  3719. $end_day = date('j', $end_timestamp);
  3720. $end_month = date('n', $end_timestamp);
  3721. $end_year = date('Y', $end_timestamp);
  3722. $start_end_array['start']['day'] = $start_day;
  3723. $start_end_array['start']['month'] = $start_month;
  3724. $start_end_array['start']['year'] = $start_year;
  3725. $start_end_array['end']['day'] = $end_day;
  3726. $start_end_array['end']['month'] = $end_month;
  3727. $start_end_array['end']['year'] = $end_year;
  3728. return $start_end_array;
  3729. }
  3730. /**
  3731. * @return bool
  3732. */
  3733. public function getIsAllowedToEdit()
  3734. {
  3735. return $this->isAllowedToEdit;
  3736. }
  3737. /**
  3738. * @param bool $isAllowedToEdit
  3739. */
  3740. public function setIsAllowedToEdit($isAllowedToEdit)
  3741. {
  3742. $this->isAllowedToEdit = $isAllowedToEdit;
  3743. }
  3744. /**
  3745. * @param int $userId
  3746. * @param array $event
  3747. *
  3748. * @return bool
  3749. */
  3750. public function sendEmail($userId, $event)
  3751. {
  3752. $userInfo = api_get_user_info($userId);
  3753. if (!empty($this->sessionInfo)) {
  3754. $courseTitle = $this->course['name'].' ('.$this->sessionInfo['title'].')';
  3755. } else {
  3756. $courseTitle = $this->course['name'];
  3757. }
  3758. api_mail_html(
  3759. $userInfo['complete_name'],
  3760. $userInfo['mail'],
  3761. $subject,
  3762. $emailBody
  3763. );
  3764. return true;
  3765. }
  3766. }