PageRenderTime 59ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/moodle1917/webserviceMoodle1.9.17/moduloCursos.php

https://github.com/gustavoramirezrugerio/moodle1.9.17
PHP | 1020 lines | 698 code | 28 blank | 294 comment | 117 complexity | 8c0e6de7a47dbc38f3f3384d885dd5fd MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-2.0, GPL-2.0
  1. <?php
  2. function modulocursosRegistrarCurso($curso) {
  3. ob_start();
  4. //Para evitar que imprima los echos
  5. global $CFG;
  6. //global $USER;
  7. $maxbyte = 0;
  8. foreach (get_max_upload_sizes($CFG->maxbytes) as $max => $tmp) {
  9. $maxbyte = $max;
  10. break;
  11. }
  12. $curso['MAX_FILE_SIZE'] = $maxbyte;
  13. $curso["timemodified"] = time();
  14. $curso['fullname'] = utf8_encode($curso['fullname']);
  15. $curso['shortname'] = utf8_encode($curso['shortname']);
  16. $curso['summary'] = utf8_encode($curso['summary']);
  17. $curso['category'] = utf8_encode($curso['category']);
  18. $curso['idnumber'] = utf8_encode($curso['idnumber']);
  19. $curso["format"] = "weekssae";
  20. $curso["numsections"] = 52;
  21. //$curso["startdate"] = time(); //[Fecha de inicio del curso (time() + 3600 * 24)]
  22. $curso["startdate"] = strtotime($curso['startdate']);
  23. // convertimos la fecha a time
  24. $curso["hiddensections"] = 1;
  25. $curso["newsitems"] = 5;
  26. $curso["showgrades"] = 1;
  27. $curso["showreports"] = 0;
  28. $curso["maxbytes"] = $maxbyte;
  29. $curso["metacourse"] = 0;
  30. $curso["enrol"] = "manual";
  31. //[vacio para que se enrolen de cualquier manera]
  32. $curso["defaultrole"] = 0;
  33. $curso["enrollable"] = 0;
  34. //[Para indicar si el curso esta abirto o no]
  35. $curso["enrolstartdate"] = 0;
  36. $curso["enrolstartdisabled"] = 1;
  37. $curso["enrolenddate"] = 0;
  38. $curso["enrolenddisabled"] = 1;
  39. $curso["enrolperiod"] = 0;
  40. $curso["expirynotify"] = 0;
  41. $curso["notifystudents"] = 0;
  42. $curso["expirythreshold"] = 864000;
  43. $curso["groupmode"] = 1;
  44. $curso["groupmodeforce"] = 0;
  45. $curso["visible"] = 0;
  46. $curso["guest"] = 0;
  47. $curso["restrictmodules"] = 0;
  48. $curso[id] = 0;
  49. $curso[teacher] = get_string('defaultcourseteacher');
  50. $curso[teachers] = get_string('defaultcourseteachers');
  51. $curso[student] = get_string('defaultcoursestudent');
  52. $curso[students] = get_string('defaultcoursestudents');
  53. $data = (object) $curso;
  54. if (!$course = create_course($data)) {
  55. throw new Exception(get_string('coursenotcreated'));
  56. } else {
  57. $curso['id'] = $course->id;
  58. $context = get_context_instance(CONTEXT_COURSE, $course->id);
  59. // assign default role to creator if not already having permission to manage course assignments
  60. if (!has_capability('moodle/course:view', $context) or !has_capability('moodle/role:assign', $context)) {
  61. //TODO verificar la asignacion del rol
  62. //-->role_assign($CFG->creatornewroleid, 2, 0, $context->id); //El 2 es del usuario admin. Si se envia otro usuario, se enrola como profesor del grupo
  63. }
  64. // ensure we can use the course right after creating it
  65. // this means trigger a reload of accessinfo...
  66. mark_context_dirty($context->path);
  67. }
  68. $curso["urlportada"] = urlportada().'/'.$curso['shortname'];
  69. $curso["urlcurso"] = urlcurso()."/course/view.php?id=".$curso["id"];
  70. $week = optional_param('week', -1, PARAM_INT);
  71. if ($week != -1) {
  72. $displaysection = course_set_display($course->id, $week);
  73. } else {
  74. $displaysection = course_set_display($course->id, 0);
  75. }
  76. $timenow = time();
  77. $weekdate = $course->startdate;
  78. // this should be 0:00 Monday of that week
  79. $weekdate += 7200;
  80. // Add two hours to avoid possible DST problems
  81. $section = 1;
  82. $sectionmenu = array();
  83. $weekofseconds = 604800;
  84. $course->enddate = $course->startdate+($weekofseconds*$course->numsections);
  85. while ($weekdate < $course->enddate) {
  86. $nextweekdate = $weekdate+($weekofseconds);
  87. $weekday = userdate($weekdate, $strftimedateshort);
  88. $endweekday = userdate($weekdate+518400, $strftimedateshort);
  89. if (!empty($sections[$section])) {
  90. $thissection = $sections[$section];
  91. } else {
  92. unset($thissection);
  93. $thissection->course = $course->id;
  94. // Create a new week structure
  95. $thissection->section = $section;
  96. $thissection->summary = '';
  97. $thissection->visible = 1;
  98. if (!$thissection->id = insert_record('course_sections', $thissection)) {
  99. notify('Error inserting new week!');
  100. }
  101. }
  102. if (!empty($displaysection) and $displaysection != $section) {
  103. // Check this week is visible
  104. if ($showsection) {
  105. $sectionmenu['week='.$section] = s("$strweek $section | $weekday - $endweekday");
  106. }
  107. $section++;
  108. $weekdate = $nextweekdate;
  109. continue;
  110. }
  111. if ($showsection) {
  112. $currentweek = (($weekdate <= $timenow) && ($timenow < $nextweekdate));
  113. $currenttext = '';
  114. if (!$thissection->visible) {
  115. $sectionstyle = ' hidden';
  116. } elseif ($currentweek) {
  117. $sectionstyle = ' current';
  118. $currenttext = get_accesshide(get_string('currentweek', 'access'));
  119. } else {
  120. $sectionstyle = '';
  121. }
  122. $weekperiod = $weekday.' - '.$endweekday;
  123. }
  124. $section++;
  125. $weekdate = $nextweekdate;
  126. }
  127. ob_end_clean();
  128. //Para que no imprima los echos
  129. return $curso;
  130. }
  131. /* CONSUMO DE SERVICIOS WEB :: MODULO :: CURSOS */
  132. /*
  133. $curso = array(
  134. 'category' => '66',
  135. 'fullname' => 'Curso de revision WS',
  136. 'shortname' => date(),
  137. 'summary' => 'Curso creado para validar WS'.time()
  138. );
  139. $resultado1 = modulocursosRegistrarCurso($curso);
  140. echo "<pre>";
  141. print_r($resultado1);
  142. exit;
  143. */
  144. /*
  145. $valores = array('category' => '26', 'fullname'=> 'LO que sea JPCP ');
  146. $editarSecciones = modulocursosRegistrarCurso($valores);
  147. echo "<pre>";
  148. print("modulocursosRegistrarCurso");
  149. print_r($editarSecciones);
  150. echo "</pre>";
  151. */
  152. /*
  153. function modulocursosDetallesCurso($idCurso){
  154. ob_start(); //Para evitar que imprima los echos
  155. $oCurso = get_record('course', 'id', $idCurso['id']);
  156. print_r($oCurso); exit;
  157. //$oCurso = get_record('course', 'shortname', $idCurso['shortname']);
  158. $curso['id'] = utf8_decode($oCurso->id);
  159. $curso['fullname'] = utf8_decode($oCurso->fullname);
  160. $curso['shortname'] = utf8_decode($oCurso->shortname);
  161. $curso['summary'] = utf8_decode($oCurso->summary);
  162. $curso['category'] = utf8_decode($oCurso->category);
  163. $curso['format'] = utf8_decode($oCurso->format);
  164. $curso['numsections'] = utf8_decode($oCurso->numsections);
  165. $curso['visible'] = utf8_decode($oCurso->visible);
  166. $curso['timemodified'] = utf8_decode($oCurso->timemodified);
  167. $curso['apertura'] = utf8_decode($oCurso->apertura);
  168. $curso['cierre'] = utf8_decode($oCurso->cierre);
  169. $curso['enrolstartdate'] = utf8_decode($oCurso->enrolstartdate);
  170. $curso['enrolenddate'] = utf8_decode($oCurso->enrolenddate);
  171. $curso["urlportada"] = urlportada().'/'.$curso['shortname'];
  172. $curso["urlcurso"] = urlcurso()."/course/view.php?id=".$curso["id"];
  173. ob_end_clean();//Para que no imprima los echos
  174. return $curso;
  175. }
  176. */
  177. function modulocursosDetallesCurso($idCurso) {
  178. ob_start();
  179. //Para evitar que imprima los echos
  180. $oCurso = get_record('course', 'id', $idCurso['id']);
  181. //$oCurso = get_record('course', 'shortname', $idCurso['shortname']);
  182. $curso['id'] = utf8_decode($oCurso->id);
  183. $curso['fullname'] = utf8_decode($oCurso->fullname);
  184. $curso['shortname'] = utf8_decode($oCurso->shortname);
  185. $curso['summary'] = utf8_decode($oCurso->summary);
  186. $curso['category'] = utf8_decode($oCurso->category);
  187. $curso['format'] = utf8_decode($oCurso->format);
  188. $curso['numsections'] = utf8_decode($oCurso->numsections);
  189. $curso['visible'] = utf8_decode($oCurso->visible);
  190. $curso['timemodified'] = utf8_decode($oCurso->timemodified);
  191. $curso["urlportada"] = urlportada().'/'.$curso['shortname'];
  192. $curso["urlcurso"] = urlcurso()."/course/view.php?id=".$curso["id"];
  193. $curso['apertura'] = utf8_decode($oCurso->apertura);
  194. $curso['cierre'] = utf8_decode($oCurso->cierre);
  195. $curso['enrolstartdate'] = utf8_decode($oCurso->enrolstartdate);
  196. $curso['enrolenddate'] = utf8_decode($oCurso->enrolenddate);
  197. ob_end_clean();
  198. //Para que no imprima los echos
  199. return $curso;
  200. }
  201. /*
  202. $valores = array('id' => '2');
  203. $curso = modulocursosDetallesCurso($valores);
  204. echo "<pre>";
  205. print("modulo cursos Detalles Curso");
  206. print_r($curso);
  207. echo "</pre>";
  208. exit;
  209. */
  210. function modulocursosDetallesCursoXnombreCorto($nombreCortoCurso) {
  211. ob_start();
  212. //Para evitar que imprima los echos
  213. $oCurso = get_record('course', 'shortname', $nombreCortoCurso['shortname']);
  214. //$oCurso = get_record('course', 'shortname', $idCurso['shortname']);
  215. $curso['id'] = utf8_decode($oCurso->id);
  216. $curso['fullname'] = utf8_decode($oCurso->fullname);
  217. $curso['shortname'] = utf8_decode($oCurso->shortname);
  218. $curso['summary'] = utf8_decode($oCurso->summary);
  219. $curso['category'] = utf8_decode($oCurso->category);
  220. $curso['format'] = utf8_decode($oCurso->format);
  221. $curso['numsections'] = utf8_decode($oCurso->numsections);
  222. $curso['visible'] = utf8_decode($oCurso->visible);
  223. $curso['timemodified'] = utf8_decode($oCurso->timemodified);
  224. $curso["urlportada"] = urlportada().'/'.$curso['shortname'];
  225. $curso["urlcurso"] = urlcurso()."course/view.php?id=".$curso["id"];
  226. ob_end_clean();
  227. //Para que no imprima los echos
  228. return $curso;
  229. }
  230. // $valores = array('id' => '56');
  231. // //$valores = array('shortname'=> 'C_D');
  232. // $curso = modulocursosDetallesCurso($valores);
  233. // echo "<pre>";
  234. // print("modulo cursos Detalles Curso");
  235. // print_r($curso);
  236. // echo "</pre>";
  237. /// Funcional
  238. function modulocursosObtenerCursos($categoria) {
  239. ob_start();
  240. //Para evitar que imprima los echos
  241. if (!$category = get_record("course_categories", "id", $categoria['id'])) {
  242. throw new Exception(get_string("Category not known!"));
  243. }
  244. if (!is_object($category) && $category == 0) {
  245. $categories = get_child_categories(0);
  246. // Parent = 0 ie top-level categories only
  247. if (is_array($categories) && count($categories) == 1) {
  248. $category = array_shift($categories);
  249. $courses = get_courses_wmanagers($category->id, 'c.sortorder ASC', array('password', 'summary', 'currency'));
  250. } else {
  251. $courses = get_courses_wmanagers('all', 'c.sortorder ASC', array('password', 'summary', 'currency'));
  252. }
  253. unset($categories);
  254. } else {
  255. $courses = get_courses_wmanagers($category->id, 'c.sortorder ASC', array('password', 'summary', 'currency'));
  256. }
  257. $categoria['name'] = utf8_decode($category->name);
  258. $categoria['description'] = utf8_decode($category->description);
  259. $categoria['cursos'] = array();
  260. if ($courses) {
  261. foreach ($courses as $course) {
  262. $categoria['cursos'][] = obj2array($course);
  263. }
  264. }
  265. ob_end_clean();
  266. //Para que no imprima los echos
  267. return $categoria;
  268. }
  269. // $valores = array('id' => '1');
  270. // $curso = modulocursosObtenerCursos($valores);
  271. // echo "<pre>"; print_r($curso); exit;
  272. function modulocursosEditarCurso($data) {
  273. ob_start();
  274. //Para evitar que imprima los echos
  275. $newcourse = new stdClass();
  276. $newcourse->id = utf8_encode($data['id']);
  277. $newcourse->visible = $data['visible'];
  278. if (!empty($data['fullname'])) {
  279. $newcourse->fullname = utf8_encode($data['fullname']);
  280. }
  281. if (!empty($data['shortname'])) {
  282. $newcourse->shortname = utf8_encode($data['shortname']);
  283. }
  284. if (!empty($data['summary'])) {
  285. $newcourse->summary = utf8_encode($data['summary']);
  286. }
  287. if (!empty($data['numsections'])) {
  288. $newcourse->numsections = utf8_encode($data['numsections']);
  289. }
  290. if (!empty($data['startdate'])) {
  291. $newcourse->fullname = utf8_encode($data['startdate']);
  292. }
  293. $USER = get_record('user', 'id', 2);
  294. if (!$newcourse->id = update_record('course', $newcourse)) {
  295. throw new Exception("Could not edit the course'$newcourse->fullname' ");
  296. } else {
  297. $newcourse->context = get_context_instance(CONTEXT_COURSE, $newcourse->id);
  298. mark_context_dirty($newcourse->context->path);
  299. $data['id'] = $newcourse->id;
  300. fix_course_sortorder();
  301. }
  302. ob_end_clean();
  303. //Para que no imprima los echos
  304. return $data;
  305. }
  306. function modulocursosEliminarCurso($idCurso) {
  307. ob_start();
  308. //Para evitar que imprima los echos
  309. return delete_records('course', 'id', $idCurso['id']);
  310. ob_end_clean();
  311. //Para que no imprima los echos
  312. }
  313. function modulocursosOcultarMostrarCurso($curso) {
  314. ob_start();
  315. //Para evitar que imprima los echos
  316. $consulta = get_record('course', 'id', $curso['id']);
  317. if ($consulta->visible == 1) {
  318. return set_field('course_categories', 'visible', 0, 'id', $curso['id']);
  319. } else {
  320. return set_field('course_categories', 'visible', 1, 'id', $curso['id']);
  321. }
  322. ob_end_clean();
  323. //Para que no imprima los echos
  324. }
  325. function modulocursosMoverArribaCurso($idCurso) {
  326. ob_start();
  327. //Para evitar que imprima los echos
  328. $consulta = get_record('course', 'id', $idCurso['id']);
  329. if ($consulta->sortorder != 0 and $consulta->parent == 0 and $consulta->category == $idCurso['category']) {
  330. $nueva = $consulta->sortorder;
  331. $primera = set_field('course', 'sortorder', $consulta->sortorder-2, 'id', $idCurso['id']);
  332. $consulta2 = get_record('course', 'category', $idCurso['category'], 'sortorder', $nueva);
  333. $nueva2 = $consulta2->sortorder+1;
  334. $idnue = $consulta2->id;
  335. $segunda = set_field('course', 'sortorder', $nueva2, 'id', $idnue);
  336. return $segunda.$primera;
  337. } else {
  338. return get_record('course', 'id', $idCurso['id']);
  339. }
  340. ob_end_clean();
  341. //Para que no imprima los echos
  342. }
  343. function modulocursosMoverAbajoCurso($idCurso) {
  344. ob_start();
  345. //Para evitar que imprima los echos
  346. $consulta = get_record('course', 'id', $idCurso['id']);
  347. if ($consulta->parent == 0 and $consulta->category == $idCurso['category']) {
  348. $nueva = $consulta->sortorder;
  349. $primera = set_field('course', 'sortorder', $consulta->sortorder+1, 'id', $idCurso['id']);
  350. $consulta2 = get_record('course', 'category', $idCurso['category'], 'sortorder', $nueva);
  351. $nueva2 = $consulta2->sortorder-1;
  352. $idnue = $consulta2->id;
  353. $segunda = set_field('course', 'sortorder', $nueva2, 'id', $idnue);
  354. return $segunda.$primera;
  355. } else {
  356. return get_record('course', 'id', $idCurso['id']);
  357. }
  358. ob_end_clean();
  359. //Para que no imprima los echos
  360. }
  361. function modulocursosObtenerRecursosActividades() {
  362. ob_start();
  363. //Para evitar que imprima los echos
  364. global $CFG;
  365. $obtenerModulos = obj2array($allmods = get_records("modules", "visible", 1));
  366. foreach ($obtenerModulos as $obtenerModulos => $modulos) {
  367. $libfile = $CFG->dirroot."/mod/".$modulos['name']."/lib.php";
  368. if (!file_exists($libfile)) {
  369. continue;
  370. }
  371. include_once ($libfile);
  372. $gettypesfunc = $modulos['name'].'_get_types';
  373. if (function_exists($gettypesfunc)) {
  374. $types = $gettypesfunc();
  375. foreach ($types as $type) {
  376. $contador += 1;
  377. if (!isset($type->modclass) or !isset($type->typestr)) {
  378. debugging('Incorrect activity type in '.$modulos['name']);
  379. continue;
  380. }
  381. if ($type->type != 'assignment_group_start' && $type->type != 'assignment_group_end') {
  382. if ($type->modclass == MOD_CLASS_RESOURCE) {
  383. $resources[$type->type] = $type->typestr;
  384. } else {
  385. $activities[$type->type] = $type->typestr;
  386. }
  387. }
  388. }
  389. } else {
  390. // all mods without type are considered activity
  391. $activities[$modulos['name']] = get_string('modulename', $modulos['name']);;
  392. }
  393. }
  394. $conteoActividades = 0;
  395. /* foreach($activities as $clave => $valor) {
  396. $tmpActividades[$conteoActividades++] = array($clave => $valor);
  397. }*/
  398. foreach ($activities as $clave => $valor) {
  399. if (($clave != "sae") && ($clave != "feedback") && ($clave != "survey")) {
  400. $tmpActividades['clave'] = $clave;
  401. $tmpActividades['valor'] = $valor;
  402. $actividades[$conteoActividades] = $tmpActividades;
  403. $conteoActividades++;
  404. }
  405. }
  406. $conteoRecursos = 0;
  407. foreach ($resources as $clave => $valor) {
  408. if ($clave != "label") {
  409. $tmpResources['clave'] = $clave;
  410. $tmpResources['valor'] = $valor;
  411. $recursos[$conteoRecursos] = $tmpResources;
  412. $conteoRecursos++;
  413. }
  414. }
  415. $output['actividades'] = $actividades;
  416. $output['recursos'] = $recursos;
  417. revisionParametros($output);
  418. return $output;
  419. ob_end_clean();
  420. //Para que no imprima los echos
  421. }
  422. /*
  423. $ObteberRecursosActividades = modulocursosObtenerRecursosActividades();
  424. echo "<pre>";
  425. print_r($ObteberRecursosActividades);
  426. */
  427. /*
  428. function modulocursosRegistrarBackup($parametros) {
  429. //ob_start (); // Para evitar que imprima los echos
  430. global $COURSE, $CFG, $SESSION, $USER;
  431. define('BACKUP_SILENTLY', 1);
  432. define('RESTORE_SILENTLY_NOFLUSH', 1);
  433. $course = get_record ( "course", "id", $parametros['id']); // Curso
  434. $prefs['backup_users'] = 2;
  435. $resultado = backup_course_silently( $course->id,$prefs);
  436. $result = substr($resultado, strrpos( $resultado,'/' )+1);
  437. //ob_end_clean (); // Para que no imprima los echos
  438. return $result;
  439. }
  440. */
  441. function modulocursosRegistrarBackup($parametros) {
  442. ob_start();
  443. // Para evitar que imprima los echos
  444. global $COURSE, $CFG, $SESSION, $USER;
  445. define('BACKUP_SILENTLY', 1);
  446. define('RESTORE_SILENTLY_NOFLUSH', 1);
  447. $course = get_record("course", "id", $parametros['id']);
  448. // Curso
  449. $prefs['backup_users'] = 1;
  450. $prefs['backup_logs'] = 0;
  451. $prefs['backup_user_files'] = 1;
  452. $prefs['backup_course_files'] = 1;
  453. $prefs['backup_site_files'] = 1;
  454. $prefs['backup_gradebook_history'] = 0;
  455. $resultado = backup_course_silently($course->id, $prefs);
  456. $result = substr($resultado, strrpos($resultado, '/')+1);
  457. ob_end_clean();
  458. // Para que no imprima los echos
  459. return $result;
  460. }
  461. // $parametros = array('id' => 222);
  462. // echo $generarBackup = modulocursosRegistrarBackup($parametros); exit;
  463. /*function modulocursosRestaurarCurso($parametros){
  464. revisionParametros($parametros);
  465. ob_start (); // Para evitar que imprima los echos
  466. global $COURSE, $CFG, $SESSION, $USER;
  467. $fuente = $CFG->dataroot . '/' . $parametros ['id'] . '/backupdata/' . $parametros ['archivo'];
  468. $result = import_backup_file_silently ( $fuente, $parametros ['id_destino'], false, false, $preferences);
  469. //print_r($result); exit;
  470. ob_end_clean (); // Para que no imprima los echos
  471. return "se restauro el sitio";
  472. }*/
  473. // function modulocursosRestaurarCurso($parametros){
  474. // revisionParametros($parametros);
  475. // ob_start (); // Para evitar que imprima los echos
  476. // global $COURSE, $CFG, $SESSION, $USER;
  477. // $fuente = $CFG->dataroot . '/' . $parametros ['id'] . '/backupdata/' . $parametros ['archivo'];
  478. // $result = import_backup_file_silently ( $fuente, $parametros ['id_destino'], false, false, $preferences);
  479. // //print_r($result); exit;
  480. // ob_end_clean (); // Para que no imprima los echos
  481. // return "se restauro el sitio: ".$result->id;
  482. // }
  483. function modulocursosRestaurarCurso($parametros) {
  484. //echo "<pre>"; print_r($parametros); exit;
  485. revisionParametros($parametros);
  486. ob_start();
  487. // Para evitar que imprima los echos
  488. global $COURSE, $CFG, $SESSION, $USER;
  489. //$preferences = new array();
  490. $preferences['restore_course_files'] = 1;
  491. $preferences['backup_site_files'] = 1;
  492. $preferences['backup_user_files'] = 1;
  493. $preferences['restoreto'] = 1;
  494. $fuente = $CFG->dataroot.'/'.$parametros['id'].'/backupdata/'.$parametros['archivo'];
  495. $result = import_backup_file_silently($fuente, $parametros['id_destino'], false, false, $preferences);
  496. $direccion = $CFG->dataroot."/".$result."/scriptsAgregados.xml";
  497. $url = $direccion;
  498. $contenido_xml = "";
  499. if ($d = @fopen($url, "r")) {
  500. while ($aux = fgets($d, 4096)) {
  501. $idcursoBackup = $parametros['id'];
  502. $idcursoDestino = $result;
  503. $linea = str_replace($idcursoBackup, $idcursoDestino, $aux);
  504. $contenido_xml .= $linea;
  505. }
  506. @fclose($d);
  507. }
  508. if ($d = @fopen($url, "r+")) {
  509. fwrite($d, $contenido_xml);
  510. @fclose($d);
  511. }
  512. /* // para ver el contenido del XML
  513. if ( $xml = simplexml_load_string($contenido_xml) ) {
  514. print_r($xml);
  515. exit;
  516. }
  517. */
  518. ob_end_clean();
  519. // Para que no imprima los echos
  520. return $result;
  521. }
  522. /*
  523. $parametros = array('id' => 200, 'id_destino' => '', 'archivo' => 'copia_de_seguridad-pens_707-s01-20130530-1330.zip');
  524. $generarBackup = modulocursosRestaurarCurso($parametros);
  525. echo "<pre>";
  526. print_r($generarBackup);
  527. echo "</pre>";
  528. */
  529. function modulocursosEditarSeccion($data) {
  530. ob_start();
  531. //Para evitar que imprima los echos
  532. $edicion = new stdClass();
  533. $edicion->id = utf8_encode($data['id']);
  534. $edicion->summary = utf8_encode($data['summary']);
  535. if (!empty($edicion->summary)) {
  536. set_field("course_sections", "summary", $edicion->summary, "id", $edicion->id);
  537. //echo "Actualizado";
  538. } else {
  539. throw new Exception("No se puede editar la seccion'$edicion->summary' ");
  540. }
  541. return $data;
  542. ob_end_clean();
  543. //Para que no imprima los echos
  544. }
  545. /*
  546. $valores = array( 'id' =>1, 'summary'=> 'VALORES para la seccion');
  547. $editarSecciones = modulocursosEditarSeccion($valores);
  548. echo "<pre>";
  549. //print("editar Secciones");
  550. print_r($editarSecciones);
  551. echo "</pre>";
  552. */
  553. function modulocursosObtenerRecursosActividadesCurso($curso) {
  554. ob_start();
  555. //Para evitar que imprima los echos
  556. $obtenerRecursosActividades = obj2array(get_array_of_activities($curso['id']));
  557. //echo "<pre>"; print_r($obtenerRecursosActividades); exit;
  558. foreach ($obtenerRecursosActividades as $elementos) {
  559. if (($elementos['section'] != '0') && ($elementos['mod'] != 'label') && ($elementos['mod'] != 'sae')) {
  560. $curso['elementos'][] = obj2array($elementos);
  561. }
  562. }
  563. $ObtenerSemanas = get_all_sections($curso['id']);
  564. foreach ($ObtenerSemanas as $semana) {
  565. $curso['semanas'][] = obj2array($semana);
  566. }
  567. ob_end_clean();
  568. //Para que no imprima los echos
  569. return $curso;
  570. }
  571. // $curso = array( 'id' =>447);
  572. // $modulocursosObtenerSeccion = modulocursosObtenerRecursosActividadesCurso($curso);
  573. // // echo urldecode($modulocursosObtenerSeccion ['elementos'][0]['name']); exit;
  574. // echo "<pre>"; print_r($modulocursosObtenerSeccion); exit;
  575. function modulocursosObtenerSemanas($courseid) {
  576. ob_start();
  577. //Para evitar que imprima los echos
  578. $ObtenerSemanas = get_all_sections($courseid['id']);
  579. foreach ($ObtenerSemanas as $semana) {
  580. $curso['semanas'][] = obj2array($semana);
  581. }
  582. unset($curso['semanas'][0]);
  583. return $curso;
  584. ob_end_clean();
  585. //Para que no imprima los echos
  586. }
  587. // $curso = array( 'id' => 447 );
  588. // $modulocursosObtenerSemanas = modulocursosObtenerSemanas($curso);
  589. // echo "<pre>"; print_r($modulocursosObtenerSemanas); exit;
  590. function modulocursosRegistarSemanas($section, $courseid) {
  591. ob_start();
  592. //Para evitar que imprima los echos
  593. get_course_section($section, $courseid['id']);
  594. return "";
  595. ob_end_clean();
  596. //Para que no imprima los echos
  597. }
  598. /*
  599. $curso = array( 'id' =>122);
  600. $modulocursosRegistarSemanas = modulocursosRegistarSemanas($section=5 ,$curso);
  601. echo "<pre>";
  602. print_r($modulocursosRegistarSemanas);
  603. echo "</pre>";
  604. */
  605. function modulocursos_get_studentshtml($curso) {
  606. ob_start();
  607. //Para evitar que imprima los echos
  608. global $CFG;
  609. // $sort = "u.idnumber";
  610. // $sqlusuarios = "SELECT DISTINCT u.id, u.firstname, u.lastname, u.imagealt, u.picture, u.idnumber
  611. // FROM {$CFG->prefix}user u
  612. // JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
  613. // WHERE ra.roleid in (5)
  614. // ORDER BY $sort";
  615. // return $sqlusuarios;
  616. $usurios = get_course_students($curso['id']);
  617. $idsUsuarios = array_keys($usurios);
  618. //$usurios = get_records_sql($sqlusuarios);
  619. //print_object($usurios);
  620. foreach ($idsUsuarios as $userid) {
  621. //echo "<br>".$userid;
  622. //$aver = grade_report_user_profilereport($curso['id'], $userid);
  623. /*print_object($aver);
  624. exit;
  625. echo "<br>---------<br>";*/
  626. $sql = "
  627. select gi.id, gg.itemid, gg.userid, gg.finalgrade, gi.itemname, gi.courseid
  628. from {$CFG->prefix
  629. }
  630. grade_items gi LEFT JOIN {$CFG->prefix
  631. }
  632. grade_grades gg ON(gi.id = gg.itemid)
  633. where gg.userid=$userid
  634. AND gi.courseid={$curso['id']
  635. }
  636. order by gi.id";
  637. //return $sql;
  638. //$grades= get_records_sql($sql);
  639. //print_object($grades);
  640. $grades[$userid] = get_records_sql($sql);
  641. }
  642. //exit;
  643. //$conteo=0;
  644. /*
  645. foreach ($usurios as $userid => $user) {
  646. $sql = "
  647. select gi.id, gg.itemid, gg.userid, gg.finalgrade, gi.itemname, gi.courseid
  648. from {$CFG->prefix}grade_items gi LEFT JOIN {$CFG->prefix}grade_grades gg ON(gi.id = gg.itemid)
  649. where gg.userid=$userid
  650. AND gi.courseid={$curso['id']}
  651. order by gi.id";
  652. //return $sql;
  653. // $grades= get_records_sql($sql);
  654. $grades[$conteo++] = get_records_sql($sql);
  655. }*/
  656. return obj2array($grades);
  657. ob_end_clean();
  658. //Para que no imprima los echos
  659. }
  660. // $curso = array( 'id' =>97);
  661. // print_object(modulocursos_get_studentshtml($curso));
  662. function modulocursosLimpiarCurso($parametros) {
  663. ob_start();
  664. // Para evitar que imprima los echos
  665. global $CFG;
  666. $id_curso = $parametros['id'];
  667. $sql = "SELECT id FROM {$CFG->prefix
  668. }
  669. course WHERE id = ".$id_curso;
  670. $dato = get_record_sql($sql);
  671. if ($dato) {
  672. return $result = "si";
  673. } else {
  674. return $result = "no";
  675. }
  676. ob_end_clean();
  677. //Para que no imprima los echos
  678. }
  679. /*
  680. $parametros = array('id' => 129);
  681. $limpiarcurso = modulocursosLimpiarCurso($parametros);
  682. echo "<pre>";
  683. print("modulocursosLimpiarCurso \n ");
  684. print_r($limpiarcurso);
  685. echo "</pre>";
  686. */
  687. function modulousuariosMatricularUsuariosDelete($usuarios, $grupo) {
  688. ob_start();
  689. //Para evitar que imprima los echos
  690. //---- VALIDAR LA INFORMACION DE ENTRADA ----------------
  691. foreach ($usuarios as $usuario) {
  692. if (!$usuario['idnumber'] || !$usuario['username'] || !$usuario['rol']['id']) {
  693. return new soap_fault('Client', '', 'Los datos de un usuario no son correctos'.print_r($usuario, true));
  694. } else {
  695. //---- SI NO EXISTE EL USUARIO, IMPORTO SUS DATOS DE LDAP
  696. $user = get_complete_user_data('username', $usuario['username']);
  697. if (!$user) {
  698. $user = create_user_record($usuario['username'], '', 'ldap');
  699. //$authplugin->sync_roles($user);
  700. if (!$user) {
  701. throw new Exception('Los datos de un usuario no son correctos');
  702. }
  703. }
  704. }
  705. }
  706. if ($grupo['id'] != -1) {
  707. if (!$ogroup = get_record('groups', 'id', $grupo['id'])) {
  708. return new soap_fault('Client', '', 'Group ID was incorrect');
  709. }
  710. if (!$course = get_record('course', 'id', $ogroup->courseid)) {
  711. return new soap_fault('Client', '', 'Group ID was incorrect');
  712. }
  713. } else {
  714. if (!$course = get_record('course', 'id', $grupo['courseid'])) {
  715. return new soap_fault('Client', '', 'Group ID was incorrect');
  716. }
  717. }
  718. $context = get_context_instance(CONTEXT_COURSE, $course->id);
  719. foreach ($usuarios as $usuario) {
  720. $user = get_complete_user_data('username', $usuario['username']);
  721. // ------- ENROLAR --------
  722. $extendbase = 0;
  723. // Para especificar la fecha limite de enrolamiento.
  724. switch ($extendbase) {
  725. case 0:
  726. $timestart = $course->startdate;
  727. break;
  728. case 3:
  729. $timestart = $today;
  730. break;
  731. case 4:
  732. $timestart = $course->enrolstartdate;
  733. break;
  734. case 5:
  735. $timestart = $course->enrolenddate;
  736. break;
  737. }
  738. if ($extendperiod > 0) {
  739. $timeend = $timestart+$extendperiod;
  740. } else {
  741. $timeend = 0;
  742. }
  743. if ($grupo['id'] == -1) {
  744. if (!role_assign($usuario['rol']['id'], $user->id, 0, $context->id, $timestart, $timeend, 0, 'webservice')) {
  745. return new soap_fault('Client', '', "Could not add user with id {$user->id
  746. }
  747. to this role!");
  748. }
  749. }
  750. if ($grupo['id'] != -1 && (!groups_add_member($ogroup->id, $user->id))) {
  751. return new soap_fault('Client', '', 'erroraddremoveuser');
  752. }
  753. }
  754. ob_end_clean();
  755. //Para que no imprima los echos
  756. return obtenerUsuarios($grupo);
  757. }
  758. function modulocursosReiniciarCurso($parametros) {
  759. // function reset_course_userdata
  760. revisionParametros($parametros);
  761. global $CFG, $USER, $COURSE;
  762. ob_start();
  763. // Para evitar que imprima los echos
  764. $id = $dato->id = utf8_encode($parametros['id']);
  765. $roles = array();
  766. $roles[0] = 3;
  767. $roles[1] = 4;
  768. $roles[2] = 5;
  769. $roles[3] = 6;
  770. $noRegistrados = array();
  771. $noRegistrados[0] = 3;
  772. $noRegistrados[1] = 5;
  773. $noRegistrados[2] = 9;
  774. $noRegistrados[3] = 84;
  775. $data->MAX_FILE_SIZE = 2097152;
  776. $data->reset_start_date = 0;
  777. $data->reset_events = 1;
  778. $data->reset_logs = 1;
  779. $data->reset_notes = 1;
  780. $data->reset_roles = $roles;
  781. $data->mform_showadvanced_last = 1;
  782. $data->reset_roles_local = 1;
  783. $data->reset_gradebook_grades = 1;
  784. $data->reset_groups_remove = 1;
  785. $data->reset_assignment_submissions = 1;
  786. $data->reset_chat = 1;
  787. $data->reset_choice = 1;
  788. $data->reset_data_ratings = 1;
  789. $data->reset_data_comments = 1;
  790. $data->reset_forum_all = 1;
  791. $data->reset_forum_subscriptions = 1;
  792. $data->reset_glossary_ratings = 1;
  793. $data->reset_glossary_comments = 1;
  794. $data->reset_lesson = 1;
  795. $data->reset_quiz_attempts = 1;
  796. $data->reset_scorm = 1;
  797. $data->reset_survey_answers = 1;
  798. $data->feedback_reset_data_6 = 1;
  799. $data->id = $id;
  800. $data->submitbutton = 'Reset course';
  801. $data->reset_start_date_old = 0;
  802. $data->courseid = $id;
  803. $data->timeshift = 0;
  804. //$data->unenrolled = $noRegistrados; // TODO Revisar su funcoinalidad
  805. $context = get_context_instance(CONTEXT_COURSE, $data->courseid);
  806. $data->timeshift = 0;
  807. $status = array();
  808. $componentstr = get_string('general');
  809. $data->reset_logs = 1;
  810. if (!empty($data->reset_logs)) {
  811. delete_records('log', 'course', $data->courseid);
  812. $status[] = array(
  813. 'component' => $componentstr,
  814. 'item' => get_string('deletelogs'),
  815. 'error' => false,
  816. );
  817. }
  818. $data->reset_events = 1;
  819. if (!empty($data->reset_events)) {
  820. delete_records('event', 'courseid', $data->courseid);
  821. $status[] = array(
  822. 'component' => $componentstr,
  823. 'item' => get_string('deleteevents',
  824. 'calendar'),
  825. 'error' => false,
  826. );
  827. }
  828. $data->reset_events = 1;
  829. if (!empty($data->reset_notes)) {
  830. require_once ($CFG ->dirroot.'/notes/lib.php');
  831. note_delete_all($data->courseid);
  832. $status[] = array(
  833. 'component' => $componentstr,
  834. 'item' => get_string('deletenotes',
  835. 'notes'),
  836. 'error' => false,
  837. );
  838. }
  839. $componentstr = get_string('roles');
  840. $data->reset_roles_local = 1;
  841. if (!empty($data->reset_roles_local)) {
  842. $children = get_child_contexts($context);
  843. foreach ($children as $child) {
  844. role_unassign(0, 0, 0, $child->id);
  845. }
  846. //force refresh for logged in users
  847. mark_context_dirty($context->path);
  848. $status[] = array(
  849. 'component' => $componentstr,
  850. 'item' => get_string('deletelocalroles',
  851. 'role'),
  852. 'error' => false,
  853. );
  854. }
  855. // First unenrol users - this cleans some of related user data too, such as forum subscriptions, tracking, etc.
  856. $roles = array();
  857. $roles[1] = 3;
  858. $roles[2] = 4;
  859. $roles[3] = 5;
  860. $roles[4] = 6;
  861. $data->unenrolled = array();
  862. if (!empty($roles)) {
  863. foreach ($roles as $roleid) {
  864. if ($users = get_role_users($roleid, $context, false, 'u.id', 'u.id ASC')) {
  865. foreach ($users as $user) {
  866. role_unassign($roleid, $user->id, 0, $context->id);
  867. if (!has_capability('moodle/course:view', $context, $user->id)) {
  868. $data->unenrolled[$user->id] = $user->id;
  869. }
  870. }
  871. }
  872. }
  873. }
  874. if (!empty($data->unenrolled)) {
  875. $status[] = array(
  876. 'component' => $componentstr,
  877. 'item' => get_string('unenrol').' ('.count($data->unenrolled).')',
  878. 'error' => false,
  879. );
  880. }
  881. $componentstr = get_string('groups');
  882. // remove all group members
  883. groups_delete_group_members($data->courseid);
  884. $status[] = array(
  885. 'component' => $componentstr,
  886. 'item' => get_string('removegroupsmembers',
  887. 'group'),
  888. 'error' => false,
  889. );
  890. // remove all groups
  891. groups_delete_groups($data->courseid, false);
  892. $status[] = array(
  893. 'component' => $componentstr,
  894. 'item' => get_string('deleteallgroups',
  895. 'group'),
  896. 'error' => false,
  897. );
  898. // remove all grouping members
  899. groups_delete_groupings_groups($data->courseid, false);
  900. $status[] = array(
  901. 'component' => $componentstr,
  902. 'item' => get_string('removegroupingsmembers',
  903. 'group'),
  904. 'error' => false,
  905. );
  906. // remove all groupings
  907. groups_delete_groupings($data->courseid, false);
  908. $status[] = array(
  909. 'component' => $componentstr,
  910. 'item' => get_string('deleteallgroupings',
  911. 'group'),
  912. 'error' => false,
  913. );
  914. // Look in every instance of every module for data to delete
  915. $unsupported_mods = array();
  916. if ($allmods = get_records('modules')) {
  917. foreach ($allmods as $mod) {
  918. $modname = $mod->name;
  919. if (!count_records($modname, 'course', $data->courseid)) {
  920. continue;
  921. // skip mods with no instances
  922. }
  923. $modfile = $CFG->dirroot.'/mod/'.$modname.'/lib.php';
  924. $moddeleteuserdata = $modname.'_reset_userdata';
  925. // Function to delete user data
  926. if (file_exists($modfile)) {
  927. //include_once($modfile);
  928. if (function_exists($moddeleteuserdata)) {
  929. $modstatus = $moddeleteuserdata($data);
  930. if (is_array($modstatus)) {
  931. $status = array_merge($status, $modstatus);
  932. } else {
  933. debugging('Module '.$modname.' returned incorrect staus - must be an array!');
  934. }
  935. } else {
  936. $unsupported_mods[] = $mod;
  937. }
  938. } else {
  939. debugging('Missing lib.php in '.$modname.' module!');
  940. }
  941. }
  942. }
  943. // mention unsupported mods
  944. if (!empty($unsupported_mods)) {
  945. foreach ($unsupported_mods as $mod) {
  946. $status[] = array(
  947. 'component' => get_string('modulenameplural',
  948. $mod->name),
  949. 'item' => '',
  950. 'error' => get_string('resetnotimplemented')
  951. );
  952. }
  953. }
  954. /*TODO Solo falta revisar este fragmento de Calificaciones si funciona con el curso
  955. $componentstr = get_string('gradebook', 'grades');
  956. // reset gradebook
  957. if (!empty($data->reset_gradebook_items)) {
  958. remove_course_grades($data ->courseid, false);
  959. grade_grab_course_grades($data ->courseid);
  960. grade_regrade_final_grades($data->courseid);
  961. $status[] = array('component'=>$componentstr, 'item'=>get_string('removeallcourseitems', 'grades'), 'error'=>false);
  962. } else if (!empty($data->reset_gradebook_grades)) {
  963. grade_course_reset($data->courseid);
  964. $status[] = array('component'=>$componentstr, 'item'=>get_string('removeallcoursegrades', 'grades'), 'error'=>false);
  965. }
  966. */
  967. ob_end_clean();
  968. //Para que no imprima los echos
  969. return "se reinicio el curso: ".$parametros['id'];
  970. }
  971. /*
  972. $parametros = array('id' => 129);
  973. $reiniciarCurso = modulocursosReiniciarCurso($parametros);
  974. echo "<pre>";
  975. print("modulocursosReiniciarCurso \n ");
  976. print_r($reiniciarCurso);
  977. echo "</pre>";
  978. */
  979. //REVISARLOS ESTOS
  980. function modulocursosExisteCurso($shortname) {
  981. //revisionParametros($shortname);
  982. $foundcourses = get_records('course', 'shortname', $shortname);
  983. if (!empty($foundcourses)) {
  984. return true;
  985. }
  986. return false;
  987. }
  988. ?>