agenda.lib.php 146 KB

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