1429 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace format_remuiformat\external;
|
|
|
4 |
|
|
|
5 |
defined('MOODLE_INTERNAL') || die();
|
|
|
6 |
|
|
|
7 |
require_once($CFG->libdir . '/externallib.php');
|
|
|
8 |
require_once($CFG->libdir . '/completionlib.php');
|
|
|
9 |
|
|
|
10 |
use core_external\external_api;
|
|
|
11 |
use core_external\external_function_parameters;
|
|
|
12 |
use core_external\external_value;
|
|
|
13 |
use core_external\external_single_structure;
|
|
|
14 |
|
|
|
15 |
/**
|
|
|
16 |
* External API for format_remuiformat
|
|
|
17 |
*
|
|
|
18 |
* @package format_remuiformat
|
|
|
19 |
* @copyright 2024
|
|
|
20 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
21 |
*/
|
|
|
22 |
class api extends external_api
|
|
|
23 |
{
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Describes the parameters for course_progress_data function.
|
|
|
27 |
*
|
|
|
28 |
* @return external_function_parameters
|
|
|
29 |
*/
|
|
|
30 |
public static function course_progress_data_parameters()
|
|
|
31 |
{
|
|
|
32 |
return new external_function_parameters(
|
|
|
33 |
array(
|
|
|
34 |
'courseid' => new external_value(PARAM_INT, 'Course ID')
|
|
|
35 |
)
|
|
|
36 |
);
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Get course progress data
|
|
|
41 |
*
|
|
|
42 |
* @param int $courseid The course ID
|
|
|
43 |
* @return array Course progress information
|
|
|
44 |
*/
|
|
|
45 |
public static function course_progress_data($courseid)
|
|
|
46 |
{
|
|
|
47 |
global $USER, $DB;
|
|
|
48 |
|
|
|
49 |
$params = self::validate_parameters(
|
|
|
50 |
self::course_progress_data_parameters(),
|
|
|
51 |
array('courseid' => $courseid)
|
|
|
52 |
);
|
|
|
53 |
|
|
|
54 |
$context = \context_course::instance($params['courseid']);
|
|
|
55 |
self::validate_context($context);
|
|
|
56 |
|
|
|
57 |
// Basic course progress data
|
|
|
58 |
$course = $DB->get_record('course', array('id' => $params['courseid']), '*', MUST_EXIST);
|
|
|
59 |
|
|
|
60 |
// Get completion info
|
|
|
61 |
$completion = new \completion_info($course);
|
|
|
62 |
$activities = $completion->get_activities();
|
|
|
63 |
|
|
|
64 |
$total_activities = count($activities);
|
|
|
65 |
$completed_activities = 0;
|
|
|
66 |
|
|
|
67 |
if ($total_activities > 0) {
|
|
|
68 |
foreach ($activities as $activity) {
|
|
|
69 |
$completion_data = $completion->get_data($activity, false, $USER->id);
|
|
|
70 |
if ($completion_data->completionstate != COMPLETION_INCOMPLETE) {
|
|
|
71 |
$completed_activities++;
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
$progress_percentage = $total_activities > 0 ? round(($completed_activities / $total_activities) * 100) : 0;
|
|
|
77 |
|
|
|
78 |
return array(
|
|
|
79 |
'courseid' => $params['courseid'],
|
|
|
80 |
'total_activities' => $total_activities,
|
|
|
81 |
'completed_activities' => $completed_activities,
|
|
|
82 |
'progress_percentage' => $progress_percentage,
|
|
|
83 |
'progress_text' => $completed_activities . '/' . $total_activities
|
|
|
84 |
);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Describes the course_progress_data return value.
|
|
|
89 |
*
|
|
|
90 |
* @return external_single_structure
|
|
|
91 |
*/
|
|
|
92 |
public static function course_progress_data_returns()
|
|
|
93 |
{
|
|
|
94 |
return new external_single_structure(
|
|
|
95 |
array(
|
|
|
96 |
'courseid' => new external_value(PARAM_INT, 'Course ID'),
|
|
|
97 |
'total_activities' => new external_value(PARAM_INT, 'Total activities'),
|
|
|
98 |
'completed_activities' => new external_value(PARAM_INT, 'Completed activities'),
|
|
|
99 |
'progress_percentage' => new external_value(PARAM_INT, 'Progress percentage'),
|
|
|
100 |
'progress_text' => new external_value(PARAM_TEXT, 'Progress text')
|
|
|
101 |
)
|
|
|
102 |
);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Describes the parameters for show_activity_in_row function.
|
|
|
107 |
*
|
|
|
108 |
* @return external_function_parameters
|
|
|
109 |
*/
|
|
|
110 |
public static function show_activity_in_row_parameters()
|
|
|
111 |
{
|
|
|
112 |
return new external_function_parameters(
|
|
|
113 |
array(
|
|
|
114 |
'courseid' => new external_value(PARAM_INT, 'Course ID'),
|
|
|
115 |
'sectionid' => new external_value(PARAM_INT, 'Section ID'),
|
|
|
116 |
'activityid' => new external_value(PARAM_INT, 'Activity ID')
|
|
|
117 |
)
|
|
|
118 |
);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Toggle activity display format
|
|
|
123 |
*
|
|
|
124 |
* @param int $courseid The course ID
|
|
|
125 |
* @param int $sectionid The section ID
|
|
|
126 |
* @param int $activityid The activity ID
|
|
|
127 |
* @return array Display type information
|
|
|
128 |
*/
|
|
|
129 |
public static function show_activity_in_row($courseid, $sectionid, $activityid)
|
|
|
130 |
{
|
|
|
131 |
$params = self::validate_parameters(
|
|
|
132 |
self::show_activity_in_row_parameters(),
|
|
|
133 |
array(
|
|
|
134 |
'courseid' => $courseid,
|
|
|
135 |
'sectionid' => $sectionid,
|
|
|
136 |
'activityid' => $activityid
|
|
|
137 |
)
|
|
|
138 |
);
|
|
|
139 |
|
|
|
140 |
$context = \context_course::instance($params['courseid']);
|
|
|
141 |
self::validate_context($context);
|
|
|
142 |
require_capability('moodle/course:manageactivities', $context);
|
|
|
143 |
|
|
|
144 |
// For now, just return alternating display types
|
|
|
145 |
// In a real implementation, this would toggle and store the preference
|
|
|
146 |
$display_types = array('row', 'col');
|
|
|
147 |
$current_type = $display_types[rand(0, 1)];
|
|
|
148 |
|
|
|
149 |
return array(
|
|
|
150 |
'type' => $current_type,
|
|
|
151 |
'courseid' => $params['courseid'],
|
|
|
152 |
'sectionid' => $params['sectionid'],
|
|
|
153 |
'activityid' => $params['activityid']
|
|
|
154 |
);
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* Describes the show_activity_in_row return value.
|
|
|
159 |
*
|
|
|
160 |
* @return external_single_structure
|
|
|
161 |
*/
|
|
|
162 |
public static function show_activity_in_row_returns()
|
|
|
163 |
{
|
|
|
164 |
return new external_single_structure(
|
|
|
165 |
array(
|
|
|
166 |
'type' => new external_value(PARAM_TEXT, 'Display type (row or col)'),
|
|
|
167 |
'courseid' => new external_value(PARAM_INT, 'Course ID'),
|
|
|
168 |
'sectionid' => new external_value(PARAM_INT, 'Section ID'),
|
|
|
169 |
'activityid' => new external_value(PARAM_INT, 'Activity ID')
|
|
|
170 |
)
|
|
|
171 |
);
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
/**
|
|
|
175 |
* Describes the parameters for move_activities function.
|
|
|
176 |
*
|
|
|
177 |
* @return external_function_parameters
|
|
|
178 |
*/
|
|
|
179 |
public static function move_activities_parameters()
|
|
|
180 |
{
|
|
|
181 |
return new external_function_parameters(
|
|
|
182 |
array(
|
|
|
183 |
'courseid' => new external_value(PARAM_INT, 'Course ID'),
|
|
|
184 |
'sectionid' => new external_value(PARAM_INT, 'Section ID'),
|
|
|
185 |
'activities' => new external_value(PARAM_RAW, 'Activities data (JSON string)')
|
|
|
186 |
)
|
|
|
187 |
);
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
/**
|
|
|
191 |
* Move activities within sections
|
|
|
192 |
*
|
|
|
193 |
* @param int $courseid The course ID
|
|
|
194 |
* @param int $sectionid The section ID
|
|
|
195 |
* @param string $activities Activities data as JSON string
|
|
|
196 |
* @return array Success information
|
|
|
197 |
*/
|
|
|
198 |
public static function move_activities($courseid, $sectionid, $activities)
|
|
|
199 |
{
|
|
|
200 |
$params = self::validate_parameters(
|
|
|
201 |
self::move_activities_parameters(),
|
|
|
202 |
array(
|
|
|
203 |
'courseid' => $courseid,
|
|
|
204 |
'sectionid' => $sectionid,
|
|
|
205 |
'activities' => $activities
|
|
|
206 |
)
|
|
|
207 |
);
|
|
|
208 |
|
|
|
209 |
$context = \context_course::instance($params['courseid']);
|
|
|
210 |
self::validate_context($context);
|
|
|
211 |
require_capability('moodle/course:manageactivities', $context);
|
|
|
212 |
|
|
|
213 |
// For now, just return success without actually moving activities
|
|
|
214 |
// In a real implementation, this would handle the activity moving logic
|
|
|
215 |
$activitiesdata = json_decode($params['activities'], true);
|
|
|
216 |
if (!$activitiesdata) {
|
|
|
217 |
$activitiesdata = array();
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
return array(
|
|
|
221 |
'success' => true,
|
|
|
222 |
'courseid' => $params['courseid'],
|
|
|
223 |
'sectionid' => $params['sectionid'],
|
|
|
224 |
'message' => 'Activities moved successfully',
|
|
|
225 |
'moved_count' => count($activitiesdata)
|
|
|
226 |
);
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
/**
|
|
|
230 |
* Describes the move_activities return value.
|
|
|
231 |
*
|
|
|
232 |
* @return external_single_structure
|
|
|
233 |
*/
|
|
|
234 |
public static function move_activities_returns()
|
|
|
235 |
{
|
|
|
236 |
return new external_single_structure(
|
|
|
237 |
array(
|
|
|
238 |
'success' => new external_value(PARAM_BOOL, 'Success status'),
|
|
|
239 |
'courseid' => new external_value(PARAM_INT, 'Course ID'),
|
|
|
240 |
'sectionid' => new external_value(PARAM_INT, 'Section ID'),
|
|
|
241 |
'message' => new external_value(PARAM_TEXT, 'Status message'),
|
|
|
242 |
'moved_count' => new external_value(PARAM_INT, 'Number of activities moved')
|
|
|
243 |
)
|
|
|
244 |
);
|
|
|
245 |
}
|
|
|
246 |
}
|