WhispeakMyStudentsQuizTrackingHook.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class WhispeakMyStudentsQuizTrackingHook.
  5. */
  6. class WhispeakMyStudentsQuizTrackingHook extends HookObserver implements HookMyStudentsQuizTrackingObserverInterface
  7. {
  8. /**
  9. * WhispeakMyStudentsQuizTrackingHook constructor.
  10. */
  11. protected function __construct()
  12. {
  13. parent::__construct(
  14. 'plugin/whispeakauth/WhispeakAuthPlugin.php',
  15. 'whispeakauth'
  16. );
  17. }
  18. /**
  19. * Return an associative array this value and attributes.
  20. * <code>
  21. * [
  22. * 'value' => 'Users online',
  23. * 'attrs' => ['class' => 'text-center'],
  24. * ]
  25. * </code>.
  26. *
  27. * @param HookMyStudentsQuizTrackingEventInterface $hook
  28. *
  29. * @return array
  30. */
  31. public function trackingHeader(HookMyStudentsQuizTrackingEventInterface $hook)
  32. {
  33. return [
  34. 'value' => WhispeakAuthPlugin::create()->get_lang('plugin_title'),
  35. 'attrs' => [
  36. 'class' => 'text-center',
  37. ],
  38. ];
  39. }
  40. /**
  41. * Return an associative array this value and attributes.
  42. * <code>
  43. * [
  44. * 'value' => '5 connected users ',
  45. * 'attrs' => ['class' => 'text-center text-success'],
  46. * ]
  47. * </code>.
  48. *
  49. * @param HookMyStudentsQuizTrackingEventInterface $hook
  50. *
  51. * @throws \Doctrine\ORM\Query\QueryException
  52. *
  53. * @return array
  54. */
  55. public function trackingContent(HookMyStudentsQuizTrackingEventInterface $hook)
  56. {
  57. $data = $hook->getEventData();
  58. $totalCount = WhispeakAuthPlugin::countAllAttemptsInQuiz($data['quiz_id'], $data['student_id']);
  59. if (0 === $totalCount) {
  60. return [
  61. 'value' => '-',
  62. 'attrs' => ['class' => 'text-center'],
  63. ];
  64. }
  65. $successCount = WhispeakAuthPlugin::countSuccessAttemptsInQuiz($data['quiz_id'], $data['student_id']);
  66. $attrs = ['class' => 'text-center '];
  67. $attrs['class'] .= $successCount <= $totalCount / 2 ? 'text-danger' : 'text-success';
  68. return [
  69. 'value' => Display::tag('strong', "$successCount / $totalCount"),
  70. 'attrs' => $attrs,
  71. ];
  72. }
  73. }