LpCalendarPlugin.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. <?php
  2. /* For license terms, see /license.txt */
  3. /**
  4. * Class LpCalendarPlugin.
  5. */
  6. class LpCalendarPlugin extends Plugin
  7. {
  8. const EVENT_TYPE_TAKEN = 1;
  9. const EVENT_TYPE_EXAM = 2;
  10. /**
  11. * Class constructor.
  12. */
  13. protected function __construct()
  14. {
  15. $this->hasPersonalEvents = true;
  16. $version = '0.1';
  17. $author = 'Julio Montoya';
  18. parent::__construct($version, $author, ['enabled' => 'boolean']);
  19. }
  20. /**
  21. * @return array
  22. */
  23. public static function getEventTypeList()
  24. {
  25. return [
  26. //self::EVENT_TYPE_FREE => 'green',
  27. self::EVENT_TYPE_TAKEN => 'red',
  28. self::EVENT_TYPE_EXAM => 'yellow',
  29. ];
  30. }
  31. /**
  32. * Get the class instance.
  33. *
  34. * @return $this
  35. */
  36. public static function create()
  37. {
  38. static $result = null;
  39. return $result ?: $result = new self();
  40. }
  41. /**
  42. * Get the plugin directory name.
  43. */
  44. public function get_name()
  45. {
  46. return 'lp_calendar';
  47. }
  48. /**
  49. * Install the plugin. Setup the database.
  50. */
  51. public function install()
  52. {
  53. $sql = "
  54. CREATE TABLE IF NOT EXISTS learning_calendar(
  55. id int not null AUTO_INCREMENT primary key,
  56. title varchar(255) not null default '',
  57. description longtext default null,
  58. total_hours int not null default 0,
  59. minutes_per_day int not null default 0,
  60. disabled int default 0
  61. )
  62. ";
  63. Database::query($sql);
  64. $sql = "
  65. CREATE TABLE IF NOT EXISTS learning_calendar_events(
  66. id int not null AUTO_INCREMENT primary key,
  67. name varchar(255) default '',
  68. calendar_id int not null,
  69. start_date date not null,
  70. end_date date not null,
  71. type int not null
  72. )
  73. ";
  74. Database::query($sql);
  75. $sql = "
  76. CREATE TABLE IF NOT EXISTS learning_calendar_user(
  77. id int not null AUTO_INCREMENT primary key,
  78. user_id int(11) not null,
  79. calendar_id int not null
  80. )
  81. ";
  82. Database::query($sql);
  83. $extraField = new ExtraField('lp_item');
  84. $params = [
  85. 'variable' => 'calendar',
  86. 'visible_to_self' => 1,
  87. 'changeable' => 1,
  88. 'visible_to_others' => 1,
  89. 'field_type' => ExtraField::FIELD_TYPE_CHECKBOX,
  90. ];
  91. $extraField->save($params);
  92. $extraField = new ExtraField('course');
  93. $params = [
  94. 'variable' => 'course_hours_duration',
  95. 'visible_to_self' => 1,
  96. 'changeable' => 1,
  97. 'visible_to_others' => 1,
  98. 'field_type' => ExtraField::FIELD_TYPE_TEXT,
  99. ];
  100. $extraField->save($params);
  101. return true;
  102. }
  103. /**
  104. * Uninstall the plugin.
  105. */
  106. public function uninstall()
  107. {
  108. $tables = [
  109. 'learning_calendar',
  110. 'learning_calendar_events',
  111. 'learning_calendar_user',
  112. ];
  113. foreach ($tables as $table) {
  114. $sql = "DROP TABLE IF EXISTS $table";
  115. Database::query($sql);
  116. }
  117. $extraField = new ExtraField('lp_item');
  118. $fieldInfo = $extraField->get_handler_field_info_by_field_variable('calendar');
  119. if ($fieldInfo) {
  120. $extraField->delete($fieldInfo['id']);
  121. }
  122. $extraField = new ExtraField('course');
  123. $fieldInfo = $extraField->get_handler_field_info_by_field_variable('course_hours_duration');
  124. if ($fieldInfo) {
  125. $extraField->delete($fieldInfo['id']);
  126. }
  127. return true;
  128. }
  129. /**
  130. * @param int $from
  131. * @param int $numberOfItems
  132. * @param int $column
  133. * @param string $direction
  134. *
  135. * @return array|\Doctrine\DBAL\Driver\Statement
  136. */
  137. public static function getCalendars(
  138. $from,
  139. $numberOfItems,
  140. $column,
  141. $direction = 'DESC'
  142. ) {
  143. $column = (int) $column;
  144. $from = (int) $from;
  145. $numberOfItems = (int) $numberOfItems;
  146. $direction = strtoupper($direction);
  147. if (!in_array($direction, ['ASC', 'DESC'])) {
  148. $direction = 'DESC';
  149. }
  150. $sql = 'select * FROM learning_calendar';
  151. $sql .= " LIMIT $from, $numberOfItems ";
  152. $result = Database::query($sql);
  153. $list = [];
  154. $link = api_get_path(WEB_PLUGIN_PATH).'lp_calendar/start.php';
  155. while ($row = Database::fetch_array($result)) {
  156. $id = $row['id'];
  157. $row['title'] = Display::url(
  158. $row['title'],
  159. api_get_path(WEB_PLUGIN_PATH).'lp_calendar/calendar.php?id='.$id
  160. );
  161. $actions = Display::url(
  162. Display::return_icon('edit.png', get_lang('Edit')),
  163. $link.'?action=edit&id='.$id
  164. );
  165. $actions .= Display::url(
  166. Display::return_icon('copy.png', get_lang('Copy')),
  167. $link.'?action=copy&id='.$id
  168. );
  169. $actions .= Display::url(
  170. Display::return_icon('delete.png', get_lang('Delete')),
  171. $link.'?action=delete&id='.$id
  172. );
  173. $row['actions'] = $actions;
  174. $list[] = $row;
  175. }
  176. return $list;
  177. }
  178. /**
  179. * @param array $calendarInfo
  180. * @param int $start
  181. * @param int $end
  182. * @param int $type
  183. * @param bool $getCount
  184. *
  185. * @return array
  186. */
  187. public static function getCalendarsEventsByDate($calendarInfo, $start, $end, $type = 0, $getCount = false)
  188. {
  189. if (empty($calendarInfo)) {
  190. return [];
  191. }
  192. $calendarId = (int) $calendarInfo['id'];
  193. $start = (int) $start;
  194. $end = (int) $end;
  195. $startCondition = '';
  196. $endCondition = '';
  197. $typeCondition = '';
  198. if ($start !== 0) {
  199. $start = api_get_utc_datetime($start);
  200. $startCondition = "AND start_date >= '".$start."'";
  201. }
  202. if ($end !== 0) {
  203. $end = api_get_utc_datetime($end);
  204. $endCondition = "AND (end_date <= '".$end."' OR end_date IS NULL)";
  205. }
  206. if (!empty($type)) {
  207. $type = (int) $type;
  208. $typeCondition = " AND type = $type ";
  209. }
  210. $select = '*';
  211. if ($getCount) {
  212. $select = 'count(id) count ';
  213. }
  214. $sql = "SELECT $select FROM learning_calendar_events
  215. WHERE calendar_id = $calendarId $startCondition $endCondition ";
  216. $result = Database::query($sql);
  217. if ($getCount) {
  218. $row = Database::fetch_array($result, 'ASSOC');
  219. return $row['count'];
  220. }
  221. $list = [];
  222. //$link = api_get_path(WEB_PLUGIN_PATH).'lp_calendar/start.php';
  223. while ($row = Database::fetch_array($result, 'ASSOC')) {
  224. $list[] = $row;
  225. }
  226. return ['calendar' => $calendarInfo, 'events' => $list];
  227. }
  228. /**
  229. * @param array $calendarInfo
  230. *
  231. * @return array
  232. */
  233. public static function getFirstCalendarDate($calendarInfo)
  234. {
  235. if (empty($calendarInfo)) {
  236. return [];
  237. }
  238. $calendarId = (int) $calendarInfo['id'];
  239. /*if (!empty($type)) {
  240. $type = (int) $type;
  241. $typeCondition = " AND type = $type ";
  242. }*/
  243. $sql = "SELECT start_date FROM learning_calendar_events
  244. WHERE calendar_id = $calendarId ORDER BY start_date LIMIT 1";
  245. $result = Database::query($sql);
  246. $row = Database::fetch_array($result, 'ASSOC');
  247. return $row['start_date'];
  248. }
  249. /**
  250. * @return int
  251. */
  252. public static function getCalendarCount()
  253. {
  254. $sql = 'select count(*) as count FROM learning_calendar';
  255. $result = Database::query($sql);
  256. $result = Database::fetch_array($result);
  257. return (int) $result['count'];
  258. }
  259. /**
  260. * @param int $id
  261. */
  262. public function toggleVisibility($id)
  263. {
  264. $extraField = new ExtraField('lp_item');
  265. $fieldInfo = $extraField->get_handler_field_info_by_field_variable('calendar');
  266. if ($fieldInfo) {
  267. $itemInfo = $this->getItemVisibility($id);
  268. if (empty($itemInfo)) {
  269. $extraField = new ExtraFieldValue('lp_item');
  270. $value = 1;
  271. $params = [
  272. 'field_id' => $fieldInfo['id'],
  273. 'value' => $value,
  274. 'item_id' => $id,
  275. ];
  276. $extraField->save($params);
  277. } else {
  278. $newValue = (int) $itemInfo['value'] === 1 ? 0 : 1;
  279. $extraField = new ExtraFieldValue('lp_item');
  280. $params = [
  281. 'id' => $itemInfo['id'],
  282. 'value' => $newValue,
  283. ];
  284. $extraField->update($params);
  285. }
  286. }
  287. }
  288. /**
  289. * @param int $id
  290. *
  291. * @return array
  292. */
  293. public function getItemVisibility($id)
  294. {
  295. $extraField = new ExtraFieldValue('lp_item');
  296. $values = $extraField->get_values_by_handler_and_field_variable($id, 'calendar');
  297. return $values;
  298. }
  299. /**
  300. * @param int $calendarId
  301. *
  302. * @return array|mixed
  303. */
  304. public static function getCalendar($calendarId)
  305. {
  306. $calendarId = (int) $calendarId;
  307. $sql = "SELECT * FROM learning_calendar WHERE id = $calendarId";
  308. $result = Database::query($sql);
  309. $item = Database::fetch_array($result, 'ASSOC');
  310. return $item;
  311. }
  312. /**
  313. * @param int $userId
  314. *
  315. * @return array|mixed
  316. */
  317. public static function getUserCalendar($userId)
  318. {
  319. $userId = (int) $userId;
  320. $sql = "SELECT * FROM learning_calendar_user WHERE user_id = $userId";
  321. $result = Database::query($sql);
  322. $item = Database::fetch_array($result, 'ASSOC');
  323. return $item;
  324. }
  325. /**
  326. * @param int $userId
  327. * @param int $start
  328. * @param int $end
  329. * @param int $type
  330. * @param bool $getCount
  331. *
  332. * @return array|int
  333. */
  334. public static function getUserEvents($userId, $start, $end, $type = 0, $getCount = false)
  335. {
  336. $calendarRelUser = self::getUserCalendar($userId);
  337. if (!empty($calendarRelUser)) {
  338. $calendar = self::getCalendar($calendarRelUser['calendar_id']);
  339. return self::getCalendarsEventsByDate($calendar, $start, $end, $type, $getCount);
  340. }
  341. if ($getCount) {
  342. return 0;
  343. }
  344. return [];
  345. }
  346. /**
  347. * @param int $userId
  348. *
  349. * @return mixed|string
  350. */
  351. public static function getUserCalendarToString($userId)
  352. {
  353. $calendar = self::getUserCalendar($userId);
  354. if ($calendar) {
  355. $calendarInfo = self::getCalendar($calendar['calendar_id']);
  356. return $calendarInfo['title'];
  357. }
  358. return '';
  359. }
  360. /**
  361. * @param int $calendarId
  362. * @param int $userId
  363. *
  364. * @return bool
  365. */
  366. public static function addUserToCalendar($calendarId, $userId)
  367. {
  368. $calendar = self::getUserCalendar($userId);
  369. if (empty($calendar)) {
  370. $params = [
  371. 'calendar_id' => $calendarId,
  372. 'user_id' => $userId,
  373. ];
  374. Database::insert('learning_calendar_user', $params);
  375. }
  376. return true;
  377. }
  378. /**
  379. * @param int $calendarId
  380. * @param int $userId
  381. *
  382. * @return bool
  383. */
  384. public static function updateUserToCalendar($calendarId, $userId)
  385. {
  386. $calendar = self::getUserCalendar($userId);
  387. if (!empty($calendar)) {
  388. $params = [
  389. 'calendar_id' => $calendarId,
  390. 'user_id' => $userId,
  391. ];
  392. Database::update('learning_calendar_user', $params, ['id = ?' => $calendar['id']]);
  393. }
  394. return true;
  395. }
  396. /**
  397. * @param int $calendarId
  398. * @param int $userId
  399. *
  400. * @return bool
  401. */
  402. public static function deleteAllCalendarFromUser($calendarId, $userId)
  403. {
  404. $calendarId = (int) $calendarId;
  405. $userId = (int) $userId;
  406. $sql = "DELETE FROM learning_calendar_user
  407. WHERE user_id = $userId AND calendar_id = $calendarId";
  408. Database::query($sql);
  409. return true;
  410. }
  411. /*public static function getUserCalendar($calendarId, $userId)
  412. {
  413. $params = [
  414. 'calendar_id' => $calendarId,
  415. 'user_id' => $calendarId,
  416. ];
  417. Database::insert('learning_calendar_user', $params);
  418. return true;
  419. }*/
  420. /**
  421. * @param FormValidator $form
  422. */
  423. public function getForm(FormValidator &$form)
  424. {
  425. $form->addText('title', get_lang('Title'));
  426. $form->addText('total_hours', get_lang('TotalHours'));
  427. $form->addText('minutes_per_day', get_lang('MinutesPerDay'));
  428. $form->addHtmlEditor('description', get_lang('Description'), false);
  429. }
  430. /**
  431. * @param int $start
  432. * @param int $end
  433. *
  434. * @return array
  435. */
  436. public function getPersonalEvents($calendar, $start, $end)
  437. {
  438. $userId = api_get_user_id();
  439. $events = self::getUserEvents($userId, $start, $end);
  440. if (empty($events)) {
  441. return [];
  442. }
  443. $calendarInfo = $events['calendar'];
  444. $events = $events['events'];
  445. $list = [];
  446. $typeList = self::getEventTypeList();
  447. foreach ($events as $row) {
  448. $event['id'] = 'personal_'.$row['id'];
  449. $event['title'] = $calendarInfo['title'];
  450. $event['className'] = 'personal';
  451. $color = isset($typeList[$row['type']]) ? $typeList[$row['type']] : 'green';
  452. $event['borderColor'] = $color;
  453. $event['backgroundColor'] = $color;
  454. $event['editable'] = false;
  455. $event['sent_to'] = get_lang('Me');
  456. $event['type'] = 'personal';
  457. if (!empty($row['start_date'])) {
  458. $event['start'] = $calendar->formatEventDate($row['start_date']);
  459. $event['start_date_localtime'] = api_get_local_time($row['start_date']);
  460. }
  461. if (!empty($row['end_date'])) {
  462. $event['end'] = $calendar->formatEventDate($row['end_date']);
  463. $event['end_date_localtime'] = api_get_local_time($row['end_date']);
  464. }
  465. $event['description'] = 'plugin';
  466. $event['allDay'] = 1;
  467. $event['parent_event_id'] = 0;
  468. $event['has_children'] = 0;
  469. $list[] = $event;
  470. }
  471. return $list;
  472. }
  473. /**
  474. * @param array $coursesAndSessions
  475. *
  476. * @return int
  477. */
  478. public static function getItemCountChecked($userId, $coursesAndSessions)
  479. {
  480. $userId = (int) $userId;
  481. if (empty($coursesAndSessions)) {
  482. return 0;
  483. }
  484. $tableItem = Database::get_course_table(TABLE_LP_ITEM);
  485. $tableLp = Database::get_course_table(TABLE_LP_MAIN);
  486. $tableLpItemView = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  487. $tableLpView = Database::get_course_table(TABLE_LP_VIEW);
  488. $extraField = new ExtraField('lp_item');
  489. $fieldInfo = $extraField->get_handler_field_info_by_field_variable('calendar');
  490. if (empty($fieldInfo)) {
  491. return 0;
  492. }
  493. $courseAndSessionCondition = [];
  494. foreach ($coursesAndSessions as $sessionId => $courseList) {
  495. if (isset($courseList['course_list'])) {
  496. $courseList = array_keys($courseList['course_list']);
  497. }
  498. if (empty($courseList)) {
  499. continue;
  500. }
  501. $courseListToString = implode("','", $courseList);
  502. if (empty($sessionId)) {
  503. $courseAndSessionCondition[] =
  504. " ((l.session_id = 0 OR l.session_id is NULL) AND i.c_id IN ('$courseListToString'))";
  505. } else {
  506. $courseAndSessionCondition[] = "
  507. (
  508. ((l.session_id = 0 OR l.session_id is NULL) OR l.session_id = $sessionId) AND
  509. i.c_id IN ('$courseListToString')
  510. )";
  511. }
  512. }
  513. if (empty($courseAndSessionCondition)) {
  514. return 0;
  515. }
  516. $courseSessionConditionToString = 'AND ('.implode(' OR ', $courseAndSessionCondition).') ';
  517. $sql = "SELECT count(*) as count
  518. FROM $tableItem i INNER JOIN $tableLp l
  519. ON (i.c_id = l.c_id AND i.lp_id = l.iid)
  520. INNER JOIN $tableLpItemView iv
  521. ON (iv.c_id = l.c_id AND i.iid = iv.lp_item_id)
  522. INNER JOIN $tableLpView v
  523. ON (v.c_id = l.c_id AND v.lp_id = l.iid AND iv.lp_view_id = v.iid)
  524. INNER JOIN extra_field_values e
  525. ON (e.item_id = i.iid AND value = 1 AND field_id = ".$fieldInfo['id'].")
  526. WHERE v.user_id = $userId AND status = 'completed' $courseSessionConditionToString";
  527. $result = Database::query($sql);
  528. if (Database::num_rows($result)) {
  529. $row = Database::fetch_array($result, 'ASSOC');
  530. return $row['count'];
  531. }
  532. return 0;
  533. }
  534. /**
  535. * @param int $userId
  536. * @param array $courseAndSessionList
  537. *
  538. * @return string
  539. */
  540. public function getUserStatsPanel($userId, $courseAndSessionList)
  541. {
  542. // @todo use translation
  543. // get events from this year to today
  544. $stats = self::getUserStats($userId, $courseAndSessionList);
  545. $html = $this->get_lang('NumberDaysAccumulatedInCalendar').$stats['user_event_count'];
  546. if (!empty($courseAndSessionList)) {
  547. $html .= '<br />';
  548. $html .= $this->get_lang('NumberDaysAccumulatedInLp').$stats['completed'];
  549. $html .= '<br />';
  550. $html .= $this->get_lang('NumberDaysInRetard').' '.$stats['diff'];
  551. }
  552. $html = Display::panel($html, $this->get_lang('LearningCalendar'));
  553. return $html;
  554. }
  555. /**
  556. * @param int $userId
  557. * @param array $courseAndSessionList
  558. *
  559. * @return array
  560. */
  561. public static function getUserStats($userId, $courseAndSessionList)
  562. {
  563. // Get events from this year to today
  564. $takenCount = self::getUserEvents(
  565. $userId,
  566. strtotime(date('Y-01-01')),
  567. time(),
  568. self::EVENT_TYPE_TAKEN,
  569. true
  570. );
  571. $completed = 0;
  572. $diff = 0;
  573. if (!empty($courseAndSessionList)) {
  574. $completed = self::getItemCountChecked($userId, $courseAndSessionList);
  575. if ($takenCount > $completed) {
  576. $diff = $takenCount - $completed;
  577. }
  578. }
  579. return [
  580. 'user_event_count' => $takenCount,
  581. 'completed' => $completed,
  582. 'diff' => $diff,
  583. ];
  584. }
  585. }