settings_ilm.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. /**
  3. * Settings iLM manager.
  4. * Interface:
  5. * 1. Reaches this code from administrative Moodle area
  6. * 'Site administration|Plugins|Activity modules|iAssign:Add iLM'; or
  7. * 'Site administration|Plugins|Activity modules|iAssign:Import iLM'; or
  8. * 'Site administration|Plugins|Activity modules|iAssign: "click" on the "gear" of any iLM then in "Add new iLM version" or "edit"
  9. *
  10. * Release Notes:
  11. * - v 2.0.1 2022/09/15
  12. * + Considering table 'iassign_ilm': messages to fields 'evaluate', 'reevaluate', 'editingbehavior'; fixes to allow the edition of 'reevaluate', 'editingbehavior'
  13. * - v 1.6.1 2017/12/02
  14. * + Changed 'echo' to 'print'
  15. * - v 1.6 2013/10/31
  16. * + Insert support of import iLM from zip packages.
  17. * - v 1.5 2013/10/24
  18. * + Insert function for upgrade an iLM.
  19. * - v 1.4 2013/08/02
  20. * + Insert list of iLMs informations for teacher view.
  21. * - v 1.3 2013/07/12
  22. * + Insert actions: copy (new version from an iLM) and new version (empty new version).
  23. *
  24. * @author Patricia Alves Rodrigues
  25. * @author Leo^nidas de Oliveira Branda~o
  26. * @version v 2.0.1 2022/09/15
  27. * @package mod_iassign_settings
  28. * @since 2013/01/29
  29. * @see locallib.php : class ilm_settings
  30. * @see settings_form.php
  31. * @copyleft iMatica (<a href="http://www.matematica.br">iMath</a>) - Computer Science Dep. of IME-USP (Brazil)
  32. * @copyleft LInE (<a href="http://www.usp.br/line">LInE</a>) - IME-USP (Brazil)
  33. *
  34. * <b>License</b>
  35. * - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36. */
  37. global $CFG, $USER, $PAGE, $OUTPUT, $DB;
  38. require_once("../../config.php");
  39. require_once($CFG->dirroot . '/mod/iassign/locallib.php');
  40. require_once($CFG->dirroot . '/mod/iassign/settings_form.php');
  41. require_login();
  42. if (isguestuser()) {
  43. die();
  44. }
  45. // Parameters GET e POST
  46. $ilm_id = optional_param('ilm_id', 0, PARAM_INT);
  47. $ilmid = optional_param('ilmid', 0, PARAM_INT);
  48. $ilm_parent = optional_param('ilm_parent', 0, PARAM_INT);
  49. $status = optional_param('status', 0, PARAM_INT);
  50. $action = optional_param('action', NULL, PARAM_TEXT);
  51. $url = new moodle_url('/admin/settings.php', array('section' => 'modsettingiassign'));
  52. $from = optional_param('from', NULL, PARAM_TEXT);
  53. $contextuser = context_user::instance($USER->id);
  54. $PAGE->set_url($url);
  55. $PAGE->set_context($contextuser);
  56. $PAGE->blocks->show_only_fake_blocks(); //
  57. $PAGE->set_pagelayout('popup');
  58. if ($action == 'edit') { // Edit data of an iLM => processed in 'settings_form.php'
  59. $title = get_string('edit_ilm', 'iassign') . $OUTPUT->help_icon('add_ilm_iassign', 'iassign');
  60. $PAGE->set_title($title);
  61. // Get all fields of this iLM: name, type, set_lang, description_lang, author, action, timecreated, timemodified, parent, ...
  62. // On the first time $ilm_id is well defined, but on the second (it is empty), however we need to call "add_edit_copy_ilm(.)"
  63. $param = ilm_settings::add_edit_copy_ilm($ilm_id, $action); // locallib.php: class ilm_settings: add_edit_copy_ilm($ilm_id, $action)
  64. // On the first call $ilm_id is defined: the calling is necessary to fill in data to the form
  65. // on the second call $ilm_id empty and could be avoided
  66. // without 'ilm_settings::add_edit_copy_ilm(.)' the process are not successfully finished
  67. $description = $param->description_lang; // used to present the iLM description in 'settings_form.php' - {"en":"...","pt":"..."}
  68. $mform = new mod_ilm_form(); // in 'settings_form.php': class mod_ilm_form
  69. //DEBUG: do NOT use "mod_ilm_form($param)" Warning: htmlspecialchars() expects parameter 1 to be string, object given in /var/www/html/saw/lib/pear/HTML/Common.php on line 177
  70. //DEBUg: since bellow fills form data
  71. $mform->set_data($param);
  72. if ($mform->is_cancelled()) {
  73. close_window();
  74. die;
  75. }
  76. else if ($formdata = $mform->get_submitted_data()) { // chegando aqui com $formdata->type vazio!
  77. // $formdata = { [name] [version] [type]vazio! [url] [lang] [description] [extension] [width] [height] [evaluate] [file] [id] [set_lang] [description_lang] [author] [action] [timecreated] [timemodified] [parent] [enable] [submitbutton]
  78. ilm_settings::edit_ilm($formdata, $formdata->file); // localib.php: class ilm_settings
  79. close_window(0, true);
  80. die;
  81. }
  82. print $OUTPUT->header();
  83. print $OUTPUT->heading($title); // put the header title
  84. $mform->display();
  85. print $OUTPUT->footer();
  86. die;
  87. } // if ($action == 'edit')
  88. else
  89. if ($action == 'new_version') { // Administration > plugins > iAssign : after select the iLM and the option 'Add new iLM version'
  90. $title = get_string('new_version_ilm', 'iassign') . $OUTPUT->help_icon('add_ilm_iassign', 'iassign');
  91. $PAGE->set_title($title);
  92. //echo "settings_ilm.php: form = "; print_r($form); echo "<br/>";
  93. if (!isset($mform) || !$mform) {
  94. // Get values from the parent iLM in {iassign_ilm}, put them in array $param
  95. $param = ilm_settings::add_edit_copy_ilm($ilm_id, $action); // locallib.php: class iassign
  96. // Field "description_lang" is used to present the iLM description in 'settings_form.php' - {"en":"...","pt":"..."}
  97. // {iassign_ilm}.description is JSON like: {"en":"interactive...", "pt_br":"interativo..."}
  98. // $lang = current_language(); $sizel = strlen($lang); if ($sizel>2) $lang = $lang[0] . $lang[1];
  99. // locallib.php : class iassign_language : static function get_description_lang($lang, $descriptions)
  100. $description = iassign_language::get_description_lang(current_language(), $param->description_lang);
  101. if (isset($description)) // necessary to present a simple text inside form "text" field
  102. $param->description = $description;
  103. //D echo "settings_ilm.php: " . $msg . ", description=" . $description . "<br/>lang=" . $lang . "<br/>";
  104. //D echo "param="; print_r($param); echo "<br/>"; // description_lang
  105. //D print_r($description); echo "<br/>";
  106. $mform = new mod_ilm_form(); // in 'settings_form.php': class mod_ilm_form
  107. $mform->set_data($param);
  108. }
  109. //D $msg1 .= "param->ilm_id=" . $param->ilm_id; echo "settings_ilm.php: msg1 = " . $msg1 . "<br/>";
  110. if (isset($mform) && $mform->is_cancelled()) {
  111. close_window();
  112. die;
  113. }
  114. else if (isset($mform)) { // Final form processing! Registered by 'locallib.php' with ilm_settings::copy_new_version_ilm($formdata)
  115. $formdata = $mform->get_data();
  116. if ($formdata) { // already exists the iLM
  117. ilm_settings::copy_new_version_ilm($formdata); // locallib.php: class ilm_settings
  118. close_window(0, true);
  119. die;
  120. }
  121. }
  122. print($OUTPUT->header());
  123. print($OUTPUT->heading($title));
  124. if (isset($mform))
  125. $mform->display();
  126. print($OUTPUT->footer());
  127. die;
  128. }
  129. else
  130. if ($action == 'copy') {
  131. $title = get_string('copy_ilm', 'iassign') . $OUTPUT->help_icon('add_ilm_iassign', 'iassign');
  132. $PAGE->set_title($title);
  133. $param = ilm_settings::add_edit_copy_ilm($ilm_id, $action);
  134. $mform = new mod_ilm_form(); // in 'settings_form.php': class mod_ilm_form
  135. $mform->set_data($param);
  136. if ($mform->is_cancelled()) {
  137. close_window();
  138. die;
  139. }
  140. else if ($formdata = $mform->get_data()) {
  141. ilm_settings::copy_new_version_ilm($formdata);
  142. close_window(0, true);
  143. die;
  144. }
  145. print($OUTPUT->header());
  146. print($OUTPUT->heading($title));
  147. $mform->display();
  148. print($OUTPUT->footer());
  149. die;
  150. }
  151. else
  152. if ($action == 'add') { // add new iLM using data from form and package from ZIP file
  153. $title = get_string('add_ilm_iassign', 'iassign') . $OUTPUT->help_icon('add_ilm_iassign', 'iassign'); // add_ilm_iassign='Add iLM'
  154. $PAGE->set_title($title);
  155. $param = ilm_settings::add_edit_copy_ilm($ilm_id, $action); // ./mod/iassign/locallib.php
  156. $mform = new mod_ilm_form(); // iLM form to fill data (name, extension,...)
  157. $mform->set_data($param);
  158. if ($mform->is_cancelled()) {
  159. close_window();
  160. die;
  161. }
  162. else if ($formdata = $mform->get_data()) {
  163. $filename = strtolower($mform->get_new_filename('file'));
  164. $extension = explode(".", $filename);
  165. if ($extension[count($extension) - 1] == 'zip') { // accept ZIP file
  166. // {iassign_ilm}.typ: $formdata->type : 0 = package JavaScript; 1 = package Java (JAR)
  167. $retorno = ilm_settings::new_ilm($formdata); // ./mod/iassign/locallib.php: unpack and process it (create directory...)
  168. if ($retorno == true) {
  169. close_window(0, true);
  170. die;
  171. }
  172. }
  173. else { // Open a frame (on the same page) with warnings
  174. // 'Error while extracting the content from iLM package file.'
  175. print($OUTPUT->notification(get_string('error_add_ilm_zip', 'iassign'), 'notifyproblem'));
  176. }
  177. }
  178. print($OUTPUT->header());
  179. print($OUTPUT->heading($title));
  180. $mform->display();
  181. print($OUTPUT->footer());
  182. die;
  183. } // if ($action == 'add')
  184. else
  185. if ($action == 'import') { // must be file with extension 'ipz'
  186. echo "settings_ilm.php: 5 - action=" . $action . "<br/>"; //leo //DEBUG remover
  187. // About IPZ format see https://www.matematica.br/ia/about_ilm-application.html
  188. $title = get_string('import_ilm', 'iassign') . $OUTPUT->help_icon('import_ilm', 'iassign');
  189. $PAGE->set_title($title);
  190. $param = new stdClass();
  191. $param->action = $action;
  192. $CFG->action_ilm = $action;
  193. echo "settings_ilm.php: 6 - action=" . $action . "<br/>"; //leo //DEBUG remover
  194. $mform = new mod_ilm_form(); // in 'settings_form.php': class mod_ilm_form
  195. echo "settings_ilm.php: 7 - action=" . $action . "<br/>"; //leo //DEBUG remover
  196. $mform->set_data($param);
  197. if ($mform->is_cancelled()) { // enter here if user "clicks" at button "Cancel"
  198. echo "settings_ilm.php: 8 - action=" . $action . "<br/>"; //leo //DEBUG remover
  199. close_window();
  200. die;
  201. }
  202. else if ($formdata = $mform->get_data()) { // enter here if user adds an IPZ file and "clicks" at button "Sava changes"
  203. echo "settings_ilm.php: 9 - action=" . $action . "<br/>"; //leo //DEBUG remover
  204. $filename = strtolower($mform->get_new_filename('file'));
  205. $extension = explode(".", $filename);
  206. echo "settings_ilm.php: 10 - explode filename="; print_r($extension); echo "<br/>"; //leo //DEBUG remover
  207. // settings_ilm.php: 10 - explode filename=Array ( [0] => ifractions_2025_04_07 [1] => ipz )
  208. //if (is_array($extension)) $extension = $extension[sizeof($extension)-1];
  209. //echo "settings_ilm.php: 11 - filename=" . $filename . ", extension=" . $extension . "<br/>"; //leo //DEBUG remover
  210. if ($extension[count($extension) - 1] == 'ipz') {
  211. echo "settings_ilm.php: 12 - action=" . $action . "<br/>"; //leo //DEBUG remover
  212. $retorno = ilm_settings::import_ilm($formdata->file); // locallib.php: class ilm_settings
  213. echo "settings_ilm.php: 13 - retorno=" . ($retorno?"true":"false") . "<br/>"; //leo //DEBUG remover
  214. if ($retorno == true) {
  215. close_window(5, true);
  216. die;
  217. }
  218. }
  219. else { // Open a frame (on the same page) with warnings
  220. echo "settings_ilm.php: 10 - action=" . $action . "<br/>"; //leo //DEBUG remover
  221. print($OUTPUT->notification(get_string('error_upload_ilm', 'iassign'), 'notifyproblem'));
  222. }
  223. }
  224. print($OUTPUT->header());
  225. print($OUTPUT->heading($title));
  226. $mform->display();
  227. print($OUTPUT->footer());
  228. die;
  229. } // if ($action == 'import')
  230. else
  231. if ($action == 'confirm_delete_ilm') { // Step 1 to remove iLM: will provide information about activities with this iLM
  232. echo "<br/><br/><br/><br/><br/>settings_ilm.php: (0) delete action=" . $action . ", ilm_parent=" . $ilm_parent . "<br/>\n"; //2025/04/11 leo
  233. // iLM removal: step 1
  234. // After Admin "click" button "Delete iLM", it reaches this point (verify the presence of activities of this iLM and ask Admin to confirm his intention)
  235. // If Admin "click" "Continue" (to confirm remotion) it calls "settings_ilm.php!$action=='delete'"
  236. $title = get_string('delete_ilm', 'iassign');
  237. $PAGE->set_title($title);
  238. $PAGE->set_pagelayout('base');
  239. $delete_ilm = ilm_settings::confirm_delete_ilm($ilm_id, $ilm_parent); // ./locallib.php : class ilm_settings : static function confirm_delete_ilm($ilm_id, $ilm_parent)
  240. print($OUTPUT->header());
  241. print($OUTPUT->heading($title));
  242. print($delete_ilm);
  243. print($OUTPUT->footer());
  244. die;
  245. }
  246. else
  247. if ($action == 'delete') { // Step 2 to remove iLM:
  248. echo "<br/><br/><br/><br/><br/>settings_ilm.php: delete action=" . $action . "<br/>\n"; //2025/04/11 leo
  249. // iLM removal: step 2
  250. // After Admin "click" button "Delete iLM" and confirm his intention to remove the iLM, reaches this point (really remove the iLM)
  251. // If Admin "click" "Continue" (to confirm remotion) it calls "settings_ilm.php!$action=='delete'"
  252. $title = get_string('delete_ilm', 'iassign');
  253. $PAGE->set_title($title);
  254. $PAGE->set_pagelayout('redirect');
  255. $parent = ilm_settings::delete_ilm($ilm_id); // ./locallib.php
  256. echo "settings_ilm.php: (1) delete parent null, action=" . $action . "<br/>\n"; //2025/04/11 leo
  257. if ($parent == null) {
  258. echo "settings_ilm.php: (2) delete parent null<br/>\n"; //2025/04/11 leo
  259. $title = get_string('delete_ilm', 'iassign');
  260. $PAGE->set_title($title);
  261. $PAGE->set_pagelayout('base');
  262. $delete_ilm = ilm_settings::confirm_delete_ilm($ilm_id, $ilm_parent); // ./locallib.php : class ilm_settings : static function confirm_delete_ilm($ilm_id, $ilm_parent)
  263. $msg_error = get_string('error_folder_permission_denied', 'iassign');
  264. if ($CFG->debugdisplay) // debug messages is turned on
  265. $msg_error .= "<br/>On settings_ilm.php: no iLM returned!<br/>";
  266. // Open a frame (on the same page) with warnings
  267. print($OUTPUT->header());
  268. print($OUTPUT->heading($title));
  269. print($OUTPUT->notification($msg_error, 'notifyproblem'));
  270. print($delete_ilm);
  271. print($OUTPUT->footer());
  272. die;
  273. }
  274. if ($parent == 0)
  275. redirect(new moodle_url('/admin/settings.php?', array('section' => 'modsettingiassign', 'action' => 'view')));
  276. else
  277. redirect(new moodle_url('/admin/settings.php?', array('section' => 'modsettingiassign', 'action' => 'config', 'ilm_id' => $ilm_parent)));
  278. } // if ($action == 'delete') : step 2
  279. else
  280. if ($action == 'confirm_default_ilm') {
  281. $title = get_string('confirm_default', 'iassign');
  282. $PAGE->set_title($title);
  283. $PAGE->set_pagelayout('base');
  284. $default_ilm = ilm_settings::confirm_default_ilm($ilm_id, $ilm_parent);
  285. print($OUTPUT->header());
  286. print($default_ilm);
  287. print($OUTPUT->footer());
  288. die;
  289. }
  290. else
  291. if ($action == 'default') {
  292. $title = get_string('default_ilm', 'iassign');
  293. $PAGE->set_title($title);
  294. $PAGE->set_pagelayout('redirect');
  295. ilm_settings::default_ilm($ilm_id);
  296. redirect(new moodle_url('/admin/settings.php?', array('section' => 'modsettingiassign', 'action' => 'config', 'ilm_id' => $ilm_parent)));
  297. }
  298. else
  299. if ($action == 'list') {
  300. $title = get_string('list_ilm', 'iassign');
  301. $PAGE->set_title($title);
  302. $list_ilm = ilm_settings::list_ilm();
  303. print($OUTPUT->header());
  304. print($OUTPUT->heading($title . $OUTPUT->help_icon('list_ilm', 'iassign')));
  305. print($list_ilm);
  306. print($OUTPUT->footer());
  307. die;
  308. }
  309. else
  310. if ($action == 'upgrade') {
  311. $title = get_string('upgrade_ilm_title', 'iassign');
  312. $PAGE->set_title($title);
  313. $PAGE->set_pagelayout('redirect');
  314. $ilm = ilm_settings::upgrade_ilm($ilm_id);
  315. if ($ilm == 0)
  316. redirect(new moodle_url('/admin/settings.php?', array('section' => 'modsettingiassign', 'action' => 'view')));
  317. else
  318. redirect(new moodle_url('/admin/settings.php?', array('section' => 'modsettingiassign', 'action' => 'config', 'ilm_id' => $ilm)));
  319. }
  320. else
  321. if ($action == 'view') {
  322. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  323. $title = get_string('view_ilm', 'iassign') . $OUTPUT->help_icon('add_ilm_iassign', 'iassign'); // add_ilm_iassign='Add iLM'
  324. $PAGE->set_title($title . ': ' . $iassign_ilm->name . ' ' . $iassign_ilm->version);
  325. $view_ilm = ilm_settings::view_ilm($ilm_id, $from);
  326. print($OUTPUT->header());
  327. print($OUTPUT->heading($title . ': ' . $iassign_ilm->name . ' ' . $iassign_ilm->version));
  328. print($view_ilm);
  329. print($OUTPUT->footer());
  330. die;
  331. }