<?php

/**
 * This php script contains all the stuff to display iAssign.
 * 
 * @author Patricia Alves Rodrigues
 * @author Leo^nidas O. Branda~o
 * @version v 1.0 2012/10/16
 * @package mod_iassign
 * @since 2010/09/27
 * @copyright iMatica (<a href="http://www.matematica.br">iMath</a>) - Computer Science Dep. of IME-USP (Brazil)
 * 
 * <b>License</b> 
 *  - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

require_once("../../config.php");
require_once("lib.php");
require_once($CFG->libdir . '/completionlib.php');
require_once($CFG->libdir . '/plagiarismlib.php');

//Parameters GET e POST (parâmetros GET e POST)
$id = optional_param('id', 0, PARAM_INT); // Course Module ID
$a = optional_param('a', 0, PARAM_INT); //  iAssign instance id (from table 'iassign')

$mood_url = new moodle_url('/mod/iassign/view.php');

if ($id) {
  // ./lib/datalib.php : function get_coursemodule_from_id($modulename, $cmid, $courseid=0, $sectionnum=false, $strictness=IGNORE_MISSING): returns 'course_modules.*' and 'modules.name'
  $cm = get_coursemodule_from_id('iassign', $id);
  if (!$cm) { // Moodle function 'get_coursemodule_from_id(...)' returns the object from table '*_iassign_statement'
    print_error('invalidcoursemodule');
    }

  $iassign = $DB->get_record("iassign", array("id" => $cm->instance));
  if (!$iassign) { // 'course_modules.instance = iassign.id'
    print_error('invalidid', 'iassign');
    }

  $course = $DB->get_record("course", array("id" => $iassign->course));
  if (!$course) {
    print_error('coursemisconf', 'iassign');
    }
  $mood_url->param('id', $id);
  }
else {
  $iassign = $DB->get_record("iassign", array("id" => $a));
  if (!$iassign) {
    print_error('invalidid', 'iassign');
    }
  $course = $DB->get_record("course", array("id" => $iassign->course));
  if (!$course) {
    print_error('coursemisconf', 'iassign');
    }
  $cm = get_coursemodule_from_instance("iassign", $iassign->id, $course->id);
  if (!$cm) {
    print_error('invalidcoursemodule');
    }
  $mood_url->param('a', $a);
  }

$PAGE->set_url($mood_url);

require_login($course, true, $cm);

$PAGE->set_title(format_string($iassign->name));
$PAGE->set_heading($course->fullname);

// Mark viewed by user (if required)
$completion = new completion_info($course);
$completion->set_module_viewed($cm);

// About each object
// - $iassing :: object from table '*_iassign_statement'
// - $cm      :: object from table '*_course_modules'
// - $course  :: object from table '*_course_modules_completion'

$write_solution = 1;
//$iassigninstance = new iassign($iassign, $cm, $course, array('write_solution' => 1)); // locallib.php : class iassign : function __construct ($iassign, $cm, $course)
$iassigninstance = new iassign($iassign, $cm, $course); // locallib.php : class iassign : function __construct ($iassign, $cm, $course)

$iassigninstance->view(); // ./mod/iassign/locallib.php : actually who display the iAssign whose id is '$id'! (this function ignores parameters)
