123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161 |
- <?php
- /* For license terms, see /license.txt */
- /**
- * Class LearningCalendarPlugin.
- */
- class LearningCalendarPlugin extends Plugin
- {
- const EVENT_TYPE_TAKEN = 1;
- const EVENT_TYPE_EXAM = 2;
- const EVENT_TYPE_FREE = 3;
- /**
- * Class constructor.
- */
- protected function __construct()
- {
- $version = '0.1';
- $author = 'Julio Montoya';
- parent::__construct($version, $author, ['enabled' => 'boolean']);
- $this->setHasPersonalEvents(true);
- }
- /**
- * Event definition.
- *
- * @return array
- */
- public function getEventTypeList()
- {
- return [
- self::EVENT_TYPE_TAKEN => ['color' => 'red', 'name' => self::get_lang('EventTypeTaken')],
- self::EVENT_TYPE_EXAM => ['color' => 'yellow', 'name' => self::get_lang('EventTypeExam')],
- self::EVENT_TYPE_FREE => ['color' => 'green', 'name' => self::get_lang('EventTypeFree')],
- ];
- }
- /**
- * @return array
- */
- public function getEventTypeColorList()
- {
- $list = $this->getEventTypeList();
- $newList = [];
- foreach ($list as $eventId => $event) {
- $newList[$eventId] = $event['color'];
- }
- return $newList;
- }
- /**
- * Get the class instance.
- *
- * @return $this
- */
- public static function create()
- {
- static $result = null;
- return $result ?: $result = new self();
- }
- /**
- * Get the plugin directory name.
- */
- public function get_name()
- {
- return 'learning_calendar';
- }
- /**
- * Install the plugin. Setup the database.
- */
- 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,
- author_id int(11) not null
- )
- ";
- 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);
- $sql = "
- CREATE TABLE IF NOT EXISTS learning_calendar_control_point(
- id int not null AUTO_INCREMENT primary key,
- user_id int(11) not null,
- control_date date not null,
- control_value int not null,
- created_at datetime not null,
- updated_at datetime not null
- )
- ";
- Database::query($sql);
- $extraField = new ExtraField('lp_item');
- $params = [
- 'display_text' => $this->get_lang('LearningCalendarOneDayMarker'),
- '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 = [
- 'display_text' => $this->get_lang('CourseHoursDuration'),
- '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;
- }
- /**
- * Uninstall the plugin.
- */
- 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;
- }
- /**
- * @param int $from
- * @param int $numberOfItems
- * @param int $column
- * @param string $direction
- *
- * @return array
- */
- public 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';
- }
- if (api_is_platform_admin()) {
- $sql = 'SELECT * FROM learning_calendar';
- } else {
- $userId = api_get_user_id();
- $sql = "SELECT * FROM learning_calendar WHERE author_id = $userId";
- }
- $sql .= " LIMIT $from, $numberOfItems ";
- $result = Database::query($sql);
- $list = [];
- $link = api_get_path(WEB_PLUGIN_PATH).'learning_calendar/start.php';
- while ($row = Database::fetch_array($result)) {
- $id = $row['id'];
- $row['title'] = Display::url(
- $row['title'],
- api_get_path(WEB_PLUGIN_PATH).'learning_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;
- }
- /**
- * @param array $calendarInfo
- * @param int $start
- * @param int $end
- * @param int $type
- * @param bool $getCount
- *
- * @return array
- */
- public function getCalendarsEventsByDate($calendarInfo, $start, $end, $type = 0, $getCount = false)
- {
- if (empty($calendarInfo)) {
- if ($getCount) {
- return 0;
- }
- 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 $typeCondition";
- $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];
- }
- /**
- * @param array $calendarInfo
- *
- * @return array
- */
- public function getFirstCalendarDate($calendarInfo)
- {
- if (empty($calendarInfo)) {
- return [];
- }
- $calendarId = (int) $calendarInfo['id'];
- /*if (!empty($type)) {
- $type = (int) $type;
- $typeCondition = " AND type = $type ";
- }*/
- $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'];
- }
- /**
- * @return int
- */
- public function getCalendarCount()
- {
- if (api_is_platform_admin()) {
- $sql = 'select count(*) as count FROM learning_calendar';
- } else {
- $userId = api_get_user_id();
- $sql = "select count(*) as count FROM learning_calendar WHERE author_id = $userId";
- }
- $result = Database::query($sql);
- $result = Database::fetch_array($result);
- return (int) $result['count'];
- }
- /**
- * @param int $calendarId
- *
- * @return array
- */
- public function getUsersPerCalendar($calendarId)
- {
- $calendarId = (int) $calendarId;
- $sql = "SELECT * FROM learning_calendar_user
- WHERE calendar_id = $calendarId";
- $result = Database::query($sql);
- $list = [];
- while ($row = Database::fetch_array($result, 'ASSOC')) {
- $userInfo = api_get_user_info($row['user_id']);
- $userInfo['exam'] = 'exam';
- $list[] = $userInfo;
- }
- return $list;
- }
- /**
- * @param int $calendarId
- *
- * @return int
- */
- public function getUsersPerCalendarCount($calendarId)
- {
- $calendarId = (int) $calendarId;
- $sql = "SELECT count(id) as count FROM learning_calendar_user
- WHERE calendar_id = $calendarId";
- $result = Database::query($sql);
- $row = Database::fetch_array($result, 'ASSOC');
- return (int) $row['count'];
- }
- /**
- * @param int $id
- */
- 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);
- }
- }
- }
- /**
- * @param int $id
- *
- * @return array
- */
- public function getItemVisibility($id)
- {
- $extraField = new ExtraFieldValue('lp_item');
- $values = $extraField->get_values_by_handler_and_field_variable($id, 'calendar');
- if (empty($values)) {
- return [];
- }
- return $values;
- }
- /**
- * @param int $calendarId
- *
- * @return array|mixed
- */
- public 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;
- }
- /**
- * @param int $userId
- *
- * @return array|mixed
- */
- public 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;
- }
- /**
- * @param int $userId
- * @param int $start
- * @param int $end
- * @param int $type
- * @param bool $getCount
- *
- * @return array|int
- */
- public function getUserEvents($userId, $start, $end, $type = 0, $getCount = false)
- {
- $calendarRelUser = $this->getUserCalendar($userId);
- if (!empty($calendarRelUser)) {
- $calendar = $this->getCalendar($calendarRelUser['calendar_id']);
- return $this->getCalendarsEventsByDate($calendar, $start, $end, $type, $getCount);
- }
- if ($getCount) {
- return 0;
- }
- return [];
- }
- /**
- * @param int $userId
- *
- * @return mixed|string
- */
- public function getUserCalendarToString($userId)
- {
- $calendar = $this->getUserCalendar($userId);
- if ($calendar) {
- $calendarInfo = $this->getCalendar($calendar['calendar_id']);
- return $calendarInfo['title'];
- }
- return '';
- }
- /**
- * @param int $calendarId
- * @param int $userId
- *
- * @return bool
- */
- public function addUserToCalendar($calendarId, $userId)
- {
- $calendar = $this->getUserCalendar($userId);
- if (empty($calendar)) {
- $params = [
- 'calendar_id' => $calendarId,
- 'user_id' => $userId,
- ];
- Database::insert('learning_calendar_user', $params);
- return true;
- }
- return false;
- }
- /**
- * @param int $calendarId
- * @param int $userId
- *
- * @return bool
- */
- public function updateUserToCalendar($calendarId, $userId)
- {
- $calendar = $this->getUserCalendar($userId);
- if (!empty($calendar)) {
- $params = [
- 'calendar_id' => $calendarId,
- 'user_id' => $userId,
- ];
- Database::update('learning_calendar_user', $params, ['id = ?' => $calendar['id']]);
- }
- return true;
- }
- /**
- * @param int $calendarId
- * @param int $userId
- *
- * @return bool
- */
- public 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 static function getUserCalendar($calendarId, $userId)
- {
- $params = [
- 'calendar_id' => $calendarId,
- 'user_id' => $calendarId,
- ];
- Database::insert('learning_calendar_user', $params);
- return true;
- }*/
- /**
- * @param FormValidator $form
- */
- 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);
- }
- /**
- * @param Agenda $agenda
- * @param int $start
- * @param int $end
- *
- * @return array
- */
- public function getPersonalEvents($agenda, $start, $end)
- {
- $userId = api_get_user_id();
- $events = $this->getUserEvents($userId, $start, $end);
- if (empty($events)) {
- return [];
- }
- $calendarInfo = $events['calendar'];
- $events = $events['events'];
- $list = [];
- $typeList = $this->getEventTypeColorList();
- foreach ($events as $row) {
- $event = [];
- $event['id'] = 'personal_'.$row['id'];
- $event['title'] = $calendarInfo['title'];
- $event['className'] = 'personal';
- $color = isset($typeList[$row['type']]) ? $typeList[$row['type']] : $typeList[self::EVENT_TYPE_FREE];
- $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'] = $agenda->formatEventDate($row['start_date']);
- $event['start_date_localtime'] = api_get_local_time($row['start_date']);
- }
- if (!empty($row['end_date'])) {
- $event['end'] = $agenda->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;
- }
- /**
- * @param int $userId
- * @param array $coursesAndSessions
- *
- * @return string
- */
- public function getGradebookEvaluationListToString($userId, $coursesAndSessions)
- {
- $list = $this->getGradebookEvaluationList($userId, $coursesAndSessions);
- $html = '';
- if (!empty($list)) {
- $html = implode(' ', array_column($list, 'name'));
- }
- return $html;
- }
- /**
- * @param int $userId
- * @param array $coursesAndSessions
- *
- * @return array
- */
- public function getGradebookEvaluationList($userId, $coursesAndSessions)
- {
- $userId = (int) $userId;
- if (empty($coursesAndSessions)) {
- return 0;
- }
- $courseSessionConditionToString = '';
- 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[] =
- " c.id IN ('$courseListToString') ";
- } else {
- $courseAndSessionCondition[] = "
- (
- c.id IN ('$courseListToString')
- )";
- }*/
- $courseSessionConditionToString = " AND c.id IN ('".implode("','", $courseList)."') ";
- }
- if (empty($courseSessionConditionToString)) {
- return 0;
- }
- $tableEvaluation = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
- $tableCourse = Database::get_main_table(TABLE_MAIN_COURSE);
- $tableResult = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
- $sql = "SELECT DISTINCT e.name, e.id
- FROM $tableEvaluation e
- INNER JOIN $tableCourse c
- ON (course_code = c.code)
- INNER JOIN $tableResult r
- ON (r.evaluation_id = e.id)
- WHERE
- e.type = 'evaluation' AND
- r.score >= 2 AND
- r.user_id = $userId
- $courseSessionConditionToString
- ";
- $result = Database::query($sql);
- $list = [];
- if (Database::num_rows($result)) {
- while ($row = Database::fetch_array($result, 'ASSOC')) {
- $list[$row['id']] = $row;
- }
- }
- return $list;
- }
- /**
- * @param int $userId
- * @param array $coursesAndSessions
- *
- * @return int
- */
- public 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
- GROUP BY iv.view_count
- ";
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- $row = Database::fetch_array($result, 'ASSOC');
- return $row['count'];
- }
- return 0;
- }
- /**
- * @param array $htmlHeadXtra
- */
- public function setJavaScript(&$htmlHeadXtra)
- {
- $htmlHeadXtra[] = api_get_js('jqplot/jquery.jqplot.js');
- $htmlHeadXtra[] = api_get_js('jqplot/plugins/jqplot.dateAxisRenderer.js');
- $htmlHeadXtra[] = api_get_js('jqplot/plugins/jqplot.canvasOverlay.js');
- $htmlHeadXtra[] = api_get_js('jqplot/plugins/jqplot.pointLabels.js');
- $htmlHeadXtra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/jqplot/jquery.jqplot.css');
- }
- /**
- * @param int $userId
- * @param array $courseAndSessionList
- *
- * @return string
- */
- public function getUserStatsPanel($userId, $courseAndSessionList)
- {
- // @todo use translation
- // get events from this year to today
- $stats = $this->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['completed'] - $stats['user_event_count']);
- }
- $controlList = $this->getControlPointsToPlot($userId);
- if (!empty($controlList)) {
- $listToString = json_encode($controlList);
- $date = $this->get_lang('Date');
- $controlPoint = $this->get_lang('NumberOfDays');
- $html .= '<div id="control_point_chart"></div>';
- $html .= '<script>
- $(document).ready(function(){
- var cosPoints = '.$listToString.';
- var plot1 = $.jqplot(\'control_point_chart\', [cosPoints], {
- //animate: !$.jqplot.use_excanvas,
- series:[{
- showMarker:true,
- pointLabels: { show:true },
- }],
- axes:{
- xaxis:{
- label: "'.$date.'",
- renderer: $.jqplot.DateAxisRenderer,
- tickOptions:{formatString: "%Y-%m-%d"},
- tickInterval: \'30 day\',
- },
- yaxis:{
- label: "'.$controlPoint.'",
- max: 20,
- min: -20,
- }
- },
- canvasOverlay: {
- show: true,
- objects: [{
- horizontalLine: {
- name: \'0 mark\',
- y: 0,
- lineWidth: 2,
- color: \'rgb(f, f, f)\',
- shadow: false
- }
- }]
- },
- });
- });
- </script>';
- }
- $html = Display::panel($html, $this->get_lang('LearningCalendar'));
- return $html;
- }
- /**
- * @param int $userId
- * @param array $courseAndSessionList
- *
- * @return array
- */
- public function getUserStats($userId, $courseAndSessionList)
- {
- // Get events from this year to today
- $takenCount = $this->getUserEvents(
- $userId,
- strtotime(date('Y-01-01')),
- time(),
- self::EVENT_TYPE_TAKEN,
- true
- );
- $completed = 0;
- $diff = 0;
- if (!empty($courseAndSessionList)) {
- $completed = $this->getItemCountChecked($userId, $courseAndSessionList);
- $diff = $takenCount - $completed;
- }
- return [
- 'user_event_count' => $takenCount,
- 'completed' => $completed,
- 'diff' => $diff,
- ];
- }
- /**
- * @param int $calendarId
- *
- * @return bool
- */
- public function copyCalendar($calendarId)
- {
- $item = $this->getCalendar($calendarId);
- $this->protectCalendar($item);
- $item['author_id'] = api_get_user_id();
- if (empty($item)) {
- return false;
- }
- $calendarId = (int) $calendarId;
- unset($item['id']);
- //$item['title'] = $item['title'];
- $newCalendarId = Database::insert('learning_calendar', $item);
- if (!empty($newCalendarId)) {
- $sql = "SELECT * FROM learning_calendar_events WHERE calendar_id = $calendarId";
- $result = Database::query($sql);
- while ($row = Database::fetch_array($result, 'ASSOC')) {
- unset($row['id']);
- $row['calendar_id'] = $newCalendarId;
- Database::insert('learning_calendar_events', $row);
- }
- return true;
- }
- return false;
- }
- /**
- * @param int $calendarId
- *
- * @return bool
- */
- public function deleteCalendar($calendarId)
- {
- $item = $this->getCalendar($calendarId);
- $this->protectCalendar($item);
- if (empty($item)) {
- return false;
- }
- $calendarId = (int) $calendarId;
- $sql = "DELETE FROM learning_calendar WHERE id = $calendarId";
- Database::query($sql);
- // Delete events
- $sql = "DELETE FROM learning_calendar_events WHERE calendar_id = $calendarId";
- Database::query($sql);
- return true;
- }
- /**
- * @param int $calendarId
- * @param string $startDate
- */
- public function toogleDayType($calendarId, $startDate)
- {
- $startDate = Database::escape_string($startDate);
- $calendarId = (int) $calendarId;
- $eventTypeList = $this->getEventTypeColorList();
- // Remove the free type to loop correctly when toogle days.
- unset($eventTypeList[self::EVENT_TYPE_FREE]);
- $sql = "SELECT * FROM learning_calendar_events
- WHERE start_date = '$startDate' AND calendar_id = $calendarId ";
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- $row = Database::fetch_array($result, 'ASSOC');
- $currentType = $row['type'];
- $currentType++;
- if ($currentType > count($eventTypeList)) {
- Database::delete(
- 'learning_calendar_events',
- [' calendar_id = ? AND start_date = ?' => [$calendarId, $startDate]]
- );
- } else {
- $params = [
- 'type' => $currentType,
- ];
- Database::update(
- 'learning_calendar_events',
- $params,
- [' calendar_id = ? AND start_date = ?' => [$calendarId, $startDate]]
- );
- }
- } else {
- $params = [
- 'name' => '',
- 'calendar_id' => $calendarId,
- 'start_date' => $startDate,
- 'end_date' => $startDate,
- 'type' => self::EVENT_TYPE_TAKEN,
- ];
- Database::insert('learning_calendar_events', $params);
- }
- }
- /**
- * @param int $calendarId
- *
- * @return array
- */
- public function getEvents($calendarId)
- {
- $calendarId = (int) $calendarId;
- $eventTypeList = $this->getEventTypeColorList();
- $sql = "SELECT * FROM learning_calendar_events
- WHERE calendar_id = $calendarId ";
- $result = Database::query($sql);
- $list = [];
- while ($row = Database::fetch_array($result, 'ASSOC')) {
- $list[] = [
- 'start_date' => $row['start_date'],
- 'end_date' => $row['start_date'],
- 'color' => $eventTypeList[$row['type']],
- ];
- }
- return $list;
- }
- /**
- * @param array $calendarInfo
- */
- public function protectCalendar(array $calendarInfo)
- {
- $allow = api_is_platform_admin() || api_is_teacher();
- if (!$allow) {
- api_not_allowed(true);
- }
- if (!empty($calendarInfo)) {
- if (!api_is_platform_admin() && api_is_teacher()) {
- if ($calendarInfo['author_id'] != api_get_user_id()) {
- api_not_allowed(true);
- }
- }
- }
- }
- /**
- * @param int $userId
- *
- * @return array
- */
- public function getControlPoints($userId)
- {
- $userId = (int) $userId;
- $sql = "SELECT control_date, control_value
- FROM learning_calendar_control_point
- WHERE user_id = $userId
- ORDER BY control_date";
- $result = Database::query($sql);
- $list = Database::store_result($result, 'ASSOC');
- return $list;
- }
- /**
- * @param int $userId
- *
- * @return array
- */
- public function getControlPointsToPlot($userId)
- {
- $list = $this->getControlPoints($userId);
- $points = [];
- foreach ($list as $item) {
- $points[] = [$item['control_date'], $item['control_value']];
- }
- return $points;
- }
- /**
- * @param int $userId
- * @param int $value
- */
- public function addControlPoint($userId, $value)
- {
- $userId = (int) $userId;
- $value = (int) $value;
- $local = api_get_local_time();
- $date = substr($local, 0, 10);
- $sql = "SELECT id
- FROM learning_calendar_control_point
- WHERE user_id = $userId AND control_date = '$date'";
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- $params = [
- 'control_value' => $value,
- 'updated_at' => api_get_utc_datetime(),
- ];
- $data = Database::fetch_array($result);
- $id = $data['id'];
- Database::update('learning_calendar_control_point', $params, ['id = ?' => $id]);
- } else {
- $params = [
- 'user_id' => $userId,
- 'control_date' => $date,
- 'control_value' => $value,
- 'created_at' => api_get_utc_datetime(),
- 'updated_at' => api_get_utc_datetime(),
- ];
- Database::insert('learning_calendar_control_point', $params);
- }
- }
- /**
- * @param FormValidator $form
- */
- public function getAddUserToCalendarForm(FormValidator &$form)
- {
- $calendars = $this->getCalendars(0, 1000, '');
- if (empty($calendars)) {
- echo Display::return_message(get_lang('NoData'), 'warning');
- exit;
- }
- $calendars = array_column($calendars, 'title', 'id');
- $calendars = array_map('strip_tags', $calendars);
- $form->addSelect('calendar_id', get_lang('Calendar'), $calendars, ['disable_js' => true]);
- }
- }
|