3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * Library functions to facilitate the use of ajax JavaScript in Moodle.
23 * @copyright 2009 Tim Hunt
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 * You need to call this function if you wish to use the set_user_preference
29 * method in javascript_static.php, to white-list the preference you want to update
30 * from JavaScript, and to specify the type of cleaning you expect to be done on
33 * @param string $name the name of the user_perference we should allow to be
34 * updated by remote calls.
35 * @param integer $paramtype one of the PARAM_{TYPE} constants, user to clean
36 * submitted values before set_user_preference is called.
39 function user_preference_allow_ajax_update($name, $paramtype) {
42 // Make sure that the required JavaScript libraries are loaded.
43 $PAGE->requires->yui2_lib('connection');
45 // Record in the session that this user_preference is allowed to updated remotely.
46 $USER->ajax_updatable_user_prefs[$name] = $paramtype;
50 * Returns whether ajax is enabled/allowed or not.
51 * @param array $browsers optional list of alowed browsers, empty means use default list
54 function ajaxenabled(array $browsers = null) {
57 if (!empty($browsers)) {
59 foreach ($browsers as $brand => $version) {
60 if (check_browser_version($brand, $version)) {
70 $ie = check_browser_version('MSIE', 6.0);
71 $ff = check_browser_version('Gecko', 20051106);
72 $op = check_browser_version('Opera', 9.0);
73 $sa = check_browser_version('Safari', 412);
74 $ch = check_browser_version('Chrome', 6);
76 if (!$ie && !$ff && !$op && !$sa && !$ch) {
77 /** @see http://en.wikipedia.org/wiki/User_agent */
78 // Gecko build 20051107 is what is in Firefox 1.5.
79 // We still have issues with AJAX in other browsers.
83 if (!empty($CFG->enableajax) && (!empty($USER->ajax) || !isloggedin())) {
92 // ==============================================================================
93 // TODO: replace this with something more up-to-date with our coding standards
96 * Used to create view of document to be passed to JavaScript on pageload.
97 * We use this class to pass data from PHP to JavaScript.
101 var $currentblocksection = null;
102 var $blocks = array();
106 * Takes id of block and adds it
108 function block_add($id, $hidden=false){
114 $this->blocks[count($this->blocks)] = array($this->currentblocksection, $id, $hidden_binary);
119 * Prints the JavaScript code needed to set up AJAX for the course.
121 function print_javascript($courseid, $return=false) {
122 global $CFG, $USER, $OUTPUT, $COURSE, $DB;
124 $blocksoutput = $output = '';
125 for ($i=0; $i<count($this->blocks); $i++) {
126 $blocksoutput .= "['".$this->blocks[$i][0]."',
127 '".$this->blocks[$i][1]."',
128 '".$this->blocks[$i][2]."']";
130 if ($i != (count($this->blocks) - 1)) {
131 $blocksoutput .= ',';
134 $output .= "<script type=\"text/javascript\">\n";
135 $output .= " main.portal.id = ".$courseid.";\n";
136 $output .= " main.portal.blocks = new Array(".$blocksoutput.");\n";
137 $output .= " main.portal.strings['courseformat']='".$COURSE->format."';\n";
138 $output .= " main.portal.strings['wwwroot']='".$CFG->wwwroot."';\n";
139 $output .= " main.portal.strings['marker']='".addslashes_js(get_string('markthistopic', '', '_var_'))."';\n";
140 $output .= " main.portal.strings['marked']='".addslashes_js(get_string('markedthistopic', '', '_var_'))."';\n";
141 $output .= " main.portal.numsections = ".$COURSE->numsections.";\n";
142 $output .= " main.portal.lastsection = ".$DB->get_field_sql("SELECT MAX(section) FROM {course_sections} WHERE course = ?", array($courseid)).";\n"; // needed for orphaned activities in unavailable sections
143 $output .= " main.portal.strings['hide']='".addslashes_js(get_string('hide'))."';\n";
144 $output .= " main.portal.strings['hidesection']='".addslashes_js(get_string('hidesection', '', '_var_'))."';\n";
145 $output .= " main.portal.strings['show']='".addslashes_js(get_string('show'))."';\n";
146 $output .= " main.portal.strings['delete']='".addslashes_js(get_string('delete'))."';\n";
147 $output .= " main.portal.strings['move']='".addslashes_js(get_string('move'))."';\n";
148 $output .= " main.portal.strings['movesection']='".addslashes_js(get_string('movesection', '', '_var_'))."';\n";
149 $output .= " main.portal.strings['moveleft']='".addslashes_js(get_string('moveleft'))."';\n";
150 $output .= " main.portal.strings['moveright']='".addslashes_js(get_string('moveright'))."';\n";
151 $output .= " main.portal.strings['update']='".addslashes_js(get_string('update'))."';\n";
152 $output .= " main.portal.strings['groupsnone']='".addslashes_js(get_string('groupsnone'))."';\n";
153 $output .= " main.portal.strings['groupsseparate']='".addslashes_js(get_string('groupsseparate'))."';\n";
154 $output .= " main.portal.strings['groupsvisible']='".addslashes_js(get_string('groupsvisible'))."';\n";
155 $output .= " main.portal.strings['clicktochange']='".addslashes_js(get_string('clicktochange'))."';\n";
156 $output .= " main.portal.strings['deletecheck']='".addslashes_js(get_string('deletecheckfull','','_var_'))."';\n";
157 $output .= " main.portal.strings['resource']='".addslashes_js(get_string('resource'))."';\n";
158 $output .= " main.portal.strings['activity']='".addslashes_js(get_string('activity'))."';\n";
159 $output .= " main.portal.strings['sesskey']='".sesskey()."';\n";
160 foreach (array_keys(get_plugin_list('mod')) as $modtype) {
161 $output .= " main.portal.strings['modtype_".$modtype."']='".addslashes_js(get_string('pluginname', 'mod_'.$modtype))."';\n";
163 $output .= " main.portal.icons['spacerimg']='".$OUTPUT->pix_url('spacer')."';\n";
164 $output .= " main.portal.icons['marker']='".$OUTPUT->pix_url('i/marker')."';\n";
165 $output .= " main.portal.icons['ihide']='".$OUTPUT->pix_url('i/hide')."';\n";
166 $output .= " main.portal.icons['move_2d']='".$OUTPUT->pix_url('i/move_2d')."';\n";
167 $output .= " main.portal.icons['show']='".$OUTPUT->pix_url('t/show')."';\n";
168 $output .= " main.portal.icons['hide']='".$OUTPUT->pix_url('t/hide')."';\n";
169 $output .= " main.portal.icons['delete']='".$OUTPUT->pix_url('t/delete')."';\n";
170 $output .= " main.portal.icons['groupn']='".$OUTPUT->pix_url('t/groupn')."';\n";
171 $output .= " main.portal.icons['groups']='".$OUTPUT->pix_url('t/groups')."';\n";
172 $output .= " main.portal.icons['groupv']='".$OUTPUT->pix_url('t/groupv')."';\n";
173 if (right_to_left()) {
174 $output .= " main.portal.icons['backwards']='".$OUTPUT->pix_url('t/right')."';\n";
175 $output .= " main.portal.icons['forwards']='".$OUTPUT->pix_url('t/left')."';\n";
177 $output .= " main.portal.icons['backwards']='".$OUTPUT->pix_url('t/left')."';\n";
178 $output .= " main.portal.icons['forwards']='".$OUTPUT->pix_url('t/right')."';\n";
181 $output .= " onloadobj.load();\n";
182 $output .= " main.process_blocks();\n";
183 $output .= "</script>";