GradebookUtils.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class GradebookUtils
  5. */
  6. class GradebookUtils
  7. {
  8. /**
  9. * Adds a resource to the unique gradebook of a given course
  10. * @param int
  11. * @param string Course code
  12. * @param int Resource type (use constants defined in linkfactory.class.php)
  13. * @param int Resource ID in the corresponding tool
  14. * @param string Resource name to show in the gradebook
  15. * @param int Resource weight to set in the gradebook
  16. * @param int Resource max
  17. * @param string Resource description
  18. * @param int Visibility (0 hidden, 1 shown)
  19. * @param int Session ID (optional or 0 if not defined)
  20. * @param int
  21. * @return boolean True on success, false on failure
  22. */
  23. public static function add_resource_to_course_gradebook(
  24. $category_id,
  25. $course_code,
  26. $resource_type,
  27. $resource_id,
  28. $resource_name = '',
  29. $weight = 0,
  30. $max = 0,
  31. $resource_description = '',
  32. $visible = 0,
  33. $session_id = 0,
  34. $link_id = null
  35. ) {
  36. $link = LinkFactory :: create($resource_type);
  37. $link->set_user_id(api_get_user_id());
  38. $link->set_course_code($course_code);
  39. if (empty($category_id)) {
  40. return false;
  41. }
  42. $link->set_category_id($category_id);
  43. if ($link->needs_name_and_description()) {
  44. $link->set_name($resource_name);
  45. } else {
  46. $link->set_ref_id($resource_id);
  47. }
  48. $link->set_weight($weight);
  49. if ($link->needs_max()) {
  50. $link->set_max($max);
  51. }
  52. if ($link->needs_name_and_description()) {
  53. $link->set_description($resource_description);
  54. }
  55. $link->set_visible(empty($visible) ? 0 : 1);
  56. if (!empty($session_id)) {
  57. $link->set_session_id($session_id);
  58. }
  59. $link->add();
  60. return true;
  61. }
  62. /**
  63. * Update a resource weight
  64. * @param int Link/Resource ID
  65. * @param string
  66. * @param float
  67. * @return bool false on error, true on success
  68. */
  69. public static function update_resource_from_course_gradebook($link_id, $course_code, $weight)
  70. {
  71. $course_code = Database::escape_string($course_code);
  72. if (!empty($link_id)) {
  73. $link_id = intval($link_id);
  74. $sql = 'UPDATE ' . Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK) . '
  75. SET weight = ' . "'" . Database::escape_string((float) $weight) . "'" . '
  76. WHERE course_code = "' . $course_code . '" AND id = ' . $link_id;
  77. Database::query($sql);
  78. }
  79. return true;
  80. }
  81. /**
  82. * Remove a resource from the unique gradebook of a given course
  83. * @param int Link/Resource ID
  84. * @return bool false on error, true on success
  85. */
  86. public static function remove_resource_from_course_gradebook($link_id)
  87. {
  88. if (empty($link_id)) {
  89. return false;
  90. }
  91. // TODO find the corresponding category (the first one for this course, ordered by ID)
  92. $l = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  93. $sql = "DELETE FROM $l WHERE id = ".(int)$link_id;
  94. Database::query($sql);
  95. return true;
  96. }
  97. /**
  98. * Block students
  99. */
  100. public static function block_students()
  101. {
  102. if (!api_is_allowed_to_edit()) {
  103. api_not_allowed();
  104. }
  105. }
  106. /**
  107. * Builds an img tag for a gradebook item
  108. * @param string $type value returned by a gradebookitem's get_icon_name()
  109. */
  110. public static function build_type_icon_tag($kind, $attributes = array())
  111. {
  112. return Display::return_icon(self::get_icon_file_name($kind), ' ', $attributes, ICON_SIZE_SMALL);
  113. }
  114. /**
  115. * Returns the icon filename for a gradebook item
  116. * @param string $type value returned by a gradebookitem's get_icon_name()
  117. */
  118. public static function get_icon_file_name($type)
  119. {
  120. switch ($type) {
  121. case 'cat':
  122. $icon = 'gradebook.png';
  123. break;
  124. case 'evalempty':
  125. $icon = 'empty_evaluation.png';
  126. break;
  127. case 'evalnotempty':
  128. $icon = 'no_empty_evaluation.png';
  129. break;
  130. case 'exercise':
  131. case LINK_EXERCISE:
  132. $icon = 'quiz.gif';
  133. break;
  134. case 'learnpath':
  135. case LINK_LEARNPATH:
  136. $icon = 'learnpath.png';
  137. break;
  138. case 'studentpublication':
  139. case LINK_STUDENTPUBLICATION:
  140. $icon = 'works.gif';
  141. break;
  142. case 'link':
  143. $icon = 'link.gif';
  144. break;
  145. case 'forum':
  146. case LINK_FORUM_THREAD:
  147. $icon = 'forum.gif';
  148. break;
  149. case 'attendance':
  150. case LINK_ATTENDANCE:
  151. $icon = 'attendance.gif';
  152. break;
  153. case 'survey':
  154. case LINK_SURVEY:
  155. $icon = 'survey.gif';
  156. break;
  157. case 'dropbox':
  158. case LINK_DROPBOX:
  159. $icon = 'dropbox.gif';
  160. break;
  161. default:
  162. $icon = 'link.gif';
  163. break;
  164. }
  165. return $icon;
  166. }
  167. /**
  168. * Builds the course or platform admin icons to edit a category
  169. * @param Category $cat category
  170. * @param Category $selectcat id of selected category
  171. */
  172. public static function build_edit_icons_cat($cat, $selectcat)
  173. {
  174. $show_message = $cat->show_message_resource_delete($cat->get_course_code());
  175. $grade_model_id = $selectcat->get_grade_model_id();
  176. $selectcat = $selectcat->get_id();
  177. $modify_icons = null;
  178. if ($show_message === false) {
  179. $visibility_icon = ($cat->is_visible() == 0) ? 'invisible' : 'visible';
  180. $visibility_command = ($cat->is_visible() == 0) ? 'set_visible' : 'set_invisible';
  181. $modify_icons .= '<a class="view_children" data-cat-id="' . $cat->get_id() . '" href="javascript:void(0);">' .
  182. Display::return_icon('view_more_stats.gif', get_lang('Show'), '', ICON_SIZE_SMALL) . '</a>';
  183. if (!api_is_allowed_to_edit(null, true)) {
  184. $modify_icons .= Display::url(
  185. Display::return_icon(
  186. 'stats.png',
  187. get_lang('FlatView'),
  188. '',
  189. ICON_SIZE_SMALL
  190. ),
  191. 'personal_stats.php?' . http_build_query([
  192. 'selectcat' => $cat->get_id()
  193. ]) . '&' . api_get_cidreq(),
  194. [
  195. 'class' => 'ajax',
  196. 'data-title' => get_lang('FlatView')
  197. ]
  198. );
  199. }
  200. if (api_is_allowed_to_edit(null, true)) {
  201. // Locking button
  202. if (api_get_setting('gradebook_locking_enabled') == 'true') {
  203. if ($cat->is_locked()) {
  204. if (api_is_platform_admin()) {
  205. $modify_icons .= '&nbsp;<a onclick="javascript:if (!confirm(\'' . addslashes(get_lang('ConfirmToUnlockElement')) . '\')) return false;" href="' . api_get_self() . '?' . api_get_cidreq() . '&category_id=' . $cat->get_id() . '&action=unlock">' .
  206. Display::return_icon('lock.png', get_lang('UnLockEvaluation'), '', ICON_SIZE_SMALL) . '</a>';
  207. } else {
  208. $modify_icons .= '&nbsp;<a href="#">' . Display::return_icon('lock_na.png', get_lang('GradebookLockedAlert'), '', ICON_SIZE_SMALL) . '</a>';
  209. }
  210. $modify_icons .= '&nbsp;<a href="gradebook_flatview.php?export_pdf=category&selectcat=' . $cat->get_id() . '" >' . Display::return_icon('pdf.png', get_lang('ExportToPDF'), '', ICON_SIZE_SMALL) . '</a>';
  211. } else {
  212. $modify_icons .= '&nbsp;<a onclick="javascript:if (!confirm(\'' . addslashes(get_lang('ConfirmToLockElement')) . '\')) return false;" href="' . api_get_self() . '?' . api_get_cidreq() . '&category_id=' . $cat->get_id() . '&action=lock">' .
  213. Display::return_icon('unlock.png', get_lang('LockEvaluation'), '', ICON_SIZE_SMALL) . '</a>';
  214. $modify_icons .= '&nbsp;<a href="#" >' . Display::return_icon('pdf_na.png', get_lang('ExportToPDF'), '', ICON_SIZE_SMALL) . '</a>';
  215. }
  216. }
  217. if (empty($grade_model_id) || $grade_model_id == -1) {
  218. if ($cat->is_locked() && !api_is_platform_admin()) {
  219. $modify_icons .= Display::return_icon('edit_na.png', get_lang('Modify'), '', ICON_SIZE_SMALL);
  220. } else {
  221. $modify_icons .= '<a href="gradebook_edit_cat.php?' .'editcat=' . $cat->get_id() . '&cidReq=' .$cat->get_course_code() . '&id_session='.$cat->get_session_id().'">' .
  222. Display::return_icon(
  223. 'edit.png',
  224. get_lang('Modify'),
  225. '',
  226. ICON_SIZE_SMALL
  227. ) . '</a>';
  228. }
  229. }
  230. $modify_icons .= '<a href="gradebook_edit_all.php?selectcat=' .$cat->get_id() . '&cidReq=' . $cat->get_course_code() . '&id_session='.$cat->get_session_id().'">' .
  231. Display::return_icon(
  232. 'percentage.png',
  233. get_lang('EditAllWeights'),
  234. '',
  235. ICON_SIZE_SMALL
  236. ) . '</a>';
  237. $modify_icons .= '<a href="gradebook_flatview.php?selectcat=' .$cat->get_id() . '&cidReq=' . $cat->get_course_code() . '&id_session='.$cat->get_session_id(). '">' .
  238. Display::return_icon(
  239. 'stats.png',
  240. get_lang('FlatView'),
  241. '',
  242. ICON_SIZE_SMALL
  243. ) . '</a>';
  244. $modify_icons .= '&nbsp;<a href="' . api_get_self() .'?visiblecat=' . $cat->get_id() . '&' .$visibility_command . '=&selectcat=' . $selectcat .'&cidReq=' . $cat->get_course_code() . '&id_session='.$cat->get_session_id(). '">' .
  245. Display::return_icon(
  246. $visibility_icon . '.png',
  247. get_lang('Visible'),
  248. '',
  249. ICON_SIZE_SMALL
  250. ) . '</a>';
  251. //no move ability for root categories
  252. if ($cat->is_movable()) {
  253. /* $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?movecat=' . $cat->get_id() . '&amp;selectcat=' . $selectcat . ' &amp;cidReq='.$cat->get_course_code().'">
  254. <img src="../img/icons/22/move.png" border="0" title="' . get_lang('Move') . '" alt="" /></a>'; */
  255. } else {
  256. //$modify_icons .= '&nbsp;<img src="../img/deplacer_fichier_na.gif" border="0" title="' . get_lang('Move') . '" alt="" />';
  257. }
  258. if ($cat->is_locked() && !api_is_platform_admin()) {
  259. $modify_icons .= Display::return_icon('delete_na.png', get_lang('DeleteAll'), '', ICON_SIZE_SMALL);
  260. } else {
  261. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?deletecat=' . $cat->get_id() . '&selectcat=' . $selectcat . '&cidReq=' . $cat->get_course_code() . '&id_session='.$cat->get_session_id(). '" onclick="return confirmation();">' .
  262. Display::return_icon('delete.png', get_lang('DeleteAll'), '', ICON_SIZE_SMALL) . '</a>';
  263. }
  264. }
  265. return $modify_icons;
  266. }
  267. }
  268. /**
  269. * Builds the course or platform admin icons to edit an evaluation
  270. * @param Evaluation $eval evaluation object
  271. * @param int $selectcat id of selected category
  272. */
  273. public static function build_edit_icons_eval($eval, $selectcat)
  274. {
  275. $status = CourseManager::get_user_in_course_status(api_get_user_id(), api_get_course_id());
  276. $is_locked = $eval->is_locked();
  277. $eval->get_course_code();
  278. $cat = new Category();
  279. $message_eval = $cat->show_message_resource_delete($eval->get_course_code());
  280. if ($message_eval === false && api_is_allowed_to_edit(null, true)) {
  281. $visibility_icon = ($eval->is_visible() == 0) ? 'invisible' : 'visible';
  282. $visibility_command = ($eval->is_visible() == 0) ? 'set_visible' : 'set_invisible';
  283. if ($is_locked && !api_is_platform_admin()) {
  284. $modify_icons = Display::return_icon('edit_na.png', get_lang('Modify'), '', ICON_SIZE_SMALL);
  285. } else {
  286. $modify_icons = '<a href="gradebook_edit_eval.php?editeval=' . $eval->get_id() . '&cidReq=' . $eval->get_course_code() . '&id_session='.$eval->getSessionId(). '">' .
  287. Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL) . '</a>';
  288. }
  289. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?visibleeval=' . $eval->get_id() . '&' . $visibility_command . '=&selectcat=' . $selectcat . '&id_session='.$eval->getSessionId(). ' ">' .
  290. Display::return_icon($visibility_icon . '.png', get_lang('Visible'), '', ICON_SIZE_SMALL) . '</a>';
  291. if (api_is_allowed_to_edit(null, true)) {
  292. $modify_icons .= '&nbsp;<a href="gradebook_showlog_eval.php?visiblelog=' . $eval->get_id() . '&selectcat=' . $selectcat . ' &cidReq=' . $eval->get_course_code() . '&id_session='.$eval->getSessionId(). '">' .
  293. Display::return_icon('history.png', get_lang('GradebookQualifyLog'), '', ICON_SIZE_SMALL) . '</a>';
  294. }
  295. /*
  296. if ($locked_status == 0){
  297. $modify_icons .= "&nbsp;<a href=\"javascript:if (confirm('".addslashes(get_lang('AreYouSureToLockedTheEvaluation'))."')) { location.href='".api_get_self().'?lockedeval=' . $eval->get_id() . '&selectcat=' . $selectcat . ' &cidReq='.$eval->get_course_code()."'; }\">".Display::return_icon('unlock.png',get_lang('LockEvaluation'), array(), ICON_SIZE_SMALL)."</a>";
  298. } else {
  299. if (api_is_platform_admin()){
  300. $modify_icons .= "&nbsp;<a href=\"javascript:if (confirm('".addslashes(get_lang('AreYouSureToUnLockedTheEvaluation'))."')) { location.href='".api_get_self().'?lockedeval=' . $eval->get_id() . '&typelocked=&selectcat=' . $selectcat . ' &cidReq='.$eval->get_course_code()."';\">".Display::return_icon('lock.png',get_lang('UnLockEvaluation'), array(), ICON_SIZE_SMALL)."</a>";
  301. } else {
  302. $modify_icons .= '&nbsp;<img src="../img/locked_na.png" border="0" title="' . get_lang('TheEvaluationIsLocked') . '" alt="" />';
  303. }
  304. } */
  305. if ($is_locked && !api_is_platform_admin()) {
  306. $modify_icons .= '&nbsp;' . Display::return_icon('delete_na.png', get_lang('Delete'), '', ICON_SIZE_SMALL);
  307. } else {
  308. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?deleteeval=' . $eval->get_id() . '&selectcat=' . $selectcat . ' &cidReq=' . $eval->get_course_code() . '&id_session='.$eval->getSessionId(). '" onclick="return confirmation();">' . Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL) . '</a>';
  309. }
  310. return $modify_icons;
  311. }
  312. }
  313. /**
  314. * Builds the course or platform admin icons to edit a link
  315. * @param AbstractLink $link
  316. * @param int $selectcat id of selected category
  317. */
  318. public static function build_edit_icons_link($link, $selectcat)
  319. {
  320. $cat = new Category();
  321. $message_link = $cat->show_message_resource_delete($link->get_course_code());
  322. $is_locked = $link->is_locked();
  323. $modify_icons = null;
  324. if (!api_is_allowed_to_edit(null, true)) {
  325. return null;
  326. }
  327. if ($message_link === false) {
  328. $visibility_icon = ($link->is_visible() == 0) ? 'invisible' : 'visible';
  329. $visibility_command = ($link->is_visible() == 0) ? 'set_visible' : 'set_invisible';
  330. if ($is_locked && !api_is_platform_admin()) {
  331. $modify_icons = Display::return_icon('edit_na.png', get_lang('Modify'), '', ICON_SIZE_SMALL);
  332. } else {
  333. $modify_icons = '<a href="gradebook_edit_link.php?editlink=' . $link->get_id() . '&cidReq=' . $link->get_course_code() . '&id_session='.$link->get_session_id().'">' .
  334. Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL) . '</a>';
  335. }
  336. //$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>';
  337. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?visiblelink=' . $link->get_id() . '&' . $visibility_command . '=&selectcat=' . $selectcat . '&id_session='.$link->get_session_id(). ' ">' .
  338. Display::return_icon($visibility_icon . '.png', get_lang('Visible'), '', ICON_SIZE_SMALL) . '</a>';
  339. $modify_icons .= '&nbsp;<a href="gradebook_showlog_link.php?visiblelink=' . $link->get_id() . '&selectcat=' . $selectcat . '&cidReq=' . $link->get_course_code() . '&id_session='.$link->get_session_id(). '">' .
  340. Display::return_icon('history.png', get_lang('GradebookQualifyLog'), '', ICON_SIZE_SMALL) . '</a>';
  341. //If a work is added in a gradebook you can only delete the link in the work tool
  342. if ($is_locked && !api_is_platform_admin()) {
  343. $modify_icons .= '&nbsp;' . Display::return_icon('delete_na.png', get_lang('Delete'), '', ICON_SIZE_SMALL);
  344. } else {
  345. $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?deletelink=' . $link->get_id() . '&selectcat=' . $selectcat . ' &cidReq=' . $link->get_course_code() . '&id_session='.$link->get_session_id(). '" onclick="return confirmation();">' .
  346. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL) . '</a>';
  347. }
  348. return $modify_icons;
  349. }
  350. }
  351. /**
  352. * Checks if a resource is in the unique gradebook of a given course
  353. * @param string Course code
  354. * @param int Resource type (use constants defined in linkfactory.class.php)
  355. * @param int Resource ID in the corresponding tool
  356. * @param int Session ID (optional - 0 if not defined)
  357. * @return int false on error or link ID
  358. */
  359. public static function is_resource_in_course_gradebook($course_code, $resource_type, $resource_id, $session_id = 0)
  360. {
  361. $l = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  362. $course_code = Database::escape_string($course_code);
  363. $sql = "SELECT * FROM $l l
  364. WHERE
  365. course_code = '$course_code' AND
  366. type = ".(int)$resource_type . " AND
  367. ref_id = " . (int)$resource_id;
  368. $res = Database::query($sql);
  369. if (Database::num_rows($res) < 1) {
  370. return false;
  371. }
  372. $row = Database::fetch_array($res, 'ASSOC');
  373. return $row;
  374. }
  375. /**
  376. * Remove a resource from the unique gradebook of a given course
  377. * @param int Link/Resource ID
  378. * @return bool false on error, true on success
  379. */
  380. public static function get_resource_from_course_gradebook($link_id)
  381. {
  382. if (empty($link_id)) {
  383. return false;
  384. }
  385. // TODO find the corresponding category (the first one for this course, ordered by ID)
  386. $l = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  387. $sql = "SELECT * FROM $l WHERE id = " . (int) $link_id;
  388. $res = Database::query($sql);
  389. $row = array();
  390. if (Database::num_rows($res) > 0) {
  391. $row = Database::fetch_array($res, 'ASSOC');
  392. }
  393. return $row;
  394. }
  395. /**
  396. * Return the course id
  397. * @param int
  398. * @return String
  399. */
  400. public static function get_course_id_by_link_id($id_link)
  401. {
  402. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  403. $tbl_grade_links = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  404. $sql = 'SELECT c.id FROM ' . $course_table . ' c
  405. INNER JOIN ' . $tbl_grade_links . ' l
  406. ON c.code = l.course_code
  407. WHERE l.id=' . intval($id_link) . ' OR l.category_id=' . intval($id_link);
  408. $res = Database::query($sql);
  409. $array = Database::fetch_array($res, 'ASSOC');
  410. return $array['id'];
  411. }
  412. /**
  413. * @param $type
  414. * @return string
  415. */
  416. public static function get_table_type_course($type)
  417. {
  418. global $table_evaluated;
  419. return Database::get_course_table($table_evaluated[$type][0]);
  420. }
  421. /**
  422. * @param Category $cat
  423. * @param $users
  424. * @param $alleval
  425. * @param $alllinks
  426. * @param $params
  427. * @param null $mainCourseCategory
  428. * @return array
  429. */
  430. public static function get_printable_data($cat, $users, $alleval, $alllinks, $params, $mainCourseCategory = null)
  431. {
  432. $datagen = new FlatViewDataGenerator(
  433. $users,
  434. $alleval,
  435. $alllinks,
  436. $params,
  437. $mainCourseCategory
  438. );
  439. $offset = isset($_GET['offset']) ? $_GET['offset'] : '0';
  440. $offset = intval($offset);
  441. // step 2: generate rows: students
  442. $datagen->category = $cat;
  443. $count = (($offset + 10) > $datagen->get_total_items_count()) ? ($datagen->get_total_items_count() - $offset) : GRADEBOOK_ITEM_LIMIT;
  444. $header_names = $datagen->get_header_names($offset, $count, true);
  445. $data_array = $datagen->get_data(
  446. FlatViewDataGenerator :: FVDG_SORT_LASTNAME,
  447. 0,
  448. null,
  449. $offset,
  450. $count,
  451. true,
  452. true
  453. );
  454. $result = array();
  455. foreach ($data_array as $data) {
  456. $result[] = array_slice($data, 1);
  457. }
  458. $return = array($header_names, $result);
  459. return $return;
  460. }
  461. /**
  462. * XML-parser: handle character data
  463. */
  464. public static function character_data($parser, $data)
  465. {
  466. global $current_value;
  467. $current_value = $data;
  468. }
  469. /**
  470. * XML-parser: handle end of element
  471. */
  472. public static function element_end($parser, $data)
  473. {
  474. global $user;
  475. global $users;
  476. global $current_value;
  477. switch ($data) {
  478. case 'Result' :
  479. $users[] = $user;
  480. break;
  481. default :
  482. $user[$data] = $current_value;
  483. break;
  484. }
  485. }
  486. /**
  487. * XML-parser: handle start of element
  488. */
  489. public static function element_start($parser, $data)
  490. {
  491. global $user;
  492. global $current_tag;
  493. switch ($data) {
  494. case 'Result' :
  495. $user = array();
  496. break;
  497. default :
  498. $current_tag = $data;
  499. }
  500. }
  501. public static function overwritescore($resid, $importscore, $eval_max)
  502. {
  503. $result = Result :: load($resid);
  504. if ($importscore > $eval_max) {
  505. header('Location: gradebook_view_result.php?selecteval=' . Security::remove_XSS($_GET['selecteval']) . '&overwritemax=');
  506. exit;
  507. }
  508. $result[0]->set_score($importscore);
  509. $result[0]->save();
  510. unset($result);
  511. }
  512. /**
  513. * Read the XML-file
  514. * @param string $file Path to the XML-file
  515. * @return array All user information read from the file
  516. */
  517. public static function parse_xml_data($file)
  518. {
  519. global $current_tag;
  520. global $current_value;
  521. global $user;
  522. global $users;
  523. $users = array();
  524. $parser = xml_parser_create();
  525. xml_set_element_handler($parser, 'element_start', 'element_end');
  526. xml_set_character_data_handler($parser, "character_data");
  527. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
  528. xml_parse($parser, file_get_contents($file));
  529. xml_parser_free($parser);
  530. return $users;
  531. }
  532. /**
  533. * register user info about certificate
  534. * @param int The category id
  535. * @param int The user id
  536. * @param float The score obtained for certified
  537. * @param Datetime The date when you obtained the certificate
  538. * @return void()
  539. */
  540. public static function register_user_info_about_certificate($cat_id, $user_id, $score_certificate, $date_certificate)
  541. {
  542. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  543. $sql = 'SELECT COUNT(*) as count
  544. FROM ' . $table . ' gc
  545. WHERE gc.cat_id="' . intval($cat_id) . '" AND user_id="' . intval($user_id) . '" ';
  546. $rs_exist = Database::query($sql);
  547. $row = Database::fetch_array($rs_exist);
  548. if ($row['count'] == 0) {
  549. $params = [
  550. 'cat_id' => $cat_id,
  551. 'user_id' => $user_id,
  552. 'score_certificate' => $score_certificate,
  553. 'created_at' => $date_certificate
  554. ];
  555. Database::insert($table, $params);
  556. }
  557. }
  558. /**
  559. * Get date of user certificate
  560. * @param int $cat_id The category id
  561. * @param int $user_id The user id
  562. * @return Datetime The date when you obtained the certificate
  563. */
  564. public static function get_certificate_by_user_id($cat_id, $user_id)
  565. {
  566. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  567. $sql = 'SELECT * FROM ' . $table_certificate . '
  568. WHERE cat_id="' . intval($cat_id) . '" AND user_id="' . intval($user_id) . '"';
  569. $result = Database::query($sql);
  570. $row = Database::fetch_array($result, 'ASSOC');
  571. return $row;
  572. }
  573. /**
  574. * Get list of users certificates
  575. * @param int $cat_id The category id
  576. * @param array $userList Only users in this list
  577. * @return array
  578. */
  579. public static function get_list_users_certificates($cat_id = null, $userList = array())
  580. {
  581. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  582. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  583. $sql = 'SELECT DISTINCT u.user_id, u.lastname, u.firstname, u.username
  584. FROM ' . $table_user . ' u
  585. INNER JOIN ' . $table_certificate . ' gc
  586. ON u.user_id=gc.user_id ';
  587. if (!is_null($cat_id) && $cat_id > 0) {
  588. $sql.=' WHERE cat_id=' . intval($cat_id);
  589. }
  590. if (!empty($userList)) {
  591. $userList = array_map('intval', $userList);
  592. $userListCondition = implode("','", $userList);
  593. $sql .= " AND u.user_id IN ('$userListCondition')";
  594. }
  595. $sql.=' ORDER BY u.firstname';
  596. $rs = Database::query($sql);
  597. $list_users = array();
  598. while ($row = Database::fetch_array($rs)) {
  599. $list_users[] = $row;
  600. }
  601. return $list_users;
  602. }
  603. /**
  604. * Gets the certificate list by user id
  605. * @param int $user_id The user id
  606. * @param int $cat_id The category id
  607. * @return array
  608. */
  609. public static function get_list_gradebook_certificates_by_user_id($user_id, $cat_id = null)
  610. {
  611. $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  612. $sql = 'SELECT gc.score_certificate, gc.created_at, gc.path_certificate, gc.cat_id, gc.user_id, gc.id
  613. FROM ' . $table_certificate . ' gc
  614. WHERE gc.user_id="' . intval($user_id) . '" ';
  615. if (!is_null($cat_id) && $cat_id > 0) {
  616. $sql.=' AND cat_id=' . intval($cat_id);
  617. }
  618. $rs = Database::query($sql);
  619. $list_certificate = array();
  620. while ($row = Database::fetch_array($rs)) {
  621. $list_certificate[] = $row;
  622. }
  623. return $list_certificate;
  624. }
  625. /**
  626. * @param $user_id
  627. * @param $course_code
  628. * @param bool $is_preview
  629. * @param bool $hide_print_button
  630. * @return array
  631. */
  632. public static function get_user_certificate_content($user_id, $course_code, $is_preview = false, $hide_print_button = false)
  633. {
  634. // Generate document HTML
  635. $content_html = DocumentManager::replace_user_info_into_html($user_id, $course_code, $is_preview);
  636. $new_content_html = null;
  637. $variables = null;
  638. $contentHead = null;
  639. if (isset($content_html['content'])) {
  640. $new_content = explode('</head>', $content_html['content']);
  641. $new_content_html = $new_content[1];
  642. $contentHead = $new_content[0];
  643. }
  644. if (isset($content_html['variables'])) {
  645. $variables = $content_html['variables'];
  646. }
  647. $path_image = api_get_path(WEB_COURSE_PATH) . api_get_course_path($course_code) . '/document/images/gallery';
  648. $new_content_html = str_replace('../images/gallery', $path_image, $new_content_html);
  649. $path_image_in_default_course = api_get_path(WEB_CODE_PATH) . 'default_course_document';
  650. $new_content_html = str_replace('/main/default_course_document', $path_image_in_default_course, $new_content_html);
  651. $new_content_html = str_replace(SYS_CODE_PATH . 'img/', api_get_path(WEB_IMG_PATH), $new_content_html);
  652. //add print header
  653. if ($hide_print_button == false) {
  654. $print = '<style media="print" type="text/css">#print_div {visibility:hidden;}</style>';
  655. $print .= '<a href="javascript:window.print();" style="float:right; padding:4px;" id="print_div"><img src="' . api_get_path(WEB_CODE_PATH) . 'img/printmgr.gif" alt="' . get_lang('Print') . '" /> ' . get_lang('Print') . '</a>';
  656. }
  657. // Add header
  658. $new_content_html = $contentHead. $print . '</head>' . $new_content_html;
  659. return array(
  660. 'content' => $new_content_html,
  661. 'variables' => $variables
  662. );
  663. }
  664. /**
  665. * @param null $course_code
  666. * @param int $gradebook_model_id
  667. * @return mixed
  668. */
  669. public static function create_default_course_gradebook($course_code = null, $gradebook_model_id = 0)
  670. {
  671. if (api_is_allowed_to_edit(true, true)) {
  672. if (!isset($course_code) || empty($course_code)) {
  673. $course_code = api_get_course_id();
  674. }
  675. $session_id = api_get_session_id();
  676. $t = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  677. $sql = "SELECT * FROM $t WHERE course_code = '" . Database::escape_string($course_code) . "' ";
  678. if (!empty($session_id)) {
  679. $sql .= " AND session_id = " . (int) $session_id;
  680. } else {
  681. $sql .= " AND (session_id IS NULL OR session_id = 0) ";
  682. }
  683. $sql .= " ORDER BY id";
  684. $res = Database::query($sql);
  685. if (Database::num_rows($res) < 1) {
  686. //there is no unique category for this course+session combination,
  687. $cat = new Category();
  688. if (!empty($session_id)) {
  689. $my_session_id = api_get_session_id();
  690. $s_name = api_get_session_name($my_session_id);
  691. $cat->set_name($course_code . ' - ' . get_lang('Session') . ' ' . $s_name);
  692. $cat->set_session_id($session_id);
  693. } else {
  694. $cat->set_name($course_code);
  695. }
  696. $cat->set_course_code($course_code);
  697. $cat->set_description(null);
  698. $cat->set_user_id(api_get_user_id());
  699. $cat->set_parent_id(0);
  700. $default_weight_setting = api_get_setting('gradebook_default_weight');
  701. $default_weight = isset($default_weight_setting) && !empty($default_weight_setting) ? $default_weight_setting : 100;
  702. $cat->set_weight($default_weight);
  703. $cat->set_grade_model_id($gradebook_model_id);
  704. $cat->set_certificate_min_score(75);
  705. $cat->set_visible(0);
  706. $cat->add();
  707. $category_id = $cat->get_id();
  708. unset($cat);
  709. } else {
  710. $row = Database::fetch_array($res);
  711. $category_id = $row['id'];
  712. }
  713. }
  714. return $category_id;
  715. }
  716. /**
  717. * @param FormValidator $form
  718. */
  719. public static function load_gradebook_select_in_tool($form)
  720. {
  721. $course_code = api_get_course_id();
  722. $session_id = api_get_session_id();
  723. self::create_default_course_gradebook();
  724. // Cat list
  725. $all_categories = Category :: load(null, null, $course_code, null, null, $session_id, false);
  726. $select_gradebook = $form->addElement('select', 'category_id', get_lang('SelectGradebook'));
  727. if (!empty($all_categories)) {
  728. foreach ($all_categories as $my_cat) {
  729. if ($my_cat->get_course_code() == api_get_course_id()) {
  730. $grade_model_id = $my_cat->get_grade_model_id();
  731. if (empty($grade_model_id)) {
  732. if ($my_cat->get_parent_id() == 0) {
  733. //$default_weight = $my_cat->get_weight();
  734. $select_gradebook->addoption(get_lang('Default'), $my_cat->get_id());
  735. $cats_added[] = $my_cat->get_id();
  736. } else {
  737. $select_gradebook->addoption($my_cat->get_name(), $my_cat->get_id());
  738. $cats_added[] = $my_cat->get_id();
  739. }
  740. } else {
  741. $select_gradebook->addoption(get_lang('Select'), 0);
  742. }
  743. }
  744. }
  745. }
  746. }
  747. /**
  748. * @param FlatViewTable $flatviewtable
  749. * @param Category $cat
  750. * @param $users
  751. * @param $alleval
  752. * @param $alllinks
  753. * @param array $params
  754. * @param null $mainCourseCategory
  755. */
  756. public static function export_pdf_flatview($flatviewtable, $cat, $users, $alleval, $alllinks, $params = array(), $mainCourseCategory = null)
  757. {
  758. // Getting data
  759. $printable_data = self::get_printable_data($cat[0], $users, $alleval, $alllinks, $params, $mainCourseCategory);
  760. // HTML report creation first
  761. $course_code = trim($cat[0]->get_course_code());
  762. $displayscore = ScoreDisplay :: instance();
  763. $customdisplays = $displayscore->get_custom_score_display_settings();
  764. $total = array();
  765. if (is_array($customdisplays) && count(($customdisplays))) {
  766. foreach ($customdisplays as $custom) {
  767. $total[$custom['display']] = 0;
  768. }
  769. $user_results = $flatviewtable->datagen->get_data_to_graph2();
  770. foreach ($user_results as $user_result) {
  771. $total[$user_result[count($user_result) - 1][1]]++;
  772. }
  773. }
  774. $parent_id = $cat[0]->get_parent_id();
  775. if (isset($cat[0]) && isset($parent_id)) {
  776. if ($parent_id == 0) {
  777. $grade_model_id = $cat[0]->get_grade_model_id();
  778. } else {
  779. $parent_cat = Category::load($parent_id);
  780. $grade_model_id = $parent_cat[0]->get_grade_model_id();
  781. }
  782. }
  783. $use_grade_model = true;
  784. if (empty($grade_model_id) || $grade_model_id == -1) {
  785. $use_grade_model = false;
  786. }
  787. if ($use_grade_model) {
  788. if ($parent_id == 0) {
  789. $title = api_strtoupper(get_lang('Average')) . '<br />' . get_lang('Detailed');
  790. } else {
  791. $title = api_strtoupper(get_lang('Average')) . '<br />' . $cat[0]->get_description() . ' - (' . $cat[0]->get_name() . ')';
  792. }
  793. } else {
  794. if ($parent_id == 0) {
  795. $title = api_strtoupper(get_lang('Average')) . '<br />' . get_lang('Detailed');
  796. } else {
  797. $title = api_strtoupper(get_lang('Average'));
  798. }
  799. }
  800. $columns = count($printable_data[0]);
  801. $has_data = is_array($printable_data[1]) && count($printable_data[1]) > 0;
  802. $table = new HTML_Table(array('class' => 'data_table'));
  803. $row = 0;
  804. $column = 0;
  805. $table->setHeaderContents($row, $column, get_lang('NumberAbbreviation'));
  806. $column++;
  807. foreach ($printable_data[0] as $printable_data_cell) {
  808. $printable_data_cell = strip_tags($printable_data_cell);
  809. $table->setHeaderContents($row, $column, $printable_data_cell);
  810. $column++;
  811. }
  812. $row++;
  813. if ($has_data) {
  814. $counter = 1;
  815. foreach ($printable_data[1] as &$printable_data_row) {
  816. $column = 0;
  817. $table->setCellContents($row, $column, $counter);
  818. $table->updateCellAttributes($row, $column, 'align="center"');
  819. $column++;
  820. $counter++;
  821. foreach ($printable_data_row as $key => &$printable_data_cell) {
  822. $attributes = array();
  823. $attributes['align'] = 'center';
  824. $attributes['style'] = null;
  825. if ($key === 'name') {
  826. $attributes['align'] = 'left';
  827. }
  828. if ($key === 'total') {
  829. $attributes['style'] = 'font-weight:bold';
  830. }
  831. $table->setCellContents($row, $column, $printable_data_cell);
  832. $table->updateCellAttributes($row, $column, $attributes);
  833. $column++;
  834. }
  835. $table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
  836. $row++;
  837. }
  838. } else {
  839. $column = 0;
  840. $table->setCellContents($row, $column, get_lang('NoResults'));
  841. $table->updateCellAttributes($row, $column, 'colspan="' . $columns . '" align="center" class="row_odd"');
  842. }
  843. $pdfParams = array(
  844. 'filename' => get_lang('FlatView') . '_' . api_get_utc_datetime(),
  845. 'pdf_title' => $title,
  846. 'course_code' => $course_code,
  847. 'add_signatures' => true
  848. );
  849. $page_format = $params['orientation'] == 'landscape' ? 'A4-L' : 'A4';
  850. $pdf = new PDF($page_format, $page_format, $pdfParams);
  851. $pdf->html_to_pdf_with_template($flatviewtable->return_table());
  852. // Default
  853. //$pdf->html_to_pdf_with_template($table->toHtml());
  854. exit;
  855. }
  856. /**
  857. * @param array $list_values
  858. * @return string
  859. */
  860. public static function score_badges($list_values)
  861. {
  862. $counter = 1;
  863. $badges = array();
  864. foreach ($list_values as $value) {
  865. $class = 'info';
  866. if ($counter == 1) {
  867. $class = 'success';
  868. }
  869. $counter++;
  870. $badges[] = Display::badge($value, $class);
  871. }
  872. return Display::badge_group($badges);
  873. }
  874. /**
  875. * returns users within a course given by param
  876. * @param string $courseCode
  877. */
  878. public static function get_users_in_course($courseCode)
  879. {
  880. $tbl_course_user = Database:: get_main_table(TABLE_MAIN_COURSE_USER);
  881. $tbl_session_course_user = Database:: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  882. $tbl_user = Database:: get_main_table(TABLE_MAIN_USER);
  883. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname ASC' : ' ORDER BY lastname, firstname ASC';
  884. $current_session = api_get_session_id();
  885. $courseCode = Database::escape_string($courseCode);
  886. $courseInfo = api_get_course_info($courseCode);
  887. $courseId = $courseInfo['real_id'];
  888. if (!empty($current_session)) {
  889. $sql = "SELECT user.user_id, user.username, lastname, firstname, official_code
  890. FROM $tbl_session_course_user as scru, $tbl_user as user
  891. WHERE
  892. scru.user_id = user.user_id AND
  893. scru.status=0 AND
  894. scru.c_id='$courseId' AND
  895. session_id ='$current_session'
  896. $order_clause
  897. ";
  898. } else {
  899. $sql = 'SELECT user.user_id, user.username, lastname, firstname, official_code
  900. FROM '.$tbl_course_user.' as course_rel_user, '.$tbl_user.' as user
  901. WHERE
  902. course_rel_user.user_id=user.user_id AND
  903. course_rel_user.status='.STUDENT.' AND
  904. course_rel_user.c_id = "'.$courseId.'" '.
  905. $order_clause;
  906. }
  907. $result = Database::query($sql);
  908. return self::get_user_array_from_sql_result($result);
  909. }
  910. /**
  911. * @param $result
  912. * @return array
  913. */
  914. public static function get_user_array_from_sql_result($result)
  915. {
  916. $a_students = array();
  917. while ($user = Database::fetch_array($result)) {
  918. if (!array_key_exists($user['user_id'], $a_students)) {
  919. $a_current_student = array ();
  920. $a_current_student[] = $user['user_id'];
  921. $a_current_student[] = $user['username'];
  922. $a_current_student[] = $user['lastname'];
  923. $a_current_student[] = $user['firstname'];
  924. $a_current_student[] = $user['official_code'];
  925. $a_students['STUD'.$user['user_id']] = $a_current_student;
  926. }
  927. }
  928. return $a_students;
  929. }
  930. /**
  931. * @param array $evals
  932. * @param array $links
  933. * @return array
  934. */
  935. public static function get_all_users($evals = array(), $links = array())
  936. {
  937. $coursecodes = array();
  938. $users = array();
  939. // By default add all user in course
  940. $coursecodes[api_get_course_id()] = '1';
  941. $users = GradebookUtils::get_users_in_course(api_get_course_id());
  942. foreach ($evals as $eval) {
  943. $coursecode = $eval->get_course_code();
  944. // evaluation in course
  945. if (isset($coursecode) && !empty($coursecode)) {
  946. if (!array_key_exists($coursecode, $coursecodes)) {
  947. $coursecodes[$coursecode] = '1';
  948. $users = array_merge($users, GradebookUtils::get_users_in_course($coursecode));
  949. }
  950. } else {
  951. // course independent evaluation
  952. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  953. $tbl_res = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  954. $sql = 'SELECT user.user_id, lastname, firstname, user.official_code
  955. FROM '.$tbl_res.' as res, '.$tbl_user.' as user
  956. WHERE
  957. res.evaluation_id = '.intval($eval->get_id()).' AND
  958. res.user_id = user.user_id
  959. ';
  960. $sql .= ' ORDER BY lastname, firstname';
  961. if (api_is_western_name_order()) {
  962. $sql .= ' ORDER BY firstname, lastname';
  963. }
  964. $result = Database::query($sql);
  965. $users = array_merge($users, GradebookUtils::get_user_array_from_sql_result($result));
  966. }
  967. }
  968. foreach ($links as $link) {
  969. // links are always in a course
  970. $coursecode = $link->get_course_code();
  971. if (!array_key_exists($coursecode,$coursecodes)) {
  972. $coursecodes[$coursecode] = '1';
  973. $users = array_merge($users, GradebookUtils::get_users_in_course($coursecode));
  974. }
  975. }
  976. return $users;
  977. }
  978. /**
  979. * Search students matching a given last name and/or first name
  980. * @author Bert Steppé
  981. */
  982. public static function find_students($mask= '')
  983. {
  984. // students shouldn't be here // don't search if mask empty
  985. if (!api_is_allowed_to_edit() || empty ($mask)) {
  986. return null;
  987. }
  988. $mask = Database::escape_string($mask);
  989. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  990. $tbl_cru = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  991. $sql = 'SELECT DISTINCT user.user_id, user.lastname, user.firstname, user.email, user.official_code
  992. FROM ' . $tbl_user . ' user';
  993. if (!api_is_platform_admin()) {
  994. $sql .= ', ' . $tbl_cru . ' cru';
  995. }
  996. $sql .= ' WHERE user.status = ' . STUDENT;
  997. $sql .= ' AND (user.lastname LIKE '."'%" . $mask . "%'";
  998. $sql .= ' OR user.firstname LIKE '."'%" . $mask . "%')";
  999. if (!api_is_platform_admin()) {
  1000. $sql .= ' AND user.user_id = cru.user_id AND
  1001. cru.relation_type <> '.COURSE_RELATION_TYPE_RRHH.' AND
  1002. cru.c_id in (
  1003. SELECT c_id FROM '.$tbl_cru . '
  1004. WHERE
  1005. user_id = ' . api_get_user_id() . ' AND
  1006. status = ' . COURSEMANAGER . '
  1007. )
  1008. ';
  1009. }
  1010. $sql .= ' ORDER BY lastname, firstname';
  1011. if (api_is_western_name_order()) {
  1012. $sql .= ' ORDER BY firstname, lastname';
  1013. }
  1014. $result = Database::query($sql);
  1015. $users = Database::store_result($result);
  1016. return $users;
  1017. }
  1018. /**
  1019. * @param int $linkId
  1020. * @param float $weight
  1021. */
  1022. public static function updateLinkWeight($linkId, $name, $weight)
  1023. {
  1024. $linkId = intval($linkId);
  1025. $weight = floatval($weight);
  1026. $course_id = api_get_course_int_id();
  1027. AbstractLink::add_link_log($linkId, $name);
  1028. $table_link = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  1029. $table_evaluation = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  1030. $tbl_forum_thread = Database:: get_course_table(TABLE_FORUM_THREAD);
  1031. $tbl_work = Database:: get_course_table(TABLE_STUDENT_PUBLICATION);
  1032. $tbl_attendance = Database:: get_course_table(TABLE_ATTENDANCE);
  1033. $sql = 'UPDATE '.$table_link.' SET weight = '."'".Database::escape_string($weight)."'".'
  1034. WHERE id = '.$linkId;
  1035. Database::query($sql);
  1036. // Update weight for attendance
  1037. $sql = 'SELECT ref_id FROM '.$table_link.'
  1038. WHERE id = '.$linkId.' AND type='.LINK_ATTENDANCE;
  1039. $rs_attendance = Database::query($sql);
  1040. if (Database::num_rows($rs_attendance) > 0) {
  1041. $row_attendance = Database::fetch_array($rs_attendance);
  1042. $sql = 'UPDATE '.$tbl_attendance.' SET attendance_weight ='.$weight.'
  1043. WHERE c_id = '.$course_id.' AND id = '.intval($row_attendance['ref_id']);
  1044. Database::query($sql);
  1045. }
  1046. // Update weight into forum thread
  1047. $sql = 'UPDATE '.$tbl_forum_thread.' SET thread_weight='.$weight.'
  1048. WHERE
  1049. c_id = '.$course_id.' AND
  1050. thread_id = (
  1051. SELECT ref_id FROM '.$table_link.'
  1052. WHERE id='.$linkId.' AND type='.LINK_FORUM_THREAD.'
  1053. )
  1054. ';
  1055. Database::query($sql);
  1056. //Update weight into student publication(work)
  1057. $sql = 'UPDATE '.$tbl_work.' SET weight='.$weight.'
  1058. WHERE
  1059. c_id = '.$course_id.' AND id = (
  1060. SELECT ref_id FROM '.$table_link.'
  1061. WHERE id='.$linkId.' AND type = '.LINK_STUDENTPUBLICATION.'
  1062. ) ';
  1063. Database::query($sql);
  1064. }
  1065. /**
  1066. * @param int $id
  1067. * @param float $weight
  1068. */
  1069. public static function updateEvaluationWeight($id, $weight)
  1070. {
  1071. $table_evaluation = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  1072. $id = intval($id);
  1073. $evaluation = new Evaluation();
  1074. $evaluation->add_evaluation_log($id);
  1075. $sql = 'UPDATE '.$table_evaluation.'
  1076. SET weight = '."'".Database::escape_string($weight)."'".'
  1077. WHERE id = '.$id;
  1078. Database::query($sql);
  1079. }
  1080. /**
  1081. *
  1082. * Get the achieved certificates for a user in courses
  1083. * @param int $userId The user id
  1084. * @param type $includeNonPublicCertificates Whether include the non-plublic certificates
  1085. * @return array
  1086. */
  1087. public static function getUserCertificatesInCourses($userId, $includeNonPublicCertificates = true)
  1088. {
  1089. $userId = intval($userId);
  1090. $courseList = [];
  1091. $courses = CourseManager::get_courses_list_by_user_id($userId);
  1092. foreach ($courses as $course) {
  1093. if (!$includeNonPublicCertificates) {
  1094. $allowPublicCertificates = api_get_course_setting('allow_public_certificates', $course['code']);
  1095. if (empty($allowPublicCertificates)) {
  1096. continue;
  1097. }
  1098. }
  1099. $courseGradebookCategory = Category::load(null, null, $course['code']);
  1100. if (empty($courseGradebookCategory)) {
  1101. continue;
  1102. }
  1103. $courseGradebookId = $courseGradebookCategory[0]->get_id();
  1104. $certificateInfo = GradebookUtils::get_certificate_by_user_id($courseGradebookId, $userId);
  1105. if (empty($certificateInfo)) {
  1106. continue;
  1107. }
  1108. $courseInfo = api_get_course_info_by_id($course['real_id']);
  1109. $courseList[] = [
  1110. 'course' => $courseInfo['title'],
  1111. 'score' => $certificateInfo['score_certificate'],
  1112. 'date' => api_format_date($certificateInfo['created_at'], DATE_FORMAT_SHORT),
  1113. 'link' => api_get_path(WEB_PATH) . "certificates/index.php?id={$certificateInfo['id']}"
  1114. ];
  1115. }
  1116. return $courseList;
  1117. }
  1118. /**
  1119. * Get the achieved certificates for a user in course sessions
  1120. * @param int $userId The user id
  1121. * @param type $includeNonPublicCertificates Whether include the non-plublic certificates
  1122. * @return array
  1123. */
  1124. public static function getUserCertificatesInSessions($userId, $includeNonPublicCertificates = true)
  1125. {
  1126. $userId = intval($userId);
  1127. $sessionList = [];
  1128. $sessions = SessionManager::get_sessions_by_user($userId);
  1129. foreach ($sessions as $session) {
  1130. if (empty($session['courses'])) {
  1131. continue;
  1132. }
  1133. $sessionCourses = SessionManager::get_course_list_by_session_id($session['session_id']);
  1134. foreach ($sessionCourses as $course) {
  1135. if (!$includeNonPublicCertificates) {
  1136. $allowPublicCertificates = api_get_course_setting('allow_public_certificates', $course['code']);
  1137. if (empty($allowPublicCertificates)) {
  1138. continue;
  1139. }
  1140. }
  1141. $courseGradebookCategory = Category::load(
  1142. null,
  1143. null,
  1144. $course['code'],
  1145. null,
  1146. null,
  1147. $session['session_id']
  1148. );
  1149. if (empty($courseGradebookCategory)) {
  1150. continue;
  1151. }
  1152. $courseGradebookId = $courseGradebookCategory[0]->get_id();
  1153. $certificateInfo = GradebookUtils::get_certificate_by_user_id($courseGradebookId, $userId);
  1154. if (empty($certificateInfo)) {
  1155. continue;
  1156. }
  1157. $sessionList[] = [
  1158. 'session' => $session['session_name'],
  1159. 'course' => $course['title'],
  1160. 'score' => $certificateInfo['score_certificate'],
  1161. 'date' => api_format_date($certificateInfo['created_at'], DATE_FORMAT_SHORT),
  1162. 'link' => api_get_path(WEB_PATH) . "certificates/index.php?id={$certificateInfo['id']}"
  1163. ];
  1164. }
  1165. }
  1166. return $sessionList;
  1167. }
  1168. /**
  1169. * @param int $userId
  1170. * @param int $categoryId
  1171. * @param bool $saveToFile
  1172. * @param bool $saveToHtmlFile
  1173. *
  1174. * @return string
  1175. */
  1176. public static function generateTable($userId, $categoryId, $saveToFile = false, $saveToHtmlFile = false)
  1177. {
  1178. $courseInfo = api_get_course_info();
  1179. $userInfo = api_get_user_info($userId);
  1180. $cats = Category::load($categoryId, null, null, null, null, null, false);
  1181. $cat = $cats[0];
  1182. $allcat = $cats[0]->get_subcategories(
  1183. $userId,
  1184. api_get_course_id(),
  1185. api_get_session_id()
  1186. );
  1187. $alleval = $cats[0]->get_evaluations($userId);
  1188. $alllink = $cats[0]->get_links($userId);
  1189. $gradebooktable = new GradebookTable(
  1190. $cat,
  1191. $allcat,
  1192. $alleval,
  1193. $alllink,
  1194. null, // params
  1195. true, // $exportToPdf
  1196. false, // showteacher
  1197. $userId
  1198. );
  1199. if (api_is_allowed_to_edit()) {
  1200. $gradebooktable->td_attributes = [
  1201. 4 => 'class=centered'
  1202. ];
  1203. } else {
  1204. $gradebooktable->td_attributes = [
  1205. 3 => 'class=centered',
  1206. 4 => 'class=centered',
  1207. 5 => 'class=centered',
  1208. 6 => 'class=centered',
  1209. 7 => 'class=centered'
  1210. ];
  1211. }
  1212. $table = $gradebooktable->return_table();
  1213. $graph = $gradebooktable->getGraph();
  1214. $sessionName = api_get_session_name(api_get_session_id());
  1215. $sessionName = !empty($sessionName) ? " - $sessionName" : '';
  1216. $params = array(
  1217. //'filename' => get_lang('FlatView') . '_' . api_get_utc_datetime(),
  1218. 'pdf_title' => $courseInfo['title'].$sessionName,
  1219. 'course_code' => api_get_course_id(),
  1220. 'session_info' => api_get_session_info(api_get_session_id()),
  1221. 'add_signatures' => false,
  1222. 'student_info' => $userInfo,
  1223. 'show_grade_generated_date' => true,
  1224. 'show_real_course_teachers' => true
  1225. );
  1226. $file = api_get_path(SYS_ARCHIVE_PATH).uniqid().'.html';
  1227. $content =
  1228. $table.
  1229. $graph.
  1230. '<br />'.get_lang('Feedback').'<br />
  1231. <textarea rows="5" cols="100" ></textarea>';
  1232. $pdf = new PDF('A4', $params['orientation'], $params);
  1233. $result = $pdf->html_to_pdf_with_template(
  1234. $content,
  1235. $saveToFile,
  1236. $saveToHtmlFile
  1237. );
  1238. if ($saveToHtmlFile) {
  1239. file_put_contents($file, $result);
  1240. return $file;
  1241. }
  1242. return $file;
  1243. }
  1244. }