translator->addDomain("tester", 'i18n'); $TeKe->translator->useDomain("tester"); $TeKe->template->setTemplateRepository(dirname(__FILE__).'/views/templates'); $TeKe->template->setTemplateRepository(dirname(__FILE__).'/views/macros'); $TeKe->template->setTemplateRepository(dirname(__FILE__).'/views/users'); $TeKe->template->setTemplateRepository(dirname(__FILE__).'/views/items'); $TeKe->template->setTemplateRepository(dirname(__FILE__).'/views/tests'); $TeKe->template->setTemplateRepository(dirname(__FILE__).'/views/assessments'); if (count($page)>0) { $this->loadContext($page); } $this->setNavigation($TeKe); $this->users = new Users(); $this->items = new Items(); $this->categories = new Categories(); $this->tags = new Tags(); $this->tests = new Tests(); $this->assessments = new Assessments(); } function setNavigation($teke) { if ($teke->is_admin()) { $teke->navigation['users'] = array('title'=>_('Users'), 'url'=>"users/view", 'current'=>$teke->is_current_main('users'), 'level'=>0); $teke->navigation['items'] = array('title'=>_('Items'), 'url'=>"items/view", 'current'=>$teke->is_current_main('items'), 'level'=>0); $teke->navigation['tests'] = array('title'=>_('Tests'), 'url'=>"tests/view", 'current'=>$teke->is_current_main('tests'), 'level'=>0); $teke->navigation['assessments'] = array('title'=>_('Assessments'), 'url'=>"assessments/view", 'current'=>$teke->is_current_main('assessments'), 'level'=>0); } } function loadType($type, $id = false) { if (!$type && is_numeric($id) && $id > 0) { $type_q = query_row("SELECT type FROM " . DB_PREFIX . "base_item WHERE id=".$id); $type = $type_q->type; } $type = lcfirst($type); if ($type) { if (is_file(dirname(__FILE__)."/objects/".$type.".php")) { require_once(dirname(__FILE__)."/objects/".$type.".php"); $classname = ucfirst($type); return new $classname($id); } } return false; } function deleteType($type, $id) { if (!$type && is_numeric($id) && $id > 0) { $type_q = query_row("SELECT type FROM " . DB_PREFIX . "base_item WHERE id=".$id); $type = $type_q->type; } if ($type) { if (is_file(dirname(__FILE__)."/objects/".$type.".php")) { require_once(dirname(__FILE__)."/objects/".$type.".php"); $obj = new $type($id); return $obj->delete(); } } return false; } function loadContext($page) { if (count($page)>=2 && is_numeric($page[1])) { if ($page[0] == "user") { //global $TeKe; //$this->context = $TeKe->user->getUserById($page[1]); $this->context = query_row("SELECT * FROM ". DB_PREFIX ."users WHERE id=".$page[1]); } else { $this->context = $this->loadType($page[0], $page[1]); } } else if (count($page)>1) { $this->context = $this->loadType($page[0], false); } } function formatDate($date, $format="d.m.Y") { if (!$date) { return ""; } $time = strtotime($date); return date($format, $time); } function is_test_constructor() { global $TeKe; return $TeKe->has_access(7); } /** * Used in top of actions, for admins and test constructors only. * All others will be forwarded to index. */ function test_constructor_gatekeeper() { if (!$this->is_test_constructor()) { forward("index"); } } function is_proctor() { global $TeKe; return $TeKe->has_access(5); } function addUser($first_name, $last_name, $email, $role, $institution) { global $TeKe; $password = $TeKe->generate_random_string(6); $salt = $this->generate_salt($email); //XXX check if special symbols is ok $hash = $this->hash_password($password, $salt); $approved = 1; if ($institution) { $institution = $this->getInstitution($institution); } else { $institution = "NULL"; } $q = "INSERT INTO " . DB_PREFIX . "users (first_name, last_name, email, username, password, salt, registered, approved, role, institution) values ('".$first_name."', '".$last_name."', '".$email."', '".$email."', '".$hash."', '".$salt."', NOW(), '".$approved."', '".$role."', ".$institution.")"; $uid = query_insert($q); if ($uid) { $user = $TeKe->user->get_user_by_username_or_email($email); $this->send_registration_mail($user, $password); return $uid; } return 0; } function getInstitution($name) { global $TeKe; $obj = $TeKe->plugin->loadType("institution", $name); if ($obj->getId()) { $id = $obj->getId(); } else { $id = $obj->build(array("name"=>$name)); } return $id; } function getInstitutions($order="name", $search_string = "") { $query = "SELECT * FROM ". DB_PREFIX ."institutions"; if ($search_string) { $query .= " WHERE name LIKE '${search_string}%'"; } $query .= " ORDER BY {$order} ASC"; $res = query_rows($query); return $res; } function getInstitutionName($institution_id) { if ($institution_id) { $institution = $this->loadType("institution", $institution_id); return $institution->getName(); } return ""; } private function generate_salt($username) { $salt = sha1('~'.$username.'~'.microtime(TRUE).'~'); $salt = substr($salt, rand(0,30), 10); return $salt; } private function hash_password($password, $salt) { return sha1('~'.$password.'~'.$salt.'~'); } function send_registration_mail($user, $password) { global $TeKe; $subject = SITE_NAME . " " . _("registration"); $message = "Hi %s,\n\n"; $message .= "You have been registered to %s.\n\n"; $message .= "Here are your login information:\n"; $message .= "User Identificator: %s\n"; $message .= "Password: %s\n\n"; $message .= "Best,\n"; $message .= "%s\n\n"; $message .= "--\n"; $message .= "Please do not reply to this message."; $msg = _($message); $msg = sprintf($msg, $user->getFullName(), SITE_NAME, $user->email, $password, SITE_NAME); return $TeKe->send_mail($user, $subject, $msg); } function getSiteLanguages() { return array( "en" => "english", "et" => "eesti", "ru" => "русский", ); } function getRoles() { return array( 5 => _("proctor"), 7 => _("test constructor"), 9 => _("admin"), ); } function getDifficulties() { return array( 1 => _("beginner"), 2 => _("intermediate"), 3 => _("advanced"), ); } function calculatePercent($score, $max_score) { if ($max_score == 0) { return 100; } $percent = ($score / $max_score) * 100; if ($percent > 100) return 100; if ($percent < 0) return 0; return round($percent); } } ?>