function validation($data, $files)
 {
     global $COURSE, $CFG;
     $errors = parent::validation($data, $files);
     if (isset($data['limitgroup']['maxpostsblock']) && !preg_match('/^[0-9]{1,9}$/', $data['limitgroup']['maxpostsblock'])) {
         $errors['limitgroup'] = get_string('err_numeric', 'form');
     }
     if (!empty($data['reportingemail']) && !validate_email($data['reportingemail'])) {
         $errors['reportingemail'] = get_string('invalidemail', 'forumng');
     }
     // If old discussions are set to be moved to another forum...
     $targetforumid = isset($data['removeto']) ? $data['removeto'] : 0;
     $removeafter = isset($data['removeafter']) ? $data['removeafter'] : 0;
     if ($removeafter && $targetforumid) {
         $modinfo = get_fast_modinfo($COURSE);
         // Look for target forum
         if (!array_key_exists($targetforumid, $modinfo->instances['forumng'])) {
             $errors['removeto'] = get_string('errorinvalidforum', 'forumng');
         }
     }
     // If sharing is turned on, check requirements
     if (!empty($data['shared'])) {
         if (!empty($data['groupmode'])) {
             $errors['groupmode'] = get_string('error_notwhensharing', 'forumng');
         }
         if (!empty($data['grading'])) {
             $errors['grading'] = get_string('error_notwhensharing', 'forumng');
         }
         if (empty($data['cmidnumber'])) {
             $errors['cmidnumber'] = get_string('error_sharingrequiresidnumber', 'forumng');
         } else {
             // Check it's unique
             $cmid = isset($data['coursemodule']) ? (int) $data['coursemodule'] : 0;
             if (count_records_select('course_modules', "idnumber = '" . addslashes($data['cmidnumber']) . "' AND id <> " . $cmid)) {
                 $errors['cmidnumber'] = get_string('error_sharingrequiresidnumber', 'forumng');
             }
         }
     } else {
         if (isset($data['shared'])) {
             // They are trying to turn sharing off. You aren't allowed to do
             // this if there are existing references.
             $cmid = isset($data['coursemodule']) ? (int) $data['coursemodule'] : -1;
             if (count_records('forumng', 'originalcmid', $cmid)) {
                 $errors['shared'] = get_string('error_sharinginuse', 'forumng');
             }
         }
     }
     if (!empty($data['usesharedgroup']['useshared'])) {
         if (empty($data['usesharedgroup']['originalcmidnumber'])) {
             $errors['usesharedgroup'] = get_string('error_sharingidnumbernotfound', 'forumng');
         } else {
             // Check we can find it
             if (!forum::get_shared_cm_from_idnumber($data['usesharedgroup']['originalcmidnumber'])) {
                 $errors['usesharedgroup'] = get_string('error_sharingidnumbernotfound', 'forumng');
             }
         }
     }
     return $errors;
 }
function forumng_decode_content_links_caller($restore)
{
    // Get all the items that might have links in, from the relevant new course
    try {
        global $CFG, $db;
        // 1. Intros
        if ($intros = get_records_select('forumng', 'course=' . $restore->course_id . ' AND intro IS NOT NULL', '', 'id, intro, name')) {
            foreach ($intros as $intro) {
                $newintro = $intro->intro;
                // Special behaviour hidden in intro
                $matches = array();
                if (preg_match('~%%CMIDNUMBER:([^%]+)%%$~', $newintro, $matches)) {
                    $newintro = substr($newintro, 0, -strlen($matches[0]));
                    $idnumber = $matches[1];
                    $cm = forum::get_shared_cm_from_idnumber($idnumber);
                    if ($cm) {
                        set_field('forumng', 'originalcmid', $cm->id, 'id', $intro->id);
                    } else {
                        // The original forum cannot be found, so restore
                        // this as not shared
                        if (!defined('RESTORE_SILENTLY')) {
                            $a = (object) array('name' => s($intro->name), 'idnumber' => s($idnumber));
                            print '<br />' . get_string('error_nosharedforum', 'forumng', $a) . '<br />';
                        }
                    }
                }
                if (preg_match('~%%REMOVETHIS%%$~', $newintro)) {
                    $newintro = substr($newintro, 0, -14);
                }
                $newintro = restore_decode_content_links_worker($newintro, $restore);
                if ($newintro != $intro->intro) {
                    if (!set_field('forumng', 'intro', addslashes($newintro), 'id', $intro->id)) {
                        throw new forum_exception("Failed to set intro for forum {$intro->id}: " . $db->ErrorMsg());
                    }
                }
            }
        }
        // 2. Post content
        $rs = get_recordset_sql("\nSELECT\n    fp.id, fp.message, fp.format\nFROM\n    {$CFG->prefix}forumng f\n    INNER JOIN {$CFG->prefix}forumng_discussions d ON d.forumid = f.id\n    INNER JOIN {$CFG->prefix}forumng_posts fp ON fp.discussionid = d.id\nWHERE\n    f.course={$restore->course_id}\n");
        if (!$rs) {
            throw new forum_exception("Failed to query for forum data: " . $db->ErrorMsg());
        }
        while ($rec = rs_fetch_next_record($rs)) {
            $newcontent = restore_decode_content_links_worker($rec->message, $restore);
            if ($newcontent != $rec->message) {
                if (!set_field('forumng_posts', 'message', addslashes($newcontent), 'id', $rec->id)) {
                    throw new forum_exception("Failed to update content {$ec->id}: " . $db->ErrorMsg());
                }
            }
        }
        rs_close($rs);
        // 3. Update search data (note this is not actually do with content
        //    links, but it has to be done here because we need a course-module
        //    id.
        if (forum::search_installed()) {
            forumng_ousearch_update_all(false, $restore->course_id);
        }
        return true;
    } catch (Exception $e) {
        forum_utils::handle_backup_exception($e, 'restore');
        return false;
    }
}