|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Puerari\Moodle; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @trait Account |
|
7
|
|
|
* @package Puerari\Cwp |
|
8
|
|
|
* @author Leandro Puerari <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
trait Core |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @param string $field |
|
14
|
|
|
* @param string $value |
|
15
|
|
|
* @return array|string |
|
16
|
|
|
* @throws MraException |
|
17
|
|
|
*/ |
|
18
|
|
|
public function getCoursesByField(/*string*/ $field, /*string*/ $value)/*: array|string*/ |
|
19
|
|
|
{ |
|
20
|
|
|
$validFields = ['', 'id', 'ids', 'shortname', 'idnumber', 'category']; |
|
21
|
|
|
if (!in_array($field, $validFields)) { |
|
22
|
|
|
throw new MraException("Invalid field value: '$field'.\n Valid values: " . implode(', ', $validFields)); |
|
23
|
|
|
} |
|
24
|
|
|
$this->data = compact('field', 'value'); |
|
|
|
|
|
|
25
|
|
|
$this->data['wsfunction'] = 'core_course_get_courses_by_field'; |
|
26
|
|
|
return $this->execGetCurl(); |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param array $ids |
|
31
|
|
|
* @return array|string |
|
32
|
|
|
* @throws MraException |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getCourses(array $ids) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->data = []; |
|
|
|
|
|
|
37
|
|
|
$this->data['wsfunction'] = 'core_course_get_courses'; |
|
38
|
|
|
$cont = 0; |
|
39
|
|
|
foreach ($ids as $vlr) { |
|
40
|
|
|
$this->data['options']['ids'][$cont] = intval($vlr); |
|
41
|
|
|
++$cont; |
|
42
|
|
|
} |
|
43
|
|
|
return $this->execGetCurl(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param $course_id |
|
48
|
|
|
* @param array $options |
|
49
|
|
|
* @return array|string |
|
50
|
|
|
* @throws MraException |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getEnrolledUsers(/*string*/ $course_id, array $options = []) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->data = []; |
|
|
|
|
|
|
55
|
|
|
$this->data['courseid'] = $course_id; |
|
56
|
|
|
$this->data['options'] = $options; |
|
57
|
|
|
$this->data['wsfunction'] = 'core_enrol_get_enrolled_users'; |
|
58
|
|
|
return $this->execGetCurl(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param $course_id |
|
63
|
|
|
* @param array $options |
|
64
|
|
|
* @return array |
|
65
|
|
|
* @throws MraException |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getEnrolledStudents(/*string*/ $course_id, array $options = [])/*: array*/ |
|
68
|
|
|
{ |
|
69
|
|
|
$oldReturnFormat = $this->getReturnFormat(); |
|
|
|
|
|
|
70
|
|
|
$this->setReturnFormat(self::RETURN_ARRAY); |
|
|
|
|
|
|
71
|
|
|
$students = $this->getEnrolledUsers($course_id, $options); |
|
72
|
|
|
$this->setReturnFormat($oldReturnFormat); |
|
73
|
|
|
$studentsToReturn = []; |
|
74
|
|
|
foreach ($students as $student) { |
|
75
|
|
|
if (is_array($student['roles'])) { |
|
76
|
|
|
foreach ($student['roles'] as $role) { |
|
77
|
|
|
if ($role['shortname'] == 'student') { |
|
78
|
|
|
$studentsToReturn[] = $student; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
return $studentsToReturn; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|