Dokeos PHP Cross Reference Learning Management Systems

Source: /main/inc/lib/system_announcements.lib.php - 429 lines - 15918 bytes - Summary - Text - Print

   1  <?php
   2  /* For licensing terms, see /dokeos_license.txt */
   3  define('VISIBLE_GUEST', 1);
   4  define('VISIBLE_STUDENT', 2);
   5  define('VISIBLE_TEACHER', 3);
   6  /**
   7  ==============================================================================
   8  *    This is the system announcements library for Dokeos.
   9  *
  10  *    @package dokeos.library
  11  ==============================================================================
  12  */
  13  class SystemAnnouncementManager
  14  {
  15      /**
  16       * Displays all announcements
  17       * @param int $visible VISIBLE_GUEST, VISIBLE_STUDENT or VISIBLE_TEACHER
  18       * @param int $id The identifier of the announcement to display
  19       */
  20  	function display_announcements($visible, $id = -1)
  21      {
  22          $user_selected_language = api_get_interface_language();
  23          $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  24          $sql = "SELECT *, DATE_FORMAT(date_start,'%d-%m-%Y') AS display_date FROM ".$db_table." WHERE (lang='$user_selected_language' OR lang IS NULL) AND ((NOW() BETWEEN date_start AND date_end) OR date_end='0000-00-00') ";
  25          switch ($visible)
  26          {
  27              case VISIBLE_GUEST :
  28                  $sql .= " AND visible_guest = 1 ";
  29                  break;
  30              case VISIBLE_STUDENT :
  31                  $sql .= " AND visible_student = 1 ";
  32                  break;
  33              case VISIBLE_TEACHER :
  34                  $sql .= " AND visible_teacher = 1 ";
  35                  break;
  36          }
  37          $sql .= " ORDER BY date_start DESC LIMIT 0,7";
  38          $announcements = Database::query($sql,__FILE__,__LINE__);
  39          if (Database::num_rows($announcements))
  40          {
  41              $query_string = ereg_replace('announcement=[1-9]+', '', $_SERVER['QUERY_STRING']);
  42              $query_string = ereg_replace('&$', '', $query_string);
  43              $url = api_get_self();
  44              echo '<div class="system_announcements">';
  45              echo '<h3>'.get_lang('SystemAnnouncements').'</h3>';
  46              echo '<table border="0">';
  47              while ($announcement = Database::fetch_object($announcements))
  48              {
  49  
  50                  if ($id != $announcement->id)
  51                  {
  52                      if (strlen($query_string) > 0)
  53                      {
  54                          $show_url = 'news_list.php#'.$announcement->id;
  55                          //$show_url = $url.'?'.$query_string.'&announcement='.$announcement->id;
  56                      }
  57                      else
  58                      {
  59                          $show_url = 'news_list.php#'.$announcement->id;
  60                          //$show_url = $url.'?announcement='.$announcement->id;
  61                      }
  62                      echo '<tr class="system_announcement">
  63                              <td width="80px" valign="top" class="system_announcement_title">'
  64                                  .$announcement->display_date.'
  65                              </td>
  66                              <td valign="top">
  67                                  <a name="ann'.$announcement->id.'" href="'.$show_url.'">'.$announcement->title.'</a>
  68                              </td>
  69                          </tr>';
  70                  }
  71                  else
  72                  {
  73                      echo '<div class="system_announcement">
  74                              <div class="system_announcement_title">'
  75                                  .$announcement->display_date.'
  76                                  <a name="ann'.$announcement->id.'" href="'.$url.'?'.$query_string.'#ann'.$announcement->id.'">'.$announcement->title.'</a>
  77                              </div>
  78                              <div class="system_announcement_content">'
  79                                  .$announcement->content.'
  80                              </div>';
  81                  }
  82  
  83              }
  84  
  85              /*echo '<tr><td height="15px"></td></tr>';*/
  86              echo '<tr><td colspan="2">';
  87              echo '<a href="news_list.php">'.get_lang("More").'</a>';
  88              echo '</td></tr>';
  89              echo '</table>';
  90              echo '</div>';
  91          }
  92          return;
  93      }
  94      
  95      /**
  96       * Count all announcements
  97       * @param int $visible VISIBLE_GUEST, VISIBLE_STUDENT or VISIBLE_TEACHER
  98       * @param int $id The identifier of the announcement to display
  99       */
 100  	function count_announcements($visible, $id = -1)
 101      {
 102          $user_selected_language = api_get_interface_language();
 103          $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
 104          $sql = "SELECT *, DATE_FORMAT(date_start,'%d-%m-%Y') AS display_date FROM ".$db_table." WHERE (lang='$user_selected_language' OR lang IS NULL) AND ((NOW() BETWEEN date_start AND date_end) OR date_end='0000-00-00') ";
 105          switch ($visible)
 106          {
 107              case VISIBLE_GUEST :
 108                  $sql .= " AND visible_guest = 1 ";
 109                  break;
 110              case VISIBLE_STUDENT :
 111                  $sql .= " AND visible_student = 1 ";
 112                  break;
 113              case VISIBLE_TEACHER :
 114                  $sql .= " AND visible_teacher = 1 ";
 115                  break;
 116          }
 117          $sql .= " ORDER BY date_start DESC LIMIT 0,7";
 118          $announcements = Database::query($sql,__FILE__,__LINE__);
 119          $numberofrows = Database::num_rows($announcements);
 120          return $numberofrows;
 121      }
 122  
 123  	function display_all_announcements($visible, $id = -1,$start = 0,$user_id='')
 124      {
 125          $user_selected_language = api_get_interface_language();
 126  
 127          $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
 128          $sql = "SELECT *, DATE_FORMAT(date_start,'%d-%m-%Y') AS display_date FROM ".$db_table."
 129                  WHERE (lang='$user_selected_language' OR lang IS NULL) AND ((NOW() BETWEEN date_start AND date_end)    OR date_end='0000-00-00')";
 130          switch ($visible)
 131          {
 132              case VISIBLE_GUEST :
 133                  $sql .= " AND visible_guest = 1 ";
 134                  break;
 135              case VISIBLE_STUDENT :
 136                  $sql .= " AND visible_student = 1 ";
 137                  break;
 138              case VISIBLE_TEACHER :
 139                  $sql .= " AND visible_teacher = 1 ";
 140                  break;
 141          }
 142  
 143          if(!isset($_GET['start']) || $_GET['start'] == 0) {
 144              $sql .= " ORDER BY date_start DESC LIMIT ".$start.",20";
 145          } else {
 146              $sql .= " ORDER BY date_start DESC LIMIT ".($start+1).",20";
 147          }
 148          $announcements = Database::query($sql,__FILE__,__LINE__);
 149  
 150          if (Database::num_rows($announcements)) {
 151              $query_string = ereg_replace('announcement=[1-9]+', '', $_SERVER['QUERY_STRING']);
 152              $query_string = ereg_replace('&$', '', $query_string);
 153              $url = api_get_self();
 154              echo '<div class="system_announcements">';
 155              echo '<h3>'.get_lang('SystemAnnouncements').'</h3>';
 156              echo '<table align="center">';
 157                  echo '<tr>';
 158                      echo '<td>';
 159                          SystemAnnouncementManager :: display_fleche($user_id);
 160                      echo '</td>';
 161                  echo '</tr>';
 162              echo '</table>';
 163              echo '<table align="center" border="0" width="900px">';
 164              while ($announcement = Database::fetch_object($announcements)) {
 165                      echo '<tr><td>';
 166                      echo '<a name="'.$announcement->id.'"></a>
 167                              <div class="system_announcement">
 168                              <div class="system_announcement_title">'
 169                                  .$announcement->display_date.' <strong>'.$announcement->title.'</strong>
 170                              </div>
 171                              <br />
 172                                <div class="system_announcement_content">'
 173                                        .$announcement->content.'
 174                              </div>
 175                            </div>
 176                              <br />
 177                            <hr noshade size="1">';
 178                      echo '</tr></td>';
 179              }
 180              echo '</table>';
 181              echo '<table align="center">';
 182                  echo '<tr>';
 183                      echo '<td>';
 184                          SystemAnnouncementManager :: display_fleche($user_id);
 185                      echo '</td>';
 186                  echo '</tr>';
 187              echo '</table>';
 188              echo '</div>';
 189          }
 190          return;
 191      }
 192  
 193  	function display_fleche($user_id)
 194      {
 195          $start = (int)$_GET['start'];
 196          $nb_announcement = SystemAnnouncementManager :: count_nb_announcement($start,$user_id);
 197          $next = ((int)$_GET['start']+19);
 198          $prev = ((int)$_GET['start']-19);
 199  
 200          if(!isset($_GET['start']) || $_GET['start'] == 0) {
 201  
 202              if($nb_announcement > 20) {
 203                  echo '<a href="news_list.php?start='.$next.'">'.get_lang('NextBis').' >> </a>';
 204              }
 205  
 206          } else {
 207              echo '<a href="news_list.php?start='.$prev.'"> << '.get_lang('Prev').'</a>';
 208  
 209              if($nb_announcement > 20) {
 210                  echo '<a href="news_list.php?start='.$next.'">'.get_lang('NextBis').' >> </a>';
 211              }
 212  
 213      }
 214  
 215      }
 216  
 217  	function count_nb_announcement($start = 0,$user_id = '')
 218      {
 219          $start = intval($start);
 220          $visibility = api_is_allowed_to_create_course() ? VISIBLE_TEACHER : VISIBLE_STUDENT;
 221          $user_selected_language = api_get_interface_language();
 222          $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
 223          $sql = 'SELECT id
 224                  FROM '.$db_table.'
 225                  WHERE (lang="'.$user_selected_language.'" OR lang IS NULL) ';
 226          if (isset($user_id)) {
 227              switch ($visibility)
 228              {
 229                  case VISIBLE_GUEST :
 230                      $sql .= " AND visible_guest = 1 ";
 231                      break;
 232                  case VISIBLE_STUDENT :
 233                      $sql .= " AND visible_student = 1 ";
 234                      break;
 235                  case VISIBLE_TEACHER :
 236                      $sql .= " AND visible_teacher = 1 ";
 237                      break;
 238              }
 239           }
 240          $sql .= 'LIMIT '.$start.',21';
 241          $announcements = Database::query($sql,__FILE__,__LINE__);
 242          $i = 0;
 243          while($rows = Database::fetch_array($announcements))
 244          {
 245              $i++;
 246          }
 247          return $i;
 248      }
 249  
 250      /**
 251       * Get all announcements
 252       * @return array An array with all available system announcements (as php
 253       * objects)
 254       */
 255  	function get_all_announcements()
 256      {
 257          $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
 258  
 259          $sql = "SELECT *, IF( NOW() BETWEEN date_start AND date_end, '1', '0') AS visible FROM ".$db_table." ORDER BY date_start ASC";
 260          $announcements = Database::query($sql,__FILE__,__LINE__);
 261          $all_announcements = array();
 262          while ($announcement = Database::fetch_object($announcements))
 263          {
 264              $all_announcements[] = $announcement;
 265          }
 266          return $all_announcements;
 267      }
 268      /**
 269       * Adds an announcement to the database
 270       * @param string $title Title of the announcement
 271       * @param string $content Content of the announcement
 272       * @param string $date_start Start date (YYYY-MM-DD HH:II: SS)
 273       * @param string $date_end End date (YYYY-MM-DD HH:II: SS)
 274       */
 275  	function add_announcement($title, $content, $date_start, $date_end, $visible_teacher = 0, $visible_student = 0, $visible_guest = 0, $lang = null, $send_mail=0)
 276      {
 277  
 278          $a_dateS = explode(' ',$date_start);
 279          $a_arraySD = explode('-',$a_dateS[0]);
 280          $a_arraySH = explode(':',$a_dateS[1]);
 281          $date_start = array_merge($a_arraySD,$a_arraySH);
 282  
 283          $a_dateE = explode(' ',$date_end);
 284          $a_arrayED = explode('-',$a_dateE[0]);
 285          $a_arrayEH = explode(':',$a_dateE[1]);
 286          $date_end = array_merge($a_arrayED,$a_arrayEH);
 287  
 288          $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
 289  
 290          if (!checkdate($date_start[1], $date_start[2], $date_start[0])) {
 291              Display :: display_normal_message(get_lang('InvalidStartDate'));
 292              return false;
 293          }
 294          if (($date_end[1] || $date_end[2] || $date_end[0]) && !checkdate($date_end[1], $date_end[2], $date_end[0])) {
 295              Display :: display_normal_message(get_lang('InvalidEndDate'));
 296              return false;
 297          }
 298          if( strlen(trim($title)) == 0) {
 299              Display::display_normal_message(get_lang('InvalidTitle'));
 300              return false;
 301          }
 302          $start = $date_start[0]."-".$date_start[1]."-".$date_start[2]." ".$date_start[3].":".$date_start[4].":".$date_start[5];
 303          $end = $date_end[0]."-".$date_end[1]."-".$date_end[2]." ".$date_end[3].":".$date_end[4].":".$date_start[5];
 304          $title = Database::escape_string($title);
 305          $content = Database::escape_string($content);
 306          $lang = is_null($lang) ? 'NULL' : "'".Database::escape_string($lang)."'";
 307          $sql = "INSERT INTO ".$db_table." (title,content,date_start,date_end,visible_teacher,visible_student,visible_guest, lang)
 308                                                  VALUES ('".$title."','".$content."','".$start."','".$end."','".$visible_teacher."','".$visible_student."','".$visible_guest."',".$lang.")";
 309          if ($send_mail==1) {
 310              SystemAnnouncementManager::send_system_announcement_by_email($title, $content,$visible_teacher, $visible_student);
 311          }
 312          return Database::query($sql,__FILE__,__LINE__);
 313      }
 314      /**
 315       * Updates an announcement to the database
 316       * @param integer $id      : id of the announcement
 317       * @param string  $title   : title of the announcement
 318       * @param string  $content : content of the announcement
 319       * @param array $date_start: start date of announcement (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute)
 320       * @param array $date_end : end date of announcement (0 => day ; 1 => month ; 2 => year ; 3 => hour ; 4 => minute)
 321       */
 322  	function update_announcement($id, $title, $content, $date_start, $date_end, $visible_teacher = 0, $visible_student = 0, $visible_guest = 0,$lang=null, $send_mail=0)
 323      {
 324  
 325          $a_dateS = explode(' ',$date_start);
 326          $a_arraySD = explode('-',$a_dateS[0]);
 327          $a_arraySH = explode(':',$a_dateS[1]);
 328          $date_start = array_merge($a_arraySD,$a_arraySH);
 329  
 330          $a_dateE = explode(' ',$date_end);
 331          $a_arrayED = explode('-',$a_dateE[0]);
 332          $a_arrayEH = explode(':',$a_dateE[1]);
 333          $date_end = array_merge($a_arrayED,$a_arrayEH);
 334          $lang = is_null($lang) ? 'NULL' : "'".Database::escape_string($lang)."'";
 335          $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
 336          if (!checkdate($date_start[1], $date_start[2], $date_start[0])) {
 337              Display :: display_normal_message(get_lang('InvalidStartDate'));
 338              return false;
 339          }
 340          if (($date_end[1] || $date_end[2] || $date_end[0]) && !checkdate($date_end[1], $date_end[2], $date_end[0])) {
 341              Display :: display_normal_message(get_lang('InvalidEndDate'));
 342              return false;
 343          }
 344          if( strlen(trim($title)) == 0) {
 345              Display::display_normal_message(get_lang('InvalidTitle'));
 346              return false;
 347          }
 348          $start = $date_start[0]."-".$date_start[1]."-".$date_start[2]." ".$date_start[3].":".$date_start[4].":".$date_start[5];
 349          $end = $date_end[0]."-".$date_end[1]."-".$date_end[2]." ".$date_end[3].":".$date_end[4].":".$date_start[5];
 350          $title = Database::escape_string($title);
 351          $content = Database::escape_string($content);
 352          $id = intval($id);
 353          $sql = "UPDATE ".$db_table." SET lang=$lang,title='".$title."',content='".$content."',date_start='".$start."',date_end='".$end."', ";
 354          $sql .= " visible_teacher = '".$visible_teacher."', visible_student = '".$visible_student."', visible_guest = '".$visible_guest."' WHERE id='".$id."'";
 355  
 356          if ($send_mail==1) {
 357              SystemAnnouncementManager::send_system_announcement_by_email($title, $content,$visible_teacher, $visible_student);
 358          }
 359          return Database::query($sql,__FILE__,__LINE__);
 360      }
 361      /**
 362       * Deletes an announcement
 363       * @param integer $id The identifier of the announcement that should be
 364       * deleted
 365       */
 366  	function delete_announcement($id)
 367      {
 368          $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
 369          $id = intval($id);
 370          $sql = "DELETE FROM ".$db_table." WHERE id='".$id."'";
 371          return Database::query($sql,__FILE__,__LINE__);
 372      }
 373      /**
 374       * Gets an announcement
 375       * @param integer $id The identifier of the announcement that should be
 376       * deleted
 377       */
 378  	function get_announcement($id)
 379      {
 380          $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
 381          $id = intval($id);
 382          $sql = "SELECT * FROM ".$db_table." WHERE id='".$id."'";
 383          $announcement = Database::fetch_object(Database::query($sql,__FILE__,__LINE__));
 384          return $announcement;
 385      }
 386      /**
 387       * Change the visibility of an announcement
 388       * @param integer $announcement_id
 389       * @param integer $user For who should the visibility be changed (possible
 390       * values are VISIBLE_TEACHER, VISIBLE_STUDENT, VISIBLE_GUEST)
 391       */
 392  	function set_visibility($announcement_id, $user, $visible)
 393      {
 394          $db_table = Database :: get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
 395          $announcement_id = intval($announcement_id);
 396          $field = ($user == VISIBLE_TEACHER ? 'visible_teacher' : ($user == VISIBLE_STUDENT ? 'visible_student' : 'visible_guest'));
 397          $sql = "UPDATE ".$db_table." SET ".$field." = '".$visible."' WHERE id='".$announcement_id."'";
 398          return Database::query($sql,__FILE__,__LINE__);
 399      }
 400  
 401  	function send_system_announcement_by_email($title,$content,$teacher, $student)
 402      {
 403          global $_user;
 404          global $_setting;
 405          global $charset;
 406          $user_table = Database :: get_main_table(TABLE_MAIN_USER);
 407          if ($teacher<>0 AND $student == '0') {
 408              $sql = "SELECT * FROM $user_table WHERE email<>'' AND status = '1'";
 409          }
 410          if ($teacher == '0' AND $student <> '0') {
 411              $sql = "SELECT * FROM $user_table WHERE email<>'' AND status = '5'";
 412          }
 413          if ($teacher<>'0' AND $student <> '0') {
 414              $sql = "SELECT * FROM $user_table WHERE email<>''";
 415          }
 416          if ($teacher == '0' AND $student == '0') {
 417              return true;
 418          }
 419  
 420          $result = Database::query($sql,__FILE__,__LINE__);
 421          while($row = Database::fetch_array($result,'ASSOC'))
 422          {
 423              $content = str_replace('\r','',$content);
 424              $content = str_replace('\n','',$content);
 425              api_mail_html(api_get_person_name($row['firstname'], $row['lastname'], null, PERSON_NAME_EMAIL_ADDRESS), $row['email'], api_html_entity_decode($title, ENT_QUOTES, $charset), api_html_entity_decode($content, ENT_QUOTES, $charset), api_get_person_name($_user['firstName'], $_user['lastName'], null, PERSON_NAME_EMAIL_ADDRESS), api_get_setting('emailAdministrator'), api_get_setting('emailAdministrator'));
 426          }
 427      }
 428  }
 429  ?>

title

Description

title

Description

title

Description

title

title

Body