GradebookUtils.php 54 KB

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