Moving to GitHub.
[platal.git] / include / reminder.inc.php
index 835fab0..aabd989 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2009 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -22,7 +22,7 @@
 // Base class for a reminder; it offers the factory for creating valid reminders
 // tailored for a given user, as well as base methods for reminder impls.
 // Sub-classes should define at least the abstract methods, and the static
-// IsCandidate method (prototype: (User &$user)).
+// IsCandidate method (prototype: (User $user)).
 //
 // Usage:
 //   // Instantiates and returns a valid Reminder object for the user.
@@ -47,7 +47,7 @@ abstract class Reminder
 
     // Constructs the Reminder object from a mandatory User instance, a list of
     // key-value pairs from the `reminder_type` and `reminder` tables.
-    function __construct(User &$user, array $type)
+    function __construct(User $user, array $type)
     {
         $this->user = &$user;
 
@@ -69,27 +69,30 @@ abstract class Reminder
     // Updates (or creates) the reminder line for the pair (|user|, |reminder_id|)
     // using the |status| as status, and the |next_ask| as the delay between now
     // and the next ask (if any).
-    private function UpdateStatus($status, $next_ask)
+    private static function UpdateStatus($uid, $type_id, $status, $next_ask)
     {
-        XDB::execute('REPLACE INTO  reminder
-                               SET  uid = {?}, type_id = {?}, status = {?},
-                                    remind_last = NOW(), remind_next = FROM_UNIXTIME({?})',
-                     $this->user->id(), $this->type_id, $status,
+        XDB::execute('INSERT INTO  reminder (uid, type_id, status, remind_last, remind_next)
+                           VALUES  ({?}, {?}, {?}, NOW(), FROM_UNIXTIME({?}))
+          ON DUPLICATE KEY UPDATE  status = VALUES(status), remind_last = VALUES(remind_last), remind_next = VALUES(remind_next)',
+                     $uid, $type_id, $status,
                      ($next_ask > 0 ? time() + $next_ask * 24 * 60 * 60 : null));
     }
 
     // Updates the status of the reminder for the current user.
     protected function UpdateOnYes()
     {
-        $this->UpdateStatus('yes', $this->remind_delay_yes);
+        $this->UpdateStatus($this->user->id(), $this->type_id,
+                            'yes', $this->remind_delay_yes);
     }
     protected function UpdateOnNo()
     {
-        $this->UpdateStatus('no', $this->remind_delay_no);
+        $this->UpdateStatus($this->user->id(), $this->type_id,
+                            'no', $this->remind_delay_no);
     }
     protected function UpdateOnDismiss()
     {
-        $this->UpdateStatus('dismiss', $this->remind_delay_dismiss);
+        $this->UpdateStatus($this->user->id(), $this->type_id,
+                            'dismiss', $this->remind_delay_dismiss);
     }
 
     // Display and http handling helpers --------------------------------------
@@ -98,39 +101,71 @@ abstract class Reminder
     // method below).
     abstract public function HandleAction($action);
 
-    // Returns the content of the onebox reminder. Default implementation displays
-    // a text and three links (yes, no, dismiss); it uses the text from method
-    // GetDisplayText.
-    public function Display(&$page)
+    // Displays a reduced version of the reminder and notifies that the action
+    // has been taken into account.
+    public function NotifiesAction($page)
     {
-        header('Content-Type: text/html; charset=utf-8');
-        $page->changeTpl('reminder/default.tpl', NO_SKIN);
-        $page->assign('text', $this->GetDisplayText());
-        $page->assign('baseurl', $this->GetBaseUrl());
+        pl_content_headers("text/html");
+        $page->changeTpl('reminder/notification.tpl', NO_SKIN);
+        $page->assign('previous_reminder', $this->title());
     }
 
-    // Helper for returning the content as a string, instead of using the existing
-    // globale XorgPage instance.
-    public function GetDisplayAsString()
+    // Displays the reminder as a standalone html snippet. It should be used
+    // when the reminder is the only output of a page.
+    public function DisplayStandalone($page, $previous_reminder = null)
     {
-        $page = new XorgPage();
-        $this->Display($page);
-        return $page->raw();
+        pl_content_headers("text/html");
+        $page->changeTpl('reminder/base.tpl', NO_SKIN);
+        $this->Prepare($page);
+        if ($previous_reminder) {
+            $page->assign('previous_reminder', $previous_reminder);
+        }
+    }
+
+    // Prepares the display by assigning template variables.
+    public function Prepare($page)
+    {
+        $page->assign_by_ref('reminder', $this);
     }
 
-    // Returns the text to display in the onebox.
-    abstract protected function GetDisplayText();
+    // Returns the name of the inner template, or null if a simple text obtained
+    // from GetText should be printed.
+    public function template() { return null; }
+
+    // Returns the text to display in the onebox, or null if a
+    public function text() { return ''; }
+
+    // Returns the title of the onebox.
+    public function title() { return ''; }
+
+    // Should return true if this onebox needs to be considered as a warning and
+    // not just as a subscription offer.
+    public function warning() { return false; }
 
     // Returns the base url for the reminder module.
-    protected function GetBaseUrl()
+    public function baseurl()
     {
         return 'ajax/reminder/' . $this->name;
     }
 
+    // Returns the url for the information page.
+    public function info() { return ''; }
+
+    // Static status update methods -------------------------------------------
+
+    // Marks the candidate reminder as having been accepted for user |uid|.
+    // It is intended to be used when a reminder box has been bypassed, and when
+    // it should behave as if the user had clicked on 'yes'.
+    protected static function MarkCandidateAsAccepted($uid, $candidate)
+    {
+        Reminder::UpdateStatus($uid, $candidate['type_id'],
+                               'yes', $candidate['remind_delay_yes']);
+    }
+
     // Static factories -------------------------------------------------------
 
     // Returns a chosen class using the user data from |user|, and from the database.
-    public static function GetCandidateReminder(User &$user)
+    public static function GetCandidateReminder(User $user)
     {
         $res = XDB::query('SELECT  rt.*, r.status, r.remind_last
                              FROM  reminder_type AS rt
@@ -146,7 +181,7 @@ abstract class Reminder
                 $position -= $candidate['weight'];
                 if ($position <= 0) {
                     $class = self::GetClassName($candidate['name']);
-                    if ($class && call_user_func(array($class, 'IsCandidate'), $user)) {
+                    if ($class && call_user_func(array($class, 'IsCandidate'), $user, $candidate)) {
                         return new $class($user, $candidate);
                     }
                     unset($candidates[$key]);
@@ -159,7 +194,7 @@ abstract class Reminder
 
     // Returns an instantiation of the reminder class which name is |name|, using
     // user data from |user|, and from the database.
-    public static function GetByName(User &$user, $name)
+    public static function GetByName(User $user, $name)
     {
         if (!($class = self::GetClassName($name))) {
             return null;
@@ -181,12 +216,12 @@ abstract class Reminder
     // the class.
     private static function GetClassName($name)
     {
-        @include_once "reminder/$name.inc.php";
+        include_once "reminder/$name.inc.php";
         $class = 'Reminder' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name)));
         return (class_exists($class) ? $class : null);
     }
 }
 
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>