view.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Prints an instance of colab.
  18. *
  19. * @package colab
  20. * @copyright 2020 Your Name <you@example.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require(__DIR__.'/../../config.php');
  24. require_once(__DIR__.'/lib.php');
  25. global $COURSE, $DB, $CFG;
  26. // Course_module ID, or
  27. $id = optional_param('id', 0, PARAM_INT);
  28. // ... module instance id.
  29. $c = optional_param('c', 0, PARAM_INT);
  30. if ($id) {
  31. $cm = get_coursemodule_from_id('colab', $id, 0, false, MUST_EXIST);
  32. $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  33. $moduleinstance = $DB->get_record('colab', array('id' => $cm->instance), '*', MUST_EXIST);
  34. } else if ($c) {
  35. $moduleinstance = $DB->get_record('colab', array('id' => $n), '*', MUST_EXIST);
  36. $course = $DB->get_record('course', array('id' => $moduleinstance->course), '*', MUST_EXIST);
  37. $cm = get_coursemodule_from_instance('colab', $moduleinstance->id, $course->id, false, MUST_EXIST);
  38. } else {
  39. print_error(get_string('missingidandcmid', colab));
  40. }
  41. require_login($course, true, $cm);
  42. $modulecontext = context_module::instance($cm->id);
  43. $event = \mod_colab\event\course_module_viewed::create(array(
  44. 'objectid' => $moduleinstance->id,
  45. 'context' => $modulecontext
  46. ));
  47. $event->add_record_snapshot('course', $course);
  48. $event->add_record_snapshot('colab', $moduleinstance);
  49. $event->trigger();
  50. $PAGE->set_url('/mod/colab/view.php', array('id' => $cm->id));
  51. $PAGE->set_title(format_string($moduleinstance->name));
  52. $PAGE->set_heading(format_string($course->fullname));
  53. $PAGE->set_context($modulecontext);
  54. echo $OUTPUT->header();
  55. echo "meu ip : ".ip2long(getremoteaddr())."<br>" ;
  56. $achou_dupla = 0;
  57. //verifica se já não foi excolhido
  58. try{
  59. $params = array('ativade_atual' => $moduleinstance->id,'usuario'=>$USER->id);
  60. $records = $DB->get_record_sql("select obs as obs from {colab_ip} where userid = :usuario" ,$params);
  61. print("reultado obs: " );
  62. print($records->obs);
  63. } catch (Exception $e) {
  64. echo 'erro obs: ', $e->getMessage(), "\n";
  65. $dupla = '';
  66. }
  67. //se não localizou dupla ou não tem registro
  68. if($records->obs == 0 or $records->obs == ' ' ){
  69. echo "não registou ou não foi escolhido <br>";
  70. //tenta ver se tem uma dupla
  71. //tenta achar uma pessoa esperando
  72. try{
  73. $params = array('usuario'=>$USER->id);
  74. $records = $DB->get_record_sql("select ip_user as ip_dupla from {colab_ip} where userid != :usuario and obs = 0 limit 1" ,$params);
  75. //echo "usuário".$USER->id;
  76. print("reultado dupla " );
  77. print($records->ip_dupla);
  78. } catch (Exception $e) {
  79. echo 'erro dupla: ', $e->getMessage(), "\n";
  80. $dupla = '';
  81. }
  82. if($records->ip_dupla == 0 or $records->ip_dupla == ' ' ){print("vc é o primeiro, recarregue a página" );}
  83. else {
  84. //futura dupla localizada
  85. try{
  86. $value-> id = 1;
  87. $value-> userid = $USER->id ;
  88. $value-> ip_user = ip2long(getremoteaddr()) ;
  89. $value-> time = time();
  90. $value-> obs = $records->ip_dupla;
  91. $DB->insert_record("colab_ip", $value);
  92. echo "registra o IP <br>";
  93. } catch (Exception $e) {
  94. echo 'erro regisra ip pós dupla: ', $e->getMessage(), "\n";
  95. $dupla = '';
  96. }
  97. print("achou uma dupla esperando" );
  98. print($records->ip_dupla );
  99. $DB->execute('update mdla_colab_ip set obs = :ip_meu where ip_user = :ip_dupla', array('ip_meu'=>ip2long(getremoteaddr()),'ip_dupla'=>$records->ip_dupla));
  100. //executa o jogo com a dupla
  101. $achou_dupla = 1;
  102. echo "<iframe src='http://192.168.1.182/ihanoi/index.html?id='.$records->ip_dupla width = '1000' height = '1000' >";
  103. echo "</iframe>";
  104. }
  105. } else {
  106. //tem uma dupla te esperando
  107. echo "tem uma dupla te esperando ";
  108. echo $records->obs;
  109. $achou_dupla = 1;
  110. $DB->execute('update mdla_colab_ip set obs = :ip_dupla where ip_user = :ip_meu', array('ip_meu'=>ip2long(getremoteaddr()),'ip_dupla'=>$records->obs));
  111. echo "<iframe src='http://192.168.1.182/ihanoi/index.html?id='.$records->obs width = '1000' height = '1000' >";
  112. echo "</iframe>";
  113. }
  114. //caso já tenha localizado a dupla, não precisa atualizar o ip
  115. if($achou_dupla == 0){
  116. //atualização de IP
  117. //deleta o ultimo ip
  118. try {
  119. //$DB->delete_records("colab_ip", array('userid'=>$CFG->dbuser));
  120. $DB->delete_records("colab_ip", array('userid'=>$USER->id));
  121. } catch (Exception $e) {
  122. echo 'Caught exception sem registro de ip: ', $e->getMessage(), "\n";
  123. }
  124. //salva o ip atual
  125. try {
  126. $value-> id = 1;
  127. $value-> userid = $USER->id ;
  128. $value-> ip_user = ip2long(getremoteaddr()) ;
  129. $value-> time = time();
  130. $value-> obs = 0;
  131. $DB->insert_record("colab_ip", $value);
  132. } catch (Exception $e) {
  133. echo 'Caught exception não foi possivel grava registo de ip: ', $e->getMessage(), "\n";
  134. }
  135. }
  136. //print_r($CFG);
  137. echo $OUTPUT->footer();