Proyectos de Subversion Moodle

Rev

Rev 37 | Rev 57 | 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
 
37 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);
31
 
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
}
68
 
69
 
37 efrain 70
 
12 efrain 71
$response = [
72
    'success' => true,
73
    'data' => []
74
];
75
 
76
$enrolmethod = 'manual';
77
$url_noimage =  $CFG->wwwroot . '/theme/' . $PAGE->theme->name . '/pix/coursenoimage.jpg';
78
 
79
//$courses_with_autoenrol  = $DB->get_records('enrol', ['enrol' => 'manual', 'status' => 0]);
80
//print_r($courses_with_autoenrol); exit;
81
 
21 efrain 82
 
83
header('Content-type: text/plain');
12 efrain 84
$enrol = enrol_get_plugin($enrolmethod);
85
if ($enrol) {
86
 
21 efrain 87
 
16 efrain 88
 
12 efrain 89
    $courses = get_courses();
24 efrain 90
 
35 efrain 91
    $courses = $DB->get_records('course'); //, ['visible' => 1], 'shortname', 'courseid, enddate, fullname, idnumber, shortname, startdate, summary');
24 efrain 92
 
12 efrain 93
    foreach($courses as $course)
94
    {
33 efrain 95
        if(!$course->visible) {
96
            continue;
97
        }
98
 
99
 
36 efrain 100
 
12 efrain 101
        $instances = enrol_get_instances($course->id, true);
102
 
103
        $manualinstance = null;
104
        foreach ($instances as $instance)
105
        {
106
            if ($instance->enrol == $enrolmethod) {
107
                $manualinstance = $instance;
108
                break;
109
            }
110
        }
111
 
112
 
113
        if (!$manualinstance) {
114
            continue;
115
        }
116
 
37 efrain 117
 
12 efrain 118
        if ($course instanceof stdClass) {
119
            $coreCourseList = new core_course_list_element($course);
120
        } else {
121
            $coreCourseList = [];
122
        }
123
 
124
        $image = $url_noimage;
125
        foreach ($coreCourseList->get_course_overviewfiles() as $file)
126
        {
127
            $isimage = $file->is_valid_image();
128
            $image = file_encode_url("{$CFG->wwwroot}/pluginfile.php", '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage);
129
            if (!$isimage) {
130
                $image = $url_noimage;
131
            }
132
        }
133
 
134
 
135
 
136
        array_push($response['data'], [
137
           'courseid' => $course->id,
138
           'enddate' => $course->enddate,
139
           'fullname' => $course->fullname,
140
           'idnumber' => $course->idnumber,
141
           'shortname' => $course->shortname,
142
           'startdate' => $course->startdate,
143
        ]);
144
 
145
    }
146
}
147
 
148
echo json_encode($response) ;
149
 
150
 
151
 
152