/**
 * is called by the cron-job to process the notifications stored into
 * watchlist_queue.
 */
function watchlist_process_notifications()
{
    $delayMin = get_config('watchlistnotification_delay');
    $comparetime = time() - $delayMin * 60;
    $sql = "SELECT usr, view, MAX(changed_on) AS time\n            FROM {watchlist_queue}\n            GROUP BY usr, view";
    $results = get_records_sql_array($sql, array());
    if (false === $results) {
        return;
    }
    foreach ($results as $viewuserdaterow) {
        if ($viewuserdaterow->time > date('Y-m-d H:i:s', $comparetime)) {
            continue;
        }
        // don't send a notification if only blockinstances are referenced
        // that were deleted (block exists but corresponding
        // block_instance doesn't)
        $sendnotification = false;
        $blockinstance_ids = get_column('watchlist_queue', 'block', 'usr', $viewuserdaterow->usr, 'view', $viewuserdaterow->view);
        if (is_array($blockinstance_ids)) {
            $blockinstance_ids = array_unique($blockinstance_ids);
        }
        $viewuserdaterow->blocktitles = array();
        // need to check if view has an owner, group or institution
        $view = get_record('view', 'id', $viewuserdaterow->view);
        if (empty($view->owner) && empty($view->group) && empty($view->institution)) {
            continue;
        }
        // ignore root pages, owner = 0, this account is not meant to produce content
        if (isset($view->owner) && empty($view->owner)) {
            continue;
        }
        foreach ($blockinstance_ids as $blockinstance_id) {
            if (empty($blockinstance_id)) {
                // if no blockinstance is given, assume that the form itself
                // was changed, e.g. the theme, or a block was removed
                $sendnotification = true;
                continue;
            }
            require_once get_config('docroot') . 'blocktype/lib.php';
            try {
                $block = new BlockInstance($blockinstance_id);
            } catch (BlockInstanceNotFoundException $exc) {
                // maybe the block was deleted
                continue;
            }
            $blocktype = $block->get('blocktype');
            $title = '';
            // try to get title rendered by plugin-class
            safe_require('blocktype', $blocktype);
            if (class_exists(generate_class_name('blocktype', $blocktype))) {
                $title = $block->get_title();
            } else {
                log_warn('class for blocktype could not be loaded: ' . $blocktype);
                $title = $block->get('title');
            }
            // if no title was given to the blockinstance, try to get one
            // from the artefact
            if (empty($title)) {
                $configdata = $block->get('configdata');
                if (array_key_exists('artefactid', $configdata)) {
                    try {
                        $artefact = $block->get_artefact_instance($configdata['artefactid']);
                        $title = $artefact->get('title');
                    } catch (Exception $exc) {
                        log_warn('couldn\'t identify title of blockinstance ' . $block->get('id') . $exc->getMessage());
                    }
                }
            }
            // still no title, maybe the default-name for the blocktype
            if (empty($title)) {
                $title = get_string('title', 'blocktype.' . $blocktype);
            }
            // no title could be retrieved, so let's tell the user at least
            // what type of block was changed
            if (empty($title)) {
                $title = '[' . $blocktype . '] (' . get_string('nonamegiven', 'activity') . ')';
            }
            $viewuserdaterow->blocktitles[] = $title;
            $sendnotification = true;
        }
        // only send notification if there is something to talk about (don't
        // send notification for example when new blockelement was aborted)
        if ($sendnotification) {
            try {
                $watchlistnotification = new ActivityTypeWatchlistnotification($viewuserdaterow, false);
                $watchlistnotification->notify_users();
            } catch (ViewNotFoundException $exc) {
                // Seems like the view has been deleted, don't do anything
            } catch (SystemException $exc) {
                // if the view that was changed doesn't have an owner
            }
        }
        delete_records('watchlist_queue', 'usr', $viewuserdaterow->usr, 'view', $viewuserdaterow->view);
    }
}