123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686 |
- <?php
- class LpCalendarPlugin extends Plugin
- {
- const EVENT_TYPE_TAKEN = 1;
- const EVENT_TYPE_EXAM = 2;
-
- protected function __construct()
- {
- $this->hasPersonalEvents = true;
- $version = '0.1';
- $author = 'Julio Montoya';
- parent::__construct($version, $author, ['enabled' => 'boolean']);
- }
-
- public static function getEventTypeList()
- {
- return [
-
- self::EVENT_TYPE_TAKEN => 'red',
- self::EVENT_TYPE_EXAM => 'yellow',
- ];
- }
-
- public static function create()
- {
- static $result = null;
- return $result ?: $result = new self();
- }
-
- public function get_name()
- {
- return 'lp_calendar';
- }
-
- public function install()
- {
- $sql = "
- CREATE TABLE IF NOT EXISTS learning_calendar(
- id int not null AUTO_INCREMENT primary key,
- title varchar(255) not null default '',
- description longtext default null,
- total_hours int not null default 0,
- minutes_per_day int not null default 0,
- disabled int default 0
- )
- ";
- Database::query($sql);
- $sql = "
- CREATE TABLE IF NOT EXISTS learning_calendar_events(
- id int not null AUTO_INCREMENT primary key,
- name varchar(255) default '',
- calendar_id int not null,
- start_date date not null,
- end_date date not null,
- type int not null
- )
- ";
- Database::query($sql);
- $sql = "
- CREATE TABLE IF NOT EXISTS learning_calendar_user(
- id int not null AUTO_INCREMENT primary key,
- user_id int(11) not null,
- calendar_id int not null
- )
- ";
- Database::query($sql);
- $extraField = new ExtraField('lp_item');
- $params = [
- 'variable' => 'calendar',
- 'visible_to_self' => 1,
- 'changeable' => 1,
- 'visible_to_others' => 1,
- 'field_type' => ExtraField::FIELD_TYPE_CHECKBOX,
- ];
- $extraField->save($params);
- $extraField = new ExtraField('course');
- $params = [
- 'variable' => 'course_hours_duration',
- 'visible_to_self' => 1,
- 'changeable' => 1,
- 'visible_to_others' => 1,
- 'field_type' => ExtraField::FIELD_TYPE_TEXT,
- ];
- $extraField->save($params);
- return true;
- }
-
- public function uninstall()
- {
- $tables = [
- 'learning_calendar',
- 'learning_calendar_events',
- 'learning_calendar_user',
- ];
- foreach ($tables as $table) {
- $sql = "DROP TABLE IF EXISTS $table";
- Database::query($sql);
- }
- $extraField = new ExtraField('lp_item');
- $fieldInfo = $extraField->get_handler_field_info_by_field_variable('calendar');
- if ($fieldInfo) {
- $extraField->delete($fieldInfo['id']);
- }
- $extraField = new ExtraField('course');
- $fieldInfo = $extraField->get_handler_field_info_by_field_variable('course_hours_duration');
- if ($fieldInfo) {
- $extraField->delete($fieldInfo['id']);
- }
- return true;
- }
-
- public static function getCalendars(
- $from,
- $numberOfItems,
- $column,
- $direction = 'DESC'
- ) {
- $column = (int) $column;
- $from = (int) $from;
- $numberOfItems = (int) $numberOfItems;
- $direction = strtoupper($direction);
- if (!in_array($direction, ['ASC', 'DESC'])) {
- $direction = 'DESC';
- }
- $sql = 'select * FROM learning_calendar';
- $sql .= " LIMIT $from, $numberOfItems ";
- $result = Database::query($sql);
- $list = [];
- $link = api_get_path(WEB_PLUGIN_PATH).'lp_calendar/start.php';
- while ($row = Database::fetch_array($result)) {
- $id = $row['id'];
- $row['title'] = Display::url(
- $row['title'],
- api_get_path(WEB_PLUGIN_PATH).'lp_calendar/calendar.php?id='.$id
- );
- $actions = Display::url(
- Display::return_icon('edit.png', get_lang('Edit')),
- $link.'?action=edit&id='.$id
- );
- $actions .= Display::url(
- Display::return_icon('copy.png', get_lang('Copy')),
- $link.'?action=copy&id='.$id
- );
- $actions .= Display::url(
- Display::return_icon('delete.png', get_lang('Delete')),
- $link.'?action=delete&id='.$id
- );
- $row['actions'] = $actions;
- $list[] = $row;
- }
- return $list;
- }
-
- public static function getCalendarsEventsByDate($calendarInfo, $start, $end, $type = 0, $getCount = false)
- {
- if (empty($calendarInfo)) {
- return [];
- }
- $calendarId = (int) $calendarInfo['id'];
- $start = (int) $start;
- $end = (int) $end;
- $startCondition = '';
- $endCondition = '';
- $typeCondition = '';
- if ($start !== 0) {
- $start = api_get_utc_datetime($start);
- $startCondition = "AND start_date >= '".$start."'";
- }
- if ($end !== 0) {
- $end = api_get_utc_datetime($end);
- $endCondition = "AND (end_date <= '".$end."' OR end_date IS NULL)";
- }
- if (!empty($type)) {
- $type = (int) $type;
- $typeCondition = " AND type = $type ";
- }
- $select = '*';
- if ($getCount) {
- $select = 'count(id) count ';
- }
- $sql = "SELECT $select FROM learning_calendar_events
- WHERE calendar_id = $calendarId $startCondition $endCondition ";
- $result = Database::query($sql);
- if ($getCount) {
- $row = Database::fetch_array($result, 'ASSOC');
- return $row['count'];
- }
- $list = [];
-
- while ($row = Database::fetch_array($result, 'ASSOC')) {
- $list[] = $row;
- }
- return ['calendar' => $calendarInfo, 'events' => $list];
- }
-
- public static function getFirstCalendarDate($calendarInfo)
- {
- if (empty($calendarInfo)) {
- return [];
- }
- $calendarId = (int) $calendarInfo['id'];
-
- $sql = "SELECT start_date FROM learning_calendar_events
- WHERE calendar_id = $calendarId ORDER BY start_date LIMIT 1";
- $result = Database::query($sql);
- $row = Database::fetch_array($result, 'ASSOC');
- return $row['start_date'];
- }
-
- public static function getCalendarCount()
- {
- $sql = 'select count(*) as count FROM learning_calendar';
- $result = Database::query($sql);
- $result = Database::fetch_array($result);
- return (int) $result['count'];
- }
-
- public function toggleVisibility($id)
- {
- $extraField = new ExtraField('lp_item');
- $fieldInfo = $extraField->get_handler_field_info_by_field_variable('calendar');
- if ($fieldInfo) {
- $itemInfo = $this->getItemVisibility($id);
- if (empty($itemInfo)) {
- $extraField = new ExtraFieldValue('lp_item');
- $value = 1;
- $params = [
- 'field_id' => $fieldInfo['id'],
- 'value' => $value,
- 'item_id' => $id,
- ];
- $extraField->save($params);
- } else {
- $newValue = (int) $itemInfo['value'] === 1 ? 0 : 1;
- $extraField = new ExtraFieldValue('lp_item');
- $params = [
- 'id' => $itemInfo['id'],
- 'value' => $newValue,
- ];
- $extraField->update($params);
- }
- }
- }
-
- public function getItemVisibility($id)
- {
- $extraField = new ExtraFieldValue('lp_item');
- $values = $extraField->get_values_by_handler_and_field_variable($id, 'calendar');
- return $values;
- }
-
- public static function getCalendar($calendarId)
- {
- $calendarId = (int) $calendarId;
- $sql = "SELECT * FROM learning_calendar WHERE id = $calendarId";
- $result = Database::query($sql);
- $item = Database::fetch_array($result, 'ASSOC');
- return $item;
- }
-
- public static function getUserCalendar($userId)
- {
- $userId = (int) $userId;
- $sql = "SELECT * FROM learning_calendar_user WHERE user_id = $userId";
- $result = Database::query($sql);
- $item = Database::fetch_array($result, 'ASSOC');
- return $item;
- }
-
- public static function getUserEvents($userId, $start, $end, $type = 0, $getCount = false)
- {
- $calendarRelUser = self::getUserCalendar($userId);
- if (!empty($calendarRelUser)) {
- $calendar = self::getCalendar($calendarRelUser['calendar_id']);
- return self::getCalendarsEventsByDate($calendar, $start, $end, $type, $getCount);
- }
- if ($getCount) {
- return 0;
- }
- return [];
- }
-
- public static function getUserCalendarToString($userId)
- {
- $calendar = self::getUserCalendar($userId);
- if ($calendar) {
- $calendarInfo = self::getCalendar($calendar['calendar_id']);
- return $calendarInfo['title'];
- }
- return '';
- }
-
- public static function addUserToCalendar($calendarId, $userId)
- {
- $calendar = self::getUserCalendar($userId);
- if (empty($calendar)) {
- $params = [
- 'calendar_id' => $calendarId,
- 'user_id' => $userId,
- ];
- Database::insert('learning_calendar_user', $params);
- }
- return true;
- }
-
- public static function updateUserToCalendar($calendarId, $userId)
- {
- $calendar = self::getUserCalendar($userId);
- if (!empty($calendar)) {
- $params = [
- 'calendar_id' => $calendarId,
- 'user_id' => $userId,
- ];
- Database::update('learning_calendar_user', $params, ['id = ?' => $calendar['id']]);
- }
- return true;
- }
-
- public static function deleteAllCalendarFromUser($calendarId, $userId)
- {
- $calendarId = (int) $calendarId;
- $userId = (int) $userId;
- $sql = "DELETE FROM learning_calendar_user
- WHERE user_id = $userId AND calendar_id = $calendarId";
- Database::query($sql);
- return true;
- }
-
-
- public function getForm(FormValidator &$form)
- {
- $form->addText('title', get_lang('Title'));
- $form->addText('total_hours', get_lang('TotalHours'));
- $form->addText('minutes_per_day', get_lang('MinutesPerDay'));
- $form->addHtmlEditor('description', get_lang('Description'), false);
- }
-
- public function getPersonalEvents($calendar, $start, $end)
- {
- $userId = api_get_user_id();
- $events = self::getUserEvents($userId, $start, $end);
- if (empty($events)) {
- return [];
- }
- $calendarInfo = $events['calendar'];
- $events = $events['events'];
- $list = [];
- $typeList = self::getEventTypeList();
- foreach ($events as $row) {
- $event['id'] = 'personal_'.$row['id'];
- $event['title'] = $calendarInfo['title'];
- $event['className'] = 'personal';
- $color = isset($typeList[$row['type']]) ? $typeList[$row['type']] : 'green';
- $event['borderColor'] = $color;
- $event['backgroundColor'] = $color;
- $event['editable'] = false;
- $event['sent_to'] = get_lang('Me');
- $event['type'] = 'personal';
- if (!empty($row['start_date'])) {
- $event['start'] = $calendar->formatEventDate($row['start_date']);
- $event['start_date_localtime'] = api_get_local_time($row['start_date']);
- }
- if (!empty($row['end_date'])) {
- $event['end'] = $calendar->formatEventDate($row['end_date']);
- $event['end_date_localtime'] = api_get_local_time($row['end_date']);
- }
- $event['description'] = 'plugin';
- $event['allDay'] = 1;
- $event['parent_event_id'] = 0;
- $event['has_children'] = 0;
- $list[] = $event;
- }
- return $list;
- }
-
- public static function getItemCountChecked($userId, $coursesAndSessions)
- {
- $userId = (int) $userId;
- if (empty($coursesAndSessions)) {
- return 0;
- }
- $tableItem = Database::get_course_table(TABLE_LP_ITEM);
- $tableLp = Database::get_course_table(TABLE_LP_MAIN);
- $tableLpItemView = Database::get_course_table(TABLE_LP_ITEM_VIEW);
- $tableLpView = Database::get_course_table(TABLE_LP_VIEW);
- $extraField = new ExtraField('lp_item');
- $fieldInfo = $extraField->get_handler_field_info_by_field_variable('calendar');
- if (empty($fieldInfo)) {
- return 0;
- }
- $courseAndSessionCondition = [];
- foreach ($coursesAndSessions as $sessionId => $courseList) {
- if (isset($courseList['course_list'])) {
- $courseList = array_keys($courseList['course_list']);
- }
- if (empty($courseList)) {
- continue;
- }
- $courseListToString = implode("','", $courseList);
- if (empty($sessionId)) {
- $courseAndSessionCondition[] =
- " ((l.session_id = 0 OR l.session_id is NULL) AND i.c_id IN ('$courseListToString'))";
- } else {
- $courseAndSessionCondition[] = "
- (
- ((l.session_id = 0 OR l.session_id is NULL) OR l.session_id = $sessionId) AND
- i.c_id IN ('$courseListToString')
- )";
- }
- }
- if (empty($courseAndSessionCondition)) {
- return 0;
- }
- $courseSessionConditionToString = 'AND ('.implode(' OR ', $courseAndSessionCondition).') ';
- $sql = "SELECT count(*) as count
- FROM $tableItem i INNER JOIN $tableLp l
- ON (i.c_id = l.c_id AND i.lp_id = l.iid)
- INNER JOIN $tableLpItemView iv
- ON (iv.c_id = l.c_id AND i.iid = iv.lp_item_id)
- INNER JOIN $tableLpView v
- ON (v.c_id = l.c_id AND v.lp_id = l.iid AND iv.lp_view_id = v.iid)
- INNER JOIN extra_field_values e
- ON (e.item_id = i.iid AND value = 1 AND field_id = ".$fieldInfo['id'].")
- WHERE v.user_id = $userId AND status = 'completed' $courseSessionConditionToString";
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- $row = Database::fetch_array($result, 'ASSOC');
- return $row['count'];
- }
- return 0;
- }
-
- public function getUserStatsPanel($userId, $courseAndSessionList)
- {
-
-
- $stats = self::getUserStats($userId, $courseAndSessionList);
- $html = $this->get_lang('NumberDaysAccumulatedInCalendar').$stats['user_event_count'];
- if (!empty($courseAndSessionList)) {
- $html .= '<br />';
- $html .= $this->get_lang('NumberDaysAccumulatedInLp').$stats['completed'];
- $html .= '<br />';
- $html .= $this->get_lang('NumberDaysInRetard').' '.$stats['diff'];
- }
- $html = Display::panel($html, $this->get_lang('LearningCalendar'));
- return $html;
- }
-
- public static function getUserStats($userId, $courseAndSessionList)
- {
-
- $takenCount = self::getUserEvents(
- $userId,
- strtotime(date('Y-01-01')),
- time(),
- self::EVENT_TYPE_TAKEN,
- true
- );
- $completed = 0;
- $diff = 0;
- if (!empty($courseAndSessionList)) {
- $completed = self::getItemCountChecked($userId, $courseAndSessionList);
- if ($takenCount > $completed) {
- $diff = $takenCount - $completed;
- }
- }
- return [
- 'user_event_count' => $takenCount,
- 'completed' => $completed,
- 'diff' => $diff,
- ];
- }
- }
|