<?php

	class TodoListModel {

		public function addTodoListItem(
			$user_id , 
			$todo_name ,
			$todo_title ,
			$todo_url , 
			$is_mandatory = false ,
			$is_done = false ) {

			$ins = array();
			$ins["user_id"]=sprintf( "%d" , $user_id );
			$ins["todo_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $todo_name ));
			$ins["todo_title"]=sprintf( "'%s'" , FNTH_db_addslashes( $todo_title ));
			$ins["todo_url"]=sprintf( "'%s'" , FNTH_db_addslashes( $todo_url ) );
			$ins["is_mandatory"]=($is_mandatory) ? "true" : "false";
			$ins["is_done"]=($is_done) ? "true" : "false";

			FNTH_db_free_result(
				FNTH_db_query(
					FNTH_db_generate_insert_query( "todo_listitems",$ins)
				)
			);
		}

		public function getUserTodoListItems( $user_id , $only_mandatories = false ) {
			$query = sprintf( "select * from todo_listitems where
				user_id=%d and
				not is_done" , $user_id );
			if ($only_mandatories)
				$query.=" and is_mandatory";
			$query.=" order by todo_listitem_id";
			return FNTH_db_get_resultset( $query );
		}

		public function getUserTodoListCount( $user_id , $only_mandatories = false ) {
			$query = sprintf( "select count(*) as num from todo_listitems where
				user_id=%d and
				not is_done" , $user_id );
			if ($only_mandatories) $query.=" and is_mandatory";
			return FNTH_db_get_field( $query , "num" );
		}

		public function clearTodoListItem( $user_id , $todo_name ) {
			$query = sprintf( "update todo_listitems set is_done = true where user_id=%d and todo_name='%s'",
				$user_id , FNTH_db_addslashes( $todo_name ) );
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}
	}

?>
