-
Notifications
You must be signed in to change notification settings - Fork 11
/
block_if_sence_login.php
213 lines (198 loc) · 11.2 KB
/
block_if_sence_login.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This is a one-line short description of the file
*
* You can have a rather longer description of the file as well,
* if you like, and it can span multiple lines.
*
* @package block_if_sence_login
* @copyright authors: Daniel Torres and J AND J SPA
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__).'/../../config.php');
require_once(dirname(__FILE__).'/model/courses_sence_list.php');
require_once(dirname(__FILE__).'/model/session_store_helpers.php');
defined('MOODLE_INTERNAL')||die();
class block_if_sence_login extends block_base {
public function init() {
$this->title = get_string('pluginname', 'block_if_sence_login');
}
function get_required_javascript() {
parent::get_required_javascript();
$this->page->requires->jquery();
$this->page->requires->jquery_plugin('ui');
$this->page->requires->jquery_plugin('ui-css');
}
function has_config() {return true;}
function is_coursetopicpage(){
global $PAGE;
if ($PAGE->bodyid == 'page-site-index') {
return true;
}else{
return false;
}
}
function set_formcontent(){
global $USER;
$runotec = get_config('block_if_sence_login','runotec');
$tokenotec = get_config('block_if_sence_login','tokenotec');
//Desarrollo
//$DB->set_debug(true);
if(!empty($runotec) ){
if(!empty($tokenotec)){
return $this->get_body_plugin();
}else{
return $text = $this->get_missing_data();
}
}else{
return $text = $this->get_missing_data();
}
}
public function get_body_plugin(){
global $USER;
global $CFG;
$runalumno = $USER->profile['runalumno'];
$userid = $USER->id;
$runotec = get_config('block_if_sence_login','runotec');
$tokenotec = get_config('block_if_sence_login','tokenotec');
$idsesionalumno = sesskey();
$courses_sence = get_courses_sence_list();
$courses_sence_2 = get_courses_sence_list();
$new_link = $CFG->wwwroot;
$text = '<h5>Selecciona el curso que deseas</h5>';
$text .= '<form method="post" id="form-sence">';
$text .= '<select id="course-selector">';
$text .= '<option value="-1">Seleccione...</option>';
foreach ($courses_sence as $course){
if (is_registered_in_course($userid,$course->id)){
$text .='<option value="'.$course->id.'">'.$course->fullname."</option>";
}
}
$text .= '</select>';
$text .= '<input type="hidden" value="'.$runotec.'" name="RutOtec">';
$text .= '<input type="hidden" value="'.$tokenotec.'" name="Token">';
$text .= '<input type="hidden" value="" name="CodSence" id="cod-sence">';
$text .= '<input type="hidden" value="" name="CodigoCurso" id="cod-curso">';
$text .= '<input type="hidden" value="" name="LineaCapacitacion" id="linea-cap">';
$text .= '<input type="hidden" value="'.$new_link.'/blocks/if_sence_login/pages/exito.php?type=login" name="UrlRetoma">';
$text .= '<input type="hidden" value="'.$new_link.'/blocks/if_sence_login/pages/error.php" name="UrlError">';
$text .= '<input type="hidden" value="'.$runalumno.'" name="RunAlumno">';
$text .= '<input type="hidden" value="'.$idsesionalumno.'" name="IdSesionAlumno"><br>';
$text .= '<br><input type="submit" value="Iniciar" disabled="disabled" id="submit-button">';
$text .='</form>';
$text .='<script>';
$text .= 'var firstFolder = window.location.pathname.split("/")[1];';
$text .= '$(document).ready(function(){';
$text .= 'var prot = document.location.protocol;';
$text .= 'var host = document.location.hostname;';
foreach ($courses_sence_2 as $course){
$data_course = get_data_course_by_courseid($course->id,$userid);
$text .= '$("a[href*=\'/course/view.php?id='.$course->id.'\']:contains(\''.$course->fullname.'\')").remove(".if_block_sence_'.$course->id.'");';
$text .= '$("a[href*=\'/course/view.php?id='.$course->id.'\']:contains(\''.$course->fullname.'\')").append("<div style=\'margin-top:10px;font-size:12px;\' class=\'if_block_sence_'.$course->id.'\'>';
if(is_only_student_in_course($userid,$course->id)){
//Posee solo el rol de estudiante y hace la verificacion de la sesi+on
if(sence_validate_session($runalumno,$course->codsence)){
//Continua su sesion iniciada sin logearse nuevamente en sence
$text .= '<form action=\""+prot+"//"+host+"/"+firstFolder+"/course/view.php?id='.$course->id.'\" method=\"post\">';
$text .= '<input type=\"submit\" value=\"Seguir con el curso\">';
}else{
//la sesion no es valida o se encuentra caducada por lo que se env铆a a sence para login
$text .= '<form action=\"https://sistemas.sence.cl/rce/Registro/IniciarSesion\" method=\"post\">';
$text .= '<input type=\"hidden\" value=\"'.$runotec.'\" name=\"RutOtec\">';
$text .= '<input type=\"hidden\" value=\"'.$tokenotec.'\" name=\"Token\">';
$text .= '<input type=\"hidden\" value=\"'.$course->codsence.'\" name=\"CodSence\">';
$text .= '<input type=\"hidden\" value=\"'.$data_course['codcurso'].'\" name=\"CodigoCurso\">';
$text .= '<input type=\"hidden\" value=\"'.$data_course['lineacap'].'\" name=\"LineaCapacitacion\">';
$text .= '<input type=\"hidden\" value=\""+prot+"//"+host+"/"+firstFolder+"/blocks/if_sence_login/pages/exito.php?type=login\" name=\"UrlRetoma\">';
$text .= '<input type=\"hidden\" value=\""+prot+"//"+host+"/"+firstFolder+"/blocks/if_sence_login/pages/error.php\" name=\"UrlError\">';
$text .= '<input type=\"hidden\" value=\"'.$runalumno.'\" name=\"RunAlumno\">';
$text .= '<input type=\"hidden\" value=\"'.$idsesionalumno.'\" name=\"IdSesionAlumno\">';
if(is_registered_in_course($userid,$course->id)){
//El estudiante se encuentra matriculado
$text .= '<input type=\"submit\" value=\"Iniciar con SENCE\">';
}else{
//El estudiante no se encuentra matriculado
$text .= '<input type=\"submit\" value=\"Iniciar con SENCE\" disabled=\"disabled\" title=\"No estas matriculado en este curso\">';
}
}
}else{
//el usuario no es estudiante o posee mas de un perfil en el curso
$text .= '<form action=\""+prot+"//"+host+"/"+firstFolder+"/course/view.php?id='.$course->id.'\" method=\"post\">';
$text .= '<input type=\"submit\" value=\"Ingresar al curso\">';
}
$text .= '");';
$text .= 'var coursebox = $(".fp-coursebox");';
$text .= '$("a[href*=\'/course/view.php?id='.$course->id.'\']",coursebox).removeAttr("href");';
}
$text .= '});';
$text .= 'var url = "blocks/if_sence_login/model/sence_data_filter.php";';
$text .= '$("#course-selector").change(function(){';
$text .= 'if($("#course-selector").val()!=="-1"){';
$text .= 'var id = $("#course-selector").val();';
$text .= '$.ajax({';
$text .= 'url: url,';
$text .= 'type: "POST",';
$text .= 'data: {id:id,runalumno:"'.$runalumno.'"}';
$text .= '}).done(function(js) {';
$text .= 'eval(js);';
$text .= '$("#submit-button").attr("disabled", false);';
$text .= '});';
$text .= '}else{';
$text .= '$("#submit-button").attr("disabled", true);';
$text .= '}';
$text .=' });';
$text .='</script>';
//$text .= '<hr>';
$courses_user = get_courses_data_from_alumnoid($userid);
//var_dump($userid);die;
$text .= '<h5>Sesiones iniciadas</h5>';
$count = 1;
foreach($courses_user as $c_u){
$text .= '<form name="course_list_form_'.$count.'" action="https://sistemas.sence.cl/rce/Registro/CerrarSesion" method="post">';
$text .= '<p>'.$c_u->nombrecurso.' <input type="submit" value="Cerrar"/></p>';
$text .= '<input type="hidden" value="'.$runotec.'" name="RutOtec">';
$text .= '<input type="hidden" value="'.$tokenotec.'" name="Token">';
$text .= '<input type="hidden" value="'.$c_u->codsence.'" name="CodSence">';
$text .= '<input type="hidden" value="'.$c_u->codcurso.'" name="CodigoCurso">';
$text .= '<input type="hidden" value="'.$c_u->lineacap.'" name="LineaCapacitacion">';
$text .= '<input type="hidden" value="'.$new_link.'/blocks/if_sence_login/pages/exito.php?type=logout" name="UrlRetoma">';
$text .= '<input type="hidden" value="'.$new_link.'/blocks/if_sence_login/pages/error.php" name="UrlError">';
$text .= '<input type="hidden" value="'.$runalumno.'" name="RunAlumno">';
$text .= '<input type="hidden" value="'.$idsesionalumno.'" name="IdSesionAlumno">';
$text .= '<input type="hidden" value="'.$c_u->idsesionsence.'" name="IdSesionSence"><br>';
$text .= '</form>';
$count++;
}
return $text;
}
public function get_missing_data(){
$text = '<h5>Hola. Falta algo!</h5>';
$text = '<p>Por favor, echa un vistazo al formulario de Información OTEC.</p>';
return $text;
}
public function get_content() {
if ($this->content !== null) {
return $this->content;
}
if(isloggedin()){
$this->content = new stdClass;
$this->content->text = $this->set_formcontent();
}
return $this->content;
}
}
?>