354562c72a43252befb461f4079024b83d6645c9
[atutor.git] / mods / plog / sync_plog11.php
1 <?php
2 /*
3 This is a slightly modified version of the sync_plog file, adjusted to work with LifeType 1.1
4 It is not implemented in the atutor_plog-0.1 module. To implement it switch the require
5 lines at the top of the admin.plog.php and the instructor_index.php files.
6 */
7 if($_REQUEST['plog_sync'] == 1){
8         //update the plog_users table with ATutor members
9
10         $sql = "SELECT * from ".TABLE_PREFIX."members";
11         $result = mysql_query($sql,$db);
12
13         // But first check to see if the user already exists
14         $sqlu = "SELECT user, password FROM ".PLOG_PREFIX."users";
15         $resultu = mysql_query($sqlu,$db);
16         $i = '';
17
18         while($rowu = mysql_fetch_array($resultu)){
19                 $i++;
20                 $existing_users[$i]= $rowu[0];
21                 $existing_pwd[$i]= $rowu[1];
22         }
23
24         while ($row = mysql_fetch_array($result)){
25                 if(!in_array($row[1],$existing_users) || (in_array($row[1],$existing_users) && !in_array(md5($row[2]),$existing_pwd))){
26                         $sql2  = "REPLACE INTO ".PLOG_PREFIX."users SET
27                         id= '$row[0]',
28                         user = '$row[1]',
29                         password = '".md5($row[2])."',
30                         email = '$row[3]',
31                         full_name = '".$row[5].$row[6]."',
32                         about = '',
33                         properties = '',
34                         status = '1',
35                         resource_picture_id = '',
36                         site_admin = '0'";
37                                                 
38
39 //this was the old 1.0.2 query
40 //VALUES ('$row[0]','$row[1]','".md5($row[2])."','$row[3]','$row[5]  $row[6]','','a:0:{}','1','0')";
41
42                         if(!$result1 = mysql_query($sql2)){     
43                                 $msg->addError('PLOG_UPDATE_MEMBERS_FAILED');
44
45                         }else{  
46                                 $msg->addFeedback('PLOG_UPDATE_MEMBERS_SAVED');
47                                 $message = 1;
48                         }
49                 }
50         }
51
52
53 // The  synchronizing of admin accounts is a problem because they are not admin members
54 // and don't have a user ID. or a course they are associated to. The following code  was a first attempt
55 // at merging the admins into the plog_users table. 
56
57
58 /*
59         $sql = "SELECT * from ".TABLE_PREFIX."admins";
60         $result3 = mysql_query($sql);
61
62         $sql5="SELECT user FROM ".PLOG_PREFIX."users";
63         $result5 = mysql_query($sql5,$db);
64                 
65         $i='';
66         while($row5 = mysql_fetch_array($result5)){
67                         $i++;
68                         $existing_admins[$i] = $row5[0];
69                 }
70         //update the plog_users table with ATutor admins
71
72         while ($row = mysql_fetch_array($result3)){
73
74                 $admin_pref = 's:0:"";';
75                 if(!in_array($row[0], $existing_admins)){
76                         $sql3  = "REPLACE INTO ".PLOG_PREFIX."users VALUES ( ";
77                 
78                         if($existing_admins != ''){
79                                 $sql3 .= '  \''. $existing_admins[$row[0]].'\',';
80                         }else{
81                                 $sql3 .= "'',";
82                         }
83
84                         $sql3  .= "'$row[0]','".md5($row[1])."','$row[3]','$row[2]','','$admin_pref ','1','0')";
85
86                         if(!$result4 = mysql_query($sql3, $db)){
87                                 $msg->addError('PLOG_UPDATE_ADMINS_FAILED');
88                         }else{
89                                 $msg->addFeedback('PLOG_UPDATE_ADMINS_SAVED');
90                                 $message = 1;
91                         }
92                 }
93         }
94 */
95
96
97         // get a list of course titles and create values for the mangled_blog values in the plog_blogs table
98
99         //update the plog_blogs table with ATutor courses. Creates one blog per course and assigns the instructor as the blog owner
100         
101         $sql5 = "SELECT * FROM ".TABLE_PREFIX."courses";
102         $result5 = mysql_query($sql5,$db);
103
104         // But first check to see if the course already exists
105         $sqlb = "SELECT id FROM ".PLOG_PREFIX."blogs";
106         $resultb = mysql_query($sqlb,$db);
107         $i = '';
108         while($rowb = mysql_fetch_array($resultb)){
109                 $i++;
110                 $existing_blogs[$i]= $rowb[0];
111         }
112
113         // the 1.0.2 default blog setting
114         //$default_blog_settings = 'O:12:"blogsettings":3:{s:6:"_objId";N;s:3:"log";N;s:6:"_props";a:14:{s:6:"locale";s:5:"en_UK";s:14:"show_posts_max";s:2:"15";s:8:"template";s:7:"blueish";s:17:"show_more_enabled";b:1;s:16:"recent_posts_max";s:2:"10";s:17:"xmlrpc_ping_hosts";a:2:{i:0;s:27:"http://rpc.weblogs.com/RPC2";i:1;s:0:"";}s:16:"htmlarea_enabled";b:1;s:16:"comments_enabled";b:1;s:16:"categories_order";s:1:"1";s:14:"comments_order";s:1:"1";s:11:"time_offset";s:2:"-5";s:21:"link_categories_order";s:1:"1";s:29:"show_future_posts_in_calendar";b:0;s:27:"new_drafts_autosave_enabled";b:0;}}';
115
116         // the 1.1 default blog settings
117         $default_blog_settings = 'O:12:"blogsettings":1:{s:6:"_props";a:11:{s:6:"locale";s:5:"en_UK";s:14:"show_posts_max";s:2:"15";s:8:"template";s:7:"blueish";s:17:"show_more_enabled";s:1:"1";s:16:"recent_posts_max";s:2:"10";s:17:"xmlrpc_ping_hosts";a:2:{i:0;s:27:"http://rpc.weblogs.com/RPC2";i:1;s:0:"";}s:16:"htmlarea_enabled";s:1:"1";s:16:"comments_enabled";s:1:"1";s:16:"categories_order";i:0;s:14:"comments_order";s:1:"1";s:11:"time_offset";s:1:"0";}}';
118
119         while ($row = mysql_fetch_array($result5)){
120
121                 if(!in_array($row[0], $existing_blogs)){
122                         $course_title = addslashes($row[6]);
123                         $spec_chars = array("'");
124                         $temp_course_title = str_replace($spec_chars, "","$row[6]"); 
125                         $temp_array = explode(" ", $temp_course_title);
126                         $temp_mangle = strtolower($temp_array[0]."_".$temp_array[1]);
127                         $sql2  = "REPLACE INTO ".PLOG_PREFIX."blogs SET
128                                 id ='$row[0]',
129                                 blog = '$course_title',
130                                 owner_id = '$row[1]',
131                                 blog_category_id = '0',
132                                 about = '',
133                                 settings = '$default_blog_settings',
134                                 mangled_blog = '$temp_mangl',
135                                 status = '1',
136                                 show_in_summary = '1',
137                                 create_date = 'now()',
138                                 last_update_date = 'now()',
139                                 num_posts = '0',
140                                 num_comments = '0',
141                                 num_trackbacks = '0'";
142
143 //VALUES ('$row[0]','$course_title','$row[1]','','$default_blog_settings','$temp_mangle','1','1')";
144
145                         if(!$result1 = mysql_query($sql2)){
146                                 $msg->addError('PLOG_UPDATE_COURSE_FAILED');
147
148                         }else{
149                                 $msg->addFeedback('PLOG_UPDATE_COURSE_SAVED');
150                                         $message = 1;
151                         }
152                 }
153         }
154
155         //update plog_users_permissions with enrolled courses. Allows course members to write to the course blog
156
157         $sql6 = "SELECT * FROM ".TABLE_PREFIX."course_enrollment WHERE approved='y'";
158         $result6 = mysql_query($sql6,$db);
159
160         // first check if permission to write to course blogs already exists
161
162         $sqlk = "SELECT * FROM ".PLOG_PREFIX."users_permissions";
163         $resultk = mysql_query($sqlk,$db);
164         $i = '';
165
166         while($rowk = mysql_fetch_array($resultk)){
167                 $i++;
168                 $existing_permissions[$i]= $rowk[1].','.$rowk[2];
169         }
170
171         while ($row = mysql_fetch_array($result6)){
172                 $this_permission = $row[1].','.$row[0];
173                 if(!in_array($this_permission, $existing_permissions)) {
174                         $sqlx  = "REPLACE INTO ".PLOG_PREFIX."users_permissions VALUES ('','$row[1]','$row[0]','2')";
175                         if(!$resultx = mysql_query($sqlx,$db)){
176                                 $msg->addError('PLOG_UPDATE_PERMISSIONS_FAILED');
177
178                         }else{
179                                 $msg->addFeedback('PLOG_UPDATE_PERMISSIONS_SAVED');
180                                         $message = 1;
181                         }
182                 }
183         }
184
185         //create an initial category for each course
186         $sqlcat = "SELECT * from ".TABLE_PREFIX."courses";
187         $resultcat = mysql_query($sqlcat,$db);
188
189         // But first check to see if a default course blog categoy already exists
190         $sqlm = "SELECT blog_id FROM ".PLOG_PREFIX."articles_categories";
191         $resultm = mysql_query($sqlm,$db);
192         $i = '';
193
194         while($rowm = mysql_fetch_array($resultm)){
195                 $i++;
196                 $existing_cats[$i]= $rowm[0];
197         }
198
199
200         $default_cat_properties = 's:0:"";';
201         while($rowcat = mysql_fetch_array($resultcat)){
202                 if(!in_array($rowcat[0], $existing_cats)){
203                         $temp_title = str_replace("'","", $rowcat[6]);
204                         $temp_cat_array = explode(" ", $temp_title);
205                         $temp_cat_mangle = strtolower($temp_cat_array[0]."_".$temp_cat_array[1]);
206                         $course_title = addslashes($rowcat[6]);
207                         $sqlcat1  = "REPLACE INTO ".PLOG_PREFIX."articles_categories SET
208                                 id = '',
209                                 name = '$course_title',
210                                 url = '',
211                                 blog_id = '$rowcat[0]',
212                                 last_modification = now(),
213                                 in_main_page = '1',
214                                 parent_id = '0',
215                                 description = '$course_title',
216                                 properties = '$default_cat_properties',
217                                 mangled_name = '$temp_cat_mangle'";
218
219                         if(!$resultcat1 = mysql_query($sqlcat1,$db)){
220                                         $msg->addError('PLOG_UPDATE_CATS_FAILED');
221
222                         }else{
223                                         $msg->addFeedback('PLOG_UPDATE_CATS_SAVED');
224                                         $message = 1;
225
226                         }
227                 }
228
229         }
230         if( !$msg->containsFeedbacks() &&  !$msg->containsErrors()){
231                 $msg->addFeedback('PLOG_UPDATE_NOT_REQUIRED');
232         }
233
234 }
235
236 ?>