Proyectos de Subversion Moodle

Rev

Rev 12 | Rev 40 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
12 efrain 1
<?php
2
header('Access-Control-Allow-Origin: *');
3
header('Access-Control-Allow-Methods: GET');
4
//POST, GET, DELETE, PUT, PATCH, OPTIONS
5
header('Access-Control-Allow-Headers: token, Content-Type');
6
header('Access-Control-Max-Age: 1728000');
7
 
8
 
9
header('Content-Type: application/json');
10
 
11
 
12
 
13
require_once(__DIR__ . '/config.php');
14
require_once(__DIR__ . '/../config.php');
15
global $DB, $PAGE, $CFG;
16
 
17
require_once($CFG->libdir.'/moodlelib.php');
18
require_once($CFG->libdir . '/externallib.php');
19
require_once($CFG->libdir.'/authlib.php');
20
require_once( $CFG->libdir . '/gdlib.php' );
21
 
22
require_once($CFG->dirroot.'/user/lib.php');
23
require_once __DIR__ . '/rsa.php';
24
require_once __DIR__ . '/lib.php';
25
 
39 efrain 26
/*
12 efrain 27
$username   = trim(isset($_SERVER['HTTP_USERNAME']) ? filter_var($_SERVER['HTTP_USERNAME'], FILTER_SANITIZE_STRING) : '');
28
$password   = trim(isset($_SERVER['HTTP_PASSWORD']) ? filter_var($_SERVER['HTTP_PASSWORD'], FILTER_SANITIZE_STRING) : '');
29
$timestamp  = trim(isset($_SERVER['HTTP_TIMESTAMP']) ? filter_var($_SERVER['HTTP_TIMESTAMP'], FILTER_SANITIZE_STRING) : '');
30
$rand       = intval(isset($_SERVER['HTTP_RAND']) ? filter_var($_SERVER['HTTP_RAND'], FILTER_SANITIZE_NUMBER_INT) : 0,10);
39 efrain 31
$id         = intval(isset($_GET['id']) ? filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT) : 0,10);
12 efrain 32
 
33
 
34
if(empty($username) || empty($password) || empty($timestamp) || empty($rand) || !is_integer($rand)) {
35
    echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY1']) ;
36
    exit;
37
}
38
 
39
if($username != LLWS_USERNAME) {
40
    echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY2']) ;
41
    exit;
42
}
43
 
44
$dt = \DateTime::createFromFormat('Y-m-d\TH:i:s', $timestamp);
45
if(!$dt) {
46
    echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY3']) ;
47
    exit;
48
}
49
 
50
$dt = \DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s',  gmdate('Y-m-d\TH:i:s'));
51
$dtMax = $dt->add(\DateInterval::createFromDateString('5 minutes'));
52
$dtMin = $dt->sub(\DateInterval::createFromDateString('5 minutes'));
53
 
54
 
55
$t0 = $dt->getTimestamp();
56
$t1 = $dtMin->getTimestamp();
57
$t2 = $dtMax->getTimestamp();
58
 
59
if($t0 < $t1 || $t0 > $t2) {
60
    echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY4']) ;
61
    exit;
62
}
63
 
64
if(!password_verify( $username.'-'. LLWS_PASSWORD. '-' . $rand. '-' . $timestamp, $password)) {
65
    echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY5']) ;
66
    exit;
67
}
39 efrain 68
*/
12 efrain 69
 
70
 
71
 
39 efrain 72
$id = 81;
12 efrain 73
 
74
 
75
 
76
$course = get_course($id);
77
if(!$course) {
78
    echo json_encode([
79
        'success' => false,
80
        'data' => 'El curso solicitado no existe'
81
    ]);
82
    exit;
83
}
84
 
85
$enrolmethod = 'manual';
86
$url_noimage =  $CFG->wwwroot . '/theme/' . $PAGE->theme->name . '/pix/coursenoimage.jpg';
87
 
88
 
89
$enrol = enrol_get_plugin($enrolmethod);
90
if (!$enrol) {
91
    echo json_encode([
92
        'success' => false,
93
        'data' => 'El Plugin de enrolamiento manual no esta disponible'
94
    ]);
95
    exit;
96
}
97
 
98
$instances = enrol_get_instances($course->id, true);
99
$manualinstance = null;
100
foreach ($instances as $instance)
101
{
102
    if ($instance->enrol == $enrolmethod) {
103
        $manualinstance = $instance;
104
        break;
105
    }
106
}
107
 
108
 
109
if (!$manualinstance) {
110
    echo json_encode([
111
        'success' => false,
112
        'data' => 'Este curso no soporta en enrolamiento manual'
113
    ]);
114
}
115
 
116
if ($course instanceof stdClass) {
117
    $coreCourseList = new core_course_list_element($course);
118
} else {
119
    $coreCourseList = [];
120
}
121
 
122
$image = $url_noimage;
123
foreach ($coreCourseList->get_course_overviewfiles() as $file)
124
{
125
    $isimage = $file->is_valid_image();
126
    $image = file_encode_url("{$CFG->wwwroot}/pluginfile.php", '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage);
127
    if (!$isimage) {
128
        $image = $url_noimage;
129
    }
130
}
131
 
132
 
133
echo json_encode([
134
    'success' => true,
135
    'data' =>
136
    [
137
        'courseid' => $course->id,
138
        'courseimage' => $image,
139
        'enddate' => $course->enddate,
140
        'fullname' => $course->fullname,
141
        'idnumber' => $course->idnumber,
142
        'shortname' => $course->shortname,
143
        'startdate' => $course->startdate,
144
        'summary' => $course->summary,
145
    ]
146
]);
147
 
148
 
149
 
150