Core::getCoursesByField()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 2
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');
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
25
        $this->data['wsfunction'] = 'core_course_get_courses_by_field';
26
        return $this->execGetCurl();
0 ignored issues
show
Bug introduced by
It seems like execGetCurl() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
        return $this->/** @scrutinizer ignore-call */ execGetCurl();
Loading history...
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 = [];
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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 = [];
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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();
0 ignored issues
show
Bug introduced by
It seems like getReturnFormat() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
        /** @scrutinizer ignore-call */ 
70
        $oldReturnFormat = $this->getReturnFormat();
Loading history...
70
        $this->setReturnFormat(self::RETURN_ARRAY);
0 ignored issues
show
Bug introduced by
It seems like setReturnFormat() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        $this->/** @scrutinizer ignore-call */ 
71
               setReturnFormat(self::RETURN_ARRAY);
Loading history...
Bug introduced by
The constant Puerari\Moodle\Core::RETURN_ARRAY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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