<commit_before><?php

function display_course_content() {

	$content_id  = (int)params('id');
	$cursus_sn   = (string)params('cursus');
	$course_code = (string)params('course');

	$content = ContentQuery::create()->findOneById($content_id);

    if (!$content) {
        halt(NOT_FOUND);
    }

	$user    = $content->getAuthor();
	$course	 = $content->getCourse();
	$cursus	 = $content->getCursus();

    if (!$course || !$cursus) {
        halt(NOT_FOUND);
    }

    if ($course->getCode() != $course_code || $cursus->getShortName() != $cursus_sn) {
        redirect_to('/cursus/'.$cursus->getShortName().'/'.$course->getCode().'/'.$content_id);
    }

    $rights    = $content->getAccessRights();
    $user_rank = is_connected() ? user()->getRank() : 0;

    $type      = $content->getContentType();

    if ($type && $type->getRights() > $rights) {
        $rights = $type->getRights();
    }

    if ($rights > $user_rank) {
        halt(HTTP_FORBIDDEN, 'Vous n\'avez pas le droit d\'accéder à ce contenu.');
    }

    $msg_str  = null;
    $msg_type = null;

    $tpl_report = null;
	$tpl_proposed = null;

    $tpl_files = null;

    if (!$content->getValidated()) {
        if (!is_connected() || (user()->getId() != $user->getId() && !user()->isAdmin())) {
            halt(NOT_FOUND);
        }
       $msg_str  = 'Ce contenu est en attente de validation.';
       $msg_type = 'notice';

		$post_token = generate_post_token(user());
		FormData::create($post_token)->store('proposed', $content);

		$tpl_proposed = array(
			'form' => array(
				'action' => Config::$root_uri. 'admin/content/proposed',
				'post_token' => $post_token
			)
		);

    }
    else {
        $report = ReportQuery::create()->findOneByContent($content);

        if ($report && is_connected() && user()->isAdmin()) {

            $post_token = generate_post_token(user());

            FormData::create($post_token)->store('report', $report);

            $r_author = $report->getAuthor();

            $tpl_report = array(
                'author' => array(
                    'name' => $r_author->getPublicName(),
                    'href' => user_url($r_author)
                ),
                'date'   => array(
                    'text'     => Lang\date_fr($report->getDate()),
                    'datetime' => datetime_attr($report->getDate())
                ),
                'form'   => array(
                    'action'      => Config::$root_uri.'admin/reports',
                    'post_token'  => $post_token
                ),
                'explication' => $report->getText()
            );
        }
    }

    $files = FileQuery::create()
                ->filterByAccessRights(array(
                    'min' => 0,
                    'max' => $user_rank
                ))
                ->limit(10)
                ->findByContent($content);

    if ($files) {
        $tpl_files = array();

        foreach ($files as $f) {
            $tpl_files []= tpl_file($f);
        }
    }

    $type_name = $type ? $type->getName() : null;

    $tpl_content = array(
        'title'  => $content->getTitle(),
        'text'   => format_text($content->getText()),
        'date'   => array(
            'text'     => Lang\date_fr($content->getDate()),
            'datetime' => datetime_attr($content->getDate())
        ),
        'files'  => $tpl_files,
        'author' => array(
            'name' => $user->getPublicName(),
            'href' => Config::$root_uri.'p/'.$user->getUsername()
        ),
        'type'   => $type_name
    );

	return tpl_render('contents/base.html', array(
        'page' => Array(
            'title' => $content->getTitle(),
            'keywords' => array( $cursus->getName(), $course->getName(), $course->getCode() ),
            'description' => '',

            'breadcrumbs' => array(
                1 => array(
                    'href'  => cursus_url($cursus),
                    'title' => $cursus->getName()
                ),
                2 => array(
                    'href'  => course_url($cursus,$course),
                    'title' => $course->getCode()
                ),
                3 => array(
                    'href'  => url(),
                    'title' => $content->getTitle()
                )
            ),

				'proposed' => $tpl_proposed,
            'report'   => $tpl_report,
    	      'content'  => $tpl_content,

            'message'      => $msg_str,
            'message_type' => $msg_type
        )
    ));

}

function display_member_proposing_content_form() {

    $cursus_name = params('cursus');
    $course_name = params('course');

    $cursus = CursusQuery::create()->findOneByShortName($cursus_name);

    if (!$cursus) {
        halt(NOT_FOUND);
    }

    $course = CourseQuery::create()
                            ->filterByCursus($cursus)
                            ->findOneByCode($course_name);

    if (!$course) {
        halt(NOT_FOUND);
    }

    $url = course_url($cursus, $course).'/proposer';

    $tpl_content_types = array(
        array( 'value' => 0, 'name' => '' )
    );

    $content_types = ContentTypeQuery::create()->find();

    foreach ($content_types as $t) {
        $tpl_content_types []= array(
            'value' => $t->getId(),
            'name'  => $t->getName()
        );
    }

    $token = generate_post_token(user());

    $fd = FormData::create($token)
            ->store('course', $course)
            ->store('cursus', $cursus);

    return tpl_render('contents/proposing.html', array(
        'page' => array(
            'title' => 'Proposer un contenu '.Lang\de($course->getCode()),

            'breadcrumbs' => array(
                1 => array(
                    'href'  => cursus_url($cursus),
                    'title' => $cursus->getName()
                ),
                2 => array(
                    'href'  => course_url($cursus,$course),
                    'title' => $course->getCode()
                ),
                3 => array(
                    'href'  => $url,
                    'title' => 'Proposer un contenu'
                )
            ),

            'token' => $token,

            'form' => array(
                'action' => $url.'/prévisualiser',

                'values' => array(
                    'title' => '',
                    'text' => '',
                    'type' => ''
                ),

                'max_files_nb' => 5,

                'types' => $tpl_content_types
            )
        )
    ));
}

function display_post_member_proposed_content_preview() {
    check_sent_content();

    $msg_str = '';
    $msg_type = null;

    // ** Token **

    $token = $_POST['t'];

    if (!use_token($token, 'POST')) {
        halt(HTTP_FORBIDDEN, 'Le jeton d\'authentification est invalide ou a expiré.');
    }

    $fd = FormData::create($token);

    if (!$fd->exists()) {
        halt(
            HTTP_INTERNAL_SERVER_ERROR,
              'Un problème interne est survenu avec le jeton d\'authentification'
            . ', veuillez réessayer.'
        );
    }

    // transfer of course/cursus to a new token
    $token2 = generate_post_token(user());
    $fd2 = FormData::create($token2)->store($fd->get());
    $fd->destroy();

    // ** Content type **

    if (has_post('type', true)) {
        $c_type = ContentTypeQuery::create()->findOneById(intval($_POST['type']));

        if ($c_type) {
            if ($c_type->getRights() > user()->getRank()) {
                halt(
                    HTTP_FORBIDDEN,
                      'Vous ne disposez pas des droits suffisant pour proposer '
                    . 'ce type de contenu.'
                );
            }
            $fd2->store('type', $c_type);
        }
    }

    // ** Title **

    // passing content informations via $_SESSION instead of <input type="hidden"/>
    $title = get_string('title', 'post');

    $title_exists = ContentQuery::create()
                        ->filterByCursus($fd2->get('cursus'))
                        ->filterByCourse($fd2->get('course'))
                        ->findOneByTitle($title);

    if ($title_exists) {
        halt(HTTP_BAD_REQUEST, 'Ce titre est déjà pris.');
    }

    $fd2->store('title', $title);

    // ** Files **

    if (has_file('userfiles')) {
        $desc = has_post('desc') ? $_POST['desc'] : '';

        $fd2->store('files',
                upload_user_file(user(), 'userfiles', $desc, &$msg_str, &$msg_type));
    }

    $tpl_files = array();

    foreach($fd2->get('files') as $k => $file) {
        $tpl_files []= array( 'name' => $file->getName() );
    }

    // ** Text **

    $text = has_post('text') ? format_text($_POST['text']) : '';

    if (!count($fd2->get('files')) && !$text) {
        halt(HTTP_BAD_REQUEST, 'Ce contenu est vide.');
    }

    $fd2->store('text', get_string('text', 'post'));


    return tpl_render('contents/proposing_preview.html', array(
        'page' => array(
            'title' => 'Prévisualisation de « '.$title.' »',

            'message'      => $msg_str,
            'message_type' => $msg_type,

            'breadcrumbs' => false,

            'content' => array(
                'title' => $title,
                'text'  => $text,
                'files' => $tpl_files
            ),

            'form' => array(
                'token' => $token2,
                'action' => course_url($fd2->get('cursus'), $fd2->get('course')).'/proposer'
            )
        )
    ));
}

function display_post_member_proposed_content() {
    $token = $_POST['t'];

    $fd = FormData::create($token);

    if (!use_token($token, 'POST') || !$fd->exists()) {
        halt(HTTP_FORBIDDEN, 'Le jeton d\'authentification est invalide ou a expiré.');
    }

    $course = $fd->get('course');
    $cursus = $fd->get('cursus');

    $title  = $fd->get('title');
    $text   = $fd->get('text');

    $type   = $fd->get('type');

    $files = $fd->get('files');

    $fd->destroy();

    $c = new Content();
    $c->setTitle($title);
    $c->setText($text);
    $c->setAuthor(user());
    $c->setDate(time());
    $c->setCursus($cursus);
    $c->setCourse($course);

    if ($type) {
        $c->setContentType($type);
        $c->setAccessRights($type->getRights());
    }

    foreach ($files as $k => $file) {
        $c->addFile($file);
    }

    if (!$c->validate()) {
        return 'validation error.'; //TODO
    }
    else {
        $c->save();
        redirect_to('/cursus/'.$cursus->getShortName().'/'.$course->getCode().'/'.$c->getId());
    }
}

?>
<commit_msg>script added to proposed content form (cf #155).<commit_after><?php

function display_course_content() {

	$content_id  = (int)params('id');
	$cursus_sn   = (string)params('cursus');
	$course_code = (string)params('course');

	$content = ContentQuery::create()->findOneById($content_id);

    if (!$content) {
        halt(NOT_FOUND);
    }

	$user    = $content->getAuthor();
	$course	 = $content->getCourse();
	$cursus	 = $content->getCursus();

    if (!$course || !$cursus) {
        halt(NOT_FOUND);
    }

    if ($course->getCode() != $course_code || $cursus->getShortName() != $cursus_sn) {
        redirect_to('/cursus/'.$cursus->getShortName().'/'.$course->getCode().'/'.$content_id);
    }

    $rights    = $content->getAccessRights();
    $user_rank = is_connected() ? user()->getRank() : 0;

    $type      = $content->getContentType();

    if ($type && $type->getRights() > $rights) {
        $rights = $type->getRights();
    }

    if ($rights > $user_rank) {
        halt(HTTP_FORBIDDEN, 'Vous n\'avez pas le droit d\'accéder à ce contenu.');
    }

    $msg_str  = null;
    $msg_type = null;

    $tpl_report = null;
	$tpl_proposed = null;

    $tpl_files = null;

    if (!$content->getValidated()) {
        if (!is_connected() || (user()->getId() != $user->getId() && !user()->isAdmin())) {
            halt(NOT_FOUND);
        }
       $msg_str  = 'Ce contenu est en attente de validation.';
       $msg_type = 'notice';

		$post_token = generate_post_token(user());
		FormData::create($post_token)->store('proposed', $content);

		$tpl_proposed = array(
			'form' => array(
				'action' => Config::$root_uri. 'admin/content/proposed',
				'post_token' => $post_token
			)
		);

    }
    else {
        $report = ReportQuery::create()->findOneByContent($content);

        if ($report && is_connected() && user()->isAdmin()) {

            $post_token = generate_post_token(user());

            FormData::create($post_token)->store('report', $report);

            $r_author = $report->getAuthor();

            $tpl_report = array(
                'author' => array(
                    'name' => $r_author->getPublicName(),
                    'href' => user_url($r_author)
                ),
                'date'   => array(
                    'text'     => Lang\date_fr($report->getDate()),
                    'datetime' => datetime_attr($report->getDate())
                ),
                'form'   => array(
                    'action'      => Config::$root_uri.'admin/reports',
                    'post_token'  => $post_token
                ),
                'explication' => $report->getText()
            );
        }
    }

    $files = FileQuery::create()
                ->filterByAccessRights(array(
                    'min' => 0,
                    'max' => $user_rank
                ))
                ->limit(10)
                ->findByContent($content);

    if ($files) {
        $tpl_files = array();

        foreach ($files as $f) {
            $tpl_files []= tpl_file($f);
        }
    }

    $type_name = $type ? $type->getName() : null;

    $tpl_content = array(
        'title'  => $content->getTitle(),
        'text'   => format_text($content->getText()),
        'date'   => array(
            'text'     => Lang\date_fr($content->getDate()),
            'datetime' => datetime_attr($content->getDate())
        ),
        'files'  => $tpl_files,
        'author' => array(
            'name' => $user->getPublicName(),
            'href' => Config::$root_uri.'p/'.$user->getUsername()
        ),
        'type'   => $type_name
    );

	return tpl_render('contents/base.html', array(
        'page' => Array(
            'title' => $content->getTitle(),
            'keywords' => array( $cursus->getName(), $course->getName(), $course->getCode() ),
            'description' => '',

            'breadcrumbs' => array(
                1 => array(
                    'href'  => cursus_url($cursus),
                    'title' => $cursus->getName()
                ),
                2 => array(
                    'href'  => course_url($cursus,$course),
                    'title' => $course->getCode()
                ),
                3 => array(
                    'href'  => url(),
                    'title' => $content->getTitle()
                )
            ),

				'proposed' => $tpl_proposed,
            'report'   => $tpl_report,
    	      'content'  => $tpl_content,

            'message'      => $msg_str,
            'message_type' => $msg_type
        )
    ));

}

function display_member_proposing_content_form() {

    $cursus_name = params('cursus');
    $course_name = params('course');

    $cursus = CursusQuery::create()->findOneByShortName($cursus_name);

    if (!$cursus) {
        halt(NOT_FOUND);
    }

    $course = CourseQuery::create()
                            ->filterByCursus($cursus)
                            ->findOneByCode($course_name);

    if (!$course) {
        halt(NOT_FOUND);
    }

    $url = course_url($cursus, $course).'/proposer';

    $tpl_content_types = array(
        array( 'value' => 0, 'name' => '' )
    );

    $content_types = ContentTypeQuery::create()->find();

    foreach ($content_types as $t) {
        $tpl_content_types []= array(
            'value' => $t->getId(),
            'name'  => $t->getName()
        );
    }

    $token = generate_post_token(user());

    $fd = FormData::create($token)
            ->store('course', $course)
            ->store('cursus', $cursus);

    return tpl_render('contents/proposing.html', array(
        'page' => array(
            'title' => 'Proposer un contenu '.Lang\de($course->getCode()),

            'breadcrumbs' => array(
                1 => array(
                    'href'  => cursus_url($cursus),
                    'title' => $cursus->getName()
                ),
                2 => array(
                    'href'  => course_url($cursus,$course),
                    'title' => $course->getCode()
                ),
                3 => array(
                    'href'  => $url,
                    'title' => 'Proposer un contenu'
                )
            ),

            'token' => $token,

            'form' => array(
                'action' => $url.'/prévisualiser',

                'values' => array(
                    'title' => '',
                    'text' => '',
                    'type' => ''
                ),

                'max_files_nb' => 5,

                'types' => $tpl_content_types
            ),

            'scripts' => array(
                array( 'src' => Config::$root_uri.'views/static/js/proposing-content.js' )
            )
        )
    ));
}

function display_post_member_proposed_content_preview() {
    check_sent_content();

    $msg_str = '';
    $msg_type = null;

    // ** Token **

    $token = $_POST['t'];

    if (!use_token($token, 'POST')) {
        halt(HTTP_FORBIDDEN, 'Le jeton d\'authentification est invalide ou a expiré.');
    }

    $fd = FormData::create($token);

    if (!$fd->exists()) {
        halt(
            HTTP_INTERNAL_SERVER_ERROR,
              'Un problème interne est survenu avec le jeton d\'authentification'
            . ', veuillez réessayer.'
        );
    }

    // transfer of course/cursus to a new token
    $token2 = generate_post_token(user());
    $fd2 = FormData::create($token2)->store($fd->get());
    $fd->destroy();

    // ** Content type **

    if (has_post('type', true)) {
        $c_type = ContentTypeQuery::create()->findOneById(intval($_POST['type']));

        if ($c_type) {
            if ($c_type->getRights() > user()->getRank()) {
                halt(
                    HTTP_FORBIDDEN,
                      'Vous ne disposez pas des droits suffisant pour proposer '
                    . 'ce type de contenu.'
                );
            }
            $fd2->store('type', $c_type);
        }
    }

    // ** Title **

    // passing content informations via $_SESSION instead of <input type="hidden"/>
    $title = get_string('title', 'post');

    $title_exists = ContentQuery::create()
                        ->filterByCursus($fd2->get('cursus'))
                        ->filterByCourse($fd2->get('course'))
                        ->findOneByTitle($title);

    if ($title_exists) {
        halt(HTTP_BAD_REQUEST, 'Ce titre est déjà pris.');
    }

    $fd2->store('title', $title);

    // ** Files **

    if (has_file('userfiles')) {
        $desc = has_post('desc') ? $_POST['desc'] : '';

        $fd2->store('files',
                upload_user_file(user(), 'userfiles', $desc, &$msg_str, &$msg_type));
    }

    $tpl_files = array();

    foreach($fd2->get('files') as $k => $file) {
        $tpl_files []= array( 'name' => $file->getName() );
    }

    // ** Text **

    $text = has_post('text') ? format_text($_POST['text']) : '';

    if (!count($fd2->get('files')) && !$text) {
        halt(HTTP_BAD_REQUEST, 'Ce contenu est vide.');
    }

    $fd2->store('text', get_string('text', 'post'));


    return tpl_render('contents/proposing_preview.html', array(
        'page' => array(
            'title' => 'Prévisualisation de « '.$title.' »',

            'message'      => $msg_str,
            'message_type' => $msg_type,

            'breadcrumbs' => false,

            'content' => array(
                'title' => $title,
                'text'  => $text,
                'files' => $tpl_files
            ),

            'form' => array(
                'token' => $token2,
                'action' => course_url($fd2->get('cursus'), $fd2->get('course')).'/proposer'
            )
        )
    ));
}

function display_post_member_proposed_content() {
    $token = $_POST['t'];

    $fd = FormData::create($token);

    if (!use_token($token, 'POST') || !$fd->exists()) {
        halt(HTTP_FORBIDDEN, 'Le jeton d\'authentification est invalide ou a expiré.');
    }

    $course = $fd->get('course');
    $cursus = $fd->get('cursus');

    $title  = $fd->get('title');
    $text   = $fd->get('text');

    $type   = $fd->get('type');

    $files = $fd->get('files');

    $fd->destroy();

    $c = new Content();
    $c->setTitle($title);
    $c->setText($text);
    $c->setAuthor(user());
    $c->setDate(time());
    $c->setCursus($cursus);
    $c->setCourse($course);

    if ($type) {
        $c->setContentType($type);
        $c->setAccessRights($type->getRights());
    }

    foreach ($files as $k => $file) {
        $c->addFile($file);
    }

    if (!$c->validate()) {
        return 'validation error.'; //TODO
    }
    else {
        $c->save();
        redirect_to('/cursus/'.$cursus->getShortName().'/'.$course->getCode().'/'.$c->getId());
    }
}

?>

{'fraction_non_alphanumeric': 0.1831738885762521, 'fraction_numerical': 0.0025792534233727254, 'mean_word_length': 1.7605177993527508, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 379, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2063044', 'n_tokens_mistral': 6854, 'n_tokens_neox': 6563, 'n_words': 1114}