Renaming global functions to include lti prefix
[moodle.git] / mod / lti / mod_form.php
CommitLineData
b7e436b0
CS
1<?php\r
2// This file is part of BasicLTI4Moodle\r
3//\r
4// BasicLTI4Moodle is an IMS BasicLTI (Basic Learning Tools for Interoperability)\r
5// consumer for Moodle 1.9 and Moodle 2.0. BasicLTI is a IMS Standard that allows web\r
6// based learning tools to be easily integrated in LMS as native ones. The IMS BasicLTI\r
7// specification is part of the IMS standard Common Cartridge 1.1 Sakai and other main LMS\r
8// are already supporting or going to support BasicLTI. This project Implements the consumer\r
9// for Moodle. Moodle is a Free Open source Learning Management System by Martin Dougiamas.\r
10// BasicLTI4Moodle is a project iniciated and leaded by Ludo(Marc Alier) and Jordi Piguillem\r
11// at the GESSI research group at UPC.\r
12// SimpleLTI consumer for Moodle is an implementation of the early specification of LTI\r
13// by Charles Severance (Dr Chuck) htp://dr-chuck.com , developed by Jordi Piguillem in a\r
14// Google Summer of Code 2008 project co-mentored by Charles Severance and Marc Alier.\r
15//\r
16// BasicLTI4Moodle is copyright 2009 by Marc Alier Forment, Jordi Piguillem and Nikolas Galanis\r
17// of the Universitat Politecnica de Catalunya http://www.upc.edu\r
18// Contact info: Marc Alier Forment granludo @ gmail.com or marc.alier @ upc.edu\r
19//\r
20// Moodle is free software: you can redistribute it and/or modify\r
21// it under the terms of the GNU General Public License as published by\r
22// the Free Software Foundation, either version 3 of the License, or\r
23// (at your option) any later version.\r
24//\r
25// Moodle is distributed in the hope that it will be useful,\r
26// but WITHOUT ANY WARRANTY; without even the implied warranty of\r
27// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
28// GNU General Public License for more details.\r
29//\r
30// You should have received a copy of the GNU General Public License\r
31// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\r
32\r
33/**\r
34 * This file defines the main basiclti configuration form\r
35 *\r
73300339 36 * @package lti\r
b7e436b0
CS
37 * @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis\r
38 * marc.alier@upc.edu\r
39 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu\r
40 *\r
41 * @author Marc Alier\r
42 * @author Jordi Piguillem\r
43 * @author Nikolas Galanis\r
44 *\r
45 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\r
46 */\r
47\r
48defined('MOODLE_INTERNAL') || die;\r
49\r
50require_once($CFG->dirroot.'/course/moodleform_mod.php');\r
73300339 51require_once($CFG->dirroot.'/mod/lti/locallib.php');\r
b7e436b0 52\r
73300339 53class mod_lti_mod_form extends moodleform_mod {\r
b7e436b0
CS
54\r
55 function definition() {\r
56 global $DB;\r
57\r
58 $typename = optional_param('type', false, PARAM_ALPHA);\r
59\r
60 if (empty($typename)) {\r
61 //Updating instance\r
62 if (!empty($this->_instance)) {\r
73300339 63 $basiclti = $DB->get_record('lti', array('id' => $this->_instance));\r
b7e436b0
CS
64 $this->typeid = $basiclti->typeid;\r
65\r
73300339 66 $typeconfig = lti_get_config($basiclti);\r
b7e436b0
CS
67 $this->typeconfig = $typeconfig;\r
68\r
69 } else { // New not pre-configured instance\r
70 $this->typeid = 0;\r
71 }\r
72 } else {\r
73 // New pre-configured instance\r
73300339 74 $basicltitype = $DB->get_record('lti_types', array('rawname' => $typename));\r
b7e436b0
CS
75 $this->typeid = $basicltitype->id;\r
76\r
73300339 77 $typeconfig = lti_get_type_config($this->typeid);\r
b7e436b0
CS
78 $this->typeconfig = $typeconfig;\r
79 }\r
80\r
81 $mform =& $this->_form;\r
82//-------------------------------------------------------------------------------\r
83 /// Adding the "general" fieldset, where all the common settings are shown\r
84 $mform->addElement('header', 'general', get_string('general', 'form'));\r
85 /// Adding the standard "name" field\r
73300339 86 $mform->addElement('text', 'name', get_string('basicltiname', 'lti'), array('size'=>'64'));\r
b7e436b0
CS
87 $mform->setType('name', PARAM_TEXT);\r
88 $mform->addRule('name', null, 'required', null, 'client');\r
89 /// Adding the optional "intro" and "introformat" pair of fields\r
73300339 90 $this->add_intro_editor(false, get_string('basicltiintro', 'lti'));\r
285f8250
CS
91 $mform->setAdvanced('introeditor');\r
92\r
73300339 93 $mform->addElement('checkbox', 'showtitle', '&nbsp;', ' ' . get_string('display_name', 'lti'));\r
285f8250
CS
94 $mform->setAdvanced('showtitle');\r
95 \r
73300339 96 $mform->addElement('checkbox', 'showdescription', '&nbsp;', ' ' . get_string('display_description', 'lti'));\r
285f8250
CS
97 $mform->setAdvanced('showdescription');\r
98 \r
99 //Tool settings\r
73300339 100 $mform->addElement('select', 'typeid', get_string('external_tool_type', 'lti'), lti_get_types_for_add_instance());\r
285f8250
CS
101 //$mform->setDefault('typeid', '0');\r
102 \r
73300339 103 $mform->addElement('text', 'toolurl', get_string('launch_url', 'lti'), array('size'=>'64'));\r
285f8250
CS
104 $mform->setType('toolurl', PARAM_TEXT);\r
105 \r
0d8afb44 106 $launchoptions=array();\r
73300339
CS
107 $launchoptions[LTI_LAUNCH_CONTAINER_DEFAULT] = get_string('default', 'lti');\r
108 $launchoptions[LTI_LAUNCH_CONTAINER_EMBED] = get_string('embed', 'lti');\r
109 $launchoptions[LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS] = get_string('embed_no_blocks', 'lti');\r
110 $launchoptions[LTI_LAUNCH_CONTAINER_WINDOW] = get_string('new_window', 'lti');\r
0d8afb44 111\r
73300339
CS
112 $mform->addElement('select', 'launchcontainer', get_string('launchinpopup', 'lti'), $launchoptions);\r
113 $mform->setDefault('launchcontainer', LTI_LAUNCH_CONTAINER_DEFAULT);\r
0d8afb44 114 \r
73300339 115 $mform->addElement('text', 'resourcekey', get_string('resourcekey', 'lti'));\r
285f8250
CS
116 $mform->setType('resourcekey', PARAM_TEXT);\r
117 $mform->setAdvanced('resourcekey');\r
118\r
73300339 119 $mform->addElement('passwordunmask', 'password', get_string('password', 'lti'));\r
285f8250
CS
120 $mform->setType('password', PARAM_TEXT);\r
121 $mform->setAdvanced('password');\r
122 \r
73300339 123 $mform->addElement('textarea', 'instructorcustomparameters', get_string('custom', 'lti'), array('rows'=>4, 'cols'=>60));\r
285f8250
CS
124 $mform->setType('instructorcustomparameters', PARAM_TEXT);\r
125 $mform->setAdvanced('instructorcustomparameters');\r
126 \r
b7e436b0 127//-------------------------------------------------------------------------------\r
285f8250
CS
128 //$mform->addElement('hidden', 'typeid', $this->typeid);\r
129 //$mform->addElement('hidden', 'toolurl', $this->typeconfig['toolurl']);\r
b7e436b0
CS
130 $mform->addElement('hidden', 'type', $typename);\r
131\r
132//-------------------------------------------------------------------------------\r
133 // Add privacy preferences fieldset where users choose whether to send their data\r
73300339 134 $mform->addElement('header', 'privacy', get_string('privacy', 'lti'));\r
b7e436b0 135\r
73300339 136 $mform->addElement('checkbox', 'instructorchoicesendname', '&nbsp;', ' ' . get_string('share_name', 'lti'));\r
285f8250
CS
137 $mform->setDefault('instructorchoicesendname', '1');\r
138 \r
73300339 139 $mform->addElement('checkbox', 'instructorchoicesendemailaddr', '&nbsp;', ' ' . get_string('share_email', 'lti'));\r
285f8250
CS
140 $mform->setDefault('instructorchoicesendemailaddr', '1');\r
141 \r
73300339 142 $mform->addElement('checkbox', 'instructorchoiceacceptgrades', '&nbsp;', ' ' . get_string('accept_grades', 'lti'));\r
285f8250
CS
143 $mform->setDefault('instructorchoiceacceptgrades', '1');\r
144 \r
73300339 145 $mform->addElement('checkbox', 'instructorchoiceallowroster', '&nbsp;', ' ' . get_string('share_roster', 'lti'));\r
285f8250 146 $mform->setDefault('instructorchoiceallowroster', '1');\r
b7e436b0
CS
147\r
148//-------------------------------------------------------------------------------\r
b7e436b0 149\r
285f8250 150/* $debugoptions=array();\r
73300339
CS
151 $debugoptions[0] = get_string('debuglaunchoff', 'lti');\r
152 $debugoptions[1] = get_string('debuglaunchon', 'lti');\r
b7e436b0 153\r
73300339 154 $mform->addElement('select', 'debuglaunch', get_string('debuglaunch', 'lti'), $debugoptions);\r
b7e436b0
CS
155\r
156 if (isset($this->typeconfig['debuglaunch'])) {\r
157 if ($this->typeconfig['debuglaunch'] == 0) {\r
158 $mform->setDefault('debuglaunch', '0');\r
159 } else if ($this->typeconfig['debuglaunch'] == 1) {\r
160 $mform->setDefault('debuglaunch', '1');\r
161 }\r
162 }\r
285f8250 163*/\r
b7e436b0
CS
164\r
165//-------------------------------------------------------------------------------\r
166 // add standard elements, common to all modules\r
167 $this->standard_coursemodule_elements();\r
0d8afb44
CS
168 \r
169 $mform->setAdvanced('cmidnumber');\r
b7e436b0
CS
170//-------------------------------------------------------------------------------\r
171 // add standard buttons, common to all modules\r
172 $this->add_action_buttons();\r
173 }\r
174\r
175 /**\r
176 * Make fields editable or non-editable depending on the administrator choices\r
177 * @see moodleform_mod::definition_after_data()\r
178 */\r
179 function definition_after_data() {\r
180 parent::definition_after_data();\r
285f8250 181 /* $mform =& $this->_form;\r
b7e436b0
CS
182 $typeid =& $mform->getElement('typeid');\r
183 $typeidvalue = $mform->getElementValue('typeid');\r
184\r
185 //Depending on the selection of the administrator\r
186 //we don't want to have these appear as possible selections in the form but\r
187 //we want the form to display them if they are set.\r
188 if (!empty($typeidvalue)) {\r
73300339 189 $typeconfig = lti_get_type_config($typeidvalue);\r
b7e436b0
CS
190\r
191 if ($typeconfig["sendname"] != 2) {\r
192 $field =& $mform->getElement('instructorchoicesendname');\r
193 $mform->setDefault('instructorchoicesendname', $typeconfig["sendname"]);\r
194 $field->freeze();\r
195 $field->setPersistantFreeze(true);\r
196 }\r
197 if ($typeconfig["sendemailaddr"] != 2) {\r
198 $field =& $mform->getElement('instructorchoicesendemailaddr');\r
199 $mform->setDefault('instructorchoicesendemailaddr', $typeconfig["sendemailaddr"]);\r
200 $field->freeze();\r
201 $field->setPersistantFreeze(true);\r
202 }\r
203 if ($typeconfig["acceptgrades"] != 2) {\r
204 $field =& $mform->getElement('instructorchoiceacceptgrades');\r
205 $mform->setDefault('instructorchoiceacceptgrades', $typeconfig["acceptgrades"]);\r
206 $field->freeze();\r
207 $field->setPersistantFreeze(true);\r
208 }\r
209 if ($typeconfig["allowroster"] != 2) {\r
210 $field =& $mform->getElement('instructorchoiceallowroster');\r
211 $mform->setDefault('instructorchoiceallowroster', $typeconfig["allowroster"]);\r
212 $field->freeze();\r
213 $field->setPersistantFreeze(true);\r
214 }\r
215 if ($typeconfig["allowsetting"] != 2) {\r
216 $field =& $mform->getElement('instructorchoiceallowsetting');\r
217 $mform->setDefault('instructorchoiceallowsetting', $typeconfig["allowsetting"]);\r
218 $field->freeze();\r
219 $field->setPersistantFreeze(true);\r
220 }\r
285f8250 221 }*/\r
b7e436b0
CS
222 }\r
223\r
224 /**\r
225 * Function overwritten to change default values using\r
226 * global configuration\r
227 *\r
228 * @param array $default_values passed by reference\r
229 */\r
230 function data_preprocessing(&$default_values) {\r
285f8250 231/* global $CFG;\r
b7e436b0
CS
232 $default_values['typeid'] = $this->typeid;\r
233\r
234 if (!isset($default_values['toolurl'])) {\r
235 if (isset($this->typeconfig['toolurl'])) {\r
236 $default_values['toolurl'] = $this->typeconfig['toolurl'];\r
73300339
CS
237 } else if (isset($CFG->lti_toolurl)) {\r
238 $default_values['toolurl'] = $CFG->lti_toolurl;\r
b7e436b0
CS
239 }\r
240 }\r
241\r
242 if (!isset($default_values['resourcekey'])) {\r
243 if (isset($this->typeconfig['resourcekey'])) {\r
244 $default_values['resourcekey'] = $this->typeconfig['resourcekey'];\r
73300339
CS
245 } else if (isset($CFG->lti_resourcekey)) {\r
246 $default_values['resourcekey'] = $CFG->lti_resourcekey;\r
b7e436b0
CS
247 }\r
248 }\r
249\r
250 if (!isset($default_values['password'])) {\r
251 if (isset($this->typeconfig['password'])) {\r
252 $default_values['password'] = $this->typeconfig['password'];\r
73300339
CS
253 } else if (isset($CFG->lti_password)) {\r
254 $default_values['password'] = $CFG->lti_password;\r
b7e436b0
CS
255 }\r
256 }\r
257\r
258 if (!isset($default_values['preferheight'])) {\r
259 if (isset($this->typeconfig['preferheight'])) {\r
260 $default_values['preferheight'] = $this->typeconfig['preferheight'];\r
73300339
CS
261 } else if (isset($CFG->lti_preferheight)) {\r
262 $default_values['preferheight'] = $CFG->lti_preferheight;\r
b7e436b0
CS
263 }\r
264 }\r
265\r
266 if (!isset($default_values['sendname'])) {\r
267 if (isset($this->typeconfig['sendname'])) {\r
268 $default_values['sendname'] = $this->typeconfig['sendname'];\r
73300339
CS
269 } else if (isset($CFG->lti_sendname)) {\r
270 $default_values['sendname'] = $CFG->lti_sendname;\r
b7e436b0
CS
271 }\r
272 }\r
273\r
274 if (!isset($default_values['instructorchoicesendname'])) {\r
275 if (isset($this->typeconfig['instructorchoicesendname'])) {\r
276 $default_values['instructorchoicesendname'] = $this->typeconfig['instructorchoicesendname'];\r
277 } else {\r
278 if ($this->typeconfig['sendname'] == 2) {\r
73300339 279 $default_values['instructorchoicesendname'] = $CFG->lti_instructorchoicesendname;\r
b7e436b0
CS
280 } else {\r
281 $default_values['instructorchoicesendname'] = $this->typeconfig['sendname'];\r
282 }\r
283 }\r
284 }\r
285\r
286 if (!isset($default_values['sendemailaddr'])) {\r
287 if (isset($this->typeconfig['sendemailaddr'])) {\r
288 $default_values['sendemailaddr'] = $this->typeconfig['sendemailaddr'];\r
73300339
CS
289 } else if (isset($CFG->lti_sendemailaddr)) {\r
290 $default_values['sendemailaddr'] = $CFG->lti_sendemailaddr;\r
b7e436b0
CS
291 }\r
292 }\r
293\r
294 if (!isset($default_values['instructorchoicesendemailaddr'])) {\r
295 if (isset($this->typeconfig['instructorchoicesendemailaddr'])) {\r
296 $default_values['instructorchoicesendemailaddr'] = $this->typeconfig['instructorchoicesendemailaddr'];\r
297 } else {\r
298 if ($this->typeconfig['sendemailaddr'] == 2) {\r
73300339 299 $default_values['instructorchoicesendemailaddr'] = $CFG->lti_instructorchoicesendemailaddr;\r
b7e436b0
CS
300 } else {\r
301 $default_values['instructorchoicesendemailaddr'] = $this->typeconfig['sendemailaddr'];\r
302 }\r
303 }\r
304 }\r
305\r
306 if (!isset($default_values['acceptgrades'])) {\r
307 if (isset($this->typeconfig['acceptgrades'])) {\r
308 $default_values['acceptgrades'] = $this->typeconfig['acceptgrades'];\r
73300339
CS
309 } else if (isset($CFG->lti_acceptgrades)) {\r
310 $default_values['acceptgrades'] = $CFG->lti_acceptgrades;\r
b7e436b0
CS
311 }\r
312 }\r
313\r
314 if (!isset($default_values['instructorchoiceacceptgrades'])) {\r
315 if (isset($this->typeconfig['instructorchoiceacceptgrades'])) {\r
316 $default_values['instructorchoiceacceptgrades'] = $this->typeconfig['instructorchoiceacceptgrades'];\r
317 } else {\r
318 if ($this->typeconfig['acceptgrades'] == 2) {\r
73300339 319 $default_values['instructorchoiceacceptgrades'] = $CFG->lti_instructorchoiceacceptgrades;\r
b7e436b0
CS
320 } else {\r
321 $default_values['instructorchoiceacceptgrades'] = $this->typeconfig['acceptgrades'];\r
322 }\r
323 }\r
324 }\r
325\r
326 if (!isset($default_values['allowroster'])) {\r
327 if (isset($this->typeconfig['allowroster'])) {\r
328 $default_values['allowroster'] = $this->typeconfig['allowroster'];\r
73300339
CS
329 } else if (isset($CFG->lti_allowroster)) {\r
330 $default_values['allowroster'] = $CFG->lti_allowroster;\r
b7e436b0
CS
331 }\r
332 }\r
333\r
334 if (!isset($default_values['instructorchoiceallowroster'])) {\r
335 if (isset($this->typeconfig['instructorchoiceallowroster'])) {\r
336 $default_values['instructorchoiceallowroster'] = $this->typeconfig['instructorchoiceallowroster'];\r
337 } else {\r
338 if ($this->typeconfig['allowroster'] == 2) {\r
73300339 339 $default_values['instructorchoiceallowroster'] = $CFG->lti_instructorchoiceallowroster;\r
b7e436b0
CS
340 } else {\r
341 $default_values['instructorchoiceallowroster'] = $this->typeconfig['allowroster'];\r
342 }\r
343 }\r
344 }\r
345\r
346 if (!isset($default_values['allowsetting'])) {\r
347 if (isset($this->typeconfig['allowsetting'])) {\r
348 $default_values['allowsetting'] = $this->typeconfig['allowsetting'];\r
73300339
CS
349 } else if (isset($CFG->lti_allowsetting)) {\r
350 $default_values['allowsetting'] = $CFG->lti_allowsetting;\r
b7e436b0
CS
351 }\r
352 }\r
353\r
354 if (!isset($default_values['instructorchoiceallowsetting'])) {\r
355 if (isset($this->typeconfig['instructorchoiceallowsetting'])) {\r
356 $default_values['instructorchoiceallowsetting'] = $this->typeconfig['instructorchoiceallowsetting'];\r
357 } else {\r
358 if ($this->typeconfig['allowsetting'] == 2) {\r
73300339 359 $default_values['instructorchoiceallowsetting'] = $CFG->lti_instructorchoiceallowsetting;\r
b7e436b0
CS
360 } else {\r
361 $default_values['instructorchoiceallowsetting'] = $this->typeconfig['allowsetting'];\r
362 }\r
363 }\r
364 }\r
365\r
366 if (!isset($default_values['customparameters'])) {\r
367 if (isset($this->typeconfig['customparameters'])) {\r
368 $default_values['customparameters'] = $this->typeconfig['customparameters'];\r
73300339
CS
369 } else if (isset($CFG->lti_customparameters)) {\r
370 $default_values['customparameters'] = $CFG->lti_customparameters;\r
b7e436b0
CS
371 }\r
372 }\r
373\r
374 if (!isset($default_values['allowinstructorcustom'])) {\r
375 if (isset($this->typeconfig['allowinstructorcustom'])) {\r
376 $default_values['allowinstructorcustom'] = $this->typeconfig['allowinstructorcustom'];\r
73300339
CS
377 } else if (isset($CFG->lti_allowinstructorcustom)) {\r
378 $default_values['allowinstructorcustom'] = $CFG->lti_allowinstructorcustom;\r
b7e436b0
CS
379 }\r
380 }\r
381\r
382 if (!isset($default_values['organizationid'])) {\r
383 if (isset($this->typeconfig['organizationid'])) {\r
384 $default_values['organizationid'] = $this->typeconfig['organizationid'];\r
73300339
CS
385 } else if (isset($CFG->lti_organizationid)) {\r
386 $default_values['organizationid'] = $CFG->lti_organizationid;\r
b7e436b0
CS
387 }\r
388 }\r
389\r
390 if (!isset($default_values['organizationurl'])) {\r
391 if (isset($this->typeconfig['organizationurl'])) {\r
392 $default_values['organizationurl'] = $this->typeconfig['organizationurl'];\r
73300339
CS
393 } else if (isset($CFG->lti_organizationurl)) {\r
394 $default_values['organizationurl'] = $CFG->lti_organizationurl;\r
b7e436b0
CS
395 }\r
396 }\r
397\r
398 if (!isset($default_values['organizationdescr'])) {\r
399 if (isset($this->typeconfig['organizationdescr'])) {\r
400 $default_values['organizationdescr'] = $this->typeconfig['organizationdescr'];\r
73300339
CS
401 } else if (isset($CFG->lti_organizationdescr)) {\r
402 $default_values['organizationdescr'] = $CFG->lti_organizationdescr;\r
b7e436b0
CS
403 }\r
404 }\r
405\r
406 if (!isset($default_values['launchinpopup'])) {\r
407 if (isset($this->typeconfig['launchinpopup'])) {\r
408 $default_values['launchinpopup'] = $this->typeconfig['launchinpopup'];\r
73300339
CS
409 } else if (isset($CFG->lti_launchinpopup)) {\r
410 $default_values['launchinpopup'] = $CFG->lti_launchinpopup;\r
b7e436b0
CS
411 }\r
412 }\r
285f8250 413*/\r
b7e436b0
CS
414 }\r
415}\r
416\r