skala
cvargas 16 years ago
commit 83c0dbefc2
  1. 2
      main/inc/global.inc.php
  2. 2
      main/inc/lib/internationalization.lib.php
  3. 98
      main/inc/lib/main_api.lib.php
  4. 353
      tests/main/gradebook/lib/be/evaluation.class.test.php

@ -136,7 +136,7 @@ while ($row = @Database::fetch_array($result)) {
$charset = $row[0];
}
if (empty($charset)) {
$charset = 'ISO-8859-15';
$charset = 'UTF-8';
}
// Preserving the value of the global variable $charset.
$charset_initial_value = $charset;

@ -129,7 +129,7 @@ function get_lang($variable, $notrans = 'DLTT', $language = null) {
// Caching results from some API functions, for speed.
static $langpath;
if (!isset($langpath)) {
$langpath = api_get_path(SYS_CODE_PATH).'lang/';
$langpath = api_get_path(SYS_LANG_PATH);
}
static $test_server_mode;
if (!isset($test_server_mode)) {

@ -789,6 +789,61 @@ function api_get_user_courses($userid, $fetch_session = true) {
return $courses;
}
/**
* Formats user information into a standard array
*
* @param array Non-standard user array
* @return array Standard user array
*/
function _api_format_user($user) {
$result = array();
if (isset($user['firstname']) AND isset($user['lastname'])) {
$firstname = $user['firstname'];
$lastname = $user['lastname'];
} else if (isset($user['firstName']) AND isset($user['lastName'])) {
$firstname = $user['firstName'];
$lastname = $user['lastName'];
}
$result['firstname'] = $firstname;
$result['lastname'] = $lastname;
//Kept for historical reasons
$result['firstName'] = $firstname;
$result['lastName'] = $lastname;
if(isset($user['email'])) {
$result['mail'] = $user['email'];
} else {
$result['mail'] = $user['mail'];
}
$result['picture_uri'] = $user['picture_uri'];
$result['user_id'] = intval($user['user_id']);
$result['official_code'] = $user['official_code'];
$result['status'] = $user['status'];
$result['auth_source'] = $user['auth_source'];
$result['username'] = $user['username'];
$result['theme'] = $user['theme'];
$result['language'] = $user['language'];
if (!isset($user['lastLogin']) AND !isset($user['last_login'])) {
require_once api_get_path(LIBRARY_PATH).'/tracking.lib.php';
$timestamp = Tracking::get_last_connection_date($result['user_id'], false, true);
// Convert the timestamp back into a datetime
// NOTE: this timestamp has ALREADY been converted to the local timezone in the get_last_connection_date function
$last_login = date("Y-m-d H:i:s", $timestamp);
} else {
if (isset($user['lastLogin'])) {
$last_login = $user['lastLogin'];
} else {
$last_login = $user['last_login'];
}
}
$result['last_login'] = $last_login;
// Kept for historical reasons
$result['lastLogin'] = $last_login;
return $result;
}
/**
* Find all the information about a user. If no paramater is passed you find all the information about the current user.
* @param $user_id (integer): the id of the user
@ -799,33 +854,14 @@ function api_get_user_courses($userid, $fetch_session = true) {
function api_get_user_info($user_id = '') {
global $tbl_user;
if ($user_id == '') {
return $GLOBALS['_user'];
return _api_format_user($GLOBALS['_user']);
}
$sql = "SELECT * FROM ".Database :: get_main_table(TABLE_MAIN_USER)." WHERE user_id='".Database::escape_string($user_id)."'";
$result = Database::query($sql);
if (Database::num_rows($result) > 0) {
$result_array = Database::fetch_array($result);
// this is done so that it returns the same array-index-names
// ideally the names of the fields of the user table are renamed so that they match $_user (or vice versa)
// $_user should also contain every field of the user table (except password maybe). This would make the
// following lines obsolete (and the code cleaner and slimmer !!!
$user_info['firstname'] = $result_array['firstname'];
$user_info['lastname'] = $result_array['lastname'];
//Kept for historical reasons
$user_info['firstName'] = $result_array['firstname'];
$user_info['lastName'] = $result_array['lastname'];
$user_info['mail'] = $result_array['email'];
$user_info['picture_uri'] = $result_array['picture_uri'];
$user_info['user_id'] = $result_array['user_id'];
$user_info['official_code'] = $result_array['official_code'];
$user_info['status'] = $result_array['status'];
$user_info['auth_source'] = $result_array['auth_source'];
$user_info['username'] = $result_array['username'];
$user_info['theme'] = $result_array['theme'];
return $user_info;
return _api_format_user($result_array);
}
return false;
}
@ -843,23 +879,7 @@ function api_get_user_info_from_username($username = '') {
$result = Database::query($sql);
if (Database::num_rows($result) > 0) {
$result_array = Database::fetch_array($result);
// this is done so that it returns the same array-index-names
// ideally the names of the fields of the user table are renamed so that they match $_user (or vice versa)
// $_user should also contain every field of the user table (except password maybe). This would make the
// following lines obsolete (and the code cleaner and slimmer !!!
$user_info['firstName'] = $result_array['firstname'];
$user_info['lastName'] = $result_array['lastname'];
$user_info['firstname'] = $result_array['firstname'];
$user_info['lastname'] = $result_array['lastname'];
$user_info['mail'] = $result_array['email'];
$user_info['picture_uri'] = $result_array['picture_uri'];
$user_info['user_id'] = $result_array['user_id'];
$user_info['official_code'] = $result_array['official_code'];
$user_info['status'] = $result_array['status'];
$user_info['auth_source'] = $result_array['auth_source'];
$user_info['username'] = $result_array['username'];
$user_info['theme'] = $result_array['theme'];
return $user_info;
return _api_format_user($result_array);
}
return false;
}

@ -0,0 +1,353 @@
<?php
require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/gradebookitem.class.php';
require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/abstractlink.class.php';
require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/evallink.class.php';
require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/dropboxlink.class.php';
require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/result.class.php';
class TestEvaluation extends UnitTestCase {
public function TestEvaluation() {
$this->UnitTestCase('Test Evaluation');
}
public function __construct() {
// The constructor acts like a global setUp for the class
require_once api_get_path(SYS_TEST_PATH).'setup.inc.php';
global $date;
$this->evaluation = new Evaluation();
$this->evaluation-> set_id (1);
$this->evaluation-> set_name ('test');
$this->evaluation-> set_description ('test description');
$this->evaluation-> set_user_id (1);
$this->evaluation-> set_course_code ('COURSETEST');
$this->evaluation-> set_category_id (1);
$this->evaluation-> set_date ($date);
$this->evaluation-> set_weight (1);
$this->evaluation-> set_max (1);
$this->evaluation-> set_visible (1);
}
/**
* Insert this evaluation into the database
*/
public function testadd() {
$res = $this->evaluation->add();
$this->assertTrue(is_null($res));
//var_dump($res);
}
public function testadd_evaluation_log() {
$idevaluation = 1;
$res = $this->evaluation->add_evaluation_log($idevaluation);
$this->assertTrue(is_null($res));
//var_dump($res);
}
/**
* Calculate the score of this evaluation
* @param $stud_id student id (default: all students who have results for this eval - then the average is returned)
* @return array (score, max) if student is given
* array (sum of scores, number of scores) otherwise
* or null if no scores available
*/
public function testcalc_score() {
$res = $this->evaluation->calc_score($stud_id = null);
$this->assertTrue(is_null($res));
//var_dump($res);
}
/**
* Delete this evaluation from the database
*/
public function testdelete() {
$res = $this->evaluation->delete();
$this->assertTrue(is_null($res));
//var_dump($res);
}
/**
* Delete all results for this evaluation
*/
public function testdelete_results() {
$res = $this->evaluation->delete_results();
$this->assertTrue(is_null($res));
//var_dump($res);
}
/**
* Delete this evaluation and all underlying results.
*/
public function testdelete_with_results() {
$res = $this->evaluation->delete_with_results();
$this->assertTrue(is_null($res));
//var_dump($res);
}
/**
* Check if an evaluation name (with the same parent category) already exists
* @param $name name to check (if not given, the name property of this object will be checked)
* @param $parent parent category
*/
public function testdoes_name_exist() {
$name = 'test name';
$parent = 1;
$res = $this->evaluation->does_name_exist($name, $parent);
$this->assertTrue(is_bool($res));
//var_dump($res);
}
/**
* Find evaluations by name
* @param string $name_mask search string
* @return array evaluation objects matching the search criterium
* @todo can be written more efficiently using a new (but very complex) sql query
*/
//problem with the call get_evaluations(): Call to a member function get_evaluations() on a non-object
/*public function testfind_evaluations() {
$name_mask = 'test name mask';
$selectcat = 1;
$res = Evaluation::find_evaluations($name_mask,$selectcat);
$this->assertTrue(is_array($res));
//var_dump($res);
}*/
public function testget_category_id() {
$res = $this->evaluation->get_category_id();
$this->assertTrue(is_numeric($res));
//var_dump($res);
}
public function testget_course_code() {
$res = $this->evaluation->get_course_code();
$this->assertTrue(is_string($res));
//var_dump($res);
}
public function testget_date() {
$res = $this->evaluation->get_date();
$this->assertTrue(is_null($res));
//var_dump($res);
}
public function testget_description() {
$res = $this->evaluation->get_description();
$this->assertTrue(is_string($res));
//var_dump($res);
}
/**
* Retrieve evaluations where a student has results for
* and return them as an array of Evaluation objects
* @param $cat_id parent category (use 'null' to retrieve them in all categories)
* @param $stud_id student id
*/
public function testget_evaluations_with_result_for_student() {
$stud_id = 1;
$res = $this->evaluation->get_evaluations_with_result_for_student($cat_id = null, $stud_id);
$this->assertTrue(is_array($res));
//var_dump($res);
}
public function testget_icon_name() {
$res = $this->evaluation->get_icon_name();
$this->assertTrue(is_string($res));
//var_dump($res);
}
public function testget_id() {
$res = $this->evaluation->get_id();
$this->assertTrue(is_numeric($res));
//var_dump($res);
}
public function testget_item_type() {
$res = $this->evaluation->get_item_type();
$this->assertTrue(is_string($res));
//var_dump($res);
}
public function testget_max() {
$res = $this->evaluation->get_max();
$this->assertTrue(is_numeric($res));
//var_dump($res);
}
public function testget_name() {
$res = $this->evaluation->get_name();
$this->assertTrue(is_string($res));
//var_dump($res);
}
/**
* Get a list of students that do not have a result record for this evaluation
*/
public function testget_not_subscribed_students() {
$res = $this->evaluation->get_not_subscribed_students($first_letter_user = '');
$this->assertTrue(is_array($res));
//var_dump($res);
}
/**
* Generate an array of possible categories where this evaluation can be moved to.
* Notice: its own parent will be included in the list: it's up to the frontend
* to disable this element.
* @return array 2-dimensional array - every element contains 3 subelements (id, name, level)
*/
public function testget_target_categories() {
$res = $this->evaluation->get_target_categories();
$this->assertTrue(is_array($res));
//var_dump($res);
}
public function testget_user_id() {
$res = $this->evaluation->get_user_id();
$this->assertTrue(is_numeric($res));
//var_dump($res);
}
public function testget_weight() {
$res = $this->evaluation->get_weight();
$this->assertTrue(is_numeric($res));
//var_dump($res);
}
/**
* Are there any results for this evaluation yet ?
* The 'max' property should not be changed then.
*/
public function testhas_results() {
$res = $this->evaluation->has_results();
$this->assertTrue(is_bool($res));
//var_dump($res);
}
public function testis_valid_score() {
$score = 1;
$res = $this->evaluation->is_valid_score($score);
$this->assertTrue(is_bool($res));
//var_dump($res);
}
public function testis_visible() {
$res = $this->evaluation->is_visible();
$this->assertTrue(is_numeric($res));
//var_dump($res);
}
/**
* Retrieve evaluations and return them as an array of Evaluation objects
* @param $id evaluation id
* @param $user_id user id (evaluation owner)
* @param $course_code course code
* @param $category_id parent category
* @param $visible visible
*/
public function testload() {
$res = $this->evaluation->load($id = null, $user_id = null, $course_code = null, $category_id = null, $visible = null);
$this->assertTrue(is_array($res));
//var_dump($res);
}
/**
* Move this evaluation to the given category.
* If this evaluation moves from inside a course to outside,
* its course code is also changed.
*/
public function testmove_to_cat() {
$cat = $this->evaluation;
$res = $this->evaluation->move_to_cat($cat);
$this->assertTrue(is_null($res));
//var_dump($res);
}
/**
* Update the properties of this evaluation in the database
*/
public function testsave() {
$res = $this->evaluation->save();
$this->assertTrue(is_null($res));
//var_dump($res);
}
public function testset_category_id() {
$res = $this->evaluation->set_category_id(1);
$this->assertTrue(is_null($res));
//var_dump($res);
}
public function testset_course_code() {
$res = $this->evaluation->set_course_code('COURSETEST');
$this->assertTrue(is_null($res));
//var_dump($res);
}
public function testset_date() {
global $date;
$res = $this->evaluation->set_date('02/02/2010');
$this->assertTrue(is_null($res));
//var_dump($res);
}
public function testset_description() {
$res = $this->evaluation->set_description('test description');
$this->assertTrue(is_null($res));
//var_dump($res);
}
public function testset_id() {
$res = $this->evaluation->set_id(1);
$this->assertTrue(is_null($res));
//var_dump($res);
}
public function testset_max() {
$res = $this->evaluation->set_max(1);
$this->assertTrue(is_null($res));
//var_dump($res);
}
public function testset_name() {
$res = $this->evaluation->set_name('test name');
$this->assertTrue(is_null($res));
//var_dump($res);
}
public function testset_user_id() {
$res = $this->evaluation->set_user_id(1);
$this->assertTrue(is_null($res));
//var_dump($res);
}
public function testset_visible() {
$res = $this->evaluation->set_visible(1);
$this->assertTrue(is_null($res));
//var_dump($res);
}
public function testset_weight() {
$res = $this->evaluation->set_weight(1);
$this->assertTrue(is_null($res));
//var_dump($res);
}
public function __destruct() {
// The destructor acts like a global tearDown for the class
require_once api_get_path(SYS_TEST_PATH).'teardown.inc.php';
}
}
?>
Loading…
Cancel
Save