/
block_game_content_unlock.php
156 lines (128 loc) · 4.78 KB
/
block_game_content_unlock.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/blocks/game_content_unlock/lib.php');
class block_game_content_unlock extends block_base
{
public static $resource_events = array('\block_game_points\event\points_earned', '\block_game_achievements\event\achievement_reached', '\block_game_content_unlock\event\content_unlocked');
public function init()
{
$this->title = get_string('title', 'block_game_content_unlock');
}
public function applicable_formats()
{
return array(
'all' => true
);
}
public function instance_allow_multiple()
{
return true;
}
public function get_content()
{
global $DB, $USER;
$this->content = new stdClass;
$this->content->text = '';
$this->content->footer = '';
if(user_has_role_assignment($USER->id, 5))
{
$eventsarray = content_unlock_generate_events_list();
$us = $DB->get_records('content_unlock_system', array('deleted' => 0, 'blockinstanceid' => $this->instance->id));
if(!empty($us))
{
$unlocklist = '';
$course = $DB->get_record('course', array('id' => $this->page->course->id));
$info = get_fast_modinfo($course);
foreach($us as $unlocksystem)
{
$sql = "SELECT *
FROM {content_unlock_log} c
INNER JOIN {logstore_standard_log} l ON c.logid = l.id
WHERE l.userid = :userid
AND c.unlocksystemid = :unlocksystemid";
$params['userid'] = $USER->id;
$params['unlocksystemid'] = $unlocksystem->id;
$unlocked_content = $DB->record_exists_sql($sql, $params);
if($unlocked_content)
{
continue;
}
$ccm = get_course_and_cm_from_cmid($unlocksystem->coursemoduleid);
if($this->page->course->id != $ccm[0]->id)
{
continue;
}
if(!(content_unlock_satisfies_conditions($unlocksystem->restrictions, $this->page->course->id, $USER->id) && (in_array($unlocksystem->conditions, self::$resource_events) || content_unlock_satisfies_block_conditions($unlocksystem, $this->page->course->id, $USER->id))))
{
continue;
}
$cm = $info->get_cm($unlocksystem->coursemoduleid);
$eventdescription = is_null($unlocksystem->eventdescription) ? $eventsarray[$unlocksystem->conditions] : $unlocksystem->eventdescription;
$unlocklist = $unlocklist . '<li>' . $cm->name . ' (' . get_string('modulename', $cm->modname) . ') por ' . (in_array($unlocksystem->conditions, self::$resource_events) ? content_unlock_get_block_conditions_text($unlocksystem) : $eventdescription) . '</li>';
}
if(strlen($unlocklist) > 0)
{
$this->content->text = '<p>Você pode desbloquear:<ul>' . $unlocklist . '</ul></p>';
}
}
if(isset($this->config))
{
$lastunlocksnumber = isset($this->config->lastunlocksnumber) ? $this->config->lastunlocksnumber : 1;
}
else
{
$lastunlocksnumber = 0;
}
if($lastunlocksnumber > 0)
{
$sql = "SELECT c.id as id, s.coursemoduleid as coursemoduleid, s.eventdescription as eventdescription, s.conditions as conditions, s.connective as connective
FROM
{content_unlock_log} c
INNER JOIN {logstore_standard_log} l ON c.logid = l.id
INNER JOIN {content_unlock_system} s ON c.unlocksystemid = s.id
WHERE l.userid = :userid
AND l.courseid = :courseid
AND s.blockinstanceid = :blockinstanceid
ORDER BY c.id DESC";
$params['userid'] = $USER->id;
$params['courseid'] = $this->page->course->id;
$params['blockinstanceid'] = $this->instance->id;
$lastunlocks = $DB->get_records_sql($sql, $params, 0, $lastunlocksnumber);
if(!empty($lastunlocks))
{
$lastunlockslist = '';
foreach($lastunlocks as $lu)
{
$ccm = get_course_and_cm_from_cmid($lu->coursemoduleid);
$course = $DB->get_record('course', array('id' => $ccm[0]->id));
$info = get_fast_modinfo($course);
$cm = $info->get_cm($lu->coursemoduleid);
$eventdescription = is_null($lu->eventdescription) ? $eventsarray[$lu->conditions] : $lu->eventdescription;
$lastunlockslist = $lastunlockslist . '<li>' . $cm->name . ' (' . get_string('modulename', $cm->modname) . ') por ' . (in_array($lu->conditions, self::$resource_events) ? content_unlock_get_block_conditions_text($lu) : $eventdescription) . '</li>';
}
$this->content->text = $this->content->text . '<p>Você desbloqueou recentemente:<ul>' . $lastunlockslist . '</ul></p>';
}
}
}
return $this->content;
}
public function specialization()
{
if(isset($this->config))
{
if(empty($this->config->title))
{
$this->title = get_string('title', 'block_game_content_unlock');
}
else
{
$this->title = $this->config->title;
}
}
}
public function has_config()
{
return true;
}
}
?>