evaluation.class.php 18 KB

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