gradebook_functions.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2008 Dokeos SPRL
  6. For a full list of contributors, see "credits.txt".
  7. The full license can be read in "license.txt".
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12. See the GNU General Public License for more details.
  13. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  14. Mail: info@dokeos.com
  15. ==============================================================================
  16. */
  17. /*
  18. * These are functions used in gradebook
  19. *
  20. * @author Stijn Konings <konings.stijn@skynet.be>, Hogeschool Ghent
  21. * @version april 2007
  22. */
  23. require_once ('gradebook_functions_users.inc.php');
  24. /**
  25. * Adds a resource to the unique gradebook of a given course
  26. * @param string Course code
  27. * @param int Resource type (use constants defined in linkfactory.class.php)
  28. * @param int Resource ID in the corresponding tool
  29. * @param string Resource name to show in the gradebook
  30. * @param int Resource weight to set in the gradebook
  31. * @param int Resource max
  32. * @param string Resource description
  33. * @param string Date
  34. * @param int Visibility (0 hidden, 1 shown)
  35. * @param int Session ID (optional or 0 if not defined)
  36. * @return boolean True on success, false on failure
  37. */
  38. function add_resource_to_course_gradebook($course_code, $resource_type, $resource_id, $resource_name='', $weight=0, $max=0, $resource_description='', $date=null, $visible=0, $session_id = 0) {
  39. /* See defines in lib/be/linkfactory.class.php
  40. define('LINK_EXERCISE',1);
  41. define('LINK_DROPBOX',2);
  42. define('LINK_STUDENTPUBLICATION',3);
  43. define('LINK_LEARNPATH',4);
  44. define('LINK_FORUM_THREAD',5),
  45. define('LINK_WORK',6);
  46. */
  47. $category = 0;
  48. require_once (api_get_path(SYS_CODE_PATH).'gradebook/lib/be.inc.php');
  49. $link= LinkFactory :: create($resource_type);
  50. $link->set_user_id(api_get_user_id());
  51. $link->set_course_code($course_code);
  52. // TODO find the corresponding category (the first one for this course, ordered by ID)
  53. $t = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  54. $sql = "SELECT * FROM $t WHERE course_code = '".Database::escape_string($course_code)."' ";
  55. if (!empty($session_id)) {
  56. $sql .= " AND session_id = ".(int)$session_id;
  57. } else {
  58. $sql .= " AND (session_id IS NULL OR session_id = 0) ";
  59. }
  60. $sql .= " ORDER BY id";
  61. $res = Database::query($sql,__FILE__,__LINE__);
  62. if (Database::num_rows($res)<1){
  63. //there is no unique category for this course+session combination,
  64. // => create one
  65. $cat= new Category();
  66. if (!empty($session_id)) {
  67. $my_session_id=api_get_session_id();
  68. $s_name = api_get_session_name($my_session_id);
  69. $cat->set_name($course_code.' - '.get_lang('Session').' '.$s_name);
  70. $cat->set_session_id($session_id);
  71. } else {
  72. $cat->set_name($course_code);
  73. }
  74. $cat->set_course_code($course_code);
  75. $cat->set_description(null);
  76. $cat->set_user_id(api_get_user_id());
  77. $cat->set_parent_id(0);
  78. $cat->set_weight(100);
  79. $cat->set_visible(0);
  80. $can_edit = api_is_allowed_to_edit(true, true);
  81. if ($can_edit) {
  82. $cat->add();
  83. }
  84. $category = $cat->get_id();
  85. unset ($cat);
  86. } else {
  87. $row = Database::fetch_array($res);
  88. $category = $row['id'];
  89. }
  90. $link->set_category_id($category);
  91. if ($link->needs_name_and_description()) {
  92. $link->set_name($resource_name);
  93. } else {
  94. $link->set_ref_id($resource_id);
  95. }
  96. $link->set_weight($weight);
  97. if ($link->needs_max()) {
  98. $link->set_max($max);
  99. }
  100. if (isset($date)) {
  101. $link->set_date($date);
  102. }
  103. if ($link->needs_name_and_description()) {
  104. $link->set_description($resource_description);
  105. }
  106. $link->set_visible(empty ($visible) ? 0 : 1);
  107. if (!empty($session_id)) {
  108. $link->set_session_id($session_id);
  109. }
  110. $link->add();
  111. return true;
  112. }
  113. function block_students() {
  114. if (!api_is_allowed_to_create_course()) {
  115. require_once (api_get_path(INCLUDE_PATH)."header.inc.php");
  116. api_not_allowed();
  117. }
  118. }
  119. /**
  120. * Returns the info header for the user result page
  121. * @param $userid
  122. */
  123. /**
  124. * Returns the course name from a given code
  125. * @param string $code
  126. */
  127. function get_course_name_from_code($code) {
  128. $tbl_main_categories= Database :: get_main_table(TABLE_MAIN_COURSE);
  129. $sql= 'SELECT title,code FROM ' . $tbl_main_categories . 'WHERE code = "' . $code . '"';
  130. $result= Database::query($sql,__FILE__,__LINE__);
  131. if ($col= Database::fetch_array($result)) {
  132. return $col['title'];
  133. }
  134. }
  135. /**
  136. * Builds an img tag for a gradebook item
  137. * @param string $type value returned by a gradebookitem's get_icon_name()
  138. */
  139. function build_type_icon_tag($kind) {
  140. return '<img src="' . get_icon_file_name ($kind) . '" border="0" hspace="5" align="middle" alt="" />';
  141. }
  142. /**
  143. * Returns the icon filename for a gradebook item
  144. * @param string $type value returned by a gradebookitem's get_icon_name()
  145. */
  146. function get_icon_file_name ($type) {
  147. if ($type == 'cat') {
  148. return api_get_path(WEB_CODE_PATH) . 'img/gradebook.gif';
  149. } elseif ($type == 'evalempty') {
  150. return api_get_path(WEB_CODE_PATH) . 'img/empty.gif';
  151. } elseif ($type == 'evalnotempty') {
  152. return api_get_path(WEB_CODE_PATH) . 'img/gradebook_eval_not_empty.gif';
  153. } elseif ($type == 'link') {
  154. return api_get_path(WEB_CODE_PATH) . 'img/link.gif';
  155. } else {
  156. return null;
  157. }
  158. }
  159. /**
  160. * Builds the course or platform admin icons to edit a category
  161. * @param object $cat category object
  162. * @param int $selectcat id of selected category
  163. */
  164. function build_edit_icons_cat($cat, $selectcat) {
  165. $show_message=$cat->show_message_resource_delete($cat->get_course_code());
  166. if ($show_message===false) {
  167. $visibility_icon= ($cat->is_visible() == 0) ? 'invisible' : 'visible';
  168. $visibility_command= ($cat->is_visible() == 0) ? 'set_visible' : 'set_invisible';
  169. $modify_icons= '<a href="gradebook_edit_cat.php?editcat=' . $cat->get_id() . ' &amp;cidReq='.$cat->get_course_code().'"><img src="../img/edit.gif" border="0" title="' . get_lang('Modify') . '" alt="" /></a>';
  170. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?deletecat=' . $cat->get_id() . '&amp;selectcat=' . $selectcat . '&amp;cidReq='.$cat->get_course_code().'" onclick="return confirmation();"><img src="../img/delete.gif" border="0" title="' . get_lang('DeleteAll') . '" alt="" /></a>';
  171. //no move ability for root categories
  172. if ($cat->is_movable()) {
  173. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?movecat=' . $cat->get_id() . '&amp;selectcat=' . $selectcat . ' &amp;cidReq='.$cat->get_course_code().'"><img src="../img/deplacer_fichier.gif" border="0" title="' . get_lang('Move') . '" alt="" /></a>';
  174. } else {
  175. //$modify_icons .= '&nbsp;<img src="../img/deplacer_fichier_na.gif" border="0" title="' . get_lang('Move') . '" alt="" />';
  176. }
  177. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?visiblecat=' . $cat->get_id() . '&amp;' . $visibility_command . '=&amp;selectcat=' . $selectcat . ' "><img src="../img/' . $visibility_icon . '.gif" border="0" title="' . get_lang('Visible') . '" alt="" /></a>';
  178. return $modify_icons;
  179. }
  180. }
  181. /**
  182. * Builds the course or platform admin icons to edit an evaluation
  183. * @param object $eval evaluation object
  184. * @param int $selectcat id of selected category
  185. */
  186. function build_edit_icons_eval($eval, $selectcat) {
  187. $status=CourseManager::get_user_in_course_status(api_get_user_id(), api_get_course_id());
  188. $eval->get_course_code();
  189. $cat=new Category();
  190. $message_eval=$cat->show_message_resource_delete($eval->get_course_code());
  191. if ($message_eval===false) {
  192. $visibility_icon= ($eval->is_visible() == 0) ? 'invisible' : 'visible';
  193. $visibility_command= ($eval->is_visible() == 0) ? 'set_visible' : 'set_invisible';
  194. $modify_icons= '<a href="gradebook_edit_eval.php?editeval=' . $eval->get_id() . ' &amp;cidReq='.$eval->get_course_code().'"><img src="../img/edit.gif" border="0" title="' . get_lang('Modify') . '" alt="" /></a>';
  195. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?deleteeval=' . $eval->get_id() . '&selectcat=' . $selectcat . ' &amp;cidReq='.$eval->get_course_code().'" onclick="return confirmation();"><img src="../img/delete.gif" border="0" title="' . get_lang('Delete') . '" alt="" /></a>';
  196. //$modify_icons .= '&nbsp;<a href="' . api_get_self() . '?moveeval=' . $eval->get_id() . '&selectcat=' . $selectcat . '"><img src="../img/deplacer_fichier.gif" border="0" title="' . get_lang('Move') . '" alt="" /></a>';
  197. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?visibleeval=' . $eval->get_id() . '&amp;' . $visibility_command . '=&amp;selectcat=' . $selectcat . ' "><img src="../img/' . $visibility_icon . '.gif" border="0" title="' . get_lang('Visible') . '" alt="" /></a>';
  198. if ($status==1 || is_null($status)){
  199. $modify_icons .= '&nbsp;<a href="gradebook_showlog_eval.php?visiblelog=' . $eval->get_id() . '&amp;selectcat=' . $selectcat . ' &amp;cidReq='.$eval->get_course_code().'"><img src="../img/file_txt_small.gif" border="0" title="' . get_lang('GradebookQualifyLog') . '" alt="" /></a>';
  200. }
  201. return $modify_icons;
  202. }
  203. }
  204. /**
  205. * Builds the course or platform admin icons to edit a link
  206. * @param object $linkobject
  207. * @param int $selectcat id of selected category
  208. */
  209. function build_edit_icons_link($link, $selectcat) {
  210. $link->get_course_code();
  211. $cat=new Category();
  212. $message_link=$cat->show_message_resource_delete($link->get_course_code());
  213. if ($message_link===false) {
  214. $visibility_icon= ($link->is_visible() == 0) ? 'invisible' : 'visible';
  215. $visibility_command= ($link->is_visible() == 0) ? 'set_visible' : 'set_invisible';
  216. $modify_icons= '<a href="gradebook_edit_link.php?editlink=' . $link->get_id() . ' &amp;cidReq='.$link->get_course_code().'"><img src="../img/edit.gif" border="0" title="' . get_lang('Modify') . '" alt="" /></a>';
  217. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?deletelink=' . $link->get_id() . '&selectcat=' . $selectcat . ' &amp;cidReq='.$link->get_course_code().'" onclick="return confirmation();"><img src="../img/delete.gif" border="0" title="' . get_lang('Delete') . '" alt="" /></a>';
  218. //$modify_icons .= '&nbsp;<a href="' . api_get_self() . '?movelink=' . $link->get_id() . '&selectcat=' . $selectcat . '"><img src="../img/deplacer_fichier.gif" border="0" title="' . get_lang('Move') . '" alt="" /></a>';
  219. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?visiblelink=' . $link->get_id() . '&amp;' . $visibility_command . '=&amp;selectcat=' . $selectcat . ' "><img src="../img/' . $visibility_icon . '.gif" border="0" title="' . get_lang('Visible') . '" alt="" /></a>';
  220. $modify_icons .= '&nbsp;<a href="gradebook_showlog_link.php?visiblelink=' . $link->get_id() . '&amp;selectcat=' . $selectcat . '&amp;cidReq='.$link->get_course_code().'"><img src="../img/file_txt_small.gif" border="0" title="' . get_lang('GradebookQualifyLog') . '" alt="" /></a>';
  221. //if (api_is_course_admin() == true) {
  222. //$modify_icons .= '&nbsp;<a href="gradebook_showlog_eval.php?visiblelog=' . $eval->get_id() . '&amp;' . $visibility_command . '=&amp;selectcat=' . $selectcat . '"><img src="../img/file_txt_small.gif" border="0" title="' . get_lang('GradebookQualifyLog') . '" alt="" /></a>';
  223. //}
  224. return $modify_icons;
  225. }
  226. }
  227. /**
  228. * Checks if a resource is in the unique gradebook of a given course
  229. * @param string Course code
  230. * @param int Resource type (use constants defined in linkfactory.class.php)
  231. * @param int Resource ID in the corresponding tool
  232. * @param int Session ID (optional - 0 if not defined)
  233. * @return int false on error or link ID
  234. */
  235. function is_resource_in_course_gradebook($course_code, $resource_type, $resource_id, $session_id = 0) {
  236. /* See defines in lib/be/linkfactory.class.php
  237. define('LINK_EXERCISE',1);
  238. define('LINK_DROPBOX',2);
  239. define('LINK_STUDENTPUBLICATION',3);
  240. define('LINK_LEARNPATH',4);
  241. define('LINK_FORUM_THREAD',5),
  242. define('LINK_WORK',6);
  243. */
  244. require_once(api_get_path(SYS_CODE_PATH).'gradebook/lib/be/linkfactory.class.php');
  245. require_once (api_get_path(SYS_CODE_PATH).'gradebook/lib/be.inc.php');
  246. require_once(api_get_path(SYS_CODE_PATH).'gradebook/lib/be/linkfactory.class.php');
  247. // TODO find the corresponding category (the first one for this course, ordered by ID)
  248. $t = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  249. $l = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  250. $sql = "SELECT * FROM $t WHERE course_code = '".Database::escape_string($course_code)."' ";
  251. if (!empty($session_id)) {
  252. $sql .= " AND session_id = ".(int)$session_id;
  253. } else {
  254. $sql .= " AND (session_id IS NULL OR session_id = 0) ";
  255. }
  256. $sql .= " ORDER BY id";
  257. $res = Database::query($sql,__FILE__,__LINE__);
  258. if (Database::num_rows($res)<1) {
  259. return false;
  260. }
  261. $row = Database::fetch_array($res);
  262. $category = $row['id'];
  263. $sql = "SELECT * FROM $l l WHERE l.category_id = $category AND type = ".(int) $resource_type." and ref_id = ".(int) $resource_id;
  264. $res = Database::query($sql,__FILE__,__LINE__);
  265. if (Database::num_rows($res)<1) {
  266. return false;
  267. }
  268. $row = Database::fetch_array($res);
  269. return $row['id'];
  270. }
  271. /**
  272. * Remove a resource from the unique gradebook of a given course
  273. * @param int Link/Resource ID
  274. * @return bool false on error, true on success
  275. */
  276. function remove_resource_from_course_gradebook($link_id) {
  277. if ( empty($link_id) ) { return false; }
  278. require_once (api_get_path(SYS_CODE_PATH).'gradebook/lib/be.inc.php');
  279. // TODO find the corresponding category (the first one for this course, ordered by ID)
  280. $l = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  281. $sql = "DELETE FROM $l WHERE id = ".(int)$link_id;
  282. $res = Database::query($sql,__FILE__,__LINE__);
  283. return true;
  284. }
  285. /**
  286. * return the database name
  287. * @param int
  288. * @return String
  289. */
  290. function get_database_name_by_link_id($id_link) {
  291. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  292. $tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  293. $res=Database::query('SELECT db_name from '.$course_table.' c inner join '.$tbl_grade_links.' l
  294. on c.code=l.course_code WHERE l.id='.$id_link.' OR l.category_id='.$id_link);
  295. $my_db_name=Database::fetch_array($res,'ASSOC');
  296. return $my_db_name['db_name'];
  297. }