setTranslator(); $this->template = new PHPTAL(); $this->template->setTemplateRepository( array(dirname(dirname(__FILE__)).'/views/templates/', dirname(dirname(__FILE__)).'/views/macros/', dirname(dirname(__FILE__)).'/views/forms/', dirname(dirname(__FILE__)).'/views/pages/', dirname(dirname(__FILE__)).'/views/objects/', )); $this->template->setTranslator($this->getTranslator()); // Forward to wsdl_error page if WSDL error occurs try { // If there is no answer within 10 seconds, bail out $old_timeout = ini_set('default_socket_timeout', 10); $this->backoffice = new BackOfficeService(); if ($old_timeout) { ini_set('default_socket_timeout', $old_timeout); } //This catches IF bos is missing and user logged in Dippler $service_version = $this->backoffice->isAlive(); if ( $service_version > 0 ) { if ( $service_version != $this->getVersion() ) { $this->add_system_message( _("Version mismatch, please update Dippler to version ".$service_version ) ); } } } catch (Exception $e) { //Kill session, if theres any active - logs user out if ( isSet( $_SESSION['backoffice_session'] ) ) { unset($_SESSION['backoffice_session']); } $this->backoffice = NULL; $this->view('wsdl_error'); exit; } $this->db = new DB(); } function getVersion() { if ( defined( 'APP_VERSION' ) ) { return APP_VERSION; } return false; } private function setTranslator() { $language = DEFAULT_LANGUAGE; if (isset($_SESSION['language']) && in_array($_SESSION['language'], array_keys($this->getAvailableLanguages()))) { $language = $_SESSION['language']; } $tr = new PHPTAL_GetTextTranslator(); $tr->setLanguage(constant('LOCALE_'.$language).DEFAULT_ENCODING, constant('LOCALE_'.$language).DEFAULT_ENCODING); $tr->addDomain(DEFAULT_DOMAIN, 'i18n'); $tr->useDomain(DEFAULT_DOMAIN); $this->translator = $tr; } function getTranslator() { return $this->translator; } /** * Returns input values for form cached within $_SESSION variable or an empty array. * * @return array */ private function getSessionInputValues() { $input_values = array(); if (isset($_SESSION['input_values'])) $input_values = $_SESSION['input_values']; return $input_values; } /** * Tries to clear input values cached within $_SESSION variable if present. * Always returns true as the input values are empty in any case. * * @return true */ public function clearSessionInputValues() { if (isset($_SESSION['input_values'])) unset($_SESSION['input_values']); return true; } private function view($view="page_not_found") { if ($this->execute_page()) { $this->template->dippler = $this; $this->template->request = $_GET; $this->template->input_values = $this->getSessionInputValues(); $this->template->setTemplate($view.".html"); echo $this->template->execute(); } } public function view_page($page, $handler=NULL) { $this->page = $page; $template = $page[0]; if (in_array(end($page), array("add", "edit")) && $this->is_logged_in() && $this->is_learner()) { $this->edit = true; } if ($handler) { if ($handler == "course") { $this->handler = new CourseHandler($page); if ((count($page) == 4) && ($page[0] == 'participants') && ($page[2] == 'portfolio') && is_numeric($page[1]) && is_numeric($page[3])) { $template = "course_learner_portfolio"; } else if (count($page) >= 3 && ($page[0] == 'participants') && ($page[2] == 'grades') && is_numeric($page[1])) { $template = "course_grades"; } else if ((count($page) == 4) && ($page[0] == 'groups') && ($page[2] == 'group') && is_numeric($page[1]) && is_numeric($page[3])) { $template = "course_group"; } else if ((count($page) >= 4) && ($page[0] == 'announcements') && ($page[2] == 'post') && is_numeric($page[1]) && is_numeric($page[3])) { $template = "course_announcements_post"; } else if ((count($page) >= 4) && ($page[0] == 'assignments') && ($page[2] == 'assignment') && is_numeric($page[1]) && is_numeric($page[3])) { $template = "course_assignment"; } else if ((count($page) >= 4) && ($page[0] == 'resources') && ($page[2] == 'folder') && is_numeric($page[1]) && is_numeric($page[3])) { $template = "course_folder"; } else if ((count($page) >= 4) && ($page[0] == 'resources') && ($page[2] == 'resource') && is_numeric($page[1]) && is_numeric($page[3])) { $template = "course_resource"; } else if ((count($page) >= 4) && ($page[0] == 'outcomes') && ($page[2] == 'outcome') && is_numeric($page[1]) && is_numeric($page[3])) { $template = "course_outcome"; } elseif ((count($page) >= 4) && ($page[0] == 'assignments') && ($page[2] == 'answer') && is_numeric($page[1]) && is_numeric($page[3])) { $template = "course_answer"; } else { $template = "course_".$page[0]; } } else if ($handler == "courses") { $this->handler = new CoursesHandler($page); $template = "courses_".$page[0]; } else if ($handler == "profile") { $this->handler = new ProfileHandler($page); $template = "profile_".$page[0]; } else if ($handler == "administrate" && is_admin()) { $this->handler = new AdministrateHandler($page); $template = "administrate_".$page[0]; } else { $template = "page_not_found"; } } if (is_file(dirname(dirname(__FILE__)).'/views/pages/'.$template.'.html')) { $this->view($template); } else { $this->view(); } } public function get_navigation_items() { if (is_object($this->handler)) { return $this->handler->navigation; } } public function getCourse() { return $this->get_object("course"); } public function getUser() { return $this->get_object("user"); } public function get_object($object) { if ($object == "course" && isset($this->page[1])) { $course_id = $this->page[1]; $course = new Course($course_id); $course->load(); return $course; } else if ($object == "user" && isset($this->page[1])) { $user_id = $this->page[1]; $user = new User($user_id); $user->load(); return $user; } return false; } public function is_logged_in() { if (isset($_SESSION['backoffice_session'])) { return true; } return false; } public function is_admin($user = NULL) { if (!$user && isset($_SESSION['backoffice_session'])) { $user = get_backoffice_session()->user; } if (($user instanceof User) && $user->getLevel()>=90) { return true; } return false; } public function is_teacher($user = NULL) { if (!$user && isset($_SESSION['backoffice_session'])) { $user = get_backoffice_session()->user; } if (($user instanceof User) && $user->getLevel()>=50) { return true; } return false; } public function is_learner($user = NULL) { if (!$user && isset($_SESSION['backoffice_session'])) { $user = get_backoffice_session()->user; } if (($user instanceof User) && $user->getLevel()>=10) { return true; } return false; } public function get_logged_in_user() { if (isset($_SESSION['backoffice_session'])) { $user = get_backoffice_session()->user; if ($user instanceof User) { return $user; } } return false; } public function get_time_zone() { if (isset($_SESSION['backoffice_session'])) { return get_backoffice_session()->timeZone; } return false; } public function execute_page() { $request_uri = $_SERVER["REQUEST_URI"]; if (substr_count($request_uri, "/actions/")) { return false; } if (substr_count($request_uri, "/images/")) { return false; } if (substr_count($request_uri, "/views/js/")) { return false; } if (substr_count($request_uri, "/cron/")) { if (is_array($this->page) && sizeof($this->page) >= 2) { CronRunner::run($this->page[1]); } return false; } return true; } public function add_system_message($message, $type='success') { $messages = array(); if (!in_array($type, array('info', 'success', 'warning', 'error'))) { $type = "success"; } if (isset($_SESSION['system_messages'])) { $messages = $_SESSION['system_messages']; } //raido - added m5() key, this way we can make sure, messages do not get duplicates //don't know if a good fix,but worked for version mismatch message $messages[md5($message)] = array('type' => $type, 'message' => $message); $_SESSION['system_messages'] = $messages; } public function get_system_messages() { $messages = array(); if (isset($_SESSION['system_messages'])) { $messages = $_SESSION['system_messages']; unset($_SESSION['system_messages']); } return $messages; } public function get_system_messages_html() { $html = ""; $system_messages = $this->get_system_messages(); if ($system_messages && is_array($system_messages) && sizeof($system_messages) > 0) { $i = 1; foreach ($system_messages as $message) { $html .= "
"; $html .= $message['message']; $html .= " "; $html .= _("(Close)"); $html .= ""; $html .= "
"; $i++; } } return $html; } public function getAvailableLanguages() { return array( 'et' => _("Estonian"), 'en' => _("English") ); } public function get_locale() { $language = DEFAULT_LANGUAGE; if (isset($_SESSION['language']) && in_array($_SESSION['language'], array_keys($this->getAvailableLanguages()))) { $language = $_SESSION['language']; } return $language; } public function getAffiliations() { $affiliations = array( 'first' => _('First'), 'second' => _('Second'), 'third' => _('Third'), 'fourth' => _('Fourth'), 'fifth' => _('Fifth') ); return $affiliations; } public function getPermissions() { $permissions = array( 1 => _('All'), 2 => _('Loggedin'), 3 => _('Course members') ); return $permissions; } public function getRegistrationTypes() { $types = array( 1 => _('Self-registration with teacher confirmation'), 2 => _('Teaher registers'), 3 => _('Registration by invitations only') ); return $types; } /* Action token system */ function generate_action_token($timestamp) { $secret = SITE_SECRET; $session_id = session_id(); $bos_session_id = $_SESSION['__sid']; if (($secret) && ($session_id) && ($bos_session_id) && ($timestamp)) { return md5($secret.$session_id.$bos_session_id.$timestamp); } return false; } function dippler_http_build_url(array $parts) { $scheme = isset($parts['scheme']) ? "{$parts['scheme']}://" : ''; $host = isset($parts['host']) ? "{$parts['host']}" : ''; $port = isset($parts['port']) ? ":{$parts['port']}" : ''; $path = isset($parts['path']) ? "{$parts['path']}" : ''; $query = isset($parts['query']) ? "?{$parts['query']}" : ''; return $scheme.$host.$port.$path.$query; } function add_action_tokens_to_url($url) { $components = parse_url($url); if (isset($components['query'])) { if (is_callable('mb_parse_str')) { mb_parse_str($components['query'], $query); } else { parse_str($components['query'], $query); } } else { $query = array(); } if (isset($query['__ts']) && isset($query['__token'])) { return $url; } $query['__ts'] = time(); $query['__token'] = $this->generate_action_token($query['__ts']); $components['query'] = http_build_query($query); return $this->dippler_http_build_url($components); } function shortenText($text, $allowed=50, $strip=true) { if ($strip) { $text = strip_tags($text); } // Use multibyte if possible if (is_callable('mb_strlen') && is_callable('mb_substr') && is_callable('mb_strrpos')) { if (mb_strlen($text)>$allowed) { $text = $text." "; $text = mb_substr($text,0,$allowed); $text = mb_substr($text,0,mb_strrpos($text," ")); return $text."..."; } } else { if (strlen($text)>$allowed) { $text = $text." "; $text = substr($text,0,$allowed); $text = substr($text, 0, strrpos($text," ")); return $text."..."; } } return $text; } function getStringLength($text) { if (is_callable('mb_strlen')) { return mb_strlen($text); } return strlen($text); } function getProviders() { $list = array(); $remote_list = $this->backoffice->listDictionaryProvider(); foreach ($remote_list as $item_row) { foreach ($item_row as $item) { $list[$item["id"][0][0]] = $item["word"][0][0]; } } return $list; } private function usersDataToArrayOfUserObjects($data) { $users = array(); if (isset($data->user)) { foreach ($data->user as $single) { $user = new User(); $user->load((object) $single); $users[$user->id] = $user; } } return $users; } function listUsersFull($offset = 0, $limit = 20) { $data = $this->backoffice->listUsersFull($offset, $limit); $users = $this->usersDataToArrayOfUserObjects($data["objects"]); return array("pages" => $data["pages"], "users" => $users); } function listUsersNotApprovedFull($offset = 0, $limit = 20) { $data = $this->backoffice->listUsersNotApprovedFull($offset, $limit); $users = $this->usersDataToArrayOfUserObjects($data["objects"]); return array("pages" => $data["pages"], "users" => $users); } function listCoursesFull($offset = 0, $limit = 20) { $data = $this->backoffice->listCoursesFull($offset, $limit); $courses = $this->coursesDataToArrayOfCourseObjects($data["objects"]); return array("pages" => $data["pages"], "courses" => $courses); } function listMyCoursesFull($offset = 0, $limit = 20) { $data = $this->backoffice->listMyCoursesFull($offset, $limit); $courses = $this->coursesDataToArrayOfCourseObjects($data["objects"]); return array("pages" => $data["pages"], "courses" => $courses); } function listUnapprovedCoursesFull($offset = 0) { $data = $this->backoffice->listUnapprovedCoursesFull($offset); $courses = $this->coursesDataToArrayOfCourseObjects($data); return $courses; } function listAdminsFull() { $data = $this->backoffice->listAdminsFull(); $users = array(); if (isset($data->user)) { foreach ($data->user as $single) { $user = new User(); $user->load((object) $single); $users[$user->id] = $user; } } return $users; } function coursesDataToArrayOfCourseObjects($data) { $courses = array(); if (isset($data->course)) { foreach ($data->course as $single) { $course = new Course(); $course->load((object) $single); $courses[$course->id] = $course; } } return $courses; } function listCourseCategoriesFull($course_id) { $categories = array(); $data = $this->backoffice->listCategoriesFull($course_id); if (isset($data->category)) { foreach ($data->category as $single) { $cat = new Category(); $cat->load((object) $single); $categories[$cat->id] = $cat; } } return $categories; } function listCategoryTreeFull($course_id) { $categories = array(); $data = $this->backoffice->listCategoriesTreeFull($course_id); if (isset($data->category)) { foreach ($data->category as $single) { $cat = new Category(); $cat->load((object) $single); $categories[$cat->id] = $cat; } } return $categories; } function createSorter($field, $direction="desc") { $sorter = new Sorter(); $sorter->setField($field); $sorter->setDirection($direction); return $sorter; } function getSetting($setting) { $setting = new Setting($setting); $setting->load(); return $setting->getValue(); } function bulkApproveCourses($ids) { if ($this->is_logged_in() && $this->is_admin()) { if ($ids && is_array($ids) && sizeof($ids) > 0) { $data = ""; $data .= ""; $data .= ""; foreach ($ids as $id) { $data .= "{$id}"; } $data .= ""; $data .= ""; return $this->backoffice->bulkApproveCourses($data); } } return false; } function isRegistrationEnabled() { if (defined('DIPPLER_REGISTRATION_ENABLED')) { return DIPPLER_REGISTRATION_ENABLED; } return true; } } ?>