Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 12... Línea 12...
12
// GNU General Public License for more details.
12
// GNU General Public License for more details.
13
//
13
//
14
// You should have received a copy of the GNU General Public License
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea -...
16
 
-
 
17
/**
-
 
18
 * Badge assertion library.
-
 
19
 *
-
 
20
 * @package    core
-
 
21
 * @subpackage badges
-
 
22
 * @copyright  2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
-
 
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
24
 * @author     Yuliya Bozhko <yuliya.bozhko@totaralms.com>
-
 
25
 */
-
 
26
 
16
 
Línea 27... Línea 17...
27
namespace core_badges;
17
namespace core_badges;
Línea 28... Línea 18...
28
 
18
 
Línea 42... Línea 32...
42
use stdClass;
32
use stdClass;
Línea 43... Línea 33...
43
 
33
 
44
/**
34
/**
45
 * Class that represents badge.
35
 * Class that represents badge.
-
 
36
 *
46
 *
37
 * @package    core_badges
47
 * @copyright  2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
38
 * @copyright  2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
-
 
39
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 * @author     Yuliya Bozhko <yuliya.bozhko@totaralms.com>
49
 */
41
 */
50
class badge {
42
class badge {
51
    /** @var int Badge id */
43
    /** @var int Badge id */
Línea 112... Línea 104...
112
    public $version;
104
    public $version;
Línea 113... Línea 105...
113
 
105
 
114
    /** @var string What language is this badge written in. */
106
    /** @var string What language is this badge written in. */
Línea 115... Línea -...
115
    public $language;
-
 
116
 
-
 
117
    /** @var string The author of the image for this badge. */
-
 
118
    public $imageauthorname;
-
 
119
 
-
 
120
    /** @var string The email of the author of the image for this badge. */
-
 
121
    public $imageauthoremail;
-
 
122
 
-
 
123
    /** @var string The url of the author of the image for this badge. */
-
 
124
    public $imageauthorurl;
107
    public $language;
125
 
108
 
Línea 126... Línea 109...
126
    /** @var string The caption of the image for this badge. */
109
    /** @var string The caption of the image for this badge. */
127
    public $imagecaption;
110
    public $imagecaption;
Línea 166... Línea 149...
166
            if (property_exists($this, $field)) {
149
            if (property_exists($this, $field)) {
167
                $this->{$field} = $value;
150
                $this->{$field} = $value;
168
            }
151
            }
169
        }
152
        }
Línea 170... Línea -...
170
 
-
 
171
        if (badges_open_badges_backpack_api() != OPEN_BADGES_V1) {
-
 
172
            // For Open Badges 2 we need to use a single site issuer with no exceptions.
-
 
173
            $issuer = badges_get_default_issuer();
-
 
174
            $this->issuername = $issuer['name'];
-
 
175
            $this->issuercontact = $issuer['email'];
-
 
176
            $this->issuerurl = $issuer['url'];
-
 
177
        }
-
 
178
 
153
 
179
        $this->criteria = self::get_criteria();
154
        $this->criteria = self::get_criteria();
Línea 180... Línea 155...
180
    }
155
    }
181
 
156
 
Línea 960... Línea 935...
960
     *
935
     *
961
     * @param int $obversion OB version to use.
936
     * @param int $obversion OB version to use.
962
     * @return array Issuer informations of the badge.
937
     * @return array Issuer informations of the badge.
963
     */
938
     */
964
    public function get_badge_issuer(?int $obversion = null) {
939
    public function get_badge_issuer(?int $obversion = null) {
965
        global $DB;
-
 
966
 
-
 
967
        $issuer = [];
-
 
968
        if ($obversion == OPEN_BADGES_V1) {
-
 
969
            $data = $DB->get_record('badge', ['id' => $this->id]);
-
 
970
            $issuer['name'] = $data->issuername;
-
 
971
            $issuer['url'] = $data->issuerurl;
-
 
972
            $issuer['email'] = $data->issuercontact;
-
 
973
        } else {
940
        return [
974
            $issuer['name'] = $this->issuername;
941
            'name' => $this->issuername,
975
            $issuer['url'] = $this->issuerurl;
942
            'url' => $this->issuerurl,
976
            $issuer['email'] = $this->issuercontact;
943
            'email' => $this->issuercontact,
977
            $issuer['@context'] = OPEN_BADGES_V2_CONTEXT;
944
            '@context' => OPEN_BADGES_V2_CONTEXT,
978
            $issueridurl = new moodle_url('/badges/issuer_json.php', array('id' => $this->id));
945
            'id' => (new moodle_url('/badges/issuer_json.php', ['id' => $this->id]))->out(false),
979
            $issuer['id'] = $issueridurl->out(false);
-
 
980
            $issuer['type'] = OPEN_BADGES_V2_TYPE_ISSUER;
946
            'type' => OPEN_BADGES_V2_TYPE_ISSUER,
981
        }
947
        ];
982
 
-
 
983
        return $issuer;
-
 
984
    }
948
    }
Línea 985... Línea 949...
985
 
949
 
986
    /**
950
    /**
987
     * Get tags of badge.
951
     * Get tags of badge.
988
     *
952
     *
989
     * @return array Badge tags.
953
     * @return array Badge tags.
990
     */
954
     */
991
    public function get_badge_tags(): array {
955
    public function get_badge_tags(): array {
992
        return array_values(\core_tag_tag::get_item_tags_array('core_badges', 'badge', $this->id));
956
        return array_values(\core_tag_tag::get_item_tags_array('core_badges', 'badge', $this->id));
-
 
957
    }
-
 
958
 
-
 
959
    /**
-
 
960
     * Create a badge, to store it in the database.
-
 
961
     *
-
 
962
     * @param stdClass $data Data to create a badge.
-
 
963
     * @param int|null $courseid The course where the badge will be added.
-
 
964
     * @return badge The badge object created.
-
 
965
     */
-
 
966
    public static function create_badge(stdClass $data, ?int $courseid = null): badge {
-
 
967
        global $DB, $USER, $CFG;
-
 
968
 
-
 
969
        $now = time();
-
 
970
 
-
 
971
        $fordb = new stdClass();
-
 
972
        $fordb->id = null;
-
 
973
        $fordb->courseid = $courseid;
-
 
974
        $fordb->type = $courseid ? BADGE_TYPE_COURSE : BADGE_TYPE_SITE;
-
 
975
        $fordb->name = trim($data->name);
-
 
976
        $fordb->version = $data->version;
-
 
977
        $fordb->language = $data->language;
-
 
978
        $fordb->description = $data->description;
-
 
979
        $fordb->imagecaption = $data->imagecaption;
-
 
980
        $fordb->timecreated = $now;
-
 
981
        $fordb->timemodified = $now;
-
 
982
        $fordb->usercreated = $USER->id;
-
 
983
        $fordb->usermodified = $USER->id;
-
 
984
        $fordb->issuername = $data->issuername;
-
 
985
        $fordb->issuerurl = $data->issuerurl;
-
 
986
        $fordb->issuercontact = $data->issuercontact;
-
 
987
 
-
 
988
        if (!property_exists($data, 'expiry')) {
-
 
989
            $data->expiry = 0;
-
 
990
        }
-
 
991
        $fordb->expiredate = ($data->expiry == 1) ? $data->expiredate : null;
-
 
992
        $fordb->expireperiod = ($data->expiry == 2) ? $data->expireperiod : null;
-
 
993
        $fordb->messagesubject = get_string('messagesubject', 'badges');
-
 
994
        $fordb->message = get_string('messagebody', 'badges',
-
 
995
                html_writer::link($CFG->wwwroot . '/badges/mybadges.php', get_string('managebadges', 'badges')));
-
 
996
        $fordb->attachment = 1;
-
 
997
        $fordb->notification = BADGE_MESSAGE_NEVER;
-
 
998
        $fordb->status = BADGE_STATUS_INACTIVE;
-
 
999
 
-
 
1000
        $badgeid = $DB->insert_record('badge', $fordb, true);
-
 
1001
 
-
 
1002
        if ($courseid) {
-
 
1003
            $course = get_course($courseid);
-
 
1004
            $context = context_course::instance($course->id);
-
 
1005
        } else {
-
 
1006
            $context = context_system::instance();
-
 
1007
        }
-
 
1008
 
-
 
1009
        // Trigger event, badge created.
-
 
1010
        $eventparams = [
-
 
1011
            'objectid' => $badgeid,
-
 
1012
            'context' => $context,
-
 
1013
        ];
-
 
1014
        $event = \core\event\badge_created::create($eventparams);
-
 
1015
        $event->trigger();
-
 
1016
 
-
 
1017
        $badge = new badge($badgeid);
-
 
1018
        if (property_exists($data, 'tags')) {
-
 
1019
            \core_tag_tag::set_item_tags('core_badges', 'badge', $badgeid, $context, $data->tags);
-
 
1020
        }
-
 
1021
 
-
 
1022
        return $badge;
-
 
1023
    }
-
 
1024
 
-
 
1025
    /**
-
 
1026
     * Update badge data.
-
 
1027
     *
-
 
1028
     * @param stdClass $data Data to update a badge.
-
 
1029
     * @return bool A status for update a badge.
-
 
1030
     */
-
 
1031
    public function update(stdClass $data): bool {
-
 
1032
        global $USER;
-
 
1033
 
-
 
1034
        $this->name = trim($data->name);
-
 
1035
        $this->version = trim($data->version);
-
 
1036
        $this->language = $data->language;
-
 
1037
        $this->description = $data->description;
-
 
1038
        $this->imagecaption = $data->imagecaption;
-
 
1039
        $this->usermodified = $USER->id;
-
 
1040
        $this->issuername = $data->issuername;
-
 
1041
        $this->issuerurl = $data->issuerurl;
-
 
1042
        $this->issuercontact = $data->issuercontact;
-
 
1043
        $this->expiredate = ($data->expiry == 1) ? $data->expiredate : null;
-
 
1044
        $this->expireperiod = ($data->expiry == 2) ? $data->expireperiod : null;
-
 
1045
 
-
 
1046
        // Need to unset message_editor options to avoid errors on form edit.
-
 
1047
        unset($this->messageformat);
-
 
1048
        unset($this->message_editor);
-
 
1049
 
-
 
1050
        if (!$this->save()) {
-
 
1051
            return false;
-
 
1052
        }
-
 
1053
 
-
 
1054
        \core_tag_tag::set_item_tags('core_badges', 'badge', $this->id, $this->get_context(), $data->tags);
-
 
1055
 
-
 
1056
        return true;
-
 
1057
    }
-
 
1058
 
-
 
1059
    /**
-
 
1060
     * Update the message of badge.
-
 
1061
     *
-
 
1062
     * @param stdClass $data Data to update a badge message.
-
 
1063
     * @return bool A status for update a badge message.
-
 
1064
     */
-
 
1065
    public function update_message(stdClass $data): bool {
-
 
1066
        // Calculate next message cron if form data is different from original badge data.
-
 
1067
        if ($data->notification != $this->notification) {
-
 
1068
            if ($data->notification > BADGE_MESSAGE_ALWAYS) {
-
 
1069
                $this->nextcron = badges_calculate_message_schedule($data->notification);
-
 
1070
            } else {
-
 
1071
                $this->nextcron = null;
-
 
1072
            }
-
 
1073
        }
-
 
1074
 
-
 
1075
        $this->message = clean_text($data->message_editor['text'], FORMAT_HTML);
-
 
1076
        $this->messagesubject = $data->messagesubject;
-
 
1077
        $this->notification = $data->notification;
-
 
1078
        $this->attachment = $data->attachment;
-
 
1079
 
-
 
1080
        unset($this->messageformat);
-
 
1081
        unset($this->message_editor);
-
 
1082
        return $this->save();
993
    }
1083
    }