title: Moodle moodle_page::set_state : Invalid state error
body: <p>In a page in a local plugin, I get error for $OUTPUT->header(); :</p>
  
  <p>Coding error detected, it must be fixed by a programmer: Invalid state passed to moodle_page::set_state. We are in state 1 and state 1 was requested.</p>
  
  <p>I have checked on google that the cause could be a second call to $OUTPUT->header();</p>
  
  <p>But there is no second call.</p>
  
  <p>Also, what is puzzling and surprising is that there is No error in all other pages, including, similar other local plugins, error is only for this local plugin page.</p>
  
  <p>Also, I tried $OUTPUT->has_started() , it returns false.</p>
  
  <p>Also, $PAGE->state is 0 , just before call to header.</p>
  
  <p>Moodle version 2.8</p>
  
  <p>Please help and guide.</p>
  
  <p>Edit :</p>
  
  <pre><code>    require_once('../../config.php');
  
  require_login();
  
  global $CFG,$PAGE;
  
  $PAGE-&gt;set_context(context_system::instance());
  $PAGE-&gt;set_pagelayout('standard');
  $PAGE-&gt;set_title('');
  $PAGE-&gt;set_heading('');
  $PAGE-&gt;set_url($CFG-&gt;wwwroot.'/local/tpmg/pending-manual.php');
  
  if( ! $OUTPUT-&gt;has_started() )
  {
      // echo $PAGE-&gt;state;exit;
      echo $OUTPUT-&gt;header();
  }
  
  if( isloggedin() )
  {
      require_once('lib.php');
  
      $pmQuizzesObj = get_pm_quizzes();
  
      if( $pmQuizzesObj-&gt;pendingAttsCount )
      {
          $pmQuizzesArr = $pmQuizzesObj-&gt;quizzesArr; 
  
          echo get_string( 'tpmgpano','local_tpmg' ) . " : " . $pmQuizzesObj-&gt;pendingAttsCount . "&lt;br/&gt;";
  
          // Here goes table for Quizzes needing manual grading.
          $content = "&lt;table class='mot_table'&gt;";
          $content .= "&lt;th&gt;" . get_string( 'tpmgthead','local_tpmg' ) . "&lt;/th&gt;";
          foreach( $pmQuizzesArr as  $key=&gt;$currPmQuiz )
          {
              $tpmgQuizHref = $CFG-&gt;wwwroot . '/mod/quiz/report.php?id=' . $currPmQuiz-&gt;cmid . '&amp;mode=grading';
              $content .= "&lt;tr&gt;";
              $content .= "&lt;td&gt;&amp;nbsp;&lt;a href=" . $tpmgQuizHref . "&gt;" . $currPmQuiz-&gt;quizname . "&lt;/a&gt;&lt;/td&gt;";
              $content .= "&lt;/tr&gt;";
          }
          $content .= "&lt;/table&gt;";
  
          echo $content;
      }
      else
      {
          echo get_string( 'tpmgnopm','local_tpmg' );
      }
  }
  else
  {
      require_login();
  }
  
  echo $OUTPUT-&gt;footer();
  </code></pre>
  
  <p>Edit :
  I have found out the page body, and possibly page header also, are getting printed twice. Please guide on this.</p>