made a copy
[atutor.git] / documentation / developer / modules.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2 <html lang="en">
3 <head>
4         <title>ATutor Module Documentation</title>
5         <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
6         <meta name="author" content="Joel Kronenberg/Greg Gay" />
7         <meta name="description" content="ATutor module developer documentation" />
8 <style type="text/css">
9
10 h1 {
11         background-color: #CCCCCC;
12         padding-left: 20px;
13         padding-right: 20px;
14         margin-bottom: 10px;
15         text-align: right;
16 }
17
18 h2 {
19         background-color: #DDDDDD;
20         padding-left: 10px;
21         margin-bottom: 0px;
22 }
23 h3 {
24         background-color: #EFEFEF;
25         padding-left: 20px;
26         margin-bottom: 0px;
27 }
28 h4  { margin-bottom: 0px; }
29 p   { margin-top: 0px;    }
30 dl  { margin-top: 0px;    }
31 kbd {
32         padding: 0px 1px 0px 1px;
33         border-width: 1px 2px 2px 1px;
34         border-style: solid;
35         border-color: #edd #baa #baa #eed;
36         white-space: pre;
37 }
38 kbd em {
39         font-weight: bold;
40         background-color: #efefef;
41 }
42 blockquote { font-style: italic; }
43 pre.code {
44         background-color: #EEEEFF;
45         padding: 5px;
46         margin-left: 20px;
47         color:#761596;
48     margin-top: 0px;
49 }
50 .top {
51         float: right;
52         color: green;
53         padding-top: 2px;
54         padding-right: 5px;
55 }
56 pre {
57         background-color: #EEEEFF;
58         font-family: Courier, monospace;
59         border-left: 1px solid #761596;
60         padding: 0px 0px 0px 10px;
61         color: #333;
62         margin: 0px 0px 10px 20px;
63         font-size: 88%;
64         line-height: 1.2em;
65 }
66 @media print {
67         h2 {
68                 page-break-after: avoid;
69                 border-bottom: solid 1px black;
70         }
71         h3 {
72                 page-break-after: avoid;
73                 border-bottom: solid 1px black;
74                 width: 75%;
75         }
76         .top {
77                 display: none;
78         }
79
80         pre.code {
81                 page-break-inside: avoid;
82         }
83 }
84 }
85 acronym {
86         cursor: help;
87 }
88 </style>
89
90 </style>
91 </head>
92 <body><h1>Module Development Documentation</h1>
93 <ul>
94 <li><a href="#intro">Introduction</a></li>
95 <li><a href="#structure">Structure</a></li>
96         <ul>
97         <li><a href="#dirname">Directory Name</a></li>
98         <li><a href="#files">Files</a>
99         <ul>
100         <li><a href="#module.xml">module.xml</a></li>
101         <li><a href="#module.php">module.php</a></li>
102         <li><a href="#module.sql">module.sql</a></li>
103         </ul></li>
104         </ul>
105 <li><a href="#installation">Installation</a></li>
106         <ul>
107         <li><a href="#module_install">module_install.php</a></li>
108         <li><a href="#privs">Specifying Privileges</a></li>
109         <li><a href="#data_dir">Creating a Data Directory</a></li>
110         <li><a href="#execute">Executing an SQL FILE</a></li>
111         <li><a href="#errors">Generating Error Messages</a></li>
112         <li><a href="#uninstall">Uninstalling a Module</a></li>
113         </ul>
114
115 <li><a href="#auth">Authentication &amp; Privileges</a></li>
116 <li><a href="#localisation">Localisation</a></li>
117 <li><a href="#config">Configuration Options</a></li>
118 <li><a href="#styles">Custom Style Sheets</a></li>
119 <li><a href="#sidemenu">Side Menu Boxes</a></li>
120 <li><a href="#sublinks">Sublinks for Details View</a></li>
121 <li><a href="#tools">Student Tools</a></li>
122 <li><a href="#groups">Group Tools</a></li>
123 <li><a href="#navigation">Navigation &amp; Hierarchy</a></li>
124 <li><a href="#delete">Course Deletion</a></li>
125 <li><a href="#backup">Backing-Up and Restoring</a></li>
126 <ul>
127 <li><a href="#directories">Directories</a></li>
128 <li><a href="#database">Database Tables</a></li>
129 </ul>
130 <li><a href="#cron">Running Cron/Scheduling</a></li>
131 </ul>
132
133 <a name="intro"></a>
134 <h2>Introduction</h2>
135         <p>ATutor 1.5.2 introduced the concept of modules, providing developers with a framework to implement additional functionality in a coherent and loosely coupled way.</p>
136
137         <p>The framework defines methods for assigning privileges, backing-up and restoring content, deleting course specific content, and adding side menu blocks,  student tools, course management and administrative tools, as well as public tools and other types of added functionality . </p>
138
139         <p>The intent is to allow for the development and distribution of modules independent of the ongoing development and release of ATutor. The module structure does allow for the creation of modules that run software that is not distributed under the <acronym title="GNU Not Unix">GNU</acronym> General Public License, but distributed separately under their own, perhaps commercial licenses.</p>
140
141         <p>The <em>Hello World</em> example module is included with each ATutor distribution for developers who want to investigate how modules work. The module is found in the <kbd>mods/hello_world</kbd> directory. A copy of the Hello World module works well as a starting point for creating a new module, since it implements (in a simple way) just about all the features found in modules. Also see the files from <a href="http://www.atutor.ca/atutor/modules.php">other modules</a> that operate like you expect your module to operate.</p>
142
143 <a name="structure"></a>
144 <h2>Structure</h2>
145         <p>Modules are stored under ATutor's <kbd>mods</kbd> directory. <em>Core</em> modules are stored in the <kbd>mods/_core</kbd> subdirectory and are made available with every release of ATutor. These modules cannot be disabled by the administrator as they are vital to ATutor's functionality. <em>Standard</em> modules are stored in the <kbd>mods/_standard</kbd> subdirectory and are also made available with every release of ATutor. Standard modules can be disabled by the administrator. <em>Extra</em> modules are stored in the <kbd>mods</kbd> directory and are installed and distributed independently of ATutor. Although the process of developing modules is the same for each type of module, only <em>extra</em> modules can be distributed separately, while <em>core</em> and <em>standard</em> modules are added to the ATutor code repository (i.e. <acronym title="Subversion">SVN</acronym> trunk).</p>
146
147         <p>Whenever a module identifier is needed within code, it should appear in lowercase with spaces converted to underscores.</p>
148
149         <p>The module name, and hence the directory and function names (see below for additional details), must be unique across all possible modules. A module should not be made available if an existing module is already being distributed under that same name. It is up to the module developer to ensure that their module name is unique.</p>
150         <a name="dirname"></a>
151         <h3>Directory Name</h3>
152                 <p>The name given to the directory must be chosen carefully. The name is used to namespace the module's function by prefixing required functions with that directory name. For example, a module named <em>Example Maker</em> should be placed in a directory named <kbd>example_maker</kbd> and the delete function would be named <kbd><em>example_maker</em>_delete()</kbd>.</p>
153         <a name="files"></a>
154         <h3>Files</h3>
155                 <p>The following files should exist under the module's top level directory: <kbd>mods/<em>module_name</em></kbd>.</p>
156
157                 <dl>
158                         <dt><kbd>module.php</kbd></dt>
159                         <dd>This is the main module file which gets included whenever a page is loaded. <em>Required</em>.</dd>
160
161                         <dt><kbd>module.xml</kbd></dt>
162                         <dd>This file is used only for identifying the module for distribution and is only used when viewing a module's details. <em>Required</em>.</dd>
163
164                         <dt><kbd>module_install.php</kbd></dt>
165                         <dd>This file is used when installing the module. <em>Required</em>.</dd>
166
167                         <dt><kbd>module_uninstall.php</kbd></dt>
168                         <dd>This file is used to remove the module from the system. <em>Required for ATutor  1.6.2+</em>.</dd>
169
170                         <dt><kbd>module_backup.php</kbd></dt>
171                         <dd>This file is used when backing-up and restoring course content. <em>Optional</em>.</dd>
172
173                         <dt><kbd>module_delete.php</kbd></dt>
174                         <dd>This file is used when deleting course specific content. <em>Optional</em>.</dd>
175
176
177                         <dt><kbd>module_cron.php</kbd></dt>
178                         <dd>This file is used to run module related commands at specified intervals. <em>Optional</em>.</dd>
179
180                         <dt><kbd>module.sql</kbd></dt>
181                         <dd>This file is used to add tables and/or modify data in the ATutor database. Also used to insert language into the <kbd>language_text</kbd> table. <em>Optional</em>.</dd>
182                 </dl>
183 <a name="module.xml"></a>
184         <h3>The <kbd>module.xml</kbd> File</h3>
185                 <p>The <kbd>module.xml</kbd> file is used for displaying information about the module before it is installed and is useful when distributing the module.</p>
186 <pre>
187 &lt;?xml version="1.0" encoding="ISO-8859-1"?> 
188 &lt;module version="0.1"> 
189     &lt;name lang="en">Example Maker&lt;/name> 
190     &lt;description lang="en">This is an example module that makes examples.&lt;/description> 
191     &lt;maintainers>
192         &lt;maintainer> 
193             &lt;name>ATutor Team&lt;/name> 
194             &lt;email>info@atutor.ca&lt;/email> 
195         &lt;/maintainer>
196         &lt;maintainer> 
197             &lt;name>John Doe&lt;/name> 
198             &lt;email>jd@example.com&lt;/email> 
199         &lt;/maintainer>
200     &lt;/maintainers> 
201     &lt;url>http://www.example.com&lt;/url> 
202     &lt;license>BSD&lt;/license> 
203     &lt;release> 
204         &lt;version>0.2&lt;/version> 
205         &lt;date>2005-08-22&lt;/date> 
206         &lt;state>stable&lt;/state> 
207         &lt;notes>Fixes several bugs in previous version.&lt;/notes> 
208     &lt;/release> 
209 &lt;/module>
210 </pre>
211
212 <a name="module.php"></a>
213         <h3>The <kbd>module.php</kbd> File</h3>
214 <p>The <kbd>module.php</kbd> file is typically used to set permissions, and to link module components into the ATutor navigation elements, as tool icons, navigation tabs, sub navigation menus, or side menu blocks, as administrator, course management, and student tools. It is also used to link tools to  My Start Page, or to various public pages that appear where a user is browsing the system without being logged in. The <kbd>module.php</kbd> file can also be used to run module specific functionality. It runs everytime a module screen is viewed, loading whatever settings it may contain, or running any scripts that may be required by the module.</p>
215 <pre>
216 &lt;?php
217 /*******
218  * doesn't allow this file to be loaded with a browser.
219  */
220 if (!defined('AT_INCLUDE_PATH')) { exit; }
221
222 /******
223  * this file must only be included within a Module obj
224  */
225 if (!isset($this) || (isset($this) && (strtolower(get_class($this)) != 'module'))) { exit(__FILE__ . ' is not a Module'); }
226
227
228 /******
229 * modules sub-content to display on course home detailed view
230 */
231 $this->_list['hello_world'] = array('title_var'=>'hello_world','file'=>'mods/hello_world/sublinks.php');
232
233 /*******
234  * assign the instructor and admin privileges to the constants.
235  */
236 define('AT_PRIV_EXAMPLE_MAKER',       $this->getPrivilege());
237 define('AT_ADMIN_PRIV_EXAMPLE_MAKER', $this->getAdminPrivilege());
238
239 /*******
240  * create a side menu box/stack.
241  */
242 $this->_stacks['example_maker'] = array('title_var'=>'example_maker', 'file'=>'mods/example_maker/side_menu.inc.php');
243 // ** possible alternative: **
244 // $this->addStack('example_maker', array('title_var' => 'example_maker', 'file' => './side_menu.inc.php');
245
246 /*******
247  * if this module is to be made available to students on the Home or Main Navigation.
248  */
249 $_student_tool = 'mods/example_maker/index.php';
250 // ** possible alternative: **
251 // $this->addTool('./index.php');
252
253 /*******
254  * add the admin pages when needed.
255  */
256 if (admin_authenticate(AT_ADMIN_PRIV_EXAMPLE_MAKER, TRUE) || admin_authenticate(AT_ADMIN_PRIV_ADMIN, TRUE)) {
257         $this->_pages[AT_NAV_ADMIN] = array('mods/example_maker/index_admin.php');
258         $this->_pages['mods/example_maker/index_admin.php']['title_var'] = 'example_maker';
259         $this->_pages['mods/example_maker/index_admin.php']['parent']    = AT_NAV_ADMIN;
260 }
261
262 /*******
263  * instructor Manage section:
264  */
265 $this->_pages['mods/example_maker/index_instructor.php']['title_var'] = 'example_maker';
266 $this->_pages['mods/example_maker/index_instructor.php']['parent']   = 'tools/index.php';
267 // ** possible alternative: **
268 // $this->pages['./index_instructor.php']['title_var'] = 'example_maker';
269 // $this->pages['./index_instructor.php']['parent']    = 'tools/index.php';
270
271 /*******
272  * student page.
273  */
274 $this->_pages['mods/example_maker/index.php']['title_var'] = 'example_maker';
275 $this->_pages['mods/example_maker/index.php']['img']       = 'mods/example_maker/example_maker.jpg';
276
277
278 /* public pages */
279 $this->_pages[AT_NAV_PUBLIC] = array('mods/example_maker/index_public.php');
280 $this->_pages['mods/example_maker/index_public.php']['title_var'] = 'example_maker';
281 $this->_pages['mods/example_maker/index_public.php']['parent'] = 'login.php';
282 $this->_pages['login.php']['children'] = array('mods/example_maker/index_public.php');
283
284 /* my start page pages */
285 $this->_pages[AT_NAV_START]  = array('mods/example_maker/index_mystart.php');
286 $this->_pages['mods/example_maker/index_mystart.php']['title_var'] = 'example_maker';
287 $this->_pages['mods/example_maker/index_mystart.php']['parent'] = 'users/index.php';
288 $this->_pages['users/index.php']['children'] = array('mods/example_maker/index_mystart.php');
289 ?>
290 </pre>
291 <a name="module.sql"></a>
292         <h3>The <kbd>module.sql</kbd> File</h3>
293 <p>A very simple <kbd>module.sql</kbd> file such as the following, creates a table for the module, and inserts it's language into the ATutor <kbd>language_text</kbd> table. Module language can then be managed from the ATutor Language Manager. The <kbd>module.sql</kbd> file can contain any number of SQL statements used to add tables or insert data into the ATutor database. </p>
294
295 <pre>
296 # sql file for example maker module
297
298 CREATE TABLE example_maker (
299    `course_id` mediumint(8) unsigned NOT NULL,
300    `value` VARCHAR( 30 ) NOT NULL ,
301    PRIMARY KEY ( `course_id` )
302 );
303
304 INSERT INTO `language_text` VALUES ('en', '_module','example_maker','Example Maker',NOW(),'');
305 INSERT INTO `language_text` VALUES ('en', '_module','AT_ERROR_GOES_HERE','Example Maker Error Message',NOW(),'');
306
307 </pre>
308 <a name="installation"></a>
309 <h2>Installation</h2>
310         <p>The <kbd>module_install.php</kbd> script gets executed during the installation process, using the administrator's Install Module feature. If the script's execution results in <kbd>$msg->containsErrors()</kbd> evaluating to <kbd>TRUE</kbd>, then the errors are displayed and the user is prompted to correct them. The process is then repeated until errors are no longer being generated and the module is installed successfully. Ultimately, it is up to the module to determine the logical steps involved in its installation. For example, it might be better to create the data directories before trying to create any database tables since creating the directory may require several attempts. Typically the flow we describe here should be suitable in most cases.</p>
311
312         <p>Theoretically, the install script's execution is wide-open and does not have to adhere to the process outlined below or make use of any special privileges, provided it generates errors as appropriate.</p>
313
314 <pre># pseudo-code for installing a module:
315 while (there are errors)
316     print the error message
317
318     # inside module_install.php:
319     define the privileges used
320
321     if (create database tables is unsuccessful) then
322         generate an error message
323
324     if (there are no errors AND there is an SQL file) then
325         execute the SQL file
326 end while
327
328 add the module to the system using the defined privileges
329 </pre>
330
331 <a name="module_install"></a>
332         <h3>The <kbd>module_install.php</kbd> File</h3>
333 <p>The <kbd>module_install.php</kbd> file is typically used to run any installation related files, such as an sql file that sets up a database table, or installs the language for the module. The following is an example module_install.php file</p>
334 <pre>
335 &lt;?php
336 /*******
337  * the line below safe-guards this file from being accessed directly from
338  * a web browser. It will only execute if required from within an ATutor script,
339  * in our case the Module::install() method.
340  */
341 if (!defined('AT_INCLUDE_PATH')) { exit; }
342
343 /*******
344  * Note: the many options for these variables are used to decrease confusion.
345  *       TRUE | FALSE | 1 will be the convention.
346  *
347  * $_course_privilege
348  *     specifies the type of instructor privilege this module uses.
349  *     set to empty | FALSE | 0   to disable any privileges.
350  *     set to 1 | AT_PRIV_ADMIN   to use the instructor only privilege.
351  *     set to TRUE | 'new'        to create a privilege specifically for this module:
352  *                                will make this module available as a student privilege.
353  *
354  * $_admin_privilege
355  *    specifies the type of ATutor administrator privilege this module uses.
356  *    set to FALSE | AT_ADMIN_PRIV_ADMIN   to use the super administrator only privilege.
357  *    set to TRUE | 'new'                  to create a privilege specifically for this module:
358  *                                         will make this module available as an administrator privilege.
359  *
360  *
361  * $_cron_interval
362  *    if non-zero specifies in minutes how often the module's cron job should be run.
363  *    set to 0 or not set to disable.
364  */
365 $_course_privilege = TRUE; // possible values: FALSE | AT_PRIV_ADMIN | TRUE
366 $_admin_privilege  = TRUE; // possible values: FALSE | TRUE
367 $_cron_interval    = 35; // run every 30 minutes
368
369
370 /********
371  * the following code is used for creating a module-specific directory.
372  * it generates appropriate error messages to aid in its creation.
373  */
374 $directory = AT_CONTENT_DIR .'example_maker';
375
376 // check if the directory is writeable
377 if (!is_dir($directory) && !@mkdir($directory)) {
378         $msg->addError(array('MODULE_INSTALL', '&lt;li>'.$directory.' does not exist. Please create it.&lt;/li>'));
379 } else if (!is_writable($directory) && @chmod($directory, 0666)) {
380         $msg->addError(array('MODULE_INSTALL', '&lt;li>'.$directory.' is not writeable. On Unix issue the command &lt;kbd>chmod a+rw&lt;/kbd>.&lt;/li>'));
381 }
382
383
384 /******
385  * the following code checks if there are any errors (generated previously)
386  * then uses the SqlUtility to run any database queries it needs, ie. to create
387  * its own tables.
388  */
389 if (!$msg->containsErrors() && file_exists(dirname(__FILE__) . '/module.sql')) {
390         // deal with the SQL file:
391         require(AT_INCLUDE_PATH . 'classes/sqlutility.class.php');
392         $sqlUtility =& new SqlUtility();
393
394         /*
395          * the SQL file could be stored anywhere, and named anything, "module.sql" is simply
396          * a convention we're using.
397          */
398         $sqlUtility->queryFromFile(dirname(__FILE__) . '/module.sql', TABLE_PREFIX);
399 }
400
401 ?>
402 </pre>
403
404
405 <a name="privs"></a>
406         <h3>Specifying Privileges</h3>
407                 <p>Privileges control who has access to the course management and administrative sections.</p>
408                 
409                 <p>See the Authentication &amp; Privileges section for additional details using the privileges.</p>
410
411                 <dl>
412                         <dt><kbd>$_course_privilege</kbd></dt>
413                         <dd><p>This variable controls access to a course's management section and can take one of the following values:</p>
414                                 <ul>
415                                         <li><kbd>TRUE</kbd>: To use a custom assignable privilege level. If a custom privilege is created, the module will appear as an option when assigning privileges to enrolled students.</li>
416                                         <li><kbd>AT_PRIV_ADMIN</kbd>: To use the instructor privilege. Only the instructor will be given access to the module's management section.</li>
417                                         <li><kbd>FALSE</kbd>: To disable the privilege if there is no management section for the module.</li>
418                                 </ul>
419                         </dd>
420
421                         <dt><kbd>$_admin_privilege</kbd></dt>
422                         <dd><p>This variable can take one of the following values:</p>
423                                 <ul>
424                                         <li><kbd>TRUE</kbd>: To use a custom assignable privilege level. If a custom privilege is created then the module will appear as an option when assigning privileges to administrators</li>
425                                         <li><kbd>AT_ADMIN_PRIV_ADMIN</kbd>: To use the super administrator privilege. Only the super administrator will be given access to the module's administration section.</li>
426                                 </ul>
427                         </dd>
428                 </dl>
429
430                 <p>Note that creating a privilege is not in itself enough to make the module appear in the Manage section! The hierarchy and navigation path to the management page must be set correctly. See the Navigation &amp; Hierarchy section for additional details.</p>
431 <a name="data_dir"></a>
432         <h3>Creating a Data Directory</h3>
433                 <p>It is best to keep the directory within the <kbd>AT_CONTENT_DIR</kbd> directory as it should already allow the creation of files and directories by the web server. It is then up to the module to create individual course directories as needed.</p>
434 <pre>
435 $directory = AT_CONTENT_DIR .'example_maker';
436
437 // check if the directory is writable
438 if (!is_dir($directory) && !@mkdir($directory)) {
439     $msg->addError(array('MODULE_INSTALL', '&lt;li>'
440                          .$directory
441                          .' does not exist. Please create it.&lt;/li>'));
442 } else if (!is_writable($directory) && @chmod($directory, 0666)) {
443     $msg->addError(array('MODULE_INSTALL', '&lt;li>'
444                          . $directory
445                          .' is not writable.&lt;/li>'));
446 } // else: it was created successfully.
447 </pre>
448 <a name="execute"></a>
449         <h3>Executing an <acronym title="Structured Query Language">SQL</acronym> File</h3>
450                 <p>If the module requires its own database tables or custom language, then it will have to create them itself. The <acronym title="Structured Query Language">SQL</acronym> can either be executed inline using <acronym title="PHP Hypertext Processor">PHP</acronym> database execution directly, or using the <kbd>SQLUtility</kbd> class to execute an external <acronym title="Structured Query Language">SQL</acronym> file. Also see <a href="#module.sql">module.sql</a> for more about creating an SQL file.</p>
451 <pre>
452 if (!$msg->containsErrors() && file_exists(dirname(__FILE__) . '/module.sql')) {
453     // deal with the SQL file:
454     require(AT_INCLUDE_PATH . 'classes/sqlutility.class.php');
455     $sqlUtility =& new SqlUtility();
456     $sqlUtility->queryFromFile(dirname(__FILE__) . '/module.sql', TABLE_PREFIX);
457 }
458 </pre>
459 <a name="errors"></a>
460         <h3>Generating Errors Messages</h3>
461                 <p>It is up to the module to generate and check for any errors that occur during the installation. An error message can be generated using <kbd>$msg->addError(array('MODULE_INSTALL', '&lt;li>your error msg goes here&lt;/li>'));</kbd>. Note that the text supplied to the error message is not translated in this case. If the language should be localised, then the appropriate language vairable should replace the text. Something like <kbd>$msg->addError(array('MODULE_INSTALL', 'AT_ERROR_GOES_HERE'));</kbd> . The corresponding SQL INSERT statement should then be found in the <a href="#module.sql">module.sql </a>file, so the language gets added to the <kbd>language_text</kbd> table in ATutor. </p>
462
463                 <p>To check if any errors have been generated, use <kbd>$msg->containsErrors()</kbd> which evaluates to <kbd>TRUE</kbd> if a previous error has been generated.</p>
464                 
465                 <p>See the <a href="guidelines.html#error-feedback-messages">Error and Feedback </a>section of the Developer Documentation for more details about displaying messages.</p>
466
467 <a name="uninstall"></a>
468         <h3>Uninstalling a Module</h3>
469 <p>As of ATutor 1.6.2 a <kbd>module_uninstall.php</kbd> file is required with each module. This file uses the original module.sql file to deletes any database tables, and any language that may have been installed with <kbd>module.sql</kbd>, and removes any directories used by the module, and deletes the module directory itself.</p>
470
471 <pre>
472
473 /*******
474  * module_uninstall.php performs reversion of module_install.php
475  */
476
477 /*******
478  * the line below safe-guards this file from being accessed directly from
479  * a web browser. It will only execute if required from within an ATutor script,
480  * in our case the Module::uninstall() method.
481  */
482 if (!defined('AT_INCLUDE_PATH')) { exit; }
483
484 /********
485  * the following code is used for removing a module-specific directory created in module_install.php.
486  * it generates appropriate error messages to aid in its creation.
487  */
488 $directory = AT_CONTENT_DIR .'example_maker';
489
490 // check if the directory exists
491 if (is_dir($directory)) {
492         require(AT_INCLUDE_PATH.'lib/filemanager.inc.php');
493
494         if (!clr_dir($directory))
495                 $msg->addError(array('MODULE_UNINSTALL', '<li>'.$directory.' can not be removed. Please manually remove it.</li>'));
496 }
497
498 /******
499  * the following code checks if there are any errors (generated previously)
500  * then uses the SqlUtility to run reverted database queries of module.sql, 
501  * ie. "create table" statement in module.sql is run as drop according table.
502  */
503 if (!$msg->containsErrors() && file_exists(dirname(__FILE__) . '/module.sql')) {
504         // deal with the SQL file:
505         require(AT_INCLUDE_PATH . 'classes/sqlutility.class.php');
506         $sqlUtility =& new SqlUtility();
507
508         /*
509          * the SQL file could be stored anywhere, and named anything, "module.sql" is simply
510          * a convention we're using.
511          */
512         $sqlUtility->revertQueryFromFile(dirname(__FILE__) . '/module.sql', TABLE_PREFIX);
513 }
514
515 </pre>
516
517 <a name="auth"></a>
518 <h2>Authentication &amp; Privileges</h2>
519         <p>See the <em>Installation: Specifying Privileges</em> section on creating privileges during the installation process.</p>
520
521         <p>Authentication uses constants for the privilege levels. The privileges should be declared in the <kbd>module.php</kbd> file using the <kbd>$this->getPrivilege()</kbd> and <kbd>$this->getAdminPrivilege()</kbd> methods, respectively.</p>
522
523 <pre>
524 define('AT_PRIV_FORUMS',       $this->getPrivilege()      );
525 define('AT_ADMIN_PRIV_FORUMS', $this->getAdminPrivilege() );
526 </pre>
527
528         <p>Once declared, a page can then authenticate against those privileges using either the <kbd>authentication()</kbd> or the <kbd>admin_authenticate()</kbd> functions.</p>
529
530 <pre>
531 define('AT_INCLUDE_PATH', '../include/');
532 require(AT_INCLUDE_PATH.'vitals.inc.php');
533 // authenticate the administrator forums section:
534 admin_authenticate(AT_ADMIN_PRIV_FORUMS);
535 </pre>
536 <a name="localisation"></a>
537 <h2>Localisation</h2>
538         <p>Although a module can be created with all hard-coded language, we recommended you use ATutor's localisation functions. All of ATutor's language is stored in the database, which is then retrieved using the <kbd>_AT()</kbd> function for simple terms and the <kbd>$msg</kbd> object for feedback and error messages.</p>
539
540         <p>Additional details on localising ATutor can be found on the <a href="http://atutor.ca/atutor/docs/translate.php">Thing You Should Know Before Translating</a> and in the <a href="guidelines.html#fn-at">ATutor Developer Documentation</a>.</p>
541
542         <p>Module-specific language should be inserted into the <kbd>language_text</kbd> table during the installation process. The fields in the table are as follows:</p>
543                                           
544         <dl>
545                 <dt><kbd>language_code</kbd></dt>
546                 <dd>The ISO-639-1 language code plus locale.</dd>
547
548                 <dt><kbd>variable</kbd></dt>
549                 <dd>Set to <kbd>_module</kbd> for modules.</dd>
550
551                 <dt><kbd>term</kbd></dt>
552                 <dd>The variable used for retrieving the language.</dd>
553
554                 <dt><kbd>text</kbd></dt>
555                 <dd>The language text.</dd>
556
557                 <dt><kbd>revised_date</kbd></dt>
558                 <dd>Set to <kbd>NOW()</kbd> for modules.</dd>
559
560                 <dt><kbd>context</kbd></dt>
561                 <dd>Short description of the language text.</dd>
562         </dl>
563
564 <p>Each language item should have a corresponding SQL INSERT line in the module.sql file, that gets inserted into the ATutor <kbd>language_text</kbd> table during installation. Do not include a prefix (e.g. "AT_") on the <kbd>language_text</kbd> table name. The installer will detect the right prefix, and automatically prepend it to tables names. Also see the section <a href="#module.sql">module.sql</a> for information about creating a file to install the module's language.</p>
565 <pre>
566 # Insert module specific language:
567 INSERT INTO `language_text` VALUES ('en',
568                                     '_module',
569                                     'example_maker',
570                                     'Example Maker',
571                                      NOW(),
572                                     'the module title');
573 </pre>
574 <p>(Introduced in ATutor 1.5.4) On occassion it may be necessary to modify existing ATutor language to accommodate module functionality that alters the way ATutor itself functions by default. For example, when the payments module is installed the message displayed when a student enrolls in a course that requires a payment, needs to include mention of how to make a payment. In the SQL statement below, the second value (i.e. "variable" in the language_text table) is prefixed with "_c" for custom language. Possible custom language variables are _c_msgs, _c_template, and _c_module.</p>
575
576 <pre>
577 # Insert custom language:
578 INSERT INTO `language_text` VALUES ('en', 
579                                         '_c_msgs',
580                                         'AT_INFOS_EC_PAYMENTS_TURNED_OFF','
581                                         Your request has been made. You will be notifed when your request has been approved. If course fees are pending, they will be listed under the <a href="mods/ecomm/index_mystart.php">Payments</a> tab above, where they can be paid.',
582                                         NOW(),'');
583 </pre>
584
585 <a name="config"></a>
586 <h2>Configuration Options</h2>
587 <p>Module configuration options can be stored in the ATutor <kbd>config</kbd> table, and retrieved using a <kbd>$_config[]</kbd> array variable. All module configuration key/value pairs are automatically loaded from the table into global memory while ATutor is running, and can be accessed from any of the module scripts (or from anywhere is ATutor for that matter). The value for a particular configuration option is retrieved by entering its key into the array. To retrieve a URL for the Example Maker module for example,  you might use <kbd>$_config['example_maker']</kbd>. </p>
588 <p>In the following example of an <kbd>index_admin.php</kbd> file, the form below accepts a URL to an external application used by the Example Maker module (though it could be any value). In this case imagine a third party application has been installed, and the URL to the application is being stored as a configuration option for the example_maker module. When the form is submitted, <kbd>$_POST['uri']</kbd> is inserted into the config table as the value for the <kbd>example_maker</kbd> key. The following is an example module administrator script used to add/edit a config option for the example_maker module.</p>
589
590 <h3>The <kbd>index_admin.php</kbd> File</h3>
591 <pre>
592
593 &lt;?php
594 // make sure user is allowed to see this page (admins only)
595
596 admin_authenticate(AT_ADMIN_PRIV_EXAMPLE_MAKER);
597         
598 if (isset($_POST['submit'])) {
599         // trim whitespace from the value submitted
600         $_POST['uri'] = trim($_POST['uri']);
601
602         // display an error message if the value is empty
603         if (!$_POST['uri']){
604                 $msg->addError('EXAMPLE_MAKER_ADD_EMPTY');
605         }
606         
607         // if no errors, insert the key "example_maker" and value "$_POST['uri']" into the config table 
608         if (!$msg->containsErrors()) {
609                 $_POST['uri'] = $addslashes($_POST['uri']);
610                 $sql = "REPLACE INTO ".TABLE_PREFIX."config VALUES ('example_maker', '$_POST[uri]')";
611                 mysql_query($sql, $db);
612                 $msg->addFeedback('EXAMPLE_MAKER_URL_SAVED');
613
614                 header('Location: '.$_SERVER['PHP_SELF']);
615                 exit;
616         }
617 }
618
619 require (AT_INCLUDE_PATH.'header.inc.php');
620
621 /*******
622  *  First check to see if there is a value for the example_maker key $_config['example_maker']
623  *  If there isn't a value then a missing value message is displayed
624  *  The form below that has a single field for submitting a value, in this case a URL
625  *  If the value exists in the config table, then display it in the text field using  $_config['example_maker']
626  */
627         
628 ?>
629
630 &lt;?php if ($_config['example_maker']): ?>
631         &lt;div class="input-form">
632                 &lt;div class="row">
633                         &lt;p>&lt;?php echo _AT('example_maker_text'); ?>&lt;/p>
634                 &lt;/div>
635         &lt;/div>
636 &lt;?php else: ?>
637         &lt;div class="input-form">
638                 &lt;div class="row">
639                         &lt;p>&lt;?php echo _AT('example_maker_missing_url');  ?>&lt;/p>
640                 &lt;/div>
641         &lt;/div>
642 &lt;?php endif; ?>
643
644 &lt;form action="&lt;?php  $_SERVER['PHP_SELF']; ?>" method="post">
645         &lt;div class="input-form">
646                 &lt;div class="row">
647                         &lt;p>&lt;label for="uri">&lt;?php echo _AT('example_maker_url'); ?>&lt;/label>&lt;/p>
648         
649                         &lt;input type="text" name="uri" value="&lt;?php echo $_config['example_maker']; ?>" id="uri" size="60" style="min-width: 65%;" />
650                 &lt;/div>
651                 &lt;div class="row buttons">
652                         &lt;input type="submit" name="submit" value="&lt;?php echo _AT('save'); ?>"  />
653                 &lt;/div>
654         &lt;/div>
655 &lt;/form>
656 </pre>
657
658 <a name="styles"></a>
659 <h2>Custom Style Sheets</h2>
660         <p>A custom style sheet can be linked into pages by setting <kbd>$_custom_css</kbd> to be the absolute path to the style sheet. This variable must be set on every page that requires that style sheet.</p>
661
662 <pre>
663 define('AT_INCLUDE_PATH', '../../include/');
664 require(AT_INCLUDE_PATH.'vitals.inc.php');
665 // using a custom style sheet:
666 $_custom_css = $_base_path . 'mods/example_maker/module.css';
667 </pre>
668
669 <a name="sidemenu"></a>
670 <h2>Side Menu Boxes</h2>
671         <p>Side menu boxes generally appear in a column at the side of a course (though this layout can be altered by a theme). A module may implement one or more side menu boxes.</p>
672
673         <p>Side menus are specified using the <kbd>$_module_stacks</kbd> array in <kbd>module.php</kbd>. <kbd>$_module_stacks</kbd> have the attributes <kbd>title_var</kbd> (or <kbd>title</kbd>) and <kbd>file</kbd>. The <kbd>title_var</kbd> value is the language key used for that box; the title will be generated by executing <kbd>_AT($title_var)</kbd>. If <kbd>title</kbd> is set instead, a hard-coded title will be used. The <kbd>file</kbd> attribute specifies the absolute path to the side menu's include file.</p>
674
675         <p>The key to the <kbd>$_module_stacks</kbd> should be the name of the module.</p>
676
677 <pre>
678 $_module_stacks['example_maker'] = array('title_var' => 'example_maker', 
679                                          'file' => dirname(__FILE__).'\side_menu.inc.php');
680 </pre>
681
682         <p>Creating a side menu box involves using the <kbd>$savant</kbd> template object and assigning the output of the box to the <kbd>dropdown_contents</kbd> variable.</p>
683
684 <pre>
685 &lt;?php global $savant;
686
687 $box_content = 'This is my side menu box';
688
689 $savant->assign('dropdown_contents', $box_content);
690
691 $savant->assign('title', _AT('example_maker'));
692 $savant->display('include/box.tmpl.php');
693 ?>
694 </pre>
695 <a name="sublinks"></a>
696 <h2>Course tool details using <kbd>sublinks.php</kbd></h2>
697
698 <p>When viewing the detailed course home page, it is possible to display with each module icon, the latest changes, current information for the module, or just a text description of the module. This information is contained in a module sublinks.php file. The following example sublinks.php is free form PHP that gets a list of the three current tests so they can be accessed directly from the course home page along with the link to the Tests & Survey Tool.</p>
699
700 <pre>
701
702 if (!defined('AT_INCLUDE_PATH')) { exit; }
703 global $_base_path, $include_all, $include_one;
704 global $savant;
705 global $db;
706
707 $tests_limit = 3;               //Maximum number of items to display on the course homepage
708
709 $sql = "SELECT test_id, title, UNIX_TIMESTAMP(start_date) AS sd, UNIX_TIMESTAMP(end_date) AS ed FROM ".TABLE_PREFIX."tests WHERE course_id=$_SESSION[course_id] ORDER BY end_date DESC LIMIT $tests_limit";
710 $result = mysql_query($sql, $db);
711
712 if (mysql_num_rows($result) > 0) {
713         while ($row = mysql_fetch_assoc($result)) {
714                 if ( ($row['sd'] <= time()) && ($row['ed'] >= time() ))         
715                         $tests_list[] = array('sub_url' => $_base_path.url_rewrite('tools/test_intro.php?tid=' . $row['test_id']) , 'sub_text' => $row['title']); 
716         }
717         return $tests_list;     
718 } else {
719         return 0;
720 }
721
722 </pre>
723 <a name="tools"></a>
724 <h2>Student Tools</h2>
725         <p>Student tools are pages linked from the home page or the main navigation of courses. A module can only implement one student tool. An instructor controls which student tools are available to a course using the <em>Student Tools</em> section found under <em>Manage</em>.</p>
726
727         <p>The tool main page must be specified using the <kbd>$_student_tool</kbd> variable in the <kbd>module.php</kbd> file. The value of that variable must be the relative path to the file from the ATutor base directory (not the module directory). Example: <kbd>$_student_tool = 'mods/example_maker/index.php';</kbd>.</p>
728
729         <p>For the tool to correctly appear its Navigation &amp; Hierarchy must be defined correctly. If the tool is to have an instructor management section then the <em>parent</em> must be specified as being <kbd>tools/index.php</kbd> and the module must have a non-zero privilege level.</p>
730
731 <a name="groups"></a>
732 <h2>Group Tools</h2>
733         <p>Group tools are student tools that can also be group specific. A module can only implement one group tool. An instructor controls which group tools are available to a each group during the group creation process. Only group tools that are made available to students via the home page or main navigation are available for selection.</p>
734
735         <p>The group tool main page must be specified using the <kbd>$_group_tool</kbd> variable in the <kbd>module.php</kbd> file. The value of that variable must be the relative path to the file from the ATutor base directory (not the module directory). Example: <kbd>$_group_tool = 'mods/example_maker/index.php';</kbd>.</p>
736
737 <a name="navigation"></a>
738 <h2>Navigation &amp; Hierarchy</h2>
739         <p><em>Every</em> page in ATutor must have an entry in the global <kbd>$_pages</kbd> array where the key to the array is the relative path to the file from ATutor's base directory. Module pages are specified using the <kbd>$_module_pages</kbd> array, which are then merged into the <kbd>$_pages</kbd> array when the <kbd>module.php</kbd> file is loaded. The array supports the following attributes:</p>
740
741         <dl>
742                 <dt><kbd>title_var</kbd></dt>
743                 <dd>The language variable to be used with <kbd>_AT()</kbd>.</dd>
744
745                 <dt><kbd>title</kbd></dt>
746                 <dd>The hard-coded version of the language title. If set, overrides the usage of <kbd>_AT(title_var)</kbd>. This version is not language independent.</dd>
747
748                 <dt><kbd>parent</kbd></dt>
749                 <dd>The relative ATutor path to the parent page. Omit for Student Tools.</dd>
750
751                 <dt><kbd>img</kbd></dt>
752                 <dd>The relative ATutor path to the icon to use. Only for Student Tools.</dd>
753
754                 <dt><kbd>children</kbd></dt>
755                 <dd>An array whose values are relative ATutor paths to sub pages.</dd>
756
757                 <dt><kbd>guide</kbd></dt>
758                 <dd>The the section of the handbook that the module page should link to. Not used for modules at this time.</dd>
759         </dl>
760
761         <p>For pages to appear in the instructor Manage section, their <kbd>parent</kbd> field must be set to <kbd>tools/index.php</kbd>.</p>
762
763 <pre>
764 $path = 'mods/example_maker/';
765
766 // the student tool:
767 $_module_pages[$path.'index.php']['title_var'] = 'example_maker';
768 $_module_pages[$path.'index.php']['img']       = $path.'icon.gif';
769 $_module_pages[$path.'index.php']['children']  = array($path.'sub.php', $path.'more.php');
770
771     $_module_pages[$path.'sub.php']['title_var'] = 'sub_page';
772     $_module_pages[$path.'sub.php']['parent']    = $path.'index.php';
773
774     $_module_pages[$path.'more.php']['title_var'] = 'more_page';
775     $_module_pages[$path.'more.php']['parent']    = $path.'index.php';
776
777 // the instructor page:
778 $_module_pages[$path . 'inst_index.php']['title_var'] = 'example_maker';
779 $_module_pages[$path . 'inst_index.php']['parent']    = 'tools/index.php';
780 </pre>
781 <a name="delete"></a>
782 <h2>Course Deletion</h2>
783         <p>When a course is being deleted, or when a back-up is being restored by overriding (i.e. deleting) existing content, a module has to ensure that the content for that course is also deleted. If the module maintains course data directories, then those directories have to either be emptied or deleted. If the module uses database tables for course content, then it has to delete the appropriate entries for that course.</p>
784
785         <p>The function used to delete the course content for that module must be stored in the <kbd>module_delete.php</kbd> file and named <kbd><em>module_name</em>_delete()</kbd>. The delete function takes a single argument which is the ID of the course to delete.</p>
786
787 <pre>
788 &lt;?php
789 function example_maker_delete($course) {
790     global $db;
791
792     // delete directory
793     $path = AT_CONTENT_DIR . 'example_maker/' . $course . '/';
794     clr_dir($path);
795
796     // delete from database
797     $sql = "DELETE 
798             FROM ".TABLE_PREFIX."example_content 
799             WHERE course_id=$course";
800     mysql_query($sql, $db);
801 }
802 ?>
803 </pre>
804 <a name="backup"></a>
805 <h2>Backing-Up and Restoring</h2>
806         <p>It is possible for a module to include its content when a course backup is being created or restored. Backups support database tables with foreign-key constraints as well as course specific directories.</p>
807 <a name="directories"></a>
808         <h3>Directories</h3>
809                 <p>A module can backup as many directories as it requires, all specified using the <kbd>$dirs</kbd> array variable.</p>
810
811                 <p>The example below uses the special <kbd>?</kbd> token as the place holder for the course ID. When the course is backed-up, the question mark will be replaced with the correct course ID. The key to the array is the unique name of the directory to be used inside the backup archive file. The same information to create the backup is also used to restore it, so no additional details are required.</p>
812 <pre>
813 $dirs = array();
814 $dirs['example_maker/'] = AT_CONTENT_DIR . 'example_maker/?/';
815 </pre>
816
817 <a name="database"></a>
818         <h3>Database Tables</h3>
819                 <p>There are two parts to backing-up and restoring a module's database tables. First, the <acronym title="Structured Query Language">SQL</acronym> queries must be specified using the <kbd>$sql</kbd> array variable and then the restore functions must convert the rows so that they can be inserted into the database tables.</p>
820
821                 <p>The example below uses the special <kbd>?</kbd> token as the place holder for the course ID. When the course is backed-up the question-mark will be replaced with the correct course ID. The key to the array is the unique name of the <acronym title="Comma Separated Values">CSV</acronym> file to save in the backup archive, without the extension. The <acronym title="Structured Query Language">SQL</acronym> query itself must only select the fields that will be backed up. If there are foreign key constraints to preserve then the key will have to be retrieved as well so that it can be used when restoring the tables.</p>
822
823 <pre>
824 $sql = array();
825 $sql['example']  = 'SELECT title FROM '.TABLE_PREFIX.'example WHERE course_id=?';
826 </pre>
827
828                 <p>For each key in the <kbd>$sql</kbd> array there must be a function with the same name, but suffixed with <kbd>_convert</kbd>. The <kbd><em>tbl_name</em>_convert()</kbd> function must return the newly transformed row with respect to the version of ATutor that was used to generate the <acronym title="Comma Separated Values">CSV</acronym> file. The function accepts the following arguments:</p>
829
830                 <dl>
831                         <dt><kbd>$row</kbd></dt>
832                         <dd>An array which represents a single row in the <acronym title="Comma Separated Values">CSV</acronym> file.</dd>
833
834                         <dt><kbd>$course_id</kbd></dt>
835                         <dd>The course ID which this content should be associated with.</dd>
836                         
837                         <dt><kbd>$table_id_map</kbd></dt>
838                         <dd>An associative array representing previously restored tables and their new keys. Used to preserve foreign key constraints.</dd>
839
840                         <dt><kbd>$version</kbd></dt>
841                         <dd>The version of ATutor that was used to generate this file.</dd>
842                 </dl>
843
844 <pre>
845 function example_convert($row, $course_id, $table_id_map, $version) {
846     $new_row = array();
847     $new_row[0]  = 0; // auto-increment field
848     $new_row[1]  = $course_id;
849     $new_row[2]  = $row[1]; // the title
850     if (version_compare($version, '1.5.2', '&lt;')) {
851         // this field did not exist prior to 1.5.2
852         $new_row[3] = '';
853     } else {
854         $new_row[3] = $row[2];
855     }
856
857     return $new_row;
858 }
859 </pre>
860 <a name="cron"></a>
861         <h2>Running Cron/Scheduling</h2>
862 <p>The <kbd>module_cron.php</kbd> file can be used to run module related functions at specified intervals. The following example doesn't do much of anything..., but you get the idea.  If cron has been enabled in ATutor, all the module_cron.php files from each module are run at the interval specified when the Cron is setup. See the Cron Setup sections in the Administrator Handbook, and in ATutor in the Administrator's System Preferences.</p>
863
864 <pre>
865 &lt;?php
866 /*******
867  * this function named [module_name]_cron is run by the global cron script at the module's specified
868  * interval.
869  */
870
871 function hello_world_cron() {
872         global $db;
873         debug('yay i am running!');
874 }
875
876 ?>
877 </pre>
878 </body>
879 </html>