Fix course ajax to be compatible with language packs.
[moodle.git] / lib / ajax / ajaxlib.php
CommitLineData
f72f94a2 1<?php
b2330db6 2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18
19/**
0bb38e8c 20 * Library functions to facilitate the use of ajax JavaScript in Moodle.
b2330db6 21 *
22 * @package moodlecore
23 * @copyright 2009 Tim Hunt
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
b2330db6 27/**
0bb38e8c
PS
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
31 * values.
b2330db6 32 *
0bb38e8c
PS
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.
37 * @return void
b2330db6 38 */
0bb38e8c
PS
39function user_preference_allow_ajax_update($name, $paramtype) {
40 global $USER, $PAGE;
2b8c3f8c 41
0bb38e8c
PS
42 // Make sure that the required JavaScript libraries are loaded.
43 $PAGE->requires->yui2_lib('connection');
2b8c3f8c 44
0bb38e8c
PS
45 // Record in the session that this user_preference is allowed to updated remotely.
46 $USER->ajax_updatable_user_prefs[$name] = $paramtype;
b2330db6 47}
48
88c5092a 49/**
50 * Returns whether ajax is enabled/allowed or not.
0bb38e8c
PS
51 * @param array $browsers optional list of alowed browsers, empty means use default list
52 * @return bool
88c5092a 53 */
0bb38e8c 54function ajaxenabled(array $browsers = null) {
88c5092a 55 global $CFG, $USER;
483f3067 56
c2a9fc91 57 if (!empty($browsers)) {
58 $valid = false;
59 foreach ($browsers as $brand => $version) {
60 if (check_browser_version($brand, $version)) {
61 $valid = true;
483f3067 62 }
c2a9fc91 63 }
483f3067 64
c2a9fc91 65 if (!$valid) {
66 return false;
67 }
68 }
483f3067 69
d499142e 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);
06671035 74 $ch = check_browser_version('Chrome', 6);
d499142e 75
06671035 76 if (!$ie && !$ff && !$op && !$sa && !$ch) {
d499142e 77 /** @see http://en.wikipedia.org/wiki/User_agent */
483f3067 78 // Gecko build 20051107 is what is in Firefox 1.5.
88c5092a 79 // We still have issues with AJAX in other browsers.
80 return false;
81 }
82
2f11bfc0 83 if (!empty($CFG->enableajax) && (!empty($USER->ajax) || !isloggedin())) {
88c5092a 84 return true;
85 } else {
86 return false;
87 }
88}
9bb74178 89
35b974da 90
0bb38e8c
PS
91
92// ==============================================================================
93// TODO: replace this with something more up-to-date with our coding standards
94
35b974da 95/**
2469f7ea 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.
35b974da 98 */
4fc45e1d 99class jsportal {
9bb74178 100
0a0bb380 101 var $currentblocksection = null;
9bb74178 102 var $blocks = array();
0a0bb380 103
9bb74178 104
35b974da 105 /**
106 * Takes id of block and adds it
107 */
2469f7ea 108 function block_add($id, $hidden=false){
0a0bb380 109 $hidden_binary = 0;
9bb74178 110
111 if ($hidden) {
112 $hidden_binary = 1;
113 }
35b974da 114 $this->blocks[count($this->blocks)] = array($this->currentblocksection, $id, $hidden_binary);
0a0bb380 115 }
9bb74178 116
117
2469f7ea 118 /**
119 * Prints the JavaScript code needed to set up AJAX for the course.
120 */
121 function print_javascript($courseid, $return=false) {
e8b32e2b 122 global $CFG, $USER, $OUTPUT, $COURSE, $DB;
9bb74178 123
d4df8fdc 124 $blocksoutput = $output = '';
35b974da 125 for ($i=0; $i<count($this->blocks); $i++) {
2469f7ea 126 $blocksoutput .= "['".$this->blocks[$i][0]."',
127 '".$this->blocks[$i][1]."',
128 '".$this->blocks[$i][2]."']";
129
130 if ($i != (count($this->blocks) - 1)) {
35b974da 131 $blocksoutput .= ',';
9bb74178 132 }
133 }
32f0b38a 134 $output .= "<script type=\"text/javascript\">\n";
72d28452 135 $output .= " main.portal.id = ".$courseid.";\n";
2469f7ea 136 $output .= " main.portal.blocks = new Array(".$blocksoutput.");\n";
ddc66060 137 $output .= " main.portal.strings['courseformat']='".$COURSE->format."';\n";
2469f7ea 138 $output .= " main.portal.strings['wwwroot']='".$CFG->wwwroot."';\n";
81284044
RW
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";
b1b4c2d7 141 $output .= " main.portal.numsections = ".$COURSE->numsections.";\n";
e8b32e2b 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
81284044
RW
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";
d4a1fcaf 159 $output .= " main.portal.strings['sesskey']='".sesskey()."';\n";
b31b2d84 160 foreach (array_keys(get_plugin_list('mod')) as $modtype) {
81284044 161 $output .= " main.portal.strings['modtype_".$modtype."']='".addslashes_js(get_string('pluginname', 'mod_'.$modtype))."';\n";
b31b2d84 162 }
5233807c 163 $output .= " main.portal.icons['spacerimg']='".$OUTPUT->pix_url('spacer')."';\n";
b5d0cafc
PS
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";
ddedf979 173 if (right_to_left()) {
b5d0cafc
PS
174 $output .= " main.portal.icons['backwards']='".$OUTPUT->pix_url('t/right')."';\n";
175 $output .= " main.portal.icons['forwards']='".$OUTPUT->pix_url('t/left')."';\n";
ddedf979 176 } else {
b5d0cafc
PS
177 $output .= " main.portal.icons['backwards']='".$OUTPUT->pix_url('t/left')."';\n";
178 $output .= " main.portal.icons['forwards']='".$OUTPUT->pix_url('t/right')."';\n";
ddedf979 179 }
180
2469f7ea 181 $output .= " onloadobj.load();\n";
4fc45e1d 182 $output .= " main.process_blocks();\n";
35b974da 183 $output .= "</script>";
2469f7ea 184 if ($return) {
185 return $output;
186 } else {
187 echo $output;
188 }
0a0bb380 189 }
9bb74178 190
2469f7ea 191}