evaluation.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Defines a gradebook Evaluation object
  5. * @package chamilo.gradebook
  6. */
  7. /**
  8. * Class
  9. * @package chamilo.gradebook
  10. */
  11. class Evaluation implements GradebookItem
  12. {
  13. // PROPERTIES
  14. private $id;
  15. private $name;
  16. private $description;
  17. private $user_id;
  18. private $course_code;
  19. private $category;
  20. private $created_at;
  21. private $weight;
  22. private $eval_max;
  23. private $visible;
  24. // CONSTRUCTORS
  25. function __construct() {
  26. }
  27. // GETTERS AND SETTERS
  28. public function get_id() {
  29. return $this->id;
  30. }
  31. public function get_name() {
  32. return $this->name;
  33. }
  34. public function get_description() {
  35. return $this->description;
  36. }
  37. public function get_user_id() {
  38. return $this->user_id;
  39. }
  40. public function get_course_code() {
  41. return $this->course_code;
  42. }
  43. public function get_category_id() {
  44. return $this->category;
  45. }
  46. public function get_date() {
  47. return $this->created_at;
  48. }
  49. public function get_weight() {
  50. return $this->weight;
  51. }
  52. public function get_max() {
  53. return $this->eval_max;
  54. }
  55. public function get_type() {
  56. return $this->type;
  57. }
  58. public function is_visible() {
  59. return $this->visible;
  60. }
  61. public function get_locked() {
  62. return $this->locked;
  63. }
  64. public function set_id ($id) {
  65. $this->id = $id;
  66. }
  67. public function set_name ($name) {
  68. $this->name = $name;
  69. }
  70. public function set_description ($description) {
  71. $this->description = $description;
  72. }
  73. public function set_user_id ($user_id) {
  74. $this->user_id = $user_id;
  75. }
  76. public function set_course_code ($course_code) {
  77. $this->course_code = $course_code;
  78. }
  79. public function set_category_id ($category_id) {
  80. $this->category = $category_id;
  81. }
  82. public function set_date ($date) {
  83. $this->created_at = $date;
  84. }
  85. public function set_weight ($weight) {
  86. $this->weight = $weight;
  87. }
  88. public function set_max ($max) {
  89. $this->eval_max = $max;
  90. }
  91. public function set_visible ($visible) {
  92. $this->visible = $visible;
  93. }
  94. public function set_type ($type) {
  95. $this->type = $type;
  96. }
  97. public function set_locked ($locked) {
  98. $this->locked = $locked;
  99. }
  100. // CRUD FUNCTIONS
  101. /**
  102. * Retrieve evaluations and return them as an array of Evaluation objects
  103. * @param $id evaluation id
  104. * @param $user_id user id (evaluation owner)
  105. * @param $course_code course code
  106. * @param $category_id parent category
  107. * @param $visible visible
  108. */
  109. public function load ($id = null, $user_id = null, $course_code = null, $category_id = null, $visible = null, $locked = null) {
  110. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  111. $sql='SELECT id,name,description,user_id,course_code,category_id,created_at,weight,max,visible,type,locked FROM '.$tbl_grade_evaluations;
  112. $paramcount = 0;
  113. if (isset ($id)) {
  114. $sql.= ' WHERE id = '.intval($id);
  115. $paramcount ++;
  116. }
  117. if (isset ($user_id)) {
  118. if ($paramcount != 0) $sql .= ' AND';
  119. else $sql .= ' WHERE';
  120. $sql .= ' user_id = '.intval($user_id);
  121. $paramcount ++;
  122. }
  123. if (isset ($course_code) && $course_code <> '-1') {
  124. if ($paramcount != 0) $sql .= ' AND';
  125. else $sql .= ' WHERE';
  126. $sql .= " course_code = '".Database::escape_string($course_code)."'";
  127. $paramcount ++;
  128. }
  129. if (isset ($category_id)) {
  130. if ($paramcount != 0) $sql .= ' AND';
  131. else $sql .= ' WHERE';
  132. $sql .= ' category_id = '.intval($category_id);
  133. $paramcount ++;
  134. }
  135. if (isset ($visible)) {
  136. if ($paramcount != 0) $sql .= ' AND';
  137. else $sql .= ' WHERE';
  138. $sql .= ' visible = '.intval($visible);
  139. $paramcount ++;
  140. }
  141. if (isset ($locked)) {
  142. if ($paramcount != 0) $sql .= ' AND';
  143. else $sql .= ' WHERE';
  144. $sql .= ' visible = '.intval($locked);
  145. $paramcount ++;
  146. }
  147. //var_dump($sql);
  148. $result = Database::query($sql);
  149. $alleval = Evaluation::create_evaluation_objects_from_sql_result($result);
  150. return $alleval;
  151. }
  152. private function create_evaluation_objects_from_sql_result($result) {
  153. $alleval=array();
  154. if (Database::num_rows($result)) {
  155. while ($data = Database::fetch_array($result)) {
  156. $eval= new Evaluation();
  157. $eval->set_id($data['id']);
  158. $eval->set_name($data['name']);
  159. $eval->set_description($data['description']);
  160. $eval->set_user_id($data['user_id']);
  161. $eval->set_course_code($data['course_code']);
  162. $eval->set_category_id($data['category_id']);
  163. $eval->set_date(api_get_local_time($data['created_at']));
  164. $eval->set_weight($data['weight']);
  165. $eval->set_max($data['max']);
  166. $eval->set_visible($data['visible']);
  167. $eval->set_type($data['type']);
  168. $eval->set_locked($data['locked']);
  169. $alleval[]=$eval;
  170. }
  171. }
  172. return $alleval;
  173. }
  174. /**
  175. * Insert this evaluation into the database
  176. */
  177. public function add()
  178. {
  179. if (isset($this->name) && isset($this->user_id) && isset($this->weight) && isset ($this->eval_max) && isset($this->visible)) {
  180. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  181. $sql = 'INSERT INTO '.$tbl_grade_evaluations
  182. .' (name, user_id, weight, max, visible';
  183. if (isset($this->description)) {
  184. $sql .= ',description';
  185. }
  186. if (isset($this->course_code)) {
  187. $sql .= ', course_code';
  188. }
  189. if (isset($this->category)) {
  190. $sql .= ', category_id';
  191. }
  192. $sql .= ', created_at';
  193. $sql .= ',type';
  194. $sql .= ") VALUES ('".Database::escape_string($this->get_name())."'"
  195. .','.intval($this->get_user_id())
  196. .','.intval($this->get_weight())
  197. .','.intval($this->get_max())
  198. .','.intval($this->is_visible());
  199. if (isset($this->description)) {
  200. $sql .= ",'".Database::escape_string($this->get_description())."'";
  201. }
  202. if (isset($this->course_code)) {
  203. $sql .= ",'".Database::escape_string($this->get_course_code())."'";
  204. }
  205. if (isset($this->category)) {
  206. $sql .= ','.intval($this->get_category_id());
  207. }
  208. if (empty($this->type)) {
  209. $this->type = 'evaluation';
  210. }
  211. $sql .= ", '".api_get_utc_datetime()."'";
  212. $sql .= ',\''.Database::escape_string($this->type).'\'';
  213. $sql .= ")";
  214. Database::query($sql);
  215. $this->set_id(Database::insert_id());
  216. }
  217. else {
  218. die('Error in Evaluation add: required field empty');
  219. }
  220. }
  221. public function add_evaluation_log($idevaluation){
  222. if (!empty($idevaluation)) {
  223. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  224. $tbl_grade_linkeval_log = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINKEVAL_LOG);
  225. $eval=new Evaluation();
  226. $dateobject=$eval->load ($idevaluation,null,null,null,null);
  227. $arreval=get_object_vars($dateobject[0]);
  228. if (!empty($arreval['id'])) {
  229. $sql_eval='SELECT weight from '.$tbl_grade_evaluations.' WHERE id='.$arreval['id'];
  230. $rs=Database::query($sql_eval);
  231. $row_old_weight=Database::fetch_array($rs,'ASSOC');
  232. $current_date=api_get_utc_datetime();
  233. $sql="INSERT INTO ".$tbl_grade_linkeval_log."(id_linkeval_log,name,description,created_at,weight,visible,type,user_id_log)
  234. VALUES('".Database::escape_string($arreval['id'])."','".Database::escape_string($arreval['name'])."','".Database::escape_string($arreval['description'])."','".$current_date."','".Database::escape_string($row_old_weight['weight'])."','".Database::escape_string($arreval['visible'])."','evaluation',".api_get_user_id().")";
  235. Database::query($sql);
  236. }
  237. }
  238. }
  239. /**
  240. * Update the properties of this evaluation in the database
  241. */
  242. public function save() {
  243. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  244. $sql = 'UPDATE '.$tbl_grade_evaluations
  245. ." SET name = '".Database::escape_string($this->get_name())."'"
  246. .', description = ';
  247. if (isset($this->description)) {
  248. $sql .= "'".Database::escape_string($this->get_description())."'";
  249. }else {
  250. $sql .= 'null';
  251. }
  252. $sql .= ', user_id = '.intval($this->get_user_id())
  253. .', course_code = ';
  254. if (isset($this->course_code)) {
  255. $sql .= "'".Database::escape_string($this->get_course_code())."'";
  256. } else {
  257. $sql .= 'null';
  258. }
  259. $sql .= ', category_id = ';
  260. if (isset($this->category)) {
  261. $sql .= intval($this->get_category_id());
  262. } else {
  263. $sql .= 'null';
  264. }
  265. $sql .= ', weight = '.Database::escape_string($this->get_weight())
  266. .', max = '.Database::escape_string($this->get_max())
  267. .', visible = '.intval($this->is_visible())
  268. .' WHERE id = '.intval($this->id);
  269. //recorded history
  270. $eval_log=new Evaluation();
  271. $eval_log->add_evaluation_log($this->id);
  272. Database::query($sql);
  273. }
  274. /**
  275. * Delete this evaluation from the database
  276. */
  277. public function delete() {
  278. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  279. $sql = 'DELETE FROM '.$tbl_grade_evaluations.' WHERE id = '.intval($this->id);
  280. Database::query($sql);
  281. }
  282. // OTHER FUNCTIONS
  283. /**
  284. * Check if an evaluation name (with the same parent category) already exists
  285. * @param $name name to check (if not given, the name property of this object will be checked)
  286. * @param $parent parent category
  287. */
  288. public function does_name_exist($name, $parent) {
  289. if (!isset ($name)) {
  290. $name = $this->name;
  291. $parent = $this->category;
  292. }
  293. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  294. $sql = 'SELECT count(id) AS number'
  295. .' FROM '.$tbl_grade_evaluations
  296. ." WHERE name = '".Database::escape_string($name)."'";
  297. if (api_is_allowed_to_edit()) {
  298. $parent = Category::load($parent);
  299. $code = $parent[0]->get_course_code();
  300. if (isset($code) && $code != '0') {
  301. $main_course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  302. $sql .= ' AND user_id IN ('
  303. .' SELECT user_id FROM '.$main_course_user_table
  304. ." WHERE course_code = '".Database::escape_string($code)."'"
  305. .' AND status = '.COURSEMANAGER
  306. .')';
  307. } else {
  308. $sql .= ' AND user_id = '.api_get_user_id();
  309. }
  310. }else {
  311. $sql .= ' AND user_id = '.api_get_user_id();
  312. }
  313. if (!isset ($parent)) {
  314. $sql.= ' AND category_id is null';
  315. } else {
  316. $sql.= ' AND category_id = '.intval($parent);
  317. }
  318. $result = Database::query($sql);
  319. $number=Database::fetch_row($result);
  320. return ($number[0] != 0);
  321. }
  322. /**
  323. * Are there any results for this evaluation yet ?
  324. * The 'max' property should not be changed then.
  325. */
  326. public function has_results() {
  327. $tbl_grade_results = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  328. $sql='SELECT count(id) AS number FROM '.$tbl_grade_results
  329. .' WHERE evaluation_id = '.intval($this->id);
  330. $result = Database::query($sql);
  331. $number=Database::fetch_row($result);
  332. return ($number[0] != 0);
  333. }
  334. /**
  335. * Delete all results for this evaluation
  336. */
  337. public function delete_results() {
  338. $tbl_grade_results = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  339. $sql = 'DELETE FROM '.$tbl_grade_results.' WHERE evaluation_id = '.intval($this->id);
  340. Database::query($sql);
  341. }
  342. /**
  343. * Delete this evaluation and all underlying results.
  344. */
  345. public function delete_with_results(){
  346. $this->delete_results();
  347. $this->delete();
  348. }
  349. /**
  350. * Check if the given score is possible for this evaluation
  351. */
  352. public function is_valid_score ($score) {
  353. return (is_numeric($score) && $score >= 0 && $score <= $this->eval_max);
  354. }
  355. /**
  356. * Calculate the score of this evaluation
  357. * @param $stud_id student id (default: all students who have results for this eval - then the average is returned)
  358. * @return array (score, max) if student is given
  359. * array (sum of scores, number of scores) otherwise
  360. * or null if no scores available
  361. */
  362. public function calc_score ($stud_id = null) {
  363. $results = Result::load(null,$stud_id,$this->id);
  364. $rescount = 0;
  365. $sum = 0;
  366. foreach ($results as $res) {
  367. $score = $res->get_score();
  368. if ((!empty ($score)) || ($score == '0')) {
  369. $rescount++;
  370. $sum += ($score / $this->get_max());
  371. }
  372. }
  373. if ($rescount == 0) {
  374. return null;
  375. }
  376. else if (isset($stud_id)) {
  377. return array ($score, $this->get_max());
  378. } else {
  379. return array ($sum, $rescount);
  380. }
  381. }
  382. /**
  383. * Generate an array of possible categories where this evaluation can be moved to.
  384. * Notice: its own parent will be included in the list: it's up to the frontend
  385. * to disable this element.
  386. * @return array 2-dimensional array - every element contains 3 subelements (id, name, level)
  387. */
  388. public function get_target_categories() {
  389. // - course independent evaluation
  390. // -> movable to root or other course independent categories
  391. // - evaluation inside a course
  392. // -> movable to root, independent categories or categories inside the course
  393. $user = (api_is_platform_admin() ? null : api_get_user_id());
  394. $targets = array();
  395. $level = 0;
  396. $root = array(0, get_lang('RootCat'), $level);
  397. $targets[] = $root;
  398. if (isset($this->course_code) && !empty($this->course_code)) {
  399. $crscats = Category::load(null,null,$this->course_code,0);
  400. foreach ($crscats as $cat) {
  401. $targets[] = array ($cat->get_id(), $cat->get_name(), $level+1);
  402. $targets = $this->add_target_subcategories($targets, $level+1, $cat->get_id());
  403. }
  404. }
  405. $indcats = Category::load(null,$user,0,0);
  406. foreach ($indcats as $cat) {
  407. $targets[] = array ($cat->get_id(), $cat->get_name(), $level+1);
  408. $targets = $this->add_target_subcategories($targets, $level+1, $cat->get_id());
  409. }
  410. return $targets;
  411. }
  412. /**
  413. * Internal function used by get_target_categories()
  414. */
  415. private function add_target_subcategories($targets, $level, $catid) {
  416. $subcats = Category::load(null,null,null,$catid);
  417. foreach ($subcats as $cat) {
  418. $targets[] = array ($cat->get_id(), $cat->get_name(), $level+1);
  419. $targets = $this->add_target_subcategories($targets, $level+1, $cat->get_id());
  420. }
  421. return $targets;
  422. }
  423. /**
  424. * Move this evaluation to the given category.
  425. * If this evaluation moves from inside a course to outside,
  426. * its course code is also changed.
  427. */
  428. public function move_to_cat ($cat) {
  429. $this->set_category_id($cat->get_id());
  430. if ($this->get_course_code() != $cat->get_course_code()) {
  431. $this->set_course_code($cat->get_course_code());
  432. }
  433. $this->save();
  434. }
  435. /**
  436. * Retrieve evaluations where a student has results for
  437. * and return them as an array of Evaluation objects
  438. * @param $cat_id parent category (use 'null' to retrieve them in all categories)
  439. * @param $stud_id student id
  440. */
  441. public function get_evaluations_with_result_for_student ($cat_id = null, $stud_id) {
  442. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  443. $tbl_grade_results = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  444. $sql = 'SELECT * FROM '.$tbl_grade_evaluations
  445. .' WHERE id IN'
  446. .'(SELECT evaluation_id FROM '.$tbl_grade_results
  447. .' WHERE user_id = '.intval($stud_id).' AND score IS NOT NULL)';
  448. if (!api_is_allowed_to_edit()) {
  449. $sql .= ' AND visible = 1';
  450. }
  451. if (isset($cat_id)) {
  452. $sql .= ' AND category_id = '.intval($cat_id);
  453. } else {
  454. $sql .= ' AND category_id >= 0';
  455. }
  456. $result = Database::query($sql);
  457. $alleval = Evaluation::create_evaluation_objects_from_sql_result($result);
  458. return $alleval;
  459. }
  460. /**
  461. * Get a list of students that do not have a result record for this evaluation
  462. */
  463. public function get_not_subscribed_students ($first_letter_user = '') {
  464. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  465. $tbl_grade_results = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  466. $sql = 'SELECT user_id,lastname,firstname,username FROM '.$tbl_user
  467. ." WHERE lastname LIKE '".Database::escape_string($first_letter_user)."%'"
  468. .' AND status = '.STUDENT
  469. .' AND user_id NOT IN'
  470. .' (SELECT user_id FROM '.$tbl_grade_results
  471. .' WHERE evaluation_id = '.intval($this->id)
  472. .' )'
  473. .' ORDER BY lastname';
  474. $result = Database::query($sql);
  475. $db_users = Database::store_result($result);
  476. return $db_users;
  477. }
  478. /**
  479. * Find evaluations by name
  480. * @param string $name_mask search string
  481. * @return array evaluation objects matching the search criterium
  482. * @todo can be written more efficiently using a new (but very complex) sql query
  483. */
  484. public function find_evaluations ($name_mask,$selectcat) {
  485. $rootcat = Category::load($selectcat);
  486. $evals = $rootcat[0]->get_evaluations((api_is_allowed_to_create_course() ? null : api_get_user_id()), true);
  487. $foundevals = array();
  488. foreach ($evals as $eval) {
  489. if (!(api_strpos(api_strtolower($eval->get_name()), api_strtolower($name_mask)) === false)) {
  490. $foundevals[] = $eval;
  491. }
  492. }
  493. return $foundevals;
  494. }
  495. // Other methods implementing GradebookItem
  496. public function get_item_type() {
  497. return 'E';
  498. }
  499. public function get_icon_name() {
  500. return $this->has_results() ? 'evalnotempty' : 'evalempty';
  501. }
  502. /***
  503. * This function, locks an evaluation, only one who can unlock it is the platform administrator.
  504. * @param int evaluation id
  505. * @param int locked 1 or unlocked 0
  506. * @return bool
  507. *
  508. * */
  509. function locked_evaluation($id_evaluation, $locked) {
  510. $table_evaluation = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  511. $sql = "UPDATE $table_evaluation SET locked = '".intval($locked)."' WHERE id='".intval($id_evaluation)."'";
  512. $rs = Database::query($sql);
  513. $affected_rows = Database::affected_rows();
  514. if (!empty($affected_rows)) {
  515. return true;
  516. }
  517. }
  518. }