PageRenderTime 76ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/locallib.php

https://github.com/ccle/Moodle-Flashcard
PHP | 557 lines | 383 code | 62 blank | 112 comment | 76 complexity | 79929e4b16346e3dd55eb50eb46579c3 MD5 | raw file
  1. <?php
  2. /* @var $DB mysqli_native_moodle_database */
  3. /* @var $OUTPUT core_renderer */
  4. /* @var $PAGE moodle_page */
  5. ?>
  6. <?php
  7. /**
  8. * internal library of functions and constants for module flashcard
  9. * @package mod-flashcard
  10. * @category mod
  11. * @author Gustav Delius
  12. * @contributors Valery Fremaux
  13. * @version Moodle 2.0
  14. */
  15. /**
  16. * Includes and requires
  17. */
  18. /**
  19. *
  20. */
  21. define('FLASHCARD_MEDIA_TEXT', 0);
  22. define('FLASHCARD_MEDIA_IMAGE', 1);
  23. define('FLASHCARD_MEDIA_SOUND', 2);
  24. define('FLASHCARD_MEDIA_IMAGE_AND_SOUND', 3);
  25. define('FLASHCARD_CARDS_PER_PAGE', 10);
  26. /**
  27. * computes the last accessed date for a deck as the oldest card being in the deck
  28. * @param reference $flashcard the flashcard object
  29. * @param int $deck the deck number
  30. * @param int $userid the user the deck belongs to
  31. * @uses $USER for setting default user
  32. * @uses $CFG, $DB
  33. */
  34. function flashcard_get_lastaccessed(&$flashcard, $deck, $userid = 0) {
  35. global $USER, $CFG, $DB;
  36. if ($userid == 0) $userid = $USER->id;
  37. $sql = "
  38. SELECT
  39. MIN(lastaccessed) as lastaccessed
  40. FROM
  41. {flashcard_card}
  42. WHERE
  43. flashcardid = ? AND
  44. userid = ? AND
  45. deck = ?
  46. ";
  47. $rec = $DB->get_record_sql($sql, array($flashcard->id, $userid, $deck));
  48. return $rec->lastaccessed;
  49. }
  50. /**
  51. * prints a deck depending on deck status
  52. * @param reference $cm the coursemodule
  53. * @param int $deck the deck number
  54. * @uses $CFG
  55. */
  56. function flashcard_print_deck(&$cm, $deck) {
  57. global $CFG;
  58. if ($deck == 0) {
  59. echo "<img src=\"{$CFG->wwwroot}/mod/flashcard/pix/emptydeck.jpg\"/>";
  60. }
  61. if ($deck > 0) {
  62. echo "<a href=\"view.php?view=play&amp;id={$cm->id}&amp;deck={$deck}&amp;what=initialize\" title=\"" . get_string('playwithme',
  63. 'flashcard') . "\"><img src=\"{$CFG->wwwroot}/mod/flashcard/pix/enableddeck.jpg\"/></a>";
  64. }
  65. if ($deck < 0) {
  66. $deck = -$deck;
  67. echo "<a href=\"view.php?view=play&amp;id={$cm->id}&amp;deck={$deck}&amp;what=initialize\" title=\"" . get_string('reinforce',
  68. 'flashcard') . "\"><img src=\"{$CFG->wwwroot}/mod/flashcard/pix/disableddeck.jpg\"/></a>";
  69. }
  70. }
  71. /**
  72. * prints the deck status for use in teacher's overview
  73. * @param reference $flashcard the flashcard object
  74. * @param int $userid the user for which printing status
  75. * @param object $status a status object to be filled by the function
  76. * @param boolean $return if true, returns the produced HTML, elsewhere prints it.
  77. * @uses $CFG
  78. */
  79. function flashcard_print_deck_status(&$flashcard, $userid, &$status, $return) {
  80. global $CFG, $OUTPUT;
  81. $str = '';
  82. $str = "<table width=\"100%\"><tr valign=\"bottom\"><td width=\"30%\" align=\"center\">";
  83. // print for deck 1
  84. if ($status->decks[0]->count) {
  85. $image = ($status->decks[0]->reactivate) ? 'topenabled' : 'topdisabled';
  86. $height = $status->decks[0]->count * 3;
  87. $str .= "<table cellspacing=\"2\"><tr><td><div style=\"padding-bottom: {$height}px\" class=\"graphdeck\" align=\"top\"><img src=\"{$CFG->wwwroot}/mod/flashcard/pix/{$image}.png\" title=\"" . get_string('cardsindeck',
  88. 'flashcard', $status->decks[0]->count) . "\"/></div></td><td>";
  89. $dayslateness = floor((time() - $status->decks[0]->lastaccess) / DAYSECS);
  90. // echo "late 1 : $dayslateness";
  91. $timetoreview = round(max(0,
  92. ($status->decks[0]->lastaccess + ($flashcard->deck1_delay * HOURSECS) - time()) / DAYSECS));
  93. $strtimetoreview = get_string('timetoreview', 'flashcard', $timetoreview);
  94. for ($i = 0; $i < min($dayslateness, floor($flashcard->deck1_delay / 24)); $i++) {
  95. $str .= "<img src=\"{$CFG->wwwroot}/mod/flashcard/pix/clock.png\" valign=\"bottom\" title=\"$strtimetoreview\" />";
  96. }
  97. if ($dayslateness < $flashcard->deck1_delay / 24) {
  98. for (; $i < $flashcard->deck1_delay / 24; $i++) {
  99. $str .= "<img src=\"{$CFG->wwwroot}/mod/flashcard/pix/shadowclock.png\" valign=\"bottom\" title=\"$strtimetoreview\" />";
  100. }
  101. } elseif ($dayslateness > $flashcard->deck1_delay / 24) {
  102. // Deck 1 has no release limit as cards can stay here as long as not viewed.
  103. for ($i = 0; $i < min($dayslateness - floor($flashcard->deck1_delay / 24), 4); $i++) {
  104. $str .= "<img src=\"{$CFG->wwwroot}/mod/flashcard/pix/overtime.png\" valign=\"bottom\" />";
  105. }
  106. }
  107. $str .= '</td></tr></table>';
  108. } else {
  109. $str .= "<div height=\"12px\" align=\"top\"><img src=\"{$CFG->wwwroot}/mod/flashcard/pix/topempty.png\" /></div>";
  110. }
  111. $str .= "</td><td>" . $OUTPUT->pix_icon('a/r_breadcrumb', 'right breadcrumb icon') . "</td><td width=\"30%\" align=\"center\">";
  112. // print for deck 2
  113. if ($status->decks[1]->count) {
  114. $image = ($status->decks[1]->reactivate) ? 'topenabled' : 'topdisabled';
  115. $height = $status->decks[1]->count * 3;
  116. $str .= "<table cellspacing=\"2\"><tr><td><div style=\"padding-bottom: {$height}px\" class=\"graphdeck\" align=\"top\"><img src=\"{$CFG->wwwroot}/mod/flashcard/pix/{$image}.png\" title=\"" . get_string('cardsindeck',
  117. 'flashcard', $status->decks[1]->count) . "\"/></div></td><td>";
  118. $dayslateness = floor((time() - $status->decks[1]->lastaccess) / DAYSECS);
  119. // echo "late 2 : $dayslateness ";
  120. $timetoreview = round(max(0,
  121. ($status->decks[1]->lastaccess + ($flashcard->deck2_delay * HOURSECS) - time()) / DAYSECS));
  122. $strtimetoreview = get_string('timetoreview', 'flashcard', $timetoreview);
  123. for ($i = 0; $i < min($dayslateness, floor($flashcard->deck2_delay / 24)); $i++) {
  124. $str .= "<img src=\"{$CFG->wwwroot}/mod/flashcard/pix/clock.png\" valign=\"bottom\" title=\"$strtimetoreview\" />";
  125. }
  126. if ($dayslateness < $flashcard->deck2_delay / 24) {
  127. for (; $i < $flashcard->deck2_delay / 24; $i++) {
  128. $str .= "<img src=\"{$CFG->wwwroot}/mod/flashcard/pix/shadowclock.png\" valign=\"bottom\" title=\"$strtimetoreview\" />";
  129. }
  130. } elseif ($dayslateness > $flashcard->deck2_delay / 24) {
  131. for ($i = 0; $i < min($dayslateness - floor($flashcard->deck2_delay / 24), $flashcard->deck2_release / 24);
  132. $i++) {
  133. $str .= "<img src=\"{$CFG->wwwroot}/mod/flashcard/pix/overtime.png\" valign=\"bottom\" />";
  134. }
  135. }
  136. $str .= '</td></tr></table>';
  137. } else {
  138. $str .= "<div height=\"12px\" align=\"top\"><img src=\"{$CFG->wwwroot}/mod/flashcard/pix/topempty.png\" /></div>";
  139. }
  140. if ($flashcard->decks >= 3) {
  141. $str .= "</td><td>" . $OUTPUT->pix_icon('a/r_breadcrumb', 'right breadcrumb icon') . "</td><td width=\"30%\" align=\"center\">";
  142. // print for deck 3
  143. if ($status->decks[2]->count) {
  144. $image = ($status->decks[2]->reactivate) ? 'topenabled' : 'topdisabled';
  145. $height = $status->decks[2]->count * 3;
  146. $str .= "<table cellspacing=\"2\"><tr><td><div style=\"padding-bottom: {$height}px\" class=\"graphdeck\" align=\"top\"><img src=\"{$CFG->wwwroot}/mod/flashcard/pix/{$image}.png\" title=\"" . get_string('cardsindeck',
  147. 'flashcard', $status->decks[2]->count) . "\"/></div></td><td>";
  148. $dayslateness = floor((time() - $status->decks[2]->lastaccess) / DAYSECS);
  149. // echo "late 3 : $dayslateness ";
  150. $timetoreview = round(max(0,
  151. ($status->decks[2]->lastaccess + ($flashcard->deck3_delay * HOURSECS) - time()) / DAYSECS));
  152. $strtimetoreview = get_string('timetoreview', 'flashcard', $timetoreview);
  153. for ($i = 0; $i < min($dayslateness, floor($flashcard->deck3_delay / 24)); $i++) {
  154. $str .= "<img src=\"{$CFG->wwwroot}/mod/flashcard/pix/clock.png\" valign=\"bottom\" />";
  155. }
  156. if ($dayslateness < $flashcard->deck3_delay / 24) {
  157. for (; $i < $flashcard->deck3_delay / 24; $i++) {
  158. $str .= "<img src=\"{$CFG->wwwroot}/mod/flashcard/pix/shadowclock.png\" valign=\"bottom\" title=\"$strtimetoreview\" />";
  159. }
  160. } elseif ($dayslateness > $flashcard->deck3_delay / 24) {
  161. for ($i = 0;
  162. $i < min($dayslateness - floor($flashcard->deck3_delay / 24), $flashcard->deck3_release / 24);
  163. $i++) {
  164. $str .= "<img src=\"{$CFG->wwwroot}/mod/flashcard/pix/overtime.png\" valign=\"bottom\" />";
  165. }
  166. }
  167. $str .= '</td></tr></table>';
  168. } else {
  169. $str .= "<div height=\"12px\" align=\"top\"><img src=\"{$CFG->wwwroot}/mod/flashcard/pix/topempty.png\" title=\"$strtimetoreview\" /></div>";
  170. }
  171. }
  172. if ($flashcard->decks >= 4) {
  173. $str .= "</td><td>" . $OUTPUT->pix_icon('a/r_breadcrumb', 'right breadcrumb icon') . "</td><td width=\"30%\" align=\"center\">";
  174. // print for deck 4
  175. if ($status->decks[3]->count) {
  176. $image = ($status->decks[3]->reactivate) ? 'topenabled' : 'topdisabled';
  177. $height = $status->decks[3]->count * 3;
  178. $str .= "<table cellspacing=\"2\"><tr><td><div style=\"padding-bottom: {$height}px\" class=\"graphdeck\" align=\"top\"><img src=\"{$CFG->wwwroot}/mod/flashcard/pix/{$image}.png\" title=\"" . get_string('cardsindeck',
  179. 'flashcard', $status->decks[3]->count) . "\"/></div></td><td>";
  180. $dayslateness = floor((time() - $status->decks[3]->lastaccess) / DAYSECS);
  181. $timetoreview = round(max(0,
  182. ($status->decks[3]->lastaccess + ($flashcard->deck4_delay * HOURSECS) - time()) / DAYSECS));
  183. $strtimetoreview = get_string('timetoreview', 'flashcard', $timetoreview);
  184. for ($i = 0; $i < min($dayslateness, floor($flashcard->deck4_delay / 24)); $i++) {
  185. $str .= "<img src=\"{$CFG->wwwroot}/mod/flashcard/pix/clock.png\" valign=\"bottom\" />";
  186. }
  187. if ($dayslateness < $flashcard->deck4_delay / 24) {
  188. for (; $i < $flashcard->deck4_delay / 24; $i++) {
  189. $str .= "<img src=\"{$CFG->wwwroot}/mod/flashcard/pix/shadowclock.png\" valign=\"bottom\" />";
  190. }
  191. } elseif ($dayslateness > $flashcard->deck4_delay / 24) {
  192. for ($i = 0;
  193. $i < min($dayslateness - floor($flashcard->deck4_delay / 24), $flashcard->deck4_release / 24);
  194. $i++) {
  195. $str .= "<img src=\"{$CFG->wwwroot}/mod/flashcard/pix/overtime.png\" valign=\"bottom\" />";
  196. }
  197. }
  198. $str .= '</td></tr></table>';
  199. } else {
  200. $str .= "<div height=\"12px\" align=\"top\"><img src=\"{$CFG->wwwroot}/mod/flashcard/pix/topempty.png\" /></div>";
  201. }
  202. }
  203. $str .= '</td></tr></table><br/>';
  204. $options['id'] = $flashcard->cm->id;
  205. $options['view'] = 'summary';
  206. $options['what'] = 'reset';
  207. $options['userid'] = $userid;
  208. $str .= $OUTPUT->single_button(new moodle_url("view.php", $options), get_string('reset'), 'get');
  209. if ($return) return $str;
  210. echo $str;
  211. }
  212. /**
  213. * prints some statistic counters about decks
  214. * @param reference $flashcard
  215. * @param boolean $return
  216. * @param int $userid
  217. * @uses $USER
  218. * @uses $CFG
  219. * @uses $DB
  220. */
  221. function flashcard_print_deckcounts($flashcard, $return, $userid = 0) {
  222. global $USER, $CFG, $DB;
  223. if ($userid == 0) $userid = $USER->id;
  224. $sql = "
  225. SELECT
  226. MIN(accesscount) AS minaccess,
  227. MAX(accesscount) AS maxaccess,
  228. AVG(accesscount) AS avgaccess,
  229. SUM(accesscount) AS sumaccess
  230. FROM
  231. {flashcard_card}
  232. WHERE
  233. flashcardid = ? AND
  234. userid = ?
  235. ";
  236. $rec = $DB->get_record_sql($sql, array($flashcard->id, $userid));
  237. $strminaccess = get_string('minaccess', 'flashcard');
  238. $strmaxaccess = get_string('maxaccess', 'flashcard');
  239. $stravgaccess = get_string('avgaccess', 'flashcard');
  240. $strsumaccess = get_string('sumaccess', 'flashcard');
  241. $str = "<table><tr valign=\"top\"><td class=\"smalltext\"><b>$strminaccess</b>:</td>";
  242. $str .= "<td class=\"smalltext\">{$rec->minaccess}</td></tr>";
  243. $str .= "<tr valign=\"top\"><td class=\"smalltext\"><b>$strmaxaccess</b>:</td>";
  244. $str .= "<td class=\"smalltext\">{$rec->maxaccess}</td></tr>";
  245. $str .= "<tr valign=\"top\"><td class=\"smalltext\"><b>$stravgaccess</b>:</td>";
  246. $str .= "<td class=\"smalltext\">{$rec->avgaccess}</td></tr>";
  247. $str .= "<tr valign=\"top\"><td class=\"smalltext\"><b>$strsumaccess</b>:</td>";
  248. $str .= "<td class=\"smalltext\">{$rec->sumaccess}</td></tr></table>";
  249. if ($return) return $str;
  250. echo $str;
  251. }
  252. /**
  253. * prints an image on card side.
  254. * @param reference $flashcard the flashcard object
  255. * @param string $imagename
  256. * @param boolean $return
  257. * @uses $CFG
  258. * @uses $COURSE
  259. */
  260. function flashcard_print_image(&$flashcard, $imagename, $return = false) {
  261. global $CFG, $COURSE;
  262. $strmissingimage = get_string('missingimage', 'flashcard');
  263. if (empty($imagename)) return $strmissingimage;
  264. $imagepath = ($CFG->slasharguments) ? "/{$COURSE->id}/{$imagename}" : "?file=/{$COURSE->id}/{$imagename}";
  265. if (file_exists($CFG->dataroot . "/{$COURSE->id}/{$imagename}")) {
  266. $imagehtml = "<img src=\"{$CFG->wwwroot}/file.php{$imagepath}\" />";
  267. } else {
  268. $imagehtml = "<span class=\"error\">$strmissingimage</span>";
  269. }
  270. if (!$return) echo $imagehtml;
  271. return $imagehtml;
  272. }
  273. /**
  274. * plays a soundcard
  275. * @param reference $flashcard
  276. * @param string $soundname the local name of the sound file. Should be wav or any playable sound format.
  277. * @param string $autostart if 'true' the sound starts playing immediately
  278. * @param boolean $return if true returns the html string
  279. * @uses $CFG
  280. * @uses $COURSE
  281. */
  282. function flashcard_play_sound(&$flashcard, $soundname, $autostart = 'false', $return = false, $htmlname = '') {
  283. global $CFG, $COURSE;
  284. $strmissingsound = get_string('missingsound', 'flashcard');
  285. if (empty($soundname)) return $strmissingsound;
  286. $magic = rand(0, 100000);
  287. if ($htmlname == '') $htmlname = "bell_{$magic}";
  288. $soundpath = ($CFG->slasharguments) ? "/{$COURSE->id}/{$soundname}" : "?file=/{$COURSE->id}/{$soundname}";
  289. if (file_exists($CFG->dataroot . "/{$COURSE->id}/{$soundname}")) {
  290. $soundhtml = "<embed src=\"{$CFG->wwwroot}/file.php{$soundpath}\" autostart=\"$autostart\" hidden=\"false\" id=\"{$htmlname}\" height=\"30\" width=\"150\" />";
  291. } else {
  292. $soundhtml = "<span class=\"error\">$strmissingsound</span>";
  293. }
  294. if (!$return) echo $soundhtml;
  295. return $soundhtml;
  296. }
  297. /**
  298. * initialize decks for a given user. The initialization is soft as it will
  299. * be able to add new subquestions
  300. * @param reference $flashcard
  301. * @param int $userid
  302. * @ues $DB
  303. */
  304. function flashcard_initialize(&$flashcard, $userid) {
  305. global $DB;
  306. // get all cards (all decks)
  307. $cards = $DB->get_records_select('flashcard_card', 'flashcardid = ? AND userid = ?', array($flashcard->id, $userid));
  308. $registered = array();
  309. if (!empty($cards)) {
  310. foreach ($cards as $card) {
  311. $registered[] = $card->entryid;
  312. }
  313. }
  314. // get all subquestions
  315. if ($subquestions = $DB->get_records('flashcard_deckdata', array('flashcardid' => $flashcard->id), '', 'id,id')) {
  316. foreach ($subquestions as $subquestion) {
  317. if (in_array($subquestion->id, $registered)) continue;
  318. $card->userid = $userid;
  319. $card->flashcardid = $flashcard->id;
  320. $card->lastaccessed = time() - ($flashcard->deck1_delay * HOURSECS);
  321. $card->deck = 1;
  322. $card->entryid = $subquestion->id;
  323. if (!$DB->insert_record('flashcard_card', $card)) {
  324. print_error('dbcouldnotinsert', 'flashcard');
  325. }
  326. }
  327. } else {
  328. return false;
  329. }
  330. return true;
  331. }
  332. /**
  333. * get count, last access time and reactivability for all decks
  334. * @param reference $flashcard
  335. * @param int $userid
  336. * @uses $USER
  337. * @uses $DB
  338. */
  339. function flashcard_get_deck_status(&$flashcard, $userid = 0) {
  340. global $USER, $DB;
  341. if ($userid == 0) $userid = $USER->id;
  342. $status = new stdClass();
  343. $dk3 = 0;
  344. $dk4 = 0;
  345. $dk1 = $DB->count_records('flashcard_card', array('flashcardid' => $flashcard->id, 'userid' => $userid, 'deck' => 1));
  346. $status->decks[0] = new stdClass();
  347. $status->decks[0]->count = $dk1;
  348. $dk2 = $DB->count_records('flashcard_card', array('flashcardid' => $flashcard->id, 'userid' => $userid, 'deck' => 2));
  349. $status->decks[1] = new stdClass();
  350. $status->decks[1]->count = $dk2;
  351. if ($flashcard->decks >= 3) {
  352. $dk3 = $DB->count_records('flashcard_card',
  353. array('flashcardid' => $flashcard->id, 'userid' => $userid, 'deck' => 3));
  354. $status->decks[2]->count = $dk3;
  355. }
  356. if ($flashcard->decks >= 4) {
  357. $dk4 = $DB->count_records('flashcard_card',
  358. array('flashcardid' => $flashcard->id, 'userid' => $userid, 'deck' => 4));
  359. $status->decks[3]->count = $dk4;
  360. }
  361. // not initialized for this user
  362. if ($dk1 + $dk2 + $dk3 + $dk4 == 0) {
  363. return null;
  364. }
  365. if ($dk1 > 0) {
  366. $status->decks[0]->lastaccess = flashcard_get_lastaccessed($flashcard, 1, $userid);
  367. $status->decks[0]->reactivate = (time() > ($status->decks[0]->lastaccess + $flashcard->deck1_delay * HOURSECS));
  368. }
  369. if ($dk2 > 0) {
  370. $status->decks[1]->lastaccess = flashcard_get_lastaccessed($flashcard, 2, $userid);
  371. $status->decks[1]->reactivate = (time() > ($status->decks[1]->lastaccess + $flashcard->deck2_delay * HOURSECS));
  372. }
  373. if ($flashcard->decks >= 3 && $dk3 > 0) {
  374. $status->decks[2]->lastaccess = flashcard_get_lastaccessed($flashcard, 3, $userid);
  375. $status->decks[2]->reactivate = (time() > ($status->decks[2]->lastaccess + $flashcard->deck3_delay * HOURSECS));
  376. }
  377. if ($flashcard->decks >= 4 && $dk4 > 0) {
  378. $status->decks[3]->lastaccess = flashcard_get_lastaccessed($flashcard, 4, $userid);
  379. $status->decks[3]->reactivate = (time() > ($status->decks[3]->lastaccess + $flashcard->deck4_delay));
  380. }
  381. return $status;
  382. }
  383. /**
  384. * get card status structure
  385. * @param reference $flashcard
  386. * @uses $CFG
  387. * @uses $DB
  388. */
  389. function flashcard_get_card_status(&$flashcard) {
  390. global $CFG, $DB;
  391. // get decks by card
  392. $sql = "
  393. SELECT
  394. dd.questiontext,
  395. COUNT(c.id) as amount,
  396. c.deck AS deck
  397. FROM
  398. {flashcard_deckdata} dd
  399. LEFT JOIN
  400. {flashcard_card} c
  401. ON
  402. c.entryid = dd.id
  403. WHERE
  404. c.flashcardid = ?
  405. GROUP BY
  406. c.entryid,
  407. c.deck
  408. ";
  409. $recs = $DB->get_records_sql($sql, array($flashcard->id));
  410. // get accessed by card
  411. $sql = "
  412. SELECT
  413. dd.questiontext,
  414. SUM(accesscount) AS accessed
  415. FROM
  416. {flashcard_deckdata} dd
  417. LEFT JOIN
  418. {flashcard_card} c
  419. ON
  420. c.entryid = dd.id
  421. WHERE
  422. c.flashcardid = ?
  423. GROUP BY
  424. c.entryid
  425. ";
  426. $accesses = $DB->get_records_sql($sql, array($flashcard->id));
  427. $cards = array();
  428. foreach ($recs as $question => $rec) {
  429. if ($rec->deck == 1) $cards[$question]->deck[0] = $rec->amount;
  430. if ($rec->deck == 2) $cards[$question]->deck[1] = $rec->amount;
  431. if ($rec->deck == 3) $cards[$question]->deck[2] = $rec->amount;
  432. if ($rec->deck == 4) $cards[$question]->deck[3] = $rec->amount;
  433. $cards[$question]->accesscount = $accesses[$question]->accessed;
  434. }
  435. return $cards;
  436. }
  437. /**
  438. * prints a graphical represnetation of decks, proportionnaly to card count
  439. * @param reference $flashcard
  440. * @param object $card
  441. * @param boolean $return
  442. * @uses $CFG
  443. */
  444. function flashcard_print_cardcounts(&$flashcard, $card, $return = false) {
  445. global $CFG;
  446. $str = '';
  447. $strs[] = "<td><img src=\"{$CFG->wwwroot}/mod/flashcard/pix/topenabled.png\" /> (1) </td><td>" . '<div class="bar" style="height: 10px; width: ' . (1 + @$card->deck[0]) . 'px"></div></td>';
  448. $strs[] = "<td><img src=\"{$CFG->wwwroot}/mod/flashcard/pix/topenabled.png\" /> (2) </td><td>" . '<div class="bar" style="height: 10px; width: ' . (1 + @$card->deck[1]) . 'px"></div></td>';
  449. if ($flashcard->decks >= 3) {
  450. $strs[] = "<td><img src=\"{$CFG->wwwroot}/mod/flashcard/pix/topenabled.png\" /> (3) </td><td>" . '<div class="bar" style="height: 10px; width: ' . (1 + @$card->deck[2]) . 'px"></div></td>';
  451. }
  452. if ($flashcard->decks >= 4) {
  453. $strs[] = "<td><img src=\"{$CFG->wwwroot}/mod/flashcard/pix/topenabled.png\" /> (4) </td><td>" . '<div class="bar" style="height: 10px; width: ' . (1 + @$card->deck[3]) . 'px"></div></td>';
  454. }
  455. $str = "<table cellspacing=\"2\"><tr valign\"middle\">" . implode("</tr><tr valign=\"middle\">", $strs) . "</tr></table>";
  456. if ($return) return $str;
  457. echo $str;
  458. }
  459. /**
  460. * Get array of questions & answers for edit $page
  461. * If $page == -1 then take the last page
  462. * @param type $flashcard
  463. * @param type $page
  464. */
  465. function flashcard_get_page($flashcard, $page) {
  466. global $DB, $CFG;
  467. if ($page == -1) {
  468. //take the last page
  469. $cardsnum = $DB->count_records('flashcard_deckdata', array('flashcardid' => $flashcard->id));
  470. $page = (int) ($cardsnum / FLASHCARD_CARDS_PER_PAGE);
  471. }
  472. $cards = $DB->get_records('flashcard_deckdata', array('flashcardid' => $flashcard->id), 'id', '*',
  473. FLASHCARD_CARDS_PER_PAGE * $page, FLASHCARD_CARDS_PER_PAGE);
  474. $cm = get_coursemodule_from_instance('flashcard', $flashcard->id);
  475. $context = context_module::instance($cm->id);
  476. $ret = new object();
  477. $ret->answer = array();
  478. $ret->question = array();
  479. $ret->id = array();
  480. $editoroptions = array('context'=>$context ,'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>true, 'noclean'=>true);
  481. $i = 0;
  482. foreach ($cards as $card) {
  483. $itemid = NULL;
  484. $currenttext = file_prepare_draft_area($itemid, $context->id, 'mod_flashcard', 'question', $card->id,
  485. $editoroptions, $card->questiontext);
  486. $ret->question[$i] = array('text' => $currenttext, 'format' => 1, 'itemid' => $itemid);
  487. $itemid = NULL;
  488. $currenttext = file_prepare_draft_area($itemid, $context->id, 'mod_flashcard', 'answer', $card->id,
  489. $editoroptions, $card->answertext);
  490. $ret->answer[$i] = array('text' => $currenttext, 'format' => 1, 'itemid' => $itemid);
  491. $ret->id[$i] = $card->id;
  492. $i++;
  493. }
  494. return $ret;
  495. }