1 |
efrain |
1 |
<?php
|
|
|
2 |
defined('MOODLE_INTERNAL') || die();
|
|
|
3 |
|
|
|
4 |
use core_completion\progress;
|
|
|
5 |
|
|
|
6 |
global $CFG;
|
|
|
7 |
require_once $CFG->libdir . '/externallib.php';
|
|
|
8 |
require_once $CFG->dirroot . '/course/classes/category.php';
|
|
|
9 |
require_once $CFG->dirroot . '/blocks/moodleblock.class.php';
|
|
|
10 |
require_once $CFG->dirroot . '/blocks/course_list/block_course_list.php';
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
class block_cursos_recientes_ajax_external extends \external_api
|
|
|
14 |
{
|
|
|
15 |
public static function get_listado_de_cursos_recientes_parameters()
|
|
|
16 |
{
|
|
|
17 |
return new \external_function_parameters([
|
|
|
18 |
'category_id' => new \external_value(PARAM_INT, 'Categoría de los Cursos', VALUE_DEFAULT, 0),
|
|
|
19 |
'search_text' => new \external_value(PARAM_ALPHANUM, 'Palabra de busqueda', VALUE_DEFAULT, ' ' ),
|
|
|
20 |
]);
|
|
|
21 |
}
|
|
|
22 |
|
|
|
23 |
public static function get_listado_de_cursos_recientes($category_id, $search_text)
|
|
|
24 |
{
|
|
|
25 |
|
|
|
26 |
/*
|
|
|
27 |
[
|
|
|
28 |
'category_id' => $category_id,
|
|
|
29 |
'search_text' => $search_text,
|
|
|
30 |
] = self::validate_parameters(self::execute_parameters(), [
|
|
|
31 |
'category_id' => $category_id,
|
|
|
32 |
'search_text' => $search_text
|
|
|
33 |
]);
|
|
|
34 |
*/
|
|
|
35 |
|
|
|
36 |
global $USER, $DB, $CFG, $OUTPUT, $PAGE;
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
$userid = $USER->id;
|
|
|
40 |
$url_noimage = $CFG->wwwroot . '/theme/' . $PAGE->theme->name . '/pix/coursenoimage.jpg';
|
|
|
41 |
|
|
|
42 |
$courses = [];
|
|
|
43 |
$courselist = array();
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
$mycourses = enrol_get_users_courses($userid);
|
|
|
47 |
$courselist = array();
|
|
|
48 |
foreach ($mycourses as $course)
|
|
|
49 |
{
|
|
|
50 |
if(!$course->visible) {
|
|
|
51 |
continue;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
if($search_text) {
|
|
|
56 |
if(stripos($course->fullname, $search_text) === false) {
|
|
|
57 |
continue;
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
if($category_id) {
|
|
|
62 |
if($category_id != $course->category) {
|
|
|
63 |
continue;
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
$category = $DB->get_record('course_categories',array('id'=>$course->category, 'visible' => 1));
|
|
|
68 |
if(!$category) {
|
|
|
69 |
continue;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
if ($course instanceof stdClass) {
|
|
|
74 |
$courseInList = new core_course_list_element($course);
|
|
|
75 |
}
|
|
|
76 |
$image = $url_noimage;
|
|
|
77 |
foreach ($courseInList->get_course_overviewfiles() as $file)
|
|
|
78 |
{
|
|
|
79 |
$isimage = $file->is_valid_image();
|
|
|
80 |
$image = file_encode_url("{$CFG->wwwroot}/pluginfile.php", '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage);
|
|
|
81 |
if (!$isimage) {
|
|
|
82 |
$image = $url_noimage;
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
$lastaccess = null;
|
|
|
87 |
$sql = "select timecreated from {logstore_standard_log} where courseid = :courseid and userid = :userid " .
|
|
|
88 |
" and eventname = '\\\\core\\\\event\\\\course_viewed' order by id desc limit 1 ";
|
|
|
89 |
|
|
|
90 |
$timecreated = $DB->get_field_sql($sql, array('courseid' => $course->id, 'userid' => $userid));
|
|
|
91 |
if($timecreated) {
|
|
|
92 |
|
|
|
93 |
$lastaccess = date('d/m/Y h:i a', $timecreated);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
$miProgreso = number_format(progress::get_course_progress_percentage($course),2); // Progreso por curso
|
|
|
99 |
|
|
|
100 |
|
|
|
101 |
$first_section = 0;
|
|
|
102 |
$sections = $DB->get_records('course_sections', ['course' => $course->id], 'section ASC', 'id,name,section,sequence,visible');
|
|
|
103 |
|
|
|
104 |
foreach($sections as $section)
|
|
|
105 |
{
|
|
|
106 |
if(!empty($section->section)) {
|
|
|
107 |
$first_section = $section->id;
|
|
|
108 |
break;
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
$course_context = context_course::instance($course->id);
|
|
|
112 |
$roles = get_user_roles($course_context, $USER->id, true);
|
|
|
113 |
|
|
|
114 |
$completion_edit_curso = false;
|
|
|
115 |
foreach ($roles as $role) {
|
|
|
116 |
if ($role->shortname == 'companydepartmentmanager' || $role->shortname == 'companycoursenoneditor') {
|
|
|
117 |
$completion_edit_curso = true;
|
|
|
118 |
break;
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
if (has_capability('moodle/course:manageactivities', $course_context, $USER->id) || has_capability('moodle/course:viewhiddenactivities', $course_context, $USER->id) || $completion_edit_curso) {
|
|
|
123 |
$editurl = $CFG->wwwroot . '/course/view.php?id='.$course->id.'¬ifyeditingon=1';
|
|
|
124 |
|
|
|
125 |
//$editurl = $CFG->wwwroot . '/course/edit.php?id=' . $course->id;
|
|
|
126 |
} else {
|
|
|
127 |
$editurl = '';
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
|
132 |
$modules = get_fast_modinfo($course->id)->get_cms();
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
$linkurl = '';
|
|
|
137 |
foreach ($modules as $module)
|
|
|
138 |
{
|
|
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
|
|
|
143 |
//print "!".$module->uservisible ."||". $module->is_stealth() ."||". $module->url ."||". $module->section."<br>";
|
|
|
144 |
if (!$module->uservisible || $module->is_stealth() || empty($module->url) || empty($module->section)) {
|
|
|
145 |
continue;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
if($module->section == $first_section || $completion_edit_curso) {
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
|
152 |
|
|
|
153 |
|
|
|
154 |
$linkurl = new moodle_url($module->url, array('forceview' => 1));
|
|
|
155 |
//print_r($linkurl);
|
|
|
156 |
//var_dump($linkurl);
|
|
|
157 |
break;
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
/*
|
|
|
161 |
if( $completion_edit_curso){
|
|
|
162 |
$linkurl = $CFG->wwwroot . '/course/view.php?id=' . $course->id;
|
|
|
163 |
}*/
|
|
|
164 |
|
|
|
165 |
$summary = trim(strip_tags($course->summary));
|
|
|
166 |
|
|
|
167 |
if(empty($summary)) {
|
|
|
168 |
$summary = `<p></p>`;
|
|
|
169 |
} else if(strlen($summary) > 80) {
|
|
|
170 |
|
|
|
171 |
$summary = substr($summary, 0, 80) . '...';
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
$courses[] = [
|
|
|
175 |
|
|
|
176 |
'coursecategory' => $category->name,
|
|
|
177 |
'courseimage' => $image,
|
|
|
178 |
'enddate' => $course->enddate,
|
|
|
179 |
'fullname' => $course->fullname,
|
|
|
180 |
'fullnamedisplay' => get_course_display_name_for_list($course),
|
|
|
181 |
'hasprogress' => true,
|
|
|
182 |
'hidden' => false,
|
|
|
183 |
'id' => $course->id,
|
|
|
184 |
'idnumber' => $course->idnumber,
|
|
|
185 |
'isfavourite' => false,
|
|
|
186 |
'progress' => $miProgreso,
|
|
|
187 |
'shortname' => $course->shortname,
|
|
|
188 |
'showshortname' => false,
|
|
|
189 |
'startdate' => $course->startdate,
|
|
|
190 |
'summary' => $summary,
|
|
|
191 |
'summaryformat' => $course->summaryformat,
|
|
|
192 |
'timeaccess' => $lastaccess,
|
|
|
193 |
'viewurl' => $linkurl,
|
|
|
194 |
'editurl' => $editurl,
|
|
|
195 |
'visible' => true,
|
|
|
196 |
|
|
|
197 |
];
|
|
|
198 |
|
|
|
199 |
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
$data = [
|
|
|
203 |
'courses' => $courses
|
|
|
204 |
];
|
|
|
205 |
|
|
|
206 |
$cards = $OUTPUT->render_from_template('block_cursos_recientes_ajax/cards', $data);
|
|
|
207 |
|
|
|
208 |
|
|
|
209 |
return json_encode(['success' => true, 'search_text' => $search_text, 'category_id' => $category_id, 'cards' => $cards]);
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
|
|
|
213 |
public static function get_listado_de_cursos_recientes_returns()
|
|
|
214 |
{
|
|
|
215 |
return new \external_value(PARAM_RAW, 'The updated JSON output');
|
|
|
216 |
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
|
222 |
|
|
|
223 |
|
|
|
224 |
|
|
|
225 |
|
|
|
226 |
}
|