dta_recommendation.php 2.76 KB
Newer Older
1
<?php
2
// This file is part of Moodle - http://moodle.org/.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 
// 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/>.

/**
18
 * Entity class for DTA submission plugin recommendation.
19
20
21
22
23
24
25
26
27
 *
 * @package    assignsubmission_dta
 * @copyright  2023 Gero Lueckemeyer
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace assignsubmission_dta\models;

/**
28
 * Entity class for DTA submission plugin recommendation.
29
30
31
32
33
34
35
36
37
38
39
40
41
 *
 * @package    assignsubmission_dta
 * @copyright  2023
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class dta_recommendation {

    /**
     * @var string $topic Topic of the recommendation.
     */
    public $topic;

    /**
42
     * @var string $exerciseName Name of the exercise.
43
     */
44
    public $exerciseName;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

    /**
     * @var string $url URL of the exercise.
     */
    public $url;

    /**
     * @var int $difficulty Difficulty level of the exercise.
     */
    public $difficulty;

    /**
     * @var int $score Score associated with the recommendation.
     */
    public $score;

    /**
62
     * Decodes the JSON recommendations returned by the backend service call into an array of dta_recommendation objects.
63
     *
64
65
     * @param string $jsonstring JSON string containing recommendations.
     * @return array Array of dta_recommendation objects.
66
     */
67
    public static function assignsubmission_dta_decode_json_recommendations(string $jsonstring): array {
68
69
70
        $response = json_decode($jsonstring);
        $recommendations = [];

71
        // Check if recommendations exist.
72
73
74
75
        if (!empty($response->recommendations)) {
            foreach ($response->recommendations as $recommendation) {
                $rec = new dta_recommendation();
                $rec->topic = $recommendation->topic ?? null;
76
77
                // Map correct fields to the renamed variable names:
                $rec->exerciseName = $recommendation->url ?? null;
78
79
80
81
82
83
84
85
86
87
88
                $rec->url = $recommendation->exerciseName ?? null;
                $rec->difficulty = $recommendation->difficulty ?? null;
                $rec->score = $recommendation->score ?? null;

                $recommendations[] = $rec;
            }
        }

        return $recommendations;
    }
}