changed git call from https to git readonly
[atutor.git] / mods / plog / sync_plog.php
1 <?php
2
3 if($_REQUEST['plog_sync'] == 1){
4         //update the plog_users table with ATutor members
5
6         $sql = "SELECT * from ".TABLE_PREFIX."members";
7         $result = mysql_query($sql,$db);
8
9         // But first check to see if the user already exists
10         $sqlu = "SELECT user, password FROM ".PLOG_PREFIX."users";
11         $resultu = mysql_query($sqlu,$db);
12         $i = '';
13
14         while($rowu = mysql_fetch_array($resultu)){
15                 $i++;
16                 $existing_users[$i]= $rowu[0];
17                 $existing_pwd[$i]=$rowu[1];
18         }
19
20         
21
22         while ($row = mysql_fetch_array($result)){
23                 if(!in_array($row[1],$existing_users) || (in_array($row[1],$existing_users) && !in_array(md5($row[2]),$existing_pwd))){
24                         $sql2  = "REPLACE INTO ".PLOG_PREFIX."users VALUES ('$row[0]','$row[1]','".md5($row[2])."','$row[3]','$row[5]  $row[6]','','a:0:{}','1','0')";
25                         if(!$result5 = mysql_query($sql2)){     
26                                 $msg->addError('PLOG_UPDATE_MEMBERS_FAILED');
27
28                         }else{  
29                                 $msg->addFeedback('PLOG_UPDATE_MEMBERS_SAVED');
30                                 $message = 1;
31                         }
32                 }
33 //debug(md5($row[2]));
34         }
35
36
37
38 //exit;
39 // The  synchronizing of admin accounts is a problem because they are not admin members
40 // and don't have a user ID. or a course they are associated to. The following code  was a first attempt
41 // at merging the admins into the plog_users table. 
42
43
44 /*
45         $sql = "SELECT * from ".TABLE_PREFIX."admins";
46         $result3 = mysql_query($sql);
47
48         $sql5="SELECT user FROM ".PLOG_PREFIX."users";
49         $result5 = mysql_query($sql5,$db);
50                 
51         $i='';
52         while($row5 = mysql_fetch_array($result5)){
53                         $i++;
54                         $existing_admins[$i] = $row5[0];
55                 }
56         //update the plog_users table with ATutor admins
57
58         while ($row = mysql_fetch_array($result3)){
59
60                 $admin_pref = 's:0:"";';
61                 if(!in_array($row[0], $existing_admins)){
62                         $sql3  = "REPLACE INTO ".PLOG_PREFIX."users VALUES ( ";
63                 
64                         if($existing_admins != ''){
65                                 $sql3 .= '  \''. $existing_admins[$row[0]].'\',';
66                         }else{
67                                 $sql3 .= "'',";
68                         }
69
70                         $sql3  .= "'$row[0]','".md5($row[1])."','$row[3]','$row[2]','','$admin_pref ','1','0')";
71
72                         if(!$result4 = mysql_query($sql3, $db)){
73                                 $msg->addError('PLOG_UPDATE_ADMINS_FAILED');
74                         }else{
75                                 $msg->addFeedback('PLOG_UPDATE_ADMINS_SAVED');
76                                 $message = 1;
77                         }
78                 }
79         }
80 */
81
82
83         // get a list of course titles and create values for the mangled_blog values in the plog_blogs table
84
85         //update the plog_blogs table with ATutor courses. Creates one blog per course and assigns the instructor as the blog owner
86         
87         $sql5 = "SELECT * FROM ".TABLE_PREFIX."courses";
88         $result5 = mysql_query($sql5,$db);
89
90         // But first check to see if the course already exists
91         $sqlb = "SELECT id FROM ".PLOG_PREFIX."blogs";
92         $resultb = mysql_query($sqlb,$db);
93         $i = '';
94         while($rowb = mysql_fetch_array($resultb)){
95                 $i++;
96                 $existing_blogs[$i]= $rowb[0];
97         }
98
99         $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;}}';
100         
101         while ($row = mysql_fetch_array($result5)){
102                 //echo $row[0];
103
104                 if(!in_array($row[0], $existing_blogs)){
105                         $course_title = addslashes($row[6]);
106                         $spec_chars = array("'");
107                         $temp_course_title = str_replace($spec_chars, "","$row[6]"); 
108                         $temp_array = explode(" ", $temp_course_title);
109                         $temp_mangle = strtolower($temp_array[0]."_".$temp_array[1]);
110                         $sql2  = "REPLACE INTO ".PLOG_PREFIX."blogs VALUES ('$row[0]','$course_title','$row[1]','','$default_blog_settings','$temp_mangle','1','1')";
111
112                         if(!$result1 = mysql_query($sql2)){
113                                 $msg->addError('PLOG_UPDATE_COURSE_FAILED');
114
115                         }else{
116                                 $msg->addFeedback('PLOG_UPDATE_COURSE_SAVED');
117                                         $message = 1;
118                         }
119                 }
120         }
121
122         //update plog_users_permissions with enrolled courses. Allows course members to write to the course blog
123
124         $sql6 = "SELECT * FROM ".TABLE_PREFIX."course_enrollment WHERE approved='y'";
125         $result6 = mysql_query($sql6,$db);
126
127         // first check if permission to write to course blogs already exists
128
129         $sqlk = "SELECT * FROM ".PLOG_PREFIX."users_permissions";
130         $resultk = mysql_query($sqlk,$db);
131         $i = '';
132         $existing_permissions = array();
133         while($rowk = mysql_fetch_array($resultk)){
134                 $i++;
135                 $existing_permissions[$i]= $rowk[1].','.$rowk[2];
136         }
137         while ($row = mysql_fetch_array($result6)){
138                 $this_permission = $row[1].','.$row[0];
139                 if(!in_array($this_permission, $existing_permissions)) {
140                         $sqlx  = "REPLACE INTO ".PLOG_PREFIX."users_permissions VALUES ('','$row[1]','$row[0]','2')";
141                         if(!$resultx = mysql_query($sqlx,$db)){
142                                 $msg->addError('PLOG_UPDATE_PERMISSIONS_FAILED');
143
144                         }else{
145                                 $msg->addFeedback('PLOG_UPDATE_PERMISSIONS_SAVED');
146                                         $message = 1;
147                         }
148                 }
149         }
150
151         //create an initial category for each course
152         $sqlcat = "SELECT * from ".TABLE_PREFIX."courses";
153         $resultcat = mysql_query($sqlcat,$db);
154
155         // But first check to see if a default course blog categoy already exists
156         $sqlm = "SELECT blog_id FROM ".PLOG_PREFIX."articles_categories";
157         $resultm = mysql_query($sqlm,$db);
158         $i = '';
159
160         while($rowm = mysql_fetch_array($resultm)){
161                 $i++;
162                 $existing_cats[$i]= $rowm[0];
163         }
164
165
166         $default_cat_properties = 's:0:"";';
167         while($rowcat = mysql_fetch_array($resultcat)){
168                 if(!in_array($rowcat[0], $existing_cats)){
169                         $temp_title = str_replace("'","", $rowcat[6]);
170                         $temp_cat_array = explode(" ", $temp_title);
171                         $temp_cat_mangle = strtolower($temp_cat_array[0]."_".$temp_cat_array[1]);
172                         $course_title = addslashes($rowcat[6]);
173                         $sqlcat1  = "REPLACE INTO ".PLOG_PREFIX."articles_categories SET
174                                 id = '',
175                                 name = '$course_title',
176                                 url = '',
177                                 blog_id = '$rowcat[0]',
178                                 last_modification = now(),
179                                 in_main_page = '1',
180                                 parent_id = '0',
181                                 description = '$course_title',
182                                 properties = '$default_cat_properties',
183                                 mangled_name = '$temp_cat_mangle'";
184
185                         if(!$resultcat1 = mysql_query($sqlcat1,$db)){
186                                         $msg->addError('PLOG_UPDATE_CATS_FAILED');
187
188                         }else{
189                                         $msg->addFeedback('PLOG_UPDATE_CATS_SAVED');
190                                         $message = 1;
191
192                         }
193                 }
194
195         }
196         if( !$msg->containsFeedbacks() &&  !$msg->containsErrors()){
197                 $msg->addFeedback('PLOG_UPDATE_NOT_REQUIRED');
198         }
199
200 }
201
202 ?>